-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathFacade_Func.ahk
460 lines (419 loc) · 9.1 KB
/
Facade_Func.ahk
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#Include <Is>
#Include <Validate>
#Include <Op>
class _Func_Bindable
{
__New(Func)
{
local
global _Func_Bindable
if (Is(Func, _Func_Bindable))
{
Result := Func
}
else
{
this._Func := Func
this._Bindings := []
Result := this
}
return Result
}
Bind(Args*)
{
local
Result := Func_Bindable(this._Func)
Result._Bindings.Push(this._Bindings*)
Result._Bindings.Push(Args*)
return Result
}
Call(Args*)
{
local
FullArgs := []
FullArgs.Push(this._Bindings*)
FullArgs.Push(Args*)
return this._Func.Call(FullArgs*)
}
__Get(Key)
{
local
; Members we did not override pass through.
if (Key != "base" and not this.base.HasKey(Key))
{
return this._Func[Key]
}
}
__Call(Method, Args*)
{
local
; %Func%(Args*)
if (Method == "")
{
return this.Call(Args*)
}
}
}
Func_Bindable(Func)
{
local
global _Func_Bindable
Sig := "Func_Bindable(Func)"
_Validate_FuncArg(Sig, "Func", Func)
return new _Func_Bindable(Func)
}
class _Func_Applicable extends _Func_Bindable
{
__New(Obj)
{
local
global _Func_Applicable
if (Is(Obj, _Func_Applicable))
{
Result := Obj
}
else
{
this._Obj := Obj
this._Bindings := []
Result := this
}
return Result
}
Clone()
{
local
Result := Func_Applicable(this._Obj.Clone())
Result._Bindings.Push(this._Bindings*)
return Result
}
Bind(Args*)
{
local
Result := Func_Applicable(this._Obj)
Result._Bindings.Push(this._Bindings*)
Result._Bindings.Push(Args*)
return Result
}
Call(Args*)
{
local
FullArgs := []
FullArgs.Push(this._Bindings*)
FullArgs.Push(Args*)
return Op_Get(this._Obj, FullArgs*)
}
__Get(Key)
{
local
; Members we did not override pass through.
if (Key != "base" and not this.base.HasKey(Key))
{
return Op_Get(this._Obj, Key)
}
}
__Call(Method, Args*)
{
local
; %Func%(Args*)
if (Method == "")
{
return this.Call(Args*)
}
; Members we did not override pass through.
if (not this.base.HasKey(Method))
{
return (this._Obj)[Method](Args*)
}
}
}
Func_Applicable(Obj)
{
local
global _Func_Applicable
Sig := "Func_Applicable(Obj)"
_Validate_ObjArg(Sig, "Obj", Obj)
return new _Func_Applicable(Obj)
}
Func_Apply(Func, Args)
{
local
Sig := "Func_Apply(Func, Args)"
_Validate_FuncArg(Sig, "Func", Func)
_Validate_BadArrayArg(Sig, "Args", Args)
return Func.Call(Args*)
}
_Func_CompAux(Funcs, Args*)
{
local
Index := Funcs.Length()
Result := Funcs[Index].Call(Args*)
--Index
while (Index >= 1)
{
Result := Funcs[Index].Call(Result)
--Index
}
return Result
}
Func_Comp(Funcs*)
{
local
Sig := "Func_Comp(Funcs*)"
_Validate_FuncArgs(Sig, Funcs)
return Funcs.Length() == 0 ? Func_Bindable(Func("Func_Id"))
: Funcs.Length() == 1 ? Func_Bindable(Funcs[1])
: Func_Bindable(Func("_Func_CompAux").Bind(Funcs))
}
_Func_ReparamAux(Func, Positions, Args*)
{
local
NewArgs := []
MaxPosition := 0
for Destination, Source in Positions
{
NewArgs[Destination] := Args[1 + Source]
MaxPosition := 1 + Source > MaxPosition ? 1 + Source
: MaxPosition
}
while (MaxPosition < Args.Length())
{
++MaxPosition
NewArgs.Push(Args[MaxPosition])
}
return Func.Call(NewArgs*)
}
Func_Reparam(Func, Positions)
{
local
Sig := "Func_Reparam(Func, Positions)"
_Validate_FuncArg(Sig, "Func", Func)
_Validate_BadArrayArg(Sig, "Positions", Positions)
FilteredPositions := []
for Index, Value in Positions
{
if ( Is(Index, "integer")
and 1 <= Index)
{
if (not Is(Value, "integer"))
{
throw Exception("Type Error", -1
,Sig . " Invalid position " . _Validate_TypeRepr(Value) . ".")
}
if (not 0 <= Value)
{
throw Exception("Value Error", -1
,Sig . " Invalid position " . Value . ".")
}
FilteredPositions[Index] := Value
}
}
return Func_Bindable(Func("_Func_ReparamAux").Bind(Func, FilteredPositions))
}
Func_Flip(F)
{
local
Sig := "Func_Flip(F)"
_Validate_FuncArg(Sig, "F", F)
return Func_Reparam(F, [1, 0])
}
Func_Id(X)
{
local
return X
}
Func_Const(X)
{
local
; Function objects ignore excess arguments.
;
; Make it bindable, not because binding is useful here, but because
; Bind(Args*) is expected to return a function object.
return Func_Bindable(Func("Func_Id").Bind(X))
}
_Func_OnAux(G, F, X, Y)
{
local
return G.Call(F.Call(X), F.Call(Y))
}
Func_On(G, F)
{
local
Sig := "Func_On(G, F)"
_Validate_FuncArg(Sig, "G", G)
_Validate_FuncArg(Sig, "F", F)
return Func_Bindable(Func("_Func_OnAux").Bind(G, F))
}
Func_Hook1(G, F)
{
local
Sig := "Func_Hook1(G, F)"
_Validate_FuncArg(Sig, "G", G)
_Validate_FuncArg(Sig, "F", F)
return Func_Dup(Func_Hook2(G, F))
}
_Func_Hook2Aux(G, F, X, Y)
{
local
return G.Call(X, F.Call(Y))
}
Func_Hook2(G, F)
{
local
Sig := "Func_Hook2(G, F)"
_Validate_FuncArg(Sig, "G", G)
_Validate_FuncArg(Sig, "F", F)
return Func_Bindable(Func("_Func_Hook2Aux").Bind(G, F))
}
_Func_ForkAux(F, H, G, Args*)
{
local
return H.Call(F.Call(Args*), G.Call(Args*))
}
Func_Fork(F, H, G)
{
local
Sig := "Func_Fork(F, H, G)"
_Validate_FuncArg(Sig, "F", F)
_Validate_FuncArg(Sig, "H", G)
_Validate_FuncArg(Sig, "G", G)
return Func_Bindable(Func("_Func_ForkAux").Bind(F, H, G))
}
Func_Dup(F)
{
local
Sig := "Func_Dup(F)"
_Validate_FuncArg(Sig, "F", F)
return Func_Reparam(F, [0, 0])
}
_Func_NotAux(Pred, Args*)
{
local
return not Pred.Call(Args*)
}
Func_Not(Pred)
{
local
Sig := "Func_Not(Pred)"
_Validate_FuncArg(Sig, "Pred", Pred)
return Func_Bindable(Func("_Func_NotAux").Bind(Pred))
}
_Func_AndAux(Preds, Args*)
{
local
Index := 1
Result := Preds[Index].Call(Args*)
++Index
while (Result and Index <= Preds.Length())
{
Result := Preds[Index].Call(Args*)
++Index
}
return Result
}
Func_And(Preds*)
{
local
Sig := "Func_And(Preds*)"
_Validate_FuncArgs(Sig, Preds)
return Preds.Length() == 0 ? Func_Const(true)
: Preds.Length() == 1 ? Func_Bindable(Preds[1])
: Func_Bindable(Func("_Func_AndAux").Bind(Preds))
}
_Func_OrAux(Preds, Args*)
{
local
Index := 1
Result := Preds[Index].Call(Args*)
++Index
while (not Result and Index <= Preds.Length())
{
Result := Preds[Index].Call(Args*)
++Index
}
return Result
}
Func_Or(Preds*)
{
local
Sig := "Func_Or(Preds*)"
_Validate_FuncArgs(Sig, Preds)
return Preds.Length() == 0 ? Func_Const(false)
: Preds.Length() == 1 ? Func_Bindable(Preds[1])
: Func_Bindable(Func("_Func_OrAux").Bind(Preds))
}
_Func_IfAux(Pred, ThenFunc, ElseFunc, Args*)
{
local
return Pred.Call(Args*) ? ThenFunc.Call(Args*) : ElseFunc.Call(Args*)
}
Func_If(Pred, ThenFunc, ElseFunc)
{
local
Sig := "Func_If(Pred, ThenFunc, ElseFunc)"
_Validate_FuncArg(Sig, "Pred", Pred)
_Validate_FuncArg(Sig, "ThenFunc", ThenFunc)
_Validate_FuncArg(Sig, "ElseFunc", ElseFunc)
return Func_Bindable(Func("_Func_IfAux").Bind(Pred, ThenFunc, ElseFunc))
}
_Func_ConvAux(F, X)
{
local
PredecessorX := {}
while (not X == PredecessorX)
{
PredecessorX := X
X := F.Call(X)
}
return X
}
Func_Conv(F)
{
local
Sig := "Func_Conv(F)"
_Validate_FuncArg(Sig, "F", F)
return Func_Bindable(Func("_Func_ConvAux").Bind(F))
}
_Func_AdverseAux(Funcs, Args*)
{
local
Failed := true
Index := 1
while (Failed and Index <= Funcs.Length())
{
try
{
Result := Funcs[Index].Call(Args*)
Failed := false
}
catch E
{
++Index
}
}
if (Failed)
{
throw E
}
return Result
}
Func_Adverse(Funcs*)
{
local
Sig := "Func_Adverse(Funcs*)"
_Validate_FuncArgs(Sig, Funcs)
if (Funcs.Length() == 0)
{
throw Exception("Arity Error", -1
,Sig . " Called with 0 arguments.")
}
else if (Funcs.Length() == 1)
{
Result := Func_Bindable(Funcs[1])
}
else
{
Result := Func_Bindable(Func("_Func_AdverseAux").Bind(Funcs))
}
return Result
}