[pypy-commit] pypy numpy-minilang: function call and move one more test
fijal
noreply at buildbot.pypy.org
Fri Oct 28 11:13:52 CEST 2011
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-minilang
Changeset: r48555:7d18eb124a06
Date: 2011-10-28 11:13 +0200
http://bitbucket.org/pypy/pypy/changeset/7d18eb124a06/
Log: function call and move one more test
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
@@ -12,6 +12,15 @@
class BogusBytecode(Exception):
pass
+class ArgumentMismatch(Exception):
+ pass
+
+class ArgumentNotAnArray(Exception):
+ pass
+
+class WrongFunctionName(Exception):
+ pass
+
class FakeSpace(object):
w_ValueError = None
w_TypeError = None
@@ -174,8 +183,7 @@
if isinstance(w_rhs, Scalar):
index = int(interp.space.float_w(
w_rhs.value.wrap(interp.space)))
- dtype = interp.space.fromcache(W_Float64Dtype)
- return Scalar(dtype, w_lhs.get_concrete().eval(index))
+ return w_lhs.get_concrete().eval(index).wrap(interp.space)
else:
raise NotImplementedError
else:
@@ -244,6 +252,26 @@
def execute(self, interp):
interp.results.append(self.expr.execute(interp))
+class FunctionCall(Node):
+ def __init__(self, name, args):
+ self.name = name
+ self.args = args
+
+ def __repr__(self):
+ return "%s(%s)" % (self.name, ", ".join([repr(arg)
+ for arg in self.args]))
+
+ def execute(self, interp):
+ if self.name == 'sum':
+ if len(self.args) != 1:
+ raise ArgumentMismatch
+ arr = self.args[0].execute(interp)
+ if not isinstance(arr, BaseArray):
+ raise ArgumentNotAnArray
+ return arr.descr_sum(interp.space)
+ else:
+ raise WrongFunctionName
+
class Parser(object):
def parse_identifier(self, id):
id = id.strip(" ")
@@ -292,9 +320,21 @@
return True
return False
+ def parse_function_call(self, v):
+ l = v.split('(')
+ assert len(l) == 2
+ name = l[0]
+ cut = len(l[1]) - 1
+ assert cut >= 0
+ args = [self.parse_constant_or_identifier(id)
+ for id in l[1][:cut].split(",")]
+ return FunctionCall(name, args)
+
def parse_constant_or_identifier(self, v):
c = v[0]
if (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z'):
+ if '(' in v:
+ return self.parse_function_call(v)
return self.parse_identifier(v)
return self.parse_constant(v)
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
@@ -18,9 +18,7 @@
assert interp.code.statements[1].expr.v == 3
def test_array_literal(self):
- code = """
- a = [1,2,3]
- """
+ code = "a = [1,2,3]"
interp = self.compile(code)
assert isinstance(interp.code.statements[0].expr, ArrayConstant)
st = interp.code.statements[0]
@@ -28,9 +26,7 @@
FloatConstant(3)]
def test_array_literal2(self):
- code = """
- a = [[1],[2],[3]]
- """
+ code = "a = [[1],[2],[3]]"
interp = self.compile(code)
assert isinstance(interp.code.statements[0].expr, ArrayConstant)
st = interp.code.statements[0]
@@ -39,17 +35,13 @@
ArrayConstant([FloatConstant(3)])]
def test_expr_1(self):
- code = """
- b = a + 1
- """
+ code = "b = a + 1"
interp = self.compile(code)
assert (interp.code.statements[0].expr ==
Operator(Variable("a"), "+", FloatConstant(1)))
def test_expr_2(self):
- code = """
- b = a + b - 3
- """
+ code = "b = a + b - 3"
interp = self.compile(code)
assert (interp.code.statements[0].expr ==
Operator(Operator(Variable("a"), "+", Variable("b")), "-",
@@ -57,28 +49,28 @@
def test_expr_3(self):
# an equivalent of range
- code = """
- a = |20|
- """
+ code = "a = |20|"
interp = self.compile(code)
assert interp.code.statements[0].expr == RangeConstant(20)
def test_expr_only(self):
- code = """
- 3 + a
- """
+ code = "3 + a"
interp = self.compile(code)
assert interp.code.statements[0] == Execute(
Operator(FloatConstant(3), "+", Variable("a")))
def test_array_access(self):
- code = """
- a -> 3
- """
+ code = "a -> 3"
interp = self.compile(code)
assert interp.code.statements[0] == Execute(
Operator(Variable("a"), "->", FloatConstant(3)))
+ def test_function_call(self):
+ code = "sum(a)"
+ interp = self.compile(code)
+ assert interp.code.statements[0] == Execute(
+ FunctionCall("sum", [Variable("a")]))
+
class TestRunner(object):
def run(self, code):
interp = numpy_compile(code)
@@ -112,7 +104,7 @@
a + b -> 3
"""
interp = self.run(code)
- assert interp.results[0].value.val == 3 + 6
+ assert interp.results[0].floatval == 3 + 6
def test_range_getitem(self):
code = """
@@ -120,4 +112,13 @@
r -> 3
"""
interp = self.run(code)
- assert interp.results[0].value.val == 6
+ assert interp.results[0].floatval == 6
+
+ def test_sum(self):
+ code = """
+ a = [1,2,3,4,5]
+ r = sum(a)
+ r
+ """
+ interp = self.run(code)
+ assert interp.results[0].floatval == 15
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
@@ -23,8 +23,7 @@
interp = numpy_compile(hlstr(code))
interp.run(space)
res = interp.results[0]
- assert isinstance(res, BaseArray)
- return interp.space.float_w(res.eval(0).wrap(interp.space))
+ return interp.space.float_w(res)
if self.graph is None:
interp, graph = self.meta_interp(f, [llstr(code)],
@@ -58,6 +57,17 @@
"setarrayitem_raw": 1, "int_add": 1,
"int_lt": 1, "guard_true": 1, "jump": 1})
+ def test_sum(self):
+ result = self.run("""
+ a = |30|
+ b = a + a
+ sum(b)
+ """)
+ assert result == 2 * sum(range(30))
+ self.check_loops({"getarrayitem_raw": 2, "float_add": 2,
+ "int_add": 1,
+ "int_lt": 1, "guard_true": 1, "jump": 1})
+
class DisabledTestNumpy(object):
def test_sum(self):
space = self.space
More information about the pypy-commit
mailing list