Changed SetValue() as SetVar() and GetValue() as GetVar()

This commit is contained in:
Celestino Amoroso 2024-04-03 06:29:57 +02:00
parent 6c02b02d4a
commit 088e347c95
11 changed files with 38 additions and 38 deletions

2
ast.go
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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