visitIf

mail@pastecode.io avatar
unknown
python
a month ago
2.5 kB
0
Indexable
Never
    def visitIf(self, ast, o):
        if_exprCode, if_exprType = self.visit(ast.expr, Access(o.frame, o.sym, False, True))
        self.emit.printout(if_exprCode)
        # print("COMPARE CODE >>>",if_exprCode)
        labelExit = o.frame.getNewLabel()
        if ast.elseStmt is not None:
            labelElse = o.frame.getNewLabel()
            labelElif = o.frame.getNewLabel()
            self.emit.printout(self.emit.emitIFFALSE(labelElif, o.frame))
            self.emit.printout(self.emit.emitIFTRUE(labelExit, o.frame))
            self.visit(ast.thenStmt, o)
            
            # print('ELSE >>>', ast.elseStmt)
            self.emit.printout(self.emit.emitLABEL(labelElif, o.frame))

            labelElif_list = list()

            for _ in len(ast.elifStmt[1:]):        
                # print('STMT >>>',stmt)
                labelElif = o.frame.getNewLabel()
                labelElif_list.append(labelElif)
    
            for expr, stmt in ast.elifStmt:        
                # print('STMT >>>',stmt)
                labelElif = labelElif_list.pop(0)
                self.emit.printout(self.emit.emitLABEL(labelElif, o.frame))
                exprCode, _ = self.visit(expr, Access(o.frame, o.sym, False, True))
                self.emit.printout(exprCode)
                self.emit.printout(self.emit.emitIFTRUE(labelExit, o.frame))
                self.emit.printout(self.emit.emitIFFALSE(labelElif, o.frame))
                self.visit(stmt, o)
            # if self.has_return == False:
            #     self.emit.printout(self.emit.emitGOTO(labelExit, o.frame))
            self.emit.printout(self.emit.emitLABEL(labelElse, o.frame))
            self.visit(ast.elseStmt, o)
            # self.emit.printout(self.emit.emitGOTO(labelExit, o.frame))
            #if self.has_return == False:
            self.emit.printout(self.emit.emitLABEL(labelExit, o.frame))
        else:
            self.visit(ast.thenStmt, o) 
            self.emit.printout(self.emit.emitIFFALSE(labelExit, o.frame))
            for stmt_element in ast.elifStmt:        
                expr, stmt = stmt_element
                # print('STMT >>>',stmt)
                exprCode, exprType = self.visit(expr, Access(o.frame, o.sym, False, True))
                self.emit.printout(exprCode)
                self.emit.printout(self.emit.emitIFTRUE(labelExit, o.frame))
                self.visit(stmt, o)
            self.emit.printout(self.emit.emitLABEL(labelExit, o.frame))
Leave a Comment