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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# ............................................................................................... #
#
# Copyright (c) 2012-2021 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0.
#
# SPDX-License-Identifier: EPL-2.0
#
# ............................................................................................... #
----
This module provides the [`quote`](#quote_1v) and [`unquote`](#unquote_1) macros
to ease the creation of IR node.
Using this macros is an alternative to creating IR nodes from scratch with the
fluent API. It can be seen as an equivalent of string interpolation for IR
nodes.
For instance, to create a node representing the following code:
```golo
|a, b| -> a + b
```
one can use the [fluent builder API](../ir.html), as in:
```golo
let node = lambda("a", "b"): returns(
plus(refLookup("a"), refLookup("b")))
```
or the [`quote`](#quote_1v) macro, as in:
```golo
let node = "e(|a, b| -> a + b)
```
Variable declared in the quoted code are mangled to prevent name clashes. For
instance:
```golo
let node = "e {
let tmp = 42
}
```
creates a code equivalent to:
```golo
let node = define("__$$_quoted_tmp"): as(constant(42))
```
Since the mangling function use a
[`SymbolGenerator`](../../../javadoc/org/eclipse/golo/compiler/SymbolGenerator.html),
variables and references whose name starts with `$` are not mangled (but the `$` is removed).
Unquoting can be compared to string interpolation. Unquoted variables containing
IR nodes are injected as is, without being quoted. For instance, to create a
`do while` macro, one can use:
```golo
macro doWhile = |condition, block| -> "e {
$(block)
while $(condition) {
$(block)
}
}
```
It is obviously possible to mix the API approach with the quoting one, as in:
```golo
`augment(String.class): `with("e {
function foo = |this| -> 42
})
```
----
module gololang.ir.Quote
import gololang.ir
let SYMBOLS = DynamicVariable(org.eclipse.golo.compiler.SymbolGenerator("quoted"))
local function mangle = |name, protected| -> match {
when protected is null then name
when protected: contains(name) then name
otherwise SYMBOLS: value(): getFor(name)
}
local function augmentProtected = |p, ns| {
if p is null {
return null
}
let newP = set[]
newP: addAll(p)
if ns isnt null {
newP: addAll(ns)
}
return newP
}
local function callIr = |name| ->
FunctionInvocation.of("gololang.ir." + name)
local function enumValue = |val| -> FunctionInvocation.of(
val: class(): name() + "." + val: name())
local function quoteNodeArray = |a, p| -> array[quoteNode(e, p) foreach e in a]
local function quoteConstant = |node, p| -> callIr("ConstantStatement.of"): withArgs(node)
local function quoteBlock = |node, p| -> match {
when node is null or node: isEmpty() then callIr("Block.empty")
when node: statements(): size() == 1 then quoteNode(node: statements(): get(0), p)
otherwise quoteLocalDeclaration(
callIr("Block.block"): withArgs(quoteNodeArray(node: statements(), p)),
node: declarations(), p)
}
local function quoteBinaryOp = |node, p| -> quoteLocalDeclaration(
callIr("BinaryOperation.create")
: withArgs(
enumValue(node: type()),
quoteNode(node: left(), p),
quoteNode(node: right(), p)),
node: declarations(), p)
local function quoteLocalDeclaration = |builder, declarations, p| {
var ir = builder
foreach dec in declarations {
ir = MethodInvocation.invoke("with")
: withArgs(quoteAssignment(dec, p))
: on(ir)
}
return ir
}
local function quoteUnaryOp = |node, p| -> quoteLocalDeclaration(
callIr("UnaryOperation.create")
: withArgs(
enumValue(node: type()),
quoteNode(node: expression(), p)),
node: declarations(), p)
local function quoteFunctionInvocation = |node, p| -> quoteLocalDeclaration(
callIr("FunctionInvocation.create")
: withArgs(
ConstantStatement.of(match {
when node: isAnonymous() then null
otherwise node: name()
}),
ConstantStatement.of(node: isOnReference()),
ConstantStatement.of(node: isOnModuleState()),
ConstantStatement.of(node: isConstant()))
: withArgs(quoteNodeArray(node: arguments(), p)),
node: declarations(), p)
local function quoteReturn = |node, p| -> match {
when node: isReturningVoid() then callIr("ReturnStatement.empty()")
otherwise callIr("ReturnStatement.of")
: withArgs(quoteNode(node: expression(), p))
}
local function quoteThrow = |node, p| -> callIr("ThrowStatement.of")
: withArgs(quoteNode(node: expression(), p))
local function quoteLocalRef = |node, p| -> callIr("LocalReference.create"): withArgs(
ConstantStatement.of(mangle(node: name(), p)),
enumValue(node: kind()))
local function quoteAssignment = |node, p| -> callIr("AssignmentStatement.create")
: withArgs(
quoteNode(node: localReference(), p),
quoteNode(node: expression(), p),
ConstantStatement.of(node: isDeclaring()))
local function quoteReference = |node, p| -> quoteLocalDeclaration(
callIr("ReferenceLookup.of"): withArgs(
ConstantStatement.of(mangle(node: name(), p))),
node: declarations(), p)
local function quoteCollection = |node, p| -> quoteLocalDeclaration(
callIr("CollectionLiteral.create")
: withArgs(enumValue(node: type()))
: withArgs(quoteNodeArray(node: expressions(), p)),
node: declarations(), p)
local function quoteModuleImport = |node, p| -> callIr("ModuleImport.of")
: withArgs(ConstantStatement.of(node: packageAndClass(): toString()))
local function quoteNamedArgument = |node, p| ->
callIr("NamedArgument.of")
: withArgs(
ConstantStatement.of(node: name()),
quoteNode(node: expression(), p))
local function quoteMember = |node, p| -> callIr("Member.of"): withArgs(
ConstantStatement.of(node: name()))
local function quoteMethodInvocation = |node, p| -> quoteLocalDeclaration(
MethodInvocation.invoke("withArgs")
: withArgs(quoteNodeArray(node: arguments(), p))
: on(MethodInvocation.invoke("nullSafe")
: withArgs(ConstantStatement.of(node: isNullSafeGuarded()))
: on(callIr("DSL.invoke"): withArgs(ConstantStatement.of(node: name())))),
node: declarations(), p)
local function quoteTryCatch = |node, p| -> callIr("TryCatchFinally.create"): withArgs(
ConstantStatement.of(match {
when node: hasCatchBlock() then mangle(node: exceptionId(), p)
otherwise null
}),
quoteBlock(node: tryBlock(), p),
match {
when node: hasCatchBlock() then quoteBlock(node: catchBlock(), p)
otherwise ConstantStatement.of(null)
},
match {
when node: hasFinallyBlock() then quoteBlock(node: finallyBlock(), p)
otherwise ConstantStatement.of(null)
})
local function quoteStruct = |node, p| -> MethodInvocation.invoke("members")
:withArgs(quoteNodeArray(node: getMembers(), p))
: on(callIr("Struct.struct"): withArgs(ConstantStatement.of(node: name())))
local function quoteUnion = |node, p| {
var ir = callIr("Union.union"): withArgs(ConstantStatement.of(node: name()))
foreach val in node: values() {
ir = MethodInvocation.invoke("value")
: withArgs(ConstantStatement.of(val: name()))
: withArgs(quoteNodeArray(val: getMembers(), p))
: on(ir)
}
return ir
}
local function quoteLoopBreak = |node, p| -> match {
when node: type() is LoopBreakFlowStatement$Type.BREAK() then
callIr("LoopBreakFlowStatement.newBreak")
when node: type() is LoopBreakFlowStatement$Type.CONTINUE() then
callIr("LoopBreakFlowStatement.newContinue")
otherwise raise("Unknown type")
}
local function quoteLoopStatement = |node, p| -> callIr("LoopStatement.create"): withArgs(
quoteNode(node: init(), p),
quoteNode(node: condition(), p),
quoteNode(node: post(), p),
quoteNode(node: block(), p))
local function quoteForEachLoop = |node, p| -> callIr("ForEachLoopStatement.create")
: withArgs(
ConstantStatement.of(node: isVarargs()),
quoteNode(node: iterable(), p),
quoteNode(node: whenClause(), p),
quoteNode(node: block(), p))
: withArgs(quoteNodeArray(node: references(), p))
local function quoteWhenClauses = |element, node, p| {
var ir = MethodInvocation.invoke("otherwise")
: withArgs(quoteNode(node: `otherwise(), p))
: on(element)
foreach c, v in node: clauses() {
ir = MethodInvocation.invoke("then"): withArgs(quoteNode(v, p))
: on(MethodInvocation.invoke("when"): withArgs(quoteNode(c, p))
: on(ir))
}
return ir
}
local function quoteConditional = |node, p| -> callIr("ConditionalBranching.create"): withArgs(
quoteNode(node: condition(), p),
quoteNode(node: trueBlock(), p),
quoteNode(node: falseBlock(), p),
quoteNode(node: elseConditionalBranching(), p))
local function quoteClosure = |node, p| {
var ir = callIr("DSL.lambda")
: withArgs(node: target(): parameterNames(): toArray())
let protected = augmentProtected(p, node: target(): parameterNames())
if node: isVarargs() {
ir = MethodInvocation.invoke("varargs"): on(ir)
}
ir = MethodInvocation.invoke("block")
: withArgs(callIr("Block.of")
: withArgs(quoteNode(node: target(): block(), protected)))
: on(ir)
return quoteLocalDeclaration(ir, node: declarations(), p)
}
local function quoteFunction = |node, p| {
let protected = augmentProtected(p, node: parameterNames())
var ir = callIr("GoloFunction.function"): withArgs(ConstantStatement.of(node: name()))
ir = MethodInvocation.invoke("withParameters")
: withArgs(array[ConstantStatement.of(n) foreach n in node: parameterNames()])
: on(ir)
ir = MethodInvocation.invoke("block")
: withArgs(callIr("Block.of")
: withArgs(quoteBlock(node: block(), protected)))
: on(ir)
if node: isVarargs() {
ir = MethodInvocation.invoke("varargs"): on(ir)
}
if node: isMacro() {
ir = MethodInvocation.invoke("asMacro"): on(ir)
}
if node: isLocal() {
ir = MethodInvocation.invoke("local"): on(ir)
}
if node: isDecorated() {
ir = MethodInvocation.invoke("decoratedWith")
: withArgs(quoteNodeArray(node: decorators(), p))
: on(ir)
}
ir = MethodInvocation.invoke("contextual")
: withArgs(ConstantStatement.of(node: isContextualMacro()))
: on(ir)
ir = MethodInvocation.invoke("special")
: withArgs(ConstantStatement.of(node: isSpecialMacro()))
: on(ir)
return ir
}
local function quoteDecorator = |node, p| {
let protected = augmentProtected(p, null)
if node: expression() oftype ReferenceLookup.class {
protected?: add(node: expression(): name())
}
return MethodInvocation.invoke("constant")
: withArgs(ConstantStatement.of(node: isConstant()))
: on(callIr("Decorator.of")
: withArgs(quoteNode(node: expression(), protected)))
}
local function quoteAugmentation = |node, p| {
var ir = callIr("Augmentation.of"): withArgs(ConstantStatement.of(node: target(): toString()))
ir = MethodInvocation.invoke("with")
: withArgs(array[ConstantStatement.of(name) foreach name in node: names()])
: on(ir)
ir = MethodInvocation.invoke("with")
: withArgs(quoteNodeArray(node: functions(), p))
: on(ir)
return ir
}
local function quoteNamedAugmentation = |node, p| -> MethodInvocation.invoke("add")
: withArgs(quoteNodeArray(node: functions(), p))
: on(callIr("NamedAugmentation.of"): withArgs(ConstantStatement.of(node: name())))
local function quoteDestruct = |node, p| {
var ir = callIr("DestructuringAssignment.destruct"): withArgs(quoteNode(node: expression(), p))
ir = MethodInvocation.invoke("to")
: withArgs(quoteNodeArray(node: references(), p))
: on(ir)
ir = MethodInvocation.invoke("declaring")
: withArgs(ConstantStatement.of(node: isDeclaring()))
: on(ir)
ir = MethodInvocation.invoke("varargs")
: withArgs(ConstantStatement.of(node: isVarargs()))
: on(ir)
if not node: isConstant() {
ir = MethodInvocation.invoke("variable"): on(ir)
}
return ir
}
local function quoteCollComprehension = |node, p| -> quoteLocalDeclaration(
callIr("CollectionComprehension.create")
: withArgs(enumValue(node: type()))
: withArgs(quoteNode(node: expression(), p))
: withArgs(quoteNodeArray(node: loops(), p)),
node: declarations(), p)
----
Quotes the given node by converting it into calls to [IR API](../ir.html).
This function should mainly be used by the [`quote`](#quote_1v) macro and not
called directly.
- *param* `node`: the node to quote
- *param* `p`: a set of names that must not be mangled, or `null` if no name
must be mangled
- *returns* an expression that evaluate to the quoted node.
----
function quoteNode = |node, p| -> match {
when node is null then ConstantStatement.of(null)
when isUnquoted(node) then node: metadata("unquoted", null)
when node oftype MacroInvocation.class then node
when node oftype ConstantStatement.class then quoteConstant(node, p)
when node oftype Block.class then quoteBlock(node, p)
when node oftype BinaryOperation.class then quoteBinaryOp(node, p)
when node oftype UnaryOperation.class then quoteUnaryOp(node, p)
when node oftype FunctionInvocation.class then quoteFunctionInvocation(node, p)
when node oftype ReturnStatement.class then quoteReturn(node, p)
when node oftype ThrowStatement.class then quoteThrow(node, p)
when node oftype AssignmentStatement.class then quoteAssignment(node, p)
when node oftype ReferenceLookup.class then quoteReference(node, p)
when node oftype CollectionLiteral.class then quoteCollection(node, p)
when node oftype ModuleImport.class then quoteModuleImport(node, p)
when node oftype NamedArgument.class then quoteNamedArgument(node, p)
when node oftype LocalReference.class then quoteLocalRef(node, p)
when node oftype MethodInvocation.class then quoteMethodInvocation(node, p)
when node oftype TryCatchFinally.class then quoteTryCatch(node, p)
when node oftype Struct.class then quoteStruct(node, p)
when node oftype Member.class then quoteMember(node, p)
when node oftype Union.class then quoteUnion(node, p)
when node oftype LoopBreakFlowStatement.class then quoteLoopBreak(node, p)
when node oftype LoopStatement.class then quoteLoopStatement(node, p)
when node oftype ForEachLoopStatement.class then quoteForEachLoop(node, p)
when node oftype MatchExpression.class then quoteWhenClauses(callIr("MatchExpression.match"), node, p)
when node oftype CaseStatement.class then quoteWhenClauses(callIr("CaseStatement.cases"), node, p)
when node oftype ConditionalBranching.class then quoteConditional(node, p)
when node oftype ClosureReference.class then quoteClosure(node, p)
when node oftype GoloFunction.class then quoteFunction(node, p)
when node oftype Decorator.class then quoteDecorator(node, p)
when node oftype Augmentation.class then quoteAugmentation(node, p)
when node oftype NamedAugmentation.class then quoteNamedAugmentation(node, p)
when node oftype DestructuringAssignment.class then quoteDestruct(node, p)
when node oftype CollectionComprehension.class then quoteCollComprehension(node, p)
when node oftype Noop.class then callIR("Noop.of"): withArgs(
ConstantStatement.of(node: comment()))
otherwise raise(String.format("Can't quote `%s`", node))
}
----
Checks if the node is unquoted.
----
function isUnquoted = |node| -> node: metadata("unquoted") orIfNull false
----
Wrapper function to make quote work on collections of node, blocks, and
toplevels
----
local function _quote_ = |protected, nodes| -> match {
when nodes: isEmpty() then ConstantStatement.of(null)
when nodes: size() == 1 then quoteNode(nodes: get(0), protected)
when nodes: get(0) oftype ToplevelGoloElement.class then
callIr("ToplevelElements.of"): withArgs(quoteNodeArray(nodes, protected))
otherwise callIr("Block.block"): withArgs(quoteNodeArray(nodes, protected))
}
----
Converts the given nodes to calls to builder functions.
This macro should "just work". If applied to a single statement, a single node is returned.
When applied to several statements, a block is returned, unless the statements are top-level ones, in which case a
`ToplevelElements` is returned.
See [`quoteNode`](#quoteNode_2).
----
@contextual
macro quote = |self, nodes...| {
SYMBOLS: value(): enter(manglePrefix(self))
let quoted = _quote_(set[], nodes)
SYMBOLS: value(): exit()
return quoted
}
----
Generate a mangle prefix from the macro call parameters.
This helps to make unique names when calling the same macro several times.
----
local function manglePrefix = |invocation| {
let mod = invocation: enclosingModule()
let fun = invocation: ancestorOfType(GoloFunction.class)
return "%s_%s_%d": format(
mod: packageAndClass(): toString(),
fun?: name() orIfNull "",
fun?: arity() orIfNull 0)
}
----
Converts the given nodes to calls to builder functions.
Contrary to [`quote`](#quote_1v), no name is mangled by this macro.
See [`quoteNode`](#quoteNode_2).
----
macro quoteNoMangle = |nodes...| -> _quote_(null, nodes)
----
Marks the node as unquoted so that it will be returned unchanged by the
[`quote`](#quote_1v) macro.
----
macro unquote = |node| -> node: metadata("unquoted", true)
----
Alias for [`unquote`](#unquote_1)
----
macro $ = |node| -> node: metadata("unquoted", true)