feat: implemented basic log package.

This commit is contained in:
Andrey Parhomenko 2024-06-06 17:00:39 +05:00
parent 696fe8843d
commit 4df97fc21d
3 changed files with 73 additions and 2 deletions

65
logx/main.go Normal file
View file

@ -0,0 +1,65 @@
package logx
import "github.com/d5/tengo/v2"
import "log"
var Module = map[string]tengo.Object{
"printf": &tengo.BuiltinFunction{
Name: "printf",
Value: func(
args ...tengo.Object,
) (tengo.Object, error) {
if len(args) < 1 {
return nil, tengo.ErrWrongNumArguments
}
formatObject := args[0]
format, hasFormat := tengo.ToString(
formatObject,
)
if !hasFormat {
return nil, tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "string",
Found: formatObject.TypeName(),
}
}
v := make([]any, len(args[1:]))
for i, o := range args[1:] {
v[i] = tengo.ToInterface(o)
}
log.Printf(format, v...)
return nil, nil
},
},
"print": &tengo.BuiltinFunction{
Name: "print",
Value: func(
args ...tengo.Object,
) (tengo.Object, error){
v := make([]any, len(args))
for i, o := range args {
v[i] = tengo.ToInterface(o)
}
log.Print(v...)
return nil, nil
},
},
"println": &tengo.BuiltinFunction{
Name: "println",
Value: func(
args ...tengo.Object,
) (tengo.Object, error){
v := make([]any, len(args))
for i, o := range args {
v[i] = tengo.ToInterface(o)
}
log.Println(v...)
return nil, nil
},
},
}

View file

@ -1,9 +1,12 @@
cjson := import("cjson")
fmt := import("fmt")
fmt.println(cjson.__check())
log := import("log")
dec := cjson.new_decoder("tests/data.cjson")
log.print("cock")
log.print("die")
log.println("some log")
for {
v := dec.decode()
if is_error(v) {
@ -12,5 +15,6 @@ for {
if !v {
break
}
log.println("got one more value")
fmt.println(v)
}

View file

@ -20,6 +20,7 @@ import (
import "surdeus.su/core/cli/mtool"
import "surdeus.su/core/xgo/cjson"
import "surdeus.su/core/xgo/logx"
const (
sourceFileExt = ".xgo"
@ -54,6 +55,7 @@ func Run(flags *mtool.Flags) {
}
modules := stdlib.GetModuleMap(stdlib.AllModuleNames()...)
modules.AddBuiltinModule("cjson", cjson.Module)
modules.AddBuiltinModule("log", logx.Module)
if len(iargs) == 0 {
// REPL
RunREPL(modules, os.Stdin, os.Stdout)