|
| 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 | + |
| 14 | + # TODO: fix in base |
| 15 | + file = file isa Ptr{UInt8} ? unsafe_string(file) : file |
| 16 | + line = !(line isa Int64) ? Int64(line) : line |
| 17 | + |
| 18 | + st0 = code isa Expr ? expr_to_syntaxtree(code, LineNumberNode(line, file)) : code |
| 19 | + try |
| 20 | + ctx1, st1 = expand_forms_1( mod, st0) |
| 21 | + ctx2, st2 = expand_forms_2( ctx1, st1) |
| 22 | + ctx3, st3 = resolve_scopes( ctx2, st2) |
| 23 | + ctx4, st4 = convert_closures(ctx3, st3) |
| 24 | + ctx5, st5 = linearize_ir( ctx4, st4) |
| 25 | + ex = to_lowered_expr(mod, st5) |
| 26 | + return Core.svec(ex, st5, ctx5) |
| 27 | + catch exc |
| 28 | + @error("JuliaLowering failed — falling back to flisp!", |
| 29 | + exception=(exc,catch_backtrace()), |
| 30 | + code=code, file=file, line=line, mod=mod) |
| 31 | + return Base.fl_lower(code, mod, file, line, world, warn) |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +# TODO: Write a parser hook here. The input to `core_lowering_hook` should |
| 36 | +# eventually be a (convertible to) SyntaxTree, but we need to make updates to |
| 37 | +# the parsing API to include a parameter for AST type. |
| 38 | + |
| 39 | +const _has_v1_13_hooks = isdefined(Core, :_lower) |
| 40 | + |
| 41 | +function activate!(enable=true) |
| 42 | + if !_has_v1_13_hooks |
| 43 | + error("Cannot use JuliaLowering without `Core._lower` binding or in $VERSION < 1.13") |
| 44 | + end |
| 45 | + |
| 46 | + if enable |
| 47 | + Core._setlowerer!(core_lowering_hook) |
| 48 | + else |
| 49 | + Core._setlowerer!(Base.fl_lower) |
| 50 | + end |
| 51 | +end |
0 commit comments