expr/simple-var-store.go

113 lines
2.7 KiB
Go
Raw Normal View History

// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
2024-03-28 06:29:11 +01:00
// simple-var-store.go
package expr
import (
"fmt"
"strings"
)
2024-03-28 06:29:11 +01:00
type SimpleVarStore struct {
varStore map[string]any
2024-03-28 06:29:11 +01:00
}
func NewSimpleVarStore() *SimpleVarStore {
return &SimpleVarStore{
varStore: make(map[string]any),
2024-03-28 06:29:11 +01:00
}
}
func (ctx *SimpleVarStore) cloneVars() (vars map[string]any) {
return CloneFilteredMap(ctx.varStore, func(name string) bool { return name[0] != '@' })
}
func (ctx *SimpleVarStore) Clone() (clone ExprContext) {
// fmt.Println("*** Cloning context ***")
clone = &SimpleVarStore{
varStore: ctx.cloneVars(),
}
return clone
}
func (ctx *SimpleVarStore) GetVar(varName string) (v any, exists bool) {
v, exists = ctx.varStore[varName]
2024-03-28 06:29:11 +01:00
return
}
func (ctx *SimpleVarStore) setVar(varName string, value any) {
// fmt.Printf("[%p] setVar(%v, %v)\n", ctx, varName, value)
ctx.varStore[varName] = value
}
func (ctx *SimpleVarStore) 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 *SimpleVarStore) 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
2024-03-28 06:29:11 +01:00
}
func (ctx *SimpleVarStore) GetFuncInfo(name string) (f ExprFunc, exists bool) {
2024-03-28 06:29:11 +01:00
return
}
func (ctx *SimpleVarStore) Call(name string, args []any) (result any, err error) {
return
}
func (ctx *SimpleVarStore) RegisterFunc(name string, functor Functor, minArgs, maxArgs int) {
}
2024-04-06 01:00:29 +02:00
func (ctx *SimpleVarStore) EnumFuncs(acceptor func(name string) (accept bool)) (funcNames []string) {
return
}
func CtxToBuilder(sb *strings.Builder, ctx ExprContext, indent int) {
sb.WriteString("{\n")
first := true
for _, name := range ctx.EnumVars(func(name string) bool { return name[0] != '_' }) {
value, _ := ctx.GetVar(name)
sb.WriteString(strings.Repeat("\t", indent+1))
sb.WriteString(name)
sb.WriteString("=")
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))
}
if first {
first = false
} else {
sb.WriteByte(',')
}
sb.WriteByte('\n')
}
sb.WriteString(strings.Repeat("\t", indent))
sb.WriteString("}\n")
}
func CtxToString(ctx ExprContext, indent int) string {
var sb strings.Builder
CtxToBuilder(&sb, ctx, indent)
return sb.String()
}