|
25 | 25 | */
|
26 | 26 | package com.oracle.graal.python.builtins.modules;
|
27 | 27 |
|
| 28 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__; |
28 | 29 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError;
|
29 | 30 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
|
30 | 31 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
|
|
50 | 51 | import com.oracle.graal.python.builtins.objects.tuple.PTuple;
|
51 | 52 | import com.oracle.graal.python.nodes.ErrorMessages;
|
52 | 53 | import com.oracle.graal.python.nodes.PGuards;
|
| 54 | +import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode; |
53 | 55 | import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
|
54 | 56 | import com.oracle.graal.python.nodes.control.GetIteratorExpressionNode.GetIteratorNode;
|
| 57 | +import com.oracle.graal.python.nodes.expression.BinaryArithmetic; |
55 | 58 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
56 | 59 | import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
|
57 | 60 | import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
|
@@ -2621,4 +2624,36 @@ private void raiseIfNegative(boolean condition) {
|
2621 | 2624 | }
|
2622 | 2625 | }
|
2623 | 2626 | }
|
| 2627 | + |
| 2628 | + @Builtin(name = "prod", minNumOfPositionalArgs = 1, parameterNames = {"iterable"}, keywordOnlyNames = {"start"}) |
| 2629 | + @GenerateNodeFactory |
| 2630 | + public abstract static class ProdNode extends PythonBuiltinNode { |
| 2631 | + |
| 2632 | + @Child private GetIteratorNode iter = GetIteratorNode.create(); |
| 2633 | + @Child private LookupAndCallUnaryNode next = LookupAndCallUnaryNode.create(__NEXT__); |
| 2634 | + @Child private LookupAndCallBinaryNode mul = BinaryArithmetic.Mul.create(); |
| 2635 | + @Child private IsBuiltinClassProfile errorProfile = IsBuiltinClassProfile.create(); |
| 2636 | + |
| 2637 | + @Specialization |
| 2638 | + @SuppressWarnings("unused") |
| 2639 | + public Object doGenericNoStart(VirtualFrame frame, Object iterable, PNone start) { |
| 2640 | + return doGeneric(frame, iterable, 1); |
| 2641 | + } |
| 2642 | + |
| 2643 | + @Specialization(guards = "!isNoValue(start)") |
| 2644 | + public Object doGeneric(VirtualFrame frame, Object iterable, Object start) { |
| 2645 | + Object iterator = iter.executeWith(frame, iterable); |
| 2646 | + Object value = start; |
| 2647 | + while (true) { |
| 2648 | + Object nextValue; |
| 2649 | + try { |
| 2650 | + nextValue = next.executeObject(frame, iterator); |
| 2651 | + } catch (PException e) { |
| 2652 | + e.expectStopIteration(errorProfile); |
| 2653 | + return value; |
| 2654 | + } |
| 2655 | + value = mul.executeObject(frame, value, nextValue); |
| 2656 | + } |
| 2657 | + } |
| 2658 | + } |
2624 | 2659 | }
|
0 commit comments