This repository was archived by the owner on Oct 3, 2021. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -16,3 +16,16 @@ func (vd *Assign) TokenLiteral() string { return vd.Token.Name() }
1616func (vd * Assign ) String () string {
1717 return fmt .Sprintf ("%v = %v" , vd .Identifier .Value , vd .Value .String ())
1818}
19+
20+ type IndexAssign struct {
21+ Token tokens.Token
22+ Object Expression
23+ Index Expression
24+ Value Expression
25+ }
26+
27+ func (vd * IndexAssign ) statementNode () {}
28+ func (vd * IndexAssign ) TokenLiteral () string { return vd .Token .Name () }
29+ func (vd * IndexAssign ) String () string {
30+ return fmt .Sprintf ("%v = %v" , vd .Object .String (), vd .Value .String ())
31+ }
Original file line number Diff line number Diff line change @@ -48,6 +48,7 @@ func (p *Parser) parseIndexExpression(left ast.Expression) ast.Expression {
4848 }
4949
5050 p .NextToken ()
51+
5152 if p .CurrentToken .Type != tokens .RightBracket {
5253 p .AddError (fmt .Sprintf ("wrong token. expected=%q. got=%q" , "]" , p .CurrentToken .Literal ))
5354 return nil
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ func (p *Parser) parseStatement() ast.Statement {
2929 }
3030}
3131
32- func (p * Parser ) parseExpressionStatement () * ast.ExpressionStatement {
32+ func (p * Parser ) parseExpressionStatement () ast.Statement {
3333 expression := & ast.ExpressionStatement {
3434 Token : p .CurrentToken ,
3535 }
@@ -40,6 +40,23 @@ func (p *Parser) parseExpressionStatement() *ast.ExpressionStatement {
4040 p .NextToken ()
4141 }
4242
43+ if arr , ok := expression .Expression .(* ast.IndexExpression ); ok {
44+ fmt .Println (":)" )
45+ if p .peekTokenIs (tokens .Assign ) {
46+ p .NextToken ()
47+ p .NextToken ()
48+
49+ value := p .parseExpression (precedence .LOWEST )
50+
51+ return & ast.IndexAssign {
52+ Token : p .CurrentToken ,
53+ Object : arr .Value ,
54+ Index : arr .Index ,
55+ Value : value ,
56+ }
57+ }
58+ }
59+
4360 return expression
4461}
4562
Original file line number Diff line number Diff line change @@ -17,6 +17,8 @@ true; false;
1717if(true) {} else if(true) {}else if(true) {} else {};
18181000;
1919i = 100
20+
21+ test[0] = 20
2022`
2123
2224 l := lexer .Create (input )
@@ -31,7 +33,7 @@ i = 100
3133 log .Fatalln ("Errors! ^" )
3234 }
3335
34- if len (program .Statements ) != 8 {
36+ if len (program .Statements ) != 9 {
3537 t .Fatalf ("len(program.Statements) is not correct. expected=%d. got=%d" , 7 , len (program .Statements ))
3638 }
3739}
You can’t perform that action at this time.
0 commit comments