Skip to content

Commit

Permalink
Some cleanup to demo examples
Browse files Browse the repository at this point in the history
  • Loading branch information
c42f committed May 23, 2024
1 parent ef6f9f5 commit 18afd46
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 94 deletions.
153 changes: 59 additions & 94 deletions test/demo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ using JuliaLowering: SyntaxGraph, SyntaxTree, ensure_attributes!, ensure_attribu
using JuliaSyntaxFormatter
using JuliaSyntaxFormatter: FormatContext

function formatsrc(ex; color_by=nothing, kws...)
format_token_style = if !isnothing(color_by)
e->get(e, color_by, nothing)
else
e->nothing
end
Text(JuliaSyntaxFormatter.format(ex; format_token_style, kws...))
end

function annotate_scopes(mod, ex)
ex = ensure_attributes(ex, var_id=Int)
ctx1, ex_macroexpand = JuliaLowering.expand_forms_1(mod, ex)
ctx2, ex_desugar = JuliaLowering.expand_forms_2(ctx1, ex_macroexpand)
ctx3, ex_scoped = JuliaLowering.resolve_scopes!(ctx2, ex_desugar)
ex
end

#-------------------------------------------------------------------------------
# Demos of the prototype

Expand Down Expand Up @@ -85,48 +102,7 @@ end
# end
# """

JuliaLowering.include_string(Main, """
module M
using JuliaLowering: JuliaLowering, @ast, @chk, adopt_scope
using JuliaSyntax
# Introspection
macro __MODULE__()
__context__.scope_layer.mod
end
macro __FILE__()
JuliaLowering.filename(__context__.macroname)
end
macro __LINE__()
JuliaLowering.source_location(__context__.macroname)[1]
end
someglobal = "global in module M"
# Macro with local variables
macro foo(ex)
:(begin
x = "`x` from @foo"
(x, someglobal, \$ex)
end)
end
macro set_a_global(val)
:(begin
global a_global = \$val
end)
end
macro set_global_in_parent(ex)
e1 = adopt_scope(:(sym_introduced_from_M), __context__)
quote
\$e1 = \$ex
end
end
end
""")
JuliaLowering.include(Main, "demo_include.jl")

Base.eval(M, quote
function var"@inert"(__context__::JuliaLowering.MacroContext, ex)
Expand Down Expand Up @@ -180,17 +156,33 @@ function softscope_test(ex)
wrapscope(wrapscope(JuliaLowering.reparent(g, ex), :neutral), :soft)
end

# src = """
# M.@test_inert_quote()
# """

# src = """
# macro mmm(a; b=2)
# end
# macro A.b(ex)
# end
# """

src = """
begin
x = 42
M.@foo x
end
M.@set_global_in_parent "bent hygiene!"
"""

# src = """
# begin
# M.@__LINE__
# end
# """

# src = """@foo z"""

src = """
begin
M.@set_a_global 42
M.a_global
x = 42
M.@foo x
end
"""

Expand All @@ -199,12 +191,16 @@ end
# """

# src = """
# macro mmm(a; b=2)
# end
# macro A.b(ex)
# begin
# M.@set_a_global 1000
# M.a_global
# end
# """

# src = """
# M.@set_global_in_parent "bent hygiene!"
# """

src = """
begin
x = 10
Expand All @@ -222,65 +218,34 @@ begin
end
"""

src = """
M.@set_global_in_parent "bent hygiene!"
"""

# src = """
# begin
# M.@__LINE__
# x = -1
# M.@baz x
# end
# """

# src = """@foo z"""

# TODO:
# "hygiene bending" / (being unhygenic, or bending hygiene to the context of a
# macro argument on purpose)
# * bend to macro name to get to parent layer?
# * already needed in `#self#` argument

function printsrc(ex; color_by=nothing, kws...)
format_token_style = if !isnothing(color_by)
e->get(e, color_by, nothing)
else
e->nothing
end
print(JuliaSyntaxFormatter.format(ex; format_token_style, kws...))
end

function annotate_scopes(mod, ex)
ex = ensure_attributes(ex, var_id=Int)
ctx1, ex_macroexpand = JuliaLowering.expand_forms_1(mod, ex)
ctx2, ex_desugar = JuliaLowering.expand_forms_2(ctx1, ex_macroexpand)
ctx3, ex_scoped = JuliaLowering.resolve_scopes!(ctx2, ex_desugar)
ex
end

ex = parsestmt(SyntaxTree, src, filename="foo.jl")
ex = ensure_attributes(ex, var_id=Int)
#ex = softscope_test(ex)
@info "Input code" ex

in_mod = Main
ctx1, ex_macroexpand = JuliaLowering.expand_forms_1(in_mod, ex)
@info "Macro expanded" ex_macroexpand
@info "Macro expanded" formatsrc(ex_macroexpand, color_by=:scope_layer)

ctx2, ex_desugar = JuliaLowering.expand_forms_2(ctx1, ex_macroexpand)
@info "Desugared" ex_desugar
@info "Desugared" formatsrc(ex_desugar, color_by=:scope_layer)

ctx3, ex_scoped = JuliaLowering.resolve_scopes!(ctx2, ex_desugar)
@info "Resolved scopes" ex_scoped

#printsrc(ex, color_by=:var_id)
#printsrc(ex_macroexpand, color_by=:scope_layer)
@info "Resolved scopes" formatsrc(ex_scoped, color_by=:var_id)

ctx4, ex_compiled = JuliaLowering.linearize_ir(ctx3, ex_scoped)
@info "Linear IR" ex_compiled

ex_expr = JuliaLowering.to_lowered_expr(in_mod, ctx4.var_info, ex_compiled)
@info "CodeInfo" ex_expr

eval_result = Base.eval(in_mod, ex_expr)
@info "Eval" eval_result
@info "Linear IR" formatsrc(ex_compiled, color_by=:var_id)

# ex_expr = JuliaLowering.to_lowered_expr(in_mod, ctx4.var_info, ex_compiled)
# @info "CodeInfo" ex_expr
#
# eval_result = Base.eval(in_mod, ex_expr)
# @info "Eval" eval_result
#
59 changes: 59 additions & 0 deletions test/demo_include.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module M
using JuliaLowering: JuliaLowering, @ast, @chk, adopt_scope
using JuliaSyntax

# Introspection
macro __MODULE__()
__context__.scope_layer.mod
end

macro __FILE__()
JuliaLowering.filename(__context__.macroname)
end

macro __LINE__()
JuliaLowering.source_location(__context__.macroname)[1]
end

module A
another_global = "global in A"

macro bar(ex)
quote
x = "`x` in @bar"
(x, another_global, $ex)
end
end
end

someglobal = "global in module M"

# Macro with local variables
macro foo(ex)
quote
x = "`x` from @foo"
(x, someglobal, A.@bar $ex)
end
end

macro set_a_global(val)
quote
global a_global = $val
end
end

macro set_global_in_parent(ex)
e1 = adopt_scope(:(sym_introduced_from_M), __context__)
quote
$e1 = $ex
end
end

macro baz(ex)
quote
let $ex = 10
$ex
end
end
end
end

0 comments on commit 18afd46

Please sign in to comment.