Skip to content

Commit

Permalink
feat: array indexing parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Houcine EL ADDALI committed Jan 27, 2024
1 parent 7dc7dde commit de7404b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/parser/data/test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,5 @@ var (
}

Arrays = "[1,12 - 8 ,7]"
ArrayIndex = "nums[7-4];"
ArrayIndex = "nums[7-4]"
)
13 changes: 6 additions & 7 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func InitParser(l *lexer.Lexer) *Parser {
token.DECREMENT,
token.INCREMENT,
}, p.parsePostFixExpression)
p.addInfixFn(token.RB, p.parseIndexExp)
p.addInfixFn(token.LB, p.parseIndexExp)

return p
}
Expand Down Expand Up @@ -484,9 +484,9 @@ func (p *Parser) parseAssignmentExpr(left *ast.Identifier) ast.Expression {

func (p *Parser) parseArrayLit() ast.Expression {
exp := &ast.ArrayLiteral{
Token: p.currToken,
Token: p.currToken,
}
fmt.Println("------- parse array --------> ", p.currToken)

exp.Values = p.parseExpressionList(token.CreateToken(token.RB, "]"))

return exp
Expand All @@ -497,20 +497,19 @@ func (p *Parser) parseExpressionList(t token.Token) []ast.Expression {

if p.peekTokenEquals(t.Type) {
p.Next()
return res // an empty array
return res // an empty array
}

p.Next()
res = append(res, p.parseExpression(LOWEST))
fmt.Println("-------List exp--------> ", p.currToken)

for p.peekTokenEquals(token.COMMA) {
p.Next()
p.Next()
fmt.Println("-------List exp--------> ", p.currToken)

res = append(res, p.parseExpression(LOWEST))
}

fmt.Println("---------------> ", p.currToken)
if !p.expectedNextToken(t) {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func TestParseArraysLit(t *testing.T) {

}

/*func TestArrayIndexExp(t *testing.T) {
func TestArrayIndexExp(t *testing.T) {
input := data.ArrayIndex

pr, parser := getProg(input)
Expand All @@ -441,7 +441,7 @@ func TestParseArraysLit(t *testing.T) {
if !testInfixExpression(t, arrIdx.Index, 7, 4, "-") {
return
}
}*/
}

// Tests helper functions
func checkIsProgramStmLengthValid(program *ast.Program, t *testing.T, length int) {
Expand Down

0 comments on commit de7404b

Please sign in to comment.