-
Notifications
You must be signed in to change notification settings - Fork 142
/
sexp.c
4031 lines (3843 loc) · 146 KB
/
sexp.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
/* sexp.c -- standalone sexp library implementation */
/* Copyright (c) 2009-2015 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */
#include "chibi/sexp.h"
/* optional huffman-compressed immediate symbols */
struct sexp_huff_entry {
unsigned char len;
unsigned short bits;
};
#if SEXP_USE_HUFF_SYMS
#include "chibi/sexp-hufftabs.h"
#include "chibi/sexp-huff.h"
#endif
#ifdef _WIN32
#include <io.h>
#endif
static int sexp_initialized_p = 0;
static const char sexp_separators[] = {
/* 1 2 3 4 5 6 7 8 9 a b c d e f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, /* x0_ */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* x1_ */
1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, /* x2_ */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, /* x3_ */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* x4_ */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, /* x5_ */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* x6_ */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, /* x7_ */
};
static int digit_value (int c) {
return (((c)<='9') ? ((c) - '0') : ((sexp_tolower(c) - 'a') + 10));
}
static int hex_digit (int n) {
return ((n<=9) ? ('0' + n) : ('a' + n - 10));
}
static int is_precision_indicator(int c) {
return c=='d' || c=='D' || c=='e' || c=='E' || c=='f' || c=='F'
|| c=='l' || c=='L' || c=='s' || c=='S';
}
int sexp_is_separator(int c) {
return 0<c && c<0x80 && sexp_separators[c];
}
#if SEXP_USE_GLOBAL_SYMBOLS
sexp sexp_symbol_table[SEXP_SYMBOL_TABLE_SIZE];
#endif
#if ! SEXP_USE_UNSAFE_PUSH
sexp sexp_push_op(sexp ctx, sexp* loc, sexp x) {
sexp tmp = sexp_cons(ctx, x, *loc);
if (sexp_exceptionp(tmp)) return *loc;
*loc = tmp;
return tmp;
}
#endif
sexp sexp_alloc_tagged_aux(sexp ctx, size_t size, sexp_uint_t tag sexp_current_source_param) {
#if SEXP_USE_TRACK_ALLOC_BACKTRACE
int i;
void* trace[SEXP_BACKTRACE_SIZE + 1];
#endif
sexp res = (sexp) sexp_alloc(ctx, size);
if (res && ! sexp_exceptionp(res)) {
sexp_pointer_tag(res) = tag;
#if SEXP_USE_TRACK_ALLOC_SOURCE
sexp_pointer_source(res) = source;
#if SEXP_USE_TRACK_ALLOC_BACKTRACE
backtrace(trace, SEXP_BACKTRACE_SIZE + 1);
for (i=0; i<SEXP_BACKTRACE_SIZE; i++) res->backtrace[i] = trace[i+1];
#endif
#endif
#if SEXP_USE_HEADER_MAGIC
sexp_pointer_magic(res) = SEXP_POINTER_MAGIC;
#endif
}
return res;
}
#if SEXP_USE_OBJECT_BRACE_LITERALS
sexp sexp_write_simple_object (sexp ctx, sexp self, sexp_sint_t n, sexp obj, sexp writer, sexp out) {
sexp t, x;
sexp_gc_var1(args);
sexp_sint_t i, len, nulls=0;
i = sexp_pointer_tag(obj);
sexp_write_char(ctx, '{', out);
if (i >= sexp_context_num_types(ctx)) {
sexp_write_string(ctx, "invalid", out);
} else {
sexp_gc_preserve1(ctx, args);
t = sexp_object_type(ctx, obj);
sexp_write_string(ctx, sexp_string_data(sexp_type_name(t)), out);
sexp_write_char(ctx, ' ', out);
if (sexp_type_id(t) && sexp_truep(sexp_type_id(t))) {
sexp_write(ctx, sexp_type_id(t), out);
} else {
sexp_write_char(ctx, '#', out);
sexp_write(ctx, sexp_make_fixnum(sexp_type_tag(t)), out);
}
len = sexp_type_num_slots_of_object(t, obj);
args = sexp_list1(ctx, SEXP_FALSE);
for (i=0; i<len; i++) {
x = sexp_slot_ref(obj, i);
if (x) {
for ( ; nulls; --nulls)
sexp_write_string(ctx, " #f", out);
sexp_write_char(ctx, ' ', out);
if (writer && sexp_applicablep(writer)) {
sexp_car(args) = x;
x = sexp_apply(ctx, writer, args);
if (sexp_exceptionp(x)) sexp_print_exception(ctx, x, out);
} else {
sexp_write(ctx, sexp_slot_ref(obj, i), out);
}
} else {
nulls++;
}
}
sexp_gc_release1(ctx);
}
sexp_write_char(ctx, '}', out);
return SEXP_VOID;
}
#else
#define sexp_write_simple_object NULL
#endif
#if SEXP_USE_UNIFORM_VECTOR_LITERALS
sexp sexp_write_uvector(sexp ctx, sexp self, sexp_sint_t n, sexp obj, sexp writer, sexp out) {
sexp_uint_t i, len;
char* str;
sexp_gc_var2(f, tmp);
sexp_gc_preserve2(ctx, f, tmp);
f = sexp_make_flonum(ctx, 0.0f);
sexp_write_char(ctx, '#', out);
sexp_write_char(ctx, sexp_uvector_prefix(sexp_uvector_type(obj)), out);
sexp_write(ctx, sexp_make_fixnum(sexp_uvector_element_size(sexp_uvector_type(obj))), out);
sexp_write_char(ctx, '(', out);
len = sexp_uvector_length(obj);
str = (char*) sexp_uvector_data(obj);
for (i=0; i<(sexp_sint_t)len; i++) {
if (i!=0) sexp_write_char(ctx, ' ', out);
switch (sexp_uvector_type(obj)) {
case SEXP_U1: sexp_write(ctx, sexp_make_fixnum(sexp_bit_ref(obj, i)), out); break;
case SEXP_S8: sexp_write(ctx, sexp_make_fixnum(((signed char*)str)[i]), out); break;
case SEXP_S16: sexp_write(ctx, sexp_make_fixnum(((signed short*)str)[i]), out); break;
case SEXP_U16: sexp_write(ctx, sexp_make_fixnum(((unsigned short*)str)[i]), out); break;
case SEXP_S32: sexp_write(ctx, tmp=sexp_make_integer(ctx, ((int32_t*)str)[i]), out); break;
case SEXP_U32: sexp_write(ctx, tmp=sexp_make_unsigned_integer(ctx, ((uint32_t*)str)[i]), out); break;
case SEXP_S64: sexp_write(ctx, tmp=sexp_make_integer(ctx, ((int64_t*)str)[i]), out); break;
case SEXP_U64: sexp_write(ctx, tmp=sexp_make_unsigned_integer(ctx, ((uint64_t*)str)[i]), out); break;
#if SEXP_USE_FLONUMS
#if SEXP_USE_MINI_FLOAT_UNIFORM_VECTORS
case SEXP_F8: sexp_flonum_value_set(f, sexp_quarter_to_double(((unsigned char*)str)[i])); sexp_write(ctx, f, out); break;
case SEXP_F16: sexp_flonum_value_set(f, sexp_half_to_double(((unsigned short*)str)[i])); sexp_write(ctx, f, out); break;
#endif
case SEXP_F32: sexp_flonum_value_set(f, ((float*)str)[i]); sexp_write(ctx, f, out); break;
case SEXP_F64: sexp_flonum_value_set(f, ((double*)str)[i]); sexp_write(ctx, f, out); break;
#endif
#if SEXP_USE_COMPLEX
case SEXP_C64:
sexp_flonum_value_set(f, ((float*)str)[i*2]);
sexp_write(ctx, f, out);
if (((float*)str)[i*2 + 1] >= 0)
sexp_write_char(ctx, '+', out);
sexp_flonum_value_set(f, ((float*)str)[i*2 + 1]);
sexp_write(ctx, f, out);
sexp_write_char(ctx, 'i', out);
break;
case SEXP_C128:
sexp_flonum_value_set(f, ((double*)str)[i*2]);
sexp_write(ctx, f, out);
if (((double*)str)[i*2 + 1] >= 0)
sexp_write_char(ctx, '+', out);
sexp_flonum_value_set(f, ((double*)str)[i*2 + 1]);
sexp_write(ctx, f, out);
sexp_write_char(ctx, 'i', out);
break;
#endif
}
}
sexp_write_char(ctx, ')', out);
sexp_gc_release2(ctx);
return SEXP_VOID;
}
#endif
sexp sexp_finalize_fileno (sexp ctx, sexp self, sexp_sint_t n, sexp fileno) {
if (sexp_fileno_openp(fileno) && !sexp_fileno_no_closep(fileno)) {
sexp_fileno_openp(fileno) = 0;
close(sexp_fileno_fd(fileno));
}
return SEXP_VOID;
}
sexp sexp_finalize_port (sexp ctx, sexp self, sexp_sint_t n, sexp port) {
sexp res = SEXP_VOID;
if (sexp_port_openp(port)) {
sexp_port_openp(port) = 0;
if (sexp_oportp(port)) sexp_flush_forced(ctx, port);
#ifndef PLAN9
if (sexp_filenop(sexp_port_fd(port))
&& sexp_fileno_openp(sexp_port_fd(port))) {
if (sexp_port_shutdownp(port)) {
/* shutdown the socket if requested */
if (sexp_iportp(port))
shutdown(sexp_port_fileno(port), sexp_oportp(port) ? SHUT_RDWR : SHUT_RD);
if (sexp_oportp(port))
shutdown(sexp_port_fileno(port), SHUT_WR);
}
if (!sexp_port_no_closep(port)) {
if (--sexp_fileno_count(sexp_port_fd(port)) == 0)
sexp_finalize_fileno(ctx, self, n, sexp_port_fd(port));
}
}
#endif
if (sexp_port_stream(port) && ! sexp_port_no_closep(port))
/* close the stream */
fclose(sexp_port_stream(port));
sexp_port_offset(port) = 0;
sexp_port_size(port) = 0;
}
return res;
}
#if SEXP_USE_DL
#ifdef _WIN32
sexp sexp_finalize_dl (sexp ctx, sexp self, sexp_sint_t n, sexp dl) {
FreeLibrary(sexp_dl_handle(dl));
return SEXP_VOID;
}
#else
sexp sexp_finalize_dl (sexp ctx, sexp self, sexp_sint_t n, sexp dl) {
dlclose(sexp_dl_handle(dl));
return SEXP_VOID;
}
#endif
#endif
#if SEXP_USE_UNIFORM_VECTOR_LITERALS
sexp sexp_finalize_uvector (sexp ctx, sexp self, sexp_sint_t n, sexp obj) {
/* if (sexp_uvector_freep(obj)) */
/* free(sexp_uvector_data(obj)); */
return SEXP_VOID;
}
#endif
static struct sexp_type_struct _sexp_type_specs[] = {
{(sexp)"Object", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_OBJECT, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Type", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_TYPE, sexp_offsetof(type, name), 9, 9, 0, 0, sexp_sizeof(type), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Integer", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_FIXNUM, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Number", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_NUMBER, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Char", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_CHAR, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Boolean", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_BOOLEAN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Pair", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_PAIR, sexp_offsetof(pair, car), 2, 3, 0, 0, sexp_sizeof(pair), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Symbol", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_SYMBOL, 0, 0, 0, 0, 0, sexp_sizeof(symbol)+1, sexp_offsetof(symbol, length), 1, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Byte-Vector", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_BYTES, 0, 0, 0, 0, 0, sexp_sizeof(bytes)+1, sexp_offsetof(bytes, length), 1, 0, 0, 0, 0, 0, 0, NULL},
#if SEXP_USE_PACKED_STRINGS
{(sexp)"String", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_STRING, 0, 0, 0, 0, 0, sexp_sizeof(string)+1, sexp_offsetof(string, length), 1, 0, 0, 0, 0, 0, 0, NULL},
#else
{(sexp)"String", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_STRING, sexp_offsetof(string, bytes), 1, 1+SEXP_USE_STRING_INDEX_TABLE, 0, 0, sexp_sizeof(string), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
#endif
{(sexp)"Vector", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_VECTOR, sexp_sizeof(vector), 0, 0, sexp_offsetof(vector, length), 1, sexp_sizeof(vector), sexp_offsetof(vector, length), sizeof(sexp), 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Flonum", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_FLONUM, 0, 0, 0, 0, 0, sexp_sizeof(flonum), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Bignum", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_BIGNUM, 0, 0, 0, 0, 0, sexp_sizeof(bignum), sexp_offsetof(bignum, length), sizeof(sexp_uint_t), 0, 0, 0, 0, 0, 0, NULL},
#if SEXP_USE_STABLE_ABI || SEXP_USE_RATIOS
{(sexp)"Ratio", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_RATIO, sexp_offsetof(ratio, numerator), 2, 2, 0, 0, sexp_sizeof(ratio), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
#endif
#if SEXP_USE_STABLE_ABI || SEXP_USE_COMPLEX
{(sexp)"Complex", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_COMPLEX, sexp_offsetof(complex, real), 2, 2, 0, 0, sexp_sizeof(complex), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
#endif
#if SEXP_USE_STABLE_ABI || SEXP_USE_DISJOINT_STRING_CURSORS
{(sexp)"String-Cursor", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_STRING_CURSOR, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL},
#endif
{(sexp)"Input-Port", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, SEXP_FINALIZE_PORTN, SEXP_IPORT, sexp_offsetof(port, name), 3, 3, 0, 0, sexp_sizeof(port), 0, 0, 0, 0, 0, 0, 0, 0, SEXP_FINALIZE_PORT},
{(sexp)"Output-Port", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, SEXP_FINALIZE_PORTN, SEXP_OPORT, sexp_offsetof(port, name), 3, 3, 0, 0, sexp_sizeof(port), 0, 0, 0, 0, 0, 0, 0, 0, SEXP_FINALIZE_PORT},
{(sexp)"File-Descriptor", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, SEXP_FINALIZE_FILENON, SEXP_FILENO, 0, 0, 0, 0, 0, sexp_sizeof(fileno), 0, 0, 0, 0, 0, 0, 0, 0, SEXP_FINALIZE_FILENO},
{(sexp)"Exception", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, (sexp)sexp_write_simple_object, NULL, NULL, SEXP_EXCEPTION, sexp_offsetof(exception, kind), 6, 6, 0, 0, sexp_sizeof(exception), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Procedure", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_PROCEDURE, sexp_offsetof(procedure, bc), 2, 2, 0, 0, sexp_sizeof(procedure), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Macro", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_MACRO, sexp_offsetof(macro, proc), 4, 4, 0, 0, sexp_sizeof(macro), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Sc", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, (sexp)sexp_write_simple_object, NULL, NULL, SEXP_SYNCLO, sexp_offsetof(synclo, env), 4, 4, 0, 0, sexp_sizeof(synclo), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Environment", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_ENV, sexp_offsetof(env, parent), 3+(SEXP_USE_STABLE_ABI||SEXP_USE_RENAME_BINDINGS), 3+(SEXP_USE_STABLE_ABI||SEXP_USE_RENAME_BINDINGS), 0, 0, sexp_sizeof(env), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Bytecode", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_BYTECODE, sexp_offsetof(bytecode, name), 3, 3, 0, 0, sexp_sizeof(bytecode), offsetof(struct sexp_struct, value.bytecode.length), 1, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Core-Form", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_CORE, sexp_offsetof(core, name), 1, 1, 0, 0, sexp_sizeof(core), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
#if SEXP_USE_STABLE_ABI || SEXP_USE_DL
{(sexp)"Dynamic-Library", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, SEXP_FINALIZE_DLN, SEXP_DL, sexp_offsetof(dl, file), 1, 1, 0, 0, sexp_sizeof(dl), 0, 0, 0, 0, 0, 0, 0, 0, SEXP_FINALIZE_DL},
#endif
{(sexp)"Opcode", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_OPCODE, sexp_offsetof(opcode, name), 11, 11, 0, 0, sexp_sizeof(opcode), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Lambda", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, (sexp)sexp_write_simple_object, NULL, NULL, SEXP_LAMBDA, sexp_offsetof(lambda, name), 11, 11, 0, 0, sexp_sizeof(lambda), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"If", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, (sexp)sexp_write_simple_object, NULL, NULL, SEXP_CND, sexp_offsetof(cnd, test), 4, 4, 0, 0, sexp_sizeof(cnd), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Ref", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, (sexp)sexp_write_simple_object, NULL, NULL, SEXP_REF, sexp_offsetof(ref, name), 3, 3, 0, 0, sexp_sizeof(ref), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Set!", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, (sexp)sexp_write_simple_object, NULL, NULL, SEXP_SET, sexp_offsetof(set, var), 3, 3, 0, 0, sexp_sizeof(set), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Set-Syn!", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, (sexp)sexp_write_simple_object, NULL, NULL, SEXP_SET_SYN, sexp_offsetof(set_syn, var), 3, 3, 0, 0, sexp_sizeof(set_syn), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Seq", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, (sexp)sexp_write_simple_object, NULL, NULL, SEXP_SEQ, sexp_offsetof(seq, ls), 2, 2, 0, 0, sexp_sizeof(seq), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Lit", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, (sexp)sexp_write_simple_object, NULL, NULL, SEXP_LIT, sexp_offsetof(lit, value), 2, 2, 0, 0, sexp_sizeof(lit), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Stack", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_STACK, sexp_sizeof(stack), 0, 0, sexp_offsetof(stack, top), 1, sexp_sizeof(stack), offsetof(struct sexp_struct, value.stack.length), sizeof(sexp), 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Context", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_CONTEXT, sexp_offsetof(context, stack), 12+(SEXP_USE_STABLE_ABI||SEXP_USE_DL), 12+(SEXP_USE_STABLE_ABI||SEXP_USE_DL), 0, 0, sexp_sizeof(context), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
{(sexp)"Cpointer", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_CPOINTER, sexp_offsetof(cpointer, parent), 1, 0, 0, 0, sexp_sizeof(cpointer), sexp_offsetof(cpointer, length), 1, 0, 0, 0, 0, 0, 0, NULL},
#if SEXP_USE_STABLE_ABI || SEXP_USE_UNIFORM_VECTOR_LITERALS
{(sexp)"Uniform-Vector", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, (sexp)sexp_write_uvector, NULL, (sexp)"sexp_finalize_uvector", SEXP_UNIFORM_VECTOR, sexp_offsetof(uvector, bytes), 1, 1, 0, 0, sexp_sizeof(uvector), 0, 0, 0, 0, 0, 0, 0, 0, sexp_finalize_uvector},
#endif
#if SEXP_USE_STABLE_ABI || SEXP_USE_AUTO_FORCE
{(sexp)"Promise", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_PROMISE, sexp_offsetof(promise, value), 1, 1, 0, 0, sexp_sizeof(promise), 0, 0, 0, 0, 0, 0, 0, 0, NULL},
#endif
#if SEXP_USE_STABLE_ABI || SEXP_USE_WEAK_REFERENCES
{(sexp)"Ephemeron", SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, SEXP_FALSE, NULL, NULL, NULL, SEXP_EPHEMERON, sexp_offsetof(ephemeron, key), 2, 0, 0, 0, sexp_sizeof(ephemeron), 0, 0, sexp_offsetof(ephemeron, key), 1, 0, 0, 1, 0, NULL},
#endif
};
#define SEXP_INIT_NUM_TYPES (SEXP_NUM_CORE_TYPES*2)
#if SEXP_USE_TYPE_DEFS
sexp sexp_register_type_op (sexp ctx, sexp self, sexp_sint_t n, sexp name,
sexp parent, sexp slots,
sexp fb, sexp felb, sexp flb, sexp flo, sexp fls,
sexp sb, sexp so, sexp sc, sexp w, sexp wb, sexp wo,
sexp ws, sexp we, sexp p, const char* fname, sexp_proc2 f) {
sexp *v1, *v2;
sexp_gc_var2(res, type);
sexp_uint_t i, len, num_types=sexp_context_num_types(ctx),
type_array_size=sexp_context_type_array_size(ctx);
sexp_gc_preserve2(ctx, res, type);
if (num_types >= SEXP_MAXIMUM_TYPES) {
res = sexp_user_exception(ctx, self, "register-type: exceeded maximum type limit", name);
} else if (! sexp_stringp(name)) {
res = sexp_type_exception(ctx, self, SEXP_STRING, name);
} else {
if (num_types >= type_array_size) {
len = type_array_size*2;
if (len > SEXP_MAXIMUM_TYPES) len = SEXP_MAXIMUM_TYPES;
res = sexp_make_vector(ctx, sexp_make_fixnum(len), SEXP_VOID);
if (sexp_exceptionp(res)) {
sexp_gc_release2(ctx);
return res;
}
v1 = sexp_vector_data(res);
v2 = sexp_vector_data(sexp_global(ctx, SEXP_G_TYPES));
for (i=0; i<num_types; i++)
v1[i] = v2[i];
sexp_global(ctx, SEXP_G_TYPES) = res;
}
sexp_type_by_index(ctx, num_types) = sexp_alloc_type(ctx, type, SEXP_TYPE);
type = sexp_type_by_index(ctx, num_types);
if (!sexp_exceptionp(type)) {
sexp_pointer_tag(type) = SEXP_TYPE;
sexp_type_tag(type) = num_types;
sexp_type_slots(type) = slots;
sexp_type_field_base(type) = (short)sexp_unbox_fixnum(fb);
sexp_type_field_eq_len_base(type) = (short)sexp_unbox_fixnum(felb);
sexp_type_field_len_base(type) = (short)sexp_unbox_fixnum(flb);
sexp_type_field_len_off(type) = (short)sexp_unbox_fixnum(flo);
sexp_type_field_len_scale(type) = (unsigned short)sexp_unbox_fixnum(fls);
sexp_type_size_base(type) = (short)sexp_unbox_fixnum(sb);
sexp_type_size_off(type) = (short)sexp_unbox_fixnum(so);
sexp_type_size_scale(type) = (unsigned short)sexp_unbox_fixnum(sc);
sexp_type_weak_base(type) = (short)sexp_unbox_fixnum(w);
sexp_type_weak_len_base(type) = (short)sexp_unbox_fixnum(wb);
sexp_type_weak_len_off(type) = (short)sexp_unbox_fixnum(wo);
sexp_type_weak_len_scale(type) = (short)sexp_unbox_fixnum(ws);
sexp_type_weak_len_extra(type) = (short)sexp_unbox_fixnum(we);
sexp_type_name(type) = name;
sexp_type_getters(type) = SEXP_FALSE;
sexp_type_setters(type) = SEXP_FALSE;
sexp_type_finalize(type) = f;
sexp_type_finalize_name(type) = (fname) ? sexp_c_string(ctx, fname, -1) : NULL;
sexp_type_id(type) = SEXP_FALSE;
#if SEXP_USE_DL
if (f) sexp_type_dl(type) = sexp_context_dl(ctx);
#endif
sexp_type_print(type) = p;
if (parent && sexp_typep(parent)) {
len = sexp_vectorp(sexp_type_cpl(parent)) ? sexp_vector_length(sexp_type_cpl(parent)) : 1;
sexp_type_cpl(type) = sexp_make_vector(ctx, sexp_make_fixnum(len+1), SEXP_VOID);
if (parent && sexp_vectorp(sexp_type_cpl(parent)))
memcpy(sexp_vector_data(sexp_type_cpl(type)),
sexp_vector_data(sexp_type_cpl(parent)),
len * sizeof(sexp));
else
sexp_vector_data(sexp_type_cpl(type))[len-1] = parent;
} else {
len = 0;
sexp_type_cpl(type) = sexp_make_vector(ctx, SEXP_ONE, SEXP_VOID);
}
sexp_vector_data(sexp_type_cpl(type))[len] = type;
sexp_type_depth(type) = (short)len;
sexp_global(ctx, SEXP_G_NUM_TYPES) = sexp_make_fixnum(num_types + 1);
}
res = type;
}
sexp_gc_release2(ctx);
return res;
}
sexp sexp_register_simple_type_op (sexp ctx, sexp self, sexp_sint_t n, sexp name, sexp parent, sexp slots) {
short i, num_slots = (short)sexp_unbox_fixnum(sexp_length(ctx, slots));
sexp type_size, num_slots_obj, cpl, tmp;
if (parent && sexp_typep(parent)) {
num_slots += (short)sexp_unbox_fixnum(sexp_length(ctx, sexp_type_slots(parent)));
if (sexp_vectorp((cpl = sexp_type_cpl(parent))))
for (i=(short)sexp_vector_length(cpl)-1; i>=0; i--) {
tmp = sexp_vector_ref(cpl, sexp_make_fixnum(i));
num_slots += (short)sexp_unbox_fixnum(sexp_length(ctx, sexp_type_slots(tmp)));
}
}
num_slots_obj = sexp_make_fixnum(num_slots);
type_size = sexp_make_fixnum(sexp_sizeof_header + sizeof(sexp)*num_slots);
return
sexp_register_type(ctx, name, parent, slots,
sexp_make_fixnum(sexp_offsetof_slot0),
num_slots_obj, num_slots_obj, SEXP_ZERO, SEXP_ZERO,
type_size, SEXP_ZERO, SEXP_ZERO, SEXP_ZERO, SEXP_ZERO,
SEXP_ZERO, SEXP_ZERO, SEXP_ZERO,
sexp_type_print(sexp_type_by_index(ctx, SEXP_EXCEPTION)),
NULL, NULL);
}
#if SEXP_USE_OBJECT_BRACE_LITERALS
sexp sexp_lookup_type_op(sexp ctx, sexp self, sexp_sint_t n, sexp name, sexp id) {
int i;
sexp res;
const char* str;
sexp_assert_type(ctx, sexp_stringp, SEXP_STRING, name);
str = sexp_string_data(name);
if (sexp_fixnump(id)) {
i = sexp_unbox_fixnum(id);
if (i < sexp_context_num_types(ctx)
&& strcmp(str, sexp_string_data(sexp_type_name_by_index(ctx, i))) == 0)
return sexp_type_by_index(ctx, i);
else
return SEXP_FALSE;
}
for (i=sexp_context_num_types(ctx)-1; i>=0; i--)
if (strcmp(str, sexp_string_data(sexp_type_name_by_index(ctx, i))) == 0) {
res = sexp_type_by_index(ctx, i);
if (sexp_stringp(id)
&& !(sexp_stringp(sexp_type_id(res))
&& strcmp(sexp_string_data(id), sexp_string_data(sexp_type_id(res))) == 0))
continue;
return res;
}
return SEXP_FALSE;
}
#endif
sexp sexp_finalize_c_type (sexp ctx, sexp self, sexp_sint_t n, sexp obj) {
if (sexp_cpointer_freep(obj))
free(sexp_cpointer_value(obj));
return SEXP_VOID;
}
#else
#define sexp_num_types SEXP_NUM_CORE_TYPES
#endif
/****************************** contexts ******************************/
static const char* sexp_initial_features[] = {
#ifdef sexp_architecture
sexp_architecture,
#endif
#ifdef sexp_platform
sexp_platform,
#endif
#if SEXP_BSD
"bsd",
#endif
#if SEXP_DARWIN
"darwin",
#endif
#if SEXP_OPENBSD
"openbsd",
#endif
#if SEXP_FREEBSD
"freebsd",
#endif
#if SEXP_NETBSD
"netbsd",
#endif
#if SEXP_DRAGONFLY
"dragonfly",
#endif
#if defined(_WIN32)
"windows",
#endif
#if SEXP_USE_DL
"dynamic-loading",
#endif
#if SEXP_USE_BIDIRECTIONAL_PORTS
"bidir-ports",
#endif
#if SEXP_USE_MODULES
"modules",
#endif
#if SEXP_USE_BOEHM
"boehm-gc",
#endif
#if SEXP_USE_UTF8_STRINGS
"full-unicode",
#endif
#if SEXP_USE_STRING_INDEX_TABLE
"string-index",
#endif
#if SEXP_USE_STRING_REF_CACHE
"string-ref-cache",
#endif
#if SEXP_USE_GREEN_THREADS
"threads",
#endif
#if SEXP_USE_NTP_GETTIME
"ntp",
#endif
#if SEXP_USE_AUTO_FORCE
"auto-force",
#endif
#if SEXP_USE_UNIFORM_VECTOR_LITERALS
"uvector",
#endif
#if SEXP_USE_MINI_FLOAT_UNIFORM_VECTORS
"mini-float",
#endif
#if SEXP_USE_COMPLEX
"complex",
#endif
#if SEXP_USE_RATIOS
"ratios",
#endif
"r7rs",
"chibi-" sexp_version,
"chibi",
NULL,
};
void sexp_init_context_globals (sexp ctx) {
const char** features;
int i, endianess_check = 1;
sexp type, *vec, print=NULL;
sexp_gc_var1(feature);
sexp_context_globals(ctx)
= sexp_make_vector(ctx, sexp_make_fixnum(SEXP_G_NUM_GLOBALS), SEXP_VOID);
#if ! SEXP_USE_GLOBAL_SYMBOLS
sexp_global(ctx, SEXP_G_SYMBOLS) = sexp_make_vector(ctx, sexp_make_fixnum(SEXP_SYMBOL_TABLE_SIZE), SEXP_NULL);
#endif
sexp_global(ctx, SEXP_G_STRICT_P) = SEXP_FALSE;
sexp_global(ctx, SEXP_G_NO_TAIL_CALLS_P) = SEXP_FALSE;
#if SEXP_USE_FOLD_CASE_SYMS
sexp_global(ctx, SEXP_G_FOLD_CASE_P) = sexp_make_boolean(SEXP_DEFAULT_FOLD_CASE_SYMS);
#endif
#if ! SEXP_USE_BOEHM
sexp_global(ctx, SEXP_G_PRESERVATIVES) = SEXP_NULL;
#endif
#if SEXP_USE_WEAK_REFERENCES
sexp_global(ctx, SEXP_G_WEAK_OBJECTS_PRESENT) = SEXP_FALSE;
sexp_global(ctx, SEXP_G_FILE_DESCRIPTORS) = SEXP_FALSE;
sexp_global(ctx, SEXP_G_NUM_FILE_DESCRIPTORS) = SEXP_ZERO;
#endif
sexp_global(ctx, SEXP_G_OOM_ERROR) = sexp_user_exception(ctx, SEXP_FALSE, "out of memory", SEXP_NULL);
sexp_global(ctx, SEXP_G_OOS_ERROR) = sexp_user_exception(ctx, SEXP_FALSE, "out of stack space", SEXP_NULL);
sexp_global(ctx, SEXP_G_ABI_ERROR) = sexp_user_exception(ctx, SEXP_FALSE, "incompatible ABI", SEXP_NULL);
sexp_global(ctx, SEXP_G_INTERRUPT_ERROR) = sexp_user_exception(ctx, SEXP_FALSE, "interrupt", SEXP_NULL);
sexp_global(ctx, SEXP_G_QUOTE_SYMBOL) = sexp_intern(ctx, "quote", -1);
sexp_global(ctx, SEXP_G_QUASIQUOTE_SYMBOL) = sexp_intern(ctx, "quasiquote", -1);
sexp_global(ctx, SEXP_G_UNQUOTE_SYMBOL) = sexp_intern(ctx, "unquote", -1);
sexp_global(ctx, SEXP_G_UNQUOTE_SPLICING_SYMBOL) = sexp_intern(ctx, "unquote-splicing", -1);
sexp_global(ctx, SEXP_G_SYNTAX_SYMBOL) = sexp_intern(ctx, "syntax", -1);
sexp_global(ctx, SEXP_G_QUASISYNTAX_SYMBOL) = sexp_intern(ctx, "quasisyntax", -1);
sexp_global(ctx, SEXP_G_UNSYNTAX_SYMBOL) = sexp_intern(ctx, "unsyntax", -1);
sexp_global(ctx, SEXP_G_UNSYNTAX_SPLICING_SYMBOL) = sexp_intern(ctx, "unsyntax-splicing", -1);
sexp_global(ctx, SEXP_G_CUR_IN_SYMBOL) = sexp_intern(ctx, "current-input-port", -1);
sexp_global(ctx, SEXP_G_CUR_OUT_SYMBOL) = sexp_intern(ctx, "current-output-port", -1);
sexp_global(ctx, SEXP_G_CUR_ERR_SYMBOL) = sexp_intern(ctx, "current-error-port", -1);
sexp_global(ctx, SEXP_G_INTERACTION_ENV_SYMBOL) = sexp_intern(ctx, "interaction-environment", -1);
sexp_global(ctx, SEXP_G_CONTINUABLE_SYMBOL) = sexp_intern(ctx, "continuable", -1);
sexp_global(ctx, SEXP_G_EMPTY_VECTOR) = sexp_alloc_type(ctx, vector, SEXP_VECTOR);
sexp_vector_length(sexp_global(ctx, SEXP_G_EMPTY_VECTOR)) = 0;
sexp_global(ctx, SEXP_G_FEATURES) = SEXP_NULL;
sexp_push(ctx, sexp_global(ctx, SEXP_G_FEATURES), SEXP_FALSE);
sexp_car(sexp_global(ctx, SEXP_G_FEATURES)) = sexp_intern(ctx, (*(unsigned char*) &endianess_check) ? "little-endian" : "big-endian", -1);
sexp_global(ctx, SEXP_G_ENDIANNESS) = sexp_intern(ctx, (*(unsigned char*) &endianess_check) ? "little" : "big", -1);
sexp_gc_preserve1(ctx, feature);
for (features=sexp_initial_features; *features; features++) {
feature = sexp_intern(ctx, *features, -1);
if (sexp_not(sexp_memq(ctx, feature, sexp_global(ctx, SEXP_G_FEATURES)))) {
sexp_push(ctx, sexp_global(ctx, SEXP_G_FEATURES), SEXP_FALSE);
sexp_car(sexp_global(ctx, SEXP_G_FEATURES)) = feature;
}
}
sexp_gc_release1(ctx);
sexp_global(ctx, SEXP_G_NUM_TYPES) = sexp_make_fixnum(SEXP_NUM_CORE_TYPES);
sexp_global(ctx, SEXP_G_TYPES)
= sexp_make_vector(ctx, sexp_make_fixnum(SEXP_INIT_NUM_TYPES), SEXP_VOID);
vec = sexp_vector_data(sexp_global(ctx, SEXP_G_TYPES));
for (i=0; i<SEXP_NUM_CORE_TYPES; i++) {
type = sexp_alloc_type(ctx, type, SEXP_TYPE);
if (!type) {
return; /* TODO - fundamental OOM, what to do here? */
}
memcpy(&(type->value), &(_sexp_type_specs[i]), sizeof(_sexp_type_specs[0]));
vec[i] = type;
sexp_type_name(type) = sexp_c_string(ctx, (char*)sexp_type_name(type), -1);
if (sexp_type_finalize_name(type)) {
sexp_type_finalize_name(type) = sexp_c_string(ctx, (char*)sexp_type_finalize_name(type), -1);
}
if (sexp_type_print(type)) {
if (print && ((sexp_proc1)sexp_type_print(type) == sexp_opcode_func(print)))
sexp_type_print(type) = print;
else
sexp_type_print(type) = print = sexp_make_foreign(ctx, "sexp_write_simple_object", 3, 0, NULL, (sexp_proc1)sexp_type_print(type), NULL);
}
}
}
#if ! SEXP_USE_GLOBAL_HEAP
sexp sexp_bootstrap_context (sexp_uint_t size, sexp_uint_t max_size) {
sexp ctx;
sexp_heap heap;
struct sexp_struct dummy_ctx;
if (size < SEXP_MINIMUM_HEAP_SIZE) size = SEXP_INITIAL_HEAP_SIZE;
size = sexp_heap_align(size);
max_size = sexp_heap_align(max_size);
heap = sexp_make_heap(size, max_size, 0);
if (!heap) return 0;
sexp_pointer_tag(&dummy_ctx) = SEXP_CONTEXT;
sexp_context_mark_stack_ptr(&dummy_ctx) = NULL;
sexp_context_saves(&dummy_ctx) = NULL;
sexp_context_heap(&dummy_ctx) = heap;
ctx = sexp_alloc_type(&dummy_ctx, context, SEXP_CONTEXT);
if (!ctx || sexp_exceptionp(ctx)) {
sexp_free_heap(heap);
} else {
sexp_context_heap(ctx) = heap;
#if SEXP_USE_FIXED_CHUNK_SIZE_HEAPS
heap->chunk_size = sexp_heap_align(1);
heap->next = sexp_make_heap(size, max_size, 0);
if (heap->next) {
heap->next->chunk_size = sexp_heap_align(1 + sexp_heap_align(1));
heap->next->next = sexp_make_heap(size, max_size, 0);
if (heap->next->next) {
heap->next->next->chunk_size = sexp_heap_align(1 + sexp_heap_align(1 + sexp_heap_align(1)));
heap->next->next->next = sexp_make_heap(size, max_size, 0);
}
}
#endif
}
return ctx;
}
#endif
sexp sexp_make_context (sexp ctx, size_t size, size_t max_size) {
sexp_gc_var1(res);
if (ctx) sexp_gc_preserve1(ctx, res);
#if ! SEXP_USE_GLOBAL_HEAP
if (! ctx) {
res = sexp_bootstrap_context(size, max_size);
if (!res || sexp_exceptionp(res)) return res;
} else
#endif
{
res = sexp_alloc_type(ctx, context, SEXP_CONTEXT);
#if ! SEXP_USE_BOEHM && ! SEXP_USE_MALLOC
sexp_context_heap(res) = sexp_context_heap(ctx);
#endif
}
if (!res || sexp_exceptionp(res)) return res;
sexp_context_parent(res) = ctx;
sexp_context_name(res) = sexp_context_specific(res) = SEXP_FALSE;
sexp_context_mark_stack_ptr(res) = NULL;
sexp_context_saves(res) = NULL;
sexp_context_params(res) = SEXP_NULL;
sexp_context_last_fp(res) = 0;
#if SEXP_USE_TIME_GC
sexp_context_gc_count(res) = 0;
sexp_context_gc_usecs(res) = 0;
#endif
#if SEXP_USE_TRACK_ALLOC_TIMES
sexp_context_alloc_count(res) = 0;
sexp_context_alloc_usecs(res) = 0;
sexp_context_alloc_usecs_sq(res) = 0;
#endif
sexp_context_tracep(res) = 0;
sexp_context_timeoutp(res) = 0;
sexp_context_tailp(res) = 1;
#if SEXP_USE_GREEN_THREADS
sexp_context_errorp(res) = 0;
sexp_context_event(res) = SEXP_FALSE;
sexp_context_refuel(res) = SEXP_DEFAULT_QUANTUM;
#endif
#if SEXP_USE_DL
sexp_context_dl(res) = ctx ? sexp_context_dl(ctx) : SEXP_FALSE;
#endif
if (ctx) {
sexp_context_globals(res) = sexp_context_globals(ctx);
sexp_context_dk(res) = sexp_context_dk(ctx);
sexp_gc_release1(ctx);
} else {
sexp_init_context_globals(res);
}
return res;
}
#if ! SEXP_USE_GLOBAL_HEAP
sexp sexp_destroy_context (sexp ctx) {
sexp_heap heap, tmp;
size_t sum_freed;
if (sexp_context_heap(ctx)) {
heap = sexp_context_heap(ctx);
#if SEXP_USE_DEBUG_GC
sexp_debug_heap_stats(heap);
#endif
#if SEXP_USE_TRACK_ALLOC_TIMES
sexp_debug_alloc_times(ctx);
#endif
#if SEXP_USE_TRACK_ALLOC_SIZES
sexp_debug_alloc_sizes(ctx);
#endif
sexp_markedp(ctx) = 1;
sexp_markedp(sexp_context_globals(ctx)) = 1;
sexp_mark(ctx, sexp_global(ctx, SEXP_G_TYPES));
if (sexp_finalize(ctx) == SEXP_FALSE) { return SEXP_FALSE; }
sexp_sweep(ctx, &sum_freed);
if (sexp_finalize(ctx) == SEXP_FALSE) { return SEXP_FALSE; }
sexp_context_heap(ctx) = NULL;
for ( ; heap; heap=tmp) {
tmp = heap->next;
sexp_free_heap(heap);
}
}
return SEXP_TRUE;
}
#endif
/***************************** exceptions *****************************/
sexp sexp_make_exception (sexp ctx, sexp kind, sexp message, sexp irritants,
sexp procedure, sexp source) {
sexp exn = sexp_alloc_type(ctx, exception, SEXP_EXCEPTION);
sexp_exception_kind(exn) = kind;
sexp_exception_message(exn) = message;
sexp_exception_irritants(exn) = irritants;
sexp_exception_procedure(exn) = procedure;
sexp_exception_source(exn) = source;
sexp_exception_stack_trace(exn) = SEXP_FALSE;
return exn;
}
sexp sexp_string_cat3 (sexp ctx, const char *pre, const char *mid, const char* suf) {
int plen=strlen(pre), mlen=strlen(mid), slen=strlen(suf);
char *s;
sexp str;
str = sexp_make_string(ctx, sexp_make_fixnum(plen+mlen+slen), SEXP_VOID);
memcpy(s=sexp_string_data(str), pre, plen);
memcpy(s+plen, mid, mlen);
memcpy(s+plen+mlen, suf, slen);
return str;
}
sexp sexp_user_exception (sexp ctx, sexp self, const char *ms, sexp ir) {
sexp res;
sexp_gc_var3(sym, str, irr);
sexp_gc_preserve3(ctx, sym, str, irr);
res = sexp_make_exception(ctx, sym = sexp_intern(ctx, "user", -1),
str = sexp_c_string(ctx, ms, -1),
((sexp_pairp(ir) || sexp_nullp(ir))
? ir : (irr = sexp_list1(ctx, ir))),
self, SEXP_FALSE);
sexp_gc_release3(ctx);
return res;
}
sexp sexp_user_exception_ls (sexp ctx, sexp self, const char *msg, int n, ...) {
int i;
va_list ap;
sexp_gc_var2(res, ir);
sexp_gc_preserve2(ctx, res, ir);
va_start(ap, n);
for (i=0, ir=SEXP_NULL; i < n; ++i) {
ir = sexp_cons(ctx, va_arg(ap, sexp), ir);
}
ir = sexp_nreverse(ctx, ir);
res = sexp_user_exception(ctx, self, msg, ir);
sexp_gc_release2(ctx);
va_end(ap);
return res;
}
sexp sexp_file_exception (sexp ctx, sexp self, const char *ms, sexp ir) {
sexp_gc_var1(res);
sexp_gc_preserve1(ctx, res);
res = sexp_user_exception(ctx, self, ms, ir);
sexp_exception_kind(res) = sexp_intern(ctx, "file", -1);
sexp_gc_release1(ctx);
return res;
}
static sexp type_exception (sexp ctx, sexp self, sexp str, sexp obj, sexp src) {
sexp_gc_var2(res, sym);
sexp_gc_preserve2(ctx, res, sym);
sym = sexp_intern(ctx, "type", -1);
res = sexp_make_exception(ctx, sym, str, obj, self, src);
sexp_exception_irritants(res)=sexp_list1(ctx, sexp_exception_irritants(res));
sexp_gc_release2(ctx);
return res;
}
sexp sexp_xtype_exception (sexp ctx, sexp self, const char *msg, sexp obj) {
sexp_gc_var1(res);
sexp_gc_preserve1(ctx, res);
res = sexp_c_string(ctx, msg, -1);
res = type_exception(ctx, self, res, obj, SEXP_FALSE);
sexp_gc_release1(ctx);
return res;
}
sexp sexp_type_exception (sexp ctx, sexp self, sexp_uint_t type_id, sexp obj) {
sexp_gc_var1(res);
sexp_gc_preserve1(ctx, res);
res = sexp_string_cat3(ctx, "invalid type, expected ",
sexp_string_data(sexp_type_name_by_index(ctx, type_id)), "");
res = type_exception(ctx, self, res, obj, SEXP_FALSE);
sexp_gc_release1(ctx);
return res;
}
sexp sexp_range_exception (sexp ctx, sexp obj, sexp start, sexp end) {
sexp_gc_var2(res, msg);
sexp_gc_preserve2(ctx, res, msg);
msg = sexp_c_string(ctx, "bad index range", -1);
res = sexp_list2(ctx, start, end);
res = sexp_cons(ctx, obj, res);
res = sexp_make_exception(ctx, sexp_intern(ctx, "range", -1), msg, res,
SEXP_FALSE, SEXP_FALSE);
sexp_gc_release2(ctx);
return res;
}
sexp sexp_print_exception_op (sexp ctx, sexp self, sexp_sint_t n, sexp exn, sexp out) {
sexp_gc_var2(ls, tmp);
/* unwrap continuable exceptions */
if (sexp_exceptionp(exn)
&& ((sexp_exception_kind(exn) == sexp_global(ctx, SEXP_G_CONTINUABLE_SYMBOL)
&& sexp_exceptionp(sexp_exception_irritants(exn)))
|| sexp_exception_kind(exn) == SEXP_UNCAUGHT)) {
return sexp_print_exception_op(ctx, self, n, sexp_exception_irritants(exn), out);
}
sexp_gc_preserve2(ctx, ls, tmp);
if (! sexp_oportp(out))
out = tmp = sexp_make_output_port(ctx, stderr, SEXP_FALSE);
sexp_write_string(ctx, "ERROR", out);
if (sexp_exceptionp(exn)) {
if (sexp_exception_procedure(exn)) {
if (sexp_procedurep(sexp_exception_procedure(exn))) {
ls = sexp_bytecode_name(
sexp_procedure_code(sexp_exception_procedure(exn)));
if (ls && sexp_symbolp(ls)) {
sexp_write_string(ctx, " in ", out);
sexp_write(ctx, ls, out);
}
} else if (sexp_opcodep(sexp_exception_procedure(exn))) {
sexp_write_string(ctx, " in ", out);
sexp_write(ctx, sexp_opcode_name(sexp_exception_procedure(exn)), out);
}
}
ls = sexp_exception_source(exn);
if ((! (ls && sexp_pairp(ls)))
&& sexp_exception_procedure(exn)
&& sexp_procedurep(sexp_exception_procedure(exn)))
ls = sexp_bytecode_source(sexp_procedure_code(sexp_exception_procedure(exn)));
if (ls && sexp_pairp(ls)) {
if (sexp_fixnump(sexp_cdr(ls)) && (sexp_cdr(ls) >= SEXP_ZERO)) {
sexp_write_string(ctx, " on line ", out);
sexp_write(ctx, sexp_cdr(ls), out);
}
if (sexp_stringp(sexp_car(ls))) {
sexp_write_string(ctx, " of file ", out);
sexp_write_string(ctx, sexp_string_data(sexp_car(ls)), out);
}
}
sexp_write_string(ctx, ": ", out);
if (sexp_stringp(sexp_exception_message(exn)))
sexp_write_string(ctx, sexp_string_data(sexp_exception_message(exn)), out);
else
sexp_write(ctx, sexp_exception_message(exn), out);
if (sexp_exception_irritants(exn)
&& sexp_pairp(sexp_exception_irritants(exn))) {
if (sexp_nullp(sexp_cdr(sexp_exception_irritants(exn)))) {
sexp_write_string(ctx, ": ", out);
sexp_write(ctx, sexp_car(sexp_exception_irritants(exn)), out);
sexp_write_string(ctx, "\n", out);
} else {
sexp_write_string(ctx, "\n", out);
for (ls=sexp_exception_irritants(exn);
sexp_pairp(ls); ls=sexp_cdr(ls)) {
sexp_write_string(ctx, " ", out);
sexp_write(ctx, sexp_car(ls), out);
sexp_write_char(ctx, '\n', out);
}
}
} else {
sexp_write_char(ctx, '\n', out);
}
} else {
sexp_write_string(ctx, ": ", out);
if (sexp_stringp(exn))
sexp_write_string(ctx, sexp_string_data(exn), out);
else
sexp_write(ctx, exn, out);
sexp_write_char(ctx, '\n', out);
}
sexp_gc_release2(ctx);
return SEXP_VOID;
}
sexp sexp_read_error (sexp ctx, const char *msg, sexp ir, sexp port) {
sexp res;
sexp_gc_var4(sym, name, str, irr);
sexp_gc_preserve4(ctx, sym, name, str, irr);
name = (sexp_port_name(port) ? sexp_port_name(port) : SEXP_FALSE);
name = sexp_cons(ctx, name, sexp_make_fixnum(sexp_port_line(port)));
str = sexp_c_string(ctx, msg, -1);
irr = ((sexp_pairp(ir) || sexp_nullp(ir)) ? ir : sexp_list1(ctx, ir));
res = sexp_make_exception(ctx, sym = sexp_intern(ctx, "read", -1),
str, irr, SEXP_FALSE, name);
sexp_gc_release4(ctx);
return res;
}
sexp sexp_read_incomplete_error (sexp ctx, const char *msg, sexp ir, sexp port) {
sexp_gc_var1(res);
sexp_gc_preserve1(ctx, res);
res = sexp_read_error(ctx, msg, ir, port);
if (sexp_exceptionp(res))
sexp_exception_kind(res) = sexp_intern(ctx, "read-incomplete", -1);
sexp_gc_release1(ctx);
return res;
}
/*************************** list utilities ***************************/
sexp sexp_cons_op (sexp ctx, sexp self, sexp_sint_t n, sexp head, sexp tail) {
sexp pair = sexp_alloc_type(ctx, pair, SEXP_PAIR);
if (sexp_exceptionp(pair)) return pair;
sexp_car(pair) = head;
sexp_cdr(pair) = tail;
sexp_pair_source(pair) = SEXP_FALSE;
return pair;
}
sexp sexp_list2 (sexp ctx, sexp a, sexp b) {
sexp_gc_var1(res);
sexp_gc_preserve1(ctx, res);
res = sexp_cons(ctx, b, SEXP_NULL);
res = sexp_cons(ctx, a, res);
sexp_gc_release1(ctx);
return res;
}
sexp sexp_list3 (sexp ctx, sexp a, sexp b, sexp c) {
sexp_gc_var1(res);
sexp_gc_preserve1(ctx, res);
res = sexp_list2(ctx, b, c);
res = sexp_cons(ctx, a, res);
sexp_gc_release1(ctx);
return res;
}
sexp sexp_listp_op (sexp ctx, sexp self, sexp_sint_t n, sexp hare) {
sexp turtle;
if (! sexp_pairp(hare))
return sexp_make_boolean(sexp_nullp(hare));
turtle = hare;
hare = sexp_cdr(hare);
for ( ; sexp_pairp(hare); turtle=sexp_cdr(turtle)) {
if (hare == turtle) return SEXP_FALSE;
hare = sexp_cdr(hare);
if (sexp_pairp(hare)) hare = sexp_cdr(hare);
}
return sexp_make_boolean(sexp_nullp(hare));
}
sexp sexp_memq_op (sexp ctx, sexp self, sexp_sint_t n, sexp x, sexp ls) {
while (sexp_pairp(ls))
if (x == sexp_car(ls))
return ls;
else
ls = sexp_cdr(ls);
return SEXP_FALSE;
}
sexp sexp_assq_op (sexp ctx, sexp self, sexp_sint_t n, sexp x, sexp ls) {
while (sexp_pairp(ls))
if (sexp_pairp(sexp_car(ls)) && (x == sexp_caar(ls)))
return sexp_car(ls);