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 {
|
type Expr interface {
|
||||||
Eval(ctx ExprContext) (result any, err error)
|
Eval(ctx ExprContext) (result any, err error)
|
||||||
eval(ctx ExprContext, preset bool) (result any, err error)
|
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,16 +105,10 @@ func (self *ast) Finish() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *ast) Eval(ctx ExprContext) (result any, err error) {
|
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()
|
self.Finish()
|
||||||
|
|
||||||
if self.root != nil {
|
if self.root != nil {
|
||||||
if preset {
|
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 {
|
||||||
|
68
control.go
68
control.go
@ -8,10 +8,11 @@ import "strings"
|
|||||||
|
|
||||||
// Preset control variables
|
// Preset control variables
|
||||||
const (
|
const (
|
||||||
ControlLastResult = "last"
|
ControlPreset = "_preset"
|
||||||
ControlBoolShortcut = "_bool_shortcut"
|
ControlLastResult = "last"
|
||||||
ControlImportPath = "_import_path"
|
ControlBoolShortcut = "_bool_shortcut"
|
||||||
ControlPluginPath = "_plugin_path"
|
ControlSearchPath = "_search_path"
|
||||||
|
ControlParentContext = "_parent_context"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Other control variables
|
// Other control variables
|
||||||
@ -21,30 +22,61 @@ const (
|
|||||||
|
|
||||||
// Initial values
|
// Initial values
|
||||||
const (
|
const (
|
||||||
init_import_path = "~/.local/lib/go-pkg/expr/sources:/usr/local/lib/go-pkg/expr/sources:/usr/lib/go-pkg/expr/sources"
|
init_search_path = "~/.local/lib/go-pkg/expr:/usr/local/lib/go-pkg/expr:/usr/lib/go-pkg/expr"
|
||||||
init_plugin_path = "~/.local/lib/go-pkg/expr/plugins:/usr/local/lib/go-pkg/expr/plugins:/usr/lib/go-pkg/expr/plugins"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func initDefaultVars(ctx ExprContext) {
|
func initDefaultVars(ctx ExprContext) {
|
||||||
|
if _, exists := ctx.GetVar(ControlPreset); exists {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx.SetVar(ControlPreset, true)
|
||||||
ctx.SetVar(ControlBoolShortcut, true)
|
ctx.SetVar(ControlBoolShortcut, true)
|
||||||
ctx.SetVar(ControlImportPath, init_import_path)
|
ctx.SetVar(ControlSearchPath, init_search_path)
|
||||||
ctx.SetVar(ControlPluginPath, init_plugin_path)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func enable(ctx ExprContext, name string) {
|
func CtrlEnable(ctx ExprContext, name string) (currentStatus bool) {
|
||||||
if strings.HasPrefix(name, "_") {
|
if !strings.HasPrefix(name, "_") {
|
||||||
ctx.SetVar(name, true)
|
name = "_" + name
|
||||||
} else {
|
|
||||||
ctx.SetVar("_"+name, true)
|
|
||||||
}
|
}
|
||||||
|
if v, exists := ctx.GetVar(name); exists && IsBool(v) {
|
||||||
|
currentStatus, _ = v.(bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.SetVar(name, true)
|
||||||
|
return currentStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
func disable(ctx ExprContext, name string) {
|
func CtrlDisable(ctx ExprContext, name string) (currentStatus bool) {
|
||||||
if strings.HasPrefix(name, "_") {
|
if !strings.HasPrefix(name, "_") {
|
||||||
ctx.SetVar(name, false)
|
name = "_" + name
|
||||||
} else {
|
|
||||||
ctx.SetVar("_"+name, false)
|
|
||||||
}
|
}
|
||||||
|
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) {
|
func isEnabled(ctx ExprContext, name string) (status bool) {
|
||||||
|
Loading…
Reference in New Issue
Block a user