def visitDoWhileStatement(self, ctx: gmarParser.DoWhileStatementContext): beginbody = self.current_method.get_index() self.visit(ctx.getChild(1)) self.visit(ctx.getChild(4)) iffalse = self.current_method.get_index() self.current_method.add_instruction(Instruction(IF_FALSE, iffalse + 2)) self.current_method.add_instruction(Instruction(GOTO, beginbody))
def visitGtExpression(self, ctx: gmarParser.GtExpressionContext): num_cildren = ctx.getChildCount() self.visit(ctx.getChild(0)) i = 2 while i < num_cildren: self.visit(ctx.getChild(i)) self.current_method.add_instruction(Instruction(ILE, None)) self.current_method.add_instruction(Instruction(INOT, None)) i += 2
def visitWhileStatement(self, ctx: gmarParser.WhileStatementContext): whilelabel = self.current_method.get_index() self.visit(ctx.getChild(2)) iffalse = self.current_method.get_index() self.current_method.add_instruction(Instruction(IF_FALSE, None)) self.visit(ctx.getChild(4)) instr = self.current_method.get_instruction(iffalse) instr.argument = self.current_method.get_index() + 1 gotolabel = self.current_method.get_index() self.current_method.add_instruction(Instruction(GOTO, None)) instr = self.current_method.get_instruction(gotolabel) instr.argument = whilelabel
def visitIfStatement(self, ctx: gmarParser.IfStatementContext): self.visit(ctx.getChild(2)) iflabel = self.current_method.get_index() self.current_method.add_instruction(Instruction(IF_FALSE, None)) self.visit(ctx.getChild(4)) instr = self.current_method.get_instruction(iflabel) instr.argument = self.current_method.get_index() + 1 gotolabel = self.current_method.get_index() self.current_method.add_instruction(Instruction(GOTO, None)) if ctx.getChild(6) is not None: self.visit(ctx.getChild(6)) instr = self.current_method.get_instruction(gotolabel) instr.argument = self.current_method.get_index()
def visitMainClass(self, ctx: gmarParser.MainClassContext): self.current_class = ctx.getChild(1).getText() self.class_file = ClassFile() self.symbol_table.enter_scope() self.visit(ctx.getChild(3)) self.symbol_table.exit_scope() self.current_method.add_instruction(Instruction(STOP, None))
def visitMethodCallExpression(self, ctx: gmarParser.MethodCallExpressionContext): numchi = ctx.getChildCount() invclassname = '' if ctx.getChild(0).getChild(0).getText() == 'this': invclassname = self.current_class else: # print(ctx.getText()) invclassname = str(ctx.getChild(0).getChild(1).getChild(0)) paramnum = ctx.getChild(numchi - 1).getChildCount() - 3 i = 0 while i < paramnum: self.visit(ctx.getChild(numchi - 1).getChild(i + 2)) i += 2 imn = str(ctx.getChild(2).getChild(0).getChild(0)) self.current_method.add_instruction( Instruction(INVOKEVIRTUAL, f'{invclassname}.{imn}'))
def visitIntegral(self, ctx: gmarParser.IntegralContext): if not self.field_declaration: value = int(str(ctx.getChild(0))) self.current_method.add_instruction(Instruction(ICONST, value))
def visitIdentifier(self, ctx: gmarParser.IdentifierContext): index = self.current_method.get_index_of(ctx.getText()) self.current_method.add_instruction(Instruction(ILOAD, index))
def visitNotExpression(self, ctx: gmarParser.NotExpressionContext): self.visit(ctx.getChild(1)) self.current_method.add_instruction(Instruction(INOT, None))
def visitParameter(self, ctx: gmarParser.ParameterContext): num_children = ctx.getChildCount() vname = str(ctx.getChild(1).getChild(0)) index = self.current_method.get_index_of(vname) self.current_method.add_instruction(Instruction(ISTORE, index))
def visitReturnStatement(self, ctx: gmarParser.ReturnStatementContext): self.visit(ctx.getChild(1)) self.current_method.add_instruction(Instruction(IRETURN, None))
def visitPrintStatement(self, ctx: gmarParser.PrintStatementContext): self.visit(ctx.getChild(2)) self.current_method.add_instruction(Instruction(PRINT, None))
def visitAssignStatement(self, ctx: gmarParser.AssignStatementContext): lhs = ctx.getChild(0).getText() self.visit(ctx.getChild(2)) index = self.current_method.get_index_of(lhs) self.current_method.add_instruction(Instruction(ISTORE, index))