forked from justinethier/cyclone-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.c
4588 lines (4126 loc) · 133 KB
/
runtime.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
/**
* Cyclone Scheme
* https://github.com/justinethier/cyclone
*
* Copyright (c) 2014-2016, Justin Ethier
* All rights reserved.
*
* This file contains the C runtime used by compiled programs.
*/
#include <ck_hs.h>
#include <ck_pr.h>
#include "cyclone/types.h"
#include "cyclone/runtime.h"
#include "cyclone/ck_ht_hash.h"
#include <errno.h>
#include <limits.h>
#include <ctype.h>
//#include <signal.h> // TODO: only used for debugging!
//int JAE_DEBUG = 0;
//int gcMoveCountsDEBUG[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
object Cyc_global_set(void *thd, object * glo, object value)
{
gc_mut_update((gc_thread_data *) thd, *glo, value);
*(glo) = value;
return value;
}
/* Error checking section - type mismatch, num args, etc */
/* Type names to use for error messages */
const char *tag_names[] = {
/*boolean_tag */ "boolean"
/*bytevector_tag */ , "bytevector"
/*c_opaque_tag */ , "opaque"
/*closure0_tag */ , "procedure"
/*closure1_tag */ , "procedure"
/*closureN_tag */ , "procedure"
/*cond_var_tag */ , "condition variable"
/*cvar_tag */ , "C primitive"
/*double_tag */ , "number"
/*eof_tag */ , "eof"
/*forward_tag */ , ""
/*integer_tag */ , "number"
/*macro_tag */ , "macro"
/*mutex_tag */ , "mutex"
/*pair_tag */ , "pair"
/*port_tag */ , "port"
/*primitive_tag */ , "primitive"
/*string_tag */ , "string"
/*symbol_tag */ , "symbol"
/*vector_tag */ , "vector"
, "Reserved for future use"
};
void Cyc_invalid_type_error(void *data, int tag, object found)
{
char buf[256];
snprintf(buf, 255, "Invalid type: expected %s, found ", tag_names[tag]);
//snprintf(buf, 255, "Invalid type: expected %s, found (%p) ", tag_names[tag], found);
Cyc_rt_raise2(data, buf, found);
}
void Cyc_check_obj(void *data, int tag, object obj)
{
if (!is_object_type(obj)) {
Cyc_invalid_type_error(data, tag, obj);
}
}
void Cyc_check_bounds(void *data, const char *label, int len, int index)
{
if (index < 0 || index >= len) {
char buf[128];
snprintf(buf, 127, "%s - invalid index %d", label, index);
Cyc_rt_raise_msg(data, buf);
}
}
/* END error checking */
/* These macros are hardcoded here to support functions in this module. */
#define closcall1(td, clo, a1) \
if (type_is_pair_prim(clo)) { \
Cyc_apply(td, 0, (closure)(a1), clo); \
} else { \
((clo)->fn)(td, 1, clo, a1);\
}
#define return_closcall1(td, clo, a1) { \
char top; \
if (stack_overflow(&top, (((gc_thread_data *)data)->stack_limit))) { \
object buf[1]; buf[0] = a1;\
GC(td, clo, buf, 1); \
return; \
} else {\
closcall1(td, (closure) (clo), a1); \
return;\
} \
}
#define _return_closcall1(td, clo, a1) { \
char top; \
if (stack_overflow(&top, (((gc_thread_data *)data)->stack_limit))) { \
object buf[1]; buf[0] = a1;\
GC(td, clo, buf, 1); \
return NULL; \
} else {\
closcall1(td, (closure) (clo), a1); \
return NULL;\
} \
}
#define closcall2(td, clo, a1, a2) \
if (type_is_pair_prim(clo)) { \
Cyc_apply(td, 1, (closure)(a1), clo,a2); \
} else { \
((clo)->fn)(td, 2, clo, a1, a2);\
}
#define return_closcall2(td, clo, a1, a2) { \
char top; \
if (stack_overflow(&top, (((gc_thread_data *)data)->stack_limit))) { \
object buf[2]; buf[0] = a1;buf[1] = a2;\
GC(td, clo, buf, 2); \
return; \
} else {\
closcall2(td, (closure) (clo), a1, a2); \
return;\
} \
}
#define _return_closcall2(td, clo, a1, a2) { \
char top; \
if (stack_overflow(&top, (((gc_thread_data *)data)->stack_limit))) { \
object buf[2]; buf[0] = a1;buf[1] = a2;\
GC(td, clo, buf, 2); \
return NULL; \
} else {\
closcall2(td, (closure) (clo), a1, a2); \
return NULL;\
} \
}
/*END closcall section */
/* Global variables. */
static gc_heap_root *Cyc_heap;
object Cyc_global_variables = NULL;
int _cyc_argc = 0;
char **_cyc_argv = NULL;
static symbol_type __EOF = { {0}, eof_tag, ""}; // symbol_type in lieu of custom type
const object Cyc_EOF = &__EOF;
static ck_hs_t symbol_table;
static int symbol_table_initial_size = 4096;
static pthread_mutex_t symbol_table_lock;
// Functions to support concurrency kit hashset
// These are specifically for a table of symbols
static void *hs_malloc(size_t r)
{
return malloc(r);
}
static void hs_free(void *p, size_t b, bool r)
{
// (void)b;
// (void)r;
free(p);
// return;
}
static struct ck_malloc my_allocator = {
.malloc = hs_malloc,
.free = hs_free
};
static unsigned long hs_hash(const void *object, unsigned long seed)
{
const symbol_type *c = object;
unsigned long h;
h = (unsigned long)MurmurHash64A(c->desc, strlen(c->desc), seed);
return h;
}
static bool hs_compare(const void *previous, const void *compare)
{
return strcmp(symbol_desc(previous), symbol_desc(compare)) == 0;
}
static void *set_get(ck_hs_t * hs, const void *value)
{
unsigned long h;
void *v;
h = CK_HS_HASH(hs, hs_hash, value);
v = ck_hs_get(hs, h, value);
return v;
}
static bool set_insert(ck_hs_t * hs, const void *value)
{
unsigned long h;
h = CK_HS_HASH(hs, hs_hash, value);
return ck_hs_put(hs, h, value);
}
// End supporting functions
void gc_init_heap(long heap_size)
{
size_t initial_heap_size = INITIAL_HEAP_SIZE;
Cyc_heap = malloc(sizeof(gc_heap_root));
Cyc_heap->heap = malloc(sizeof(gc_heap *) * NUM_HEAP_TYPES);
Cyc_heap->heap[HEAP_REST] = gc_heap_create(HEAP_REST, initial_heap_size, 0, 0);
Cyc_heap->heap[HEAP_SM] = gc_heap_create(HEAP_SM, initial_heap_size, 0, 0);
Cyc_heap->heap[HEAP_64] = gc_heap_create(HEAP_64, initial_heap_size, 0, 0);
if (sizeof(void *) == 8) { // Only use this heap on 64-bit platforms
Cyc_heap->heap[HEAP_96] = gc_heap_create(HEAP_96, initial_heap_size, 0, 0);
}
Cyc_heap->heap[HEAP_HUGE] = gc_heap_create(HEAP_HUGE, 1024, 0, 0);
if (!ck_hs_init(&symbol_table,
CK_HS_MODE_OBJECT | CK_HS_MODE_SPMC,
hs_hash, hs_compare,
&my_allocator, symbol_table_initial_size, 43423)) {
fprintf(stderr, "Unable to initialize symbol table\n");
exit(1);
}
if (pthread_mutex_init(&(symbol_table_lock), NULL) != 0) {
fprintf(stderr, "Unable to initialize symbol_table_lock mutex\n");
exit(1);
}
}
gc_heap_root *gc_get_heap()
{
return Cyc_heap;
}
object cell_get(object cell)
{
// FUTURE: always use unsafe car here, since computed by compiler
return car(cell);
}
static boolean_type t_boolean = { {0}, boolean_tag, "t" };
static boolean_type f_boolean = { {0}, boolean_tag, "f" };
const object boolean_t = &t_boolean;
const object boolean_f = &f_boolean;
static symbol_type Cyc_void_symbol = { {0}, symbol_tag, ""};
const object quote_void = &Cyc_void_symbol;
/* Stack Traces */
void Cyc_st_add(void *data, char *frame)
{
gc_thread_data *thd = (gc_thread_data *) data;
// Do not allow recursion to remove older frames
if (frame != thd->stack_prev_frame) {
thd->stack_prev_frame = frame;
thd->stack_traces[thd->stack_trace_idx] = frame;
thd->stack_trace_idx = (thd->stack_trace_idx + 1) % MAX_STACK_TRACES;
}
}
void Cyc_st_print(void *data, FILE * out)
{
/* print to stream, note it is possible that
some traces could be on the stack after a GC.
not sure what to do about it, may need to
detect that case and stop printing.
or, with the tbl being so small, maybe it will
not be an issue in practice? a bit risky to ignore though
*/
gc_thread_data *thd = (gc_thread_data *) data;
int i = (thd->stack_trace_idx + 1) % MAX_STACK_TRACES;
while (i != thd->stack_trace_idx) {
if (thd->stack_traces[i]) {
fprintf(out, "%s\n", thd->stack_traces[i]);
}
i = (i + 1) % MAX_STACK_TRACES;
}
}
/* END Stack Traces section */
/* Symbol Table */
/* Notes for the symbol table
string->symbol can:
- lookup symbol in the table
- if found, return that pointer
- otherwise, allocate symbol in table and return ptr to it
For now, GC of symbols is missing. long-term it probably would be desirable
*/
char *_strdup(const char *s)
{
char *d = malloc(strlen(s) + 1);
if (d) {
strcpy(d, s);
}
return d;
}
object find_symbol_by_name(const char *name)
{
symbol_type tmp = { {0}, symbol_tag, name};
object result = set_get(&symbol_table, &tmp);
//if (result) {
// printf("found symbol %s\n", symbol_desc(result));
//}
return result;
}
object add_symbol(symbol_type * psym)
{
//printf("Adding symbol %s, table size = %ld\n", symbol_desc(psym), ck_hs_count(&symbol_table));
pthread_mutex_lock(&symbol_table_lock); // Only 1 "writer" allowed
set_insert(&symbol_table, psym);
pthread_mutex_unlock(&symbol_table_lock);
return psym;
}
object add_symbol_by_name(const char *name)
{
symbol_type sym = { {0}, symbol_tag, _strdup(name)};
symbol_type *psym = malloc(sizeof(symbol_type));
memcpy(psym, &sym, sizeof(symbol_type));
return add_symbol(psym);
}
object find_or_add_symbol(const char *name)
{
object sym = find_symbol_by_name(name);
if (sym) {
return sym;
} else {
return add_symbol_by_name(name);
}
}
/* END symbol table */
/* Global table */
list global_table = NULL;
void add_global(object * glo)
{
// It would probably be more efficient to allocate
// a contiguous block of memory for this... for now
// this is more expedient
global_table = malloc_make_pair(mcvar(glo), global_table);
}
void debug_dump_globals()
{
list l = global_table;
for (; l != NULL; l = cdr(l)) {
cvar_type *c = (cvar_type *) car(l);
//gc_mark(h, *(c->pvar)); // Mark actual object the global points to
printf("DEBUG %p ", c->pvar);
if (*c->pvar) {
printf("mark = %d ", mark(*c->pvar));
if (mark(*c->pvar) == gc_color_red) {
printf("obj = ");
Cyc_display(*c->pvar, stdout);
}
printf("\n");
} else {
printf(" is NULL\n");
}
}
}
/* END Global table */
/* Mutation table functions
*
* Keep track of mutations (EG: set-car!) so that new
* values are transported to the heap during GC.
* Note these functions and underlying data structure are only used by
* the calling thread, so locking is not required.
*/
void add_mutation(void *data, object var, int index, object value)
{
gc_thread_data *thd = (gc_thread_data *) data;
if (is_object_type(value)) {
thd->mutations = vpbuffer_add(thd->mutations,
&(thd->mutation_buflen),
thd->mutation_count,
var);
thd->mutation_count++;
if (index >= 0) {
// For vectors only, add index as another var. That way
// the write barrier only needs to inspect the mutated index.
thd->mutations = vpbuffer_add(thd->mutations,
&(thd->mutation_buflen),
thd->mutation_count,
obj_int2obj(index));
thd->mutation_count++;
}
}
}
void clear_mutations(void *data)
{
// Not clearing memory, just resetting count
gc_thread_data *thd = (gc_thread_data *) data;
thd->mutation_count = 0;
}
/* END mutation table */
/* Runtime globals */
object Cyc_glo_call_cc = NULL;
object Cyc_glo_eval_from_c = NULL;
/* Exception handler */
object Cyc_default_exception_handler(void *data, int argc, closure _,
object err)
{
fprintf(stderr, "Error: ");
if ((err == NULL) || is_value_type(err) || type_of(err) != pair_tag) {
Cyc_display(err, stderr);
} else {
// Error is list of form (type arg1 ... argn)
err = cdr(err); // skip type field
for (; (err != NULL); err = cdr(err)) { // output with no enclosing parens
Cyc_display(car(err), stderr);
fprintf(stderr, " ");
}
}
fprintf(stderr, "\nCall history:\n");
Cyc_st_print(data, stderr);
fprintf(stderr, "\n");
//raise(SIGINT); // break into debugger, unix only
exit(1);
return NULL;
}
object Cyc_current_exception_handler(void *data)
{
gc_thread_data *thd = (gc_thread_data *) data;
if (thd->exception_handler_stack == NULL) {
return primitive_Cyc_91default_91exception_91handler;
} else {
return car(thd->exception_handler_stack);
}
}
/* Raise an exception from the runtime code */
void Cyc_rt_raise(void *data, object err)
{
make_pair(c2, err, NULL);
make_pair(c1, boolean_f, &c2);
make_pair(c0, &c1, NULL);
apply(data, NULL, Cyc_current_exception_handler(data), &c0);
// Should never get here
fprintf(stderr, "Internal error in Cyc_rt_raise\n");
exit(1);
}
void Cyc_rt_raise2(void *data, const char *msg, object err)
{
make_string(s, msg);
make_pair(c3, err, NULL);
make_pair(c2, &s, &c3);
make_pair(c1, boolean_f, &c2);
make_pair(c0, &c1, NULL);
apply(data, NULL, Cyc_current_exception_handler(data), &c0);
// Should never get here
fprintf(stderr, "Internal error in Cyc_rt_raise2\n");
exit(1);
}
void Cyc_rt_raise_msg(void *data, const char *err)
{
make_string(s, err);
Cyc_rt_raise(data, &s);
}
/* END exception handler */
int equal(object x, object y)
{
if (x == NULL)
return (y == NULL);
if (y == NULL)
return (x == NULL);
if (obj_is_char(x))
return obj_is_char(y) && x == y;
if (obj_is_int(x))
return (obj_is_int(y) && x == y) ||
(is_object_type(y) &&
type_of(y) == integer_tag && integer_value(y) == obj_obj2int(x));
switch (type_of(x)) {
case integer_tag:
return (obj_is_int(y) && obj_obj2int(y) == integer_value(x)) ||
(is_object_type(y) &&
type_of(y) == integer_tag &&
((integer_type *) x)->value == ((integer_type *) y)->value);
case double_tag:
return (is_object_type(y) &&
type_of(y) == double_tag &&
((double_type *) x)->value == ((double_type *) y)->value);
case string_tag:
return (is_object_type(y) &&
type_of(y) == string_tag &&
strcmp(((string_type *) x)->str, ((string_type *) y)->str) == 0);
case vector_tag:
if (is_object_type(y) &&
type_of(y) == vector_tag &&
((vector) x)->num_elements == ((vector) y)->num_elements) {
int i;
for (i = 0; i < ((vector) x)->num_elements; i++) {
if (equalp(((vector) x)->elements[i], ((vector) y)->elements[i]) ==
boolean_f)
return 0;
}
return 1;
}
return 0;
default:
return x == y;
}
}
object Cyc_car(void *data, object lis)
{
Cyc_check_pair(data, lis);
return car(lis);
}
object Cyc_cdr(void *data, object lis)
{
Cyc_check_pair(data, lis);
return cdr(lis);
}
object Cyc_get_global_variables()
{
return Cyc_global_variables;
}
object Cyc_get_cvar(object var)
{
if (is_object_type(var) && type_of(var) == cvar_tag) {
return *(((cvar_type *) var)->pvar);
}
return var;
}
object Cyc_set_cvar(object var, object value)
{
if (is_object_type(var) && type_of(var) == cvar_tag) {
*(((cvar_type *) var)->pvar) = value;
}
return var;
}
object Cyc_has_cycle(object lst)
{
object slow_lst, fast_lst;
if ((lst == NULL) || is_value_type(lst) ||
(is_object_type(lst) && type_of(lst) != pair_tag)) {
return (boolean_f);
}
slow_lst = lst;
fast_lst = cdr(lst);
while (1) {
if ((fast_lst == NULL))
return boolean_f;
if (Cyc_is_pair(fast_lst) == boolean_f)
return boolean_f;
if ((cdr(fast_lst)) == NULL)
return boolean_f;
if (Cyc_is_pair(cdr(fast_lst)) == boolean_f)
return boolean_f;
if (slow_lst == fast_lst)
return boolean_t;
slow_lst = cdr(slow_lst);
fast_lst = cddr(fast_lst);
}
}
/**
* Write string representation of a double to a buffer.
* Added code from Chibi Scheme to print a ".0" if the
* double is a whole number (EG: 3.0) to avoid confusion
* in the output (EG: was "3").
*/
int double2buffer(char *buf, int buf_size, double num)
{
int i;
i = snprintf(buf, buf_size, "%.15g", num);
if (!strchr(buf, '.') && !strchr(buf, 'e')) {
buf[i++] = '.';
buf[i++] = '0';
buf[i++] = '\0';
}
return i;
}
// TODO: need to change I/O functions (including display/write below)
// to accept an optional port arg. also, if port is not specified, should
// use (current-output-port) instead of stdout. will need to expose the
// (current-*port) functions somehow (tricky since we do not have param
// object yet) then figure out how to use them.
//
// If port is omitted from any output procedure, it defaults
// to the value returned by (current-output-port). It is an
// error to attempt an output operation on a closed port
//
void dispatch_display_va(void *data, int argc, object clo, object cont,
object x, ...)
{
object result;
va_list ap;
va_start(ap, x);
result = Cyc_display_va_list(argc - 1, x, ap);
va_end(ap);
return_closcall1(data, cont, result);
}
object Cyc_display_va(int argc, object x, ...)
{
object result;
va_list ap;
va_start(ap, x);
result = Cyc_display_va_list(argc, x, ap);
va_end(ap);
return result;
}
object Cyc_display_va_list(int argc, object x, va_list ap)
{
FILE *fp = stdout; // TODO: just a placeholder, should use current-output-port
if (argc > 1) {
object tmp;
tmp = va_arg(ap, object);
fp = ((port_type *) tmp)->fp;
}
return Cyc_display(x, fp);
}
object Cyc_display(object x, FILE * port)
{
object tmp = NULL;
object has_cycle = boolean_f;
int i = 0;
if (x == NULL) {
fprintf(port, "()");
return quote_void;
}
if (obj_is_char(x)) {
fprintf(port, "%c", obj_obj2char(x));
return quote_void;
}
if (obj_is_int(x)) {
fprintf(port, "%ld", obj_obj2int(x));
return quote_void;
}
switch (type_of(x)) {
case macro_tag:
fprintf(port, "<macro %p>", (void *)((closure) x)->fn);
break;
case closure0_tag:
case closure1_tag:
case closureN_tag:
fprintf(port, "<procedure %p>", (void *)((closure) x)->fn);
break;
case eof_tag:
fprintf(port, "<EOF>");
break;
case port_tag:
fprintf(port, "<port %p>", ((port_type *) x)->fp);
break;
case primitive_tag:
fprintf(port, "<primitive %s>", prim_name(x));
break;
case cvar_tag:
Cyc_display(Cyc_get_cvar(x), port);
break;
case c_opaque_tag:
fprintf(port, "<C opaque %p>", opaque_ptr(x));
break;
case mutex_tag:
fprintf(port, "<mutex %p>", x);
break;
case cond_var_tag:
fprintf(port, "<condition variable %p>", x);
break;
case boolean_tag:
fprintf(port, "#%s", ((boolean_type *) x)->desc);
break;
case symbol_tag:
fprintf(port, "%s", ((symbol_type *) x)->desc);
break;
case integer_tag:
fprintf(port, "%d", ((integer_type *) x)->value);
break;
case double_tag: {
char buf[33];
double2buffer(buf, 32, ((double_type *) x)->value);
fprintf(port, "%s", buf);
break;
}
case string_tag:
fprintf(port, "%s", ((string_type *) x)->str);
break;
case vector_tag:
fprintf(port, "#(");
for (i = 0; i < ((vector) x)->num_elements; i++) {
if (i > 0) {
fprintf(port, " ");
}
Cyc_display(((vector) x)->elements[i], port);
}
fprintf(port, ")");
break;
case bytevector_tag:
fprintf(port, "#u8(");
for (i = 0; i < ((bytevector) x)->len; i++) {
if (i > 0) {
fprintf(port, " ");
}
fprintf(port, "%u", (unsigned char)(((bytevector) x)->data[i]));
}
fprintf(port, ")");
break;
case pair_tag:
has_cycle = Cyc_has_cycle(x);
fprintf(port, "(");
Cyc_display(car(x), port);
// Experimenting with displaying lambda defs in REPL
// not good enough but this is a start. would probably need
// the same code in write()
if (Cyc_is_symbol(car(x)) == boolean_t &&
strncmp(((symbol) car(x))->desc, "procedure", 10) == 0) {
fprintf(port, " ");
Cyc_display(cadr(x), port);
fprintf(port, " ...)"); /* skip body and env for now */
break;
}
for (tmp = cdr(x); tmp && ((closure) tmp)->tag == pair_tag; tmp = cdr(tmp)) {
if (has_cycle == boolean_t) {
if (i++ > 20)
break; /* arbitrary number, for now */
}
fprintf(port, " ");
Cyc_display(car(tmp), port);
}
if (has_cycle == boolean_t) {
fprintf(port, " ...");
} else if (tmp) {
fprintf(port, " . ");
Cyc_display(tmp, port);
}
fprintf(port, ")");
break;
default:
fprintf(port, "Cyc_display: bad tag x=%d\n", ((closure) x)->tag);
exit(1);
}
return quote_void;
}
void dispatch_write_va(void *data, int argc, object clo, object cont,
object x, ...)
{
object result;
va_list ap;
va_start(ap, x);
result = Cyc_write_va_list(argc - 1, x, ap);
va_end(ap);
return_closcall1(data, cont, result);
}
object Cyc_write_va(int argc, object x, ...)
{
object result;
va_list ap;
va_start(ap, x);
result = Cyc_write_va_list(argc, x, ap);
va_end(ap);
return result;
}
object Cyc_write_va_list(int argc, object x, va_list ap)
{
FILE *fp = stdout; // OK since this is the internal version of write
// Longer-term maybe we get rid of varargs for this one
if (argc > 1) {
object tmp;
tmp = va_arg(ap, object);
fp = ((port_type *) tmp)->fp;
}
return Cyc_write(x, fp);
}
static object _Cyc_write(object x, FILE * port)
{
object tmp = NULL;
object has_cycle = boolean_f;
int i = 0;
if (x == NULL) {
fprintf(port, "()");
return quote_void;
}
if (obj_is_char(x)) {
fprintf(port, "#\\%c", obj_obj2char(x));
return quote_void;
}
if (obj_is_int(x)) {
Cyc_display(x, port);
return quote_void;
}
switch (type_of(x)) {
case string_tag:
fprintf(port, "\"%s\"", ((string_type *) x)->str);
break;
// TODO: what about a list? contents should be displayed per (write)
case vector_tag:
fprintf(port, "#(");
for (i = 0; i < ((vector) x)->num_elements; i++) {
if (i > 0) {
fprintf(port, " ");
}
_Cyc_write(((vector) x)->elements[i], port);
}
fprintf(port, ")");
break;
case pair_tag:
has_cycle = Cyc_has_cycle(x);
fprintf(port, "(");
_Cyc_write(car(x), port);
// Experimenting with displaying lambda defs in REPL
// not good enough but this is a start. would probably need
// the same code in write()
if (Cyc_is_symbol(car(x)) == boolean_t &&
strncmp(((symbol) car(x))->desc, "procedure", 10) == 0) {
fprintf(port, " ");
_Cyc_write(cadr(x), port);
fprintf(port, " ...)"); /* skip body and env for now */
break;
}
for (tmp = cdr(x); tmp && !is_value_type(tmp) && ((closure) tmp)->tag == pair_tag; tmp = cdr(tmp)) {
if (has_cycle == boolean_t) {
if (i++ > 20)
break; /* arbitrary number, for now */
}
fprintf(port, " ");
_Cyc_write(car(tmp), port);
}
if (has_cycle == boolean_t) {
fprintf(port, " ...");
} else if (tmp) {
fprintf(port, " . ");
_Cyc_write(tmp, port);
}
fprintf(port, ")");
break;
default:
Cyc_display(x, port);
}
return quote_void;
}
object Cyc_write(object x, FILE * port)
{
object y = _Cyc_write(x, port);
//fprintf(port, "\n");
return y;
}
object Cyc_write_char(void *data, object c, object port)
{
if (obj_is_char(c)) {
fprintf(((port_type *) port)->fp, "%c", obj_obj2char(c));
} else {
Cyc_rt_raise2(data, "Argument is not a character", c);
}
return quote_void;
}
// TODO: should not be a predicate, may end up moving these to Scheme code
object memberp(void *data, object x, list l)
{
Cyc_check_pair_or_null(data, l);
for (; l != NULL; l = cdr(l)) {
if (boolean_f != equalp(x, car(l)))
return boolean_t;
}
return boolean_f;
}
object memqp(void *data, object x, list l)
{
Cyc_check_pair_or_null(data, l);
for (; l != NULL; l = cdr(l)) {
if ((x == car(l)))
return boolean_t;
}
return boolean_f;
}
/**
* Check two objects for deep equality
*/
object equalp(object x, object y)
{
int second_cycle = 0;
object slow_lis = x, fast_lis = NULL;
object pcar_x = &second_cycle, pcar_y = &second_cycle; // never a car value
if (Cyc_is_pair(x) == boolean_t &&
Cyc_is_pair(cdr(x)) == boolean_t){
fast_lis = cdr(x);
}
for (;; x = cdr(x), y = cdr(y)) {
if (equal(x, y))
return boolean_t;
if (is_value_type(x) || is_value_type(y) ||
(x == NULL) || (y == NULL) ||
type_of(x) != pair_tag || type_of(y) != pair_tag)
return boolean_f;
// Both objects are lists at this point, compare cars
if (pcar_x == car(x) &&
pcar_y == car(y)) {
// do nothing, already equal
} else {
if (boolean_f == equalp(car(x), car(y)))
return boolean_f;
pcar_x = car(x);
pcar_y = car(y);
}
// If there is no cycle, keep checking equality
if (fast_lis == NULL ||
Cyc_is_pair(fast_lis) == boolean_f ||
cdr(fast_lis) == NULL ||
Cyc_is_pair(cdr(fast_lis)) == boolean_f ||
cddr(fast_lis) == NULL){
continue;
}
// If there is a cycle, handle it
if (slow_lis == fast_lis) {
// if this is y, both lists have cycles and are equal, return #t
if (second_cycle)
return boolean_t;
// if this is x, keep going and check for a cycle in y
second_cycle = 1;
slow_lis = y;
fast_lis = NULL;
if (Cyc_is_pair(y) == boolean_t) {
fast_lis = cdr(y);
}
continue;
}
slow_lis = cdr(slow_lis);
fast_lis = cddr(fast_lis);
}
}
list assq(void *data, object x, list l)
{
if ((l == NULL) || is_value_type(l) || type_of(l) != pair_tag)
return boolean_f;
for (; (l != NULL); l = cdr(l)) {
list la = car(l);
Cyc_check_pair(data, la);
if ((x == car(la)))
return la;
}
return boolean_f;
}
list assoc(void *data, object x, list l)
{
if ((l == NULL) || is_value_type(l) || type_of(l) != pair_tag)
return boolean_f;
for (; (l != NULL); l = cdr(l)) {
list la = car(l);
Cyc_check_pair(data, la);
if (boolean_f != equalp(x, car(la)))
return la;