summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2011-11-29 14:40:49 +0000
committerPeter Wu <lekensteyn@gmail.com>2011-11-29 14:40:49 +0000
commit27a1317e8bc90cf45207cb8810ff56e12a6a0673 (patch)
tree281c0bb689c5f8b7371ccb1bc508b0b59fb52d37
parent7c750f79455e4b9d44c3d7f8f7ffe1d831f098cb (diff)
downloadpp2cc-27a1317e8bc90cf45207cb8810ff56e12a6a0673.tar.gz
Support for DeclList in for loops, fix naming of variables in asm
-rwxr-xr-xpp2cc.py26
1 files changed, 18 insertions, 8 deletions
diff --git a/pp2cc.py b/pp2cc.py
index 046ecf8..bea3a21 100755
--- a/pp2cc.py
+++ b/pp2cc.py
@@ -201,9 +201,9 @@ class Variables(object):
if name in self.local_vars:
raise RuntimeError("Redeclaration of variable '{}'".format(name))
# global variables are prefixed "var_", locals with "varl_"
- var_name = "varl_" if self.parent_variables else "var_"
- var_name += name
- self.local_vars[name] = self.uniqName(var_name)
+ var_name = ("varl_" if self.parent_variables else "var_") + name
+ var_name = self.uniqName(var_name)
+ self.local_vars[name] = var_name
self.defined_names.append(var_name)
return var_name
@@ -239,7 +239,8 @@ class LinkedNode(object):
if not defined_names:
defined_names = parent.variables.defined_names
- if self.type == "Compound" or self.type == "FileAST":
+ # for is added for the init part (C99)
+ if self.type in ("Compound", "FileAST", "For"):
# the node appears to have an own variable scope
if defined_names is None:
raise RuntimeError("No object found for storing variables")
@@ -957,7 +958,11 @@ class Parse(object):
linked_node.setContinue(lbl_for)
if node.init: # init;
- lines += self.parseExpression(node.init, linked_node)
+ if isinstance(node.init, c_ast.DeclList):
+ # loop initial declaration is supported in C99
+ lines += self.parseDeclList(LinkedNode(node.init, linked_node))
+ else:
+ lines += self.parseExpression(node.init, linked_node)
# while (
lines.append(self.asm.noop(lbl_for))
if node.cond: # if no condition, it's should evaluate to true
@@ -1088,14 +1093,19 @@ class Parse(object):
lines += lines_stmts
lines.append(self.asm.noop(switch_end))
return lines
+ def parseDeclList(self, linked_node):
+ lines = []
+ for cn in linked_node.decls:
+ lines += self.parseStatement(cn, linked_node)
def parseStatement(self, node, parent_linked_node, level_increment=False):
linked_node = LinkedNode(node, parent_linked_node, level_increment=level_increment)
self.asm.level = linked_node.level
lines = []
- if linked_node.type in ("Compound", "If", "Return", "DoWhile", "While", "For",
- "Decl", "FuncDef", "Break", "Continue", "EmptyStatement", "Switch"):
+ if linked_node.type in ("Compound", "If", "Return", "DoWhile", "While",
+ "For", "Decl", "FuncDef", "Break", "Continue",
+ "EmptyStatement", "Switch", "DeclList"):
lines += getattr(self, "parse" + linked_node.type)(linked_node)
- elif linked_node.type in ("FuncDecl", "ArrayDecl", "Case", "DeclList",
+ elif linked_node.type in ("FuncDecl", "ArrayDecl", "Case",
"Default", "EllipsisParam",# (int a, ...)
"Enum", # enum type
"Enumerator", # enum value