Skip to content
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

Fix code_lowered_single_expression bug #3

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 11 additions & 25 deletions src/macro_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,18 @@ end

fused_pairs(expr::Expr) = Meta.parse(_fused_pairs(expr))

function build_expr(s::String, code_remain)
n_subs = count("%", s)
if n_subs > 0
while n_subs > 0
regex = r"%[0-9]"
m = match(regex, s)
smatch = m.match
j = Meta.parse(smatch[2:end])
s = replace(s, smatch => string(code_remain[j]))
n_subs = count("%", s)
end
else
return s
end
return s
end

build_expr(code::Vector) = build_expr(string(code[end]), code)
# General case: do nothing (identity)
substitute(x, code) = x
substitute(x::Core.SSAValue, code) = substitute(code[x.id], code)
substitute(x::Core.ReturnNode, code) = substitute(code[x.val.id], code)
substitute(s::Symbol, code) = s
# Expression: recursively substitute for Expr
substitute(e::Expr, code) =
Expr(substitute(e.head, code), substitute.(e.args, Ref(code))...)

code_info(expr) = Base.Meta.lower(Main, expr).args[1]
function code_lowered_single_expression(expr)
code_lowered = Base.Meta.lower(Main, expr)
code_info = code_lowered.args[1]
code = code_info.code # vector
s = build_expr(code)
if startswith(s, "return ")
s = replace(s, "return " => "")
end
code = code_info(expr).code # vector
s = string(substitute(code[end], code))
return Base.Meta.parse(s)
end
2 changes: 1 addition & 1 deletion test/expr_code_lowered_single_expression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ end
thermo_params,
),
))
@test_broken MBF.code_lowered_single_expression(expr_in) == expr_out
@test MBF.code_lowered_single_expression(expr_in) == expr_out
end
Loading