diff --git a/bind-go-functions.go b/bind-go-functions.go index 0264d80..46cf8d0 100644 --- a/bind-go-functions.go +++ b/bind-go-functions.go @@ -10,7 +10,7 @@ type golangFunctor struct { f FuncTemplate } -func NewGolangFunctor2(f FuncTemplate) *golangFunctor { +func NewGolangFunctor(f FuncTemplate) *golangFunctor { return &golangFunctor{f: f} } diff --git a/builtin-base.go b/builtin-base.go index aa73807..05b5b39 100644 --- a/builtin-base.go +++ b/builtin-base.go @@ -193,21 +193,21 @@ func ImportBuiltinsFuncs(ctx ExprContext) { NewFuncParam(ParamValue), } - ctx.RegisterFunc("isNil", NewGolangFunctor2(isNilFunc), TypeBoolean, anyParams) - ctx.RegisterFunc("isInt", NewGolangFunctor2(isIntFunc), TypeBoolean, anyParams) - ctx.RegisterFunc("isFloat", NewGolangFunctor2(isFloatFunc), TypeBoolean, anyParams) - ctx.RegisterFunc("isBool", NewGolangFunctor2(isBoolFunc), TypeBoolean, anyParams) - ctx.RegisterFunc("isString", NewGolangFunctor2(isStringFunc), TypeBoolean, anyParams) - ctx.RegisterFunc("isFract", NewGolangFunctor2(isFractionFunc), TypeBoolean, anyParams) - ctx.RegisterFunc("isRational", NewGolangFunctor2(isRationalFunc), TypeBoolean, anyParams) - ctx.RegisterFunc("isList", NewGolangFunctor2(isListFunc), TypeBoolean, anyParams) - ctx.RegisterFunc("isDict", NewGolangFunctor2(isDictionaryFunc), TypeBoolean, anyParams) + ctx.RegisterFunc("isNil", NewGolangFunctor(isNilFunc), TypeBoolean, anyParams) + ctx.RegisterFunc("isInt", NewGolangFunctor(isIntFunc), TypeBoolean, anyParams) + ctx.RegisterFunc("isFloat", NewGolangFunctor(isFloatFunc), TypeBoolean, anyParams) + ctx.RegisterFunc("isBool", NewGolangFunctor(isBoolFunc), TypeBoolean, anyParams) + ctx.RegisterFunc("isString", NewGolangFunctor(isStringFunc), TypeBoolean, anyParams) + ctx.RegisterFunc("isFract", NewGolangFunctor(isFractionFunc), TypeBoolean, anyParams) + ctx.RegisterFunc("isRational", NewGolangFunctor(isRationalFunc), TypeBoolean, anyParams) + ctx.RegisterFunc("isList", NewGolangFunctor(isListFunc), TypeBoolean, anyParams) + ctx.RegisterFunc("isDict", NewGolangFunctor(isDictionaryFunc), TypeBoolean, anyParams) - ctx.RegisterFunc("bool", NewGolangFunctor2(boolFunc), TypeBoolean, anyParams) - ctx.RegisterFunc("int", NewGolangFunctor2(intFunc), TypeInt, anyParams) - ctx.RegisterFunc("dec", NewGolangFunctor2(decFunc), TypeFloat, anyParams) - ctx.RegisterFunc("string", NewGolangFunctor2(stringFunc), TypeString, anyParams) - ctx.RegisterFunc("fract", NewGolangFunctor2(fractFunc), TypeFraction, []ExprFuncParam{ + ctx.RegisterFunc("bool", NewGolangFunctor(boolFunc), TypeBoolean, anyParams) + ctx.RegisterFunc("int", NewGolangFunctor(intFunc), TypeInt, anyParams) + ctx.RegisterFunc("dec", NewGolangFunctor(decFunc), TypeFloat, anyParams) + ctx.RegisterFunc("string", NewGolangFunctor(stringFunc), TypeString, anyParams) + ctx.RegisterFunc("fract", NewGolangFunctor(fractFunc), TypeFraction, []ExprFuncParam{ NewFuncParam(ParamValue), NewFuncParamFlagDef(ParamDenominator, PfDefault, int64(1)), }) diff --git a/builtin-fmt.go b/builtin-fmt.go index a068e9b..c721d33 100644 --- a/builtin-fmt.go +++ b/builtin-fmt.go @@ -44,10 +44,10 @@ func printLnFunc(ctx ExprContext, name string, args map[string]any) (result any, } func ImportFmtFuncs(ctx ExprContext) { - ctx.RegisterFunc("print", NewGolangFunctor2(printFunc), TypeInt, []ExprFuncParam{ + ctx.RegisterFunc("print", NewGolangFunctor(printFunc), TypeInt, []ExprFuncParam{ NewFuncParamFlag(ParamItem, PfRepeat), }) - ctx.RegisterFunc("println", NewGolangFunctor2(printLnFunc), TypeInt, []ExprFuncParam{ + ctx.RegisterFunc("println", NewGolangFunctor(printLnFunc), TypeInt, []ExprFuncParam{ NewFuncParamFlag(ParamItem, PfRepeat), }) } diff --git a/builtin-import.go b/builtin-import.go index 12428ed..ee002c0 100644 --- a/builtin-import.go +++ b/builtin-import.go @@ -65,10 +65,10 @@ func doImport(ctx ExprContext, name string, dirList []string, it Iterator) (resu } func ImportImportFuncs(ctx ExprContext) { - ctx.RegisterFunc("import", NewGolangFunctor2(importFunc), TypeAny, []ExprFuncParam{ + ctx.RegisterFunc("import", NewGolangFunctor(importFunc), TypeAny, []ExprFuncParam{ NewFuncParamFlag(ParamFilepath, PfRepeat), }) - ctx.RegisterFunc("importAll", NewGolangFunctor2(importAllFunc), TypeAny, []ExprFuncParam{ + ctx.RegisterFunc("importAll", NewGolangFunctor(importAllFunc), TypeAny, []ExprFuncParam{ NewFuncParamFlag(ParamFilepath, PfRepeat), }) } diff --git a/builtin-iterator.go b/builtin-iterator.go index d99f367..2156143 100644 --- a/builtin-iterator.go +++ b/builtin-iterator.go @@ -61,7 +61,7 @@ func runFunc(ctx ExprContext, name string, args map[string]any) (result any, err if it, op, err = parseRunArgs(localCtx, args); err != nil { return } else if op == nil { - op = NewGolangFunctor2(printLnFunc) + op = NewGolangFunctor(printLnFunc) usingDefaultOp = true } @@ -92,7 +92,7 @@ func runFunc(ctx ExprContext, name string, args map[string]any) (result any, err } func ImportIterFuncs(ctx ExprContext) { - ctx.RegisterFunc("run", NewGolangFunctor2(runFunc), TypeAny, []ExprFuncParam{ + ctx.RegisterFunc("run", NewGolangFunctor(runFunc), TypeAny, []ExprFuncParam{ NewFuncParam(ParamIterator), NewFuncParamFlag(iterParamOperator, PfOptional), NewFuncParamFlag(iterParamVars, PfOptional), diff --git a/builtin-math-arith.go b/builtin-math-arith.go index baf7a46..7be529c 100644 --- a/builtin-math-arith.go +++ b/builtin-math-arith.go @@ -169,11 +169,11 @@ func mulFunc(ctx ExprContext, name string, args map[string]any) (result any, err } func ImportMathFuncs(ctx ExprContext) { - ctx.RegisterFunc("add", NewGolangFunctor2(addFunc), TypeNumber, []ExprFuncParam{ + ctx.RegisterFunc("add", NewGolangFunctor(addFunc), TypeNumber, []ExprFuncParam{ NewFuncParamFlagDef(ParamValue, PfDefault|PfRepeat, int64(0)), }) - ctx.RegisterFunc("mul", NewGolangFunctor2(mulFunc), TypeNumber, []ExprFuncParam{ + ctx.RegisterFunc("mul", NewGolangFunctor(mulFunc), TypeNumber, []ExprFuncParam{ NewFuncParamFlagDef(ParamValue, PfDefault|PfRepeat, int64(1)), }) } diff --git a/builtin-os-file.go b/builtin-os-file.go index fa377f1..1407dbc 100644 --- a/builtin-os-file.go +++ b/builtin-os-file.go @@ -221,33 +221,33 @@ func fileReadTextAllFunc(ctx ExprContext, name string, args map[string]any) (res } func ImportOsFuncs(ctx ExprContext) { - ctx.RegisterFunc("fileOpen", NewGolangFunctor2(openFileFunc), TypeFileHandle, []ExprFuncParam{ + ctx.RegisterFunc("fileOpen", NewGolangFunctor(openFileFunc), TypeFileHandle, []ExprFuncParam{ NewFuncParam(ParamFilepath), }) - ctx.RegisterFunc("fileAppend", NewGolangFunctor2(appendFileFunc), TypeFileHandle, []ExprFuncParam{ + ctx.RegisterFunc("fileAppend", NewGolangFunctor(appendFileFunc), TypeFileHandle, []ExprFuncParam{ NewFuncParam(ParamFilepath), }) - ctx.RegisterFunc("fileCreate", NewGolangFunctor2(createFileFunc), TypeFileHandle, []ExprFuncParam{ + ctx.RegisterFunc("fileCreate", NewGolangFunctor(createFileFunc), TypeFileHandle, []ExprFuncParam{ NewFuncParam(ParamFilepath), }) - ctx.RegisterFunc("fileClose", NewGolangFunctor2(closeFileFunc), TypeBoolean, []ExprFuncParam{ + ctx.RegisterFunc("fileClose", NewGolangFunctor(closeFileFunc), TypeBoolean, []ExprFuncParam{ NewFuncParam(ParamHandle), }) - ctx.RegisterFunc("fileWriteText", NewGolangFunctor2(fileWriteTextFunc), TypeInt, []ExprFuncParam{ + ctx.RegisterFunc("fileWriteText", NewGolangFunctor(fileWriteTextFunc), TypeInt, []ExprFuncParam{ NewFuncParam(ParamHandle), NewFuncParamFlagDef(ParamItem, PfDefault|PfRepeat, ""), }) - ctx.RegisterFunc("fileReadText", NewGolangFunctor2(fileReadTextFunc), TypeString, []ExprFuncParam{ + ctx.RegisterFunc("fileReadText", NewGolangFunctor(fileReadTextFunc), TypeString, []ExprFuncParam{ NewFuncParam(ParamHandle), NewFuncParamFlagDef(osLimitCh, PfDefault, "\n"), }) - ctx.RegisterFunc("fileReadTextAll", NewGolangFunctor2(fileReadTextAllFunc), TypeString, []ExprFuncParam{ + ctx.RegisterFunc("fileReadTextAll", NewGolangFunctor(fileReadTextAllFunc), TypeString, []ExprFuncParam{ NewFuncParam(ParamHandle), }) } diff --git a/builtin-string.go b/builtin-string.go index b2bf35c..6a9df5c 100644 --- a/builtin-string.go +++ b/builtin-string.go @@ -204,34 +204,34 @@ func splitStrFunc(ctx ExprContext, name string, args map[string]any) (result any // Import above functions in the context func ImportStringFuncs(ctx ExprContext) { - ctx.RegisterFunc("strJoin", NewGolangFunctor2(joinStrFunc), TypeString, []ExprFuncParam{ + ctx.RegisterFunc("strJoin", NewGolangFunctor(joinStrFunc), TypeString, []ExprFuncParam{ NewFuncParam(ParamSeparator), NewFuncParamFlag(ParamItem, PfRepeat), }) - ctx.RegisterFunc("strSub", NewGolangFunctor2(subStrFunc), TypeString, []ExprFuncParam{ + ctx.RegisterFunc("strSub", NewGolangFunctor(subStrFunc), TypeString, []ExprFuncParam{ NewFuncParam(ParamSource), NewFuncParamFlagDef(ParamStart, PfDefault, int64(0)), NewFuncParamFlagDef(ParamCount, PfDefault, int64(-1)), }) - ctx.RegisterFunc("strSplit", NewGolangFunctor2(splitStrFunc), "list of "+TypeString, []ExprFuncParam{ + ctx.RegisterFunc("strSplit", NewGolangFunctor(splitStrFunc), "list of "+TypeString, []ExprFuncParam{ NewFuncParam(ParamSource), NewFuncParamFlagDef(ParamSeparator, PfDefault, ""), NewFuncParamFlagDef(ParamCount, PfDefault, int64(-1)), }) - ctx.RegisterFunc("strTrim", NewGolangFunctor2(trimStrFunc), TypeString, []ExprFuncParam{ + ctx.RegisterFunc("strTrim", NewGolangFunctor(trimStrFunc), TypeString, []ExprFuncParam{ NewFuncParam(ParamSource), }) - ctx.RegisterFunc("strStartsWith", NewGolangFunctor2(startsWithStrFunc), TypeBoolean, []ExprFuncParam{ + ctx.RegisterFunc("strStartsWith", NewGolangFunctor(startsWithStrFunc), TypeBoolean, []ExprFuncParam{ NewFuncParam(ParamSource), NewFuncParam(ParamPrefix), NewFuncParamFlag(strParamOther, PfRepeat), }) - ctx.RegisterFunc("strEndsWith", NewGolangFunctor2(endsWithStrFunc), TypeBoolean, []ExprFuncParam{ + ctx.RegisterFunc("strEndsWith", NewGolangFunctor(endsWithStrFunc), TypeBoolean, []ExprFuncParam{ NewFuncParam(ParamSource), NewFuncParam(ParamSuffix), NewFuncParamFlag(strParamOther, PfRepeat), diff --git a/common-errors.go b/common-errors.go index 5e11e92..23ebe6f 100644 --- a/common-errors.go +++ b/common-errors.go @@ -59,6 +59,10 @@ func ErrWrongParamType(funcName, paramName, paramType string, paramValue any) er return fmt.Errorf("%s(): the %q parameter must be a %s, got a %s (%v)", funcName, paramName, paramType, TypeName(paramValue), paramValue) } +func ErrUnknownParam(funcName, paramName string) error { + return fmt.Errorf("%s(): unknown parameter %q", funcName, paramName) +} + // --- Operator errors func ErrLeftOperandMustBeVariable(leftTerm, opTerm *term) error { diff --git a/helpers.go b/helpers.go index 6e16066..f25fa10 100644 --- a/helpers.go +++ b/helpers.go @@ -38,7 +38,7 @@ func EvalStringV(source string, args []Arg) (result any, err error) { for _, arg := range args { if isFunc(arg.Value) { if f, ok := arg.Value.(FuncTemplate); ok { - functor := NewGolangFunctor2(f) + functor := NewGolangFunctor(f) // ctx.RegisterFunc(arg.Name, functor, 0, -1) ctx.RegisterFunc(arg.Name, functor, TypeAny, []ExprFuncParam{ NewFuncParamFlagDef(ParamValue, PfDefault|PfRepeat, 0), diff --git a/iterator.go b/iterator.go index 52feefc..9b0bc60 100644 --- a/iterator.go +++ b/iterator.go @@ -34,7 +34,7 @@ type Iterator interface { type ExtIterator interface { Iterator HasOperation(name string) bool - CallOperation(name string, args []any) (value any, err error) + CallOperation(name string, args map[string]any) (value any, err error) } func errNoOperation(name string) error { diff --git a/list-iterator.go b/list-iterator.go index 2070f8f..4d5f823 100644 --- a/list-iterator.go +++ b/list-iterator.go @@ -94,7 +94,7 @@ func (it *ListIterator) HasOperation(name string) bool { return yes } -func (it *ListIterator) CallOperation(name string, args []any) (v any, err error) { +func (it *ListIterator) CallOperation(name string, args map[string]any) (v any, err error) { switch name { case NextName: v, err = it.Next() diff --git a/operand-func.go b/operand-func.go index 3e02347..6aa564d 100644 --- a/operand-func.go +++ b/operand-func.go @@ -40,7 +40,7 @@ func newFuncCallTerm(tk *Token, args []*term) *term { func evalFuncCall(ctx ExprContext, opTerm *term) (v any, err error) { name, _ := opTerm.tk.Value.(string) - v, err = CallFunction3(ctx, name, opTerm) + v, err = CallFunction(ctx, name, opTerm) return } diff --git a/operator-dot.go b/operator-dot.go index ebc849e..e097438 100644 --- a/operator-dot.go +++ b/operator-dot.go @@ -32,7 +32,7 @@ func evalDot(ctx ExprContext, opTerm *term) (v any, err error) { if indexTerm.symbol() == SymVariable /*|| indexTerm.symbol() == SymString */ { opName := indexTerm.source() if unboxedValue.HasOperation(opName) { - v, err = unboxedValue.CallOperation(opName, []any{}) + v, err = unboxedValue.CallOperation(opName, map[string]any{}) } else { err = indexTerm.Errorf("this iterator do not support the %q command", opName) v = false diff --git a/t_expr_test.go b/t_expr_test.go index d285dc9..a995ba3 100644 --- a/t_expr_test.go +++ b/t_expr_test.go @@ -43,6 +43,6 @@ func TestExpr(t *testing.T) { } // t.Setenv("EXPR_PATH", ".") - runTestSuiteSpec(t, section, inputs, 17) - // runTestSuite(t, section, inputs) + // runTestSuiteSpec(t, section, inputs, 17) + runTestSuite(t, section, inputs) } diff --git a/t_funcs_test.go b/t_funcs_test.go index ff202a9..8f20025 100644 --- a/t_funcs_test.go +++ b/t_funcs_test.go @@ -35,8 +35,8 @@ func TestFuncs(t *testing.T) { /* 22 */ {`f=func(a,b){a*2+b}; f(b=2,a=1)`, int64(4), nil}, /* 23 */ {`f=func(a=10,b=10){a*2+b}; f(b=1)`, int64(21), nil}, /* 24 */ {`f=func(a,b){a*2+b}; f(a=1,2)`, nil, `f(): positional param nr 2 not allowed after named params`}, -// /* 20 */ {`a=[func(){3}]; a[0]()`, int64(3), nil}, -// /* 20 */ {`m={}; m["f"]=func(){3}; m["f"]()`, int64(3), nil}, + // /* 20 */ {`a=[func(){3}]; a[0]()`, int64(3), nil}, + // /* 20 */ {`m={}; m["f"]=func(){3}; m["f"]()`, int64(3), nil}, // /* 18 */ {`f=func(a){a*2}`, nil, errors.New(`[1:24] expected "function-param-value", got ")"`)}, } @@ -51,7 +51,7 @@ func dummy(ctx ExprContext, name string, args map[string]any) (result any, err e } func TestFunctionToStringSimple(t *testing.T) { - source := NewGolangFunctor2(dummy) + source := NewGolangFunctor(dummy) want := "func(){}" got := source.ToString(0) if got != want { @@ -59,9 +59,8 @@ func TestFunctionToStringSimple(t *testing.T) { } } - func TestFunctionGetFunc(t *testing.T) { - source := NewGolangFunctor2(dummy) + source := NewGolangFunctor(dummy) want := ExprFunc(nil) got := source.GetFunc() if got != want { diff --git a/t_iterator_test.go b/t_iterator_test.go index 238f301..bfd6f51 100644 --- a/t_iterator_test.go +++ b/t_iterator_test.go @@ -27,6 +27,6 @@ func TestIteratorParser(t *testing.T) { /* 16 */ {`include "test-resources/filter.expr"; it=$(ds,10); it++`, int64(2), nil}, } - // runTestSuiteSpec(t, section, inputs, 7) + // runTestSuiteSpec(t, section, inputs, 11) runTestSuite(t, section, inputs) }