From 193bd5c2d574df45d15559554d5ee34caf999ffd Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Sat, 23 Mar 2024 14:10:53 +0100 Subject: [PATCH] Simplify expression parsing --- parser.go | 105 ++++++------------------------------------------------ 1 file changed, 11 insertions(+), 94 deletions(-) diff --git a/parser.go b/parser.go index e644059..ed57dde 100644 --- a/parser.go +++ b/parser.go @@ -487,8 +487,8 @@ func (p *Parser) tryUnaryExpression() (*Expression, error) { return nil, nil } -func (p *Parser) tryMultiplicativeExpression() (*Expression, error) { - left, err := p.tryUnaryExpression() +func (p *Parser) tryBinaryExpression0(opFunc func() (*Expression, error), operators ...Operator) (*Expression, error) { + left, err := opFunc() if err != nil { return nil, err } @@ -498,7 +498,7 @@ func (p *Parser) tryMultiplicativeExpression() (*Expression, error) { } for { - op, err := p.tryOperator(Operator_Multiply, Operator_Divide, Operator_Modulo) + op, err := p.tryOperator(operators...) if err != nil { return nil, err } @@ -507,7 +507,7 @@ func (p *Parser) tryMultiplicativeExpression() (*Expression, error) { return left, nil } - right, err := p.tryUnaryExpression() + right, err := opFunc() if err != nil { return nil, err } @@ -520,103 +520,20 @@ func (p *Parser) tryMultiplicativeExpression() (*Expression, error) { } } +func (p *Parser) tryMultiplicativeExpression() (*Expression, error) { + return p.tryBinaryExpression0(p.tryUnaryExpression, Operator_Multiply, Operator_Divide, Operator_Modulo) +} + func (p *Parser) tryAdditiveExpression() (*Expression, error) { - left, err := p.tryMultiplicativeExpression() - if err != nil { - return nil, err - } - - if left == nil { - return nil, nil - } - - for { - op, err := p.tryOperator(Operator_Plus, Operator_Minus) - if err != nil { - return nil, err - } - - if op == nil { - return left, nil - } - - right, err := p.tryMultiplicativeExpression() - if err != nil { - return nil, err - } - - if right == nil { - return nil, p.error("expected expression") - } - - left = &Expression{Type: Expression_Binary, Value: BinaryExpression{Operation: getArithmeticOperation(*op), Left: *left, Right: *right}, Position: left.Position} - } + return p.tryBinaryExpression0(p.tryMultiplicativeExpression, Operator_Plus, Operator_Minus) } func (p *Parser) tryRelationalExpression() (*Expression, error) { - left, err := p.tryAdditiveExpression() - if err != nil { - return nil, err - } - - if left == nil { - return nil, nil - } - - for { - op, err := p.tryOperator(Operator_Less, Operator_Greater, Operator_LessEquals, Operator_GreaterEquals) - if err != nil { - return nil, err - } - - if op == nil { - return left, nil - } - - right, err := p.tryAdditiveExpression() - if err != nil { - return nil, err - } - - if right == nil { - return nil, p.error("expected expression") - } - - left = &Expression{Type: Expression_Binary, Value: BinaryExpression{Operation: getArithmeticOperation(*op), Left: *left, Right: *right}, Position: left.Position} - } + return p.tryBinaryExpression0(p.tryAdditiveExpression, Operator_Less, Operator_Greater, Operator_LessEquals, Operator_GreaterEquals) } func (p *Parser) tryEqualityExpression() (*Expression, error) { - left, err := p.tryRelationalExpression() - if err != nil { - return nil, err - } - - if left == nil { - return nil, nil - } - - for { - op, err := p.tryOperator(Operator_EqualsEquals, Operator_NotEquals) - if err != nil { - return nil, err - } - - if op == nil { - return left, nil - } - - right, err := p.tryRelationalExpression() - if err != nil { - return nil, err - } - - if right == nil { - return nil, p.error("expected expression") - } - - left = &Expression{Type: Expression_Binary, Value: BinaryExpression{Operation: getArithmeticOperation(*op), Left: *left, Right: *right}, Position: left.Position} - } + return p.tryBinaryExpression0(p.tryRelationalExpression, Operator_EqualsEquals, Operator_NotEquals) } func (p *Parser) tryBinaryExpression() (*Expression, error) {