From c100cf349d578f50e3062e8dcf2c662e6bc98407 Mon Sep 17 00:00:00 2001 From: Celestino Amoroso Date: Thu, 6 Jun 2024 05:31:35 +0200 Subject: [PATCH] some identier exported; new file import-utils.go --- common-type-names.go | 20 +++++------ control.go | 5 ++- fraction-type.go | 2 +- func-base.go | 32 +++++++++--------- func-fmt.go | 8 ++--- func-import.go | 76 +++--------------------------------------- func-math.go | 8 ++--- func-os.go | 28 ++++++++-------- func-string.go | 56 +++++++++++++++---------------- function.go | 20 +++++------ helpers.go | 6 ++-- import-utils.go | 79 ++++++++++++++++++++++++++++++++++++++++++++ operand-func.go | 4 +-- operator-assign.go | 2 +- operator-coalesce.go | 4 +-- operator-range.go | 2 +- t_funcs_test.go | 4 +-- 17 files changed, 186 insertions(+), 170 deletions(-) create mode 100644 import-utils.go diff --git a/common-type-names.go b/common-type-names.go index 8e105d0..40520ba 100644 --- a/common-type-names.go +++ b/common-type-names.go @@ -5,14 +5,14 @@ package expr const ( - typeAny = "any" - typeBoolean = "boolean" - typeFloat = "float" - typeFraction = "fraction" - typeHandle = "handle" - typeInt = "integer" - typeItem = "item" - typeNumber = "number" - typePair = "pair" - typeString = "string" + TypeAny = "any" + TypeBoolean = "boolean" + TypeFloat = "float" + TypeFraction = "fraction" + TypeHandle = "handle" + TypeInt = "integer" + TypeItem = "item" + TypeNumber = "number" + TypePair = "pair" + TypeString = "string" ) diff --git a/control.go b/control.go index f4d6a6b..0e6c7eb 100644 --- a/control.go +++ b/control.go @@ -11,6 +11,7 @@ const ( ControlLastResult = "last" ControlBoolShortcut = "_bool_shortcut" ControlImportPath = "_import_path" + ControlPluginPath = "_plugin_path" ) // Other control variables @@ -20,12 +21,14 @@ const ( // Initial values const ( - init_import_path = "~/.local/lib/go-pkg/expr:/usr/local/lib/go-pkg/expr:/usr/lib/go-pkg/expr" + init_import_path = "~/.local/lib/go-pkg/expr/sources:/usr/local/lib/go-pkg/expr/sources:/usr/lib/go-pkg/expr/sources" + init_plugin_path = "~/.local/lib/go-pkg/expr/plugins:/usr/local/lib/go-pkg/expr/plugins:/usr/lib/go-pkg/expr/plugins" ) func initDefaultVars(ctx ExprContext) { ctx.SetVar(ControlBoolShortcut, true) ctx.SetVar(ControlImportPath, init_import_path) + ctx.SetVar(ControlPluginPath, init_plugin_path) } func enable(ctx ExprContext, name string) { diff --git a/fraction-type.go b/fraction-type.go index 617fabd..42d6ce7 100644 --- a/fraction-type.go +++ b/fraction-type.go @@ -212,7 +212,7 @@ func anyToFract(v any) (f *FractionType, err error) { } } if f == nil { - err = errExpectedGot("fract", typeFraction, v) + err = errExpectedGot("fract", TypeFraction, v) } return } diff --git a/func-base.go b/func-base.go index 1cb32c6..c735ebd 100644 --- a/func-base.go +++ b/func-base.go @@ -159,25 +159,25 @@ func iteratorFunc(ctx ExprContext, name string, args []any) (result any, err err func ImportBuiltinsFuncs(ctx ExprContext) { anyParams := []ExprFuncParam{ - newFuncParam(paramValue), + NewFuncParam(paramValue), } - 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("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", newGolangFunctor(boolFunc), typeBoolean, anyParams) - ctx.RegisterFunc("int", newGolangFunctor(intFunc), typeInt, anyParams) - ctx.RegisterFunc("dec", newGolangFunctor(decFunc), typeFloat, anyParams) - ctx.RegisterFunc("fract", newGolangFunctor(fractFunc), typeFraction, []ExprFuncParam{ - newFuncParam(paramValue), - newFuncParamFlagDef("denominator", pfOptional, 1), + ctx.RegisterFunc("bool", NewGolangFunctor(boolFunc), TypeBoolean, anyParams) + ctx.RegisterFunc("int", NewGolangFunctor(intFunc), TypeInt, anyParams) + ctx.RegisterFunc("dec", NewGolangFunctor(decFunc), TypeFloat, anyParams) + ctx.RegisterFunc("fract", NewGolangFunctor(fractFunc), TypeFraction, []ExprFuncParam{ + NewFuncParam(paramValue), + NewFuncParamFlagDef("denominator", PfOptional, 1), }) } diff --git a/func-fmt.go b/func-fmt.go index 5644c3d..1e7bfef 100644 --- a/func-fmt.go +++ b/func-fmt.go @@ -23,11 +23,11 @@ func printLnFunc(ctx ExprContext, name string, args []any) (result any, err erro } func ImportFmtFuncs(ctx ExprContext) { - ctx.RegisterFunc("print", newGolangFunctor(printFunc), typeInt, []ExprFuncParam{ - newFuncParamFlag(paramItem, pfRepeat), + ctx.RegisterFunc("print", NewGolangFunctor(printFunc), TypeInt, []ExprFuncParam{ + NewFuncParamFlag(paramItem, PfRepeat), }) - ctx.RegisterFunc("println", newGolangFunctor(printLnFunc), typeInt, []ExprFuncParam{ - newFuncParamFlag(paramItem, pfRepeat), + ctx.RegisterFunc("println", NewGolangFunctor(printLnFunc), TypeInt, []ExprFuncParam{ + NewFuncParamFlag(paramItem, PfRepeat), }) } diff --git a/func-import.go b/func-import.go index bc82540..2ece721 100644 --- a/func-import.go +++ b/func-import.go @@ -5,13 +5,8 @@ package expr import ( - "errors" - "fmt" "io" "os" - "path" - "path/filepath" - "strings" ) const ENV_EXPR_PATH = "EXPR_PATH" @@ -34,69 +29,8 @@ func importGeneral(ctx ExprContext, name string, args []any) (result any, err er return } -func checkStringParamExpected(funcName string, paramValue any, paramPos int) (err error) { - if !(IsString(paramValue) /*|| isList(paramValue)*/) { - err = fmt.Errorf("%s(): param nr %d has wrong type %T, string expected", funcName, paramPos+1, paramValue) - } - return -} - -func addEnvImportDirs(dirList []string) []string { - if dirSpec, exists := os.LookupEnv(ENV_EXPR_PATH); exists { - dirs := strings.Split(dirSpec, ":") - if dirList == nil { - dirList = dirs - } else { - dirList = append(dirList, dirs...) - } - } - return dirList -} - func addPresetImportDirs(ctx ExprContext, dirList []string) []string { - if dirSpec, exists := getControlString(ctx, ControlImportPath); exists { - dirs := strings.Split(dirSpec, ":") - if dirList == nil { - dirList = dirs - } else { - dirList = append(dirList, dirs...) - } - } - return dirList -} - -func isFile(filePath string) bool { - info, err := os.Stat(filePath) - return (err == nil || errors.Is(err, os.ErrExist)) && info.Mode().IsRegular() -} - -func searchAmongPath(filename string, dirList []string) (filePath string) { - for _, dir := range dirList { - if fullPath := path.Join(dir, filename); isFile(fullPath) { - filePath = fullPath - break - } - } - return -} - -func isPathRelative(filePath string) bool { - unixPath := filepath.ToSlash(filePath) - return strings.HasPrefix(unixPath, "./") || strings.HasPrefix(unixPath, "../") -} - -func makeFilepath(filename string, dirList []string) (filePath string, err error) { - if path.IsAbs(filename) || isPathRelative(filename) { - if isFile(filename) { - filePath = filename - } - } else { - filePath = searchAmongPath(filename, dirList) - } - if len(filePath) == 0 { - err = fmt.Errorf("source file %q not found", filename) - } - return + return addPresetDirs(ctx, ControlImportPath, dirList) } func doImport(ctx ExprContext, name string, dirList []string, it Iterator) (result any, err error) { @@ -137,11 +71,11 @@ func doImport(ctx ExprContext, name string, dirList []string, it Iterator) (resu } func ImportImportFuncs(ctx ExprContext) { - ctx.RegisterFunc("import", newGolangFunctor(importFunc), typeAny, []ExprFuncParam{ - newFuncParamFlag(paramFilepath, pfRepeat), + ctx.RegisterFunc("import", NewGolangFunctor(importFunc), TypeAny, []ExprFuncParam{ + NewFuncParamFlag(paramFilepath, PfRepeat), }) - ctx.RegisterFunc("importAll", newGolangFunctor(importAllFunc), typeAny, []ExprFuncParam{ - newFuncParamFlag(paramFilepath, pfRepeat), + ctx.RegisterFunc("importAll", NewGolangFunctor(importAllFunc), TypeAny, []ExprFuncParam{ + NewFuncParamFlag(paramFilepath, PfRepeat), }) } diff --git a/func-math.go b/func-math.go index 5a2d2a0..5b37d8e 100644 --- a/func-math.go +++ b/func-math.go @@ -167,12 +167,12 @@ func mulFunc(ctx ExprContext, name string, args []any) (result any, err error) { } func ImportMathFuncs(ctx ExprContext) { - ctx.RegisterFunc("add", &golangFunctor{f: addFunc}, typeNumber, []ExprFuncParam{ - newFuncParamFlagDef(paramValue, pfOptional|pfRepeat, int64(0)), + ctx.RegisterFunc("add", &golangFunctor{f: addFunc}, TypeNumber, []ExprFuncParam{ + NewFuncParamFlagDef(paramValue, PfOptional|PfRepeat, int64(0)), }) - ctx.RegisterFunc("mul", &golangFunctor{f: mulFunc}, typeNumber, []ExprFuncParam{ - newFuncParamFlagDef(paramValue, pfOptional|pfRepeat, int64(1)), + ctx.RegisterFunc("mul", &golangFunctor{f: mulFunc}, TypeNumber, []ExprFuncParam{ + NewFuncParamFlagDef(paramValue, PfOptional|PfRepeat, int64(1)), }) } diff --git a/func-os.go b/func-os.go index 763aba3..53c905f 100644 --- a/func-os.go +++ b/func-os.go @@ -187,25 +187,25 @@ func readFileFunc(ctx ExprContext, name string, args []any) (result any, err err } func ImportOsFuncs(ctx ExprContext) { - ctx.RegisterFunc("openFile", newGolangFunctor(openFileFunc), typeHandle, []ExprFuncParam{ - newFuncParam(paramFilepath), + ctx.RegisterFunc("openFile", NewGolangFunctor(openFileFunc), TypeHandle, []ExprFuncParam{ + NewFuncParam(paramFilepath), }) - ctx.RegisterFunc("appendFile", newGolangFunctor(appendFileFunc), typeHandle, []ExprFuncParam{ - newFuncParam(paramFilepath), + ctx.RegisterFunc("appendFile", NewGolangFunctor(appendFileFunc), TypeHandle, []ExprFuncParam{ + NewFuncParam(paramFilepath), }) - ctx.RegisterFunc("createFile", newGolangFunctor(createFileFunc), typeHandle, []ExprFuncParam{ - newFuncParam(paramFilepath), + ctx.RegisterFunc("createFile", NewGolangFunctor(createFileFunc), TypeHandle, []ExprFuncParam{ + NewFuncParam(paramFilepath), }) - ctx.RegisterFunc("writeFile", newGolangFunctor(writeFileFunc), typeInt, []ExprFuncParam{ - newFuncParam(typeHandle), - newFuncParamFlagDef(typeItem, pfOptional|pfRepeat, ""), + ctx.RegisterFunc("writeFile", NewGolangFunctor(writeFileFunc), TypeInt, []ExprFuncParam{ + NewFuncParam(TypeHandle), + NewFuncParamFlagDef(TypeItem, PfOptional|PfRepeat, ""), }) - ctx.RegisterFunc("readFile", newGolangFunctor(readFileFunc), typeString, []ExprFuncParam{ - newFuncParam(typeHandle), - newFuncParamFlagDef("limitCh", pfOptional, "\n"), + ctx.RegisterFunc("readFile", NewGolangFunctor(readFileFunc), TypeString, []ExprFuncParam{ + NewFuncParam(TypeHandle), + NewFuncParamFlagDef("limitCh", PfOptional, "\n"), }) - ctx.RegisterFunc("closeFile", newGolangFunctor(closeFileFunc), typeBoolean, []ExprFuncParam{ - newFuncParam(typeHandle), + ctx.RegisterFunc("closeFile", NewGolangFunctor(closeFileFunc), TypeBoolean, []ExprFuncParam{ + NewFuncParam(TypeHandle), }) } diff --git a/func-string.go b/func-string.go index b9236f1..e52df49 100644 --- a/func-string.go +++ b/func-string.go @@ -21,7 +21,7 @@ func doJoinStr(funcName string, sep string, it Iterator) (result any, err error) if s, ok := v.(string); ok { sb.WriteString(s) } else { - err = errExpectedGot(funcName, typeString, v) + err = errExpectedGot(funcName, TypeString, v) return } } @@ -51,7 +51,7 @@ func joinStrFunc(ctx ExprContext, name string, args []any) (result any, err erro result, err = doJoinStr(name, sep, NewArrayIterator(args[1:])) } } else { - err = errWrongParamType(name, paramSeparator, typeString, args[0]) + err = errWrongParamType(name, paramSeparator, TypeString, args[0]) } return } @@ -63,7 +63,7 @@ func subStrFunc(ctx ExprContext, name string, args []any) (result any, err error var ok bool if source, ok = args[0].(string); !ok { - return nil, errWrongParamType(name, paramSource, typeString, args[0]) + return nil, errWrongParamType(name, paramSource, TypeString, args[0]) } if start, err = toInt(args[1], name+"()"); err != nil { @@ -91,7 +91,7 @@ func trimStrFunc(ctx ExprContext, name string, args []any) (result any, err erro var ok bool if source, ok = args[0].(string); !ok { - return nil, errWrongParamType(name, paramSource, typeString, args[0]) + return nil, errWrongParamType(name, paramSource, TypeString, args[0]) } result = strings.TrimSpace(source) return @@ -104,7 +104,7 @@ func startsWithStrFunc(ctx ExprContext, name string, args []any) (result any, er result = false if source, ok = args[0].(string); !ok { - return result, errWrongParamType(name, paramSource, typeString, args[0]) + return result, errWrongParamType(name, paramSource, TypeString, args[0]) } for i, targetSpec := range args[1:] { if target, ok := targetSpec.(string); ok { @@ -127,7 +127,7 @@ func endsWithStrFunc(ctx ExprContext, name string, args []any) (result any, err result = false if source, ok = args[0].(string); !ok { - return result, errWrongParamType(name, paramSource, typeString, args[0]) + return result, errWrongParamType(name, paramSource, TypeString, args[0]) } for i, targetSpec := range args[1:] { if target, ok := targetSpec.(string); ok { @@ -150,7 +150,7 @@ func splitStrFunc(ctx ExprContext, name string, args []any) (result any, err err var ok bool if source, ok = args[0].(string); !ok { - return result, errWrongParamType(name, paramSource, typeString, args[0]) + return result, errWrongParamType(name, paramSource, TypeString, args[0]) } if sep, ok = args[1].(string); !ok { @@ -182,37 +182,37 @@ func splitStrFunc(ctx ExprContext, name string, args []any) (result any, err err // Import above functions in the context func ImportStringFuncs(ctx ExprContext) { - ctx.RegisterFunc("joinStr", newGolangFunctor(joinStrFunc), typeString, []ExprFuncParam{ - newFuncParam(paramSeparator), - newFuncParamFlag(paramItem, pfRepeat), + ctx.RegisterFunc("joinStr", NewGolangFunctor(joinStrFunc), TypeString, []ExprFuncParam{ + NewFuncParam(paramSeparator), + NewFuncParamFlag(paramItem, PfRepeat), }) - ctx.RegisterFunc("subStr", newGolangFunctor(subStrFunc), typeString, []ExprFuncParam{ - newFuncParam(paramSource), - newFuncParamFlagDef(paramStart, pfOptional, int64(0)), - newFuncParamFlagDef(paramCount, pfOptional, int64(-1)), + ctx.RegisterFunc("subStr", NewGolangFunctor(subStrFunc), TypeString, []ExprFuncParam{ + NewFuncParam(paramSource), + NewFuncParamFlagDef(paramStart, PfOptional, int64(0)), + NewFuncParamFlagDef(paramCount, PfOptional, int64(-1)), }) - ctx.RegisterFunc("splitStr", newGolangFunctor(splitStrFunc), "list of "+typeString, []ExprFuncParam{ - newFuncParam(paramSource), - newFuncParamFlagDef(paramSeparator, pfOptional, ""), - newFuncParamFlagDef(paramCount, pfOptional, int64(-1)), + ctx.RegisterFunc("splitStr", NewGolangFunctor(splitStrFunc), "list of "+TypeString, []ExprFuncParam{ + NewFuncParam(paramSource), + NewFuncParamFlagDef(paramSeparator, PfOptional, ""), + NewFuncParamFlagDef(paramCount, PfOptional, int64(-1)), }) - ctx.RegisterFunc("trimStr", newGolangFunctor(trimStrFunc), typeString, []ExprFuncParam{ - newFuncParam(paramSource), + ctx.RegisterFunc("trimStr", NewGolangFunctor(trimStrFunc), TypeString, []ExprFuncParam{ + NewFuncParam(paramSource), }) - ctx.RegisterFunc("startsWithStr", newGolangFunctor(startsWithStrFunc), typeBoolean, []ExprFuncParam{ - newFuncParam(paramSource), - newFuncParam(paramPrefix), - newFuncParamFlag("other "+paramPrefix, pfRepeat), + ctx.RegisterFunc("startsWithStr", NewGolangFunctor(startsWithStrFunc), TypeBoolean, []ExprFuncParam{ + NewFuncParam(paramSource), + NewFuncParam(paramPrefix), + NewFuncParamFlag("other "+paramPrefix, PfRepeat), }) - ctx.RegisterFunc("endsWithStr", newGolangFunctor(endsWithStrFunc), typeBoolean, []ExprFuncParam{ - newFuncParam(paramSource), - newFuncParam(paramSuffix), - newFuncParamFlag("other "+paramSuffix, pfRepeat), + ctx.RegisterFunc("endsWithStr", NewGolangFunctor(endsWithStrFunc), TypeBoolean, []ExprFuncParam{ + NewFuncParam(paramSource), + NewFuncParam(paramSuffix), + NewFuncParamFlag("other "+paramSuffix, PfRepeat), }) } diff --git a/function.go b/function.go index 5cedd7d..0a51425 100644 --- a/function.go +++ b/function.go @@ -48,7 +48,7 @@ type golangFunctor struct { f FuncTemplate } -func newGolangFunctor(f FuncTemplate) *golangFunctor { +func NewGolangFunctor(f FuncTemplate) *golangFunctor { return &golangFunctor{f: f} } @@ -83,7 +83,7 @@ func (functor *exprFunctor) Invoke(ctx ExprContext, name string, args []any) (re if funcArg, ok := arg.(Functor); ok { // ctx.RegisterFunc(p, functor, 0, -1) paramSpecs := funcArg.GetParams() - ctx.RegisterFunc(p.Name(), funcArg, typeAny, paramSpecs) + ctx.RegisterFunc(p.Name(), funcArg, TypeAny, paramSpecs) } else { ctx.UnsafeSetVar(p.Name(), arg) } @@ -99,8 +99,8 @@ func (functor *exprFunctor) Invoke(ctx ExprContext, name string, args []any) (re type paramFlags uint16 const ( - pfOptional paramFlags = 1 << iota - pfRepeat + PfOptional paramFlags = 1 << iota + PfRepeat ) type funcParamInfo struct { @@ -109,15 +109,15 @@ type funcParamInfo struct { defaultValue any } -func newFuncParam(name string) ExprFuncParam { +func NewFuncParam(name string) ExprFuncParam { return &funcParamInfo{name: name} } -func newFuncParamFlag(name string, flags paramFlags) ExprFuncParam { +func NewFuncParamFlag(name string, flags paramFlags) ExprFuncParam { return &funcParamInfo{name: name, flags: flags} } -func newFuncParamFlagDef(name string, flags paramFlags, defValue any) *funcParamInfo { +func NewFuncParamFlagDef(name string, flags paramFlags, defValue any) *funcParamInfo { return &funcParamInfo{name: name, flags: flags, defaultValue: defValue} } @@ -130,11 +130,11 @@ func (param *funcParamInfo) Type() string { } func (param *funcParamInfo) IsOptional() bool { - return (param.flags & pfOptional) != 0 + return (param.flags & PfOptional) != 0 } func (param *funcParamInfo) IsRepeat() bool { - return (param.flags & pfRepeat) != 0 + return (param.flags & PfRepeat) != 0 } func (param *funcParamInfo) DefaultValue() any { @@ -226,7 +226,7 @@ func (info *funcInfo) ToString(opt FmtOpt) string { if len(info.returnType) > 0 { sb.WriteString(info.returnType) } else { - sb.WriteString(typeAny) + sb.WriteString(TypeAny) } sb.WriteString(" {}") return sb.String() diff --git a/helpers.go b/helpers.go index 3c3aa1d..9b0472d 100644 --- a/helpers.go +++ b/helpers.go @@ -38,10 +38,10 @@ 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 := newGolangFunctor(f) + functor := NewGolangFunctor(f) // ctx.RegisterFunc(arg.Name, functor, 0, -1) - ctx.RegisterFunc(arg.Name, functor, typeAny, []ExprFuncParam{ - newFuncParamFlagDef(paramValue, pfOptional|pfRepeat, 0), + ctx.RegisterFunc(arg.Name, functor, TypeAny, []ExprFuncParam{ + NewFuncParamFlagDef(paramValue, PfOptional|PfRepeat, 0), }) } else { err = fmt.Errorf("invalid function specification: %q", arg.Name) diff --git a/import-utils.go b/import-utils.go new file mode 100644 index 0000000..14ce6a7 --- /dev/null +++ b/import-utils.go @@ -0,0 +1,79 @@ +// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com). +// All rights reserved. + +// import-utils.go +package expr + +import ( + "errors" + "fmt" + "os" + "path" + "path/filepath" + "strings" +) + +func checkStringParamExpected(funcName string, paramValue any, paramPos int) (err error) { + if !(IsString(paramValue) /*|| isList(paramValue)*/) { + err = fmt.Errorf("%s(): param nr %d has wrong type %s, string expected", funcName, paramPos+1, typeName(paramValue)) + } + return +} + +func addEnvImportDirs(dirList []string) []string { + if dirSpec, exists := os.LookupEnv(ENV_EXPR_PATH); exists { + dirs := strings.Split(dirSpec, ":") + if dirList == nil { + dirList = dirs + } else { + dirList = append(dirList, dirs...) + } + } + return dirList +} + +func addPresetDirs(ctx ExprContext, ctrlKey string, dirList []string) []string { + if dirSpec, exists := getControlString(ctx, ctrlKey); exists { + dirs := strings.Split(dirSpec, ":") + if dirList == nil { + dirList = dirs + } else { + dirList = append(dirList, dirs...) + } + } + return dirList +} + +func isFile(filePath string) bool { + info, err := os.Stat(filePath) + return (err == nil || errors.Is(err, os.ErrExist)) && info.Mode().IsRegular() +} + +func searchAmongPath(filename string, dirList []string) (filePath string) { + for _, dir := range dirList { + if fullPath := path.Join(dir, filename); isFile(fullPath) { + filePath = fullPath + break + } + } + return +} + +func isPathRelative(filePath string) bool { + unixPath := filepath.ToSlash(filePath) + return strings.HasPrefix(unixPath, "./") || strings.HasPrefix(unixPath, "../") +} + +func makeFilepath(filename string, dirList []string) (filePath string, err error) { + if path.IsAbs(filename) || isPathRelative(filename) { + if isFile(filename) { + filePath = filename + } + } else { + filePath = searchAmongPath(filename, dirList) + } + if len(filePath) == 0 { + err = fmt.Errorf("source file %q not found", filename) + } + return +} diff --git a/operand-func.go b/operand-func.go index d149ee9..1d16ac0 100644 --- a/operand-func.go +++ b/operand-func.go @@ -91,12 +91,12 @@ func evalFuncDef(ctx ExprContext, self *term) (v any, err error) { var defValue any flags := paramFlags(0) if len(param.children) > 0 { - flags |= pfOptional + flags |= PfOptional if defValue, err = param.children[0].compute(ctx); err != nil { return } } - info := newFuncParamFlagDef(param.source(), flags, defValue) + info := NewFuncParamFlagDef(param.source(), flags, defValue) paramList = append(paramList, info) } v = newExprFunctor(expr, paramList, ctx) diff --git a/operator-assign.go b/operator-assign.go index fb21069..9aed2f1 100644 --- a/operator-assign.go +++ b/operator-assign.go @@ -81,7 +81,7 @@ func evalAssign(ctx ExprContext, self *term) (v any, err error) { } else if funcDef, ok := functor.(*exprFunctor); ok { paramSpecs := ForAll(funcDef.params, func(p ExprFuncParam) ExprFuncParam { return p }) - ctx.RegisterFunc(leftTerm.source(), functor, typeAny, paramSpecs) + ctx.RegisterFunc(leftTerm.source(), functor, TypeAny, paramSpecs) } else { err = self.Errorf("unknown function %s()", rightChild.source()) } diff --git a/operator-coalesce.go b/operator-coalesce.go index 2acd8b1..de323c6 100644 --- a/operator-coalesce.go +++ b/operator-coalesce.go @@ -67,8 +67,8 @@ func evalAssignCoalesce(ctx ExprContext, self *term) (v any, err error) { } else if rightValue, err = self.children[1].compute(ctx); err == nil { if functor, ok := rightValue.(Functor); ok { //ctx.RegisterFunc(leftTerm.source(), functor, 0, -1) - ctx.RegisterFunc(leftTerm.source(), functor, typeAny, []ExprFuncParam{ - newFuncParamFlag(paramValue, pfOptional|pfRepeat), + ctx.RegisterFunc(leftTerm.source(), functor, TypeAny, []ExprFuncParam{ + NewFuncParamFlag(paramValue, PfOptional|PfRepeat), }) } else { v = rightValue diff --git a/operator-range.go b/operator-range.go index 17de7b6..8b485de 100644 --- a/operator-range.go +++ b/operator-range.go @@ -12,7 +12,7 @@ type intPair struct { } func (p *intPair) TypeName() string { - return typePair + return TypePair } func (p *intPair) ToString(opt FmtOpt) string { diff --git a/t_funcs_test.go b/t_funcs_test.go index 421013f..526c52c 100644 --- a/t_funcs_test.go +++ b/t_funcs_test.go @@ -43,7 +43,7 @@ func dummy(ctx ExprContext, name string, args []any) (result any, err error) { } func TestFunctionToStringSimple(t *testing.T) { - source := newGolangFunctor(dummy) + source := NewGolangFunctor(dummy) want := "func() {}" got := source.ToString(0) if got != want { @@ -52,7 +52,7 @@ func TestFunctionToStringSimple(t *testing.T) { } func TestFunctionGetFunc(t *testing.T) { - source := newGolangFunctor(dummy) + source := NewGolangFunctor(dummy) want := ExprFunc(nil) got := source.GetFunc() if got != want {