aboutsummaryrefslogtreecommitdiff
path: root/parser/parser_test.go
diff options
context:
space:
mode:
authorBobby <[email protected]>2023-11-08 17:56:53 +0000
committerBobby <[email protected]>2023-11-08 17:56:53 +0000
commit152e63c1865d8bc1df36f54218cc9286b7fd1ff2 (patch)
tree4c8b996fb65d8b86a0c0d1ee620ff5f13732963e /parser/parser_test.go
parentaed19195e4476ed6933eac2d1d748bcdcb4277e7 (diff)
downloadmana-152e63c1865d8bc1df36f54218cc9286b7fd1ff2.tar.xz
mana-152e63c1865d8bc1df36f54218cc9286b7fd1ff2.zip
parse infix operators
Diffstat (limited to 'parser/parser_test.go')
-rw-r--r--parser/parser_test.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/parser/parser_test.go b/parser/parser_test.go
index 828db24..d3e2d1d 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -259,3 +259,60 @@ func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
return true
}
+
+// Infix expression tests.
+
+func TestParsingInfixExpressions(t *testing.T) {
+ var infixTests = []struct {
+ input string
+ leftValue int64
+ operator string
+ rightValue int64
+ }{
+ {"5 + 5;", 5, "+", 5},
+ {"5 - 5;", 5, "-", 5},
+ {"5 * 5;", 5, "*", 5},
+ {"5 / 5;", 5, "/", 5},
+ {"5 > 5;", 5, ">", 5},
+ {"5 < 5;", 5, "<", 5},
+ {"5 == 5;", 5, "==", 5},
+ {"5 != 5;", 5, "!=", 5},
+ }
+
+ for _, tt := range infixTests {
+ var l *lexer.Lexer = lexer.New(tt.input)
+ var p *Parser = New(l)
+
+ var program *ast.Program = p.ParseProgram()
+ checkParserErrors(t, p)
+
+
+ if len(program.Statements) != 1 {
+ t.Fatalf("program.Statements does not contain %d statements, got=%d\n", 1, len(program.Statements))
+ }
+
+ stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
+
+ if !ok {
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement, got=%T", program.Statements[0])
+ }
+
+ exp, ok := stmt.Expression.(*ast.InfixExpression)
+
+ if !ok {
+ t.Fatalf("exp is not ast.InfixExpression. got=%T", stmt.Expression)
+ }
+
+ if !testIntegerLiteral(t, exp.Left, tt.leftValue) {
+ return
+ }
+
+ if exp.Operator != tt.operator {
+ t.Fatalf("exp.Operator is not '%s', got=%s", tt.operator, exp.Operator)
+ }
+
+ if !testIntegerLiteral(t, exp.Right, tt.rightValue) {
+ return
+ }
+ }
+}