From 15a6039b46b86aa7c9c731cad47cb85cf2b7fbf7 Mon Sep 17 00:00:00 2001 From: surdeus Date: Tue, 18 Jun 2024 21:33:03 +0500 Subject: [PATCH] feat: added extended enum (enumx) module. --- tests/array.tengo | 22 ++++++++++++++ xmodules/enumx/main.go | 69 ++++++++++++++++++++++++++++++++++++++++++ xmodules/logx/main.go | 22 +++++++++++--- xmodules/modules.go | 2 ++ xtool/main.go | 6 ++-- 5 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 tests/array.tengo create mode 100644 xmodules/enumx/main.go diff --git a/tests/array.tengo b/tests/array.tengo new file mode 100644 index 0000000..bac2c2e --- /dev/null +++ b/tests/array.tengo @@ -0,0 +1,22 @@ +enum := import("enumx") +fmt := import("log") + +object := { + key1: "cock", + key2: "dick", + key3: "check", + key4: "die", + key5: "wasd" +} +keys := enum.keys(object) +values := enum.values(object) + +sorted_values := enum.sort(values, func(a, b){ + return b < a +}) + +ia := [5, 100, 531, 7537, 1, 2, -100, 1.535] +fmt.println(enum.sort(ia)) +fmt.println(sorted_values) + + diff --git a/xmodules/enumx/main.go b/xmodules/enumx/main.go new file mode 100644 index 0000000..fd10348 --- /dev/null +++ b/xmodules/enumx/main.go @@ -0,0 +1,69 @@ +package enumx + +import "github.com/d5/tengo/v2" + +const src = ` + +_swap := func(arr, left, right) { + if arr == undefined || len(arr) <= 1 || left < 0 || left >= len(arr) || right < 0 || right >= len(arr) || left >= right { + return + } + temp := arr[right] + arr[right] = arr[left] + arr[left] = temp +} + +_sort := func(arr, left, right, less) { + if arr == undefined || len(arr) <= 1 || left < 0 || left >= len(arr) || right < 0 || right >= len(arr) || left >= right { + return arr + } + idx := left + for i := left; i < right; i++ { + if less(arr[i], arr[right]) { + _swap(arr, idx, i) + idx++ + } + } + _swap(arr, idx, right) + _sort(arr, left, idx-1, less) + _sort(arr, idx+1, right, less) + return arr +} + +export { + keys: func(x){ + ret := [] + for k, _ in x { + ret += [k] + } + return ret + }, + values: func(x){ + ret := [] + for _, v in x { + ret += [v] + } + return ret + }, + sort: func(arr, ...less) { + if !less { + less = func(a, b){ + return a < b + } + } else { + less = less[0] + } + if arr == undefined || len(arr) <= 1 { + return arr + } + return _sort(arr, 0, len(arr)-1, less) + } +} + +` + +func GetModule() tengo.Importable { + return &tengo.SourceModule{ + Src: []byte(src), + } +} diff --git a/xmodules/logx/main.go b/xmodules/logx/main.go index 0aac329..a0a06f4 100644 --- a/xmodules/logx/main.go +++ b/xmodules/logx/main.go @@ -25,12 +25,16 @@ var Module = map[string]tengo.Object{ } } - v := make([]any, len(args[1:])) + /*v := make([]any, len(args[1:])) for i, o := range args[1:] { v[i] = tengo.ToInterface(o) - } + }*/ - log.Printf(format, v...) + str, err := tengo.Format(format, args...) + if err != nil { + return nil, err + } + log.Print(str) return nil, nil }, @@ -42,7 +46,11 @@ var Module = map[string]tengo.Object{ ) (tengo.Object, error){ v := make([]any, len(args)) for i, o := range args { - v[i] = tengo.ToInterface(o) + var ok bool + v[i], ok = tengo.ToString(o) + if !ok { + v[i] = tengo.Undefined{} + } } log.Print(v...) return nil, nil @@ -55,7 +63,11 @@ var Module = map[string]tengo.Object{ ) (tengo.Object, error){ v := make([]any, len(args)) for i, o := range args { - v[i] = tengo.ToInterface(o) + var ok bool + v[i], ok = tengo.ToString(o) + if !ok { + v[i] = tengo.Undefined{} + } } log.Println(v...) return nil, nil diff --git a/xmodules/modules.go b/xmodules/modules.go index 640379e..3a7b230 100644 --- a/xmodules/modules.go +++ b/xmodules/modules.go @@ -7,6 +7,7 @@ import "surdeus.su/core/xgo/xmodules/paths" import "surdeus.su/core/xgo/xmodules/httpx" import "surdeus.su/core/xgo/xmodules/htmlx" import "surdeus.su/core/xgo/xmodules/objects" +import "surdeus.su/core/xgo/xmodules/enumx" import "github.com/d5/tengo/v2" // The main map to import all the implemented modules. @@ -18,5 +19,6 @@ func GetModules() *tengo.ModuleMap { ret.AddBuiltinModule("http", httpx.Module) ret.AddBuiltinModule("html", htmlx.Module) ret.Add("objects", objects.GetModule()) + ret.Add("enumx", enumx.GetModule()) return ret } diff --git a/xtool/main.go b/xtool/main.go index 494eb09..2e7b6cb 100644 --- a/xtool/main.go +++ b/xtool/main.go @@ -22,6 +22,7 @@ import "surdeus.su/core/xgo/xmodules" const ( sourceFileExt = ".xgo" + sourceFileExtMain = ".tengo" replPrompt = ">> " ) @@ -84,7 +85,8 @@ func Run(flags *mtool.Flags) { _, _ = fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } - } else if filepath.Ext(inputFile) == sourceFileExt { + } else if ext := filepath.Ext(inputFile) + ext == sourceFileExt || ext == sourceFileExtMain { err := CompileAndRun(modules, inputData, inputFile) if err != nil { _, _ = fmt.Fprintln(os.Stderr, err.Error()) @@ -218,7 +220,7 @@ func RunREPL(modules *tengo.ModuleMap, in io.Reader, out io.Writer) { modules, nil, ) - c.SetImportFileExt(sourceFileExt) + c.SetImportFileExt(sourceFileExt, sourceFileExtMain) if err := c.Compile(file); err != nil { _, _ = fmt.Fprintln(out, err.Error()) continue