diff --git a/creature.go b/creature.go index 7e6b945..63f1b29 100644 --- a/creature.go +++ b/creature.go @@ -8,13 +8,24 @@ type Machine struct { pointer int } +// Execute starts execution at the address specified by offset. The machine will +// run until a HALT instruction is encountered, or the end of program memory is +// reached. func (machine *Machine) Execute (offset int) { machine.counter = offset for { - switch machine.Program[machine.counter] { + switch machine.instruction() { case 0x0: + // PUSH + // push the next word in program memory onto the stack + machine.counter ++ + machine.Push(machine.instruction()) + case 0x1: + // POP + // pop the top word off of the stack, and discard it + machine.Pop() case 0x2: case 0x3: case 0x4: @@ -34,6 +45,12 @@ func (machine *Machine) Execute (offset int) { } } +// Instruction returns the current instruction in program memory. +func (machine *Machine) instruction () (instruction int) { + instruction = machine.Program[machine.counter] + return +} + // reallocateStack changes the size of the stack to something reasonable. This // should be called then the stack pointer is really small compared to the // actual stack size, or the stack pointer is bigger than the stack.