forked from jump-dev/JuMP.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseExpr_staged.jl
638 lines (585 loc) · 21.1 KB
/
parseExpr_staged.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
function tryParseIdxSet(arg::Expr)
if arg.head === :(=)
@assert length(arg.args) == 2
return true, arg.args[1], arg.args[2]
elseif isexpr(arg, :call) && arg.args[1] === :in
return true, arg.args[2], arg.args[3]
else
return false, nothing, nothing
end
end
function parseIdxSet(arg::Expr)
parse_done, idxvar, idxset = tryParseIdxSet(arg)
if parse_done
return idxvar, idxset
end
error("Invalid syntax: $arg")
end
# addtoexpr(ex::Number, c::Number) = ex + c
addtoexpr(ex::Number, c::Number, x::Number) = ex + c*x
addtoexpr(ex::Number, c::Number, x::Variable) = AffExpr([x],[c],ex)
function addtoexpr{T<:GenericAffExpr}(ex::Number, c::Number, x::T)
# It's only safe to mutate the first argument.
if c == 0
T(ex)
else
x = copy(x)
scale!(x.coeffs, c)
x.constant *= c
x.constant += ex
x
end
end
function addtoexpr{T<:GenericQuadExpr}(ex::Number, c::Number, x::T)
# It's only safe to mutate the first argument.
if c == 0
T(ex)
else
x = copy(x)
scale!(x.qcoeffs, c)
scale!(x.aff.coeffs, c)
x.aff.constant *= c
x.aff.constant += ex
x
end
end
addtoexpr(ex::Number, c::Variable, x::Variable) = QuadExpr([c],[x],[1.0],zero(AffExpr))
function addtoexpr{T<:GenericAffExpr}(ex::Number, c::T, x::T)
q = c*x
q.aff.constant += ex
q
end
function addtoexpr{C,V}(ex::Number, c::GenericAffExpr{C,V}, x::V)
q = c*x
q.aff.constant += ex
q
end
function addtoexpr{T<:GenericQuadExpr}(ex::Number, c::T, x::Number)
if x == 0
T(ex)
else
q = c*x
q.aff.constant += ex
q
end
end
function addtoexpr(aff::GenericAffExpr, c::Number, x::Number)
aff.constant += c*x
aff
end
function addtoexpr{C,V}(aff::GenericAffExpr{C,V}, c::Number, x::V)
if c != 0
push!(aff.vars, x)
push!(aff.coeffs, c)
end
aff
end
function addtoexpr{C,V}(aff::GenericAffExpr{C,V},c::Number,x::GenericAffExpr{C,V})
if c != 0
append!(aff.vars, x.vars)
sizehint!(aff.coeffs, length(aff.coeffs)+length(x.coeffs))
for i in 1:length(x.coeffs)
push!(aff.coeffs, c*x.coeffs[i])
end
aff.constant += c*x.constant
end
aff
end
# help w/ ambiguity
addtoexpr{C,V<:Number}(aff::GenericAffExpr{C,V}, c::Number, x::Number) = aff + c*x
function addtoexpr{C,V}(aff::GenericAffExpr{C,V}, c::V, x::Number)
if x != 0
push!(aff.vars, c)
push!(aff.coeffs, x)
end
aff
end
addtoexpr{C,V}(aff::GenericAffExpr{C,V},c::V,x::V) =
GenericQuadExpr{C,V}([c],[x],[one(C)],aff)
# TODO: add generic versions of following two methods
addtoexpr(aff::AffExpr,c::AffExpr,x::Variable) =
QuadExpr(c.vars,
fill(x,length(c.vars)),
c.coeffs,
addtoexpr(aff,c.constant,x))
addtoexpr(aff::AffExpr,c::Variable,x::AffExpr) =
QuadExpr(fill(c,length(x.vars)),
x.vars,
x.coeffs,
addtoexpr(aff,c,x.constant))
function addtoexpr{C,V}(aff::GenericAffExpr{C,V},c::GenericAffExpr{C,V},x::Number)
if x != 0
append!(aff.vars, c.vars)
append!(aff.coeffs, c.coeffs * x)
aff.constant += c.constant * x
end
aff
end
function addtoexpr{C,V}(aff::GenericAffExpr{C,V}, c::GenericQuadExpr{C,V}, x::Number)
if x == 0
GenericQuadExpr{C,V}(aff)
else
GenericQuadExpr{C,V}(copy(c.qvars1),
copy(c.qvars2),
c.qcoeffs*x,
addtoexpr(aff,c.aff,x))
end
end
function addtoexpr{C,V}(aff::GenericAffExpr{C,V}, c::Number, x::GenericQuadExpr{C,V})
if c == 0
GenericQuadExpr{C,V}(aff)
else
GenericQuadExpr{C,V}(copy(x.qvars1),
copy(x.qvars2),
c*x.qcoeffs,
addtoexpr(aff,c,x.aff))
end
end
function addtoexpr{C,V}(ex::GenericAffExpr{C,V}, c::GenericAffExpr{C,V}, x::GenericAffExpr{C,V})
q = convert(GenericQuadExpr{C,V}, ex)
addtoexpr(q, one(C), c*x)
q
end
function addtoexpr{C,V}(quad::GenericQuadExpr{C,V},c::Number,x::V)
if c != 0
push!(quad.aff, convert(C,c), x)
end
quad
end
function addtoexpr(quad::GenericQuadExpr,c::Number,x::Number)
quad.aff.constant += c*x
quad
end
function addtoexpr{C,V}(quad::GenericQuadExpr{C,V},x::V,y::V)
push!(quad.qvars1, x)
push!(quad.qvars2, y)
push!(quad.qcoeffs, one(C))
quad
end
function addtoexpr{C,V}(quad::GenericQuadExpr{C,V},c::Number,x::GenericAffExpr{C,V})
if c != 0
append!(quad.aff.vars, x.vars)
sizehint!(quad.aff.coeffs, length(quad.aff.coeffs)+length(x.coeffs))
for i in 1:length(x.coeffs)
push!(quad.aff.coeffs, c*x.coeffs[i])
end
quad.aff.constant += c*x.constant
end
quad
end
function addtoexpr{C,V}(quad::GenericQuadExpr{C,V},c::GenericAffExpr{C,V},x::Number)
if x != 0
addtoexpr(quad.aff,c,x)
end
quad
end
function addtoexpr{C,V}(quad::GenericQuadExpr{C,V},c::GenericAffExpr{C,V},x::V)
append!(quad.qvars1, c.vars)
append!(quad.qvars2, fill(x,length(c.vars)))
append!(quad.qcoeffs, c.coeffs)
addtoexpr(quad.aff,c.constant,x)
quad
end
function addtoexpr{C,V}(quad::GenericQuadExpr{C,V},c::V,x::GenericAffExpr{C,V})
append!(quad.qvars1, fill(c,length(x.vars)))
append!(quad.qvars2, x.vars)
append!(quad.qcoeffs, x.coeffs)
addtoexpr(quad.aff,c,x.constant)
quad
end
function addtoexpr{C,V}(quad::GenericQuadExpr{C,V},c::GenericQuadExpr{C,V},x::Number)
if x != 0
append!(quad.qvars1,c.qvars1)
append!(quad.qvars2,c.qvars2)
sizehint!(quad.qcoeffs, length(quad.qcoeffs)+length(c.qcoeffs))
for i in 1:length(c.qcoeffs)
push!(quad.qcoeffs, c.qcoeffs[i]*x)
end
addtoexpr(quad,c.aff,x)
end
quad
end
function addtoexpr{C,V}(quad::GenericQuadExpr{C,V},c::Number,x::GenericQuadExpr{C,V})
if c != 0
append!(quad.qvars1,x.qvars1)
append!(quad.qvars2,x.qvars2)
sizehint!(quad.qcoeffs, length(quad.qcoeffs)+length(x.qcoeffs))
for i in 1:length(x.qcoeffs)
push!(quad.qcoeffs, c*x.qcoeffs[i])
end
addtoexpr(quad,c,x.aff)
end
quad
end
function addtoexpr{C,V}(ex::GenericQuadExpr{C,V}, c::GenericAffExpr{C,V}, x::GenericAffExpr{C,V})
q = c*x
addtoexpr(ex, 1.0, q)
ex
end
# Catch nonlinear expressions and parameters being used in addconstraint, etc.
const _NLExpr = Union{NonlinearExpression,NonlinearParameter}
_nlexprerr() = error("""Cannot use nonlinear expression or parameter in @constraint or @objective.
Use @NLconstraint or @NLobjective instead.""")
# Following three definitions avoid ambiguity warnings
addtoexpr{C,V<:_NLExpr}(expr::GenericQuadExpr{C,V}, c::GenericAffExpr{C,V}, x::V) = _nlexprerr()
addtoexpr{C,V<:_NLExpr}(expr::GenericQuadExpr{C,V}, c::Number, x::V) = _nlexprerr()
addtoexpr{C,V<:_NLExpr}(expr::GenericQuadExpr{C,V}, c::V, x::GenericAffExpr{C,V}) = _nlexprerr()
for T1 in (GenericAffExpr,GenericQuadExpr), T2 in (Number,Variable,GenericAffExpr,GenericQuadExpr)
@eval addtoexpr(::$T1, ::$T2, ::_NLExpr) = _nlexprerr()
@eval addtoexpr(::$T1, ::_NLExpr, ::$T2) = _nlexprerr()
end
addtoexpr{T<:GenericAffExpr}(ex::AbstractArray{T}, c::AbstractArray, x::AbstractArray) = append!.(ex, c*x)
addtoexpr{T<:GenericAffExpr}(ex::AbstractArray{T}, c::AbstractArray, x::Number) = append!.(ex, c*x)
addtoexpr{T<:GenericAffExpr}(ex::AbstractArray{T}, c::Number, x::AbstractArray) = append!.(ex, c*x)
addtoexpr(ex, c, x) = ex + c*x
@generated addtoexpr_reorder(ex, arg) = :(addtoexpr(ex, 1.0, arg))
@generated function addtoexpr_reorder(ex, x, y)
if x <: Union{Variable,AffExpr} && y <: Number
:(addtoexpr(ex, y, x))
else
:(addtoexpr(ex, x, y))
end
end
@generated function addtoexpr_reorder(ex, args...)
n = length(args)
@assert n ≥ 3
varidx = find(t -> (t == Variable || t == AffExpr), collect(args))
allscalar = all(t -> (t <: Number), args[setdiff(1:n, varidx)])
idx = (allscalar && length(varidx) == 1) ? varidx[1] : n
coef = Expr(:call, :*, [:(args[$i]) for i in setdiff(1:n,idx)]...)
:(addtoexpr(ex, $coef, args[$idx]))
end
function parseCurly(x::Expr, aff::Symbol, lcoeffs, rcoeffs, newaff=gensym())
header = x.args[1]
if length(x.args) < 3
error("Need at least two arguments for $header")
end
if issum(header)
parseSum(x, aff, lcoeffs, rcoeffs, newaff)
elseif header ∈ [:norm1, :norm2, :norminf, :norm∞]
parseNorm(header, x, aff, lcoeffs, rcoeffs, newaff)
else
error("Expected sum or norm2 outside curly braces; got $header")
end
end
function parseSum(x::Expr, aff::Symbol, lcoeffs, rcoeffs, newaff)
# we have a filter condition
if isexpr(x.args[2],:parameters)
cond = x.args[2]
if length(cond.args) != 1
error("No commas after semicolon allowed in sum expression, use && for multiple conditions")
end
# generate inner loop code first and then wrap in for loops
inneraff, innercode = parseExpr(x.args[3], aff, lcoeffs, rcoeffs, aff)
code = quote
if $(esc(cond.args[1]))
$innercode
end
end
for level in length(x.args):-1:4
_idxvar, idxset = parseIdxSet(x.args[level]::Expr)
idxvar = esc(_idxvar)
code = :(let
$(localvar(idxvar))
for $idxvar in $(esc(idxset))
$code
end
end)
end
else # no condition
inneraff, code = parseExpr(x.args[2], aff, lcoeffs, rcoeffs, aff)
for level in length(x.args):-1:3
_idxvar, idxset = parseIdxSet(x.args[level]::Expr)
idxvar = esc(_idxvar)
code = :(let
$(localvar(idxvar))
for $idxvar in $(esc(idxset))
$code
end
end)
end
len = :len
# precompute the number of elements to add
# this is unncessary if we're just summing constants
_, lastidxset = parseIdxSet(x.args[length(x.args)]::Expr)
preblock = :($len += length($(esc(lastidxset))))
for level in (length(x.args)-1):-1:3
_idxvar, idxset = parseIdxSet(x.args[level]::Expr)
idxvar = esc(_idxvar)
preblock = :(let
$(localvar(idxvar))
for $idxvar in $(esc(idxset))
$preblock
end
end)
end
preblock = quote
$len = 0
$preblock
_sizehint_expr!($aff,$len)
end
code = :($preblock;$code)
end
:($code; $newaff=$aff)
end
function parseNorm(normp::Symbol, x::Expr, aff::Symbol, lcoeffs, rcoeffs, newaff)
@assert string(x.args[1])[1:4] == "norm"
# we have a filter condition
finalaff = gensym()
gennorm = gensym()
len = gensym()
normexpr = gensym()
if isexpr(x.args[2],:parameters)
cond = x.args[2]
if length(cond.args) != 1
error("No commas after semicolon allowed in sum expression, use && for multiple conditions")
end
# generate inner loop code first and then wrap in for loops
inneraff, innercode = parseExprToplevel(x.args[3], :normaff)
code = quote
if $(esc(cond.args[1]))
normaff = 0.0
$innercode
push!($normexpr, $inneraff)
end
end
for level in length(x.args):-1:4
_idxvar, idxset = parseIdxSet(x.args[level]::Expr)
idxvar = esc(_idxvar)
code = :(let
$(localvar(idxvar))
for $idxvar in $(esc(idxset))
$code
end
end)
end
preblock = :($normexpr = GenericAffExpr[])
else # no condition
inneraff, code = parseExprToplevel(x.args[2], :normaff)
code = :(normaff = 0.0; $code; push!($normexpr, $inneraff))
_, lastidxset = parseIdxSet(x.args[length(x.args)]::Expr)
preblock = :($len += length($(esc(lastidxset))))
for level in length(x.args):-1:3
_idxvar, idxset = parseIdxSet(x.args[level]::Expr)
idxvar = esc(_idxvar)
code = :(let
$(localvar(idxvar))
for $idxvar in $(esc(idxset))
$code
end
end)
preblock = :(let
$(localvar(idxvar))
for $idxvar in $(esc(idxset))
$preblock
end
end)
end
preblock = quote
$len = 0
$preblock
$normexpr = GenericAffExpr[]
_sizehint_expr!($normexpr, $len)
end
end
if normp == :norm2
param = 2
elseif normp == :norm1
param = 1
elseif normp ∈ [:norminf, :norm∞]
param = Inf
else
error("Unrecognized norm: $normp")
end
quote
$preblock
$code
$gennorm = _build_norm($param,$normexpr)
$newaff = $(Expr(:call, :addtoexpr_reorder, aff,lcoeffs...,gennorm,rcoeffs...))
end
end
# takes a generator statement and returns a properly nested for loop
# with nested filters as specified
function parsegen(ex,atleaf)
if isexpr(ex,:flatten)
return parsegen(ex.args[1],atleaf)
end
if !isexpr(ex,:generator)
return atleaf(ex)
end
function itrsets(sets)
if isa(sets,Expr)
return sets
elseif length(sets) == 1
return sets[1]
else
return Expr(:block, sets...)
end
end
idxvars = []
if isexpr(ex.args[2], :filter) # if condition
loop = Expr(:for,esc(itrsets(ex.args[2].args[2:end])),
Expr(:if,esc(ex.args[2].args[1]),
parsegen(ex.args[1],atleaf)))
for idxset in ex.args[2].args[2:end]
idxvar, s = parseIdxSet(idxset)
push!(idxvars,idxvar)
end
else
loop = Expr(:for,esc(itrsets(ex.args[2:end])),
parsegen(ex.args[1],atleaf))
for idxset in ex.args[2:end]
idxvar, s = parseIdxSet(idxset)
push!(idxvars,idxvar)
end
end
b = Expr(:block)
for idxvar in idxvars
push!(b.args, localvar(esc(idxvar)))
end
return :(let; $b; $loop; end)
end
function parseGenerator(x::Expr, aff::Symbol, lcoeffs, rcoeffs, newaff=gensym())
@assert isexpr(x,:call)
@assert length(x.args) > 1
@assert isexpr(x.args[2],:generator) || isexpr(x.args[2],:flatten)
header = x.args[1]
if issum(header)
parseGeneratorSum(x.args[2], aff, lcoeffs, rcoeffs, newaff)
elseif header == :norm
parseGeneratorNorm(x.args[2], aff, lcoeffs, rcoeffs, newaff, length(x.args) > 2 ? x.args[3] : 2)
else
error("Expected sum or norm2 outside generator expression; got $header")
end
end
function parseGeneratorSum(x::Expr, aff::Symbol, lcoeffs, rcoeffs, newaff)
# We used to preallocate the expression at the lowest level of the loop.
# When rewriting this some benchmarks revealed that it actually doesn't
# seem to help anymore, so might as well keep the code simple.
code = parsegen(x, t -> parseExpr(t, aff, lcoeffs, rcoeffs, aff)[2])
return :($code; $newaff=$aff)
end
function parseGeneratorNorm(x::Expr, aff::Symbol, lcoeffs, rcoeffs, newaff, normp)
finalaff = gensym()
gennorm = gensym()
len = gensym()
normexpr = gensym()
function atleaf(t)
inneraff, innercode = parseExprToplevel(t, :normaff)
return Expr(:block, :(normaff = 0.0), innercode, :(push!($normexpr,$inneraff)))
end
code = parsegen(x, atleaf)
quote
$normexpr = GenericAffExpr[]
$code
$gennorm = _build_norm($(esc(normp)),$normexpr)
$newaff = $(Expr(:call, :addtoexpr_reorder, aff,lcoeffs...,gennorm,rcoeffs...))
end
end
is_complex_expr(ex) = isa(ex,Expr) && !isexpr(ex,:ref)
parseExprToplevel(x, aff::Symbol) = parseExpr(x, aff, [], [])
# output is assigned to newaff
function parseExpr(x, aff::Symbol, lcoeffs::Vector, rcoeffs::Vector, newaff::Symbol=gensym())
if !isa(x,Expr)
# at the lowest level
callexpr = Expr(:call,:addtoexpr_reorder,aff,lcoeffs...,esc(x),rcoeffs...)
return newaff, :($newaff = $callexpr)
else
if x.head == :call && x.args[1] == :+
b = Expr(:block)
aff_ = aff
for arg in x.args[2:(end-1)]
aff_, code = parseExpr(arg, aff_, lcoeffs, rcoeffs)
push!(b.args, code)
end
newaff, code = parseExpr(x.args[end], aff_, lcoeffs, rcoeffs, newaff)
push!(b.args, code)
return newaff, b
elseif x.head == :call && x.args[1] == :-
if length(x.args) == 2 # unary subtraction
return parseExpr(x.args[2], aff, vcat(-1.0, lcoeffs), rcoeffs, newaff)
else # a - b - c ...
b = Expr(:block)
aff_, code = parseExpr(x.args[2], aff, lcoeffs, rcoeffs)
push!(b.args, code)
for arg in x.args[3:(end-1)]
aff_,code = parseExpr(arg, aff_, vcat(-1.0, lcoeffs), rcoeffs)
push!(b.args, code)
end
newaff,code = parseExpr(x.args[end], aff_, vcat(-1.0, lcoeffs), rcoeffs, newaff)
push!(b.args, code)
return newaff, b
end
elseif x.head == :call && x.args[1] == :*
# we might need to recurse on multiple arguments, e.g.,
# (x+y)*(x+y)
n_expr = mapreduce(is_complex_expr, +, x.args)
if n_expr == 1 # special case, only recurse on one argument and don't create temporary objects
which_idx = 0
for i in 2:length(x.args)
if is_complex_expr(x.args[i])
which_idx = i
end
end
return parseExpr(x.args[which_idx], aff, vcat(lcoeffs, [esc(x.args[i]) for i in 2:(which_idx-1)]),
vcat(rcoeffs, [esc(x.args[i]) for i in (which_idx+1):length(x.args)]),
newaff)
else
blk = Expr(:block)
for i in 2:length(x.args)
if is_complex_expr(x.args[i])
s = gensym()
newaff_, parsed = parseExprToplevel(x.args[i], s)
push!(blk.args, :($s = 0.0; $parsed))
x.args[i] = newaff_
else
x.args[i] = esc(x.args[i])
end
end
callexpr = Expr(:call,:addtoexpr_reorder,aff,lcoeffs...,x.args[2:end]...,rcoeffs...)
push!(blk.args, :($newaff = $callexpr))
return newaff, blk
end
elseif x.head == :call && x.args[1] == :^ && is_complex_expr(x.args[2])
if x.args[3] == 2
blk = Expr(:block)
s = gensym()
newaff_, parsed = parseExprToplevel(x.args[2], s)
push!(blk.args, :($s = 0.0; $parsed))
push!(blk.args, :($newaff = $aff + $(Expr(:call,:*,lcoeffs...,newaff_,newaff_,rcoeffs...))))
return newaff, blk
elseif x.args[3] == 1
return parseExpr(x.args[2], aff, lcoeffs, rcoeffs)
elseif x.args[3] == 0
return parseExpr(1, aff, lcoeffs, rcoeffs)
else
blk = Expr(:block)
s = gensym()
newaff_, parsed = parseExprToplevel(x.args[2], s)
push!(blk.args, :($s = 0.0; $parsed))
push!(blk.args, :($newaff = $aff + $(Expr(:call,:*,lcoeffs...,Expr(:call,:^,newaff_,esc(x.args[3])),rcoeffs...))))
return newaff, blk
end
elseif x.head == :call && x.args[1] == :/
@assert length(x.args) == 3
numerator = x.args[2]
denom = x.args[3]
return parseExpr(numerator, aff, lcoeffs,vcat(esc(:(1/$denom)),rcoeffs),newaff)
elseif isexpr(x,:call) && length(x.args) >= 2 && (isexpr(x.args[2],:generator) || isexpr(x.args[2],:flatten))
return newaff, parseGenerator(x,aff,lcoeffs,rcoeffs,newaff)
elseif x.head == :curly
warn_curly(x)
return newaff, parseCurly(x,aff,lcoeffs,rcoeffs,newaff)
else # at lowest level?
!isexpr(x,:comparison) || error("Unexpected comparison in expression $x")
callexpr = Expr(:call,:addtoexpr_reorder,aff,lcoeffs...,esc(x),rcoeffs...)
return newaff, :($newaff = $callexpr)
end
end
end
# Semi-automatically generated precompilation hints
include("precompile.jl")