-
Notifications
You must be signed in to change notification settings - Fork 93
/
unwrap.go
301 lines (279 loc) · 6.11 KB
/
unwrap.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* gomacro - A Go interpreter with Lisp-like macros
*
* Copyright (C) 2018-2019 Massimiliano Ghilardi
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*
* unwrap.go
*
* Created on: May 06, 2018
* Author: Massimiliano Ghilardi
*/
package ast2
import (
"go/ast"
"go/token"
r "reflect"
"github.com/cosmos72/gomacro/go/etoken"
)
// ToNode converts Ast back ast.Node, or panics on failure
// (it fails if the argument is not AstWithNode)
func ToNode(x Ast) ast.Node {
switch x := x.(type) {
case nil:
return nil
case AstWithNode:
return x.Node()
default:
y := x.Interface()
errorf("cannot convert to ast.Node: %v // %T", y, y)
return nil
}
}
func ToBasicLit(x Ast) *ast.BasicLit {
switch x := x.(type) {
case nil:
break
case BasicLit:
return x.X
default:
y := x.Interface()
errorf("cannot convert to *ast.BasicLit: %v // %T", y, y)
}
return nil
}
func ToBlockStmt(x Ast) *ast.BlockStmt {
switch x := x.(type) {
case nil:
break
case BlockStmt:
return x.X
default:
stmt := ToStmt(x)
return &ast.BlockStmt{Lbrace: stmt.Pos(), List: []ast.Stmt{stmt}, Rbrace: stmt.End()}
}
return nil
}
func ToCallExpr(x Ast) *ast.CallExpr {
switch x := x.(type) {
case nil:
break
case CallExpr:
return x.X
default:
y := x.Interface()
errorf("cannot convert to *ast.CallExpr: %v // %T", y, y)
}
return nil
}
func ToDecl(x Ast) ast.Decl {
switch node := ToNode(x).(type) {
case ast.Decl:
return node
case nil:
default:
y := x.Interface()
errorf("cannot convert to ast.Decl: %v // %T", y, y)
}
return nil
}
func ToExpr(x Ast) ast.Expr {
switch node := ToNode(x).(type) {
case nil:
break
case ast.Expr:
return node
case *ast.BlockStmt:
return BlockStmtToExpr(node)
case *ast.EmptyStmt:
return &ast.Ident{NamePos: node.Semicolon, Name: "nil"}
case *ast.ExprStmt:
return node.X
case ast.Stmt:
list := []ast.Stmt{node}
block := &ast.BlockStmt{List: list}
return BlockStmtToExpr(block)
default:
errorf("cannot convert from %v to ast.Expr: %v <%v>",
r.TypeOf(node), node, r.TypeOf(node))
}
return nil
}
func ToExprSlice(x Ast) []ast.Expr {
switch x := x.(type) {
case nil:
break
case ExprSlice:
return x.X
case AstWithSlice:
n := x.Size()
ret := make([]ast.Expr, n)
for i := 0; i < n; i++ {
ret[i] = ToExpr(x.Get(i))
}
return ret
default:
errorf("cannot convert from %v <%v> to []ast.Expr", x, r.TypeOf(x))
}
return nil
}
func ToField(x Ast) *ast.Field {
switch node := ToNode(x).(type) {
case nil:
break
case *ast.Field:
return node
default:
errorf("cannot convert %v <%v> to *ast.Field", node, r.TypeOf(node))
}
return nil
}
func ToFile(x Ast) *ast.File {
switch node := ToNode(x).(type) {
case nil:
break
case *ast.File:
return node
default:
errorf("cannot convert %v <%v> to *ast.File", node, r.TypeOf(node))
}
return nil
}
func ToFieldList(x Ast) *ast.FieldList {
switch node := ToNode(x).(type) {
case nil:
break
case *ast.FieldList:
return node
case *ast.Field:
return &ast.FieldList{Opening: node.Pos(), List: []*ast.Field{node}, Closing: node.End()}
default:
errorf("cannot convert %v <%v> to *ast.Field", node, r.TypeOf(node))
}
return nil
}
func ToFuncType(x Ast) *ast.FuncType {
switch node := ToNode(x).(type) {
case nil:
break
case *ast.FuncType:
return node
default:
errorf("cannot convert %v <%v> to *ast.FuncType", node, r.TypeOf(node))
}
return nil
}
func ToImportSpec(x Ast) *ast.ImportSpec {
switch node := ToNode(x).(type) {
case nil:
break
case *ast.ImportSpec:
return node
default:
errorf("cannot convert %v <%v> to *ast.ImportSpec", node, r.TypeOf(node))
}
return nil
}
func ToIdent(x Ast) *ast.Ident {
switch node := ToNode(x).(type) {
case nil:
break
case *ast.Ident:
return node
default:
errorf("cannot convert %v <%v> to *ast.Ident", node, r.TypeOf(node))
}
return nil
}
func ToIdentSlice(x Ast) []*ast.Ident {
switch x := x.(type) {
case nil:
break
case IdentSlice:
return x.X
case AstWithSlice:
n := x.Size()
ret := make([]*ast.Ident, n)
for i := 0; i < n; i++ {
ret[i] = ToIdent(x.Get(i))
}
return ret
default:
errorf("unimplemented conversion from %v <%v> to []*ast.Ident", x, r.TypeOf(x))
}
return nil
}
func ToSpec(x Ast) ast.Spec {
switch node := ToNode(x).(type) {
case nil:
break
case ast.Spec:
return node
default:
errorf("cannot convert %v <%v> to ast.Spec", node, r.TypeOf(node))
}
return nil
}
func ToStmt(x Ast) ast.Stmt {
switch node := ToNode(x).(type) {
case ast.Stmt:
return node
case ast.Decl:
return &ast.DeclStmt{Decl: node}
case ast.Expr:
return &ast.ExprStmt{X: node}
case nil:
break
default:
errorf("unimplemented conversion from %v <%v> to ast.Stmt", node, r.TypeOf(node))
}
return nil
}
func ToStmtSlice(x Ast) []ast.Stmt {
switch x := x.(type) {
case nil:
break
case StmtSlice:
return x.X
case AstWithSlice:
n := x.Size()
ret := make([]ast.Stmt, n)
for i := 0; i < n; i++ {
ret[i] = ToStmt(x.Get(i))
}
return ret
default:
errorf("unimplemented conversion from %v <%v> to []ast.Stmt", x, r.TypeOf(x))
}
return nil
}
func BlockStmtToExpr(node *ast.BlockStmt) ast.Expr {
if node == nil {
return nil
}
list := node.List
switch len(list) {
case 0:
// convert {} to nil, because {} in expression context means "no useful value"
return &ast.Ident{NamePos: node.Lbrace, Name: "nil"}
case 1:
// check if we are lucky...
switch node := list[0].(type) {
case *ast.ExprStmt:
return node.X
case *ast.EmptyStmt:
// convert { ; } to nil, because { ; } in expression context means "no useful value"
return &ast.Ident{NamePos: node.Semicolon, Name: "nil"}
}
}
// due to go/ast strictly typed model, there is only one mechanism
// to insert a statement inside an expression: use a closure.
// so we return a unary expression: MACRO (func() { /*block*/ })
typ := &ast.FuncType{Func: token.NoPos, Params: &ast.FieldList{}}
fun := &ast.FuncLit{Type: typ, Body: node}
return &ast.UnaryExpr{Op: etoken.MACRO, X: fun}
}