forked from melvinzhang/bit-scheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit.c
1687 lines (1448 loc) · 28.7 KB
/
bit.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
/* Copyright (C) 1995 Danny Dube, Universite de Montreal. */
/* All rights reserved. */
/* Mini interprete Scheme. Troisieme d'une serie. */
/* Ne traite pas: */
/* les ports, */
/* les points flottants, */
/* les entiers de taille illimitee, */
/* l'entree de nombres non-decimaux. */
#include "bit.h"
/* Variables globales C ----------------------------------------- */
int *mem;
int mem_len, nb_handles, nb_obj, handle1, mem_next, mem_free, mem_stack;
int gc_mark, gc_compact, gc_ratio, gc_time, gc_trav, gc_vecting, gc_cut;
int gc_old, gc_new, gc_len, gc_src, gc_dst, gc_state;
int nb_symbols;
int eval_pc;
int (*cprim0[NBCPRIM0])(void) =
{
peek_char,
read_char,
quit,
return_current_continuation
};
int (*cprim1[NBCPRIM1])(int) =
{
pred_boolean,
pred_pair,
car,
cdr,
pred_char,
integer_to_char,
char_to_integer,
pred_string,
make_string,
string_length,
string_copy,
pred_symbol,
symbol_to_string,
string_to_symbol,
pred_number,
pred_vector,
make_vector,
vector_length,
pred_procedure,
write_char,
introspection
};
int (*cprim2[NBCPRIM2])(int, int) =
{
cons,
set_car,
set_cdr,
string_ref,
string_equal,
cppoe2,
cplus2,
cmoins2,
cfois2,
cdivise2,
vector_ref,
apply,
eq,
return_there_with_this
};
int (*cprim3[NBCPRIM3])(int, int, int) =
{
string_set,
vector_set
};
/* Variables globales SCM --------------------------------------- */
/* Les variables contenues dans stack */
/* doivent etre mises a jour par le gc */
int globs[NBGLOBS];
/* Allocation du monceau ---------------------------------------- */
void alloc_heap(int taille)
{
int j;
mem_len = taille;
mem = malloc(mem_len * (sizeof (int)));
if (mem == NULL)
{
fprintf(stderr, "Incapable d'allouer le monceau\n");
exit(1);
}
nb_handles = (mem_len + 4) / 5;
nb_obj = 0;
handle1 = 0;
for (j=0 ; j<nb_handles ; j++)
mem[j] = j + 1;
mem[nb_handles - 1] = -1;
mem_next = nb_handles;
mem_free = mem_len - mem_next;
gc_mark = FALSE;
gc_compact = FALSE;
gc_ratio = 2;
gc_time = 0;
gc_vecting = FALSEVAL;
gc_state = 0;
}
/* Garbage collector -------------------------------------------- */
int is_allocated(int d)
{
return !C_PRED_NUMBER(d) && !C_PRED_SPECIAL(d);
}
void mark_it(int d)
{
if (!is_allocated(d))
return;
if (IS_MARKED(d))
return;
mem[mem[SCM_TO_HANDLE(d)]] |= GC_MARK;
mem_stack --;
mem[mem_stack] = d;
}
/* La description des etats est ailleurs */
/* Note: mem_free n'est maintenu qu'entre deux phases de GC */
/* Note: meme chose pour nb_obj */
void gc(int size)
{
int court, start, j, nbcases, marque, d, handle, subnlen;
gc_time += size + size; /* Simuler une multiplication rapide */
if (gc_ratio >= 3)
{
gc_time += size;
if (gc_ratio >= 4)
{
gc_time += size;
if (gc_ratio >=5)
gc_time += size * (gc_ratio - 4);
}
}
while (gc_time > 0)
switch (gc_state)
{
case 0:
{
if (mem_free == 0)
gc_ratio = mem_len;
else
gc_ratio = (2 * (mem_next - nb_handles) +
2 * nb_obj +
2 * nb_scm_globs +
(5 * mem_free + 1) / 2 -
1) / mem_free;
gc_time += size * gc_ratio; /* Car le ratio etait insuffisant */
gc_time -= (gc_ratio *
(((2 * gc_ratio - 3) * mem_free -
4 * (mem_next - nb_handles) -
4 * nb_obj -
4 * nb_scm_globs) /
(2 * gc_ratio + 1)));
gc_time += 50; /* Securite! */
gc_state = 1;
break;
}
case 1:
{
mem_stack = mem_len;
gc_mark = TRUE;
gc_cut = 0;
gc_state = 2;
break;
}
case 2:
{
nbcases = ((nb_scm_globs - gc_cut <= gc_time) ?
nb_scm_globs - gc_cut :
gc_time);
for (j=gc_cut ; j<gc_cut+nbcases ; j++)
mark_it(scm_globs[j]);
gc_time -= nbcases;
gc_cut += nbcases;
gc_state = (gc_cut == nb_scm_globs) ? 5 : 2;
break;
}
case 3:
{
if (mem_stack == mem_len)
gc_state = 5;
else
{
gc_trav = mem[mem_stack];
mem_stack ++;
gc_src = mem[SCM_TO_HANDLE(gc_trav)];
switch (gc_trav & TYPEMASK)
{
case TYPEPAIR: /* PAIR */
{
start = 1;
gc_len = 3;
break;
}
case TYPECLOS: /* CLOSURE */
{
start = 2;
gc_len = 3;
break;
}
default:
{
subnlen = mem[gc_src + 1];
if (subnlen & CONTMASK) /* CONT */
{
start = 2;
gc_len = 5;
}
else if (subnlen & VECTINGMASK) /* STRING */
{
start =
2 + string_len_to_int_string_len(subnlen >> 2);
gc_len = start;
}
else /* VECTOR */
{
gc_len =
2 + vector_len_to_int_vector_len(subnlen >> 2);
gc_cut = 2;
gc_time -= 3;
gc_state = 4;
goto label001;
}
}
}
for (j=start ; j<gc_len ; j++)
mark_it(mem[gc_src + j]);
gc_time -= gc_len + 1;
gc_state = 3;
}
label001:
break;
}
case 4:
{
nbcases = (gc_len - gc_cut <= gc_time) ? gc_len - gc_cut : gc_time;
for (j=0 ; j<nbcases ; j++)
mark_it(mem[gc_src + gc_cut + j]);
gc_time -= nbcases;
gc_cut += nbcases;
gc_state = (gc_cut == gc_len) ? 3 : 4;
break;
}
case 5:
{
for (j=0 ; j<NBGLOBS ; j++)
mark_it(globs[j]);
gc_state = (mem_stack == mem_len) ? 6 : 3;
break;
}
case 6:
{
gc_mark = FALSE;
gc_compact = TRUE;
gc_src = nb_handles;
gc_dst = nb_handles;
gc_state = 7;
nb_obj = 0;
break;
}
case 7:
{
gc_state = (gc_src == mem_next) ? 10 : 8;
break;
}
case 8:
{
d = mem[gc_src];
marque = d & GC_MARK;
d &= ~GC_MARK;
mem[gc_src] = d;
switch(d & TYPEMASK)
{
case TYPEPAIR: /* PAIR */
case TYPECLOS: /* CLOSURE */
{
court = TRUE;
gc_len = 3;
break;
}
default:
{
subnlen = mem[gc_src + 1];
if (subnlen & CONTMASK) /* CONT */
{
court = TRUE;
gc_len = 5;
break;
}
else if (subnlen & VECTINGMASK) /* STRING */
{
court = FALSE;
gc_len =
2 + string_len_to_int_string_len(subnlen >> 2);
break;
}
else /* VECTOR */
{
court = FALSE;
gc_len =
2 + vector_len_to_int_vector_len(subnlen >> 2);
break;
}
}
}
handle = SCM_TO_HANDLE(d);
if (!marque)
{
mem[handle] = handle1;
handle1 = handle;
gc_time -= gc_len + 1;
gc_src += gc_len;
gc_state = 7;
}
else if (court)
{
mem[handle] = gc_dst;
for (j=0 ; j<gc_len ; j++)
mem[gc_dst + j] = mem[gc_src + j];
nb_obj ++;
gc_time -= gc_len + 1;
gc_src += gc_len;
gc_dst += gc_len;
gc_state = 7;
}
else
{
mem[handle] = gc_dst;
mem[gc_dst] = mem[gc_src];
mem[gc_dst + 1] = mem[gc_src + 1];
nb_obj ++;
gc_time -= 3;
gc_vecting = d;
gc_cut = 2;
gc_old = gc_src;
gc_new = gc_dst;
gc_dst += gc_len;
gc_state = 9;
}
break;
}
case 9:
{
nbcases = (gc_len - gc_cut <= gc_time) ? gc_len - gc_cut : gc_time;
for (j=0 ; j<nbcases ; j++)
mem[gc_new + gc_cut + j] = mem[gc_old + gc_cut + j];
gc_time -= nbcases;
gc_cut += nbcases;
if (gc_cut == gc_len)
{
gc_vecting = scm_false;
gc_src += gc_len;
gc_state = 7;
}
else
gc_state = 9;
break;
}
case 10:
{
mem_next = gc_dst;
/* printf("%s %d cases pour %d objets, ratio de %d, reste %d\n",
"GC completed:", mem_next - nb_handles,
nb_obj, gc_ratio, mem_free); */
mem_free = mem_len - mem_next - nb_obj;
gc_compact = FALSE;
gc_ratio = 1;
gc_time = 0;
gc_state = 0;
if (mem_free < size)
{
fprintf(stderr, "Manque de memoire\n");
exit(1);
}
break;
}
}
}
/* Fonction d'allocation de memoire ----------------------------- */
/* Un appel a cette fonction peut declencher le gc */
int alloc_cell(int len, int bitpattern)
{
int j, pos, handle, d, marque;
gc(len + 1);
if (gc_compact)
{
if (gc_dst + len <= gc_src)
{
pos = gc_dst;
gc_dst += len;
marque = 0;
}
else
{
pos = mem_next;
mem_next += len;
marque = GC_MARK;
}
}
else
{
pos = mem_next;
mem_next += len;
marque = 0;
}
handle = handle1;
handle1 = mem[handle1];
mem[handle] = pos;
d = (handle << 1) | bitpattern;
mem[pos] = d | marque;
return d;
}
/* Fonctions relatives a BOOLEAN -------------------------------- */
int pred_boolean(int d)
{
return C_PRED_BOOLEAN(d) ? scm_true : scm_false;
}
/* Fonctions relatives a PAIR ----------------------------------- */
int pred_pair(int d)
{
return C_PRED_PAIR(d) ? scm_true : scm_false;
}
int cons(int car, int cdr)
{
int handle, cell, pair;
cons_car = car;
cons_cdr = cdr;
pair = alloc_cell(3, TYPEPAIR);
handle = SCM_TO_HANDLE(pair);
cell = mem[handle];
mem[cell + 1] = cons_car;
mem[cell + 2] = cons_cdr;
return pair;
}
int car(int p)
{
return CHAMP(p, 0);
}
int cdr(int p)
{
return CHAMP(p, 1);
}
int set_car(int p, int d)
{
if (gc_mark && IS_MARKED(p))
mark_it(d);
CHAMP(p, 0) = d;
return scm_true;
}
int set_cdr(int p, int d)
{
if (gc_mark && IS_MARKED(p))
mark_it(d);
CHAMP(p, 1) = d;
return scm_true;
}
int list_copy(int l)
{
int temp;
if (l == scm_empty)
return l;
else
{
list_copy_l = l;
list_copy_courant = list_copy_tete = cons(car(list_copy_l), scm_empty);
list_copy_l = cdr(list_copy_l);
while (list_copy_l != scm_empty)
{
temp = cons(car(list_copy_l), scm_empty);
set_cdr(list_copy_courant, temp);
list_copy_l = cdr(list_copy_l);
list_copy_courant = cdr(list_copy_courant);
}
return list_copy_tete;
}
}
/* Fonctions relatives a CHAR ----------------------------------- */
int pred_char(int d)
{
return C_PRED_CHAR(d) ? scm_true : scm_false;
}
int integer_to_char(int n)
{
return TO_SCM_CHAR(to_c_number(n));
}
int char_to_integer(int c)
{
return TO_SCM_NUMBER(TO_C_CHAR(c));
}
/* Fonctions relatives a STRING --------------------------------- */
int c_pred_string(int d)
{
return (C_PRED_OTHER(d) &&
((mem[mem[SCM_TO_HANDLE(d)] + 1] & VECTINGMASK) == STRINGVAL));
}
int pred_string(int d)
{
return c_pred_string(d) ? scm_true : scm_false;
}
int c_make_string(int len)
{
int intlen, handle, cell, string;
intlen = string_len_to_int_string_len(len);
string = alloc_cell(2 + intlen, TYPEOTHER);
handle = SCM_TO_HANDLE(string);
cell = mem[handle];
mem[cell + 1] = (len << 2) | STRINGVAL;
return string;
}
int make_string(int len)
{
return c_make_string(to_c_number(len));
}
char *find_string_base(int s, int pos)
{
int intpos;
if (s != gc_vecting)
return (char *) (&mem[mem[SCM_TO_HANDLE(s)] + 2]);
else
{
intpos = (pos >> LOGCPI) + 2;
if (intpos >= gc_cut)
return (char *) (&mem[gc_old + 2]);
else
return (char *) (&mem[gc_new + 2]);
}
}
int c_string_ref(int s, int pos)
{
char *base;
base = find_string_base(s, pos);
return (int) (*(base + pos));
}
int string_ref(int s, int pos)
{
return TO_SCM_CHAR(c_string_ref(s, to_c_number(pos)));
}
void c_string_set(int s, int pos, int c)
{
char *base;
base = find_string_base(s, pos);
*(base + pos) = (char) c;
}
int string_set(int s, int pos, int c)
{
c_string_set(s, to_c_number(pos), TO_C_CHAR(c));
return scm_true;
}
int string_length(int s)
{
return TO_SCM_NUMBER(C_STRING_LEN(s));
}
int string_len_to_int_string_len(int len)
{
int intlen;
intlen = (len + CHARSPERINT - 1) >> LOGCPI;
return (intlen == 0) ? 1 : intlen;
}
int c_string_int_len(int s)
{
return string_len_to_int_string_len(C_STRING_LEN(s));
}
int to_scm_string(char *cs, int len)
{
int i, scms;
scms = c_make_string(len);
for (i=0 ; i<len ; i++)
c_string_set(scms, i, cs[i]);
return scms;
}
int c_string_equal(int s1, int s2)
{
int len1, len2, i;
len1 = C_STRING_LEN(s1);
len2 = C_STRING_LEN(s2);
if (len1 != len2)
return FALSE;
for (i=0 ; i<len1 ; i++)
if (c_string_ref(s1, i) != c_string_ref(s2, i))
return FALSE;
return TRUE;
}
int string_equal(int s1, int s2)
{
return c_string_equal(s1, s2) ? scm_true : scm_false;
}
int string_copy(int s1)
{
int len, i;
int s2;
len = C_STRING_LEN(s1);
string_copy_s1 = s1;
s2 = c_make_string(len);
for (i=0 ; i<len ; i++)
c_string_set(s2, i, c_string_ref(string_copy_s1, i));
return s2;
}
/* Fonctions relatives a SYMBOL --------------------------------- */
int pred_symbol(int d)
{
return C_PRED_SYMBOL(d) ? scm_true : scm_false;
}
void stretch_symbol_vector(void)
{
int oldlen, newlen, j;
int newnames;
oldlen = C_VECTOR_LEN(scm_symbol_names);
newlen = (4 * oldlen) / 3 + 1;
newnames = c_make_vector(newlen);
for (j=0 ; j<nb_symbols ; j++)
c_vector_set(newnames, j, c_vector_ref(scm_symbol_names, j));
scm_symbol_names = newnames;
}
/* Cette fonction ne doit etre utilisee */
/* que par string->symbol */
int make_symbol(int nom)
{
int numero;
if (nb_symbols == C_VECTOR_LEN(scm_symbol_names))
stretch_symbol_vector();
numero = nb_symbols;
nb_symbols ++;
c_vector_set(scm_symbol_names, numero, nom);
return NUMBER_TO_SYMBOL(numero);
}
int symbol_to_string(int s)
{
return c_vector_ref(scm_symbol_names, SYMBOL_NUMBER(s));
}
int string_to_symbol(int string)
{
int j;
for (j=0 ; j<nb_symbols ; j++)
if (c_string_equal(string, c_vector_ref(scm_symbol_names, j)))
return NUMBER_TO_SYMBOL(j);
return make_symbol(string_copy(string));
}
/* Fonctions relatives a NUMBER --------------------------------- */
int pred_number(int d)
{
return C_PRED_NUMBER(d) ? scm_true : scm_false;
}
int to_c_number(int n)
{
if (n < 0)
return (n >> 1) | BITH1;
else
return n >> 1;
}
/* Les op. se font AVEC les tags */
int cppoe2(int n1, int n2)
{
return (n1 <= n2) ? scm_true : scm_false;
}
int cplus2(int n1, int n2)
{
return n1 + n2 - NUMVAL;
}
int cmoins2(int n1, int n2)
{
return n1 - n2 + NUMVAL;
}
int cfois2(int n1, int n2)
{
return TO_SCM_NUMBER(to_c_number(n1) * to_c_number(n2));
}
int cdivise2(int n1, int n2)
{
return TO_SCM_NUMBER(to_c_number(n1) / to_c_number(n2));
}
/* Fonctions relatives a VECTOR --------------------------------- */
int c_pred_vector(int d)
{
return (C_PRED_OTHER(d) &&
((mem[mem[SCM_TO_HANDLE(d)] + 1] & VECTINGMASK) == VECTORVAL));
}
int pred_vector(int d)
{
return c_pred_vector(d) ? scm_true : scm_false;
}
int c_make_vector(int len)
{
int intlen, j;
int cell, handle, vector;
intlen = vector_len_to_int_vector_len(len);
vector = alloc_cell(2 + intlen, TYPEOTHER);
handle = SCM_TO_HANDLE(vector);
cell = mem[handle];
mem[cell + 1] = (len << 2) | VECTORVAL;
for (j=0 ; j<intlen ; j++)
mem[cell + j + 2] = scm_false;
return vector;
}
int make_vector(int len)
{
return c_make_vector(to_c_number(len));
}
int find_vector_location(int v, int pos)
{
if (v != gc_vecting)
return mem[SCM_TO_HANDLE(v)];
else if (2 + pos >= gc_cut)
return gc_old;
else
return gc_new;
}
int c_vector_ref(int v, int pos)
{
int location;
location = find_vector_location(v, pos);
return mem[location + 2 + pos];
}
int vector_ref(int v, int pos)
{
return c_vector_ref(v, to_c_number(pos));
}
void c_vector_set(int v, int pos, int val)
{
int location;
if (gc_mark && IS_MARKED(v))
mark_it(val);
location = find_vector_location(v, pos);
mem[location + 2 + pos] = val;
}
int vector_set(int v, int pos, int val)
{
c_vector_set(v, to_c_number(pos), val);
return scm_true;
}
int vector_length(int v)
{
return TO_SCM_NUMBER(C_VECTOR_LEN(v));
}
int vector_len_to_int_vector_len(int len)
{
return (len == 0) ? 1 : len;
}
int c_vector_int_len(int v)
{
return vector_len_to_int_vector_len(C_VECTOR_LEN(v));
}
/* Fonctions relatives a PROCEDURE ------------------------------ */
int pred_procedure(int d)
{
return (C_PRED_CPRIM(d) || C_PRED_CLOSURE(d)) ? scm_true : scm_false;
}
int make_closure(int entry, int env)
{
int cell, handle, closure;
make_clos_env = env;
closure = alloc_cell(3, TYPECLOS);
handle = SCM_TO_HANDLE(closure);
cell = mem[handle];
mem[cell + 1] = entry;
mem[cell + 2] = make_clos_env;
return closure;
}
int closure_entry(int c)
{
return CHAMP(c, 0);
}
int closure_env(int c)
{
return CHAMP(c, 1);
}
/* Cette fonction ne peut appeler direct. c_apply */
/* Note: elle utilise le apply-hook a la fin du bytecode */
int apply(int c, int args)
{
eval_pc = bytecode_len - 1;
eval_args = cons(c, args);
return scm_false;
}
/* Fonctions relatives a CONT ----------------------------------- */
int c_pred_cont(int d)
{
return (C_PRED_OTHER(d) &&
((mem[mem[SCM_TO_HANDLE(d)] + 1] & CONTMASK) == CONTVAL));
}
int make_cont(void)
{
int cell, handle, k;
k = alloc_cell(5, TYPEOTHER);
handle = SCM_TO_HANDLE(k);
cell = mem[handle];
mem[cell + 1] = PC_TO_PCTAG(eval_pc);
mem[cell + 2] = eval_env;
mem[cell + 3] = eval_args;
mem[cell + 4] = eval_cont;
return k;
}
void set_cont_pc(int k, int dest)
{
CHAMP(k, 0) = PC_TO_PCTAG(dest);
}
void restore_cont(int k)
{
int cell;
cell = mem[SCM_TO_HANDLE(k)];
eval_pc = PCTAG_TO_PC(mem[cell + 1]);
eval_env = mem[cell + 2];
eval_args = mem[cell + 3];
eval_cont = mem[cell + 4];
}
/* Fonctions d'entree/sortie ------------------------------------ */
int ll_input(void)
{
int c;
c = getchar();
/*
if (c == EOF)
{
printf("EOF\nQuit\n");
exit(0);
}
*/
return c;
}
static int look_ahead;
static int look_ahead_valide;
int c_peek_char(void)
{
if (!look_ahead_valide)
{
look_ahead = ll_input();
look_ahead_valide = TRUE;
}
return look_ahead;
}