function definition and usage rationalized

This commit is contained in:
2024-05-24 06:28:48 +02:00
parent d545a35acf
commit e5f63c3d9d
16 changed files with 342 additions and 251 deletions
+28 -27
View File
@@ -76,30 +76,30 @@ func newFuncDefTerm(tk *Token, args []*term) *term {
// -------- eval func def
// TODO
type funcDefFunctor struct {
params []string
expr Expr
}
// type funcDefFunctor struct {
// params []string
// expr Expr
// }
func (funcDef *funcDefFunctor) Invoke(ctx ExprContext, name string, args []any) (result any, err error) {
for i, p := range funcDef.params {
if i < len(args) {
arg := args[i]
if functor, ok := arg.(Functor); ok {
// ctx.RegisterFunc(p, functor, 0, -1)
ctx.RegisterFunc2(p, functor, typeAny, []ExprFuncParam{
newFuncParam(paramValue),
})
} else {
ctx.UnsafeSetVar(p, arg)
}
} else {
ctx.UnsafeSetVar(p, nil)
}
}
result, err = funcDef.expr.eval(ctx, false)
return
}
// func (funcDef *funcDefFunctor) Invoke(ctx ExprContext, name string, args []any) (result any, err error) {
// for i, p := range funcDef.params {
// if i < len(args) {
// arg := args[i]
// if functor, ok := arg.(Functor); ok {
// // ctx.RegisterFunc(p, functor, 0, -1)
// ctx.RegisterFunc2(p, functor, typeAny, []ExprFuncParam{
// newFuncParam(paramValue),
// })
// } else {
// ctx.UnsafeSetVar(p, arg)
// }
// } else {
// ctx.UnsafeSetVar(p, nil)
// }
// }
// result, err = funcDef.expr.eval(ctx, false)
// return
// }
func evalFuncDef(ctx ExprContext, self *term) (v any, err error) {
bodySpec := self.value()
@@ -108,10 +108,11 @@ func evalFuncDef(ctx ExprContext, self *term) (v any, err error) {
for _, param := range self.children {
paramList = append(paramList, param.source())
}
v = &funcDefFunctor{
params: paramList,
expr: expr,
}
v = newExprFunctor(expr, paramList)
// v = &funcDefFunctor{
// params: paramList,
// expr: expr,
// }
} else {
err = errors.New("invalid function definition: the body specification must be an expression")
}