Skip to content

Commit

Permalink
refactor: use Meta.isexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Nov 11, 2024
1 parent 9d666f8 commit fc8a987
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ LinearAlgebra = "1.10"
NNlib = "0.9"
OrderedCollections = "1"
Preferences = "1.4"
ReactantCore = "0.1"
ReactantCore = "0.1.1"
Reactant_jll = "0.0.24"
Scratch = "1.2"
Statistics = "1.10"
Expand Down
2 changes: 1 addition & 1 deletion lib/ReactantCore/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ReactantCore"
uuid = "a3311ec8-5e00-46d5-b541-4f83e724a433"
authors = ["William Moses <[email protected]>", "Valentin Churavy <[email protected]>", "Sergio Sánchez Ramírez <[email protected]>", "Paul Berg <[email protected]>", "Avik Pal <[email protected]>"]
version = "0.1.0"
version = "0.1.1"

[deps]
ExpressionExplorer = "21656369-7473-754a-2065-74616d696c43"
Expand Down
16 changes: 7 additions & 9 deletions lib/ReactantCore/src/ReactantCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ end
"""
macro trace(expr)
expr = macroexpand(__module__, expr)
if expr.head == :(=)
if expr.args[2] isa Expr && expr.args[2].head == :if
if Meta.isexpr(expr, :(=))
if Meta.isexpr(expr.args[2], :if)
return esc(trace_if_with_returns(__module__, expr))
end
end
expr.head == :if && return esc(trace_if(__module__, expr))
Meta.isexpr(expr, :if) && return esc(trace_if(__module__, expr))
return error("Only `if-elseif-else` blocks are currently supported by `@trace`")
end

Expand Down Expand Up @@ -129,7 +129,7 @@ function trace_if(mod, expr; store_last_line=nothing, depth=0)
counter = 0
expr = MacroTools.prewalk(expr) do x
counter += 1
if x isa Expr && x.head == :if && counter > 1
if Meta.isexpr(x, :if) && counter > 1
ex_new, dv, _ = trace_if(mod, x; store_last_line, depth=depth + 1)
append!(discard_vars_from_expansion, dv)
return ex_new
Expand Down Expand Up @@ -161,7 +161,7 @@ function trace_if(mod, expr; store_last_line=nothing, depth=0)
true_branch_fn_name = gensym(:true_branch)

else_block, discard_vars, _ = if length(expr.args) == 3
if expr.args[3].head != :elseif
if Meta.isexpr(expr.args[3], :elseif)
expr.args[3], [], nothing
else
trace_if(mod, expr.args[3]; store_last_line, depth=depth + 1)
Expand Down Expand Up @@ -292,10 +292,8 @@ end

function error_if_return(expr)
return MacroTools.postwalk(expr) do x
if x isa Expr && x.head == :return
error("Cannot use @trace on a block that contains a return statement")
end
return x
Meta.isexpr(x, :return) || return x
error("Cannot use @trace on a block that contains a return statement")
end
end

Expand Down

0 comments on commit fc8a987

Please sign in to comment.