Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Programming code: Code Generation (P1)
- def visitIntLiteral(self,ctx,o):
- # self.emit
- # o.frame
- code = self.emit.emitPUSHICONST(ctx.value, o.frame)
- typ = IntType()
- return code, typ
- ###############
- def visitFloatLiteral(self,ctx,o):
- code = self.emit.emitPUSHFCONST(ctx.value, o.frame)
- typ = FloatType()
- return code, typ
- ##################
- def visitBinExpr(self, ctx, o):
- op = ctx.op
- e1c, e1t = self.visit(ctx.e1, o)
- e2c, e2t = self.visit(ctx.e2, o)
- if op in ['+', '-']:
- return e1c + e2c + self.emit.emitADDOP(op, IntType(), o.frame), IntType()
- elif op in ['*', '/']:
- return e1c + e2c + self.emit.emitMULOP(op, IntType(), o.frame), IntType()
- elif op == '+.':
- return e1c + e2c + self.emit.jvm.emitFADD(), FloatType()
- elif op == '-.':
- return e1c + e2c + self.emit.jvm.emitFSUB(), FloatType()
- elif op == '*.':
- return e1c + e2c+ self.emit.jvm.emitFMUL(), FloatType()
- elif op == '/.':
- return e1c + e2c + self.emit.jvm.emitFDIV(), FloatType()
- ##############################
- def visitId(self, ctx, o):
- for symbol in o.sym:
- if(ctx.name == symbol.name):
- sym = symbol
- break
- if(isinstance(sym.value, Index)):
- return self.emit.emitREADVAR(sym.name, sym.mtype, sym.value.value, o.frame), sym.mtype
- else:
- return self.emit.emitGETSTATIC(sym.value.value + "." + sym.name, sym.mtype, o.frame), sym.mtype
- ###############################
- def visitBinExpr(self, ctx, o):
- op = ctx.op
- e1c, e1t = self.visit(ctx.e1, o)
- e2c, e2t = self.visit(ctx.e2, o)
- if isinstance(e1t, FloatType) or isinstance(e2t, FloatType):
- mType = FloatType()
- else:
- mType = IntType()
- if op == '/': mType = FloatType()
- if type(e1t) is IntType and type(mType) != type(e1t):
- e1c = e1c + self.emit.emitI2F(o.frame)
- if type(e2t) is IntType and type(mType) != type(e2t):
- e2c = e2c + self.emit.emitI2F(o.frame)
- if op in ['>', '<', '>=', '<=', '!=', '==']:
- return e1c + e2c + self.emit.emitREOP(op, mType, o.frame), BoolType()
- if op in ['+', '-']:
- return e1c + e2c + self.emit.emitADDOP(op, mType, o.frame), mType
- if op in ['*']:
- return e1c + e2c + self.emit.emitMULOP(op, mType, o.frame), mType
- if op in ['/']:
- return e1c + e2c + self.emit.emitMULOP(op, mType, o.frame), mType
- # Programming code: Code Generation (P2)
- def visitVarDecl(self,ctx,o):
- myFrame = o.frame
- if myFrame is None:
- emittedStr = self.emit.emitATTRIBUTE(ctx.name, ctx.typ, None)
- self.emit.printout(emittedStr)
- return Symbol(ctx.name, ctx.typ, CName("MCClass"))
- else:
- emittedStr = self.emit.emitVAR(myFrame.getCurrIndex(), ctx.name, ctx.typ, myFrame.getStartLabel(), myFrame.getEndLabel())
- self.emit.printout(emittedStr)
- result = Symbol(ctx.name, ctx.typ, Index(myFrame.getNewIndex()))
- return result
- ########################
- def visitId(self, ctx, o):
- for i in o.sym:
- if i.name == ctx.name:
- if o.isLeft == False:
- if isinstance(i.value.value, int):
- return self.emit.emitREADVAR(i.name, i.mtype, i.value.value, o.frame), i.mtype
- return self.emit.emitGETSTATIC(i.value.value + '/' + i.name, i.mtype, o.frame), i.mtype
- else:
- if isinstance(i.value.value, int):
- return self.emit.emitWRITEVAR(i.name, i.mtype, i.value.value, o.frame), i.mtype
- return self.emit.emitPUTSTATIC(i.value.value + '/' + i.name, i.mtype, o.frame), i.mtype
- ###############################
- def visitAssign(self, ctx, o):
- left_access = Access(o.frame, o.sym, True)
- right_access = Access(o.frame, o.sym, False)
- rhs = ctx.rhs.accept(self, right_access)
- lhs = ctx.lhs.accept(self, left_access)
- return self.emit.printout(rhs[0] + lhs[0])
- ####################################
- def visitIf(self,ctx,o):
- if_exprCode, if_exprType = ctx.expr.accept(self, Access(o.frame, o.sym, False))
- if ctx.estmt:
- labelElse = o.frame.getNewLabel()
- labelExit = o.frame.getNewLabel()
- self.emit.printout(if_exprCode)
- self.emit.printout(self.emit.emitIFFALSE(labelElse, o.frame))
- ctx.tstmt.accept(self, o)
- self.emit.printout(self.emit.emitGOTO(labelExit, o.frame))
- self.emit.printout(self.emit.emitLABEL(labelElse, o.frame))
- ctx.estmt.accept(self, o)
- self.emit.printout(self.emit.emitLABEL(labelExit, o.frame))
- else:
- labelExit = o.frame.getNewLabel()
- self.emit.printout(if_exprCode)
- self.emit.printout(self.emit.emitIFFALSE(labelExit, o.frame))
- ctx.tstmt.accept(self, o)
- self.emit.printout(self.emit.emitLABEL(labelExit, o.frame))
- ##############################
- def visitWhile(self,ctx,o):
- o.frame.enterLoop()
- labelExit = o.frame.getBreakLabel()
- self.emit.printout(self.emit.emitLABEL(o.frame.getContinueLabel(), o.frame))
- if_exprCode, if_exprType = ctx.expr.accept(self, Access(o.frame, o.sym, False))
- self.emit.printout(if_exprCode)
- self.emit.printout(self.emit.emitIFFALSE(labelExit, o.frame))
- ctx.stmt.accept(self, o)
- self.emit.printout(self.emit.emitGOTO(o.frame.getContinueLabel(), o.frame))
- self.emit.printout(self.emit.emitLABEL(labelExit, o.frame))
- o.frame.exitLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement