Author: Maciej Fijalkowski <[email protected]>
Branch: 
Changeset: r57953:4c46bace2a1d
Date: 2012-10-09 16:45 +0200
http://bitbucket.org/pypy/pypy/changeset/4c46bace2a1d/

Log:    implement complex for compile, fix test_zjit

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
@@ -108,6 +108,9 @@
     def newlist(self, items):
         return ListObject(items)
 
+    def newcomplex(self, r, i):
+        return ComplexObject(r, i)
+
     def listview(self, obj):
         assert isinstance(obj, ListObject)
         return obj.items
@@ -132,6 +135,11 @@
             raise OperationError(self.w_TypeError, self.wrap("slice."))
         raise NotImplementedError
 
+    def unpackcomplex(self, w_obj):
+        if isinstance(w_obj, ComplexObject):
+            return w_obj.r, w_obj.i
+        raise NotImplementedError
+
     def index(self, w_obj):
         return self.wrap(self.int_w(w_obj))
 
@@ -227,6 +235,12 @@
     def __init__(self, v):
         self.v = v
 
+class ComplexObject(W_Root):
+    tp = FakeSpace.w_complex
+    def __init__(self, r, i):
+        self.r = r
+        self.i = i
+
 class InterpreterState(object):
     def __init__(self, code):
         self.code = code
@@ -344,6 +358,20 @@
     def execute(self, interp):
         return interp.space.wrap(self.v)
 
+class ComplexConstant(Node):
+    def __init__(self, r, i):
+        self.r = float(r)
+        self.i = float(i)
+
+    def __repr__(self):
+        return 'ComplexConst(%s, %s)' % (self.r, self.i)
+
+    def wrap(self, space):
+        return space.newcomplex(self.r, self.i)
+
+    def execute(self, interp):
+        return self.wrap(interp.space)
+
 class RangeConstant(Node):
     def __init__(self, v):
         self.v = int(v)
@@ -374,8 +402,7 @@
 
     def execute(self, interp):
         w_list = self.wrap(interp.space)
-        dtype = get_dtype_cache(interp.space).w_float64dtype
-        return array(interp.space, w_list, w_dtype=dtype, w_order=None)
+        return array(interp.space, w_list)
 
     def __repr__(self):
         return "[" + ", ".join([repr(item) for item in self.items]) + "]"
@@ -608,6 +635,8 @@
                 stack.append(RangeConstant(tokens.pop().v))
                 end = tokens.pop()
                 assert end.name == 'pipe'
+            elif token.name == 'paren_left':
+                stack.append(self.parse_complex_constant(tokens))
             elif accept_comma and token.name == 'comma':
                 continue
             else:
@@ -631,6 +660,15 @@
             args += self.parse_expression(tokens, accept_comma=True)
         return FunctionCall(name, args)
 
+    def parse_complex_constant(self, tokens):
+        r = tokens.pop()
+        assert r.name == 'number'
+        assert tokens.pop().name == 'comma'
+        i = tokens.pop()
+        assert i.name == 'number'
+        assert tokens.pop().name == 'paren_right'
+        return ComplexConstant(r.v, i.v)
+
     def parse_array_const(self, tokens):
         elems = []
         while True:
@@ -639,6 +677,8 @@
                 elems.append(FloatConstant(token.v))
             elif token.name == 'array_left':
                 elems.append(ArrayConstant(self.parse_array_const(tokens)))
+            elif token.name == 'paren_left':
+                elems.append(self.parse_complex_constant(tokens))
             else:
                 raise BadToken()
             token = tokens.pop()
diff --git a/pypy/module/micronumpy/test/test_compile.py 
b/pypy/module/micronumpy/test/test_compile.py
--- a/pypy/module/micronumpy/test/test_compile.py
+++ b/pypy/module/micronumpy/test/test_compile.py
@@ -281,3 +281,13 @@
         d -> 1
         ''')
         assert interp.results[0].value == 0
+
+    def test_complex(self):
+        interp = self.run('''
+        a = (0, 1)
+        b = [(0, 1), (1, 0)]
+        b -> 0
+        ''')
+        assert interp.results[0].real == 0
+        assert interp.results[0].imag == 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
@@ -18,6 +18,7 @@
     def setup_class(cls):
         default = """
         a = [1,2,3,4]
+        z = (1, 2)
         c = a + b
         sum(c) -> 1::1
         a -> 3:1:2
@@ -490,4 +491,3 @@
                                 'new_with_vtable': 1, 
                                 'int_add': 2, 
                                 'float_ne': 1})
-
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to