@@ -306,7 +306,7 @@ generated thunks.
306
306
macro par (exs... )
307
307
opts = exs[1 : end - 1 ]
308
308
ex = exs[end ]
309
- _par (ex; lazy= true , opts= opts)
309
+ return esc ( _par (ex; lazy= true , opts= opts) )
310
310
end
311
311
312
312
"""
@@ -348,7 +348,7 @@ also passes along any options in an `Options` struct. For example,
348
348
macro spawn (exs... )
349
349
opts = exs[1 : end - 1 ]
350
350
ex = exs[end ]
351
- _par (ex; lazy= false , opts= opts)
351
+ return esc ( _par (ex; lazy= false , opts= opts) )
352
352
end
353
353
354
354
struct ExpandedBroadcast{F} end
@@ -363,39 +363,62 @@ function replace_broadcast(fn::Symbol)
363
363
end
364
364
365
365
function _par (ex:: Expr ; lazy= true , recur= true , opts= ())
366
- if ex. head == :call && recur
367
- f = replace_broadcast (ex. args[1 ])
368
- if length (ex. args) >= 2 && Meta. isexpr (ex. args[2 ], :parameters )
369
- args = ex. args[3 : end ]
370
- kwargs = ex. args[2 ]
371
- else
372
- args = ex. args[2 : end ]
373
- kwargs = Expr (:parameters )
366
+ f = nothing
367
+ body = nothing
368
+ arg1 = nothing
369
+ if recur && @capture (ex, f_ (allargs__)) || @capture (ex, f_ (allargs__) do cargs_ body_ end ) || @capture (ex, allargs__-> body_) || @capture (ex, arg1_[allargs__])
370
+ f = replace_broadcast (f)
371
+ if arg1 != = nothing
372
+ # Indexing (A[2,3])
373
+ f = Base. getindex
374
+ pushfirst! (allargs, arg1)
375
+ end
376
+ args = filter (arg-> ! Meta. isexpr (arg, :parameters ), allargs)
377
+ kwargs = filter (arg-> Meta. isexpr (arg, :parameters ), allargs)
378
+ if ! isempty (kwargs)
379
+ kwargs = only (kwargs). args
380
+ end
381
+ if body != = nothing
382
+ if f != = nothing
383
+ f = quote
384
+ ($ (args... ); $ (kwargs... ))-> $ f ($ (args... ); $ (kwargs... )) do $ cargs
385
+ $ body
386
+ end
387
+ end
388
+ else
389
+ f = quote
390
+ ($ (args... ); $ (kwargs... ))-> begin
391
+ $ body
392
+ end
393
+ end
394
+ end
374
395
end
375
- opts = esc .(opts)
376
- args_ex = _par .(args; lazy= lazy, recur= false )
377
- kwargs_ex = _par .(kwargs. args; lazy= lazy, recur= false )
378
396
if lazy
379
- return :(Dagger. delayed ($ ( esc (f)) , $ Options (;$ (opts... )))($ (args_ex ... ); $ (kwargs_ex ... )))
397
+ return :(Dagger. delayed ($ f , $ Options (;$ (opts... )))($ (args ... ); $ (kwargs ... )))
380
398
else
381
- sync_var = esc ( Base. sync_varname)
399
+ sync_var = Base. sync_varname
382
400
@gensym result
383
401
return quote
384
- let args = ( $ (args_ex ... ),)
385
- $ result = $ spawn ($ ( esc (f)) , $ Options (;$ (opts... )), args... ; $ (kwargs_ex ... ))
402
+ let
403
+ $ result = $ spawn ($ f , $ Options (;$ (opts... )), $ ( args... ) ; $ (kwargs ... ))
386
404
if $ (Expr (:islocal , sync_var))
387
405
put! ($ sync_var, schedule (Task (()-> wait ($ result))))
388
406
end
389
407
$ result
390
408
end
391
409
end
392
410
end
411
+ elseif lazy
412
+ # Recurse into the expression
413
+ return Expr (ex. head, _par_inner .(ex. args, lazy= lazy, recur= recur, opts= opts)... )
393
414
else
394
- return Expr (ex . head, _par .(ex . args, lazy = lazy, recur = recur, opts = opts) ... )
415
+ throw ( ArgumentError ( " Invalid Dagger task expression: $ex " ) )
395
416
end
396
417
end
397
- _par (ex:: Symbol ; kwargs... ) = esc (ex)
398
- _par (ex; kwargs... ) = ex
418
+ _par (ex; kwargs... ) = throw (ArgumentError (" Invalid Dagger task expression: $ex " ))
419
+
420
+ _par_inner (ex; kwargs... ) = ex
421
+ _par_inner (ex:: Expr ; kwargs... ) = _par (ex; kwargs... )
399
422
400
423
"""
401
424
Dagger.spawn(f, args...; kwargs...) -> DTask
0 commit comments