expr/simple-func-store.go

143 lines
3.3 KiB
Go
Raw Permalink Normal View History

// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// simple-func-store.go
2024-03-28 06:29:11 +01:00
package expr
import (
"fmt"
"strings"
)
2024-03-28 06:29:11 +01:00
type SimpleFuncStore struct {
SimpleVarStore
2024-04-06 01:00:29 +02:00
funcStore map[string]*funcInfo
2024-03-28 06:29:11 +01:00
}
type funcInfo struct {
name string
2024-04-06 01:00:29 +02:00
minArgs int
maxArgs int
functor Functor
2024-03-28 06:29:11 +01:00
}
func (info *funcInfo) ToString(opt FmtOpt) string {
var sb strings.Builder
var i int
sb.WriteString("func(")
for i = 0; i < info.minArgs; i++ {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(fmt.Sprintf("arg%d", i+1))
}
for ; i < info.maxArgs; i++ {
sb.WriteString(fmt.Sprintf("arg%d", i+1))
}
if info.maxArgs < 0 {
if info.minArgs > 0 {
sb.WriteString(", ")
}
sb.WriteString("...")
}
sb.WriteString(") {...}")
return sb.String()
}
2024-03-28 06:29:11 +01:00
func (info *funcInfo) Name() string {
return info.name
}
func (info *funcInfo) MinArgs() int {
2024-04-06 01:00:29 +02:00
return info.minArgs
2024-03-28 06:29:11 +01:00
}
func (info *funcInfo) MaxArgs() int {
2024-04-06 01:00:29 +02:00
return info.maxArgs
}
func (info *funcInfo) Functor() Functor {
return info.functor
2024-03-28 06:29:11 +01:00
}
func NewSimpleFuncStore() *SimpleFuncStore {
ctx := &SimpleFuncStore{
SimpleVarStore: SimpleVarStore{varStore: make(map[string]any)},
funcStore: make(map[string]*funcInfo),
}
ImportBuiltinsFuncs(ctx)
return ctx
}
func (ctx *SimpleFuncStore) Clone() ExprContext {
svs := ctx.SimpleVarStore
return &SimpleFuncStore{
// SimpleVarStore: SimpleVarStore{varStore: CloneMap(ctx.varStore)},
SimpleVarStore: SimpleVarStore{varStore: svs.cloneVars()},
funcStore: CloneFilteredMap(ctx.funcStore, func(name string) bool { return name[0] != '@' }),
2024-03-28 06:29:11 +01:00
}
}
func funcsCtxToBuilder(sb *strings.Builder, ctx ExprContext, indent int) {
sb.WriteString("funcs: {\n")
first := true
for _, name := range ctx.EnumFuncs(func(name string) bool { return true }) {
if first {
first = false
} else {
sb.WriteByte(',')
sb.WriteByte('\n')
}
value, _ := ctx.GetFuncInfo(name)
sb.WriteString(strings.Repeat("\t", indent+1))
sb.WriteString(name)
sb.WriteString("=")
if formatter, ok := value.(Formatter); ok {
sb.WriteString(formatter.ToString(0))
} else {
sb.WriteString(fmt.Sprintf("%v", value))
}
}
sb.WriteString("\n}\n")
}
func (ctx *SimpleFuncStore) ToString(opt FmtOpt) string {
var sb strings.Builder
sb.WriteString(ctx.SimpleVarStore.ToString(opt))
funcsCtxToBuilder(&sb, ctx, 0)
return sb.String()
}
func (ctx *SimpleFuncStore) GetFuncInfo(name string) (info ExprFunc, exists bool) {
info, exists = ctx.funcStore[name]
2024-03-28 06:29:11 +01:00
return
}
func (ctx *SimpleFuncStore) RegisterFunc(name string, functor Functor, minArgs, maxArgs int) {
2024-04-06 01:00:29 +02:00
ctx.funcStore[name] = &funcInfo{name: name, minArgs: minArgs, maxArgs: maxArgs, functor: functor}
}
func (ctx *SimpleFuncStore) EnumFuncs(acceptor func(name string) (accept bool)) (funcNames []string) {
funcNames = make([]string, 0)
for name := range ctx.funcStore {
if acceptor != nil {
if acceptor(name) {
funcNames = append(funcNames, name)
}
} else {
funcNames = append(funcNames, name)
}
}
return
2024-03-28 06:29:11 +01:00
}
func (ctx *SimpleFuncStore) Call(name string, args []any) (result any, err error) {
2024-04-06 01:00:29 +02:00
if info, exists := ctx.funcStore[name]; exists {
functor := info.functor
result, err = functor.Invoke(ctx, name, args)
2024-03-28 06:29:11 +01:00
} else {
err = fmt.Errorf("unknown function %s()", name)
}
return
}