1 #!/usr/bin/env python 2 3 """ 4 Simplified AST nodes for easier type propagation and analysis. 5 6 Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> 7 8 This software is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public License as 10 published by the Free Software Foundation; either version 2 of 11 the License, or (at your option) any later version. 12 13 This software is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public 19 License along with this library; see the file LICENCE.txt 20 If not, write to the Free Software Foundation, Inc., 21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 22 """ 23 24 class Node: 25 26 """ 27 A result node with common attributes: 28 29 original The original node from which this node was created. 30 expr Any contributing expression. 31 name Any name involved (variable or attribute). 32 code Any code grouped by this node. 33 ref Any reference to (for example) subprograms. 34 """ 35 36 def __init__(self, original=None, **kw): 37 self.original = original 38 if self.original is not None: 39 self.original._node = self 40 for name, value in kw.items(): 41 setattr(self, name, value) 42 43 def __repr__(self): 44 if hasattr(self, "name"): 45 return "%s '%s' (at %x)" % (self.__class__, self.name, id(self)) 46 if hasattr(self, "index"): 47 return "%s (%s) (at %x)" % (self.__class__, self.index, id(self)) 48 elif hasattr(self, "value"): 49 return "%s %s (at %x)" % (self.__class__, repr(self.value), id(self)) 50 elif hasattr(self, "ref"): 51 return "%s '%s' (at %x)" % (self.__class__, self.ref.name, id(self)) 52 else: 53 return "%s (at %x)" % (self.__class__, id(self)) 54 55 def _pprint(self, indent, continuation, s): 56 if continuation: 57 print (" " * max(0, indent - len(continuation))) + continuation + s 58 else: 59 print (" " * indent) + s 60 61 def pprint(self, indent=0, continuation=None): 62 self._pprint(indent, continuation, repr(self)) 63 64 # Show other details. 65 66 if hasattr(self, "params"): 67 for name, default in self.params: 68 self._pprint(indent + 2, "( ", "%s -> %s" % (name, default)) 69 if getattr(self, "acquire_locals", 0): 70 self._pprint(indent + 2, "( ", "acquiring locals") 71 if hasattr(self, "spec"): 72 self.spec.pprint(indent + 2, "E ") 73 if hasattr(self, "test"): 74 self.test.pprint(indent + 2, "? ") 75 for attr in "code", "tests", "body", "handlers", "else_", "finally_": 76 if hasattr(self, attr) and getattr(self, attr): 77 self._pprint(indent, "", "{ (%s)" % attr) 78 for node in getattr(self, attr): 79 node.pprint(indent + 2) 80 self._pprint(indent, "", "}") 81 if hasattr(self, "expr"): 82 self.expr.pprint(indent + 2, "- ") 83 if hasattr(self, "nodes"): 84 for node in self.nodes: 85 node.pprint(indent + 2, "- ") 86 if hasattr(self, "lvalue"): 87 self.lvalue.pprint(indent + 2, "= ") 88 if hasattr(self, "args"): 89 for arg in self.args: 90 arg.pprint(indent + 2, "( ") 91 92 class Module(Node): "A Python module." 93 class Subprogram(Node): "A subprogram: functions, methods and loops." 94 class Class(Node): "A Python class." 95 class Pass(Node): "A placeholder node corresponding to pass." 96 class Invoke(Node): "A function, method or loop invocation." 97 class Return(Node): "Return an evaluated expression." 98 class Assign(Node): "A grouping node for assignment-related operations." 99 class Keyword(Node): "A grouping node for keyword arguments." 100 class Global(Node): "A global name designator." 101 class Import(Node): "A module import operation." 102 class LoadTemp(Node): "Load a previously-stored temporary value." 103 class LoadName(Node): "Load a named object." 104 class LoadAttr(Node): "Load an object attribute." 105 class LoadConst(Node): "Load a constant." 106 class LoadRef(Node): "Load a reference, typically a subprogram." 107 class LoadExc(Node): "Load a handled exception." 108 class StoreTemp(Node): "Store a temporary value." 109 class StoreName(Node): "Associate a name with an object." 110 class StoreAttr(Node): "Associate an object's attribute with a value." 111 class ReleaseTemp(Node): "Release a temporary value." 112 class If(Node): "A multitest conditional node." 113 class Conditional(Node): "A conditional node consisting of a test and outcomes." 114 class Try(Node): "A try...except...else...finally grouping node." 115 class Except(Node): "An exception handler node." 116 class Raise(Node): "An exception raising node." 117 class Not(Node): "A negation of an expression." 118 119 # vim: tabstop=4 expandtab shiftwidth=4