Author: Maciej Fijalkowski <[email protected]>
Branch: numpy-minilang
Changeset: r48540:263d430ab0d3
Date: 2011-10-27 22:30 +0200
http://bitbucket.org/pypy/pypy/changeset/263d430ab0d3/

Log:    a bit of rpythonization

diff --git a/pypy/module/micronumpy/compile.py 
b/pypy/module/micronumpy/compile.py
--- a/pypy/module/micronumpy/compile.py
+++ b/pypy/module/micronumpy/compile.py
@@ -32,6 +32,7 @@
     def __init__(self):
         """NOT_RPYTHON"""
         self.fromcache = InternalSpaceCache(self).getorbuild
+        self.w_float64dtype = W_Float64Dtype(self)
 
     def issequence_w(self, w_obj):
         return w_obj.seq
@@ -58,8 +59,20 @@
         return w_obj
 
     def float_w(self, w_obj):
+        assert isinstance(w_obj, FloatObject)        
         return w_obj.floatval
 
+    def int_w(self, w_obj):
+        assert isinstance(w_obj, IntObject)
+        return w_obj.intval
+
+    def int(self, w_obj):
+        return w_obj
+
+    def is_true(self, w_obj):
+        assert isinstance(w_obj, BoolObject)
+        return w_obj.boolval
+
     def is_w(self, w_obj, w_what):
         return w_obj is w_what
 
@@ -156,18 +169,19 @@
         self.rhs = rhs
 
     def execute(self, interp):
+        w_lhs = self.lhs.execute(interp)
+        w_rhs = self.rhs.execute(interp)
+        assert isinstance(w_lhs, BaseArray)
         if self.name == '+':
-            w_lhs = self.lhs.execute(interp)
-            w_rhs = self.rhs.execute(interp)
             return w_lhs.descr_add(interp.space, w_rhs)
         elif self.name == '->':
-            w_lhs = self.lhs.execute(interp)
-            w_rhs = self.rhs.execute(interp)
             if isinstance(w_rhs, Scalar):
                 index = int(space.float_w(w_rhs.value.wrap(interp.space)))
                 return w_lhs.get_concrete().eval(index)
             else:
-                xxx
+                raise NotImplementedError
+        else:
+            raise NotImplementedError
 
     def __repr__(self):
         return '(%r %s %r)' % (self.lhs, self.name, self.rhs)
@@ -183,7 +197,7 @@
         return space.wrap(self.v)
 
     def execute(self, interp):
-        dtype = interp.space.fromcache(W_Float64Dtype)
+        dtype = space.w_float64dtype
         return Scalar(dtype, dtype.box(self.v))
 
 class RangeConstant(Node):
@@ -226,8 +240,8 @@
 
 class Parser(object):
     def parse_identifier(self, id):
-        id = id.strip()
-        assert id.isalpha()
+        id = id.strip(" ")
+        #assert id.isalpha()
         return Variable(id)
 
     def parse_expression(self, expr):
@@ -239,17 +253,17 @@
         while tokens:
             token = tokens.pop()
             if token == ')':
-                xxx
+                raise NotImplementedError
             elif self.is_identifier_or_const(token):
                 if stack:
-                    name = stack.pop()
+                    name = stack.pop().name
                     lhs = stack.pop()
                     rhs = self.parse_constant_or_identifier(token)
                     stack.append(Operator(lhs, name, rhs))
                 else:
                     stack.append(self.parse_constant_or_identifier(token))
             else:
-                stack.append(token)
+                stack.append(Variable(token))
         assert len(stack) == 1
         return stack[-1]
 
diff --git a/pypy/module/micronumpy/test/test_zjit.py 
b/pypy/module/micronumpy/test/test_zjit.py
--- a/pypy/module/micronumpy/test/test_zjit.py
+++ b/pypy/module/micronumpy/test/test_zjit.py
@@ -1,7 +1,7 @@
 from pypy.jit.metainterp.test.support import LLJitMixin
 from pypy.module.micronumpy import interp_ufuncs, signature
-from pypy.module.micronumpy.compile import (numpy_compile, FakeSpace,
-    FloatObject, IntObject)
+from pypy.module.micronumpy.compile import (InterpreterState, FakeSpace,
+    FloatObject, IntObject, Parser)
 from pypy.module.micronumpy.interp_dtype import W_Int32Dtype, W_Float64Dtype, 
W_Int64Dtype, W_UInt64Dtype
 from pypy.module.micronumpy.interp_numarray import (BaseArray, SingleDimArray,
     SingleDimSlice, scalar_w)
@@ -21,23 +21,31 @@
         cls.int32_dtype = cls.space.fromcache(W_Int32Dtype)
 
     def run(self, code):
+        # trick annotator
+        c = """
+        a = 3
+        """
+        
         space = FakeSpace()
-        interp = numpy_compile(code)
-        def f():
+        parser = Parser()
+        codes = [parser.parse(code), parser.parse(c)]
+        
+        def f(i):
+            interp = InterpreterState(codes[i])
             interp.run(space)
-        self.meta_interpxxx
+            return interp.results[0]
+        return self.meta_interp(f, [0], listops=True, backendopt=True)
 
     def test_add(self):
-        def f(i):
-            ar = SingleDimArray(i, dtype=self.float64_dtype)
-            v = interp_ufuncs.get(self.space).add.call(self.space, [ar, ar])
-            return v.get_concrete().eval(3).val
-
-        result = self.meta_interp(f, [5], listops=True, backendopt=True)
+        result = self.run("""
+        a = |30|
+        b = a + a
+        b -> 3
+        """)
         self.check_loops({'getarrayitem_raw': 2, 'float_add': 1,
                           'setarrayitem_raw': 1, 'int_add': 1,
                           'int_lt': 1, 'guard_true': 1, 'jump': 1})
-        assert result == f(5)
+        assert result == 3 + 3
 
     def test_floatadd(self):
         def f(i):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to