the function call procedure now check the number of actual parameters against the numer of formal parameters

This commit is contained in:
2024-05-04 22:35:03 +02:00
parent 0fdd51049d
commit 1d8569d3a9
5 changed files with 32 additions and 8 deletions
+21 -4
View File
@@ -6,6 +6,7 @@ package expr
import (
"errors"
"fmt"
)
// -------- function call term
@@ -21,6 +22,20 @@ func newFuncCallTerm(tk *Token, args []*term) *term {
}
// -------- eval func call
func checkFunctionCall(ctx ExprContext, name string, params []any) (err error) {
if info, exists := ctx.GetFuncInfo(name); exists {
if info.MinArgs() > len(params) {
err = fmt.Errorf("too few params -- expected %d, got %d", info.MinArgs(), len(params))
}
if info.MaxArgs() >= 0 && info.MaxArgs() < len(params) {
err = fmt.Errorf("too much params -- expected %d, got %d", info.MaxArgs(), len(params))
}
} else {
err = fmt.Errorf("unknown function %s()", name)
}
return
}
func evalFuncCall(parentCtx ExprContext, self *term) (v any, err error) {
ctx := cloneContext(parentCtx)
name, _ := self.tk.Value.(string)
@@ -34,8 +49,10 @@ func evalFuncCall(parentCtx ExprContext, self *term) (v any, err error) {
params[i] = param
}
if err == nil {
if v, err = ctx.Call(name, params); err == nil {
exportObjects(parentCtx, ctx)
if err = checkFunctionCall(ctx, name, params); err == nil {
if v, err = ctx.Call(name, params); err == nil {
exportObjects(parentCtx, ctx)
}
}
}
return
@@ -44,9 +61,9 @@ func evalFuncCall(parentCtx ExprContext, self *term) (v any, err error) {
// -------- function definition term
func newFuncDefTerm(tk *Token, args []*term) *term {
return &term{
tk: *tk,
tk: *tk, // value is the expression body
parent: nil,
children: args, // arg[0]=formal-param-list, arg[1]=*ast
children: args, // function params
position: posLeaf,
priority: priValue,
evalFunc: evalFuncDef,