creature/creature.go

300 lines
7.4 KiB
Go
Raw Normal View History

2022-08-28 11:25:00 -06:00
package creature
// Machine is a stack machine. It contains an array of integers as its program
// data, and provides methods to run this program data, as well as interact with
// it.
2022-08-28 11:25:00 -06:00
type Machine struct {
// Program is not modified by the machine, and can be freely set before
// the machine is started.
2022-08-28 15:52:39 -06:00
Program []int
stack []int
block []int
counter int
pointer int
2022-08-28 15:52:39 -06:00
functions map[int]MachineFunction
}
// MachineFunction is a function that can extend the functionality of the stack
// machine. It is passed a pointer to the machine that is calling it, and the
// machine will halt execution if true is returned.
2022-08-28 15:52:39 -06:00
type MachineFunction func(machine *Machine) (stop bool)
2022-08-28 16:34:23 -06:00
// All supported opcodes
const (
PUSH = 0x0
POP = 0x1
PEEK = 0x2
POKE = 0x3
2022-08-28 16:34:23 -06:00
ADD = 0x4
SUB = 0x5
MUL = 0x6
DIV = 0x7
EQ = 0x8
GT = 0x9
LT = 0xa
NEQ = 0xb
MOD = 0xc
HALT = 0xd
JMP = 0xe
CAL = 0xf
)
// Error is an error type that can be returned from the machine's methods if
// they experience problems.
type Error int
const (
// ErrorIDTaken is returned by Register when a new function is
// registered with the same ID as another one.
ErrorIDTaken Error = iota
ErrorUnknownInstruction
2022-08-28 16:20:09 -06:00
ErrorDivideByZero
)
// Error returns a textual description of the error.
2022-08-28 15:52:39 -06:00
func (err Error) Error() (description string) {
switch err {
case ErrorIDTaken:
description = "this ID is taken by another function"
case ErrorUnknownInstruction:
description = "machine encountered unknown instruction"
2022-08-28 16:20:09 -06:00
case ErrorDivideByZero:
description = "cannot divide by zero"
}
return
2022-08-28 11:25:00 -06:00
}
2022-08-28 15:35:30 -06:00
// Reset resets the stack of the machine. This should be called before Execute,
// since Execute does not reset the stack.
2022-08-28 15:52:39 -06:00
func (machine *Machine) Reset() {
machine.stack = nil
2022-08-28 15:35:30 -06:00
machine.pointer = 0
}
2022-08-28 11:30:54 -06:00
// 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. If an unknown instruction is encountered, the machine halts and
// returns an error.
func (machine *Machine) Execute(offset int) (err error) {
2022-08-28 11:25:00 -06:00
machine.counter = offset
for machine.counter < len(machine.Program) {
2022-08-28 11:30:54 -06:00
switch machine.instruction() {
2022-08-28 16:34:23 -06:00
case PUSH:
2022-08-28 11:30:54 -06:00
// push the next word in program memory onto the stack
2022-08-28 15:52:39 -06:00
machine.counter++
2022-08-28 11:30:54 -06:00
machine.Push(machine.instruction())
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case POP:
2022-08-28 11:30:54 -06:00
// pop the top word off of the stack, and discard it
machine.Pop()
2022-08-28 15:52:39 -06:00
case PEEK:
// push the word at an address onto the stack
machine.Push(machine.Peek(machine.Pop()))
2022-08-28 15:52:39 -06:00
case POKE:
// store a word at an address
2022-08-28 22:43:13 -06:00
address := machine.Pop()
word := machine.Pop()
machine.Poke(address, word)
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case ADD:
// adds the last two words on the stack
machine.Push(machine.Pop() + machine.Pop())
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case SUB:
2022-08-28 14:59:11 -06:00
// subtracts the second to last word on the stack from
// the last word on the stack
machine.Push(machine.Pop() - machine.Pop())
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case MUL:
// multiplies the last two words on the stack
machine.Push(machine.Pop() * machine.Pop())
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case DIV:
2022-08-28 14:59:11 -06:00
// divides the last word on the stack by the second to
// last word on the stack
2022-08-28 16:20:09 -06:00
left := machine.Pop()
right := machine.Pop()
2022-08-28 17:59:00 -06:00
if right == 0 {
2022-08-28 16:20:09 -06:00
err = ErrorDivideByZero
return
}
machine.Push(left / right)
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case EQ:
2022-08-28 14:59:11 -06:00
// checks if the last two words on the stack are equal
2022-08-28 15:03:15 -06:00
equal := 0
2022-08-28 15:52:39 -06:00
if machine.Pop() == machine.Pop() {
equal = 1
}
2022-08-28 15:03:15 -06:00
machine.Push(equal)
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case GT:
2022-08-28 15:03:15 -06:00
// checks if the last word on the stack is greater than
// the second to last word on the stack
greater := 0
2022-08-28 15:52:39 -06:00
if machine.Pop() > machine.Pop() {
greater = 1
}
2022-08-28 15:03:15 -06:00
machine.Push(greater)
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case LT:
2022-08-28 15:03:15 -06:00
// checks if the last word on the stack is less than the
// second to last word on the stack
less := 0
2022-08-28 18:13:55 -06:00
if machine.Pop() < machine.Pop() {
2022-08-28 15:52:39 -06:00
less = 1
}
2022-08-28 15:03:15 -06:00
machine.Push(less)
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case NEQ:
2022-08-28 15:03:15 -06:00
// checks if the last two words on the stack are not
// equal
notEqual := 0
2022-08-28 15:52:39 -06:00
if machine.Pop() != machine.Pop() {
notEqual = 1
}
2022-08-28 15:03:15 -06:00
machine.Push(notEqual)
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case MOD:
// performs a modulo operation of the second to last
// word on the stack to the last word on the stack
machine.Push(machine.Pop() % machine.Pop())
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case HALT:
// stops execution
return
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case JMP:
// jump to the address specified by the last word on the
// stack if the second to last word on the stack is
// nonzero
jumpTo := machine.Pop()
if machine.Pop() != 0 {
machine.counter = jumpTo - 1
}
2022-08-28 15:52:39 -06:00
2022-08-28 16:34:23 -06:00
case CAL:
// call an implementation-defined subroutine, with the
// id specified by the last word on the stack. this may
// push and pop various things from the stack
id := machine.Pop()
2022-08-28 15:52:39 -06:00
if machine.functions == nil {
break
}
function, exists := machine.functions[id]
if exists {
if function(machine) {
return
}
}
default:
err = ErrorUnknownInstruction
return
2022-08-28 11:25:00 -06:00
}
2022-08-28 15:52:39 -06:00
machine.counter++
2022-08-28 11:25:00 -06:00
}
return
2022-08-28 11:25:00 -06:00
}
2022-08-28 11:30:54 -06:00
// Instruction returns the current instruction in program memory.
2022-08-28 15:52:39 -06:00
func (machine *Machine) instruction() (instruction int) {
2022-08-28 11:30:54 -06:00
instruction = machine.Program[machine.counter]
return
}
2022-08-28 11:25:00 -06:00
// 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.
2022-08-28 15:52:39 -06:00
func (machine *Machine) reallocateStack() {
2022-08-28 16:26:44 -06:00
reallocatedStack := make([]int, machine.pointer*2)
2022-08-28 11:25:00 -06:00
copy(reallocatedStack, machine.stack)
machine.stack = reallocatedStack
}
// Push pushes a word onto the stack, increasing the stack pointer and
// reallocating the stack if necessary.
2022-08-28 15:52:39 -06:00
func (machine *Machine) Push(word int) {
machine.pointer++
2022-08-28 11:25:00 -06:00
if len(machine.stack) <= machine.pointer {
machine.reallocateStack()
}
2022-08-28 16:26:44 -06:00
machine.stack[machine.pointer] = word
2022-08-28 11:25:00 -06:00
}
// Pop pops the last word off of the stack, and returns it, decreasing the stack
// pointer and reallocating the stack if necessary.
2022-08-28 15:52:39 -06:00
func (machine *Machine) Pop() (word int) {
if machine.pointer <= 0 || machine.pointer >= len(machine.stack) {
return
}
2022-08-28 11:25:00 -06:00
word = machine.stack[machine.pointer]
2022-08-28 15:52:39 -06:00
machine.pointer--
2022-08-28 11:25:00 -06:00
2022-08-28 15:52:39 -06:00
if machine.pointer < len(machine.stack)/3 {
2022-08-28 11:25:00 -06:00
machine.reallocateStack()
}
2022-08-28 15:52:39 -06:00
2022-08-28 11:25:00 -06:00
return
}
2022-08-28 15:52:39 -06:00
// Peek returns the word at address in the block.
func (machine *Machine) Peek(address int) (word int) {
if address < len(machine.block) {
word = machine.block[address]
}
return
}
2022-08-28 15:52:39 -06:00
// Poke sets the value at address in the block to word.
func (machine *Machine) Poke(address int, word int) {
if address >= len(machine.block) {
2022-08-28 15:52:39 -06:00
reallocatedBlock := make([]int, address*3/2)
copy(reallocatedBlock, machine.block)
machine.block = reallocatedBlock
}
2022-08-28 22:43:13 -06:00
machine.block[address] = word
}
// Register registers a function at the specified ID. If there is already a
// function registered at that ID, this method will return an error.
2022-08-28 15:52:39 -06:00
func (machine *Machine) Register(id int, function MachineFunction) (err error) {
if machine.functions == nil {
2022-08-28 15:52:39 -06:00
machine.functions = make(map[int]MachineFunction)
}
_, exists := machine.functions[id]
if exists {
err = ErrorIDTaken
return
}
machine.functions[id] = function
return
}
2022-08-28 15:36:45 -06:00
// Unregister removes a function that is registered at the specified ID.
2022-08-28 15:52:39 -06:00
func (machine *Machine) Unregister(id int) {
if machine.functions == nil {
return
}
2022-08-28 15:36:45 -06:00
delete(machine.functions, id)
}
// LoadMemory loads the contents of block into the machine's memory.
2022-08-28 15:52:39 -06:00
func (machine *Machine) LoadMemory(block []int) {
machine.block = make([]int, len(block))
copy(machine.block, block)
}
2022-08-28 15:52:39 -06:00
// Milk milks the stack machine.
func (machine *Machine) Milk() {}