Skip to content

Commit

Permalink
Collapse more nested loops.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Aug 6, 2024
1 parent f37b8ce commit ad80fdc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
44 changes: 32 additions & 12 deletions parser/tokenise.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,22 @@ func Tokenise(input io.ByteReader) (program []Instruction, err error) {
jmpStack = jmpStack[:len(jmpStack)-1]
program[pc].operand = jmpPc
program[jmpPc].operand = pc

redOk, reduceInst := reduceInst(program[pc-1])
switch {
case program[jmpPc-1].IsZeroOp():
pc = jmpPc
program = program[:pc]
pc--
case pc-jmpPc == 2 &&
(program[pc-1].SameOp(NewInstruction('+')) ||
program[pc-1].operator == opSetVal):
case pc-jmpPc == 2 && redOk:
pc = jmpPc
if program[jmpPc-1].SameOp(NewInstruction('+')) {
if (program[jmpPc-1].operator == opAddVal ||
program[jmpPc-1].operator == opSetVal) &&
reduceInst.operator == opSetVal {
pc--
}
program = program[:pc]
program = append(program, Instruction{opSetVal, 0})
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})
program = append(program, reduceInst)
case pc-jmpPc == 5: // looking for opMulVal and opMove
pointers, factors, ok := evalFactors(program[jmpPc+1 : pc])

Expand Down Expand Up @@ -138,3 +133,28 @@ func evalFactors(program []Instruction) (pointers, factors []int, ok bool) {
}
return
}

func reduceInst(inst Instruction) (bool, Instruction) {
switch inst.operator {
case opAddDp:
return true, Instruction{opSkip, inst.operand}
case opSkip:
return true, inst
case opAddVal:
if inst.operand == 1 || inst.operand == -1 {
return true, Instruction{opSetVal, 0}
}
return false, Instruction{opNoop, 0}
case opSetVal:
if inst.operand == 0 {
return true, inst
}
return false, Instruction{opNoop, 0}
case opMove:
return true, inst
case opMovN:
return true, inst
default:
return false, Instruction{opNoop, 0}
}
}
19 changes: 14 additions & 5 deletions parser/tokenise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestTokenise(t *testing.T) {
}{
{
"small_prog",
">>>>>++[-]zero+++++>+++++[->>+<<]move>>[>>-<<-]movn",
">>>>>++[-]zero+++++>+++++[[->>+<<]move]>>[[>>-<<-]movn]",
[]Instruction{
{opNoop, 0},
{opAddDp, 5},
Expand All @@ -31,7 +31,7 @@ func TestTokenise(t *testing.T) {
},
{
"op_mul",
"+[<++++++>-]>[->>---<<]",
"+[<++++++>-]>[+>>+++<<]",
[]Instruction{
{opNoop, 0},
{opSetVal, 1},
Expand Down Expand Up @@ -71,14 +71,15 @@ func TestTokenise(t *testing.T) {
},
{
"op_move",
">[->>+<<]>[<<<+>>>-]++",
">[->>+<<]>[<<<+>>>-]++[[>-<-]]",
[]Instruction{
{opNoop, 0},
{opAddDp, 1},
{opMove, 2},
{opAddDp, 1},
{opMove, -3},
{opSetVal, 2},
{opMovN, 1},
},
},
{
Expand All @@ -96,7 +97,7 @@ func TestTokenise(t *testing.T) {
},
{
"op_jmp_z_nz",
">[->>,>+<<<]",
">[->>,>+<<<]>[<<+>>--]",
[]Instruction{
{opNoop, 0},
{opAddDp, 1},
Expand All @@ -108,15 +109,23 @@ func TestTokenise(t *testing.T) {
{opAddVal, 1},
{opAddDp, -3},
{opJmpNz, 2},
{opAddDp, 1},
{opJmpZ, 16},
{opAddDp, -2},
{opAddVal, 1},
{opAddDp, 2},
{opAddVal, -2},
{opJmpNz, 11},
},
},
{
"op_nested",
">[[[[[[[>]]]]]]][comment.]",
">[[[[[[[>]]]]]]][comment.]+++[[-]]",
[]Instruction{
{opNoop, 0},
{opAddDp, 1},
{opSkip, 1},
{opSetVal, 0},
},
},
}
Expand Down

0 comments on commit ad80fdc

Please sign in to comment.