From a3237826645c4114b76eba6ba9907ccc8bfc9ee0 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 16 Apr 2024 15:06:25 +0200 Subject: [PATCH] Finishing touches --- app/src/main/java/Programs/Factorial.java | 20 ++++++++++++++++++++ app/src/main/java/hatelace/Computer.java | 2 +- app/src/main/java/hatelace/Main.java | 17 +++-------------- app/src/main/java/hatelace/Program.java | 17 +++-------------- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/Programs/Factorial.java b/app/src/main/java/Programs/Factorial.java index e69de29..bc5fb38 100644 --- a/app/src/main/java/Programs/Factorial.java +++ b/app/src/main/java/Programs/Factorial.java @@ -0,0 +1,20 @@ +package Programs; + +import hatelace.*; +import hatelace.instructions.*; + +public class Factorial extends Program { + public Factorial() { + Address n = new Address(0), + fac = new Address(1); + + add(new Copy(new IntWord(5), n)); + add(new Copy(new IntWord(1), fac)); + add(new JumpEq(6, n, new IntWord(1))); + add(new Mul(fac, n, fac)); + add(new Add(n, new IntWord(-1), n)); + add(new Jump(2)); + add(new Print(fac)); + add(new Halt()); + } +} \ No newline at end of file diff --git a/app/src/main/java/hatelace/Computer.java b/app/src/main/java/hatelace/Computer.java index 50d762c..f8dcf99 100644 --- a/app/src/main/java/hatelace/Computer.java +++ b/app/src/main/java/hatelace/Computer.java @@ -15,7 +15,7 @@ public class Computer { public void run() { // Note that the instructions themselves are responsible for incrementing the PC for(ProgramCounter PC = new ProgramCounter(); PC.getPC() < this.program.size() && !PC.halted() && PC.getSysTicks() < 100; ) { - program.getInstructions()[PC.getPC()].execute(this.memory, PC); + program.get(PC.getPC()).execute(this.memory, PC); } } } diff --git a/app/src/main/java/hatelace/Main.java b/app/src/main/java/hatelace/Main.java index f06c53e..9dcb7f1 100644 --- a/app/src/main/java/hatelace/Main.java +++ b/app/src/main/java/hatelace/Main.java @@ -1,6 +1,6 @@ package hatelace; -import hatelace.instructions.*; +import Programs.Factorial; public class Main { public static void main(String[] args) { @@ -8,26 +8,15 @@ public class Main { Memory memory = new IntMemory(64); // 64 words of memory Computer computer = new Computer(memory); + Program program = new Factorial(); - Address n = new Address(0), fac = new Address(1); - - Program program = new Program(new Instruction[] { - new Copy(new IntWord(5), n), - new Copy(new IntWord(1), fac), - new JumpEq(6, n, new IntWord(1)), - new Mul(fac, n, fac), - new Add(n, new IntWord(-1), n), - new Jump(2), - new Print(fac), - new Halt() - }); computer.load(program); computer.run(); memory.dump(); // Print the program System.out.println("Program:"); - for (Instruction instruction : program.getInstructions()) { + for (Instruction instruction : program) { System.out.println(instruction); } diff --git a/app/src/main/java/hatelace/Program.java b/app/src/main/java/hatelace/Program.java index 859b86b..510478f 100644 --- a/app/src/main/java/hatelace/Program.java +++ b/app/src/main/java/hatelace/Program.java @@ -1,17 +1,6 @@ package hatelace; -public class Program { - private Instruction[] instructions; +import java.util.ArrayList; - public Program(Instruction[] instructions) { - this.instructions = instructions; - } - - public Instruction[] getInstructions() { - return this.instructions; - } - - public int size() { - return this.instructions.length; - } -} +// This is simply an intermediate class to comply with the specified API requirements. +public abstract class Program extends ArrayList {}