-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
280 lines (255 loc) · 7.61 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Playing around with metaprogramming
package main
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"log"
"os"
"os/exec"
"reflect"
"runtime"
"strings"
)
func main() {
// I'm not really sure what this FileSet variable does
fset := token.NewFileSet()
_, thisFile, _, ok := runtime.Caller(0)
if !ok {
log.Fatal("could not get current file name")
}
file, err := parser.ParseFile(fset, thisFile, nil, 0)
if err != nil {
log.Fatal(err)
}
fn := visualizeFn{fnName: "fib"}
ast.Walk(&fn, file)
// printer.Fprint(os.Stdout, token.NewFileSet(), fn.fnDecl)
b := &bytes.Buffer{}
printer.Fprint(b, token.NewFileSet(), fn.fnDecl)
if err := visualizeRecFnCalls("fib", "4", b.String()); err != nil {
log.Fatal(err)
}
fn = visualizeFn{fnName: "fact"}
ast.Walk(&fn, file)
b = &bytes.Buffer{}
printer.Fprint(b, token.NewFileSet(), fn.fnDecl)
if err := visualizeRecFnCalls("fact", "4", b.String()); err != nil {
log.Fatal(err)
}
}
const prg = `
package main
import (
"fmt"
"strings"
)
func main() {
%s(%s, 0)
}
%s
`
func visualizeRecFnCalls(fnName string, args string, fnBody string) error {
return generateRunRmGoFile("./pleaseWork.go", fmt.Sprintf(prg, fnName, args, fnBody))
}
func runGoProgram(file string) ([]byte, error) {
return exec.Command("go", "run", file).Output()
}
func writeGoProgram(file string, contents string) error {
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
if _, err := f.Write([]byte(contents)); err != nil {
return err
}
return nil
}
func generateRunRmGoFile(file string, contents string) error {
if err := writeGoProgram(file, contents); err != nil {
return err
}
defer os.Remove(file)
output, err := runGoProgram(file)
if err != nil {
return fmt.Errorf("%s: %v", output, err)
}
fmt.Print(string(output))
return nil
}
type fnNameVisitor struct{}
func (v *fnNameVisitor) Visit(node ast.Node) (w ast.Visitor) {
switch t := node.(type) {
case *ast.FuncDecl:
t.Name = ast.NewIdent(strings.Title(t.Name.Name))
}
return v
}
type importVisitor struct{}
func (v *importVisitor) Visit(node ast.Node) (w ast.Visitor) {
switch t := node.(type) {
case *ast.GenDecl:
if t.Tok == token.IMPORT {
newSpecs := make([]ast.Spec, len(t.Specs)+1)
copy(newSpecs, t.Specs)
newPackage := &ast.BasicLit{token.NoPos, token.STRING, `"woweee, this cannot be a real import can it?"`}
newSpecs[len(t.Specs)] = &ast.ImportSpec{nil, nil, newPackage, nil, token.NoPos}
t.Specs = newSpecs
return nil
}
}
return v
}
type fnBodyVisitor struct{}
func (v *fnBodyVisitor) Visit(node ast.Node) (w ast.Visitor) {
switch t := node.(type) {
case *ast.FuncDecl:
newBodyList := make([]ast.Stmt, 1, len(t.Body.List)+1)
fnCall := &ast.BasicLit{token.NoPos, token.STRING, "fmt.Println"}
arg := &ast.BasicLit{token.NoPos, token.STRING, fmt.Sprintf("\"%s\"", t.Name.Name)}
newStmt := &ast.ExprStmt{&ast.CallExpr{fnCall, token.NoPos, []ast.Expr{arg}, token.NoPos, token.NoPos}}
newBodyList[0] = newStmt
newBodyList = append(newBodyList, t.Body.List...)
t.Body.List = newBodyList
}
return v
}
func thisIsNotExported() {
fmt.Println("hello")
}
func visualizeCall(s string) {
expr, err := parser.ParseExpr(s)
if err != nil {
log.Fatal(err)
}
switch t := expr.(type) {
case *ast.CallExpr:
fnIdent := t.Fun.(*ast.Ident)
fmt.Println(fnIdent.Name)
fnRef := reflect.ValueOf(fnIdent.Name)
fmt.Println(fnRef.Kind())
fmt.Println(reflect.Func)
// fmt.Printf("%T\n", t.Fun)
// t.Fun is of type *ast.Ident
// visualizeCall(`thisIsNotExported()`)
// t.Fun is of type *ast.SelectorExpr
//visualizeCall(`fmt.Println("hello there buddy")`)
default:
log.Fatal("could not parse string into a function call")
}
}
type modifyFn string
func (v *modifyFn) Visit(node ast.Node) (w ast.Visitor) {
switch t := node.(type) {
case *ast.FuncDecl:
if t.Name.Name == string(*v) {
args := t.Type.Params.List
printParams := []string{}
printArgs := []string{}
for _, arg := range args {
printParams = append(printParams, "%v")
printArgs = append(printArgs, arg.Names[0].Name)
}
fmt.Println(args[0].Names[0])
newBodyList := make([]ast.Stmt, 1, len(t.Body.List)+1)
printCall := &ast.BasicLit{token.NoPos, token.STRING, "fmt.Printf"}
printCallArg := &ast.BasicLit{token.NoPos, token.STRING, fmt.Sprintf("\"%s(%s)\\n\", %v", t.Name.Name, strings.Join(printParams, ", "), strings.Join(printArgs, ", "))}
newPrintStmt := &ast.ExprStmt{&ast.CallExpr{printCall, token.NoPos, []ast.Expr{printCallArg}, token.NoPos, token.NoPos}}
newBodyList[0] = newPrintStmt
newBodyList = append(newBodyList, t.Body.List...)
t.Body.List = newBodyList
}
}
return v
}
type visualizeFn struct {
fnName string
fnDecl *ast.FuncDecl
}
func (v *visualizeFn) Visit(node ast.Node) (w ast.Visitor) {
switch t := node.(type) {
case *ast.FuncDecl:
if t.Name.Name == v.fnName {
// Modify the recursive calls to add "depth+1" as the second
// parameter to the recursive function call.
addDepthParamToRecCalls(t.Name.Name, t.Body)
// Modify the body to add a print statement
printParams := []string{}
printArgs := []string{`strings.Repeat("-", depth)`}
for _, arg := range t.Type.Params.List {
printParams = append(printParams, "%v")
printArgs = append(printArgs, arg.Names[0].Name)
}
newBodyList := []ast.Stmt{}
// I think if we were "properly" constructing the AST to add a
// fmt.Printf statement we would need these types:
// *ast.ExprStmt: &{0x82032ca80}
// *ast.CallExpr: &{0x82032ee20 7545 [0x82032ee40 n] 0 7559}
// *ast.SelectorExpr: &{fmt Print}
// *ast.BasicLit: &{7546 STRING "hello %d"}
// *ast.Ident: n
printCall := &ast.BasicLit{token.NoPos, token.STRING, "fmt.Printf"}
printCallArg := &ast.BasicLit{token.NoPos, token.STRING, fmt.Sprintf("\"%%s%s(%s)\\n\", %v", t.Name.Name, strings.Join(printParams, ", "), strings.Join(printArgs, ", "))}
newPrintStmt := &ast.ExprStmt{&ast.CallExpr{printCall, token.NoPos, []ast.Expr{printCallArg}, token.NoPos, token.NoPos}}
newBodyList = append(newBodyList, newPrintStmt)
newBodyList = append(newBodyList, t.Body.List...)
t.Body.List = newBodyList
// Modify the function signature to accept a "depth" argument as the first parameter
depthFieldName := &ast.Ident{token.NoPos, "depth", nil}
depthFieldType := &ast.Ident{token.NoPos, "int", nil}
depthField := &ast.Field{nil, []*ast.Ident{depthFieldName}, depthFieldType, nil, nil}
t.Type.Params.List = append(t.Type.Params.List, depthField)
// Store the modified function
v.fnDecl = t
}
}
return v
}
func addDepthParamToRecCalls(fnName string, node ast.Node) {
// fmt.Printf("%T: %v\n", node, node)
switch t := node.(type) {
case *ast.BlockStmt:
for _, stmt := range t.List {
addDepthParamToRecCalls(fnName, stmt)
}
case *ast.ExprStmt:
addDepthParamToRecCalls(fnName, t.X)
case *ast.IfStmt:
addDepthParamToRecCalls(fnName, t.Body)
case *ast.ReturnStmt:
for _, result := range t.Results {
addDepthParamToRecCalls(fnName, result)
}
case *ast.BinaryExpr:
addDepthParamToRecCalls(fnName, t.X)
addDepthParamToRecCalls(fnName, t.Y)
case *ast.CallExpr:
addDepthParamToRecCalls(fnName, t.Fun)
for _, arg := range t.Args {
addDepthParamToRecCalls(fnName, arg)
}
name, ok := t.Fun.(*ast.Ident)
if !ok || name.Name != fnName {
return
}
depthIdent := &ast.Ident{token.NoPos, "depth", nil}
oneLit := &ast.BasicLit{token.NoPos, token.INT, "1"}
t.Args = append(t.Args, &ast.BinaryExpr{depthIdent, token.NoPos, token.ADD, oneLit})
}
}
func fib(n int) int {
if n < 2 {
return n
}
return fib(n-1) + fib(n-2)
}
func fact(n int) int {
if n == 0 {
return 1
}
return n * fact(n-1)
}