-
Notifications
You must be signed in to change notification settings - Fork 9
Add hook for testing JuliaLowering in core #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| """ | ||
| Becomes `Core._lower()` upon activating JuliaLowering. | ||
|
|
||
| Returns an svec with the lowered code (usually expr) as its first element, and | ||
| (until integration is less experimental) whatever we want after it | ||
| """ | ||
| function core_lowering_hook(@nospecialize(code), mod::Module, | ||
| file="none", line=0, world=typemax(Csize_t), warn=false) | ||
| if !(code isa SyntaxTree || code isa Expr) | ||
| # e.g. LineNumberNode, integer... | ||
| return Core.svec(code) | ||
| end | ||
|
|
||
| # TODO: fix in base | ||
| file = file isa Ptr{UInt8} ? unsafe_string(file) : file | ||
| line = !(line isa Int64) ? Int64(line) : line | ||
|
|
||
| st0 = code isa Expr ? expr_to_syntaxtree(code, LineNumberNode(line, file)) : code | ||
| try | ||
| ctx1, st1 = expand_forms_1( mod, st0) | ||
| ctx2, st2 = expand_forms_2( ctx1, st1) | ||
| ctx3, st3 = resolve_scopes( ctx2, st2) | ||
| ctx4, st4 = convert_closures(ctx3, st3) | ||
| ctx5, st5 = linearize_ir( ctx4, st4) | ||
| ex = to_lowered_expr(mod, st5) | ||
| return Core.svec(ex, st5, ctx5) | ||
| catch exc | ||
| @error("JuliaLowering failed — falling back to flisp!", | ||
| exception=(exc,catch_backtrace()), | ||
| code=code, file=file, line=line, mod=mod) | ||
| return Base.fl_lower(code, mod, file, line, world, warn) | ||
| end | ||
| end | ||
|
|
||
| # TODO: Write a parser hook here. The input to `core_lowering_hook` should | ||
| # eventually be a (convertible to) SyntaxTree, but we need to make updates to | ||
| # the parsing API to include a parameter for AST type. | ||
|
|
||
| const _has_v1_13_hooks = isdefined(Core, :_lower) | ||
|
|
||
| function activate!(enable=true) | ||
| if !_has_v1_13_hooks | ||
| error("Cannot use JuliaLowering without `Core._lower` binding or in $VERSION < 1.13") | ||
| end | ||
|
|
||
| if enable | ||
| Core._setlowerer!(core_lowering_hook) | ||
| else | ||
| Core._setlowerer!(Base.fl_lower) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| const JL = JuliaLowering | ||
|
|
||
| @testset "hooks" begin | ||
| test_mod = Module() | ||
|
|
||
| @testset "`core_lowering_hook`" begin | ||
| # Non-AST types are often sent through lowering | ||
| stuff = Any[LineNumberNode(1), 123, 123.123, true, "foo", test_mod] | ||
| for s in stuff | ||
| @test JL.core_lowering_hook(s, test_mod) == Core.svec(s) | ||
| end | ||
|
|
||
| for ast_type in (Expr, JL.SyntaxTree) | ||
| ex = parsestmt(ast_type, "[1,2,3] .+= 1") | ||
| out = JL.core_lowering_hook(ex, test_mod) | ||
| @test out isa Core.SimpleVector && out[1] isa Expr | ||
| val = Core.eval(test_mod, out[1]) | ||
| @test val == [2,3,4] | ||
| end | ||
| end | ||
|
|
||
| @testset "integration: `JuliaLowering.activate!`" begin | ||
| prog = parseall(Expr, "global asdf = 1") | ||
| JL.activate!() | ||
| out = Core.eval(test_mod, prog) | ||
| JL.activate!(false) | ||
| @test out === 1 | ||
| @test isdefined(test_mod, :asdf) | ||
|
|
||
| prog = parseall(Expr, "module M; x = 1; end") | ||
| JL.activate!() | ||
| out = Core.eval(test_mod, prog) | ||
| JL.activate!(false) | ||
| @test out isa Module | ||
| @test isdefined(test_mod, :M) | ||
| @test isdefined(test_mod.M, :x) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,4 +26,5 @@ include("utils.jl") | |
| include("scopes.jl") | ||
| include("typedefs.jl") | ||
| include("compat.jl") | ||
| include("hooks.jl") | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so I see that in JuliaLang/julia#58207 you allowed for the trailing elements of this to be anything and we'll specify it later (seems reasonable). Are
st5andctx5used by JETLS somehow? (I see they're not used by the C code as expected ... who knows what the ctx5 API even is 😅 )There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not used by JETLS yet, and even if it was, you're free to "break" this non-API. JETLS does use
ctxobjects outside ofCore._lower, though, I think mostly for the binding table.The C code unfortunately has no idea what a
SyntaxTreeis yet. I ran into this when trying to makeCore._parseproduce a syntax tree---toplevel.c had no way to positively check the thing it parsed was aSyntaxTree, and I had to smuggle it through by wrapping it inExprin the parse hook, which is pretty goofy.