This module provides the quote
and unquote
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:
|a, b| -> a + b
one can use the fluent builder API, as in:
let node = lambda("a", "b"): returns(
plus(refLookup("a"), refLookup("b")))
or the quote
macro, as in:
let node = "e(|a, b| -> a + b)
Variable declared in the quoted code are mangled to prevent name clashes. For instance:
let node = "e {
let tmp = 42
}
creates a code equivalent to:
let node = define("__$$_quoted_tmp"): as(constant(42))
Since the mangling function use a
SymbolGenerator
,
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:
macro doWhile = |condition, block| -> "e {
$(block)
while $(condition) {
$(block)
}
}
It is obviously possible to mix the API approach with the quoting one, as in:
`augment(String.class): `with("e {
function foo = |this| -> 42
})
Alias for unquote
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
.
Converts the given nodes to calls to builder functions.
Contrary to quote
, no name is mangled by this macro.
See quoteNode
.
Marks the node as unquoted so that it will be returned unchanged by the
quote
macro.
Checks if the node is unquoted.
Quotes the given node by converting it into calls to IR API.
This function should mainly be used by the quote
macro and not
called directly.
node
: the node to quotep
: a set of names that must not be mangled, or null
if no name
must be mangled