Skip to content

Commit 98654b0

Browse files
optimize term_matcher_contructor a bit
1 parent bd02a51 commit 98654b0

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/matchers.jl

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,32 +107,34 @@ function matcher(segment::Segment)
107107
end
108108

109109
function term_matcher_constructor(term)
110-
matchers = (matcher(operation(term)), map(matcher, arguments(term))...,)
110+
matchers = vcat([matcher(operation(term))], map(matcher, parent(arguments(term))))
111111

112-
function term_matcher(success, data, bindings)
113-
!islist(data) && return nothing # if data is not a list, return nothing
114-
!iscall(car(data)) && return nothing # if first element is not a call, return nothing
112+
let matchers = matchers
113+
function term_matcher(success, data, bindings)
114+
!islist(data) && return nothing # if data is not a list, return nothing
115+
!iscall(car(data)) && return nothing # if first element is not a call, return nothing
115116

116-
function loop(term, bindings′, matchers′) # Get it to compile faster
117-
if !islist(matchers′)
118-
if !islist(term)
119-
return success(bindings′, 1)
117+
function loop(term, bindings′, matchers′) # Get it to compile faster
118+
if !islist(matchers′)
119+
if !islist(term)
120+
return success(bindings′, 1)
121+
end
122+
return nothing
120123
end
121-
return nothing
122-
end
123-
car(matchers′)(term, bindings′) do b, n
124-
loop(drop_n(term, n), b, cdr(matchers′))
124+
car(matchers′)(term, bindings′) do b, n
125+
loop(drop_n(term, n), b, cdr(matchers′))
126+
end
127+
# explenation of above 3 lines:
128+
# car(matchers′)(b,n -> loop(drop_n(term, n), b, cdr(matchers′)), term, bindings′)
129+
# <------ next(b,n) ---------------------------->
130+
# car = first element of list, cdr = rest of the list, drop_n = drop first n elements of list
131+
# Calls the first matcher, with the "next" function being loop again but with n terms dropepd from term
132+
# Term is a linked list (a list and a index). drop n advances the index. when the index sorpasses
133+
# the length of the list, is considered empty
125134
end
126-
# explenation of above 3 lines:
127-
# car(matchers′)(b,n -> loop(drop_n(term, n), b, cdr(matchers′)), term, bindings′)
128-
# <------ next(b,n) ---------------------------->
129-
# car = first element of list, cdr = rest of the list, drop_n = drop first n elements of list
130-
# Calls the first matcher, with the "next" function being loop again but with n terms dropepd from term
131-
# Term is a linked list (a list and a index). drop n advances the index. when the index sorpasses
132-
# the length of the list, is considered empty
133-
end
134135

135-
loop(car(data), bindings, matchers) # Try to eat exactly one term
136+
loop(car(data), bindings, matchers) # Try to eat exactly one term
137+
end
136138
end
137139
end
138140

@@ -149,7 +151,7 @@ end
149151
# calls the success function like term_matcher would do
150152

151153
function defslot_term_matcher_constructor(term)
152-
a = arguments(term) # lenght two bc defslot term matcher is allowed only with +,* and ^, that accept two arguments
154+
a = parent(arguments(term)) # lenght two bc defslot term matcher is allowed only with +,* and ^, that accept two arguments
153155
matchers = (matcher(operation(term)), map(matcher, a)...) # create matchers for the operation and the two arguments of the term
154156

155157
defslot_index = findfirst(x -> isa(x, DefSlot), a) # find the defslot in the term

0 commit comments

Comments
 (0)