Change assignment into expressions
Some checks failed
Test / build (push) Waiting to run
Build / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-19 20:30:30 -04:00
parent aa0582ed72
commit be81b9a6d6
12 changed files with 478 additions and 153 deletions

View File

@@ -2,6 +2,7 @@ package parser
import (
"fmt"
"github.com/stretchr/testify/assert"
"monkey/ast"
"monkey/lexer"
"testing"
@@ -540,6 +541,85 @@ func TestIfElseExpression(t *testing.T) {
}
}
func TestIfElseIfExpression(t *testing.T) {
input := `if (x < y) { x } else if (x == y) { 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 {
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 len(exp.Alternative.Statements) != 1 {
t.Errorf("exp.Alternative.Statements does not contain 1 statements. got=%d\n",
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])
}
altexp, ok := alternative.Expression.(*ast.IfExpression)
if !ok {
t.Fatalf("alternative.Expression is not ast.IfExpression. got=%T", alternative.Expression)
}
if !testInfixExpression(t, altexp.Condition, "x", "==", "y") {
return
}
if len(altexp.Consequence.Statements) != 1 {
t.Errorf("consequence is not 1 statements. got=%d\n",
len(altexp.Consequence.Statements))
}
altconsequence, ok := altexp.Consequence.Statements[0].(*ast.ExpressionStatement)
if !ok {
t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
exp.Consequence.Statements[0])
}
if !testIdentifier(t, altconsequence.Expression, "y") {
return
}
}
func TestFunctionLiteralParsing(t *testing.T) {
input := `fn(x, y) { x + y; }`
@@ -952,15 +1032,18 @@ func TestWhileExpression(t *testing.T) {
}
}
func TestAssignmentStatements(t *testing.T) {
func TestAssignmentExpressions(t *testing.T) {
assertions := assert.New(t)
tests := []struct {
input string
expectedIdentifier string
expectedValue interface{}
input string
expected string
}{
{"x = 5;", "x", 5},
{"y = true;", "y", true},
{"foobar = y;", "foobar", "y"},
{"x = 5;", "x=5"},
{"y = true;", "y=true"},
{"foobar = y;", "foobar=y"},
{"[1, 2, 3][1] = 4", "([1, 2, 3][1])=4"},
{`{"a": 1}["b"] = 2`, `({a:1}[b])=2`},
}
for _, tt := range tests {
@@ -969,20 +1052,7 @@ func TestAssignmentStatements(t *testing.T) {
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]
if !testAssignmentStatement(t, stmt, tt.expectedIdentifier) {
return
}
val := stmt.(*ast.AssignmentStatement).Value
if !testLiteralExpression(t, val, tt.expectedValue) {
return
}
assertions.Equal(tt.expected, program.String())
}
}
@@ -1132,32 +1202,6 @@ func checkParserErrors(t *testing.T, p *Parser) {
t.FailNow()
}
func testAssignmentStatement(t *testing.T, s ast.Statement, name string) bool {
if s.TokenLiteral() != "=" {
t.Errorf("s.TokenLiteral not '='. got=%q", s.TokenLiteral())
return false
}
assignStmt, ok := s.(*ast.AssignmentStatement)
if !ok {
t.Errorf("s not *ast.AssignmentStatement. got=%T", s)
return false
}
if assignStmt.Name.Value != name {
t.Errorf("assignStmt.Name.Value not '%s'. got=%s", name, assignStmt.Name.Value)
return false
}
if assignStmt.Name.TokenLiteral() != name {
t.Errorf("assignStmt.Name.TokenLiteral() not '%s'. got=%s",
name, assignStmt.Name.TokenLiteral())
return false
}
return true
}
func TestComments(t *testing.T) {
tests := []struct {
input string