some identier exported; new file import-utils.go
This commit is contained in:
parent
8144122d2c
commit
c100cf349d
@ -5,14 +5,14 @@
|
|||||||
package expr
|
package expr
|
||||||
|
|
||||||
const (
|
const (
|
||||||
typeAny = "any"
|
TypeAny = "any"
|
||||||
typeBoolean = "boolean"
|
TypeBoolean = "boolean"
|
||||||
typeFloat = "float"
|
TypeFloat = "float"
|
||||||
typeFraction = "fraction"
|
TypeFraction = "fraction"
|
||||||
typeHandle = "handle"
|
TypeHandle = "handle"
|
||||||
typeInt = "integer"
|
TypeInt = "integer"
|
||||||
typeItem = "item"
|
TypeItem = "item"
|
||||||
typeNumber = "number"
|
TypeNumber = "number"
|
||||||
typePair = "pair"
|
TypePair = "pair"
|
||||||
typeString = "string"
|
TypeString = "string"
|
||||||
)
|
)
|
||||||
|
@ -11,6 +11,7 @@ const (
|
|||||||
ControlLastResult = "last"
|
ControlLastResult = "last"
|
||||||
ControlBoolShortcut = "_bool_shortcut"
|
ControlBoolShortcut = "_bool_shortcut"
|
||||||
ControlImportPath = "_import_path"
|
ControlImportPath = "_import_path"
|
||||||
|
ControlPluginPath = "_plugin_path"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Other control variables
|
// Other control variables
|
||||||
@ -20,12 +21,14 @@ const (
|
|||||||
|
|
||||||
// Initial values
|
// Initial values
|
||||||
const (
|
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) {
|
func initDefaultVars(ctx ExprContext) {
|
||||||
ctx.SetVar(ControlBoolShortcut, true)
|
ctx.SetVar(ControlBoolShortcut, true)
|
||||||
ctx.SetVar(ControlImportPath, init_import_path)
|
ctx.SetVar(ControlImportPath, init_import_path)
|
||||||
|
ctx.SetVar(ControlPluginPath, init_plugin_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func enable(ctx ExprContext, name string) {
|
func enable(ctx ExprContext, name string) {
|
||||||
|
@ -212,7 +212,7 @@ func anyToFract(v any) (f *FractionType, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if f == nil {
|
if f == nil {
|
||||||
err = errExpectedGot("fract", typeFraction, v)
|
err = errExpectedGot("fract", TypeFraction, v)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
32
func-base.go
32
func-base.go
@ -159,25 +159,25 @@ func iteratorFunc(ctx ExprContext, name string, args []any) (result any, err err
|
|||||||
|
|
||||||
func ImportBuiltinsFuncs(ctx ExprContext) {
|
func ImportBuiltinsFuncs(ctx ExprContext) {
|
||||||
anyParams := []ExprFuncParam{
|
anyParams := []ExprFuncParam{
|
||||||
newFuncParam(paramValue),
|
NewFuncParam(paramValue),
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.RegisterFunc("isNil", newGolangFunctor(isNilFunc), typeBoolean, anyParams)
|
ctx.RegisterFunc("isNil", NewGolangFunctor(isNilFunc), TypeBoolean, anyParams)
|
||||||
ctx.RegisterFunc("isInt", newGolangFunctor(isIntFunc), typeBoolean, anyParams)
|
ctx.RegisterFunc("isInt", NewGolangFunctor(isIntFunc), TypeBoolean, anyParams)
|
||||||
ctx.RegisterFunc("isFloat", newGolangFunctor(isFloatFunc), typeBoolean, anyParams)
|
ctx.RegisterFunc("isFloat", NewGolangFunctor(isFloatFunc), TypeBoolean, anyParams)
|
||||||
ctx.RegisterFunc("isBool", newGolangFunctor(isBoolFunc), typeBoolean, anyParams)
|
ctx.RegisterFunc("isBool", NewGolangFunctor(isBoolFunc), TypeBoolean, anyParams)
|
||||||
ctx.RegisterFunc("isString", newGolangFunctor(isStringFunc), typeBoolean, anyParams)
|
ctx.RegisterFunc("isString", NewGolangFunctor(isStringFunc), TypeBoolean, anyParams)
|
||||||
ctx.RegisterFunc("isFract", newGolangFunctor(isFractionFunc), typeBoolean, anyParams)
|
ctx.RegisterFunc("isFract", NewGolangFunctor(isFractionFunc), TypeBoolean, anyParams)
|
||||||
ctx.RegisterFunc("isRational", newGolangFunctor(isRationalFunc), typeBoolean, anyParams)
|
ctx.RegisterFunc("isRational", NewGolangFunctor(isRationalFunc), TypeBoolean, anyParams)
|
||||||
ctx.RegisterFunc("isList", newGolangFunctor(isListFunc), typeBoolean, anyParams)
|
ctx.RegisterFunc("isList", NewGolangFunctor(isListFunc), TypeBoolean, anyParams)
|
||||||
ctx.RegisterFunc("isDict", newGolangFunctor(isDictionaryFunc), typeBoolean, anyParams)
|
ctx.RegisterFunc("isDict", NewGolangFunctor(isDictionaryFunc), TypeBoolean, anyParams)
|
||||||
|
|
||||||
ctx.RegisterFunc("bool", newGolangFunctor(boolFunc), typeBoolean, anyParams)
|
ctx.RegisterFunc("bool", NewGolangFunctor(boolFunc), TypeBoolean, anyParams)
|
||||||
ctx.RegisterFunc("int", newGolangFunctor(intFunc), typeInt, anyParams)
|
ctx.RegisterFunc("int", NewGolangFunctor(intFunc), TypeInt, anyParams)
|
||||||
ctx.RegisterFunc("dec", newGolangFunctor(decFunc), typeFloat, anyParams)
|
ctx.RegisterFunc("dec", NewGolangFunctor(decFunc), TypeFloat, anyParams)
|
||||||
ctx.RegisterFunc("fract", newGolangFunctor(fractFunc), typeFraction, []ExprFuncParam{
|
ctx.RegisterFunc("fract", NewGolangFunctor(fractFunc), TypeFraction, []ExprFuncParam{
|
||||||
newFuncParam(paramValue),
|
NewFuncParam(paramValue),
|
||||||
newFuncParamFlagDef("denominator", pfOptional, 1),
|
NewFuncParamFlagDef("denominator", PfOptional, 1),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,11 +23,11 @@ func printLnFunc(ctx ExprContext, name string, args []any) (result any, err erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ImportFmtFuncs(ctx ExprContext) {
|
func ImportFmtFuncs(ctx ExprContext) {
|
||||||
ctx.RegisterFunc("print", newGolangFunctor(printFunc), typeInt, []ExprFuncParam{
|
ctx.RegisterFunc("print", NewGolangFunctor(printFunc), TypeInt, []ExprFuncParam{
|
||||||
newFuncParamFlag(paramItem, pfRepeat),
|
NewFuncParamFlag(paramItem, PfRepeat),
|
||||||
})
|
})
|
||||||
ctx.RegisterFunc("println", newGolangFunctor(printLnFunc), typeInt, []ExprFuncParam{
|
ctx.RegisterFunc("println", NewGolangFunctor(printLnFunc), TypeInt, []ExprFuncParam{
|
||||||
newFuncParamFlag(paramItem, pfRepeat),
|
NewFuncParamFlag(paramItem, PfRepeat),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,13 +5,8 @@
|
|||||||
package expr
|
package expr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const ENV_EXPR_PATH = "EXPR_PATH"
|
const ENV_EXPR_PATH = "EXPR_PATH"
|
||||||
@ -34,69 +29,8 @@ func importGeneral(ctx ExprContext, name string, args []any) (result any, err er
|
|||||||
return
|
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 {
|
func addPresetImportDirs(ctx ExprContext, dirList []string) []string {
|
||||||
if dirSpec, exists := getControlString(ctx, ControlImportPath); exists {
|
return addPresetDirs(ctx, ControlImportPath, dirList)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func doImport(ctx ExprContext, name string, dirList []string, it Iterator) (result any, err error) {
|
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) {
|
func ImportImportFuncs(ctx ExprContext) {
|
||||||
ctx.RegisterFunc("import", newGolangFunctor(importFunc), typeAny, []ExprFuncParam{
|
ctx.RegisterFunc("import", NewGolangFunctor(importFunc), TypeAny, []ExprFuncParam{
|
||||||
newFuncParamFlag(paramFilepath, pfRepeat),
|
NewFuncParamFlag(paramFilepath, PfRepeat),
|
||||||
})
|
})
|
||||||
ctx.RegisterFunc("importAll", newGolangFunctor(importAllFunc), typeAny, []ExprFuncParam{
|
ctx.RegisterFunc("importAll", NewGolangFunctor(importAllFunc), TypeAny, []ExprFuncParam{
|
||||||
newFuncParamFlag(paramFilepath, pfRepeat),
|
NewFuncParamFlag(paramFilepath, PfRepeat),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,12 +167,12 @@ func mulFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ImportMathFuncs(ctx ExprContext) {
|
func ImportMathFuncs(ctx ExprContext) {
|
||||||
ctx.RegisterFunc("add", &golangFunctor{f: addFunc}, typeNumber, []ExprFuncParam{
|
ctx.RegisterFunc("add", &golangFunctor{f: addFunc}, TypeNumber, []ExprFuncParam{
|
||||||
newFuncParamFlagDef(paramValue, pfOptional|pfRepeat, int64(0)),
|
NewFuncParamFlagDef(paramValue, PfOptional|PfRepeat, int64(0)),
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx.RegisterFunc("mul", &golangFunctor{f: mulFunc}, typeNumber, []ExprFuncParam{
|
ctx.RegisterFunc("mul", &golangFunctor{f: mulFunc}, TypeNumber, []ExprFuncParam{
|
||||||
newFuncParamFlagDef(paramValue, pfOptional|pfRepeat, int64(1)),
|
NewFuncParamFlagDef(paramValue, PfOptional|PfRepeat, int64(1)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
28
func-os.go
28
func-os.go
@ -187,25 +187,25 @@ func readFileFunc(ctx ExprContext, name string, args []any) (result any, err err
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ImportOsFuncs(ctx ExprContext) {
|
func ImportOsFuncs(ctx ExprContext) {
|
||||||
ctx.RegisterFunc("openFile", newGolangFunctor(openFileFunc), typeHandle, []ExprFuncParam{
|
ctx.RegisterFunc("openFile", NewGolangFunctor(openFileFunc), TypeHandle, []ExprFuncParam{
|
||||||
newFuncParam(paramFilepath),
|
NewFuncParam(paramFilepath),
|
||||||
})
|
})
|
||||||
ctx.RegisterFunc("appendFile", newGolangFunctor(appendFileFunc), typeHandle, []ExprFuncParam{
|
ctx.RegisterFunc("appendFile", NewGolangFunctor(appendFileFunc), TypeHandle, []ExprFuncParam{
|
||||||
newFuncParam(paramFilepath),
|
NewFuncParam(paramFilepath),
|
||||||
})
|
})
|
||||||
ctx.RegisterFunc("createFile", newGolangFunctor(createFileFunc), typeHandle, []ExprFuncParam{
|
ctx.RegisterFunc("createFile", NewGolangFunctor(createFileFunc), TypeHandle, []ExprFuncParam{
|
||||||
newFuncParam(paramFilepath),
|
NewFuncParam(paramFilepath),
|
||||||
})
|
})
|
||||||
ctx.RegisterFunc("writeFile", newGolangFunctor(writeFileFunc), typeInt, []ExprFuncParam{
|
ctx.RegisterFunc("writeFile", NewGolangFunctor(writeFileFunc), TypeInt, []ExprFuncParam{
|
||||||
newFuncParam(typeHandle),
|
NewFuncParam(TypeHandle),
|
||||||
newFuncParamFlagDef(typeItem, pfOptional|pfRepeat, ""),
|
NewFuncParamFlagDef(TypeItem, PfOptional|PfRepeat, ""),
|
||||||
})
|
})
|
||||||
ctx.RegisterFunc("readFile", newGolangFunctor(readFileFunc), typeString, []ExprFuncParam{
|
ctx.RegisterFunc("readFile", NewGolangFunctor(readFileFunc), TypeString, []ExprFuncParam{
|
||||||
newFuncParam(typeHandle),
|
NewFuncParam(TypeHandle),
|
||||||
newFuncParamFlagDef("limitCh", pfOptional, "\n"),
|
NewFuncParamFlagDef("limitCh", PfOptional, "\n"),
|
||||||
})
|
})
|
||||||
ctx.RegisterFunc("closeFile", newGolangFunctor(closeFileFunc), typeBoolean, []ExprFuncParam{
|
ctx.RegisterFunc("closeFile", NewGolangFunctor(closeFileFunc), TypeBoolean, []ExprFuncParam{
|
||||||
newFuncParam(typeHandle),
|
NewFuncParam(TypeHandle),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ func doJoinStr(funcName string, sep string, it Iterator) (result any, err error)
|
|||||||
if s, ok := v.(string); ok {
|
if s, ok := v.(string); ok {
|
||||||
sb.WriteString(s)
|
sb.WriteString(s)
|
||||||
} else {
|
} else {
|
||||||
err = errExpectedGot(funcName, typeString, v)
|
err = errExpectedGot(funcName, TypeString, v)
|
||||||
return
|
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:]))
|
result, err = doJoinStr(name, sep, NewArrayIterator(args[1:]))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err = errWrongParamType(name, paramSeparator, typeString, args[0])
|
err = errWrongParamType(name, paramSeparator, TypeString, args[0])
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -63,7 +63,7 @@ func subStrFunc(ctx ExprContext, name string, args []any) (result any, err error
|
|||||||
var ok bool
|
var ok bool
|
||||||
|
|
||||||
if source, ok = args[0].(string); !ok {
|
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 {
|
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
|
var ok bool
|
||||||
|
|
||||||
if source, ok = args[0].(string); !ok {
|
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)
|
result = strings.TrimSpace(source)
|
||||||
return
|
return
|
||||||
@ -104,7 +104,7 @@ func startsWithStrFunc(ctx ExprContext, name string, args []any) (result any, er
|
|||||||
result = false
|
result = false
|
||||||
|
|
||||||
if source, ok = args[0].(string); !ok {
|
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:] {
|
for i, targetSpec := range args[1:] {
|
||||||
if target, ok := targetSpec.(string); ok {
|
if target, ok := targetSpec.(string); ok {
|
||||||
@ -127,7 +127,7 @@ func endsWithStrFunc(ctx ExprContext, name string, args []any) (result any, err
|
|||||||
result = false
|
result = false
|
||||||
|
|
||||||
if source, ok = args[0].(string); !ok {
|
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:] {
|
for i, targetSpec := range args[1:] {
|
||||||
if target, ok := targetSpec.(string); ok {
|
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
|
var ok bool
|
||||||
|
|
||||||
if source, ok = args[0].(string); !ok {
|
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 {
|
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
|
// Import above functions in the context
|
||||||
func ImportStringFuncs(ctx ExprContext) {
|
func ImportStringFuncs(ctx ExprContext) {
|
||||||
ctx.RegisterFunc("joinStr", newGolangFunctor(joinStrFunc), typeString, []ExprFuncParam{
|
ctx.RegisterFunc("joinStr", NewGolangFunctor(joinStrFunc), TypeString, []ExprFuncParam{
|
||||||
newFuncParam(paramSeparator),
|
NewFuncParam(paramSeparator),
|
||||||
newFuncParamFlag(paramItem, pfRepeat),
|
NewFuncParamFlag(paramItem, PfRepeat),
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx.RegisterFunc("subStr", newGolangFunctor(subStrFunc), typeString, []ExprFuncParam{
|
ctx.RegisterFunc("subStr", NewGolangFunctor(subStrFunc), TypeString, []ExprFuncParam{
|
||||||
newFuncParam(paramSource),
|
NewFuncParam(paramSource),
|
||||||
newFuncParamFlagDef(paramStart, pfOptional, int64(0)),
|
NewFuncParamFlagDef(paramStart, PfOptional, int64(0)),
|
||||||
newFuncParamFlagDef(paramCount, pfOptional, int64(-1)),
|
NewFuncParamFlagDef(paramCount, PfOptional, int64(-1)),
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx.RegisterFunc("splitStr", newGolangFunctor(splitStrFunc), "list of "+typeString, []ExprFuncParam{
|
ctx.RegisterFunc("splitStr", NewGolangFunctor(splitStrFunc), "list of "+TypeString, []ExprFuncParam{
|
||||||
newFuncParam(paramSource),
|
NewFuncParam(paramSource),
|
||||||
newFuncParamFlagDef(paramSeparator, pfOptional, ""),
|
NewFuncParamFlagDef(paramSeparator, PfOptional, ""),
|
||||||
newFuncParamFlagDef(paramCount, pfOptional, int64(-1)),
|
NewFuncParamFlagDef(paramCount, PfOptional, int64(-1)),
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx.RegisterFunc("trimStr", newGolangFunctor(trimStrFunc), typeString, []ExprFuncParam{
|
ctx.RegisterFunc("trimStr", NewGolangFunctor(trimStrFunc), TypeString, []ExprFuncParam{
|
||||||
newFuncParam(paramSource),
|
NewFuncParam(paramSource),
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx.RegisterFunc("startsWithStr", newGolangFunctor(startsWithStrFunc), typeBoolean, []ExprFuncParam{
|
ctx.RegisterFunc("startsWithStr", NewGolangFunctor(startsWithStrFunc), TypeBoolean, []ExprFuncParam{
|
||||||
newFuncParam(paramSource),
|
NewFuncParam(paramSource),
|
||||||
newFuncParam(paramPrefix),
|
NewFuncParam(paramPrefix),
|
||||||
newFuncParamFlag("other "+paramPrefix, pfRepeat),
|
NewFuncParamFlag("other "+paramPrefix, PfRepeat),
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx.RegisterFunc("endsWithStr", newGolangFunctor(endsWithStrFunc), typeBoolean, []ExprFuncParam{
|
ctx.RegisterFunc("endsWithStr", NewGolangFunctor(endsWithStrFunc), TypeBoolean, []ExprFuncParam{
|
||||||
newFuncParam(paramSource),
|
NewFuncParam(paramSource),
|
||||||
newFuncParam(paramSuffix),
|
NewFuncParam(paramSuffix),
|
||||||
newFuncParamFlag("other "+paramSuffix, pfRepeat),
|
NewFuncParamFlag("other "+paramSuffix, PfRepeat),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
20
function.go
20
function.go
@ -48,7 +48,7 @@ type golangFunctor struct {
|
|||||||
f FuncTemplate
|
f FuncTemplate
|
||||||
}
|
}
|
||||||
|
|
||||||
func newGolangFunctor(f FuncTemplate) *golangFunctor {
|
func NewGolangFunctor(f FuncTemplate) *golangFunctor {
|
||||||
return &golangFunctor{f: f}
|
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 {
|
if funcArg, ok := arg.(Functor); ok {
|
||||||
// ctx.RegisterFunc(p, functor, 0, -1)
|
// ctx.RegisterFunc(p, functor, 0, -1)
|
||||||
paramSpecs := funcArg.GetParams()
|
paramSpecs := funcArg.GetParams()
|
||||||
ctx.RegisterFunc(p.Name(), funcArg, typeAny, paramSpecs)
|
ctx.RegisterFunc(p.Name(), funcArg, TypeAny, paramSpecs)
|
||||||
} else {
|
} else {
|
||||||
ctx.UnsafeSetVar(p.Name(), arg)
|
ctx.UnsafeSetVar(p.Name(), arg)
|
||||||
}
|
}
|
||||||
@ -99,8 +99,8 @@ func (functor *exprFunctor) Invoke(ctx ExprContext, name string, args []any) (re
|
|||||||
type paramFlags uint16
|
type paramFlags uint16
|
||||||
|
|
||||||
const (
|
const (
|
||||||
pfOptional paramFlags = 1 << iota
|
PfOptional paramFlags = 1 << iota
|
||||||
pfRepeat
|
PfRepeat
|
||||||
)
|
)
|
||||||
|
|
||||||
type funcParamInfo struct {
|
type funcParamInfo struct {
|
||||||
@ -109,15 +109,15 @@ type funcParamInfo struct {
|
|||||||
defaultValue any
|
defaultValue any
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFuncParam(name string) ExprFuncParam {
|
func NewFuncParam(name string) ExprFuncParam {
|
||||||
return &funcParamInfo{name: name}
|
return &funcParamInfo{name: name}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFuncParamFlag(name string, flags paramFlags) ExprFuncParam {
|
func NewFuncParamFlag(name string, flags paramFlags) ExprFuncParam {
|
||||||
return &funcParamInfo{name: name, flags: flags}
|
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}
|
return &funcParamInfo{name: name, flags: flags, defaultValue: defValue}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,11 +130,11 @@ func (param *funcParamInfo) Type() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (param *funcParamInfo) IsOptional() bool {
|
func (param *funcParamInfo) IsOptional() bool {
|
||||||
return (param.flags & pfOptional) != 0
|
return (param.flags & PfOptional) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (param *funcParamInfo) IsRepeat() bool {
|
func (param *funcParamInfo) IsRepeat() bool {
|
||||||
return (param.flags & pfRepeat) != 0
|
return (param.flags & PfRepeat) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (param *funcParamInfo) DefaultValue() any {
|
func (param *funcParamInfo) DefaultValue() any {
|
||||||
@ -226,7 +226,7 @@ func (info *funcInfo) ToString(opt FmtOpt) string {
|
|||||||
if len(info.returnType) > 0 {
|
if len(info.returnType) > 0 {
|
||||||
sb.WriteString(info.returnType)
|
sb.WriteString(info.returnType)
|
||||||
} else {
|
} else {
|
||||||
sb.WriteString(typeAny)
|
sb.WriteString(TypeAny)
|
||||||
}
|
}
|
||||||
sb.WriteString(" {<body>}")
|
sb.WriteString(" {<body>}")
|
||||||
return sb.String()
|
return sb.String()
|
||||||
|
@ -38,10 +38,10 @@ func EvalStringV(source string, args []Arg) (result any, err error) {
|
|||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
if isFunc(arg.Value) {
|
if isFunc(arg.Value) {
|
||||||
if f, ok := arg.Value.(FuncTemplate); ok {
|
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, 0, -1)
|
||||||
ctx.RegisterFunc(arg.Name, functor, typeAny, []ExprFuncParam{
|
ctx.RegisterFunc(arg.Name, functor, TypeAny, []ExprFuncParam{
|
||||||
newFuncParamFlagDef(paramValue, pfOptional|pfRepeat, 0),
|
NewFuncParamFlagDef(paramValue, PfOptional|PfRepeat, 0),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("invalid function specification: %q", arg.Name)
|
err = fmt.Errorf("invalid function specification: %q", arg.Name)
|
||||||
|
79
import-utils.go
Normal file
79
import-utils.go
Normal file
@ -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
|
||||||
|
}
|
@ -91,12 +91,12 @@ func evalFuncDef(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
var defValue any
|
var defValue any
|
||||||
flags := paramFlags(0)
|
flags := paramFlags(0)
|
||||||
if len(param.children) > 0 {
|
if len(param.children) > 0 {
|
||||||
flags |= pfOptional
|
flags |= PfOptional
|
||||||
if defValue, err = param.children[0].compute(ctx); err != nil {
|
if defValue, err = param.children[0].compute(ctx); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
info := newFuncParamFlagDef(param.source(), flags, defValue)
|
info := NewFuncParamFlagDef(param.source(), flags, defValue)
|
||||||
paramList = append(paramList, info)
|
paramList = append(paramList, info)
|
||||||
}
|
}
|
||||||
v = newExprFunctor(expr, paramList, ctx)
|
v = newExprFunctor(expr, paramList, ctx)
|
||||||
|
@ -81,7 +81,7 @@ func evalAssign(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
} else if funcDef, ok := functor.(*exprFunctor); ok {
|
} else if funcDef, ok := functor.(*exprFunctor); ok {
|
||||||
paramSpecs := ForAll(funcDef.params, func(p ExprFuncParam) ExprFuncParam { return p })
|
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 {
|
} else {
|
||||||
err = self.Errorf("unknown function %s()", rightChild.source())
|
err = self.Errorf("unknown function %s()", rightChild.source())
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
} else if rightValue, err = self.children[1].compute(ctx); err == nil {
|
||||||
if functor, ok := rightValue.(Functor); ok {
|
if functor, ok := rightValue.(Functor); ok {
|
||||||
//ctx.RegisterFunc(leftTerm.source(), functor, 0, -1)
|
//ctx.RegisterFunc(leftTerm.source(), functor, 0, -1)
|
||||||
ctx.RegisterFunc(leftTerm.source(), functor, typeAny, []ExprFuncParam{
|
ctx.RegisterFunc(leftTerm.source(), functor, TypeAny, []ExprFuncParam{
|
||||||
newFuncParamFlag(paramValue, pfOptional|pfRepeat),
|
NewFuncParamFlag(paramValue, PfOptional|PfRepeat),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
v = rightValue
|
v = rightValue
|
||||||
|
@ -12,7 +12,7 @@ type intPair struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *intPair) TypeName() string {
|
func (p *intPair) TypeName() string {
|
||||||
return typePair
|
return TypePair
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *intPair) ToString(opt FmtOpt) string {
|
func (p *intPair) ToString(opt FmtOpt) string {
|
||||||
|
@ -43,7 +43,7 @@ func dummy(ctx ExprContext, name string, args []any) (result any, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFunctionToStringSimple(t *testing.T) {
|
func TestFunctionToStringSimple(t *testing.T) {
|
||||||
source := newGolangFunctor(dummy)
|
source := NewGolangFunctor(dummy)
|
||||||
want := "func() {<body>}"
|
want := "func() {<body>}"
|
||||||
got := source.ToString(0)
|
got := source.ToString(0)
|
||||||
if got != want {
|
if got != want {
|
||||||
@ -52,7 +52,7 @@ func TestFunctionToStringSimple(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFunctionGetFunc(t *testing.T) {
|
func TestFunctionGetFunc(t *testing.T) {
|
||||||
source := newGolangFunctor(dummy)
|
source := NewGolangFunctor(dummy)
|
||||||
want := ExprFunc(nil)
|
want := ExprFunc(nil)
|
||||||
got := source.GetFunc()
|
got := source.GetFunc()
|
||||||
if got != want {
|
if got != want {
|
||||||
|
Loading…
Reference in New Issue
Block a user