-
Notifications
You must be signed in to change notification settings - Fork 159
/
main.go
434 lines (363 loc) · 10.6 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Package c2go contains the main function for running the executable.
//
// Installation
//
// go get -u github.com/elliotchance/c2go
//
// Usage
//
// c2go transpile myfile.c
//
package main
import (
"flag"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"errors"
"github.com/elliotchance/c2go/ast"
"github.com/elliotchance/c2go/preprocessor"
"github.com/elliotchance/c2go/program"
"github.com/elliotchance/c2go/transpiler"
)
var stderr io.Writer = os.Stderr
// ProgramArgs defines the options available when processing the program. There
// is no constructor since the zeroed out values are the appropriate defaults -
// you need only set the options you need.
//
// TODO: Better separation on CLI modes
// https://github.com/elliotchance/c2go/issues/134
//
// Do not instantiate this directly. Instead use DefaultProgramArgs(); then
// modify any specific attributes.
type ProgramArgs struct {
verbose bool
ast bool
inputFiles []string
clangFlags []string
outputFile string
packageName string
// A private option to output the Go as a *_test.go file.
outputAsTest bool
}
// DefaultProgramArgs default value of ProgramArgs
func DefaultProgramArgs() ProgramArgs {
return ProgramArgs{
verbose: false,
ast: false,
packageName: "main",
clangFlags: []string{},
outputAsTest: false,
}
}
func readAST(data []byte) []string {
return strings.Split(string(data), "\n")
}
type treeNode struct {
indent int
node ast.Node
}
func convertLinesToNodes(lines []string) []treeNode {
nodes := make([]treeNode, len(lines))
var counter int
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
// It is tempting to discard null AST nodes, but these may
// have semantic importance: for example, they represent omitted
// for-loop conditions, as in for(;;).
line = strings.Replace(line, "<<<NULL>>>", "NullStmt", 1)
trimmed := strings.TrimLeft(line, "|\\- `")
node := ast.Parse(trimmed)
indentLevel := (len(line) - len(trimmed)) / 2
nodes[counter] = treeNode{indentLevel, node}
counter++
}
nodes = nodes[0:counter]
return nodes
}
func convertLinesToNodesParallel(lines []string) []treeNode {
// function f separate full list on 2 parts and
// then each part can recursive run function f
var f func([]string, int) []treeNode
f = func(lines []string, deep int) []treeNode {
deep = deep - 2
part := len(lines) / 2
var tr1 = make(chan []treeNode)
var tr2 = make(chan []treeNode)
go func(lines []string, deep int) {
if deep <= 0 || len(lines) < deep {
tr1 <- convertLinesToNodes(lines)
return
}
tr1 <- f(lines, deep)
}(lines[0:part], deep)
go func(lines []string, deep int) {
if deep <= 0 || len(lines) < deep {
tr2 <- convertLinesToNodes(lines)
return
}
tr2 <- f(lines, deep)
}(lines[part:], deep)
defer close(tr1)
defer close(tr2)
return append(<-tr1, <-tr2...)
}
// Parameter of deep - can be any, but effective to use
// same amount of CPU
return f(lines, runtime.NumCPU())
}
// buildTree converts an array of nodes, each prefixed with a depth into a tree.
func buildTree(nodes []treeNode, depth int) []ast.Node {
if len(nodes) == 0 {
return []ast.Node{}
}
// Split the list into sections, treat each section as a tree with its own
// root.
sections := [][]treeNode{}
for _, node := range nodes {
if node.indent == depth {
sections = append(sections, []treeNode{node})
} else {
sections[len(sections)-1] = append(sections[len(sections)-1], node)
}
}
results := []ast.Node{}
for _, section := range sections {
slice := []treeNode{}
for _, n := range section {
if n.indent > depth {
slice = append(slice, n)
}
}
children := buildTree(slice, depth+1)
for _, child := range children {
section[0].node.AddChild(child)
}
results = append(results, section[0].node)
}
return results
}
// Start begins transpiling an input file.
func Start(args ProgramArgs) (err error) {
if args.verbose {
fmt.Println("Start tanspiling ...")
}
// 1. Compile it first (checking for errors)
for _, in := range args.inputFiles {
_, err := os.Stat(in)
if err != nil {
return fmt.Errorf("Input file %s is not found", in)
}
}
// 2. Preprocess
if args.verbose {
fmt.Println("Running clang preprocessor...")
}
pp, comments, includes, err := preprocessor.Analyze(args.inputFiles, args.clangFlags, args.verbose)
if err != nil {
return fmt.Errorf("issue running preprocessor: %w", err)
}
if args.verbose {
fmt.Println("Writing preprocessor ...")
}
dir, err := os.MkdirTemp("", "c2go")
if err != nil {
return fmt.Errorf("Cannot create temp folder: %v", err)
}
defer os.RemoveAll(dir) // clean up
ppFilePath := path.Join(dir, "pp.c")
err = os.WriteFile(ppFilePath, pp, 0644)
if err != nil {
return fmt.Errorf("writing to %s failed: %v", ppFilePath, err)
}
// 3. Generate JSON from AST
if args.verbose {
fmt.Println("Running clang for AST tree...")
}
astPP, err := exec.Command("clang", "-Xclang", "-ast-dump",
"-fsyntax-only", "-fno-color-diagnostics", ppFilePath).Output()
if err != nil {
// If clang fails it still prints out the AST, so we have to run it
// again to get the real error.
errBody, _ := exec.Command("clang", ppFilePath).CombinedOutput()
panic("clang failed: " + err.Error() + ":\n\n" + string(errBody))
}
if args.verbose {
fmt.Println("Reading clang AST tree...")
}
lines := readAST(astPP)
if args.ast {
for _, l := range lines {
fmt.Println(l)
}
fmt.Println()
return nil
}
p := program.NewProgram()
p.Verbose = args.verbose
p.OutputAsTest = args.outputAsTest
p.Comments = comments
p.IncludeHeaders = includes
// Converting to nodes
if args.verbose {
fmt.Println("Converting to nodes...")
}
nodes := convertLinesToNodesParallel(lines)
// build tree
if args.verbose {
fmt.Println("Building tree...")
}
tree := buildTree(nodes, 0)
ast.FixPositions(tree)
p.SetNodes(tree)
// Repair the character literals. See RepairCharacterLiteralsFromSource for
// more information.
characterErrors := ast.RepairCharacterLiteralsFromSource(tree[0], ppFilePath)
for _, cErr := range characterErrors {
message := fmt.Sprintf("could not read exact character literal: %s",
cErr.Err.Error())
p.AddMessage(p.GenerateWarningMessage(errors.New(message), cErr.Node))
}
// Repair the floating literals. See RepairFloatingLiteralsFromSource for
// more information.
floatingErrors := ast.RepairFloatingLiteralsFromSource(tree[0], ppFilePath)
for _, fErr := range floatingErrors {
message := fmt.Sprintf("could not read exact floating literal: %s",
fErr.Err.Error())
p.AddMessage(p.GenerateWarningMessage(errors.New(message), fErr.Node))
}
outputFilePath := args.outputFile
if outputFilePath == "" {
// Choose inputFile for creating name of output file
input := args.inputFiles[0]
// We choose name for output Go code at the base
// on filename for choosed input file
cleanFileName := filepath.Clean(filepath.Base(input))
extension := filepath.Ext(input)
outputFilePath = cleanFileName[0:len(cleanFileName)-len(extension)] + ".go"
}
// transpile ast tree
if args.verbose {
fmt.Println("Transpiling tree...")
}
err = transpiler.TranspileAST(args.outputFile, args.packageName,
p, tree[0].(ast.Node))
if err != nil {
return fmt.Errorf("cannot transpile AST : %v", err)
}
// write the output Go code
if args.verbose {
fmt.Println("Writing the output Go code...")
}
err = os.WriteFile(outputFilePath, []byte(p.String()), 0644)
if err != nil {
return fmt.Errorf("writing Go output file failed: %v", err)
}
// simplify Go code by `gofmt`
// error ignored, because it is not change the workflow
_, _ = exec.Command("gofmt", "-w", outputFilePath).Output()
return nil
}
type inputDataFlags []string
func (i *inputDataFlags) String() (s string) {
for pos, item := range *i {
s += fmt.Sprintf("Flag %d. %s\n", pos, item)
}
return
}
func (i *inputDataFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
var clangFlags inputDataFlags
func init() {
transpileCommand.Var(&clangFlags, "clang-flag", "Pass arguments to clang. You may provide multiple -clang-flag items.")
astCommand.Var(&clangFlags, "clang-flag", "Pass arguments to clang. You may provide multiple -clang-flag items.")
}
var (
versionFlag = flag.Bool("v", false, "print the version and exit")
transpileCommand = flag.NewFlagSet("transpile", flag.ContinueOnError)
verboseFlag = transpileCommand.Bool("V", false, "print progress as comments")
outputFlag = transpileCommand.String("o", "", "output Go generated code to the specified file")
packageFlag = transpileCommand.String("p", "main", "set the name of the generated package")
transpileHelpFlag = transpileCommand.Bool("h", false, "print help information")
astCommand = flag.NewFlagSet("ast", flag.ContinueOnError)
astHelpFlag = astCommand.Bool("h", false, "print help information")
)
func main() {
code := runCommand()
if code != 0 {
os.Exit(code)
}
}
func runCommand() int {
flag.Usage = func() {
usage := "Usage: %s [-v] [<command>] [<flags>] file1.c ...\n\n"
usage += "Commands:\n"
usage += " transpile\ttranspile an input C source file or files to Go\n"
usage += " ast\t\tprint AST before translated Go code\n\n"
usage += "Flags:\n"
fmt.Fprintf(stderr, usage, os.Args[0])
flag.PrintDefaults()
}
transpileCommand.SetOutput(stderr)
astCommand.SetOutput(stderr)
flag.Parse()
if *versionFlag {
// Simply print out the version and exit.
fmt.Println(program.Version)
return 0
}
if flag.NArg() < 1 {
flag.Usage()
return 1
}
args := DefaultProgramArgs()
switch os.Args[1] {
case "ast":
err := astCommand.Parse(os.Args[2:])
if err != nil {
fmt.Printf("ast command cannot parse: %v", err)
return 1
}
if *astHelpFlag || astCommand.NArg() == 0 {
fmt.Fprintf(stderr, "Usage: %s ast file.c\n", os.Args[0])
astCommand.PrintDefaults()
return 1
}
args.ast = true
args.inputFiles = astCommand.Args()
args.clangFlags = clangFlags
case "transpile":
err := transpileCommand.Parse(os.Args[2:])
if err != nil {
fmt.Printf("transpile command cannot parse: %v", err)
return 1
}
if *transpileHelpFlag || transpileCommand.NArg() == 0 {
fmt.Fprintf(stderr, "Usage: %s transpile [-V] [-o file.go] [-p package] file1.c ...\n", os.Args[0])
transpileCommand.PrintDefaults()
return 1
}
args.inputFiles = transpileCommand.Args()
args.outputFile = *outputFlag
args.packageName = *packageFlag
args.verbose = *verboseFlag
args.clangFlags = clangFlags
default:
flag.Usage()
return 1
}
if err := Start(args); err != nil {
fmt.Printf("Error: %v\n", err)
return 1
}
return 0
}