Skip to content

Commit

Permalink
fix: let&return value parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Houcine EL ADDALI committed Dec 17, 2023
1 parent 46bb884 commit fad3808
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
10 changes: 6 additions & 4 deletions parser/data/test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ var (
`
DefStmExpected = []struct {
ExpectedIdentifier string
ExpectedValue int
}{
{"num1"},
{"total"},
{"a"},
{"num1", 13},
{"total", 0},
{"a", 5321},
}

ReturnStm = `
return 545;
return 101232;
return 0;
`
Identifier = `varName;`
ExpectedReturnValue = []int{545, 101232, 0}
Identifier = `varName;`

IntegerLit = "81;"

Expand Down
12 changes: 8 additions & 4 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ func (p *Parser) parseDefStmt() *ast.DefStatement {
return nil
}

for !p.currentTokenEquals(token.S_COLON) {
p.NextToken() // move to the expression after =

stm.Value = p.parseExpression(LOWEST)

for p.peekTokenEquals(token.S_COLON) {
p.NextToken()
}

Expand Down Expand Up @@ -262,9 +266,9 @@ func (p *Parser) parseReturnStmt() *ast.ReturnStatement {
stm := &ast.ReturnStatement{Token: p.currToken}

p.NextToken()

//TODO:
for !p.currentTokenEquals(token.S_COLON) {
stm.ReturnValue = p.parseExpression(LOWEST)
//
for p.peekTokenEquals(token.S_COLON) {
p.NextToken()
}

Expand Down
19 changes: 14 additions & 5 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@ func TestDefStatement(t *testing.T) {

stm := program.Statements[i]

if !testDefStatement(t, stm, t1.ExpectedIdentifier) {
if !testDefStatement(t, stm, t1.ExpectedIdentifier, t1.ExpectedValue) {
return
}

}
}

// test return statement
func TestReturnStatement(t *testing.T) {

input := data.ReturnStm
pr, _ := getProg(input)
expectReturnValue := data.ExpectedReturnValue
pr, parser := getProg(input)

//check is length of the program statement slice is 3
checkParserErrors(parser, t)
checkIsProgramStmLengthValid(pr, t, 3)

for _, stm := range pr.Statements {
for i, stm := range pr.Statements {
returnStm, ok := stm.(*ast.ReturnStatement)
if !ok {
t.Errorf("statement not *ast.ReturnStatement type got:%T", stm)
Expand All @@ -55,6 +57,9 @@ func TestReturnStatement(t *testing.T) {
t.Errorf("returnStatement token literal is not 'return' instead got: %s",
returnStm.TokenLiteral())
}
if !testLiteralExpression(t, returnStm.ReturnValue, expectReturnValue[i]) {
return
}

}
}
Expand Down Expand Up @@ -342,7 +347,7 @@ func checkParserErrors(p *Parser, t *testing.T) {
t.FailNow() // mark tests as failed and stop execution
}

func testDefStatement(t *testing.T, stm ast.Statement, name string) bool {
func testDefStatement(t *testing.T, stm ast.Statement, name string, val int) bool {
if stm.TokenLiteral() != "def" {
t.Errorf("s.tokenLiteral is not 'def'. got instead:%q", stm.TokenLiteral())
return false
Expand All @@ -365,6 +370,10 @@ func testDefStatement(t *testing.T, stm ast.Statement, name string) bool {
return false
}

if !testLiteralExpression(t, defStm.Value, val) {
return false
}

return true
}

Expand Down

0 comments on commit fad3808

Please sign in to comment.