Skip to content

Commit b7f3d62

Browse files
committed
Add hook for testing JuliaLowering in core
1 parent 41f1725 commit b7f3d62

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/JuliaLowering.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ _include("syntax_macros.jl")
3333

3434
_include("eval.jl")
3535
_include("compat.jl")
36+
_include("hooks.jl")
3637

3738
function __init__()
3839
_register_kinds()

src/hooks.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Becomes `Core._lower()` upon activating JuliaLowering.
3+
4+
Returns an svec with the lowered code (usually expr) as its first element, and
5+
(until integration is less experimental) whatever we want after it
6+
"""
7+
function core_lowering_hook(@nospecialize(code), mod::Module,
8+
file="none", line=0, world=typemax(Csize_t), warn=false)
9+
if !(code isa SyntaxTree || code isa Expr)
10+
# e.g. LineNumberNode, integer...
11+
return Core.svec(code)
12+
end
13+
st0 = code isa Expr ? expr_to_syntaxtree(code) : code
14+
try
15+
ctx1, st1 = expand_forms_1( mod, st0)
16+
ctx2, st2 = expand_forms_2( ctx1, st1)
17+
ctx3, st3 = resolve_scopes( ctx2, st2)
18+
ctx4, st4 = convert_closures(ctx3, st3)
19+
ctx5, st5 = linearize_ir( ctx4, st4)
20+
ex = to_lowered_expr(mod, st5)
21+
return Core.svec(ex, st5, ctx5)
22+
catch exc
23+
@error("JuliaLowering failed — falling back to flisp!",
24+
exception=(exc,catch_backtrace()),
25+
code=code, file=file, line=line, mod=mod)
26+
return Base.fl_lower(code, mod, file, line, world, warn)
27+
end
28+
end
29+
30+
# TODO: Write a parser hook here. The input to `core_lowering_hook` should
31+
# eventually be a (convertible to) SyntaxTree, but we need to make updates to
32+
# the parsing API to include a parameter for AST type.
33+
34+
const _has_v1_13_hooks = isdefined(Core, :_lower)
35+
36+
function activate!(enable=true)
37+
if !_has_v1_13_hooks
38+
error("Cannot use JuliaLowering without `Core._lower` binding or in $VERSION < 1.13")
39+
end
40+
41+
if enable
42+
Core._setlowerer!(core_lowering_hook)
43+
else
44+
Core._setlowerer!(Base.fl_lower)
45+
end
46+
end

0 commit comments

Comments
 (0)