103 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			103 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // simple-var-store.go
 | |
| package expr
 | |
| 
 | |
| import "fmt"
 | |
| 
 | |
| type SimpleFuncStore struct {
 | |
| 	varStore  map[string]any
 | |
| 	funcStore map[string]*funcInfo
 | |
| }
 | |
| 
 | |
| type funcInfo struct {
 | |
| 	name string
 | |
| 	minArgs int
 | |
| 	maxArgs int
 | |
| 	functor Functor
 | |
| }
 | |
| 
 | |
| 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
 | |
| }
 | |
| 
 | |
| func NewSimpleFuncStore() *SimpleFuncStore {
 | |
| 	return &SimpleFuncStore{
 | |
| 		varStore:  make(map[string]any),
 | |
| 		funcStore: make(map[string]*funcInfo),
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (ctx *SimpleFuncStore) Clone() exprContext {
 | |
| 	return &SimpleFuncStore{
 | |
| 		varStore:  CloneMap(ctx.varStore),
 | |
| 		funcStore: CloneMap(ctx.funcStore),
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (ctx *SimpleFuncStore) GetVar(varName string) (v any, exists bool) {
 | |
| 	v, exists = ctx.varStore[varName]
 | |
| 	return
 | |
| }
 | |
| 
 | |
| func (ctx *SimpleFuncStore) SetVar(varName string, value any) {
 | |
| 	ctx.varStore[varName] = value
 | |
| }
 | |
| 
 | |
| func (ctx *SimpleFuncStore) EnumVars(acceptor func(name string) (accept bool)) (varNames []string) {
 | |
| 	varNames = make([]string, 0)
 | |
| 	for name := range ctx.varStore {
 | |
| 		if acceptor != nil {
 | |
| 			if acceptor(name) {
 | |
| 				varNames = append(varNames, name)
 | |
| 			}
 | |
| 		} else {
 | |
| 			varNames = append(varNames, name)
 | |
| 		}
 | |
| 	}
 | |
| 	return
 | |
| }
 | |
| 
 | |
| func (ctx *SimpleFuncStore) GetFuncInfo(name string) (info exprFunc) {
 | |
| 	info, _ = ctx.funcStore[name]
 | |
| 	return
 | |
| }
 | |
| 
 | |
| func (ctx *SimpleFuncStore) RegisterFunc(name string, functor Functor, minArgs, maxArgs int) {
 | |
| 	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
 | |
| }
 | |
| 
 | |
| func (ctx *SimpleFuncStore) Call(name string, args []any) (result any, err error) {
 | |
| 	if info, exists := ctx.funcStore[name]; exists {
 | |
| 		functor := info.functor
 | |
| 		result, err = functor.Invoke(ctx, name, args)
 | |
| 	} else {
 | |
| 		err = fmt.Errorf("unknown function %s()", name)
 | |
| 	}
 | |
| 	return
 | |
| }
 |