feat: restructuring.

This commit is contained in:
Andrey Parhomenko 2024-06-07 13:29:46 +05:00
parent c9eb64783f
commit e8eb6b60f5
13 changed files with 51 additions and 11 deletions

View file

@ -1,2 +0,0 @@
package httpx

8
tests/dogs.xgo Normal file
View file

@ -0,0 +1,8 @@
export {
"new": func(name) {
return {
name: name
}
}
}

6
tests/import.xgo Normal file
View file

@ -0,0 +1,6 @@
fmt := import("fmt")
dogs := import("./tests/dogs")
dog := dogs.new("check")
fmt.println(dog)

7
xmodules/httpx/module.go Normal file
View file

@ -0,0 +1,7 @@
package httpx
import "github.com/d5/tengo/v2"
var Module = map[string] tengo.Object {
}

18
xmodules/modules.go Normal file
View file

@ -0,0 +1,18 @@
package xmodules
import "github.com/d5/tengo/v2/stdlib"
import "surdeus.su/core/xgo/xmodules/cjson"
import "surdeus.su/core/xgo/xmodules/logx"
import "surdeus.su/core/xgo/xmodules/paths"
import "surdeus.su/core/xgo/xmodules/httpx"
import "github.com/d5/tengo/v2"
// The main map to import all the implemented modules.
var Modules = func() *tengo.ModuleMap {
ret := stdlib.GetModuleMap(stdlib.AllModuleNames()...)
ret.AddBuiltinModule("cjson", cjson.Module)
ret.AddBuiltinModule("log", logx.Module)
ret.AddBuiltinModule("paths", paths.Module)
ret.AddBuiltinModule("http", httpx.Module)
return ret
}()

View file

@ -14,14 +14,11 @@ import (
"github.com/d5/tengo/v2" "github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/parser" "github.com/d5/tengo/v2/parser"
"github.com/d5/tengo/v2/stdlib"
) )
import "surdeus.su/core/cli/mtool" import "surdeus.su/core/cli/mtool"
import "surdeus.su/core/xgo/cjson" import "surdeus.su/core/xgo/xmodules"
import "surdeus.su/core/xgo/logx"
import "surdeus.su/core/xgo/paths"
const ( const (
sourceFileExt = ".xgo" sourceFileExt = ".xgo"
@ -54,10 +51,8 @@ func Run(flags *mtool.Flags) {
fmt.Println(version) fmt.Println(version)
return return
} }
modules := stdlib.GetModuleMap(stdlib.AllModuleNames()...)
modules.AddBuiltinModule("cjson", cjson.Module) modules := xmodules.Modules
modules.AddBuiltinModule("log", logx.Module)
modules.AddBuiltinModule("paths", paths.Module)
if len(iargs) == 0 { if len(iargs) == 0 {
// REPL // REPL
RunREPL(modules, os.Stdin, os.Stdout) RunREPL(modules, os.Stdin, os.Stdout)
@ -216,7 +211,14 @@ func RunREPL(modules *tengo.ModuleMap, in io.Reader, out io.Writer) {
} }
file = addPrints(file) file = addPrints(file)
c := tengo.NewCompiler(srcFile, symbolTable, constants, modules, nil) c := tengo.NewCompiler(
srcFile,
symbolTable,
constants,
modules,
nil,
)
c.SetImportFileExt(sourceFileExt)
if err := c.Compile(file); err != nil { if err := c.Compile(file); err != nil {
_, _ = fmt.Fprintln(out, err.Error()) _, _ = fmt.Fprintln(out, err.Error())
continue continue
@ -247,6 +249,7 @@ func compileSrc(
} }
c := tengo.NewCompiler(srcFile, nil, nil, modules, nil) c := tengo.NewCompiler(srcFile, nil, nil, modules, nil)
c.SetImportFileExt(sourceFileExt)
c.EnableFileImport(true) c.EnableFileImport(true)
if resolvePath { if resolvePath {
c.SetImportDir(filepath.Dir(inputFile)) c.SetImportDir(filepath.Dir(inputFile))