def ver(self, quad): '''Verifies if index is inbounds''' func = utils.func_stack[len(utils.func_stack) - 1] var_name = quad[4] + '-' + str(memory.get(quad[2])) if (var_name not in memory.func_dir[func]['vars'] and var_name not in memory.func_dir['global']['vars']): err('Out of bounds!', var_name)
def gen_era(id): '''Expands activation register''' if id in func_dir: utils.cur_stack = id gen_quad('ERA', '', '', id) else: err('Function does not exist!', id)
def eval(self, op, quad): '''Assings operation to quadruple''' if op in dir(self): # Checks that function exists in class method = getattr(self, op) return method(quad) # Executes corresponding method else: err('Operation does not exist!', op)
def era(func_name): stack_frame = {} vars = func_dir[func_name]['vars'] for v in vars: stack_frame[vars[v]['addr']] = vars[v]['value'] if (len(local_stack) < MAX_STACK_CALLS): local_stack.append(stack_frame) else: err('Stack frame calls overflow!', func_name)
def insert_temp(type, temp_name): func_table = symbol_table.func_table context = utils.context addr = func_table[context][type][2] if (addr >= func_table[context][type][1]): temp = [temp_name, addr] func_table[context][type][0].append(temp) func_table[context][type][2] = addr - 1 func_dir[context]['temps'][temp_name] = {'type': type, 'addr': addr} return addr else: err('Stack overflow!', utils.context)
def insert_cte(type, val): '''Inserts constant in constan directory''' if val in cte_dict: #if value is already stored, return addr return cte_dict[val] addr = cte_table[type][1] if (addr <= cte_table[type][2]): cte_table[type][0].append([val, addr]) cte_dir.append({'value': val, 'type': type, 'addr': addr}) cte_dict[val] = addr cte_table[type][1] += 1 return addr else: err('Constant segment overflow!', val)
def fill_params(params): '''Receives a list of all params passed to a function call Checks if they are compatible with what is stored in func dir Then creates for each param a quadruple that takes value to called func''' func = utils.cur_stack if (len(params) != len(func_dir[func]['params'])): err('Quantity of params not correct!', func) for i, param in enumerate(params): func_param = func_dir[func]['params'][i] if param.type == func_param[1]: addr = func_dir[func]['vars'][func_param[0]]['addr'] gen_quad_addr('PARAM', param.addr, '', addr) gen_quad_name('PARAM', param.value, '', func_param[0]) else: err('Param type mismatch!', func)
def insert_func(func_name, type='void', pos=0): '''Inserts function in function directory''' if func_name in func_dir.keys(): # Validates that it doesn exits err('Function already declared!', func_name) else: func_table[func_name] = { # Creates register that assigns directions 'int': [[], l_limits.INT_MIN, l_limits.INT_MAX], 'float': [[], l_limits.FLOAT_MIN, l_limits.FLOAT_MAX], 'string': [[], l_limits.STRING_MIN, l_limits.STRING_MAX], 'bool': [[], l_limits.BOOL_MIN, l_limits.BOOL_MAX] } func_dir[func_name] = { # Creates register for execution 'type': type, 'pos': pos, 'vars': {}, 'temps': {}, 'params': [], 'end': 0 }
def insert_local_var(func_name, var_name, type, value=None, exp_type=None): '''Inserts localvariable for a function''' if (exp_type == None): exp_type = type if (type != exp_type): type_mismatch(type, '=', exp_type) if var_name in func_dir[func_name]['vars'].keys(): err('Variable with name ' + var_name + ' already declared!', func_name) elif var_name in func_dir['global']['vars'].keys(): err('Variable with name ' + var_name + ' already declared globally!', func_name) else: if (value == None): value = defaults[type] if (func_table[func_name][type][1] <= func_table[func_name][type][2]): func_table[func_name][type][0].append( [var_name, value, func_table[func_name][type][1]]) addr = func_table[func_name][type][1] func_dir[func_name]['vars'][var_name] = { 'type': type, 'addr': addr, 'value': value } func_table[func_name][type][1] += 1 return Var(type, value, addr) else: err('Stack overflow!', func_name)
def insert_global_var(var_name, type, value=None, exp_type=None): '''Inserts global variable in func dir''' if (exp_type == None): exp_type = type if (type != exp_type): type_mismatch(type, '=', exp_type) if var_name in func_dir['global']['vars'].keys(): err('Variable already declared globally!', var_name) else: if (value == None): value = defaults[type] if (g_table[type][1] <= g_table[type][2]): g_table[type][0].append([var_name, value, g_table[type][1]]) addr = g_table[type][1] func_dir['global']['vars'][var_name] = { 'type': type, 'addr': addr, 'value': value } g_table[type][1] += 1 return Var(type, value, addr) else: err('Data segment overflow!', var_name)
def t_error(t): global success success = False err('Illegal character!', t.value[0])
def p_error(p): global success success = False err('Syntax error!', p.value)