reassign values
Some checks failed
Build / build (push) Failing after 1m46s
Test / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-15 15:24:48 -04:00
parent c818591f79
commit 2988719c9c
13 changed files with 327 additions and 17 deletions

View File

@@ -127,6 +127,11 @@ func (p *Parser) noPrefixParseFnError(t token.TokenType) {
p.errors = append(p.errors, msg)
}
func (p *Parser) noInfixParseFnError(t token.TokenType) {
msg := fmt.Sprintf("no infix parse function for %s found", t)
p.errors = append(p.errors, msg)
}
func (p *Parser) ParseProgram() *ast.Program {
program := &ast.Program{}
program.Statements = []ast.Statement{}
@@ -143,6 +148,10 @@ 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.LET:
return p.parseLetStatement()
@@ -218,7 +227,8 @@ func (p *Parser) parseExpression(precedence int) ast.Expression {
for !p.peekTokenIs(token.SEMICOLON) && precedence < p.peekPrecedence() {
infix := p.infixParseFns[p.peekToken.Type]
if infix == nil {
return leftExp
p.noInfixParseFnError(p.peekToken.Type)
//return leftExp
}
p.nextToken()
@@ -516,3 +526,19 @@ 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()
}
return stmt
}

View File

@@ -921,6 +921,40 @@ func TestWhileExpression(t *testing.T) {
}
}
func TestAssignmentStatements(t *testing.T) {
tests := []struct {
input string
expectedIdentifier string
expectedValue interface{}
}{
{"x = 5;", "x", 5},
{"y = true;", "y", true},
{"foobar = y;", "foobar", "y"},
}
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]
if !testAssignmentStatement(t, stmt, tt.expectedIdentifier) {
return
}
val := stmt.(*ast.AssignmentStatement).Value
if !testLiteralExpression(t, val, tt.expectedValue) {
return
}
}
}
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())
@@ -1066,3 +1100,29 @@ 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
}