ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/microscheme/scheme.c
(Generate patch)

Comparing cvsroot/microscheme/scheme.c (file contents):
Revision 1.43 by root, Mon Nov 30 06:40:57 2015 UTC vs.
Revision 1.65 by root, Wed Dec 2 17:01:51 2015 UTC

16 * (MINISCM) This is a revised and modified version by Akira KIDA. 16 * (MINISCM) This is a revised and modified version by Akira KIDA.
17 * (MINISCM) current version is 0.85k4 (15 May 1994) 17 * (MINISCM) current version is 0.85k4 (15 May 1994)
18 * 18 *
19 */ 19 */
20 20
21#define EXPERIMENT 1 21#define _POSIX_C_SOURCE 200201
22 22#define _XOPEN_SOURCE 600
23#define PAGE_SIZE 4096 /* does not work on sparc/alpha */ 23#define _GNU_SOURCE 1 /* for malloc mremap */
24#include "malloc.c"
25 24
26#define SCHEME_SOURCE 25#define SCHEME_SOURCE
27#include "scheme-private.h" 26#include "scheme-private.h"
28#ifndef WIN32 27#ifndef WIN32
29# include <unistd.h> 28# include <unistd.h>
30#endif 29#endif
31#if USE_MATH 30#if USE_MATH
32# include <math.h> 31# include <math.h>
33#endif 32#endif
34 33
34#define ECB_NO_THREADS 1
35#include "ecb.h" 35#include "ecb.h"
36 36
37#include <sys/types.h> 37#include <sys/types.h>
38#include <sys/stat.h> 38#include <sys/stat.h>
39#include <fcntl.h> 39#include <fcntl.h>
47#include <string.h> 47#include <string.h>
48 48
49#include <limits.h> 49#include <limits.h>
50#include <inttypes.h> 50#include <inttypes.h>
51#include <float.h> 51#include <float.h>
52//#include <ctype.h> 52
53#if !USE_SYSTEM_MALLOC
54# define PAGE_SIZE 4096 /* does not work on sparc/alpha */
55# include "malloc.c"
56# define malloc(n) tiny_malloc (n)
57# define realloc(p,n) tiny_realloc (p, n)
58# define free(p) tiny_free (p)
59#endif
53 60
54#if '1' != '0' + 1 \ 61#if '1' != '0' + 1 \
55 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ 62 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \
56 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ 63 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \
57 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ 64 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \
79 86
80#define BACKQUOTE '`' 87#define BACKQUOTE '`'
81#define WHITESPACE " \t\r\n\v\f" 88#define WHITESPACE " \t\r\n\v\f"
82#define DELIMITERS "()\";" WHITESPACE 89#define DELIMITERS "()\";" WHITESPACE
83 90
84#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 91#define NIL POINTER (&SCHEME_V->xNIL)
85#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 92#define S_T POINTER (&SCHEME_V->xT)
86#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 93#define S_F POINTER (&SCHEME_V->xF)
87#define S_SINK (&SCHEME_V->xsink) 94#define S_SINK POINTER (&SCHEME_V->xsink)
88#define S_EOF (&SCHEME_V->xEOF_OBJ) 95#define S_EOF POINTER (&SCHEME_V->xEOF_OBJ)
89 96
90#if !USE_MULTIPLICITY 97#if !USE_MULTIPLICITY
91static scheme sc; 98static scheme sc;
92#endif 99#endif
93 100
94static void 101ecb_cold static void
95xbase (char *s, long n, int base) 102xbase (char *s, long n, int base)
96{ 103{
97 if (n < 0) 104 if (n < 0)
98 { 105 {
99 *s++ = '-'; 106 *s++ = '-';
101 } 108 }
102 109
103 char *p = s; 110 char *p = s;
104 111
105 do { 112 do {
106 *p++ = '0' + n % base; 113 *p++ = "0123456789abcdef"[n % base];
107 n /= base; 114 n /= base;
108 } while (n); 115 } while (n);
109 116
110 *p-- = 0; 117 *p-- = 0;
111 118
114 char x = *s; *s = *p; *p = x; 121 char x = *s; *s = *p; *p = x;
115 --p; ++s; 122 --p; ++s;
116 } 123 }
117} 124}
118 125
119static void 126ecb_cold static void
120xnum (char *s, long n) 127xnum (char *s, long n)
121{ 128{
122 xbase (s, n, 10); 129 xbase (s, n, 10);
123} 130}
124 131
125static void 132ecb_cold static void
126xwrstr (const char *s) 133putnum (SCHEME_P_ long n)
127{
128 write (1, s, strlen (s));
129}
130
131static void
132xwrnum (long n)
133{ 134{
134 char buf[64]; 135 char buf[64];
135 136
136 xnum (buf, n); 137 xnum (buf, n);
137 xwrstr (buf); 138 putstr (SCHEME_A_ buf);
138} 139}
140
141#if USE_CHAR_CLASSIFIERS
142#include <ctype.h>
143#else
139 144
140static char 145static char
141xtoupper (char c) 146xtoupper (char c)
142{ 147{
143 if (c >= 'a' && c <= 'z') 148 if (c >= 'a' && c <= 'z')
163 168
164#define toupper(c) xtoupper (c) 169#define toupper(c) xtoupper (c)
165#define tolower(c) xtolower (c) 170#define tolower(c) xtolower (c)
166#define isdigit(c) xisdigit (c) 171#define isdigit(c) xisdigit (c)
167 172
173#endif
174
168#if USE_IGNORECASE 175#if USE_IGNORECASE
169static const char * 176ecb_cold static const char *
170xstrlwr (char *s) 177xstrlwr (char *s)
171{ 178{
172 const char *p = s; 179 const char *p = s;
173 180
174 while (*s) 181 while (*s)
187# define stricmp(a,b) strcmp (a, b) 194# define stricmp(a,b) strcmp (a, b)
188# define strlwr(s) (s) 195# define strlwr(s) (s)
189#endif 196#endif
190 197
191#ifndef prompt 198#ifndef prompt
192# define prompt "ts> " 199# define prompt "ms> "
193#endif 200#endif
194 201
195#ifndef InitFile 202#ifndef InitFile
196# define InitFile "init.scm" 203# define InitFile "init.scm"
197#endif 204#endif
198 205
199#ifndef FIRST_CELLSEGS
200# define FIRST_CELLSEGS 3
201#endif
202
203enum scheme_types 206enum scheme_types
204{ 207{
205 T_INTEGER, 208 T_INTEGER,
209 T_CHARACTER,
206 T_REAL, 210 T_REAL,
207 T_STRING, 211 T_STRING,
208 T_SYMBOL, 212 T_SYMBOL,
209 T_PROC, 213 T_PROC,
210 T_PAIR, /* also used for free cells */ 214 T_PAIR, /* also used for free cells */
211 T_CLOSURE, 215 T_CLOSURE,
216 T_BYTECODE, // temp
217 T_MACRO,
212 T_CONTINUATION, 218 T_CONTINUATION,
213 T_FOREIGN, 219 T_FOREIGN,
214 T_CHARACTER,
215 T_PORT, 220 T_PORT,
216 T_VECTOR, 221 T_VECTOR,
217 T_MACRO,
218 T_PROMISE, 222 T_PROMISE,
219 T_ENVIRONMENT, 223 T_ENVIRONMENT,
220 /* one more... */ 224
221 T_NUM_SYSTEM_TYPES 225 T_NUM_SYSTEM_TYPES
222}; 226};
223 227
224#define T_MASKTYPE 0x000f 228#define T_MASKTYPE 0x000f
225#define T_SYNTAX 0x0010 229#define T_SYNTAX 0x0010
258static num num_op (enum num_op op, num a, num b); 262static num num_op (enum num_op op, num a, num b);
259static num num_intdiv (num a, num b); 263static num num_intdiv (num a, num b);
260static num num_rem (num a, num b); 264static num num_rem (num a, num b);
261static num num_mod (num a, num b); 265static num num_mod (num a, num b);
262 266
263#if USE_MATH
264static double round_per_R5RS (double x);
265#endif
266static int is_zero_rvalue (RVALUE x); 267static int is_zero_rvalue (RVALUE x);
267 268
268static num num_zero; 269static num num_zero;
269static num num_one; 270static num num_one;
270 271
272/* convert "pointer" to cell* / cell* to pointer */
273#define CELL(p) ((struct cell *)(p) + 0)
274#define POINTER(c) ((void *)((c) - 0))
275
271/* macros for cell operations */ 276/* macros for cell operations */
272#define typeflag(p) ((p)->flag + 0) 277#define typeflag(p) (CELL(p)->flag + 0)
273#define set_typeflag(p,v) ((p)->flag = (v)) 278#define set_typeflag(p,v) (CELL(p)->flag = (v))
274#define type(p) (typeflag (p) & T_MASKTYPE) 279#define type(p) (typeflag (p) & T_MASKTYPE)
275 280
276INTERFACE int 281INTERFACE int
277is_string (pointer p) 282is_string (pointer p)
278{ 283{
279 return type (p) == T_STRING; 284 return type (p) == T_STRING;
280} 285}
281 286
282#define strvalue(p) ((p)->object.string.svalue) 287#define strvalue(p) (CELL(p)->object.string.svalue)
283#define strlength(p) ((p)->object.string.length) 288#define strlength(p) (CELL(p)->object.string.length)
284 289
285INTERFACE int 290INTERFACE int
286is_vector (pointer p) 291is_vector (pointer p)
287{ 292{
288 return type (p) == T_VECTOR; 293 return type (p) == T_VECTOR;
289} 294}
290 295
291#define vecvalue(p) ((p)->object.vector.vvalue) 296#define vecvalue(p) (CELL(p)->object.vector.vvalue)
292#define veclength(p) ((p)->object.vector.length) 297#define veclength(p) (CELL(p)->object.vector.length)
293INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); 298INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
294INTERFACE pointer vector_get (pointer vec, uint32_t ielem); 299INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
295INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); 300INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
296 301
297INTERFACE int 302INTERFACE int
323string_value (pointer p) 328string_value (pointer p)
324{ 329{
325 return strvalue (p); 330 return strvalue (p);
326} 331}
327 332
328#define ivalue_unchecked(p) (p)->object.ivalue 333#define ivalue_unchecked(p) CELL(p)->object.ivalue
329#define set_ivalue(p,v) (p)->object.ivalue = (v) 334#define set_ivalue(p,v) CELL(p)->object.ivalue = (v)
330 335
331#if USE_REAL 336#if USE_REAL
332#define rvalue_unchecked(p) (p)->object.rvalue 337#define rvalue_unchecked(p) CELL(p)->object.rvalue
333#define set_rvalue(p,v) (p)->object.rvalue = (v) 338#define set_rvalue(p,v) CELL(p)->object.rvalue = (v)
334#else 339#else
335#define rvalue_unchecked(p) (p)->object.ivalue 340#define rvalue_unchecked(p) CELL(p)->object.ivalue
336#define set_rvalue(p,v) (p)->object.ivalue = (v) 341#define set_rvalue(p,v) CELL(p)->object.ivalue = (v)
337#endif 342#endif
338 343
339INTERFACE long 344INTERFACE long
340charvalue (pointer p) 345charvalue (pointer p)
341{ 346{
342 return ivalue_unchecked (p); 347 return ivalue_unchecked (p);
343} 348}
344 349
350#define port(p) CELL(p)->object.port
351#define set_port(p,v) port(p) = (v)
345INTERFACE int 352INTERFACE int
346is_port (pointer p) 353is_port (pointer p)
347{ 354{
348 return type (p) == T_PORT; 355 return type (p) == T_PORT;
349} 356}
350 357
351INTERFACE int 358INTERFACE int
352is_inport (pointer p) 359is_inport (pointer p)
353{ 360{
354 return is_port (p) && p->object.port->kind & port_input; 361 return is_port (p) && port (p)->kind & port_input;
355} 362}
356 363
357INTERFACE int 364INTERFACE int
358is_outport (pointer p) 365is_outport (pointer p)
359{ 366{
360 return is_port (p) && p->object.port->kind & port_output; 367 return is_port (p) && port (p)->kind & port_output;
361} 368}
362 369
363INTERFACE int 370INTERFACE int
364is_pair (pointer p) 371is_pair (pointer p)
365{ 372{
366 return type (p) == T_PAIR; 373 return type (p) == T_PAIR;
367} 374}
368 375
369#define car(p) ((p)->object.cons.car + 0) 376#define car(p) (POINTER (CELL(p)->object.cons.car))
370#define cdr(p) ((p)->object.cons.cdr + 0) 377#define cdr(p) (POINTER (CELL(p)->object.cons.cdr))
371 378
372static pointer caar (pointer p) { return car (car (p)); } 379static pointer caar (pointer p) { return car (car (p)); }
373static pointer cadr (pointer p) { return car (cdr (p)); } 380static pointer cadr (pointer p) { return car (cdr (p)); }
374static pointer cdar (pointer p) { return cdr (car (p)); } 381static pointer cdar (pointer p) { return cdr (car (p)); }
375static pointer cddr (pointer p) { return cdr (cdr (p)); } 382static pointer cddr (pointer p) { return cdr (cdr (p)); }
376 383
377static pointer cadar (pointer p) { return car (cdr (car (p))); } 384static pointer cadar (pointer p) { return car (cdr (car (p))); }
378static pointer caddr (pointer p) { return car (cdr (cdr (p))); } 385static pointer caddr (pointer p) { return car (cdr (cdr (p))); }
379static pointer cdaar (pointer p) { return cdr (car (car (p))); } 386static pointer cdaar (pointer p) { return cdr (car (car (p))); }
380 387
388static pointer cadddr (pointer p) { return car (car (car (cdr (p)))); }
389
381INTERFACE void 390INTERFACE void
382set_car (pointer p, pointer q) 391set_car (pointer p, pointer q)
383{ 392{
384 p->object.cons.car = q; 393 CELL(p)->object.cons.car = CELL (q);
385} 394}
386 395
387INTERFACE void 396INTERFACE void
388set_cdr (pointer p, pointer q) 397set_cdr (pointer p, pointer q)
389{ 398{
390 p->object.cons.cdr = q; 399 CELL(p)->object.cons.cdr = CELL (q);
391} 400}
392 401
393INTERFACE pointer 402INTERFACE pointer
394pair_car (pointer p) 403pair_car (pointer p)
395{ 404{
413{ 422{
414 return strvalue (p); 423 return strvalue (p);
415} 424}
416 425
417#if USE_PLIST 426#if USE_PLIST
427#error plists are broken because symbols are no longer pairs
418#define symprop(p) cdr(p) 428#define symprop(p) cdr(p)
419SCHEME_EXPORT int 429SCHEME_EXPORT int
420hasprop (pointer p) 430hasprop (pointer p)
421{ 431{
422 return typeflag (p) & T_SYMBOL; 432 return typeflag (p) & T_SYMBOL;
524 proper list: length 534 proper list: length
525 circular list: -1 535 circular list: -1
526 not even a pair: -2 536 not even a pair: -2
527 dotted list: -2 minus length before dot 537 dotted list: -2 minus length before dot
528*/ 538*/
529INTERFACE int 539ecb_hot INTERFACE int
530list_length (SCHEME_P_ pointer a) 540list_length (SCHEME_P_ pointer a)
531{ 541{
532 int i = 0; 542 int i = 0;
533 pointer slow, fast; 543 pointer slow, fast;
534 544
573{ 583{
574 return list_length (SCHEME_A_ a) >= 0; 584 return list_length (SCHEME_A_ a) >= 0;
575} 585}
576 586
577#if USE_CHAR_CLASSIFIERS 587#if USE_CHAR_CLASSIFIERS
588
578ecb_inline int 589ecb_inline int
579Cisalpha (int c) 590Cisalpha (int c)
580{ 591{
581 return isascii (c) && isalpha (c); 592 return isascii (c) && isalpha (c);
582} 593}
640 "gs", 651 "gs",
641 "rs", 652 "rs",
642 "us" 653 "us"
643}; 654};
644 655
645static int 656ecb_cold static int
646is_ascii_name (const char *name, int *pc) 657is_ascii_name (const char *name, int *pc)
647{ 658{
648 int i; 659 int i;
649 660
650 for (i = 0; i < 32; i++) 661 for (i = 0; i < 32; i++)
669 680
670static int file_push (SCHEME_P_ const char *fname); 681static int file_push (SCHEME_P_ const char *fname);
671static void file_pop (SCHEME_P); 682static void file_pop (SCHEME_P);
672static int file_interactive (SCHEME_P); 683static int file_interactive (SCHEME_P);
673ecb_inline int is_one_of (const char *s, int c); 684ecb_inline int is_one_of (const char *s, int c);
674static int alloc_cellseg (SCHEME_P_ int n); 685static int alloc_cellseg (SCHEME_P);
675ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 686ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
676static void finalize_cell (SCHEME_P_ pointer a); 687static void finalize_cell (SCHEME_P_ pointer a);
677static int count_consecutive_cells (pointer x, int needed);
678static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 688static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
679static pointer mk_number (SCHEME_P_ const num n); 689static pointer mk_number (SCHEME_P_ const num n);
680static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 690static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
681static pointer mk_vector (SCHEME_P_ uint32_t len); 691static pointer mk_vector (SCHEME_P_ uint32_t len);
682static pointer mk_atom (SCHEME_P_ char *q); 692static pointer mk_atom (SCHEME_P_ char *q);
683static pointer mk_sharp_const (SCHEME_P_ char *name); 693static pointer mk_sharp_const (SCHEME_P_ char *name);
684 694
695static pointer mk_port (SCHEME_P_ port *p);
696
685#if USE_PORTS 697#if USE_PORTS
686static pointer mk_port (SCHEME_P_ port *p);
687static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); 698static pointer port_from_filename (SCHEME_P_ const char *fn, int prop);
688static pointer port_from_file (SCHEME_P_ int, int prop); 699static pointer port_from_file (SCHEME_P_ int, int prop);
689static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 700static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
690static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); 701static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop);
691static port *port_rep_from_file (SCHEME_P_ int, int prop); 702static port *port_rep_from_file (SCHEME_P_ int, int prop);
692static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 703static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
693static void port_close (SCHEME_P_ pointer p, int flag); 704static void port_close (SCHEME_P_ pointer p, int flag);
694#endif 705#endif
706
695static void mark (pointer a); 707static void mark (pointer a);
696static void gc (SCHEME_P_ pointer a, pointer b); 708static void gc (SCHEME_P_ pointer a, pointer b);
697static int basic_inchar (port *pt); 709static int basic_inchar (port *pt);
698static int inchar (SCHEME_P); 710static int inchar (SCHEME_P);
699static void backchar (SCHEME_P_ int c); 711static void backchar (SCHEME_P_ int c);
700static char *readstr_upto (SCHEME_P_ int skip, const char *delim); 712static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
701static pointer readstrexp (SCHEME_P_ char delim); 713static pointer readstrexp (SCHEME_P_ char delim);
702ecb_inline int skipspace (SCHEME_P); 714static int skipspace (SCHEME_P);
703static int token (SCHEME_P); 715static int token (SCHEME_P);
704static void printslashstring (SCHEME_P_ char *s, int len); 716static void printslashstring (SCHEME_P_ char *s, int len);
705static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 717static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
706static void printatom (SCHEME_P_ pointer l, int f); 718static void printatom (SCHEME_P_ pointer l, int f);
707static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 719static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
873 } 885 }
874 886
875 return ret; 887 return ret;
876} 888}
877 889
878#if USE_MATH
879
880/* Round to nearest. Round to even if midway */
881static double
882round_per_R5RS (double x)
883{
884 double fl = floor (x);
885 double ce = ceil (x);
886 double dfl = x - fl;
887 double dce = ce - x;
888
889 if (dfl > dce)
890 return ce;
891 else if (dfl < dce)
892 return fl;
893 else
894 {
895 if (fmod (fl, 2) == 0) /* I imagine this holds */
896 return fl;
897 else
898 return ce;
899 }
900}
901#endif
902
903static int 890static int
904is_zero_rvalue (RVALUE x) 891is_zero_rvalue (RVALUE x)
905{ 892{
906 return x == 0; 893 return x == 0;
907#if 0 894#if 0
912#endif 899#endif
913#endif 900#endif
914} 901}
915 902
916/* allocate new cell segment */ 903/* allocate new cell segment */
917static int 904ecb_cold static int
918alloc_cellseg (SCHEME_P_ int n) 905alloc_cellseg (SCHEME_P)
919{ 906{
920 pointer newp; 907 struct cell *newp;
921 pointer last; 908 struct cell *last;
922 pointer p; 909 struct cell *p;
923 char *cp; 910 char *cp;
924 long i; 911 long i;
925 int k; 912 int k;
926 913
927 static int segsize = CELL_SEGSIZE >> 1; 914 static int segsize = CELL_SEGSIZE >> 1;
928 segsize <<= 1; 915 segsize <<= 1;
929 916
930 for (k = 0; k < n; k++)
931 {
932 if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1)
933 return k;
934
935 cp = malloc (segsize * sizeof (struct cell)); 917 cp = malloc (segsize * sizeof (struct cell));
936 918
937 if (!cp && USE_ERROR_CHECKING) 919 if (!cp && USE_ERROR_CHECKING)
938 return k; 920 return k;
939 921
940 i = ++SCHEME_V->last_cell_seg; 922 i = ++SCHEME_V->last_cell_seg;
941 SCHEME_V->alloc_seg[i] = cp;
942 923
943 newp = (pointer)cp; 924 newp = (struct cell *)cp;
944 SCHEME_V->cell_seg[i] = newp; 925 SCHEME_V->cell_seg[i] = newp;
945 SCHEME_V->cell_segsize[i] = segsize; 926 SCHEME_V->cell_segsize[i] = segsize;
946 SCHEME_V->fcells += segsize; 927 SCHEME_V->fcells += segsize;
947 last = newp + segsize - 1; 928 last = newp + segsize - 1;
948 929
949 for (p = newp; p <= last; p++) 930 for (p = newp; p <= last; p++)
950 { 931 {
932 pointer cp = POINTER (p);
951 set_typeflag (p, T_PAIR); 933 set_typeflag (cp, T_PAIR);
952 set_car (p, NIL); 934 set_car (cp, NIL);
953 set_cdr (p, p + 1); 935 set_cdr (cp, POINTER (p + 1));
954 } 936 }
955 937
956 set_cdr (last, SCHEME_V->free_cell); 938 set_cdr (POINTER (last), SCHEME_V->free_cell);
957 SCHEME_V->free_cell = newp; 939 SCHEME_V->free_cell = POINTER (newp);
958 }
959 940
960 return n; 941 return 1;
961} 942}
962 943
963/* get new cell. parameter a, b is marked by gc. */ 944/* get new cell. parameter a, b is marked by gc. */
964ecb_inline pointer 945ecb_inline pointer
965get_cell_x (SCHEME_P_ pointer a, pointer b) 946get_cell_x (SCHEME_P_ pointer a, pointer b)
969 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 950 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
970 return S_SINK; 951 return S_SINK;
971 952
972 if (SCHEME_V->free_cell == NIL) 953 if (SCHEME_V->free_cell == NIL)
973 { 954 {
974 const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; 955 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2;
975 956
976 gc (SCHEME_A_ a, b); 957 gc (SCHEME_A_ a, b);
977 958
978 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 959 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
979 { 960 {
980 /* if only a few recovered, get more to avoid fruitless gc's */ 961 /* if only a few recovered, get more to avoid fruitless gc's */
981 if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) 962 if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL)
982 { 963 {
983#if USE_ERROR_CHECKING 964#if USE_ERROR_CHECKING
984 SCHEME_V->no_memory = 1; 965 SCHEME_V->no_memory = 1;
985 return S_SINK; 966 return S_SINK;
986#endif 967#endif
998 } 979 }
999} 980}
1000 981
1001/* To retain recent allocs before interpreter knows about them - 982/* To retain recent allocs before interpreter knows about them -
1002 Tehom */ 983 Tehom */
1003 984ecb_hot static void
1004static void
1005push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 985push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
1006{ 986{
1007 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 987 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1008 988
1009 set_typeflag (holder, T_PAIR); 989 set_typeflag (holder, T_PAIR);
1011 set_car (holder, recent); 991 set_car (holder, recent);
1012 set_cdr (holder, car (S_SINK)); 992 set_cdr (holder, car (S_SINK));
1013 set_car (S_SINK, holder); 993 set_car (S_SINK, holder);
1014} 994}
1015 995
1016static pointer 996ecb_hot static pointer
1017get_cell (SCHEME_P_ pointer a, pointer b) 997get_cell (SCHEME_P_ pointer a, pointer b)
1018{ 998{
1019 pointer cell = get_cell_x (SCHEME_A_ a, b); 999 pointer cell = get_cell_x (SCHEME_A_ a, b);
1020 1000
1021 /* For right now, include "a" and "b" in "cell" so that gc doesn't 1001 /* For right now, include "a" and "b" in "cell" so that gc doesn't
1030} 1010}
1031 1011
1032static pointer 1012static pointer
1033get_vector_object (SCHEME_P_ uint32_t len, pointer init) 1013get_vector_object (SCHEME_P_ uint32_t len, pointer init)
1034{ 1014{
1035 pointer v = get_cell_x (SCHEME_A_ 0, 0); 1015 pointer v = get_cell_x (SCHEME_A_ NIL, NIL);
1036 pointer *e = malloc (len * sizeof (pointer)); 1016 pointer *e = malloc (len * sizeof (pointer));
1037 1017
1038 if (!e && USE_ERROR_CHECKING) 1018 if (!e && USE_ERROR_CHECKING)
1039 return S_SINK; 1019 return S_SINK;
1040 1020
1041 /* Record it as a vector so that gc understands it. */ 1021 /* Record it as a vector so that gc understands it. */
1042 set_typeflag (v, T_VECTOR | T_ATOM); 1022 set_typeflag (v, T_VECTOR | T_ATOM);
1043 1023
1044 v->object.vector.vvalue = e; 1024 CELL(v)->object.vector.vvalue = e;
1045 v->object.vector.length = len; 1025 CELL(v)->object.vector.length = len;
1046 fill_vector (v, 0, init); 1026 fill_vector (v, 0, init);
1047 push_recent_alloc (SCHEME_A_ v, NIL); 1027 push_recent_alloc (SCHEME_A_ v, NIL);
1048 1028
1049 return v; 1029 return v;
1050} 1030}
1059static void 1039static void
1060check_cell_alloced (pointer p, int expect_alloced) 1040check_cell_alloced (pointer p, int expect_alloced)
1061{ 1041{
1062 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ 1042 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */
1063 if (typeflag (p) & !expect_alloced) 1043 if (typeflag (p) & !expect_alloced)
1064 xwrstr ("Cell is already allocated!\n"); 1044 putstr (SCHEME_A_ "Cell is already allocated!\n");
1065 1045
1066 if (!(typeflag (p)) & expect_alloced) 1046 if (!(typeflag (p)) & expect_alloced)
1067 xwrstr ("Cell is not allocated!\n"); 1047 putstr (SCHEME_A_ "Cell is not allocated!\n");
1068} 1048}
1069 1049
1070static void 1050static void
1071check_range_alloced (pointer p, int n, int expect_alloced) 1051check_range_alloced (pointer p, int n, int expect_alloced)
1072{ 1052{
1078#endif 1058#endif
1079 1059
1080/* Medium level cell allocation */ 1060/* Medium level cell allocation */
1081 1061
1082/* get new cons cell */ 1062/* get new cons cell */
1083pointer 1063ecb_hot static pointer
1084xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1064xcons (SCHEME_P_ pointer a, pointer b)
1085{ 1065{
1086 pointer x = get_cell (SCHEME_A_ a, b); 1066 pointer x = get_cell (SCHEME_A_ a, b);
1087 1067
1088 set_typeflag (x, T_PAIR); 1068 set_typeflag (x, T_PAIR);
1089
1090 if (immutable)
1091 setimmutable (x);
1092 1069
1093 set_car (x, a); 1070 set_car (x, a);
1094 set_cdr (x, b); 1071 set_cdr (x, b);
1095 1072
1096 return x; 1073 return x;
1097} 1074}
1098 1075
1099static pointer 1076ecb_hot static pointer
1077ximmutable_cons (SCHEME_P_ pointer a, pointer b)
1078{
1079 pointer x = xcons (SCHEME_A_ a, b);
1080 setimmutable (x);
1081 return x;
1082}
1083
1084#define cons(a,b) xcons (SCHEME_A_ a, b)
1085#define immutable_cons(a,b) ximmutable_cons (SCHEME_A_ a, b)
1086
1087ecb_cold static pointer
1100generate_symbol (SCHEME_P_ const char *name) 1088generate_symbol (SCHEME_P_ const char *name)
1101{ 1089{
1102 pointer x = mk_string (SCHEME_A_ name); 1090 pointer x = mk_string (SCHEME_A_ name);
1103 setimmutable (x); 1091 setimmutable (x);
1104 set_typeflag (x, T_SYMBOL | T_ATOM); 1092 set_typeflag (x, T_SYMBOL | T_ATOM);
1110#ifndef USE_OBJECT_LIST 1098#ifndef USE_OBJECT_LIST
1111 1099
1112static int 1100static int
1113hash_fn (const char *key, int table_size) 1101hash_fn (const char *key, int table_size)
1114{ 1102{
1115 const unsigned char *p = key; 1103 const unsigned char *p = (unsigned char *)key;
1116 uint32_t hash = 2166136261; 1104 uint32_t hash = 2166136261U;
1117 1105
1118 while (*p) 1106 while (*p)
1119 hash = (hash ^ *p++) * 16777619; 1107 hash = (hash ^ *p++) * 16777619;
1120 1108
1121 return hash % table_size; 1109 return hash % table_size;
1122} 1110}
1123 1111
1124static pointer 1112ecb_cold static pointer
1125oblist_initial_value (SCHEME_P) 1113oblist_initial_value (SCHEME_P)
1126{ 1114{
1127 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1115 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1128} 1116}
1129 1117
1130/* returns the new symbol */ 1118/* returns the new symbol */
1131static pointer 1119ecb_cold static pointer
1132oblist_add_by_name (SCHEME_P_ const char *name) 1120oblist_add_by_name (SCHEME_P_ const char *name)
1133{ 1121{
1134 pointer x = generate_symbol (SCHEME_A_ name); 1122 pointer x = generate_symbol (SCHEME_A_ name);
1135 int location = hash_fn (name, veclength (SCHEME_V->oblist)); 1123 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1136 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); 1124 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1137 return x; 1125 return x;
1138} 1126}
1139 1127
1140ecb_inline pointer 1128ecb_cold static pointer
1141oblist_find_by_name (SCHEME_P_ const char *name) 1129oblist_find_by_name (SCHEME_P_ const char *name)
1142{ 1130{
1143 int location; 1131 int location;
1144 pointer x; 1132 pointer x;
1145 char *s; 1133 char *s;
1156 } 1144 }
1157 1145
1158 return NIL; 1146 return NIL;
1159} 1147}
1160 1148
1161static pointer 1149ecb_cold static pointer
1162oblist_all_symbols (SCHEME_P) 1150oblist_all_symbols (SCHEME_P)
1163{ 1151{
1164 int i; 1152 int i;
1165 pointer x; 1153 pointer x;
1166 pointer ob_list = NIL; 1154 pointer ob_list = NIL;
1172 return ob_list; 1160 return ob_list;
1173} 1161}
1174 1162
1175#else 1163#else
1176 1164
1177static pointer 1165ecb_cold static pointer
1178oblist_initial_value (SCHEME_P) 1166oblist_initial_value (SCHEME_P)
1179{ 1167{
1180 return NIL; 1168 return NIL;
1181} 1169}
1182 1170
1183ecb_inline pointer 1171ecb_cold static pointer
1184oblist_find_by_name (SCHEME_P_ const char *name) 1172oblist_find_by_name (SCHEME_P_ const char *name)
1185{ 1173{
1186 pointer x; 1174 pointer x;
1187 char *s; 1175 char *s;
1188 1176
1197 1185
1198 return NIL; 1186 return NIL;
1199} 1187}
1200 1188
1201/* returns the new symbol */ 1189/* returns the new symbol */
1202static pointer 1190ecb_cold static pointer
1203oblist_add_by_name (SCHEME_P_ const char *name) 1191oblist_add_by_name (SCHEME_P_ const char *name)
1204{ 1192{
1205 pointer x = mk_string (SCHEME_A_ name); 1193 pointer x = generate_symbol (SCHEME_A_ name);
1206 set_typeflag (x, T_SYMBOL);
1207 setimmutable (x);
1208 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1194 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1209 return x; 1195 return x;
1210} 1196}
1211 1197
1212static pointer 1198ecb_cold static pointer
1213oblist_all_symbols (SCHEME_P) 1199oblist_all_symbols (SCHEME_P)
1214{ 1200{
1215 return SCHEME_V->oblist; 1201 return SCHEME_V->oblist;
1216} 1202}
1217 1203
1218#endif 1204#endif
1219 1205
1220#if USE_PORTS
1221static pointer 1206ecb_cold static pointer
1222mk_port (SCHEME_P_ port *p) 1207mk_port (SCHEME_P_ port *p)
1223{ 1208{
1224 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1209 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1225 1210
1226 set_typeflag (x, T_PORT | T_ATOM); 1211 set_typeflag (x, T_PORT | T_ATOM);
1227 x->object.port = p; 1212 set_port (x, p);
1228 1213
1229 return x; 1214 return x;
1230} 1215}
1231#endif
1232 1216
1233pointer 1217ecb_cold pointer
1234mk_foreign_func (SCHEME_P_ foreign_func f) 1218mk_foreign_func (SCHEME_P_ foreign_func f)
1235{ 1219{
1236 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1220 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1237 1221
1238 set_typeflag (x, (T_FOREIGN | T_ATOM)); 1222 set_typeflag (x, T_FOREIGN | T_ATOM);
1239 x->object.ff = f; 1223 CELL(x)->object.ff = f;
1240 1224
1241 return x; 1225 return x;
1242} 1226}
1243 1227
1244INTERFACE pointer 1228INTERFACE pointer
1245mk_character (SCHEME_P_ int c) 1229mk_character (SCHEME_P_ int c)
1246{ 1230{
1247 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1231 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1248 1232
1249 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1233 set_typeflag (x, T_CHARACTER | T_ATOM);
1250 set_ivalue (x, c & 0xff); 1234 set_ivalue (x, c & 0xff);
1251 1235
1252 return x; 1236 return x;
1253} 1237}
1254 1238
1255/* get number atom (integer) */ 1239/* get number atom (integer) */
1256INTERFACE pointer 1240INTERFACE pointer
1257mk_integer (SCHEME_P_ long n) 1241mk_integer (SCHEME_P_ long n)
1258{ 1242{
1243 pointer p = 0;
1244 pointer *pp = &p;
1245
1246#if USE_INTCACHE
1247 if (n >= INTCACHE_MIN && n <= INTCACHE_MAX)
1248 pp = &SCHEME_V->intcache[n - INTCACHE_MIN];
1249#endif
1250
1251 if (!*pp)
1252 {
1259 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1253 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1260 1254
1261 set_typeflag (x, (T_INTEGER | T_ATOM)); 1255 set_typeflag (x, T_INTEGER | T_ATOM);
1256 setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */
1262 set_ivalue (x, n); 1257 set_ivalue (x, n);
1263 1258
1259 *pp = x;
1260 }
1261
1264 return x; 1262 return *pp;
1265} 1263}
1266 1264
1267INTERFACE pointer 1265INTERFACE pointer
1268mk_real (SCHEME_P_ RVALUE n) 1266mk_real (SCHEME_P_ RVALUE n)
1269{ 1267{
1270#if USE_REAL 1268#if USE_REAL
1271 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1269 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1272 1270
1273 set_typeflag (x, (T_REAL | T_ATOM)); 1271 set_typeflag (x, T_REAL | T_ATOM);
1274 set_rvalue (x, n); 1272 set_rvalue (x, n);
1275 1273
1276 return x; 1274 return x;
1277#else 1275#else
1278 return mk_integer (SCHEME_A_ n); 1276 return mk_integer (SCHEME_A_ n);
1389 x = oblist_add_by_name (SCHEME_A_ name); 1387 x = oblist_add_by_name (SCHEME_A_ name);
1390 1388
1391 return x; 1389 return x;
1392} 1390}
1393 1391
1394INTERFACE pointer 1392ecb_cold INTERFACE pointer
1395gensym (SCHEME_P) 1393gensym (SCHEME_P)
1396{ 1394{
1397 pointer x; 1395 pointer x;
1398 char name[40] = "gensym-"; 1396 char name[40] = "gensym-";
1399 xnum (name + 7, ++SCHEME_V->gensym_cnt); 1397 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1406{ 1404{
1407 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; 1405 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1408} 1406}
1409 1407
1410/* make symbol or number atom from string */ 1408/* make symbol or number atom from string */
1411static pointer 1409ecb_cold static pointer
1412mk_atom (SCHEME_P_ char *q) 1410mk_atom (SCHEME_P_ char *q)
1413{ 1411{
1414 char c, *p; 1412 char c, *p;
1415 int has_dec_point = 0; 1413 int has_dec_point = 0;
1416 int has_fp_exp = 0; 1414 int has_fp_exp = 0;
1487 1485
1488 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1486 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1489} 1487}
1490 1488
1491/* make constant */ 1489/* make constant */
1492static pointer 1490ecb_cold static pointer
1493mk_sharp_const (SCHEME_P_ char *name) 1491mk_sharp_const (SCHEME_P_ char *name)
1494{ 1492{
1495 if (!strcmp (name, "t")) 1493 if (!strcmp (name, "t"))
1496 return S_T; 1494 return S_T;
1497 else if (!strcmp (name, "f")) 1495 else if (!strcmp (name, "f"))
1498 return S_F; 1496 return S_F;
1499 else if (*name == '\\') /* #\w (character) */ 1497 else if (*name == '\\') /* #\w (character) */
1500 { 1498 {
1501 int c; 1499 int c;
1502 1500
1501 // TODO: optimise
1503 if (stricmp (name + 1, "space") == 0) 1502 if (stricmp (name + 1, "space") == 0)
1504 c = ' '; 1503 c = ' ';
1505 else if (stricmp (name + 1, "newline") == 0) 1504 else if (stricmp (name + 1, "newline") == 0)
1506 c = '\n'; 1505 c = '\n';
1507 else if (stricmp (name + 1, "return") == 0) 1506 else if (stricmp (name + 1, "return") == 0)
1508 c = '\r'; 1507 c = '\r';
1509 else if (stricmp (name + 1, "tab") == 0) 1508 else if (stricmp (name + 1, "tab") == 0)
1510 c = '\t'; 1509 c = '\t';
1510 else if (stricmp (name + 1, "alarm") == 0)
1511 c = 0x07;
1512 else if (stricmp (name + 1, "backspace") == 0)
1513 c = 0x08;
1514 else if (stricmp (name + 1, "escape") == 0)
1515 c = 0x1b;
1516 else if (stricmp (name + 1, "delete") == 0)
1517 c = 0x7f;
1518 else if (stricmp (name + 1, "null") == 0)
1519 c = 0;
1511 else if (name[1] == 'x' && name[2] != 0) 1520 else if (name[1] == 'x' && name[2] != 0)
1512 { 1521 {
1513 long c1 = strtol (name + 2, 0, 16); 1522 long c1 = strtol (name + 2, 0, 16);
1514 1523
1515 if (0 <= c1 && c1 <= UCHAR_MAX) 1524 if (0 <= c1 && c1 <= UCHAR_MAX)
1540 return NIL; 1549 return NIL;
1541 } 1550 }
1542} 1551}
1543 1552
1544/* ========== garbage collector ========== */ 1553/* ========== garbage collector ========== */
1554
1555static void
1556finalize_cell (SCHEME_P_ pointer a)
1557{
1558 /* TODO, fast bitmap check? */
1559 if (is_string (a) || is_symbol (a))
1560 free (strvalue (a));
1561 else if (is_vector (a))
1562 free (vecvalue (a));
1563#if USE_PORTS
1564 else if (is_port (a))
1565 {
1566 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1567 port_close (SCHEME_A_ a, port_input | port_output);
1568
1569 free (port (a));
1570 }
1571#endif
1572}
1545 1573
1546/*-- 1574/*--
1547 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, 1575 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1,
1548 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1576 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1549 * for marking. 1577 * for marking.
1550 * 1578 *
1551 * The exception is vectors - vectors are currently marked recursively, 1579 * The exception is vectors - vectors are currently marked recursively,
1552 * which is inherited form tinyscheme and could be fixed by having another 1580 * which is inherited form tinyscheme and could be fixed by having another
1553 * word of context in the vector 1581 * word of context in the vector
1554 */ 1582 */
1555static void 1583ecb_hot static void
1556mark (pointer a) 1584mark (pointer a)
1557{ 1585{
1558 pointer t, q, p; 1586 pointer t, q, p;
1559 1587
1560 t = 0; 1588 t = 0;
1617 p = q; 1645 p = q;
1618 goto E6; 1646 goto E6;
1619 } 1647 }
1620} 1648}
1621 1649
1650ecb_hot static void
1651gc_free (SCHEME_P)
1652{
1653 int i;
1654 uint32_t total = 0;
1655
1656 /* Here we scan the cells to build the free-list. */
1657 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1658 {
1659 struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1660 struct cell *p;
1661 total += SCHEME_V->cell_segsize [i];
1662
1663 for (p = SCHEME_V->cell_seg[i]; p < end; ++p)
1664 {
1665 pointer c = POINTER (p);
1666
1667 if (is_mark (c))
1668 clrmark (c);
1669 else
1670 {
1671 /* reclaim cell */
1672 if (typeflag (c) != T_PAIR)
1673 {
1674 finalize_cell (SCHEME_A_ c);
1675 set_typeflag (c, T_PAIR);
1676 set_car (c, NIL);
1677 }
1678
1679 ++SCHEME_V->fcells;
1680 set_cdr (c, SCHEME_V->free_cell);
1681 SCHEME_V->free_cell = c;
1682 }
1683 }
1684 }
1685
1686 if (SCHEME_V->gc_verbose)
1687 {
1688 putstr (SCHEME_A_ "done: "); putnum (SCHEME_A_ SCHEME_V->fcells); putstr (SCHEME_A_ " out of "); putnum (SCHEME_A_ total); putstr (SCHEME_A_ " cells were recovered.\n");
1689 }
1690}
1691
1622/* garbage collection. parameter a, b is marked. */ 1692/* garbage collection. parameter a, b is marked. */
1623static void 1693ecb_cold static void
1624gc (SCHEME_P_ pointer a, pointer b) 1694gc (SCHEME_P_ pointer a, pointer b)
1625{ 1695{
1626 pointer p;
1627 int i; 1696 int i;
1628 1697
1629 if (SCHEME_V->gc_verbose) 1698 if (SCHEME_V->gc_verbose)
1630 putstr (SCHEME_A_ "gc..."); 1699 putstr (SCHEME_A_ "gc...");
1631 1700
1647 /* Mark recent objects the interpreter doesn't know about yet. */ 1716 /* Mark recent objects the interpreter doesn't know about yet. */
1648 mark (car (S_SINK)); 1717 mark (car (S_SINK));
1649 /* Mark any older stuff above nested C calls */ 1718 /* Mark any older stuff above nested C calls */
1650 mark (SCHEME_V->c_nest); 1719 mark (SCHEME_V->c_nest);
1651 1720
1721#if USE_INTCACHE
1722 /* mark intcache */
1723 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1724 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1725 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1726#endif
1727
1652 /* mark variables a, b */ 1728 /* mark variables a, b */
1653 mark (a); 1729 mark (a);
1654 mark (b); 1730 mark (b);
1655 1731
1656 /* garbage collect */ 1732 /* garbage collect */
1657 clrmark (NIL); 1733 clrmark (NIL);
1658 SCHEME_V->fcells = 0; 1734 SCHEME_V->fcells = 0;
1659 SCHEME_V->free_cell = NIL; 1735 SCHEME_V->free_cell = NIL;
1660 1736
1661 /* free-list is kept sorted by address so as to maintain consecutive
1662 ranges, if possible, for use with vectors. Here we scan the cells
1663 (which are also kept sorted by address) downwards to build the
1664 free-list in sorted order.
1665 */
1666 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1667 {
1668 p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1669
1670 while (--p >= SCHEME_V->cell_seg[i])
1671 {
1672 if (is_mark (p))
1673 clrmark (p);
1674 else
1675 {
1676 /* reclaim cell */
1677 if (typeflag (p) != T_PAIR)
1678 {
1679 finalize_cell (SCHEME_A_ p);
1680 set_typeflag (p, T_PAIR);
1681 set_car (p, NIL);
1682 }
1683
1684 ++SCHEME_V->fcells;
1685 set_cdr (p, SCHEME_V->free_cell);
1686 SCHEME_V->free_cell = p;
1687 }
1688 }
1689 }
1690
1691 if (SCHEME_V->gc_verbose) 1737 if (SCHEME_V->gc_verbose)
1692 { 1738 putstr (SCHEME_A_ "freeing...");
1693 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1694 }
1695}
1696 1739
1697static void 1740 gc_free (SCHEME_A);
1698finalize_cell (SCHEME_P_ pointer a)
1699{
1700 /* TODO, fast bitmap check? */
1701 if (is_string (a) || is_symbol (a))
1702 free (strvalue (a));
1703 else if (is_vector (a))
1704 free (vecvalue (a));
1705#if USE_PORTS
1706 else if (is_port (a))
1707 {
1708 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit)
1709 port_close (SCHEME_A_ a, port_input | port_output);
1710
1711 free (a->object.port);
1712 }
1713#endif
1714} 1741}
1715 1742
1716/* ========== Routines for Reading ========== */ 1743/* ========== Routines for Reading ========== */
1717 1744
1718static int 1745ecb_cold static int
1719file_push (SCHEME_P_ const char *fname) 1746file_push (SCHEME_P_ const char *fname)
1720{ 1747{
1721#if USE_PORTS
1722 int fin; 1748 int fin;
1723 1749
1724 if (SCHEME_V->file_i == MAXFIL - 1) 1750 if (SCHEME_V->file_i == MAXFIL - 1)
1725 return 0; 1751 return 0;
1726 1752
1732 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; 1758 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1;
1733 SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; 1759 SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input;
1734 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; 1760 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin;
1735 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; 1761 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1;
1736 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; 1762 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0;
1737 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1763 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1738 1764
1739#if SHOW_ERROR_LINE 1765#if SHOW_ERROR_LINE
1740 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; 1766 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0;
1741 1767
1742 if (fname) 1768 if (fname)
1743 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); 1769 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0);
1744#endif 1770#endif
1745 } 1771 }
1746 1772
1747 return fin >= 0; 1773 return fin >= 0;
1748
1749#else
1750 return 1;
1751#endif
1752} 1774}
1753 1775
1754static void 1776ecb_cold static void
1755file_pop (SCHEME_P) 1777file_pop (SCHEME_P)
1756{ 1778{
1757 if (SCHEME_V->file_i != 0) 1779 if (SCHEME_V->file_i != 0)
1758 { 1780 {
1759 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1781 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1760#if USE_PORTS 1782#if USE_PORTS
1761 port_close (SCHEME_A_ SCHEME_V->loadport, port_input); 1783 port_close (SCHEME_A_ SCHEME_V->loadport, port_input);
1762#endif 1784#endif
1763 SCHEME_V->file_i--; 1785 SCHEME_V->file_i--;
1764 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1786 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1765 } 1787 }
1766} 1788}
1767 1789
1768static int 1790ecb_cold static int
1769file_interactive (SCHEME_P) 1791file_interactive (SCHEME_P)
1770{ 1792{
1771#if USE_PORTS 1793#if USE_PORTS
1772 return SCHEME_V->file_i == 0 1794 return SCHEME_V->file_i == 0
1773 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1795 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1774 && (SCHEME_V->inport->object.port->kind & port_file); 1796 && (port (SCHEME_V->inport)->kind & port_file);
1775#else 1797#else
1776 return 0; 1798 return 0;
1777#endif 1799#endif
1778} 1800}
1779 1801
1780#if USE_PORTS 1802#if USE_PORTS
1781static port * 1803ecb_cold static port *
1782port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1804port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1783{ 1805{
1784 int fd; 1806 int fd;
1785 int flags; 1807 int flags;
1786 char *rw; 1808 char *rw;
1809# endif 1831# endif
1810 1832
1811 return pt; 1833 return pt;
1812} 1834}
1813 1835
1814static pointer 1836ecb_cold static pointer
1815port_from_filename (SCHEME_P_ const char *fn, int prop) 1837port_from_filename (SCHEME_P_ const char *fn, int prop)
1816{ 1838{
1817 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1839 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1818 1840
1819 if (!pt && USE_ERROR_CHECKING) 1841 if (!pt && USE_ERROR_CHECKING)
1820 return NIL; 1842 return NIL;
1821 1843
1822 return mk_port (SCHEME_A_ pt); 1844 return mk_port (SCHEME_A_ pt);
1823} 1845}
1824 1846
1825static port * 1847ecb_cold static port *
1826port_rep_from_file (SCHEME_P_ int f, int prop) 1848port_rep_from_file (SCHEME_P_ int f, int prop)
1827{ 1849{
1828 port *pt = malloc (sizeof *pt); 1850 port *pt = malloc (sizeof *pt);
1829 1851
1830 if (!pt && USE_ERROR_CHECKING) 1852 if (!pt && USE_ERROR_CHECKING)
1835 pt->rep.stdio.file = f; 1857 pt->rep.stdio.file = f;
1836 pt->rep.stdio.closeit = 0; 1858 pt->rep.stdio.closeit = 0;
1837 return pt; 1859 return pt;
1838} 1860}
1839 1861
1840static pointer 1862ecb_cold static pointer
1841port_from_file (SCHEME_P_ int f, int prop) 1863port_from_file (SCHEME_P_ int f, int prop)
1842{ 1864{
1843 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1865 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1844 1866
1845 if (!pt && USE_ERROR_CHECKING) 1867 if (!pt && USE_ERROR_CHECKING)
1846 return NIL; 1868 return NIL;
1847 1869
1848 return mk_port (SCHEME_A_ pt); 1870 return mk_port (SCHEME_A_ pt);
1849} 1871}
1850 1872
1851static port * 1873ecb_cold static port *
1852port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1874port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1853{ 1875{
1854 port *pt = malloc (sizeof (port)); 1876 port *pt = malloc (sizeof (port));
1855 1877
1856 if (!pt && USE_ERROR_CHECKING) 1878 if (!pt && USE_ERROR_CHECKING)
1862 pt->rep.string.curr = start; 1884 pt->rep.string.curr = start;
1863 pt->rep.string.past_the_end = past_the_end; 1885 pt->rep.string.past_the_end = past_the_end;
1864 return pt; 1886 return pt;
1865} 1887}
1866 1888
1867static pointer 1889ecb_cold static pointer
1868port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1890port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1869{ 1891{
1870 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); 1892 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop);
1871 1893
1872 if (!pt && USE_ERROR_CHECKING) 1894 if (!pt && USE_ERROR_CHECKING)
1875 return mk_port (SCHEME_A_ pt); 1897 return mk_port (SCHEME_A_ pt);
1876} 1898}
1877 1899
1878# define BLOCK_SIZE 256 1900# define BLOCK_SIZE 256
1879 1901
1880static port * 1902ecb_cold static port *
1881port_rep_from_scratch (SCHEME_P) 1903port_rep_from_scratch (SCHEME_P)
1882{ 1904{
1883 char *start; 1905 char *start;
1884 port *pt = malloc (sizeof (port)); 1906 port *pt = malloc (sizeof (port));
1885 1907
1899 pt->rep.string.curr = start; 1921 pt->rep.string.curr = start;
1900 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1922 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1901 return pt; 1923 return pt;
1902} 1924}
1903 1925
1904static pointer 1926ecb_cold static pointer
1905port_from_scratch (SCHEME_P) 1927port_from_scratch (SCHEME_P)
1906{ 1928{
1907 port *pt = port_rep_from_scratch (SCHEME_A); 1929 port *pt = port_rep_from_scratch (SCHEME_A);
1908 1930
1909 if (!pt && USE_ERROR_CHECKING) 1931 if (!pt && USE_ERROR_CHECKING)
1910 return NIL; 1932 return NIL;
1911 1933
1912 return mk_port (SCHEME_A_ pt); 1934 return mk_port (SCHEME_A_ pt);
1913} 1935}
1914 1936
1915static void 1937ecb_cold static void
1916port_close (SCHEME_P_ pointer p, int flag) 1938port_close (SCHEME_P_ pointer p, int flag)
1917{ 1939{
1918 port *pt = p->object.port; 1940 port *pt = port (p);
1919 1941
1920 pt->kind &= ~flag; 1942 pt->kind &= ~flag;
1921 1943
1922 if ((pt->kind & (port_input | port_output)) == 0) 1944 if ((pt->kind & (port_input | port_output)) == 0)
1923 { 1945 {
1940 } 1962 }
1941} 1963}
1942#endif 1964#endif
1943 1965
1944/* get new character from input file */ 1966/* get new character from input file */
1945static int 1967ecb_cold static int
1946inchar (SCHEME_P) 1968inchar (SCHEME_P)
1947{ 1969{
1948 int c; 1970 int c;
1949 port *pt; 1971 port *pt = port (SCHEME_V->inport);
1950
1951 pt = SCHEME_V->inport->object.port;
1952 1972
1953 if (pt->kind & port_saw_EOF) 1973 if (pt->kind & port_saw_EOF)
1954 return EOF; 1974 return EOF;
1955 1975
1956 c = basic_inchar (pt); 1976 c = basic_inchar (pt);
1966 } 1986 }
1967 1987
1968 return c; 1988 return c;
1969} 1989}
1970 1990
1971static int ungot = -1; 1991ecb_cold static int
1972
1973static int
1974basic_inchar (port *pt) 1992basic_inchar (port *pt)
1975{ 1993{
1976#if USE_PORTS
1977 if (pt->unget != -1) 1994 if (pt->unget != -1)
1978 { 1995 {
1979 int r = pt->unget; 1996 int r = pt->unget;
1980 pt->unget = -1; 1997 pt->unget = -1;
1981 return r; 1998 return r;
1982 } 1999 }
1983 2000
2001#if USE_PORTS
1984 if (pt->kind & port_file) 2002 if (pt->kind & port_file)
1985 { 2003 {
1986 char c; 2004 char c;
1987 2005
1988 if (!read (pt->rep.stdio.file, &c, 1)) 2006 if (!read (pt->rep.stdio.file, &c, 1))
1996 return EOF; 2014 return EOF;
1997 else 2015 else
1998 return *pt->rep.string.curr++; 2016 return *pt->rep.string.curr++;
1999 } 2017 }
2000#else 2018#else
2001 if (ungot == -1)
2002 {
2003 char c; 2019 char c;
2004 if (!read (0, &c, 1)) 2020
2021 if (!read (pt->rep.stdio.file, &c, 1))
2005 return EOF; 2022 return EOF;
2006 2023
2007 ungot = c;
2008 }
2009
2010 {
2011 int r = ungot;
2012 ungot = -1;
2013 return r; 2024 return c;
2014 }
2015#endif 2025#endif
2016} 2026}
2017 2027
2018/* back character to input buffer */ 2028/* back character to input buffer */
2019static void 2029ecb_cold static void
2020backchar (SCHEME_P_ int c) 2030backchar (SCHEME_P_ int c)
2021{ 2031{
2022#if USE_PORTS 2032 port *pt = port (SCHEME_V->inport);
2023 port *pt;
2024 2033
2025 if (c == EOF) 2034 if (c == EOF)
2026 return; 2035 return;
2027 2036
2028 pt = SCHEME_V->inport->object.port;
2029 pt->unget = c; 2037 pt->unget = c;
2030#else
2031 if (c == EOF)
2032 return;
2033
2034 ungot = c;
2035#endif
2036} 2038}
2037 2039
2038#if USE_PORTS 2040#if USE_PORTS
2039static int 2041ecb_cold static int
2040realloc_port_string (SCHEME_P_ port *p) 2042realloc_port_string (SCHEME_P_ port *p)
2041{ 2043{
2042 char *start = p->rep.string.start; 2044 char *start = p->rep.string.start;
2043 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; 2045 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE;
2044 char *str = malloc (new_size); 2046 char *str = malloc (new_size);
2057 else 2059 else
2058 return 0; 2060 return 0;
2059} 2061}
2060#endif 2062#endif
2061 2063
2062INTERFACE void 2064ecb_cold static void
2063putstr (SCHEME_P_ const char *s) 2065putchars (SCHEME_P_ const char *s, int len)
2064{ 2066{
2067 port *pt = port (SCHEME_V->outport);
2068
2065#if USE_PORTS 2069#if USE_PORTS
2066 port *pt = SCHEME_V->outport->object.port;
2067
2068 if (pt->kind & port_file)
2069 write (pt->rep.stdio.file, s, strlen (s));
2070 else
2071 for (; *s; s++)
2072 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2073 *pt->rep.string.curr++ = *s;
2074 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2075 *pt->rep.string.curr++ = *s;
2076
2077#else
2078 xwrstr (s);
2079#endif
2080}
2081
2082static void
2083putchars (SCHEME_P_ const char *s, int len)
2084{
2085#if USE_PORTS
2086 port *pt = SCHEME_V->outport->object.port;
2087
2088 if (pt->kind & port_file) 2070 if (pt->kind & port_file)
2089 write (pt->rep.stdio.file, s, len); 2071 write (pt->rep.stdio.file, s, len);
2090 else 2072 else
2091 { 2073 {
2092 for (; len; len--) 2074 for (; len; len--)
2097 *pt->rep.string.curr++ = *s++; 2079 *pt->rep.string.curr++ = *s++;
2098 } 2080 }
2099 } 2081 }
2100 2082
2101#else 2083#else
2102 write (1, s, len); 2084 write (1, s, len); // output not initialised
2103#endif 2085#endif
2086}
2087
2088INTERFACE void
2089putstr (SCHEME_P_ const char *s)
2090{
2091 putchars (SCHEME_A_ s, strlen (s));
2104} 2092}
2105 2093
2106INTERFACE void 2094INTERFACE void
2107putcharacter (SCHEME_P_ int c) 2095putcharacter (SCHEME_P_ int c)
2108{ 2096{
2109#if USE_PORTS
2110 port *pt = SCHEME_V->outport->object.port;
2111
2112 if (pt->kind & port_file)
2113 {
2114 char cc = c;
2115 write (pt->rep.stdio.file, &cc, 1);
2116 }
2117 else
2118 {
2119 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2120 *pt->rep.string.curr++ = c;
2121 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2122 *pt->rep.string.curr++ = c;
2123 }
2124
2125#else
2126 char cc = c; 2097 char cc = c;
2127 write (1, &c, 1); 2098
2128#endif 2099 putchars (SCHEME_A_ &cc, 1);
2129} 2100}
2130 2101
2131/* read characters up to delimiter, but cater to character constants */ 2102/* read characters up to delimiter, but cater to character constants */
2132static char * 2103ecb_cold static char *
2133readstr_upto (SCHEME_P_ int skip, const char *delim) 2104readstr_upto (SCHEME_P_ int skip, const char *delim)
2134{ 2105{
2135 char *p = SCHEME_V->strbuff + skip; 2106 char *p = SCHEME_V->strbuff + skip;
2136 2107
2137 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2108 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2146 2117
2147 return SCHEME_V->strbuff; 2118 return SCHEME_V->strbuff;
2148} 2119}
2149 2120
2150/* read string expression "xxx...xxx" */ 2121/* read string expression "xxx...xxx" */
2151static pointer 2122ecb_cold static pointer
2152readstrexp (SCHEME_P_ char delim) 2123readstrexp (SCHEME_P_ char delim)
2153{ 2124{
2154 char *p = SCHEME_V->strbuff; 2125 char *p = SCHEME_V->strbuff;
2155 int c; 2126 int c;
2156 int c1 = 0; 2127 int c1 = 0;
2189 case '7': 2160 case '7':
2190 state = st_oct1; 2161 state = st_oct1;
2191 c1 = c - '0'; 2162 c1 = c - '0';
2192 break; 2163 break;
2193 2164
2165 case 'a': *p++ = '\a'; state = st_ok; break;
2166 case 'n': *p++ = '\n'; state = st_ok; break;
2167 case 'r': *p++ = '\r'; state = st_ok; break;
2168 case 't': *p++ = '\t'; state = st_ok; break;
2169
2170 // this overshoots the minimum requirements of r7rs
2171 case ' ':
2172 case '\t':
2173 case '\r':
2174 case '\n':
2175 skipspace (SCHEME_A);
2176 state = st_ok;
2177 break;
2178
2179 //TODO: x should end in ;, not two-digit hex
2194 case 'x': 2180 case 'x':
2195 case 'X': 2181 case 'X':
2196 state = st_x1; 2182 state = st_x1;
2197 c1 = 0; 2183 c1 = 0;
2198 break;
2199
2200 case 'n':
2201 *p++ = '\n';
2202 state = st_ok;
2203 break;
2204
2205 case 't':
2206 *p++ = '\t';
2207 state = st_ok;
2208 break;
2209
2210 case 'r':
2211 *p++ = '\r';
2212 state = st_ok;
2213 break; 2184 break;
2214 2185
2215 default: 2186 default:
2216 *p++ = c; 2187 *p++ = c;
2217 state = st_ok; 2188 state = st_ok;
2269 } 2240 }
2270 } 2241 }
2271} 2242}
2272 2243
2273/* check c is in chars */ 2244/* check c is in chars */
2274ecb_inline int 2245ecb_cold int
2275is_one_of (const char *s, int c) 2246is_one_of (const char *s, int c)
2276{ 2247{
2277 return c == EOF || !!strchr (s, c); 2248 return c == EOF || !!strchr (s, c);
2278} 2249}
2279 2250
2280/* skip white characters */ 2251/* skip white characters */
2281ecb_inline int 2252ecb_cold int
2282skipspace (SCHEME_P) 2253skipspace (SCHEME_P)
2283{ 2254{
2284 int c, curr_line = 0; 2255 int c, curr_line = 0;
2285 2256
2286 do 2257 do
2306 backchar (SCHEME_A_ c); 2277 backchar (SCHEME_A_ c);
2307 return 1; 2278 return 1;
2308} 2279}
2309 2280
2310/* get token */ 2281/* get token */
2311static int 2282ecb_cold static int
2312token (SCHEME_P) 2283token (SCHEME_P)
2313{ 2284{
2314 int c = skipspace (SCHEME_A); 2285 int c = skipspace (SCHEME_A);
2315 2286
2316 if (c == EOF) 2287 if (c == EOF)
2414} 2385}
2415 2386
2416/* ========== Routines for Printing ========== */ 2387/* ========== Routines for Printing ========== */
2417#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2388#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2418 2389
2419static void 2390ecb_cold static void
2420printslashstring (SCHEME_P_ char *p, int len) 2391printslashstring (SCHEME_P_ char *p, int len)
2421{ 2392{
2422 int i; 2393 int i;
2423 unsigned char *s = (unsigned char *) p; 2394 unsigned char *s = (unsigned char *) p;
2424 2395
2480 2451
2481 putcharacter (SCHEME_A_ '"'); 2452 putcharacter (SCHEME_A_ '"');
2482} 2453}
2483 2454
2484/* print atoms */ 2455/* print atoms */
2485static void 2456ecb_cold static void
2486printatom (SCHEME_P_ pointer l, int f) 2457printatom (SCHEME_P_ pointer l, int f)
2487{ 2458{
2488 char *p; 2459 char *p;
2489 int len; 2460 int len;
2490 2461
2491 atom2str (SCHEME_A_ l, f, &p, &len); 2462 atom2str (SCHEME_A_ l, f, &p, &len);
2492 putchars (SCHEME_A_ p, len); 2463 putchars (SCHEME_A_ p, len);
2493} 2464}
2494 2465
2495/* Uses internal buffer unless string pointer is already available */ 2466/* Uses internal buffer unless string pointer is already available */
2496static void 2467ecb_cold static void
2497atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2468atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2498{ 2469{
2499 char *p; 2470 char *p;
2500 2471
2501 if (l == NIL) 2472 if (l == NIL)
2708 return car (d); 2679 return car (d);
2709 2680
2710 p = cons (car (d), cdr (d)); 2681 p = cons (car (d), cdr (d));
2711 q = p; 2682 q = p;
2712 2683
2713 while (cdr (cdr (p)) != NIL) 2684 while (cddr (p) != NIL)
2714 { 2685 {
2715 d = cons (car (p), cdr (p)); 2686 d = cons (car (p), cdr (p));
2716 2687
2717 if (cdr (cdr (p)) != NIL) 2688 if (cddr (p) != NIL)
2718 p = cdr (d); 2689 p = cdr (d);
2719 } 2690 }
2720 2691
2721 set_cdr (p, car (cdr (p))); 2692 set_cdr (p, cadr (p));
2722 return q; 2693 return q;
2723} 2694}
2724 2695
2725/* reverse list -- produce new list */ 2696/* reverse list -- produce new list */
2726static pointer 2697ecb_hot static pointer
2727reverse (SCHEME_P_ pointer a) 2698reverse (SCHEME_P_ pointer a)
2728{ 2699{
2729 /* a must be checked by gc */ 2700 /* a must be checked by gc */
2730 pointer p = NIL; 2701 pointer p = NIL;
2731 2702
2734 2705
2735 return p; 2706 return p;
2736} 2707}
2737 2708
2738/* reverse list --- in-place */ 2709/* reverse list --- in-place */
2739static pointer 2710ecb_hot static pointer
2740reverse_in_place (SCHEME_P_ pointer term, pointer list) 2711reverse_in_place (SCHEME_P_ pointer term, pointer list)
2741{ 2712{
2742 pointer result = term; 2713 pointer result = term;
2743 pointer p = list; 2714 pointer p = list;
2744 2715
2752 2723
2753 return result; 2724 return result;
2754} 2725}
2755 2726
2756/* append list -- produce new list (in reverse order) */ 2727/* append list -- produce new list (in reverse order) */
2757static pointer 2728ecb_hot static pointer
2758revappend (SCHEME_P_ pointer a, pointer b) 2729revappend (SCHEME_P_ pointer a, pointer b)
2759{ 2730{
2760 pointer result = a; 2731 pointer result = a;
2761 pointer p = b; 2732 pointer p = b;
2762 2733
2771 2742
2772 return S_F; /* signal an error */ 2743 return S_F; /* signal an error */
2773} 2744}
2774 2745
2775/* equivalence of atoms */ 2746/* equivalence of atoms */
2776int 2747ecb_hot int
2777eqv (pointer a, pointer b) 2748eqv (pointer a, pointer b)
2778{ 2749{
2779 if (is_string (a)) 2750 if (is_string (a))
2780 { 2751 {
2781 if (is_string (b)) 2752 if (is_string (b))
2875 } 2846 }
2876 else 2847 else
2877 set_car (env, immutable_cons (slot, car (env))); 2848 set_car (env, immutable_cons (slot, car (env)));
2878} 2849}
2879 2850
2880static pointer 2851ecb_hot static pointer
2881find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2852find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2882{ 2853{
2883 pointer x, y; 2854 pointer x, y;
2884 2855
2885 for (x = env; x != NIL; x = cdr (x)) 2856 for (x = env; x != NIL; x = cdr (x))
2906 return NIL; 2877 return NIL;
2907} 2878}
2908 2879
2909#else /* USE_ALIST_ENV */ 2880#else /* USE_ALIST_ENV */
2910 2881
2911ecb_inline void 2882static void
2912new_frame_in_env (SCHEME_P_ pointer old_env) 2883new_frame_in_env (SCHEME_P_ pointer old_env)
2913{ 2884{
2914 SCHEME_V->envir = immutable_cons (NIL, old_env); 2885 SCHEME_V->envir = immutable_cons (NIL, old_env);
2915 setenvironment (SCHEME_V->envir); 2886 setenvironment (SCHEME_V->envir);
2916} 2887}
2917 2888
2918ecb_inline void 2889static void
2919new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2890new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2920{ 2891{
2921 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2892 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2922} 2893}
2923 2894
2924static pointer 2895ecb_hot static pointer
2925find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2896find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2926{ 2897{
2927 pointer x, y; 2898 pointer x, y;
2928 2899
2929 for (x = env; x != NIL; x = cdr (x)) 2900 for (x = env; x != NIL; x = cdr (x))
2943 return NIL; 2914 return NIL;
2944} 2915}
2945 2916
2946#endif /* USE_ALIST_ENV else */ 2917#endif /* USE_ALIST_ENV else */
2947 2918
2948ecb_inline void 2919static void
2949new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2920new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2950{ 2921{
2951 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 2922 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2952 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2923 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2953} 2924}
2954 2925
2955ecb_inline void 2926static void
2956set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2927set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2957{ 2928{
2958 set_cdr (slot, value); 2929 set_cdr (slot, value);
2959} 2930}
2960 2931
2961ecb_inline pointer 2932static pointer
2962slot_value_in_env (pointer slot) 2933slot_value_in_env (pointer slot)
2963{ 2934{
2964 return cdr (slot); 2935 return cdr (slot);
2965} 2936}
2966 2937
2967/* ========== Evaluation Cycle ========== */ 2938/* ========== Evaluation Cycle ========== */
2968 2939
2969static int 2940ecb_cold static int
2970xError_1 (SCHEME_P_ const char *s, pointer a) 2941xError_1 (SCHEME_P_ const char *s, pointer a)
2971{ 2942{
2972#if USE_ERROR_HOOK
2973 pointer x;
2974 pointer hdl = SCHEME_V->ERROR_HOOK;
2975#endif
2976
2977#if USE_PRINTF 2943#if USE_PRINTF
2978#if SHOW_ERROR_LINE 2944#if SHOW_ERROR_LINE
2979 char sbuf[STRBUFFSIZE]; 2945 char sbuf[STRBUFFSIZE];
2980 2946
2981 /* make sure error is not in REPL */ 2947 /* make sure error is not in REPL */
2996 } 2962 }
2997#endif 2963#endif
2998#endif 2964#endif
2999 2965
3000#if USE_ERROR_HOOK 2966#if USE_ERROR_HOOK
3001 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); 2967 pointer x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->ERROR_HOOK, 1);
3002 2968
3003 if (x != NIL) 2969 if (x != NIL)
3004 { 2970 {
3005 pointer code = a 2971 pointer code = a
3006 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) 2972 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL)
3050 pointer code; 3016 pointer code;
3051}; 3017};
3052 3018
3053# define STACK_GROWTH 3 3019# define STACK_GROWTH 3
3054 3020
3055static void 3021ecb_hot static void
3056s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3022s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3057{ 3023{
3058 int nframes = (uintptr_t)SCHEME_V->dump; 3024 int nframes = (uintptr_t)SCHEME_V->dump;
3059 struct dump_stack_frame *next_frame; 3025 struct dump_stack_frame *next_frame;
3060 3026
3061 /* enough room for the next frame? */ 3027 /* enough room for the next frame? */
3062 if (nframes >= SCHEME_V->dump_size) 3028 if (ecb_expect_false (nframes >= SCHEME_V->dump_size))
3063 { 3029 {
3064 SCHEME_V->dump_size += STACK_GROWTH; 3030 SCHEME_V->dump_size += STACK_GROWTH;
3065 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); 3031 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size);
3066 } 3032 }
3067 3033
3073 next_frame->code = code; 3039 next_frame->code = code;
3074 3040
3075 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3041 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3076} 3042}
3077 3043
3078static int 3044static ecb_hot int
3079xs_return (SCHEME_P_ pointer a) 3045xs_return (SCHEME_P_ pointer a)
3080{ 3046{
3081 int nframes = (uintptr_t)SCHEME_V->dump; 3047 int nframes = (uintptr_t)SCHEME_V->dump;
3082 struct dump_stack_frame *frame; 3048 struct dump_stack_frame *frame;
3083 3049
3094 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3060 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3095 3061
3096 return 0; 3062 return 0;
3097} 3063}
3098 3064
3099ecb_inline void 3065ecb_cold void
3100dump_stack_reset (SCHEME_P) 3066dump_stack_reset (SCHEME_P)
3101{ 3067{
3102 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3068 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3103 SCHEME_V->dump = (pointer)+0; 3069 SCHEME_V->dump = (pointer)+0;
3104} 3070}
3105 3071
3106ecb_inline void 3072ecb_cold void
3107dump_stack_initialize (SCHEME_P) 3073dump_stack_initialize (SCHEME_P)
3108{ 3074{
3109 SCHEME_V->dump_size = 0; 3075 SCHEME_V->dump_size = 0;
3110 SCHEME_V->dump_base = 0; 3076 SCHEME_V->dump_base = 0;
3111 dump_stack_reset (SCHEME_A); 3077 dump_stack_reset (SCHEME_A);
3112} 3078}
3113 3079
3114static void 3080ecb_cold static void
3115dump_stack_free (SCHEME_P) 3081dump_stack_free (SCHEME_P)
3116{ 3082{
3117 free (SCHEME_V->dump_base); 3083 free (SCHEME_V->dump_base);
3118 SCHEME_V->dump_base = 0; 3084 SCHEME_V->dump_base = 0;
3119 SCHEME_V->dump = (pointer)0; 3085 SCHEME_V->dump = (pointer)0;
3120 SCHEME_V->dump_size = 0; 3086 SCHEME_V->dump_size = 0;
3121} 3087}
3122 3088
3123static void 3089ecb_cold static void
3124dump_stack_mark (SCHEME_P) 3090dump_stack_mark (SCHEME_P)
3125{ 3091{
3126 int nframes = (uintptr_t)SCHEME_V->dump; 3092 int nframes = (uintptr_t)SCHEME_V->dump;
3127 int i; 3093 int i;
3128 3094
3134 mark (frame->envir); 3100 mark (frame->envir);
3135 mark (frame->code); 3101 mark (frame->code);
3136 } 3102 }
3137} 3103}
3138 3104
3139static pointer 3105ecb_cold static pointer
3140ss_get_cont (SCHEME_P) 3106ss_get_cont (SCHEME_P)
3141{ 3107{
3142 int nframes = (uintptr_t)SCHEME_V->dump; 3108 int nframes = (uintptr_t)SCHEME_V->dump;
3143 int i; 3109 int i;
3144 3110
3156 } 3122 }
3157 3123
3158 return cont; 3124 return cont;
3159} 3125}
3160 3126
3161static void 3127ecb_cold static void
3162ss_set_cont (SCHEME_P_ pointer cont) 3128ss_set_cont (SCHEME_P_ pointer cont)
3163{ 3129{
3164 int i = 0; 3130 int i = 0;
3165 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3131 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3166 3132
3178 SCHEME_V->dump = (pointer)(uintptr_t)i; 3144 SCHEME_V->dump = (pointer)(uintptr_t)i;
3179} 3145}
3180 3146
3181#else 3147#else
3182 3148
3183ecb_inline void 3149ecb_cold void
3184dump_stack_reset (SCHEME_P) 3150dump_stack_reset (SCHEME_P)
3185{ 3151{
3186 SCHEME_V->dump = NIL; 3152 SCHEME_V->dump = NIL;
3187} 3153}
3188 3154
3189ecb_inline void 3155ecb_cold void
3190dump_stack_initialize (SCHEME_P) 3156dump_stack_initialize (SCHEME_P)
3191{ 3157{
3192 dump_stack_reset (SCHEME_A); 3158 dump_stack_reset (SCHEME_A);
3193} 3159}
3194 3160
3195static void 3161ecb_cold static void
3196dump_stack_free (SCHEME_P) 3162dump_stack_free (SCHEME_P)
3197{ 3163{
3198 SCHEME_V->dump = NIL; 3164 SCHEME_V->dump = NIL;
3199} 3165}
3200 3166
3201static int 3167ecb_hot static int
3202xs_return (SCHEME_P_ pointer a) 3168xs_return (SCHEME_P_ pointer a)
3203{ 3169{
3204 pointer dump = SCHEME_V->dump; 3170 pointer dump = SCHEME_V->dump;
3205 3171
3206 SCHEME_V->value = a; 3172 SCHEME_V->value = a;
3216 SCHEME_V->dump = dump; 3182 SCHEME_V->dump = dump;
3217 3183
3218 return 0; 3184 return 0;
3219} 3185}
3220 3186
3221static void 3187ecb_hot static void
3222s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3188s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3223{ 3189{
3224 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3190 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3225 cons (args, 3191 cons (args,
3226 cons (SCHEME_V->envir, 3192 cons (SCHEME_V->envir,
3227 cons (code, 3193 cons (code,
3228 SCHEME_V->dump)))); 3194 SCHEME_V->dump))));
3229} 3195}
3230 3196
3231static void 3197ecb_cold static void
3232dump_stack_mark (SCHEME_P) 3198dump_stack_mark (SCHEME_P)
3233{ 3199{
3234 mark (SCHEME_V->dump); 3200 mark (SCHEME_V->dump);
3235} 3201}
3236 3202
3237static pointer 3203ecb_cold static pointer
3238ss_get_cont (SCHEME_P) 3204ss_get_cont (SCHEME_P)
3239{ 3205{
3240 return SCHEME_V->dump; 3206 return SCHEME_V->dump;
3241} 3207}
3242 3208
3243static void 3209ecb_cold static void
3244ss_set_cont (SCHEME_P_ pointer cont) 3210ss_set_cont (SCHEME_P_ pointer cont)
3245{ 3211{
3246 SCHEME_V->dump = cont; 3212 SCHEME_V->dump = cont;
3247} 3213}
3248 3214
3249#endif 3215#endif
3250 3216
3251#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3217#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3252 3218
3253#if EXPERIMENT 3219#if EXPERIMENT
3220
3254static int 3221static int
3255debug (SCHEME_P_ int indent, pointer x) 3222dtree (SCHEME_P_ int indent, pointer x)
3256{ 3223{
3257 int c; 3224 int c;
3258 3225
3259 if (is_syntax (x)) 3226 if (is_syntax (x))
3260 { 3227 {
3278 printf ("%*sS<%s>\n", indent, "", symname (x)); 3245 printf ("%*sS<%s>\n", indent, "", symname (x));
3279 return 24+8; 3246 return 24+8;
3280 3247
3281 case T_CLOSURE: 3248 case T_CLOSURE:
3282 printf ("%*sS<%s>\n", indent, "", "closure"); 3249 printf ("%*sS<%s>\n", indent, "", "closure");
3283 debug (SCHEME_A_ indent + 3, cdr(x)); 3250 dtree (SCHEME_A_ indent + 3, cdr(x));
3284 return 32 + debug (SCHEME_A_ indent + 3, car (x)); 3251 return 32 + dtree (SCHEME_A_ indent + 3, car (x));
3285 3252
3286 case T_PAIR: 3253 case T_PAIR:
3287 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); 3254 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3288 c = debug (SCHEME_A_ indent + 3, car (x)); 3255 c = dtree (SCHEME_A_ indent + 3, car (x));
3289 c += debug (SCHEME_A_ indent + 3, cdr (x)); 3256 c += dtree (SCHEME_A_ indent + 3, cdr (x));
3290 return c + 1; 3257 return c + 1;
3291 3258
3292 case T_PORT: 3259 case T_PORT:
3293 printf ("%*sS<%s>\n", indent, "", "port"); 3260 printf ("%*sS<%s>\n", indent, "", "port");
3294 return 24+8; 3261 return 24+8;
3297 printf ("%*sS<%s>\n", indent, "", "vector"); 3264 printf ("%*sS<%s>\n", indent, "", "vector");
3298 return 24+8; 3265 return 24+8;
3299 3266
3300 case T_ENVIRONMENT: 3267 case T_ENVIRONMENT:
3301 printf ("%*sS<%s>\n", indent, "", "environment"); 3268 printf ("%*sS<%s>\n", indent, "", "environment");
3302 return 0 + debug (SCHEME_A_ indent + 3, car (x)); 3269 return 0 + dtree (SCHEME_A_ indent + 3, car (x));
3303 3270
3304 default: 3271 default:
3305 printf ("unhandled type %d\n", type (x)); 3272 printf ("unhandled type %d\n", type (x));
3306 break; 3273 break;
3307 } 3274 }
3308} 3275}
3309#endif
3310 3276
3277#define DUMP(t) do { printf ("DUMP %s:%d\n", __FILE__, __LINE__); dtree (SCHEME_A_ 0, (t)); } while (0)
3278
3279typedef void *stream[1];
3280
3281#define stream_init() { 0 }
3282#define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2)
3283#define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2)
3284#define stream_free(s) free (s[0])
3285
3286ecb_cold static void
3287stream_put (stream s, uint8_t byte)
3288{
3289 uint32_t *sp = *s;
3290 uint32_t size = sizeof (uint32_t) * 2;
3291 uint32_t offs = size;
3292
3293 if (ecb_expect_true (sp))
3294 {
3295 offs = sp[0];
3296 size = sp[1];
3297 }
3298
3299 if (ecb_expect_false (offs == size))
3300 {
3301 size *= 2;
3302 sp = realloc (sp, size);
3303 *s = sp;
3304 sp[1] = size;
3305
3306 }
3307
3308 ((uint8_t *)sp)[offs++] = byte;
3309 sp[0] = offs;
3310}
3311
3312ecb_cold static void
3313stream_put_v (stream s, uint32_t v)
3314{
3315 while (v > 0x7f)
3316 {
3317 stream_put (s, v | 0x80);
3318 v >>= 7;
3319 }
3320
3321 stream_put (s, v);
3322}
3323
3324ecb_cold static void
3325stream_put_tv (stream s, int bop, uint32_t v)
3326{
3327 printf ("put tv %d %d\n", bop, v);//D
3328 stream_put (s, bop);
3329 stream_put_v (s, v);
3330}
3331
3332ecb_cold static void
3333stream_put_stream (stream s, stream o)
3334{
3335 uint32_t i;
3336
3337 for (i = 0; i < stream_size (o); ++i)
3338 stream_put (s, stream_data (o)[i]);
3339
3340 stream_free (o);
3341}
3342
3343// calculates a (preferably small) integer that makes it possible to find
3344// the symbol again. if pointers were offsets into a memory area... until
3345// then, we return segment number in the low bits, and offset in the high
3346// bits.
3347// also, this function must never return 0.
3348ecb_cold static uint32_t
3349symbol_id (SCHEME_P_ pointer sym)
3350{
3351 struct cell *p = CELL (sym);
3352 int i;
3353
3354 for (i = SCHEME_V->last_cell_seg; i >= 0; --i)
3355 if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i])
3356 return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG);
3357
3358 abort ();
3359}
3360
3361ecb_cold static uint32_t
3362cell_id (SCHEME_P_ pointer p)
3363{
3364 return symbol_id (SCHEME_A_ p);
3365}
3366
3367enum byteop
3368{
3369 BOP_NIL,
3370 BOP_SYNTAX,
3371 BOP_INTEGER,
3372 BOP_SYMBOL,
3373 BOP_LIST_BEG,
3374 BOP_LIST_END,
3375 BOP_BIFT, // branch if true
3376 BOP_BIFF, // branch if false
3377 BOP_BIFNE, // branch if not eqv?
3378 BOP_BRA, // "short" branch
3379 BOP_JMP, // "long" jump
3380 BOP_DATUM,
3381 BOP_LET,
3382 BOP_LETAST,
3383 BOP_LETREC,
3384 BOP_DEFINE,
3385 BOP_MACRO,
3386 BOP_SET,
3387 BOP_BEGIN,
3388 BOP_LAMBDA,
3389};
3390
3391ecb_cold static void compile_expr (SCHEME_P_ stream s, pointer x);
3392
3393ecb_cold static void
3394compile_list (SCHEME_P_ stream s, pointer x)
3395{
3396 for (; x != NIL; x = cdr (x))
3397 compile_expr (SCHEME_A_ s, car (x));
3398}
3399
3311static int 3400static void
3401compile_if (SCHEME_P_ stream s, pointer cond, pointer ift, pointer iff)
3402{
3403 //TODO: borked
3404 stream sift = stream_init (); compile_expr (SCHEME_A_ sift, ift);
3405
3406 stream_put (s, BOP_BIFF);
3407 compile_expr (SCHEME_A_ s, cond);
3408 stream_put_v (s, stream_size (sift));
3409 stream_put_stream (s, sift);
3410
3411 if (iff != NIL)
3412 {
3413 stream siff = stream_init (); compile_expr (SCHEME_A_ siff, iff);
3414 stream_put_tv (s, BOP_BRA, stream_size (siff));
3415 stream_put_stream (s, siff);
3416 }
3417}
3418
3419typedef uint32_t stream_fixup;
3420
3421static stream_fixup
3422stream_put_fixup (stream s)
3423{
3424 stream_put (s, 0);
3425 stream_put (s, 0);
3426
3427 return stream_size (s);
3428}
3429
3430static void
3431stream_fix_fixup (stream s, stream_fixup fixup, uint32_t target)
3432{
3433 target -= fixup;
3434 assert (target < (1 << 14));
3435 stream_data (s)[fixup - 2] = target | 0x80;
3436 stream_data (s)[fixup - 1] = target >> 7;
3437}
3438
3439static void
3440compile_and_or (SCHEME_P_ stream s, int and, pointer x)
3441{
3442 if (cdr (x) == NIL)
3443 compile_expr (SCHEME_A_ s, car (x));
3444 else
3445 {
3446 stream_put (s, and ? BOP_BIFF : BOP_BIFT);
3447 compile_expr (SCHEME_A_ s, car (x));
3448 stream_fixup end = stream_put_fixup (s);
3449
3450 compile_and_or (SCHEME_A_ s, and, cdr (x));
3451 stream_fix_fixup (s, end, stream_size (s));
3452 }
3453}
3454
3455ecb_cold static void
3456compile_expr (SCHEME_P_ stream s, pointer x)
3457{
3458 if (x == NIL)
3459 {
3460 stream_put (s, BOP_NIL);
3461 return;
3462 }
3463
3464 if (is_pair (x))
3465 {
3466 pointer head = car (x);
3467
3468 if (is_syntax (head))
3469 {
3470 x = cdr (x);
3471
3472 switch (syntaxnum (head))
3473 {
3474 case OP_IF0: /* if */
3475 compile_if (SCHEME_A_ s, car (x), cadr (x), caddr (x));
3476 break;
3477
3478 case OP_OR0: /* or */
3479 compile_and_or (SCHEME_A_ s, 0, x);
3480 break;
3481
3482 case OP_AND0: /* and */
3483 compile_and_or (SCHEME_A_ s, 1, x);
3484 break;
3485
3486 case OP_CASE0: /* case */
3487 abort ();
3488 break;
3489
3490 case OP_COND0: /* cond */
3491 abort ();
3492 break;
3493
3494 case OP_LET0: /* let */
3495 case OP_LET0AST: /* let* */
3496 case OP_LET0REC: /* letrec */
3497 switch (syntaxnum (head))
3498 {
3499 case OP_LET0: stream_put (s, BOP_LET ); break;
3500 case OP_LET0AST: stream_put (s, BOP_LETAST); break;
3501 case OP_LET0REC: stream_put (s, BOP_LETREC); break;
3502 }
3503
3504 {
3505 pointer bindings = car (x);
3506 pointer body = cadr (x);
3507
3508 for (x = bindings; x != NIL; x = cdr (x))
3509 {
3510 pointer init = NIL;
3511 pointer var = car (x);
3512
3513 if (is_pair (var))
3514 {
3515 init = cdr (var);
3516 var = car (var);
3517 }
3518
3519 stream_put_v (s, symbol_id (SCHEME_A_ var));
3520 compile_expr (SCHEME_A_ s, init);
3521 }
3522
3523 stream_put_v (s, 0);
3524 compile_expr (SCHEME_A_ s, body);
3525 }
3526 break;
3527
3528 case OP_DEF0: /* define */
3529 case OP_MACRO0: /* macro */
3530 stream_put (s, syntaxnum (head) == OP_DEF0 ? BOP_DEFINE : BOP_MACRO);
3531 stream_put_v (s, cell_id (SCHEME_A_ car (x)));
3532 compile_expr (SCHEME_A_ s, cadr (x));
3533 break;
3534
3535 case OP_SET0: /* set! */
3536 stream_put (s, BOP_SET);
3537 stream_put_v (s, symbol_id (SCHEME_A_ car (x)));
3538 compile_expr (SCHEME_A_ s, cadr (x));
3539 break;
3540
3541 case OP_BEGIN: /* begin */
3542 stream_put (s, BOP_BEGIN);
3543 compile_list (SCHEME_A_ s, x);
3544 return;
3545
3546 case OP_DELAY: /* delay */
3547 abort ();
3548 break;
3549
3550 case OP_QUOTE: /* quote */
3551 stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x));
3552 break;
3553
3554 case OP_LAMBDA: /* lambda */
3555 {
3556 pointer formals = car (x);
3557 pointer body = cadr (x);
3558
3559 stream_put (s, BOP_LAMBDA);
3560
3561 for (; is_pair (formals); formals = cdr (formals))
3562 stream_put_v (s, symbol_id (SCHEME_A_ car (formals)));
3563
3564 stream_put_v (s, 0);
3565 stream_put_v (s, formals == NIL ? 0 : symbol_id (SCHEME_A_ formals));
3566
3567 compile_expr (SCHEME_A_ s, body);
3568 }
3569 break;
3570
3571 case OP_C0STREAM:/* cons-stream */
3572 abort ();
3573 break;
3574 }
3575
3576 return;
3577 }
3578
3579 pointer m = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, head, 1);
3580
3581 if (m != NIL)
3582 {
3583 m = slot_value_in_env (m);
3584
3585 if (is_macro (m))
3586 {
3587 s_save (SCHEME_A_ OP_DEBUG2, SCHEME_V->args, SCHEME_V->code);
3588 SCHEME_V->code = m;
3589 SCHEME_V->args = cons (x, NIL);
3590 Eval_Cycle (SCHEME_A_ OP_APPLY);
3591 x = SCHEME_V->value;
3592 compile_expr (SCHEME_A_ s, SCHEME_V->value);
3593 return;
3594 }
3595 }
3596 }
3597
3598 switch (type (x))
3599 {
3600 case T_INTEGER:
3601 {
3602 IVALUE iv = ivalue_unchecked (x);
3603 iv = iv < 0 ? ((uint32_t)-iv << 1) | 1 : (uint32_t)iv << 1;
3604 stream_put_tv (s, BOP_INTEGER, iv);
3605 }
3606 return;
3607
3608 case T_SYMBOL:
3609 stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x));
3610 return;
3611
3612 case T_PAIR:
3613 stream_put (s, BOP_LIST_BEG);
3614
3615 for (; x != NIL; x = cdr (x))
3616 compile_expr (SCHEME_A_ s, car (x));
3617
3618 stream_put (s, BOP_LIST_END);
3619 return;
3620
3621 default:
3622 stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x));
3623 break;
3624 }
3625}
3626
3627ecb_cold static int
3628compile_closure (SCHEME_P_ pointer p)
3629{
3630 stream s = stream_init ();
3631
3632 compile_list (SCHEME_A_ s, cdar (p));
3633
3634 FILE *xxd = popen ("xxd", "we");
3635 fwrite (stream_data (s), 1, stream_size (s), xxd);
3636 fclose (xxd);
3637
3638 return stream_size (s);
3639}
3640
3641#endif
3642
3643/* syntax, eval, core, ... */
3644ecb_hot static int
3312opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3645opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3313{ 3646{
3314 pointer args = SCHEME_V->args; 3647 pointer args = SCHEME_V->args;
3315 pointer x, y; 3648 pointer x, y;
3316 3649
3317 switch (op) 3650 switch (op)
3318 { 3651 {
3319#if EXPERIMENT //D 3652#if EXPERIMENT //D
3320 case OP_DEBUG: 3653 case OP_DEBUG:
3321 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); 3654 {
3655 uint32_t len = compile_closure (SCHEME_A_ car (args));
3656 printf ("len = %d\n", len);
3322 printf ("\n"); 3657 printf ("\n");
3323 s_return (S_T); 3658 s_return (S_T);
3659 }
3660
3661 case OP_DEBUG2:
3662 return -1;
3324#endif 3663#endif
3664
3325 case OP_LOAD: /* load */ 3665 case OP_LOAD: /* load */
3326 if (file_interactive (SCHEME_A)) 3666 if (file_interactive (SCHEME_A))
3327 { 3667 {
3328 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3668 putstr (SCHEME_A_ "Loading ");
3329 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); 3669 putstr (SCHEME_A_ strvalue (car (args)));
3670 putcharacter (SCHEME_A_ '\n');
3330 } 3671 }
3331 3672
3332 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3673 if (!file_push (SCHEME_A_ strvalue (car (args))))
3333 Error_1 ("unable to open", car (args)); 3674 Error_1 ("unable to open", car (args));
3334 else 3675
3335 {
3336 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3676 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3337 s_goto (OP_T0LVL); 3677 s_goto (OP_T0LVL);
3338 }
3339 3678
3340 case OP_T0LVL: /* top level */ 3679 case OP_T0LVL: /* top level */
3341 3680
3342 /* If we reached the end of file, this loop is done. */ 3681 /* If we reached the end of file, this loop is done. */
3343 if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) 3682 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3344 { 3683 {
3345 if (SCHEME_V->file_i == 0) 3684 if (SCHEME_V->file_i == 0)
3346 { 3685 {
3347 SCHEME_V->args = NIL; 3686 SCHEME_V->args = NIL;
3348 s_goto (OP_QUIT); 3687 s_goto (OP_QUIT);
3359 /* If interactive, be nice to user. */ 3698 /* If interactive, be nice to user. */
3360 if (file_interactive (SCHEME_A)) 3699 if (file_interactive (SCHEME_A))
3361 { 3700 {
3362 SCHEME_V->envir = SCHEME_V->global_env; 3701 SCHEME_V->envir = SCHEME_V->global_env;
3363 dump_stack_reset (SCHEME_A); 3702 dump_stack_reset (SCHEME_A);
3364 putstr (SCHEME_A_ "\n"); 3703 putcharacter (SCHEME_A_ '\n');
3704#if EXPERIMENT
3705 system ("ps v $PPID");
3706#endif
3365 putstr (SCHEME_A_ prompt); 3707 putstr (SCHEME_A_ prompt);
3366 } 3708 }
3367 3709
3368 /* Set up another iteration of REPL */ 3710 /* Set up another iteration of REPL */
3369 SCHEME_V->nesting = 0; 3711 SCHEME_V->nesting = 0;
3404 { 3746 {
3405 SCHEME_V->print_flag = 1; 3747 SCHEME_V->print_flag = 1;
3406 SCHEME_V->args = SCHEME_V->value; 3748 SCHEME_V->args = SCHEME_V->value;
3407 s_goto (OP_P0LIST); 3749 s_goto (OP_P0LIST);
3408 } 3750 }
3409 else 3751
3410 s_return (SCHEME_V->value); 3752 s_return (SCHEME_V->value);
3411 3753
3412 case OP_EVAL: /* main part of evaluation */ 3754 case OP_EVAL: /* main part of evaluation */
3413#if USE_TRACING 3755#if USE_TRACING
3414 if (SCHEME_V->tracing) 3756 if (SCHEME_V->tracing)
3415 { 3757 {
3426#endif 3768#endif
3427 if (is_symbol (SCHEME_V->code)) /* symbol */ 3769 if (is_symbol (SCHEME_V->code)) /* symbol */
3428 { 3770 {
3429 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); 3771 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1);
3430 3772
3431 if (x != NIL) 3773 if (x == NIL)
3432 s_return (slot_value_in_env (x));
3433 else
3434 Error_1 ("eval: unbound variable:", SCHEME_V->code); 3774 Error_1 ("eval: unbound variable:", SCHEME_V->code);
3775
3776 s_return (slot_value_in_env (x));
3435 } 3777 }
3436 else if (is_pair (SCHEME_V->code)) 3778 else if (is_pair (SCHEME_V->code))
3437 { 3779 {
3438 x = car (SCHEME_V->code); 3780 x = car (SCHEME_V->code);
3439 3781
3448 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ 3790 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */
3449 SCHEME_V->code = x; 3791 SCHEME_V->code = x;
3450 s_goto (OP_EVAL); 3792 s_goto (OP_EVAL);
3451 } 3793 }
3452 } 3794 }
3453 else 3795
3454 s_return (SCHEME_V->code); 3796 s_return (SCHEME_V->code);
3455 3797
3456 case OP_E0ARGS: /* eval arguments */ 3798 case OP_E0ARGS: /* eval arguments */
3457 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ 3799 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3458 { 3800 {
3459 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3801 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3460 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3802 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3461 SCHEME_V->code = SCHEME_V->value; 3803 SCHEME_V->code = SCHEME_V->value;
3462 s_goto (OP_APPLY); 3804 s_goto (OP_APPLY);
3463 } 3805 }
3464 else 3806
3465 {
3466 SCHEME_V->code = cdr (SCHEME_V->code); 3807 SCHEME_V->code = cdr (SCHEME_V->code);
3467 s_goto (OP_E1ARGS); 3808 s_goto (OP_E1ARGS);
3468 }
3469 3809
3470 case OP_E1ARGS: /* eval arguments */ 3810 case OP_E1ARGS: /* eval arguments */
3471 args = cons (SCHEME_V->value, args); 3811 args = cons (SCHEME_V->value, args);
3472 3812
3473 if (is_pair (SCHEME_V->code)) /* continue */ 3813 if (is_pair (SCHEME_V->code)) /* continue */
3484 SCHEME_V->args = cdr (args); 3824 SCHEME_V->args = cdr (args);
3485 s_goto (OP_APPLY); 3825 s_goto (OP_APPLY);
3486 } 3826 }
3487 3827
3488#if USE_TRACING 3828#if USE_TRACING
3489
3490 case OP_TRACING: 3829 case OP_TRACING:
3491 { 3830 {
3492 int tr = SCHEME_V->tracing; 3831 int tr = SCHEME_V->tracing;
3493 3832
3494 SCHEME_V->tracing = ivalue_unchecked (car (args)); 3833 SCHEME_V->tracing = ivalue_unchecked (car (args));
3495 s_return (mk_integer (SCHEME_A_ tr)); 3834 s_return (mk_integer (SCHEME_A_ tr));
3496 } 3835 }
3497
3498#endif 3836#endif
3499 3837
3500 case OP_APPLY: /* apply 'code' to 'args' */ 3838 case OP_APPLY: /* apply 'code' to 'args' */
3501#if USE_TRACING 3839#if USE_TRACING
3502 if (SCHEME_V->tracing) 3840 if (SCHEME_V->tracing)
3516 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3854 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3517 else if (is_foreign (SCHEME_V->code)) 3855 else if (is_foreign (SCHEME_V->code))
3518 { 3856 {
3519 /* Keep nested calls from GC'ing the arglist */ 3857 /* Keep nested calls from GC'ing the arglist */
3520 push_recent_alloc (SCHEME_A_ args, NIL); 3858 push_recent_alloc (SCHEME_A_ args, NIL);
3521 x = SCHEME_V->code->object.ff (SCHEME_A_ args); 3859 x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args);
3522 3860
3523 s_return (x); 3861 s_return (x);
3524 } 3862 }
3525 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ 3863 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */
3526 { 3864 {
3556 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3894 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3557 { 3895 {
3558 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3896 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3559 s_return (args != NIL ? car (args) : NIL); 3897 s_return (args != NIL ? car (args) : NIL);
3560 } 3898 }
3561 else 3899
3562 Error_0 ("illegal function"); 3900 Error_0 ("illegal function");
3563 3901
3564 case OP_DOMACRO: /* do macro */ 3902 case OP_DOMACRO: /* do macro */
3565 SCHEME_V->code = SCHEME_V->value; 3903 SCHEME_V->code = SCHEME_V->value;
3566 s_goto (OP_EVAL); 3904 s_goto (OP_EVAL);
3567
3568#if 1
3569 3905
3570 case OP_LAMBDA: /* lambda */ 3906 case OP_LAMBDA: /* lambda */
3571 /* If the hook is defined, apply it to SCHEME_V->code, otherwise 3907 /* If the hook is defined, apply it to SCHEME_V->code, otherwise
3572 set SCHEME_V->value fall thru */ 3908 set SCHEME_V->value fall thru */
3573 { 3909 {
3580 SCHEME_V->code = slot_value_in_env (f); 3916 SCHEME_V->code = slot_value_in_env (f);
3581 s_goto (OP_APPLY); 3917 s_goto (OP_APPLY);
3582 } 3918 }
3583 3919
3584 SCHEME_V->value = SCHEME_V->code; 3920 SCHEME_V->value = SCHEME_V->code;
3585 /* Fallthru */
3586 } 3921 }
3922 /* Fallthru */
3587 3923
3588 case OP_LAMBDA1: 3924 case OP_LAMBDA1:
3589 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); 3925 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir));
3590
3591#else
3592
3593 case OP_LAMBDA: /* lambda */
3594 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3595
3596#endif
3597 3926
3598 case OP_MKCLOSURE: /* make-closure */ 3927 case OP_MKCLOSURE: /* make-closure */
3599 x = car (args); 3928 x = car (args);
3600 3929
3601 if (car (x) == SCHEME_V->LAMBDA) 3930 if (car (x) == SCHEME_V->LAMBDA)
3640 else 3969 else
3641 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 3970 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
3642 3971
3643 s_return (SCHEME_V->code); 3972 s_return (SCHEME_V->code);
3644 3973
3645
3646 case OP_DEFP: /* defined? */ 3974 case OP_DEFP: /* defined? */
3647 x = SCHEME_V->envir; 3975 x = SCHEME_V->envir;
3648 3976
3649 if (cdr (args) != NIL) 3977 if (cdr (args) != NIL)
3650 x = cadr (args); 3978 x = cadr (args);
3668 s_return (SCHEME_V->value); 3996 s_return (SCHEME_V->value);
3669 } 3997 }
3670 else 3998 else
3671 Error_1 ("set!: unbound variable:", SCHEME_V->code); 3999 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3672 4000
3673
3674 case OP_BEGIN: /* begin */ 4001 case OP_BEGIN: /* begin */
3675 if (!is_pair (SCHEME_V->code)) 4002 if (!is_pair (SCHEME_V->code))
3676 s_return (SCHEME_V->code); 4003 s_return (SCHEME_V->code);
3677 4004
3678 if (cdr (SCHEME_V->code) != NIL) 4005 if (cdr (SCHEME_V->code) != NIL)
3689 case OP_IF1: /* if */ 4016 case OP_IF1: /* if */
3690 if (is_true (SCHEME_V->value)) 4017 if (is_true (SCHEME_V->value))
3691 SCHEME_V->code = car (SCHEME_V->code); 4018 SCHEME_V->code = car (SCHEME_V->code);
3692 else 4019 else
3693 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ 4020 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
4021
3694 s_goto (OP_EVAL); 4022 s_goto (OP_EVAL);
3695 4023
3696 case OP_LET0: /* let */ 4024 case OP_LET0: /* let */
3697 SCHEME_V->args = NIL; 4025 SCHEME_V->args = NIL;
3698 SCHEME_V->value = SCHEME_V->code; 4026 SCHEME_V->value = SCHEME_V->code;
3699 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); 4027 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code);
3700 s_goto (OP_LET1); 4028 s_goto (OP_LET1);
3701 4029
3702 case OP_LET1: /* let (calculate parameters) */ 4030 case OP_LET1: /* let (calculate parameters) */
4031 case OP_LET1REC: /* letrec (calculate parameters) */
3703 args = cons (SCHEME_V->value, args); 4032 args = cons (SCHEME_V->value, args);
3704 4033
3705 if (is_pair (SCHEME_V->code)) /* continue */ 4034 if (is_pair (SCHEME_V->code)) /* continue */
3706 { 4035 {
3707 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 4036 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3708 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); 4037 Error_1 ("Bad syntax of binding spec in let/letrec:", car (SCHEME_V->code));
3709 4038
3710 s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); 4039 s_save (SCHEME_A_ op, args, cdr (SCHEME_V->code));
3711 SCHEME_V->code = cadar (SCHEME_V->code); 4040 SCHEME_V->code = cadar (SCHEME_V->code);
3712 SCHEME_V->args = NIL; 4041 SCHEME_V->args = NIL;
3713 s_goto (OP_EVAL); 4042 s_goto (OP_EVAL);
3714 } 4043 }
3715 else /* end */ 4044
3716 { 4045 /* end */
3717 args = reverse_in_place (SCHEME_A_ NIL, args); 4046 args = reverse_in_place (SCHEME_A_ NIL, args);
3718 SCHEME_V->code = car (args); 4047 SCHEME_V->code = car (args);
3719 SCHEME_V->args = cdr (args); 4048 SCHEME_V->args = cdr (args);
3720 s_goto (OP_LET2); 4049 s_goto (op == OP_LET1 ? OP_LET2 : OP_LET2REC);
3721 }
3722 4050
3723 case OP_LET2: /* let */ 4051 case OP_LET2: /* let */
3724 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4052 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3725 4053
3726 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; 4054 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args;
3730 if (is_symbol (car (SCHEME_V->code))) /* named let */ 4058 if (is_symbol (car (SCHEME_V->code))) /* named let */
3731 { 4059 {
3732 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) 4060 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3733 { 4061 {
3734 if (!is_pair (x)) 4062 if (!is_pair (x))
3735 Error_1 ("Bad syntax of binding in let :", x); 4063 Error_1 ("Bad syntax of binding in let:", x);
3736 4064
3737 if (!is_list (SCHEME_A_ car (x))) 4065 if (!is_list (SCHEME_A_ car (x)))
3738 Error_1 ("Bad syntax of binding in let :", car (x)); 4066 Error_1 ("Bad syntax of binding in let:", car (x));
3739 4067
3740 args = cons (caar (x), args); 4068 args = cons (caar (x), args);
3741 } 4069 }
3742 4070
3743 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)), 4071 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)),
3760 SCHEME_V->code = cdr (SCHEME_V->code); 4088 SCHEME_V->code = cdr (SCHEME_V->code);
3761 s_goto (OP_BEGIN); 4089 s_goto (OP_BEGIN);
3762 } 4090 }
3763 4091
3764 if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code))) 4092 if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code)))
3765 Error_1 ("Bad syntax of binding spec in let* :", car (SCHEME_V->code)); 4093 Error_1 ("Bad syntax of binding spec in let*:", car (SCHEME_V->code));
3766 4094
3767 s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code)); 4095 s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code));
3768 SCHEME_V->code = car (cdaar (SCHEME_V->code)); 4096 SCHEME_V->code = car (cdaar (SCHEME_V->code));
3769 s_goto (OP_EVAL); 4097 s_goto (OP_EVAL);
3770 4098
3781 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); 4109 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3782 SCHEME_V->code = cadar (SCHEME_V->code); 4110 SCHEME_V->code = cadar (SCHEME_V->code);
3783 SCHEME_V->args = NIL; 4111 SCHEME_V->args = NIL;
3784 s_goto (OP_EVAL); 4112 s_goto (OP_EVAL);
3785 } 4113 }
3786 else /* end */ 4114
4115 /* end */
3787 { 4116
3788 SCHEME_V->code = args; 4117 SCHEME_V->code = args;
3789 SCHEME_V->args = NIL; 4118 SCHEME_V->args = NIL;
3790 s_goto (OP_BEGIN); 4119 s_goto (OP_BEGIN);
3791 }
3792 4120
3793 case OP_LET0REC: /* letrec */ 4121 case OP_LET0REC: /* letrec */
3794 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4122 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3795 SCHEME_V->args = NIL; 4123 SCHEME_V->args = NIL;
3796 SCHEME_V->value = SCHEME_V->code; 4124 SCHEME_V->value = SCHEME_V->code;
3797 SCHEME_V->code = car (SCHEME_V->code); 4125 SCHEME_V->code = car (SCHEME_V->code);
3798 s_goto (OP_LET1REC); 4126 s_goto (OP_LET1REC);
3799 4127
3800 case OP_LET1REC: /* letrec (calculate parameters) */ 4128 /* OP_LET1REC handled by OP_LET1 */
3801 args = cons (SCHEME_V->value, args);
3802
3803 if (is_pair (SCHEME_V->code)) /* continue */
3804 {
3805 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3806 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3807
3808 s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code));
3809 SCHEME_V->code = cadar (SCHEME_V->code);
3810 SCHEME_V->args = NIL;
3811 s_goto (OP_EVAL);
3812 }
3813 else /* end */
3814 {
3815 args = reverse_in_place (SCHEME_A_ NIL, args);
3816 SCHEME_V->code = car (args);
3817 SCHEME_V->args = cdr (args);
3818 s_goto (OP_LET2REC);
3819 }
3820 4129
3821 case OP_LET2REC: /* letrec */ 4130 case OP_LET2REC: /* letrec */
3822 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y)) 4131 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y))
3823 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 4132 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3824 4133
3854 } 4163 }
3855 else 4164 else
3856 { 4165 {
3857 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 4166 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
3858 s_return (NIL); 4167 s_return (NIL);
3859 else 4168
3860 {
3861 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 4169 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
3862 SCHEME_V->code = caar (SCHEME_V->code); 4170 SCHEME_V->code = caar (SCHEME_V->code);
3863 s_goto (OP_EVAL); 4171 s_goto (OP_EVAL);
3864 }
3865 } 4172 }
3866 4173
3867 case OP_DELAY: /* delay */ 4174 case OP_DELAY: /* delay */
3868 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); 4175 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
3869 set_typeflag (x, T_PROMISE); 4176 set_typeflag (x, T_PROMISE);
3880 case OP_AND1: /* and */ 4187 case OP_AND1: /* and */
3881 if (is_false (SCHEME_V->value)) 4188 if (is_false (SCHEME_V->value))
3882 s_return (SCHEME_V->value); 4189 s_return (SCHEME_V->value);
3883 else if (SCHEME_V->code == NIL) 4190 else if (SCHEME_V->code == NIL)
3884 s_return (SCHEME_V->value); 4191 s_return (SCHEME_V->value);
3885 else 4192
3886 {
3887 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 4193 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
3888 SCHEME_V->code = car (SCHEME_V->code); 4194 SCHEME_V->code = car (SCHEME_V->code);
3889 s_goto (OP_EVAL); 4195 s_goto (OP_EVAL);
3890 }
3891 4196
3892 case OP_OR0: /* or */ 4197 case OP_OR0: /* or */
3893 if (SCHEME_V->code == NIL) 4198 if (SCHEME_V->code == NIL)
3894 s_return (S_F); 4199 s_return (S_F);
3895 4200
3900 case OP_OR1: /* or */ 4205 case OP_OR1: /* or */
3901 if (is_true (SCHEME_V->value)) 4206 if (is_true (SCHEME_V->value))
3902 s_return (SCHEME_V->value); 4207 s_return (SCHEME_V->value);
3903 else if (SCHEME_V->code == NIL) 4208 else if (SCHEME_V->code == NIL)
3904 s_return (SCHEME_V->value); 4209 s_return (SCHEME_V->value);
3905 else 4210
3906 {
3907 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 4211 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
3908 SCHEME_V->code = car (SCHEME_V->code); 4212 SCHEME_V->code = car (SCHEME_V->code);
3909 s_goto (OP_EVAL); 4213 s_goto (OP_EVAL);
3910 }
3911 4214
3912 case OP_C0STREAM: /* cons-stream */ 4215 case OP_C0STREAM: /* cons-stream */
3913 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 4216 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3914 SCHEME_V->code = car (SCHEME_V->code); 4217 SCHEME_V->code = car (SCHEME_V->code);
3915 s_goto (OP_EVAL); 4218 s_goto (OP_EVAL);
3980 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); 4283 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x));
3981 SCHEME_V->code = caar (x); 4284 SCHEME_V->code = caar (x);
3982 s_goto (OP_EVAL); 4285 s_goto (OP_EVAL);
3983 } 4286 }
3984 } 4287 }
3985 else 4288
3986 s_return (NIL); 4289 s_return (NIL);
3987 4290
3988 case OP_CASE2: /* case */ 4291 case OP_CASE2: /* case */
3989 if (is_true (SCHEME_V->value)) 4292 if (is_true (SCHEME_V->value))
3990 s_goto (OP_BEGIN); 4293 s_goto (OP_BEGIN);
3991 else 4294
3992 s_return (NIL); 4295 s_return (NIL);
3993 4296
3994 case OP_PAPPLY: /* apply */ 4297 case OP_PAPPLY: /* apply */
3995 SCHEME_V->code = car (args); 4298 SCHEME_V->code = car (args);
3996 SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); 4299 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3997 /*SCHEME_V->args = cadr(args); */ 4300 /*SCHEME_V->args = cadr(args); */
4011 } 4314 }
4012 4315
4013 if (USE_ERROR_CHECKING) abort (); 4316 if (USE_ERROR_CHECKING) abort ();
4014} 4317}
4015 4318
4016static int 4319/* math, cxr */
4320ecb_hot static int
4017opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4321opexe_1 (SCHEME_P_ enum scheme_opcodes op)
4018{ 4322{
4019 pointer args = SCHEME_V->args; 4323 pointer args = SCHEME_V->args;
4020 pointer x = car (args); 4324 pointer x = car (args);
4021 num v; 4325 num v;
4022 4326
4023 switch (op) 4327 switch (op)
4024 { 4328 {
4025#if USE_MATH 4329#if USE_MATH
4026 case OP_INEX2EX: /* inexact->exact */ 4330 case OP_INEX2EX: /* inexact->exact */
4027 {
4028 if (is_integer (x)) 4331 if (!is_integer (x))
4029 s_return (x); 4332 {
4030
4031 RVALUE r = rvalue_unchecked (x); 4333 RVALUE r = rvalue_unchecked (x);
4032 4334
4033 if (r == (RVALUE)(IVALUE)r) 4335 if (r == (RVALUE)(IVALUE)r)
4034 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); 4336 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
4035 else 4337 else
4036 Error_1 ("inexact->exact: not integral:", x); 4338 Error_1 ("inexact->exact: not integral:", x);
4037 } 4339 }
4038 4340
4341 s_return (x);
4342
4343 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4344 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4345 case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x))));
4346 case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x))));
4347
4348 case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4039 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4349 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4040 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 4350 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
4351 / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args))))));
4041 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4352 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
4042 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 4353 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
4043 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); 4354 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x))));
4044 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); 4355 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x))));
4045 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); 4356 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4046 4357
4047 case OP_ATAN: 4358 case OP_ATAN:
4359 s_return (mk_real (SCHEME_A_
4048 if (cdr (args) == NIL) 4360 cdr (args) == NIL
4049 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4361 ? atan (rvalue (x))
4050 else 4362 : atan2 (rvalue (x), rvalue (cadr (args)))));
4051 {
4052 pointer y = cadr (args);
4053 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4054 }
4055
4056 case OP_SQRT:
4057 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4058 4363
4059 case OP_EXPT: 4364 case OP_EXPT:
4060 { 4365 {
4061 RVALUE result; 4366 RVALUE result;
4062 int real_result = 1; 4367 int real_result = 1;
4085 if (real_result) 4390 if (real_result)
4086 s_return (mk_real (SCHEME_A_ result)); 4391 s_return (mk_real (SCHEME_A_ result));
4087 else 4392 else
4088 s_return (mk_integer (SCHEME_A_ result)); 4393 s_return (mk_integer (SCHEME_A_ result));
4089 } 4394 }
4090
4091 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4092 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4093
4094 case OP_TRUNCATE:
4095 {
4096 RVALUE n = rvalue (x);
4097 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4098 }
4099
4100 case OP_ROUND:
4101 if (is_integer (x))
4102 s_return (x);
4103
4104 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4105#endif 4395#endif
4106 4396
4107 case OP_ADD: /* + */ 4397 case OP_ADD: /* + */
4108 v = num_zero; 4398 v = num_zero;
4109 4399
4197 else 4487 else
4198 Error_0 ("modulo: division by zero"); 4488 Error_0 ("modulo: division by zero");
4199 4489
4200 s_return (mk_number (SCHEME_A_ v)); 4490 s_return (mk_number (SCHEME_A_ v));
4201 4491
4202 case OP_CAR: /* car */ 4492 /* the compiler will optimize this mess... */
4203 s_return (caar (args)); 4493 case OP_CAR: op_car: s_return (car (x));
4204 4494 case OP_CDR: op_cdr: s_return (cdr (x));
4205 case OP_CDR: /* cdr */ 4495 case OP_CAAR: op_caar: x = car (x); goto op_car;
4206 s_return (cdar (args)); 4496 case OP_CADR: op_cadr: x = cdr (x); goto op_car;
4497 case OP_CDAR: op_cdar: x = car (x); goto op_cdr;
4498 case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr;
4499 case OP_CAAAR: op_caaar: x = car (x); goto op_caar;
4500 case OP_CAADR: op_caadr: x = cdr (x); goto op_caar;
4501 case OP_CADAR: op_cadar: x = car (x); goto op_cadr;
4502 case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr;
4503 case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar;
4504 case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar;
4505 case OP_CDDAR: op_cddar: x = car (x); goto op_cddr;
4506 case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr;
4507 case OP_CAAAAR: x = car (x); goto op_caaar;
4508 case OP_CAAADR: x = cdr (x); goto op_caaar;
4509 case OP_CAADAR: x = car (x); goto op_caadr;
4510 case OP_CAADDR: x = cdr (x); goto op_caadr;
4511 case OP_CADAAR: x = car (x); goto op_cadar;
4512 case OP_CADADR: x = cdr (x); goto op_cadar;
4513 case OP_CADDAR: x = car (x); goto op_caddr;
4514 case OP_CADDDR: x = cdr (x); goto op_caddr;
4515 case OP_CDAAAR: x = car (x); goto op_cdaar;
4516 case OP_CDAADR: x = cdr (x); goto op_cdaar;
4517 case OP_CDADAR: x = car (x); goto op_cdadr;
4518 case OP_CDADDR: x = cdr (x); goto op_cdadr;
4519 case OP_CDDAAR: x = car (x); goto op_cddar;
4520 case OP_CDDADR: x = cdr (x); goto op_cddar;
4521 case OP_CDDDAR: x = car (x); goto op_cdddr;
4522 case OP_CDDDDR: x = cdr (x); goto op_cdddr;
4207 4523
4208 case OP_CONS: /* cons */ 4524 case OP_CONS: /* cons */
4209 set_cdr (args, cadr (args)); 4525 set_cdr (args, cadr (args));
4210 s_return (args); 4526 s_return (args);
4211 4527
4385 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4701 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4386 4702
4387 s_return (newstr); 4703 s_return (newstr);
4388 } 4704 }
4389 4705
4390 case OP_SUBSTR: /* substring */ 4706 case OP_STRING_COPY: /* substring/string-copy */
4391 { 4707 {
4392 char *str = strvalue (x); 4708 char *str = strvalue (x);
4393 int index0 = ivalue_unchecked (cadr (args)); 4709 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4394 int index1; 4710 int index1;
4395 int len; 4711 int len;
4396 4712
4397 if (index0 > strlength (x)) 4713 if (index0 > strlength (x))
4398 Error_1 ("substring: start out of bounds:", cadr (args)); 4714 Error_1 ("string->copy: start out of bounds:", cadr (args));
4399 4715
4400 if (cddr (args) != NIL) 4716 if (cddr (args) != NIL)
4401 { 4717 {
4402 index1 = ivalue_unchecked (caddr (args)); 4718 index1 = ivalue_unchecked (caddr (args));
4403 4719
4404 if (index1 > strlength (x) || index1 < index0) 4720 if (index1 > strlength (x) || index1 < index0)
4405 Error_1 ("substring: end out of bounds:", caddr (args)); 4721 Error_1 ("string->copy: end out of bounds:", caddr (args));
4406 } 4722 }
4407 else 4723 else
4408 index1 = strlength (x); 4724 index1 = strlength (x);
4409 4725
4410 len = index1 - index0; 4726 len = index1 - index0;
4411 x = mk_empty_string (SCHEME_A_ len, ' '); 4727 x = mk_counted_string (SCHEME_A_ str + index0, len);
4412 memcpy (strvalue (x), str + index0, len);
4413 strvalue (x)[len] = 0;
4414 4728
4415 s_return (x); 4729 s_return (x);
4416 } 4730 }
4417 4731
4418 case OP_VECTOR: /* vector */ 4732 case OP_VECTOR: /* vector */
4492 } 4806 }
4493 4807
4494 if (USE_ERROR_CHECKING) abort (); 4808 if (USE_ERROR_CHECKING) abort ();
4495} 4809}
4496 4810
4497static int 4811/* relational ops */
4812ecb_hot static int
4498opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4813opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4499{ 4814{
4500 pointer x = SCHEME_V->args; 4815 pointer x = SCHEME_V->args;
4501 4816
4502 for (;;) 4817 for (;;)
4523 } 4838 }
4524 4839
4525 s_return (S_T); 4840 s_return (S_T);
4526} 4841}
4527 4842
4528static int 4843/* predicates */
4844ecb_hot static int
4529opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4845opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4530{ 4846{
4531 pointer args = SCHEME_V->args; 4847 pointer args = SCHEME_V->args;
4532 pointer a = car (args); 4848 pointer a = car (args);
4533 pointer d = cdr (args); 4849 pointer d = cdr (args);
4580 } 4896 }
4581 4897
4582 s_retbool (r); 4898 s_retbool (r);
4583} 4899}
4584 4900
4585static int 4901/* promises, list ops, ports */
4902ecb_hot static int
4586opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4903opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4587{ 4904{
4588 pointer args = SCHEME_V->args; 4905 pointer args = SCHEME_V->args;
4589 pointer a = car (args); 4906 pointer a = car (args);
4590 pointer x, y; 4907 pointer x, y;
4603 } 4920 }
4604 else 4921 else
4605 s_return (SCHEME_V->code); 4922 s_return (SCHEME_V->code);
4606 4923
4607 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4924 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4608 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4925 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4609 s_return (SCHEME_V->value); 4926 s_return (SCHEME_V->value);
4610 4927
4611#if USE_PORTS 4928#if USE_PORTS
4929
4930 case OP_EOF_OBJECT: /* eof-object */
4931 s_return (S_EOF);
4612 4932
4613 case OP_WRITE: /* write */ 4933 case OP_WRITE: /* write */
4614 case OP_DISPLAY: /* display */ 4934 case OP_DISPLAY: /* display */
4615 case OP_WRITE_CHAR: /* write-char */ 4935 case OP_WRITE_CHAR: /* write-char */
4616 if (is_pair (cdr (SCHEME_V->args))) 4936 if (is_pair (cdr (SCHEME_V->args)))
4630 else 4950 else
4631 SCHEME_V->print_flag = 0; 4951 SCHEME_V->print_flag = 0;
4632 4952
4633 s_goto (OP_P0LIST); 4953 s_goto (OP_P0LIST);
4634 4954
4955 //TODO: move to scheme
4635 case OP_NEWLINE: /* newline */ 4956 case OP_NEWLINE: /* newline */
4636 if (is_pair (args)) 4957 if (is_pair (args))
4637 { 4958 {
4638 if (a != SCHEME_V->outport) 4959 if (a != SCHEME_V->outport)
4639 { 4960 {
4641 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4962 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4642 SCHEME_V->outport = a; 4963 SCHEME_V->outport = a;
4643 } 4964 }
4644 } 4965 }
4645 4966
4646 putstr (SCHEME_A_ "\n"); 4967 putcharacter (SCHEME_A_ '\n');
4647 s_return (S_T); 4968 s_return (S_T);
4648#endif 4969#endif
4649 4970
4650 case OP_ERR0: /* error */ 4971 case OP_ERR0: /* error */
4651 SCHEME_V->retcode = -1; 4972 SCHEME_V->retcode = -1;
4660 putstr (SCHEME_A_ strvalue (car (args))); 4981 putstr (SCHEME_A_ strvalue (car (args)));
4661 SCHEME_V->args = cdr (args); 4982 SCHEME_V->args = cdr (args);
4662 s_goto (OP_ERR1); 4983 s_goto (OP_ERR1);
4663 4984
4664 case OP_ERR1: /* error */ 4985 case OP_ERR1: /* error */
4665 putstr (SCHEME_A_ " "); 4986 putcharacter (SCHEME_A_ ' ');
4666 4987
4667 if (args != NIL) 4988 if (args != NIL)
4668 { 4989 {
4669 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); 4990 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4670 SCHEME_V->args = a; 4991 SCHEME_V->args = a;
4671 SCHEME_V->print_flag = 1; 4992 SCHEME_V->print_flag = 1;
4672 s_goto (OP_P0LIST); 4993 s_goto (OP_P0LIST);
4673 } 4994 }
4674 else 4995 else
4675 { 4996 {
4676 putstr (SCHEME_A_ "\n"); 4997 putcharacter (SCHEME_A_ '\n');
4677 4998
4678 if (SCHEME_V->interactive_repl) 4999 if (SCHEME_V->interactive_repl)
4679 s_goto (OP_T0LVL); 5000 s_goto (OP_T0LVL);
4680 else 5001 else
4681 return -1; 5002 return -1;
4758 SCHEME_V->gc_verbose = (a != S_F); 5079 SCHEME_V->gc_verbose = (a != S_F);
4759 s_retbool (was); 5080 s_retbool (was);
4760 } 5081 }
4761 5082
4762 case OP_NEWSEGMENT: /* new-segment */ 5083 case OP_NEWSEGMENT: /* new-segment */
5084#if 0
4763 if (!is_pair (args) || !is_number (a)) 5085 if (!is_pair (args) || !is_number (a))
4764 Error_0 ("new-segment: argument must be a number"); 5086 Error_0 ("new-segment: argument must be a number");
4765 5087#endif
4766 alloc_cellseg (SCHEME_A_ ivalue (a)); 5088 s_retbool (alloc_cellseg (SCHEME_A));
4767
4768 s_return (S_T);
4769 5089
4770 case OP_OBLIST: /* oblist */ 5090 case OP_OBLIST: /* oblist */
4771 s_return (oblist_all_symbols (SCHEME_A)); 5091 s_return (oblist_all_symbols (SCHEME_A));
4772 5092
4773#if USE_PORTS 5093#if USE_PORTS
4843 s_return (p == NIL ? S_F : p); 5163 s_return (p == NIL ? S_F : p);
4844 } 5164 }
4845 5165
4846 case OP_GET_OUTSTRING: /* get-output-string */ 5166 case OP_GET_OUTSTRING: /* get-output-string */
4847 { 5167 {
4848 port *p; 5168 port *p = port (a);
4849 5169
4850 if ((p = a->object.port)->kind & port_string) 5170 if (p->kind & port_string)
4851 { 5171 {
4852 off_t size; 5172 off_t size;
4853 char *str; 5173 char *str;
4854 5174
4855 size = p->rep.string.curr - p->rep.string.start + 1; 5175 size = p->rep.string.curr - p->rep.string.start + 1;
4890 } 5210 }
4891 5211
4892 if (USE_ERROR_CHECKING) abort (); 5212 if (USE_ERROR_CHECKING) abort ();
4893} 5213}
4894 5214
4895static int 5215/* reading */
5216ecb_cold static int
4896opexe_5 (SCHEME_P_ enum scheme_opcodes op) 5217opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4897{ 5218{
4898 pointer args = SCHEME_V->args; 5219 pointer args = SCHEME_V->args;
4899 pointer x; 5220 pointer x;
4900 5221
4960 int res; 5281 int res;
4961 5282
4962 if (is_pair (args)) 5283 if (is_pair (args))
4963 p = car (args); 5284 p = car (args);
4964 5285
4965 res = p->object.port->kind & port_string; 5286 res = port (p)->kind & port_string;
4966 5287
4967 s_retbool (res); 5288 s_retbool (res);
4968 } 5289 }
4969 5290
4970 case OP_SET_INPORT: /* set-input-port */ 5291 case OP_SET_INPORT: /* set-input-port */
4979 case OP_RDSEXPR: 5300 case OP_RDSEXPR:
4980 switch (SCHEME_V->tok) 5301 switch (SCHEME_V->tok)
4981 { 5302 {
4982 case TOK_EOF: 5303 case TOK_EOF:
4983 s_return (S_EOF); 5304 s_return (S_EOF);
4984 /* NOTREACHED */
4985 5305
4986 case TOK_VEC: 5306 case TOK_VEC:
4987 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); 5307 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL);
4988 /* fall through */ 5308 /* fall through */
4989 5309
4992 5312
4993 if (SCHEME_V->tok == TOK_RPAREN) 5313 if (SCHEME_V->tok == TOK_RPAREN)
4994 s_return (NIL); 5314 s_return (NIL);
4995 else if (SCHEME_V->tok == TOK_DOT) 5315 else if (SCHEME_V->tok == TOK_DOT)
4996 Error_0 ("syntax error: illegal dot expression"); 5316 Error_0 ("syntax error: illegal dot expression");
4997 else 5317
4998 {
4999 SCHEME_V->nesting_stack[SCHEME_V->file_i]++; 5318 SCHEME_V->nesting_stack[SCHEME_V->file_i]++;
5000 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); 5319 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL);
5001 s_goto (OP_RDSEXPR); 5320 s_goto (OP_RDSEXPR);
5002 }
5003 5321
5004 case TOK_QUOTE: 5322 case TOK_QUOTE:
5005 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); 5323 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL);
5006 SCHEME_V->tok = token (SCHEME_A); 5324 SCHEME_V->tok = token (SCHEME_A);
5007 s_goto (OP_RDSEXPR); 5325 s_goto (OP_RDSEXPR);
5013 { 5331 {
5014 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); 5332 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL);
5015 SCHEME_V->tok = TOK_LPAREN; 5333 SCHEME_V->tok = TOK_LPAREN;
5016 s_goto (OP_RDSEXPR); 5334 s_goto (OP_RDSEXPR);
5017 } 5335 }
5018 else 5336
5019 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); 5337 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL);
5020
5021 s_goto (OP_RDSEXPR); 5338 s_goto (OP_RDSEXPR);
5022 5339
5023 case TOK_COMMA: 5340 case TOK_COMMA:
5024 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); 5341 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL);
5025 SCHEME_V->tok = token (SCHEME_A); 5342 SCHEME_V->tok = token (SCHEME_A);
5036 case TOK_DOTATOM: 5353 case TOK_DOTATOM:
5037 SCHEME_V->strbuff[0] = '.'; 5354 SCHEME_V->strbuff[0] = '.';
5038 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); 5355 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5039 5356
5040 case TOK_STRATOM: 5357 case TOK_STRATOM:
5358 //TODO: haven't checked whether the garbage collector could interfere and free x
5359 gc (SCHEME_A_ NIL, NIL); //TODO: superheavyhanded
5041 x = readstrexp (SCHEME_A_ '|'); 5360 x = readstrexp (SCHEME_A_ '|');
5042 //TODO: haven't checked whether the garbage collector could interfere
5043 s_return (mk_atom (SCHEME_A_ strvalue (x))); 5361 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5044 5362
5045 case TOK_DQUOTE: 5363 case TOK_DQUOTE:
5046 x = readstrexp (SCHEME_A_ '"'); 5364 x = readstrexp (SCHEME_A_ '"');
5047 5365
5055 { 5373 {
5056 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1); 5374 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1);
5057 5375
5058 if (f == NIL) 5376 if (f == NIL)
5059 Error_0 ("undefined sharp expression"); 5377 Error_0 ("undefined sharp expression");
5060 else 5378
5061 {
5062 SCHEME_V->code = cons (slot_value_in_env (f), NIL); 5379 SCHEME_V->code = cons (slot_value_in_env (f), NIL);
5063 s_goto (OP_EVAL); 5380 s_goto (OP_EVAL);
5064 }
5065 } 5381 }
5066 5382
5067 case TOK_SHARP_CONST: 5383 case TOK_SHARP_CONST:
5068 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) 5384 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5069 Error_0 ("undefined sharp expression"); 5385 Error_0 ("undefined sharp expression");
5070 else 5386
5071 s_return (x); 5387 s_return (x);
5072 5388
5073 default: 5389 default:
5074 Error_0 ("syntax error: illegal token"); 5390 Error_0 ("syntax error: illegal token");
5075 } 5391 }
5076 5392
5169 pointer b = cdr (args); 5485 pointer b = cdr (args);
5170 int ok_abbr = ok_abbrev (b); 5486 int ok_abbr = ok_abbrev (b);
5171 SCHEME_V->args = car (b); 5487 SCHEME_V->args = car (b);
5172 5488
5173 if (a == SCHEME_V->QUOTE && ok_abbr) 5489 if (a == SCHEME_V->QUOTE && ok_abbr)
5174 putstr (SCHEME_A_ "'"); 5490 putcharacter (SCHEME_A_ '\'');
5175 else if (a == SCHEME_V->QQUOTE && ok_abbr) 5491 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5176 putstr (SCHEME_A_ "`"); 5492 putcharacter (SCHEME_A_ '`');
5177 else if (a == SCHEME_V->UNQUOTE && ok_abbr) 5493 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5178 putstr (SCHEME_A_ ","); 5494 putcharacter (SCHEME_A_ ',');
5179 else if (a == SCHEME_V->UNQUOTESP && ok_abbr) 5495 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5180 putstr (SCHEME_A_ ",@"); 5496 putstr (SCHEME_A_ ",@");
5181 else 5497 else
5182 { 5498 {
5183 putstr (SCHEME_A_ "("); 5499 putcharacter (SCHEME_A_ '(');
5184 s_save (SCHEME_A_ OP_P1LIST, b, NIL); 5500 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5185 SCHEME_V->args = a; 5501 SCHEME_V->args = a;
5186 } 5502 }
5187 5503
5188 s_goto (OP_P0LIST); 5504 s_goto (OP_P0LIST);
5190 5506
5191 case OP_P1LIST: 5507 case OP_P1LIST:
5192 if (is_pair (args)) 5508 if (is_pair (args))
5193 { 5509 {
5194 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); 5510 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5195 putstr (SCHEME_A_ " "); 5511 putcharacter (SCHEME_A_ ' ');
5196 SCHEME_V->args = car (args); 5512 SCHEME_V->args = car (args);
5197 s_goto (OP_P0LIST); 5513 s_goto (OP_P0LIST);
5198 } 5514 }
5199 else if (is_vector (args)) 5515 else if (is_vector (args))
5200 { 5516 {
5208 { 5524 {
5209 putstr (SCHEME_A_ " . "); 5525 putstr (SCHEME_A_ " . ");
5210 printatom (SCHEME_A_ args, SCHEME_V->print_flag); 5526 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5211 } 5527 }
5212 5528
5213 putstr (SCHEME_A_ ")"); 5529 putcharacter (SCHEME_A_ ')');
5214 s_return (S_T); 5530 s_return (S_T);
5215 } 5531 }
5216 5532
5217 case OP_PVECFROM: 5533 case OP_PVECFROM:
5218 { 5534 {
5220 pointer vec = car (args); 5536 pointer vec = car (args);
5221 int len = veclength (vec); 5537 int len = veclength (vec);
5222 5538
5223 if (i == len) 5539 if (i == len)
5224 { 5540 {
5225 putstr (SCHEME_A_ ")"); 5541 putcharacter (SCHEME_A_ ')');
5226 s_return (S_T); 5542 s_return (S_T);
5227 } 5543 }
5228 else 5544 else
5229 { 5545 {
5230 pointer elem = vector_get (vec, i); 5546 pointer elem = vector_get (vec, i);
5232 ivalue_unchecked (cdr (args)) = i + 1; 5548 ivalue_unchecked (cdr (args)) = i + 1;
5233 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5549 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5234 SCHEME_V->args = elem; 5550 SCHEME_V->args = elem;
5235 5551
5236 if (i > 0) 5552 if (i > 0)
5237 putstr (SCHEME_A_ " "); 5553 putcharacter (SCHEME_A_ ' ');
5238 5554
5239 s_goto (OP_P0LIST); 5555 s_goto (OP_P0LIST);
5240 } 5556 }
5241 } 5557 }
5242 } 5558 }
5243 5559
5244 if (USE_ERROR_CHECKING) abort (); 5560 if (USE_ERROR_CHECKING) abort ();
5245} 5561}
5246 5562
5247static int 5563/* list ops */
5564ecb_hot static int
5248opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5565opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5249{ 5566{
5250 pointer args = SCHEME_V->args; 5567 pointer args = SCHEME_V->args;
5251 pointer a = car (args); 5568 pointer a = car (args);
5252 pointer x, y; 5569 pointer x, y;
5275 break; 5592 break;
5276 } 5593 }
5277 5594
5278 if (is_pair (y)) 5595 if (is_pair (y))
5279 s_return (car (y)); 5596 s_return (car (y));
5280 else 5597
5281 s_return (S_F); 5598 s_return (S_F);
5282
5283 5599
5284 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5600 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5285 SCHEME_V->args = a; 5601 SCHEME_V->args = a;
5286 5602
5287 if (SCHEME_V->args == NIL) 5603 if (SCHEME_V->args == NIL)
5288 s_return (S_F); 5604 s_return (S_F);
5289 else if (is_closure (SCHEME_V->args)) 5605 else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args))
5290 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5606 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5291 else if (is_macro (SCHEME_V->args)) 5607
5292 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5293 else
5294 s_return (S_F); 5608 s_return (S_F);
5295 5609
5296 case OP_CLOSUREP: /* closure? */ 5610 case OP_CLOSUREP: /* closure? */
5297 /* 5611 /*
5298 * Note, macro object is also a closure. 5612 * Note, macro object is also a closure.
5299 * Therefore, (closure? <#MACRO>) ==> #t 5613 * Therefore, (closure? <#MACRO>) ==> #t
5310 5624
5311/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5625/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5312typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5626typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5313 5627
5314typedef int (*test_predicate)(pointer); 5628typedef int (*test_predicate)(pointer);
5315static int 5629
5630ecb_hot static int
5316tst_any (pointer p) 5631tst_any (pointer p)
5317{ 5632{
5318 return 1; 5633 return 1;
5319} 5634}
5320 5635
5321static int 5636ecb_hot static int
5322tst_inonneg (pointer p) 5637tst_inonneg (pointer p)
5323{ 5638{
5324 return is_integer (p) && ivalue_unchecked (p) >= 0; 5639 return is_integer (p) && ivalue_unchecked (p) >= 0;
5325} 5640}
5326 5641
5327static int 5642ecb_hot static int
5328tst_is_list (SCHEME_P_ pointer p) 5643tst_is_list (SCHEME_P_ pointer p)
5329{ 5644{
5330 return p == NIL || is_pair (p); 5645 return p == NIL || is_pair (p);
5331} 5646}
5332 5647
5375#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" 5690#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5376#include "opdefines.h" 5691#include "opdefines.h"
5377#undef OP_DEF 5692#undef OP_DEF
5378; 5693;
5379 5694
5380static const char * 5695ecb_cold static const char *
5381opname (int idx) 5696opname (int idx)
5382{ 5697{
5383 const char *name = opnames; 5698 const char *name = opnames;
5384 5699
5385 /* should do this at compile time, but would require external program, right? */ 5700 /* should do this at compile time, but would require external program, right? */
5387 name += strlen (name) + 1; 5702 name += strlen (name) + 1;
5388 5703
5389 return *name ? name : "ILLEGAL"; 5704 return *name ? name : "ILLEGAL";
5390} 5705}
5391 5706
5392static const char * 5707ecb_cold static const char *
5393procname (pointer x) 5708procname (pointer x)
5394{ 5709{
5395 return opname (procnum (x)); 5710 return opname (procnum (x));
5396} 5711}
5397 5712
5417#undef OP_DEF 5732#undef OP_DEF
5418 {0} 5733 {0}
5419}; 5734};
5420 5735
5421/* kernel of this interpreter */ 5736/* kernel of this interpreter */
5422static void ecb_hot 5737ecb_hot static void
5423Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5738Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5424{ 5739{
5425 SCHEME_V->op = op; 5740 SCHEME_V->op = op;
5426 5741
5427 for (;;) 5742 for (;;)
5510 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) 5825 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5511 return; 5826 return;
5512 5827
5513 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5828 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5514 { 5829 {
5515 xwrstr ("No memory!\n"); 5830 putstr (SCHEME_A_ "No memory!\n");
5516 return; 5831 return;
5517 } 5832 }
5518 } 5833 }
5519} 5834}
5520 5835
5521/* ========== Initialization of internal keywords ========== */ 5836/* ========== Initialization of internal keywords ========== */
5522 5837
5523static void 5838ecb_cold static void
5524assign_syntax (SCHEME_P_ const char *name) 5839assign_syntax (SCHEME_P_ const char *name)
5525{ 5840{
5526 pointer x = oblist_add_by_name (SCHEME_A_ name); 5841 pointer x = oblist_add_by_name (SCHEME_A_ name);
5527 set_typeflag (x, typeflag (x) | T_SYNTAX); 5842 set_typeflag (x, typeflag (x) | T_SYNTAX);
5528} 5843}
5529 5844
5530static void 5845ecb_cold static void
5531assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5846assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5532{ 5847{
5533 pointer x = mk_symbol (SCHEME_A_ name); 5848 pointer x = mk_symbol (SCHEME_A_ name);
5534 pointer y = mk_proc (SCHEME_A_ op); 5849 pointer y = mk_proc (SCHEME_A_ op);
5535 new_slot_in_env (SCHEME_A_ x, y); 5850 new_slot_in_env (SCHEME_A_ x, y);
5543 ivalue_unchecked (y) = op; 5858 ivalue_unchecked (y) = op;
5544 return y; 5859 return y;
5545} 5860}
5546 5861
5547/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5862/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5548static int 5863ecb_hot static int
5549syntaxnum (pointer p) 5864syntaxnum (pointer p)
5550{ 5865{
5551 const char *s = strvalue (p); 5866 const char *s = strvalue (p);
5552 5867
5553 switch (strlength (p)) 5868 switch (strlength (p))
5633ecb_cold int 5948ecb_cold int
5634scheme_init (SCHEME_P) 5949scheme_init (SCHEME_P)
5635{ 5950{
5636 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5951 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5637 pointer x; 5952 pointer x;
5953
5954 /* this memset is not strictly correct, as we assume (intcache)
5955 * that memset 0 will also set pointers to 0, but memset does
5956 * of course not guarantee that. screw such systems.
5957 */
5958 memset (SCHEME_V, 0, sizeof (*SCHEME_V));
5638 5959
5639 num_set_fixnum (num_zero, 1); 5960 num_set_fixnum (num_zero, 1);
5640 num_set_ivalue (num_zero, 0); 5961 num_set_ivalue (num_zero, 0);
5641 num_set_fixnum (num_one, 1); 5962 num_set_fixnum (num_one, 1);
5642 num_set_ivalue (num_one, 1); 5963 num_set_ivalue (num_one, 1);
5654 SCHEME_V->save_inport = NIL; 5975 SCHEME_V->save_inport = NIL;
5655 SCHEME_V->loadport = NIL; 5976 SCHEME_V->loadport = NIL;
5656 SCHEME_V->nesting = 0; 5977 SCHEME_V->nesting = 0;
5657 SCHEME_V->interactive_repl = 0; 5978 SCHEME_V->interactive_repl = 0;
5658 5979
5659 if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) 5980 if (!alloc_cellseg (SCHEME_A))
5660 { 5981 {
5661#if USE_ERROR_CHECKING 5982#if USE_ERROR_CHECKING
5662 SCHEME_V->no_memory = 1; 5983 SCHEME_V->no_memory = 1;
5663 return 0; 5984 return 0;
5664#endif 5985#endif
5665 } 5986 }
5666 5987
5667 SCHEME_V->gc_verbose = 0; 5988 SCHEME_V->gc_verbose = 0;
5668 dump_stack_initialize (SCHEME_A); 5989 dump_stack_initialize (SCHEME_A);
5669 SCHEME_V->code = NIL; 5990 SCHEME_V->code = NIL;
5670 SCHEME_V->args = NIL; 5991 SCHEME_V->args = NIL;
5671 SCHEME_V->envir = NIL; 5992 SCHEME_V->envir = NIL;
5993 SCHEME_V->value = NIL;
5672 SCHEME_V->tracing = 0; 5994 SCHEME_V->tracing = 0;
5673 5995
5674 /* init NIL */ 5996 /* init NIL */
5675 set_typeflag (NIL, T_ATOM | T_MARK); 5997 set_typeflag (NIL, T_ATOM | T_MARK);
5676 set_car (NIL, NIL); 5998 set_car (NIL, NIL);
5732 6054
5733 return !SCHEME_V->no_memory; 6055 return !SCHEME_V->no_memory;
5734} 6056}
5735 6057
5736#if USE_PORTS 6058#if USE_PORTS
5737void 6059ecb_cold void
5738scheme_set_input_port_file (SCHEME_P_ int fin) 6060scheme_set_input_port_file (SCHEME_P_ int fin)
5739{ 6061{
5740 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 6062 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5741} 6063}
5742 6064
5743void 6065ecb_cold void
5744scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) 6066scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end)
5745{ 6067{
5746 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); 6068 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input);
5747} 6069}
5748 6070
5749void 6071ecb_cold void
5750scheme_set_output_port_file (SCHEME_P_ int fout) 6072scheme_set_output_port_file (SCHEME_P_ int fout)
5751{ 6073{
5752 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 6074 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5753} 6075}
5754 6076
5755void 6077ecb_cold void
5756scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) 6078scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end)
5757{ 6079{
5758 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); 6080 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output);
5759} 6081}
5760#endif 6082#endif
5761 6083
5762void 6084ecb_cold void
5763scheme_set_external_data (SCHEME_P_ void *p) 6085scheme_set_external_data (SCHEME_P_ void *p)
5764{ 6086{
5765 SCHEME_V->ext_data = p; 6087 SCHEME_V->ext_data = p;
5766} 6088}
5767 6089
5799 SCHEME_V->loadport = NIL; 6121 SCHEME_V->loadport = NIL;
5800 SCHEME_V->gc_verbose = 0; 6122 SCHEME_V->gc_verbose = 0;
5801 gc (SCHEME_A_ NIL, NIL); 6123 gc (SCHEME_A_ NIL, NIL);
5802 6124
5803 for (i = 0; i <= SCHEME_V->last_cell_seg; i++) 6125 for (i = 0; i <= SCHEME_V->last_cell_seg; i++)
5804 free (SCHEME_V->alloc_seg[i]); 6126 free (SCHEME_V->cell_seg[i]);
5805 6127
5806#if SHOW_ERROR_LINE 6128#if SHOW_ERROR_LINE
5807 for (i = 0; i <= SCHEME_V->file_i; i++) 6129 for (i = 0; i <= SCHEME_V->file_i; i++)
5808 {
5809 if (SCHEME_V->load_stack[i].kind & port_file) 6130 if (SCHEME_V->load_stack[i].kind & port_file)
5810 { 6131 {
5811 fname = SCHEME_V->load_stack[i].rep.stdio.filename; 6132 fname = SCHEME_V->load_stack[i].rep.stdio.filename;
5812 6133
5813 if (fname) 6134 if (fname)
5814 free (fname); 6135 free (fname);
5815 } 6136 }
5816 }
5817#endif 6137#endif
5818} 6138}
5819 6139
5820void 6140ecb_cold void
5821scheme_load_file (SCHEME_P_ int fin) 6141scheme_load_file (SCHEME_P_ int fin)
5822{ 6142{
5823 scheme_load_named_file (SCHEME_A_ fin, 0); 6143 scheme_load_named_file (SCHEME_A_ fin, 0);
5824} 6144}
5825 6145
5826void 6146ecb_cold void
5827scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 6147scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5828{ 6148{
5829 dump_stack_reset (SCHEME_A); 6149 dump_stack_reset (SCHEME_A);
5830 SCHEME_V->envir = SCHEME_V->global_env; 6150 SCHEME_V->envir = SCHEME_V->global_env;
5831 SCHEME_V->file_i = 0; 6151 SCHEME_V->file_i = 0;
5832 SCHEME_V->load_stack[0].unget = -1; 6152 SCHEME_V->load_stack[0].unget = -1;
5833 SCHEME_V->load_stack[0].kind = port_input | port_file; 6153 SCHEME_V->load_stack[0].kind = port_input | port_file;
5834 SCHEME_V->load_stack[0].rep.stdio.file = fin; 6154 SCHEME_V->load_stack[0].rep.stdio.file = fin;
5835#if USE_PORTS
5836 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6155 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5837#endif
5838 SCHEME_V->retcode = 0; 6156 SCHEME_V->retcode = 0;
5839 6157
5840#if USE_PORTS
5841 if (fin == STDIN_FILENO) 6158 if (fin == STDIN_FILENO)
5842 SCHEME_V->interactive_repl = 1; 6159 SCHEME_V->interactive_repl = 1;
5843#endif
5844 6160
5845#if USE_PORTS 6161#if USE_PORTS
5846#if SHOW_ERROR_LINE 6162#if SHOW_ERROR_LINE
5847 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; 6163 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0;
5848 6164
5852#endif 6168#endif
5853 6169
5854 SCHEME_V->inport = SCHEME_V->loadport; 6170 SCHEME_V->inport = SCHEME_V->loadport;
5855 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6171 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5856 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6172 Eval_Cycle (SCHEME_A_ OP_T0LVL);
6173
5857 set_typeflag (SCHEME_V->loadport, T_ATOM); 6174 set_typeflag (SCHEME_V->loadport, T_ATOM);
5858 6175
5859 if (SCHEME_V->retcode == 0) 6176 if (SCHEME_V->retcode == 0)
5860 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6177 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5861} 6178}
5862 6179
5863void 6180ecb_cold void
5864scheme_load_string (SCHEME_P_ const char *cmd) 6181scheme_load_string (SCHEME_P_ const char *cmd)
5865{ 6182{
6183#if USE_PORTs
5866 dump_stack_reset (SCHEME_A); 6184 dump_stack_reset (SCHEME_A);
5867 SCHEME_V->envir = SCHEME_V->global_env; 6185 SCHEME_V->envir = SCHEME_V->global_env;
5868 SCHEME_V->file_i = 0; 6186 SCHEME_V->file_i = 0;
5869 SCHEME_V->load_stack[0].kind = port_input | port_string; 6187 SCHEME_V->load_stack[0].kind = port_input | port_string;
5870 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ 6188 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */
5871 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); 6189 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd);
5872 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; 6190 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5873#if USE_PORTS
5874 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6191 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5875#endif
5876 SCHEME_V->retcode = 0; 6192 SCHEME_V->retcode = 0;
5877 SCHEME_V->interactive_repl = 0; 6193 SCHEME_V->interactive_repl = 0;
5878 SCHEME_V->inport = SCHEME_V->loadport; 6194 SCHEME_V->inport = SCHEME_V->loadport;
5879 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6195 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5880 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6196 Eval_Cycle (SCHEME_A_ OP_T0LVL);
5881 set_typeflag (SCHEME_V->loadport, T_ATOM); 6197 set_typeflag (SCHEME_V->loadport, T_ATOM);
5882 6198
5883 if (SCHEME_V->retcode == 0) 6199 if (SCHEME_V->retcode == 0)
5884 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6200 SCHEME_V->retcode = SCHEME_V->nesting != 0;
6201#else
6202 abort ();
6203#endif
5885} 6204}
5886 6205
5887void 6206ecb_cold void
5888scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 6207scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5889{ 6208{
5890 pointer x; 6209 pointer x;
5891 6210
5892 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6211 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5897 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6216 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5898} 6217}
5899 6218
5900#if !STANDALONE 6219#if !STANDALONE
5901 6220
5902void 6221ecb_cold void
5903scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6222scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5904{ 6223{
5905 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); 6224 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f));
5906} 6225}
5907 6226
5908void 6227ecb_cold void
5909scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6228scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5910{ 6229{
5911 int i; 6230 int i;
5912 6231
5913 for (i = 0; i < count; i++) 6232 for (i = 0; i < count; i++)
5914 scheme_register_foreign_func (SCHEME_A_ list + i); 6233 scheme_register_foreign_func (SCHEME_A_ list + i);
5915} 6234}
5916 6235
5917pointer 6236ecb_cold pointer
5918scheme_apply0 (SCHEME_P_ const char *procname) 6237scheme_apply0 (SCHEME_P_ const char *procname)
5919{ 6238{
5920 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); 6239 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL));
5921} 6240}
5922 6241
5923void 6242ecb_cold void
5924save_from_C_call (SCHEME_P) 6243save_from_C_call (SCHEME_P)
5925{ 6244{
5926 pointer saved_data = cons (car (S_SINK), 6245 pointer saved_data = cons (car (S_SINK),
5927 cons (SCHEME_V->envir, 6246 cons (SCHEME_V->envir,
5928 SCHEME_V->dump)); 6247 SCHEME_V->dump));
5932 /* Truncate the dump stack so TS will return here when done, not 6251 /* Truncate the dump stack so TS will return here when done, not
5933 directly resume pre-C-call operations. */ 6252 directly resume pre-C-call operations. */
5934 dump_stack_reset (SCHEME_A); 6253 dump_stack_reset (SCHEME_A);
5935} 6254}
5936 6255
5937void 6256ecb_cold void
5938restore_from_C_call (SCHEME_P) 6257restore_from_C_call (SCHEME_P)
5939{ 6258{
5940 set_car (S_SINK, caar (SCHEME_V->c_nest)); 6259 set_car (S_SINK, caar (SCHEME_V->c_nest));
5941 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 6260 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
5942 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 6261 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
5943 /* Pop */ 6262 /* Pop */
5944 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 6263 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
5945} 6264}
5946 6265
5947/* "func" and "args" are assumed to be already eval'ed. */ 6266/* "func" and "args" are assumed to be already eval'ed. */
5948pointer 6267ecb_cold pointer
5949scheme_call (SCHEME_P_ pointer func, pointer args) 6268scheme_call (SCHEME_P_ pointer func, pointer args)
5950{ 6269{
5951 int old_repl = SCHEME_V->interactive_repl; 6270 int old_repl = SCHEME_V->interactive_repl;
5952 6271
5953 SCHEME_V->interactive_repl = 0; 6272 SCHEME_V->interactive_repl = 0;
5960 SCHEME_V->interactive_repl = old_repl; 6279 SCHEME_V->interactive_repl = old_repl;
5961 restore_from_C_call (SCHEME_A); 6280 restore_from_C_call (SCHEME_A);
5962 return SCHEME_V->value; 6281 return SCHEME_V->value;
5963} 6282}
5964 6283
5965pointer 6284ecb_cold pointer
5966scheme_eval (SCHEME_P_ pointer obj) 6285scheme_eval (SCHEME_P_ pointer obj)
5967{ 6286{
5968 int old_repl = SCHEME_V->interactive_repl; 6287 int old_repl = SCHEME_V->interactive_repl;
5969 6288
5970 SCHEME_V->interactive_repl = 0; 6289 SCHEME_V->interactive_repl = 0;
5982 6301
5983/* ========== Main ========== */ 6302/* ========== Main ========== */
5984 6303
5985#if STANDALONE 6304#if STANDALONE
5986 6305
5987int 6306ecb_cold int
5988main (int argc, char **argv) 6307main (int argc, char **argv)
5989{ 6308{
5990# if USE_MULTIPLICITY 6309# if USE_MULTIPLICITY
5991 scheme ssc; 6310 scheme ssc;
5992 scheme *const SCHEME_V = &ssc; 6311 scheme *const SCHEME_V = &ssc;
5994# endif 6313# endif
5995 int fin; 6314 int fin;
5996 char *file_name = InitFile; 6315 char *file_name = InitFile;
5997 int retcode; 6316 int retcode;
5998 int isfile = 1; 6317 int isfile = 1;
6318#if EXPERIMENT
5999 system ("ps v $PPID");//D 6319 system ("ps v $PPID");
6320#endif
6000 6321
6001 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6322 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6002 { 6323 {
6003 xwrstr ("Usage: tinyscheme -?\n"); 6324 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
6004 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6325 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
6005 xwrstr ("followed by\n"); 6326 putstr (SCHEME_A_ "followed by\n");
6006 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6327 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
6007 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6328 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
6008 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6329 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
6009 xwrstr ("Use - as filename for stdin.\n"); 6330 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
6010 return 1; 6331 return 1;
6011 } 6332 }
6012 6333
6013 if (!scheme_init (SCHEME_A)) 6334 if (!scheme_init (SCHEME_A))
6014 { 6335 {
6015 xwrstr ("Could not initialize!\n"); 6336 putstr (SCHEME_A_ "Could not initialize!\n");
6016 return 2; 6337 return 2;
6017 } 6338 }
6018 6339
6019# if USE_PORTS 6340# if USE_PORTS
6020 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6341 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
6033 } 6354 }
6034#endif 6355#endif
6035 6356
6036 do 6357 do
6037 { 6358 {
6038#if USE_PORTS
6039 if (strcmp (file_name, "-") == 0) 6359 if (strcmp (file_name, "-") == 0)
6040 fin = STDIN_FILENO; 6360 fin = STDIN_FILENO;
6041 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) 6361 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0)
6042 { 6362 {
6043 pointer args = NIL; 6363 pointer args = NIL;
6061 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); 6381 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args);
6062 6382
6063 } 6383 }
6064 else 6384 else
6065 fin = open (file_name, O_RDONLY); 6385 fin = open (file_name, O_RDONLY);
6066#endif
6067 6386
6068 if (isfile && fin < 0) 6387 if (isfile && fin < 0)
6069 { 6388 {
6070 xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); 6389 putstr (SCHEME_A_ "Could not open file ");
6390 putstr (SCHEME_A_ file_name);
6391 putcharacter (SCHEME_A_ '\n');
6071 } 6392 }
6072 else 6393 else
6073 { 6394 {
6074 if (isfile) 6395 if (isfile)
6075 scheme_load_named_file (SCHEME_A_ fin, file_name); 6396 scheme_load_named_file (SCHEME_A_ fin, file_name);
6076 else 6397 else
6077 scheme_load_string (SCHEME_A_ file_name); 6398 scheme_load_string (SCHEME_A_ file_name);
6078 6399
6079#if USE_PORTS
6080 if (!isfile || fin != STDIN_FILENO) 6400 if (!isfile || fin != STDIN_FILENO)
6081 { 6401 {
6082 if (SCHEME_V->retcode != 0) 6402 if (SCHEME_V->retcode != 0)
6083 { 6403 {
6084 xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); 6404 putstr (SCHEME_A_ "Errors encountered reading ");
6405 putstr (SCHEME_A_ file_name);
6406 putcharacter (SCHEME_A_ '\n');
6085 } 6407 }
6086 6408
6087 if (isfile) 6409 if (isfile)
6088 close (fin); 6410 close (fin);
6089 } 6411 }
6090#endif
6091 } 6412 }
6092 6413
6093 file_name = *argv++; 6414 file_name = *argv++;
6094 } 6415 }
6095 while (file_name != 0); 6416 while (file_name != 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines