From 0f28e96e4d42b5fee5e7dec094f61fdf9d46577f Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:37:02 -0500 Subject: remove the show_original_lines global --- preprocessor.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'preprocessor.py') diff --git a/preprocessor.py b/preprocessor.py index b6bdfbe7d..6ddd45539 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -31,9 +31,6 @@ macros += movement_command_classes macros += music_classes macros += effect_classes -# show lines before preprocessing in stdout -show_original_lines = False - # helpful for debugging macros do_macro_sanity_check = False @@ -434,11 +431,12 @@ def is_based_on(something, base): options += [something.__name__] return (base in options) -def macro_translator(macro, token, line): +def macro_translator(macro, token, line, show_original_lines=False): """ Converts a line with a macro into a rgbasm-compatible line. - """ + @param show_original_lines: show lines before preprocessing in stdout + """ assert macro.macro_name == token, "macro/token mismatch" original_line = line -- cgit v1.2.3 From 998fa0b198c19088189634920b2c9829a5324576 Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:37:55 -0500 Subject: remove the do_macro_sanity_check global --- preprocessor.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'preprocessor.py') diff --git a/preprocessor.py b/preprocessor.py index 6ddd45539..ae9b5624b 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -31,9 +31,6 @@ macros += movement_command_classes macros += music_classes macros += effect_classes -# helpful for debugging macros -do_macro_sanity_check = False - chars = { "ガ": 0x05, "ギ": 0x06, @@ -431,11 +428,12 @@ def is_based_on(something, base): options += [something.__name__] return (base in options) -def macro_translator(macro, token, line, show_original_lines=False): +def macro_translator(macro, token, line, show_original_lines=False, do_macro_sanity_check=False): """ Converts a line with a macro into a rgbasm-compatible line. @param show_original_lines: show lines before preprocessing in stdout + @param do_macro_sanity_check: helpful for debugging macros """ assert macro.macro_name == token, "macro/token mismatch" -- cgit v1.2.3 From 1ce2bccd3747f5c311f40389fa3895e29dfae21b Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:45:12 -0500 Subject: generic preprocessor-specific exception classes These are basic python Exception subclasses that can be used to throw more specific errors and exceptions from within the preprocessor. AssertionError is not a good idea. --- preprocessor.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'preprocessor.py') diff --git a/preprocessor.py b/preprocessor.py index b6bdfbe7d..69121747d 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -302,6 +302,16 @@ chars = { "9": 0xFF } +class PreprocessorException(Exception): + """ + There was a problem in the preprocessor. + """ + +class MacroException(PreprocessorException): + """ + There was a problem with a macro. + """ + def separate_comment(l): """ Separates asm and comments on a single line. -- cgit v1.2.3 From ecedde19931bd7d1eb71c59498dab98944639b85 Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:46:11 -0500 Subject: replace an assert in macro_translator Use a MacroException instead of an AssertionError. --- preprocessor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'preprocessor.py') diff --git a/preprocessor.py b/preprocessor.py index 69121747d..a6a9efd29 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -449,7 +449,8 @@ def macro_translator(macro, token, line): Converts a line with a macro into a rgbasm-compatible line. """ - assert macro.macro_name == token, "macro/token mismatch" + if macro.macro_name != token: + raise MacroException("macro/token mismatch") original_line = line -- cgit v1.2.3 From 2c22d9220c48b2a36550b5a5a2bae1e0988da0d6 Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:50:17 -0500 Subject: fix "raise Exception" formatting in preprocessor --- preprocessor.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'preprocessor.py') diff --git a/preprocessor.py b/preprocessor.py index a6a9efd29..905e27533 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -478,7 +478,7 @@ def macro_translator(macro, token, line): # check if there are no params (redundant) if len(params) == 1 and params[0] == "": - raise Exception, "macro has no params?" + raise Exception("macro has no params?") # write out a comment showing the original line if show_original_lines: @@ -519,10 +519,14 @@ def macro_translator(macro, token, line): 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" + 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: {klass} in line {line}" + .format(klass=param_klass, line=original_line) + ) # sometimes the allowed length can vary if hasattr(macro, "allowed_lengths"): @@ -577,9 +581,13 @@ def macro_translator(macro, token, line): output += ("db " + param_klass.from_asm(param) + "\n") index += 1 else: - raise Exception, "dunno what to do with this macro " + \ - "param (" + str(param_klass) + ") " + "on this line: " + \ - original_line + raise Exception( + "dunno what to do with this macro param ({klass}) in line: {line}" + .format( + klass=param_klass, + line=original_line, + ) + ) # or just print out the byte else: -- cgit v1.2.3 From 95f7270141cbdb32c7d178a8a89a822e66e3a113 Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:51:31 -0500 Subject: raise MacroException instead of Exception A more specific exception means that error handling can actually work in the future. --- preprocessor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'preprocessor.py') diff --git a/preprocessor.py b/preprocessor.py index 905e27533..daed157d4 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -478,7 +478,7 @@ def macro_translator(macro, token, line): # check if there are no params (redundant) if len(params) == 1 and params[0] == "": - raise Exception("macro has no params?") + raise MacroException("macro has no params?") # write out a comment showing the original line if show_original_lines: @@ -519,11 +519,11 @@ def macro_translator(macro, token, line): elif param_klass.size == 3: allowed_length += 2 # bank and label else: - raise Exception( + raise MacroException( "dunno what to do with a macro param with a size > 3" ) else: - raise Exception( + raise MacroException( "dunno what to do with this non db/dw macro param: {klass} in line {line}" .format(klass=param_klass, line=original_line) ) @@ -581,7 +581,7 @@ def macro_translator(macro, token, line): output += ("db " + param_klass.from_asm(param) + "\n") index += 1 else: - raise Exception( + raise MacroException( "dunno what to do with this macro param ({klass}) in line: {line}" .format( klass=param_klass, -- cgit v1.2.3 From 93514b18629fe42c0a2688dac57d90998ea5daaa Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:54:03 -0500 Subject: convert a macro_translator assert in preprocessor AssertionError -> PreprocessorException --- preprocessor.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'preprocessor.py') diff --git a/preprocessor.py b/preprocessor.py index daed157d4..c6ae137c4 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -534,9 +534,15 @@ def macro_translator(macro, token, line): else: allowed_lengths = [allowed_length] - assert len(params) in allowed_lengths, \ - "mismatched number of parameters on this line: " + \ - original_line + if len(params) not in allowed_lengths: + raise PreprocessorException( + "mismatched number of parameters ({count}, instead of any of {allowed}) on this line: {line}" + .format( + count=len(params), + allowed=allowed_lengths, + line=original_line, + ) + ) # --- end of ridiculously long sanity check --- -- cgit v1.2.3 From ebb591a7ec624acce35d0378201c641724ac8d4c Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 31 Aug 2013 09:56:24 -0500 Subject: make a MacroException more verbose in preprocessor --- preprocessor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'preprocessor.py') diff --git a/preprocessor.py b/preprocessor.py index c6ae137c4..a31386801 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -520,7 +520,8 @@ def macro_translator(macro, token, line): allowed_length += 2 # bank and label else: raise MacroException( - "dunno what to do with a macro param with a size > 3" + "dunno what to do with a macro param with a size > 3 (size={size})" + .format(size=param_klass.size) ) else: raise MacroException( -- cgit v1.2.3