@@ -11,7 +11,7 @@ function matcher(val::Any)
11
11
if iscall (val)
12
12
# if has two arguments and one of them is a DefSlot, create a term matcher with defslot
13
13
# 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))
15
15
return defslot_term_matcher_constructor (val)
16
16
end
17
17
# else return a normal term matcher
@@ -188,13 +188,6 @@ function term_matcher_constructor(term)
188
188
return term_matcher_pow
189
189
# if the operation is commutative
190
190
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
-
198
191
function term_matcher_comm (success, data, bindings)
199
192
! islist (data) && return nothing # if data is not a list, return nothing
200
193
! iscall (car (data)) && return nothing # if first element is not a call, return nothing
@@ -223,10 +216,17 @@ end
223
216
# (~x + ...complicated pattern...) * ~!y
224
217
# normal part (can bee a tree) operation defslot part
225
218
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)
227
220
defslot_index = findfirst (x -> isa (x, DefSlot), a) # find the defslot in the term
228
221
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
230
230
231
231
normal_matcher = term_matcher_constructor (term)
232
232
@@ -236,12 +236,14 @@ function defslot_term_matcher_constructor(term)
236
236
result != = nothing && return result
237
237
# if no match, try to match with a defslot
238
238
# 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
246
248
end
247
249
end
0 commit comments