diff --git a/zxb/zxb.py b/zxb/zxb.py index 345fc0a2f..6c70e828f 100755 --- a/zxb/zxb.py +++ b/zxb/zxb.py @@ -267,7 +267,7 @@ def main(args=None, emitter=None): input_ = zxbpp.OUTPUT zxbparser.parser.parse(input_, lexer=zxblex.lexer, tracking=True, - debug=(OPTIONS.Debug.value > 2)) + debug=(OPTIONS.Debug.value > 1)) if gl.has_errors: debug.__DEBUG__("exiting due to errors.") return 1 # Exit with errors diff --git a/zxbasm/asmparse.py b/zxbasm/asmparse.py index 4161c0402..8160d5033 100755 --- a/zxbasm/asmparse.py +++ b/zxbasm/asmparse.py @@ -1460,7 +1460,7 @@ def assemble(input_): else: parser_ = parser - parser_.parse(input_, lexer=LEXER, debug=OPTIONS.Debug.value > 2) + parser_.parse(input_, lexer=LEXER, debug=OPTIONS.Debug.value > 1) if len(MEMORY.scopes): error(MEMORY.scopes[-1], 'Missing ENDP to close this scope') diff --git a/zxbpp/prepro/id_.py b/zxbpp/prepro/id_.py index 289e34cf0..56005b2bb 100644 --- a/zxbpp/prepro/id_.py +++ b/zxbpp/prepro/id_.py @@ -14,8 +14,8 @@ from .output import CURRENT_FILE -class ID(object): - """ This class represents an identifier. It's stores a string +class ID: + """ This class represents an identifier. It stores a string (the ID name and value by default). """ def __init__(self, id_, args=None, value=None, lineno=None, fname=None): @@ -41,14 +41,14 @@ def hasArgs(self): def __str__(self): return self.name - def __dumptable(self, table): - """ Dumps table on screen - for debugging purposes + @staticmethod + def __dumptable(table): + """ Dumps table on screen for debugging purposes """ for x in table.table.keys(): sys.stdout.write("{0}\t<--- {1} {2}".format(x, table[x], type(table[x]))) if isinstance(table[x], ID): - sys.stdout(" {0}".format(table[x].value)), + sys.stdout.write(" {0}".format(table[x].value)), sys.stdout.write("\n") def __call__(self, table): diff --git a/zxbpp/prepro/macrocall.py b/zxbpp/prepro/macrocall.py index ce1049790..72c8bc094 100644 --- a/zxbpp/prepro/macrocall.py +++ b/zxbpp/prepro/macrocall.py @@ -7,7 +7,10 @@ from api.debug import __DEBUG__ -class MacroCall(object): +DEBUG_LEVEL = 3 # Which -d level is required to show debug info + + +class MacroCall: """ A call to a macro, stored in an object. Every time the macro() is called, the macro returns it value. @@ -21,7 +24,8 @@ def __init__(self, lineno, table, id_, args=None): self.callargs = args self.lineno = lineno - def eval(self, arg): + @staticmethod + def eval(arg): """ Evaluates a given argument. The token will be returned by default "as is", except if it's a macrocall. In such case it will be evaluated recursively. @@ -31,41 +35,41 @@ def eval(self, arg): def __call__(self, symbolTable=None): """ Execute the macro call using LAZY evaluation """ - __DEBUG__("evaluating '%s'" % self.id_, 2) + __DEBUG__("evaluating '%s'" % self.id_, DEBUG_LEVEL) if symbolTable is None: symbolTable = self.table # The macro is not defined => returned as is if not self.is_defined(symbolTable): - __DEBUG__("macro '%s' not defined" % self.id_, 2) + __DEBUG__("macro '%s' not defined" % self.id_, DEBUG_LEVEL) tmp = self.id_ if self.callargs is not None: tmp += str(self.callargs) - __DEBUG__("evaluation result: %s" % tmp, 2) + __DEBUG__("evaluation result: %s" % tmp, DEBUG_LEVEL) return tmp # The macro is defined - __DEBUG__("macro '%s' defined" % self.id_, 2) + __DEBUG__("macro '%s' defined" % self.id_, DEBUG_LEVEL) TABLE = copy.deepcopy(symbolTable) ID = TABLE[self.id_] # Get the defined macro if ID.hasArgs and self.callargs is None: return self.id_ # If no args passed, returned as is if self.callargs: # has args. Evaluate them removing spaces - __DEBUG__("'%s' has args defined" % self.id_, 2) + __DEBUG__("'%s' has args defined" % self.id_, DEBUG_LEVEL) __DEBUG__("evaluating %i arg(s) for '%s'" % - (len(self.callargs), self.id_), 2) + (len(self.callargs), self.id_), DEBUG_LEVEL) args = [x(TABLE).strip() for x in self.callargs] __DEBUG__("macro call: %s%s" % - (self.id_, '(' + ', '.join(args) + ')'), 2) + (self.id_, '(' + ', '.join(args) + ')'), DEBUG_LEVEL) if not ID.hasArgs: # The macro doesn't need args - __DEBUG__("'%s' has no args defined" % self.id_, 2) + __DEBUG__("'%s' has no args defined" % self.id_, DEBUG_LEVEL) tmp = ID(TABLE) # If no args passed, returned as is if self.callargs is not None: tmp += '(' + ', '.join(args) + ')' - __DEBUG__("evaluation result: %s" % tmp, 2) + __DEBUG__("evaluation result: %s" % tmp, DEBUG_LEVEL) return tmp # Now ensure both args and callargs have the same length @@ -75,14 +79,14 @@ def __call__(self, symbolTable=None): len(self.callargs)), self.lineno) # Carry out unification - __DEBUG__('carrying out args unification', 2) + __DEBUG__('carrying out args unification', DEBUG_LEVEL) for i in range(len(self.callargs)): - __DEBUG__("arg '%s' = '%s'" % (ID.args[i].name, args[i]), 2) + __DEBUG__("arg '%s' = '%s'" % (ID.args[i].name, args[i]), DEBUG_LEVEL) TABLE.set(ID.args[i].name, self.lineno, args[i]) tmp = ID(TABLE) if '\n' in tmp: - tmp += '\n#line %i\n' % (self.lineno) + tmp += '\n#line %i\n' % self.lineno return tmp diff --git a/zxbpp/zxbpp.py b/zxbpp/zxbpp.py index 836dabdc3..7d268dbc8 100755 --- a/zxbpp/zxbpp.py +++ b/zxbpp/zxbpp.py @@ -77,6 +77,7 @@ def init(): global ID_TABLE global CURRENT_FILE + OPTIONS.add_option_if_not_defined('debug_zxbpp', bool, False) global_.FILENAME = '(stdin)' OUTPUT = '' INCLUDED = {} @@ -750,7 +751,7 @@ def filter_(input_, filename='', state='INITIAL'): CURRENT_DIR = os.path.dirname(CURRENT_FILE[-1]) LEXER.input(input_, filename) LEXER.lex.begin(state) - parser.parse(lexer=LEXER, debug=OPTIONS.Debug.value > 2) + parser.parse(lexer=LEXER, debug=OPTIONS.debug_zxbpp.value) CURRENT_FILE.pop() CURRENT_DIR = prev_dir @@ -776,7 +777,7 @@ def main(argv): if len(OUTPUT) and OUTPUT[-1] != '\n': OUTPUT += '\n' - parser.parse(lexer=LEXER, debug=OPTIONS.Debug.value > 2) + parser.parse(lexer=LEXER, debug=OPTIONS.debug_zxbpp.value) CURRENT_FILE.pop() CURRENT_DIR = os.path.dirname(CURRENT_FILE[-1]) @@ -786,7 +787,7 @@ def main(argv): if len(OUTPUT) and OUTPUT[-1] != '\n': OUTPUT += '\n' - parser.parse(lexer=LEXER, debug=OPTIONS.Debug.value > 2) + parser.parse(lexer=LEXER, debug=OPTIONS.debug_zxbpp.value) CURRENT_FILE.pop() global_.FILENAME = prev_file return global_.has_errors @@ -809,16 +810,17 @@ def entry_point(args=None): parser = argparse.ArgumentParser() parser.add_argument('-o', '--output', type=str, dest='output_file', default=None, - help='Sets output file. If not specified, will output to console (STDOUT)') + help='Sets output file. Default is to output to console (STDOUT)') parser.add_argument('-d', '--debug', dest='debug', default=OPTIONS.Debug.value, action='count', - help='Enable verbosity/debugging output. Additional -d increase verbosity/debug level') + help='Enable verbosity/debugging output. Additional -d increases verbosity/debug level') parser.add_argument('-e', '--errmsg', type=str, dest='stderr', default=None, - help='Error messages file (standard error console by default)') + help='Error messages file. Standard error console by default (STDERR)') parser.add_argument('input_file', type=str, default=None, nargs='?', help="File to parse. If not specified, console input will be used (STDIN)") options = parser.parse_args(args=args) OPTIONS.Debug.value = options.debug + OPTIONS.debug_zxbpp.value = OPTIONS.Debug.value > 0 if options.stderr: OPTIONS.StdErrFileName.value = options.stderr