|
|
|
|
@ -278,7 +278,89 @@ func TestParseBlockStatement(t *testing.T) {
|
|
|
|
|
{Type: token.RIGHT_BRACE, Lexeme: "}"},
|
|
|
|
|
{Type: token.EOF},
|
|
|
|
|
},
|
|
|
|
|
expected: "(block\n (var foo 42)\n)\n",
|
|
|
|
|
expected: "{\n (var foo 42)\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)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseIfStatement(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
tokens []token.Token
|
|
|
|
|
expected string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "simple if statement",
|
|
|
|
|
tokens: []token.Token{
|
|
|
|
|
{Type: token.IF, Lexeme: "if"},
|
|
|
|
|
{Type: token.LEFT_PAREN, Lexeme: "("},
|
|
|
|
|
{Type: token.NUMBER, Lexeme: "42", Literal: 42},
|
|
|
|
|
{Type: token.RIGHT_PAREN, Lexeme: ")"},
|
|
|
|
|
{Type: token.PRINT, Lexeme: "print"},
|
|
|
|
|
{Type: token.NUMBER, Lexeme: "42", Literal: 42},
|
|
|
|
|
{Type: token.SEMICOLON, Lexeme: ";"},
|
|
|
|
|
{Type: token.EOF},
|
|
|
|
|
},
|
|
|
|
|
expected: "(if 42 {\n (print 42)\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)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseIfElseStatement(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
tokens []token.Token
|
|
|
|
|
expected string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "simple if else statement",
|
|
|
|
|
tokens: []token.Token{
|
|
|
|
|
{Type: token.IF, Lexeme: "if"},
|
|
|
|
|
{Type: token.LEFT_PAREN, Lexeme: "("},
|
|
|
|
|
{Type: token.NUMBER, Lexeme: "42", Literal: 42},
|
|
|
|
|
{Type: token.RIGHT_PAREN, Lexeme: ")"},
|
|
|
|
|
{Type: token.PRINT, Lexeme: "print"},
|
|
|
|
|
{Type: token.NUMBER, Lexeme: "42", Literal: 42},
|
|
|
|
|
{Type: token.SEMICOLON, Lexeme: ";"},
|
|
|
|
|
{Type: token.ELSE, Lexeme: "else"},
|
|
|
|
|
{Type: token.PRINT, Lexeme: "print"},
|
|
|
|
|
{Type: token.NUMBER, Lexeme: "24", Literal: 24},
|
|
|
|
|
{Type: token.SEMICOLON, Lexeme: ";"},
|
|
|
|
|
{Type: token.EOF},
|
|
|
|
|
},
|
|
|
|
|
expected: "(if 42 {\n (print 42)\n} else {\n (print 24)\n})\n",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|