Removed eval() function from ast interface and implementation. Check on preset control variables is now done in initDefaultVars()
This commit is contained in:
parent
0d01afcc9f
commit
227944b3fb
9
ast.go
9
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 {
|
||||
|
68
control.go
68
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user