aboutsummaryrefslogtreecommitdiff
path: root/parser
diff options
context:
space:
mode:
authorDimitri Sokolyuk <sokolyuk@gmail.com>2022-08-24 14:27:07 +0200
committerDimitri Sokolyuk <sokolyuk@gmail.com>2022-08-24 14:27:07 +0200
commite452a2ec377334144aa9204514b8ad4bd2c2254e (patch)
tree8aadd92002a6c9587e2582c13eb0b62db164aa3f /parser
parent6293378bb84702e9cbfc392158114c07d23d9642 (diff)
more cleanup
Diffstat (limited to 'parser')
-rw-r--r--parser/parser_test.go153
-rw-r--r--parser/parser_tracing.go3
2 files changed, 53 insertions, 103 deletions
diff --git a/parser/parser_test.go b/parser/parser_test.go
index ed019af..a7f9775 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -26,8 +26,7 @@ func TestLetStatements(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Statements does not contain 1 statements. got=%d",
- len(program.Statements))
+ t.Fatalf("program.Statements does not contain 1 statements. got=%d", len(program.Statements))
}
stmt := program.Statements[0]
@@ -59,8 +58,7 @@ func TestReturnStatements(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Statements does not contain 1 statements. got=%d",
- len(program.Statements))
+ t.Fatalf("program.Statements does not contain 1 statements. got=%d", len(program.Statements))
}
stmt := program.Statements[0]
@@ -69,8 +67,7 @@ func TestReturnStatements(t *testing.T) {
t.Fatalf("stmt not *ast.returnStatement. got=%T", stmt)
}
if returnStmt.TokenLiteral() != "return" {
- t.Fatalf("returnStmt.TokenLiteral not 'return', got %q",
- returnStmt.TokenLiteral())
+ t.Fatalf("returnStmt.TokenLiteral not 'return', got %q", returnStmt.TokenLiteral())
}
if testLiteralExpression(t, returnStmt.ReturnValue, tt.expectedValue) {
return
@@ -87,13 +84,11 @@ func TestIdentifierExpression(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program has not enough statements. got=%d",
- len(program.Statements))
+ 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])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
ident, ok := stmt.Expression.(*ast.Identifier)
@@ -104,8 +99,7 @@ func TestIdentifierExpression(t *testing.T) {
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())
+ t.Errorf("ident.TokenLiteral not %s. got=%s", "foobar", ident.TokenLiteral())
}
}
@@ -118,13 +112,11 @@ func TestIntegerLiteralExpression(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program has not enough statements. got=%d",
- len(program.Statements))
+ 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])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
literal, ok := stmt.Expression.(*ast.IntegerLiteral)
@@ -135,8 +127,7 @@ func TestIntegerLiteralExpression(t *testing.T) {
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())
+ t.Errorf("literal.TokenLiteral not %s. got=%s", "5", literal.TokenLiteral())
}
}
@@ -161,14 +152,12 @@ func TestParsingPrefixExpressions(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Statements does not contain %d statements. got=%d", 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])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
exp, ok := stmt.Expression.(*ast.PrefixExpression)
@@ -176,8 +165,7 @@ func TestParsingPrefixExpressions(t *testing.T) {
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)
+ t.Fatalf("exp.Operator is not '%s'. got=%s", tt.operator, exp.Operator)
}
if !testLiteralExpression(t, exp.Right, tt.value) {
return
@@ -220,18 +208,15 @@ func TestParsingInfixExpressions(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Statements does not contain %d statements. got=%d", 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])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
- if !testInfixExpression(t, stmt.Expression, tt.leftValue,
- tt.operator, tt.rightValue) {
+ if !testInfixExpression(t, stmt.Expression, tt.leftValue, tt.operator, tt.rightValue) {
return
}
}
@@ -385,14 +370,12 @@ func TestBooleanExpression(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program has not enough statements. got=%d",
- len(program.Statements))
+ 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])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
boolean, ok := stmt.Expression.(*ast.Boolean)
@@ -400,8 +383,7 @@ func TestBooleanExpression(t *testing.T) {
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)
+ t.Errorf("boolean.Value not %t. got=%t", tt.expectedBoolean, boolean.Value)
}
}
}
@@ -415,20 +397,17 @@ func TestIfExpression(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Body does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Body does not contain %d statements. got=%d", 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])
+ 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)
+ t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T", stmt.Expression)
}
if !testInfixExpression(t, exp.Condition, "x", "<", "y") {
@@ -436,14 +415,12 @@ func TestIfExpression(t *testing.T) {
}
if len(exp.Consequence.Statements) != 1 {
- t.Errorf("consequence is not 1 statements. got=%d\n",
- len(exp.Consequence.Statements))
+ t.Errorf("consequence is not 1 statements. got=%d", 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])
+ t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T", exp.Consequence.Statements[0])
}
if !testIdentifier(t, consequence.Expression, "x") {
@@ -464,14 +441,12 @@ func TestIfElseExpression(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Body does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Body does not contain %d statements. got=%d", 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])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
exp, ok := stmt.Expression.(*ast.IfExpression)
@@ -484,14 +459,12 @@ func TestIfElseExpression(t *testing.T) {
}
if len(exp.Consequence.Statements) != 1 {
- t.Errorf("consequence is not 1 statements. got=%d\n",
- len(exp.Consequence.Statements))
+ t.Errorf("consequence is not 1 statements. got=%d", 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])
+ t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T", exp.Consequence.Statements[0])
}
if !testIdentifier(t, consequence.Expression, "x") {
@@ -499,14 +472,12 @@ func TestIfElseExpression(t *testing.T) {
}
if len(exp.Alternative.Statements) != 1 {
- t.Errorf("exp.Alternative.Statements does not contain 1 statements. got=%d\n",
- len(exp.Alternative.Statements))
+ t.Errorf("exp.Alternative.Statements does not contain 1 statements. got=%d", len(exp.Alternative.Statements))
}
alternative, ok := exp.Alternative.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
- exp.Alternative.Statements[0])
+ t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T", exp.Alternative.Statements[0])
}
if !testIdentifier(t, alternative.Expression, "y") {
@@ -523,39 +494,33 @@ func TestFunctionLiteralParsing(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Body does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Body does not contain %d statements. got=%d", 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])
+ 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)
+ 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))
+ t.Fatalf("function literal parameters wrong. want 2, got=%d", 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))
+ t.Fatalf("function.Body.Statements has not 1 statements. got=%d", 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])
+ t.Fatalf("function body stmt is not ast.ExpressionStatement. got=%T", function.Body.Statements[0])
}
testInfixExpression(t, bodyStmt.Expression, "x", "+", "y")
@@ -581,8 +546,7 @@ func TestFunctionParameterParsing(t *testing.T) {
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))
+ t.Errorf("length parameters wrong. want %d, got=%d", len(tt.expectedParams), len(function.Parameters))
}
for i, ident := range tt.expectedParams {
@@ -600,20 +564,17 @@ func TestCallExpressionParsing(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Statements does not contain %d statements. got=%d", 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])
+ 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)
+ t.Fatalf("stmt.Expression is not ast.CallExpression. got=%T", stmt.Expression)
}
if !testIdentifier(t, exp.Function, "add") {
@@ -661,8 +622,7 @@ func TestCallExpressionParameterParsing(t *testing.T) {
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)
+ t.Fatalf("stmt.Expression is not ast.CallExpression. got=%T", stmt.Expression)
}
if !testIdentifier(t, exp.Function, tt.expectedIdent) {
@@ -670,14 +630,12 @@ func TestCallExpressionParameterParsing(t *testing.T) {
}
if len(exp.Arguments) != len(tt.expectedArgs) {
- t.Fatalf("wrong number of arguments. want=%d, got=%d",
- len(tt.expectedArgs), len(exp.Arguments))
+ 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())
+ t.Errorf("argument %d wrong. want=%q, got=%q", i, arg, exp.Arguments[i].String())
}
}
}
@@ -1030,8 +988,7 @@ func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
}
if integ.TokenLiteral() != fmt.Sprintf("%d", value) {
- t.Errorf("integ.TokenLiteral not %d. got=%s", value,
- integ.TokenLiteral())
+ t.Errorf("integ.TokenLiteral not %d. got=%s", value, integ.TokenLiteral())
return false
}
@@ -1052,8 +1009,7 @@ func testIdentifier(t *testing.T, exp ast.Expression, value string) bool {
}
if ident.TokenLiteral() != value {
- t.Errorf("ident.TokenLiteral not %s. got=%s", value,
- ident.TokenLiteral())
+ t.Errorf("ident.TokenLiteral not %s. got=%s", value, ident.TokenLiteral())
return false
}
@@ -1074,8 +1030,7 @@ func testBooleanLiteral(t *testing.T, exp ast.Expression, value bool) bool {
}
if bo.TokenLiteral() != fmt.Sprintf("%t", value) {
- t.Errorf("bo.TokenLiteral not %t. got=%s",
- value, bo.TokenLiteral())
+ t.Errorf("bo.TokenLiteral not %t. got=%s", value, bo.TokenLiteral())
return false
}
@@ -1104,39 +1059,33 @@ func TestMacroLiteralParsing(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Statements does not contain %d statements. got=%d", 1, len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("statement is not ast.ExpressionStatement. got=%T",
- program.Statements[0])
+ t.Fatalf("statement is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
macro, ok := stmt.Expression.(*ast.MacroLiteral)
if !ok {
- t.Fatalf("stmt.Expression is not ast.MacroLiteral. got=%T",
- stmt.Expression)
+ t.Fatalf("stmt.Expression is not ast.MacroLiteral. got=%T", stmt.Expression)
}
if len(macro.Parameters) != 2 {
- t.Fatalf("macro literal parameters wrong. want 2, got=%d\n",
- len(macro.Parameters))
+ t.Fatalf("macro literal parameters wrong. want 2, got=%d", len(macro.Parameters))
}
testLiteralExpression(t, macro.Parameters[0], "x")
testLiteralExpression(t, macro.Parameters[1], "y")
if len(macro.Body.Statements) != 1 {
- t.Fatalf("macro.Body.Statements has not 1 statements. got=%d\n",
- len(macro.Body.Statements))
+ t.Fatalf("macro.Body.Statements has not 1 statements. got=%d", len(macro.Body.Statements))
}
bodyStmt, ok := macro.Body.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("macro body stmt is not ast.ExpressionStatement. got=%T",
- macro.Body.Statements[0])
+ t.Fatalf("macro body stmt is not ast.ExpressionStatement. got=%T", macro.Body.Statements[0])
}
testInfixExpression(t, bodyStmt.Expression, "x", "+", "y")
diff --git a/parser/parser_tracing.go b/parser/parser_tracing.go
index bdffbd5..937aee3 100644
--- a/parser/parser_tracing.go
+++ b/parser/parser_tracing.go
@@ -1,3 +1,4 @@
+//go:build ignore
// +build ignore
package parser
@@ -16,7 +17,7 @@ func identLevel() string {
}
func tracePrint(fs string) {
- fmt.Printf("%s%s\n", identLevel(), fs)
+ fmt.Println(identLevel(), fs)
}
func incIdent() { traceLevel = traceLevel + 1 }