-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
179 lines (152 loc) · 3.96 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
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"log"
"os"
"strings"
// "strings"
)
const version = "golang2cpp 0.1"
var includeFileMap map[string]string = make(map[string]string)
func main() {
inputFilename := "test/interface_test.go"
outputFilename := ""
if len(os.Args) > 1 {
if os.Args[1] == "--version" {
fmt.Println(version)
return
} else if os.Args[1] == "--help" {
fmt.Println("supported arguments:")
fmt.Println(" a .go file as the first argument")
fmt.Println("supported options:")
fmt.Println(" -o : indicate the output file")
return
}
inputFilename = os.Args[1]
}
if len(os.Args) > 3 {
if os.Args[2] != "-o" {
log.Fatal("The second argument must be -o (format sources with clang-format) or -O (don't format sources with clang-format)")
}
outputFilename = os.Args[3]
}
var sourceData []byte
var err error
if inputFilename != "" {
sourceData, err = ioutil.ReadFile(inputFilename)
} else {
log.Fatal("no input file")
//sourceData, err = ioutil.ReadAll(os.Stdin)
}
if err != nil {
log.Fatal(err)
}
cppSource := golang2cpp(inputFilename, string(sourceData))
cppSource = FormatCode(cppSource)
if outputFilename != "" {
err = ioutil.WriteFile(outputFilename, []byte(cppSource), 0755)
if err != nil {
log.Fatal(err)
}
} else {
fmt.Println(cppSource)
}
}
func parseGolang(f *ast.File) []string {
var ret []string
var objectTypeMap ObjectTypeMap
objectTypeMap.typeMap = make(map[string]string)
objectTypeMap.next = nil
ret = append(ret, "#include <unordered_map>")
ret = append(ret, "#include <string>")
ret = append(ret, "#include <vector>")
ret = append(ret, "#include <iostream>")
ret = append(ret, "using namespace std;")
ret = append(ret, GetSliceTemplate()) // slice template
ret = append(ret, GetDeferTemplte()) // defer template
// init object map
InsertInitObjectMap(&objectTypeMap)
for _, decl := range f.Decls {
if g, ok := decl.(*ast.GenDecl); ok {
ret = append(ret, ParseGenDecl(g, &objectTypeMap)...)
}
if g, ok := decl.(*ast.FuncDecl); ok {
ret = append(ret, ParseFuncDecl(g, &objectTypeMap)...)
}
}
// get struct type's declaration and definition
ret = append(ret, GetStructDeclAndDefinition()...)
//
//ret = append(ret, "int main() {")
//ret = append(ret, "\tstd::cout << \"hello world\" << std::endl;")
//ret = append(ret, "}\n")
var includes []string
for _, v := range includeFileMap {
includes = append(includes, "#include <" + v + ">")
}
includes = append(includes, ret...)
objectTypeMap.PrintObjectMap()
return includes
}
var prog *Program
func GetProgram() *Program {
return prog
}
func golang2cpp(file, source string) string {
prog = NewProgram(map[string]string {
file: source,
})
_, f, err := prog.LoadPackage(file)
if err != nil {
log.Fatal(err)
}
//ast.Print(prog.fset, pkg)
//buf := new(bytes.Buffer)
//ast.Fprint(buf, prog.fset, f, ast.NotNilFilter)
//println(buf.String())
ret := parseGolang(f)
return strings.Join(ret, "\n")
}
type Program struct {
fs map[string]string
ast map[string]*ast.File
pkgs map[string]*types.Package
fset *token.FileSet
}
func NewProgram(fs map[string]string) *Program {
return &Program{
fs: fs,
ast: make(map[string]*ast.File),
pkgs: make(map[string]*types.Package),
fset: token.NewFileSet(),
}
}
func (p *Program) LoadPackage(path string) (pkg *types.Package, f *ast.File, err error) {
if pkg, ok := p.pkgs[path]; ok {
return pkg, p.ast[path], nil
}
f, err = parser.ParseFile(p.fset, path, p.fs[path], parser.AllErrors)
if err != nil {
return nil, nil, err
}
// conf := types.Config{Importer: importer.Default()}
// pkg, err = conf.Check(path, p.fset, []*ast.File{f}, nil)
// if err != nil {
// return nil, nil, err
// }
p.ast[path] = f
p.pkgs[path] = pkg
return pkg, f, nil
}
func (p *Program) Import(path string) (*types.Package, error) {
if pkg, ok := p.pkgs[path]; ok {
return pkg, nil
}
pkg, _, err := p.LoadPackage(path)
return pkg, err
}