Changed SetValue() as SetVar() and GetValue() as GetVar()
This commit is contained in:
parent
6c02b02d4a
commit
088e347c95
2
ast.go
2
ast.go
@ -109,7 +109,7 @@ func (self *ast) Eval(ctx exprContext, preset bool) (result any, err error) {
|
|||||||
if self.forest != nil {
|
if self.forest != nil {
|
||||||
for i, root := range self.forest {
|
for i, root := range self.forest {
|
||||||
if result, err = root.compute(ctx); err == nil {
|
if result, err = root.compute(ctx); err == nil {
|
||||||
ctx.SetValue(preset_last_result, result)
|
ctx.SetVar(preset_last_result, result)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("error in expression nr %d: %v", i+1, err)
|
err = fmt.Errorf("error in expression nr %d: %v", i+1, err)
|
||||||
break
|
break
|
||||||
|
@ -30,8 +30,8 @@ type exprFunc interface {
|
|||||||
// ----Expression Context
|
// ----Expression Context
|
||||||
type exprContext interface {
|
type exprContext interface {
|
||||||
Clone() exprContext
|
Clone() exprContext
|
||||||
GetValue(varName string) (value any, exists bool)
|
GetVar(varName string) (value any, exists bool)
|
||||||
SetValue(varName string, value any)
|
SetVar(varName string, value any)
|
||||||
GetFuncInfo(name string) exprFunc
|
GetFuncInfo(name string) exprFunc
|
||||||
Call(name string, args []any) (result any, err error)
|
Call(name string, args []any) (result any, err error)
|
||||||
RegisterFunc(name string, f Functor, minArgs, maxArgs int)
|
RegisterFunc(name string, f Functor, minArgs, maxArgs int)
|
||||||
|
@ -39,13 +39,13 @@ func EvalStringV(source string, args []EvalArg) (result any, err error) {
|
|||||||
err = fmt.Errorf("invalid function specification: %q", arg.name)
|
err = fmt.Errorf("invalid function specification: %q", arg.name)
|
||||||
}
|
}
|
||||||
} else if integer, ok := anyInteger(arg.value); ok {
|
} else if integer, ok := anyInteger(arg.value); ok {
|
||||||
ctx.SetValue(arg.name, integer)
|
ctx.SetVar(arg.name, integer)
|
||||||
} else if float, ok := anyFloat(arg.value); ok {
|
} else if float, ok := anyFloat(arg.value); ok {
|
||||||
ctx.SetValue(arg.name, float)
|
ctx.SetVar(arg.name, float)
|
||||||
} else if _, ok := arg.value.(string); ok {
|
} else if _, ok := arg.value.(string); ok {
|
||||||
ctx.SetValue(arg.name, arg.value)
|
ctx.SetVar(arg.name, arg.value)
|
||||||
} else if _, ok := arg.value.(bool); ok {
|
} else if _, ok := arg.value.(bool); ok {
|
||||||
ctx.SetValue(arg.name, arg.value)
|
ctx.SetVar(arg.name, arg.value)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("unsupported type %T specified for item %q", arg.value, arg.name)
|
err = fmt.Errorf("unsupported type %T specified for item %q", arg.value, arg.name)
|
||||||
}
|
}
|
||||||
|
@ -51,18 +51,18 @@ func TestEvalStringA(t *testing.T) {
|
|||||||
func TestEvalString(t *testing.T) {
|
func TestEvalString(t *testing.T) {
|
||||||
|
|
||||||
ctx := NewSimpleVarStore()
|
ctx := NewSimpleVarStore()
|
||||||
ctx.SetValue("a", uint8(1))
|
ctx.SetVar("a", uint8(1))
|
||||||
ctx.SetValue("b", int8(2))
|
ctx.SetVar("b", int8(2))
|
||||||
ctx.SetValue("f", 2.0)
|
ctx.SetVar("f", 2.0)
|
||||||
// force coverage
|
// force coverage
|
||||||
ctx.SetValue("a16", uint16(1))
|
ctx.SetVar("a16", uint16(1))
|
||||||
ctx.SetValue("b16", int16(2))
|
ctx.SetVar("b16", int16(2))
|
||||||
ctx.SetValue("a32", uint32(1))
|
ctx.SetVar("a32", uint32(1))
|
||||||
ctx.SetValue("b32", int32(2))
|
ctx.SetVar("b32", int32(2))
|
||||||
ctx.SetValue("a64", uint64(1))
|
ctx.SetVar("a64", uint64(1))
|
||||||
ctx.SetValue("b64", int64(2))
|
ctx.SetVar("b64", int64(2))
|
||||||
ctx.SetValue("f32", float32(1.0))
|
ctx.SetVar("f32", float32(1.0))
|
||||||
ctx.SetValue("f64", float64(1.0))
|
ctx.SetVar("f64", float64(1.0))
|
||||||
|
|
||||||
// force coverage
|
// force coverage
|
||||||
ctx.GetFuncInfo("dummy")
|
ctx.GetFuncInfo("dummy")
|
||||||
|
@ -65,9 +65,9 @@ func (functor *funcDefFunctor) Invoke(parentCtx exprContext, name string, args [
|
|||||||
ctx := parentCtx.Clone()
|
ctx := parentCtx.Clone()
|
||||||
for i, p := range functor.params {
|
for i, p := range functor.params {
|
||||||
if i < len(args) {
|
if i < len(args) {
|
||||||
ctx.SetValue(p, args[i])
|
ctx.SetVar(p, args[i])
|
||||||
} else {
|
} else {
|
||||||
ctx.SetValue(p, nil)
|
ctx.SetVar(p, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result, err = functor.expr.Eval(ctx, false)
|
result, err = functor.expr.Eval(ctx, false)
|
||||||
|
@ -23,7 +23,7 @@ func newVarTerm(tk *Token) *term {
|
|||||||
// -------- eval func
|
// -------- eval func
|
||||||
func evalVar(ctx exprContext, self *term) (v any, err error) {
|
func evalVar(ctx exprContext, self *term) (v any, err error) {
|
||||||
var exists bool
|
var exists bool
|
||||||
if v, exists = ctx.GetValue(self.tk.source); !exists {
|
if v, exists = ctx.GetVar(self.tk.source); !exists {
|
||||||
err = fmt.Errorf("undefined variable %q", self.tk.source)
|
err = fmt.Errorf("undefined variable %q", self.tk.source)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -33,7 +33,7 @@ func evalAssign(ctx exprContext, self *term) (v any, err error) {
|
|||||||
if functor, ok := v.(Functor); ok {
|
if functor, ok := v.(Functor); ok {
|
||||||
ctx.RegisterFunc(leftTerm.source(), functor, 0, -1)
|
ctx.RegisterFunc(leftTerm.source(), functor, 0, -1)
|
||||||
} else {
|
} else {
|
||||||
ctx.SetValue(leftTerm.tk.source, v)
|
ctx.SetVar(leftTerm.tk.source, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -152,8 +152,8 @@ func TestParser(t *testing.T) {
|
|||||||
var gotErr error
|
var gotErr error
|
||||||
|
|
||||||
ctx := NewSimpleFuncStore()
|
ctx := NewSimpleFuncStore()
|
||||||
ctx.SetValue("var1", int64(123))
|
ctx.SetVar("var1", int64(123))
|
||||||
ctx.SetValue("var2", "abc")
|
ctx.SetVar("var2", "abc")
|
||||||
importMathFuncs(ctx)
|
importMathFuncs(ctx)
|
||||||
importImportFunc(ctx)
|
importImportFunc(ctx)
|
||||||
parser := NewParser(ctx)
|
parser := NewParser(ctx)
|
||||||
@ -228,8 +228,8 @@ func TestListParser(t *testing.T) {
|
|||||||
var gotErr error
|
var gotErr error
|
||||||
|
|
||||||
ctx := NewSimpleFuncStore()
|
ctx := NewSimpleFuncStore()
|
||||||
ctx.SetValue("var1", int64(123))
|
ctx.SetVar("var1", int64(123))
|
||||||
ctx.SetValue("var2", "abc")
|
ctx.SetVar("var2", "abc")
|
||||||
importMathFuncs(ctx)
|
importMathFuncs(ctx)
|
||||||
parser := NewParser(ctx)
|
parser := NewParser(ctx)
|
||||||
|
|
||||||
|
16
preset.go
16
preset.go
@ -16,28 +16,28 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initDefaultVars(ctx exprContext) {
|
func initDefaultVars(ctx exprContext) {
|
||||||
ctx.SetValue(preset_bool_shortcut, true)
|
ctx.SetVar(preset_bool_shortcut, true)
|
||||||
ctx.SetValue(preset_import_path, init_import_path)
|
ctx.SetVar(preset_import_path, init_import_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func enable(ctx exprContext, name string) {
|
func enable(ctx exprContext, name string) {
|
||||||
if strings.HasPrefix(name, "_") {
|
if strings.HasPrefix(name, "_") {
|
||||||
ctx.SetValue(name, true)
|
ctx.SetVar(name, true)
|
||||||
} else {
|
} else {
|
||||||
ctx.SetValue("_"+name, true)
|
ctx.SetVar("_"+name, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func disable(ctx exprContext, name string) {
|
func disable(ctx exprContext, name string) {
|
||||||
if strings.HasPrefix(name, "_") {
|
if strings.HasPrefix(name, "_") {
|
||||||
ctx.SetValue(name, false)
|
ctx.SetVar(name, false)
|
||||||
} else {
|
} else {
|
||||||
ctx.SetValue("_"+name, false)
|
ctx.SetVar("_"+name, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isEnabled(ctx exprContext, name string) (status bool) {
|
func isEnabled(ctx exprContext, name string) (status bool) {
|
||||||
if v, exists := ctx.GetValue(name); exists {
|
if v, exists := ctx.GetVar(name); exists {
|
||||||
if b, ok := v.(bool); ok {
|
if b, ok := v.(bool); ok {
|
||||||
status = b
|
status = b
|
||||||
}
|
}
|
||||||
@ -47,7 +47,7 @@ func isEnabled(ctx exprContext, name string) (status bool) {
|
|||||||
|
|
||||||
func getPresetString(ctx exprContext, name string) (s string, exists bool) {
|
func getPresetString(ctx exprContext, name string) (s string, exists bool) {
|
||||||
var v any
|
var v any
|
||||||
if v, exists = ctx.GetValue(name); exists {
|
if v, exists = ctx.GetVar(name); exists {
|
||||||
s, exists = v.(string)
|
s, exists = v.(string)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -40,12 +40,12 @@ func (ctx *SimpleFuncStore) Clone() exprContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *SimpleFuncStore) GetValue(varName string) (v any, exists bool) {
|
func (ctx *SimpleFuncStore) GetVar(varName string) (v any, exists bool) {
|
||||||
v, exists = ctx.varStore[varName]
|
v, exists = ctx.varStore[varName]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *SimpleFuncStore) SetValue(varName string, value any) {
|
func (ctx *SimpleFuncStore) SetVar(varName string, value any) {
|
||||||
ctx.varStore[varName] = value
|
ctx.varStore[varName] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,12 +18,12 @@ func (ctx *SimpleVarStore) Clone() (clone exprContext) {
|
|||||||
return clone
|
return clone
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *SimpleVarStore) GetValue(varName string) (v any, exists bool) {
|
func (ctx *SimpleVarStore) GetVar(varName string) (v any, exists bool) {
|
||||||
v, exists = ctx.store[varName]
|
v, exists = ctx.store[varName]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *SimpleVarStore) SetValue(varName string, value any) {
|
func (ctx *SimpleVarStore) SetVar(varName string, value any) {
|
||||||
ctx.store[varName] = value
|
ctx.store[varName] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user