Created
December 5, 2016 20:10
-
-
Save fcard/85f4295e9c7ecb6b831793f83f0a6a9c to your computer and use it in GitHub Desktop.
Local macro in julia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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