Skip to content

Commit 15ee0a6

Browse files
committed
Implementation of math.prod()
1 parent e4d2d01 commit 15ee0a6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626
package com.oracle.graal.python.builtins.modules;
2727

28+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__;
2829
import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError;
2930
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
3031
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
@@ -50,8 +51,10 @@
5051
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
5152
import com.oracle.graal.python.nodes.ErrorMessages;
5253
import com.oracle.graal.python.nodes.PGuards;
54+
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
5355
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
5456
import com.oracle.graal.python.nodes.control.GetIteratorExpressionNode.GetIteratorNode;
57+
import com.oracle.graal.python.nodes.expression.BinaryArithmetic;
5558
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5659
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5760
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
@@ -2621,4 +2624,36 @@ private void raiseIfNegative(boolean condition) {
26212624
}
26222625
}
26232626
}
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+
}
26242659
}

0 commit comments

Comments
 (0)