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

@@ -11,6 +11,7 @@ import (
const (
_ int = iota
LOWEST
ASSIGN // =
EQUALS // ==
LESSGREATER // > or <
SUM // +
@@ -21,6 +22,7 @@ const (
)
var precedences = map[token.TokenType]int{
token.ASSIGN: ASSIGN,
token.EQ: EQUALS,
token.NOT_EQ: EQUALS,
token.LT: LESSGREATER,
@@ -86,6 +88,7 @@ func New(l *lexer.Lexer) *Parser {
p.registerInfix(token.GTE, p.parseInfixExpression)
p.registerInfix(token.LPAREN, p.parseCallExpression)
p.registerInfix(token.LBRACKET, p.parseIndexExpression)
p.registerInfix(token.ASSIGN, p.parseAssignmentExpression)
// Read two tokens, so curToken and peekToken are both set
p.nextToken()
@@ -153,10 +156,6 @@ func (p *Parser) ParseProgram() *ast.Program {
}
func (p *Parser) parseStatement() ast.Statement {
if p.peekToken.Type == token.ASSIGN {
return p.parseAssignmentStatement()
}
switch p.curToken.Type {
case token.COMMENT:
return p.parseComment()
@@ -347,6 +346,18 @@ func (p *Parser) parseIfExpression() ast.Expression {
if p.peekTokenIs(token.ELSE) {
p.nextToken()
if p.peekTokenIs(token.IF) {
p.nextToken()
expression.Alternative = &ast.BlockStatement{
Statements: []ast.Statement{
&ast.ExpressionStatement{
Expression: p.parseIfExpression(),
},
},
}
return expression
}
if !p.expectPeek(token.LBRACE) {
return nil
}
@@ -534,20 +545,22 @@ func (p *Parser) parseWhileExpression() ast.Expression {
return expression
}
func (p *Parser) parseAssignmentStatement() ast.Statement {
stmt := &ast.AssignmentStatement{Token: p.peekToken}
stmt.Name = &ast.Identifier{Token: p.curToken, Value: p.curToken.Literal}
p.nextToken()
p.nextToken()
stmt.Value = p.parseExpression(LOWEST)
if p.peekTokenIs(token.SEMICOLON) {
p.nextToken()
func (p *Parser) parseAssignmentExpression(exp ast.Expression) ast.Expression {
switch node := exp.(type) {
case *ast.Identifier, *ast.IndexExpression:
default:
msg := fmt.Sprintf("expected identifier or index expression on left but got %T %#v", node, exp)
p.errors = append(p.errors, msg)
return nil
}
return stmt
ae := &ast.AssignmentExpression{Token: p.curToken, Left: exp}
p.nextToken()
ae.Value = p.parseExpression(LOWEST)
return ae
}
func (p *Parser) parseComment() ast.Statement {

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