How to rewrite this macro?

I use the macro below for a partial evaluation for a Macro to serialize a linear solver code.
The code change is done on the string form.
Can the macro be written without converting forth and back?
Got lost in quoting and escaping.

macro mint(e)
    @show e.args # [:A, :i, :j]
    s = string(e)                 # Expr -> String
    s = replace(s, "i" => "\$i")  # replace index names by their interpolation
    s = replace(s, "j" => "\$j")
    s = replace(s, "k" => "\$k")
    s = ":($s)"                   # quote to keep expression
    e = Meta.parse(s)             # String -> Expr
    @show e.args # [:(A[$(Expr(:$, :i)), $(Expr(:$, :j))])]
    quote
      $(esc(e))                           
    end
end

i =1; j = 2
A = rand(2,2)
@mint A[i, j] # :(A[1, 2])

dump is your friend. Just pass it the final expression you want and you can “dissect” it with that.

2 Likes

Works - TYVM!

macro mint(e)
  e = Expr(:quote, Expr(:ref, e.args[1], map(i->:($(Expr(:$, i))), e.args[2:end])...))
  quote
    $(esc(e))         
  end
end