Skip to content

Instantly share code, notes, and snippets.

@olynch
Created May 12, 2023 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olynch/72109e5d9d35fd293ab68fd5f60c64f2 to your computer and use it in GitHub Desktop.
Save olynch/72109e5d9d35fd293ab68fd5f60c64f2 to your computer and use it in GitHub Desktop.
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