From 165716953fd5ca68a1d9df917147ae948b6fdf2b Mon Sep 17 00:00:00 2001 From: Tristan Morgan Date: Tue, 7 May 2024 17:13:34 +1000 Subject: [PATCH] Drop unreachable code. --- parser/tokenise.go | 8 +++++++- parser/tokenise_test.go | 27 +++++++++++++++------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/parser/tokenise.go b/parser/tokenise.go index 1160efa..81adb73 100644 --- a/parser/tokenise.go +++ b/parser/tokenise.go @@ -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}) diff --git a/parser/tokenise_test.go b/parser/tokenise_test.go index acde61e..9649dea 100644 --- a/parser/tokenise_test.go +++ b/parser/tokenise_test.go @@ -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}, @@ -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}, }, }, }