-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
95 lines (80 loc) · 1.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Command nux executes Uxn ROMs on a Varvara machine.
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime/pprof"
"github.com/nf/nux/varvara"
)
func main() {
log.SetPrefix("nux: ")
log.SetFlags(0)
var (
cliFlag = flag.Bool("cli", false, "disable GUI features")
devFlag = flag.Bool("dev", false, "enable developer mode (live re-build and run an untxal program)")
debugFlag = flag.Bool("debug", false, "enable debugger (implies -dev)")
cpuProfileFlag = flag.String("cpu_profile", "", "write CPU profile to `file`")
)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [-cli] <program.rom | program.tal>\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s [-cli] <-dev | -debug> <program.tal>\n", os.Args[0])
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
}
if *devFlag || *debugFlag {
if err := devMode(!*cliFlag, *debugFlag, flag.Arg(0)); err != nil {
log.Fatal(err)
}
return
}
var cpuProfile io.Closer
if prof := *cpuProfileFlag; prof != "" {
f, err := os.Create(prof)
if err != nil {
log.Fatalf("creating CPU profile file: %v", err)
}
pprof.StartCPUProfile(f)
cpuProfile = f
}
code, err := run(flag.Arg(0), !*cliFlag)
if f := cpuProfile; f != nil {
pprof.StopCPUProfile()
f.Close()
}
if err != nil {
log.Fatal(err)
}
os.Exit(code)
}
func run(romFile string, guiEnabled bool) (int, error) {
var (
rom []byte
err error
)
if filepath.Ext(romFile) == ".tal" {
tmp, err := os.MkdirTemp("", "nux-build-*")
if err != nil {
return 0, err
}
defer os.RemoveAll(tmp)
talFile := romFile
romFile = filepath.Join(tmp, filepath.Base(talFile)+".rom")
rom, err = devBuild(os.Stderr, talFile, romFile)
} else {
rom, err = os.ReadFile(romFile)
}
if err != nil {
return 0, err
}
r := varvara.NewRunner(guiEnabled, false, nil)
code := r.Run(rom)
return code, nil
}