Skip to content

Commit

Permalink
Remove dump option and improve flags. v0.1.0 complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed May 27, 2024
1 parent e70aab7 commit b0b9237
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 99 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 19 additions & 12 deletions cmd/bfg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
24 changes: 0 additions & 24 deletions parser/dump.go

This file was deleted.

31 changes: 0 additions & 31 deletions parser/dump_test.go

This file was deleted.

22 changes: 0 additions & 22 deletions parser/instruction.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package parser

import (
"fmt"
)

// Instruction structure for intermediate program
type Instruction struct {
operator Opcode
Expand All @@ -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 {
Expand Down
19 changes: 18 additions & 1 deletion parser/instruction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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]

Expand Down

0 comments on commit b0b9237

Please sign in to comment.