Advertisement
minh_tran_782

PPLCodeGenQuiz

Mar 21st, 2023 (edited)
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.94 KB | None | 0 0
  1.  # Programming code: Code Generation (P1)
  2.     def visitIntLiteral(self,ctx,o):
  3.         # self.emit
  4.         # o.frame
  5.         code = self.emit.emitPUSHICONST(ctx.value, o.frame)
  6.         typ = IntType()
  7.         return code, typ
  8.  
  9. ###############
  10.     def visitFloatLiteral(self,ctx,o):
  11.         code = self.emit.emitPUSHFCONST(ctx.value, o.frame)
  12.         typ = FloatType()
  13.         return code, typ
  14. ##################
  15.     def visitBinExpr(self, ctx, o):
  16.         op = ctx.op
  17.         e1c, e1t = self.visit(ctx.e1, o)
  18.         e2c, e2t = self.visit(ctx.e2, o)
  19.        
  20.         if op in ['+', '-']:
  21.             return e1c + e2c + self.emit.emitADDOP(op, IntType(), o.frame), IntType()
  22.         elif op in ['*', '/']:
  23.             return e1c + e2c + self.emit.emitMULOP(op, IntType(), o.frame), IntType()
  24.         elif op == '+.':
  25.             return e1c + e2c + self.emit.jvm.emitFADD(), FloatType()
  26.         elif op == '-.':
  27.             return e1c + e2c + self.emit.jvm.emitFSUB(), FloatType()
  28.         elif op == '*.':
  29.             return e1c + e2c+ self.emit.jvm.emitFMUL(), FloatType()
  30.         elif op == '/.':
  31.             return e1c + e2c + self.emit.jvm.emitFDIV(), FloatType()
  32. ##############################
  33.     def visitId(self, ctx, o):
  34.         for symbol in o.sym:
  35.             if(ctx.name == symbol.name):
  36.                 sym = symbol
  37.                 break
  38.         if(isinstance(sym.value, Index)):
  39.             return self.emit.emitREADVAR(sym.name, sym.mtype, sym.value.value, o.frame), sym.mtype
  40.         else:
  41.             return self.emit.emitGETSTATIC(sym.value.value + "." + sym.name, sym.mtype, o.frame), sym.mtype
  42. ###############################
  43.     def visitBinExpr(self, ctx, o):
  44.         op = ctx.op
  45.         e1c, e1t = self.visit(ctx.e1, o)
  46.         e2c, e2t = self.visit(ctx.e2, o)
  47.        
  48.         if isinstance(e1t, FloatType) or isinstance(e2t, FloatType):
  49.             mType = FloatType()
  50.         else:
  51.             mType = IntType()
  52.            
  53.         if op == '/': mType = FloatType()
  54.        
  55.         if type(e1t) is IntType and type(mType) != type(e1t):
  56.             e1c = e1c + self.emit.emitI2F(o.frame)
  57.        
  58.         if type(e2t) is IntType and type(mType) != type(e2t):
  59.             e2c = e2c + self.emit.emitI2F(o.frame)
  60.        
  61.         if op in ['>', '<', '>=', '<=', '!=', '==']:
  62.             return e1c + e2c + self.emit.emitREOP(op, mType, o.frame), BoolType()
  63.        
  64.         if op in ['+', '-']:
  65.             return e1c + e2c + self.emit.emitADDOP(op, mType, o.frame), mType
  66.        
  67.         if op in ['*']:
  68.             return e1c + e2c + self.emit.emitMULOP(op, mType, o.frame), mType
  69.        
  70.         if op in ['/']:
  71.             return e1c + e2c + self.emit.emitMULOP(op, mType, o.frame), mType
  72.  # Programming code: Code Generation (P2)
  73.     def visitVarDecl(self,ctx,o):
  74.         myFrame  = o.frame
  75.         if myFrame is None:
  76.             emittedStr = self.emit.emitATTRIBUTE(ctx.name, ctx.typ, None)
  77.             self.emit.printout(emittedStr)
  78.             return Symbol(ctx.name, ctx.typ, CName("MCClass"))
  79.         else:
  80.             emittedStr = self.emit.emitVAR(myFrame.getCurrIndex(), ctx.name, ctx.typ, myFrame.getStartLabel(), myFrame.getEndLabel())
  81.             self.emit.printout(emittedStr)
  82.             result = Symbol(ctx.name, ctx.typ, Index(myFrame.getNewIndex()))
  83.             return result
  84. ########################
  85.     def visitId(self, ctx, o):
  86.         for i in o.sym:
  87.             if i.name == ctx.name:
  88.                 if o.isLeft == False:
  89.                     if isinstance(i.value.value, int):
  90.                         return self.emit.emitREADVAR(i.name, i.mtype, i.value.value, o.frame), i.mtype
  91.                     return self.emit.emitGETSTATIC(i.value.value + '/' + i.name, i.mtype, o.frame), i.mtype
  92.                 else:
  93.                     if isinstance(i.value.value, int):
  94.                         return self.emit.emitWRITEVAR(i.name, i.mtype, i.value.value, o.frame), i.mtype
  95.                     return self.emit.emitPUTSTATIC(i.value.value + '/' + i.name, i.mtype, o.frame), i.mtype
  96. ###############################
  97.     def visitAssign(self, ctx, o):
  98.         left_access = Access(o.frame, o.sym, True)
  99.         right_access = Access(o.frame, o.sym, False)
  100.         rhs = ctx.rhs.accept(self, right_access)
  101.         lhs = ctx.lhs.accept(self, left_access)
  102.         return self.emit.printout(rhs[0] + lhs[0])
  103. ####################################
  104.     def visitIf(self,ctx,o):
  105.         if_exprCode, if_exprType = ctx.expr.accept(self, Access(o.frame, o.sym, False))
  106.         if ctx.estmt:
  107.             labelElse = o.frame.getNewLabel()
  108.             labelExit = o.frame.getNewLabel()
  109.             self.emit.printout(if_exprCode)
  110.             self.emit.printout(self.emit.emitIFFALSE(labelElse, o.frame))
  111.             ctx.tstmt.accept(self, o)
  112.             self.emit.printout(self.emit.emitGOTO(labelExit, o.frame))
  113.             self.emit.printout(self.emit.emitLABEL(labelElse, o.frame))
  114.             ctx.estmt.accept(self, o)
  115.             self.emit.printout(self.emit.emitLABEL(labelExit, o.frame))
  116.         else:
  117.             labelExit = o.frame.getNewLabel()
  118.             self.emit.printout(if_exprCode)
  119.             self.emit.printout(self.emit.emitIFFALSE(labelExit, o.frame))
  120.             ctx.tstmt.accept(self, o)
  121.             self.emit.printout(self.emit.emitLABEL(labelExit, o.frame))
  122. ##############################
  123.     def visitWhile(self,ctx,o):
  124.         o.frame.enterLoop()
  125.         labelExit = o.frame.getBreakLabel()
  126.         self.emit.printout(self.emit.emitLABEL(o.frame.getContinueLabel(), o.frame))
  127.         if_exprCode, if_exprType = ctx.expr.accept(self, Access(o.frame, o.sym, False))
  128.         self.emit.printout(if_exprCode)
  129.         self.emit.printout(self.emit.emitIFFALSE(labelExit, o.frame))
  130.         ctx.stmt.accept(self, o)
  131.         self.emit.printout(self.emit.emitGOTO(o.frame.getContinueLabel(), o.frame))
  132.         self.emit.printout(self.emit.emitLABEL(labelExit, o.frame))
  133.         o.frame.exitLoop()
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement