-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.jl
More file actions
330 lines (307 loc) · 9.77 KB
/
Copy pathparse.jl
File metadata and controls
330 lines (307 loc) · 9.77 KB
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
# Largely inspired by MathOptInterface/src/Nonlinear/parse.jl
# Most functions have been copy-pasted and slightly modified to adapt to small changes in OperatorRegistry and Model.
function _parse_multivariate_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
parent_index::Int,
)
@assert Meta.isexpr(x, :call)
broadcasted = false
# if first char of x is a dot, then it is broadcasted and we should look up the operator without the dot
if x.args[1] isa Symbol && startswith(string(x.args[1]), ".")
x = Expr(:call, Symbol(string(x.args[1])[2:end]), x.args[2:end]...)
broadcasted = true
end
id = get(data.operators.multivariate_operator_to_id, x.args[1], nothing)
if id === nothing
if haskey(data.operators.univariate_operator_to_id, x.args[1])
# It may also be a unary variate operator with splatting.
_parse_univariate_expression(stack, data, expr, x, parent_index)
elseif x.args[1] in data.operators.comparison_operators
# Or it may be a binary (in)equality operator.
_parse_inequality_expression(stack, data, expr, x, parent_index)
else
throw(MOI.UnsupportedNonlinearOperator(x.args[1]))
end
return
end
if broadcasted
push!(
expr.nodes,
Node(NODE_CALL_MULTIVARIATE_BROADCASTED, id, parent_index),
)
else
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id, parent_index))
end
for i in length(x.args):-1:2
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end
function parse_expression(
::Model,
expr::Expression,
x::MOI.VariableIndex,
parent_index::Int,
)
push!(expr.nodes, Node(NODE_MOI_VARIABLE, x.value, parent_index))
return
end
function parse_expression(data::Model, input)
expr = Expression()
parse_expression(data, expr, input, -1)
return expr
end
function parse_expression(::Model, expr::Expression, x::Real, parent_index::Int)
push!(expr.values, convert(Float64, x)::Float64)
push!(expr.nodes, Node(NODE_VALUE, length(expr.values), parent_index))
return
end
function parse_expression(
::Model,
expr::Expression,
x::ParameterIndex,
parent_index::Int,
)
push!(expr.nodes, Node(NODE_PARAMETER, x.value, parent_index))
return
end
function parse_expression(
data::Model,
expr::Expression,
x::Expr,
parent_index::Int,
)
stack = Tuple{Int,Any}[]
push!(stack, (parent_index, x))
while !isempty(stack)
parent, item = pop!(stack)
if item isa Expr
_parse_expression(stack, data, expr, item, parent)
else
parse_expression(data, expr, item, parent)
end
end
return
end
function parse_expression(
::Model,
expr::Expression,
x::ExpressionIndex,
parent_index::Int,
)
push!(expr.nodes, Node(NODE_SUBEXPRESSION, x.value, parent_index))
return
end
function _parse_univariate_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
parent_index::Int,
)
@assert Meta.isexpr(x, :call, 2) || Meta.isexpr(x, :., 2)
broadcasted = false
if Meta.isexpr(x, :.)
broadcasted = true
end
# if first char of x is a dot, then it is broadcasted and we should look up the operator without the dot
if x.args[1] isa Symbol && startswith(string(x.args[1]), ".")
x = Expr(:call, Symbol(string(x.args[1])[2:end]), x.args[2:end]...)
broadcasted = true
end
id = get(data.operators.univariate_operator_to_id, x.args[1], nothing)
if id === nothing
# It may also be a multivariate operator like * with one argument.
if haskey(data.operators.multivariate_operator_to_id, x.args[1])
_parse_multivariate_expression(stack, data, expr, x, parent_index)
return
end
throw(MOI.UnsupportedNonlinearOperator(x.args[1]))
end
if broadcasted
push!(
expr.nodes,
Node(NODE_CALL_UNIVARIATE_BROADCASTED, id, parent_index),
)
else
push!(expr.nodes, Node(NODE_CALL_UNIVARIATE, id, parent_index))
end
push!(stack, (length(expr.nodes), x.args[2]))
return
end
function _parse_logic_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
parent_index::Int,
)
id = data.operators.logic_operator_to_id[x.head]
push!(expr.nodes, Node(NODE_LOGIC, id, parent_index))
parent_var = length(expr.nodes)
push!(stack, (parent_var, x.args[2]))
push!(stack, (parent_var, x.args[1]))
return
end
function _parse_expression(stack, data, expr, x, parent_index)
if Meta.isexpr(x, :call)
if x.args[1] == :reduce
_parse_reduce_expression(stack, data, expr, x, parent_index)
elseif length(x.args) == 2 && !Meta.isexpr(x.args[2], :...)
_parse_univariate_expression(stack, data, expr, x, parent_index)
else
# The call is either n-ary, or it is a splat, in which case we
# cannot tell just yet whether the expression is unary or nary.
# Punt to multivariate and try to recover later.
_parse_multivariate_expression(stack, data, expr, x, parent_index)
end
elseif Meta.isexpr(x, :.)
# This is a special case for handling univariate broadcasted operators
_parse_univariate_expression(stack, data, expr, x, parent_index)
elseif Meta.isexpr(x, :comparison)
_parse_comparison_expression(stack, data, expr, x, parent_index)
elseif Meta.isexpr(x, :...)
MOI.Nonlinear._parse_splat_expression(
stack,
data,
expr,
x,
parent_index,
)
elseif Meta.isexpr(x, :&&) || Meta.isexpr(x, :||)
_parse_logic_expression(stack, data, expr, x, parent_index)
elseif Meta.isexpr(x, :vect)
_parse_vect_expression(stack, data, expr, x, parent_index)
elseif Meta.isexpr(x, :hcat)
_parse_hcat_expression(stack, data, expr, x, parent_index)
elseif Meta.isexpr(x, :vcat)
_parse_vcat_expression(stack, data, expr, x, parent_index)
elseif Meta.isexpr(x, :row)
_parse_row_expression(stack, data, expr, x, parent_index)
elseif Meta.isexpr(x, :tuple) && length(x.args) == 1
push!(stack, (parent_index, x.args[1]))
else
error("Unsupported expression: $x")
end
end
function _parse_comparison_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
parent_index::Int,
)
for k in 2:2:(length(x.args)-1)
@assert x.args[k] == x.args[2] # don't handle a <= b >= c
end
operator_id = data.operators.comparison_operator_to_id[x.args[2]]
push!(expr.nodes, Node(NODE_COMPARISON, operator_id, parent_index))
for i in length(x.args):-2:1
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end
function _parse_vect_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
parent_index::Int,
)
@assert Meta.isexpr(x, :vect)
id = get(data.operators.multivariate_operator_to_id, :vect, nothing)
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id, parent_index))
for i in length(x.args):-1:1
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end
function _parse_row_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
parent_index::Int,
)
@assert Meta.isexpr(x, :row)
id = get(data.operators.multivariate_operator_to_id, :row, nothing)
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id, parent_index))
for i in length(x.args):-1:1
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end
function _parse_hcat_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
parent_index::Int,
)
@assert Meta.isexpr(x, :hcat)
id = get(data.operators.multivariate_operator_to_id, :hcat, nothing)
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id, parent_index))
for i in length(x.args):-1:1
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end
function _parse_vcat_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
parent_index::Int,
)
@assert Meta.isexpr(x, :vcat)
id = get(data.operators.multivariate_operator_to_id, :vcat, nothing)
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id, parent_index))
for i in length(x.args):-1:1
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end
function _parse_reduce_expression(stack, data, expr, x, parent_index)
if length(x.args) != 3
error(
"Unsupported reduce expression: $x. Expected reduce(op, collection).",
)
end
op = x.args[2]
collection = x.args[3]
if !Meta.isexpr(collection, :vect)
error(
"Unsupported reduce collection: $collection. Expected a vector literal.",
)
end
args = collection.args
if isempty(args)
error("Unsupported reduce on empty collection.")
elseif length(args) == 1
push!(stack, (parent_index, args[1]))
return
end
folded = Expr(:call, op, args[1], args[2])
for i in 3:length(args)
folded = Expr(:call, op, folded, args[i])
end
push!(stack, (parent_index, folded))
return
end
function _parse_inequality_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
parent_index::Int,
)
operator_id = data.operators.comparison_operator_to_id[x.args[1]]
push!(expr.nodes, Node(NODE_COMPARISON, operator_id, parent_index))
for i in length(x.args):-1:2
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end