Skip to content

Commit 9492fb8

Browse files
committed
...
1 parent 935e129 commit 9492fb8

File tree

4 files changed

+462
-32
lines changed

4 files changed

+462
-32
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>io.github.coderodde.simple.stack.machine</groupId>
5-
<artifactId>SimpleStackMachine</artifactId>
5+
<artifactId>SimpleStackMachine.java</artifactId>
66
<version>1.0.0</version>
77
<packaging>jar</packaging>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1010
<maven.compiler.release>24</maven.compiler.release>
1111
<exec.mainClass>io.github.coderodde.simple.stack.machine.SimpleStackMachine</exec.mainClass>
1212
</properties>
13+
<name>SimpleStackMachine.java</name>
1314
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3+
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
4+
*/
5+
package io.github.coderodde.simple.stack.machine;
6+
7+
/**
8+
*
9+
* @author rodio
10+
*/
11+
public class CompareInstructionImplementation {
12+
13+
}

src/main/java/io/github/coderodde/simple/stack/machine/Operation.java

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
package io.github.coderodde.simple.stack.machine;
22

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;
320
import java.util.HashMap;
421
import java.util.Map;
522

@@ -12,56 +29,66 @@
1229
*/
1330
public enum Operation {
1431

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
2138
// at a given address of the tape.
2239

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+
3452
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.
3856

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
4058
// 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.
4361
JNZ ("jnz", (byte) 0x12), // Jump if zero flag is not set.
4462
JBZ ("jbz", (byte) 0x13), // Jump if the "below zero" flag is set.
4563
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.
4770

4871
PRINT_INT ("iout", (byte) 0x15), // Prints the top most number.
4972
PRINT_STRING ("sout", (byte) 0x16), // Prints the string with bytes on the
5073
// 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
5275
// 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
5477
// stores its bytes in the top of the
5578
// stack.
79+
HALT ("halt", (byte) 0xff, new HaltInstructionImplementation()); // Halts the machine.
5680

5781
private static final Map<String, Operation> mapOperationNameToOperationEnum
5882
= new HashMap<>();
5983

6084
private final byte opcodeByte;
85+
private final InstructionImplementation impl;
6186

6287
private Operation(final String name,
63-
final byte opcodeByte) {
88+
final byte opcodeByte,
89+
final InstructionImplementation impl) {
6490
this.opcodeByte = opcodeByte;
91+
this.impl = impl;
6592
}
6693

6794
public Operation getOperation(final String operationName) {

0 commit comments

Comments
 (0)