-
Notifications
You must be signed in to change notification settings - Fork 9
/
parse_composite_lit.go
48 lines (45 loc) · 1.26 KB
/
parse_composite_lit.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
package main
import (
"go/ast"
"log"
"strings"
)
func ParseCompositeLite(compLit *ast.CompositeLit) string {
var ret []string
var vals []string
if mapType, ok := compLit.Type.(*ast.MapType); ok {
ret = append(ret, ParseMapType(mapType))
vals = append(vals, "{")
for _, elt := range compLit.Elts {
vals = append(vals, ParseExpr(elt) + ",")
}
vals = append(vals, "}")
ret = append(ret, vals...)
} else if arrayType, ok := compLit.Type.(*ast.ArrayType); ok {
ret = append(ret, ParseArrayType(arrayType))
vals = append(vals, "{")
for _, elt := range compLit.Elts {
if _, ok := elt.(* ast.KeyValueExpr); ok {
log.Fatal("not support key-value in array type now")
}
vals = append(vals, ParseExpr(elt) + ",")
}
vals = append(vals, "}")
ret = append(ret, vals...)
} else {
ret = append(ret, ParseExpr(compLit.Type))
vals = append(vals, "{")
for idx, elt := range compLit.Elts {
if idx == 0 {
vals = append(vals, ParseExpr(elt))
} else {
vals = append(vals, ", " + ParseExpr(elt))
}
}
vals = append(vals, "}")
ret = append(ret, vals...)
//p := GetProgram()
//log.Fatal("not implemented var type: " + ParseExpr(compLit.Type) + ", pos = " + p.fset.Position(compLit.Pos()).String())
}
return strings.Join(ret, "\n")
}