moved a subset of source file to the kern package
This commit is contained in:
+77
-29
@@ -7,43 +7,62 @@ package expr
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
// "strings"
|
||||
)
|
||||
|
||||
type SimpleStore struct {
|
||||
parent ExprContext
|
||||
global kern.ExprContext
|
||||
parent kern.ExprContext
|
||||
varStore map[string]any
|
||||
funcStore map[string]ExprFunc
|
||||
funcStore map[string]kern.ExprFunc
|
||||
}
|
||||
|
||||
func NewSimpleStore() *SimpleStore {
|
||||
global := InitGlobal()
|
||||
ctx := &SimpleStore{
|
||||
global: global,
|
||||
varStore: make(map[string]any),
|
||||
funcStore: make(map[string]kern.ExprFunc),
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
func NewSimpleStoreWithoutGlobalContext() *SimpleStore {
|
||||
ctx := &SimpleStore{
|
||||
varStore: make(map[string]any),
|
||||
funcStore: make(map[string]ExprFunc),
|
||||
funcStore: make(map[string]kern.ExprFunc),
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (ss *SimpleStore) Init() {
|
||||
ss.varStore = make(map[string]any)
|
||||
ss.funcStore = make(map[string]ExprFunc)
|
||||
ss.funcStore = make(map[string]kern.ExprFunc)
|
||||
}
|
||||
|
||||
func filterRefName(name string) bool { return name[0] != '@' }
|
||||
func filterRefName(name string) bool { return name[0] != '@' }
|
||||
|
||||
//func filterPrivName(name string) bool { return name[0] != '_' }
|
||||
|
||||
func (ctx *SimpleStore) SetParent(parentCtx ExprContext) {
|
||||
func (ctx *SimpleStore) SetParent(parentCtx kern.ExprContext) {
|
||||
ctx.parent = parentCtx
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) GetParent() ExprContext {
|
||||
func (ctx *SimpleStore) GetParent() kern.ExprContext {
|
||||
return ctx.parent
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) Clone() ExprContext {
|
||||
func (ctx *SimpleStore) GetGlobal() (globalCtx kern.ExprContext) {
|
||||
return ctx.global
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) Clone() kern.ExprContext {
|
||||
clone := &SimpleStore{
|
||||
varStore: CloneFilteredMap(ctx.varStore, filterRefName),
|
||||
funcStore: CloneFilteredMap(ctx.funcStore, filterRefName),
|
||||
global: ctx.global,
|
||||
varStore: kern.CloneFilteredMap(ctx.varStore, filterRefName),
|
||||
funcStore: kern.CloneFilteredMap(ctx.funcStore, filterRefName),
|
||||
}
|
||||
return clone
|
||||
}
|
||||
@@ -57,19 +76,19 @@ func (ctx *SimpleStore) Clone() ExprContext {
|
||||
// }
|
||||
// }
|
||||
|
||||
func (ctx *SimpleStore) ToString(opt FmtOpt) string {
|
||||
func (ctx *SimpleStore) ToString(opt kern.FmtOpt) string {
|
||||
dict := ctx.ToDict()
|
||||
return dict.ToString(opt)
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) varsToDict(dict *DictType) *DictType {
|
||||
func (ctx *SimpleStore) varsToDict(dict *kern.DictType) *kern.DictType {
|
||||
names := ctx.EnumVars(nil)
|
||||
slices.Sort(names)
|
||||
for _, name := range ctx.EnumVars(nil) {
|
||||
value, _ := ctx.GetVar(name)
|
||||
if f, ok := value.(Formatter); ok {
|
||||
if f, ok := value.(kern.Formatter); ok {
|
||||
(*dict)[name] = f.ToString(0)
|
||||
} else if _, ok = value.(Functor); ok {
|
||||
} else if _, ok = value.(kern.Functor); ok {
|
||||
(*dict)[name] = "func(){}"
|
||||
} else {
|
||||
(*dict)[name] = fmt.Sprintf("%v", value)
|
||||
@@ -78,12 +97,12 @@ func (ctx *SimpleStore) varsToDict(dict *DictType) *DictType {
|
||||
return dict
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) funcsToDict(dict *DictType) *DictType {
|
||||
func (ctx *SimpleStore) funcsToDict(dict *kern.DictType) *kern.DictType {
|
||||
names := ctx.EnumFuncs(func(name string) bool { return true })
|
||||
slices.Sort(names)
|
||||
for _, name := range names {
|
||||
value, _ := ctx.GetFuncInfo(name)
|
||||
if formatter, ok := value.(Formatter); ok {
|
||||
if formatter, ok := value.(kern.Formatter); ok {
|
||||
(*dict)[name] = formatter.ToString(0)
|
||||
} else {
|
||||
(*dict)[name] = fmt.Sprintf("%v", value)
|
||||
@@ -92,20 +111,23 @@ func (ctx *SimpleStore) funcsToDict(dict *DictType) *DictType {
|
||||
return dict
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) ToDict() (dict *DictType) {
|
||||
dict = MakeDict()
|
||||
(*dict)["variables"] = ctx.varsToDict(MakeDict())
|
||||
(*dict)["functions"] = ctx.funcsToDict(MakeDict())
|
||||
func (ctx *SimpleStore) ToDict() (dict *kern.DictType) {
|
||||
dict = kern.MakeDict()
|
||||
(*dict)["variables"] = ctx.varsToDict(kern.MakeDict())
|
||||
(*dict)["functions"] = ctx.funcsToDict(kern.MakeDict())
|
||||
return
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) GetVar(varName string) (v any, exists bool) {
|
||||
v, exists = ctx.varStore[varName]
|
||||
func (ctx *SimpleStore) GetVar(varName string) (value any, exists bool) {
|
||||
if value, exists = ctx.varStore[varName]; !exists && ctx.global != nil {
|
||||
value, exists = ctx.global.GetVar(varName)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) GetLast() (v any) {
|
||||
v = ctx.varStore["last"]
|
||||
v = ctx.varStore[kern.ControlLastResult]
|
||||
return
|
||||
}
|
||||
|
||||
@@ -116,7 +138,7 @@ func (ctx *SimpleStore) UnsafeSetVar(varName string, value any) {
|
||||
|
||||
func (ctx *SimpleStore) SetVar(varName string, value any) {
|
||||
// fmt.Printf("[%p] SetVar(%v, %v)\n", ctx, varName, value)
|
||||
if allowedValue, ok := fromGenericAny(value); ok {
|
||||
if allowedValue, ok := kern.FromGenericAny(value); ok {
|
||||
ctx.varStore[varName] = allowedValue
|
||||
} else {
|
||||
panic(fmt.Errorf("unsupported type %T of value %v", value, value))
|
||||
@@ -145,16 +167,42 @@ func (ctx *SimpleStore) DeleteVar(varName string) {
|
||||
delete(ctx.varStore, varName)
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) GetFuncInfo(name string) (info ExprFunc, exists bool) {
|
||||
info, exists = ctx.funcStore[name]
|
||||
func (ctx *SimpleStore) GetFuncInfo(name string) (info kern.ExprFunc, exists bool) {
|
||||
info, exists, _ = ctx.GetFuncInfoAndOwner(name)
|
||||
return
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) RegisterFuncInfo(info ExprFunc) {
|
||||
func (ctx *SimpleStore) GetFuncInfoAndOwner(name string) (info kern.ExprFunc, exists bool, ownerCtx kern.ExprContext) {
|
||||
if len(name) > 0 {
|
||||
if info, exists = ctx.GetLocalFuncInfo(name); exists {
|
||||
ownerCtx = ctx
|
||||
} else if globalCtx := ctx.GetGlobal(); globalCtx != nil {
|
||||
if info, exists = globalCtx.GetFuncInfo(name); exists {
|
||||
ownerCtx = globalCtx
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) GetLocalFuncInfo(name string) (info kern.ExprFunc, exists bool) {
|
||||
var v any
|
||||
if len(name) > 0 {
|
||||
if v, exists = ctx.GetVar(name); exists && kern.IsFunctor(v) {
|
||||
f, _ := v.(kern.Functor)
|
||||
info = f.GetFunc()
|
||||
} else {
|
||||
info, exists = ctx.funcStore[name]
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) RegisterFuncInfo(info kern.ExprFunc) {
|
||||
ctx.funcStore[info.Name()], _ = info.(*funcInfo)
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) RegisterFunc(name string, functor Functor, returnType string, params []ExprFuncParam) (exprFunc ExprFunc, err error) {
|
||||
func (ctx *SimpleStore) RegisterFunc(name string, functor kern.Functor, returnType string, params []kern.ExprFuncParam) (exprFunc kern.ExprFunc, err error) {
|
||||
var info *funcInfo
|
||||
if info, err = newFuncInfo(name, functor, returnType, params); err == nil {
|
||||
ctx.funcStore[name] = info
|
||||
@@ -186,7 +234,7 @@ func (ctx *SimpleStore) DeleteFunc(funcName string) {
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) Call(name string, args map[string]any) (result any, err error) {
|
||||
if info, exists := GetLocalFuncInfo(ctx, name); exists {
|
||||
if info, exists := ctx.GetLocalFuncInfo(name); exists {
|
||||
functor := info.Functor()
|
||||
result, err = functor.InvokeNamed(ctx, name, args)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user