Пример #1
0
 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)
Пример #2
0
 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
Пример #3
0
 def __condt(self, condNode):
     if self.current_token.tokentype == token.ELIF:
         basicIfNode = ast.BasicIf()
         self.__eat(token.ELIF, 'expecting elif')
         self.__bexpr(basicIfNode.bool_expr)  #here
         self.__eat(token.THEN, 'expecting then')
         self.__bstmts(basicIfNode.stmt_list)
         condNode.elseifs.append(basicIfNode)
         self.__condt(condNode)
     elif self.current_token.tokentype == token.ELSE:
         condNode.has_else = True
         self.__advance()
         self.__bstmts(condNode.else_stmts)  #here
Пример #4
0
 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
Пример #5
0
    def __condt(self, if_node):
        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')

            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)
Пример #6
0
    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
Пример #7
0
 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
Пример #8
0
 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
Пример #9
0
    def __condt(self, if_stmt):
        """<condt> ::= ELIF <bexpr> THEN <bstmts> <condt> | ELSE <bstmts> | e"""

        if self.__tokenIs(token.ELIF):
            basic_else_if = ast.BasicIf()
            self.__advance()
            basic_else_if.bool_expr = self.__bexpr()
            self.__eat(token.THEN, "expected 'then'")
            self.__bstmts(basic_else_if.stmt_list.stmts)
            if_stmt.elseifs.append(basic_else_if)
            self.__condt(if_stmt)
        elif self.__tokenIs(token.ELSE):
            if_stmt.has_else = True
            self.__advance()
            self.__bstmts(if_stmt.else_stmts.stmts)
        else:
            pass
Пример #10
0
 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
Пример #11
0
 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
Пример #12
0
 def __condt(self, nodeFromAbove):
     """<condt> ::= ELIF <bexpr> THEN <bstmts><condt>| ELSE <bstmts>| * """
     condtStmt = ast.BasicIf()
     thingsThatStartBSTMTS = [
         token.VAR, token.SET, token.IF, token.WHILE, token.RETURN,
         token.LPAREN, token.STRINGVAL, token.INTVAL, token.BOOLVAL,
         token.FLOATVAL, token.NIL, token.NEW, token.ID
     ]
     temp = self.current_token.tokentype
     if temp == token.ELIF:
         self.__advance()
         condtStmt.bool_expr = self.__bexpr()
         self.__eat(token.THEN, "expecting a then")
         self.__bstmts(condtStmt.stmt_list)
         nodeFromAbove.elseifs.append(condtStmt)
         self.__condt(nodeFromAbove)
     if temp == token.ELSE:
         self.__advance()
         nodeFromAbove.has_else = True
         if self.current_token.tokentype in thingsThatStartBSTMTS:
             self.__bstmts(nodeFromAbove.else_stmts)
Пример #13
0
    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)