Control vars are now stored in the globalCtx only.
However, it is still allowed to store control var in a local context in special situation
This commit is contained in:
parent
115ce26ce9
commit
f347b15146
2
ast.go
2
ast.go
@ -108,7 +108,7 @@ func (self *ast) Eval(ctx ExprContext) (result any, err error) {
|
|||||||
self.Finish()
|
self.Finish()
|
||||||
|
|
||||||
if self.root != nil {
|
if self.root != nil {
|
||||||
initDefaultVars(ctx)
|
// initDefaultVars(ctx)
|
||||||
if self.forest != nil {
|
if self.forest != nil {
|
||||||
for _, root := range self.forest {
|
for _, root := range self.forest {
|
||||||
if result, err = root.compute(ctx); err == nil {
|
if result, err = root.compute(ctx); err == nil {
|
||||||
|
@ -22,13 +22,11 @@ func exportFunc(ctx ExprContext, name string, info ExprFunc) {
|
|||||||
if name[0] == '@' {
|
if name[0] == '@' {
|
||||||
name = name[1:]
|
name = name[1:]
|
||||||
}
|
}
|
||||||
// ctx.RegisterFunc(name, info.Functor(), info.MinArgs(), info.MaxArgs())
|
|
||||||
// ctx.RegisterFuncInfo(name, info)
|
|
||||||
ctx.RegisterFunc(name, info.Functor(), info.ReturnType(), info.Params())
|
ctx.RegisterFunc(name, info.Functor(), info.ReturnType(), info.Params())
|
||||||
}
|
}
|
||||||
|
|
||||||
func exportObjects(destCtx, sourceCtx ExprContext) {
|
func exportObjects(destCtx, sourceCtx ExprContext) {
|
||||||
exportAll := isEnabled(sourceCtx, control_export_all)
|
exportAll := CtrlIsEnabled(sourceCtx, control_export_all)
|
||||||
// fmt.Printf("Exporting from sourceCtx [%p] to destCtx [%p] -- exportAll=%t\n", sourceCtx, destCtx, exportAll)
|
// fmt.Printf("Exporting from sourceCtx [%p] to destCtx [%p] -- exportAll=%t\n", sourceCtx, destCtx, exportAll)
|
||||||
// Export variables
|
// Export variables
|
||||||
for _, refName := range sourceCtx.EnumVars(func(name string) bool { return exportAll || name[0] == '@' }) {
|
for _, refName := range sourceCtx.EnumVars(func(name string) bool { return exportAll || name[0] == '@' }) {
|
||||||
|
56
control.go
56
control.go
@ -4,8 +4,6 @@
|
|||||||
// control.go
|
// control.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
import "strings"
|
|
||||||
|
|
||||||
// Preset control variables
|
// Preset control variables
|
||||||
const (
|
const (
|
||||||
ControlPreset = "_preset"
|
ControlPreset = "_preset"
|
||||||
@ -34,60 +32,6 @@ func initDefaultVars(ctx ExprContext) {
|
|||||||
ctx.SetVar(ControlSearchPath, init_search_path)
|
ctx.SetVar(ControlSearchPath, init_search_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CtrlEnable(ctx ExprContext, name string) (currentStatus bool) {
|
|
||||||
if !strings.HasPrefix(name, "_") {
|
|
||||||
name = "_" + name
|
|
||||||
}
|
|
||||||
if v, exists := ctx.GetVar(name); exists && IsBool(v) {
|
|
||||||
currentStatus, _ = v.(bool)
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.SetVar(name, true)
|
|
||||||
return currentStatus
|
|
||||||
}
|
|
||||||
|
|
||||||
func CtrlDisable(ctx ExprContext, name string) (currentStatus bool) {
|
|
||||||
if !strings.HasPrefix(name, "_") {
|
|
||||||
name = "_" + name
|
|
||||||
}
|
|
||||||
if v, exists := ctx.GetVar(name); exists && IsBool(v) {
|
|
||||||
currentStatus, _ = v.(bool)
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.SetVar(name, false)
|
|
||||||
return currentStatus
|
|
||||||
}
|
|
||||||
|
|
||||||
func CtrlSet(ctx ExprContext, name string, newValue any) (currentValue any) {
|
|
||||||
if !strings.HasPrefix(name, "_") {
|
|
||||||
name = "_" + name
|
|
||||||
}
|
|
||||||
currentValue, _ = ctx.GetVar(name)
|
|
||||||
|
|
||||||
ctx.SetVar(name, newValue)
|
|
||||||
for parent := ctx.GetParent(); parent != nil; parent = parent.GetParent() {
|
|
||||||
parent.SetVar(name, newValue)
|
|
||||||
}
|
|
||||||
return currentValue
|
|
||||||
}
|
|
||||||
|
|
||||||
func CtrlGet(ctx ExprContext, name string) (currentValue any) {
|
|
||||||
if !strings.HasPrefix(name, "_") {
|
|
||||||
name = "_" + name
|
|
||||||
}
|
|
||||||
currentValue, _ = ctx.GetVar(name)
|
|
||||||
return currentValue
|
|
||||||
}
|
|
||||||
|
|
||||||
func isEnabled(ctx ExprContext, name string) (status bool) {
|
|
||||||
if v, exists := ctx.GetVar(name); exists {
|
|
||||||
if b, ok := v.(bool); ok {
|
|
||||||
status = b
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func getControlString(ctx ExprContext, name string) (s string, exists bool) {
|
func getControlString(ctx ExprContext, name string) (s string, exists bool) {
|
||||||
var v any
|
var v any
|
||||||
if v, exists = ctx.GetVar(name); exists {
|
if v, exists = ctx.GetVar(name); exists {
|
||||||
|
@ -4,7 +4,10 @@
|
|||||||
// global-context.go
|
// global-context.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
import "path/filepath"
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
var globalCtx *SimpleStore
|
var globalCtx *SimpleStore
|
||||||
|
|
||||||
@ -63,7 +66,70 @@ func GetFuncInfo(ctx ExprContext, name string) (item ExprFunc, exists bool, owne
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GlobalCtrlSet(name string, newValue any) (currentValue any) {
|
||||||
|
if !strings.HasPrefix(name, "_") {
|
||||||
|
name = "_" + name
|
||||||
|
}
|
||||||
|
currentValue, _ = globalCtx.GetVar(name)
|
||||||
|
|
||||||
|
globalCtx.SetVar(name, newValue)
|
||||||
|
return currentValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func GlobalCtrlGet(name string) (currentValue any) {
|
||||||
|
if !strings.HasPrefix(name, "_") {
|
||||||
|
name = "_" + name
|
||||||
|
}
|
||||||
|
currentValue, _ = globalCtx.GetVar(name)
|
||||||
|
return currentValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func CtrlEnable(ctx ExprContext, name string) (currentStatus bool) {
|
||||||
|
if !strings.HasPrefix(name, "_") {
|
||||||
|
name = "_" + name
|
||||||
|
}
|
||||||
|
if v, exists := ctx.GetVar(name); exists && IsBool(v) {
|
||||||
|
currentStatus, _ = v.(bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.SetVar(name, true)
|
||||||
|
return currentStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
func CtrlDisable(ctx ExprContext, name string) (currentStatus bool) {
|
||||||
|
if !strings.HasPrefix(name, "_") {
|
||||||
|
name = "_" + name
|
||||||
|
}
|
||||||
|
if v, exists := ctx.GetVar(name); exists && IsBool(v) {
|
||||||
|
currentStatus, _ = v.(bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.SetVar(name, false)
|
||||||
|
return currentStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
func CtrlIsEnabled(ctx ExprContext, name string) (status bool) {
|
||||||
|
var v any
|
||||||
|
var exists bool
|
||||||
|
|
||||||
|
if !strings.HasPrefix(name, "_") {
|
||||||
|
name = "_" + name
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, exists = ctx.GetVar(name); !exists {
|
||||||
|
v, exists = globalCtx.GetVar(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if exists {
|
||||||
|
if b, ok := v.(bool); ok {
|
||||||
|
status = b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
globalCtx = NewSimpleStore()
|
globalCtx = NewSimpleStore()
|
||||||
|
initDefaultVars(globalCtx)
|
||||||
ImportBuiltinsFuncs(globalCtx)
|
ImportBuiltinsFuncs(globalCtx)
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,7 @@ func newAndTerm(tk *Token) (inst *term) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func evalAnd(ctx ExprContext, self *term) (v any, err error) {
|
func evalAnd(ctx ExprContext, self *term) (v any, err error) {
|
||||||
if isEnabled(ctx, ControlBoolShortcut) {
|
if CtrlIsEnabled(ctx, ControlBoolShortcut) {
|
||||||
v, err = evalAndWithShortcut(ctx, self)
|
v, err = evalAndWithShortcut(ctx, self)
|
||||||
} else {
|
} else {
|
||||||
v, err = evalAndWithoutShortcut(ctx, self)
|
v, err = evalAndWithoutShortcut(ctx, self)
|
||||||
@ -117,7 +117,7 @@ func newOrTerm(tk *Token) (inst *term) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func evalOr(ctx ExprContext, self *term) (v any, err error) {
|
func evalOr(ctx ExprContext, self *term) (v any, err error) {
|
||||||
if isEnabled(ctx, ControlBoolShortcut) {
|
if CtrlIsEnabled(ctx, ControlBoolShortcut) {
|
||||||
v, err = evalOrWithShortcut(ctx, self)
|
v, err = evalOrWithShortcut(ctx, self)
|
||||||
} else {
|
} else {
|
||||||
v, err = evalOrWithoutShortcut(ctx, self)
|
v, err = evalOrWithoutShortcut(ctx, self)
|
||||||
|
@ -34,7 +34,8 @@ func evalContextValue(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
if formatter, ok := sourceCtx.(Formatter); ok {
|
if formatter, ok := sourceCtx.(Formatter); ok {
|
||||||
v = formatter.ToString(0)
|
v = formatter.ToString(0)
|
||||||
} else {
|
} else {
|
||||||
keys := sourceCtx.EnumVars(func(name string) bool { return name[0] != '_' })
|
// keys := sourceCtx.EnumVars(func(name string) bool { return name[0] != '_' })
|
||||||
|
keys := sourceCtx.EnumVars(nil)
|
||||||
d := make(map[string]any)
|
d := make(map[string]any)
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
d[key], _ = sourceCtx.GetVar(key)
|
d[key], _ = sourceCtx.GetVar(key)
|
||||||
|
@ -54,7 +54,7 @@ func (ctx *SimpleStore) Merge(src ExprContext) {
|
|||||||
func varsCtxToBuilder(sb *strings.Builder, ctx ExprContext, indent int) {
|
func varsCtxToBuilder(sb *strings.Builder, ctx ExprContext, indent int) {
|
||||||
sb.WriteString("vars: {\n")
|
sb.WriteString("vars: {\n")
|
||||||
first := true
|
first := true
|
||||||
for _, name := range ctx.EnumVars(filterPrivName) {
|
for _, name := range ctx.EnumVars(nil) {
|
||||||
if first {
|
if first {
|
||||||
first = false
|
first = false
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user