Skip to content

Commit

Permalink
Builtin program parser output
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Apr 26, 2024
1 parent 8eeb769 commit 76e4205
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ for performance comparison see no_optimisation branch.
bf [option] source.bf [input]

Options:
-dump
dump parsed program
-eight
eight bit execution
-version
Expand Down
5 changes: 4 additions & 1 deletion cmd/bfg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func main() {
version := flag.Bool("version", false, "display version")
eight := flag.Bool("eight", false, "eight bit execution")
dump := flag.Bool("dump", false, "dump parsed program")

flag.Usage = func() {
fmt.Printf("Usage:\n %s [option] source.bf [input]\n", os.Args[0])
Expand Down Expand Up @@ -48,7 +49,9 @@ func main() {
fmt.Println("error compiling program: err:", err)
os.Exit(1)
}
if *eight {
if *dump {
parser.Dump(program, outputBuf)
} else if *eight {
data := make([]byte, parser.DataSize)
parser.Execute(data, program, inputBuf, outputBuf)
} else {
Expand Down
24 changes: 24 additions & 0 deletions parser/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package parser

import (
"bufio"
"fmt"
"strings"
)

// Dump prints out the parsed program.
func Dump(program []Instruction, writer *bufio.Writer) {
depth := 0
startLoop := NewInstruction('[')
endLoop := NewInstruction(']')
for idx, inst := range program {
if inst.SameOp(endLoop) {
depth--
}
fmt.Fprintln(writer, idx, strings.Repeat("\t", depth), inst.String())
if inst.SameOp(startLoop) {
depth++
}
}
writer.Flush()
}
31 changes: 31 additions & 0 deletions parser/dump_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package parser

import (
"bufio"
"bytes"
"reflect"
"testing"
)

func TestDump(t *testing.T) {
program := []Instruction{
{opNoop, 0},
{opAddDp, 5},
{opSetVal, 0},
{opAddVal, 5},
{opMove, 2},
{opJmpZ, 7},
{opIn, 1},
{opJmpNz, 5},
{opAddDp, 2},
}
var buf bytes.Buffer
outputBuf := bufio.NewWriter(&buf)
Dump(program, outputBuf)
got := buf.String()
want := "0 nop: 0\n1 ptr: 5\n2 set: 0\n3 add: 5\n4 mov: 2\n5 jmp: 7\n6 \t inp: 1\n7 jnz: 5\n8 ptr: 2\n"

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}
10 changes: 5 additions & 5 deletions parser/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func (inst Instruction) String() string {
"add",
"set",
"out",
"in",
"jmpz",
"jmpnz",
"inp",
"jmp",
"jnz",
"mov",
"skip",
"skp",
}
return fmt.Sprintf("%s:%v", opName[inst.operator], inst.operand)
return fmt.Sprintf("%s: %v", opName[inst.operator], inst.operand)
}

// NewInstruction created from a sourcecode byte
Expand Down

0 comments on commit 76e4205

Please sign in to comment.