1.1 --- a/common.py Sat Sep 03 22:37:23 2016 +0200
1.2 +++ b/common.py Sat Sep 03 22:38:21 2016 +0200
1.3 @@ -23,6 +23,7 @@
1.4 from errors import *
1.5 from os import listdir, makedirs, remove
1.6 from os.path import exists, isdir, join, split
1.7 +from results import ConstantValueRef, LiteralSequenceRef, NameRef
1.8 import compiler
1.9
1.10 class CommonOutput:
1.11 @@ -228,6 +229,12 @@
1.12
1.13 def get_literal_reference(self, name, ref, items, cls):
1.14
1.15 + """
1.16 + Return a literal reference for the given type 'name', literal 'ref',
1.17 + node 'items' and employing the given 'cls' as the class of the returned
1.18 + reference object.
1.19 + """
1.20 +
1.21 # Construct an invocation using the items as arguments.
1.22
1.23 typename = "$L%s" % name
1.24 @@ -465,6 +472,8 @@
1.25 self.next_iterator()
1.26 self.process_structure_node(node)
1.27
1.28 + # Node rewriting, without processing.
1.29 +
1.30 def convert_ifexp_node(self, n):
1.31
1.32 """
1.33 @@ -580,6 +589,8 @@
1.34 compiler.ast.CallFunc(
1.35 compiler.ast.Getattr(compiler.ast.Name(temp), "append"), [expr]))
1.36
1.37 + # More node processing.
1.38 +
1.39 def process_literal_sequence_node(self, n, name, ref, cls):
1.40
1.41 """
1.42 @@ -795,114 +806,6 @@
1.43 l.append(arg)
1.44 return l
1.45
1.46 -# Classes representing inspection and translation observations.
1.47 -
1.48 -class Result:
1.49 -
1.50 - "An abstract expression result."
1.51 -
1.52 - def is_name(self):
1.53 - return False
1.54 - def get_origin(self):
1.55 - return None
1.56 -
1.57 -class NameRef(Result):
1.58 -
1.59 - "A reference to a name."
1.60 -
1.61 - def __init__(self, name, expr=None):
1.62 - self.name = name
1.63 - self.expr = expr
1.64 -
1.65 - def is_name(self):
1.66 - return True
1.67 -
1.68 - def reference(self):
1.69 - return None
1.70 -
1.71 - def final(self):
1.72 - return None
1.73 -
1.74 - def __repr__(self):
1.75 - return "NameRef(%r, %r)" % (self.name, self.expr)
1.76 -
1.77 -class LocalNameRef(NameRef):
1.78 -
1.79 - "A reference to a local name."
1.80 -
1.81 - def __init__(self, name, number):
1.82 - NameRef.__init__(self, name)
1.83 - self.number = number
1.84 -
1.85 - def __repr__(self):
1.86 - return "LocalNameRef(%r, %r)" % (self.name, self.number)
1.87 -
1.88 -class ResolvedNameRef(NameRef):
1.89 -
1.90 - "A resolved name-based reference."
1.91 -
1.92 - def __init__(self, name, ref, expr=None):
1.93 - NameRef.__init__(self, name, expr)
1.94 - self.ref = ref
1.95 -
1.96 - def reference(self):
1.97 - return self.ref
1.98 -
1.99 - def get_name(self):
1.100 - return self.ref and self.ref.get_name() or None
1.101 -
1.102 - def get_origin(self):
1.103 - return self.ref and self.ref.get_origin() or None
1.104 -
1.105 - def static(self):
1.106 - return self.ref and self.ref.static() or None
1.107 -
1.108 - def final(self):
1.109 - return self.ref and self.ref.final() or None
1.110 -
1.111 - def has_kind(self, kinds):
1.112 - return self.ref and self.ref.has_kind(kinds)
1.113 -
1.114 - def __repr__(self):
1.115 - return "ResolvedNameRef(%r, %r, %r)" % (self.name, self.ref, self.expr)
1.116 -
1.117 -class ConstantValueRef(ResolvedNameRef):
1.118 -
1.119 - "A constant reference representing a single literal value."
1.120 -
1.121 - def __init__(self, name, ref, value, number=None):
1.122 - ResolvedNameRef.__init__(self, name, ref)
1.123 - self.value = value
1.124 - self.number = number
1.125 -
1.126 - def __repr__(self):
1.127 - return "ConstantValueRef(%r, %r, %r, %r)" % (self.name, self.ref, self.value, self.number)
1.128 -
1.129 -class InstanceRef(Result):
1.130 -
1.131 - "An instance reference."
1.132 -
1.133 - def __init__(self, ref):
1.134 - self.ref = ref
1.135 -
1.136 - def reference(self):
1.137 - return self.ref
1.138 -
1.139 - def __repr__(self):
1.140 - return "InstanceRef(%r)" % self.ref
1.141 -
1.142 -class LiteralSequenceRef(ResolvedNameRef):
1.143 -
1.144 - "A reference representing a sequence of values."
1.145 -
1.146 - def __init__(self, name, ref, node, items=None):
1.147 - ResolvedNameRef.__init__(self, name, ref)
1.148 - self.node = node
1.149 - self.items = items
1.150 -
1.151 - def __repr__(self):
1.152 - return "LiteralSequenceRef(%r, %r, %r, %r)" % (self.name, self.ref, self.node, self.items)
1.153 -
1.154 # Dictionary utilities.
1.155
1.156 def init_item(d, key, fn):
1.157 @@ -996,18 +899,31 @@
1.158 # Attribute chain decoding.
1.159
1.160 def get_attrnames(attrnames):
1.161 +
1.162 + """
1.163 + Split the qualified attribute chain 'attrnames' into its components,
1.164 + handling special attributes starting with "#" that indicate type
1.165 + conformance.
1.166 + """
1.167 +
1.168 if attrnames.startswith("#"):
1.169 return [attrnames]
1.170 else:
1.171 return attrnames.split(".")
1.172
1.173 def get_attrname_from_location(location):
1.174 +
1.175 + """
1.176 + Extract the first attribute from the attribute names employed in a
1.177 + 'location'.
1.178 + """
1.179 +
1.180 path, name, attrnames, access = location
1.181 return get_attrnames(attrnames)[0]
1.182
1.183 # Useful data.
1.184
1.185 -predefined_constants = "Ellipsis", "False", "None", "NotImplemented", "True"
1.186 +predefined_constants = "False", "None", "NotImplemented", "True"
1.187
1.188 operator_functions = {
1.189
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/results.py Sat Sep 03 22:38:21 2016 +0200
2.3 @@ -0,0 +1,158 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +Result abstractions.
2.8 +
2.9 +Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
2.10 +
2.11 +This program is free software; you can redistribute it and/or modify it under
2.12 +the terms of the GNU General Public License as published by the Free Software
2.13 +Foundation; either version 3 of the License, or (at your option) any later
2.14 +version.
2.15 +
2.16 +This program is distributed in the hope that it will be useful, but WITHOUT
2.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
2.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
2.19 +details.
2.20 +
2.21 +You should have received a copy of the GNU General Public License along with
2.22 +this program. If not, see <http://www.gnu.org/licenses/>.
2.23 +"""
2.24 +
2.25 +# Classes representing inspection and translation observations.
2.26 +
2.27 +class Result:
2.28 +
2.29 + "An abstract expression result."
2.30 +
2.31 + def is_name(self):
2.32 + return False
2.33 + def get_origin(self):
2.34 + return None
2.35 +
2.36 +class AccessRef(Result):
2.37 +
2.38 + """
2.39 + A reference to an attribute access that is generally only returned from a
2.40 + processed access for possible initialiser resolution for assignments.
2.41 + """
2.42 +
2.43 + def __init__(self, original_name, attrnames, number):
2.44 + self.original_name = original_name
2.45 + self.attrnames = attrnames
2.46 + self.number = number
2.47 +
2.48 + def reference(self):
2.49 + return None
2.50 +
2.51 + def __repr__(self):
2.52 + return "AccessRef(%r, %r, %r)" % (self.original_name, self.attrnames, self.number)
2.53 +
2.54 +class InvocationRef(Result):
2.55 +
2.56 + "An invocation of a name reference."
2.57 +
2.58 + def __init__(self, name_ref):
2.59 + self.name_ref = name_ref
2.60 +
2.61 + def __repr__(self):
2.62 + return "InvocationRef(%r)" % self.name_ref
2.63 +
2.64 +class NameRef(Result):
2.65 +
2.66 + "A reference to a name."
2.67 +
2.68 + def __init__(self, name, expr=None):
2.69 + self.name = name
2.70 + self.expr = expr
2.71 +
2.72 + def is_name(self):
2.73 + return True
2.74 +
2.75 + def reference(self):
2.76 + return None
2.77 +
2.78 + def final(self):
2.79 + return None
2.80 +
2.81 + def __repr__(self):
2.82 + return "NameRef(%r, %r)" % (self.name, self.expr)
2.83 +
2.84 +class LocalNameRef(NameRef):
2.85 +
2.86 + "A reference to a local name."
2.87 +
2.88 + def __init__(self, name, number):
2.89 + NameRef.__init__(self, name)
2.90 + self.number = number
2.91 +
2.92 + def __repr__(self):
2.93 + return "LocalNameRef(%r, %r)" % (self.name, self.number)
2.94 +
2.95 +class ResolvedNameRef(NameRef):
2.96 +
2.97 + "A resolved name-based reference."
2.98 +
2.99 + def __init__(self, name, ref, expr=None):
2.100 + NameRef.__init__(self, name, expr)
2.101 + self.ref = ref
2.102 +
2.103 + def reference(self):
2.104 + return self.ref
2.105 +
2.106 + def get_name(self):
2.107 + return self.ref and self.ref.get_name() or None
2.108 +
2.109 + def get_origin(self):
2.110 + return self.ref and self.ref.get_origin() or None
2.111 +
2.112 + def static(self):
2.113 + return self.ref and self.ref.static() or None
2.114 +
2.115 + def final(self):
2.116 + return self.ref and self.ref.final() or None
2.117 +
2.118 + def has_kind(self, kinds):
2.119 + return self.ref and self.ref.has_kind(kinds)
2.120 +
2.121 + def __repr__(self):
2.122 + return "ResolvedNameRef(%r, %r, %r)" % (self.name, self.ref, self.expr)
2.123 +
2.124 +class ConstantValueRef(ResolvedNameRef):
2.125 +
2.126 + "A constant reference representing a single literal value."
2.127 +
2.128 + def __init__(self, name, ref, value, number=None):
2.129 + ResolvedNameRef.__init__(self, name, ref)
2.130 + self.value = value
2.131 + self.number = number
2.132 +
2.133 + def __repr__(self):
2.134 + return "ConstantValueRef(%r, %r, %r, %r)" % (self.name, self.ref, self.value, self.number)
2.135 +
2.136 +class InstanceRef(Result):
2.137 +
2.138 + "An instance reference."
2.139 +
2.140 + def __init__(self, ref):
2.141 + self.ref = ref
2.142 +
2.143 + def reference(self):
2.144 + return self.ref
2.145 +
2.146 + def __repr__(self):
2.147 + return "InstanceRef(%r)" % self.ref
2.148 +
2.149 +class LiteralSequenceRef(ResolvedNameRef):
2.150 +
2.151 + "A reference representing a sequence of values."
2.152 +
2.153 + def __init__(self, name, ref, node, items=None):
2.154 + ResolvedNameRef.__init__(self, name, ref)
2.155 + self.node = node
2.156 + self.items = items
2.157 +
2.158 + def __repr__(self):
2.159 + return "LiteralSequenceRef(%r, %r, %r, %r)" % (self.name, self.ref, self.node, self.items)
2.160 +
2.161 +# vim: tabstop=4 expandtab shiftwidth=4