Skip to content

Commit 6243644

Browse files
authored
Merge pull request #343 from boriel/feature/refact_debug_level_in_zxbpp
Feature/refact debug level in zxbpp
2 parents b6695b7 + 85e52ee commit 6243644

5 files changed

Lines changed: 34 additions & 28 deletions

File tree

zxb/zxb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def main(args=None, emitter=None):
267267

268268
input_ = zxbpp.OUTPUT
269269
zxbparser.parser.parse(input_, lexer=zxblex.lexer, tracking=True,
270-
debug=(OPTIONS.Debug.value > 2))
270+
debug=(OPTIONS.Debug.value > 1))
271271
if gl.has_errors:
272272
debug.__DEBUG__("exiting due to errors.")
273273
return 1 # Exit with errors

zxbasm/asmparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ def assemble(input_):
14601460
else:
14611461
parser_ = parser
14621462

1463-
parser_.parse(input_, lexer=LEXER, debug=OPTIONS.Debug.value > 2)
1463+
parser_.parse(input_, lexer=LEXER, debug=OPTIONS.Debug.value > 1)
14641464
if len(MEMORY.scopes):
14651465
error(MEMORY.scopes[-1], 'Missing ENDP to close this scope')
14661466

zxbpp/prepro/id_.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from .output import CURRENT_FILE
1515

1616

17-
class ID(object):
18-
""" This class represents an identifier. It's stores a string
17+
class ID:
18+
""" This class represents an identifier. It stores a string
1919
(the ID name and value by default).
2020
"""
2121
def __init__(self, id_, args=None, value=None, lineno=None, fname=None):
@@ -41,14 +41,14 @@ def hasArgs(self):
4141
def __str__(self):
4242
return self.name
4343

44-
def __dumptable(self, table):
45-
""" Dumps table on screen
46-
for debugging purposes
44+
@staticmethod
45+
def __dumptable(table):
46+
""" Dumps table on screen for debugging purposes
4747
"""
4848
for x in table.table.keys():
4949
sys.stdout.write("{0}\t<--- {1} {2}".format(x, table[x], type(table[x])))
5050
if isinstance(table[x], ID):
51-
sys.stdout(" {0}".format(table[x].value)),
51+
sys.stdout.write(" {0}".format(table[x].value)),
5252
sys.stdout.write("\n")
5353

5454
def __call__(self, table):

zxbpp/prepro/macrocall.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
from api.debug import __DEBUG__
88

99

10-
class MacroCall(object):
10+
DEBUG_LEVEL = 3 # Which -d level is required to show debug info
11+
12+
13+
class MacroCall:
1114
""" A call to a macro, stored in an object.
1215
Every time the macro() is called, the macro returns
1316
it value.
@@ -21,7 +24,8 @@ def __init__(self, lineno, table, id_, args=None):
2124
self.callargs = args
2225
self.lineno = lineno
2326

24-
def eval(self, arg):
27+
@staticmethod
28+
def eval(arg):
2529
""" Evaluates a given argument. The token will be returned by default
2630
"as is", except if it's a macrocall. In such case it will be evaluated
2731
recursively.
@@ -31,41 +35,41 @@ def eval(self, arg):
3135
def __call__(self, symbolTable=None):
3236
""" Execute the macro call using LAZY evaluation
3337
"""
34-
__DEBUG__("evaluating '%s'" % self.id_, 2)
38+
__DEBUG__("evaluating '%s'" % self.id_, DEBUG_LEVEL)
3539
if symbolTable is None:
3640
symbolTable = self.table
3741

3842
# The macro is not defined => returned as is
3943
if not self.is_defined(symbolTable):
40-
__DEBUG__("macro '%s' not defined" % self.id_, 2)
44+
__DEBUG__("macro '%s' not defined" % self.id_, DEBUG_LEVEL)
4145
tmp = self.id_
4246
if self.callargs is not None:
4347
tmp += str(self.callargs)
44-
__DEBUG__("evaluation result: %s" % tmp, 2)
48+
__DEBUG__("evaluation result: %s" % tmp, DEBUG_LEVEL)
4549
return tmp
4650

4751
# The macro is defined
48-
__DEBUG__("macro '%s' defined" % self.id_, 2)
52+
__DEBUG__("macro '%s' defined" % self.id_, DEBUG_LEVEL)
4953
TABLE = copy.deepcopy(symbolTable)
5054
ID = TABLE[self.id_] # Get the defined macro
5155
if ID.hasArgs and self.callargs is None:
5256
return self.id_ # If no args passed, returned as is
5357

5458
if self.callargs: # has args. Evaluate them removing spaces
55-
__DEBUG__("'%s' has args defined" % self.id_, 2)
59+
__DEBUG__("'%s' has args defined" % self.id_, DEBUG_LEVEL)
5660
__DEBUG__("evaluating %i arg(s) for '%s'" %
57-
(len(self.callargs), self.id_), 2)
61+
(len(self.callargs), self.id_), DEBUG_LEVEL)
5862
args = [x(TABLE).strip() for x in self.callargs]
5963
__DEBUG__("macro call: %s%s" %
60-
(self.id_, '(' + ', '.join(args) + ')'), 2)
64+
(self.id_, '(' + ', '.join(args) + ')'), DEBUG_LEVEL)
6165

6266
if not ID.hasArgs: # The macro doesn't need args
63-
__DEBUG__("'%s' has no args defined" % self.id_, 2)
67+
__DEBUG__("'%s' has no args defined" % self.id_, DEBUG_LEVEL)
6468
tmp = ID(TABLE) # If no args passed, returned as is
6569
if self.callargs is not None:
6670
tmp += '(' + ', '.join(args) + ')'
6771

68-
__DEBUG__("evaluation result: %s" % tmp, 2)
72+
__DEBUG__("evaluation result: %s" % tmp, DEBUG_LEVEL)
6973
return tmp
7074

7175
# Now ensure both args and callargs have the same length
@@ -75,14 +79,14 @@ def __call__(self, symbolTable=None):
7579
len(self.callargs)), self.lineno)
7680

7781
# Carry out unification
78-
__DEBUG__('carrying out args unification', 2)
82+
__DEBUG__('carrying out args unification', DEBUG_LEVEL)
7983
for i in range(len(self.callargs)):
80-
__DEBUG__("arg '%s' = '%s'" % (ID.args[i].name, args[i]), 2)
84+
__DEBUG__("arg '%s' = '%s'" % (ID.args[i].name, args[i]), DEBUG_LEVEL)
8185
TABLE.set(ID.args[i].name, self.lineno, args[i])
8286

8387
tmp = ID(TABLE)
8488
if '\n' in tmp:
85-
tmp += '\n#line %i\n' % (self.lineno)
89+
tmp += '\n#line %i\n' % self.lineno
8690

8791
return tmp
8892

zxbpp/zxbpp.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def init():
7777
global ID_TABLE
7878
global CURRENT_FILE
7979

80+
OPTIONS.add_option_if_not_defined('debug_zxbpp', bool, False)
8081
global_.FILENAME = '(stdin)'
8182
OUTPUT = ''
8283
INCLUDED = {}
@@ -750,7 +751,7 @@ def filter_(input_, filename='<internal>', state='INITIAL'):
750751
CURRENT_DIR = os.path.dirname(CURRENT_FILE[-1])
751752
LEXER.input(input_, filename)
752753
LEXER.lex.begin(state)
753-
parser.parse(lexer=LEXER, debug=OPTIONS.Debug.value > 2)
754+
parser.parse(lexer=LEXER, debug=OPTIONS.debug_zxbpp.value)
754755
CURRENT_FILE.pop()
755756
CURRENT_DIR = prev_dir
756757

@@ -776,7 +777,7 @@ def main(argv):
776777
if len(OUTPUT) and OUTPUT[-1] != '\n':
777778
OUTPUT += '\n'
778779

779-
parser.parse(lexer=LEXER, debug=OPTIONS.Debug.value > 2)
780+
parser.parse(lexer=LEXER, debug=OPTIONS.debug_zxbpp.value)
780781
CURRENT_FILE.pop()
781782
CURRENT_DIR = os.path.dirname(CURRENT_FILE[-1])
782783

@@ -786,7 +787,7 @@ def main(argv):
786787
if len(OUTPUT) and OUTPUT[-1] != '\n':
787788
OUTPUT += '\n'
788789

789-
parser.parse(lexer=LEXER, debug=OPTIONS.Debug.value > 2)
790+
parser.parse(lexer=LEXER, debug=OPTIONS.debug_zxbpp.value)
790791
CURRENT_FILE.pop()
791792
global_.FILENAME = prev_file
792793
return global_.has_errors
@@ -809,16 +810,17 @@ def entry_point(args=None):
809810

810811
parser = argparse.ArgumentParser()
811812
parser.add_argument('-o', '--output', type=str, dest='output_file', default=None,
812-
help='Sets output file. If not specified, will output to console (STDOUT)')
813+
help='Sets output file. Default is to output to console (STDOUT)')
813814
parser.add_argument('-d', '--debug', dest='debug', default=OPTIONS.Debug.value, action='count',
814-
help='Enable verbosity/debugging output. Additional -d increase verbosity/debug level')
815+
help='Enable verbosity/debugging output. Additional -d increases verbosity/debug level')
815816
parser.add_argument('-e', '--errmsg', type=str, dest='stderr', default=None,
816-
help='Error messages file (standard error console by default)')
817+
help='Error messages file. Standard error console by default (STDERR)')
817818
parser.add_argument('input_file', type=str, default=None, nargs='?',
818819
help="File to parse. If not specified, console input will be used (STDIN)")
819820

820821
options = parser.parse_args(args=args)
821822
OPTIONS.Debug.value = options.debug
823+
OPTIONS.debug_zxbpp.value = OPTIONS.Debug.value > 0
822824

823825
if options.stderr:
824826
OPTIONS.StdErrFileName.value = options.stderr

0 commit comments

Comments
 (0)