Skip to content

Instantly share code, notes, and snippets.

@fcard
Created December 5, 2016 20:10
Show Gist options
  • Select an option

  • Save fcard/85f4295e9c7ecb6b831793f83f0a6a9c to your computer and use it in GitHub Desktop.

Select an option

Save fcard/85f4295e9c7ecb6b831793f83f0a6a9c to your computer and use it in GitHub Desktop.
Local macro in julia
module LetMacro
export @letmacro
macro letmacro(def, block)
sign = def.args[1]
body = def.args[2]
name = Symbol("@$(sign.args[1])")
args = sign.args[2:end]
newname = Symbol("@$(gensym(name))")
new_block = replace_macroname(block, name, newname)
eval(current_module(), :($newname($(args...)) = $(body)))
esc(new_block)
end
function replace_macroname(ex::Expr, name, newname)
if ex.head == :macrocall && ex.args[1] == name
Expr(:macrocall, newname, ex.args[2:end]...)
else
Expr(ex.head, replace_macroname.(ex.args, name, newname)...)
end
end
function replace_macroname(ex, name, newname)
ex
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment