Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion zxb/zxb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion zxbasm/asmparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
12 changes: 6 additions & 6 deletions zxbpp/prepro/id_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
32 changes: 18 additions & 14 deletions zxbpp/prepro/macrocall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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

Expand Down
14 changes: 8 additions & 6 deletions zxbpp/zxbpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -750,7 +751,7 @@ def filter_(input_, filename='<internal>', 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

Expand All @@ -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])

Expand All @@ -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
Expand All @@ -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
Expand Down