|
| 1 | +package com.falsepattern.jfunge.interpreter.instructions.fingerprints; |
| 2 | + |
| 3 | +import com.falsepattern.jfunge.interpreter.ExecutionContext; |
| 4 | +import com.falsepattern.jfunge.interpreter.instructions.Fingerprint; |
| 5 | +import com.falsepattern.jfunge.storage.FungeSpace; |
| 6 | +import com.falsepattern.jfunge.util.MemoryStack; |
| 7 | +import lombok.AccessLevel; |
| 8 | +import lombok.Cleanup; |
| 9 | +import lombok.NoArgsConstructor; |
| 10 | +import lombok.val; |
| 11 | +import org.joml.Vector3i; |
| 12 | + |
| 13 | +@NoArgsConstructor(access = AccessLevel.PRIVATE) |
| 14 | +public class INDV implements Fingerprint { |
| 15 | + public static final INDV INSTANCE = new INDV(); |
| 16 | + @Override |
| 17 | + public int code() { |
| 18 | + return 0x494e4456; |
| 19 | + } |
| 20 | + |
| 21 | + private static void getVector(FungeSpace fs, int dimensions, int x, int y, int z, Vector3i output) { |
| 22 | + switch (dimensions) { |
| 23 | + default: throw new IllegalStateException("Pointer logic only works on 3d or lower funge"); |
| 24 | + case 3: |
| 25 | + output.z = fs.get(x, y, z); |
| 26 | + x++; |
| 27 | + case 2: |
| 28 | + output.y = fs.get(x, y, z); |
| 29 | + x++; |
| 30 | + case 1: |
| 31 | + output.x = fs.get(x, y, z); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private static void putVector(FungeSpace fs, int dimensions, int x, int y, int z, Vector3i input) { |
| 36 | + switch (dimensions) { |
| 37 | + default: throw new IllegalStateException("Pointer logic only works on 3d or lower funge"); |
| 38 | + case 3: |
| 39 | + fs.set(x, y, z, input.z); |
| 40 | + x++; |
| 41 | + case 2: |
| 42 | + fs.set(x, y, z, input.y); |
| 43 | + x++; |
| 44 | + case 1: |
| 45 | + fs.set(x, y, z, input.x); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static void retrievePointer(ExecutionContext ctx, Vector3i output) { |
| 50 | + val stack = ctx.stack(); |
| 51 | + val ip = ctx.IP(); |
| 52 | + val fs = ctx.fungeSpace(); |
| 53 | + @Cleanup val mStack = MemoryStack.stackPush(); |
| 54 | + |
| 55 | + val pointer = mStack.vec3i(); |
| 56 | + stack.popVecDimProof(ctx.dimensions(), pointer); |
| 57 | + pointer.add(ip.storageOffset()); |
| 58 | + getVector(fs, ctx.dimensions(), pointer.x, pointer.y, pointer.z, output); |
| 59 | + } |
| 60 | + |
| 61 | + @Instr('G') |
| 62 | + public static void getNumberAtPointer(ExecutionContext ctx) { |
| 63 | + val stack = ctx.stack(); |
| 64 | + val ip = ctx.IP(); |
| 65 | + |
| 66 | + @Cleanup val mStack = MemoryStack.stackPush(); |
| 67 | + val pointer = mStack.vec3i(); |
| 68 | + retrievePointer(ctx, pointer); |
| 69 | + pointer.add(ip.storageOffset()); |
| 70 | + stack.push(ctx.fungeSpace().get(pointer)); |
| 71 | + } |
| 72 | + |
| 73 | + @Instr('P') |
| 74 | + public static void putNumberAtPointer(ExecutionContext ctx) { |
| 75 | + val stack = ctx.stack(); |
| 76 | + val ip = ctx.IP(); |
| 77 | + |
| 78 | + @Cleanup val mStack = MemoryStack.stackPush(); |
| 79 | + val pointer = mStack.vec3i(); |
| 80 | + retrievePointer(ctx, pointer); |
| 81 | + pointer.add(ip.storageOffset()); |
| 82 | + ctx.fungeSpace().set(pointer, stack.pop()); |
| 83 | + } |
| 84 | + |
| 85 | + @Instr('V') |
| 86 | + public static void getVectorAtPointer(ExecutionContext ctx) { |
| 87 | + val stack = ctx.stack(); |
| 88 | + val ip = ctx.IP(); |
| 89 | + |
| 90 | + @Cleanup val mStack = MemoryStack.stackPush(); |
| 91 | + val pointer = mStack.vec3i(); |
| 92 | + retrievePointer(ctx, pointer); |
| 93 | + pointer.add(ip.storageOffset()); |
| 94 | + getVector(ctx.fungeSpace(), ctx.dimensions(), pointer.x, pointer.y, pointer.z, pointer); |
| 95 | + stack.pushVecDimProof(ctx.dimensions(), pointer); |
| 96 | + } |
| 97 | + |
| 98 | + @Instr('W') |
| 99 | + public static void putVectorAtPointer(ExecutionContext ctx) { |
| 100 | + val stack = ctx.stack(); |
| 101 | + val ip = ctx.IP(); |
| 102 | + |
| 103 | + @Cleanup val mStack = MemoryStack.stackPush(); |
| 104 | + val pointer = mStack.vec3i(); |
| 105 | + retrievePointer(ctx, pointer); |
| 106 | + pointer.add(ip.storageOffset()); |
| 107 | + val vec = mStack.vec3i(); |
| 108 | + stack.popVecDimProof(ctx.dimensions(), vec); |
| 109 | + putVector(ctx.fungeSpace(), ctx.dimensions(), pointer.x, pointer.y, pointer.z, vec); |
| 110 | + } |
| 111 | +} |
0 commit comments