From 678bafeaa214cf33342b28e76b77c0f1e264671f Mon Sep 17 00:00:00 2001 From: Alessandro Arzilli Date: Fri, 7 Dec 2018 17:28:35 +0100 Subject: Add while statement and allow recursion (#39) Adds a flag, AllowRecursion, that enables while statements and recursive functions. --- syntax/parse.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'syntax/parse.go') 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? -- cgit v1.2.3