don't use globals in the preprocessor

Macros are now passed around as lists and dicts.
This commit is contained in:
Bryan Bishop 2013-08-28 17:53:26 -05:00
parent 16bfc01124
commit e4d3ea7256
2 changed files with 18 additions and 17 deletions

View File

@ -44,10 +44,6 @@ do_macro_sanity_check = False
class SkippableMacro(object): class SkippableMacro(object):
macro_name = "db" macro_name = "db"
skippable_macros = [SkippableMacro]
macros += skippable_macros
chars = { chars = {
"": 0x05, "": 0x05,
"": 0x06, "": 0x06,
@ -415,11 +411,10 @@ def quote_translator(asm):
def extract_token(asm): def extract_token(asm):
return asm.split(" ")[0].strip() return asm.split(" ")[0].strip()
def make_macro_table(): def make_macro_table(macros):
return dict(((macro.macro_name, macro) for macro in macros)) return dict(((macro.macro_name, macro) for macro in macros))
macro_table = make_macro_table()
def macro_test(asm): def macro_test(asm, macro_table):
""" """
Returns a matching macro, or None/False. Returns a matching macro, or None/False.
""" """
@ -485,7 +480,7 @@ def macro_translator(macro, token, line, skippable_macros):
# "db" is a macro because of SkippableMacro # "db" is a macro because of SkippableMacro
# rgbasm can handle "db" so no preprocessing is required # rgbasm can handle "db" so no preprocessing is required
# (don't check its param count) # (don't check its param count)
if macro.macro_name == "db" and macro in skippable_macros: if macro.__name__ in skippable_macros or (macro.macro_name == "db" and macro in skippable_macros):
sys.stdout.write(original_line) sys.stdout.write(original_line)
return return
@ -542,7 +537,10 @@ def macro_translator(macro, token, line, skippable_macros):
index = 0 index = 0
while index < len(params): while index < len(params):
param_type = macro.param_types[index - correction] try:
param_type = macro.param_types[index - correction]
except KeyError as exception:
raise Exception("line is: " + str(line) + " and macro is: " + str(macro))
description = param_type["name"] description = param_type["name"]
param_klass = param_type["class"] param_klass = param_type["class"]
byte_type = param_klass.byte_type # db or dw byte_type = param_klass.byte_type # db or dw
@ -588,7 +586,7 @@ def macro_translator(macro, token, line, skippable_macros):
sys.stdout.write(output) sys.stdout.write(output)
def read_line(l, skippable_macros): def read_line(l, skippable_macros, macro_table):
"""Preprocesses a given line of asm.""" """Preprocesses a given line of asm."""
# strip comments from asm # strip comments from asm
@ -614,7 +612,7 @@ def read_line(l, skippable_macros):
# check against other preprocessor features # check against other preprocessor features
else: else:
macro, token = macro_test(asm) macro, token = macro_test(asm, macro_table)
if macro: if macro:
macro_translator(macro, token, asm, skippable_macros) macro_translator(macro, token, asm, skippable_macros)
else: else:
@ -622,13 +620,16 @@ def read_line(l, skippable_macros):
if comment: sys.stdout.write(comment) if comment: sys.stdout.write(comment)
def preprocess(skippable_macros=None, lines=None): def preprocess(macros, skippable_macros=None, lines=None):
"""Main entry point for the preprocessor.""" """Main entry point for the preprocessor."""
if skippable_macros == None: if skippable_macros == None:
# Note that this is bad because the macro table doesn't include the
# skippable macros.
skippable_macros = [SkippableMacro] skippable_macros = [SkippableMacro]
macro_table = make_macro_table(list(set(macros + skippable_macros)))
# HACK for pokecrystal. Must be after make_macro_table call.
skippable_macros += ["TextEndingCommand"]
if not lines: if not lines:
# read each line from stdin # read each line from stdin
lines = (sys.stdin.readlines()) lines = (sys.stdin.readlines())
@ -637,8 +638,8 @@ def preprocess(skippable_macros=None, lines=None):
lines = lines.split("\n") lines = lines.split("\n")
for l in lines: for l in lines:
read_line(l, skippable_macros) read_line(l, skippable_macros, macro_table)
# only run against stdin when not included as a module # only run against stdin when not included as a module
if __name__ == "__main__": if __name__ == "__main__":
preprocess(skippable_macros=skippable_macros) preprocess(macros)

View File

@ -13,4 +13,4 @@ if __name__ == '__main__':
dest = os.path.splitext(source)[0] + '.tx' dest = os.path.splitext(source)[0] + '.tx'
sys.stdin = open(source, 'r') sys.stdin = open(source, 'r')
sys.stdout = open(dest, 'w') sys.stdout = open(dest, 'w')
preprocessor.preprocess() preprocessor.preprocess(preprocessor.macros)