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