From d86d01eb11da9a736dc708ceb0341cdc24526a6a Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sun, 27 Nov 2011 11:40:15 +0000 Subject: Support while loops --- pp2cc.py | 19 ++++++++++++++++--- 1 file 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 -- cgit v1.2.1