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