2018-11-15 00:03:51 +00:00
|
|
|
import re, util
|
|
|
|
|
|
|
|
class Keys:
|
|
|
|
MACRO_DEFINITION = "MACRO: (.*)"
|
|
|
|
END = "END"
|
2018-11-17 18:56:40 +00:00
|
|
|
INSTRUCTION = "^([A-Za-z]+)(?:\s)?(.*)?$"
|
|
|
|
LABEL = "^([A-Za-z0-9]+):"
|
2018-11-15 00:03:51 +00:00
|
|
|
@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)]
|
|
|
|
|
2018-11-08 01:12:55 +00:00
|
|
|
def asm(i):
|
2018-11-15 00:03:51 +00:00
|
|
|
macros = dict()
|
|
|
|
lines = [l.strip() for l in i.split("\n") if l.strip()]
|
2018-11-17 18:56:40 +00:00
|
|
|
ignore_list = []
|
|
|
|
ret = []
|
2018-11-15 00:03:51 +00:00
|
|
|
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])
|
2018-11-17 18:56:40 +00:00
|
|
|
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)
|