From 76e42053e4648f7ae75991d95903e388092facbe Mon Sep 17 00:00:00 2001 From: Tristan Morgan Date: Fri, 26 Apr 2024 10:08:50 +1000 Subject: [PATCH] Builtin program parser output --- README.md | 2 ++ cmd/bfg/main.go | 5 ++++- parser/dump.go | 24 ++++++++++++++++++++++++ parser/dump_test.go | 31 +++++++++++++++++++++++++++++++ parser/instruction.go | 10 +++++----- 5 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 parser/dump.go create mode 100644 parser/dump_test.go diff --git a/README.md b/README.md index 3e49fbc..759476a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmd/bfg/main.go b/cmd/bfg/main.go index afb8dbc..1b1b37f 100644 --- a/cmd/bfg/main.go +++ b/cmd/bfg/main.go @@ -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]) @@ -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 { diff --git a/parser/dump.go b/parser/dump.go new file mode 100644 index 0000000..98b1065 --- /dev/null +++ b/parser/dump.go @@ -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() +} diff --git a/parser/dump_test.go b/parser/dump_test.go new file mode 100644 index 0000000..ffeb0ee --- /dev/null +++ b/parser/dump_test.go @@ -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) + } +} diff --git a/parser/instruction.go b/parser/instruction.go index 14ccb1e..d188a65 100644 --- a/parser/instruction.go +++ b/parser/instruction.go @@ -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