Skip to content

Commit

Permalink
simplify print logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Aug 22, 2024
1 parent ad80fdc commit 126fd0b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
10 changes: 4 additions & 6 deletions parser/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,13 @@ func Print(program []Instruction, writer *bufio.Writer) {
endLoop := NewInstruction(']')
lastInst := NewInstruction('!')
for _, inst := range program {
if inst.operator == opMulVal ||
inst.operator == opDupVal {
lastInst = inst
continue
}
if inst.SameOp(endLoop) {
depth--
}
fmt.Fprintln(writer, strings.Repeat("\t", depth), instPrint(inst, lastInst))
printout := instPrint(inst, lastInst)
if printout != "" {
fmt.Fprintln(writer, strings.Repeat("\t", depth), printout)
}
if inst.SameOp(startLoop) {
depth++
}
Expand Down
2 changes: 1 addition & 1 deletion parser/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestPrint(t *testing.T) {
outputBuf := bufio.NewWriter(&buf)
Print(program, outputBuf)
got := buf.String()
want := " \n >>>>>\n [-]\n +++++\n [->>+<<]\n [\n\t ,\n\t [<<<]\n\t ++\n\t [->>++<<]\n\t [->+>+<<]\n ]\n >>\n .\n [->>-<<]\n"
want := " >>>>>\n [-]\n +++++\n [->>+<<]\n [\n\t ,\n\t [<<<]\n\t ++\n\t [->>++<<]\n\t [->+>+<<]\n ]\n >>\n .\n [->>-<<]\n"

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

0 comments on commit 126fd0b

Please sign in to comment.