summaryrefslogtreecommitdiff
path: root/pp2cc.py
diff options
context:
space:
mode:
Diffstat (limited to 'pp2cc.py')
-rwxr-xr-xpp2cc.py46
1 files changed, 26 insertions, 20 deletions
diff --git a/pp2cc.py b/pp2cc.py
index 6205e94..c2f5029 100755
--- a/pp2cc.py
+++ b/pp2cc.py
@@ -37,14 +37,14 @@ class Logger(object):
def error(self, message, linked_node=None):
self.log(message, linked_node=linked_node, type="error")
while linked_node:
- print " in", linked_node.node.coord, linked_node.type
+ print(" in {} {}".format(linked_node.node.coord, linked_node.type))
linked_node = linked_node.parent
raise
def log(self, message, linked_node=None, type="log"):
source = ""
if isinstance(linked_node, LinkedNode):
source = str(linked_node.getLocation()) + ": "
- print type + ":" + source, message
+ print("{}:{} {}".format(type, source, message))
class Parse(object):
def __init__(self):
@@ -151,8 +151,8 @@ class Parse(object):
str(thing), linked_node=linked_node)
self.codeSegment += self.parseStatement(thing, root_node)
# hack: static functions are local to a file, so remove them
- for funcname, function in self.functions.items():
- if function.isStatic():
+ for funcname in list(self.functions.keys()):
+ if self.functions[funcname].isStatic():
del self.functions[funcname]
def compileASM(self):
"""Processes lines of assembly and merge it into the output"""
@@ -163,7 +163,7 @@ class Parse(object):
new_label = self.uniqLbl(label)
self.asm_node.renameId(label, new_label)
self.labels.add(new_label)
- for name, init_vals in self.asm_node.getDataDefinitions().iteritems():
+ for name, init_vals in self.asm_node.getDataDefinitions().items():
if name in self.varNames:
old_count = len(self.varNames[name])
new_count = len(init_vals)
@@ -182,7 +182,7 @@ class Parse(object):
"""Retrieves the ASM source. You need to compile it first"""
output = []
output.append("@DATA")
- for varName, initializers in self.varNames.iteritems():
+ for varName, initializers in self.varNames.items():
padding = " " * (16 - len(varName) - 1)
assert len(initializers) > 0, "Size of '{}' must be at least 1".format(varName)
output.append(varName + padding + " DW " + ",".join(initializers))
@@ -1151,21 +1151,28 @@ class Parse(object):
linked_cn))
except RuntimeError:
e = sys.exc_info()[1]
- print "case label does not reduce to an integer constant"
- print e
+ print("case label does not reduce to an integer constant")
+ print(e)
raise
lines_cases.append(self.asm.binary_op("CMP", switch_reg, case_val))
lines_cases.append(self.asm.branch_op("BEQ", lbl_case))
lines_stmts.append(self.asm.noop(lbl_case))
- linked_cn = LinkedNode(cn.stmt, linked_cn.parent)
+ if hasattr(cn, "stmt"): # pycparser-2.05
+ linked_cn = LinkedNode(cn.stmt, linked_cn.parent)
elif linked_cn.type == "Default":
lbl_default = self.uniqLbl("default")
lines_stmts.append(self.asm.noop(lbl_default))
- linked_cn = LinkedNode(cn.stmt, linked_cn.parent)
+ if hasattr(cn, "stmt"): # pycparser-2.05
+ linked_cn = LinkedNode(cn.stmt, linked_cn.parent)
if linked_cn.type in ("Case", "Default"):
- # nested case/default labels like case 1: case 2: stmt;
- pass
+ # pycparser-2.05: nested case/default labels like case 1: case 2: stmt;
+ if hasattr(cn, "stmts"):
+ # pycparser-2.06: may be empty if there is a following case
+ for node in cn.stmts:
+ # regular body
+ lines_stmts += self.parseStatement(node, linked_cn)
+ linked_cn = None
else:
# regular body
lines_stmts += self.parseStatement(linked_cn.node, linked_cn.parent)
@@ -1411,7 +1418,7 @@ if __name__ == "__main__":
class ArgumentError(Exception):
pass
def usage():
- print """Usage: pp2cc.py [options] filename..
+ print("""Usage: pp2cc.py [options] filename..
Multiple input files can be specified, options can be specified before and
after filenames.
Options:
@@ -1425,8 +1432,7 @@ Options:
adding #define name or #define name definition respectively
-U name This option is passed to the cpp program and acts like
adding #undef name
- --no-cpp Disable the use of the C Preprocessor
- """
+ --no-cpp Disable the use of the C Preprocessor""")
try:
for arg in sys.argv[1:]:
if not set_arg is None:
@@ -1470,8 +1476,8 @@ Options:
raise ArgumentError("No input files")
except ArgumentError:
e = sys.exc_info()[1]
- print str(e)
- print "For usage, run: python pp2cc.py --help"
+ print(e)
+ print("For usage, run: python pp2cc.py --help")
sys.exit(255)
# end of arguments processing
@@ -1494,9 +1500,9 @@ Options:
outf = open(settings["output_filename"], "w")
outf.write(source)
outf.close()
- print "Compilation succesful"
- print "The output is written to", settings["output_filename"]
+ print("Compilation succesful")
+ print("The output is written to " + settings["output_filename"])
except c_parser.ParseError:
e = sys.exc_info()[1]
- print "A fatal (syntax) error was found during parsing:", str(e)
+ print("A fatal (syntax) error was found during parsing:" + str(e))
sys.exit(1)