[pypy-svn] rev 1749 - in pypy/trunk/src/pypy/translator: . test
sanxiyn at codespeak.net
sanxiyn at codespeak.net
Sun Oct 12 06:51:35 CEST 2003
Author: sanxiyn
Date: Sun Oct 12 06:51:34 2003
New Revision: 1749
Modified:
pypy/trunk/src/pypy/translator/annotation.py
pypy/trunk/src/pypy/translator/gencl.py
pypy/trunk/src/pypy/translator/test/test_cltrans.py
pypy/trunk/src/pypy/translator/translator.py
Log:
gencl: Handles op_not_ by means of emit_truth_test.
test_cltrans: Test for above change.
annotation: Consider_op_not_.
translator: Updated docstring and example.
You can test annotation working by examine my_bool
with translator.py. It's implemented as "not not",
and gencl wouldn't use typecase for second not, for it
knows it is already boolean.
Modified: pypy/trunk/src/pypy/translator/annotation.py
==============================================================================
--- pypy/trunk/src/pypy/translator/annotation.py (original)
+++ pypy/trunk/src/pypy/translator/annotation.py Sun Oct 12 06:51:34 2003
@@ -104,6 +104,9 @@
consider_op_and_ = consider_op_add # don't forget the trailing '_'
# XXX add more
+ def consider_op_not_(self, op, annotations):
+ annotations.set_type(op.result, bool)
+
# XXX give them Bool result type
consider_op_lt = consider_op_add
consider_op_le = consider_op_add
Modified: pypy/trunk/src/pypy/translator/gencl.py
==============================================================================
--- pypy/trunk/src/pypy/translator/gencl.py (original)
+++ pypy/trunk/src/pypy/translator/gencl.py Sun Oct 12 06:51:34 2003
@@ -9,6 +9,7 @@
class Op:
def __init__(self, gen, op):
+ self.gen = gen
self.str = gen.str
self.opname = op.opname
self.args = op.args
@@ -17,7 +18,9 @@
if self.opname in self.binary_ops:
self.op_binary(self.opname)
else:
- self.op_default()
+ default = self.op_default
+ meth = getattr(self, "op_" + self.opname, default)
+ meth()
binary_ops = {
"add": "+",
"inplace_add": "+", # weird, but it works
@@ -25,13 +28,19 @@
"lt": "<",
"eq": "=",
}
- def op_default(self):
- print "; Op", self.opname, "is missing"
def op_binary(self, op):
s = self.str
result, (arg1, arg2) = self.result, self.args
cl_op = self.binary_ops[op]
print "(setq", s(result), "(", cl_op, s(arg1), s(arg2), "))"
+ def op_not_(self):
+ s = self.str
+ result, (arg1,) = self.result, self.args
+ print "(setq", s(result), "(not"
+ self.gen.emit_truth_test(arg1)
+ print "))"
+ def op_default(self):
+ print "; Op", self.opname, "is missing"
class GenCL:
def __init__(self, fun):
Modified: pypy/trunk/src/pypy/translator/test/test_cltrans.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_cltrans.py (original)
+++ pypy/trunk/src/pypy/translator/test/test_cltrans.py Sun Oct 12 06:51:34 2003
@@ -56,5 +56,14 @@
self.assertEquals(cl_perfect(24), False)
self.assertEquals(cl_perfect(28), True)
+ #___________________________________
+ def my_bool(x):
+ return not not x
+ def test_bool(self):
+ cl_bool = self.cl_func(self.my_bool)
+ self.assertEquals(cl_bool(0), False)
+ self.assertEquals(cl_bool(42), True)
+ self.assertEquals(cl_bool(True), True)
+
if __name__ == '__main__':
test.main()
Modified: pypy/trunk/src/pypy/translator/translator.py
==============================================================================
--- pypy/trunk/src/pypy/translator/translator.py (original)
+++ pypy/trunk/src/pypy/translator/translator.py Sun Oct 12 06:51:34 2003
@@ -1,32 +1,27 @@
"""
-Glue script putting together the pieces of the translator.
+Glue script putting together the various pieces of the translator.
Can also be used for interactive testing of the translator, when run as:
python -i translator.py
Example:
- def f1(x):
- total = 0
- for i in range(1, x+1):
- total = total + i
- return total
-
- t = Translator(f1)
- t.gv() # show the control flow graph -- requires 'dot' and 'gv'
-
- t.simplify()
- t.gv()
- print t.pyrex()
-
- a = t.annotate([int]) # the list is the input args types
- print t.pyrex()
-
- a.simplify() # simplifications done by the Annotator
- print t.pyrex()
+ t = Translator(func)
+ t.gv() # control flow graph
- f = t.compile()
- print f(10)
+ print t.source() # original source
+ print t.pyrex() # pyrex translation
+ print t.cl() # common lisp translation
+
+ t.simplify() # flow graph simplification
+ a = t.annotate([int]) # pass the list of args types
+ a.simplify() # simplification by annotator
+
+ f = t.compile() # pyrex compilation
+ assert f(arg) == func(arg)
+
+Some functions will be provided for the benefit of interactive testing.
+Currently there are my_bool and my_range.
"""
import autopath
@@ -97,18 +92,16 @@
if __name__ == '__main__':
- def f1(x):
- total = 0
- for i in range(1, x+1):
- total = total + i
- return total
-
- def f2(x):
- total = 0
- while x > 0:
- total = total + x
- x = x - 1
- return total
+ def my_bool(x):
+ return not not x
+
+ def my_range(i):
+ lst = []
+ while i > 0:
+ i = i - 1
+ lst.append(i)
+ lst.reverse()
+ return lst
print __doc__
More information about the Pypy-commit
mailing list