-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (54 loc) · 1.15 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
package main
import (
"./pokeparser"
"./pokeprinter"
"./poketoken"
"flag"
"fmt"
"os"
"os/exec"
"path"
"strings"
)
func createOutfile(outfile string) *os.File {
f, err := os.Create(outfile)
if err != nil {
panic(err)
}
return f
}
func run(outfile *os.File, args []string) {
goExecPath, err := exec.LookPath("go")
progArgs := []string{goExecPath, "run", outfile.Name()}
progArgs = append(progArgs, args...)
cmd := exec.Cmd{
Path: goExecPath,
Args: progArgs,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
err = cmd.Run()
if err != nil {
panic(err)
}
}
func main() {
fileName := flag.String("filename", "", "name of the file to compile")
flag.Parse()
args := flag.Args()
fset := poketoken.NewFileSet()
exp, err := pokeparser.ParseFile(fset, *fileName, nil, 0)
if err != nil {
fmt.Printf("parsing failed: %s", err)
return
}
fileBase := strings.TrimSuffix(*fileName, path.Ext(*fileName))
outfileName := fmt.Sprintf("/tmp/%s.go", fileBase)
outfile := createOutfile(outfileName)
err = pokeprinter.Fprint(outfile, poketoken.NewFileSet(), exp)
if err != nil {
fmt.Printf("output failed: %s", err)
}
run(outfile, args)
fmt.Printf("\n")
}