@@ -149,7 +149,7 @@ function _extend_unary_operator(
149149 f_inside:: Symbol , f_outside:: Symbol , type_requirements, internal
150150)
151151 quote
152- @gensym _constructorof _AbstractExpressionNode
152+ @gensym _constructorof _AbstractExpressionNode _latest_op_idx _T _N _l
153153 quote
154154 if $$ internal
155155 import .. NodeModule. constructorof as $ _constructorof
@@ -161,13 +161,13 @@ function _extend_unary_operator(
161161 end
162162
163163 function $ ($ f_outside)(
164- l :: N
165- ) where {T <: $ ($ type_requirements),N <: $_AbstractExpressionNode{T } }
166- return if (l . degree == 0 && l . constant)
167- $ _constructorof (N)(T ; val= $ ($ f_inside)(l . val))
164+ $ _l :: $_N
165+ ) where {$ _T <: $ ($ type_requirements),$ _N <: $_AbstractExpressionNode{$_T } }
166+ return if ($ _l . degree == 0 && $ _l . constant)
167+ $ _constructorof ($ _N)( $ _T ; val= $ ($ f_inside)($ _l . val))
168168 else
169- latest_op_idx = $ ($ lookup_op)($ ($ f_inside), Val (1 ))
170- $ _constructorof (N )(; op= latest_op_idx , children= (l ,))
169+ $ _latest_op_idx = $ ($ lookup_op)($ ($ f_inside), Val (1 ))
170+ $ _constructorof ($ _N )(; op= $ _latest_op_idx , children= ($ _l ,))
171171 end
172172 end
173173 end
@@ -178,7 +178,7 @@ function _extend_binary_operator(
178178 f_inside:: Symbol , f_outside:: Symbol , type_requirements, build_converters, internal
179179)
180180 quote
181- @gensym _constructorof _AbstractExpressionNode
181+ @gensym _constructorof _AbstractExpressionNode _latest_op_idx _T _T1 _T2 _N _l _r
182182 quote
183183 if $$ internal
184184 import .. NodeModule. constructorof as $ _constructorof
@@ -190,62 +190,64 @@ function _extend_binary_operator(
190190 end
191191
192192 function $ ($ f_outside)(
193- l :: N , r :: N
194- ) where {T <: $ ($ type_requirements),N <: $_AbstractExpressionNode{T } }
195- if (l . degree == 0 && l . constant && r . degree == 0 && r . constant)
196- $ _constructorof (N)(T ; val= $ ($ f_inside)(l . val, r . val))
193+ $ _l :: $_N , $ _r :: $_N
194+ ) where {$ _T <: $ ($ type_requirements),$ _N <: $_AbstractExpressionNode{$_T } }
195+ if ($ _l . degree == 0 && $ _l . constant && $ _r . degree == 0 && $ _r . constant)
196+ $ _constructorof ($ _N)( $ _T ; val= $ ($ f_inside)($ _l . val, $ _r . val))
197197 else
198- latest_op_idx = $ ($ lookup_op)($ ($ f_inside), Val (2 ))
199- $ _constructorof (N )(; op= latest_op_idx , children= (l, r ))
198+ $ _latest_op_idx = $ ($ lookup_op)($ ($ f_inside), Val (2 ))
199+ $ _constructorof ($ _N )(; op= $ _latest_op_idx , children= ($ _l, $ _r ))
200200 end
201201 end
202202 function $ ($ f_outside)(
203- l :: N , r :: T
204- ) where {T <: $ ($ type_requirements),N <: $_AbstractExpressionNode{T } }
205- if l . degree == 0 && l . constant
206- $ _constructorof (N)(T ; val= $ ($ f_inside)(l . val, r ))
203+ $ _l :: $_N , $ _r :: $_T
204+ ) where {$ _T <: $ ($ type_requirements),$ _N <: $_AbstractExpressionNode{$_T } }
205+ if $ _l . degree == 0 && $ _l . constant
206+ $ _constructorof ($ _N)( $ _T ; val= $ ($ f_inside)($ _l . val, $ _r ))
207207 else
208- latest_op_idx = $ ($ lookup_op)($ ($ f_inside), Val (2 ))
209- $ _constructorof (N)(;
210- op= latest_op_idx, children= (l, $ _constructorof (N)(T; val= r))
208+ $ _latest_op_idx = $ ($ lookup_op)($ ($ f_inside), Val (2 ))
209+ $ _constructorof ($ _N)(;
210+ op= $ _latest_op_idx,
211+ children= ($ _l, $ _constructorof ($ _N)($ _T; val= $ _r)),
211212 )
212213 end
213214 end
214215 function $ ($ f_outside)(
215- l :: T , r :: N
216- ) where {T <: $ ($ type_requirements),N <: $_AbstractExpressionNode{T } }
217- if r . degree == 0 && r . constant
218- $ _constructorof (N)(T ; val= $ ($ f_inside)(l, r . val))
216+ $ _l :: $_T , $ _r :: $_N
217+ ) where {$ _T <: $ ($ type_requirements),$ _N <: $_AbstractExpressionNode{$_T } }
218+ if $ _r . degree == 0 && $ _r . constant
219+ $ _constructorof ($ _N)( $ _T ; val= $ ($ f_inside)($ _l, $ _r . val))
219220 else
220- latest_op_idx = $ ($ lookup_op)($ ($ f_inside), Val (2 ))
221- $ _constructorof (N)(;
222- op= latest_op_idx, children= ($ _constructorof (N)(T; val= l), r)
221+ $ _latest_op_idx = $ ($ lookup_op)($ ($ f_inside), Val (2 ))
222+ $ _constructorof ($ _N)(;
223+ op= $ _latest_op_idx,
224+ children= ($ _constructorof ($ _N)($ _T; val= $ _l), $ _r),
223225 )
224226 end
225227 end
226228 if $ ($ build_converters)
227229 # Converters:
228230 function $ ($ f_outside)(
229- l :: $_AbstractExpressionNode{T1 } , r :: $_AbstractExpressionNode{T2 }
230- ) where {T1 <: $ ($ type_requirements),T2 <: $ ($ type_requirements)}
231- if l isa GraphNode || r isa GraphNode
231+ $ _l :: $_AbstractExpressionNode{$_T1 } , $ _r :: $_AbstractExpressionNode{$_T2 }
232+ ) where {$ _T1 <: $ ($ type_requirements),$ _T2 <: $ ($ type_requirements)}
233+ if $ _l isa $ ( GraphNode) || $ _r isa $ ( GraphNode)
232234 error (
233235 " Refusing to promote `GraphNode` as it would break the graph structure. " *
234236 " Please convert to a common type first." ,
235237 )
236238 end
237- return $ ($ f_outside)(promote (l, r )... )
239+ return $ ($ f_outside)(promote ($ _l, $ _r )... )
238240 end
239241
240242 function $ ($ f_outside)(
241- l :: $_AbstractExpressionNode{T1 } , r :: T2
242- ) where {T1 <: $ ($ type_requirements),T2 <: $ ($ type_requirements)}
243- return $ ($ f_outside)(l , convert (T1, r ))
243+ $ _l :: $_AbstractExpressionNode{$_T1 } , $ _r :: $_T2
244+ ) where {$ _T1 <: $ ($ type_requirements),$ _T2 <: $ ($ type_requirements)}
245+ return $ ($ f_outside)($ _l , convert ($ _T1, $ _r ))
244246 end
245247 function $ ($ f_outside)(
246- l :: T1 , r :: $_AbstractExpressionNode{T2 }
247- ) where {T1 <: $ ($ type_requirements),T2 <: $ ($ type_requirements)}
248- return $ ($ f_outside)(convert (T2, l ), r )
248+ $ _l :: $_T1 , $ _r :: $_AbstractExpressionNode{$_T2 }
249+ ) where {$ _T1 <: $ ($ type_requirements),$ _T2 <: $ ($ type_requirements)}
250+ return $ ($ f_outside)(convert ($ _T2, $ _l ), $ _r )
249251 end
250252 end
251253 end
@@ -256,9 +258,9 @@ function _extend_nary_operator(
256258 degree:: Symbol , f_inside:: Symbol , f_outside:: Symbol , type_requirements, internal
257259)
258260 quote
259- @gensym _constructorof _AbstractExpressionNode
261+ @gensym _constructorof _AbstractExpressionNode _is_chainable _latest_op_idx _T _N
260262 arg_syms = [$ (Symbol)(" arg" , i) for i in 1 : ($ (degree))]
261- args = [Expr (:(:: ), arg_syms[i], :N ) for i in 1 : ($ (degree))]
263+ args = [Expr (:(:: ), arg_syms[i], _N ) for i in 1 : ($ (degree))]
262264 quote
263265 if $$ internal
264266 import .. NodeModule. constructorof as $ _constructorof
@@ -271,24 +273,26 @@ function _extend_nary_operator(
271273
272274 function $ ($ f_outside)(
273275 $ (args... )
274- ) where {T <: $ ($ type_requirements),N <: $_AbstractExpressionNode{T } }
276+ ) where {$ _T <: $ ($ type_requirements),$ _N <: $_AbstractExpressionNode{$_T } }
275277 # Standard n-ary operator behavior
276278 if all (c -> c. degree == 0 && c. constant, ($ (arg_syms... ),))
277- $ _constructorof (N )(
278- T ; val= $ ($ f_inside)(map (c -> c. val, ($ (arg_syms... ),))... )
279+ $ _constructorof ($ _N )(
280+ $ _T ; val= $ ($ f_inside)(map (c -> c. val, ($ (arg_syms... ),))... )
279281 )
280282 else
281- __is_chainable = $$ f_inside ∈ (+ , * )
282- latest_op_idx = $$ lookup_op (
283- $$ f_inside, Val ($$ degree); allow_failure= __is_chainable
283+ $ _is_chainable = $$ f_inside ∈ (+ , * )
284+ $ _latest_op_idx = $$ lookup_op (
285+ $$ f_inside, Val ($$ degree); allow_failure= $ _is_chainable
284286 )
285- if __is_chainable && iszero (latest_op_idx )
287+ if $ _is_chainable && iszero ($ _latest_op_idx )
286288 # If this method exists, and then the operator is removed from the mapping,
287289 # Julia will chain the calls (like `+(x1, x2, x3)`, even though `+` is only given as a binary operator).
288290 # Therefore, we need to manually chain the calls in such instances.
289291 $$ foldl ($$ f_outside, ($ (arg_syms... ),))
290292 else
291- $ _constructorof (N)(; op= latest_op_idx, children= ($ (arg_syms... ),))
293+ $ _constructorof ($ _N)(;
294+ op= $ _latest_op_idx, children= ($ (arg_syms... ),)
295+ )
292296 end
293297 end
294298 end
0 commit comments