[pypy-svn] r13404 - in pypy/dist/pypy/rpython: . test

hpk at codespeak.net hpk at codespeak.net
Tue Jun 14 20:38:13 CEST 2005


Author: hpk
Date: Tue Jun 14 20:38:12 2005
New Revision: 13404

Modified:
   pypy/dist/pypy/rpython/interp.py
   pypy/dist/pypy/rpython/test/test_interp.py
Log:
add branching and some more operations to the ll-interpreter 
(now using pypy.objspace.flow.operation although 
i am still not sure it's really worth it) 



Modified: pypy/dist/pypy/rpython/interp.py
==============================================================================
--- pypy/dist/pypy/rpython/interp.py	(original)
+++ pypy/dist/pypy/rpython/interp.py	Tue Jun 14 20:38:12 2005
@@ -20,7 +20,6 @@
         
     def setvar(self, var, val): 
         # XXX assert that val "matches" lowlevel type 
-        assert var not in self.bindings 
         self.bindings[var] = val 
 
     def getval(self, varorconst): 
@@ -63,6 +62,7 @@
     def eval_operation(self, operation): 
         g = globals()
         opname = operation.opname
+        print "considering", operation
         assert opname in g, (
                 "cannot handle operation %r yet" %(opname,))
         ophandler = g[opname]
@@ -72,15 +72,29 @@
 
 ##############################
 # primitive operations 
-import operator
+from pypy.objspace.flow.operation import FunctionByName 
+opimpls = FunctionByName.copy()
+opimpls['is_true'] = bool 
+
+def same_as(x): 
+    return x
 
 for typ in (float, int): 
     typname = typ.__name__
-    for opname in 'add', 'sub', 'mul', 'div': 
-        assert hasattr(operator, opname)
+    for opname in 'add', 'sub', 'mul', 'div', 'gt', 'lt': 
+        assert opname in opimpls 
         exec py.code.Source("""
-            def %(typname)s_%(opname)s(x,y): 
+            def %(typname)s_%(opname)s(x, y): 
                 assert isinstance(x, %(typname)s)
                 assert isinstance(y, %(typname)s)
-                return operator.%(opname)s(x, y) 
+                func = opimpls[%(opname)r]
+                return func(x, y) 
+        """ % locals()).compile()
+    for opname in 'is_true',: 
+        assert opname in opimpls 
+        exec py.code.Source("""
+            def %(typname)s_%(opname)s(x): 
+                assert isinstance(x, %(typname)s)
+                func = opimpls[%(opname)r]
+                return func(x) 
         """ % locals()).compile()

Modified: pypy/dist/pypy/rpython/test/test_interp.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_interp.py	(original)
+++ pypy/dist/pypy/rpython/test/test_interp.py	Tue Jun 14 20:38:12 2005
@@ -13,6 +13,9 @@
     #t.view()
     t.checkgraphs()
     return t
+
+#__________________________________________________________________
+# tests 
     
 def test_int_ops(): 
     t = gengraph(number_ops, [int])
@@ -26,7 +29,23 @@
     res = interp.eval_function(number_ops, [3.5])
     assert res == 4.5 
 
+def interpret(func, values): 
+    t = gengraph(func, [type(x) for x in values])
+    interp = LLInterpreter(t.flowgraphs)
+    res = interp.eval_function(func, values) 
+    return res 
 
+def test_ifs(): 
+    res = interpret(simple_ifs, [0])
+    assert res == 43 
+    res = interpret(simple_ifs, [1])
+    assert res == 42 
+
+def test_while_simple(): 
+    res = interpret(while_simple, [3])
+    assert res == 6
+    
+    
 #__________________________________________________________________
 # example functions for testing the LLInterpreter 
 _snap = globals().copy()
@@ -37,6 +56,19 @@
     m = k / 2
     return m - 1
 
+def simple_ifs(i): 
+    if i: 
+        return 42 
+    else: 
+        return 43 
+
+def while_simple(i): 
+    sum = 0
+    while i > 0: 
+        sum += i
+        i -= 1
+    return sum 
+
 #__________________________________________________________________
 # interactive playing 
 



More information about the Pypy-commit mailing list