2025-01-02 19:46:45 -08:00
|
|
|
package parser
|
|
|
|
|
|
|
|
|
|
import (
|
2025-01-03 19:38:03 -08:00
|
|
|
"fmt"
|
2025-01-02 19:46:45 -08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"code.jmug.me/jmug/interpreter-in-go/pkg/ast"
|
|
|
|
|
"code.jmug.me/jmug/interpreter-in-go/pkg/lexer"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestLetStatements(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
2025-01-05 16:59:06 -08:00
|
|
|
input string
|
2025-01-02 19:46:45 -08:00
|
|
|
expectedIdentifier string
|
2025-01-13 19:38:57 -08:00
|
|
|
expectedValue interface{}
|
2025-01-02 19:46:45 -08:00
|
|
|
}{
|
2025-01-05 16:59:06 -08:00
|
|
|
{"let x = 5;", "x", 5},
|
|
|
|
|
{"let y = true;", "y", true},
|
|
|
|
|
{"let foobar = y;", "foobar", "y"},
|
2025-01-02 19:46:45 -08:00
|
|
|
}
|
|
|
|
|
|
2025-01-05 16:59:06 -08:00
|
|
|
for _, tt := range tests {
|
|
|
|
|
l := lexer.New(tt.input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program.Statements does not contain 1 statements. got=%d",
|
|
|
|
|
len(program.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt := program.Statements[0]
|
2025-01-02 19:46:45 -08:00
|
|
|
if !testLetStatement(t, stmt, tt.expectedIdentifier) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-01-05 16:59:06 -08:00
|
|
|
|
|
|
|
|
val := stmt.(*ast.LetStatement).Value
|
|
|
|
|
if !testLiteralExpression(t, val, tt.expectedValue) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-01-02 19:46:45 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReturnStatements(t *testing.T) {
|
2025-01-05 16:59:06 -08:00
|
|
|
tests := []struct {
|
|
|
|
|
input string
|
|
|
|
|
expectedValue interface{}
|
|
|
|
|
}{
|
|
|
|
|
{"return 5;", 5},
|
|
|
|
|
{"return true;", true},
|
|
|
|
|
{"return foobar;", "foobar"},
|
|
|
|
|
}
|
2025-01-02 19:46:45 -08:00
|
|
|
|
2025-01-05 16:59:06 -08:00
|
|
|
for _, tt := range tests {
|
|
|
|
|
l := lexer.New(tt.input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
2025-01-02 19:46:45 -08:00
|
|
|
|
2025-01-05 16:59:06 -08:00
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program.Statements does not contain 1 statements. got=%d",
|
|
|
|
|
len(program.Statements))
|
|
|
|
|
}
|
2025-01-02 19:46:45 -08:00
|
|
|
|
2025-01-05 16:59:06 -08:00
|
|
|
stmt := program.Statements[0]
|
2025-01-02 19:46:45 -08:00
|
|
|
returnStmt, ok := stmt.(*ast.ReturnStatement)
|
|
|
|
|
if !ok {
|
2025-01-05 16:59:06 -08:00
|
|
|
t.Fatalf("stmt not *ast.ReturnStatement. got=%T", stmt)
|
2025-01-02 19:46:45 -08:00
|
|
|
}
|
|
|
|
|
if returnStmt.TokenLiteral() != "return" {
|
2025-01-05 16:59:06 -08:00
|
|
|
t.Fatalf("returnStmt.TokenLiteral not 'return', got %q",
|
2025-01-02 19:46:45 -08:00
|
|
|
returnStmt.TokenLiteral())
|
|
|
|
|
}
|
2025-01-05 16:59:06 -08:00
|
|
|
if testLiteralExpression(t, returnStmt.ReturnValue, tt.expectedValue) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-01-02 19:46:45 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
func TestIdentifierExpression(t *testing.T) {
|
2025-01-03 19:38:03 -08:00
|
|
|
input := "foobar;"
|
2025-01-13 19:38:57 -08:00
|
|
|
|
2025-01-03 19:38:03 -08:00
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
2025-01-13 19:38:57 -08:00
|
|
|
|
2025-01-03 19:38:03 -08:00
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program has not enough statements. got=%d",
|
|
|
|
|
len(program.Statements))
|
|
|
|
|
}
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
program.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ident, ok := stmt.Expression.(*ast.Identifier)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("exp not *ast.Identifier. got=%T", stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
if ident.Value != "foobar" {
|
|
|
|
|
t.Errorf("ident.Value not %s. got=%s", "foobar", ident.Value)
|
|
|
|
|
}
|
|
|
|
|
if ident.TokenLiteral() != "foobar" {
|
|
|
|
|
t.Errorf("ident.TokenLiteral not %s. got=%s", "foobar",
|
|
|
|
|
ident.TokenLiteral())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIntegerLiteralExpression(t *testing.T) {
|
|
|
|
|
input := "5;"
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program has not enough statements. got=%d",
|
|
|
|
|
len(program.Statements))
|
|
|
|
|
}
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
program.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
literal, ok := stmt.Expression.(*ast.IntegerLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("exp not *ast.IntegerLiteral. got=%T", stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
if literal.Value != 5 {
|
|
|
|
|
t.Errorf("literal.Value not %d. got=%d", 5, literal.Value)
|
|
|
|
|
}
|
|
|
|
|
if literal.TokenLiteral() != "5" {
|
|
|
|
|
t.Errorf("literal.TokenLiteral not %s. got=%s", "5",
|
|
|
|
|
literal.TokenLiteral())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParsingPrefixExpressions(t *testing.T) {
|
|
|
|
|
prefixTests := []struct {
|
2025-01-04 19:16:22 -08:00
|
|
|
input string
|
|
|
|
|
operator string
|
2025-01-13 19:38:57 -08:00
|
|
|
value interface{}
|
2025-01-03 19:38:03 -08:00
|
|
|
}{
|
|
|
|
|
{"!5;", "!", 5},
|
|
|
|
|
{"-15;", "-", 15},
|
2025-01-13 19:38:57 -08:00
|
|
|
{"!foobar;", "!", "foobar"},
|
|
|
|
|
{"-foobar;", "-", "foobar"},
|
2025-01-04 19:16:22 -08:00
|
|
|
{"!true;", "!", true},
|
|
|
|
|
{"!false;", "!", false},
|
2025-01-03 19:38:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range prefixTests {
|
|
|
|
|
l := lexer.New(tt.input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
|
|
|
|
|
1, len(program.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
program.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exp, ok := stmt.Expression.(*ast.PrefixExpression)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("stmt is not ast.PrefixExpression. got=%T", stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
if exp.Operator != tt.operator {
|
|
|
|
|
t.Fatalf("exp.Operator is not '%s'. got=%s",
|
|
|
|
|
tt.operator, exp.Operator)
|
|
|
|
|
}
|
2025-01-04 19:16:22 -08:00
|
|
|
if !testLiteralExpression(t, exp.Right, tt.value) {
|
2025-01-03 19:38:03 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-04 17:56:10 -08:00
|
|
|
func TestParsingInfixExpressions(t *testing.T) {
|
|
|
|
|
infixTests := []struct {
|
|
|
|
|
input string
|
2025-01-13 19:38:57 -08:00
|
|
|
leftValue interface{}
|
2025-01-04 17:56:10 -08:00
|
|
|
operator string
|
2025-01-13 19:38:57 -08:00
|
|
|
rightValue interface{}
|
2025-01-04 17:56:10 -08:00
|
|
|
}{
|
|
|
|
|
{"5 + 5;", 5, "+", 5},
|
|
|
|
|
{"5 - 5;", 5, "-", 5},
|
|
|
|
|
{"5 * 5;", 5, "*", 5},
|
|
|
|
|
{"5 / 5;", 5, "/", 5},
|
|
|
|
|
{"5 > 5;", 5, ">", 5},
|
|
|
|
|
{"5 < 5;", 5, "<", 5},
|
|
|
|
|
{"5 == 5;", 5, "==", 5},
|
|
|
|
|
{"5 != 5;", 5, "!=", 5},
|
2025-01-13 19:38:57 -08:00
|
|
|
{"foobar + barfoo;", "foobar", "+", "barfoo"},
|
|
|
|
|
{"foobar - barfoo;", "foobar", "-", "barfoo"},
|
|
|
|
|
{"foobar * barfoo;", "foobar", "*", "barfoo"},
|
|
|
|
|
{"foobar / barfoo;", "foobar", "/", "barfoo"},
|
|
|
|
|
{"foobar > barfoo;", "foobar", ">", "barfoo"},
|
|
|
|
|
{"foobar < barfoo;", "foobar", "<", "barfoo"},
|
|
|
|
|
{"foobar == barfoo;", "foobar", "==", "barfoo"},
|
|
|
|
|
{"foobar != barfoo;", "foobar", "!=", "barfoo"},
|
2025-01-04 19:16:22 -08:00
|
|
|
{"true == true", true, "==", true},
|
|
|
|
|
{"true != false", true, "!=", false},
|
|
|
|
|
{"false == false", false, "==", false},
|
2025-01-04 17:56:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range infixTests {
|
|
|
|
|
l := lexer.New(tt.input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
|
|
|
|
|
1, len(program.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
program.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
if !testInfixExpression(t, stmt.Expression, tt.leftValue,
|
|
|
|
|
tt.operator, tt.rightValue) {
|
2025-01-04 17:56:10 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestOperatorPrecedenceParsing(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
input string
|
|
|
|
|
expected string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
"-a * b",
|
|
|
|
|
"((-a) * b)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"!-a",
|
|
|
|
|
"(!(-a))",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"a + b + c",
|
|
|
|
|
"((a + b) + c)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"a + b - c",
|
|
|
|
|
"((a + b) - c)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"a * b * c",
|
|
|
|
|
"((a * b) * c)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"a * b / c",
|
|
|
|
|
"((a * b) / c)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"a + b / c",
|
|
|
|
|
"(a + (b / c))",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"a + b * c + d / e - f",
|
|
|
|
|
"(((a + (b * c)) + (d / e)) - f)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"3 + 4; -5 * 5",
|
|
|
|
|
"(3 + 4)((-5) * 5)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"5 > 4 == 3 < 4",
|
|
|
|
|
"((5 > 4) == (3 < 4))",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"5 < 4 != 3 > 4",
|
|
|
|
|
"((5 < 4) != (3 > 4))",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"3 + 4 * 5 == 3 * 1 + 4 * 5",
|
|
|
|
|
"((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))",
|
|
|
|
|
},
|
2025-01-04 19:16:22 -08:00
|
|
|
{
|
|
|
|
|
"true",
|
|
|
|
|
"true",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"false",
|
|
|
|
|
"false",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"3 > 5 == false",
|
|
|
|
|
"((3 > 5) == false)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"3 < 5 == true",
|
|
|
|
|
"((3 < 5) == true)",
|
|
|
|
|
},
|
2025-01-05 11:37:13 -08:00
|
|
|
{
|
|
|
|
|
"1 + (2 + 3) + 4",
|
|
|
|
|
"((1 + (2 + 3)) + 4)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"(5 + 5) * 2",
|
|
|
|
|
"((5 + 5) * 2)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"2 / (5 + 5)",
|
|
|
|
|
"(2 / (5 + 5))",
|
|
|
|
|
},
|
2025-01-13 19:38:57 -08:00
|
|
|
{
|
|
|
|
|
"(5 + 5) * 2 * (5 + 5)",
|
|
|
|
|
"(((5 + 5) * 2) * (5 + 5))",
|
|
|
|
|
},
|
2025-01-05 11:37:13 -08:00
|
|
|
{
|
|
|
|
|
"-(5 + 5)",
|
|
|
|
|
"(-(5 + 5))",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"!(true == true)",
|
|
|
|
|
"(!(true == true))",
|
|
|
|
|
},
|
2025-01-05 16:59:06 -08:00
|
|
|
{
|
|
|
|
|
"a + add(b * c) + d",
|
|
|
|
|
"((a + add((b * c))) + d)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"add(a, b, 1, 2 * 3, 4 + 5, add(6, 7 * 8))",
|
|
|
|
|
"add(a, b, 1, (2 * 3), (4 + 5), add(6, (7 * 8)))",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"add(a + b + c * d / f + g)",
|
|
|
|
|
"add((((a + b) + ((c * d) / f)) + g))",
|
|
|
|
|
},
|
2025-01-10 17:48:54 -08:00
|
|
|
{
|
|
|
|
|
"a * [1, 2, 3, 4][b * c] * d",
|
|
|
|
|
"((a * ([1, 2, 3, 4][(b * c)])) * d)",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"add(a * b[2], b[1], 2 * [1, 2][1])",
|
|
|
|
|
"add((a * (b[2])), (b[1]), (2 * ([1, 2][1])))",
|
|
|
|
|
},
|
2025-01-04 17:56:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
l := lexer.New(tt.input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
actual := program.String()
|
|
|
|
|
if actual != tt.expected {
|
|
|
|
|
t.Errorf("expected=%q, got=%q", tt.expected, actual)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-04 19:16:22 -08:00
|
|
|
func TestBooleanExpression(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
input string
|
|
|
|
|
expectedBoolean bool
|
|
|
|
|
}{
|
|
|
|
|
{"true;", true},
|
|
|
|
|
{"false;", false},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
l := lexer.New(tt.input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program has not enough statements. got=%d",
|
|
|
|
|
len(program.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
program.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean, ok := stmt.Expression.(*ast.Boolean)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("exp not *ast.Boolean. got=%T", stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
if boolean.Value != tt.expectedBoolean {
|
|
|
|
|
t.Errorf("boolean.Value not %t. got=%t", tt.expectedBoolean,
|
|
|
|
|
boolean.Value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-05 15:35:00 -08:00
|
|
|
func TestIfExpression(t *testing.T) {
|
|
|
|
|
input := `if (x < y) { x }`
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
|
|
|
|
|
1, len(program.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
program.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exp, ok := stmt.Expression.(*ast.IfExpression)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T",
|
|
|
|
|
stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !testInfixExpression(t, exp.Condition, "x", "<", "y") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(exp.Consequence.Statements) != 1 {
|
|
|
|
|
t.Errorf("consequence is not 1 statements. got=%d\n",
|
|
|
|
|
len(exp.Consequence.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
consequence, ok := exp.Consequence.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
exp.Consequence.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !testIdentifier(t, consequence.Expression, "x") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if exp.Alternative != nil {
|
|
|
|
|
t.Errorf("exp.Alternative.Statements was not nil. got=%+v", exp.Alternative)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIfElseExpression(t *testing.T) {
|
|
|
|
|
input := `if (x < y) { x } else { y }`
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
|
|
|
|
|
1, len(program.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
program.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exp, ok := stmt.Expression.(*ast.IfExpression)
|
|
|
|
|
if !ok {
|
2025-01-13 19:38:57 -08:00
|
|
|
t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T", stmt.Expression)
|
2025-01-05 15:35:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !testInfixExpression(t, exp.Condition, "x", "<", "y") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(exp.Consequence.Statements) != 1 {
|
|
|
|
|
t.Errorf("consequence is not 1 statements. got=%d\n",
|
|
|
|
|
len(exp.Consequence.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
consequence, ok := exp.Consequence.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
2025-01-13 19:38:57 -08:00
|
|
|
t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
|
2025-01-05 15:35:00 -08:00
|
|
|
exp.Consequence.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !testIdentifier(t, consequence.Expression, "x") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(exp.Alternative.Statements) != 1 {
|
2025-01-13 19:38:57 -08:00
|
|
|
t.Errorf("exp.Alternative.Statements does not contain 1 statements. got=%d\n",
|
2025-01-05 15:35:00 -08:00
|
|
|
len(exp.Alternative.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
alternative, ok := exp.Alternative.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
2025-01-13 19:38:57 -08:00
|
|
|
t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
|
2025-01-05 15:35:00 -08:00
|
|
|
exp.Alternative.Statements[0])
|
|
|
|
|
}
|
2025-01-13 19:38:57 -08:00
|
|
|
|
2025-01-05 15:35:00 -08:00
|
|
|
if !testIdentifier(t, alternative.Expression, "y") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-05 16:13:08 -08:00
|
|
|
|
|
|
|
|
func TestFunctionLiteralParsing(t *testing.T) {
|
|
|
|
|
input := `fn(x, y) { x + y; }`
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
|
|
|
|
|
1, len(program.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
program.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function, ok := stmt.Expression.(*ast.FunctionLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("stmt.Expression is not ast.FunctionLiteral. got=%T",
|
|
|
|
|
stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(function.Parameters) != 2 {
|
|
|
|
|
t.Fatalf("function literal parameters wrong. want 2, got=%d\n",
|
|
|
|
|
len(function.Parameters))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testLiteralExpression(t, function.Parameters[0], "x")
|
|
|
|
|
testLiteralExpression(t, function.Parameters[1], "y")
|
|
|
|
|
|
|
|
|
|
if len(function.Body.Statements) != 1 {
|
|
|
|
|
t.Fatalf("function.Body.Statements has not 1 statements. got=%d\n",
|
|
|
|
|
len(function.Body.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bodyStmt, ok := function.Body.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("function body stmt is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
function.Body.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testInfixExpression(t, bodyStmt.Expression, "x", "+", "y")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFunctionParameterParsing(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
input string
|
|
|
|
|
expectedParams []string
|
|
|
|
|
}{
|
|
|
|
|
{input: "fn() {};", expectedParams: []string{}},
|
|
|
|
|
{input: "fn(x) {};", expectedParams: []string{"x"}},
|
|
|
|
|
{input: "fn(x, y, z) {};", expectedParams: []string{"x", "y", "z"}},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
l := lexer.New(tt.input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
stmt := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
function := stmt.Expression.(*ast.FunctionLiteral)
|
|
|
|
|
|
|
|
|
|
if len(function.Parameters) != len(tt.expectedParams) {
|
|
|
|
|
t.Errorf("length parameters wrong. want %d, got=%d\n",
|
|
|
|
|
len(tt.expectedParams), len(function.Parameters))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, ident := range tt.expectedParams {
|
|
|
|
|
testLiteralExpression(t, function.Parameters[i], ident)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-05 16:59:06 -08:00
|
|
|
func TestCallExpressionParsing(t *testing.T) {
|
|
|
|
|
input := "add(1, 2 * 3, 4 + 5);"
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
|
|
|
|
|
1, len(program.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("stmt is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
program.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exp, ok := stmt.Expression.(*ast.CallExpression)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("stmt.Expression is not ast.CallExpression. got=%T",
|
|
|
|
|
stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !testIdentifier(t, exp.Function, "add") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(exp.Arguments) != 3 {
|
|
|
|
|
t.Fatalf("wrong length of arguments. got=%d", len(exp.Arguments))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testLiteralExpression(t, exp.Arguments[0], 1)
|
|
|
|
|
testInfixExpression(t, exp.Arguments[1], 2, "*", 3)
|
|
|
|
|
testInfixExpression(t, exp.Arguments[2], 4, "+", 5)
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
func TestCallExpressionParameterParsing(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
input string
|
|
|
|
|
expectedIdent string
|
|
|
|
|
expectedArgs []string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
input: "add();",
|
|
|
|
|
expectedIdent: "add",
|
|
|
|
|
expectedArgs: []string{},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: "add(1);",
|
|
|
|
|
expectedIdent: "add",
|
|
|
|
|
expectedArgs: []string{"1"},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: "add(1, 2 * 3, 4 + 5);",
|
|
|
|
|
expectedIdent: "add",
|
|
|
|
|
expectedArgs: []string{"1", "(2 * 3)", "(4 + 5)"},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
l := lexer.New(tt.input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
stmt := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
exp, ok := stmt.Expression.(*ast.CallExpression)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("stmt.Expression is not ast.CallExpression. got=%T",
|
|
|
|
|
stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !testIdentifier(t, exp.Function, tt.expectedIdent) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(exp.Arguments) != len(tt.expectedArgs) {
|
|
|
|
|
t.Fatalf("wrong number of arguments. want=%d, got=%d",
|
|
|
|
|
len(tt.expectedArgs), len(exp.Arguments))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, arg := range tt.expectedArgs {
|
|
|
|
|
if exp.Arguments[i].String() != arg {
|
|
|
|
|
t.Errorf("argument %d wrong. want=%q, got=%q", i,
|
|
|
|
|
arg, exp.Arguments[i].String())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 17:48:54 -08:00
|
|
|
func TestStringLiteralExpression(t *testing.T) {
|
|
|
|
|
input := `"hello world";`
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
stmt := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
literal, ok := stmt.Expression.(*ast.StringLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("exp not *ast.StringLiteral. got=%T", stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if literal.Value != "hello world" {
|
|
|
|
|
t.Errorf("literal.Value not %q. got=%q", "hello world", literal.Value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
func TestParsingEmptyArrayLiterals(t *testing.T) {
|
|
|
|
|
input := "[]"
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
array, ok := stmt.Expression.(*ast.ArrayLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("exp not ast.ArrayLiteral. got=%T", stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(array.Elements) != 0 {
|
|
|
|
|
t.Errorf("len(array.Elements) not 0. got=%d", len(array.Elements))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 17:48:54 -08:00
|
|
|
func TestParsingArrayLiterals(t *testing.T) {
|
|
|
|
|
input := "[1, 2 * 2, 3 + 3]"
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
array, ok := stmt.Expression.(*ast.ArrayLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("exp not ast.ArrayLiteral. got=%T", stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(array.Elements) != 3 {
|
|
|
|
|
t.Fatalf("len(array.Elements) not 3. got=%d", len(array.Elements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testIntegerLiteral(t, array.Elements[0], 1)
|
|
|
|
|
testInfixExpression(t, array.Elements[1], 2, "*", 2)
|
|
|
|
|
testInfixExpression(t, array.Elements[2], 3, "+", 3)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParsingIndexExpressions(t *testing.T) {
|
|
|
|
|
input := "myArray[1 + 1]"
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
indexExp, ok := stmt.Expression.(*ast.IndexExpression)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("exp not *ast.IndexExpression. got=%T", stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !testIdentifier(t, indexExp.Left, "myArray") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !testInfixExpression(t, indexExp.Index, 1, "+", 1) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
func TestParsingEmptyHashLiteral(t *testing.T) {
|
|
|
|
|
input := "{}"
|
2025-01-10 21:09:44 -08:00
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
stmt := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
hash, ok := stmt.Expression.(*ast.HashLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
if len(hash.Pairs) != 0 {
|
2025-01-10 21:09:44 -08:00
|
|
|
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
|
|
|
|
|
}
|
2025-01-13 19:38:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParsingHashLiteralsStringKeys(t *testing.T) {
|
|
|
|
|
input := `{"one": 1, "two": 2, "three": 3}`
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
stmt := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
hash, ok := stmt.Expression.(*ast.HashLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
|
|
|
|
|
}
|
2025-01-10 21:09:44 -08:00
|
|
|
|
|
|
|
|
expected := map[string]int64{
|
|
|
|
|
"one": 1,
|
|
|
|
|
"two": 2,
|
|
|
|
|
"three": 3,
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
if len(hash.Pairs) != len(expected) {
|
|
|
|
|
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 21:09:44 -08:00
|
|
|
for key, value := range hash.Pairs {
|
|
|
|
|
literal, ok := key.(*ast.StringLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Errorf("key is not ast.StringLiteral. got=%T", key)
|
2025-01-13 19:38:57 -08:00
|
|
|
continue
|
2025-01-10 21:09:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedValue := expected[literal.String()]
|
2025-01-13 19:38:57 -08:00
|
|
|
testIntegerLiteral(t, value, expectedValue)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParsingHashLiteralsBooleanKeys(t *testing.T) {
|
|
|
|
|
input := `{true: 1, false: 2}`
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
2025-01-10 21:09:44 -08:00
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
stmt := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
hash, ok := stmt.Expression.(*ast.HashLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected := map[string]int64{
|
|
|
|
|
"true": 1,
|
|
|
|
|
"false": 2,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(hash.Pairs) != len(expected) {
|
|
|
|
|
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for key, value := range hash.Pairs {
|
|
|
|
|
boolean, ok := key.(*ast.Boolean)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Errorf("key is not ast.BooleanLiteral. got=%T", key)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedValue := expected[boolean.String()]
|
2025-01-10 21:09:44 -08:00
|
|
|
testIntegerLiteral(t, value, expectedValue)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
func TestParsingHashLiteralsIntegerKeys(t *testing.T) {
|
|
|
|
|
input := `{1: 1, 2: 2, 3: 3}`
|
2025-01-10 21:09:44 -08:00
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
stmt := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
hash, ok := stmt.Expression.(*ast.HashLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
expected := map[string]int64{
|
|
|
|
|
"1": 1,
|
|
|
|
|
"2": 2,
|
|
|
|
|
"3": 3,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(hash.Pairs) != len(expected) {
|
2025-01-10 21:09:44 -08:00
|
|
|
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
|
|
|
|
|
}
|
2025-01-13 19:38:57 -08:00
|
|
|
|
|
|
|
|
for key, value := range hash.Pairs {
|
|
|
|
|
integer, ok := key.(*ast.IntegerLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Errorf("key is not ast.IntegerLiteral. got=%T", key)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedValue := expected[integer.String()]
|
|
|
|
|
|
|
|
|
|
testIntegerLiteral(t, value, expectedValue)
|
|
|
|
|
}
|
2025-01-10 21:09:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParsingHashLiteralsWithExpressions(t *testing.T) {
|
|
|
|
|
input := `{"one": 0 + 1, "two": 10 - 8, "three": 15 / 5}`
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
stmt := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
hash, ok := stmt.Expression.(*ast.HashLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(hash.Pairs) != 3 {
|
|
|
|
|
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tests := map[string]func(ast.Expression){
|
|
|
|
|
"one": func(e ast.Expression) {
|
|
|
|
|
testInfixExpression(t, e, 0, "+", 1)
|
|
|
|
|
},
|
|
|
|
|
"two": func(e ast.Expression) {
|
|
|
|
|
testInfixExpression(t, e, 10, "-", 8)
|
|
|
|
|
},
|
|
|
|
|
"three": func(e ast.Expression) {
|
|
|
|
|
testInfixExpression(t, e, 15, "/", 5)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for key, value := range hash.Pairs {
|
|
|
|
|
literal, ok := key.(*ast.StringLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Errorf("key is not ast.StringLiteral. got=%T", key)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testFunc, ok := tests[literal.String()]
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Errorf("No test function for key %q found", literal.String())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testFunc(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
func testLetStatement(t *testing.T, s ast.Statement, name string) bool {
|
|
|
|
|
if s.TokenLiteral() != "let" {
|
|
|
|
|
t.Errorf("s.TokenLiteral not 'let'. got=%q", s.TokenLiteral())
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
letStmt, ok := s.(*ast.LetStatement)
|
2025-01-04 19:16:22 -08:00
|
|
|
if !ok {
|
2025-01-13 19:38:57 -08:00
|
|
|
t.Errorf("s not *ast.LetStatement. got=%T", s)
|
2025-01-04 19:16:22 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
if letStmt.Name.Value != name {
|
|
|
|
|
t.Errorf("letStmt.Name.Value not '%s'. got=%s", name, letStmt.Name.Value)
|
2025-01-04 19:16:22 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
if letStmt.Name.TokenLiteral() != name {
|
|
|
|
|
t.Errorf("letStmt.Name.TokenLiteral() not '%s'. got=%s",
|
|
|
|
|
name, letStmt.Name.TokenLiteral())
|
2025-01-04 19:16:22 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
func testInfixExpression(t *testing.T, exp ast.Expression, left interface{},
|
|
|
|
|
operator string, right interface{}) bool {
|
|
|
|
|
|
|
|
|
|
opExp, ok := exp.(*ast.InfixExpression)
|
2025-01-04 19:16:22 -08:00
|
|
|
if !ok {
|
2025-01-13 19:38:57 -08:00
|
|
|
t.Errorf("exp is not ast.InfixExpression. got=%T(%s)", exp, exp)
|
2025-01-04 19:16:22 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
if !testLiteralExpression(t, opExp.Left, left) {
|
2025-01-04 19:16:22 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
if opExp.Operator != operator {
|
|
|
|
|
t.Errorf("exp.Operator is not '%s'. got=%q", operator, opExp.Operator)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !testLiteralExpression(t, opExp.Right, right) {
|
2025-01-04 19:16:22 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testLiteralExpression(
|
|
|
|
|
t *testing.T,
|
|
|
|
|
exp ast.Expression,
|
2025-01-13 19:38:57 -08:00
|
|
|
expected interface{},
|
2025-01-04 19:16:22 -08:00
|
|
|
) bool {
|
|
|
|
|
switch v := expected.(type) {
|
|
|
|
|
case int:
|
|
|
|
|
return testIntegerLiteral(t, exp, int64(v))
|
|
|
|
|
case int64:
|
|
|
|
|
return testIntegerLiteral(t, exp, v)
|
|
|
|
|
case string:
|
|
|
|
|
return testIdentifier(t, exp, v)
|
|
|
|
|
case bool:
|
|
|
|
|
return testBooleanLiteral(t, exp, v)
|
|
|
|
|
}
|
|
|
|
|
t.Errorf("type of exp not handled. got=%T", exp)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
|
|
|
|
|
integ, ok := il.(*ast.IntegerLiteral)
|
2025-01-04 19:16:22 -08:00
|
|
|
if !ok {
|
2025-01-13 19:38:57 -08:00
|
|
|
t.Errorf("il not *ast.IntegerLiteral. got=%T", il)
|
2025-01-04 19:16:22 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
if integ.Value != value {
|
|
|
|
|
t.Errorf("integ.Value not %d. got=%d", value, integ.Value)
|
2025-01-04 19:16:22 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
if integ.TokenLiteral() != fmt.Sprintf("%d", value) {
|
|
|
|
|
t.Errorf("integ.TokenLiteral not %d. got=%s", value,
|
|
|
|
|
integ.TokenLiteral())
|
2025-01-04 19:16:22 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testIdentifier(t *testing.T, exp ast.Expression, value string) bool {
|
|
|
|
|
ident, ok := exp.(*ast.Identifier)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Errorf("exp not *ast.Identifier. got=%T", exp)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ident.Value != value {
|
|
|
|
|
t.Errorf("ident.Value not %s. got=%s", value, ident.Value)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ident.TokenLiteral() != value {
|
|
|
|
|
t.Errorf("ident.TokenLiteral not %s. got=%s", value,
|
|
|
|
|
ident.TokenLiteral())
|
2025-01-04 19:16:22 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
func testBooleanLiteral(t *testing.T, exp ast.Expression, value bool) bool {
|
|
|
|
|
bo, ok := exp.(*ast.Boolean)
|
2025-01-03 19:38:03 -08:00
|
|
|
if !ok {
|
2025-01-13 19:38:57 -08:00
|
|
|
t.Errorf("exp not *ast.Boolean. got=%T", exp)
|
2025-01-03 19:38:03 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
if bo.Value != value {
|
|
|
|
|
t.Errorf("bo.Value not %t. got=%t", value, bo.Value)
|
2025-01-03 19:38:03 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:38:57 -08:00
|
|
|
if bo.TokenLiteral() != fmt.Sprintf("%t", value) {
|
|
|
|
|
t.Errorf("bo.TokenLiteral not %t. got=%s",
|
|
|
|
|
value, bo.TokenLiteral())
|
2025-01-03 19:38:03 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-02 19:46:45 -08:00
|
|
|
func checkParserErrors(t *testing.T, p *Parser) {
|
|
|
|
|
errors := p.Errors()
|
|
|
|
|
if len(errors) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Errorf("parser has %d errors", len(errors))
|
|
|
|
|
for _, msg := range errors {
|
|
|
|
|
t.Errorf("parser error: %q", msg)
|
|
|
|
|
}
|
|
|
|
|
t.FailNow()
|
|
|
|
|
}
|