def __parse_application_target(self, target_name, it): link_with = variables.Variable() library_dirs = variables.Variable() common_parameters = CommonTargetParameters( os.path.dirname(self.filename), self.name, target_name) cxx_parameters = CxxParameters() while True: token = it.next() if token == lexer.Token.LITERAL: if self.__try_parse_target_common_parameters( common_parameters, token, it): pass elif self.__try_parse_cxx_parameters(cxx_parameters, token, it): pass elif token.content == "link_with": link_with = self.__parse_list(it) elif token.content == "library_dirs": library_dirs = self.__parse_list(it) else: ui.parse_error(token) elif token == lexer.Token.NEWLINE: break else: ui.parse_error(token) target = targets.Application(common_parameters, cxx_parameters, link_with, library_dirs) targets.add_target(target)
def divide(variable_x, variable_y, program, modulo=False): x = variable_x.register y = variable_y.register # results quotient = 'C' remainder = 'A' # temporary registers helper_d = 'D' helper_e = 'E' if modulo: quotient, remainder = remainder, quotient out_code = [ f'COPY {remainder} {x} #division', f'JZERO {y} {program.line_no + 24} #zero_divison', f'COPY {helper_d} {y}', f'COPY {quotient} {helper_d}', f'SUB {quotient} {remainder}', f'JZERO {quotient} {program.line_no + 7}', f'JUMP {program.line_no + 9}', f'ADD {helper_d} {helper_d}', f'JUMP {program.line_no + 3}', f'SUB {quotient} {quotient}', f'COPY {helper_e} {helper_d}', f'SUB {helper_e} {remainder}', f'JZERO {helper_e} {program.line_no + 16}', f'ADD {quotient} {quotient}', f'HALF {helper_d}', f'JUMP {program.line_no + 20}', f'ADD {quotient} {quotient}', f'INC {quotient}', f'SUB {remainder} {helper_d}', f'HALF {helper_d}', f'COPY {helper_e} {y}', f'SUB {helper_e} {helper_d}', f'JZERO {helper_e} {program.line_no + 10}', f'JUMP {program.line_no + 26}', f'SUB {remainder} {remainder}', f'SUB {quotient} {quotient} #division end', ] [program.code.append(command) for command in out_code] program.line_no += len(out_code) # return proper result based on modulo flag if modulo: return variables.Variable('helper', remainder, -1) else: return variables.Variable('helper', quotient, -1)
def _parse_configuration_export(self, it): ret = [] token = it.next() if token == lexer.Token.OPEN_PARENTHESIS: while True: token = it.next() if token in [lexer.Token.LITERAL, lexer.Token.VARIABLE]: value = self._token_to_variable(token) token = it.next() if token == lexer.Token.COLON: token = it.next() if token == lexer.Token.VARIABLE: variable = variables.Variable( self.name, token.content, value) ret.append(variable) else: ui.parse_error(token, msg="expected variable") else: ui.parse_error(token, msg="expected colon") elif token == lexer.Token.CLOSE_PARENTHESIS: break else: ui.parse_error(token) else: ui.parse_error(token) ui.debug("colon list: " + str(ret)) return ret
def decrement(variable_x, variable_y, program): x = variable_x.register out_code = [f'DEC {x}' for _ in range(variable_y)] [program.code.append(command) for command in out_code] program.line_no += len(out_code) return variables.Variable('helper', 'G', -1)
def multiply(variable_x, variable_y, program): x = variable_x.register y = variable_y.register result_register = 'D' tmp_register = 'E' out_code = [ f'COPY A {y} #checking which is bigger', f'SUB A {x}', f'JZERO A {program.line_no + 13}', f'COPY A {y} #multiplying if x is smaller', f'COPY {tmp_register} {x}', f'SUB {result_register} {result_register}', f'JZERO {tmp_register} {program.line_no + 23}', f'JODD {tmp_register} {program.line_no + 9}', f'JUMP {program.line_no + 10}', f'ADD {result_register} A', f'ADD A A', f'HALF {tmp_register}', f'JUMP {program.line_no + 6} # multiplying if x end', f'COPY A {x} #multiplying if y is smaller', f'COPY {tmp_register} {y}', f'SUB {result_register} {result_register}', f'JZERO {tmp_register} {program.line_no + 23}', f'JODD {tmp_register} {program.line_no + 19}', f'JUMP {program.line_no + 20}', f'ADD {result_register} A', f'ADD A A', f'HALF {tmp_register}', f'JUMP {program.line_no + 16} #multiplying if y end' ] [program.code.append(command) for command in out_code] program.line_no += len(out_code) return variables.Variable('helper', 'D', -1)
def generate_code(self, program): logging.info("Generating code for pidentifier") program.variables[self.value] = variables.Variable( self.value, None, program.used_memory) program.used_memory += 1 variable = program.get_variable(self.value) return variable
def add(variable_x, variable_y, program): x = variable_x.register y = variable_y.register # optimazition purposes but didnt want to swap xD out_code = [f'ADD {x} {y}'] [program.code.append(command) for command in out_code] program.line_no += len(out_code) return variables.Variable('helper', 'G', -1)
def declarations(self, p): if self.analysis or p.PIDENTIFIER not in self.ignored_variables: self.variables[p.PIDENTIFIER] = variables.Variable( p.PIDENTIFIER, None, self.used_memory) self.used_memory += 1 if self.variables_use.get(p.PIDENTIFIER) is None: variable_use = analysis_help.VariableUse(p.PIDENTIFIER) self.variables_use[p.PIDENTIFIER] = variable_use elif self.analysis: raise exceptions.DoubledDeclarationVariable( p.PIDENTIFIER, p.lineno) return None
def bin_pow( variable_x, variable_y, program, ): x = variable_x.register out_code = [] while variable_y != 1: out_code.append(f'ADD {x} {x}') variable_y //= 2 [program.code.append(command) for command in out_code] program.line_no += len(out_code) return variables.Variable('helper', 'G', -1)
def __parse_list(self, it): ret = variables.Variable() token = it.next() if token == lexer.Token.OPEN_PARENTHESIS: while True: token = it.next() variable = self._token_to_variable(token) if variable: ret.content.append(variable) elif token == lexer.Token.CLOSE_PARENTHESIS: break else: ui.parse_error(token) else: ui.parse_error(token) return ret
def generate_code(self, program, register_name='H'): code = [] # optimization purposes if self.value < 28: for _ in range(self.value): code.append(f'INC {register_name}') else: # changing value into binary while self.value != 0: if self.value % 2 == 0: code.append(f'ADD {register_name} {register_name}') else: code.append(f'ADD {register_name} {register_name}') code.append(f'INC {register_name}') self.value //= 2 code = code [:0:-1] code.insert(0, f'SUB {register_name} {register_name} ') [program.code.append(command) for command in code] program.line_no += len(code) logging.info(f"Generating number {self.value}") return variables.Variable('accumulator_tmp', register_name, memory_localisation=-1)