From 3498876f06104515002191468fd99019d40051c2 Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 14 Jun 2022 00:07:40 +0000 Subject: Fix BinExpr precendence not being drilled down --- pkg/lang/parser/bin_order.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 pkg/lang/parser/bin_order.go (limited to 'pkg/lang/parser/bin_order.go') diff --git a/pkg/lang/parser/bin_order.go b/pkg/lang/parser/bin_order.go new file mode 100644 index 0000000..666d1fc --- /dev/null +++ b/pkg/lang/parser/bin_order.go @@ -0,0 +1,34 @@ +package parser + +import ( + "jinx/pkg/lang/ast" +) + +func (p *Parser) mergeIntoBinary(left ast.Expr, op ast.BinOp, right ast.Expr) ast.ExprBinary { + if right.Kind == ast.ExprKindBinary { + rightBin := right.Value.(ast.ExprBinary) + + needsSwitch := (op.Precedence() > rightBin.Op.Precedence()) || + (op.Precedence() == rightBin.Op.Precedence() && op.Associativity() == ast.AssociativityLeft) + + if needsSwitch { + leftBin := p.mergeIntoBinary(left, op, rightBin.Left) + + left = ast.Expr{ + At: left.At, + Kind: ast.ExprKindBinary, + Value: leftBin, + } + + right = rightBin.Right + + op = rightBin.Op + } + } + + return ast.ExprBinary{ + Left: left, + Op: op, + Right: right, + } +} -- cgit 1.4.1