def parse(self): """succeeds if program is syntactically well-formed""" stmt_list_node = mypl_ast.StmtList() self.advance() self.stmts(stmt_list_node) self.eat(mytoken.EOS, 'expecting end-of-file') return stmt_list_node
def __condt(self, if_node): if_node.else_stmts = ast.StmtList() basic_if_node = ast.BasicIf() basic_if_node.bool_expr = ast.BoolExpr() if self.current_token.tokentype == token.ELIF: basic_if_node.stmt_list = ast.StmtList() self.__advance() self.__bexpr(basic_if_node.bool_expr) self.__eat(token.THEN, "expecting then") self.__bstmts(basic_if_node.stmt_list) if_node.elseifs.append(basic_if_node) self.__condt(if_node) elif self.current_token.tokentype == token.ELSE: if_node.has_else = True self.__advance() self.__bstmts(if_node.else_stmts)
def parse(self): self.__advance() # None -> first token # passing in a StmtList node because __stmts() recurses with a StmtList parameter stmt_list_node = self.__stmts(ast.StmtList()) self.__eat(token.EOS, "expected EOS" ) # this could just be advance(), but it's here to be safe return stmt_list_node
def parse(self): """succeeds if program is syntatically well-formed""" stmt_list_node = ast.StmtList() self.__advance() self.__stmts(stmt_list_node) self.__eat(token.EOS, 'expecting end of file') return stmt_list_node
def __condt(self, if_stmt_node): """<condt> ::= ELIF <bexpr> THEN <bstmts> <condt> | ELSE <bstmts> | e""" if (self.current_token.tokentype == token.ELIF): self.__advance() basic_if_node = ast.BasicIf() basic_if_node.bool_expr = self.__bexpr() self.__eat(token.THEN, 'expecting "then"') basic_if_node.stmt_list = self.__bstmts(ast.StmtList()) if_stmt_node.elseifs.append(basic_if_node) return self.__condt(if_stmt_node) elif (self.current_token.tokentype == token.ELSE): if_stmt_node.has_else = True self.__advance() if_stmt_node.else_stmts = self.__bstmts(ast.StmtList()) return if_stmt_node else: return if_stmt_node
def __condt(self, if_stmt_node): # print("condt: " + str(self.current_token)) if self.current_token.tokentype == token.ELIF: self.__advance() # eat ELIF (we already know this from 1 line up) new_basic_if_node = ast.BasicIf() new_basic_if_node.bool_expr = self.__bexpr() self.__eat(token.THEN, 'expected "then"') new_basic_if_node.stmt_list = self.__bstmts(ast.StmtList()) if_stmt_node.elseifs.append(new_basic_if_node) return self.__condt(if_stmt_node) elif self.current_token.tokentype == token.ELSE: self.__advance() # eat ELSE (we already know this from 1 line up) if_stmt_node.has_else = True if_stmt_node.else_stmts = self.__bstmts(ast.StmtList()) return if_stmt_node else: return if_stmt_node
def __while(self): while_node = ast.WhileStmt() while_node.bool_expr = ast.BoolExpr() self.__bexpr(while_node.bool_expr) while_node.stmt_list = ast.StmtList() self.__eat(token.DO, "expecting do") self.__bstmts(while_node.stmt_list) self.__eat(token.END, "expecting end") return while_node
def __while(self): """<while> ::= WHILE <bexpr> DO <bstmts> END""" while_stmt_node = ast.WhileStmt() self.__eat(token.WHILE, 'expecting "while"') while_stmt_node.bool_expr = self.__bexpr() self.__eat(token.DO, 'expecting "do"') while_stmt_node.stmt_list = self.__bstmts(ast.StmtList()) self.__eat(token.END, 'expecting "end"') return while_stmt_node
def __while(self): # print("while: " + str(self.current_token)) self.__advance() # eat WHILE (we already know from bstmt) while_stmt_node = ast.WhileStmt() while_stmt_node.bool_expr = self.__bexpr() # WhileStmt boolean expression self.__eat(token.DO, 'expected "do"') while_stmt_node.stmt_list = self.__bstmts(ast.StmtList()) # WhileStmt statement list self.__eat(token.END, 'expected "end"') return while_stmt_node
def __condt(self, if_node): """<condt> ::= ELIF <bexpr> THEN <bstmts> <condt> | ELSE <bstmts> | e""" if self.current_token.tokentype == token.ELIF: self.__advance() if_part_node = ast.BasicIf() 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.elseifs.append(if_part_node) self.__condt(if_node) elif self.current_token.tokentype == token.ELSE: self.__advance() if_node.has_else = True stmt_list_node = ast.StmtList() self.__bstmts(stmt_list_node) if_node.else_stmts = stmt_list_node
def cond_t(self, if_stmt_node): if self.current_token.tokentype == mytoken.ELSEIF: basic_if_node = mypl_ast.BasicIf() self.advance() basic_if_node.bool_expr = self.bexpr() self.eat(mytoken.THEN, 'expected then') elseif_stmts = mypl_ast.StmtList() self.stmts(elseif_stmts) basic_if_node.stmt_list = elseif_stmts if_stmt_node.elseifs.append(basic_if_node) self.cond_t(if_stmt_node) elif self.current_token.tokentype == mytoken.ELSE: if_stmt_node.has_else = True else_stmts = mypl_ast.StmtList() self.advance() self.stmts(else_stmts) if_stmt_node.else_stmts = else_stmts return if_stmt_node
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 clear(): global value_table global type_table global repl_heap global total_stmt value_table = sym_tbl.SymbolTable() type_table = sym_tbl.SymbolTable() repl_heap = {} total_stmt = ast.StmtList()
def __while(self): """<while> ::= WHILE <bexpr> DO <bstmts> END""" while_stmt_node = ast.WhileStmt() self.__eat(token.WHILE, 'expecting a "while" keyword') while_stmt_node.bool_expr = self.__bexpr() self.__eat(token.DO, 'expecting a "do" keyword') stmt_list_node = ast.StmtList() self.__bstmts(stmt_list_node) while_stmt_node.stmt_list = stmt_list_node self.__eat(token.END, 'expecting an "end" keyword') return while_stmt_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): """<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 __fdecl(self, stmt_list_node): fun_node = ast.FunDeclStmt() fun_node.stmt_list = ast.StmtList() if self.current_token.tokentype == token.NIL: fun_node.return_type = self.current_token self.__advance() else: fun_node.return_type = self.__type() fun_node.fun_name = self.current_token self.__eat(token.ID, "expecting an ID") self.__eat(token.LPAREN, "expecting '('") fun_node.params = self.__params() self.__eat(token.RPAREN, "expecting ')'") self.__bstmts(fun_node.stmt_list) self.__eat(token.END, "expecting end") stmt_list_node.stmts.append(fun_node) # check
def __fdecl(self): """<fdecl> ::= FUN ( <type> | NIL ) ID LPAREN <params> RPAREN <bstmts> END""" fun_decl_node = ast.FunDeclStmt() self.__eat(token.FUN, 'expecting "fun"') fun_decl_node.return_type = self.current_token if (self.current_token.tokentype == token.NIL): self.__advance() else: self.__type() fun_decl_node.fun_name = self.current_token self.__eat(token.ID, 'expecting "ID"') self.__eat(token.LPAREN, 'expecting "("') fun_decl_node.params = self.__params() self.__eat(token.RPAREN, 'expecting ")"') fun_decl_node.stmt_list = self.__bstmts(ast.StmtList()) self.__eat(token.END, 'expecting "end"') return fun_decl_node
def __fdecl(self): """<fdecl> ::= FUN (<type>|NIL) ID LPAREN <params> RPAREN <bstmts> END""" fun_decl_node = ast.FunDeclStmt() self.__eat(token.FUN, "expecting fun keyword") if self.current_token.tokentype == token.NIL: self.__advance() else: fun_decl_node.return_type = self.__type() fun_decl_node.fun_name = self.current_token self.__eat(token.ID, "expecting an identifyer") self.__eat(token.LPAREN, 'expecting a "("') fun_decl_node.params = self.__params() self.__eat(token.RPAREN, 'expecting a ")"') stmt_list_node = ast.StmtList() self.__bstmts(stmt_list_node) fun_decl_node.stmt_list = stmt_list_node self.__eat(token.END, "expecting end") return fun_decl_node
def __fdecl(self): # print("fdecl: " + str(self.current_token)) self.__advance() # eat FUN (we already know from stmt) fun_decl_stmt_node = ast.FunDeclStmt() fun_decl_stmt_node.return_type = self.current_token # FunDeclStmt return type if self.current_token.tokentype == token.NIL: self.__advance() # eat NIL (we already know from 1 line up) else: self.__type() fun_decl_stmt_node.fun_name = self.current_token # FunDeclStmt function name self.__eat(token.ID, 'expected ID') self.__eat(token.LPAREN, 'expected "("') fun_decl_stmt_node.params = self.__params() self.__eat(token.RPAREN, 'expected ")"') # passing in a StmtList node because __bstmts() recurses with a StmtList parameter fun_decl_stmt_node.stmt_list = self.__bstmts(ast.StmtList()) self.__eat(token.END, 'expected "end"') return fun_decl_stmt_node
def parse(self): stmt_list_node = ast.StmtList() self.__advance() self.__stmts(stmt_list_node) self.__eat(token.EOS, 'expecting end of file') return stmt_list_node
import mypl_print_visitor as ast_printer import sys # needed for load import copy from io import StringIO # history try: import readline except ImportError: readline = None value_table = sym_tbl.SymbolTable() type_table = sym_tbl.SymbolTable() repl_heap = {} total_stmt = ast.StmtList() def main(): vocab = {'load', 'save', 'clear', 'exit', 'help'} readline.parse_and_bind('tab: complete') readline.set_completer(make_completer(vocab)) print('MyPL REPL Version 1.0') print('Use ":" for commands, :help for list of commands.') keepGoing = True while (keepGoing): cur_stmt = input('>>> ') # See if the user just pressed enter if (len(cur_stmt) == 0): pass
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)