Skip to content

Commit f1d3a83

Browse files
committed
style: clean up macro generation in operator construction
1 parent 11f4465 commit f1d3a83

File tree

1 file changed

+23
-50
lines changed

1 file changed

+23
-50
lines changed

src/OperatorEnumConstruction.jl

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -149,25 +149,16 @@ function _extend_unary_operator(
149149
f_inside::Symbol, f_outside::Symbol, type_requirements, internal
150150
)
151151
quote
152-
@gensym _constructorof _AbstractExpressionNode _latest_op_idx _T _N _l
152+
@gensym _latest_op_idx _T _N _l
153153
quote
154-
if $$internal
155-
import ..NodeModule.constructorof as $_constructorof
156-
import ..NodeModule.AbstractExpressionNode as $_AbstractExpressionNode
157-
else
158-
using DynamicExpressions:
159-
constructorof as $_constructorof,
160-
AbstractExpressionNode as $_AbstractExpressionNode
161-
end
162-
163154
function $($f_outside)(
164155
$_l::$_N
165-
) where {$_T<:$($type_requirements),$_N<:$_AbstractExpressionNode{$_T}}
156+
) where {$_T<:$($type_requirements),$_N<:$$AbstractExpressionNode{$_T}}
166157
return if ($_l.degree == 0 && $_l.constant)
167-
$_constructorof($_N)($_T; val=$($f_inside)($_l.val))
158+
$$constructorof($_N)($_T; val=$($f_inside)($_l.val))
168159
else
169160
$_latest_op_idx = $($lookup_op)($($f_inside), Val(1))
170-
$_constructorof($_N)(; op=$_latest_op_idx, children=($_l,))
161+
$$constructorof($_N)(; op=$_latest_op_idx, children=($_l,))
171162
end
172163
end
173164
end
@@ -178,57 +169,48 @@ function _extend_binary_operator(
178169
f_inside::Symbol, f_outside::Symbol, type_requirements, build_converters, internal
179170
)
180171
quote
181-
@gensym _constructorof _AbstractExpressionNode _latest_op_idx _T _T1 _T2 _N _l _r
172+
@gensym _latest_op_idx _T _T1 _T2 _N _l _r
182173
quote
183-
if $$internal
184-
import ..NodeModule.constructorof as $_constructorof
185-
import ..NodeModule.AbstractExpressionNode as $_AbstractExpressionNode
186-
else
187-
using DynamicExpressions:
188-
constructorof as $_constructorof,
189-
AbstractExpressionNode as $_AbstractExpressionNode
190-
end
191-
192174
function $($f_outside)(
193175
$_l::$_N, $_r::$_N
194-
) where {$_T<:$($type_requirements),$_N<:$_AbstractExpressionNode{$_T}}
176+
) where {$_T<:$($type_requirements),$_N<:$$AbstractExpressionNode{$_T}}
195177
if ($_l.degree == 0 && $_l.constant && $_r.degree == 0 && $_r.constant)
196-
$_constructorof($_N)($_T; val=$($f_inside)($_l.val, $_r.val))
178+
$$constructorof($_N)($_T; val=$($f_inside)($_l.val, $_r.val))
197179
else
198180
$_latest_op_idx = $($lookup_op)($($f_inside), Val(2))
199-
$_constructorof($_N)(; op=$_latest_op_idx, children=($_l, $_r))
181+
$$constructorof($_N)(; op=$_latest_op_idx, children=($_l, $_r))
200182
end
201183
end
202184
function $($f_outside)(
203185
$_l::$_N, $_r::$_T
204-
) where {$_T<:$($type_requirements),$_N<:$_AbstractExpressionNode{$_T}}
186+
) where {$_T<:$($type_requirements),$_N<:$$AbstractExpressionNode{$_T}}
205187
if $_l.degree == 0 && $_l.constant
206-
$_constructorof($_N)($_T; val=$($f_inside)($_l.val, $_r))
188+
$$constructorof($_N)($_T; val=$($f_inside)($_l.val, $_r))
207189
else
208190
$_latest_op_idx = $($lookup_op)($($f_inside), Val(2))
209-
$_constructorof($_N)(;
191+
$$constructorof($_N)(;
210192
op=$_latest_op_idx,
211-
children=($_l, $_constructorof($_N)($_T; val=$_r)),
193+
children=($_l, $$constructorof($_N)($_T; val=$_r)),
212194
)
213195
end
214196
end
215197
function $($f_outside)(
216198
$_l::$_T, $_r::$_N
217-
) where {$_T<:$($type_requirements),$_N<:$_AbstractExpressionNode{$_T}}
199+
) where {$_T<:$($type_requirements),$_N<:$$AbstractExpressionNode{$_T}}
218200
if $_r.degree == 0 && $_r.constant
219-
$_constructorof($_N)($_T; val=$($f_inside)($_l, $_r.val))
201+
$$constructorof($_N)($_T; val=$($f_inside)($_l, $_r.val))
220202
else
221203
$_latest_op_idx = $($lookup_op)($($f_inside), Val(2))
222-
$_constructorof($_N)(;
204+
$$constructorof($_N)(;
223205
op=$_latest_op_idx,
224-
children=($_constructorof($_N)($_T; val=$_l), $_r),
206+
children=($$constructorof($_N)($_T; val=$_l), $_r),
225207
)
226208
end
227209
end
228210
if $($build_converters)
229211
# Converters:
230212
function $($f_outside)(
231-
$_l::$_AbstractExpressionNode{$_T1}, $_r::$_AbstractExpressionNode{$_T2}
213+
$_l::$$AbstractExpressionNode{$_T1}, $_r::$$AbstractExpressionNode{$_T2}
232214
) where {$_T1<:$($type_requirements),$_T2<:$($type_requirements)}
233215
if $_l isa $(GraphNode) || $_r isa $(GraphNode)
234216
error(
@@ -240,12 +222,12 @@ function _extend_binary_operator(
240222
end
241223

242224
function $($f_outside)(
243-
$_l::$_AbstractExpressionNode{$_T1}, $_r::$_T2
225+
$_l::$$AbstractExpressionNode{$_T1}, $_r::$_T2
244226
) where {$_T1<:$($type_requirements),$_T2<:$($type_requirements)}
245227
return $($f_outside)($_l, convert($_T1, $_r))
246228
end
247229
function $($f_outside)(
248-
$_l::$_T1, $_r::$_AbstractExpressionNode{$_T2}
230+
$_l::$_T1, $_r::$$AbstractExpressionNode{$_T2}
249231
) where {$_T1<:$($type_requirements),$_T2<:$($type_requirements)}
250232
return $($f_outside)(convert($_T2, $_l), $_r)
251233
end
@@ -258,25 +240,16 @@ function _extend_nary_operator(
258240
degree::Symbol, f_inside::Symbol, f_outside::Symbol, type_requirements, internal
259241
)
260242
quote
261-
@gensym _constructorof _AbstractExpressionNode _is_chainable _latest_op_idx _T _N
243+
@gensym _is_chainable _latest_op_idx _T _N
262244
arg_syms = [$(Symbol)("arg", i) for i in 1:($(degree))]
263245
args = [Expr(:(::), arg_syms[i], _N) for i in 1:($(degree))]
264246
quote
265-
if $$internal
266-
import ..NodeModule.constructorof as $_constructorof
267-
import ..NodeModule.AbstractExpressionNode as $_AbstractExpressionNode
268-
else
269-
using DynamicExpressions:
270-
constructorof as $_constructorof,
271-
AbstractExpressionNode as $_AbstractExpressionNode
272-
end
273-
274247
function $($f_outside)(
275248
$(args...)
276-
) where {$_T<:$($type_requirements),$_N<:$_AbstractExpressionNode{$_T}}
249+
) where {$_T<:$($type_requirements),$_N<:$$AbstractExpressionNode{$_T}}
277250
# Standard n-ary operator behavior
278251
if all(c -> c.degree == 0 && c.constant, ($(arg_syms...),))
279-
$_constructorof($_N)(
252+
$$constructorof($_N)(
280253
$_T; val=$($f_inside)(map(c -> c.val, ($(arg_syms...),))...)
281254
)
282255
else
@@ -290,7 +263,7 @@ function _extend_nary_operator(
290263
# Therefore, we need to manually chain the calls in such instances.
291264
$$foldl($$f_outside, ($(arg_syms...),))
292265
else
293-
$_constructorof($_N)(;
266+
$$constructorof($_N)(;
294267
op=$_latest_op_idx, children=($(arg_syms...),)
295268
)
296269
end

0 commit comments

Comments
 (0)