Skip to content

Instantly share code, notes, and snippets.

@saterus
Created June 24, 2011 22:37
Show Gist options
  • Save saterus/1045824 to your computer and use it in GitHub Desktop.
Save saterus/1045824 to your computer and use it in GitHub Desktop.
Instructions Idea
public interface Instruction {
/** Executes an instruction on the Machine.
* @param m The machine upon which to execute the instruction.
* @return true if the executed instruction modified a general purpose register.
*/
public boolean execute(Machine m);
}
public class AddImmediate implements Instruction {
private final Register dest;
private final Register src;
private final short immediate;
public AddImmediate(Register dest, Register src, short immediate) {
this.dest = dest;
this.src = src;
this.immediate = immediate;
}
public boolean execute(Machine m) {
m.setRegister(this.dest, this.src.get() + this.immediate);
return true;
}
}
public class DebugInstruction implements Instruction {
public boolean execute(Machine m) {
m.Syscalls.showDebug(m);
return false;
}
}
public class TrapOUTN implements Instruction {
public boolean execute(Machine m) {
m.Syscalls.outN(m.getRegister(RegisterBank.R0));
return false;
}
}
public class Interpreter {
// ...
public void runInstruction(String rawInstruction) {
Instruction instruction = this.parser.parse(rawInstruction);
boolean wroteToRegBank = instruction.execute(this.machine);
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment