summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2011-11-27 11:40:15 +0000
committerPeter Wu <lekensteyn@gmail.com>2011-11-27 11:40:15 +0000
commitd86d01eb11da9a736dc708ceb0341cdc24526a6a (patch)
tree7cde72b2d1ebe0611c1903a675e3aa41aeb62a5b
parent9fab57d4267770c5dd36524445c915e76b2d606a (diff)
downloadpp2cc-d86d01eb11da9a736dc708ceb0341cdc24526a6a.tar.gz
Support while loops
-rwxr-xr-xpp2cc.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/pp2cc.py b/pp2cc.py
index 040f7c9..38858d1 100755
--- a/pp2cc.py
+++ b/pp2cc.py
@@ -710,6 +710,21 @@ class Parse(object):
lines += self.parseStatement(node.cond, linked_node)
lines.append(self.asm.branch_op("BNE", lbl_do))
return lines
+ def parseWhile(self, linked_node):
+ node = linked_node.node
+ lbl_while = self.uniqLbl("while")
+ lbl_while_end = self.uniqLbl("whileEnd")
+ # while (
+ lines = [self.asm.noop(lbl_while)]
+ # ..
+ lines += self.parseStatement(node.cond, linked_node)
+ lines.append(self.asm.branch_op("BEQ", lbl_while_end))
+ # ){..
+ lines += self.parseStatement(node.stmt, linked_node)
+ lines.append(self.asm.branch_op("BRA", lbl_while))
+ # }
+ lines.append(self.asm.noop(lbl_while_end))
+ return lines
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
@@ -726,12 +741,10 @@ class Parse(object):
raise RuntimeError("Not implemented for type " + repr(node))
elif isinstance(node, c_ast.PtrDecl):
raise RuntimeError("Not implemented for type " + repr(node))
- elif isinstance(node, c_ast.While):
- raise RuntimeError("Not implemented for type " + repr(node))
else:
for entry in ("Compound", "BinaryOp", "If", "Constant", "ID",
"Return", "FuncCall", "UnaryOp", "Cast",
- "TernaryOp", "DoWhile"):
+ "TernaryOp", "DoWhile", "While"):
if isinstance(node, getattr(c_ast, entry)):
lines += getattr(self, "parse" + entry)(linked_node)
break