// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com). // All rights reserved. // function.go package expr import ( "fmt" "strings" ) // ---- Function template type FuncTemplate func(ctx ExprContext, name string, args []any) (result any, err error) // ---- Common functor definition type baseFunctor struct { info ExprFunc } func (functor *baseFunctor) ToString(opt FmtOpt) (s string) { if functor.info != nil { s = functor.info.ToString(opt) } else { s = "func() {}" } return s } func (functor *baseFunctor) GetParams() (params []ExprFuncParam) { if functor.info != nil { return functor.info.Params() } else { return []ExprFuncParam{} } } func (functor *baseFunctor) SetFunc(info ExprFunc) { functor.info = info } func (functor *baseFunctor) GetFunc() ExprFunc { return functor.info } // ---- Linking with Go functions type golangFunctor struct { baseFunctor f FuncTemplate } func newGolangFunctor(f FuncTemplate) *golangFunctor { return &golangFunctor{f: f} } func (functor *golangFunctor) Invoke(ctx ExprContext, name string, args []any) (result any, err error) { return functor.f(ctx, name, args) } // ---- Linking with Expr functions type exprFunctor struct { baseFunctor params []string expr Expr defCtx ExprContext } func newExprFunctor(e Expr, params []string, ctx ExprContext) *exprFunctor { return &exprFunctor{expr: e, params: params, defCtx: ctx} } func (functor *exprFunctor) Invoke(ctx ExprContext, name string, args []any) (result any, err error) { if functor.defCtx != nil { ctx.Merge(functor.defCtx) } for i, p := range functor.params { if i < len(args) { arg := args[i] if funcArg, ok := arg.(Functor); ok { // ctx.RegisterFunc(p, functor, 0, -1) paramSpecs := funcArg.GetParams() ctx.RegisterFunc(p, funcArg, typeAny, paramSpecs) } else { ctx.UnsafeSetVar(p, arg) } } else { ctx.UnsafeSetVar(p, nil) } } result, err = functor.expr.eval(ctx, false) return } // ---- Function Parameters type paramFlags uint16 const ( pfOptional paramFlags = 1 << iota pfRepeat ) type funcParamInfo struct { name string flags paramFlags defaultValue any } func newFuncParam(name string) ExprFuncParam { return &funcParamInfo{name: name} } func newFuncParamFlag(name string, flags paramFlags) ExprFuncParam { return &funcParamInfo{name: name, flags: flags} } func newFuncParamFlagDef(name string, flags paramFlags, defValue any) *funcParamInfo { return &funcParamInfo{name: name, flags: flags, defaultValue: defValue} } func (param *funcParamInfo) Name() string { return param.name } func (param *funcParamInfo) Type() string { return "any" } func (param *funcParamInfo) IsOptional() bool { return (param.flags & pfOptional) != 0 } func (param *funcParamInfo) IsRepeat() bool { return (param.flags & pfRepeat) != 0 } func (param *funcParamInfo) DefaultValue() any { return param.defaultValue } // --- Functions type funcInfo struct { name string minArgs int maxArgs int functor Functor params []ExprFuncParam returnType string } func newFuncInfo(name string, functor Functor, returnType string, params []ExprFuncParam) (info *funcInfo, err error) { var minArgs = 0 var maxArgs = 0 if params != nil { for _, p := range params { if maxArgs == -1 { return nil, fmt.Errorf("no more params can be specified after the ellipsis symbol: %q", p.Name()) } if p.IsOptional() { maxArgs++ } else if maxArgs == minArgs { minArgs++ maxArgs++ } else { return nil, fmt.Errorf("can't specify non-optional param after optional ones: %q", p.Name()) } if p.IsRepeat() { maxArgs = -1 } } } info = &funcInfo{ name: name, minArgs: minArgs, maxArgs: maxArgs, functor: functor, returnType: returnType, params: params, } functor.SetFunc(info) return info, nil } func newUnnamedFuncInfo(functor Functor, returnType string, params []ExprFuncParam) (info *funcInfo, err error) { return newFuncInfo("unnamed", functor, returnType, params) } func (info *funcInfo) Params() []ExprFuncParam { return info.params } func (info *funcInfo) ReturnType() string { return info.returnType } func (info *funcInfo) ToString(opt FmtOpt) string { var sb strings.Builder if len(info.Name()) == 0 { sb.WriteString("func") } else { sb.WriteString(info.Name()) } sb.WriteByte('(') if info.params != nil { for i, p := range info.params { if i > 0 { sb.WriteString(", ") } sb.WriteString(p.Name()) if p.IsOptional() { sb.WriteByte('=') if s, ok := p.DefaultValue().(string); ok { sb.WriteByte('"') sb.WriteString(s) sb.WriteByte('"') } else { sb.WriteString(fmt.Sprintf("%v", p.DefaultValue())) } } } } if info.maxArgs < 0 { sb.WriteString(" ...") } sb.WriteString("): ") if len(info.returnType) > 0 { sb.WriteString(info.returnType) } else { sb.WriteString(typeAny) } sb.WriteString(" {}") return sb.String() } func (info *funcInfo) Name() string { return info.name } func (info *funcInfo) MinArgs() int { return info.minArgs } func (info *funcInfo) MaxArgs() int { return info.maxArgs } func (info *funcInfo) Functor() Functor { return info.functor }