-
Notifications
You must be signed in to change notification settings - Fork 16
/
find.go
249 lines (244 loc) · 3.88 KB
/
find.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
. "github.com/ktye/wg/module"
)
type f3i = func(int32, int32, int32) int32
func Fnd(x, y K) K { // x?y
var r K
xt, yt := tp(x), tp(y)
if xt < 16 {
if yt == Tt { // s?T
r = Drp(rx(x), rx(y))
return Atx(r, Grp(Atx(y, x)))
} else {
return deal(x, y)
}
}
if xt > Lt {
if xt == Tt {
trap() //nyi t?..
}
r = x0(x)
return Atx(r, Fnd(r1(x), y))
} else if xt == yt {
return Ecr(18+16*K(I32B(yt == Lt)), l2(x, y))
} else if xt == yt+16 {
r = Ki(fnd(x, y, yt))
} else if xt == Lt {
return fdl(x, y)
} else if yt == Lt {
return Ecr(18, l2(x, y))
} else {
trap() //type
}
dxy(x, y)
return r
}
func fnd(x, y K, t T) int32 {
if nn(x) == 0 {
return nai
}
xp := int32(x)
r := Func[268+t].(f3i)(int32(y), xp, ep(x))
if r == 0 {
return nai
}
return (r - xp) >> (31 - I32clz(sz(16+t)))
}
func fdl(x, y K) K {
xp := int32(x)
dxy(x, y)
e := ep(x)
for xp < e {
if match(K(I64(xp)), y) != 0 {
return Ki((xp - int32(x)) >> 3)
}
xp += 8
}
return Ki(nai)
}
func idx(x, a, b int32) int32 {
for i := a; i < b; i++ {
if x == I8(i) {
return i - a
}
}
return -1
}
func Find(x, y K) K { // find[pattern;string] returns all matches (It)
if tp(x) != Ct || tp(y) != Ct {
trap()
}
xn, yn := nn(x), nn(y)
if xn*yn == 0 {
dxy(x, y)
return mk(It, 0)
}
r := mk(It, 0)
yp := int32(y)
e := yp + yn + 1 - xn
for yp < e { // todo rabin-karp / knuth-morris / boyes-moore..
if findat(int32(x), yp, xn) != 0 {
r = cat1(r, Ki(yp-int32(y)))
yp += xn
} else {
yp++
}
continue
}
dxy(x, y)
return r
}
func findat(xp, yp, n int32) int32 {
for i := int32(0); i < n; i++ {
if I8(xp+i) != I8(yp+i) {
return 0
}
}
return 1
}
func Mtc(x, y K) K {
dxy(x, y)
return Ki(match(x, y))
}
func match(x, y K) int32 {
if x == y {
return 1
}
xt := tp(x)
if xt != tp(y) {
return 0
}
if xt > 16 {
n := nn(x)
if n != nn(y) {
return 0
}
if n == 0 {
return 1
}
xp, yp := int32(x), int32(y)
if xt < Dt {
return Func[246+xt].(f3i)(xp, yp, ep(y))
} else {
if match(K(I64(xp)), K(I64(yp))) != 0 {
return match(K(I64(xp+8)), K(I64(yp+8)))
}
return 0
}
}
yn := int32(0)
xp, yp := int32(x), int32(y)
if xt < ft {
return I32B(xp == yp)
}
switch int32(xt-ft) - 3*I32B(xt > 9) {
case 0: // ft
return I32B(0 == cmF(xp, yp))
case 1: // zt
return I32B(0 == cmZ(xp, yp))
case 2: // composition
yn = 8 * nn(y)
case 3: // derived
yn = 16
case 4: // projection
yn = 24
case 5: // lambda
return match(K(I64(xp+8)), K(I64(yp+8))) // compare strings
default: // xf
return I32B(I64(xp) == I64(yp))
}
for yn > 0 { // composition, derived, projection
yn -= 8
if match(K(I64(xp+yn)), K(I64(yp+yn))) == 0 {
return 0
}
}
return 1
}
func mtC(xp, yp, e int32) int32 {
ve := e &^ 7
for yp < ve {
if I64(xp) != I64(yp) {
return 0
}
xp += 8
yp += 8
}
for yp < e {
if I8(xp) != I8(yp) {
return 0
}
xp++
yp++
}
return 1
}
func mtF(xp, yp, e int32) int32 {
for yp < e {
if cmF(xp, yp) != 0 {
return 0
}
xp += 8
yp += 8
continue
}
return 1
}
func mtL(xp, yp, e int32) int32 {
for yp < e {
if match(K(I64(xp)), K(I64(yp))) == 0 {
return 0
}
xp += 8
yp += 8
continue
}
return 1
}
func In(x, y K) K {
xt, yt := tp(x), tp(y)
if xt == yt && xt > 16 {
return Ecl(30, l2(x, y))
} else if xt+16 != yt {
trap() //type
}
dxy(x, y)
return Ki(I32B(Func[268+xt].(f3i)(int32(x), int32(y), ep(y)) != 0))
}
func inC(x, yp, e int32) int32 {
for yp < e { // maybe splat x to int64
if x == I8(yp) {
return yp
}
yp++
}
return 0
}
func inI(x, yp, e int32) int32 {
for yp < e {
if x == I32(yp) {
return yp
}
yp += 4
}
return 0
}
func inF(xp, yp, e int32) int32 {
for yp < e {
if cmF(xp, yp) == 0 {
return yp
}
yp += 8
}
return 0
}
func inZ(xp, yp, e int32) int32 {
for yp < e {
if cmZ(xp, yp) == 0 {
return yp
}
yp += 16
}
return 0
}