package creature type Machine struct { stack []int Program []int Block []int counter int pointer int } func (machine *Machine) Execute (offset int) { machine.counter = offset for { switch machine.Program[machine.counter] { case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7: case 0x8: case 0x9: case 0xa: case 0xb: case 0xc: case 0xd: case 0xe: case 0xf: } machine.counter ++ } } // 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. func (machine *Machine) reallocateStack () { reallocatedStack := make([]int, machine.pointer * 3 / 2) copy(reallocatedStack, machine.stack) machine.stack = reallocatedStack } // Push pushes a word onto the stack, increasing the stack pointer and // reallocating the stack if necessary. func (machine *Machine) Push (word int) { machine.pointer ++ if len(machine.stack) <= machine.pointer { machine.reallocateStack() } } // Pop pops the last word off of the stack, and returns it, decreasing the stack // pointer and reallocating the stack if necessary. func (machine *Machine) Pop () (word int) { word = machine.stack[machine.pointer] machine.pointer -- if machine.pointer < len(machine.stack) / 3 { machine.reallocateStack() } return }