This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sandal.go
78 lines (64 loc) · 1.39 KB
/
sandal.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
package main
import (
"fmt"
"github.com/jessevdk/go-flags"
"github.com/k0kubun/sandal/lang"
"io/ioutil"
"log"
"os"
)
type Options struct {
Ast bool `short:"a" long:"ast" default:"false" description:"dump parsed ast"`
Ir1 bool `short:"1" long:"ir1" default:"false" description:"dump IR1"`
Ir2 bool `short:"2" long:"ir2" default:"false" description:"dump IR2"`
Graph bool `short:"g" long:"graph" default:"false" description:"dump state transition to dot lang"`
}
func run(filePath string) {
body, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal(filePath, err)
}
compiled, err := lang.CompileFile(string(body))
if err != nil {
log.Fatalf("%s: %s", filePath, err.Error())
}
fmt.Print(compiled)
}
func processOptions(filePath string, options *Options) bool {
body, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal(filePath, err)
}
if options.Ast {
lang.DumpAST(string(body))
return true
}
if options.Ir1 {
lang.DumpIR1(string(body))
return true
}
if options.Ir2 {
lang.DumpIR2(string(body))
return true
}
if options.Graph {
lang.DumpGraph(string(body))
return true
}
return false
}
func main() {
options := new(Options)
args, err := flags.Parse(options)
if err != nil {
return
}
if len(args) != 1 {
fmt.Println("Usage: sandal [programfile]")
os.Exit(1)
}
if processOptions(args[0], options) {
return
}
run(args[0])
}