-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo2cpp.go
More file actions
135 lines (118 loc) · 2.66 KB
/
Copy pathgo2cpp.go
File metadata and controls
135 lines (118 loc) · 2.66 KB
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
package go2cpp
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"strings"
)
func ConvertFile(dst, src string) error {
fset := token.NewFileSet()
fullText, e := ioutil.ReadFile(src)
if e != nil {
log.Println(e)
return e
}
file, e := parser.ParseFile(fset, src, nil, parser.ParseComments)
if e != nil {
return e
}
fo, e := os.OpenFile(dst, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if e != nil {
return e
}
defer fo.Close()
for _, decl := range file.Decls {
if d, ok := decl.(*ast.FuncDecl); ok {
s, e := parseFuncDecl(fullText, d)
if e != nil {
log.Println(e)
return e
}
fo.Write([]byte(s))
}
}
return nil
}
func parseFuncDecl(fullText []byte, d *ast.FuncDecl) (string, error) {
buf := new(strings.Builder)
// return values
if d.Type != nil && d.Type.Results != nil && len(d.Type.Results.List) > 0 {
if len(d.Type.Results.List) > 1 {
return "", fmt.Errorf("multiple return values are not supported in C++: (%s)", stringifyFieldList(fullText, d.Type.Results))
}
r, e := parseFieldType(fullText, d.Type.Results.List[0].Type)
if e != nil {
log.Println(e)
return "", e
}
buf.WriteString(r + " ")
} else {
buf.WriteString("void ")
}
if d.Name != nil {
buf.WriteString(d.Name.Name)
}
buf.WriteString("(")
// args
if d.Type != nil && d.Type.Params != nil {
ss := []string{}
for _, arg := range d.Type.Params.List {
s, e := parseField(fullText, arg)
if e != nil {
return "", e
}
ss = append(ss, s)
}
buf.WriteString(strings.Join(ss, ", "))
}
buf.WriteString(")")
// body
if d.Body != nil {
s, e := parseBlockStmt(fullText, d.Body)
if e != nil {
log.Println(e)
return "", e
}
buf.WriteString(s)
}
return buf.String(), nil
}
func parseDecl(fullText []byte, decl ast.Decl, variables map[string]struct{}) (string, error) {
switch v := decl.(type) {
case *ast.GenDecl:
buf := new(strings.Builder)
for _, spec := range v.Specs {
s, e := parseSpec(fullText, spec, variables)
if e != nil {
log.Println(e)
return "", e
}
buf.WriteString(s)
}
return buf.String(), nil
default:
return "", fmt.Errorf("unsupported declaration: %s", stringifyNode(fullText, decl))
}
}
func parseIdent(fullText []byte, ident *ast.Ident) (string, error) {
return ident.Name, nil
}
func parseBasicLit(fullText []byte, v *ast.BasicLit) (string, error) {
return v.Value, nil
}
func parseField(fullText []byte, v *ast.Field) (string, error) {
t, e := parseFieldType(fullText, v.Type)
if e != nil {
log.Println(e)
return "", e
}
//name
if len(v.Names) == 0 {
return "", fmt.Errorf("field value is empty: %v", v)
}
return t + " " + v.Names[0].Name, nil
}