def __assign(self): assign_node = ast.AssignStmt() assign_node.lhs = self.__lvalue() self.__eat(token.ASSIGN, "expecting '='") assign_node.rhs = self.__expr() self.__eat(token.SEMICOLON, "expecting ';'") return assign_node
def __assign(self, stmt_list_node): assignNode = ast.AssignStmt() self.__eat(token.SET, 'expecting set') self.__lvalue(assignNode) self.__eat(token.ASSIGN, 'expecting =') assignNode.rhs = self.__expr() self.__eat(token.SEMICOLON, 'expecting ;') stmt_list_node.stmts.append(assignNode)
def __assign(self): """<assign> ::= SET <lvalue> ASSIGN <expr> SEMICOLON""" assign_node = ast.AssignStmt() self.__eat(token.SET, 'expecting a "set" keyword') assign_node.lhs = self.__lvalue() self.__eat(token.ASSIGN, 'expectring a "="') assign_node.rhs = self.__expr() self.__eat(token.SEMICOLON, "Expecting a semicolon") return assign_node
def __assign(self): """<assign> ::= SET <lvalue> ASSIGN <expr> SEMICOLON""" assignStmt = ast.AssignStmt() self.__eat(token.SET, "expecting set") assignStmt.lhs = self.__lvalue() self.__eat(token.ASSIGN, "expecting '='") assignStmt.rhs = self.__expr() self.__eat(token.SEMICOLON, "expecting ';'") return assignStmt
def __assign(self): self.__eat(token.SET, 'expecting set') assign_node = ast.AssignStmt() #lvalue_node = ast.LValue() self.__lvalue(assign_node) self.__eat(token.ASSIGN, 'expecting assign') assign_node.rhs = self.__expr() self.__eat(token.SEMICOLON, 'expecting semicolon') return assign_node
def __assign(self): """<assign> ::= SET <lvalue> ASSIGN <expr> SEMICOLON""" assign_stmt_node = ast.AssignStmt() self.__eat(token.SET, 'expecting "set"') assign_stmt_node.lhs = self.__lvalue() self.__eat(token.ASSIGN, 'expecting "="') assign_stmt_node.rhs = self.__expr() self.__eat(token.SEMICOLON, 'expecting ";"') return assign_stmt_node
def __assign(self): # print("assign: " + str(self.current_token)) self.__advance() # eat SET (we already know from bstmt) assign_stmt_node = ast.AssignStmt() assign_stmt_node.lhs = self.__lvalue() # AssignStmt lhs self.__eat(token.ASSIGN, 'expected "="') assign_stmt_node.rhs = self.__expr() # AssignStmt rhs self.__eat(token.SEMICOLON, 'expected ";"') return assign_stmt_node
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)