From 0086c2a0a421df52be49817293fcdab0221e010f Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Mon, 28 Nov 2011 13:11:50 +0000 Subject: Set the type property of LinkedNode to the node type --- pp2cc.py | 70 +++++++++++++++++++++++----------------------------------------- 1 file changed, 25 insertions(+), 45 deletions(-) diff --git a/pp2cc.py b/pp2cc.py index 79c2e3e..97e091d 100755 --- a/pp2cc.py +++ b/pp2cc.py @@ -174,6 +174,7 @@ class LinkedNode(object): self.function = None self.break_label = None self.continue_label = None + self.type = type(node).__name__ self.level = 0 # inherit level from parent if parent: @@ -183,7 +184,7 @@ class LinkedNode(object): def incrementLevel(self): self.level += 1 def getFunctionNode(self): - if isinstance(self.node, c_ast.FuncDef): + if self.type == "FuncDef": return self if self.parent: assert isinstance(self.parent, LinkedNode), "parent is not a LinkedNode!" @@ -923,35 +924,19 @@ class Parse(object): linked_node = LinkedNode(node, parent_linked_node, level_increment=level_increment) self.asm.level = linked_node.level lines = [] - - for entry in ("Compound", "If", "Return", "DoWhile", "While", "For", + if linked_node.type in ("Compound", "If", "Return", "DoWhile", "While", "For", "Decl", "FuncDef", "Break", "Continue", "EmptyStatement"): - if isinstance(node, getattr(c_ast, entry)): - lines += getattr(self, "parse" + entry)(linked_node) - break + lines += getattr(self, "parse" + linked_node.type)(linked_node) + elif linked_node.type in ("FuncDecl", "ArrayDecl", "Case", "DeclList", + "Default", "EllipsisParam",# (int a, ...) + "Enum", # enum type + "Enumerator", # enum value + "EnumeratorList", # list of enum values + "FuncDecl", "Goto", "Label", "ParamList", "PtrDecl", "Struct", + "TypeDecl", "Typedef", "Switch", "Union"): + raise RuntimeError("Not implemented for type " + repr(node)) else: - if (isinstance(node, c_ast.FuncDecl) or - isinstance(node, c_ast.ArrayDecl) or - isinstance(node, c_ast.Case) or - isinstance(node, c_ast.DeclList) or - isinstance(node, c_ast.Default) or - isinstance(node, c_ast.EllipsisParam) or# (int a, ...) - isinstance(node, c_ast.Enum) or # enum type - isinstance(node, c_ast.Enumerator) or # enum value - isinstance(node, c_ast.EnumeratorList) or # list of enum values - isinstance(node, c_ast.FuncDecl) or - isinstance(node, c_ast.Goto) or - isinstance(node, c_ast.Label) or - isinstance(node, c_ast.ParamList) or - isinstance(node, c_ast.PtrDecl) or - isinstance(node, c_ast.Struct) or - isinstance(node, c_ast.TypeDecl) or#? - isinstance(node, c_ast.Typedef) or#? - isinstance(node, c_ast.Switch) or - isinstance(node, c_ast.Union)): - raise RuntimeError("Not implemented for type " + repr(node)) - else: - lines += self.parseExpression(node, parent_linked_node, level_increment) + lines += self.parseExpression(node, parent_linked_node, level_increment) return lines # currently, there is no difference between an expression and statement. This might @@ -961,25 +946,20 @@ class Parse(object): self.asm.level = linked_node.level lines = [] - for entry in ("ID", "Constant", "UnaryOp", "FuncCall", "Cast", + if linked_node.type in ("ID", "Constant", "UnaryOp", "FuncCall", "Cast", "BinaryOp", "TernaryOp", "Assignment", "ExprList"): - if isinstance(node, getattr(c_ast, entry)): - lines += getattr(self, "parse" + entry)(linked_node) - break + lines += getattr(self, "parse" + linked_node.type)(linked_node) + elif linked_node.type in ("Compound", "If", "Return", "DoWhile", + "While", "For", "Decl", "FuncDef", "Break", "Continue", + "EmptyStatement"): + raise RuntimeError("A statement is used in expression context") + elif linked_node.type in ("ArrayRef", + "CompoundLiteral",# won't be supported + "IdentifierType", "NamedInitializer",# attributes, remove + "StructRef", "Typename"): + raise RuntimeError("Not implemented for expression type " + repr(node)) else: - for entry in ("Compound", "If", "Return", "DoWhile", "While", "For", - "Decl", "FuncDef", "Break", "Continue", "EmptyStatement"): - if isinstance(node, getattr(c_ast, entry)): - raise RuntimeError("A statement is used in expression context") - if (isinstance(node, c_ast.ArrayRef) or# - isinstance(node, c_ast.CompoundLiteral) or# won't be supported - isinstance(node, c_ast.IdentifierType) or#attribute? remove - isinstance(node, c_ast.NamedInitializer) or#attribute, remove - isinstance(node, c_ast.StructRef) or# - isinstance(node, c_ast.Typename)): - raise RuntimeError("Not implemented for expression type " + repr(node)) - else: - raise RuntimeError("Not implemented expression and unknown type: " + repr(node)) + raise RuntimeError("Not implemented expression and unknown type: " + repr(node)) return lines if __name__ == "__main__": -- cgit v1.2.1