feat: added extended enum (enumx) module.

This commit is contained in:
Andrey Parhomenko 2024-06-18 21:33:03 +05:00
parent c8df427cca
commit 15a6039b46
5 changed files with 114 additions and 7 deletions

22
tests/array.tengo Normal file
View file

@ -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)

69
xmodules/enumx/main.go Normal file
View file

@ -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),
}
}

View file

@ -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

View file

@ -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
}

View file

@ -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