-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (172 loc) · 4.42 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2022 Massimo Comuzzo, Michele Pasqua and Marino Miculan
// SPDX-License-Identifier: Apache-2.0
package main
import (
"flag"
"fmt"
"io"
"os"
"runtime"
"github.com/abu-lang/abuc/internal/compiler"
"github.com/abu-lang/abuc/internal/compiler/version"
)
func main() {
flag.Usage = func() { printUsage(flag.CommandLine.Output()) }
config := ""
output := ""
system := runtime.GOOS
target := runtime.GOARCH
version_opt := false
intermediate := false
flag.StringVar(&config, "config", config, "configuration file for target")
flag.StringVar(&config, "c", config, "configuration file for target")
flag.BoolVar(&intermediate, "intermediate", false, "generate intermediate (single node) .abu files")
flag.BoolVar(&intermediate, "i", false, "generate intermediate (single node) .abu files")
flag.StringVar(&output, "output", output, "output file name for the compiled source")
flag.StringVar(&output, "o", output, "output file name for the compiled source")
flag.StringVar(&system, "system", system, "target operating system")
flag.StringVar(&system, "s", system, "target operating system")
flag.StringVar(&target, "target", target, "target language or architecture")
flag.StringVar(&target, "t", target, "target language or architecture")
flag.BoolVar(&version_opt, "version", false, "print version information and exit")
flag.BoolVar(&version_opt, "v", false, "print version information and exit")
flag.Parse()
if version_opt {
fmt.Println("abuc version " + version.Get())
fmt.Println(version.License)
os.Exit(0)
}
source := flag.Arg(0)
if intermediate {
target = "abu"
}
switch target {
case "go", "abu", "amd64", "arm64", "arm":
default:
fmt.Fprintf(os.Stderr, "unknown target %s\n", target)
os.Exit(1)
}
preprocs, symbolTable := preprocess(source, func(errs []error) {
for _, err := range errs {
fmt.Fprintln(os.Stderr, err.Error())
}
os.Exit(2)
})
compiler, err := compiler.MakeCompileStrategy(system, target, output, config)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(4)
}
outcomes := make(chan struct {
device string
errs []error
})
for d, ts := range preprocs {
d := d
ts := ts
go func(out chan<- struct {
device string
errs []error
},
) {
o := struct {
device string
errs []error
}{
device: d,
}
st, err := symbolTable.DeviceSymbolTable(d)
if err != nil {
o.errs = []error{err}
} else {
o.errs = compiler.Compile(d, ts, st)
}
out <- o
}(outcomes)
}
remaining := len(preprocs)
ok := true
for remaining > 0 {
o := <-outcomes
remaining--
if len(o.errs) > 0 {
ok = false
fmt.Fprintln(os.Stderr, o.device+":")
for _, err := range o.errs {
fmt.Fprintln(os.Stderr, ` `, err.Error())
}
}
}
err = compiler.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
if err != nil || !ok {
os.Exit(5)
}
}
type flagInfo struct {
short string
long string
typ string
usage string
def string
}
// printUsage prints abuc usage on an io.Writer.
// It should be called after flag.Parse()
func printUsage(out io.Writer) error {
_, err := fmt.Fprintf(out, "Usage of %s:\n", os.Args[0])
if err != nil {
return err
}
for _, f := range getFlagInfos() {
if f.typ == "boolean" {
_, err = fmt.Fprintf(out, " -%s, --%s :\t%s\n\n", f.short, f.long, f.usage)
} else {
_, err = fmt.Fprintf(out, " -%s, --%s %s\n \t%s %s\n\n", f.short, f.long, f.typ, f.usage, f.def)
}
if err != nil {
return err
}
}
_, err = fmt.Fprintln(out, "Get help/Report bugs/Provide suggestions at: https://github.com/abu-lang/abuc")
return err
}
// getFlagInfos returns a []flagInfo with the information
// on all the specifiable flags of abuc. The values of the
// returned slice are ordered lexicographically on the "short"
// fields. It should be called after flag.Parse().
func getFlagInfos() []flagInfo {
longFlag := map[string]string{
"c": "config",
"i": "intermediate",
"o": "output",
"s": "system",
"t": "target",
"v": "version",
}
res := make([]flagInfo, 0, len(longFlag))
flag.VisitAll(func(f *flag.Flag) {
if len(f.Name) > 1 {
return
}
t, u := flag.UnquoteUsage(f)
if t == "" {
t = "boolean"
}
var d string
if t == "string" {
d = fmt.Sprintf(" (default %q)", f.DefValue)
} else {
d = fmt.Sprintf(" (default %v)", f.DefValue)
}
res = append(res, flagInfo{
short: f.Name,
long: longFlag[f.Name],
typ: t,
usage: u,
def: d,
})
})
return res
}