forked from elliotchance/c2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
var_decl.go
77 lines (66 loc) · 1.56 KB
/
var_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
package main
import (
"bytes"
"fmt"
"strings"
)
type VarDecl struct {
Address string
Position string
Position2 string
Name string
Type string
Type2 string
IsExtern bool
IsUsed bool
IsCInit bool
Children []interface{}
}
func parseVarDecl(line string) *VarDecl {
groups := groupsFromRegex(
`<(?P<position>.*)>(?P<position2> .+:\d+)?
(?P<used> used)?
(?P<name> \w+)?
'(?P<type>.+?)'
(?P<type2>:'.*?')?
(?P<extern> extern)?
(?P<cinit> cinit)?`,
line,
)
type2 := groups["type2"]
if type2 != "" {
type2 = type2[2 : len(type2)-1]
}
return &VarDecl{
Address: groups["address"],
Position: groups["position"],
Position2: strings.TrimSpace(groups["position2"]),
Name: strings.TrimSpace(groups["name"]),
Type: groups["type"],
Type2: type2,
IsExtern: len(groups["extern"]) > 0,
IsUsed: len(groups["used"]) > 0,
IsCInit: len(groups["cinit"]) > 0,
Children: []interface{}{},
}
}
func (n *VarDecl) Render() []string {
theType := resolveType(n.Type)
name := n.Name
// Go does not allow the name of a variable to be called "type".
// For the moment I will rename this to avoid the error.
if name == "type" {
name = "type_"
}
suffix := ""
if len(n.Children) > 0 {
children := n.Children
suffix = fmt.Sprintf(" = %s", renderExpression(children[0])[0])
}
if suffix == " = (0)" {
suffix = " = nil"
}
return []string{fmt.Sprintf("var %s %s%s", name, theType, suffix), "unknown3"}
}
func (n *VarDecl) RenderLine(out *bytes.Buffer, functionName string, indent int, returnType string) {
}