@@ -142,21 +142,22 @@ function parse_cat_ast(ex::Expr)
142142 cat_any (Val (maxdim), Val (catdim), nargs)
143143end
144144
145+ escall (args) = Iterators. map (esc, args)
145146function static_array_gen (:: Type{SA} , @nospecialize (ex), mod:: Module ) where {SA}
146147 if ! isa (ex, Expr)
147148 error (" Bad input for @$SA " )
148149 end
149150 head = ex. head
150151 if head === :vect # vector
151- return :($ SA {Tuple{$(length(ex.args))}} (tuple ($ (ex. args... ))))
152+ return :($ SA {$ Tuple{$(length(ex.args))}} ($ tuple ($ (escall ( ex. args) ... ))))
152153 elseif head === :ref # typed, vector
153- return :($ SA {Tuple{$(length(ex.args)-1)},$(ex.args[1])} ( tuple ($ (ex. args[2 : end ]. .. ))))
154+ return :($ SA {$ Tuple{$(length(ex.args)-1)},$(esc( ex.args[1]))} ( $ tuple ($ (escall ( ex. args[2 : end ]) ... ))))
154155 elseif head === :typed_vcat || head === :typed_hcat || head === :typed_ncat # typed, cat
155156 args = parse_cat_ast (ex)
156- return :($ SA {Tuple{$(size(args)...)},$(ex.args[1])} ( tuple ($ (args... ))))
157+ return :($ SA {$ Tuple{$(size(args)...)},$(esc( ex.args[1]))} ( $ tuple ($ (escall ( args) ... ))))
157158 elseif head === :vcat || head === :hcat || head === :ncat # untyped, cat
158159 args = parse_cat_ast (ex)
159- return :($ SA {Tuple{$(size(args)...)}} (tuple ($ (args... ))))
160+ return :($ SA {$ Tuple{$(size(args)...)}} ($ tuple ($ (escall ( args) ... ))))
160161 elseif head === :comprehension
161162 if length (ex. args) != 1 || ! isa (ex. args[1 ], Expr) || ex. args[1 ]. head != :generator
162163 error (" Expected generator in comprehension, e.g. [f(i,j) for i = 1:3, j = 1:3]" )
@@ -167,42 +168,44 @@ function static_array_gen(::Type{SA}, @nospecialize(ex), mod::Module) where {SA}
167168 rngs = Any[Core. eval (mod, ex. args[i+ 1 ]. args[2 ]) for i = 1 : n_rng]
168169 exprs = (:(f ($ (j... ))) for j in Iterators. product (rngs... ))
169170 return quote
170- let f ($ (rng_args... )) = $ (ex. args[1 ])
171- $ SA {Tuple{$(size(exprs)...)}} (tuple ($ (exprs... )))
171+ let
172+ f ($ (escall (rng_args). .. )) = $ (esc (ex. args[1 ]))
173+ $ SA {$Tuple{$(size(exprs)...)}} ($ tuple ($ (exprs... )))
172174 end
173175 end
174176 elseif head === :typed_comprehension
175177 if length (ex. args) != 2 || ! isa (ex. args[2 ], Expr) || ex. args[2 ]. head != :generator
176178 error (" Expected generator in typed comprehension, e.g. Float64[f(i,j) for i = 1:3, j = 1:3]" )
177179 end
178- T = ex. args[1 ]
180+ T = esc ( ex. args[1 ])
179181 ex = ex. args[2 ]
180182 n_rng = length (ex. args) - 1
181183 rng_args = (ex. args[i+ 1 ]. args[1 ] for i = 1 : n_rng)
182184 rngs = Any[Core. eval (mod, ex. args[i+ 1 ]. args[2 ]) for i = 1 : n_rng]
183185 exprs = (:(f ($ (j... ))) for j in Iterators. product (rngs... ))
184186 return quote
185- let f ($ (rng_args... )) = $ (ex. args[1 ])
186- $ SA {Tuple{$(size(exprs)...)},$T} (tuple ($ (exprs... )))
187+ let
188+ f ($ (escall (rng_args). .. )) = $ (esc (ex. args[1 ]))
189+ $ SA {$Tuple{$(size(exprs)...)},$T} ($ tuple ($ (exprs... )))
187190 end
188191 end
189192 elseif head === :call
190193 f = ex. args[1 ]
191194 if f === :zeros || f === :ones || f === :rand || f === :randn || f === :randexp
192195 if length (ex. args) == 1
193196 f === :zeros || f === :ones || error (" @$SA got bad expression: $(ex) " )
194- return :($ f ($ SA{Tuple{},Float64}))
197+ return :($ f ($ SA{$ Tuple{},$ Float64}))
195198 end
196199 return quote
197- if isa ($ (ex. args[2 ]), DataType)
198- $ f ($ SA{Tuple{$ (ex. args[3 : end ]. .. )},$ (ex. args[2 ])})
200+ if isa ($ (esc ( ex. args[2 ]) ), DataType)
201+ $ f ($ SA{$ Tuple{$ (escall ( ex. args[3 : end ]) ... )},$ (esc ( ex. args[2 ]) )})
199202 else
200- $ f ($ SA{Tuple{$ (ex. args[2 : end ]. .. )}})
203+ $ f ($ SA{$ Tuple{$ (escall ( ex. args[2 : end ]) ... )}})
201204 end
202205 end
203206 elseif f === :fill
204207 length (ex. args) == 1 && error (" @$SA got bad expression: $(ex) " )
205- return :($ f ($ (ex. args[2 ]), $ SA{Tuple{$ (ex. args[3 : end ]. .. )}}))
208+ return :($ f ($ (esc ( ex. args[2 ])) , $ SA{$ Tuple{$ (escall ( ex. args[3 : end ]) ... )}}))
206209 else
207210 error (" @$SA only supports the zeros(), ones(), fill(), rand(), randn(), and randexp() functions." )
208211 end
@@ -235,7 +238,7 @@ It supports:
235238 Only support `zeros()`, `ones()`, `fill()`, `rand()`, `randn()`, and `randexp()`
236239"""
237240macro SArray (ex)
238- esc ( static_array_gen (SArray, ex, __module__) )
241+ static_array_gen (SArray, ex, __module__)
239242end
240243
241244function promote_rule (:: Type{<:SArray{S,T,N,L}} , :: Type{<:SArray{S,U,N,L}} ) where {S,T,U,N,L}
0 commit comments