def makevar(defvar): gt = Aardvark.gettype(defvar[1], Aardvark.line_num) Aardvark.variables[defvar[0]] = language.Variable(defvar[0], gt) for i in Aardvark.attributes[gt[0]]: Aardvark.variables[defvar[0] + "." + i[0]] = language.Variable( defvar[0] + "." + i[0], Aardvark.gettype(i[1](gt[1]), Aardvark.line_num))
def compile(self) -> str: to_return = "" value_in_loop = self.for_com.iterator value_in_loop.memory_cell = self.iterator_memory_cell counter = language.Variable("loop counter") counter.memory_cell = self.counter_memory_cell setup = MachineCode() # Assign with arraycells included is what causes For to break. # Assign correct start value to iterator. # FULL MANUAL setup.add_commands(set_value_to_register("H", self.for_com.start_value)) setup.add_commands(set_register_to("A", self.iterator_memory_cell)) setup.add_command("STORE H") # Give counter required value. # FULL MANUAL setup.add_commands(set_value_to_register("H", self.for_com.end_value)) setup.add_commands(set_value_to_register("G", self.for_com.start_value)) setup.add_command("SUB G H") setup.add_command("INC G") setup.add_command("INC G") setup.add_commands(set_register_to("A", self.counter_memory_cell)) setup.add_command("STORE G") condition = MachineCode() condition.add_commands(decrement(counter)) condition.add_commands(set_value_to_register("H", counter)) #condition.add_command("PUT H") block = MachineCode() block.add_commands(compile_block(self.for_com.block)) block.add_commands(decrement(value_in_loop)) block.add_command("JUMP -" + str(len(block) + len(condition) + 1)) # Jump out of the loop. condition.add_command("JZERO H +" + str(len(block) + 1)) to_return += setup.get_code() to_return += condition.get_code() to_return += block.get_code() return to_return
def p_variable_dec(p): 'variable_dec : pidentifier SEMICOLON' if IDENTIFIERS_MANAGER.identifier_exists(p[1]): print_error(p, "Redefinition of identifier " + p[1]) IDENTIFIERS_MANAGER.add_identifier(language.Variable(p[1]))
def parse_line(line, line_num, enable_return=False): global instuff, globalmaxmemory if memory_profiler.memory_usage()[0] > globalmaxmemory: error("MemoryError", line_num, line, "The program went above its maximum memory.") isfunction = re.fullmatch("[\t ]*([A-Za-z_][a-zA-Z0-9_]*[\t ]*\(.*?\)?)", line) ismethod = re.fullmatch( "[\t ]*(.+)\.([A-Za-z_][a-zA-Z0-9_]*[\t ]*\(.*?\))", line) defvar = re.fullmatch( "[\t ]*([A-Za-z_][a-zA-Z0-9_]*)[\t ]*=[\t ]*(.+?)[\t ]*", line) isif = re.fullmatch("[\t ]*if [\t ]*(.+?)[\t ]*{[\t ]*", line) iswhile = re.fullmatch("[\t ]*while [\t ]*(.+?)[\t ]*{[\t ]*", line) isforin = re.fullmatch("[\t ]*for [\t ]*(.+) [\t ]*in [\t ]*(.+)[\t ]*{", line) isinclude = re.fullmatch("[\t ]*#include [\t ]*(.+)", line) isfunctiondefinition = re.fullmatch( "[\t ]*funct [\t ]*.+\(.*\)[\t ]*{[\t ]*", line) ismaxmem = re.fullmatch("#max-memory[\t ]* (.+)", line) isreturn = re.fullmatch("[\t ]*return[\t ]* (.+?)[\t ]*", line) #print(instuff) if isforin or isfunctiondefinition or iswhile or isif and len( instuff) >= 1: instuff.append(["nothing", "nothing"]) if re.fullmatch("[\t ]*}[\t ]*", line): what = instuff[-1] instuff = instuff[:-1] print(instuff) if what[0] != "nothing": what[0](what[1], line_num) for i in instuff: i[1] += line + "\n" if len(instuff) > 0: return if isfunction: #print("PARSE FUNCTION") Aardvark.process_function(isfunction.groups()[0], line_num) elif defvar: defvar = defvar.groups() #print("Defvar is:", defvar) gt = Aardvark.gettype(defvar[1], line_num) Aardvark.variables[defvar[0]] = language.Variable(defvar[0], gt) for i in Aardvark.attributes[gt[0]]: Aardvark.variables[defvar[0] + "." + i[0]] = language.Variable( defvar[0] + "." + i[0], Aardvark.gettype(i[1](gt[1]), line_num)) elif ismethod: #print("method") ismethod = ismethod.groups() Aardvark.process_method(ismethod[0], ismethod[1], line_num) elif isif: instuff.append([ifblock, line + "\n"]) elif iswhile: instuff.append([whileblock, line + "\n"]) elif isforin: instuff.append([forinblock, line + "\n"]) elif isinclude: #print("IT IS AN #include") inculde_statement(isinclude.groups()[0], line_num) elif isfunctiondefinition: print("defining function") instuff.append([newfunction, line + "\n"]) elif ismaxmem: globalmaxmemory = float(ismaxmem.groups()[0]) elif isreturn and enable_return: return Aardvark.gettype(isreturn.groups()[0], line_num)