-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaddressable.go
107 lines (86 loc) · 2.82 KB
/
addressable.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
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package addressable
import (
"go/ast"
"go/parser"
"go/token"
"golang.org/x/tools/go/ast/astutil"
"github.com/rocketlaunchr/igo/file"
)
const randLength = 15 // TODO: Make this configurable
var encounteredBlocks blocks
func init() {
encounteredBlocks = blocks{}
}
func Process(tempFile string) error {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, tempFile, nil, parser.AllErrors|parser.ParseComments|parser.DeclarationErrors)
if err != nil {
return err
}
astutil.Apply(node, pre, post)
err = file.SaveFmtFile(tempFile, fset, node)
if err != nil {
return err
}
return nil
}
func pre(c *astutil.Cursor) bool {
currentNode := c.Node()
switch n := currentNode.(type) {
case *ast.BlockStmt:
encounteredBlocks.add(n)
case *ast.AssignStmt, *ast.BadStmt, *ast.BranchStmt, *ast.DeclStmt, *ast.DeferStmt,
*ast.EmptyStmt, *ast.ExprStmt, *ast.ForStmt, *ast.GoStmt, *ast.IfStmt, *ast.IncDecStmt,
*ast.LabeledStmt, *ast.RangeStmt, *ast.ReturnStmt, *ast.SelectStmt, *ast.SendStmt,
*ast.SwitchStmt, *ast.TypeSwitchStmt:
if currentBlock := encounteredBlocks.current(); currentBlock != nil {
// Search in current block to see if this stmt is a direct child of parent "blockStmt"
if idx, exists := currentBlock.lookup[n.(ast.Stmt)]; exists {
encounteredBlocks[len(encounteredBlocks)-1].current = &[]int{idx}[0]
}
}
case *ast.UnaryExpr:
if n.Op == token.AND { // Address Operator
switch t := n.X.(type) {
case *ast.Ident:
// Note: Assume "true" and "false" are not redefined from default boolean type
if t.Name == "true" || t.Name == "false" {
if currentBlock := encounteredBlocks.current(); currentBlock != nil {
n.X = insertSingleLine("bool", t.Name)
}
}
case *ast.CallExpr:
if currentBlock := encounteredBlocks.current(); currentBlock != nil {
varName := insertCallExpr(currentBlock.node, *currentBlock.current, t)
n.X = replaceX(varName)
// Add 1 to current
currentBlock.current = &[]int{*currentBlock.current + 1}[0]
}
case *ast.BasicLit:
switch t.Kind {
case token.STRING:
if currentBlock := encounteredBlocks.current(); currentBlock != nil {
n.X = insertSingleLine("string", t.Value)
}
case token.INT, token.FLOAT, token.IMAG, token.CHAR:
if currentBlock := encounteredBlocks.current(); currentBlock != nil {
varName := insertConstVar(currentBlock.node, t.Kind, *currentBlock.current, t.Value)
n.X = replaceX(varName)
// Add 1 to current
currentBlock.current = &[]int{*currentBlock.current + 1}[0]
}
}
}
}
}
return true
}
func post(c *astutil.Cursor) bool {
currentNode := c.Node()
switch currentNode.(type) {
case *ast.BlockStmt:
encounteredBlocks.remove()
}
return true
}