Changed some function names and param types
This commit is contained in:
parent
a46753f453
commit
3c51b8d2ee
@ -10,7 +10,7 @@ type golangFunctor struct {
|
||||
f FuncTemplate
|
||||
}
|
||||
|
||||
func NewGolangFunctor2(f FuncTemplate) *golangFunctor {
|
||||
func NewGolangFunctor(f FuncTemplate) *golangFunctor {
|
||||
return &golangFunctor{f: f}
|
||||
}
|
||||
|
||||
|
@ -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)),
|
||||
})
|
||||
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
@ -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),
|
||||
|
@ -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)),
|
||||
})
|
||||
}
|
||||
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
@ -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),
|
||||
|
@ -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 {
|
||||
|
@ -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),
|
||||
|
@ -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 {
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user