Expressions now support function definition

This commit is contained in:
2024-04-02 04:36:03 +02:00
parent f58ec3ac32
commit 072dab4144
20 changed files with 376 additions and 143 deletions
+66 -5
View File
@@ -4,8 +4,13 @@
// operand-func.go
package expr
// -------- function term
func newFuncTerm(tk *Token, args []*term) *term {
import (
"errors"
"fmt"
)
// -------- function call term
func newFuncCallTerm(tk *Token, args []*term) *term {
return &term{
tk: *tk,
class: classVar,
@@ -14,12 +19,12 @@ func newFuncTerm(tk *Token, args []*term) *term {
children: args,
position: posLeaf,
priority: priValue,
evalFunc: evalFunc,
evalFunc: evalFuncCall,
}
}
// -------- eval func
func evalFunc(ctx exprContext, self *term) (v any, err error) {
// -------- eval func call
func evalFuncCall(ctx exprContext, self *term) (v any, err error) {
name, _ := self.tk.Value.(string)
params := make([]any, len(self.children))
for i, tree := range self.children {
@@ -34,3 +39,59 @@ func evalFunc(ctx exprContext, self *term) (v any, err error) {
}
return
}
// -------- function definition term
func newFuncDefTerm(tk *Token, args []*term) *term {
return &term{
tk: *tk,
class: classVar,
kind: kindUnknown,
parent: nil,
children: args, // arg[0]=formal-param-list, arg[1]=*ast
position: posLeaf,
priority: priValue,
evalFunc: evalFuncDef,
}
}
// -------- eval func def
// TODO
type funcDefFunctor struct {
params []string
expr Expr
}
func (functor *funcDefFunctor) Invoke(parentCtx exprContext, name string, args []any) (result any, err error) {
ctx := parentCtx.Clone()
for i, p := range functor.params {
if i < len(args) {
ctx.SetValue(p, args[i])
} else {
ctx.SetValue(p, nil)
}
}
result, err = functor.expr.Eval(ctx, false)
return
}
func evalFuncDef(ctx exprContext, self *term) (v any, err error) {
bodySpec := self.value()
if expr, ok := bodySpec.(*ast); ok {
paramList := make([]string, 0, len(self.children))
for i, param := range self.children {
if paramName, ok := param.value().(string); ok {
paramList = append(paramList, paramName)
} else {
err = fmt.Errorf("invalid function definition: formal param nr %d must be an identifiers", i+1)
break
}
}
v = &funcDefFunctor{
params: paramList,
expr: expr,
}
} else {
err = errors.New("invalid function definition: the body specification must be an expression")
}
return
}