def __cond(self): """<cond> ::= IF <bexpr> THEN <bstmts><condt> END""" condStmt = ast.IfStmt() self.__eat(token.IF, "expecting an if") condStmt.if_part.bool_expr = self.__bexpr() self.__eat(token.THEN, "expecting then") self.__bstmts(condStmt.if_part.stmt_list) self.__condt(condStmt) self.__eat(token.END, "expecting an end") return condStmt
def __cond(self): """<cond> ::= IF <bexpr> THEN <bstmts> <condt> END""" if_stmt_node = ast.IfStmt() self.__eat(token.IF, 'expecting "if"') if_stmt_node.if_part.bool_expr = self.__bexpr() self.__eat(token.THEN, 'expecting "then"') if_stmt_node.if_part.stmt_list = self.__bstmts(ast.StmtList()) if_stmt_node = self.__condt(if_stmt_node) self.__eat(token.END, 'expecting "end"') return if_stmt_node
def __cond(self): # print("cond: " + str(self.current_token)) self.__advance() # eat IF (we already know from bstmt) if_stmt_node = ast.IfStmt() if_stmt_node.if_part.bool_expr = self.__bexpr() # IfStmt node -> BasicIf node -> Boolean expression self.__eat(token.THEN, 'expected "then"') if_stmt_node.if_part.stmt_list = self.__bstmts(ast.StmtList()) # IfStmt node -> BasicIf node -> statement list if_stmt_node = self.__condt(if_stmt_node) self.__eat(token.END, 'expected "end"') return if_stmt_node
def __cond(self): self.__eat(token.IF, 'expecting if') if_node = ast.IfStmt() if_node.if_part.bool_expr = self.__bexpr() self.__eat(token.THEN, 'expecting then') self.__bstmts(if_node.if_part.stmt_list) self.__condt(if_node) self.__eat(token.END, 'expecting end') return if_node
def __cond(self): if_node = ast.IfStmt() basic_if_node = ast.BasicIf() basic_if_node.bool_expr = ast.BoolExpr() basic_if_node.stmt_list = ast.StmtList() self.__bexpr(basic_if_node.bool_expr) self.__eat(token.THEN, "expecting then") self.__bstmts(basic_if_node.stmt_list) if_node.if_part = basic_if_node self.__condt(if_node) self.__eat(token.END, "expecting end") return if_node
def __cond(self, stmt_list_node): condNode = ast.IfStmt() basicIfNode = ast.BasicIf() self.__eat(token.IF, 'expecting if') basicIfNode.bool_expr.first_expr = ast.Expr() self.__bexpr(basicIfNode.bool_expr) #might need to declare before pass self.__eat(token.THEN, 'expecting then') self.__bstmts(basicIfNode.stmt_list) #same here condNode.if_part = basicIfNode self.__condt(condNode) self.__eat(token.END, 'expecting end') stmt_list_node.stmts.append(condNode)
def __cond(self): """<cond> ::= IF <bexpr> THEN <bstmts> <condt> END""" if_node = ast.IfStmt() if_part_node = ast.BasicIf() self.__eat(token.IF, 'expecting an "if" keyword') if_part_node.bool_expr = self.__bexpr() self.__eat(token.THEN, 'expecting a "then" keyword') stmt_list_node = ast.StmtList() self.__bstmts(stmt_list_node) if_part_node.stmt_list = stmt_list_node if_node.if_part = if_part_node self.__condt(if_node) self.__eat(token.END, 'expecting an "end" keyword') return if_node
def __cond(self): """<cond> ::= IF <bexpr> THEN <bstmts> <condt> END""" if_stmt = ast.IfStmt() if_stmt.if_part = ast.BasicIf() self.__eat(token.IF, "expected 'if'") if_stmt.if_part.bool_expr = self.__bexpr() self.__eat(token.THEN, "expected 'then'") if_stmt.if_part.stmt_list = ast.StmtList() self.__bstmts(if_stmt.if_part.stmt_list.stmts) if_stmt.has_else = False self.__condt(if_stmt) self.__eat(token.END, "expected 'end'") return if_stmt
def stmts(self, stmt_list_node): # output statements if self.current_token.tokentype == mytoken.PRINT: self.advance() print_node = mypl_ast.PrintStmt() self.eat(mytoken.LPAREN, 'expected "("') print_node.expr = self.expr() self.eat(mytoken.RPAREN, 'expected ")"') self.eat(mytoken.SEMICOLON, 'expected ";"') stmt_list_node.stmts.append(print_node) self.stmts(stmt_list_node) elif self.current_token.tokentype == mytoken.PRINTLN: self.advance() print_node = mypl_ast.PrintStmt() print_node.is_println = True self.eat(mytoken.LPAREN, 'expected "("') print_node.expr = self.expr() self.eat(mytoken.RPAREN, 'expected ")"') self.eat(mytoken.SEMICOLON, 'expected ";"') stmt_list_node.stmts.append(print_node) self.stmts(stmt_list_node) # assignment statements elif self.current_token.tokentype == mytoken.ID: assign_node = mypl_ast.AssignStmt() assign_node.lhs = self.current_token index_expr_node = mypl_ast.IndexExpr() index_expr_node.identifier = self.current_token self.advance() index_expr_node2 = self.list_index(index_expr_node) if index_expr_node2.expr != None: assign_node.index_expr = index_expr_node2 self.eat(mytoken.ASSIGN, 'expected "="') expr_node = self.expr() assign_node.rhs = expr_node self.eat(mytoken.SEMICOLON, 'expected ";"') stmt_list_node.stmts.append(assign_node) self.stmts(stmt_list_node) # conditional statements elif self.current_token.tokentype == mytoken.IF: if_stmt_node = mypl_ast.IfStmt() self.advance() basic_if_node = mypl_ast.BasicIf() basic_if_node.bool_expr = self.bexpr() self.eat(mytoken.THEN, 'expected "then"') cond_stmts = mypl_ast.StmtList() self.stmts(cond_stmts) basic_if_node.stmt_list = cond_stmts if_stmt_node.if_part = basic_if_node if_stmt_node2 = self.cond_t(if_stmt_node) if_stmt_node.elseifs = if_stmt_node2.elseifs if_stmt_node.has_else = if_stmt_node2.has_else if_stmt_node.else_stmts = if_stmt_node2.else_stmts self.eat(mytoken.END, 'expected "end"') stmt_list_node.stmts.append(if_stmt_node) self.stmts(stmt_list_node) # loop statements elif self.current_token.tokentype == mytoken.WHILE: while_stmt_node = mypl_ast.WhileStmt() self.advance() while_stmt_node.bool_expr = self.bexpr() self.eat(mytoken.DO, 'expected "do"') while_stmts = mypl_ast.StmtList() while_stmt_node.stmt_list = while_stmts self.stmts(while_stmts) self.eat(mytoken.END, 'expected "end"') stmt_list_node.stmts.append(while_stmt_node) self.stmts(stmt_list_node)