64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// expr-context.go
|
|
package kern
|
|
|
|
// ----Expression Context
|
|
type ExprContext interface {
|
|
Clone() ExprContext
|
|
SetParent(ctx ExprContext)
|
|
GetParent() (ctx ExprContext)
|
|
GetGlobal() (ctx ExprContext)
|
|
GetVar(varName string) (value any, exists bool)
|
|
GetLast() any
|
|
SetVar(varName string, value any)
|
|
UnsafeSetVar(varName string, value any)
|
|
|
|
EnumVars(func(name string) (accept bool)) (varNames []string)
|
|
VarCount() int
|
|
DeleteVar(varName string)
|
|
|
|
EnumFuncs(func(name string) (accept bool)) (funcNames []string)
|
|
FuncCount() int
|
|
DeleteFunc(funcName string)
|
|
|
|
GetLocalFuncInfo(name string) (info ExprFunc, exists bool)
|
|
GetFuncInfo(name string) (item ExprFunc, exists bool)
|
|
Call(name string, args map[string]any) (result any, err error)
|
|
RegisterFuncInfo(info ExprFunc)
|
|
RegisterFunc(name string, f Functor, returnType string, param []ExprFuncParam) (funcInfo ExprFunc, err error)
|
|
|
|
ToDict() (dict *DictType)
|
|
ToString(opt FmtOpt) string
|
|
}
|
|
|
|
func ContextToDict(ctx ExprContext) (dict *DictType) {
|
|
var keys []string
|
|
// Variables
|
|
keys = ctx.EnumVars(nil)
|
|
vars := MakeDict()
|
|
for _, key := range keys {
|
|
value, _ := ctx.GetVar(key)
|
|
vars.SetItem(key, value)
|
|
}
|
|
|
|
// Functions
|
|
keys = ctx.EnumFuncs(func(name string) bool { return true })
|
|
funcs := MakeDict()
|
|
for _, key := range keys {
|
|
funcInfo, _ := ctx.GetFuncInfo(key)
|
|
funcs.SetItem(key, funcInfo)
|
|
}
|
|
|
|
dict = MakeDict()
|
|
dict.SetItem("vars", vars)
|
|
dict.SetItem("funcs", funcs)
|
|
return
|
|
}
|
|
|
|
func ContextToString(ctx ExprContext, opt FmtOpt) string {
|
|
dict := ctx.ToDict()
|
|
return dict.ToString(opt)
|
|
}
|