You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

485 lines
12 KiB
Go

package parser
import (
"golox/ast"
"golox/errors"
"golox/token"
"testing"
)
func TestExpressionParsing(t *testing.T) {
tests := []struct {
name string
tokens []token.Token
expected string
}{
{
name: "Simple expression",
tokens: []token.Token{
{Type: token.NUMBER, Literal: 1},
{Type: token.PLUS, Lexeme: "+"},
{Type: token.NUMBER, Literal: 2},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.EOF},
},
expected: "(+ 1 2)",
},
{
name: "Unary expression",
tokens: []token.Token{
{Type: token.MINUS, Lexeme: "-"},
{Type: token.NUMBER, Literal: 123},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.EOF},
},
expected: "(- 123)",
},
{
name: "Grouping expression",
tokens: []token.Token{
{Type: token.LEFT_PAREN, Lexeme: "("},
{Type: token.NUMBER, Literal: 1},
{Type: token.PLUS, Lexeme: "+"},
{Type: token.NUMBER, Literal: 2},
{Type: token.RIGHT_PAREN, Lexeme: ")"},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.EOF},
},
expected: "(group (+ 1 2))",
},
{
name: "Comparison expression",
tokens: []token.Token{
{Type: token.NUMBER, Literal: 1},
{Type: token.GREATER, Lexeme: ">"},
{Type: token.NUMBER, Literal: 2},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.EOF},
},
expected: "(> 1 2)",
},
{
name: "Logical expression",
tokens: []token.Token{
{Type: token.NUMBER, Literal: 1},
{Type: token.GREATER, Lexeme: "and"},
{Type: token.NUMBER, Literal: 2},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.EOF},
},
expected: "(and 1 2)",
},
{
name: "Equality expression",
tokens: []token.Token{
{Type: token.NUMBER, Literal: 1},
{Type: token.EQUAL_EQUAL, Lexeme: "=="},
{Type: token.NUMBER, Literal: 2},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.EOF},
},
expected: "(== 1 2)",
},
{
name: "Parsing error - missing right parenthesis",
tokens: []token.Token{
{Type: token.LEFT_PAREN, Lexeme: "("},
{Type: token.NUMBER, Literal: 1},
{Type: token.PLUS, Lexeme: "+"},
{Type: token.NUMBER, Literal: 2},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.EOF},
},
expected: "Expect ')' after expression.",
},
}
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))
}
stmt := stmts[0]
var expr ast.Expr
if es, ok := stmt.(*ast.ExpressionStmt); !ok {
t.Errorf("expected ExprStmt, got %T", stmt)
} else {
expr = es.Expression
}
ap := ast.NewPrinter()
s := ap.PrintExpr(expr)
if s != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, s)
}
})
}
}
func TestParseExpressionStmt(t *testing.T) {
tests := []struct {
name string
tokens []token.Token
expected string
}{
{
name: "simple expression statement",
tokens: []token.Token{
{Type: token.NUMBER, Lexeme: "42", Literal: 42},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.EOF},
},
expected: "42\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 TestParsePrintStmt(t *testing.T) {
tests := []struct {
name string
tokens []token.Token
expected string
}{
{
name: "simple print statement",
tokens: []token.Token{
{Type: token.PRINT, Lexeme: "print"},
{Type: token.NUMBER, Lexeme: "42", Literal: 42},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.EOF},
},
expected: "(print 42)\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 TestParseVarStmt(t *testing.T) {
tests := []struct {
name string
tokens []token.Token
expected string
}{
{
name: "simple var statement",
tokens: []token.Token{
{Type: token.VAR, Lexeme: "var"},
{Type: token.IDENTIFIER, Lexeme: "foo"},
{Type: token.EQUAL, Lexeme: "="},
{Type: token.NUMBER, Lexeme: "42", Literal: 42},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.EOF},
},
expected: "(var foo 42)\n",
},
{
name: "simple var statement",
tokens: []token.Token{
{Type: token.VAR, Lexeme: "var"},
{Type: token.IDENTIFIER, Lexeme: "foo"},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.EOF},
},
expected: "(var foo)\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 TestParseAssignment(t *testing.T) {
tests := []struct {
name string
tokens []token.Token
expected string
}{
{
name: "simple assignment",
tokens: []token.Token{
{Type: token.IDENTIFIER, Lexeme: "foo"},
{Type: token.EQUAL, Lexeme: "="},
{Type: token.NUMBER, Lexeme: "42", Literal: 42},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.EOF},
},
expected: "(= foo 42)\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 TestParseBlockStatement(t *testing.T) {
tests := []struct {
name string
tokens []token.Token
expected string
}{
{
name: "simple block statement",
tokens: []token.Token{
{Type: token.LEFT_BRACE, Lexeme: "{"},
{Type: token.VAR, Lexeme: "var"},
{Type: token.IDENTIFIER, Lexeme: "foo"},
{Type: token.EQUAL, Lexeme: "="},
{Type: token.NUMBER, Lexeme: "42", Literal: 42},
{Type: token.SEMICOLON, Lexeme: ";"},
{Type: token.RIGHT_BRACE, Lexeme: "}"},
{Type: token.EOF},
},
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",
},
}
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 TestParseWhileStatement(t *testing.T) {
tests := []struct {
name string
tokens []token.Token
expected string
}{
{
name: "simple while statement",
tokens: []token.Token{
{Type: token.WHILE, Lexeme: "while"},
{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: "(while 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 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)
}
})
}
}