-
-
Save olynch/72109e5d9d35fd293ab68fd5f60c64f2 to your computer and use it in GitHub Desktop.
This file contains 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 Macrology | |
export @declare, @with_macro_arg | |
using MLStyle | |
macro declare(expr) | |
(head, val) = @match expr begin | |
:($head = $val) => (head, val) | |
end | |
quote | |
macro $(esc(head))(expr) | |
@match expr begin | |
Expr(:macrocall, name, line, args...) => | |
Expr(:macrocall, name, line, $val, args...) | |
end | |
end | |
end | |
end | |
macro with_macro_arg(macrodef) | |
@match macrodef begin | |
Expr(:macro, Expr(:call, name, x, y), body) => begin | |
internal = gensym(:internal) | |
quote | |
macro $(esc(name))($x, $y) | |
@match $x begin | |
Expr(:macrocall, name, line) => | |
Expr(:macrocall, name, line, Expr(:macrocall, $(esc(Symbol("@" * string(internal)))), line, $y)) | |
end | |
end | |
macro $(esc(internal))($x, $y) | |
$body | |
end | |
end | |
end | |
end | |
end | |
@with_macro_arg macro add(x,y) | |
x + y | |
end | |
@declare x 9 | |
(@macroexpand (@add (@x) 3)) == 12 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment