From d6a16070410dd1c45918303a0427c10e56f0175c Mon Sep 17 00:00:00 2001 From: Celestino Amoroso Date: Sun, 19 May 2024 01:34:07 +0200 Subject: [PATCH] The content of round bracket now returns an expressione term. This way the content is decoupled from external terms. --- operand-expr.go | 7 ++++--- parser.go | 3 ++- parser_test.go | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/operand-expr.go b/operand-expr.go index 3038e2c..7069fbc 100644 --- a/operand-expr.go +++ b/operand-expr.go @@ -7,7 +7,8 @@ package expr import "fmt" // -------- expr term -func newExprTerm(tk *Token) *term { +func newExprTerm(root *term) *term { + tk := NewValueToken(root.tk.row, root.tk.col, SymExpression, root.source(), root) return &term{ tk: *tk, parent: nil, @@ -20,8 +21,8 @@ func newExprTerm(tk *Token) *term { // -------- eval expr func evalExpr(ctx ExprContext, self *term) (v any, err error) { - if expr, ok := self.value().(Expr); ok { - v, err = expr.eval(ctx, false) + if expr, ok := self.value().(*term); ok { + v, err = expr.compute(ctx) } else { err = fmt.Errorf("expression expected, got %T", self.value()) } diff --git a/parser.go b/parser.go index b22c9a8..3428060 100644 --- a/parser.go +++ b/parser.go @@ -277,6 +277,7 @@ func (self *parser) parseDictionary(scanner *scanner, allowVarRef bool) (subtree err = scanner.Previous().ErrorExpectedGot("}") } else { subtree = newDictTerm(args) + // subtree = newMapTerm(args) } } return @@ -389,7 +390,7 @@ func (self *parser) parseGeneral(scanner *scanner, allowForest bool, allowVarRef var subTree *ast if subTree, err = self.parseGeneral(scanner, false, allowVarRef, SymClosedRound); err == nil { subTree.root.priority = priValue - err = tree.addTerm(subTree.root) + err = tree.addTerm(newExprTerm(subTree.root)) currentTerm = subTree.root } case SymFuncCall: diff --git a/parser_test.go b/parser_test.go index e75f0a3..5705da5 100644 --- a/parser_test.go +++ b/parser_test.go @@ -137,7 +137,7 @@ func TestGeneralParser(t *testing.T) { /* 116 */ {`null`, nil, errors.New(`undefined variable or function "null"`)}, /* 117 */ {`{"key"}`, nil, errors.New(`[1:8] expected ":", got "}"`)}, /* 118 */ {`{"key":}`, nil, errors.New(`[1:9] expected "dictionary-value", got "}"`)}, - /* 119 */ {`{}`, map[any]any{}, nil}, + /* 119 */ {`{}`, &DictType{}, nil}, /* 120 */ {`1|2`, newFraction(1, 2), nil}, /* 121 */ {`1|2 + 1`, newFraction(3, 2), nil}, /* 122 */ {`1|2 - 1`, newFraction(-1, 2), nil},