There are two solutions to this problem that I like:
1: Have the inner macro body in a function then reuse the function after
macro outer()
function inner()
end
quote
macro inner()
$(inner)()There are two solutions to this problem that I like:
1: Have the inner macro body in a function then reuse the function after
macro outer()
function inner()
end
quote
macro inner()
$(inner)()| const __reload_ignore_names = union( | |
| Set(names(Core)), | |
| Set(names(Base)), | |
| Set([:Main, :LastMain, :Base, :Core, :ans]) | |
| ) | |
| function reload_file(filename) | |
| @gensym filemod_name mainmod_name | |
| mainmod = current_module() |
| module DynamicScope | |
| export @dynamic, @define_dynamic, UnboundDynamicVarError, InvalidDynamicPathError | |
| type DynamicVar{T} | |
| name::Symbol | |
| values::Vector{T} | |
| end | |
| type UnboundDynamicVarError <: Exception | |
| name::Symbol |
| module BenchmarkWith | |
| using BenchmarkTools | |
| export @benchmark_with | |
| export @benchmark_function | |
| macro benchmark_with(args...) | |
| esc(benchmark_with(args...)) | |
| end | |
| macro benchmark_function(name, args...) |
| # All expressions must evaluate to true | |
| # Swap the first two arguments of a call expression | |
| @swap_arguments(1 - 2) == 2 - 1 | |
| # "Invert" the operator of an expression | |
| @invert_op(1 - 2) == 1 + 2 | |
| @invert_op(1 + 2) == 1 - 2 | |
| @invert_op(1 * 2) == 1 / 2 | |
| @invert_op(1 / 2) == 1 * 2 |
| 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))") |
| module Muladd | |
| macro muladd(ex) | |
| @assert ex.head == :call | |
| @assert ex.args != [:+] | |
| @assert ex.args[1] == :+ | |
| esc(_muladd_meta(ex)) | |
| end | |
| function _muladd_meta(ex) |
| module MuladdMacro | |
| export @muladd, UnexpectedMuladdExpression | |
| macro muladd(add) | |
| validate_muladd(add) | |
| esc(to_muladd(add)) | |
| end | |
| function to_muladd(add) | |
| mul = operands(add)[1] |
| macro muladd(ex) | |
| esc(to_muladd(ex)) | |
| end | |
| function to_muladd(ex) | |
| is_add_operation(ex) || return ex | |
| all_operands = ex.args[2:end] | |
| mul_operands = filter(is_mul_operation, all_operands) | |
| odd_operands = filter(x->!is_mul_operation(x), all_operands) |
| function OptLets(name,default) | |
| let a:let1 = "let a:__OPT_ARGNUM__ = get(a:, '__OPT_ARGNUM__', 0)+1" | |
| let a:let2 = "let a:".a:name." = get(a:, a:__OPT_ARGNUM__, ".a:default.")" | |
| return a:let1."\n".a:let2 | |
| endfunction | |
| command -nargs=+ Optional execute OptLets(<f-args>) |