-
Notifications
You must be signed in to change notification settings - Fork 2
/
lazy.c
654 lines (575 loc) · 12 KB
/
lazy.c
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/* Lazy K Interpreter/Compiler
*
* Copyright 2015 Total Spectrum Software Inc.
*
* +--------------------------------------------------------------------
* ¦ TERMS OF USE: MIT License
* +--------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* +--------------------------------------------------------------------
*/
//
// SKI evaluator
//
#include <stdlib.h>
#include "lazy.h"
#ifdef __propeller__
//#define DEBUG_RUNTIME
#endif
Cell *partial_eval(Cell *node);
//
// simple memory allocator
//
#ifndef RUNTIME
// main tree
Cell *g_root;
#endif
#ifdef INTERPRETER
Cell mem[NUMCELLS];
#endif
static Cell *free_list;
void
fatal(const char *msg) {
putstr(msg); putstr("\r\n");
abort();
}
// stack of roots that we may have to sweep
static Cell *root_stack[ROOT_STACK_SIZE];
static int root_stack_top;
void push_root(Cell *x) {
if (root_stack_top >= ROOT_STACK_SIZE) {
fatal("root stack overflow");
}
root_stack[root_stack_top++] = x;
}
Cell * pop_root() {
Cell *x;
if (root_stack_top == 0) {
fatal("root stack underflow");
}
--root_stack_top;
x = root_stack[root_stack_top];
return x;
}
static void
gc_mark(Cell *root)
{
CellType t;
Cell *left, *right;
Cell *arg;
if (!root) return;
if (getused(root)) return;
setused(root, true);
t = gettype(root);
switch(t) {
case CT_A_PAIR:
case CT_S2_PAIR:
case CT_NUM_PAIR:
case CT_C2_PAIR:
left = getleft(root);
right = getright(root);
if (left) gc_mark(left);
if (right) gc_mark(right);
return;
case CT_FUNC:
arg = getarg(root);
if (arg) gc_mark(arg);
return;
default:
return;
}
}
// in general we shouldn't have many cells allocated but not yet
// assigned types
#define MAX_PENDING 8
static void
gc_sweep(void)
{
int i;
bool used;
Cell *cur;
int pending_count = 0;
free_list = NULL;
// count down so that the free list starts at the bottom of
// memory; this makes optimizing the compiler output easier
for (i = NUMCELLS-1; i >= 0; --i) {
cur = &mem[i];
used = getused(cur);
if (used) {
setused(cur, false); // in preparation for the next mark round
} else if (ispending(cur)) {
// allocated but not yet used; do not reclaim it
pending_count++;
} else {
// add to free list
settype(cur, CT_FREE);
setstack(cur, free_list);
free_list = cur;
}
}
if (pending_count > MAX_PENDING) {
fatal("unexpectedly high number of pending cells in gc");
}
}
//
// garbage collection function
//
void gc(void)
{
int i;
gc_mark(g_root);
for (i = 0; i < root_stack_top; i++) {
gc_mark(root_stack[i]);
}
gc_sweep();
}
Cell *alloc_cell() {
Cell *next = free_list;
if (!next) {
gc();
next = free_list;
}
if (!next) {
fatal("unable to alloc");
}
assert(gettype(next) == CT_FREE);
free_list = getstack(next);
setpending(next);
return next;
}
//
// make a cell into a number
//
void
mknum(Cell *c, int n)
{
settype(c, CT_NUM);
setnum(c, n);
}
//
// make an s2 pair (eval of SXY)
//
void
mkpair(Cell *c, Cell *X, Cell *Y, CellType t)
{
settype(c, t);
setleft(c, X);
setright(c, Y);
}
//
// make a cell into a function
//
void
mkfunc(Cell *c, CellFunc *func, Cell *arg)
{
settype(c, CT_FUNC);
setfunc(c, func);
setarg(c, arg);
}
//
// function applications
//
// Kx -> (K1 x)
// (K1 x)y -> x
//
Cell *
K1_func(Cell *r, Cell *self, Cell *rhs)
{
return getarg(self);
}
Cell *
K_func(Cell *r, Cell *self, Cell *rhs)
{
mkfunc(r, K1_func, rhs);
return r;
}
//
// special case: KI_func
// KIx = i = 1
Cell *
KI_func(Cell *r, Cell *self, Cell *rhs)
{
mknum(r, 1);
return r;
}
//
// Sx -> (S1 x)
// ((S1 x)y) -> (S2 x y)
//
Cell *
S1_func(Cell *r, Cell *self, Cell *rhs)
{
Cell *lhs;
lhs = getarg(self);
mks2(r, lhs, rhs);
return r;
}
Cell *
S_func(Cell *r, Cell *self, Cell *rhs)
{
mkfunc(r, S1_func, rhs);
return r;
}
//
// Increment function
// Must be applied to a number (or something that evaluates to a number)
//
Cell *
Inc_func(Cell *r, Cell *self, Cell *rhs)
{
int n;
while (gettype(rhs) == CT_A_PAIR) {
push_root(rhs);
rhs = partial_eval(rhs);
pop_root();
}
if (gettype(rhs) != CT_NUM) {
fatal("Inc called on non-number");
}
n = getnum(rhs);
mknum(r, n+1);
return r;
}
//
// useful utility to build a cons
// cons is represented by (lambda (f) (f X Y))
// which is then applied to either the car function (K) or cdr (KI)
// \f.``fXY
// we simplify this with a special C2 pair internally
//
//
Cell *
Cons_func(Cell *r, Cell *X, Cell *Y)
{
mkc2(r, X, Y);
return r;
}
Cell *
C1_func(Cell *r, Cell *self, Cell *rhs)
{
return Cons_func(r, getarg(self), rhs);
}
Cell *
C_func(Cell *r, Cell *self, Cell *rhs)
{
mkfunc(r, &C1_func, rhs);
return r;
}
//
// a cons C2(X,Y) applied to a func f
// produces (fX)Y
//
// so ```Cxyf -> ``fxy
//
Cell *
apply_C2(Cell *r, Cell *self, Cell *f)
{
Cell *A;
Cell *x = getleft(self);
Cell *y = getright(self);
A = alloc_cell();
mkapply(A, f, x);
mkapply(r, A, y);
return r;
}
//
// function which reads the next character
// Read x -> cons( getchar(), Read x )
//
Cell *
Read_func(Cell *r, Cell *self, Cell *rhs)
{
int c;
Cell *getresult;
Cell *apply;
Cell *readf;
#ifdef DBUG_RUNTIME
putstr("Read_func\r\n");
#endif
c = getch();
if (c < 0) c = 256;
#ifdef DEBUG_RUNTIME
putstr("Read_func got: ");
puthex(c);
putstr("\r\n");
#endif
getresult = alloc_cell();
apply = alloc_cell();
readf = alloc_cell();
// Cons_func allocates memory,
// so we can't set the types
// of our newly allocated cells until
// after we call it
Cons_func(r, getresult, apply);
mknum(getresult, c);
mkfunc(readf, Read_func, NULL);
mkapply(apply, readf, rhs);
return r;
}
//
// some special case evaluations
//
// for a number:
// 0f x -> 1x -> x (so 0f = ki)
// 1f x -> fx
// 2f x -> f(fx)
// Nf x -> f((N-1)f x)
//
// the way that works is:
// CT_NUM(n) applied to f -> CT_NUM_PAIR(n,f)
// CT_NUM_PAIR(n,f) applied to x -> f(f(f(...f(x))))
// so for example ((3f)x) -> (f(f(fx)))
Cell *
apply_Num(Cell *r, Cell *self, Cell *rhs)
{
int n = getnum(self);
if (n == 0) {
mknum(r, 1); return r;
}
if (n == 1) {
return rhs;
}
mknumpair(r, self, rhs);
return r;
}
Cell *
apply_NumPair(Cell *r, Cell *self, Cell *rhs)
{
Cell *N = getleft(self);
Cell *F = getright(self);
Cell *Asub, *N_1_x, *N_1;
int n;
if (gettype(N) != CT_NUM) {
fatal("NumPair without a NUM");
}
n = getnum(N);
if ( n == 0 ) return rhs;
// special case the Inc operator
if (gettype(F) == CT_FUNC && getfunc(F) == Inc_func) {
// (N+)x -> (N + x)
if (gettype(rhs) == CT_NUM) {
int m = getnum(rhs);
mknum(r, m+n);
return r;
}
}
//
// (NF)x -> F ((N-1)Fx )
//
if (n == 1) {
mkapply(r, F, rhs);
return r;
}
Asub = alloc_cell();
N_1_x = alloc_cell();
N_1 = alloc_cell();
mknum(N_1, n-1);
mknumpair(N_1_x, N_1, F);
mkapply(Asub, N_1_x, rhs);
mkapply(r, F, Asub);
return r;
}
//
// to apply S2:
// self(x,y) z -> Apply(Apply(x, z), Apply(y, z))
//
Cell *
apply_S2(Cell *r, Cell *self, Cell *z)
{
Cell *A1, *A2;
Cell *x, *y;
A1 = alloc_cell();
A2 = alloc_cell();
x = getleft(self);
y = getright(self);
mkapply(A1, x, z);
mkapply(A2, y, z);
setleft(r, A1);
setright(r, A2);
return r;
}
//
// partial function application
//
Cell *
partial_apply_primitive(Cell *A)
{
Cell *lhs = getleft(A);
Cell *rhs = getright(A);
CellType t = gettype(lhs);
CellFunc *f = NULL;
#ifdef DEBUG_RUNTIME
puthex(A); putstr(" "); puthex(lhs); putstr(" "); puthex(rhs);
putstr(" type="); puthex(t); putstr("\r\n");
#endif
switch (t) {
case CT_S2_PAIR:
f = apply_S2;
break;
case CT_C2_PAIR:
f = apply_C2;
break;
case CT_NUM_PAIR:
f = apply_NumPair;
break;
case CT_NUM:
f = apply_Num;
break;
case CT_FUNC:
f = getfunc(lhs);
break;
default:
fatal("apply_primitive to a non-primitive");
return lhs;
}
#ifdef DEBUG_RUNTIME
putstr("Calling "); puthex(f); putstr("\r\n");
#endif
return (*f)(A, lhs, rhs);
}
Cell *
partial_eval(Cell *node)
{
Cell *prev;
Cell *lhs;
Cell *cur;
push_root(node);
cur = node;
prev = 0;
for(;;) {
while (gettype(cur) == CT_A_PAIR) {
push_root(prev);
prev = cur;
cur = getleft(cur);
}
if (!prev) break;
// lhs is not an A_PAIR, so apply it to the rhs of prev
lhs = cur;
cur = prev; // go back to the apply node
prev = pop_root();
assert(lhs == getleft(cur));
assert( prev == 0 || cur == getleft(prev));
cur = partial_apply_primitive(cur);
//make sure it goes in the tree
if (prev) {
setleft(prev, cur);
}
}
pop_root();
return cur;
}
Cell *car_cdr(Cell *list, CellFunc *fn)
{
Cell *a_node = alloc_cell();
Cell *cK = alloc_cell();
mkfunc(cK, fn, NULL);
mkapply(a_node, list, cK);
return a_node;
}
Cell *car(Cell *list)
{
return car_cdr(list, K_func);
}
Cell *cdr(Cell *list)
{
return car_cdr(list, KI_func);
}
//
// fully evaluate
// basically we evaluate ((list Inc) 0)
// to get a numeric value
//
int
getintvalue(Cell *X)
{
Cell *inc;
Cell *a1, *a2;
Cell *zero;
push_root(X);
// want to evaluate X as a number, so
// ((X inc) 0)
a1 = alloc_cell();
a2 = alloc_cell();
inc = alloc_cell();
zero = alloc_cell();
mknum(zero, 0);
mkfunc(inc, Inc_func, NULL);
mkapply(a2, X, inc);
mkapply(a1, a2, zero);
a1 = partial_eval(a1);
if (gettype(a1) != CT_NUM) {
fatal("getintval evaluated to a non-integer");
}
pop_root();
return getnum(a1);
}
//
// the main loop
//
int
eval_loop()
{
int outc;
Cell *head;
for(;;) {
g_root = partial_eval(g_root);
head = car(g_root);
outc = getintvalue(head);
if (outc >= 256) {
return outc - 256;
}
putch(outc);
g_root = cdr(g_root);
}
}
#ifdef USE_FDS
FullDuplexSerial ser;
#endif
#ifdef RUNTIME
int
main(int argc, char **argv)
{
# ifdef USE_FDS
FullDuplexSerial_start(&ser, 31, 30, 0, 115200);
# endif
return eval_loop(g_root);
}
#endif
#ifdef INTERPRETER
int
main(int argc, char **argv)
{
FILE *f;
if (argc != 2) {
fprintf(stderr, "Usage: %s file.lazy\n", argv[0]);
return 2;
}
f = fopen(argv[1], "r");
if (!f) {
perror(argv[1]);
return 1;
}
#ifdef SMALL
gl_optimize = true;
#endif
g_root = parse_whole(f);
return eval_loop(g_root);
}
#endif