aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-06-15 09:27:13 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-06-15 09:27:13 +0200
commit65eaac67536b0dd0755dd10b32b5c1088c9976fb (patch)
treedbba741a1ae61902d5a6948f5bb4394e6466e68f
parentda312e375eb0a0758a4dd72e287d3aba86c04d99 (diff)
Name OPs
-rw-r--r--eval.go32
-rw-r--r--eval_test.go64
-rw-r--r--parse.go32
3 files changed, 64 insertions, 64 deletions
diff --git a/eval.go b/eval.go
index 387e993..c7277be 100644
--- a/eval.go
+++ b/eval.go
@@ -119,43 +119,43 @@ func (vm *J1) eval(ins Instruction) {
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
+ case opT: // T
return T
- case 1: // N
+ case opN: // N
return N
- case 2: // T+N
+ case opTplusN: // T+N
return T + N
- case 3: // T&N
+ case opTandN: // T&N
return T & N
- case 4: // T|N
+ case opTorN: // T|N
return T | N
- case 5: // T^N
+ case opTxorN: // T^N
return T ^ N
- case 6: // ~T
+ case opNotT: // ~T
return ^T
- case 7: // N==T
+ case opNeqT: // N==T
if N == T {
return 1
}
return 0
- case 8: // N<T
+ case opNleT: // N<T
if int16(N) < int16(T) {
return 1
}
return 0
- case 9: // N>>T
+ case opNrshiftT: // N>>T
return N >> (T & 0xf)
- case 10: // T-1
+ case opTminus1: // T-1
return T - 1
- case 11: // R (rT)
+ case opR: // R (rT)
return R
- case 12: // [T]
+ case opAtT: // [T]
return vm.memory[T]
- case 13: // N<<T
+ case opNlshiftT: // N<<T
return N << (T & 0xf)
- case 14: // depth (dsp)
+ case opDepth: // depth (dsp)
return (vm.rsp << 8) | vm.dsp
- case 15: // Nu<T
+ case opNuleT: // Nu<T
if N < T {
return 1
}
diff --git a/eval_test.go b/eval_test.go
index ae24629..fb9f4fc 100644
--- a/eval_test.go
+++ b/eval_test.go
@@ -35,55 +35,55 @@ func TestEval(t *testing.T) {
end: J1{pc: 2, st0: 0xfe, dstack: [32]uint16{0x00, 0x00, 0xff}, dsp: 2},
},
{ // dup
- ins: []Instruction{Lit(0xff), ALU{Opcode: 0, TtoN: true, Ddir: 1}},
+ ins: []Instruction{Lit(0xff), ALU{Opcode: opT, TtoN: true, Ddir: 1}},
end: J1{pc: 2, st0: 0xff, dstack: [32]uint16{0x00, 0x00, 0xff}, dsp: 2},
},
{ // over
- ins: []Instruction{Lit(0xaa), Lit(0xbb), ALU{Opcode: 1, TtoN: true, Ddir: 1}},
+ ins: []Instruction{Lit(0xaa), Lit(0xbb), ALU{Opcode: opN, TtoN: true, Ddir: 1}},
end: J1{pc: 3, st0: 0xaa, dstack: [32]uint16{0x00, 0x00, 0xaa, 0xbb}, dsp: 3},
},
{ // invert
- ins: []Instruction{Lit(0x00ff), ALU{Opcode: 6}},
+ ins: []Instruction{Lit(0x00ff), ALU{Opcode: opNotT}},
end: J1{pc: 2, st0: 0xff00, dsp: 1},
},
{ // +
- ins: []Instruction{Lit(1), Lit(2), ALU{Opcode: 2, Ddir: -1}},
+ ins: []Instruction{Lit(1), Lit(2), ALU{Opcode: opTplusN, Ddir: -1}},
end: J1{pc: 3, st0: 3, dsp: 1, dstack: [32]uint16{0, 0, 1}},
},
{ // swap
- ins: []Instruction{Lit(2), Lit(3), ALU{Opcode: 1, TtoN: true}},
+ ins: []Instruction{Lit(2), Lit(3), ALU{Opcode: opN, TtoN: true}},
end: J1{pc: 3, st0: 2, dsp: 2, dstack: [32]uint16{0, 0, 3}},
},
{ // nip
- ins: []Instruction{Lit(2), Lit(3), ALU{Opcode: 0, Ddir: -1}},
+ ins: []Instruction{Lit(2), Lit(3), ALU{Opcode: opT, Ddir: -1}},
end: J1{pc: 3, st0: 3, dsp: 1, dstack: [32]uint16{0, 0, 2}},
},
{ // drop
- ins: []Instruction{Lit(2), Lit(3), ALU{Opcode: 1, Ddir: -1}},
+ ins: []Instruction{Lit(2), Lit(3), ALU{Opcode: opN, Ddir: -1}},
end: J1{pc: 3, st0: 2, dsp: 1, dstack: [32]uint16{0, 0, 2}},
},
{ // ;
- ins: []Instruction{Call(10), Call(20), ALU{Opcode: 0, RtoPC: true, Rdir: -1}},
+ ins: []Instruction{Call(10), Call(20), ALU{Opcode: opT, RtoPC: true, Rdir: -1}},
end: J1{pc: 11, rsp: 1, rstack: [32]uint16{0, 1, 11}},
},
{ // >r
- ins: []Instruction{Lit(10), ALU{Opcode: 1, TtoR: true, Ddir: -1, Rdir: 1}},
+ ins: []Instruction{Lit(10), ALU{Opcode: opN, TtoR: true, Ddir: -1, Rdir: 1}},
end: J1{pc: 2, rsp: 1, rstack: [32]uint16{0, 10}},
},
{ // r>
- ins: []Instruction{Lit(10), Call(20), ALU{Opcode: 11, TtoN: true, TtoR: true, Ddir: 1, Rdir: -1}},
+ ins: []Instruction{Lit(10), Call(20), ALU{Opcode: opR, TtoN: true, TtoR: true, Ddir: 1, Rdir: -1}},
end: J1{pc: 21, st0: 2, dsp: 2, dstack: [32]uint16{0, 0, 10}, rsp: 0, rstack: [32]uint16{10, 2}},
},
{ // r@
- ins: []Instruction{Lit(10), ALU{Opcode: 11, TtoN: true, TtoR: true, Ddir: 1}},
+ ins: []Instruction{Lit(10), ALU{Opcode: opR, TtoN: true, TtoR: true, Ddir: 1}},
end: J1{pc: 2, dsp: 2, dstack: [32]uint16{0, 0, 10}, rstack: [32]uint16{10}},
},
{ // @
- ins: []Instruction{ALU{Opcode: 12}},
+ ins: []Instruction{ALU{Opcode: opAtT}},
end: J1{pc: 1},
},
{ // !
- ins: []Instruction{Lit(1), Lit(0), ALU{Opcode: 1, NtoAtT: true, Ddir: -1}},
+ ins: []Instruction{Lit(1), Lit(0), ALU{Opcode: opN, NtoAtT: true, Ddir: -1}},
end: J1{pc: 3, st0: 1, dsp: 1, dstack: [32]uint16{0, 0, 1}, memory: [0x8000]uint16{1}},
},
}
@@ -109,25 +109,25 @@ func TestNextST0(t *testing.T) {
st0 uint16
state J1
}{
- {ins: ALU{Opcode: 0}, st0: 0xff, state: J1{st0: 0xff}},
- {ins: ALU{Opcode: 1}, st0: 0xbb, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
- {ins: ALU{Opcode: 2}, st0: 0x01ba, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
- {ins: ALU{Opcode: 3}, st0: 0xbb, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
- {ins: ALU{Opcode: 4}, st0: 0xff, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
- {ins: ALU{Opcode: 5}, st0: 0x44, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
- {ins: ALU{Opcode: 6}, st0: 0xff55, state: J1{st0: 0xaa}},
- {ins: ALU{Opcode: 7}, st0: 0x00, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
- {ins: ALU{Opcode: 7}, st0: 0x01, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xff}, dsp: 2}},
- {ins: ALU{Opcode: 8}, st0: 0x01, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
- {ins: ALU{Opcode: 8}, st0: 0x00, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xff}, dsp: 2}},
- {ins: ALU{Opcode: 9}, st0: 0x3f, state: J1{st0: 0x02, dstack: [32]uint16{0, 0xaa, 0xff}, dsp: 2}},
- {ins: ALU{Opcode: 10}, st0: 0x54, state: J1{st0: 0x55}},
- {ins: ALU{Opcode: 11}, st0: 0x5, state: J1{rstack: [32]uint16{0, 0x05}, rsp: 1}},
- {ins: ALU{Opcode: 12}, st0: 0x5, state: J1{st0: 0x01, memory: [0x8000]uint16{0, 5, 10}}},
- {ins: ALU{Opcode: 13}, st0: 0x3fc, state: J1{st0: 0x02, dstack: [32]uint16{0, 0xaa, 0xff}, dsp: 2}},
- {ins: ALU{Opcode: 14}, st0: 0x305, state: J1{rsp: 3, dsp: 5}},
- {ins: ALU{Opcode: 15}, st0: 0x01, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
- {ins: ALU{Opcode: 15}, st0: 0x00, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xff}, dsp: 2}},
+ {ins: ALU{Opcode: opT}, st0: 0xff, state: J1{st0: 0xff}},
+ {ins: ALU{Opcode: opN}, st0: 0xbb, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
+ {ins: ALU{Opcode: opTplusN}, st0: 0x01ba, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
+ {ins: ALU{Opcode: opTandN}, st0: 0xbb, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
+ {ins: ALU{Opcode: opTorN}, st0: 0xff, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
+ {ins: ALU{Opcode: opTxorN}, st0: 0x44, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
+ {ins: ALU{Opcode: opNotT}, st0: 0xff55, state: J1{st0: 0xaa}},
+ {ins: ALU{Opcode: opNeqT}, st0: 0x00, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
+ {ins: ALU{Opcode: opNeqT}, st0: 0x01, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xff}, dsp: 2}},
+ {ins: ALU{Opcode: opNleT}, st0: 0x01, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
+ {ins: ALU{Opcode: opNleT}, st0: 0x00, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xff}, dsp: 2}},
+ {ins: ALU{Opcode: opNrshiftT}, st0: 0x3f, state: J1{st0: 0x02, dstack: [32]uint16{0, 0xaa, 0xff}, dsp: 2}},
+ {ins: ALU{Opcode: opTminus1}, st0: 0x54, state: J1{st0: 0x55}},
+ {ins: ALU{Opcode: opR}, st0: 0x5, state: J1{rstack: [32]uint16{0, 0x05}, rsp: 1}},
+ {ins: ALU{Opcode: opAtT}, st0: 0x5, state: J1{st0: 0x01, memory: [0x8000]uint16{0, 5, 10}}},
+ {ins: ALU{Opcode: opNlshiftT}, st0: 0x3fc, state: J1{st0: 0x02, dstack: [32]uint16{0, 0xaa, 0xff}, dsp: 2}},
+ {ins: ALU{Opcode: opDepth}, st0: 0x305, state: J1{rsp: 3, dsp: 5}},
+ {ins: ALU{Opcode: opNuleT}, st0: 0x01, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xbb}, dsp: 2}},
+ {ins: ALU{Opcode: opNuleT}, st0: 0x00, state: J1{st0: 0xff, dstack: [32]uint16{0, 0xaa, 0xff}, dsp: 2}},
}
for _, tc := range testCases {
t.Run(fmt.Sprint(tc.ins), func(t *testing.T) {
diff --git a/parse.go b/parse.go
index 2e66b26..bf39ee8 100644
--- a/parse.go
+++ b/parse.go
@@ -85,22 +85,22 @@ func expand(v uint16) int8 {
}
const (
- opT = iota
- opN
- opTplusN
- opTandN
- opTorN
- opTxorN
- opNotT
- opNeqT
- opNleT
- opNrshiftT
- opTminus1
- opR
- opAtT
- opNlshiftT
- opDepth
- opNuleT
+ opT = iota // 0
+ opN // 1
+ opTplusN // 2
+ opTandN // 3
+ opTorN // 4
+ opTxorN // 5
+ opNotT // 6
+ opNeqT // 7
+ opNleT // 8
+ opNrshiftT // 9
+ opTminus1 // 10
+ opR // 11
+ opAtT // 12
+ opNlshiftT // 13
+ opDepth // 14
+ opNuleT // 15
)
var opcodes = []string{