def while_statement(self, level): """ WhileStatement -> while '(' Expression ')' Statement :param level: indicates level of statements; type(level) - int :return: ast.WhileStatement object :raise CliteSyntaxError if an unexpected token is seen """ # Consume the keyword if identifying the start of a while statement self.curr_tok = self.lex.__next__() # Match left parenthesis if self.curr_tok[self.CODE] != tokens.SINGLE_TOKENS[tokens.LPAREN][0]: raise errors.CliteSyntaxError("'(' expected!", self.curr_tok[self.LINE]) # Consume left parenthesis self.curr_tok = self.lex.__next__() expression = self.expression() # Match right parenthesis if self.curr_tok[self.CODE] != tokens.SINGLE_TOKENS[tokens.RPAREN][0]: raise errors.CliteSyntaxError("')' expected!", self.curr_tok[self.LINE]) # Consume right parenthesis self.curr_tok = self.lex.__next__() statement = self.statement(level + 1) return ast.WhileStatement(expression, statement, level)
def whilestatement(self): """ WhileStatement ⇒ while ( Expression ) Statement :return: """ # Check if the current token is a keyword While if self.currtok[0] == lexer.Lexer.KWDWHILE: self.currtok = next(self.tokens) # Check for an open parenthesis if self.currtok[0] == lexer.Lexer.OPENP: self.currtok = next(self.tokens) left_tree = self.expression() # Check for a closed parenthesis if self.currtok[0] == lexer.Lexer.CLOSEDP: self.currtok = next(self.tokens) right_tree = self.statement() left_tree = ast.WhileStatement(left_tree, right_tree) return left_tree # If there is no closed parenthesis, throw syntax error elif self.currtok[0] != lexer.Lexer.CLOSEDP: raise CliteSyntaxError("Parenthesis expected on line " + str(self.currtok[2])) # If there is no opening parenthesis, throw syntax error elif self.currtok[0] != lexer.Lexer.OPENP: raise CliteSyntaxError("Parenthesis expected on line " + str(self.currtok[2]))
def whileStatement(self): ''' WhileStatement --> while "(" Expression ")" Statement :return: ast.whileStatement ''' # need keyword 'while' if self.curr_tok[0] != lexer.Lexer.WHILE: print("Error: 'while' expected found:", self.curr_tok[1], "on line", self.curr_tok[2]) sys.exit(1) self.curr_tok = next(self.lex) # need '(' if self.curr_tok[0] != lexer.Lexer.LPAREN: print("Error: '(' expected found:", self.curr_tok[1], "on line", self.curr_tok[2]) sys.exit(1) self.curr_tok = next(self.lex) # need Expression expr = self.expr() # need ')' if self.curr_tok[0] != lexer.Lexer.RPAREN: print("Error: ')' expected found:", self.curr_tok[1], "on line", self.curr_tok[2]) sys.exit(1) self.curr_tok = next(self.lex) # need Statement stmt = self.statement() return ast.WhileStatement(expr, stmt)
def loop(p): return ast.WhileStatement(p[1], p[3])
def p_while_statement_02(t): '''while_statement : WHILE LPAR lor_expression RPAR SEMI''' t[0] = ast.WhileStatement(t[3], ast.NullNode())
def p_while_statement_01(t): '''while_statement : WHILE LPAR lor_expression RPAR statement''' t[0] = ast.WhileStatement(t[3], t[5]) t[5].isStatement = True
def statement_while(s): return ast.WhileStatement(s[2], s[5])
cond = self.expression() t = self.block_stmt() e : typing.Union[None, ast.Block, ast.IfElseStatement] = None if toks.add(self.match(token.ELSE)): if self.peek().type == token.IF: e = self.if_stmt() else: e = self.block_stmt() return ast.IfElseStatement(toks, cond, t, e) def while_stmt(self) -> ast.WhileStatement: if not (tok := self.match(token.WHILE)): raise Exception("Expected while.") cond = self.expression() t = self.block_stmt() return ast.WhileStatement([tok], cond, t) def print_stmt(self) -> ast.Print: toks = TokenList() if not toks.add(self.match(token.PRINT)): raise Exception("Expected print statement.") expr = self.expression() toks.add(self.match(token.SEMICOLON)) return ast.Print(toks, expr) def block_stmt(self) -> ast.Block: self.check_eof("Expected { for block statement.") toks = TokenList() if not toks.add(self.match(token.LBRACE)): raise ParseException("Expected { for block statement.") # Snapshot the current scope (before block)