[pypy-svn] rev 2267 - in pypy/trunk/src/pypy/annotation: . test
hpk at codespeak.net
hpk at codespeak.net
Wed Nov 26 15:18:24 CET 2003
Author: hpk
Date: Wed Nov 26 15:18:24 2003
New Revision: 2267
Added:
pypy/trunk/src/pypy/annotation/__init__.py
pypy/trunk/src/pypy/annotation/annset.py (contents, props changed)
- copied, changed from rev 2265, pypy/trunk/src/pypy/translator/annheap.py
pypy/trunk/src/pypy/annotation/model.py
pypy/trunk/src/pypy/annotation/test/autopath.py (props changed)
- copied unchanged from rev 2265, pypy/trunk/src/pypy/translator/test/autopath.py
Modified:
pypy/trunk/src/pypy/annotation/test/test_annset.py
Log:
intermediate checkin (armin, holger)
the annset-tests pass.
Added: pypy/trunk/src/pypy/annotation/__init__.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/annotation/__init__.py Wed Nov 26 15:18:24 2003
@@ -0,0 +1 @@
+#
Copied: pypy/trunk/src/pypy/annotation/annset.py (from rev 2265, pypy/trunk/src/pypy/translator/annheap.py)
==============================================================================
--- pypy/trunk/src/pypy/translator/annheap.py (original)
+++ pypy/trunk/src/pypy/annotation/annset.py Wed Nov 26 15:18:24 2003
@@ -1,15 +1,32 @@
from __future__ import generators
import types
-from annotation import Annotation, XCell, XConstant, nothingyet
+from model import Annotation, SomeValue, blackholevalue
-
-class AnnotationHeap:
- """An annotation heap is a (large) family of Annotations."""
+class AnnotationSet:
+ """An annotation set is a (large) family of Annotations."""
# XXX STORED AS A PLAIN LIST, THE COMPLEXITY IS PLAINLY WRONG
def __init__(self, annlist=[]):
self.annlist = list(annlist) # List of annotations
+ self._shared = {}
+
+ def getsharelist(self, someval):
+ return self._shared.get(someval, [someval])
+
+ def setshared(self, someval1, someval2):
+ list1 = self.getsharelist(someval1)
+ list2 = self.getsharelist(someval2)
+ newlist = list1 + list2
+ for someval in newlist:
+ self._shared[someval] = newlist
+
+ def isshared(self, someval1, someval2):
+ return (self._shared.get(someval1, someval1) is
+ self._shared.get(someval2, someval2))
+
+ def tempid(self, someval):
+ return id(self.getsharelist(someval)[0])
def dump(self): # debugging
for ann in self.enumerate():
@@ -21,6 +38,47 @@
__iter__ = enumerate
+ def query(self, query, *querylist):
+ # slightly limited implementation for ease of coding :-)
+ results = []
+ for match in self._getmatches(query):
+ # does the returned match also agree with the other queries?
+ for queryann in querylist:
+ boundquery = queryann.copy(renameargs={Ellipsis: match})
+ if not self.contains(boundquery):
+ break
+ else:
+ results.append(match)
+ return results
+
+ def contains(self, checkann):
+ for ann in self.annlist:
+ if ann.predicate == checkann.predicate:
+ for a1, a2 in zip(ann.args, checkann.args):
+ if not self.isshared(a1, a2):
+ break
+ else:
+ return True
+ return False
+
+ def _getmatches(self, queryann):
+ assert queryann.args.count(Ellipsis) == 1, (
+ "sorry, the algorithm is a bit too naive for this case")
+ queryarg = queryann.args.index(Ellipsis)
+ testindices = range(queryann.predicate.arity)
+ del testindices[queryarg]
+ for ann in self.annlist:
+ if ann.predicate == queryann.predicate:
+ for i in testindices:
+ if not self.isshared(ann.args[i], queryann.args[i]):
+ break
+ else:
+ yield ann.args[queryarg]
+
+# do you have an intersection algo somewherE? no mmmh we need to use
+# self._shared too...
+
+'''
def simplify(self, kill=[]):
"""Kill annotations in the list, and recursively all the annotations
that depend on them, and simplify the resulting heap to remove
@@ -108,19 +166,7 @@
# apply changes
self.simplify(kill=deleting)
return newcell
-
-
-def rename(ann, oldcell, newcell):
- "Make a copy of 'ann' in which 'oldcell' has been replaced by 'newcell'."
- args = []
- for a in ann.args:
- if a == oldcell:
- a = newcell
- args.append(a)
- a = ann.result
- if a == oldcell:
- a = newcell
- return Annotation(ann.opname, args, a)
+'''
class Transaction:
Added: pypy/trunk/src/pypy/annotation/model.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/annotation/model.py Wed Nov 26 15:18:24 2003
@@ -0,0 +1,60 @@
+
+class SomeValue:
+ pass
+
+# a conventional value for representing 'all Annotations match this one'
+blackholevalue = SomeValue()
+
+def debugname(someval, name=None, _seen = {id(blackholevalue): 'blackholevalue'}):
+ """ return a simple name for a SomeValue. """
+ try:
+ return _seen[id(someval)]
+ except KeyError:
+ if name is None:
+ name = "X%d" % len(seen)
+ _seen[id(someval)] = name
+ return name
+
+class Predicate:
+ def __init__(self, name, arity):
+ self.name = name
+ self.arity = arity
+ def __getitem__(self, args):
+ if self.arity == 1:
+ args = (args,)
+ return Annotation(self, *args)
+
+class ann:
+ add = Predicate('add', 3)
+ snuff = Predicate('snuff', 2) # for testing, to remove :-)
+
+class Annotation:
+ """An Annotation asserts something about SomeValues.
+ It is a Predicate applied to some arguments. """
+
+ def __init__(self, predicate, *args):
+ self.predicate = predicate # the operation or predicate
+ self.args = list(args) # list of SomeValues
+ assert len(args) == predicate.arity
+ # note that for predicates that are simple operations like
+ # op.add, the result is stored as the last argument.
+ for someval in args:
+ assert someval is Ellipsis or isinstance(someval, SomeValue) # bug catcher
+
+ def copy(self, renameargs={}):
+ args = [renameargs.get(arg, arg) for arg in self.args]
+ return Annotation(self.predicate, *args)
+
+ def __eq__(self, other):
+ return (self.__class__ is other.__class__ and
+ self.predicate == other.predicate and
+ self.args == other.args)
+
+ def __ne__(self, other):
+ return not (self == other)
+
+ def __repr__(self):
+ return "Annotation(%s, %s)" % (
+ self.predicate, ", ".join(map(repr, self.args)))
+
+
Modified: pypy/trunk/src/pypy/annotation/test/test_annset.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/test/test_annset.py (original)
+++ pypy/trunk/src/pypy/annotation/test/test_annset.py Wed Nov 26 15:18:24 2003
@@ -2,13 +2,9 @@
import autopath
from pypy.tool import test
-from pypy.annotation.model import Annotation, SomeValue
+from pypy.annotation.model import ann, SomeValue
+from pypy.annotation.annset import AnnotationSet
-# to avoid quoting of strings
-class _op:
- def __getattr__(self, name):
- return name
-op = _op()
class TestAnnotationSet(test.IntTestCase):
@@ -35,30 +31,30 @@
id1 = a.tempid(c1)
id2 = a.tempid(c2)
self.assertNotEquals(id1, id2)
-
+
def test_query_one_annotation_arg(self):
c1,c2,c3 = SomeValue(), SomeValue(), SomeValue()
- lst = [Annotation(op.add, c1, c3, c2)]
+ lst = [ann.add[c1, c3, c2]]
a = AnnotationSet(lst)
- c = a.query(Annotation(op.add, c1, c3, QUERYARG))
+ c = a.query(ann.add[c1, c3, ...])
self.assertEquals(c, [c2])
- c = a.query(Annotation(op.add, c1, QUERYARG, c2))
+ c = a.query(ann.add[c1, ..., c2])
self.assertEquals(c, [c3])
- c = a.query(Annotation(op.add, QUERYARG, c3, c2))
+ c = a.query(ann.add[..., c3, c2])
self.assertEquals(c, [c1])
- c = a.query(Annotation(op.add, QUERYARG, c1, c2))
+ c = a.query(ann.add[..., c1, c2])
self.assertEquals(c, [])
def test_query_multiple_annotations(self):
c1,c2,c3 = SomeValue(), SomeValue(), SomeValue()
lst = [
- Annotation(op.add, c1, c3, c2),
- Annotation(op.something, c2, c3)
+ ann.add[c1, c3, c2],
+ ann.snuff[c2, c3],
]
a = AnnotationSet(lst)
- c = a.query(Annotation(op.add, c1, c3, QUERYARG),
- Annotation(op.something, QUERYARG, c3))
+ c = a.query(ann.add[c1, c3, ...],
+ ann.snuff[..., c3])
self.assertEquals(c, [c2])
if __name__ == '__main__':
More information about the Pypy-commit
mailing list