|
| 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.NoArgsConstructor; |
| 6 | +import lombok.val; |
| 7 | + |
| 8 | +import java.util.Random; |
| 9 | + |
| 10 | +@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE) |
| 11 | +public class FIXP implements Fingerprint { |
| 12 | + public static final FIXP INSTANCE = new FIXP(); |
| 13 | + @Override |
| 14 | + public int code() { |
| 15 | + return 0x46495850; |
| 16 | + } |
| 17 | + |
| 18 | + @Instr('A') |
| 19 | + public static void and(ExecutionContext ctx) { |
| 20 | + val stack = ctx.stack(); |
| 21 | + val b = stack.pop(); |
| 22 | + val a = stack.pop(); |
| 23 | + stack.push(a & b); |
| 24 | + } |
| 25 | + |
| 26 | + @Instr('B') |
| 27 | + public static void acos(ExecutionContext ctx) { |
| 28 | + val stack = ctx.stack(); |
| 29 | + val a = stack.pop(); |
| 30 | + val result = Math.toDegrees(Math.acos(a / 10000D)); |
| 31 | + if (Double.isNaN(result)) { |
| 32 | + ctx.interpret('r'); |
| 33 | + } else { |
| 34 | + stack.push((int) (result * 10000)); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Instr('C') |
| 39 | + public static void cos(ExecutionContext ctx) { |
| 40 | + val stack = ctx.stack(); |
| 41 | + val a = stack.pop(); |
| 42 | + stack.push((int)(Math.cos(Math.toRadians(a / 10000D)) * 10000)); |
| 43 | + } |
| 44 | + |
| 45 | + @Instr('D') |
| 46 | + public static void random(ExecutionContext ctx) { |
| 47 | + val stack = ctx.stack(); |
| 48 | + val a = stack.pop(); |
| 49 | + stack.push(new Random().nextInt(3)); |
| 50 | + } |
| 51 | + |
| 52 | + @Instr('I') |
| 53 | + public static void sin(ExecutionContext ctx) { |
| 54 | + val stack = ctx.stack(); |
| 55 | + val a = stack.pop(); |
| 56 | + stack.push((int)(Math.sin(Math.toRadians(a / 10000D)) * 10000)); |
| 57 | + } |
| 58 | + |
| 59 | + @Instr('J') |
| 60 | + public static void asin(ExecutionContext ctx) { |
| 61 | + val stack = ctx.stack(); |
| 62 | + val a = stack.pop(); |
| 63 | + val result = Math.toDegrees(Math.asin(a / 10000D)); |
| 64 | + if (Double.isNaN(result)) { |
| 65 | + ctx.interpret('r'); |
| 66 | + } else { |
| 67 | + stack.push((int) (result * 10000)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Instr('N') |
| 72 | + public static void negate(ExecutionContext ctx) { |
| 73 | + val stack = ctx.stack(); |
| 74 | + val a = stack.pop(); |
| 75 | + stack.push(-a); |
| 76 | + } |
| 77 | + |
| 78 | + @Instr('O') |
| 79 | + public static void or(ExecutionContext ctx) { |
| 80 | + val stack = ctx.stack(); |
| 81 | + val b = stack.pop(); |
| 82 | + val a = stack.pop(); |
| 83 | + stack.push(a | b); |
| 84 | + } |
| 85 | + |
| 86 | + @Instr('P') |
| 87 | + public static void mulPi(ExecutionContext ctx) { |
| 88 | + val stack = ctx.stack(); |
| 89 | + val a = stack.pop(); |
| 90 | + stack.push((int)(a * Math.PI)); |
| 91 | + } |
| 92 | + |
| 93 | + @Instr('Q') |
| 94 | + public static void sqrt(ExecutionContext ctx) { |
| 95 | + val stack = ctx.stack(); |
| 96 | + val a = stack.pop(); |
| 97 | + stack.push((int)Math.sqrt(a)); |
| 98 | + } |
| 99 | + |
| 100 | + @Instr('R') |
| 101 | + public static void pow(ExecutionContext ctx) { |
| 102 | + val stack = ctx.stack(); |
| 103 | + val b = stack.pop(); |
| 104 | + val a = stack.pop(); |
| 105 | + val result = Math.pow(a, b); |
| 106 | + if (Double.isNaN(result) || Double.isInfinite(result)) { |
| 107 | + ctx.interpret('r'); |
| 108 | + } else { |
| 109 | + stack.push((int) result); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Instr('S') |
| 114 | + public static void sign(ExecutionContext ctx) { |
| 115 | + val stack = ctx.stack(); |
| 116 | + val a = stack.pop(); |
| 117 | + stack.push(Integer.compare(a, 0)); |
| 118 | + } |
| 119 | + |
| 120 | + @Instr('T') |
| 121 | + public static void tan(ExecutionContext ctx) { |
| 122 | + val stack = ctx.stack(); |
| 123 | + val a = stack.pop(); |
| 124 | + stack.push((int)(Math.tan(Math.toRadians(a / 10000D)) * 10000)); |
| 125 | + } |
| 126 | + |
| 127 | + @Instr('U') |
| 128 | + public static void atan(ExecutionContext ctx) { |
| 129 | + val stack = ctx.stack(); |
| 130 | + val a = stack.pop(); |
| 131 | + stack.push((int)(Math.toDegrees(Math.atan(a / 10000D)) * 10000)); |
| 132 | + } |
| 133 | + |
| 134 | + @Instr('V') |
| 135 | + public static void abs(ExecutionContext ctx) { |
| 136 | + val stack = ctx.stack(); |
| 137 | + val a = stack.pop(); |
| 138 | + stack.push(Math.abs(a)); |
| 139 | + } |
| 140 | + |
| 141 | + @Instr('X') |
| 142 | + public static void xor(ExecutionContext ctx) { |
| 143 | + val stack = ctx.stack(); |
| 144 | + val b = stack.pop(); |
| 145 | + val a = stack.pop(); |
| 146 | + stack.push(a ^ b); |
| 147 | + } |
| 148 | +} |
0 commit comments