コード例 #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))