示例#1
0
 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))
示例#2
0
 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
示例#3
0
    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
示例#4
0
    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()
示例#5
0
 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))
示例#6
0
 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}'))
示例#7
0
 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))
示例#8
0
 def visitIdentifier(self, ctx: gmarParser.IdentifierContext):
     index = self.current_method.get_index_of(ctx.getText())
     self.current_method.add_instruction(Instruction(ILOAD, index))
示例#9
0
 def visitNotExpression(self, ctx: gmarParser.NotExpressionContext):
     self.visit(ctx.getChild(1))
     self.current_method.add_instruction(Instruction(INOT, None))
示例#10
0
 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))
示例#11
0
 def visitReturnStatement(self, ctx: gmarParser.ReturnStatementContext):
     self.visit(ctx.getChild(1))
     self.current_method.add_instruction(Instruction(IRETURN, None))
示例#12
0
 def visitPrintStatement(self, ctx: gmarParser.PrintStatementContext):
     self.visit(ctx.getChild(2))
     self.current_method.add_instruction(Instruction(PRINT, None))
示例#13
0
 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))