aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-06-06 23:42:21 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-06-06 23:42:21 +0200
commit93c36b3f48569a25667de33d9796991bfe825681 (patch)
tree8ad7548897353c7cb8a787a4a91d1c349062630d
parent3a16aec44ccabc0a4bf7ffb96994a68e60fba2f4 (diff)
tests
-rw-r--r--eval.go43
-rw-r--r--eval_test.go19
2 files changed, 36 insertions, 26 deletions
diff --git a/eval.go b/eval.go
index e84aae8..28cef59 100644
--- a/eval.go
+++ b/eval.go
@@ -86,18 +86,18 @@ func (vm *J1) eval(ins Instruction) {
case ALU:
st0 = vm.newST0(v)
if v.RtoPC {
- pc = vm.rstack[vm.rsp]
+ pc = vm.rstack[vm.rsp-1]
}
if v.NtoAtT {
- vm.memory[vm.st0] = vm.dstack[vm.dsp]
+ vm.memory[vm.st0] = vm.dstack[vm.dsp-1]
}
dsp = uint16(int8(vm.dsp) + v.Ddir)
rsp = uint16(int8(vm.rsp) + v.Rdir)
if v.TtoR {
- vm.rstack[vm.rsp] = vm.st0
+ vm.rstack[rsp-1] = vm.st0
}
if v.TtoN {
- vm.dstack[vm.dsp] = vm.st0
+ vm.dstack[dsp-1] = vm.st0
}
}
@@ -107,47 +107,50 @@ func (vm *J1) eval(ins Instruction) {
vm.rsp = rsp
}
+func (vm *J1) T() uint16 { return vm.st0 }
+func (vm *J1) N() uint16 { return vm.dstack[vm.dsp-1] }
+func (vm *J1) R() uint16 { return vm.rstack[vm.rsp-1] }
+
func (vm *J1) newST0(v ALU) uint16 {
- T, N, R := vm.st0, vm.dstack[vm.dsp], vm.rstack[vm.rsp]
switch v.Opcode {
case 0: // T
- return T
+ return vm.T()
case 1: // N
- return N
+ return vm.N()
case 2: // T+N
- return T + N
+ return vm.T() + vm.N()
case 3: // T&N
- return T & N
+ return vm.T() & vm.N()
case 4: // T|N
- return T | N
+ return vm.T() | vm.N()
case 5: // T^N
- return T ^ N
+ return vm.T() ^ vm.N()
case 6: // ~T
- return ^T
+ return ^vm.T()
case 7: // N==T
- if N == T {
+ if vm.N() == vm.T() {
return 1
}
return 0
case 8: // N<T
- if int16(N) < int16(T) {
+ if int16(vm.N()) < int16(vm.T()) {
return 1
}
return 0
case 9: // N>>T
- return N >> (T & 0xf)
+ return vm.N() >> (vm.T() & 0xf)
case 10: // T-1
- return T - 1
+ return vm.T() - 1
case 11: // R (rT)
- return R
+ return vm.R()
case 12: // [T]
- return vm.memory[T]
+ return vm.memory[vm.T()]
case 13: // N<<T
- return N << (T & 0xf)
+ return vm.N() << (vm.T() & 0xf)
case 14: // depth (dsp)
return (vm.rsp << 8) | vm.dsp
case 15: // Nu<T
- if N < T {
+ if vm.N() < vm.T() {
return 1
}
return 0
diff --git a/eval_test.go b/eval_test.go
index c4f81a0..7f5f4e0 100644
--- a/eval_test.go
+++ b/eval_test.go
@@ -55,19 +55,24 @@ func TestEval(t *testing.T) {
end: J1{pc: 3, st0: 2, dsp: 2, dstack: [32]uint16{0, 3}},
},
{ // nip
- // ALU{Opcode: 0, Ddir: -1}
+ ins: []Instruction{Lit(2), Lit(3), ALU{Opcode: 0, Ddir: -1}},
+ end: J1{pc: 3, st0: 3, dsp: 1, dstack: [32]uint16{0, 2}},
},
{ // drop
- // ALU{Opcode: 1, Ddir: -1}
+ ins: []Instruction{Lit(2), Lit(3), ALU{Opcode: 1, Ddir: -1}},
+ end: J1{pc: 3, st0: 2, dsp: 1, dstack: [32]uint16{0, 2}},
},
{ // ;
- // ALU{Opcode: 0, RtoPC: true, Rdir: -1}
+ ins: []Instruction{Call(10), Call(20), ALU{Opcode: 0, RtoPC: true, Rdir: -1}},
+ end: J1{pc: 11, rsp: 1, rstack: [32]uint16{1, 11}},
},
{ // >r
- // ALU{Opcode: 1, TtoR: true, Ddir: -1, Rdir: 1}
+ ins: []Instruction{Lit(10), ALU{Opcode: 1, TtoR: true, Ddir: -1, Rdir: 1}},
+ end: J1{pc: 2, rsp: 1, rstack: [32]uint16{10}},
},
{ // r>
- // ALU{Opcode: 11, TtoN: true, TtoR: true, Ddir: 1, Rdir: -1}
+ // ins: []Instruction{Lit(10), Call(20), ALU{Opcode: 11, TtoN: true, TtoR: true, Ddir: 1, Rdir: -1}},
+ // end: J1{pc: 2, st0: 21, rsp: 1, rstack: [32]uint16{10}, dsp: 2, dstack: [32]uint16{10}},
},
{ // r@
// ALU{Opcode: 11, TtoN: true, TtoR: true, Ddir: 1}
@@ -76,7 +81,7 @@ func TestEval(t *testing.T) {
// ALU{Opcode: 12}
},
{ // !
- // ALU{Opcode: 1, Ddir: -1}
+ // ALU{Opcode: 1, NtoAtT: true, Ddir: -1}
},
}
@@ -87,6 +92,8 @@ func TestEval(t *testing.T) {
state.eval(ins)
}
if *state != tc.end {
+ t.Logf("D=%v", state.dstack)
+ t.Logf("R=%v", state.rstack)
t.Errorf("got %v, want %v", state, &tc.end)
}
})