|
| 1 | +package org.piccode.rt.modules; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | +import org.piccode.rt.Context; |
| 7 | +import org.piccode.rt.PiccodeArray; |
| 8 | +import org.piccode.rt.PiccodeClosure; |
| 9 | +import org.piccode.rt.PiccodeNumber; |
| 10 | +import org.piccode.rt.PiccodeTuple; |
| 11 | +import org.piccode.rt.PiccodeUnit; |
| 12 | +import org.piccode.rt.PiccodeValue; |
| 13 | +import org.piccode.rt.PiccodeValue.Type; |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * |
| 18 | + * @author hexaredecimal |
| 19 | + */ |
| 20 | +public class PiccodeProcModule { |
| 21 | + public static void addFunctions() { |
| 22 | + |
| 23 | + NativeFunctionFactory.create("forEach", List.of("array", "func"), (args, namedArgs, frame) -> { |
| 24 | + var ctx = frame == null ? |
| 25 | + Context.top |
| 26 | + : Context.getContextAt(frame); |
| 27 | + var caller = ctx.getTopFrame().caller; |
| 28 | + |
| 29 | + var array = namedArgs.get("array"); |
| 30 | + var func = namedArgs.get("func"); |
| 31 | + PiccodeValue.verifyType(caller, array, Type.ARRAY); |
| 32 | + PiccodeValue.verifyType(caller, func, Type.CLOSURE); |
| 33 | + var closure = (PiccodeClosure) func; |
| 34 | + ((PiccodeArray)array).nodes.forEach(value -> { |
| 35 | + closure.call(value); |
| 36 | + closure.evaluateIfReady(); |
| 37 | + }); |
| 38 | + return new PiccodeUnit(); |
| 39 | + }, null); |
| 40 | + |
| 41 | + NativeFunctionFactory.create("forever", List.of("func"), (args, namedArgs, frame) -> { |
| 42 | + var ctx = frame == null ? |
| 43 | + Context.top |
| 44 | + : Context.getContextAt(frame); |
| 45 | + var caller = ctx.getTopFrame().caller; |
| 46 | + |
| 47 | + var func = namedArgs.get("func"); |
| 48 | + PiccodeValue.verifyType(caller, func, Type.CLOSURE); |
| 49 | + var closure = (PiccodeClosure) func; |
| 50 | + while (true) { |
| 51 | + closure.call(new PiccodeUnit()); |
| 52 | + closure.evaluateIfReady(); |
| 53 | + } |
| 54 | + }, null); |
| 55 | + } |
| 56 | +} |
0 commit comments