-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuni.cpp
453 lines (402 loc) · 9.49 KB
/
uni.cpp
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
#include <cstdio>
#include <cassert>
#include <getopt.h>
#include <vector>
#include <memory>
#include <algorithm>
#include "IntrusiveRefCntPtr.h"
#ifdef LOG
#define log(...) fprintf(stderr, ##__VA_ARGS__)
#define logp(M) M
#else
#define log(...)
#define logp(M)
#endif
using idx = std::vector<int>::size_type;
enum {
// output
OB,
O0,
O1,
// denotes a variable, with next entry its de Bruijn index
V,
// denotes application with next entry the size of the term being applied
A,
// denotes lambda abstraction
L,
// denotes lambda abstraction for a closed term (combinator)
K
};
enum {
MARKER,
FORWARD
};
struct C;
using Cp = IntrusiveRefCntPtr<C>;
//closure
struct C {
// lambda term (index in term space)
idx t;
// pointer to environment
Cp e;
// pointer to the next closure
Cp n;
// ref count
int r;
// next free element
C* next;
// free list;
static C* freel;
//push l onto top of s
static void push(C*& top, C* const l) {
l->next = top;
top = l;
}
//pop the top element of s
static C* pop(C*& top) {
C* l = top;
top = top->next;
return l;
}
static C* newC(idx at, C* const ae) {
if (!freel) {
freel = new C();
}
C* l = pop(freel);
l->t = at;
l->e = Cp(ae);
return l;
}
void retain() {
r++;
}
void release() {
r--;
if (r == 0) {
log("EA free n%x\n", this);
push(freel, this);
}
}
};
C* C::freel = nullptr;
//machine state
struct U {
// optimization flags
bool lazy = false;
bool collapse = false;
bool shortcircuit = false;
// b=0 for bit mode and 7 for byte mode
// default is byte mode
idx b = 7;
//bits left in current byte
idx left;
//current input char
int c;
// Lambda term space
std::vector<idx> T = {
//encoding of input stream
// S = L A8,A2V0 ..
// 0 = L A8 A2V0LLV0 ..
// 1 = L A8 A2V0LLV1 ..
// E = LL V0
// bit input : 10
// encoded : 10E
// byte input: \1\0
// encoded : S00000001E S00000000E E
K,A,8,A,2, V,0,K,L,V,
//10 - start here for byte mode
//V 12 - jump here after output a byte
// V
A,30,K,A,2,V,0,K,A,5,A,7,K,V,0,OB,
//26 - start here for bit mode
//V 28 - jump here after output a bit
// V
A,14,K,A,2,V,0,K,A,5,A,2, V,0,O0,O1,A
};
// byte mode term
// (\output output (\B B (\b b O0 O1) OB)) (P input)
// bit mode term
// (\output output (\b b O0 O1)) (P input)
// current term to be processed
// 10 for byte mode
// 26 for bit mode
idx t = 10;
//current environment
Cp e;
//s points to closure on the top of the stack
C* s;
//number of steps executed
idx steps = 0;
idx lookup = 0;
idx marker = 0;
idx forward = 0;
//copy T[l..u] to end of T
void x(idx l,idx u) {
log("X %lu %lu\n", l, u);
for(; l<=u; T.push_back(T[l++]));
}
//gets one bit of input, setting i to -1 on EOF or to remaining number of bits in current byte
int g() {
if (left == 0) {
left=b;
c=getchar();
} else {
left--;
}
return (c>>left)&1;
}
//write one char to stdout
void w(char o) {
putchar(o);
log("P %c\n", o);
fflush(stdout);
}
//push l onto top of e
void push(Cp& top, C* const l) {
l->n = top;
top = Cp(l);
log("EA n%x n%x\n", top->n.get(), top.get());
}
//parses blc-encoded lambda term using g(), stores results in term space and returns length
idx p(const idx m) {
if (g()) {
T.push_back(V);
T.push_back(0);
while (g()) {
T[T.size()-1]++;
}
} else {
//01 -> application
if (g()) {
T.push_back(A);
T.push_back(0);
idx j = T.size() - 1;
T[j] = p(m+2);
//00 -> abstraction
} else {
T.push_back(L);
}
// decode the rest of the input
p(T.size());
}
return T.size()-m;
}
//mark closed terms in program with K
idx markClosed(const idx m) {
switch (T[m]) {
case A: {
return std::max(markClosed(m+2), markClosed(m+2+T[m+1]));
}
case V: {
return T[m+1];
}
case L: {
auto max = markClosed(m+1);
if (max == 0) {
T[m] = K;
return 0;
} else {
return max - 1;
}
}
default: {
return 0;
}
}
}
void showS(C* h, const char *name) {
log("%s ", name);
while (h) {
log("(t:%lu, r:%d, e:%p, a:%p) ", h->t, h->r, h->e.get(), h);
h = h->next;
}
log("\n");
}
void showE(C* h, const char *name) {
log("%s ", name);
while (h) {
log("(t:%lu, r:%d, e:%p, a:%p) ", h->t, h->r, h->e.get(), h);
h = h->n.get();
}
log("\n");
}
idx showI(idx j) {
log("T[%lu]: ", j);
switch (read(j)) {
case OB: log("OB\n"); break;
case O0: log("O0\n"); break;
case O1: log("O1\n"); break;
case V: log("V %lu\n", T[j+1]); j++; break;
case A: log("A %lu\n", T[j+1]); j++; break;
case L: log("L\n"); break;
case K: log("K\n"); break;
}
j++;
return j;
}
void showP() {
for (idx j = 44; j < T.size();) {
j = showI(j);
}
}
void read() {
g();
left++;
//not EOF and in byte mode, setup one byte
if (~c&&b) {
x(0,6);
for(T[T.size()-5]=96; left; T.push_back(!g()))
x(0,9);
}
//EOF reached
if (c < 0) {
x(7, 9);
//in bit mode, setup one bit
//in byte mode, terminate byte list
} else {
x(b, 9);
}
T.push_back(!b&&!g());
}
idx read(idx j) {
while (j >= T.size()) {
read();
}
return T[j];
}
C* deref(idx index) {
C* clo = e.get();
for(idx j=index; j--; clo=clo->n.get());
lookup += index + 1;
while (clo->t == FORWARD) {
clo = clo->e.get();
forward++;
}
return clo;
}
int run() {
// output char
char o = '\0';
while(1) {
steps++;
logp(showS(s, "S"));
logp(showE(e.get(), "E"));
logp(showI(t));
switch(read(t)) {
case OB:
w(o);
t = 12;
e = nullptr;
break;
case O0:
if (b) {
o = 2 * o + 0;
} else {
w('0');
}
t = 28;
e = nullptr;
break;
case O1:
if (b) {
o = 2 * o + 1;
} else {
w('1');
}
t = 28;
e = nullptr;
break;
case V: {
//resolve v to an closure clo and continue execution
//with t = clo->t and e = clo->e
C* clo = deref(T[t+1]);
t=clo->t;
e=clo->e;
//push marker on the stack to update clo
if (lazy && (read(t) == V || read(t) == A) && clo->r > 1) {
if (collapse && s && s->t == MARKER) {
clo->t = FORWARD;
clo->e = s->e;
} else {
C::push(s, C::newC(MARKER, clo));
}
}
break;
}
case A: {
//create a closure and push it onto S
// t = index of term that is the argument
// e = current environment
const idx size = T[t+1];
t+=2;
if (shortcircuit && read(t+size) == V) {
C* clo = deref(T[t+size+1]);
C::push(s, C::newC(FORWARD, clo));
} else if (read(t+size) == K) {
C::push(s, C::newC(t+size, nullptr));
} else {
C::push(s, C::newC(t+size, e.get()));
}
break;
}
case L:
case K: {
//pop marker from the stack and update clo
while (lazy && s && s->t == MARKER) {
C* m = C::pop(s);
C* clo = m->e.get();
clo->t = t;
clo->e = e;
delete m;
marker++;
}
//pop closure from stack and make it top level environment
if (!s) {
return 0;
}
//closed terms do not reference their environment beyond their operand
if (T[t] == K) {
e = nullptr;
}
push(e, C::pop(s));
t++;
break;
}}
}
}
void load() {
// read program into end of T and store its size in T[43];
T.push_back(0);
T[43] = p(T.size());
// clear bits left
left=0;
// mark closed terms in the program
markClosed(44);
}
};
int main(int argc, char **argv) {
U u = U();
// process options
int ch;
while ((ch = getopt(argc, argv, "bBpolcs")) != -1) {
switch (ch) {
case 'B': u.b = 7; u.t = 10; break;
case 'b': u.b = 0; u.t = 26; break;
case 'p': u.t = 44; break;
case 'o': u.lazy = true; u.collapse = true; u.shortcircuit = true; break;
case 'l': u.lazy = true; break;
case 'c': u.collapse = true; break;
case 's': u.shortcircuit = true; break;
}
}
u.load();
// show loaded program
logp(u.showP());
u.run();
fprintf(stderr, "\n");
fprintf(stderr, "steps = %ld\n", u.steps);
fprintf(stderr, "lookup = %ld\n", u.lookup);
fprintf(stderr, "marker = %ld\n", u.marker);
fprintf(stderr, "forward = %ld\n", u.forward);
return 0;
}