-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
168 lines (151 loc) · 4.07 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
package main
import (
"flag"
"fmt"
"os"
"github.com/xaxys/bubbler/compiler"
"github.com/xaxys/bubbler/definition"
"github.com/xaxys/bubbler/generator"
"github.com/xaxys/bubbler/generator/gen"
"github.com/xaxys/bubbler/util"
)
var (
BuildTags = "unknown"
BuildTime = "unknown"
GitCommit = "unknown"
GoVersion = "unknown"
)
// Font: slant
// http://www.network-science.de/ascii/
const bannerTemplate = `
{{- define "banner" }}\
__ __ __ __
/ /_ __ __/ /_ / /_ / /__ ___
/ __ \/ / / / __ \/ __ \/ / _ \/ _/
/ /_/ / /_/ / /_/ / /_/ / / __/ /
/_.___/\__,_/_.___/_.___/_/\___/_/
Welcome to use bubbler!
Version: {{ .BuildTags }}
Built: {{ .BuildTime }}
GitCommit: {{ .GitCommit }}
GoVersion: {{ .GoVersion }}
Usage:
bubbler [options] <input file>
Options:
-t <target> Target Language
-o <output> Output Path
-rmpath <path[,path...]> Remove Path Prefix for Output
-inner Generate Inner Class
-single Generate Single File
-minimal Generate Minimal Code
-decnum Force Generate Decimal Format for Constant Value
-memcpy Allocate Memory and Copy Data for Variable-Size Type
-signext <method> Sign Extension Method (shift, arith)
Targets:
{{ range .Generators }} {{ . }}
{{ end }}
Examples:
bubbler -t c -minimal -o output/ example.bb
bubbler -t c -single -o gen.hpp example.bb
bubbler -t py -decnum -signext arith -o output example.bb
bubbler -t go -o output/ -rmpath github.com/xaxys/bubbler/proto example.bb
For more information, please visit: https://github.com/xaxys/bubbler
{{ end }}
`
func printBanner() {
data := map[string]interface{}{
"BuildTags": BuildTags,
"BuildTime": BuildTime,
"GitCommit": GitCommit,
"GoVersion": GoVersion,
"Generators": generator.ListGenerators(),
}
tmpl := util.ExecuteTemplate(bannerTemplate, "banner", nil, data)
fmt.Println(tmpl)
}
func main() {
if len(os.Args) == 1 {
printBanner()
os.Exit(0)
}
target := ""
output := ""
rmpath := ""
inner := false
single := false
minimal := false
decnum := false
memcpy := false
signext := ""
flag.StringVar(&target, "t", "", "Target Language")
flag.StringVar(&output, "o", "", "Output Path")
flag.StringVar(&rmpath, "rmpath", "", "Remove Path Prefix for Output")
flag.BoolVar(&inner, "inner", false, "Generate Inner Class")
flag.BoolVar(&single, "single", false, "Generate Single File")
flag.BoolVar(&minimal, "minimal", false, "Generate Minimal Code")
flag.BoolVar(&decnum, "decnum", false, "Force Generate Decimal Format for Constant Value")
flag.BoolVar(&memcpy, "memcpy", false, "Allocate Memory and Copy Data for Variable-Size Type")
flag.StringVar(&signext, "signext", "", "Sign Extension Method (shift, arith)")
flag.Parse()
// check input file
files := flag.Args()
if len(files) == 0 {
fmt.Fprintln(os.Stderr, &definition.GeneralError{
Err: &definition.NoInputFileError{},
})
os.Exit(1)
}
if len(files) > 1 {
fmt.Fprintln(os.Stderr, &definition.GeneralError{
Err: &definition.MultipleInputFileError{
Files: files,
},
})
os.Exit(1)
}
// check target before compiling
gentor, err := generator.GetGenerator(target)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// check option before compiling
signextOpt, err := gen.SignExtMethod(signext)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// compile
input := files[0]
units, err, warning := compiler.Compile(input)
if warning != nil {
fmt.Fprintln(os.Stderr, warning)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// generate
options := gen.NewGenOptions(
gen.RemovePath(rmpath),
gen.SingleFile(single),
gen.InnerClass(inner),
gen.MinimalCode(minimal),
gen.DecimalNumber(decnum),
gen.MemoryCopy(memcpy),
signextOpt,
)
ctx := &gen.GenCtx{
Units: units,
GenOptions: options,
OutputPath: output,
}
err, warning = gentor.Generate(ctx)
if warning != nil {
fmt.Fprintln(os.Stderr, warning)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}