-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.go
49 lines (43 loc) · 987 Bytes
/
spec.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
package go2cpp
import (
"fmt"
"go/ast"
"log"
"strings"
)
func parseSpec(fullText []byte, spec ast.Spec, variables map[string]struct{}) (string, error) {
switch v := spec.(type) {
case *ast.ValueSpec:
buf := new(strings.Builder)
var e error
var t = "auto"
if v.Type != nil {
t, e = parseFieldType(fullText, v.Type)
if e != nil {
log.Println(e)
return "", e
}
}
for i, name := range v.Names {
if _, ok := variables[name.Name]; ok {
return "", fmt.Errorf("variable already declared: %s", name.Name)
}
variables[name.Name] = struct{}{}
buf.WriteString(t + " " + name.Name)
if len(v.Values) > 0 {
value, e := parseExpr(fullText, v.Values[i])
if e != nil {
log.Println(e)
return "", e
}
buf.WriteString(" = " + value)
}
if i < len(v.Names)-1 {
buf.WriteString(";\n")
}
}
return buf.String(), nil
default:
return "", fmt.Errorf("unsupported spec: %s", stringifyNode(fullText, spec))
}
}