Skip to content

Commit

Permalink
cmd: --bdump, --bload
Browse files Browse the repository at this point in the history
  • Loading branch information
wkhere committed May 6, 2024
1 parent 35e30aa commit e7ec9bd
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/bcl
*.bcl
*.bcb
/cov
/cov.html
/tr[0-9]
Expand Down
2 changes: 1 addition & 1 deletion NEXT
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ more on errors:
- reflection: allow filling toplevel structs of different types

- when the language and vm is stable, port to Python, Ruby, Zig, ...
- way to port in small steps is to make prog struct fully serializable
~ way to port in small steps is to make prog struct fully serializable
and port the vm first

- sort out scopes for processing multiple files (one prog vs many progs)
Expand Down
67 changes: 63 additions & 4 deletions cmd/bcl/args.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package main

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

type parsedArgs struct {
file string
file string

disasm bool
trace bool
result bool
stats bool
bdump bool
bload bool

bdumpFile string
bloadFile string

help func()
}

const usage = "usage: bcl [-d|--disasm] [-t|--trace] [-r|--result] [-s|--stats] [FILE|-]"
const usage = "usage: bcl" +
" [-d|--disasm] [-t|--trace] [-r|--result] [-s|--stats]" +
" [--bdump|--bdump=BFILE] [--bload|--bload=BFILE]" +
" [FILE|-]"

func parseArgs(args []string) (a parsedArgs, _ error) {
var rest []string
Expand Down Expand Up @@ -40,6 +52,28 @@ flags:
a.stats = true
continue

case strings.HasPrefix(arg, "--bdump"):
a.bdump = true
s := arg[len("--bdump"):]
if len(s) > 0 {
if s[0] != '=' {
return a, fmt.Errorf("unknown flag: %s\n%s", arg, usage)
}
a.bdumpFile = s[1:]
}
continue

case strings.HasPrefix(arg, "--bload"):
a.bload = true
s := arg[len("--bload"):]
if len(s) > 0 {
if s[0] != '=' {
return a, fmt.Errorf("unknown flag: %s\n%s", arg, usage)
}
a.bloadFile = s[1:]
}
continue

case arg == "--":
rest = append(rest, args[1:]...)
break flags
Expand Down Expand Up @@ -72,12 +106,37 @@ flags:

switch len(rest) {
case 0:
a.file = "-"
case 1:
a.file = rest[0]
default:
return a, fmt.Errorf("too many file args\n%s", usage)
}

if a.bdump && a.bdumpFile == "" {
if !strings.HasSuffix(a.file, ".bcl") {
return a, fmt.Errorf(
"--bdump requires knowing bfile name, "+
"either given as a flag, or derived from BCL file name"+
"\n%s",
usage,
)
}
a.bdumpFile = a.file[:len(a.file)-len(".bcl")] + ".bcb"
}

if a.bload {
switch {
case a.file == "" && a.bloadFile == "":
// logic below to load prog from stdin
case a.file != "" && a.bloadFile != "":
return a, fmt.Errorf("conflicting BFILE and FILE\n%s", usage)
case a.file == "" && a.bloadFile != "":
a.file = a.bloadFile
}
}

if a.file == "" {
a.file = "-"
}
return a, nil
}
43 changes: 40 additions & 3 deletions cmd/bcl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,39 @@ func run(a *parsedArgs) (err error) {
return err
}

res, err := bcl.InterpretFile(
f,
bcl.OptDisasm(a.disasm),
var prog *bcl.Prog

if a.bload {
prog, err = bcl.LoadProg(
f, a.file,
bcl.OptDisasm(a.disasm),
)
} else {
prog, err = bcl.ParseFile(
f,
bcl.OptDisasm(a.disasm),
bcl.OptStats(a.stats),
)
}
if err != nil {
return err
}

if a.bdump {
bf, err := os.Create(a.bdumpFile)
if err != nil {
return fmt.Errorf("dump: %w", err)
}

err = prog.Dump(bf)
safeClose(bf, &err)
if err != nil {
return fmt.Errorf("dump: %w", err)
}
}

res, err := bcl.Execute(
prog,
bcl.OptTrace(a.trace),
bcl.OptStats(a.stats),
)
Expand Down Expand Up @@ -56,3 +86,10 @@ func die(exitcode int, err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(exitcode)
}

func safeClose(f *os.File, errp *error) {
cerr := f.Close()
if cerr != nil && *errp == nil {
*errp = cerr
}
}

0 comments on commit e7ec9bd

Please sign in to comment.