Exemplo n.º 1
0
 def do(self, parser):
     function_symbol_address = parser.semantic_stack[-1]
     function_data_type = parser.scanner.get_token_by_address(
         function_symbol_address).data_type
     activity_record_address = parser.get_temp()
     return_address_address = parser.get_temp()
     parser.program.add_inst(Mnemonic.ASSIGN, function_symbol_address,
                             activity_record_address)
     parser.program.add_inst(Mnemonic.ADD, activity_record_address,
                             immval(1), return_address_address)
     parser.program.add_inst(Mnemonic.ASSIGN, immval(parser.program.pc + 3),
                             indval(return_address_address))
     start_pc = parser.get_temp()
     parser.program.add_inst(Mnemonic.ASSIGN,
                             indval(activity_record_address), start_pc)
     parser.program.add_inst(Mnemonic.JUMP, indval(start_pc))
     if function_data_type == DataType.INTEGER:
         return_value = parser.get_temp()
         parser.program.add_pop(return_value)
         parser.semantic_stack[-1] = return_value
     else:
         parser.semantic_stack[-1] = 'None'
     if parser.argument_stack[-1]:
         raise SemanticError('Too few arguments passed', parser.scanner)
     parser.argument_stack.pop()
Exemplo n.º 2
0
 def do(self, parser):
     main_symbol_address = parser.scanner.get_symbol_address('main')
     activity_record_address = parser.get_temp()
     return_address_address = parser.get_temp()
     parser.program.add_inst(Mnemonic.ASSIGN, main_symbol_address,
                             activity_record_address)
     parser.program.add_inst(Mnemonic.ADD, activity_record_address,
                             immval(1), return_address_address)
     parser.program.add_inst(Mnemonic.ASSIGN, immval(parser.program.pc + 3),
                             indval(return_address_address))
     start_pc = parser.get_temp()
     parser.program.add_inst(Mnemonic.ASSIGN,
                             indval(activity_record_address), start_pc)
     parser.program.add_inst(Mnemonic.JUMP, indval(start_pc))
     parser.program.add_nop()
Exemplo n.º 3
0
    def do(self, parser):

        # # FunctionSave
        activity_record_address = parser.scanner.malloc(2)
        start_pc_address = activity_record_address
        return_address_address = activity_record_address + 1
        parser.semantic_stack.append(return_address_address)
        # write the record address to the function symbol memory
        parser.program.add_inst(Mnemonic.ASSIGN,
                                immval(activity_record_address), 0)
        # write the start address to the first word of activity record
        parser.program.add_inst(Mnemonic.ASSIGN, immval(parser.program.pc + 2),
                                start_pc_address)
        parser.semantic_stack.append(parser.program.pc)
        parser.program.add_fake_inst(
        )  # skip running the function on the first pass

        # # PullID
        # # Assembly
        temporary = parser.get_temp()
        parser.program.add_pop(temporary)
        parser.program.add_inst(Mnemonic.PRINT, temporary)

        # # Function
        parser.program.edit_inst(parser.semantic_stack.pop(), Mnemonic.JUMP,
                                 parser.program.pc + 1)
        parser.program.add_inst(Mnemonic.JUMP,
                                indval(parser.semantic_stack.pop()))
Exemplo n.º 4
0
 def do(self, parser):
     tmp = parser.get_temp()
     if parser.scanner.get_token_by_address(
             parser.semantic_stack[-2]
     ) and parser.scanner.get_token_by_address(
             parser.semantic_stack[-2]
     ).declaration_type == DeclarationType.FUNCTION:
         raise SemanticError('Subscription from function is not allowed',
                             parser.scanner)
     parser.program.add_inst(Mnemonic.ADD, parser.semantic_stack.pop(),
                             parser.semantic_stack.pop(), tmp)
     parser.semantic_stack += [indval(tmp)]
Exemplo n.º 5
0
 def do(self, parser):
     if parser.scanner.get_token_by_address(
             parser.semantic_stack[-1]
     ) and parser.scanner.get_token_by_address(
             parser.semantic_stack[-1]
     ).declaration_type == DeclarationType.ARRAY:
         raise SemanticError('Assignment from array is not allowed',
                             parser.scanner)
     if parser.scanner.get_token_by_address(
             parser.semantic_stack[-1]
     ) and parser.scanner.get_token_by_address(
             parser.semantic_stack[-1]
     ).declaration_type == DeclarationType.FUNCTION:
         raise SemanticError('Assignment from function is not allowed',
                             parser.scanner)
     tmp = parser.get_temp()
     parser.program.add_inst(Mnemonic.ADD, parser.semantic_stack[-3],
                             parser.semantic_stack[-2], tmp)
     parser.program.add_inst(Mnemonic.ASSIGN, parser.semantic_stack.pop(),
                             indval(tmp))
     parser.semantic_stack.pop()
     parser.semantic_stack.pop()
     parser.semantic_stack += [indval(tmp)]
Exemplo n.º 6
0
 def do(self, parser):
     parser.program.edit_inst(parser.semantic_stack.pop(), Mnemonic.JUMP,
                              parser.program.pc + 1)
     parser.program.add_inst(Mnemonic.JUMP,
                             indval(parser.return_stack.pop()))
     if parser.function_stack[
             -1].has_return == False and parser.function_stack[
                 -1].data_type != DataType.VOID:
         raise SemanticError('Missing return statement inside the function',
                             parser.scanner)
     if len(parser.function_stack) == 1 and parser.function_stack[
             -1].lexeme == 'main' and parser.function_stack[
                 -1].data_type != DataType.VOID:
         raise SemanticError('Invalid prototype for function main',
                             parser.scanner)
     parser.function_stack.pop()
Exemplo n.º 7
0
 def add_pop(self, arg):
     self.add_inst(Mnemonic.SUBTRACT, self.sp, immval(1), self.sp)
     self.add_inst(Mnemonic.ASSIGN, indval(self.sp), arg)
Exemplo n.º 8
0
 def add_push(self, arg):
     self.add_inst(Mnemonic.ASSIGN, arg, indval(self.sp))
     self.add_inst(Mnemonic.ADD, self.sp, immval(1), self.sp)
Exemplo n.º 9
0
 def do(self, parser):
     parser.function_stack[-1].has_return = True
     parser.program.add_inst(Mnemonic.JUMP, indval(parser.return_stack[-1]))