Skip to content

Commit

Permalink
Switch to switch and collapse nested loops.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed May 31, 2024
1 parent d57dec3 commit 1b8ff35
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion parser/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type bufferWriter struct {

func (writer *bufferWriter) Write(p []byte) (int, error) {
writer.buf = append(writer.buf, p...)
return 0, nil
return len(p), nil
}

func BenchmarkExecute(b *testing.B) {
Expand Down
1 change: 1 addition & 0 deletions parser/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ func (inst Instruction) IsZeroOp() bool {
return inst.operator == opJmpNz ||
inst.operator == opNoop ||
inst.operator == opMove ||
inst.operator == opSkip ||
(inst.operator == opSetVal && inst.operand == 0)
}
2 changes: 2 additions & 0 deletions parser/instruction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func TestIsZeroOp(t *testing.T) {
{opSetVal, -1},
{opSetVal, 0},
{opOut, 1},
{opSkip, 1},
{opIn, 1},
{opJmpZ, 0},
{opJmpNz, 0},
Expand All @@ -69,6 +70,7 @@ func TestIsZeroOp(t *testing.T) {
false,
true,
false,
true,
false,
false,
true,
Expand Down
13 changes: 9 additions & 4 deletions parser/tokenise.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,28 @@ func Tokenise(input io.ByteReader) (program []Instruction, err error) {
jmpStack = jmpStack[:len(jmpStack)-1]
program[pc].operand = jmpPc
program[jmpPc].operand = pc
if program[jmpPc-1].IsZeroOp() {
switch {
case program[jmpPc-1].IsZeroOp():
pc = jmpPc
program = program[:pc]
pc--
} else if pc-jmpPc == 2 && program[pc-1].SameOp(NewInstruction('+')) {
case pc-jmpPc == 2 &&
(program[pc-1].SameOp(NewInstruction('+')) ||
program[pc-1].operator == opSetVal):
pc = jmpPc
if program[jmpPc-1].SameOp(NewInstruction('+')) {
pc--
}
program = program[:pc]
program = append(program, Instruction{opSetVal, 0})
} else if pc-jmpPc == 2 && program[pc-1].SameOp(NewInstruction('>')) {
case pc-jmpPc == 2 &&
(program[pc-1].SameOp(NewInstruction('>')) ||
program[pc-1].operator == opSkip):
offset := program[pc-1].operand
pc = jmpPc
program = program[:pc]
program = append(program, Instruction{opSkip, offset})
} else if pc-jmpPc == 5 { // looking for opMulVal and opMove
case pc-jmpPc == 5: // looking for opMulVal and opMove
var factor, offset = 0, 0
if program[pc-4].Complement(NewInstruction('+')) &&
program[pc-3].Complement(program[pc-1]) &&
Expand Down
18 changes: 2 additions & 16 deletions parser/tokenise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,11 @@ func TestTokenise(t *testing.T) {
},
{
"op_nested",
">[[[[[[[,]]]]]]][comment.]",
">[[[[[[[>]]]]]]][comment.]",
[]Instruction{
{opNoop, 0},
{opAddDp, 1},
{opJmpZ, 16},
{opJmpZ, 15},
{opJmpZ, 14},
{opJmpZ, 13},
{opJmpZ, 12},
{opJmpZ, 11},
{opJmpZ, 10},
{opIn, 1},
{opJmpNz, 8},
{opJmpNz, 7},
{opJmpNz, 6},
{opJmpNz, 5},
{opJmpNz, 4},
{opJmpNz, 3},
{opJmpNz, 2},
{opSkip, 1},
},
},
}
Expand Down

0 comments on commit 1b8ff35

Please sign in to comment.