diff --git a/ast.go b/ast.go index 704ab0b..a3be6c8 100644 --- a/ast.go +++ b/ast.go @@ -109,7 +109,7 @@ func (self *ast) Eval(ctx exprContext, preset bool) (result any, err error) { if self.forest != nil { for i, root := range self.forest { if result, err = root.compute(ctx); err == nil { - ctx.SetValue(preset_last_result, result) + ctx.SetVar(preset_last_result, result) } else { err = fmt.Errorf("error in expression nr %d: %v", i+1, err) break diff --git a/context.go b/context.go index 7ae7fd4..e32862f 100644 --- a/context.go +++ b/context.go @@ -30,8 +30,8 @@ type exprFunc interface { // ----Expression Context type exprContext interface { Clone() exprContext - GetValue(varName string) (value any, exists bool) - SetValue(varName string, value any) + GetVar(varName string) (value any, exists bool) + SetVar(varName string, value any) GetFuncInfo(name string) exprFunc Call(name string, args []any) (result any, err error) RegisterFunc(name string, f Functor, minArgs, maxArgs int) diff --git a/helpers.go b/helpers.go index 1765974..17a017a 100644 --- a/helpers.go +++ b/helpers.go @@ -39,13 +39,13 @@ func EvalStringV(source string, args []EvalArg) (result any, err error) { err = fmt.Errorf("invalid function specification: %q", arg.name) } } 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 { - ctx.SetValue(arg.name, float) + ctx.SetVar(arg.name, float) } 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 { - ctx.SetValue(arg.name, arg.value) + ctx.SetVar(arg.name, arg.value) } else { err = fmt.Errorf("unsupported type %T specified for item %q", arg.value, arg.name) } diff --git a/helpers_test.go b/helpers_test.go index a78cc50..df48693 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -51,18 +51,18 @@ func TestEvalStringA(t *testing.T) { func TestEvalString(t *testing.T) { ctx := NewSimpleVarStore() - ctx.SetValue("a", uint8(1)) - ctx.SetValue("b", int8(2)) - ctx.SetValue("f", 2.0) + ctx.SetVar("a", uint8(1)) + ctx.SetVar("b", int8(2)) + ctx.SetVar("f", 2.0) // force coverage - ctx.SetValue("a16", uint16(1)) - ctx.SetValue("b16", int16(2)) - ctx.SetValue("a32", uint32(1)) - ctx.SetValue("b32", int32(2)) - ctx.SetValue("a64", uint64(1)) - ctx.SetValue("b64", int64(2)) - ctx.SetValue("f32", float32(1.0)) - ctx.SetValue("f64", float64(1.0)) + ctx.SetVar("a16", uint16(1)) + ctx.SetVar("b16", int16(2)) + ctx.SetVar("a32", uint32(1)) + ctx.SetVar("b32", int32(2)) + ctx.SetVar("a64", uint64(1)) + ctx.SetVar("b64", int64(2)) + ctx.SetVar("f32", float32(1.0)) + ctx.SetVar("f64", float64(1.0)) // force coverage ctx.GetFuncInfo("dummy") diff --git a/operand-func.go b/operand-func.go index 3e18afd..b34c50e 100644 --- a/operand-func.go +++ b/operand-func.go @@ -65,9 +65,9 @@ func (functor *funcDefFunctor) Invoke(parentCtx exprContext, name string, args [ ctx := parentCtx.Clone() for i, p := range functor.params { if i < len(args) { - ctx.SetValue(p, args[i]) + ctx.SetVar(p, args[i]) } else { - ctx.SetValue(p, nil) + ctx.SetVar(p, nil) } } result, err = functor.expr.Eval(ctx, false) diff --git a/operand-var.go b/operand-var.go index 4c27376..4158e56 100644 --- a/operand-var.go +++ b/operand-var.go @@ -23,7 +23,7 @@ func newVarTerm(tk *Token) *term { // -------- eval func func evalVar(ctx exprContext, self *term) (v any, err error) { 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) } return diff --git a/operator-assign.go b/operator-assign.go index d6e3367..320d918 100644 --- a/operator-assign.go +++ b/operator-assign.go @@ -33,7 +33,7 @@ func evalAssign(ctx exprContext, self *term) (v any, err error) { if functor, ok := v.(Functor); ok { ctx.RegisterFunc(leftTerm.source(), functor, 0, -1) } else { - ctx.SetValue(leftTerm.tk.source, v) + ctx.SetVar(leftTerm.tk.source, v) } } return diff --git a/parser_test.go b/parser_test.go index 985e6a7..6b09b6e 100644 --- a/parser_test.go +++ b/parser_test.go @@ -152,8 +152,8 @@ func TestParser(t *testing.T) { var gotErr error ctx := NewSimpleFuncStore() - ctx.SetValue("var1", int64(123)) - ctx.SetValue("var2", "abc") + ctx.SetVar("var1", int64(123)) + ctx.SetVar("var2", "abc") importMathFuncs(ctx) importImportFunc(ctx) parser := NewParser(ctx) @@ -228,8 +228,8 @@ func TestListParser(t *testing.T) { var gotErr error ctx := NewSimpleFuncStore() - ctx.SetValue("var1", int64(123)) - ctx.SetValue("var2", "abc") + ctx.SetVar("var1", int64(123)) + ctx.SetVar("var2", "abc") importMathFuncs(ctx) parser := NewParser(ctx) diff --git a/preset.go b/preset.go index e4804db..f743b3c 100644 --- a/preset.go +++ b/preset.go @@ -16,28 +16,28 @@ const ( ) func initDefaultVars(ctx exprContext) { - ctx.SetValue(preset_bool_shortcut, true) - ctx.SetValue(preset_import_path, init_import_path) + ctx.SetVar(preset_bool_shortcut, true) + ctx.SetVar(preset_import_path, init_import_path) } func enable(ctx exprContext, name string) { if strings.HasPrefix(name, "_") { - ctx.SetValue(name, true) + ctx.SetVar(name, true) } else { - ctx.SetValue("_"+name, true) + ctx.SetVar("_"+name, true) } } func disable(ctx exprContext, name string) { if strings.HasPrefix(name, "_") { - ctx.SetValue(name, false) + ctx.SetVar(name, false) } else { - ctx.SetValue("_"+name, false) + ctx.SetVar("_"+name, false) } } 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 { status = b } @@ -47,7 +47,7 @@ func isEnabled(ctx exprContext, name string) (status bool) { func getPresetString(ctx exprContext, name string) (s string, exists bool) { var v any - if v, exists = ctx.GetValue(name); exists { + if v, exists = ctx.GetVar(name); exists { s, exists = v.(string) } return diff --git a/simple-func-store.go b/simple-func-store.go index a55c509..0c33076 100644 --- a/simple-func-store.go +++ b/simple-func-store.go @@ -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] return } -func (ctx *SimpleFuncStore) SetValue(varName string, value any) { +func (ctx *SimpleFuncStore) SetVar(varName string, value any) { ctx.varStore[varName] = value } diff --git a/simple-var-store.go b/simple-var-store.go index 613ec9c..ac33b07 100644 --- a/simple-var-store.go +++ b/simple-var-store.go @@ -18,12 +18,12 @@ func (ctx *SimpleVarStore) Clone() (clone exprContext) { 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] return } -func (ctx *SimpleVarStore) SetValue(varName string, value any) { +func (ctx *SimpleVarStore) SetVar(varName string, value any) { ctx.store[varName] = value }