Skip to content

Commit

Permalink
Merge pull request #3 from CliMA/ck/fix_build_expr
Browse files Browse the repository at this point in the history
Fix `code_lowered_single_expression` bug
  • Loading branch information
charleskawczynski authored Mar 9, 2024
2 parents 33fdb47 + 46e843d commit 44c58ed
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
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

2 comments on commit 44c58ed

@charleskawczynski
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/102559

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" 44c58ed70af3ec77138bd2dede0abfc4089f9e7e
git push origin v0.1.0

Please sign in to comment.