forked from rui314/minilisp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminilisp.c
1445 lines (1273 loc) · 43.1 KB
/
minilisp.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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//// This software is in the public domain.
// Originally from: https://github.com/rui314/minilisp
#define VERSION "Color Computer MiniLisp 0.6\n"
#include <cmoc.h>
#include <stdarg.h>
#include <coco.h>
#define bool byte
#define inline
#define __attribute(x)
#define noreturn
#define static
#define ptrdiff_t int
#define false FALSE
#undef NULL
#define NULL 0
#define true TRUE
#include <disk.h>
#include "setjmp.h"
#include "setjmp.c"
// Macros to help us with print operations
#define bprintf(...) { swap_in_basic_for_print(); printf(__VA_ARGS__); swap_out_basic_after_print(); }
#define fprintf(x, y, ...) bprintf(y, __VA_ARGS__)
// We map in an area of memory from 0x8000 to 0xA000 to store our input buffer
#define MAP_IN_INPUT_BUFFER { \
asm { lda #$34 }\
asm { sta $ffa4 }\
}
#define MAP_OUT_INPUT_BUFFER { \
asm { lda #$30 }\
asm { sta $ffa4 }\
}
// When we perform print operations, we store the old stack pointer and move the stack
// to 0x600. This is because BASIC maps the 40 and 80 column screen memory to
// 0x2000 and 0x4000 which will overlap with our stack. 0x500 is in the middle of
// the 32x16 text screen memory.
void *stack_ptr;
// Swaps Basic back in, turns on interrupts, moves stack
// so that print opertions will work.
asm void swap_in_basic_for_print() {
asm {
orcc #$50
puls d
sts stack_ptr
lds #$600
pshs d
bra swap_in_basic
}
}
// Swaps Basic back out, turns on interrupts and restores
// the stack after a print operation
asm void swap_out_basic_after_print() {
asm {
orcc #$50
puls d
lds stack_ptr
pshs d
bra swap_out_basic
}
}
// Swaps Basic back in and turns on interrupts.
asm void swap_in_basic() {
asm {
ldd #$3c3d
sta $ffa4
stb $ffa5
ldd #$3e3f
sta $ffa6
stb $ffa7
andcc #$af
rts
}
}
// Swaps Basic back out and turns off interrupts.
asm void swap_out_basic() {
asm {
orcc #$50
ldd #$3031
sta $ffa4
stb $ffa5
ldd #$3233
sta $ffa6
stb $ffa7
rts
}
}
bool doing_load = FALSE;
void bclose(struct FileDesc *fd) {
doing_load = FALSE;
swap_in_basic();
setHighSpeed(FALSE);
close(fd);
setHighSpeed(TRUE);
swap_out_basic();
}
word bread(struct FileDesc *fd, char *buf, word numBytesRequested) {
swap_in_basic();
setHighSpeed(FALSE);
word num_read = read(fd, buf, numBytesRequested);
setHighSpeed(TRUE);
swap_out_basic();
return num_read;
}
byte bopen(struct FileDesc *fd, char *filename) {
swap_in_basic();
setHighSpeed(FALSE);
byte bopen_result = openfile(fd, filename);
setHighSpeed(TRUE);
swap_out_basic();
doing_load = bopen_result;
return bopen_result;
}
jmp_buf jmpbuf;
#define error(...) {\
bprintf(__VA_ARGS__); \
longjmp(&jmpbuf, 0); \
}
#define error2(x, y) {\
memcpy(generic_buffer, y, sizeof(generic_buffer)-1); \
generic_buffer[sizeof(generic_buffer)-1] = '\0'; \
bprintf(x, generic_buffer); \
longjmp(&jmpbuf, 0); \
}
//======================================================================
// Lisp objects
//======================================================================
// The Lisp object type
// Regular objects visible from the user
#define TLONG 1
#define TCELL 2
#define TSYMBOL 3
#define TPRIMITIVE 4
#define TFUNCTION 5
#define TMACRO 6
#define TENV 7
// The marker that indicates the object has been moved to other location by GC. The new location
// can be found at the forwarding pointer. Only the functions to do garbage collection set and
// handle the object of this type. Other functions will never see the object of this type.
#define TMOVED 8
// Const objects. They are statically allocated and will never be managed by GC.
#define TTRUE 9
#define TNIL 10
#define TDOT 11
#define TCPAREN 12
// Typedef for the primitive function
typedef void *Primitive;
// The object type
typedef struct Obj {
// The first word of the object represents the type of the object. Any code that handles object
// needs to check its type first, then access the following union members.
int type;
// The total size of the object, including "type" field, this field, the contents, and the
// padding at the end of the object.
int size;
// Object values.
union val {
// long
long value;
// Cell
struct cell {
struct Obj *car;
struct Obj *cdr;
} cell;
// Symbol
char name[1];
// Primitive
Primitive *fn;
// Function or Macro
struct func {
struct Obj *params;
struct Obj *body;
struct Obj *env;
} func;
// Environment frame. This is a linked list of association lists
// containing the mapping from symbols to their value.
struct env {
struct Obj *vars;
struct Obj *up;
} env;
// Forwarding pointer
void *moved;
} val;
} Obj;
// Constants
static Obj *True;
static Obj *Nil;
static Obj *Dot;
static Obj *Cparen;
// The list containing all symbols. Such data structure is traditionally called the "obarray", but I
// avoid using it as a variable name as this is not an array but a list.
static Obj *Symbols;
//======================================================================
// Memory management
//======================================================================
// The size of the heap in byte
#define MEMORY_SIZE 15360
byte *memory1 = (byte *)0x8000;
byte *memory2 = memory1 + MEMORY_SIZE;
// The pointer pointing to the beginning of the current heap
static void *memory;
// The pointer pointing to the beginning of the old heap
static void *from_space;
// The number of bytes allocated from the heap
static size_t mem_nused = 0;
// Flags to debug GC
static bool gc_running = false;
static bool debug_gc = false;
static bool always_gc = false;
static void gc(void *root);
// Currently we are using Cheney's copying GC algorithm, with which the available memory is split
// into two halves and all objects are moved from one half to another every time GC is invoked. That
// means the address of the object keeps changing. If you take the address of an object and keep it
// in a C variable, dereferencing it could cause SEGV because the address becomes invalid after GC
// runs.
//
// In order to deal with that, all access from C to Lisp objects will go through two levels of
// pointer dereferences. The C local variable is pointing to a pointer on the C stack, and the
// pointer is pointing to the Lisp object. GC is aware of the pointers in the stack and updates
// their contents with the objects' new addresses when GC happens.
//
// The following is a macro to reserve the area in the C stack for the pointers. The contents of
// this area are considered to be GC root.
//
// Be careful not to bypass the two levels of pointer indirections. If you create a direct pointer
// to an object, it'll cause a subtle bug. Such code would work in most cases but fails with SEGV if
// GC happens during the execution of the code. Any code that allocates memory may invoke GC.
#define ROOT_END ((void *)-1)
#define ADD_ROOT(size) \
void *root_ADD_ROOT_[size + 2]; \
root_ADD_ROOT_[0] = (void *)root; \
for (int i = 1; i <= size; i++) \
root_ADD_ROOT_[i] = NULL; \
root_ADD_ROOT_[size + 1] = ROOT_END; \
root = (void *)root_ADD_ROOT_
#define DEFINE1(var1) \
ADD_ROOT(1); \
Obj **var1 = (Obj **)((unsigned int)root_ADD_ROOT_ + 2)
#define DEFINE2(var1, var2) \
ADD_ROOT(2); \
Obj **var1 = (Obj **)((unsigned int)root_ADD_ROOT_ + 2); \
Obj **var2 = (Obj **)((unsigned int)root_ADD_ROOT_ + 4)
#define DEFINE3(var1, var2, var3) \
ADD_ROOT(3); \
Obj **var1 = (Obj **)((unsigned int)root_ADD_ROOT_ + 2); \
Obj **var2 = (Obj **)((unsigned int)root_ADD_ROOT_ + 4); \
Obj **var3 = (Obj **)((unsigned int)root_ADD_ROOT_ + 6)
#define DEFINE4(var1, var2, var3, var4) \
ADD_ROOT(4); \
Obj **var1 = (Obj **)((unsigned int)root_ADD_ROOT_ + 2); \
Obj **var2 = (Obj **)((unsigned int)root_ADD_ROOT_ + 4); \
Obj **var3 = (Obj **)((unsigned int)root_ADD_ROOT_ + 6); \
Obj **var4 = (Obj **)((unsigned int)root_ADD_ROOT_ + 8)
// Round up the given value to a multiple of size. Size must be a power of 2. It adds size - 1
// first, then zero-ing the least significant bits to make the result a multiple of size. I know
// these bit operations may look a little bit tricky, but it's efficient and thus frequently used.
static inline size_t roundup(size_t var, size_t size) {
return (var + size - 1) & ~(size - 1);
}
// Allocates memory block. This may start GC if we don't have enough memory.
static Obj *alloc(void *root, int type, size_t size) {
// The object must be large enough to contain a pointer for the forwarding pointer. Make it
// larger if it's smaller than that.
//size = roundup(size, sizeof(void *));
// Add the size of the type tag and size fields.
size += offsetof(Obj, val.value);
// Round up the object size to the nearest alignment boundary, so that the next object will be
// allocated at the proper alignment boundary. Currently we align the object at the same
// boundary as the pointer.
size = roundup(size, sizeof(void *));
// If the debug flag is on, allocate a new memory space to force all the existing objects to
// move to new addresses, to invalidate the old addresses. By doing this the GC behavior becomes
// more predictable and repeatable. If there's a memory bug that the C variable has a direct
// reference to a Lisp object, the pointer will become invalid by this GC call. Dereferencing
// that will immediately cause SEGV.
if (always_gc && !gc_running)
gc(root);
// Otherwise, run GC only when the available memory is not large enough.
if (!always_gc && MEMORY_SIZE < mem_nused + size)
gc(root);
// Terminate the program if we couldn't satisfy the memory request. This can happen if the
// requested size was too large or the from-space was filled with too many live objects.
if (MEMORY_SIZE < mem_nused + size)
error("Memory exhausted %x < %x\n", MEMORY_SIZE, mem_nused + size);
// Allocate the object.
Obj *obj = (Obj *)(memory + mem_nused);
obj->type = type;
obj->size = size;
mem_nused += size;
return obj;
}
//======================================================================
// Garbage collector
//======================================================================
// Cheney's algorithm uses two pointers to keep track of GC status. At first both pointers point to
// the beginning of the to-space. As GC progresses, they are moved towards the end of the
// to-space. The objects before "scan1" are the objects that are fully copied. The objects between
// "scan1" and "scan2" have already been copied, but may contain pointers to the from-space. "scan2"
// points to the beginning of the free space.
static Obj *scan1;
static Obj *scan2;
// Moves one object from the from-space to the to-space. Returns the object's new address. If the
// object has already been moved, does nothing but just returns the new address.
static inline Obj *forward(Obj *obj) {
// If the object's address is not in the from-space, the object is not managed by GC nor it
// has already been moved to the to-space.
ptrdiff_t offset = (uint8_t *)obj - (uint8_t *)from_space;
if (offset < 0 || MEMORY_SIZE <= offset)
return obj;
// The pointer is pointing to the from-space, but the object there was a tombstone. Follow the
// forwarding pointer to find the new location of the object.
if (obj->type == TMOVED)
return (Obj *)obj->val.moved;
// Otherwise, the object has not been moved yet. Move it.
Obj *newloc = scan2;
memcpy(newloc, obj, obj->size);
scan2 = (Obj *)((uint8_t *)scan2 + obj->size);
// Put a tombstone at the location where the object used to occupy, so that the following call
// of forward() can find the object's new location.
obj->type = TMOVED;
obj->val.moved = (void *)newloc;
return newloc;
}
// Copies the root objects.
static void forward_root_objects(void *root) {
Symbols = forward(Symbols);
for (void **frame = (void **)root; frame; frame = *(void ***)frame)
for (int i = 1; frame[i] != ROOT_END; i++)
if (frame[i])
frame[i] = (void *)forward((Obj *)frame[i]);
}
// Implements Cheney's copying garbage collection algorithm.
// http://en.wikipedia.org/wiki/Cheney%27s_algorithm
static void gc(void *root) {
assert(!gc_running);
gc_running = true;
// Allocate a new semi-space.
from_space = memory;
memory = (void *)((from_space == memory1) ? memory2 : memory1);
// Initialize the two pointers for GC. Initially they point to the beginning of the to-space.
scan1 = scan2 = (Obj *)memory;
// Copy the GC root objects first. This moves the pointer scan2.
forward_root_objects(root);
// Copy the objects referenced by the GC root objects located between scan1 and scan2. Once it's
// finished, all live objects (i.e. objects reachable from the root) will have been copied to
// the to-space.
while (scan1 < scan2) {
switch (scan1->type) {
case TLONG:
case TSYMBOL:
case TPRIMITIVE:
// Any of the above types does not contain a pointer to a GC-managed object.
break;
case TCELL:
scan1->val.cell.car = forward(scan1->val.cell.car);
scan1->val.cell.cdr = forward(scan1->val.cell.cdr);
break;
case TFUNCTION:
case TMACRO:
scan1->val.func.params = forward(scan1->val.func.params);
scan1->val.func.body = forward(scan1->val.func.body);
scan1->val.func.env = forward(scan1->val.func.env);
break;
case TENV:
scan1->val.env.vars = forward(scan1->val.env.vars);
scan1->val.env.up = forward(scan1->val.env.up);
break;
default:
error("Bug: copy: unknown type %d\n", scan1->type);
}
scan1 = (Obj *)((uint8_t *)scan1 + scan1->size);
}
// Finish up GC.
size_t old_nused = mem_nused;
mem_nused = (size_t)((uint8_t *)scan1 - (uint8_t *)memory);
byte sp;
if (debug_gc)
fprintf(stderr, "GC: %x/%x SP: %x\n", mem_nused, old_nused, &sp);
gc_running = false;
}
//======================================================================
// Constructors
//======================================================================
static Obj *make_long(void *root, long value) {
Obj *r = alloc(root, TLONG, sizeof(long));
r->val.value = value;
return r;
}
static Obj *cons(void *root, Obj **car, Obj **cdr) {
Obj *cell = alloc(root, TCELL, sizeof(Obj *) * 2);
cell->val.cell.car = *car;
cell->val.cell.cdr = *cdr;
return cell;
}
static Obj *make_symbol(void *root, const char *name) {
Obj *sym = alloc(root, TSYMBOL, strlen(name) + 1);
strcpy(sym->val.name, name);
return sym;
}
static Obj *make_primitive(void *root, Primitive *fn) {
Obj *r = alloc(root, TPRIMITIVE, sizeof(Primitive *));
r->val.fn = fn;
return r;
}
static Obj *make_function(void *root, Obj **env, int type, Obj **params, Obj **body) {
assert(type == TFUNCTION || type == TMACRO);
Obj *r = alloc(root, type, sizeof(Obj *) * 3);
r->val.func.params = *params;
r->val.func.body = *body;
r->val.func.env = *env;
return r;
}
Obj *make_env(void *root, Obj **vars, Obj **up) {
Obj *r = alloc(root, TENV, sizeof(Obj *) * 2);
r->val.env.vars = *vars;
r->val.env.up = *up;
return r;
}
// Returns ((x . y) . a)
static Obj *acons(void *root, Obj **x, Obj **y, Obj **a) {
DEFINE1(cell);
*cell = cons(root, x, y);
return cons(root, cell, a);
}
//======================================================================
// Parser
//
// This is a hand-written recursive-descendent parser.
//======================================================================
#define SYMBOL_MAX_LEN 50
byte generic_buffer[SYMBOL_MAX_LEN + 1];
const char symbol_chars[] = "~!@#$%^&*-_=+:/?<>";
static Obj *read_expr(void *root);
#define stdin 1
#define stderr 1
#define EOF 0
#define SCREEN_WIDTH 80
#define INPUT_BUFFER_SIZE (SCREEN_WIDTH * SCREEN_BUFFER_HEIGHT)
#define screen_ptr ((char **)0xfe00)
#define SCREEN_BYTES_PER_CHAR 2
#define screen_x ((char *)0xfe02)
#define screen_y ((char *)0xfe03)
#define SCREEN_BUFFER_HEIGHT 24
char *buffer = (char *)0x8000;
byte has_data = false;
char *start_pos;
char *end_pos;
struct FileDesc fd;
bool has_file_data = FALSE;
char file_data;
char last_char;
char getchar() {
// If there is any buffered file data, return it now
if (has_file_data) {
has_file_data = FALSE;
return file_data;
}
// If we are doing a load, read a char
if (doing_load) {
word num_read = bread(&fd, &file_data, sizeof(file_data));
// If we ran out of chars, clean up
if (num_read == 0) {
bclose(&fd);
} else {
return file_data;
}
}
// If we have data buffered already, simply return that data.
if (has_data) {
MAP_IN_INPUT_BUFFER;
char c = *start_pos++;
MAP_OUT_INPUT_BUFFER;
if (start_pos >= end_pos) {
has_data = false;
}
return c;
}
start_pos = end_pos = buffer;
has_data = true;
while(true) {
swap_in_basic_for_print();
last_char = waitkey(true);
swap_out_basic_after_print();
// Process backspace
char c = last_char;
if ((c == 8) && (end_pos > start_pos)) {
bprintf("%c", c);
end_pos--;
MAP_IN_INPUT_BUFFER;
*end_pos = ' ';
MAP_OUT_INPUT_BUFFER;
continue;
}
// Process chars if we hit enter or ESC
if ((c == 3) || (c == '\r')) {
// Don't go over a full screen minus last line
if (c == '\r') {
if ((end_pos - start_pos) >= (INPUT_BUFFER_SIZE - SCREEN_BYTES_PER_CHAR * SCREEN_WIDTH)) {
continue;
}
}
// Increment by one line
char *before = *screen_ptr;
bprintf("\n");
char *after = *screen_ptr;
if (after <= before) {
after = after + SCREEN_BYTES_PER_CHAR * SCREEN_WIDTH;
}
// Fill in buffer with spaces
unsigned delta = (after - before) / SCREEN_BYTES_PER_CHAR;
MAP_IN_INPUT_BUFFER;
memset(end_pos, ' ', delta);
*end_pos = '\r';
MAP_OUT_INPUT_BUFFER;
end_pos += delta;
// If break was pressed, begin processing data
if (c == 3) {
MAP_IN_INPUT_BUFFER;
c = *start_pos++;
MAP_OUT_INPUT_BUFFER;
if (start_pos > end_pos) {
has_data = false;
}
return c;
}
continue;
}
// Process a normal char
if (end_pos - start_pos >= (INPUT_BUFFER_SIZE - 1)) {
continue;
}
if (c >= ' ') {
if (end_pos >= start_pos + INPUT_BUFFER_SIZE) {
start_pos = start_pos - SCREEN_WIDTH;
}
bprintf("%c", c);
MAP_IN_INPUT_BUFFER;
*end_pos++ = c;
MAP_OUT_INPUT_BUFFER;
}
}
}
void ungetc(char c, byte ignore) {
if (doing_load) {
file_data = c;
has_file_data = TRUE;
return;
}
if (start_pos <= buffer) {
return;
}
has_data = true;
--start_pos;
MAP_IN_INPUT_BUFFER;
*start_pos = c;
MAP_OUT_INPUT_BUFFER;
return;
}
char peek(void) {
char c = getchar();
ungetc(c, stdin);
return c;
}
// Destructively reverses the given list.
static Obj *reverse(Obj *p) {
Obj *ret = Nil;
while (p != Nil) {
Obj *head = p;
p = p->val.cell.cdr;
head->val.cell.cdr = ret;
ret = head;
}
return ret;
}
// Skips the input until newline is found. Newline is one of \r, \r\n or \n.
static void skip_line(void) {
for (;;) {
int c = getchar();
if (c == EOF || c == '\n')
return;
if (c == '\r') {
if (peek() == '\n')
getchar();
return;
}
}
}
// Reads a list. Note that '(' has already been read.
static Obj *read_list(void *root) {
DEFINE3(obj, head, last);
*head = Nil;
for (;;) {
*obj = read_expr(root);
if (!*obj)
error("Unclosed parenthesis\n");
if (*obj == Cparen)
return reverse(*head);
if (*obj == Dot) {
*last = read_expr(root);
if (read_expr(root) != Cparen)
error("Closed parenthesis expected after dot\n");
Obj *ret = reverse(*head);
(*head)->val.cell.cdr = *last;
return ret;
}
*head = cons(root, obj, head);
}
}
// May create a new symbol. If there's a symbol with the same name, it will not create a new symbol
// but return the existing one.
static Obj *intern(void *root, const char *name) {
for (Obj *p = Symbols; p != Nil; p = p->val.cell.cdr)
if (strcmp(name, p->val.cell.car->val.name) == 0)
return p->val.cell.car;
DEFINE1(sym);
*sym = make_symbol(root, name);
Symbols = cons(root, sym, &Symbols);
return *sym;
}
// Reader marcro ' (single quote). It reads an expression and returns (quote <expr>).
static Obj *read_quote(void *root) {
DEFINE2(sym, tmp);
*sym = intern(root, "quote");
*tmp = read_expr(root);
*tmp = cons(root, tmp, &Nil);
*tmp = cons(root, sym, tmp);
return *tmp;
}
static long read_number(long val) {
while (isdigit(peek()))
val = val * 10 + (getchar() - '0');
return val;
}
static Obj *read_symbol(void *root, char c) {
char *buf = (char *)generic_buffer;
buf[0] = c;
int len = 1;
while (isalnum(peek()) || strchr(symbol_chars, peek())) {
if (SYMBOL_MAX_LEN <= len)
error("Symbol name too long\n");
buf[len++] = getchar();
}
buf[len] = '\0';
return intern(root, buf);
}
static Obj *read_expr(void *root) {
for (;;) {
char c = getchar();
if (c == ' ' || c == '\n' || c == '\r' || c == '\t')
continue;
if (c == EOF)
return NULL;
if (c == ';') {
skip_line();
continue;
}
if (c == '(')
return read_list(root);
if (c == ')')
return Cparen;
if (c == '.')
return Dot;
if (c == '\'')
return read_quote(root);
if (isdigit(c))
return make_long(root, read_number(c - '0'));
if (c == '-' && isdigit(peek()))
return make_long(root, -read_number(0));
if (isalpha(c) || strchr(symbol_chars, c))
return read_symbol(root, c);
error("Don't know how to handle %c", c);
}
}
// Prints the given object.
static void print(Obj *obj) {
long tmp;
switch (obj->type) {
case TCELL:
bprintf("(");
for (;;) {
print(obj->val.cell.car);
if (obj->val.cell.cdr == Nil)
break;
if (obj->val.cell.cdr->type != TCELL) {
bprintf(" . ");
print(obj->val.cell.cdr);
break;
}
bprintf(" ");
obj = obj->val.cell.cdr;
}
bprintf(")");
return;
#define CASE(type, ...) \
case type: \
bprintf(__VA_ARGS__); \
return
#define CASE2(type, x, y) \
case type: \
memcpy(generic_buffer, y, sizeof(generic_buffer) - 1); \
generic_buffer[sizeof(generic_buffer)-1] = '\0'; \
bprintf(x, generic_buffer); \
return
#define CASE3(type, x, y) \
case type: \
tmp = y; \
bprintf(x, tmp); \
return
CASE3(TLONG, "%ld", obj->val.value);
CASE2(TSYMBOL, "%s", obj->val.name);
CASE(TPRIMITIVE, "<primitive>");
CASE(TFUNCTION, "<function>");
CASE(TMACRO, "<macro>");
CASE(TMOVED, "<moved>");
CASE(TTRUE, "T");
CASE(TNIL, "()");
#undef CASE
#undef CASE2
default:
error("Bug: print: unknown tag type: %d\n", obj->type);
}
}
// Returns the length of the given list. -1 if it's not a proper list.
static int length(Obj *list) {
int len = 0;
for (; list->type == TCELL; list = list->val.cell.cdr)
len++;
return list == Nil ? len : -1;
}
//======================================================================
// Evaluator
//======================================================================
static Obj *eval(void *root, Obj **env, Obj **obj);
static Obj *find(Obj **env, Obj *sym);
static void add_variable(void *root, Obj **env, Obj **sym, Obj **val) {
// Overwrite existing value
Obj *bind = find(env, *sym);
if (bind) {
bind->val.cell.cdr = *val;
return;
}
DEFINE2(vars, tmp);
*vars = (*env)->val.env.vars;
*tmp = acons(root, sym, val, vars);
(*env)->val.env.vars = *tmp;
}
// Returns a newly created environment frame.
static Obj *push_env(void *root, Obj **env, Obj **vars, Obj **vals) {
DEFINE3(map, sym, val);
*map = Nil;
for (; (*vars)->type == TCELL; *vars = (*vars)->val.cell.cdr, *vals = (*vals)->val.cell.cdr) {
if ((*vals)->type != TCELL)
error("Cannot apply function: number of arguments do not match\n");
*sym = (*vars)->val.cell.car;
*val = (*vals)->val.cell.car;
*map = acons(root, sym, val, map);
}
if (*vars != Nil)
*map = acons(root, vars, vals, map);
return make_env(root, map, env);
}
// Evaluates the list elements from head and returns the last return value.
static Obj *progn(void *root, Obj **env, Obj **list) {
DEFINE2(lp, r);
for (*lp = *list; *lp != Nil; *lp = (*lp)->val.cell.cdr) {
*r = (*lp)->val.cell.car;
*r = eval(root, env, r);
}
return *r;
}
// Evaluates all the list elements and returns their return values as a new list.
static Obj *eval_list(void *root, Obj **env, Obj **list) {
DEFINE4(head, lp, expr, result);
*head = Nil;
for (lp = list; *lp != Nil; *lp = (*lp)->val.cell.cdr) {
*expr = (*lp)->val.cell.car;
*result = eval(root, env, expr);
*head = cons(root, result, head);
}
return reverse(*head);
}
static bool is_list(Obj *obj) {
return obj == Nil || obj->type == TCELL;
}
static Obj *apply_func(void *root, Obj **env, Obj **fn, Obj **args) {
DEFINE3(params, newenv, body);
*params = (*fn)->val.func.params;
*newenv = (*fn)->val.func.env;
*newenv = push_env(root, newenv, params, args);
*body = (*fn)->val.func.body;
return progn(root, newenv, body);
}
typedef Obj *(*PrimitiveFuncPtr)(void *, Obj **, Obj **);
// Apply fn with args.
static Obj *apply(void *root, Obj **env, Obj **fn, Obj **args) {
if (!is_list(*args))
error("Argument must be a list\n");
if ((*fn)->type == TPRIMITIVE)
return (Obj *) ((PrimitiveFuncPtr) (*fn)->val.fn)(root, env, args);
if ((*fn)->type == TFUNCTION) {
DEFINE1(eargs);
*eargs = eval_list(root, env, args);
return apply_func(root, env, fn, eargs);
}
error("Not supported\n");
}
// Searches for a variable by symbol. Returns null if not found.
static Obj *find(Obj **env, Obj *sym) {
for (Obj *p = *env; p != Nil; p = p->val.env.up) {
for (Obj *cell = p->val.env.vars; cell != Nil; cell = cell->val.cell.cdr) {
Obj *bind = cell->val.cell.car;
if (sym == bind->val.cell.car)
return bind;
}
}
return NULL;
}
// Expands the given macro application form.
static Obj *macroexpand(void *root, Obj **env, Obj **obj) {
if ((*obj)->type != TCELL || (*obj)->val.cell.car->type != TSYMBOL)
return *obj;
DEFINE3(bind, macro, args);
*bind = find(env, (*obj)->val.cell.car);
if (!*bind || (*bind)->val.cell.cdr->type != TMACRO)
return *obj;
*macro = (*bind)->val.cell.cdr;
*args = (*obj)->val.cell.cdr;
return apply_func(root, env, macro, args);
}
// Evaluates the S expression.
static Obj *eval(void *root, Obj **env, Obj **obj) {
switch ((*obj)->type) {
case TLONG:
case TPRIMITIVE:
case TFUNCTION:
case TTRUE:
case TNIL:
// Self-evaluating objects
return *obj;
case TSYMBOL: {
// Variable
Obj *bind = find(env, *obj);
if (!bind)
error2("Undefined symbol: %s\n", (*obj)->val.name);
return bind->val.cell.cdr;
}
case TCELL: {
// Function application form
DEFINE3(fn, expanded, args);
*expanded = macroexpand(root, env, obj);
if (*expanded != *obj)
return eval(root, env, expanded);
*fn = (*obj)->val.cell.car;
*fn = eval(root, env, fn);
*args = (*obj)->val.cell.cdr;
if ((*fn)->type != TPRIMITIVE && (*fn)->type != TFUNCTION)
error("The head of a list must be a function\n");
return apply(root, env, fn, args);
}
default:
error("Bug: eval: known tag type: %d\n", (*obj)->type);
}
}
//======================================================================
// Primitive functions and special forms
//======================================================================
// 'expr
static Obj *prim_quote(void *root, Obj **env, Obj **list) {
if (length(*list) != 1) {
error("Malformed %s\n", "quote");
}
return (*list)->val.cell.car;
}
// (cons expr expr)
static Obj *prim_cons(void *root, Obj **env, Obj **list) {
if (length(*list) != 2) {
error("Malformed %s\n", "cons");
}
Obj *cell = eval_list(root, env, list);
cell->val.cell.cdr = cell->val.cell.cdr->val.cell.car;
return cell;
}