expr/operand-func.go

82 lines
2.0 KiB
Go
Raw Normal View History

2024-03-26 08:45:18 +01:00
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
2024-03-26 07:00:53 +01:00
// operand-func.go
package expr
import (
"errors"
)
// -------- function call term
func newFuncCallTerm(tk *Token, args []*term) *term {
2024-03-26 07:00:53 +01:00
return &term{
tk: *tk,
2024-03-26 07:00:53 +01:00
parent: nil,
children: args,
position: posLeaf,
priority: priValue,
evalFunc: evalFuncCall,
2024-03-26 07:00:53 +01:00
}
}
// -------- eval func call
// func _evalFuncCall(ctx ExprContext, opTerm *term) (v any, err error) {
// name, _ := opTerm.tk.Value.(string)
// params := make([]any, len(opTerm.children), len(opTerm.children)+5)
// for i, tree := range opTerm.children {
// var param any
// if param, err = tree.compute(ctx); err != nil {
// break
// }
// params[i] = param
// }
// if err == nil {
// v, err = CallFunction(ctx, name, params)
// }
// return
// }
2024-07-09 07:50:06 +02:00
func evalFuncCall(ctx ExprContext, opTerm *term) (v any, err error) {
name, _ := opTerm.tk.Value.(string)
v, err = CallFunction(ctx, name, opTerm)
2024-03-26 07:00:53 +01:00
return
}
// -------- function definition term
func newFuncDefTerm(tk *Token, args []*term) *term {
return &term{
tk: *tk, // value is the expression body
parent: nil,
children: args, // function params
position: posLeaf,
priority: priValue,
evalFunc: evalFuncDef,
}
}
// -------- eval func def
2024-07-09 07:50:06 +02:00
func evalFuncDef(ctx ExprContext, opTerm *term) (v any, err error) {
bodySpec := opTerm.value()
if expr, ok := bodySpec.(*ast); ok {
2024-07-09 07:50:06 +02:00
paramList := make([]ExprFuncParam, 0, len(opTerm.children))
for _, param := range opTerm.children {
var defValue any
flags := paramFlags(0)
if len(param.children) > 0 {
flags |= PfDefault
if defValue, err = param.children[0].compute(ctx); err != nil {
return
}
}
info := NewFuncParamFlagDef(param.source(), flags, defValue)
paramList = append(paramList, info)
}
2024-05-30 07:13:26 +02:00
v = newExprFunctor(expr, paramList, ctx)
} else {
err = errors.New("invalid function definition: the body specification must be an expression")
}
return
}