From 7021764d0cd6b71ac1239f7642f22fa803c318c7 Mon Sep 17 00:00:00 2001 From: khuxkm fbexl Date: Sat, 17 Nov 2018 13:56:40 -0500 Subject: [PATCH] Add code reqired to pass Macros test --- gen6502.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/gen6502.py b/gen6502.py index f3d8383..219fe05 100644 --- a/gen6502.py +++ b/gen6502.py @@ -3,6 +3,8 @@ 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()) @@ -12,9 +14,29 @@ class Keys: 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]) - print(macros) - return i + 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)