diff --git a/README.md b/README.md index 7bfde76..4bb48c2 100644 --- a/README.md +++ b/README.md @@ -31,17 +31,12 @@ for performance comparison see no_optimisation branch. ## Usage Usage: - bf [option] source.bf [input] + bfg [option] source.bf [input] Options: - -dump - dump parsed program - -eight - eight bit execution - -print - pretty print parsed program - -version - display version + -e, --eight eight bit execution + -p, --print pretty print parsed program + -v, --version display version May use - as source to read program from STDIN and output is STDOUT diff --git a/cmd/bfg/main.go b/cmd/bfg/main.go index 22bbd7f..122d8c8 100644 --- a/cmd/bfg/main.go +++ b/cmd/bfg/main.go @@ -11,9 +11,16 @@ import ( "github.com/tristanmorgan/bfg/parser" ) +const usage = ` +Options: + -e, --eight eight bit execution + -p, --print pretty print parsed program + -v, --version display version +` + var ( // Version is the main version number that is being run at the moment. - Version = "0.0.1" + Version = "0.1.0" // VersionPrerelease is A pre-release marker for the Version. If this is "" // (empty string) then it means that it is a final release. Otherwise, this @@ -22,19 +29,21 @@ var ( ) 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") - print := flag.Bool("print", false, "pretty print parsed program") + var version, eight, print bool + flag.BoolVar(&version, "v", false, "display version") + flag.BoolVar(&version, "version", false, "display version") + flag.BoolVar(&eight, "e", false, "eight bit execution") + flag.BoolVar(&eight, "eight", false, "eight bit execution") + flag.BoolVar(&print, "p", false, "pretty print parsed program") + flag.BoolVar(&print, "print", false, "pretty print parsed program") flag.Usage = func() { fmt.Printf("Usage:\n %s [option] source.bf [input]\n", os.Args[0]) - fmt.Println("\nOptions:") - flag.PrintDefaults() + fmt.Print(usage) } flag.Parse() - if *version { + if version { fmt.Printf("Version: v%s%s\n", Version, VersionPrerelease) fmt.Println("https://github.com/tristanmorgan/bfg") os.Exit(0) @@ -61,11 +70,9 @@ func main() { fmt.Println("error compiling program: err:", err) os.Exit(1) } - if *dump { - parser.Dump(program, outputBuf) - } else if *print { + if print { parser.Print(program, outputBuf) - } else if *eight { + } 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 deleted file mode 100644 index 98b1065..0000000 --- a/parser/dump.go +++ /dev/null @@ -1,24 +0,0 @@ -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 deleted file mode 100644 index ffeb0ee..0000000 --- a/parser/dump_test.go +++ /dev/null @@ -1,31 +0,0 @@ -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 39c315c..3d6a681 100644 --- a/parser/instruction.go +++ b/parser/instruction.go @@ -1,9 +1,5 @@ package parser -import ( - "fmt" -) - // Instruction structure for intermediate program type Instruction struct { operator Opcode @@ -27,24 +23,6 @@ const ( opMulVal ) -// String representation of Instruction -func (inst Instruction) String() string { - opName := [...]string{ - "nop", - "ptr", - "add", - "set", - "out", - "inp", - "jmp", - "jnz", - "mov", - "skp", - "mul", - } - return fmt.Sprintf("%s: %v", opName[inst.operator], inst.operand) -} - // NewInstruction created from a sourcecode byte func NewInstruction(chr byte) Instruction { switch chr { diff --git a/parser/instruction_test.go b/parser/instruction_test.go index 64652dd..ef32b9c 100644 --- a/parser/instruction_test.go +++ b/parser/instruction_test.go @@ -6,6 +6,23 @@ import ( "testing" ) +func (inst Instruction) string() string { + opName := [...]string{ + "nop", + "ptr", + "add", + "set", + "out", + "inp", + "jmp", + "jnz", + "mov", + "skp", + "mul", + } + return fmt.Sprintf("%s: %v", opName[inst.operator], inst.operand) +} + func TestNewInstruction(t *testing.T) { sourceCode := "g><+-.,[]" program := []Instruction{ @@ -21,7 +38,7 @@ func TestNewInstruction(t *testing.T) { } for idx, val := range []byte(sourceCode) { - t.Run(program[idx].String(), func(t *testing.T) { + t.Run(program[idx].string(), func(t *testing.T) { got := NewInstruction(val) want := program[idx]