Compare commits
23 Commits
v0.22.0
...
1a772597cb
| Author | SHA1 | Date | |
|---|---|---|---|
| 1a772597cb | |||
| 33b3e1fc29 | |||
| 4e3f5cfbc6 | |||
| e35d4e3f70 | |||
| b4529499d6 | |||
| 7745dc24e2 | |||
| be25385d02 | |||
| 79889cd8e1 | |||
| 234759158c | |||
| 00fda29606 | |||
| 2a2840bdf2 | |||
| 06373f5126 | |||
| e69dad5fb5 | |||
| 7459057df9 | |||
| 176969c956 | |||
| cde2efacf1 | |||
| d7a7b3218c | |||
| be3bb12f28 | |||
| 032916d4fa | |||
| f3cc0cc7ad | |||
| 905337f963 | |||
| 9a95a837f6 | |||
| 8547248ea2 |
@@ -19,6 +19,10 @@ func NewAst() *ast {
|
||||
return &ast{}
|
||||
}
|
||||
|
||||
func (expr *ast) TypeName() string {
|
||||
return "Expression"
|
||||
}
|
||||
|
||||
func (expr *ast) ToForest() {
|
||||
if expr.root != nil {
|
||||
if expr.forest == nil {
|
||||
|
||||
@@ -29,6 +29,10 @@ func newExprFunctor(e Expr, params []ExprFuncParam, ctx ExprContext) *exprFuncto
|
||||
return &exprFunctor{expr: e, params: params, defCtx: defCtx}
|
||||
}
|
||||
|
||||
func (functor *exprFunctor) TypeName() string {
|
||||
return "ExprFunctor"
|
||||
}
|
||||
|
||||
func (functor *exprFunctor) GetDefinitionContext() ExprContext {
|
||||
return functor.defCtx
|
||||
}
|
||||
|
||||
@@ -14,6 +14,10 @@ func NewGolangFunctor(f FuncTemplate) *golangFunctor {
|
||||
return &golangFunctor{f: f}
|
||||
}
|
||||
|
||||
func (functor *golangFunctor) TypeName() string {
|
||||
return "GoFunctor"
|
||||
}
|
||||
|
||||
func (functor *golangFunctor) Invoke(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
return functor.f(ctx, name, args)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// builtin-iterator.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
const (
|
||||
iterParamOperator = "operator"
|
||||
iterParamVars = "vars"
|
||||
iterVarStatus = "status"
|
||||
)
|
||||
|
||||
func parseRunArgs(localCtx ExprContext, args []any) (it Iterator, op Functor, err error) {
|
||||
var ok bool
|
||||
|
||||
if it, ok = args[0].(Iterator); !ok {
|
||||
err = fmt.Errorf("paramter %q must be an iterator, passed %v [%s]", ParamIterator, args[0], TypeName(args[0]))
|
||||
return
|
||||
}
|
||||
|
||||
if len(args) > 1 {
|
||||
if op, ok = args[1].(Functor); !ok || op == nil {
|
||||
err = fmt.Errorf("paramter %q must be a function, passed %v [%s]", iterParamOperator, args[1], TypeName(args[1]))
|
||||
return
|
||||
}
|
||||
if len(args) > 2 {
|
||||
var vars *DictType
|
||||
if vars, ok = args[2].(*DictType); !ok || vars == nil {
|
||||
err = fmt.Errorf("paramter %q must be a dictionary, passed %v [%s]", iterParamVars, args[2], TypeName(args[2]))
|
||||
return
|
||||
}
|
||||
for key, value := range *vars {
|
||||
var varName string
|
||||
if varName, ok = key.(string); ok {
|
||||
localCtx.UnsafeSetVar(varName, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func runFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
var it Iterator
|
||||
var ok bool
|
||||
var op Functor
|
||||
var v any
|
||||
var usingDefaultOp = false
|
||||
var params []any
|
||||
var item any
|
||||
|
||||
localCtx := ctx.Clone()
|
||||
localCtx.UnsafeSetVar(iterVarStatus, nil)
|
||||
|
||||
if it, op, err = parseRunArgs(localCtx, args); err != nil {
|
||||
return
|
||||
} else if op == nil {
|
||||
op = NewGolangFunctor(printLnFunc)
|
||||
usingDefaultOp = true
|
||||
}
|
||||
|
||||
for item, err = it.Next(); err == nil; item, err = it.Next() {
|
||||
if usingDefaultOp {
|
||||
params = []any{item}
|
||||
} else {
|
||||
params = []any{it.Index(), item}
|
||||
}
|
||||
|
||||
if v, err = op.Invoke(localCtx, iterParamOperator, params); err != nil {
|
||||
break
|
||||
} else {
|
||||
var success bool
|
||||
if success, ok = ToBool(v); !success || !ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err == io.EOF {
|
||||
err = nil
|
||||
}
|
||||
if err == nil {
|
||||
result, _ = localCtx.GetVar(iterVarStatus)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ImportIterFuncs(ctx ExprContext) {
|
||||
ctx.RegisterFunc("run", NewGolangFunctor(runFunc), TypeAny, []ExprFuncParam{
|
||||
NewFuncParam(ParamIterator),
|
||||
NewFuncParamFlag(iterParamOperator, PfOptional),
|
||||
NewFuncParamFlag(iterParamVars, PfOptional),
|
||||
})
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterBuiltinModule("iterator", ImportIterFuncs, "Iterator helper functions")
|
||||
}
|
||||
@@ -34,8 +34,8 @@ func doAdd(ctx ExprContext, name string, it Iterator, count, level int) (result
|
||||
if v, err = doAdd(ctx, name, subIter, count, level); err != nil {
|
||||
break
|
||||
}
|
||||
if extIter, ok := v.(ExtIterator); ok && extIter.HasOperation(cleanName) {
|
||||
if _, err = extIter.CallOperation(cleanName, nil); err != nil {
|
||||
if extIter, ok := v.(ExtIterator); ok && extIter.HasOperation(CleanName) {
|
||||
if _, err = extIter.CallOperation(CleanName, nil); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -107,8 +107,8 @@ func doMul(ctx ExprContext, name string, it Iterator, count, level int) (result
|
||||
if v, err = doMul(ctx, name, subIter, count, level); err != nil {
|
||||
break
|
||||
}
|
||||
if extIter, ok := v.(ExtIterator); ok && extIter.HasOperation(cleanName) {
|
||||
if _, err = extIter.CallOperation(cleanName, nil); err != nil {
|
||||
if extIter, ok := v.(ExtIterator); ok && extIter.HasOperation(CleanName) {
|
||||
if _, err = extIter.CallOperation(CleanName, nil); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ const (
|
||||
ParamEllipsis = "..."
|
||||
ParamFilepath = "filepath"
|
||||
ParamDirpath = "dirpath"
|
||||
ParamIterator = "iterator"
|
||||
)
|
||||
|
||||
// to be moved in its own source file
|
||||
|
||||
+64
-35
@@ -20,7 +20,7 @@ type dataCursor struct {
|
||||
currentFunc Functor
|
||||
}
|
||||
|
||||
func newDataCursor(ctx ExprContext, ds map[string]Functor) (dc *dataCursor) {
|
||||
func NewDataCursor(ctx ExprContext, ds map[string]Functor) (dc *dataCursor) {
|
||||
dc = &dataCursor{
|
||||
ds: ds,
|
||||
index: -1,
|
||||
@@ -29,6 +29,10 @@ func newDataCursor(ctx ExprContext, ds map[string]Functor) (dc *dataCursor) {
|
||||
return
|
||||
}
|
||||
|
||||
func (dc *dataCursor) Context() ExprContext {
|
||||
return dc.ctx
|
||||
}
|
||||
|
||||
func (dc *dataCursor) TypeName() string {
|
||||
return "DataCursor"
|
||||
}
|
||||
@@ -62,7 +66,7 @@ func (dc *dataCursor) String() string {
|
||||
}
|
||||
|
||||
func (dc *dataCursor) HasOperation(name string) (exists bool) {
|
||||
exists = name == indexName
|
||||
exists = name == IndexName
|
||||
if !exists {
|
||||
f, ok := dc.ds[name]
|
||||
exists = ok && isFunctor(f)
|
||||
@@ -71,7 +75,7 @@ func (dc *dataCursor) HasOperation(name string) (exists bool) {
|
||||
}
|
||||
|
||||
func (dc *dataCursor) CallOperation(name string, args []any) (value any, err error) {
|
||||
if name == indexName {
|
||||
if name == IndexName {
|
||||
value = int64(dc.Index())
|
||||
} else if functor, ok := dc.ds[name]; ok && isFunctor(functor) {
|
||||
if functor == dc.cleanFunc {
|
||||
@@ -93,7 +97,7 @@ func (dc *dataCursor) Reset() (success bool, err error) {
|
||||
if dc.resetFunc != nil {
|
||||
if dc.resource != nil {
|
||||
ctx := cloneContext(dc.ctx)
|
||||
if _, err = dc.resetFunc.Invoke(ctx, resetName, []any{dc.resource}); err == nil {
|
||||
if _, err = dc.resetFunc.Invoke(ctx, ResetName, []any{dc.resource}); err == nil {
|
||||
dc.index = -1
|
||||
}
|
||||
exportObjects(dc.ctx, ctx)
|
||||
@@ -101,7 +105,7 @@ func (dc *dataCursor) Reset() (success bool, err error) {
|
||||
err = errInvalidDataSource()
|
||||
}
|
||||
} else {
|
||||
err = errNoOperation(resetName)
|
||||
err = errNoOperation(ResetName)
|
||||
}
|
||||
success = err == nil
|
||||
return
|
||||
@@ -111,7 +115,7 @@ func (dc *dataCursor) Clean() (success bool, err error) {
|
||||
if dc.cleanFunc != nil {
|
||||
if dc.resource != nil {
|
||||
ctx := cloneContext(dc.ctx)
|
||||
_, err = dc.cleanFunc.Invoke(ctx, cleanName, []any{dc.resource})
|
||||
_, err = dc.cleanFunc.Invoke(ctx, CleanName, []any{dc.resource})
|
||||
dc.resource = nil
|
||||
exportObjects(dc.ctx, ctx)
|
||||
} else {
|
||||
@@ -126,61 +130,86 @@ func (dc *dataCursor) Clean() (success bool, err error) {
|
||||
|
||||
func (dc *dataCursor) Current() (item any, err error) { // must return io.EOF at the last item
|
||||
ctx := cloneContext(dc.ctx)
|
||||
if item, err = dc.currentFunc.Invoke(ctx, currentName, []any{}); err == nil && item == nil {
|
||||
if item, err = dc.currentFunc.Invoke(ctx, CurrentName, []any{}); err == nil && item == nil {
|
||||
err = io.EOF
|
||||
}
|
||||
exportObjects(dc.ctx, ctx)
|
||||
return
|
||||
}
|
||||
|
||||
func (dc *dataCursor) _Next() (item any, err error) { // must return io.EOF after the last item
|
||||
if dc.resource != nil {
|
||||
// func (dc *dataCursor) _Next() (item any, err error) { // must return io.EOF after the last item
|
||||
// if dc.resource != nil {
|
||||
// ctx := cloneContext(dc.ctx)
|
||||
// // fmt.Printf("Entering Inner-Ctx [%p]: %s\n", ctx, CtxToString(ctx, 0))
|
||||
// if item, err = dc.nextFunc.Invoke(ctx, nextName, []any{dc.resource}); err == nil {
|
||||
// if item == nil {
|
||||
// err = io.EOF
|
||||
// } else {
|
||||
// dc.index++
|
||||
// }
|
||||
// }
|
||||
// // fmt.Printf("Exiting Inner-Ctx [%p]: %s\n", ctx, CtxToString(ctx, 0))
|
||||
// exportObjects(dc.ctx, ctx)
|
||||
// // fmt.Printf("Outer-Ctx [%p]: %s\n", dc.ctx, CtxToString(dc.ctx, 0))
|
||||
// } else {
|
||||
// err = errInvalidDataSource()
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
// func (dc *dataCursor) _filter(item any) (filterdItem any, err error) {
|
||||
// if filter, ok := dc.ds[filterName]; ok {
|
||||
// ctx := cloneContext(dc.ctx)
|
||||
// filterdItem, err = filter.Invoke(ctx, filterName, []any{item, dc.index})
|
||||
// } else {
|
||||
// filterdItem = item
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
func (dc *dataCursor) checkFilter(filter Functor, item any) (accepted bool, err error) {
|
||||
var v any
|
||||
var ok bool
|
||||
ctx := cloneContext(dc.ctx)
|
||||
// fmt.Printf("Entering Inner-Ctx [%p]: %s\n", ctx, CtxToString(ctx, 0))
|
||||
if item, err = dc.nextFunc.Invoke(ctx, nextName, []any{dc.resource}); err == nil {
|
||||
if item == nil {
|
||||
err = io.EOF
|
||||
} else {
|
||||
dc.index++
|
||||
if v, err = filter.Invoke(ctx, FilterName, []any{item, dc.index}); err == nil && v != nil {
|
||||
if accepted, ok = v.(bool); !ok {
|
||||
accepted = true // NOTE: A non-boolean value that is not nil means the item has been accepted
|
||||
}
|
||||
}
|
||||
// fmt.Printf("Exiting Inner-Ctx [%p]: %s\n", ctx, CtxToString(ctx, 0))
|
||||
exportObjects(dc.ctx, ctx)
|
||||
// fmt.Printf("Outer-Ctx [%p]: %s\n", dc.ctx, CtxToString(dc.ctx, 0))
|
||||
} else {
|
||||
err = errInvalidDataSource()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const filterName = "filter"
|
||||
func (dc *dataCursor) filter(item any) (filterdItem any, err error) {
|
||||
if filter, ok := dc.ds[filterName]; ok {
|
||||
func (dc *dataCursor) mapItem(mapper Functor, item any) (mappedItem any, err error) {
|
||||
ctx := cloneContext(dc.ctx)
|
||||
filterdItem, err = filter.Invoke(ctx, filterName, []any{item, dc.index});
|
||||
} else {
|
||||
filterdItem = item
|
||||
}
|
||||
mappedItem, err = mapper.Invoke(ctx, MapName, []any{item, dc.index});
|
||||
return
|
||||
}
|
||||
|
||||
func (dc *dataCursor) Next() (item any, err error) { // must return io.EOF after the last item
|
||||
var accepted bool
|
||||
if dc.resource != nil {
|
||||
ctx := cloneContext(dc.ctx)
|
||||
// fmt.Printf("Entering Inner-Ctx [%p]: %s\n", ctx, CtxToString(ctx, 0))
|
||||
filter := dc.ds[FilterName]
|
||||
mapper := dc.ds[MapName]
|
||||
|
||||
for item == nil && err == nil {
|
||||
if item, err = dc.nextFunc.Invoke(ctx, nextName, []any{dc.resource}); err == nil {
|
||||
ctx := cloneContext(dc.ctx)
|
||||
if item, err = dc.nextFunc.Invoke(ctx, NextName, []any{dc.resource}); err == nil {
|
||||
if item == nil {
|
||||
err = io.EOF
|
||||
} else {
|
||||
dc.index++
|
||||
item, err = dc.filter(item)
|
||||
if filter != nil {
|
||||
if accepted, err = dc.checkFilter(filter, item); err != nil || !accepted {
|
||||
item = nil
|
||||
}
|
||||
}
|
||||
if item != nil && mapper != nil {
|
||||
item, err = dc.mapItem(mapper, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
// fmt.Printf("Exiting Inner-Ctx [%p]: %s\n", ctx, CtxToString(ctx, 0))
|
||||
exportObjects(dc.ctx, ctx)
|
||||
// fmt.Printf("Outer-Ctx [%p]: %s\n", dc.ctx, CtxToString(dc.ctx, 0))
|
||||
}
|
||||
} else {
|
||||
err = errInvalidDataSource()
|
||||
}
|
||||
|
||||
@@ -18,7 +18,22 @@ func MakeDict() (dict *DictType) {
|
||||
return
|
||||
}
|
||||
|
||||
func NewDict(dictAny map[any]any) (dict *DictType) {
|
||||
var d DictType
|
||||
if dictAny != nil {
|
||||
d = make(DictType, len(dictAny))
|
||||
for i, item := range dictAny {
|
||||
d[i] = item
|
||||
}
|
||||
} else {
|
||||
d = make(DictType)
|
||||
}
|
||||
dict = &d
|
||||
return
|
||||
}
|
||||
|
||||
func newDict(dictAny map[any]*term) (dict *DictType) {
|
||||
// TODO Change wi a call to NewDict()
|
||||
var d DictType
|
||||
if dictAny != nil {
|
||||
d = make(DictType, len(dictAny))
|
||||
|
||||
@@ -14,8 +14,15 @@ type ExprContext interface {
|
||||
GetLast() any
|
||||
SetVar(varName string, value any)
|
||||
UnsafeSetVar(varName string, value any)
|
||||
|
||||
EnumVars(func(name string) (accept bool)) (varNames []string)
|
||||
VarCount() int
|
||||
DeleteVar(varName string)
|
||||
|
||||
EnumFuncs(func(name string) (accept bool)) (funcNames []string)
|
||||
FuncCount() int
|
||||
DeleteFunc(funcName string)
|
||||
|
||||
GetFuncInfo(name string) (item ExprFunc, exists bool)
|
||||
Call(name string, args []any) (result any, err error)
|
||||
RegisterFuncInfo(info ExprFunc)
|
||||
|
||||
@@ -6,6 +6,7 @@ package expr
|
||||
|
||||
// ---- Functor interface
|
||||
type Functor interface {
|
||||
Typer
|
||||
Invoke(ctx ExprContext, name string, args []any) (result any, err error)
|
||||
SetFunc(info ExprFunc)
|
||||
GetFunc() ExprFunc
|
||||
|
||||
@@ -6,6 +6,7 @@ package expr
|
||||
|
||||
// ----Expression interface
|
||||
type Expr interface {
|
||||
Typer
|
||||
Eval(ctx ExprContext) (result any, err error)
|
||||
String() string
|
||||
}
|
||||
|
||||
@@ -75,7 +75,11 @@ func isFile(filePath string) bool {
|
||||
}
|
||||
|
||||
func searchAmongPath(filename string, dirList []string) (filePath string) {
|
||||
var err error
|
||||
for _, dir := range dirList {
|
||||
if dir, err = ExpandPath(dir); err != nil {
|
||||
continue
|
||||
}
|
||||
if fullPath := path.Join(dir, filename); isFile(fullPath) {
|
||||
filePath = fullPath
|
||||
break
|
||||
@@ -90,6 +94,10 @@ func isPathRelative(filePath string) bool {
|
||||
}
|
||||
|
||||
func makeFilepath(filename string, dirList []string) (filePath string, err error) {
|
||||
if filename, err = ExpandPath(filename); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if path.IsAbs(filename) || isPathRelative(filename) {
|
||||
if isFile(filename) {
|
||||
filePath = filename
|
||||
|
||||
+9
-7
@@ -12,13 +12,15 @@ import (
|
||||
// Operator names
|
||||
|
||||
const (
|
||||
initName = "init"
|
||||
cleanName = "clean"
|
||||
resetName = "reset"
|
||||
nextName = "next"
|
||||
currentName = "current"
|
||||
indexName = "index"
|
||||
countName = "count"
|
||||
InitName = "init"
|
||||
CleanName = "clean"
|
||||
ResetName = "reset"
|
||||
NextName = "next"
|
||||
CurrentName = "current"
|
||||
IndexName = "index"
|
||||
CountName = "count"
|
||||
FilterName = "filter"
|
||||
MapName = "map"
|
||||
)
|
||||
|
||||
type Iterator interface {
|
||||
|
||||
+8
-4
@@ -90,17 +90,21 @@ func (it *ListIterator) TypeName() string {
|
||||
}
|
||||
|
||||
func (it *ListIterator) HasOperation(name string) bool {
|
||||
yes := name == resetName || name == indexName || name == countName
|
||||
yes := name == NextName || name == ResetName || name == IndexName || name == CountName || name == CurrentName
|
||||
return yes
|
||||
}
|
||||
|
||||
func (it *ListIterator) CallOperation(name string, args []any) (v any, err error) {
|
||||
switch name {
|
||||
case resetName:
|
||||
case NextName:
|
||||
v, err = it.Next()
|
||||
case ResetName:
|
||||
v, err = it.Reset()
|
||||
case indexName:
|
||||
case IndexName:
|
||||
v = int64(it.Index())
|
||||
case countName:
|
||||
case CurrentName:
|
||||
v, err = it.Current()
|
||||
case CountName:
|
||||
v = it.count
|
||||
default:
|
||||
err = errNoOperation(name)
|
||||
|
||||
+8
-8
@@ -66,7 +66,7 @@ func evalFirstChild(ctx ExprContext, iteratorTerm *term) (value any, err error)
|
||||
|
||||
func getDataSourceDict(iteratorTerm *term, firstChildValue any) (ds map[string]Functor, err error) {
|
||||
if dictAny, ok := firstChildValue.(*DictType); ok {
|
||||
requiredFields := []string{currentName, nextName}
|
||||
requiredFields := []string{CurrentName, NextName}
|
||||
fieldsMask := 0b11
|
||||
foundFields := 0
|
||||
ds = make(map[string]Functor)
|
||||
@@ -108,8 +108,8 @@ func evalIterator(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
}
|
||||
|
||||
if ds != nil {
|
||||
dc := newDataCursor(ctx, ds)
|
||||
if initFunc, exists := ds[initName]; exists && initFunc != nil {
|
||||
dc := NewDataCursor(ctx, ds)
|
||||
if initFunc, exists := ds[InitName]; exists && initFunc != nil {
|
||||
var args []any
|
||||
if len(opTerm.children) > 1 {
|
||||
if args, err = evalTermArray(ctx, opTerm.children[1:]); err != nil {
|
||||
@@ -120,16 +120,16 @@ func evalIterator(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
}
|
||||
|
||||
initCtx := dc.ctx.Clone()
|
||||
if dc.resource, err = initFunc.Invoke(initCtx, initName, args); err != nil {
|
||||
if dc.resource, err = initFunc.Invoke(initCtx, InitName, args); err != nil {
|
||||
return
|
||||
}
|
||||
exportObjects(dc.ctx, initCtx)
|
||||
}
|
||||
|
||||
dc.nextFunc = ds[nextName]
|
||||
dc.currentFunc = ds[currentName]
|
||||
dc.cleanFunc = ds[cleanName]
|
||||
dc.resetFunc = ds[resetName]
|
||||
dc.nextFunc = ds[NextName]
|
||||
dc.currentFunc = ds[CurrentName]
|
||||
dc.cleanFunc = ds[CleanName]
|
||||
dc.resetFunc = ds[ResetName]
|
||||
|
||||
v = dc
|
||||
} else if list, ok := firstChildValue.(*ListType); ok {
|
||||
|
||||
+3
-3
@@ -11,7 +11,7 @@ func newDefaultTerm(tk *Token) (inst *term) {
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priCoalesce,
|
||||
priority: priDefault,
|
||||
evalFunc: evalDefault,
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ func newAlternateTerm(tk *Token) (inst *term) {
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priCoalesce,
|
||||
priority: priDefault,
|
||||
evalFunc: evalAlternate,
|
||||
}
|
||||
}
|
||||
@@ -81,7 +81,7 @@ func newDefaultAssignTerm(tk *Token) (inst *term) {
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priCoalesce,
|
||||
priority: priDefault,
|
||||
evalFunc: evalAssignDefault,
|
||||
}
|
||||
}
|
||||
|
||||
+5
-4
@@ -25,8 +25,8 @@ func evalInclude(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
|
||||
count := 0
|
||||
if IsList(childValue) {
|
||||
list, _ := childValue.([]any)
|
||||
for i, filePathSpec := range list {
|
||||
list, _ := childValue.(*ListType)
|
||||
for i, filePathSpec := range *list {
|
||||
if filePath, ok := filePathSpec.(string); ok {
|
||||
if v, err = EvalFile(ctx, filePath); err == nil {
|
||||
count++
|
||||
@@ -47,8 +47,9 @@ func evalInclude(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
} else {
|
||||
err = opTerm.errIncompatibleType(childValue)
|
||||
}
|
||||
if err == nil {
|
||||
v = count
|
||||
if err != nil {
|
||||
//v = count
|
||||
v = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -33,5 +33,6 @@ func evalIterValue(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
|
||||
// init
|
||||
func init() {
|
||||
registerTermConstructor(SymOpenClosedRound, newIterValueTerm)
|
||||
// registerTermConstructor(SymOpenClosedRound, newIterValueTerm)
|
||||
registerTermConstructor(SymCaret, newIterValueTerm)
|
||||
}
|
||||
|
||||
+2
-2
@@ -34,8 +34,8 @@ func evalLength(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
m, _ := childValue.(*DictType)
|
||||
v = int64(len(*m))
|
||||
} else if it, ok := childValue.(Iterator); ok {
|
||||
if extIt, ok := childValue.(ExtIterator); ok && extIt.HasOperation(countName) {
|
||||
count, _ := extIt.CallOperation(countName, nil)
|
||||
if extIt, ok := childValue.(ExtIterator); ok && extIt.HasOperation(CountName) {
|
||||
count, _ := extIt.CallOperation(CountName, nil)
|
||||
v, _ = ToGoInt(count, "")
|
||||
} else {
|
||||
v = int64(it.Index() + 1)
|
||||
|
||||
+2
-24
@@ -4,10 +4,6 @@
|
||||
// operator-plugin.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
//-------- plugin term
|
||||
|
||||
func newPluginTerm(tk *Token) (inst *term) {
|
||||
@@ -22,31 +18,13 @@ func newPluginTerm(tk *Token) (inst *term) {
|
||||
|
||||
func evalPlugin(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
var childValue any
|
||||
var moduleSpec any
|
||||
var count int
|
||||
|
||||
if childValue, err = opTerm.evalPrefix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
dirList := buildSearchDirList("plugin", ENV_EXPR_PLUGIN_PATH)
|
||||
count := 0
|
||||
it := NewAnyIterator(childValue)
|
||||
for moduleSpec, err = it.Next(); err == nil; moduleSpec, err = it.Next() {
|
||||
if module, ok := moduleSpec.(string); ok {
|
||||
if err = importPlugin(dirList, module); err != nil {
|
||||
break
|
||||
}
|
||||
count++
|
||||
} else {
|
||||
err = opTerm.Errorf("expected string as item nr %d, got %s", it.Index()+1, TypeName(moduleSpec))
|
||||
break
|
||||
}
|
||||
}
|
||||
if err == io.EOF {
|
||||
err = nil
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
if count, err = importPluginFromSearchPath(childValue); err == nil {
|
||||
v = int64(count)
|
||||
}
|
||||
return
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// operator-unset.go
|
||||
package expr
|
||||
|
||||
import "strings"
|
||||
|
||||
//-------- unset term
|
||||
|
||||
func newUnsetTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 1),
|
||||
position: posPrefix,
|
||||
priority: priSign,
|
||||
evalFunc: evalUnset,
|
||||
}
|
||||
}
|
||||
|
||||
func deleteContextItem(ctx ExprContext, opTerm *term, item any) (deleted bool, err error) {
|
||||
if name, ok := item.(string); ok {
|
||||
var size int
|
||||
if strings.HasSuffix(name, "()") {
|
||||
size = ctx.FuncCount()
|
||||
ctx.DeleteFunc(strings.TrimRight(name, "()"))
|
||||
deleted = ctx.FuncCount() < size
|
||||
} else {
|
||||
size = ctx.VarCount()
|
||||
ctx.DeleteVar(name)
|
||||
deleted = ctx.VarCount() < size
|
||||
}
|
||||
} else {
|
||||
err = opTerm.errIncompatibleType(item)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func evalUnset(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
var childValue any
|
||||
var deleted bool
|
||||
|
||||
if childValue, err = opTerm.evalPrefix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
count := 0
|
||||
if IsList(childValue) {
|
||||
list, _ := childValue.(*ListType)
|
||||
for _, item := range *list {
|
||||
if deleted, err = deleteContextItem(ctx, opTerm, item); err != nil {
|
||||
break
|
||||
} else if deleted {
|
||||
count++
|
||||
}
|
||||
}
|
||||
} else if deleted, err = deleteContextItem(ctx, opTerm, childValue); err == nil && deleted {
|
||||
count++
|
||||
}
|
||||
if err == nil {
|
||||
v = int64(count)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// init
|
||||
func init() {
|
||||
registerTermConstructor(SymKwUnset, newUnsetTerm)
|
||||
}
|
||||
+24
@@ -6,6 +6,7 @@ package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"plugin"
|
||||
"strings"
|
||||
@@ -90,6 +91,29 @@ func importPlugin( /*ctx ExprContext,*/ dirList []string, name string) (err erro
|
||||
return
|
||||
}
|
||||
|
||||
func importPluginFromSearchPath(name any) (count int, err error) {
|
||||
var moduleSpec any
|
||||
|
||||
dirList := buildSearchDirList("plugin", ENV_EXPR_PLUGIN_PATH)
|
||||
count = 0
|
||||
it := NewAnyIterator(name)
|
||||
for moduleSpec, err = it.Next(); err == nil; moduleSpec, err = it.Next() {
|
||||
if module, ok := moduleSpec.(string); ok {
|
||||
if err = importPlugin(dirList, module); err != nil {
|
||||
break
|
||||
}
|
||||
count++
|
||||
} else {
|
||||
err = fmt.Errorf("expected string as item nr %d, got %s", it.Index()+1, TypeName(moduleSpec))
|
||||
break
|
||||
}
|
||||
}
|
||||
if err == io.EOF {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func loadModules(dirList []string, moduleNames []string) (err error) {
|
||||
for _, name := range moduleNames {
|
||||
if err1 := importPlugin(dirList, name); err1 != nil {
|
||||
|
||||
+4
-4
@@ -269,11 +269,11 @@ func (scanner *scanner) fetchNextToken() (tk *Token) {
|
||||
tk = scanner.makeToken(SymDollar, ch)
|
||||
}
|
||||
case '(':
|
||||
if next, _ := scanner.peek(); next == ')' {
|
||||
tk = scanner.moveOn(SymOpenClosedRound, ch, next)
|
||||
} else {
|
||||
// if next, _ := scanner.peek(); next == ')' {
|
||||
// tk = scanner.moveOn(SymOpenClosedRound, ch, next)
|
||||
// } else {
|
||||
tk = scanner.makeToken(SymOpenRound, ch)
|
||||
}
|
||||
// }
|
||||
case ')':
|
||||
tk = scanner.makeToken(SymClosedRound, ch)
|
||||
case '[':
|
||||
|
||||
@@ -132,6 +132,14 @@ func (ctx *SimpleStore) EnumVars(acceptor func(name string) (accept bool)) (varN
|
||||
return
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) VarCount() int {
|
||||
return len(ctx.varStore)
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) DeleteVar(varName string) {
|
||||
delete(ctx.varStore, varName)
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) GetFuncInfo(name string) (info ExprFunc, exists bool) {
|
||||
info, exists = ctx.funcStore[name]
|
||||
return
|
||||
@@ -163,6 +171,14 @@ func (ctx *SimpleStore) EnumFuncs(acceptor func(name string) (accept bool)) (fun
|
||||
return
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) FuncCount() int {
|
||||
return len(ctx.funcStore)
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) DeleteFunc(funcName string) {
|
||||
delete(ctx.funcStore, funcName)
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) Call(name string, args []any) (result any, err error) {
|
||||
if info, exists := GetLocalFuncInfo(ctx, name); exists {
|
||||
functor := info.Functor()
|
||||
|
||||
@@ -106,6 +106,7 @@ const (
|
||||
SymKwIn
|
||||
SymKwInclude
|
||||
SymKwNil
|
||||
SymKwUnset
|
||||
)
|
||||
|
||||
var keywords map[string]Symbol
|
||||
@@ -123,5 +124,6 @@ func init() {
|
||||
"NOT": SymKwNot,
|
||||
"OR": SymKwOr,
|
||||
"NIL": SymKwNil,
|
||||
"UNSET": SymKwUnset,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// t_builtin-iterator.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFuncRun(t *testing.T) {
|
||||
section := "Builtin-Iterator"
|
||||
|
||||
inputs := []inputType{
|
||||
/* 1 */ {`builtin "iterator"; it=$(1,2,3); run(it)`, nil, nil},
|
||||
/* 2 */ {`builtin "iterator"; run($(1,2,3), func(index,item){item+10})`, nil, nil},
|
||||
/* 3 */ {`builtin "iterator"; run($(1,2,3), func(index,item){status=status+item; true}, {"status":0})`, int64(6), nil},
|
||||
/* 4 */ {`builtin ["iterator", "fmt"]; run($(1,2,3), func(index,item){println(item+10)})`, nil, nil},
|
||||
/* 5 */ {`builtin "iterator"; run(nil)`, nil, `paramter "iterator" must be an iterator, passed <nil> [nil]`},
|
||||
/* 6 */ {`builtin "iterator"; run($(1,2,3), nil)`, nil, `paramter "operator" must be a function, passed <nil> [nil]`},
|
||||
/* 7 */ {`builtin "iterator"; run($(1,2,3), func(){1}, "prrr")`, nil, `paramter "vars" must be a dictionary, passed prrr [string]`},
|
||||
}
|
||||
|
||||
//t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
//runTestSuiteSpec(t, section, inputs, 3)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
+2
-1
@@ -30,13 +30,14 @@ func TestFuncs(t *testing.T) {
|
||||
/* 17 */ {`f=func(x,n){1}; f(3,4,)`, nil, `[1:24] expected "function-param-value", got ")"`},
|
||||
/* 18 */ {`factory=func(base){func(){@base=base+1}}; inc10=factory(10); inc5=factory(5); inc10(); inc5(); inc10()`, int64(12), nil},
|
||||
/* 19 */ {`f=func(a,y=1,z="sos"){}; string(f)`, `f(a, y=1, z="sos"):any{}`, 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 ")"`)},
|
||||
}
|
||||
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 20)
|
||||
//runTestSuiteSpec(t, section, inputs, 20)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
|
||||
+8
-4
@@ -9,10 +9,10 @@ import "testing"
|
||||
func TestIteratorParser(t *testing.T) {
|
||||
section := "Iterator"
|
||||
inputs := []inputType{
|
||||
/* 1 */ {`include "test-resources/iterator.expr"; it=$(ds,3); ()it`, int64(0), nil},
|
||||
/* 1 */ {`include "test-resources/iterator.expr"; it=$(ds,3); ^it`, int64(0), nil},
|
||||
/* 2 */ {`include "test-resources/iterator.expr"; it=$(ds,3); it++; it++`, int64(1), nil},
|
||||
/* 3 */ {`include "test-resources/iterator.expr"; it=$(ds,3); it++; it++; #it`, int64(2), nil},
|
||||
/* 4 */ {`include "test-resources/iterator.expr"; it=$(ds,3); it++; it++; it.reset; ()it`, int64(0), nil},
|
||||
/* 4 */ {`include "test-resources/iterator.expr"; it=$(ds,3); it++; it++; it.reset; ^it`, int64(0), nil},
|
||||
/* 5 */ {`builtin "math.arith"; include "test-resources/iterator.expr"; it=$(ds,3); add(it)`, int64(6), nil},
|
||||
/* 6 */ {`builtin "math.arith"; include "test-resources/iterator.expr"; it=$(ds,3); mul(it)`, int64(0), nil},
|
||||
/* 7 */ {`builtin "math.arith"; include "test-resources/file-reader.expr"; it=$(ds,"test-resources/int.list"); mul(it)`, int64(12000), nil},
|
||||
@@ -20,9 +20,13 @@ func TestIteratorParser(t *testing.T) {
|
||||
/* 9 */ {`include "test-resources/file-reader.expr"; it=$(ds,"test-resources/int.list"); it.clean`, true, nil},
|
||||
/* 10 */ {`it=$(1,2,3); it++`, int64(1), nil},
|
||||
/* 11 */ {`it=$(1,2,3); it++; it.reset; it++`, int64(1), nil},
|
||||
// /* 12 */ {`include "test-resources/filter.expr"; it=$(ds,10); it++`, int64(1), nil},
|
||||
/* 12 */ {`it=$([1,2,3,4],1); it++`, int64(2), nil},
|
||||
/* 13 */ {`it=$([1,2,3,4],1,3); it++; it++;`, int64(3), nil},
|
||||
/* 14 */ {`it=$([1,2,3,4],1,3,2); it++; it++;`, int64(4), nil},
|
||||
/* 15 */ {`it=$([1,2,3,4],1,2,2); it++; it++;`, nil, `EOF`},
|
||||
/* 16 */ {`include "test-resources/filter.expr"; it=$(ds,10); it++`, int64(2), nil},
|
||||
}
|
||||
|
||||
//runTestSuiteSpec(t, section, inputs, 12)
|
||||
//runTestSuiteSpec(t, section, inputs, 4)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// t_operator_test.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOperator(t *testing.T) {
|
||||
section := "Operator"
|
||||
inputs := []inputType{
|
||||
/* 1 */ {`a=1; unset "a"; a`, nil, `undefined variable or function "a"`},
|
||||
/* 2 */ {`a=1; unset ["a", "b"]`, int64(1), nil},
|
||||
/* 3 */ {`f=func(){3}; unset "f()"`, int64(1), nil},
|
||||
}
|
||||
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
//runTestSuiteSpec(t, section, inputs, 3)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
+9
-2
@@ -9,8 +9,15 @@ import (
|
||||
)
|
||||
|
||||
func _TestImportPlugin(t *testing.T) {
|
||||
if err := importPlugin([]string{"test-resources"}, "json"); err != nil {
|
||||
t.Errorf("importPlugin() failed: %v", err)
|
||||
t.Setenv("PLUGINS", "${HOME}/go/src/git.portale-stac.it/go")
|
||||
t.Setenv("EXPR_PLUGIN_PATH","${PLUGINS}/expr-json-plugin:${PLUGINS}/expr-csv-plugin")
|
||||
|
||||
gotCount, gotErr := importPluginFromSearchPath("json")
|
||||
if gotCount != 1 {
|
||||
t.Errorf("Import count: got=%d, want=1", gotCount)
|
||||
}
|
||||
if gotErr != nil {
|
||||
t.Errorf("importPlugin() failed: %v", gotErr)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ const (
|
||||
priSign
|
||||
priFact
|
||||
priIterValue
|
||||
priCoalesce
|
||||
priDefault
|
||||
priIncDec
|
||||
priDot
|
||||
priValue
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
ds={
|
||||
"init":func(end){@end=end; @current=0 but true},
|
||||
"current":func(){current},
|
||||
"next":func(){
|
||||
((next=current+1) <= end) ? [true] {@current=next but current} :: {nil}
|
||||
}
|
||||
, "filter":func(item,index) { item > 0 }
|
||||
, "map": func(item,index) { item * 2 }
|
||||
}
|
||||
|
||||
/* Example
|
||||
;
|
||||
it=$(ds,3);
|
||||
add(it)
|
||||
*/
|
||||
@@ -6,7 +6,11 @@ package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func IsString(v any) (ok bool) {
|
||||
@@ -221,3 +225,37 @@ func ForAll[T, V any](ts []T, fn func(T) V) []V {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func ExpandPath(sourcePath string) (expandedPath string, err error) {
|
||||
for expandedPath = os.ExpandEnv(sourcePath); expandedPath != sourcePath; expandedPath = os.ExpandEnv(sourcePath) {
|
||||
sourcePath = expandedPath
|
||||
}
|
||||
|
||||
if strings.HasPrefix(sourcePath, "~") {
|
||||
var home, userName, remainder string
|
||||
|
||||
slashPos := strings.IndexRune(sourcePath, '/')
|
||||
if slashPos > 0 {
|
||||
userName = sourcePath[1:slashPos]
|
||||
remainder = sourcePath[slashPos:]
|
||||
} else {
|
||||
userName = sourcePath[1:]
|
||||
}
|
||||
|
||||
if len(userName) == 0 {
|
||||
home, err = os.UserHomeDir()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
var userInfo *user.User
|
||||
userInfo, err = user.Lookup(userName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
home = userInfo.HomeDir
|
||||
}
|
||||
expandedPath = path.Join(home, remainder)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user