|
| 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 | + |
| 9 | +@NoArgsConstructor(access = AccessLevel.PRIVATE) |
| 10 | +public class BASE implements Fingerprint { |
| 11 | + public static final BASE INSTANCE = new BASE(); |
| 12 | + |
| 13 | + @Override |
| 14 | + public int code() { |
| 15 | + return 0x42415345; |
| 16 | + } |
| 17 | + |
| 18 | + @SneakyThrows |
| 19 | + @Instr('B') |
| 20 | + public static void printBinary(ExecutionContext ctx) { |
| 21 | + ctx.output().write(Integer.toBinaryString(ctx.stack().pop()).getBytes()); |
| 22 | + } |
| 23 | + |
| 24 | + @SneakyThrows |
| 25 | + @Instr('H') |
| 26 | + public static void printHex(ExecutionContext ctx) { |
| 27 | + ctx.output().write(Integer.toHexString(ctx.stack().pop()).getBytes()); |
| 28 | + } |
| 29 | + |
| 30 | + @Instr('I') |
| 31 | + public static void readIntBase(ExecutionContext ctx) { |
| 32 | + int base = ctx.stack().pop(); |
| 33 | + //Read input until non-digit character is encountered |
| 34 | + StringBuilder sb = new StringBuilder(); |
| 35 | + int c; |
| 36 | + while (Character.isDigit(c = ctx.input(true))) { |
| 37 | + sb.append((char) c); |
| 38 | + ctx.input(false); |
| 39 | + } |
| 40 | + ctx.stack().push(Integer.parseInt(sb.toString(), base)); |
| 41 | + } |
| 42 | + |
| 43 | + @SneakyThrows |
| 44 | + @Instr('N') |
| 45 | + public static void printInBase(ExecutionContext ctx) { |
| 46 | + int base = ctx.stack().pop(); |
| 47 | + int value = ctx.stack().pop(); |
| 48 | + ctx.output().write(Integer.toString(value, base).getBytes()); |
| 49 | + } |
| 50 | + |
| 51 | + @SneakyThrows |
| 52 | + @Instr('O') |
| 53 | + public static void printOctal(ExecutionContext ctx) { |
| 54 | + ctx.output().write(Integer.toOctalString(ctx.stack().pop()).getBytes()); |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments