ast.go: presetting of system variables before starting evaluation process
This commit is contained in:
parent
e3e5ad7da8
commit
94ad968d5e
35
ast.go
35
ast.go
@ -79,9 +79,44 @@ func (self *ast) insert(tree, node *term) (root *term, err error) {
|
||||
|
||||
func (self *ast) eval(ctx exprContext) (result any, err error) {
|
||||
if self.root != nil {
|
||||
initDefaultVars(ctx)
|
||||
result, err = self.root.compute(ctx)
|
||||
} else {
|
||||
err = errors.New("empty expression")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Preset variables
|
||||
const (
|
||||
preset_bool_shortcut = "_bool_shortcut"
|
||||
)
|
||||
|
||||
func initDefaultVars(ctx exprContext) {
|
||||
ctx.SetValue(preset_bool_shortcut, true)
|
||||
}
|
||||
|
||||
func enable(ctx exprContext, name string) {
|
||||
if strings.HasPrefix(name, "_") {
|
||||
ctx.SetValue(name, true)
|
||||
} else {
|
||||
ctx.SetValue("_"+name, true)
|
||||
}
|
||||
}
|
||||
|
||||
func disable(ctx exprContext, name string) {
|
||||
if strings.HasPrefix(name, "_") {
|
||||
ctx.SetValue(name, false)
|
||||
} else {
|
||||
ctx.SetValue("_"+name, false)
|
||||
}
|
||||
}
|
||||
|
||||
func isEnabled(ctx exprContext, name string) (status bool) {
|
||||
if v, exists := ctx.GetValue(name); exists {
|
||||
if b, ok := v.(bool); ok {
|
||||
status = b
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user