Add code reqired to pass Macros test
This commit is contained in:
parent
4b4fa6acc5
commit
7021764d0c
26
gen6502.py
26
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)
|
||||
|
|
Loading…
Reference in New Issue