diff options
author | Alessandro Arzilli <alessandro.arzilli@gmail.com> | 2018-12-07 17:28:35 +0100 |
---|---|---|
committer | alandonovan <adonovan@google.com> | 2018-12-07 11:28:35 -0500 |
commit | 678bafeaa214cf33342b28e76b77c0f1e264671f (patch) | |
tree | e238dd59443cb8ec3df6453c71101cd945853aab /syntax/parse.go | |
parent | d0a402bd6a104746d6256a517e57ce1adde31b45 (diff) | |
download | starlark-go-678bafeaa214cf33342b28e76b77c0f1e264671f.tar.gz |
Add while statement and allow recursion (#39)
Adds a flag, AllowRecursion, that enables while statements and
recursive functions.
Diffstat (limited to 'syntax/parse.go')
-rw-r--r-- | syntax/parse.go | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/syntax/parse.go b/syntax/parse.go index 6bc12b3..08a40e3 100644 --- a/syntax/parse.go +++ b/syntax/parse.go @@ -111,6 +111,8 @@ func (p *parser) parseStmt(stmts []Stmt) []Stmt { return append(stmts, p.parseIfStmt()) } else if p.tok == FOR { return append(stmts, p.parseForStmt()) + } else if p.tok == WHILE { + return append(stmts, p.parseWhileStmt()) } return p.parseSimpleStmt(stmts) } @@ -182,6 +184,18 @@ func (p *parser) parseForStmt() Stmt { } } +func (p *parser) parseWhileStmt() Stmt { + whilepos := p.nextToken() // consume WHILE + cond := p.parseTest() + p.consume(COLON) + body := p.parseSuite() + return &WhileStmt{ + While: whilepos, + Cond: cond, + Body: body, + } +} + // Equivalent to 'exprlist' production in Python grammar. // // loop_variables = primary_with_suffix (COMMA primary_with_suffix)* COMMA? |