> Hello everyone, > > I am trying to write a macro that transforms an expression like: > > @foo v[1,2] + v[1,3]*v[2,4] -> v[I[1,2]] + v[I[1,3]]*v[I[2,4]] > > Basically, for each getindex I want to insert a lookup of the index in some > other variable. > The reason for this is that I am working with matrices encoded as vectors > and I want to still call them like matrices. The "I" vector in the code > is the lookup from two dimensional indexing to one dimensional. > > v is just a normal Vector so I cannot extend getindex which is why I > thought a macro would be the cleanest solution. > > I am a bit lost how to start so any help would be appreciated.
Sounds like you may want to use https://github.com/mbauman/RaggedArrays.jl (or my more primitive Ragged.jl) If not, this may work: function sub!(e::Expr) if e.head==:ref for a in e.args sub!(a) end newe = :($(e.args[1])[I[$(e.args[2:end]...)]]) e.args = newe.args else for a in e.args sub!(a) end end e end sub!(s) = s e = :(v[1,2] + v[1,3]*v[2,4]) sub!(e) macro foo(e) sub!(e) end