|
| 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