Fix expression parser
parent
2aae58b670
commit
e0e921602d
@ -1 +1 @@
|
|||||||
5f28a0838ca9382a947da5bd52756cf3
|
f630c7b1cf9f3b55f1467ac4c8704152
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package parser
|
package ast
|
||||||
|
|
||||||
import "golox/token"
|
import "golox/token"
|
||||||
|
|
||||||
@ -0,0 +1,64 @@
|
|||||||
|
package ast
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golox/token"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPrinter(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
expr Expr
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Binary expression",
|
||||||
|
expr: &BinaryExpr{
|
||||||
|
Left: &LiteralExpr{Value: 1},
|
||||||
|
Operator: token.Token{Type: token.PLUS, Lexeme: "+"},
|
||||||
|
Right: &LiteralExpr{Value: 2},
|
||||||
|
},
|
||||||
|
expected: "(+ 1 2)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Grouping expression",
|
||||||
|
expr: &GroupingExpr{
|
||||||
|
Expression: &LiteralExpr{Value: 1},
|
||||||
|
},
|
||||||
|
expected: "(group 1)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Literal expression",
|
||||||
|
expr: &LiteralExpr{Value: 123},
|
||||||
|
expected: "123",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Unary expression",
|
||||||
|
expr: &UnaryExpr{
|
||||||
|
Operator: token.Token{Type: token.MINUS, Lexeme: "-"},
|
||||||
|
Right: &LiteralExpr{Value: 123},
|
||||||
|
},
|
||||||
|
expected: "(- 123)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Nil literal expression",
|
||||||
|
expr: &LiteralExpr{Value: nil},
|
||||||
|
expected: "nil",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Error expression",
|
||||||
|
expr: &ErrorExpr{Value: "error"},
|
||||||
|
expected: "error",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
printer := NewPrinter()
|
||||||
|
result := printer.Print(tt.expr)
|
||||||
|
if result != tt.expected {
|
||||||
|
t.Errorf("expected %v, got %v", tt.expected, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,91 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golox/ast"
|
||||||
|
"golox/token"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mockErrorLogger struct{}
|
||||||
|
|
||||||
|
func (el *mockErrorLogger) Error(line int, message string) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (el *mockErrorLogger) ErrorAtToken(t token.Token, message string) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMockErrorLogger() *mockErrorLogger {
|
||||||
|
return &mockErrorLogger{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParser(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.EOF},
|
||||||
|
},
|
||||||
|
expected: "(+ 1 2)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Unary expression",
|
||||||
|
tokens: []token.Token{
|
||||||
|
{Type: token.MINUS, Lexeme: "-"},
|
||||||
|
{Type: token.NUMBER, Literal: 123},
|
||||||
|
{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.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.EOF},
|
||||||
|
},
|
||||||
|
expected: "(> 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.EOF},
|
||||||
|
},
|
||||||
|
expected: "(== 1 2)",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
parser := New(tt.tokens, newMockErrorLogger())
|
||||||
|
expr := parser.Parse()
|
||||||
|
ap := ast.NewPrinter()
|
||||||
|
s := ap.Print(expr)
|
||||||
|
if s != tt.expected {
|
||||||
|
t.Errorf("expected %v, got %v", tt.expected, s)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue