|
|
|
|
@ -3,13 +3,14 @@ package interpreter_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"golox/ast"
|
|
|
|
|
"golox/errors"
|
|
|
|
|
"golox/interpreter"
|
|
|
|
|
"golox/token"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestInterpretLiteralExpr(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
literal := &ast.LiteralExpr{Value: 42}
|
|
|
|
|
|
|
|
|
|
result := i.VisitLiteralExpr(literal)
|
|
|
|
|
@ -19,7 +20,7 @@ func TestInterpretLiteralExpr(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretGroupingExpr(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
literal := &ast.LiteralExpr{Value: 42}
|
|
|
|
|
grouping := &ast.GroupingExpr{Expression: literal}
|
|
|
|
|
|
|
|
|
|
@ -30,7 +31,7 @@ func TestInterpretGroupingExpr(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretUnaryExpr(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
literal := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
unary := &ast.UnaryExpr{
|
|
|
|
|
Operator: token.Token{Type: token.MINUS, Lexeme: "-"},
|
|
|
|
|
@ -44,7 +45,7 @@ func TestInterpretUnaryExpr(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretUnaryExprBang(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
literal := &ast.LiteralExpr{Value: true}
|
|
|
|
|
unary := &ast.UnaryExpr{
|
|
|
|
|
Operator: token.Token{Type: token.BANG, Lexeme: "!"},
|
|
|
|
|
@ -58,7 +59,7 @@ func TestInterpretUnaryExprBang(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretErrorExpr(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
errorExpr := &ast.ErrorExpr{Value: "error"}
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
@ -71,7 +72,7 @@ func TestInterpretErrorExpr(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretExpr(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
literal := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
@ -87,7 +88,7 @@ func TestInterpretExpr(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretBinaryExpr(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
left := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
right := &ast.LiteralExpr{Value: 2.0}
|
|
|
|
|
binary := &ast.BinaryExpr{
|
|
|
|
|
@ -103,7 +104,7 @@ func TestInterpretBinaryExpr(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretBinaryExprDivisionByZero(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
left := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
right := &ast.LiteralExpr{Value: 0.0}
|
|
|
|
|
binary := &ast.BinaryExpr{
|
|
|
|
|
@ -122,7 +123,7 @@ func TestInterpretBinaryExprDivisionByZero(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretBinaryExprAddition(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
left := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
right := &ast.LiteralExpr{Value: 2.0}
|
|
|
|
|
binary := &ast.BinaryExpr{
|
|
|
|
|
@ -138,7 +139,7 @@ func TestInterpretBinaryExprAddition(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretBinaryExprSubtraction(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
left := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
right := &ast.LiteralExpr{Value: 2.0}
|
|
|
|
|
binary := &ast.BinaryExpr{
|
|
|
|
|
@ -154,7 +155,7 @@ func TestInterpretBinaryExprSubtraction(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretBinaryExprStringConcatenation(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
left := &ast.LiteralExpr{Value: "foo"}
|
|
|
|
|
right := &ast.LiteralExpr{Value: "bar"}
|
|
|
|
|
binary := &ast.BinaryExpr{
|
|
|
|
|
@ -170,7 +171,7 @@ func TestInterpretBinaryExprStringConcatenation(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretBinaryExprInvalidOperands(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
left := &ast.LiteralExpr{Value: "foo"}
|
|
|
|
|
right := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
binary := &ast.BinaryExpr{
|
|
|
|
|
@ -189,7 +190,7 @@ func TestInterpretBinaryExprInvalidOperands(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretBinaryExprComparison(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
left := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
right := &ast.LiteralExpr{Value: 2.0}
|
|
|
|
|
binary := &ast.BinaryExpr{
|
|
|
|
|
@ -205,7 +206,7 @@ func TestInterpretBinaryExprComparison(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretBinaryExprComparisonEqual(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
left := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
right := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
binary := &ast.BinaryExpr{
|
|
|
|
|
@ -221,7 +222,7 @@ func TestInterpretBinaryExprComparisonEqual(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretBinaryExprComparisonNotEqual(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
left := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
right := &ast.LiteralExpr{Value: 2.0}
|
|
|
|
|
binary := &ast.BinaryExpr{
|
|
|
|
|
@ -237,7 +238,7 @@ func TestInterpretBinaryExprComparisonNotEqual(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretBinaryExprComparisonInvalidOperands(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
left := &ast.LiteralExpr{Value: "foo"}
|
|
|
|
|
right := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
binary := &ast.BinaryExpr{
|
|
|
|
|
@ -256,7 +257,7 @@ func TestInterpretBinaryExprComparisonInvalidOperands(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInterpretBinaryExprInvalidOperatorType(t *testing.T) {
|
|
|
|
|
i := interpreter.New()
|
|
|
|
|
i := interpreter.New(errors.NewMockErrorLogger())
|
|
|
|
|
left := &ast.LiteralExpr{Value: 42.0}
|
|
|
|
|
right := &ast.LiteralExpr{Value: 2.0}
|
|
|
|
|
binary := &ast.BinaryExpr{
|
|
|
|
|
|