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 statements Any underlying statements. 31 expr Any contributing expression. 32 name Any name involved (variable or attribute). 33 code Any code grouped by this node. 34 ref Any reference to (for example) subprograms. 35 """ 36 37 def __init__(self, original=None, **kw): 38 self.original = original 39 for name, value in kw.items(): 40 setattr(self, name, value) 41 42 def __repr__(self): 43 if hasattr(self, "name"): 44 return "%s '%s' (at %x)" % (self.__class__, self.name, id(self)) 45 if hasattr(self, "index"): 46 return "%s (%d) (at %x)" % (self.__class__, self.index, id(self)) 47 elif hasattr(self, "value"): 48 return "%s %s (at %x)" % (self.__class__, repr(self.value), id(self)) 49 elif hasattr(self, "ref"): 50 return "%s '%s' (at %x)" % (self.__class__, self.ref.name, id(self)) 51 else: 52 return "%s (at %x)" % (self.__class__, id(self)) 53 54 def _pprint(self, indent, continuation, s): 55 if continuation: 56 print (" " * max(0, indent - len(continuation))) + continuation + s 57 else: 58 print (" " * indent) + s 59 60 def pprint(self, indent=0, continuation=None): 61 self._pprint(indent, continuation, repr(self)) 62 63 # Show other details. 64 65 if hasattr(self, "params"): 66 for name, default in self.params: 67 self._pprint(indent + 2, "( ", "%s -> %s" % (name, default)) 68 if getattr(self, "acquire_locals", 0): 69 self._pprint(indent + 2, "( ", "acquiring locals") 70 if hasattr(self, "spec"): 71 self.spec.pprint(indent + 2, "E ") 72 if hasattr(self, "test"): 73 self.test.pprint(indent + 2, "? ") 74 for attr in "code", "tests", "body", "handlers", "else_", "finally_": 75 if hasattr(self, attr) and getattr(self, attr): 76 self._pprint(indent, "", "{ (%s)" % attr) 77 for node in getattr(self, attr): 78 node.pprint(indent + 2) 79 self._pprint(indent, "", "}") 80 if hasattr(self, "expr"): 81 self.expr.pprint(indent + 2, "- ") 82 if hasattr(self, "nodes"): 83 for node in self.nodes: 84 node.pprint(indent + 2, "- ") 85 if hasattr(self, "lvalue"): 86 self.lvalue.pprint(indent + 2, "= ") 87 if hasattr(self, "args"): 88 for arg in self.args: 89 arg.pprint(indent + 2, "( ") 90 91 class Module(Node): "A Python module." 92 class Subprogram(Node): "A subprogram: functions, methods and loops." 93 class Class(Node): "A Python class." 94 class Pass(Node): "A placeholder node corresponding to pass." 95 class Invoke(Node): "A function, method or loop invocation." 96 class Return(Node): "Return an evaluated expression." 97 class Assign(Node): "A grouping node for assignment-related operations." 98 class Keyword(Node): "A grouping node for keyword arguments." 99 class Global(Node): "A global name designator." 100 class LoadTemp(Node): "Load a previously-stored temporary value." 101 class LoadName(Node): "Load a named object." 102 class LoadAttr(Node): "Load an object attribute." 103 class LoadConst(Node): "Load a constant." 104 class LoadRef(Node): "Load a reference, typically a subprogram." 105 class LoadExc(Node): "Load a handled exception." 106 class StoreTemp(Node): "Store a temporary value." 107 class StoreName(Node): "Associate a name with an object." 108 class StoreAttr(Node): "Associate an object's attribute with a value." 109 class ReleaseTemp(Node): "Release a temporary value." 110 class If(Node): "A multitest conditional node." 111 class Conditional(Node): "A conditional node consisting of a test and outcomes." 112 class Try(Node): "A try...except...else...finally grouping node." 113 class Except(Node): "An exception handler node." 114 class And(Node): "A conjunction of expressions." 115 class Or(Node): "A disjunction of expressions." 116 class Not(Node): "A negation of an expression." 117 118 # vim: tabstop=4 expandtab shiftwidth=4