183 lines
4.5 KiB
Go
183 lines
4.5 KiB
Go
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// simple-store.go
|
|
package expr
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
type SimpleStore struct {
|
|
varStore map[string]any
|
|
funcStore map[string]ExprFunc
|
|
}
|
|
|
|
func NewSimpleStore() *SimpleStore {
|
|
ctx := &SimpleStore{
|
|
varStore: make(map[string]any),
|
|
funcStore: make(map[string]ExprFunc),
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
func filterRefName(name string) bool { return name[0] != '@' }
|
|
func filterPrivName(name string) bool { return name[0] != '_' }
|
|
|
|
func (ctx *SimpleStore) Clone() ExprContext {
|
|
return &SimpleStore{
|
|
varStore: CloneFilteredMap(ctx.varStore, filterRefName),
|
|
funcStore: CloneFilteredMap(ctx.funcStore, filterRefName),
|
|
}
|
|
}
|
|
|
|
func (ctx *SimpleStore) Merge(src ExprContext) {
|
|
for _, name := range src.EnumVars(filterRefName) {
|
|
ctx.varStore[name], _ = src.GetVar(name)
|
|
}
|
|
for _, name := range src.EnumFuncs(filterRefName) {
|
|
ctx.funcStore[name], _ = src.GetFuncInfo(name)
|
|
}
|
|
}
|
|
|
|
func varsCtxToBuilder(sb *strings.Builder, ctx ExprContext, indent int) {
|
|
sb.WriteString("vars: {\n")
|
|
first := true
|
|
for _, name := range ctx.EnumVars(filterPrivName) {
|
|
if first {
|
|
first = false
|
|
} else {
|
|
sb.WriteByte(',')
|
|
sb.WriteByte('\n')
|
|
}
|
|
|
|
value, _ := ctx.GetVar(name)
|
|
sb.WriteString(strings.Repeat("\t", indent+1))
|
|
sb.WriteString(name)
|
|
sb.WriteString(": ")
|
|
if f, ok := value.(Formatter); ok {
|
|
sb.WriteString(f.ToString(0))
|
|
} else if _, ok = value.(Functor); ok {
|
|
sb.WriteString("func(){}")
|
|
// } else if _, ok = value.(map[any]any); ok {
|
|
// sb.WriteString("dict{}")
|
|
} else {
|
|
sb.WriteString(fmt.Sprintf("%v", value))
|
|
}
|
|
}
|
|
sb.WriteString(strings.Repeat("\t", indent))
|
|
sb.WriteString("\n}\n")
|
|
}
|
|
|
|
func varsCtxToString(ctx ExprContext, indent int) string {
|
|
var sb strings.Builder
|
|
varsCtxToBuilder(&sb, ctx, indent)
|
|
return sb.String()
|
|
}
|
|
|
|
func funcsCtxToBuilder(sb *strings.Builder, ctx ExprContext, indent int) {
|
|
sb.WriteString("funcs: {\n")
|
|
first := true
|
|
names := ctx.EnumFuncs(func(name string) bool { return true })
|
|
slices.Sort(names)
|
|
for _, name := range names {
|
|
if first {
|
|
first = false
|
|
} else {
|
|
sb.WriteByte(',')
|
|
sb.WriteByte('\n')
|
|
}
|
|
value, _ := ctx.GetFuncInfo(name)
|
|
sb.WriteString(strings.Repeat("\t", indent+1))
|
|
if formatter, ok := value.(Formatter); ok {
|
|
sb.WriteString(formatter.ToString(0))
|
|
} else {
|
|
sb.WriteString(fmt.Sprintf("%v", value))
|
|
}
|
|
}
|
|
sb.WriteString("\n}\n")
|
|
}
|
|
|
|
func (ctx *SimpleStore) ToString(opt FmtOpt) string {
|
|
var sb strings.Builder
|
|
varsCtxToBuilder(&sb, ctx, 0)
|
|
funcsCtxToBuilder(&sb, ctx, 0)
|
|
return sb.String()
|
|
}
|
|
|
|
func (ctx *SimpleStore) GetVar(varName string) (v any, exists bool) {
|
|
v, exists = ctx.varStore[varName]
|
|
return
|
|
}
|
|
|
|
func (ctx *SimpleStore) UnsafeSetVar(varName string, value any) {
|
|
// fmt.Printf("[%p] setVar(%v, %v)\n", ctx, varName, value)
|
|
ctx.varStore[varName] = value
|
|
}
|
|
|
|
func (ctx *SimpleStore) SetVar(varName string, value any) {
|
|
// fmt.Printf("[%p] SetVar(%v, %v)\n", ctx, varName, value)
|
|
if allowedValue, ok := fromGenericAny(value); ok {
|
|
ctx.varStore[varName] = allowedValue
|
|
} else {
|
|
panic(fmt.Errorf("unsupported type %T of value %v", value, value))
|
|
}
|
|
}
|
|
|
|
func (ctx *SimpleStore) 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 *SimpleStore) GetFuncInfo(name string) (info ExprFunc, exists bool) {
|
|
info, exists = ctx.funcStore[name]
|
|
return
|
|
}
|
|
|
|
func (ctx *SimpleStore) RegisterFuncInfo(info ExprFunc) {
|
|
ctx.funcStore[info.Name()], _ = info.(*funcInfo)
|
|
}
|
|
|
|
func (ctx *SimpleStore) RegisterFunc(name string, functor Functor, returnType string, params []ExprFuncParam) (err error) {
|
|
var info *funcInfo
|
|
if info, err = newFuncInfo(name, functor, returnType, params); err == nil {
|
|
ctx.funcStore[name] = info
|
|
}
|
|
return
|
|
}
|
|
|
|
func (ctx *SimpleStore) 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 *SimpleStore) Call(name string, args []any) (result any, err error) {
|
|
if info, exists := GetLocalFuncInfo(ctx, name); exists {
|
|
functor := info.Functor()
|
|
result, err = functor.Invoke(ctx, name, args)
|
|
} else {
|
|
err = fmt.Errorf("unknown function %s()", name)
|
|
}
|
|
return
|
|
}
|