Skip to content

Commit 422b5c5

Browse files
committed
feat: parsing for D-degree nodes
1 parent 69cd372 commit 422b5c5

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

src/Parse.jl

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -282,50 +282,46 @@ end
282282
evaluate_on::Union{Nothing,AbstractVector};
283283
kws...,
284284
)::N where {F<:Function,N<:AbstractExpressionNode,E<:AbstractExpression}
285-
if length(args) == 2 && func operators.unaops
286-
# Regular unary operator
287-
op = findfirst(==(func), operators.unaops)::Int
285+
degree = length(args) - 1
286+
if degree <= length(operators.ops) && func operators[degree]
287+
op_idx = findfirst(==(func), operators[degree])
288288
return N(;
289-
op=op::Int,
290-
l=_parse_expression(
291-
args[2], operators, variable_names, N, E, evaluate_on; kws...
292-
),
293-
)
294-
elseif length(args) == 3 && func operators.binops
295-
# Regular binary operator
296-
op = findfirst(==(func), operators.binops)::Int
297-
return N(;
298-
op=op::Int,
299-
l=_parse_expression(
300-
args[2], operators, variable_names, N, E, evaluate_on; kws...
301-
),
302-
r=_parse_expression(
303-
args[3], operators, variable_names, N, E, evaluate_on; kws...
289+
op=op_idx::Int,
290+
children=map(
291+
arg -> _parse_expression(
292+
arg, operators, variable_names, N, E, evaluate_on; kws...
293+
),
294+
(args[2:end]...,),
304295
),
305296
)
306-
elseif length(args) > 3 && func in (+, -, *) && func operators.binops
307-
# Either + or - but used with more than two arguments
308-
op = findfirst(==(func), operators.binops)::Int
297+
elseif degree > 2 && func (+, -, *) && func operators[2]
298+
op_idx = findfirst(==(func), operators[2])::Int
309299
inner = N(;
310-
op=op::Int,
311-
l=_parse_expression(
312-
args[2], operators, variable_names, N, E, evaluate_on; kws...
313-
),
314-
r=_parse_expression(
315-
args[3], operators, variable_names, N, E, evaluate_on; kws...
300+
op=op_idx::Int,
301+
children=(
302+
_parse_expression(
303+
args[2], operators, variable_names, N, E, evaluate_on; kws...
304+
),
305+
_parse_expression(
306+
args[3], operators, variable_names, N, E, evaluate_on; kws...
307+
),
316308
),
317309
)
318310
for arg in args[4:end]
319311
inner = N(;
320-
op=op::Int,
321-
l=inner,
322-
r=_parse_expression(
323-
arg, operators, variable_names, N, E, evaluate_on; kws...
312+
op=op_idx::Int,
313+
children=(
314+
inner,
315+
_parse_expression(
316+
arg, operators, variable_names, N, E, evaluate_on; kws...
317+
),
324318
),
325319
)
326320
end
327321
return inner
328-
elseif evaluate_on !== nothing && func in evaluate_on
322+
end
323+
324+
if evaluate_on !== nothing && func in evaluate_on
329325
# External function
330326
func(
331327
map(
@@ -337,10 +333,8 @@ end
337333
)
338334
else
339335
matching_s = let
340-
s = if length(args) == 2
341-
"`" * string(operators.unaops) * "`"
342-
elseif length(args) == 3
343-
"`" * string(operators.binops) * "`"
336+
s = if degree <= length(operators.ops)
337+
join(('`', operators[degree], '`'))
344338
else
345339
""
346340
end

src/base.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ By using this instead of tree_mapreduce, we can take advantage of early exits.
177177
@generated function any(f::F, tree::AbstractNode{D}) where {F<:Function,D}
178178
quote
179179
deg = tree.degree
180-
181180
deg == 0 && return @inline(f(tree))
182-
183181
return (
184182
@inline(f(tree)) || Base.Cartesian.@nif(
185183
$D, i -> deg == i, i -> let cs = get_children(tree, Val(i))

0 commit comments

Comments
 (0)