|
| 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 lombok.AccessLevel; |
| 6 | +import lombok.NoArgsConstructor; |
| 7 | +import lombok.SneakyThrows; |
| 8 | +import lombok.val; |
| 9 | + |
| 10 | +@NoArgsConstructor(access = AccessLevel.PRIVATE) |
| 11 | +public class CPLI implements Fingerprint { |
| 12 | +public static final CPLI INSTANCE = new CPLI(); |
| 13 | + @Override |
| 14 | + public int code() { |
| 15 | + return 0x43504C49; |
| 16 | + } |
| 17 | + |
| 18 | + @Instr('A') |
| 19 | + public static void add(ExecutionContext ctx) { |
| 20 | + val stack = ctx.stack(); |
| 21 | + val bi = stack.pop(); |
| 22 | + val br = stack.pop(); |
| 23 | + val ai = stack.pop(); |
| 24 | + val ar = stack.pop(); |
| 25 | + stack.push(ar + br); |
| 26 | + stack.push(ai + bi); |
| 27 | + } |
| 28 | + |
| 29 | + @Instr('D') |
| 30 | + public static void div(ExecutionContext ctx) { |
| 31 | + val stack = ctx.stack(); |
| 32 | + val bi = stack.pop(); |
| 33 | + val br = stack.pop(); |
| 34 | + val ai = stack.pop(); |
| 35 | + val ar = stack.pop(); |
| 36 | + val r = (ar * br + ai * bi) / (br * br + bi * bi); |
| 37 | + val i = (ai * br - ar * bi) / (br * br + bi * bi); |
| 38 | + stack.push(r); |
| 39 | + stack.push(i); |
| 40 | + } |
| 41 | + |
| 42 | + @Instr('M') |
| 43 | + public static void mul(ExecutionContext ctx) { |
| 44 | + val stack = ctx.stack(); |
| 45 | + val bi = stack.pop(); |
| 46 | + val br = stack.pop(); |
| 47 | + val ai = stack.pop(); |
| 48 | + val ar = stack.pop(); |
| 49 | + val r = ar * br - ai * bi; |
| 50 | + val i = ar * bi + ai * br; |
| 51 | + stack.push(r); |
| 52 | + stack.push(i); |
| 53 | + } |
| 54 | + |
| 55 | + @SneakyThrows |
| 56 | + @Instr('O') |
| 57 | + public static void output(ExecutionContext ctx) { |
| 58 | + val stack = ctx.stack(); |
| 59 | + val i = stack.pop(); |
| 60 | + val r = stack.pop(); |
| 61 | + ctx.output().write(("(" + r + " + " + i + "i)").getBytes()); |
| 62 | + } |
| 63 | + |
| 64 | + @Instr('S') |
| 65 | + public static void sub(ExecutionContext ctx) { |
| 66 | + val stack = ctx.stack(); |
| 67 | + val bi = stack.pop(); |
| 68 | + val br = stack.pop(); |
| 69 | + val ai = stack.pop(); |
| 70 | + val ar = stack.pop(); |
| 71 | + stack.push(ar - br); |
| 72 | + stack.push(ai - bi); |
| 73 | + } |
| 74 | + |
| 75 | + @Instr('V') |
| 76 | + public static void length(ExecutionContext ctx) { |
| 77 | + val stack = ctx.stack(); |
| 78 | + val i = stack.pop(); |
| 79 | + val r = stack.pop(); |
| 80 | + stack.push((int)Math.sqrt(r * r + i * i)); |
| 81 | + } |
| 82 | +} |
0 commit comments