forked from awfeequdng/px_golang2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_var.go
62 lines (56 loc) · 1.59 KB
/
parse_var.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
package main
import (
"go/ast"
"log"
"strconv"
"strings"
)
func ParseGenDeclVar(decl *ast.GenDecl, objectTypeMap *ObjectTypeMap) []string {
var ret []string
for _, spec := range decl.Specs {
if vs, ok := spec.(*ast.ValueSpec); ok {
var names []string
var values []string
var tname string
if vs.Type == nil {
tname = "auto"
} else {
tname = ParseExpr(vs.Type)
}
for _, name := range vs.Names {
names = append(names, name.Name)
}
for _, value := range vs.Values {
values = append(values, ParseExpr(value))
}
if len(names) == len(values) {
for idx, name := range names {
objectTypeMap.InsertObjectMap(name, tname, values[idx])
ret = append(ret, tname + " " + name + " = " + values[idx] + ";")
}
} else if len(names) > 1 && len(values) == 1 {
for _, name := range names {
objectTypeMap.InsertObjectMap(name, tname, values[0])
ret = append(ret, tname + " " + name + " = " + values[0])
}
} else if len(values) == 0 && len(names) > 0 {
// have name and type
if vs.Type == nil {
log.Fatal("can not be nil when values is nil")
}
for _, name := range names {
objectTypeMap.InsertObjectMap(name, tname, "")
ret = append(ret, tname + " " + name + ";")
}
} else {
log.Print("names: " + strings.Join(names, ","))
log.Print("values: ")
log.Print(strings.Join(values, ","))
log.Fatal("invalid names and values size , name size: " + strconv.Itoa(len(names)) + ", value size: " + strconv.Itoa(len(values)))
}
} else {
log.Fatal("NOT IMPLEMENTED YET")
}
}
return ret
}