67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
// simple-var-store.go
|
|
package expr
|
|
|
|
import "fmt"
|
|
|
|
type FuncTemplate func(ctx exprContext, name string, args []any) (result any, err error)
|
|
|
|
type SimpleFuncStore struct {
|
|
varStore map[string]any
|
|
funcStore map[string]FuncTemplate
|
|
}
|
|
|
|
type funcInfo struct {
|
|
name string
|
|
// minArgs int
|
|
// maxArgs int
|
|
}
|
|
|
|
func (info *funcInfo) Name() string {
|
|
return info.name
|
|
}
|
|
|
|
func (info *funcInfo) MinArgs() int {
|
|
return 0
|
|
}
|
|
|
|
func (info *funcInfo) MaxArgs() int {
|
|
return -1
|
|
}
|
|
|
|
func NewSimpleFuncStore() *SimpleFuncStore {
|
|
return &SimpleFuncStore{
|
|
varStore: make(map[string]any),
|
|
funcStore: make(map[string]FuncTemplate),
|
|
}
|
|
}
|
|
|
|
func (ctx *SimpleFuncStore) GetValue(varName string) (v any, exists bool) {
|
|
v, exists = ctx.varStore[varName]
|
|
return
|
|
}
|
|
|
|
func (ctx *SimpleFuncStore) SetValue(varName string, value any) {
|
|
ctx.varStore[varName] = value
|
|
}
|
|
|
|
func (ctx *SimpleFuncStore) GetFuncInfo(name string) (f exprFunc) {
|
|
var exists bool
|
|
if _, exists = ctx.funcStore[name]; exists {
|
|
f = &funcInfo{name: name}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (ctx *SimpleFuncStore) addFunc(name string, f FuncTemplate) {
|
|
ctx.funcStore[name] = f
|
|
}
|
|
|
|
func (ctx *SimpleFuncStore) Call(name string, args []any) (result any, err error) {
|
|
if f, exists := ctx.funcStore[name]; exists {
|
|
result, err = f(ctx, name, args)
|
|
} else {
|
|
err = fmt.Errorf("unknown function %s()", name)
|
|
}
|
|
return
|
|
}
|