Skip to content

Commit f55df67

Browse files
authored
Implement @ eval macro for SyntaxTree (#107)
1 parent 0b7124c commit f55df67

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

src/kinds.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function _register_kinds()
165165
"new_opaque_closure"
166166
# Wrapper for the lambda of around opaque closure methods
167167
"opaque_closure_method"
168-
# World age increment
168+
# World age increment (TODO: use top level assertion and only one latestworld kind)
169169
"latestworld"
170170
"END_IR_KINDS"
171171
])

src/syntax_macros.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,33 @@ function Base.Experimental.var"@opaque"(__context__::MacroContext, ex)
272272
]
273273
end
274274

275+
function _at_eval_code(ctx, srcref, mod, ex)
276+
@ast ctx srcref [K"block"
277+
[K"local"
278+
[K"="
279+
"eval_result"::K"Identifier"
280+
[K"call"
281+
# TODO: Call "eval"::K"core" here
282+
JuliaLowering.eval::K"Value"
283+
mod
284+
[K"quote" ex]
285+
]
286+
]
287+
]
288+
(::K"latestworld_if_toplevel")
289+
"eval_result"::K"Identifier"
290+
]
291+
end
292+
293+
function Base.var"@eval"(__context__::MacroContext, ex)
294+
mod = @ast __context__ __context__.macrocall __context__.scope_layer.mod::K"Value"
295+
_at_eval_code(__context__, __context__.macrocall, mod, ex)
296+
end
297+
298+
function Base.var"@eval"(__context__::MacroContext, mod, ex)
299+
_at_eval_code(__context__, __context__.macrocall, mod, ex)
300+
end
301+
275302
#--------------------------------------------------------------------------------
276303
# The following `@islocal` and `@inert` are macros for special syntax known to
277304
# lowering which don't exist in Base but arguably should.

test/misc.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ let x = [1,2]
2020
end
2121
""") == [1,2]
2222

23+
@test JuliaLowering.include_string(test_mod, raw"""
24+
let
25+
x = 10
26+
@eval $x + 2
27+
end
28+
""") == 12
29+
30+
@test JuliaLowering.include_string(test_mod, raw"""
31+
module EvalTest
32+
_some_var = 2
33+
end
34+
let
35+
x = 10
36+
@eval EvalTest $x + _some_var
37+
end
38+
""") == 12
39+
2340
@test JuliaLowering.include_string(test_mod, """
2441
let x=11
2542
20x

test/misc_ir.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,31 @@ GC.@preserve a b g() begin
294294
body
295295
end
296296

297+
########################################
298+
# @eval without module
299+
@eval $f(x, y)
300+
#---------------------
301+
1 TestMod.f
302+
2 (call core.tuple %₁)
303+
3 (call JuliaLowering.interpolate_ast SyntaxTree (inert (call ($ f) x y)) %₂)
304+
4 (= slot₁/eval_result (call JuliaLowering.eval TestMod %₃))
305+
5 latestworld
306+
6 slot₁/eval_result
307+
7 (return %₆)
308+
309+
########################################
310+
# @eval with module
311+
@eval mod $f(x, y)
312+
#---------------------
313+
1 TestMod.mod
314+
2 TestMod.f
315+
3 (call core.tuple %₂)
316+
4 (call JuliaLowering.interpolate_ast SyntaxTree (inert (call ($ f) x y)) %₃)
317+
5 (= slot₁/eval_result (call JuliaLowering.eval %%₄))
318+
6 latestworld
319+
7 slot₁/eval_result
320+
8 (return %₇)
321+
297322
########################################
298323
# Juxtaposition
299324
20x

0 commit comments

Comments
 (0)