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