Fixed issue with pushing to stack
This commit is contained in:
parent
850de8a2bf
commit
21aaaede8e
|
@ -207,7 +207,7 @@ func (machine *Machine) instruction() (instruction int) {
|
||||||
// should be called then the stack pointer is really small compared to the
|
// 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.
|
// actual stack size, or the stack pointer is bigger than the stack.
|
||||||
func (machine *Machine) reallocateStack() {
|
func (machine *Machine) reallocateStack() {
|
||||||
reallocatedStack := make([]int, machine.pointer*3/2)
|
reallocatedStack := make([]int, machine.pointer*2)
|
||||||
copy(reallocatedStack, machine.stack)
|
copy(reallocatedStack, machine.stack)
|
||||||
machine.stack = reallocatedStack
|
machine.stack = reallocatedStack
|
||||||
}
|
}
|
||||||
|
@ -219,6 +219,7 @@ func (machine *Machine) Push(word int) {
|
||||||
if len(machine.stack) <= machine.pointer {
|
if len(machine.stack) <= machine.pointer {
|
||||||
machine.reallocateStack()
|
machine.reallocateStack()
|
||||||
}
|
}
|
||||||
|
machine.stack[machine.pointer] = word
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop pops the last word off of the stack, and returns it, decreasing the stack
|
// Pop pops the last word off of the stack, and returns it, decreasing the stack
|
||||||
|
|
|
@ -2,6 +2,28 @@ package creature
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
|
func TestPush(test *testing.T) {
|
||||||
|
machine := Machine {
|
||||||
|
Program: []int {
|
||||||
|
0x0, 654,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := machine.Execute(0)
|
||||||
|
if err != nil {
|
||||||
|
test.Log("machine exited with error:", err)
|
||||||
|
test.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
result := machine.Pop()
|
||||||
|
test.Log("popped:", result)
|
||||||
|
|
||||||
|
if result != 654 {
|
||||||
|
test.Log("result should be", 654)
|
||||||
|
test.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestArithmetic(test *testing.T) {
|
func TestArithmetic(test *testing.T) {
|
||||||
machine := Machine {
|
machine := Machine {
|
||||||
Program: []int {
|
Program: []int {
|
||||||
|
|
Loading…
Reference in New Issue