Skip to content

Commit

Permalink
Rework flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Feb 14, 2024
1 parent 3fc9dfd commit 30d378b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
60 changes: 41 additions & 19 deletions cmd/bfg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,58 @@ package main

import (
"bufio"
"flag"
"fmt"
"io"
"os"

"github.com/tristanmorgan/bfg/parser"
)

func main() {
args := os.Args
if len(args) < 2 {
fmt.Printf("Usage: %s file\n", args[0])
return
version := flag.Bool("version", false, "display version")

flag.Usage = func() {
fmt.Printf("Usage:\n %s [option] source.bf [input]\n", os.Args[0])
fmt.Println("\nOptions:")
flag.PrintDefaults()
}
file, err := os.Open(args[1])
if err != nil {
fmt.Println("Error reading source file")
return

flag.Parse()
if *version {
fmt.Println("Version 0.0.1")
os.Exit(0)
}
program, err := parser.Compile(bufio.NewReader(file))
if err != nil {
fmt.Println(err)
return

var sourceBuf, inputBuf io.ByteReader
source := flag.Arg(0)
input := flag.Arg(1)
if source == "" {
fmt.Println("please provide a source file")
os.Exit(1)
} else {
sourceFile, err := os.Open(source)
if err != nil {
fmt.Println("error opening source file: err:", err)
os.Exit(1)
}
sourceBuf = bufio.NewReader(sourceFile)
}
input := bufio.NewReader(os.Stdin)
if len(args) > 2 {
file, err = os.Open(args[2])
if input == "" {
inputBuf = bufio.NewReader(os.Stdin)
} else {
file, err := os.Open(input)
if err != nil {
fmt.Println("Error reading input file")
return
fmt.Println("error opening input file: err:", err)
os.Exit(1)
}
input = bufio.NewReader(file)
inputBuf = bufio.NewReader(file)
}

program, err := parser.Compile(sourceBuf)
if err != nil {
fmt.Println("error compiling program: err:", err)
os.Exit(1)
}
parser.Execute(program, input)
parser.Execute(program, inputBuf)
}
6 changes: 5 additions & 1 deletion parser/instruction.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package parser

import (
"fmt"
)

// Instruction structure for intermediate program
type Instruction struct {
operator Opcode
Expand Down Expand Up @@ -32,7 +36,7 @@ func (inst Instruction) String() string {
"zero",
"mov",
}
return opName[inst.operator]
return fmt.Sprintf("%s: %v", opName[inst.operator], inst.operand)
}

func (inst Instruction) SameOp(instTwo Instruction) bool {
Expand Down

0 comments on commit 30d378b

Please sign in to comment.