some identier exported; new file import-utils.go
This commit is contained in:
parent
8144122d2c
commit
c100cf349d
@ -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"
|
||||
)
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
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) {
|
||||
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),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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)),
|
||||
})
|
||||
}
|
||||
|
||||
|
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) {
|
||||
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),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
20
function.go
20
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(" {<body>}")
|
||||
return sb.String()
|
||||
|
@ -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)
|
||||
|
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
|
||||
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)
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -12,7 +12,7 @@ type intPair struct {
|
||||
}
|
||||
|
||||
func (p *intPair) TypeName() string {
|
||||
return typePair
|
||||
return TypePair
|
||||
}
|
||||
|
||||
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) {
|
||||
source := newGolangFunctor(dummy)
|
||||
source := NewGolangFunctor(dummy)
|
||||
want := "func() {<body>}"
|
||||
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user