-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterp.c
1960 lines (1795 loc) · 45.1 KB
/
interp.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
#include"interp.h"
#pragma region
#define CAR(x) ((Object)x).ptr[0]
#define CDR(x) ((Object)x).ptr[1]
#define CAAR(x) CAR(CAR(x))
#define CAAAR(x) CAR(CAR(CAR(x)))
#define CAAAAR(x) CAR(CAR(CAR(CAR(x))))
#define CDAAAR(x) CDR(CAR(CAR(CAR(x))))
#define CDAAR(x) CDR(CAR(CAR(x)))
#define CADAAR(x) CAR(CDR(CAR(CAR(x))))
#define CDDAAR(x) CDR(CDR(CAR(CAR(x))))
#define CDAR(x) CDR(CAR(x))
#define CADAR(x) CAR(CDR(CAR(x)))
#define CAADAR(x) CAR(CAR(CDR(CAR(x))))
#define CDADAR(x) CDR(CAR(CDR(CAR(x))))
#define CDDAR(x) CDR(CDR(CAR(x)))
#define CADDAR(x) CAR(CDR(CDR(CAR(x))))
#define CDDDAR(x) CDR(CDR(CDR(CAR(x))))
#define CADR(x) CAR(CDR(x))
#define CAADR(x) CAR(CAR(CDR(x)))
#define CAAADR(x) CAR(CAR(CAR(CDR(x))))
#define CDAADR(x) CDR(CAR(CAR(CDR(x))))
#define CDADR(x) CDR(CAR(CDR(x)))
#define CADADR(x) CAR(CDR(CAR(CDR(x))))
#define CDDADR(x) CDR(CDR(CAR(CDR(x))))
#define CDDR(x) CDR(CDR(x))
#define CADDR(x) CAR(CDR(CDR(x)))
#define CAADDR(x) CAR(CAR(CDR(CDR(x))))
#define CDADDR(x) CDR(CAR(CDR(CDR(x))))
#define CDDDR(x) CDR(CDR(CDR(x)))
#define CADDDR(x) CAR(CDR(CDR(CDR(x))))
#define CDDDDR(x) CDR(CDR(CDR(CDR(x))))
#pragma endregion
#define check_list_one(x) ((x).tag == PAIR_TAG)
#define check_list_two(x) check_list_one(x) && (CDR(x).tag == PAIR_TAG)
#define check_list_three(x) check_list_two(x) && (CDDR(x).tag == PAIR_TAG)
#define _make_nil() (Object){.tag=NIL_TAG};
inline static uintptr_t align_to_multiple(uintptr_t alignment, uintptr_t offset);
void load_n_run(int argc, char *argv[]);
void initialize(int argc, char *argv[]);
void end_cont();
void apply_cont();
void _s_write(FILE *fptr, Object obj);
Object make_num(int n);
Object make_pair();
Object alloc_pair();
Object make_vec(int n);
Object alloc_vec(int n);
Object copy_str(const char *s);
Object alloc_str(int sz);
Object make_fn(void (*fn_ptr)());
Object make_char(char c);
Object make_bool(bool b);
Object make_token(char tk);
Object make_fptr(const char *s, const char *mode);
void _strncpy(char *dest, const char *source, int len);
void print_all_token_cont();
void print_all_token();
void s_load_cont();
void s_read_file_cont_1();
void s_read_file_cont();
void s_read_file();
void parse();
void peek_token_cont();
void peek_token();
char peek_char(FILE *fptr);
void next_token();
int lex_num(FILE *fptr, int base);
void lex_comment_block(FILE *fptr);
bool is_num_char(char c);
bool is_id_char(char c);
void parse_cont();
void parse_abrv_cont();
void parse_list();
void parse_dotted_cont();
void parse_list_cont();
void parse_list_cont_1();
void parse_list_cont_2();
Object add_symbol(const char *s);
bool eq(Object x, Object y);
void append_mut(Object xs, Object ys);
Object assq(Object x, Object xs);
void s_load();
void s_eval();
void s_apply();
void s_exit();
void s_cmd_ln();
void s_getpid();
void s_file_exists_pred();
void s_system();
void s_file_pred();
void s_curr_stdout();
void s_curr_stderr();
void s_fopen();
void s_fclose();
void s_eof_obj_pred();
void s_read();
void s_write();
void s_newline();
void s_read_char();
void s_unread_char();
void s_write_char();
void s_writeln();
void s_null_pred();
void s_proc_pred();
void s_boolean_pred();
void s_char_pred();
void s_int_to_char();
void s_char_to_int();
void s_integer_pred();
void s_eq();
void s_add();
void s_sub();
void s_mul();
void s_div();
void s_mod();
void s_le();
void s_lt();
void s_eqn();
void s_ge();
void s_gt();
void s_ash();
void s_band();
void s_ior();
void s_pair_pred();
void s_cons();
void s_car();
void s_cdr();
void s_set_car();
void s_set_cdr();
void s_sym_pred();
void s_sym_to_str();
void s_str_to_sym();
void s_str_pred();
void s_make_str();
void s_str_len();
void s_str_ref();
void s_str_set();
void s_vec_pred();
void s_make_vec();
void s_vec_len();
void s_vec_ref();
void s_vec_set();
void eval();
void if_cont();
void seq_cont();
void assign_cont();
void app_cont();
void apply_macro_clos();
void app_macro_clos_cont_1();
void apply_clos();
void gc_flip(int sz);
void collect_scan_ptr();
Object apply_env(Object x, Object env);
struct {
bool is_deleted;
Object obj;
} GC_CELLS[HEAP_SIZE];
Object VAL = _make_nil();
Object EXP = _make_nil();
Object CONT = _make_nil();
Object ENV = _make_nil();
Object PROC = _make_nil();
Object GLOBALS = _make_nil();
Object ARGV = _make_nil();
Object NIL = _make_nil();
Object TOK = _make_nil();
Object SYMBOLS = _make_nil();
Object IF = _make_nil();
Object LAMBDA = _make_nil();
Object BEGIN = _make_nil();
Object SET = _make_nil();
Object DEFMACRO = _make_nil();
Object QUOTE = _make_nil();
Object QUASIQUOTE = _make_nil();
Object UNQUOTE = _make_nil();
Object UNQUOTE_SPLICING = _make_nil();
Object *free_ptr, *scan_ptr;
Object *fromspace_start, *fromspace_end, *tospace_start, *tospace_end;
char TOKEN_BUF[512];
void (*NEXT)();
// implementation start
inline static uintptr_t align_to_multiple(uintptr_t alignment, uintptr_t offset){
return (offset + (alignment - 1)) & -alignment;
}
void load_n_run(int argc, char *argv[]){
assert(argc >= 2);
initialize(argc, argv);
VAL = copy_str(argv[1]);
EXP = make_pair();
CAR(EXP) = VAL;
VAL = EXP;
NEXT = s_load;
for(;;){
NEXT();
}
}
void _s_write(FILE *fptr, Object obj){
switch (obj.tag)
{
case FX_TAG:{
fprintf(fptr, "%d", obj.fx);
break;
}
case BOOL_TAG:{
if(obj.b){
fprintf(fptr, "#t");
}
else{
fprintf(fptr, "#f");
}
break;
}
case CHAR_TAG:{
fprintf(fptr, "#\\%c", obj.ch);
break;
}
case NIL_TAG:{
fprintf(fptr, "()");
break;
}
case PAIR_TAG:{
fprintf(fptr, "(");
_s_write(fptr, obj.ptr[0]);
obj = obj.ptr[1];
while(obj.tag == PAIR_TAG){
fprintf(fptr, " ");
_s_write(fptr, obj.ptr[0]);
obj = obj.ptr[1];
}
if(obj.tag == NIL_TAG){
fprintf(fptr, ")");
}
else{
fprintf(fptr, " . ");
_s_write(fptr, obj);
fprintf(fptr, ")");
}
break;
}
case STR_TAG:{
fprintf(fptr, "\"");
for(int i = 0; i < obj.len; ++i){
if(obj.s[i] == '\n'){
fprintf(fptr, "\\n");
}
else if(obj.s[i] == '\t'){
fprintf(fptr, "\\t");
}
else if(obj.s[i] == '\\'){
fprintf(fptr, "\\\\");
}
else if(obj.s[i] == '"'){
fprintf(fptr, "\\\"");
}
else if(obj.s[i] == '\0'){
fprintf(fptr, "\\0");
}
else{
fprintf(fptr, "%c", obj.s[i]);
}
}
fprintf(fptr, "\"");
break;
}
case SYM_TAG:{
fprintf(fptr, "%s", obj.s);
break;
}
case VEC_TAG:{
fprintf(fptr, "#(");
for(int i = 0; i < obj.len; ++i){
_s_write(fptr, obj.ptr[i]);
if(i != obj.len - 1){
fprintf(fptr, " ");
}
}
fprintf(fptr, ")");
break;
}
case TOK_TAG:{
fprintf(fptr, "<TOKEN %c>", obj.tk);
break;
}
case FN_TAG:{
fprintf(fptr, "<BUILT-IN-FN %p>", obj.cont);
break;
}
case FILE_TAG:{
fprintf(fptr, "<FILE %p>", obj.fp);
break;
}
case EOF_TAG:{
fprintf(fptr, "!eof");
break;
}
default:
fprintf(stderr, "unknown tag: %d\n", obj.tag);
assert(false);
break;
}
}
Object make_num(int n){
return (Object){.tag = FX_TAG, .fx = n};
}
Object make_fn(void (*fn_ptr)()){
return (Object){.tag=FN_TAG, .cont = fn_ptr};
}
Object make_char(char c){
return (Object){.tag=CHAR_TAG, .ch = c};
}
Object make_bool(bool b){
return (Object){.tag=BOOL_TAG, .b = b};
}
Object make_token(char tk){
return (Object){.tag=TOK_TAG, .tk = tk};
}
Object make_pair(){
gc_flip(3);
return alloc_pair();
}
Object alloc_pair(){
Object tmp = {.tag = PAIR_TAG, .len = 2, .ptr = free_ptr + 1};
free_ptr = free_ptr + 3;
tmp.ptr[0] = NIL;
tmp.ptr[1] = NIL;
tmp.ptr[-1] = tmp;
return tmp;
}
Object make_vec(int n){
gc_flip(1 + n);
return alloc_vec(n);
}
Object alloc_vec(int n){
Object tmp = {.tag = VEC_TAG, .len = n, .ptr = free_ptr + 1};
free_ptr = free_ptr + n + 1;
tmp.ptr[-1] = tmp;
return tmp;
}
Object make_str(char c, int n){
int sz = 1 + align_to_multiple(sizeof(Object), n + 1) / sizeof(Object);
gc_flip(sz);
Object tmp = alloc_str(n);
for(int i = 0; i < n; ++i){
tmp.s[i] = c;
}
return tmp;
}
Object copy_str(const char *s){
int n = strlen(s);
int sz = 1 + align_to_multiple(sizeof(Object),n + 1) / sizeof(Object);
gc_flip(sz);
Object tmp = alloc_str(n);
strcpy(tmp.s, s);
return tmp;
}
Object alloc_str(int len){
Object tmp = {.tag = STR_TAG, .len = len, .s = (char*)(free_ptr+1)};
tmp.s[tmp.len] = '\0';
*free_ptr = tmp;
int offset = align_to_multiple(sizeof(Object), tmp.len + 1) / sizeof(Object);
free_ptr = free_ptr + 1 + offset;
return tmp;
}
bool eq(Object x, Object y){
if(x.tag != y.tag){
return false;
}
else if(x.tag == NIL_TAG){
return true;
}
else if(x.tag == FX_TAG){
return x.fx == y.fx;
}
else if(x.tag == CHAR_TAG){
return x.ch == y.ch;
}
else if(x.tag == BOOL_TAG){
return x.b == y.b;
}
else if((x.tag == SYM_TAG) || (x.tag == STR_TAG)){
return x.s == y.s;
}
else if(x.tag == VEC_TAG || x.tag == PAIR_TAG){
return x.ptr == y.ptr;
}
else if(x.tag == FILE_TAG){
return x.fp == y.fp;
}
else{
fprintf(stderr, "unknown tag: %d\n", x.tag);
assert(false);
}
}
Object add_symbol(const char *s){
Object tmp = SYMBOLS;
while(tmp.tag == PAIR_TAG){
if(strcmp(CAR(tmp).s, s) == 0){
VAL = CAR(tmp);
return VAL;
}
tmp = CDR(tmp);
}
PROC = make_pair();
CDR(PROC) = SYMBOLS;
SYMBOLS = PROC;
VAL = copy_str(s);
VAL.tag = SYM_TAG;
CAR(SYMBOLS) = VAL;
return VAL;
}
void append_mut(Object xs, Object ys){
assert(xs.tag == PAIR_TAG);
while(CDR(xs).tag == PAIR_TAG){
xs = CDR(xs);
}
assert(xs.tag == PAIR_TAG);
CDR(xs) = ys;
}
Object assq(Object x, Object xs){
while (xs.tag == PAIR_TAG){
if(eq(CAAR(xs), x)){
return CAR(xs);
}
xs = CDR(xs);
}
return make_bool(false);
}
void add_prim(const char *s, void (*pr)(void)){
PROC = make_pair();
CDR(PROC) = GLOBALS;
GLOBALS = PROC;
PROC = make_pair();
CAR(GLOBALS) = PROC;
PROC = add_symbol(s);
CAAR(GLOBALS) = PROC;
CDAR(GLOBALS) = make_fn(pr);
}
void apply_cont(){
assert(CONT.tag == VEC_TAG);
assert(CONT.len >= 1);
assert(CONT.ptr[0].tag == FN_TAG);
NEXT = CONT.ptr[0].cont;
}
void end_cont(){
exit(0);
}
void initialize(int argc, char *argv[]){
assert(HEAP_SIZE % 2 == 0);
free_ptr = calloc(HEAP_SIZE * 2, sizeof(Object));
fromspace_start = free_ptr;
fromspace_end = fromspace_start + HEAP_SIZE;
tospace_start = fromspace_end;
tospace_end = tospace_start + HEAP_SIZE;
// initialize argv
for(int i = argc-1; i > 0; --i){
PROC = make_pair();
VAL = copy_str(argv[i]);
CDR(PROC) = ARGV;
CAR(PROC) = VAL;
ARGV = PROC;
}
CONT = make_vec(1);
CONT.ptr[0] = make_fn(end_cont);
IF = add_symbol("if");
LAMBDA = add_symbol("lambda");
BEGIN = add_symbol("begin");
SET = add_symbol("set!");
DEFMACRO = add_symbol("define-macro");
QUOTE = add_symbol("quote");
QUASIQUOTE = add_symbol("quasiquote");
UNQUOTE = add_symbol("unquote");
UNQUOTE_SPLICING = add_symbol("unquote-splicing");
add_prim("apply", s_apply);
add_prim("boolean?",s_boolean_pred);
add_prim("char?",s_char_pred);
add_prim("integer->char",s_int_to_char);
add_prim("char->integer",s_char_to_int);
add_prim("integer?",s_integer_pred);
add_prim("eq?", s_eq);
add_prim("+", s_add);
add_prim("-",s_sub);
add_prim("*", s_mul);
add_prim("div", s_div);
add_prim("mod", s_mod);
add_prim("<",s_lt);
add_prim("<=",s_le);
add_prim("=",s_eqn);
add_prim(">=",s_ge);
add_prim(">",s_gt);
add_prim("ash", s_ash);
add_prim("bitwise-and", s_band);
add_prim("bitwise-ior", s_ior);
add_prim("writeln", s_writeln);
add_prim("write", s_write);
add_prim("read-char", s_read_char);
add_prim("unread-char", s_unread_char);
add_prim("write-char", s_write_char);
add_prim("newline", s_newline);
add_prim("read", s_read);
add_prim("eof-object?", s_eof_obj_pred);
add_prim("port?", s_file_pred);
add_prim("current-output-port", s_curr_stdout);
add_prim("current-error-port", s_curr_stderr);
add_prim("fopen", s_fopen);
add_prim("fclose", s_fclose);
add_prim("exit", s_exit);
add_prim("command-line", s_cmd_ln);
add_prim("get-process-id", s_getpid);
add_prim("file-exists?", s_file_exists_pred);
add_prim("system", s_system);
add_prim("load", s_load);
add_prim("eval", s_eval);
add_prim("procedure?", s_proc_pred);
add_prim("null?", s_null_pred);
add_prim("pair?", s_pair_pred);
add_prim("cons", s_cons);
add_prim("car", s_car);
add_prim("cdr", s_cdr);
add_prim("set-car!", s_set_car);
add_prim("set-cdr!", s_set_cdr);
add_prim("symbol?", s_sym_pred);
add_prim("symbol->string", s_sym_to_str);
add_prim("string->symbol", s_str_to_sym);
add_prim("string?", s_str_pred);
add_prim("make-string", s_make_str);
add_prim("string-length", s_str_len);
add_prim("string-set!", s_str_set);
add_prim("string-ref", s_str_ref);
add_prim("vector?", s_vec_pred);
add_prim("make-vector", s_make_vec);
add_prim("vector-length", s_vec_len);
add_prim("vector-ref", s_vec_ref);
add_prim("vector-set!", s_vec_set);
}
// #(_ car cont)
void s_read_file_cont_1(){
PROC = make_pair();
CAR(PROC) = CONT.ptr[1];
CDR(PROC) = VAL;
VAL = PROC;
CONT = CONT.ptr[CONT.len-1];
NEXT = apply_cont;
}
// #(_ fp cont)
void s_read_file_cont(){
if(VAL.tag == EOF_TAG){
CONT = CONT.ptr[CONT.len-1];
VAL = NIL;
NEXT = apply_cont;
}
else{
CONT.ptr[0] = make_fn(s_read_file_cont_1);
PROC = CONT.ptr[1];
CONT.ptr[1] = VAL;
VAL = PROC;
NEXT = s_read_file;
}
}
void s_read_file(){
assert(VAL.tag == FILE_TAG);
PROC = make_vec(3);
PROC.ptr[0] = make_fn(s_read_file_cont);
PROC.ptr[1] = VAL;
PROC.ptr[PROC.len-1] = CONT;
CONT = PROC;
NEXT = parse;
}
void s_load_cont(){
EXP = make_pair();
CONT = CONT.ptr[CONT.len - 1];
CAR(EXP) = BEGIN;
CDR(EXP) = VAL;
ENV = GLOBALS;
NEXT = eval;
}
void s_load(){
assert(check_list_one(VAL));
assert(CAR(VAL).tag == STR_TAG);
VAL = make_fptr(CAR(VAL).s, "r");
PROC = make_vec(2);
PROC.ptr[0] = make_fn(s_load_cont);
PROC.ptr[PROC.len-1] = CONT;
CONT = PROC;
NEXT = s_read_file;
}
Object make_fptr(const char *s, const char *mode){
FILE *fptr = fopen(s, mode);
if(fptr == NULL){
fprintf(stderr, "error file: %s\n", s);
assert(fptr != NULL);
}
return (Object){.tag = FILE_TAG, .fp = fptr};
}
void _strncpy(char *dest, const char *source, int len){
for(len = len - 1; len >= 0; --len){
dest[len] = source[len];
}
}
void print_all_token_cont(){
if(VAL.tag == TOK_TAG && VAL.tk == EOF){
CONT = CONT.ptr[CONT.len-1];
NEXT = apply_cont;
}
else{
_s_write(stdout, VAL);
putc('\n', stdout);
VAL = CONT.ptr[1];
NEXT = next_token;
}
}
void print_all_token(){
assert(VAL.tag == FILE_TAG);
PROC = make_vec(3);
PROC.ptr[0] = make_fn(print_all_token);
PROC.ptr[1] = VAL;
PROC.ptr[PROC.len-1] = CONT;
CONT = PROC;
NEXT = next_token;
}
bool is_id_char(char c){
const char extended_set[] = "!$%&*+-./:<=>?@^_~";
if((('a' <= c) && (c <= 'z'))
|| (('A' <= c) && (c <= 'Z'))
|| (('0' <= c) && (c <= '9'))){
return true;
}
for(int i = 0; i < sizeof(extended_set)/sizeof(extended_set[0]); ++i) {
if(extended_set[i] == c)
return true;
}
return false;
}
bool is_num_char(char c){
return ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
}
int lex_num(FILE *fptr, int base){
assert(base == 2 || base == 8 || base == 10 || base == 16);
int x = 0;
char c = getc(fptr);
while(is_num_char(c)){
x = x * base;
if('0' <= c && c <= '9'){
c = c - '0';
}
else if ('A' <= c && c <= 'F'){
c = c - 'A' + 10;
}
else if ('a' <= c && c <= 'f'){
c = c - 'a' + 10;
}
x = x + c;
c = getc(fptr);
}
ungetc(c, fptr);
return x;
}
void lex_comment_block(FILE *fptr){
while(true){
char c = getc(fptr);
assert(c != EOF);
if(c == '|' && peek_char(fptr) == '#'){
getc(fptr);
return;
}
else if(c == '#' && peek_char(fptr) == '|'){
getc(fptr);
lex_comment_block(fptr);
return;
}
}
}
// #(_ cont)
void peek_token_cont(){
TOK = VAL;
CONT = CONT.ptr[CONT.len-1];
NEXT = apply_cont;
}
// VAL = fptr
void peek_token(){
assert(VAL.tag == FILE_TAG);
if(TOK.tag == NIL_TAG){
PROC = make_vec(2);
PROC.ptr[0] = make_fn(peek_token_cont);
PROC.ptr[1] = CONT;
CONT = PROC;
NEXT = next_token;
return;
}
else{
VAL = TOK;
NEXT = apply_cont;
return;
}
}
char peek_char(FILE *fptr){
char ch = getc(fptr);
ungetc(ch, fptr);
return ch;
}
// VAL = fptr
void next_token(){
if(TOK.tag != NIL_TAG){
VAL = TOK;
TOK = NIL;
NEXT = apply_cont;
return;
}
assert(VAL.tag == FILE_TAG);
FILE* fptr = VAL.fp;
int c = getc(fptr);
if(c == EOF){
VAL = (Object){.tag=EOF_TAG};
NEXT = apply_cont;
}
else if(c == ' ' || c == '\n' || c == '\t'){
return;
}
else if(c == ';'){
while(c != EOF && c != '\n'){
c = getc(fptr);
}
}
else if(c == '(' || c == '['){
VAL = make_token('(');
NEXT = apply_cont;
}
else if(c == ')' || c == ']'){
VAL = make_token(')');
NEXT = apply_cont;
}
else if(c == '.' || c == '\'' || c == '`'){
VAL = make_token(c);
NEXT = apply_cont;
}
else if(c == ',' && peek_char(fptr) == '@'){
getc(fptr);
VAL = make_token('@');
NEXT = apply_cont;
}
else if(c == ','){
VAL = make_token(',');
NEXT = apply_cont;
}
else if(c == '#' && peek_char(fptr) == 't'){
getc(fptr);
VAL = make_bool(true);
NEXT = apply_cont;
}
else if(c == '#' && peek_char(fptr) == 'f'){
getc(fptr);
VAL = make_bool(false);
NEXT = apply_cont;
}
else if(c == '#' && peek_char(fptr) == '('){
getc(fptr);
VAL = make_token(VEC_TAG);
NEXT = apply_cont;
}
else if(c == '#' && peek_char(fptr) == '\\'){
getc(fptr);
c = getc(fptr);
VAL = make_char(c);
NEXT = apply_cont;
}
else if(c == '#' && peek_char(fptr) == 'b'){
getc(fptr);
VAL = make_num(lex_num(fptr, 2));
NEXT = apply_cont;
}
else if(c == '#' && peek_char(fptr) == 'o'){
getc(fptr);
VAL = make_num(lex_num(fptr, 8));
NEXT = apply_cont;
}
else if(c == '#' && peek_char(fptr) == 'x'){
getc(fptr);
VAL = make_num(lex_num(fptr, 16));
NEXT = apply_cont;
}
else if(c == '#' && peek_char(fptr) == '|'){
getc(fptr);
lex_comment_block(fptr);
}
else if(c == '-' && '0' <= peek_char(fptr) && peek_char(fptr) <= '9'){
VAL = make_num(-lex_num(fptr, 10));
NEXT = apply_cont;
}
else if('0' <= c && c <= '9'){
ungetc(c, fptr);
VAL = make_num(lex_num(fptr, 10));
NEXT = apply_cont;
}
else if(c == '"'){
int i = 0;
c = getc(fptr);
while(c != '"'){
if(c == '\\'){
c = getc(fptr);
if(c == 'n'){
c = '\n';
}
else if(c == 't'){
c = '\t';
}
else if(c == '\\'){
c = '\\';
}
else if(c == '"'){
c = '"';
}
else{
assert(false);
}
}
TOKEN_BUF[i++] = c;
c = getc(fptr);
}
TOKEN_BUF[i] = '\0';
VAL = copy_str(TOKEN_BUF);
NEXT = apply_cont;
}
else if(is_id_char(c)){
int i = 0;
TOKEN_BUF[i++] = c;
c = getc(fptr);
while(is_id_char(c)){
TOKEN_BUF[i++] = c;
c = getc(fptr);
}
TOKEN_BUF[i] = '\0';
ungetc(c, fptr);
VAL = add_symbol(TOKEN_BUF);
NEXT = apply_cont;
return;
}
else{
fprintf(stderr, "unknown: %c\n", c);
assert(false);
}
}
// #(_ _ cont)
void parse_vector_cont(){
CONT = CONT.ptr[CONT.len-1];
int len = 0;
for(Object tmp = VAL; tmp.tag == PAIR_TAG; tmp = CDR(tmp)){
++len;
}
PROC = VAL;
VAL = make_vec(len);
for(int i = 0; i < len; ++i){
VAL.ptr[i] = CAR(PROC);
PROC = CDR(PROC);
}
NEXT = apply_cont;
}
// #(_ fp cont)
void parse_dotted_cont(){
// swap VAL and fp
PROC = VAL;
VAL = CONT.ptr[1];
CONT.ptr[1] = PROC;
next_token();
VAL = CONT.ptr[1];
CONT = CONT.ptr[CONT.len - 1];
NEXT = apply_cont;
}
// #(_ car cont)
void parse_list_cont_2(){
PROC = make_pair();
CAR(PROC) = CONT.ptr[1];
CDR(PROC) = VAL;
CONT = CONT.ptr[CONT.len-1];
VAL = PROC;
NEXT = apply_cont;
}
// #(_ fp cont)
void parse_list_cont_1(){
PROC = CONT.ptr[1];
CONT.ptr[0] = make_fn(parse_list_cont_2);
CONT.ptr[1] = VAL;
VAL = PROC;
NEXT = parse_list;
}
// #(_ fp cont)
void parse_list_cont(){
if(VAL.tag == TOK_TAG && VAL.tk == ')'){
TOK = NIL;
VAL = NIL;
CONT = CONT.ptr[CONT.len-1];
NEXT = apply_cont;
}
else if(VAL.tag == TOK_TAG && VAL.tk == '.'){
VAL = CONT.ptr[1];
TOK = NIL;
VAL = CONT.ptr[1];
CONT.ptr[0] = make_fn(parse_dotted_cont);
NEXT = parse;
}
else{
VAL = CONT.ptr[1];
CONT.ptr[0] = make_fn(parse_list_cont_1);
NEXT = parse;
}
}
void parse_list(){
assert(VAL.tag == FILE_TAG);
PROC = make_vec(3);
PROC.ptr[0] = make_fn(parse_list_cont);
PROC.ptr[1] = VAL;
PROC.ptr[PROC.len-1] = CONT;
CONT = PROC;
NEXT = peek_token;
}
// #(_ abrv cont)
void parse_abrv_cont(){
PROC = make_pair();