Skip to content

Commit

Permalink
Print iterator.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Nov 1, 2024
1 parent 7345dcb commit a3cff3a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 73 deletions.
111 changes: 55 additions & 56 deletions parser/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parser
import (
"bufio"
"fmt"
"iter"
"strings"
)

Expand All @@ -20,68 +21,66 @@ func repeatDirection(neg, pos string, vect int) string {
return strings.Repeat(neg, abs(vect))
}

func instPrint(inst, lastInst, lastlastInst Instruction) string {
switch inst.operator {
case opAddDp:
return repeatDirection("<", ">", inst.operand)
case opAddVal:
return repeatDirection("-", "+", inst.operand)
case opSetVal:
prefix := "[-]"
if lastInst.IsZeroOp() && inst.operand != 0 {
prefix = ""
}
return prefix + repeatDirection("-", "+", inst.operand)
case opOut:
return "."
case opIn:
return ","
case opJmpZ:
return "["
case opJmpNz:
return "]"
case opMove:
return "[-" + repeatDirection("<", ">", inst.operand) + "+" + repeatDirection(">", "<", inst.operand) + "]"
case opMovN:
return "[-" + repeatDirection("<", ">", inst.operand) + "-" + repeatDirection(">", "<", inst.operand) + "]"
case opSkip:
return "[" + repeatDirection("<", ">", inst.operand) + "]"
case opNoop:
if lastInst.operator == opMulVal {
multiplier := repeatDirection("-", "+", inst.operand)
return "[-" + repeatDirection("<", ">", lastInst.operand) + multiplier + repeatDirection(">", "<", lastInst.operand) + "]"
} else if lastInst.operator == opDupVal {
return "[-" + repeatDirection("<", ">", lastInst.operand) + "+" + repeatDirection("<", ">", inst.operand-lastInst.operand) + "+" + repeatDirection(">", "<", inst.operand) + "]"
} else if lastlastInst.operator == opVec {
multiplier := repeatDirection("-", "+", lastInst.operand)
return "[-" + repeatDirection("<", ">", lastlastInst.operand) + multiplier + repeatDirection("<", ">", inst.operand-lastlastInst.operand) + multiplier + repeatDirection(">", "<", inst.operand) + "]"
func instPrints(program []Instruction) iter.Seq[string] {
return func(yield func(string) bool) {

depth := 0
indent := ""
for pc, inst := range program {
indent = strings.Repeat("\t", depth)
str := ""
switch inst.operator {
case opAddDp:
str = repeatDirection("<", ">", inst.operand)
case opAddVal:
str = repeatDirection("-", "+", inst.operand)
case opSetVal:
prefix := "[-]"
if program[pc-1].IsZeroOp() && inst.operand != 0 {
prefix = ""
}
str = prefix + repeatDirection("-", "+", inst.operand)
case opOut:
str = "."
case opIn:
str = ","
case opJmpZ:
str = "["
depth++
case opJmpNz:
str = "]"
depth--
indent = strings.Repeat("\t", depth)
case opMove:
str = "[-" + repeatDirection("<", ">", inst.operand) + "+" + repeatDirection(">", "<", inst.operand) + "]"
case opMovN:
str = "[-" + repeatDirection("<", ">", inst.operand) + "-" + repeatDirection(">", "<", inst.operand) + "]"
case opSkip:
str = "[" + repeatDirection("<", ">", inst.operand) + "]"
case opMulVal:
multiplier := repeatDirection("-", "+", program[pc+1].operand)
str = "[-" + repeatDirection("<", ">", inst.operand) + multiplier + repeatDirection(">", "<", inst.operand) + "]"
case opDupVal:
str = "[-" + repeatDirection("<", ">", inst.operand) + "+" + repeatDirection("<", ">", program[pc+1].operand-inst.operand) + "+" + repeatDirection(">", "<", program[pc+1].operand) + "]"
case opVec:
multiplier := repeatDirection("-", "+", program[pc+1].operand)
str = "[-" + repeatDirection("<", ">", inst.operand) + multiplier + repeatDirection("<", ">", program[pc+2].operand-inst.operand) + multiplier + repeatDirection(">", "<", program[pc+2].operand) + "]"
case opNoop:
continue
default:
return
}
if !yield(indent + str) {
return
}
}
return ""
default:
return ""
}
}

// Print pretty prints out the parsed program.
func Print(program []Instruction, writer *bufio.Writer) {
depth := 0
startLoop := NewInstruction('[')
endLoop := NewInstruction(']')
lastInst := NewInstruction('!')
lastlastInst := NewInstruction('!')
for _, inst := range program {
if inst.SameOp(endLoop) {
depth--
}
printout := instPrint(inst, lastInst, lastlastInst)
if printout != "" {
fmt.Fprintln(writer, strings.Repeat("\t", depth), printout)
}
if inst.SameOp(startLoop) {
depth++
}
lastlastInst = lastInst
lastInst = inst
for printout := range instPrints(program) {
fmt.Fprintln(writer, printout)
}
writer.Flush()
}
34 changes: 17 additions & 17 deletions parser/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (
"testing"
)

var testprogram = ` >>>>>
[-]+++++
[->>+<<]
>
[
,
[<<<]
++
[->>++<<]
>>
[->+>+<<]
>
[->+++>+++<<]
]
>>
.
[->>-<<]
var testprogram = `>>>>>
[-]+++++
[->>+<<]
>
[
,
[<<<]
++
[->>++<<]
>>
[->+>+<<]
>
[->+++>+++<<]
]
>>
.
[->>-<<]
`

func TestPrint(t *testing.T) {
Expand Down

0 comments on commit a3cff3a

Please sign in to comment.