Finishing touches

This commit is contained in:
Imbus 2024-04-16 15:06:25 +02:00
parent 4227d3998b
commit a323782664
4 changed files with 27 additions and 29 deletions

View file

@ -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());
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}

View file

@ -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<Instruction> {}