summaryrefslogtreecommitdiff
path: root/preprocessor.py
diff options
context:
space:
mode:
Diffstat (limited to 'preprocessor.py')
-rw-r--r--preprocessor.py115
1 files changed, 66 insertions, 49 deletions
diff --git a/preprocessor.py b/preprocessor.py
index abb672822..9b748dbd6 100644
--- a/preprocessor.py
+++ b/preprocessor.py
@@ -32,6 +32,12 @@ macros = command_classes + \
movement_command_classes + \
music_classes
+# show lines before preprocessing in stdout
+show_original_lines = False
+
+# helpful for debugging macros
+do_macro_sanity_check = False
+
chars = {
"ガ": 0x05,
"ギ": 0x06,
@@ -338,6 +344,7 @@ def quote_translator(asm):
sys.stdout.write(asm)
return
+ output = ""
even = False
i = 0
for token in asms:
@@ -378,15 +385,18 @@ def quote_translator(asm):
char = char + token[0]
token = token[1:]
- sys.stdout.write("${0:02X}".format(chars[char]))
+ output += ("${0:02X}".format(chars[char]))
if len(token):
- sys.stdout.write(", ")
+ output += (", ")
# if not even
else:
- sys.stdout.write(token)
+ output += (token)
even = not even
+
+ sys.stdout.write(output)
+
return
def extract_token(asm):
@@ -405,9 +415,9 @@ def macro_test(asm):
token = extract_token(asm)
# check against all names
- try:
+ if token in macro_table:
return (macro_table[token], token)
- except:
+ else:
return (None, None)
def macro_translator(macro, token, line):
@@ -445,7 +455,8 @@ def macro_translator(macro, token, line):
raise Exception, "macro has no params?"
# write out a comment showing the original line
- sys.stdout.write("; original_line: " + original_line)
+ if show_original_lines:
+ sys.stdout.write("; original_line: " + original_line)
# "db" is a macro because of TextEndingCommand
# rgbasm can handle "db" so no preprocessing is required
@@ -458,51 +469,55 @@ def macro_translator(macro, token, line):
# do: all scripting macros
# don't: signpost, warp_def, person_event, xy_trigger
if not macro.override_byte_check:
- sys.stdout.write("db $%.2x\n" % (macro.id))
+ sys.stdout.write("db ${0:02X}\n".format(macro.id))
# --- long-winded sanity check goes here ---
- # sanity check... this won't work because PointerLabelBeforeBank shows
- # up as two params, so these two lengths will always be different.
- #assert len(params) == len(macro.param_types), \
- # "mismatched number of parameters on this line: " + \
- # original_line
-
- # v2 sanity check :) although it sorta sucks that this loop happens twice?
- allowed_length = 0
- for (index, param_type) in macro.param_types.items():
- param_klass = param_type["class"]
-
- if param_klass.byte_type == "db":
- allowed_length += 1 # just one value
- elif param_klass.byte_type == "dw":
- if param_klass.size == 2:
- allowed_length += 1 # just label
- elif param_klass == MoneyByteParam:
- allowed_length += 1
- elif param_klass.size == 3:
- allowed_length += 2 # bank and label
+ if do_macro_sanity_check:
+
+ # sanity check... this won't work because PointerLabelBeforeBank shows
+ # up as two params, so these two lengths will always be different.
+ #assert len(params) == len(macro.param_types), \
+ # "mismatched number of parameters on this line: " + \
+ # original_line
+
+ # v2 sanity check :) although it sorta sucks that this loop happens twice?
+ allowed_length = 0
+ for (index, param_type) in macro.param_types.items():
+ param_klass = param_type["class"]
+
+ if param_klass.byte_type == "db":
+ allowed_length += 1 # just one value
+ elif param_klass.byte_type == "dw":
+ if param_klass.size == 2:
+ allowed_length += 1 # just label
+ elif param_klass == MoneyByteParam:
+ allowed_length += 1
+ elif param_klass.size == 3:
+ allowed_length += 2 # bank and label
+ else:
+ raise Exception, "dunno what to do with a macro param with a size > 3"
else:
- raise Exception, "dunno what to do with a macro param with a size > 3"
- else:
- raise Exception, "dunno what to do with this non db/dw macro param: " + \
- str(param_klass) + " in line: " + original_line
+ raise Exception, "dunno what to do with this non db/dw macro param: " + \
+ str(param_klass) + " in line: " + original_line
- # sometimes the allowed length can vary
- if hasattr(macro, "allowed_lengths"):
- allowed_lengths = macro.allowed_lengths + [allowed_length]
- else:
- allowed_lengths = [allowed_length]
+ # sometimes the allowed length can vary
+ if hasattr(macro, "allowed_lengths"):
+ allowed_lengths = macro.allowed_lengths + [allowed_length]
+ else:
+ allowed_lengths = [allowed_length]
- assert len(params) in allowed_lengths, \
- "mismatched number of parameters on this line: " + \
- original_line
+ assert len(params) in allowed_lengths, \
+ "mismatched number of parameters on this line: " + \
+ original_line
# --- end of ridiculously long sanity check ---
# used for storetext
correction = 0
+ output = ""
+
index = 0
while index < len(params):
param_type = macro.param_types[index - correction]
@@ -519,24 +534,24 @@ def macro_translator(macro, token, line):
if (byte_type == "dw" and size != 2) or \
(byte_type == "db" and size != 1):
- sys.stdout.write("; " + description + "\n")
+ output += ("; " + description + "\n")
if size == 3 and issubclass(param_klass, PointerLabelBeforeBank):
# write the bank first
- sys.stdout.write("db " + params[index].strip() + "\n")
+ output += ("db " + param + "\n")
# write the pointer second
- sys.stdout.write("dw " + params[index+1].strip() + "\n")
+ output += ("dw " + params[index+1].strip() + "\n")
index += 2
correction += 1
elif size == 3 and issubclass(param_klass, PointerLabelAfterBank):
# write the pointer first
- sys.stdout.write("dw " + params[index].strip() + "\n")
+ output += ("dw " + param + "\n")
# write the bank second
- sys.stdout.write("db " + params[index+1].strip() + "\n")
+ output += ("db " + params[index+1].strip() + "\n")
index += 2
correction += 1
elif size == 3 and issubclass(param_klass, MoneyByteParam):
- sys.stdout.write("db " + MoneyByteParam.from_asm(params[index]) + "\n")
+ output += ("db " + MoneyByteParam.from_asm(param) + "\n")
index += 1
else:
raise Exception, "dunno what to do with this macro " + \
@@ -545,16 +560,18 @@ def macro_translator(macro, token, line):
# or just print out the byte
else:
- sys.stdout.write(byte_type + " " + param + " ; " + description + "\n")
+ output += (byte_type + " " + param + " ; " + description + "\n")
index += 1
+ sys.stdout.write(output)
+
def include_file(asm):
"""This is more reliable than rgbasm/rgbds including files on its own."""
- filename = asm.split("\"")
- filename = filename[1].replace("\"","").replace("\n","")
- lines = open(filename, 'r').readlines()
+ filename = asm.split("\"")[1]
+
+ lines = open(filename, "r").readlines()
for line in lines:
read_line(line)