From 2b887894e6e2caade7b4ebe8a48f2c8a02e94391 Mon Sep 17 00:00:00 2001 From: "tinbox.wu" Date: Thu, 22 Feb 2024 14:16:57 +0800 Subject: [PATCH 1/2] add pow function --- builtin/builtin.go | 32 ++++++++++++++++++++++++++++++++ builtin/builtin_test.go | 1 + 2 files changed, 33 insertions(+) diff --git a/builtin/builtin.go b/builtin/builtin.go index 4aad6aa9..11da0819 100644 --- a/builtin/builtin.go +++ b/builtin/builtin.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "math" "reflect" "sort" "strings" @@ -209,6 +210,37 @@ var Builtins = []*Function{ return anyType, fmt.Errorf("invalid argument for float (type %s)", args[0]) }, }, + { + Name: "pow", + Func: func(args ...any) (any, error) { + x := Float(args[0]) + y := Float(args[1]) + res := math.Pow(x.(float64), y.(float64)) + return any(res), nil + }, + //Types: types(math.Pow), + Validate: func(args []reflect.Type) (reflect.Type, error) { + if len(args) != 2 { + return anyType, fmt.Errorf("invalid number of arguments (expected 2, got %d)", len(args)) + } + var ret reflect.Type + var err error + for _, arg := range args[0:1] { + switch kind(arg) { + case reflect.Interface: + ret = floatType + case reflect.Float32, reflect.Float64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + ret = floatType + case reflect.String: + ret = floatType + default: + return anyType, fmt.Errorf("invalid argument for float (type %s)", args) + } + } + + return ret, err + }, + }, { Name: "string", Fast: String, diff --git a/builtin/builtin_test.go b/builtin/builtin_test.go index 3a285007..f5a66008 100644 --- a/builtin/builtin_test.go +++ b/builtin/builtin_test.go @@ -51,6 +51,7 @@ func TestBuiltin(t *testing.T) { {`float(5)`, 5.0}, {`float(5.5)`, 5.5}, {`float("5.5")`, 5.5}, + {`pow(2,2)`, 4.0}, {`string(5)`, "5"}, {`string(5.5)`, "5.5"}, {`string("5.5")`, "5.5"}, From b0187b936d80d8cca4897e48dffa87896a994073 Mon Sep 17 00:00:00 2001 From: "tinbox.wu" Date: Fri, 23 Feb 2024 16:30:15 +0800 Subject: [PATCH 2/2] add switch builtin function --- builtin/builtin.go | 29 ++++++++++++++++++++++++++++- builtin/builtin_test.go | 1 + 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/builtin/builtin.go b/builtin/builtin.go index 11da0819..3ea281a1 100644 --- a/builtin/builtin.go +++ b/builtin/builtin.go @@ -210,6 +210,33 @@ var Builtins = []*Function{ return anyType, fmt.Errorf("invalid argument for float (type %s)", args[0]) }, }, + { + // switch(bool(expr1), 1, bool(expr2), 2, ...) + Name: "switch", + Func: func(args ...any) (any, error) { + for i := 0; i < len(args); i += 2 { + if args[i].(bool) { + return args[i+1], nil + } + } + return nil, nil + }, + Validate: func(args []reflect.Type) (reflect.Type, error) { + if len(args)%2 != 0 { + return anyType, fmt.Errorf("not enough arguments to call switch(expected a multiple of 2, got %d)", len(args)) + } + + for i := 0; i < len(args); i += 2 { + arg := args[i] + switch kind(arg) { + case reflect.Bool: + default: + return anyType, fmt.Errorf("invalid argument for bool (type %s)", arg) + } + } + return anyType, nil + }, + }, { Name: "pow", Func: func(args ...any) (any, error) { @@ -234,7 +261,7 @@ var Builtins = []*Function{ case reflect.String: ret = floatType default: - return anyType, fmt.Errorf("invalid argument for float (type %s)", args) + return anyType, fmt.Errorf("invalid argument for float (type %s)", arg) } } diff --git a/builtin/builtin_test.go b/builtin/builtin_test.go index f5a66008..73b01f5c 100644 --- a/builtin/builtin_test.go +++ b/builtin/builtin_test.go @@ -52,6 +52,7 @@ func TestBuiltin(t *testing.T) { {`float(5.5)`, 5.5}, {`float("5.5")`, 5.5}, {`pow(2,2)`, 4.0}, + {`switch(ArrayOfInt?.[0]==1,2, ArrayOfInt?.[2]==33, 3, true, 0)`, 2}, {`string(5)`, "5"}, {`string(5.5)`, "5.5"}, {`string("5.5")`, "5.5"},