-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb00t.c
2381 lines (2030 loc) · 49.8 KB
/
b00t.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
/*
* l1sp - library for basic in-process lisp scripting
*
* this file is part of the makeutil package:
* https://github.com/alanpost/makeutil
*
* this file is hereby placed in the public domain.
*/
#define _LARGEFILE_SOURCE 1
#define _LARGEFILE64_SOURCE 1
#define _FILE_OFFSET_BITS 64
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <setjmp.h>
#include <ctype.h>
#include <errno.h>
/*
* the default size of one semispace, which
* is the amount of space available for
* objects. actual memory used is twice this,
* as we use a copying garbage collector.
*/
#ifndef L1SP_SEMISPACE
#define L1SP_SEMISPACE 32768
#endif
/* the size of both the input buffer and the
* output buffer. is also the maximum size
* of a symbol.
*/
#ifndef L1SP_IOBUFFER
#define L1SP_IOBUFFER 256
#endif
/*
* memory alignment of allocated objects.
* this should work for most platforms.
*/
#ifndef L1SP_ALIGN
#define L1SP_ALIGN 16
#endif
/*
* number of bytes in a string formated
* hexadecimal number.
*/
#ifndef L1SP_HEX
#define L1SP_HEX 16
#endif
/*
* number of bytes in a string formated
* octal number.
*/
#ifndef L1SP_OCTAL
#define L1SP_OCTAL 3
#endif
/*
* symbolic expression tags. this value
* is stored in |l1_tag| for each object.
*/
#define L1SP_NIL 0x00U
#define L1SP_FORM0 0x01U
#define L1SP_PROC0 0x02U
#define L1SP_FORMN 0x03U
#define L1SP_PROCN 0x04U
#define L1SP_MACRO 0x05U
#define L1SP_LAMBDA 0x06U
#define L1SP_PAIR 0x07U
#define L1SP_SYMBOL 0x08U
#define L1SP_FORWARD 0x09U
/*
* every type in the interpreter
*/
typedef struct l1sp_pair l1sp_nil;
typedef struct l1sp_func l1sp_func;
typedef struct l1sp_pair l1sp_closure;
typedef struct l1sp_pair l1sp_pair;
typedef struct l1sp_symbol l1sp_symbol;
typedef struct l1sp_forward l1sp_forward;
typedef struct l1sp_forward l1sp;
/*
* forward type. any type is converted to
* a forward in |gc|, so it is also the
* smallest type.
*/
struct l1sp_forward {
unsigned l1_tag;
l1sp *l1_forward;
};
struct l1sp_pair {
unsigned l1_tag;
l1sp *l1_car;
l1sp *l1_cdr;
};
struct l1sp_symbol {
unsigned l1_tag;
char l1_data[1]; /*room for nil pointer*/
};
struct l1sp_func {
unsigned l1_tag;
l1sp *(*l1_func)();
};
typedef struct l1sp_ctx {
char *l1_semispace[2], /* copying garbage collector */
*l1_iobuffer, /* i/o buffer */
*l1_ioname; /* i/o filename */
size_t l1_sscur, /* semispace access */
l1_sssize,
l1_ssalloc,
l1_iosize, /* i/o buffer access */
l1_ioalloc;
int l1_ch, /* lookahead character */
l1_iofd; /* i/o file descriptor */
void (*l1_exit)(struct l1sp_ctx*), /* non-error exit */
(*l1_die)(struct l1sp_ctx*,
char*,
l1sp*), /* fatal error */
(*l1_nomem)(struct l1sp_ctx*,
size_t); /* out of memory */
ssize_t (*l1_read)(int,
void*,
size_t), /* read data from fd */
(*l1_write)(int,
const void*,
size_t); /* write data to fd */
l1sp *l1_nil, /* cache frequently accessed variables */
*l1_t,
*l1_quote,
*l1_quasiquote,
*l1_unquote,
*l1_unquote_splicing,
*l1_root, /* root pointers */
*l1_oblist, /* object list */
*l1_evlist; /* current environments */
l1sp_nil _l1_nil;
} l1sp_ctx;
/*
* static initialization. only for use
* in tracking root pointers.
*/
#define L1SP_CONS(x, y) { L1SP_PAIR, x, y }
/*
* protect stack variables during garbage collection
*/
#define L1SP_PROT1_DECL(g) \
l1sp_pair _p0=L1SP_CONS((g)->l1_nil, (g)->l1_root); \
l1sp *p0=(l1sp*)&_p0
#define L1SP_PROT2_DECL(g) \
l1sp_pair _p0=L1SP_CONS((g)->l1_nil, (g)->l1_root), \
_p1=L1SP_CONS((g)->l1_nil, (l1sp*)&_p0); \
l1sp *p0=(l1sp*)&_p0, \
*p1=(l1sp*)&_p1
#define L1SP_PROT3_DECL(g) \
l1sp_pair _p0=L1SP_CONS((g)->l1_nil, (g)->l1_root), \
_p1=L1SP_CONS((g)->l1_nil, (l1sp*)&_p0), \
_p2=L1SP_CONS((g)->l1_nil, (l1sp*)&_p1); \
l1sp *p0=(l1sp*)&_p0, \
*p1=(l1sp*)&_p1, \
*p2=(l1sp*)&_p2
#define L1SP_PROT4_DECL(g) \
l1sp_pair _p0=L1SP_CONS((g)->l1_nil, (g)->l1_root), \
_p1=L1SP_CONS((g)->l1_nil, (l1sp*)&_p0), \
_p2=L1SP_CONS((g)->l1_nil, (l1sp*)&_p1), \
_p3=L1SP_CONS((g)->l1_nil, (l1sp*)&_p2); \
l1sp *p0=(l1sp*)&_p0, \
*p1=(l1sp*)&_p1, \
*p2=(l1sp*)&_p2, \
*p3=(l1sp*)&_p3
#define l1sp_prot1(g, l0) \
l1sp_rplaca(p0, (l1sp*)&l0); \
(g)->l1_root=p0
#define l1sp_prot2(g, l0, l1) \
l1sp_rplaca(p0, (l1sp*)&l0); \
l1sp_rplaca(p1, (l1sp*)&l1); \
(g)->l1_root=p1
#define l1sp_prot3(g, l0, l1, l2) \
l1sp_rplaca(p0, (l1sp*)&l0); \
l1sp_rplaca(p1, (l1sp*)&l1); \
l1sp_rplaca(p2, (l1sp*)&l2); \
(g)->l1_root=p2
#define l1sp_prot4(g, l0, l1, l2, l3) \
l1sp_rplaca(p0, (l1sp*)&l0); \
l1sp_rplaca(p1, (l1sp*)&l1); \
l1sp_rplaca(p2, (l1sp*)&l2); \
l1sp_rplaca(p3, (l1sp*)&l3); \
(g)->l1_root=p3
#define l1sp_unprot1(g) (g)->l1_root=l1sp_cdr((g)->l1_root)
#define l1sp_unprot2(g) (g)->l1_root=l1sp_cddr((g)->l1_root)
#define l1sp_unprot3(g) (g)->l1_root=l1sp_cdddr((g)->l1_root)
#define l1sp_unprot4(g) (g)->l1_root=l1sp_cddddr((g)->l1_root)
/* return the tag of an object */
#define l1sp_tag(s) ((s)->l1_tag)
/* type predicates */
#define l1sp_nilp(s) (L1SP_NIL==l1sp_tag(s))
#define l1sp_procp(s) \
(L1SP_PROC0==l1sp_tag(s)||L1SP_PROCN==l1sp_tag(s)||L1SP_LAMBDA==l1sp_tag(s))
#define l1sp_formp(s) \
(L1SP_FORM0==l1sp_tag(s)||L1SP_FORMN==l1sp_tag(s)||L1SP_MACRO==l1sp_tag(s))
#define l1sp_consp(s) (L1SP_PAIR==l1sp_tag(s))
#define l1sp_symbolp(s) (L1SP_SYMBOL==l1sp_tag(s))
/* pair access */
#define l1sp_car(s) (((l1sp_pair*)(s))->l1_car)
#define l1sp_cdr(s) (((l1sp_pair*)(s))->l1_cdr)
#define l1sp_caar(s) (l1sp_car(l1sp_car(s)))
#define l1sp_cadr(s) (l1sp_car(l1sp_cdr(s)))
#define l1sp_cdar(s) (l1sp_cdr(l1sp_car(s)))
#define l1sp_cddr(s) (l1sp_cdr(l1sp_cdr(s)))
#define l1sp_caaar(s) (l1sp_car(l1sp_car(l1sp_car(s))))
#define l1sp_caadr(s) (l1sp_car(l1sp_car(l1sp_cdr(s))))
#define l1sp_cadar(s) (l1sp_car(l1sp_cdr(l1sp_car(s))))
#define l1sp_caddr(s) (l1sp_car(l1sp_cdr(l1sp_cdr(s))))
#define l1sp_cdaar(s) (l1sp_cdr(l1sp_car(l1sp_car(s))))
#define l1sp_cdadr(s) (l1sp_cdr(l1sp_car(l1sp_cdr(s))))
#define l1sp_cddar(s) (l1sp_cdr(l1sp_cdr(l1sp_car(s))))
#define l1sp_cdddr(s) (l1sp_cdr(l1sp_cdr(l1sp_cdr(s))))
#define l1sp_caaaar(s) (l1sp_car(l1sp_car(l1sp_car(l1sp_car(s)))))
#define l1sp_caaadr(s) (l1sp_car(l1sp_car(l1sp_car(l1sp_cdr(s)))))
#define l1sp_caadar(s) (l1sp_car(l1sp_car(l1sp_cdr(l1sp_car(s)))))
#define l1sp_caaddr(s) (l1sp_car(l1sp_car(l1sp_cdr(l1sp_cdr(s)))))
#define l1sp_cadaar(s) (l1sp_car(l1sp_cdr(l1sp_car(l1sp_car(s)))))
#define l1sp_cadadr(s) (l1sp_car(l1sp_cdr(l1sp_car(l1sp_cdr(s)))))
#define l1sp_caddar(s) (l1sp_car(l1sp_cdr(l1sp_cdr(l1sp_car(s)))))
#define l1sp_cadddr(s) (l1sp_car(l1sp_cdr(l1sp_cdr(l1sp_cdr(s)))))
#define l1sp_cdaaar(s) (l1sp_cdr(l1sp_car(l1sp_car(l1sp_car(s)))))
#define l1sp_cdaadr(s) (l1sp_cdr(l1sp_car(l1sp_car(l1sp_cdr(s)))))
#define l1sp_cdadar(s) (l1sp_cdr(l1sp_car(l1sp_cdr(l1sp_car(s)))))
#define l1sp_cdaddr(s) (l1sp_cdr(l1sp_car(l1sp_cdr(l1sp_cdr(s)))))
#define l1sp_cddaar(s) (l1sp_cdr(l1sp_cdr(l1sp_car(l1sp_car(s)))))
#define l1sp_cddadr(s) (l1sp_cdr(l1sp_cdr(l1sp_car(l1sp_cdr(s)))))
#define l1sp_cdddar(s) (l1sp_cdr(l1sp_cdr(l1sp_cdr(l1sp_car(s)))))
#define l1sp_cddddr(s) (l1sp_cdr(l1sp_cdr(l1sp_cdr(l1sp_cdr(s)))))
/* replace car, replace cdr */
#define l1sp_rplaca(s, x) (((l1sp_pair*)(s))->l1_car=(x))
#define l1sp_rplacd(s, x) (((l1sp_pair*)(s))->l1_cdr=(x))
/* misc */
#define l1sp_eqp(a, b) ((a)==(b))
/*
* l1sp_def convenience macros
*/
#define l1sp_def(g, n, v) \
l1sp_rplaca(g->l1_evlist, \
l1sp_cons(g, l1sp_cons(g, n, v), \
l1sp_car(g->l1_evlist)))
#define l1sp_def0(g, n, v) \
l1sp_rplaca(g->l1_evlist, \
l1sp_cons(g, l1sp_cons(g, l1sp_intern0(g, n), v), \
l1sp_car(g->l1_evlist)))
#define l1sp_defform0(g, n, v) \
l1sp_rplaca(g->l1_evlist, \
l1sp_cons(g, l1sp_cons(g, l1sp_intern0(g, n), \
l1sp_form0(g, v)), \
l1sp_car(g->l1_evlist)))
#define l1sp_defproc0(g, n, v) \
l1sp_rplaca(g->l1_evlist, \
l1sp_cons(g, l1sp_cons(g, l1sp_intern0(g, n), \
l1sp_proc0(g, v)), \
l1sp_car(g->l1_evlist)))
#define l1sp_defformn(g, n, v) \
l1sp_rplaca(g->l1_evlist, \
l1sp_cons(g, l1sp_cons(g, l1sp_intern0(g, n), \
l1sp_formn(g, v)), \
l1sp_car(g->l1_evlist)))
#define l1sp_defprocn(g, n, v) \
l1sp_rplaca(g->l1_evlist, \
l1sp_cons(g, l1sp_cons(g, l1sp_intern0(g, n), \
l1sp_procn(g, v)), \
l1sp_car(g->l1_evlist)))
/* buffer manipulation */
#define l1sp_setbuf_r(g, name, fd, buf, size) \
{ \
g->l1_ioname=(name); \
g->l1_iofd=(fd); \
g->l1_iobuffer=(buf); \
g->l1_iosize=(size_t)(size); \
g->l1_ioalloc=(size_t)(size); \
}
#define l1sp_setbuf_w(g, name, fd, buf, size) \
{ \
g->l1_ioname=(name); \
g->l1_iofd=(fd); \
g->l1_iobuffer=(buf); \
g->l1_iosize=(size_t)0U; \
g->l1_ioalloc=(size_t)(size); \
}
/* unboxing */
#define l1sp_symbits(s) (&((l1sp_symbol*)(s))->l1_data[(size_t)0U])
#define l1sp_funcalln(g,f,l) ((*((l1sp_func*)(f))->l1_func)(g,l))
#define l1sp_funcall0(g,f,a0,a1,a2,a3) \
((*((l1sp_func*)(f))->l1_func)(g,a0,a1,a2,a3))
/* initialize interpreter */
extern void l1sp_init(l1sp_ctx*,char*,char*,size_t),
l1sp_fini(l1sp_ctx*);
/* error reporting */
extern void _l1sp_exit(l1sp_ctx*), /* non-error exit callback */
_l1sp_die(l1sp_ctx*,char*,l1sp*), /* die callback */
_l1sp_dienomem(l1sp_ctx*,size_t), /* out of memory handler */
l1sp_diebadarg(l1sp_ctx*,l1sp*), /* invalid argument type */
l1sp_diebaddot(l1sp_ctx*,l1sp*), /* invalid list argument */
l1sp_dienoread(l1sp_ctx*), /* read error */
l1sp_diebadtok(l1sp_ctx*,int), /* invalid character in read */
l1sp_dienomatch(l1sp_ctx*,int), /* unmatched '(' or '"' */
l1sp_dieunbound(l1sp_ctx*,l1sp*), /* unbound variable */
l1sp_dienoproc(l1sp_ctx*,l1sp*), /* unbound procedure */
l1sp_dienowrite(l1sp_ctx*); /* write error */
#define l1sp_exit(g) ((g)->l1_exit(g)) /* non-error exit */
#define l1sp_die(g,s,l) ((g)->l1_die(g,s,l)) /* low-level die routine */
#define l1sp_dienomem(g,n) ((g)->l1_nomem(g,n)) /* out of memory handler */
/* format information */
extern size_t l1sp_fmtbase(char*, size_t, unsigned, unsigned, int),
l1sp_fmthex(char*, size_t),
l1sp_fmtuint(char*, size_t);
/* miscellaneous routines */
extern l1sp *l1sp_cat(l1sp_ctx*,l1sp*), /* concatenate strings */
*l1sp_cat0(l1sp_ctx*,
char*,
char*,
char*,
char*), /* concatenate strings */
*l1sp_cond(l1sp_ctx*,l1sp*), /* conditional evaluation */
*l1sp_nreverse(l1sp*,
l1sp*), /* destructive list reverse */
*l1sp_quasiquote(l1sp_ctx* g,
l1sp* l,
int *splice,
int *level), /* quasiquote */
*l1sp_set(l1sp_ctx*,l1sp*,l1sp*), /* set variable */
*l1sp_map(l1sp_ctx*,l1sp*,l1sp*), /* map list over procedure */
*l1sp_find(l1sp_ctx*,l1sp*,l1sp*); /* find element in list */
/* object construction */
extern void l1sp_gc(l1sp_ctx*,l1sp**,l1sp**);
extern l1sp *l1sp_alloc(l1sp_ctx*,
unsigned,
size_t,
l1sp**,
l1sp**),
*l1sp_form0(l1sp_ctx*,
l1sp *(*)()), /* special form: fixed args */
*l1sp_proc0(l1sp_ctx*,
l1sp *(*)()), /* procedure: fixed args */
*l1sp_formn(l1sp_ctx*,
l1sp *(*)()), /* special form: list args */
*l1sp_procn(l1sp_ctx*,
l1sp *(*)()), /* procedure: list args */
*l1sp_macro(l1sp_ctx*,
l1sp*,
l1sp*), /* macro expression */
*l1sp_lambda(l1sp_ctx*,
l1sp*,
l1sp*), /* lambda expression */
*l1sp_cons(l1sp_ctx*,
l1sp*, l1sp*), /* pair */
*l1sp_intern(l1sp_ctx*,
char*,
size_t), /* symbol */
*l1sp_intern0(l1sp_ctx*,
char*); /* symbol */
/* reader interface */
extern l1sp *l1sp_rdsexp(l1sp_ctx*);
/* eval interface */
extern l1sp *l1sp_eval(l1sp_ctx*,l1sp*),
*l1sp_apply(l1sp_ctx*,l1sp*,l1sp*);
/* printer interface */
extern void l1sp_printc(l1sp_ctx*,int), /* print character */
l1sp_prints(l1sp_ctx*,char*), /* print string */
l1sp_printo(l1sp_ctx*,int), /* print character in octal */
l1sp_printn(l1sp_ctx*,size_t); /* print number in hex */
extern l1sp *l1sp_print(l1sp_ctx*,l1sp*), /* print atom */
*l1sp_grindef(l1sp_ctx*,l1sp*), /* print symbolic expression */
*l1sp_flush(l1sp_ctx*), /* flush output stream */
*l1sp_terpri(l1sp_ctx*); /* terminate print line */
/* high level interface */
extern l1sp *l1sp_load(l1sp_ctx*,l1sp*), /* load file (read/eval) */
*l1sp_load0(l1sp_ctx*,char*); /* load file (read/eval) */
void
_l1sp_exit(g)
l1sp_ctx *g;
{
exit(0);
}
void
_l1sp_die(g, syscall, resource)
l1sp_ctx *g;
char *syscall;
l1sp *resource;
{
extern char *__progname;
char b[L1SP_IOBUFFER];
l1sp_setbuf_w(g,
"/dev/fd/2",
2,
&b[(size_t)0U],
sizeof b/sizeof b[(size_t)0U]);
l1sp_prints(g, __progname);
l1sp_prints(g, ": error: ");
l1sp_prints(g, syscall);
if(g->l1_nil!=resource) {
l1sp_prints(g, ": ");
l1sp_grindef(g, resource);
}
if(errno) {
l1sp_prints(g, ": ");
l1sp_prints(g, strerror(errno));
}
l1sp_terpri(g);
exit(0x80);
}
void
_l1sp_dienomem(g, n)
l1sp_ctx *g;
size_t n;
{
extern char *__progname;
char b[L1SP_IOBUFFER];
l1sp_setbuf_w(g,
"/dev/fd/2",
2,
&b[(size_t)0U],
sizeof b/sizeof b[(size_t)0U]);
l1sp_prints(g, __progname);
l1sp_prints(g, ": error: brk: out of memory allocating ");
l1sp_printn(g, n);
l1sp_prints(g, " bytes");
l1sp_terpri(g);
exit(0x80);
}
void
l1sp_diebadarg(g, l)
l1sp_ctx *g;
l1sp *l;
{
l1sp_die(g, "invalid argument", l);
}
void
l1sp_diebaddot(g, l)
l1sp_ctx *g;
l1sp *l;
{
l1sp_die(g, "invalid dot expression", l);
}
void
l1sp_dienoread(g)
l1sp_ctx *g;
{
l1sp_die(g, "read", l1sp_intern0(g, g->l1_ioname));
}
void
l1sp_diebadtok(g, ch)
l1sp_ctx *g;
{
char p[2];
p[0]=(char)ch;
p[1]='\0';
l1sp_die(g, "unrecognized character", l1sp_intern0(g, &p[(size_t)0U]));
}
void
l1sp_dienomatch(g, ch)
l1sp_ctx *g;
{
char p[2];
p[0]=(char)ch;
p[1]='\0';
l1sp_die(g, "unmatched character", l1sp_intern0(g, &p[(size_t)0U]));
}
void
l1sp_dieunbound(g, s)
l1sp_ctx *g;
l1sp *s;
{
l1sp_die(g, "unbound variable", s);
}
void
l1sp_dienoproc(g, s)
l1sp_ctx *g;
l1sp *s;
{
l1sp_die(g, "no procedure", s);
}
void
l1sp_dienowrite(g)
l1sp_ctx *g;
{
l1sp_die(g, "write", l1sp_intern0(g, g->l1_ioname));
}
#define align(a) \
(((a)+((size_t)L1SP_ALIGN)-(size_t)1U)&~(((size_t)L1SP_ALIGN)-(size_t)1U))
#define ssallocp(g, n) \
((g->l1_sssize+(n))<=g->l1_ssalloc)
#define forward(s, x) \
{ \
(s)->l1_tag=L1SP_FORWARD; \
(s)->l1_forward=(x); \
}
l1sp*
l1sp_alloc(g, t, n, a, b)
l1sp_ctx *g;
unsigned t;
size_t n;
l1sp **a, **b;
{
l1sp *s;
size_t alloc=0U;
switch(t) {
case L1SP_FORM0:
case L1SP_PROC0:
case L1SP_FORMN:
case L1SP_PROCN:
alloc=sizeof(l1sp_func);
break;
case L1SP_MACRO:
case L1SP_LAMBDA:
case L1SP_PAIR:
alloc=sizeof(l1sp_pair);
break;
case L1SP_SYMBOL:
alloc=sizeof(l1sp_symbol)+n;
break;
}
alloc=align(alloc);
if(!ssallocp(g, alloc)) {
l1sp_gc(g, a, b);
if(!ssallocp(g, alloc)) l1sp_dienomem(g, alloc);
}
s=(l1sp*)(g->l1_semispace[g->l1_sscur]+g->l1_sssize);
s->l1_tag=t;
g->l1_sssize+=alloc;
return s;
}
static l1sp*
move(g, s)
l1sp_ctx *g;
l1sp *s;
{
l1sp *x=g->l1_nil;
size_t n;
switch(l1sp_tag(s)) {
case L1SP_NIL:
x=s;
break;
case L1SP_FORM0:
case L1SP_PROC0:
case L1SP_FORMN:
case L1SP_PROCN:
x=l1sp_alloc(g, l1sp_tag(s), (size_t)0U, &g->l1_nil, &g->l1_nil);
((l1sp_func*)x)->l1_func=((l1sp_func*)s)->l1_func;
forward(s, x);
break;
case L1SP_MACRO:
case L1SP_LAMBDA:
case L1SP_PAIR:
x=l1sp_alloc(g, l1sp_tag(s), (size_t)0U, &g->l1_nil, &g->l1_nil);
l1sp_rplaca(x, l1sp_car(s));
l1sp_rplacd(x, l1sp_cdr(s));
forward(s, x);
break;
case L1SP_SYMBOL:
x=l1sp_alloc(g,
l1sp_tag(s),
n=strlen(l1sp_symbits(s)),
&g->l1_nil,
&g->l1_nil);
memcpy(l1sp_symbits(x), l1sp_symbits(s), n+(size_t)1U);
g->l1_oblist=l1sp_cons(g, x, g->l1_oblist);
forward(s, x);
break;
case L1SP_FORWARD:
x=s->l1_forward;
break;
}
return x;
}
void
l1sp_gc(g, a, b)
l1sp_ctx *g;
l1sp **a, **b;
{
l1sp *x, **c;
char *space;
size_t alloc, scan;
g->l1_sscur=!g->l1_sscur;
g->l1_sssize=(size_t)0U;
space=g->l1_semispace[g->l1_sscur];
scan=(size_t)0U;
/* clear the oblist, we will recreate as we gc */
g->l1_oblist=g->l1_nil;
g->l1_nil=move(g->l1_nil);
for(x=g->l1_root; l1sp_consp(x); x=l1sp_cdr(x)) {
c=(l1sp**)l1sp_car(x); *c=move(*c);
}
g->l1_t=move(g, g->l1_t);
g->l1_quote=move(g, g->l1_quote);
g->l1_quasiquote=move(g, g->l1_quasiquote);
g->l1_unquote=move(g, g->l1_unquote);
g->l1_unquote_splicing=move(g, g->l1_unquote_splicing);
g->l1_evlist=move(g, g->l1_evlist);
*a=move(g, *a);
*b=move(g, *b);
while(scan<g->l1_sssize) {
x=(l1sp*)(space+scan);
switch(l1sp_tag(x)) {
case L1SP_FORM0:
case L1SP_PROC0:
case L1SP_FORMN:
case L1SP_PROCN:
scan+=align(sizeof(l1sp_func));
break;
case L1SP_MACRO:
case L1SP_LAMBDA:
case L1SP_PAIR:
scan+=align(sizeof(l1sp_pair));
l1sp_rplaca(x, move(l1sp_car(x)));
l1sp_rplacd(x, move(l1sp_cdr(x)));
break;
case L1SP_SYMBOL:
alloc=sizeof(l1sp_symbol)+strlen(l1sp_symbits(x));
scan+=align(alloc);
scan+=align(sizeof(l1sp_pair));
break;
}
}
}
#define FUNC(name, type) \
l1sp* \
name(g, f) \
l1sp_ctx *g; \
l1sp *(*f)(l1sp*); \
{ \
l1sp_func *x; \
x=(l1sp_func*)l1sp_alloc(g, type, (size_t)0U, &g->l1_nil, &g->l1_nil); \
x->l1_func=f; \
return (l1sp*)x; \
}
FUNC(l1sp_form0, L1SP_FORM0)
FUNC(l1sp_proc0, L1SP_PROC0)
FUNC(l1sp_formn, L1SP_FORMN)
FUNC(l1sp_procn, L1SP_PROCN)
#define PAIR(name, type) \
l1sp* \
name(g, x, y) \
l1sp_ctx *g; \
l1sp *x, *y; \
{ \
l1sp *s; \
s=l1sp_alloc(g, type, (size_t)0U, &x, &y); \
l1sp_rplaca(s, x); \
l1sp_rplacd(s, y); \
return s; \
}
PAIR(l1sp_macro, L1SP_MACRO)
PAIR(l1sp_lambda, L1SP_LAMBDA)
PAIR(l1sp_cons, L1SP_PAIR)
l1sp*
l1sp_intern(g, p, n)
l1sp_ctx *g;
char *p;
size_t n;
{
l1sp *x;
char *s;
size_t m;
for(x=g->l1_oblist; l1sp_consp(x); x=l1sp_cdr(x)) {
s=l1sp_symbits(l1sp_car(x)); m=strlen(s);
if(n==m && 0==memcmp(p, s, n)) return l1sp_car(x);
}
x=l1sp_alloc(g, L1SP_SYMBOL, n, &g->l1_nil, &g->l1_nil);
memcpy(l1sp_symbits(x), p, n); l1sp_symbits(x)[n]='\0';
g->l1_oblist=l1sp_cons(g, x, g->l1_oblist);
return x;
}
l1sp*
l1sp_intern0(g, p)
l1sp_ctx *g;
char *p;
{
l1sp *x;
size_t n;
for(x=g->l1_oblist; l1sp_consp(x); x=l1sp_cdr(x)) {
if(0==strcmp(l1sp_symbits(l1sp_car(x)), p)) return l1sp_car(x);
}
x=l1sp_alloc(g, L1SP_SYMBOL, n=strlen(p), &g->l1_nil, &g->l1_nil);
memcpy(l1sp_symbits(x), p, n+(size_t)1U);
g->l1_oblist=l1sp_cons(g, x, g->l1_oblist);
return x;
}
/*
* l1sp_cat - concatenate symbols
*
* this routine is like |l1sp_intern0| except
* the memory for the symbol bits is not
* contiguous.
*/
l1sp*
l1sp_cat(g, l)
l1sp_ctx *g;
l1sp *l;
{
L1SP_PROT1_DECL(g);
l1sp *x, *y, *r;
char *d, *s;
size_t n;
/* determine the length of the new symbol */
n=(size_t)0U;
for(x=l; l1sp_consp(x); x=l1sp_cdr(x)) {
y=l1sp_car(x);
if(!l1sp_symbolp(y)) l1sp_diebadarg(g, y);
n+=strlen(l1sp_symbits(y));
}
/* allocate the object */
l1sp_prot1(g, l);
r=l1sp_alloc(g, L1SP_SYMBOL, n, &g->l1_nil, &g->l1_nil);
l1sp_unprot1(g);
/* copy bits for each input */
d=l1sp_symbits(r);
for(x=l; l1sp_consp(x); x=l1sp_cdr(x)) {
s=l1sp_symbits(l1sp_car(x));
memcpy(d, s, n=strlen(s));
d+=n;
}
*d='\0';
/* search for an existing symbol */
for(x=g->l1_oblist; l1sp_consp(x); x=l1sp_cdr(x)) {
if(0==strcmp(l1sp_symbits(l1sp_car(x)), l1sp_symbits(r)))
return l1sp_car(x);
}
return r;
}
l1sp*
l1sp_cat0(g, a0, a1, a2, a3)
l1sp_ctx *g;
char *a0, *a1, *a2, *a3;
{
l1sp *x, *r;
char *b;
size_t n;
/* determine the length of the new symbol */
n=(size_t)0U;
if(a0) n+=strlen(a0);
if(a1) n+=strlen(a1);
if(a2) n+=strlen(a2);
if(a3) n+=strlen(a3);
/* allocate the object */
r=l1sp_alloc(g, L1SP_SYMBOL, n, &g->l1_nil, &g->l1_nil);
/* copy bits for each input */
b=l1sp_symbits(r);
if(a0) { memcpy(b, a0, n=strlen(a0)); b+=n; }
if(a1) { memcpy(b, a1, n=strlen(a1)); b+=n; }
if(a2) { memcpy(b, a2, n=strlen(a2)); b+=n; }
if(a3) { memcpy(b, a3, n=strlen(a3)); b+=n; }
*b='\0';
/* search for an existing symbol */
for(x=g->l1_oblist; l1sp_consp(x); x=l1sp_cdr(x)) {
if(0==strcmp(l1sp_symbits(l1sp_car(x)), l1sp_symbits(r)))
return l1sp_car(x);
}
return r;
}
l1sp*
l1sp_cond(g, l)
l1sp_ctx *g;
l1sp *l;
{
L1SP_PROT3_DECL(g);
l1sp *r, *x, *y;
r=x=y=g->l1_nil;
l1sp_prot3(g, r, x, y);
/* the argument list is a list of lists */
for(x=l; l1sp_consp(x); x=l1sp_cdr(x)) {
y=l1sp_car(x);
/*
* if the element is not a list,
* we are not a proper cond
*/
if(!l1sp_consp(y)) l1sp_diebadarg(g, y);
/*
* if the first element evaluates to
* |nil|, try the next list
*/
r=l1sp_eval(g, l1sp_car(y));
if(l1sp_nilp(r)) continue;
/*
* otherwise, evaluate every other
* element in the list, returning
* the result of the last
*/
for(y=l1sp_cdr(y); l1sp_consp(y); y=l1sp_cdr(y)) {
r=l1sp_eval(g, l1sp_car(y));
}
if(!l1sp_nilp(y)) l1sp_diebaddot(g, y);
break;
}
l1sp_unprot3(g);
return r;
}
l1sp*
l1sp_nreverse(t, l)
l1sp *t, *l;
{
l1sp *p;
while(l1sp_consp(l)) {
p=l1sp_cdr(l);
l1sp_rplacd(l, t);
t=l;
l=p;
}
return t;
}
l1sp*
l1sp_quasiquote(g, l, set_splice, set_level)
l1sp_ctx *g;
l1sp *l;
int *set_splice, *set_level;
{
L1SP_PROT2_DECL(g);
l1sp *r, *x, *y;
int do_splice=0,
do_level=0;
switch(l1sp_tag(l)) {
case L1SP_PAIR:
r=x=g->l1_nil;
l1sp_prot2(g, l, x);
for(x=l; l1sp_consp(x); x=l1sp_cdr(x)) {
y=l1sp_quasiquote(g, l1sp_car(x), &do_splice, &do_level);
if(do_splice) { /* unquote-splicing */
do_splice=0;
r=l1sp_nreverse(r, y);
} else {
r=l1sp_cons(g, y, r);
}
}
r=l1sp_nreverse(g->l1_nil, r);
if(!do_level) {
if(l1sp_eqp(g->l1_unquote, l1sp_car(r))) {
if(!l1sp_nilp(x)) l1sp_diebaddot(g, l);
l1sp_unprot2(g);
return l1sp_eval(g, l1sp_cadr(r));
} else if(l1sp_eqp(g->l1_unquote_splicing, l1sp_car(r))) {
if(!l1sp_nilp(x)) l1sp_diebaddot(g, l);
l1sp_unprot2(g);
*set_splice=1;
return l1sp_eval(g, l1sp_cadr(r));
}
}
l1sp_unprot2(g);
return r;
default:
return l;
}
}
l1sp*
l1sp_set(g, n, v)
l1sp_ctx *g;
l1sp *n, *v;
{
l1sp_rplaca(g->l1_evlist,
l1sp_cons(g, l1sp_cons(g, n, v),
l1sp_car(g->l1_evlist)));