aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2023-11-08 21:08:05 +0000
committerBobby <[email protected]>2023-11-08 21:08:05 +0000
commit4d4840e1c286eb06c474b32435b5ae62b47e8192 (patch)
tree26ea75500889d744624a89af431f51d2f28ab196
parent152e63c1865d8bc1df36f54218cc9286b7fd1ff2 (diff)
downloadmana-4d4840e1c286eb06c474b32435b5ae62b47e8192.tar.xz
mana-4d4840e1c286eb06c474b32435b5ae62b47e8192.zip
parser:operator precedence tests
-rw-r--r--parser/parser_test.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/parser/parser_test.go b/parser/parser_test.go
index d3e2d1d..ddacb17 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -316,3 +316,74 @@ func TestParsingInfixExpressions(t *testing.T) {
}
}
}
+
+// Operator precedence tests.
+
+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 {
+ var l *lexer.Lexer = lexer.New(tt.input)
+ var p *Parser = New(l)
+ var program *ast.Program = p.ParseProgram()
+ checkParserErrors(t, p)
+
+ var actual string = program.String()
+
+ if actual != tt.expected {
+ t.Errorf("expected=%q, got=%q", tt.expected, actual)
+ }
+ }
+}