Skip to content

Commit

Permalink
Drop unreachable code.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed May 7, 2024
1 parent 441770a commit 1657169
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
8 changes: 7 additions & 1 deletion parser/tokenise.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ func Tokenise(input io.ByteReader) (program []Instruction, err error) {
jmpStack = jmpStack[:len(jmpStack)-1]
program[pc].operand = jmpPc
program[jmpPc].operand = pc
if pc-jmpPc == 2 && program[pc-1].SameOp(NewInstruction('+')) {
if jmpPc == 1 ||
program[jmpPc-1].Complement(Instruction{opSetVal, 0}) ||
program[jmpPc-1].SameOp(NewInstruction(']')) {
pc = jmpPc
program = program[:pc]
pc--
} else if pc-jmpPc == 2 && program[pc-1].SameOp(NewInstruction('+')) {
pc = jmpPc
program = program[:pc]
program = append(program, Instruction{opSetVal, 0})
Expand Down
27 changes: 15 additions & 12 deletions parser/tokenise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ func TestTokenise(t *testing.T) {
},
{
"op_mul",
" [<++++++>-]>[->>---<<]",
"+[<++++++>-]>[->>---<<]",
[]Instruction{
{opNoop, 0},
{opAddVal, 1},
{opMulVal, -1}, // dest value pointer
{opNoop, 6}, // multiplication factor
{opAddDp, 1},
Expand All @@ -58,59 +59,61 @@ func TestTokenise(t *testing.T) {
},
{
"op_skip",
"[>>>>>]",
">[>>>>>]",
[]Instruction{
{opNoop, 0},
{opAddDp, 1},
{opSkip, 5},
},
},
{
"op_move",
"[->>+<<][<<<+>>>-]",
">[->>+<<]>[<<<+>>>-]",
[]Instruction{
{opNoop, 0},
{opAddDp, 1},
{opMove, 2},
{opAddDp, 1},
{opMove, -3},
},
},
{
"op_jmp_z_nz",
"[->>+>+<<<]",
">[->>+>+<<<]",
[]Instruction{
{opNoop, 0},
{opJmpZ, 8},
{opAddDp, 1},
{opJmpZ, 9},
{opAddVal, -1},
{opAddDp, 2},
{opAddVal, 1},
{opAddDp, 1},
{opAddVal, 1},
{opAddDp, -3},
{opJmpNz, 1},
{opJmpNz, 2},
},
},
{
"op_nested",
"[[[[[[[,]]]]]]][comment.]",
">[[[[[[[,]]]]]]][comment.]",
[]Instruction{
{opNoop, 0},
{opAddDp, 1},
{opJmpZ, 16},
{opJmpZ, 15},
{opJmpZ, 14},
{opJmpZ, 13},
{opJmpZ, 12},
{opJmpZ, 11},
{opJmpZ, 10},
{opJmpZ, 9},
{opIn, 1},
{opJmpNz, 8},
{opJmpNz, 7},
{opJmpNz, 6},
{opJmpNz, 5},
{opJmpNz, 4},
{opJmpNz, 3},
{opJmpNz, 2},
{opJmpNz, 1},
{opJmpZ, 18},
{opOut, 1},
{opJmpNz, 16},
},
},
}
Expand Down

0 comments on commit 1657169

Please sign in to comment.