Skip to content

Commit c2e2696

Browse files
committed
added defslot on operations with multiple arguments
1 parent 7b15102 commit c2e2696

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/matchers.jl

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function matcher(val::Any)
1111
if iscall(val)
1212
# if has two arguments and one of them is a DefSlot, create a term matcher with defslot
1313
# just two arguments bc defslot is only supported with operations with two args: *, ^, +
14-
if length(arguments(val)) == 2 && any(x -> isa(x, DefSlot), arguments(val))
14+
if any(x -> isa(x, DefSlot), arguments(val))
1515
return defslot_term_matcher_constructor(val)
1616
end
1717
# else return a normal term matcher
@@ -188,13 +188,6 @@ function term_matcher_constructor(term)
188188
return term_matcher_pow
189189
# if the operation is commutative
190190
elseif operation(term) in [+, *]
191-
all_matchers = []
192-
args = arguments(term)
193-
for inds in permutations(eachindex(args), length(args))
194-
reord = @views args[inds]
195-
push!(all_matchers, (matcher(operation(term)), map(matcher, reord)...,))
196-
end
197-
198191
function term_matcher_comm(success, data, bindings)
199192
!islist(data) && return nothing # if data is not a list, return nothing
200193
!iscall(car(data)) && return nothing # if first element is not a call, return nothing
@@ -223,10 +216,17 @@ end
223216
# (~x + ...complicated pattern...) * ~!y
224217
# normal part (can bee a tree) operation defslot part
225218
function defslot_term_matcher_constructor(term)
226-
a = arguments(term) # lenght two bc defslot term matcher is allowed only with +,* and ^ that accept two arguments
219+
a = arguments(term)
227220
defslot_index = findfirst(x -> isa(x, DefSlot), a) # find the defslot in the term
228221
defslot = a[defslot_index]
229-
other_part_matcher = matcher(defslot_index==1 ? a[2] : a[1]) # find the matcher of the normal part
222+
if length(a) == 2
223+
other_part_matcher = matcher(a[defslot_index == 1 ? 2 : 1])
224+
else
225+
others = [a[i] for i in eachindex(a) if i != defslot_index]
226+
T = symtype(term)
227+
f = operation(term)
228+
other_part_matcher = term_matcher_constructor(Term{T}(f, others))
229+
end
230230

231231
normal_matcher = term_matcher_constructor(term)
232232

@@ -236,12 +236,14 @@ function defslot_term_matcher_constructor(term)
236236
result !== nothing && return result
237237
# if no match, try to match with a defslot
238238
# if data (is not a tree and is just a symbol) or (is a tree not starting with the default operation)
239-
if ( !iscall(car(data)) || (iscall(car(data)) && nameof(operation(car(data))) != defslot.operation) )
240-
# checks wether it matches the normal part if yes executes (foo)
241-
# (foo): adds the pair (default value name, default value) to the found bindings
242-
# <------------------(foo)---------------------------->
243-
result = other_part_matcher((b,n) -> assoc(b, defslot.name, defslot.defaultValue), data, bindings)
244-
result !== nothing && return success(result, 1)
245-
end
239+
240+
# checks wether it matches the normal part if yes executes (foo)
241+
# (foo): adds the pair (default value name, default value) to the found bindings
242+
# <------------------(foo)---------------------------->
243+
result = other_part_matcher((b,n) -> assoc(b, defslot.name, defslot.defaultValue), data, bindings)
244+
println(result)
245+
result !== nothing && return success(result, 1)
246+
247+
nothing
246248
end
247249
end

0 commit comments

Comments
 (0)