-
Notifications
You must be signed in to change notification settings - Fork 93
/
macroexpand.go
293 lines (280 loc) · 7.96 KB
/
macroexpand.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
/*
* gomacro - A Go interpreter with Lisp-like macros
*
* Copyright (C) 2017-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/.
*
*
* macroexpand.go
*
* Created on Jun 09, 2017
* Author Massimiliano Ghilardi
*/
package fast
import (
"go/ast"
r "reflect"
. "github.com/cosmos72/gomacro/ast2"
"github.com/cosmos72/gomacro/base"
etoken "github.com/cosmos72/gomacro/go/etoken"
xr "github.com/cosmos72/gomacro/xreflect"
)
// MacroExpandNodeCodewalk traverses the whole AST tree using pre-order traversal,
// and replaces each node with the result of MacroExpandNode(node).
// It implements the macroexpansion phase
func (c *Comp) MacroExpandNodeCodewalk(in ast.Node) (out ast.Node, anythingExpanded bool) {
if in == nil {
return nil, false
}
var form Ast = ToAst(in)
form, anythingExpanded = c.MacroExpandCodewalk(form)
out = ToNode(form)
// if !anythingExpanded {
// c.Debugf("MacroExpand1() nothing to expand: %v <%v>", out, r.TypeOf(out))
//}
return out, anythingExpanded
}
// MacroExpandCodewalk traverses the whole AST tree using pre-order traversal,
// and replaces each node with the result of MacroExpand(node).
// It implements the macroexpansion phase
func (c *Comp) MacroExpandCodewalk(in Ast) (out Ast, anythingExpanded bool) {
return c.macroExpandCodewalk(in, 0)
}
func (c *Comp) macroExpandCodewalk(in Ast, quasiquoteDepth int) (out Ast, anythingExpanded bool) {
if in == nil || in.Size() == 0 {
return in, false
}
debug := c.Options&base.OptDebugMacroExpand != 0
if quasiquoteDepth <= 0 {
if debug {
c.Debugf("MacroExpandCodewalk: qq = %d, macroexpanding %v", quasiquoteDepth, in.Interface())
}
in, anythingExpanded = c.MacroExpand(in)
}
if in != nil {
in = base.UnwrapTrivialAst(in)
}
if in == nil {
return in, anythingExpanded
}
saved := in
if expr, ok := in.(UnaryExpr); ok {
op := expr.X.Op
switch op {
case etoken.MACRO:
break
case etoken.QUOTE:
// QUOTE prevents macroexpansion only if found outside any QUASIQUOTE
if quasiquoteDepth == 0 {
return saved, anythingExpanded
}
case etoken.QUASIQUOTE:
// extract the body of QUASIQUOTE
quasiquoteDepth++
case etoken.UNQUOTE, etoken.UNQUOTE_SPLICE:
// extract the body of UNQUOTE or UNQUOTE_SPLICE
quasiquoteDepth--
default:
goto Recurse
}
inChild := base.UnwrapTrivialAst(in.Get(0).Get(1))
outChild, expanded := c.macroExpandCodewalk(inChild, quasiquoteDepth)
if op == etoken.MACRO {
return outChild, expanded
}
out := in
if expanded {
out = base.MakeQuote2(expr, outChild.(AstWithNode))
}
return out, expanded
}
Recurse:
if in == nil {
return saved, anythingExpanded
}
if debug {
c.Debugf("MacroExpandCodewalk: qq = %d, recursing on %v", quasiquoteDepth, in)
}
out = in.New()
n := in.Size()
if outSlice, appendable := out.(AstWithSlice); appendable {
// New() returns zero-length slice... resize it
for outSlice.Size() < n {
outSlice = outSlice.Append(nil)
}
out = outSlice
}
for i := 0; i < n; i++ {
child := base.UnwrapTrivialAst(in.Get(i))
if child != nil {
expanded := false
if child.Size() != 0 {
child, expanded = c.macroExpandCodewalk(child, quasiquoteDepth)
}
if expanded {
anythingExpanded = true
}
}
out.Set(i, child)
}
if debug {
c.Debugf("MacroExpandCodewalk: qq = %d, expanded to %v", quasiquoteDepth, out)
}
return out, anythingExpanded
}
// MacroExpandNode repeatedly invokes MacroExpandNode1
// as long as the node represents a macro call.
// it returns the resulting node.
func (c *Comp) MacroExpandNode(in ast.Node) (out ast.Node, everExpanded bool) {
if in == nil {
return nil, false
}
inAst := ToAst(in)
outAst, everExpanded := c.MacroExpand(inAst)
out = ToNode(outAst)
// if !everExpanded {
// c.Debugf("MacroExpand1() not a macro: %v <%v>", out, r.TypeOf(out))
//}
return out, everExpanded
}
// MacroExpand repeatedly invokes MacroExpand
// as long as the node represents a macro call.
// it returns the resulting node.
func (c *Comp) MacroExpand(form Ast) (out Ast, everExpanded bool) {
var expanded bool
for {
form, expanded = c.MacroExpand1(form)
if !expanded {
return form, everExpanded
}
everExpanded = true
}
}
// if node represents a macro call, MacroExpandNode1 executes it
// and returns the resulting node.
// Otherwise returns the node argument unchanged
func (c *Comp) MacroExpandNode1(in ast.Node) (out ast.Node, expanded bool) {
if in == nil {
return nil, false
}
var form Ast = ToAst(in)
form, expanded = c.MacroExpand1(form)
out = ToNode(form)
// if !expanded {
// c.Debugf("MacroExpandNode1: not a macro: %v <%v>", out, r.TypeOf(out))
//}
return out, expanded
}
func (c *Comp) extractMacroCall(form Ast) Macro {
form = base.UnwrapTrivialAst(form)
switch form := form.(type) {
case Ident:
sym := c.TryResolve(form.X.Name)
if sym != nil && sym.Bind.Desc.Class() == ConstBind && sym.Type != nil && sym.Type.Kind() == r.Struct {
switch value := sym.Value.(type) {
case Macro:
if c.Options&base.OptDebugMacroExpand != 0 {
c.Debugf("MacroExpand1: found macro: %v", form.X.Name)
}
return value
}
}
}
return Macro{}
}
// if node represents a macro call, MacroExpandNode1 executes it
// and returns the resulting node.
// Otherwise returns the node argument unchanged
func (c *Comp) MacroExpand1(in Ast) (out Ast, expanded bool) {
if in == nil {
return nil, false
}
// unwrap trivial nodes: DeclStmt, ParenExpr, ExprStmt
in = base.UnwrapTrivialAstKeepBlocks(in)
ins, ok := in.(AstWithSlice)
if !ok {
return in, false
}
debug := c.Options&base.OptDebugMacroExpand != 0
if debug {
c.Debugf("MacroExpand1: found list: %v", ins.Interface())
}
n := ins.Size()
outs := ins.New().(AstWithSlice)
// since macro calls are sequences of statements,
// we must scan the whole list,
// consume it as needed by the macros we find,
// and build a new list accumulating the results of macroexpansion
for i := 0; i < n; i++ {
elt := ins.Get(i)
macro := c.extractMacroCall(elt)
if macro.closure == nil {
outs = outs.Append(elt)
continue
}
argn := macro.argNum
leftn := n - i - 1
var args []xr.Value
if argn > leftn {
args := make([]xr.Value, leftn+1) // include the macro itself
for j := 0; j <= leftn; j++ {
args[j] = xr.ValueOf(ins.Get(i + j).Interface())
}
c.Errorf("not enough arguments for macroexpansion of %v: expecting %d, found %d", args, macro.argNum, leftn)
return in, false
}
if debug {
c.Debugf("MacroExpand1: found macro call %v at %d-th position of %v", elt.Interface(), i, ins.Interface())
}
// wrap each ast.Node into a reflect.Value
args = make([]xr.Value, argn)
for j := 0; j < argn; j++ {
args[j] = xr.ValueOf(ToNode(ins.Get(i + j + 1)))
}
// invoke the macro
results := macro.closure(args)
if debug {
c.Debugf("MacroExpand1: macro expanded to: %v", results)
}
// a macro expansion can return multiple values.
// each value can be:
// * ast.Node or something that implements ast.Node
// * slice of: ast.Node or something that implements ast.Node
// * Ast or something that implements Ast
for _, result := range results {
if !result.IsValid() || !result.CanInterface() {
c.Warnf("MacroExpand1: cannot extract interface{} from reflect.Value result: %v", result)
continue
}
if result == None {
continue
}
res := AnyToAst(result.Interface(), "macroexpansion")
switch res := res.(type) {
case AstWithSlice:
n := res.Size()
for i := 0; i < n; i++ {
outs = outs.Append(res.Get(i))
}
case Ast:
outs = outs.Append(res)
case nil:
default:
c.Warnf("MacroExpand1: cannot convert result to Ast: %v", result)
continue
}
}
i += argn
expanded = true
}
if !expanded {
return in, false
}
if outs.Size() == 0 {
return EmptyStmt{&ast.EmptyStmt{}}, true
}
return base.UnwrapTrivialAst(outs), true
}