def end_while(self): self.program_block.append( make_command(Commands.JP, self.semantic_stack[-3])) self.program_block[self.semantic_stack[-1]] = make_command( Commands.JPF, Unit(addressing_mode='#', value=self.semantic_stack[-2], type=Value_type.POINTER), Unit(addressing_mode='#', value=len(self.program_block), type=Value_type.POINTER)) self.semantic_stack.pop(3)
def jp_fun(self): row = self.symbol_table.table[self.semantic_stack[-2]] self.program_block.append( make_command( Commands.JP, Unit(addressing_mode='', value=row.save_space, type=row.type))) if row.value != "main": self.program_block[self.semantic_stack[-1]] = make_command( Commands.JP, Unit(addressing_mode='#', value=len(self.program_block), type=Value_type.POINTER)) self.semantic_stack.pop(2)
def return_int_fun(self): row = self.symbol_table.table[self.semantic_stack[-3]] self.program_block.append( make_command( Commands.ASSIGN, self.semantic_stack[-1], Unit(addressing_mode='', value=row.return_address, type=row.type), )) self.program_block.append( make_command( Commands.JP, Unit(addressing_mode='', value=row.save_space, type=row.type))) self.semantic_stack.pop()
def allocate_array(self): row = self.symbol_table.table[self.semantic_stack.top()] array_begin = self.memory_manager.get_temp(row.pointed_type, count=row.size) self.program_block.append( make_command( Commands.ASSIGN, Unit(addressing_mode='#', value=array_begin, type=Value_type.POINTER), Unit(addressing_mode='', value=row.address, type=Value_type.POINTER), )) self.semantic_stack.pop()
def jp(self): self.program_block[self.semantic_stack[-1]] = make_command( Commands.JP, Unit(addressing_mode='#', value=len(self.program_block), type=Value_type.POINTER)) self.semantic_stack.pop(1)
def return_void_fun(self): row = self.symbol_table.table[self.semantic_stack[-2]] self.program_block.append( make_command( Commands.JP, Unit(addressing_mode='', value=row.save_space, type=row.type), ))
def call_fun(self): method_row_index = self.semantic_stack[-1] self.semantic_stack.pop() row = self.symbol_table.table[method_row_index] self.program_block.append( make_command( Commands.ASSIGN, Unit(addressing_mode='#', value=len(self.program_block) + 2, type=Value_type.POINTER), Unit(addressing_mode='', value=row.save_space, type=Value_type.POINTER))) self.program_block.append( make_command( Commands.JP, Unit(addressing_mode='#', value=row.address, type=Value_type.POINTER))) if row.type != Value_type.VOID: tmp_var = self.memory_manager.get_variable(row.type) self.program_block.append( make_command( Commands.ASSIGN, Unit(addressing_mode='', value=row.return_address, type=row.type), Unit(addressing_mode='#', value=tmp_var, type=row.type))) self.semantic_stack.push( Unit(addressing_mode='', value=tmp_var, type=row.type)) else: self.semantic_stack.push( Unit(addressing_mode='', value=row.return_address, type=row.type))
def execute(self): tmp = Unit(addressing_mode='', value=self.memory_manager.get_temp(Value_type.INT), type=Value_type.INT) self.program_block.append( make_command(self.semantic_stack[-2], self.semantic_stack[-3], self.semantic_stack[-1], tmp)) self.semantic_stack.pop(3) self.semantic_stack.push(tmp)
def jpf_save(self): i = len(self.program_block) self.program_block.append(None) self.program_block[self.semantic_stack[-1]] = make_command( Commands.JPF, self.semantic_stack[-2], Unit(addressing_mode='#', value=len(self.program_block), type=Value_type.POINTER)) self.semantic_stack.pop(2) self.semantic_stack.push(i)
def set_arg(self): val = self.semantic_stack[-1] arg_index = self.semantic_stack[-2] - 1 method_row_index = self.semantic_stack[-3] method_row = self.symbol_table.table[method_row_index] arg_name, arg_type, arg_address = method_row.arguments[arg_index] self.program_block.append( make_command( Commands.ASSIGN, val, Unit(addressing_mode='', value=arg_address, type=arg_type))) self.semantic_stack.pop()
def push_variable(self, token): row = self.symbol_table.table[int(token[1])] if row.type is None: self.error_handler.semantic_error( "%s is not defined in this scope" % (row.value), None) raise Semantic_error("") if row.id_type != ID_type.VAR: self.error_handler.semantic_error("not a variable", None) raise Semantic_error("") self.semantic_stack.push( Unit(addressing_mode='', type=row.type, value=token[1]))
def push_array(self): arr_indx_adrs = self.memory_manager.get_temp(Value_type.POINTER) var_adrs = self.memory_manager.get_temp(Value_type.POINTER) arr_index = self.semantic_stack[-1] row = self.symbol_table.table[self.semantic_stack[-2]] self.program_block.append( make_command( Commands.MULT, arr_index, Unit(addressing_mode='#', value=row.pointed_type.size, type=Value_type.INT), Unit(addressing_mode='', value=arr_indx_adrs, type=Value_type.POINTER))) self.program_block.append( make_command( Commands.ADD, Unit(addressing_mode='', value=row.address, type=row.type), Unit(addressing_mode='', value=arr_indx_adrs, type=Value_type.POINTER), Unit(addressing_mode='', value=var_adrs, type=Value_type.POINTER))) self.semantic_stack.pop(2) self.semantic_stack.push( Unit(addressing_mode='@', value=var_adrs, type=row.pointed_type))
def push_immediate(self, token): imidiate = Unit(value=token[1], addressing_mode="#", type=Value_type.INT) self.semantic_stack.push(imidiate)