From e8eb6b60f529b538f9acc861e2cd755d63b71163 Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 7 Jun 2024 13:29:46 +0500 Subject: [PATCH] feat: restructuring. --- httpx/module.go | 2 -- tests/dogs.xgo | 8 ++++++++ tests/import.xgo | 6 ++++++ {cjson => xmodules/cjson}/dec.go | 0 {cjson => xmodules/cjson}/main.go | 0 {httpx => xmodules/httpx}/http.go | 0 xmodules/httpx/module.go | 7 +++++++ {httpx => xmodules/httpx}/request.go | 0 {httpx => xmodules/httpx}/url.go | 0 {logx => xmodules/logx}/main.go | 0 xmodules/modules.go | 18 ++++++++++++++++++ {paths => xmodules/paths}/main.go | 0 xtool/main.go | 21 ++++++++++++--------- 13 files changed, 51 insertions(+), 11 deletions(-) delete mode 100644 httpx/module.go create mode 100644 tests/dogs.xgo create mode 100644 tests/import.xgo rename {cjson => xmodules/cjson}/dec.go (100%) rename {cjson => xmodules/cjson}/main.go (100%) rename {httpx => xmodules/httpx}/http.go (100%) create mode 100644 xmodules/httpx/module.go rename {httpx => xmodules/httpx}/request.go (100%) rename {httpx => xmodules/httpx}/url.go (100%) rename {logx => xmodules/logx}/main.go (100%) create mode 100644 xmodules/modules.go rename {paths => xmodules/paths}/main.go (100%) diff --git a/httpx/module.go b/httpx/module.go deleted file mode 100644 index 0e441e6..0000000 --- a/httpx/module.go +++ /dev/null @@ -1,2 +0,0 @@ -package httpx - diff --git a/tests/dogs.xgo b/tests/dogs.xgo new file mode 100644 index 0000000..3105e67 --- /dev/null +++ b/tests/dogs.xgo @@ -0,0 +1,8 @@ + +export { + "new": func(name) { + return { + name: name + } + } +} diff --git a/tests/import.xgo b/tests/import.xgo new file mode 100644 index 0000000..3106eee --- /dev/null +++ b/tests/import.xgo @@ -0,0 +1,6 @@ +fmt := import("fmt") +dogs := import("./tests/dogs") + +dog := dogs.new("check") +fmt.println(dog) + diff --git a/cjson/dec.go b/xmodules/cjson/dec.go similarity index 100% rename from cjson/dec.go rename to xmodules/cjson/dec.go diff --git a/cjson/main.go b/xmodules/cjson/main.go similarity index 100% rename from cjson/main.go rename to xmodules/cjson/main.go diff --git a/httpx/http.go b/xmodules/httpx/http.go similarity index 100% rename from httpx/http.go rename to xmodules/httpx/http.go diff --git a/xmodules/httpx/module.go b/xmodules/httpx/module.go new file mode 100644 index 0000000..04534c2 --- /dev/null +++ b/xmodules/httpx/module.go @@ -0,0 +1,7 @@ +package httpx + +import "github.com/d5/tengo/v2" + +var Module = map[string] tengo.Object { +} + diff --git a/httpx/request.go b/xmodules/httpx/request.go similarity index 100% rename from httpx/request.go rename to xmodules/httpx/request.go diff --git a/httpx/url.go b/xmodules/httpx/url.go similarity index 100% rename from httpx/url.go rename to xmodules/httpx/url.go diff --git a/logx/main.go b/xmodules/logx/main.go similarity index 100% rename from logx/main.go rename to xmodules/logx/main.go diff --git a/xmodules/modules.go b/xmodules/modules.go new file mode 100644 index 0000000..00a6e07 --- /dev/null +++ b/xmodules/modules.go @@ -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 +}() diff --git a/paths/main.go b/xmodules/paths/main.go similarity index 100% rename from paths/main.go rename to xmodules/paths/main.go diff --git a/xtool/main.go b/xtool/main.go index 62efbc8..2f97a89 100644 --- a/xtool/main.go +++ b/xtool/main.go @@ -14,14 +14,11 @@ import ( "github.com/d5/tengo/v2" "github.com/d5/tengo/v2/parser" - "github.com/d5/tengo/v2/stdlib" ) import "surdeus.su/core/cli/mtool" -import "surdeus.su/core/xgo/cjson" -import "surdeus.su/core/xgo/logx" -import "surdeus.su/core/xgo/paths" +import "surdeus.su/core/xgo/xmodules" const ( sourceFileExt = ".xgo" @@ -54,10 +51,8 @@ func Run(flags *mtool.Flags) { fmt.Println(version) return } - modules := stdlib.GetModuleMap(stdlib.AllModuleNames()...) - modules.AddBuiltinModule("cjson", cjson.Module) - modules.AddBuiltinModule("log", logx.Module) - modules.AddBuiltinModule("paths", paths.Module) + + modules := xmodules.Modules if len(iargs) == 0 { // REPL RunREPL(modules, os.Stdin, os.Stdout) @@ -216,7 +211,14 @@ func RunREPL(modules *tengo.ModuleMap, in io.Reader, out io.Writer) { } 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 { _, _ = fmt.Fprintln(out, err.Error()) continue @@ -247,6 +249,7 @@ func compileSrc( } c := tengo.NewCompiler(srcFile, nil, nil, modules, nil) + c.SetImportFileExt(sourceFileExt) c.EnableFileImport(true) if resolvePath { c.SetImportDir(filepath.Dir(inputFile))