diff --git a/ast.go b/ast.go index 6319187..627984b 100644 --- a/ast.go +++ b/ast.go @@ -10,7 +10,6 @@ import ( type Expr interface { Eval(ctx ExprContext) (result any, err error) - eval(ctx ExprContext, preset bool) (result any, err error) String() string } @@ -106,16 +105,10 @@ func (self *ast) Finish() { } func (self *ast) Eval(ctx ExprContext) (result any, err error) { - return self.eval(ctx, true) -} - -func (self *ast) eval(ctx ExprContext, preset bool) (result any, err error) { self.Finish() if self.root != nil { - if preset { - initDefaultVars(ctx) - } + initDefaultVars(ctx) if self.forest != nil { for _, root := range self.forest { if result, err = root.compute(ctx); err == nil { diff --git a/control.go b/control.go index 0e6c7eb..028ca58 100644 --- a/control.go +++ b/control.go @@ -8,10 +8,11 @@ import "strings" // Preset control variables const ( - ControlLastResult = "last" - ControlBoolShortcut = "_bool_shortcut" - ControlImportPath = "_import_path" - ControlPluginPath = "_plugin_path" + ControlPreset = "_preset" + ControlLastResult = "last" + ControlBoolShortcut = "_bool_shortcut" + ControlSearchPath = "_search_path" + ControlParentContext = "_parent_context" ) // Other control variables @@ -21,30 +22,61 @@ const ( // Initial values const ( - init_import_path = "~/.local/lib/go-pkg/expr/sources:/usr/local/lib/go-pkg/expr/sources:/usr/lib/go-pkg/expr/sources" - init_plugin_path = "~/.local/lib/go-pkg/expr/plugins:/usr/local/lib/go-pkg/expr/plugins:/usr/lib/go-pkg/expr/plugins" + init_search_path = "~/.local/lib/go-pkg/expr:/usr/local/lib/go-pkg/expr:/usr/lib/go-pkg/expr" ) func initDefaultVars(ctx ExprContext) { + if _, exists := ctx.GetVar(ControlPreset); exists { + return + } + ctx.SetVar(ControlPreset, true) ctx.SetVar(ControlBoolShortcut, true) - ctx.SetVar(ControlImportPath, init_import_path) - ctx.SetVar(ControlPluginPath, init_plugin_path) + ctx.SetVar(ControlSearchPath, init_search_path) } -func enable(ctx ExprContext, name string) { - if strings.HasPrefix(name, "_") { - ctx.SetVar(name, true) - } else { - ctx.SetVar("_"+name, true) +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 disable(ctx ExprContext, name string) { - if strings.HasPrefix(name, "_") { - ctx.SetVar(name, false) - } else { - ctx.SetVar("_"+name, false) +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) {