Removed eval() function from ast interface and implementation. Check on preset control variables is now done in initDefaultVars()

This commit is contained in:
Celestino Amoroso 2024-06-07 09:31:33 +02:00
parent 0d01afcc9f
commit 227944b3fb
2 changed files with 51 additions and 26 deletions

9
ast.go
View File

@ -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 {

View File

@ -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) {