|
|
|
|
@ -430,3 +430,55 @@ func TestParseWhileStatement(t *testing.T) {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseForStatement(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
tokens []token.Token
|
|
|
|
|
expected string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "simple for statement",
|
|
|
|
|
tokens: []token.Token{
|
|
|
|
|
{Type: token.FOR, Lexeme: "for"},
|
|
|
|
|
{Type: token.LEFT_PAREN, Lexeme: "("},
|
|
|
|
|
{Type: token.VAR, Lexeme: "var"},
|
|
|
|
|
{Type: token.IDENTIFIER, Lexeme: "i"},
|
|
|
|
|
{Type: token.EQUAL, Lexeme: "="},
|
|
|
|
|
{Type: token.NUMBER, Lexeme: "0", Literal: 0.0},
|
|
|
|
|
{Type: token.SEMICOLON, Lexeme: ";"},
|
|
|
|
|
{Type: token.IDENTIFIER, Lexeme: "i"},
|
|
|
|
|
{Type: token.LESS, Lexeme: "<"},
|
|
|
|
|
{Type: token.NUMBER, Lexeme: "10", Literal: 10.0},
|
|
|
|
|
{Type: token.SEMICOLON, Lexeme: ";"},
|
|
|
|
|
{Type: token.IDENTIFIER, Lexeme: "i"},
|
|
|
|
|
{Type: token.EQUAL, Lexeme: "="},
|
|
|
|
|
{Type: token.IDENTIFIER, Lexeme: "i"},
|
|
|
|
|
{Type: token.PLUS, Lexeme: "+"},
|
|
|
|
|
{Type: token.NUMBER, Lexeme: "1", Literal: 1.0},
|
|
|
|
|
{Type: token.RIGHT_PAREN, Lexeme: ")"},
|
|
|
|
|
{Type: token.PRINT, Lexeme: "print"},
|
|
|
|
|
{Type: token.IDENTIFIER, Lexeme: "i"},
|
|
|
|
|
{Type: token.SEMICOLON, Lexeme: ";"},
|
|
|
|
|
{Type: token.EOF},
|
|
|
|
|
},
|
|
|
|
|
expected: "{\n (var i 0)\n (while (< i 10) {\n {\n (print i)\n (= i (+ i 1))\n}\n})\n}\n",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
parser := New(tt.tokens, errors.NewMockErrorLogger())
|
|
|
|
|
stmts := parser.Parse()
|
|
|
|
|
if len(stmts) != 1 {
|
|
|
|
|
t.Fatalf("expected 1 statement, got %d", len(stmts))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ap := ast.NewPrinter()
|
|
|
|
|
s := ap.PrintStmts(stmts)
|
|
|
|
|
if s != tt.expected {
|
|
|
|
|
t.Errorf("expected %v, got %v", tt.expected, s)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|