-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparse_func_decl.go
198 lines (177 loc) · 5.75 KB
/
parse_func_decl.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
package main
import (
"go/ast"
"log"
"regexp"
"strings"
)
var globalObjectMap map[string]string
func ParseFuncRev(list *ast.FieldList) (rec, obj string) {
if len(list.List) > 1 {
log.Fatal("rev field count greater than 1")
}
if len(list.List[0].Names) > 1 {
log.Fatal("rev field's name count greater than 1")
}
obj = ParseExpr(list.List[0].Names[0])
if starExpr, ok := list.List[0].Type.(*ast.StarExpr); ok {
rec = ParseExpr(starExpr.X)
} else {
// log.Fatal("rev's type is not StartExpr")
//log.Print("rev's type is not StartExpr")
rec = ParseExpr(list.List[0].Type)
}
return rec, obj
}
func ParseObjectTypeMapFromParams(objectTypeMap *ObjectTypeMap, params []string) {
for _, param := range params {
res := strings.Split(strings.TrimSpace(param), " ")
if len(res) != 2 {
log.Fatal("not key-value pair in param: " + param)
}
objectTypeMap.typeMap[res[1]] = res[0]
}
}
func ParseFuncSignature(decl *ast.FuncDecl, objectTypeMap *ObjectTypeMap) (funcRet, funcName, funcParams, funcVars, retValues string) {
name := decl.Name.Name
funcType := decl.Type
params := ParseFieldList(funcType.Params)
ParseObjectTypeMapFromParams(objectTypeMap, params)
var results []string
if funcType.Results != nil {
results = ParseFieldList(funcType.Results)
}
funcParams = "(" + strings.Join(params, ",") + ")"
funcName = name
if len(results) == 0 {
funcRet = "void"
} else if len(results) == 1 {
res := strings.TrimSpace(results[0])
if strings.Contains(res, " ") {
reses := strings.Split(res, " ")
if len(reses) != 2 {
log.Fatal("not key-value pair: " + strings.Join(reses, " "))
}
funcRet = reses[0]
retValues = reses[1]
funcVars = results[0] + ";"
} else {
funcRet = results[0]
}
} else if len(results) == 2 {
includeFileMap["std::pair"] = "utility"
//funcRet = "std::pair<" + results[0] + "," + results[1] + ">"
res1 := strings.TrimSpace(results[0])
res2 := strings.TrimSpace(results[1])
if strings.Contains(res1, " ") && strings.Contains(res2, " ") {
res1s := strings.Split(res1, " ")
res2s := strings.Split(res2, " ")
if len(res1s) != 2 || len(res2s) != 2 {
log.Fatal("not key value pair: " + strings.Join(res1s, " ") + ", " + strings.Join(res2s, " "))
}
funcRet = "std::pair<" + res1s[0] + "," + res2s[0] + ">"
retValues = "{" + res1s[01] + ", " + res2s[1] + "}"
funcVars = results[0] + ";"
funcVars += results[1] + ";"
} else if !strings.Contains(res1, " ") && !strings.Contains(res2, " ") {
funcRet = "std::pair<" + results[0] + "," + results[1] + ">"
} else {
log.Fatal("not all key-value pair: " + results[0] + ", " + results[1])
}
} else {
includeFileMap["std::tuple"] = "tuple"
if strings.Contains(strings.TrimSpace(results[0]), " ") {
// key-value pair
tuple := "std::tuple<"
retValues = "{ "
for id, r := range results {
res := strings.TrimSpace(r)
if !strings.Contains(res, " ") {
log.Fatal("not key-value pair: " + r)
}
reses := strings.Split(res, " ")
if len(reses) != 2 {
log.Fatal("not key-value pair: " + r)
}
if id == 0 {
tuple += reses[0]
retValues += reses[1]
} else {
tuple += ", " + reses[0]
retValues += ", " + reses[1]
}
funcVars += r + ";"
}
tuple += ">"
retValues += "}"
funcRet = tuple
} else {
// only type
tuple := "std::tuple<"
for id, r := range results {
if strings.Contains(strings.TrimSpace(r), " ") {
log.Fatal("is key-value pair: " + r)
}
if id == 0 {
tuple += r
} else {
tuple += ", " + r
}
}
tuple += ">"
funcRet = tuple
}
}
return funcRet, funcName, funcParams, funcVars, retValues
}
func ParseCommonFuncDecl(decl *ast.FuncDecl, objectTypeMap *ObjectTypeMap) []string {
var ret []string
funcRet, funcName, funcParams, funcVars, retValues := ParseFuncSignature(decl, objectTypeMap)
ret = append(ret, funcRet + " " + funcName + funcParams)
body := ParseBlockStmt(decl.Body, objectTypeMap)
strBody := strings.Join(body, "\n")
strBody = "{" + funcVars + strBody + "}"
if strings.Contains(strBody, "return;") {
strBody = strings.ReplaceAll(strBody, "return;", "return " + retValues + ";")
}
ret = append(ret, strBody)
return ret
}
var structFuncDeclMap map[string][]string = make(map[string][]string)
func GetStructFuncDeclMap() map[string][]string {
return structFuncDeclMap
}
var structFuncDefinitionMap map[string][]string = make(map[string][]string)
func GetStructFuncDefinitionMap() map[string][]string {
return structFuncDefinitionMap
}
func ParseMemberFuncDecl(decl *ast.FuncDecl, objectTypeMap *ObjectTypeMap) []string {
var ret []string
var rev string
var revObj string
rev, revObj = ParseFuncRev(decl.Recv)
funcRet, funcName, funcParams, funcVars, retValues := ParseFuncSignature(decl, objectTypeMap)
// add struct type to function map
structFuncDeclMap[rev] = append(structFuncDeclMap[rev], funcRet + " " + funcName + funcParams + ";")
body := ParseBlockStmt(decl.Body, objectTypeMap)
strBody := strings.Join(body, "\n")
strBody = "{ " + funcVars + " " + strBody + " }"
//strBody = strings.ReplaceAll(strBody, revObj + ".", "this->")
//strBody = strings.ReplaceAll(strBody, revObj, "*this")
reg, _ := regexp.Compile("\\b" + revObj + "\\b")
strBody = reg.ReplaceAllString(strBody, "(*this)")
if strings.Contains(strBody, "return;") {
strBody = strings.ReplaceAll(strBody, "return;", "return " + retValues + ";")
}
structFuncDefinitionMap[rev] = append(structFuncDefinitionMap[rev],
funcRet + " " + rev + "::" + funcName + funcParams + strBody)
return ret
}
func ParseFuncDecl(decl *ast.FuncDecl, objectTypeMap *ObjectTypeMap) []string {
if decl.Recv != nil {
// member function
return ParseMemberFuncDecl(decl, objectTypeMap)
}
// common function
return ParseCommonFuncDecl(decl, objectTypeMap)
}