|
1 | 1 | package io.github.coderodde.simple.stack.machine; |
2 | 2 |
|
| 3 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.AddInstructionImplementation; |
| 4 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.CompareInstructionImplementation; |
| 5 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.ConstInstructionImplementation; |
| 6 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.DivideInstructionImplementation; |
| 7 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.DuplicateInstructionImplementation; |
| 8 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.HaltInstructionImplementation; |
| 9 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.InstructionImplementation; |
| 10 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.LoadInstructionImplementation; |
| 11 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.ModuloInstructionImplementation; |
| 12 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.MultiplyInstructionImplementation; |
| 13 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.NopInstructionImplementation; |
| 14 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.PopInstructionImplementation; |
| 15 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.PushInstructionImplementation; |
| 16 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.StoreInstructionImplementation; |
| 17 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.SubInstructionImplementation; |
| 18 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.SwapInstructionImplementation; |
| 19 | +import io.github.coderodde.simple.stack.machine.SimpleStackMachine.UnconditionalJumpInstructionImplementation; |
3 | 20 | import java.util.HashMap; |
4 | 21 | import java.util.Map; |
5 | 22 |
|
|
12 | 29 | */ |
13 | 30 | public enum Operation { |
14 | 31 |
|
15 | | - NOP ("nop", (byte) 0x0), // No-operation. |
16 | | - PUSH ("push", (byte) 0x1), // Push a value to the stack. |
17 | | - POP ("pop", (byte) 0x2), // Pop the stack. |
18 | | - CONST ("const", (byte) 0x0), // Push a value to the stack. Effectively, POP. |
19 | | - LOAD ("load", (byte) 0x4), // Pushes a value from the tape to the stack. |
20 | | - STORE ("store", (byte) 0x5), // Pops the stack and stores the popped value |
| 32 | + NOP ("nop" , (byte) 0x0, new NopInstructionImplementation()), |
| 33 | + PUSH ("push" , (byte) 0x1, new PushInstructionImplementation()), // Push a value to the stack. |
| 34 | + POP ("pop" , (byte) 0x2, new PopInstructionImplementation()), // Pop the stack. |
| 35 | + CONST ("const", (byte) 0x0, new ConstInstructionImplementation()), // Push a value to the stack. Effectively, POP. |
| 36 | + LOAD ("load" , (byte) 0x4, new LoadInstructionImplementation()), // Pushes a value from the tape to the stack. |
| 37 | + STORE ("store", (byte) 0x5, new StoreInstructionImplementation()), // Pops the stack and stores the popped value |
21 | 38 | // at a given address of the tape. |
22 | 39 |
|
23 | | - ADD ("add", (byte) 0x6), // Pops two values from the stack, and pushes the |
24 | | - // sum of two to the stack. |
25 | | - SUB ("sub", (byte) 0x7), // Just like "add" but stores the difference |
26 | | - // between the top most number and the second |
27 | | - // top most number. |
28 | | - MUL ("mul", (byte) 0x8), // Multiplies the two topmost numbers. |
29 | | - DIV ("div", (byte) 0x9), // Divides the two numbers. Top most divided by |
30 | | - // the second top most. |
31 | | - MOD ("mod", (byte) 0xa), // The modulo operation, a % b, where a is the |
32 | | - // top most number and b is under a. (Top is the |
33 | | - // highest.) |
| 40 | + ADD ("add", (byte) 0x6, new AddInstructionImplementation()), // Pops two values from the stack, and pushes the |
| 41 | + // sum of two to the stack. |
| 42 | + SUB ("sub", (byte) 0x7, new SubInstructionImplementation()), // Just like "add" but stores the difference |
| 43 | + // between the top most number and the second |
| 44 | + // top most number. |
| 45 | + MUL ("mul", (byte) 0x8, new MultiplyInstructionImplementation()), // Multiplies the two topmost numbers. |
| 46 | + DIV ("div", (byte) 0x9, new DivideInstructionImplementation()), // Divides the two numbers. Top most divided by |
| 47 | + // the second top most. |
| 48 | + MOD ("mod", (byte) 0xa, new ModuloInstructionImplementation()), // The modulo operation, a % b, where a is the |
| 49 | + // top most number and b is under a. (Top is the |
| 50 | + // highest.) |
| 51 | + |
34 | 52 | CALL ("call", (byte) 0xb), // Procedure call. |
35 | | - RET ("ret", (byte) 0xc), // Procedure exit request. |
36 | | - DUP ("dup", (byte) 0xd), // Duplicates the stack. |
37 | | - SWAP ("swap", (byte) 0xe), // Swaps the two top most numbers in the stack. |
| 53 | + RET ("ret" , (byte) 0xc), // Procedure exit request. |
| 54 | + DUP ("dup" , (byte) 0xd, new DuplicateInstructionImplementation()), // Duplicates the stack. |
| 55 | + SWAP ("swap", (byte) 0xe, new SwapInstructionImplementation()), // Swaps the two top most numbers in the stack. |
38 | 56 |
|
39 | | - CMP ("cmp", (byte) 0x0f), // Compares the two top most numbers. Sets a |
| 57 | + CMP ("cmp", (byte) 0x0f, new CompareInstructionImplementation()), // Compares the two top most numbers. Sets a |
40 | 58 | // status flag. |
41 | | - JMP ("jmp", (byte) 0x10), // Unconditional jump. |
42 | | - JZ ("jz", (byte) 0x11), // Jump if zero flag is set. |
| 59 | + JMP ("jmp", (byte) 0x10, new UnconditionalJumpInstructionImplementation()), // Unconditional jump. |
| 60 | + JZ ("jz" , (byte) 0x11), // Jump if zero flag is set. |
43 | 61 | JNZ ("jnz", (byte) 0x12), // Jump if zero flag is not set. |
44 | 62 | JBZ ("jbz", (byte) 0x13), // Jump if the "below zero" flag is set. |
45 | 63 | JAZ ("jaz", (byte) 0x14), // Jump if the "above zero" flag is set. |
46 | | - JL ("jL", (byte) 0xf0), // Jump if the |
| 64 | + JL ("jl" , (byte) 0xf0), // Jump if the less than relation. |
| 65 | + JLE ("jle", (byte) 0xf1), // Jump if the less than or equal relation. |
| 66 | + JE ("je" , (byte) 0xf2), // Jump if equal. |
| 67 | + JNE ("jne", (byte) 0xf3), // Jump if not equal. |
| 68 | + JA ("ja" , (byte) 0xf4), // Jump if above. |
| 69 | + JAE ("jae", (byte) 0xf5), // Jump if above or equal. |
47 | 70 |
|
48 | 71 | PRINT_INT ("iout", (byte) 0x15), // Prints the top most number. |
49 | 72 | PRINT_STRING ("sout", (byte) 0x16), // Prints the string with bytes on the |
50 | 73 | // stack. |
51 | | - READ_INT ("iin", (byte) 0x17), // Read a single number and push it on |
| 74 | + READ_INT ("iin", (byte) 0x17), // Read a single number and push it on |
52 | 75 | // the stack. |
53 | | - READ_STRING ("sin", (byte) 0x18); // Reads a null-terminated string and |
| 76 | + READ_STRING ("sin", (byte) 0x18), // Reads a null-terminated string and |
54 | 77 | // stores its bytes in the top of the |
55 | 78 | // stack. |
| 79 | + HALT ("halt", (byte) 0xff, new HaltInstructionImplementation()); // Halts the machine. |
56 | 80 |
|
57 | 81 | private static final Map<String, Operation> mapOperationNameToOperationEnum |
58 | 82 | = new HashMap<>(); |
59 | 83 |
|
60 | 84 | private final byte opcodeByte; |
| 85 | + private final InstructionImplementation impl; |
61 | 86 |
|
62 | 87 | private Operation(final String name, |
63 | | - final byte opcodeByte) { |
| 88 | + final byte opcodeByte, |
| 89 | + final InstructionImplementation impl) { |
64 | 90 | this.opcodeByte = opcodeByte; |
| 91 | + this.impl = impl; |
65 | 92 | } |
66 | 93 |
|
67 | 94 | public Operation getOperation(final String operationName) { |
|
0 commit comments