Skip to content

Commit

Permalink
Test Instructions and slim opMove code.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Feb 15, 2024
1 parent 8251691 commit f9a065d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 24 deletions.
26 changes: 6 additions & 20 deletions parser/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,13 @@ func Compile(input io.ByteReader) (program []Instruction, err error) {
program = append(program, Instruction{opZero, 0})
}
if pc-jmpPc == 5 &&
program[pc-4].operator == opAddVal &&
program[pc-3].operator == opAddDp &&
program[pc-2].operator == opAddVal &&
program[pc-1].operator == opAddDp &&
program[pc-4].operand == -1 &&
program[pc-2].operand == 1 &&
program[pc-3].operand+program[pc-1].operand == 0 {
offset := program[pc-3].operand
pc -= 5
program = program[:pc]
program = append(program, Instruction{opMove, offset})
}
if pc-jmpPc == 5 &&
program[pc-4].operator == opAddDp &&
program[pc-3].operator == opAddVal &&
program[pc-2].operator == opAddDp &&
program[pc-1].operator == opAddVal &&
program[pc-3].operand == 1 &&
program[pc-1].operand == -1 &&
program[pc-4].operand+program[pc-2].operand == 0 {
program[pc-4].Complement(program[pc-2]) &&
program[pc-3].Complement(program[pc-1]) &&
!program[pc-2].SameOp(program[pc-1]) {
offset := program[pc-4].operand
if program[pc-3].operator == opAddDp {
offset = program[pc-3].operand
}
pc -= 5
program = program[:pc]
program = append(program, Instruction{opMove, offset})
Expand Down
4 changes: 2 additions & 2 deletions parser/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestCompile(t *testing.T) {
Instruction{opJmpZ, 11},
Instruction{opJmpZ, 10},
Instruction{opJmpZ, 9},
Instruction{opIn, 0},
Instruction{opIn, 1},
Instruction{opJmpNz, 7},
Instruction{opJmpNz, 6},
Instruction{opJmpNz, 5},
Expand All @@ -87,7 +87,7 @@ func TestCompile(t *testing.T) {
Instruction{opJmpNz, 2},
Instruction{opJmpNz, 1},
Instruction{opJmpZ, 18},
Instruction{opOut, 0},
Instruction{opOut, 1},
Instruction{opJmpNz, 16},
},
},
Expand Down
9 changes: 7 additions & 2 deletions parser/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func NewInstruction(chr byte) Instruction {
case '-':
return Instruction{opAddVal, -1}
case '.':
return Instruction{opOut, 0}
return Instruction{opOut, 1}
case ',':
return Instruction{opIn, 0}
return Instruction{opIn, 1}
case '[':
return Instruction{opJmpZ, 0}
case ']':
Expand All @@ -69,3 +69,8 @@ func NewInstruction(chr byte) Instruction {
func (inst Instruction) SameOp(instTwo Instruction) bool {
return inst.operator == instTwo.operator
}

// Complement compares Instructions operator and operand
func (inst Instruction) Complement(instTwo Instruction) bool {
return inst.SameOp(instTwo) && inst.operand+instTwo.operand == 0
}
62 changes: 62 additions & 0 deletions parser/instruction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package parser

import (
"fmt"
"reflect"
"testing"
)

func TestNewInstruction(t *testing.T) {
sourceCode := "g><+-.,[]"
program := []Instruction{
Instruction{opNoop, 0},
Instruction{opAddDp, 1},
Instruction{opAddDp, -1},
Instruction{opAddVal, 1},
Instruction{opAddVal, -1},
Instruction{opOut, 1},
Instruction{opIn, 1},
Instruction{opJmpZ, 0},
Instruction{opJmpNz, 0},
}

for idx, val := range []byte(sourceCode) {
t.Run(program[idx].String(), func(t *testing.T) {
got := NewInstruction(val)
want := program[idx]

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
}
}

func TestSameOp(t *testing.T) {
opsList := []Opcode{
opNoop,
opAddDp,
opAddVal,
opOut,
opIn,
opJmpZ,
opJmpNz,
opZero,
opMove,
}

for row, rval := range opsList {
for col, cval := range opsList {
t.Run(fmt.Sprintf("%d-%d", row, col), func(t *testing.T) {
rinst := Instruction{rval, col}
cinst := Instruction{cval, row}
want := row == col

if rinst.SameOp(cinst) != want {
t.Errorf("testing %v vs %v want %v", rinst, cinst, want)
}
})
}
}

}
2 changes: 2 additions & 0 deletions sample/bubblesort-2.bf → sample/bubblesort.bf
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Here is a Brainf*** program that bubblesorts its input and spits it out:
[>>>[-<<<-<+>[>]>>]<<<[<]>>
[>>>+<<<-]<[>+>>>+<<<<-]
<<]>>>[-.[-]]>>>[>>>]<<<]
++++++++++.
Empty file modified sample/insertionsort.bf
100644 → 100755
Empty file.

0 comments on commit f9a065d

Please sign in to comment.