Skip to content

Commit

Permalink
Add parser tests for operator precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
manishmeganathan committed Jun 13, 2021
1 parent e31fb49 commit 1ee88f6
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,40 @@ func TestParsingInfixExpressions(t *testing.T) {
}
}

func TestOperatorPrecedenceParsing(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"-a * b", "((-a) * b)"},
{"!-a", "(!(-a))"},
{"a + b + c", "((a + b) + c)"},
{"a + b - c", "((a + b) - c)"},
{"a * b * c", "((a * b) * c)"},
{"a * b / c", "((a * b) / c)"},
{"a + b / c", "(a + (b / c))"},
{"a + b * c + d / e - f", "(((a + (b * c)) + (d / e)) - f)"},
{"3 + 4; -5 * 5", "(3 + 4)((-5) * 5)"},
{"5 > 4 == 3 < 4", "((5 > 4) == (3 < 4))"},
{"5 < 4 != 3 > 4", "((5 < 4) != (3 > 4))"},
{"3 + 4 * 5 == 3 * 1 + 4 * 5", "((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))"},
}

for _, tt := range tests {

l := lexer.NewLexer(tt.input)
p := NewParser(l)

program := p.ParseProgram()
checkParserErrors(t, p)

actual := program.String()
if actual != tt.expected {
t.Errorf("expected=%q, got=%q", tt.expected, actual)
}
}
}

func testIntegerLiteral(t *testing.T, il syntaxtree.Expression, value int64) bool {
integ, ok := il.(*syntaxtree.IntegerLiteral)
if !ok {
Expand Down

0 comments on commit 1ee88f6

Please sign in to comment.