import re, util class Keys: MACRO_DEFINITION = "MACRO: (.*)" END = "END" INSTRUCTION = "^([A-Za-z]+)(?:\s)?(.*)?$" LABEL = "^([A-Za-z0-9]+):" @classmethod def find(cls,lines,k): pattern = getattr(cls,k.upper()) pattern = pattern if pattern is not None else "" return [(x,re.match(pattern,lines[x])) for x in util.matchinglines(lines,pattern)] def asm(i): macros = dict() lines = [l.strip() for l in i.split("\n") if l.strip()] ignore_list = [] ret = [] for index, m in Keys.find(lines,"macro_definition"): m2 = util.walkup(lines,index,Keys.END) # print(index,m2) macros[m.group(1)]="\n".join(lines[index+1:m2]) ignore_list.append([index,m2]) use = True current_ignore = [] for index in range(len(lines)): if current_ignore and (index-1)==current_ignore[1]: use = True elif index in [x[0] for x in ignore_list] and use: use = False current_ignore = [x for x in ignore_list if x[0]==index][0] if use: m = re.search(Keys.INSTRUCTION,lines[index]) if m is not None and m.group(1) in macros: ret.extend(macros[m.group(1)].format(*m.group(2).split(",")).split("\n")) elif m is not None: ret.append(m.group(0)) elif re.search(Keys.LABEL,lines[index]) is not None: ret.append(lines[index]) else: print("Syntax error line {!s}".format(index+1)) return "\n".join(ret)