def main(): global const_table, global_memory, memory_stack, funcs, function_memories, \ modules_addresses, class_memories, class_signatures file_loader = FileLoader("./output/out.obj") (quadruples, functions, ret_modules_address, func_memory, class_exe, class_memory, constants) = file_loader.get_data() funcs = functions function_memories = func_memory modules_addresses = ret_modules_address class_memories = class_memory class_signatures = class_exe const_table = constants global_memory = MemoryManager().request_globalmemory( class_memories['program']) main_memory = MemoryManager().request_localmemory(class_memories['main']) memory_stack.push(main_memory) while True: if quadruples[quad_pointer][0] == "END": print("Process terminated") break process_quad(quadruples[quad_pointer])
def call_return(self, func, tp): key = MemoryManager().request_address('temp', tp) self.t_counter += 1 self.quadruples.append(['=', func, None, key]) self.counter += 1 self.types.push(tp) return str(key)
def solve(self): op_der = self.operands.pop() op_iz = self.operands.pop() operator = self.operators.pop() tp_der = self.types.pop() tp_iz = self.types.pop() if operator != '=': try: tp_res = self.cube[tp_iz][tp_der][operator] except: raise TypeError("Invalid operand types: " + tp_iz + " " + operator + " " + tp_der) key = MemoryManager().request_address('temp', tp_res) self.t_counter += 1 self.quadruples.append([operator, op_iz, op_der, key]) self.types.push(tp_res) self.addOperand(key) self.counter += 1 else: if tp_der == tp_iz: self.quadruples.append([operator, op_der, None, op_iz]) self.counter += 1 else: raise TypeError("Assignation types do not match ", op_der, op_iz, operator)
def insert_cte(self, val, tp): if val in self.table: return self.table[val]['virtual_address'] else: mem = MemoryManager().request_address('const', tp) self.table[val] = {'virtual_address': mem} return mem
def set_dim_mul(self, op, r): key = MemoryManager().request_address('temp', 'int') self.t_counter += 1 self.quadruples.append(['*', op, r, key]) self.counter += 1 return key
def set_dim_sum(self, r_op): l_op = self.operands.pop() self.types.pop() key = MemoryManager().request_address('temp', 'int') self.t_counter += 1 self.quadruples.append(['+', l_op, r_op, key]) self.counter += 1 self.operands.push(key) self.types.push('int')
def final_arr(self, va, tp, dims): if self.dim_counter != len(dims): raise IndexError( "Array dimensions do not match with established " + str(len(dims)) + " dimensions") operand = self.operands.pop() self.types.pop() key = MemoryManager().request_address('pointers', 'int') self.t_counter += 1 self.quadruples.append(['+', operand, va, key]) # Added pointer key self.operands.push("(" + str(key) + ")") self.types.push(tp) self.counter += 1 self.dim_counter = 0
def process_quad(param_quad): global quad_pointer, modules_addresses, call_stack, idle_memory, object_subroutines, \ class_type, class_address, class_vars, idle_object, class_vars_stack quad = param_quad.copy() for i in range(1, 4): if type(quad[i]) == str and "(" in quad[i]: quad[i] = process_object_attribute( int(quad[i].replace("(", "").replace(")", ""))) if quad[0] == "goto": quad_pointer = quad[3] - 1 elif quad[0] == "+": izq = process_object_attribute(quad[1]) der = process_object_attribute(quad[2]) if type(der) == str and type(izq) == str: result = izq.strip('"') + der.strip('"') elif type(izq) == str: result = izq.strip('"') + str(der) elif type(der) == str: result = str(izq) + der.strip('"') else: result = izq + der store_value(result, quad[3]) quad_pointer += 1 elif quad[0] == "-": izq = process_object_attribute(quad[1]) der = process_object_attribute(quad[2]) result = izq - der store_value(result, quad[3]) quad_pointer += 1 elif quad[0] == "*": izq = process_object_attribute(quad[1]) der = process_object_attribute(quad[2]) result = izq * der store_value(result, quad[3]) quad_pointer += 1 elif quad[0] == "/": izq = process_object_attribute(quad[1]) der = process_object_attribute(quad[2]) if der == 0: print("[ERROR] Can not divide by 0") sys.exit() result = izq / der store_value(result, quad[3]) quad_pointer += 1 elif quad[0] == "=": value = process_object_attribute(quad[1]) store_value(value, quad[3]) quad_pointer += 1 elif quad[0] == "print": text = process_object_attribute(quad[3]) print(text) quad_pointer += 1 elif quad[0] == ">": izq = process_object_attribute(quad[1]) der = process_object_attribute(quad[2]) store_value(izq > der, quad[3]) quad_pointer += 1 elif quad[0] == "<": izq = process_object_attribute(quad[1]) der = process_object_attribute(quad[2]) store_value(izq < der, quad[3]) quad_pointer += 1 elif quad[0] == "==": izq = process_object_attribute(quad[1]) der = process_object_attribute(quad[2]) store_value(izq == der, quad[3]) quad_pointer += 1 elif quad[0] == "!=": izq = process_object_attribute(quad[1]) der = process_object_attribute(quad[2]) store_value(izq != der, quad[3]) quad_pointer += 1 elif quad[0] == "and": izq = process_object_attribute(quad[1]) der = process_object_attribute(quad[2]) store_value(izq and der, quad[3]) quad_pointer += 1 elif quad[0] == "or": izq = process_object_attribute(quad[1]) der = process_object_attribute(quad[2]) store_value(izq or der, quad[3]) quad_pointer += 1 elif quad[0] == "gotoF": var = process_object_attribute(quad[1]) if var: quad_pointer += 1 else: quad_pointer = quad[3] - 1 elif quad[0] == "ERA": func = class_type.top() + '.' + quad[1] idle_memory = MemoryManager().request_localmemory( function_memories[func]) if funcs[func]['type'] != 'void': call_stack.push(quad[1]) quad_pointer += 1 elif quad[0] == "PARAM": value = process_object_attribute(quad[1]) store_param(value, quad[3]) quad_pointer += 1 elif quad[0] == "GOSUB": if idle_object: memory_stack.push(idle_object) idle_object = None memory_stack.push(idle_memory) idle_memory = None quad_pointer_stack.push(quad_pointer) quad_pointer = funcs[class_type.top() + '.' + quad[3]]['position'] - 1 elif quad[0] == "ENDFUNC": memory_stack.pop() quad_pointer = quad_pointer_stack.pop() quad_pointer += 1 elif quad[0] == "VER": index = process_object_attribute(quad[1]) lim_inf = process_object_attribute(quad[2]) lim_sup = process_object_attribute(quad[3]) if index < lim_inf or index > lim_sup: print("[ERROR] Index out of range ", index, lim_inf, lim_sup) sys.exit() quad_pointer += 1 elif quad[0] == "RETURN": value = process_object_attribute(quad[3]) fn_id = call_stack.pop() address = modules_addresses[class_type.top() + '.' + fn_id] store_value(value, address) quad_pointer += 1 elif quad[0] == "READ": address = quad[3] value = input() try: if quad[1] == 'string': value = str(value) elif quad[1] == 'int': value = int(value) elif quad[1] == 'float': value = float(value) elif quad[1] == 'bool': value = bool(value) else: print("[ERROR] Variable type not recognized on READ") sys.exit() except ValueError: print("[ERROR] Read input type does not match var type " + quad[1]) sys.exit() store_value(value, address) quad_pointer += 1 elif quad[0] == "OBJ": class_name = quad[1] address = quad[3] memory = MemoryManager().request_classmemory( class_memories[class_name]) memory_stack.push(memory) class_address.push(address) class_type.push(class_name) object_subroutines = class_signatures[class_name].copy() quad_pointer += 1 elif quad[0] == "OBJSUB": if object_subroutines: quad_pointer_stack.push(quad_pointer) quad_pointer = object_subroutines.pop(0) - 1 else: generate_object(memory_stack.pop(), class_address.top()) class_address.pop() class_type.pop() quad_pointer += 1 elif quad[0] == "ENDCLS": quad_pointer = quad_pointer_stack.pop() elif quad[0] == "MEM": current_mem = memory_stack.top() class_type.push(quad[1]) class_address.push(quad[3]) if class_address.top() in current_mem: class_vars = current_mem[class_address.top()] else: class_vars_stack.push(class_vars) class_vars = class_vars[class_address.top()] idle_object = class_vars.copy() quad_pointer += 1 elif quad[0] == "ENDCLL": if class_vars_stack.top(): class_vars = class_vars_stack.pop() else: class_vars = {} class_type.pop() class_address.pop() memory_stack.pop() quad_pointer += 1 else: raise RuntimeError("Unimplemented Action Code: " + quad[0])
def reset_t_counter(self): self.t_counter = 0 MemoryManager().reset_module_context()
def requestArrayAddress(self, scope, type_var, size): virtual_address = MemoryManager().request_address_block( scope, type_var, size) return virtual_address
def requestAddress(self, scope, type_var): virtual_address = MemoryManager().request_address(scope, type_var) return virtual_address
def p_class_1Aux(p): '''class_1Aux : class_2 class_3 OPEN_BRACKET class_4''' class_temp = MemoryManager().get_class_temps() semantics.store_class_temporals() # Pushing extension value, class temps and implementation upwards p[0] = p[1] + [class_temp] + [p[2]]
def store_global_vars(self, id): self.directory[id]['size'] = MemoryManager().get_global_counter() clean_vartable = self.clean_export() self.var_table = None self.params = {} return clean_vartable
def delete_var_table(self, id): self.directory[id]['size'] = MemoryManager().get_module_counter() clean_vartable = self.clean_export() self.var_table = None self.params = {} return clean_vartable
def store_param(self, type_atomic, id): if not id in self.params: address = MemoryManager().request_address('local', type_atomic) self.params[id] = {'type': type_atomic, "virtual_address": address} else: raise KeyError("Duplicate parameter: " + id)