|
| 1 | +package io.github.coderodde.simple.stack.machine; |
| 2 | + |
| 3 | +import java.util.function.BinaryOperator; |
| 4 | + |
| 5 | +/** |
| 6 | + * |
| 7 | + * @version 1.0.0 (Jul 9, 2025) |
| 8 | + * @since 1.0.0 (Jul 9, 2025) |
| 9 | + */ |
| 10 | +public final class MachineLanguageSpecification { |
| 11 | + |
| 12 | + public static final class NopInstructionImplementation |
| 13 | + implements InstructionImplementation { |
| 14 | + |
| 15 | + @Override |
| 16 | + public void execute(final SimpleStackMachine machine) { |
| 17 | + machine.checkReserve(1); |
| 18 | + machine.advanceInstructionPointer(); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + public static /*final*/ class PushInstructionImplementation |
| 23 | + implements InstructionImplementation { |
| 24 | + |
| 25 | + @Override |
| 26 | + public void execute(final SimpleStackMachine machine) { |
| 27 | + machine.checkReserve(1 + Long.BYTES); |
| 28 | + machine.advanceInstructionPointer(); |
| 29 | + |
| 30 | + final long number = |
| 31 | + machine.readNumberFromTape(machine.getInstructionPointer() ); |
| 32 | + |
| 33 | + machine.push(number); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public static final class PopInstructionImplementation |
| 38 | + implements InstructionImplementation { |
| 39 | + |
| 40 | + @Override |
| 41 | + public void execute(SimpleStackMachine machine) { |
| 42 | + machine.checkReserve(1); |
| 43 | + machine.advanceInstructionPointer(); |
| 44 | + machine.pop(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static final class LoadInstructionImplementation |
| 49 | + implements InstructionImplementation { |
| 50 | + |
| 51 | + @Override |
| 52 | + public void execute(SimpleStackMachine machine) { |
| 53 | + machine.checkReserve(1 + Long.BYTES); |
| 54 | + machine.advanceInstructionPointer(); |
| 55 | + |
| 56 | + final long address = |
| 57 | + machine.readNumberFromTape(machine.getInstructionPointer()); |
| 58 | + |
| 59 | + final long datum = machine.readNumberFromTape((int) address); |
| 60 | + |
| 61 | + machine.push(datum); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static final class StoreInstructionImplementation |
| 66 | + implements InstructionImplementation { |
| 67 | + |
| 68 | + @Override |
| 69 | + public void execute(SimpleStackMachine machine) { |
| 70 | + machine.checkReserve(1 + 2 * Long.BYTES); |
| 71 | + machine.advanceInstructionPointer(); |
| 72 | + |
| 73 | + final long address = |
| 74 | + machine.readNumberFromTape(machine.getInstructionPointer()); |
| 75 | + |
| 76 | + machine.advanceInstructionPointer(Long.BYTES); |
| 77 | + |
| 78 | + final long datum = machine.readNumberFromTape(machine.getInstructionPointer()); |
| 79 | + |
| 80 | + machine.writeNumberToTape((int) address, datum); |
| 81 | + machine.advanceInstructionPointer(Long.BYTES); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Delegates to the pop operation. |
| 87 | + */ |
| 88 | + public static final class ConstInstructionImplementation |
| 89 | + extends PushInstructionImplementation { |
| 90 | + |
| 91 | + @Override |
| 92 | + public void execute(SimpleStackMachine machine) { |
| 93 | + super.execute(machine); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static /*final*/ class BinaryArithmeticInstructionImplementation |
| 98 | + implements InstructionImplementation { |
| 99 | + |
| 100 | + private final BinaryOperator<Long> func; |
| 101 | + |
| 102 | + public BinaryArithmeticInstructionImplementation( |
| 103 | + final BinaryOperator<Long> func) { |
| 104 | + this.func = func; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void execute(SimpleStackMachine machine) { |
| 109 | + machine.checkReserve(1 + 2 * Long.BYTES); |
| 110 | + machine.requireStackSize(2); |
| 111 | + machine.advanceInstructionPointer(); |
| 112 | + |
| 113 | + final long number1 = |
| 114 | + machine.readNumberFromTape(machine.getInstructionPointer()); |
| 115 | + |
| 116 | + final long number2 = |
| 117 | + machine.readNumberFromTape( |
| 118 | + machine.getInstructionPointer() + Long.BYTES); |
| 119 | + |
| 120 | + machine.pop(); |
| 121 | + machine.pop(); |
| 122 | + machine.push(func.apply(number1, number2)); |
| 123 | + machine.advanceInstructionPointer(Long.BYTES * 2); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public static final class AddInstructionImplementation |
| 128 | + extends BinaryArithmeticInstructionImplementation { |
| 129 | + |
| 130 | + public AddInstructionImplementation() { |
| 131 | + super((n1, n2) -> { return n1 + n2; }); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public static final class SubInstructionImplementation |
| 136 | + extends BinaryArithmeticInstructionImplementation { |
| 137 | + |
| 138 | + public SubInstructionImplementation() { |
| 139 | + super((n1, n2) -> { return n1 - n2; }); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public static final class MultiplyInstructionImplementation |
| 144 | + extends BinaryArithmeticInstructionImplementation { |
| 145 | + |
| 146 | + public MultiplyInstructionImplementation() { |
| 147 | + super((n1, n2) -> { return n1 * n2; }); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + public static final class DivideInstructionImplementation |
| 152 | + extends BinaryArithmeticInstructionImplementation { |
| 153 | + |
| 154 | + public DivideInstructionImplementation() { |
| 155 | + super((n1, n2) -> { return n1 / n2; }); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void execute(final SimpleStackMachine machine) { |
| 160 | + try { |
| 161 | + super.execute(machine); |
| 162 | + } catch (final ArithmeticException ex) { |
| 163 | + throw new StackMachineException(ex.getMessage()); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + public static final class ModuloInstructionImplementation |
| 169 | + extends BinaryArithmeticInstructionImplementation { |
| 170 | + |
| 171 | + public ModuloInstructionImplementation() { |
| 172 | + super((n1, n2) -> { return n1 % n2; }); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void execute(final SimpleStackMachine machine) { |
| 177 | + try { |
| 178 | + super.execute(machine); |
| 179 | + } catch (final ArithmeticException ex) { |
| 180 | + throw new StackMachineException(ex.getMessage()); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + public static final class DuplicateInstructionImplementation |
| 186 | + implements InstructionImplementation { |
| 187 | + |
| 188 | + @Override |
| 189 | + public void execute(final SimpleStackMachine machine) { |
| 190 | + machine.checkReserve(1); |
| 191 | + machine.requireStackSize(1); |
| 192 | + machine.push(machine.top()); |
| 193 | + machine.advanceInstructionPointer(); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + public static final class SwapInstructionImplementation |
| 198 | + implements InstructionImplementation { |
| 199 | + |
| 200 | + @Override |
| 201 | + public void execute(final SimpleStackMachine machine) { |
| 202 | + machine.checkReserve(1); |
| 203 | + machine.requireStackSize(2); |
| 204 | + final long number1 = machine.pop(); |
| 205 | + final long number2 = machine.pop(); |
| 206 | + machine.push(number1); |
| 207 | + machine.push(number2); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + public static final class CompareInstructionImplementation |
| 212 | + implements InstructionImplementation { |
| 213 | + |
| 214 | + @Override |
| 215 | + public void execute(final SimpleStackMachine machine) { |
| 216 | + machine.checkReserve(1); |
| 217 | + machine.requireStackSize(2); |
| 218 | + final long number1 = machine.pop(); |
| 219 | + final long number2 = machine.pop(); |
| 220 | + final int cmp = Long.compare(number1, |
| 221 | + number2); |
| 222 | + |
| 223 | + if (cmp < 0) { |
| 224 | + machine.flags().belowFlag = true; |
| 225 | + } else if (cmp > 0) { |
| 226 | + machine.flags().aboveFlag = true; |
| 227 | + } else { |
| 228 | + machine.flags().equalFlag = true; |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + public static final class UnconditionalJumpInstructionImplementation |
| 234 | + implements InstructionImplementation { |
| 235 | + |
| 236 | + @Override |
| 237 | + public void execute(final SimpleStackMachine machine) { |
| 238 | + machine.checkReserve(1 + Long.BYTES); |
| 239 | + machine.advanceInstructionPointer(); |
| 240 | + final int jumpAddress = |
| 241 | + (int) machine.readNumberFromTape( |
| 242 | + machine.getInstructionPointer()); |
| 243 | + |
| 244 | + machine.checkReserve(jumpAddress); |
| 245 | + machine.setInstructionPointer(jumpAddress); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + public static final class HaltInstructionImplementation |
| 250 | + implements InstructionImplementation { |
| 251 | + |
| 252 | + @Override |
| 253 | + public void execute(final SimpleStackMachine machine) { |
| 254 | + machine.checkReserve(1); |
| 255 | + machine.requestHalt(); |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | +} |
0 commit comments