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.28 by root, Sat Nov 28 08:09:04 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 PAGE_SIZE 4096 /* does not work on sparc/alpha */ 21#define _POSIX_C_SOURCE 200201
22#include "malloc.c" 22#define _XOPEN_SOURCE 600
23#define _GNU_SOURCE 1 /* for malloc mremap */
23 24
24#define SCHEME_SOURCE 25#define SCHEME_SOURCE
25#include "scheme-private.h" 26#include "scheme-private.h"
26#ifndef WIN32 27#ifndef WIN32
27# include <unistd.h> 28# include <unistd.h>
28#endif 29#endif
29#if USE_MATH 30#if USE_MATH
30# include <math.h> 31# include <math.h>
31#endif 32#endif
32 33
34#define ECB_NO_THREADS 1
33#include "ecb.h" 35#include "ecb.h"
34 36
35#include <sys/types.h> 37#include <sys/types.h>
36#include <sys/stat.h> 38#include <sys/stat.h>
37#include <fcntl.h> 39#include <fcntl.h>
38 40
41#if !USE_ERROR_CHECKING
42# define NDEBUG
43#endif
44
45#include <assert.h>
46#include <stdlib.h>
39#include <string.h> 47#include <string.h>
40#include <stdlib.h>
41 48
42#include <limits.h> 49#include <limits.h>
43#include <inttypes.h> 50#include <inttypes.h>
44#include <float.h> 51#include <float.h>
45//#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
60
61#if '1' != '0' + 1 \
62 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \
63 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \
64 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \
65 || 'f' != 'a' + 5
66# error "execution character set digits not consecutive"
67#endif
46 68
47enum { 69enum {
48 TOK_EOF, 70 TOK_EOF,
49 TOK_LPAREN, 71 TOK_LPAREN,
50 TOK_RPAREN, 72 TOK_RPAREN,
51 TOK_DOT, 73 TOK_DOT,
52 TOK_ATOM, 74 TOK_ATOM,
75 TOK_DOTATOM, /* atom name starting with '.' */
76 TOK_STRATOM, /* atom name enclosed in | */
53 TOK_QUOTE, 77 TOK_QUOTE,
54 TOK_DQUOTE, 78 TOK_DQUOTE,
55 TOK_BQUOTE, 79 TOK_BQUOTE,
56 TOK_COMMA, 80 TOK_COMMA,
57 TOK_ATMARK, 81 TOK_ATMARK,
59 TOK_SHARP_CONST, 83 TOK_SHARP_CONST,
60 TOK_VEC 84 TOK_VEC
61}; 85};
62 86
63#define BACKQUOTE '`' 87#define BACKQUOTE '`'
64#define DELIMITERS "()\";\f\t\v\n\r " 88#define WHITESPACE " \t\r\n\v\f"
89#define DELIMITERS "()\";" WHITESPACE
65 90
66#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 91#define NIL POINTER (&SCHEME_V->xNIL)
67#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 92#define S_T POINTER (&SCHEME_V->xT)
68#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 93#define S_F POINTER (&SCHEME_V->xF)
69#define S_SINK (&SCHEME_V->xsink) 94#define S_SINK POINTER (&SCHEME_V->xsink)
70#define S_EOF (&SCHEME_V->xEOF_OBJ) 95#define S_EOF POINTER (&SCHEME_V->xEOF_OBJ)
71 96
72#if !USE_MULTIPLICITY 97#if !USE_MULTIPLICITY
73static scheme sc; 98static scheme sc;
74#endif 99#endif
75 100
76static void 101ecb_cold static void
77xbase (char *s, long n, int base) 102xbase (char *s, long n, int base)
78{ 103{
79 if (n < 0) 104 if (n < 0)
80 { 105 {
81 *s++ = '-'; 106 *s++ = '-';
83 } 108 }
84 109
85 char *p = s; 110 char *p = s;
86 111
87 do { 112 do {
88 *p++ = '0' + n % base; 113 *p++ = "0123456789abcdef"[n % base];
89 n /= base; 114 n /= base;
90 } while (n); 115 } while (n);
91 116
92 *p-- = 0; 117 *p-- = 0;
93 118
96 char x = *s; *s = *p; *p = x; 121 char x = *s; *s = *p; *p = x;
97 --p; ++s; 122 --p; ++s;
98 } 123 }
99} 124}
100 125
101static void 126ecb_cold static void
102xnum (char *s, long n) 127xnum (char *s, long n)
103{ 128{
104 xbase (s, n, 10); 129 xbase (s, n, 10);
105} 130}
106 131
107static void 132ecb_cold static void
108xwrstr (const char *s) 133putnum (SCHEME_P_ long n)
109{
110 write (1, s, strlen (s));
111}
112
113static void
114xwrnum (long n)
115{ 134{
116 char buf[64]; 135 char buf[64];
117 136
118 xnum (buf, n); 137 xnum (buf, n);
119 xwrstr (buf); 138 putstr (SCHEME_A_ buf);
120} 139}
140
141#if USE_CHAR_CLASSIFIERS
142#include <ctype.h>
143#else
121 144
122static char 145static char
123xtoupper (char c) 146xtoupper (char c)
124{ 147{
125 if (c >= 'a' && c <= 'z') 148 if (c >= 'a' && c <= 'z')
145 168
146#define toupper(c) xtoupper (c) 169#define toupper(c) xtoupper (c)
147#define tolower(c) xtolower (c) 170#define tolower(c) xtolower (c)
148#define isdigit(c) xisdigit (c) 171#define isdigit(c) xisdigit (c)
149 172
173#endif
174
150#if USE_IGNORECASE 175#if USE_IGNORECASE
151static const char * 176ecb_cold static const char *
152xstrlwr (char *s) 177xstrlwr (char *s)
153{ 178{
154 const char *p = s; 179 const char *p = s;
155 180
156 while (*s) 181 while (*s)
169# define stricmp(a,b) strcmp (a, b) 194# define stricmp(a,b) strcmp (a, b)
170# define strlwr(s) (s) 195# define strlwr(s) (s)
171#endif 196#endif
172 197
173#ifndef prompt 198#ifndef prompt
174# define prompt "ts> " 199# define prompt "ms> "
175#endif 200#endif
176 201
177#ifndef InitFile 202#ifndef InitFile
178# define InitFile "init.scm" 203# define InitFile "init.scm"
179#endif 204#endif
180 205
181#ifndef FIRST_CELLSEGS
182# define FIRST_CELLSEGS 3
183#endif
184
185enum scheme_types 206enum scheme_types
186{ 207{
187 T_INTEGER, 208 T_INTEGER,
209 T_CHARACTER,
188 T_REAL, 210 T_REAL,
189 T_STRING, 211 T_STRING,
190 T_SYMBOL, 212 T_SYMBOL,
191 T_PROC, 213 T_PROC,
192 T_PAIR, /* also used for free cells */ 214 T_PAIR, /* also used for free cells */
193 T_CLOSURE, 215 T_CLOSURE,
216 T_BYTECODE, // temp
217 T_MACRO,
194 T_CONTINUATION, 218 T_CONTINUATION,
195 T_FOREIGN, 219 T_FOREIGN,
196 T_CHARACTER,
197 T_PORT, 220 T_PORT,
198 T_VECTOR, 221 T_VECTOR,
199 T_MACRO,
200 T_PROMISE, 222 T_PROMISE,
201 T_ENVIRONMENT, 223 T_ENVIRONMENT,
202 /* one more... */ 224
203 T_NUM_SYSTEM_TYPES 225 T_NUM_SYSTEM_TYPES
204}; 226};
205 227
206#define T_MASKTYPE 0x000f 228#define T_MASKTYPE 0x000f
207#define T_SYNTAX 0x0010 229#define T_SYNTAX 0x0010
240static num num_op (enum num_op op, num a, num b); 262static num num_op (enum num_op op, num a, num b);
241static num num_intdiv (num a, num b); 263static num num_intdiv (num a, num b);
242static num num_rem (num a, num b); 264static num num_rem (num a, num b);
243static num num_mod (num a, num b); 265static num num_mod (num a, num b);
244 266
245#if USE_MATH
246static double round_per_R5RS (double x);
247#endif
248static int is_zero_rvalue (RVALUE x); 267static int is_zero_rvalue (RVALUE x);
249 268
250static num num_zero; 269static num num_zero;
251static num num_one; 270static num num_one;
252 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
253/* macros for cell operations */ 276/* macros for cell operations */
254#define typeflag(p) ((p)->flag + 0) 277#define typeflag(p) (CELL(p)->flag + 0)
255#define set_typeflag(p,v) ((p)->flag = (v)) 278#define set_typeflag(p,v) (CELL(p)->flag = (v))
256#define type(p) (typeflag (p) & T_MASKTYPE) 279#define type(p) (typeflag (p) & T_MASKTYPE)
257 280
258INTERFACE int 281INTERFACE int
259is_string (pointer p) 282is_string (pointer p)
260{ 283{
261 return type (p) == T_STRING; 284 return type (p) == T_STRING;
262} 285}
263 286
264#define strvalue(p) ((p)->object.string.svalue) 287#define strvalue(p) (CELL(p)->object.string.svalue)
265#define strlength(p) ((p)->object.string.length) 288#define strlength(p) (CELL(p)->object.string.length)
266 289
267INTERFACE int 290INTERFACE int
268is_vector (pointer p) 291is_vector (pointer p)
269{ 292{
270 return type (p) == T_VECTOR; 293 return type (p) == T_VECTOR;
271} 294}
272 295
273#define vecvalue(p) ((p)->object.vector.vvalue) 296#define vecvalue(p) (CELL(p)->object.vector.vvalue)
274#define veclength(p) ((p)->object.vector.length) 297#define veclength(p) (CELL(p)->object.vector.length)
275INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); 298INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
276INTERFACE pointer vector_get (pointer vec, uint32_t ielem); 299INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
277INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); 300INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
278 301
279INTERFACE int 302INTERFACE int
305string_value (pointer p) 328string_value (pointer p)
306{ 329{
307 return strvalue (p); 330 return strvalue (p);
308} 331}
309 332
310#define ivalue_unchecked(p) (p)->object.ivalue 333#define ivalue_unchecked(p) CELL(p)->object.ivalue
311#define set_ivalue(p,v) (p)->object.ivalue = (v) 334#define set_ivalue(p,v) CELL(p)->object.ivalue = (v)
312 335
313#if USE_REAL 336#if USE_REAL
314#define rvalue_unchecked(p) (p)->object.rvalue 337#define rvalue_unchecked(p) CELL(p)->object.rvalue
315#define set_rvalue(p,v) (p)->object.rvalue = (v) 338#define set_rvalue(p,v) CELL(p)->object.rvalue = (v)
316#else 339#else
317#define rvalue_unchecked(p) (p)->object.ivalue 340#define rvalue_unchecked(p) CELL(p)->object.ivalue
318#define set_rvalue(p,v) (p)->object.ivalue = (v) 341#define set_rvalue(p,v) CELL(p)->object.ivalue = (v)
319#endif 342#endif
320 343
321INTERFACE long 344INTERFACE long
322charvalue (pointer p) 345charvalue (pointer p)
323{ 346{
324 return ivalue_unchecked (p); 347 return ivalue_unchecked (p);
325} 348}
326 349
350#define port(p) CELL(p)->object.port
351#define set_port(p,v) port(p) = (v)
327INTERFACE int 352INTERFACE int
328is_port (pointer p) 353is_port (pointer p)
329{ 354{
330 return type (p) == T_PORT; 355 return type (p) == T_PORT;
331} 356}
332 357
333INTERFACE int 358INTERFACE int
334is_inport (pointer p) 359is_inport (pointer p)
335{ 360{
336 return is_port (p) && p->object.port->kind & port_input; 361 return is_port (p) && port (p)->kind & port_input;
337} 362}
338 363
339INTERFACE int 364INTERFACE int
340is_outport (pointer p) 365is_outport (pointer p)
341{ 366{
342 return is_port (p) && p->object.port->kind & port_output; 367 return is_port (p) && port (p)->kind & port_output;
343} 368}
344 369
345INTERFACE int 370INTERFACE int
346is_pair (pointer p) 371is_pair (pointer p)
347{ 372{
348 return type (p) == T_PAIR; 373 return type (p) == T_PAIR;
349} 374}
350 375
351#define car(p) ((p)->object.cons.car + 0) 376#define car(p) (POINTER (CELL(p)->object.cons.car))
352#define cdr(p) ((p)->object.cons.cdr + 0) 377#define cdr(p) (POINTER (CELL(p)->object.cons.cdr))
353 378
354static pointer caar (pointer p) { return car (car (p)); } 379static pointer caar (pointer p) { return car (car (p)); }
355static pointer cadr (pointer p) { return car (cdr (p)); } 380static pointer cadr (pointer p) { return car (cdr (p)); }
356static pointer cdar (pointer p) { return cdr (car (p)); } 381static pointer cdar (pointer p) { return cdr (car (p)); }
357static pointer cddr (pointer p) { return cdr (cdr (p)); } 382static pointer cddr (pointer p) { return cdr (cdr (p)); }
358 383
359static pointer cadar (pointer p) { return car (cdr (car (p))); } 384static pointer cadar (pointer p) { return car (cdr (car (p))); }
360static pointer caddr (pointer p) { return car (cdr (cdr (p))); } 385static pointer caddr (pointer p) { return car (cdr (cdr (p))); }
361static pointer cdaar (pointer p) { return cdr (car (car (p))); } 386static pointer cdaar (pointer p) { return cdr (car (car (p))); }
362 387
388static pointer cadddr (pointer p) { return car (car (car (cdr (p)))); }
389
363INTERFACE void 390INTERFACE void
364set_car (pointer p, pointer q) 391set_car (pointer p, pointer q)
365{ 392{
366 p->object.cons.car = q; 393 CELL(p)->object.cons.car = CELL (q);
367} 394}
368 395
369INTERFACE void 396INTERFACE void
370set_cdr (pointer p, pointer q) 397set_cdr (pointer p, pointer q)
371{ 398{
372 p->object.cons.cdr = q; 399 CELL(p)->object.cons.cdr = CELL (q);
373} 400}
374 401
375INTERFACE pointer 402INTERFACE pointer
376pair_car (pointer p) 403pair_car (pointer p)
377{ 404{
391} 418}
392 419
393INTERFACE char * 420INTERFACE char *
394symname (pointer p) 421symname (pointer p)
395{ 422{
396 return strvalue (car (p)); 423 return strvalue (p);
397} 424}
398 425
399#if USE_PLIST 426#if USE_PLIST
427#error plists are broken because symbols are no longer pairs
428#define symprop(p) cdr(p)
400SCHEME_EXPORT int 429SCHEME_EXPORT int
401hasprop (pointer p) 430hasprop (pointer p)
402{ 431{
403 return typeflag (p) & T_SYMBOL; 432 return typeflag (p) & T_SYMBOL;
404} 433}
405
406# define symprop(p) cdr(p)
407#endif 434#endif
408 435
409INTERFACE int 436INTERFACE int
410is_syntax (pointer p) 437is_syntax (pointer p)
411{ 438{
425} 452}
426 453
427INTERFACE char * 454INTERFACE char *
428syntaxname (pointer p) 455syntaxname (pointer p)
429{ 456{
430 return strvalue (car (p)); 457 return strvalue (p);
431} 458}
432 459
433#define procnum(p) ivalue_unchecked (p) 460#define procnum(p) ivalue_unchecked (p)
434static const char *procname (pointer x); 461static const char *procname (pointer x);
435 462
507 proper list: length 534 proper list: length
508 circular list: -1 535 circular list: -1
509 not even a pair: -2 536 not even a pair: -2
510 dotted list: -2 minus length before dot 537 dotted list: -2 minus length before dot
511*/ 538*/
512INTERFACE int 539ecb_hot INTERFACE int
513list_length (SCHEME_P_ pointer a) 540list_length (SCHEME_P_ pointer a)
514{ 541{
515 int i = 0; 542 int i = 0;
516 pointer slow, fast; 543 pointer slow, fast;
517 544
556{ 583{
557 return list_length (SCHEME_A_ a) >= 0; 584 return list_length (SCHEME_A_ a) >= 0;
558} 585}
559 586
560#if USE_CHAR_CLASSIFIERS 587#if USE_CHAR_CLASSIFIERS
588
561ecb_inline int 589ecb_inline int
562Cisalpha (int c) 590Cisalpha (int c)
563{ 591{
564 return isascii (c) && isalpha (c); 592 return isascii (c) && isalpha (c);
565} 593}
623 "gs", 651 "gs",
624 "rs", 652 "rs",
625 "us" 653 "us"
626}; 654};
627 655
628static int 656ecb_cold static int
629is_ascii_name (const char *name, int *pc) 657is_ascii_name (const char *name, int *pc)
630{ 658{
631 int i; 659 int i;
632 660
633 for (i = 0; i < 32; i++) 661 for (i = 0; i < 32; i++)
651#endif 679#endif
652 680
653static int file_push (SCHEME_P_ const char *fname); 681static int file_push (SCHEME_P_ const char *fname);
654static void file_pop (SCHEME_P); 682static void file_pop (SCHEME_P);
655static int file_interactive (SCHEME_P); 683static int file_interactive (SCHEME_P);
656ecb_inline int is_one_of (char *s, int c); 684ecb_inline int is_one_of (const char *s, int c);
657static int alloc_cellseg (SCHEME_P_ int n); 685static int alloc_cellseg (SCHEME_P);
658ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 686ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
659static void finalize_cell (SCHEME_P_ pointer a); 687static void finalize_cell (SCHEME_P_ pointer a);
660static int count_consecutive_cells (pointer x, int needed);
661static 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);
662static pointer mk_number (SCHEME_P_ const num n); 689static pointer mk_number (SCHEME_P_ const num n);
663static 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);
664static pointer mk_vector (SCHEME_P_ uint32_t len); 691static pointer mk_vector (SCHEME_P_ uint32_t len);
665static pointer mk_atom (SCHEME_P_ char *q); 692static pointer mk_atom (SCHEME_P_ char *q);
666static pointer mk_sharp_const (SCHEME_P_ char *name); 693static pointer mk_sharp_const (SCHEME_P_ char *name);
667 694
695static pointer mk_port (SCHEME_P_ port *p);
696
668#if USE_PORTS 697#if USE_PORTS
669static pointer mk_port (SCHEME_P_ port *p);
670static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); 698static pointer port_from_filename (SCHEME_P_ const char *fn, int prop);
671static pointer port_from_file (SCHEME_P_ int, int prop); 699static pointer port_from_file (SCHEME_P_ int, int prop);
672static 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);
673static 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);
674static port *port_rep_from_file (SCHEME_P_ int, int prop); 702static port *port_rep_from_file (SCHEME_P_ int, int prop);
675static 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);
676static void port_close (SCHEME_P_ pointer p, int flag); 704static void port_close (SCHEME_P_ pointer p, int flag);
677#endif 705#endif
706
678static void mark (pointer a); 707static void mark (pointer a);
679static void gc (SCHEME_P_ pointer a, pointer b); 708static void gc (SCHEME_P_ pointer a, pointer b);
680static int basic_inchar (port *pt); 709static int basic_inchar (port *pt);
681static int inchar (SCHEME_P); 710static int inchar (SCHEME_P);
682static void backchar (SCHEME_P_ int c); 711static void backchar (SCHEME_P_ int c);
683static char *readstr_upto (SCHEME_P_ char *delim); 712static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
684static pointer readstrexp (SCHEME_P); 713static pointer readstrexp (SCHEME_P_ char delim);
685ecb_inline int skipspace (SCHEME_P); 714static int skipspace (SCHEME_P);
686static int token (SCHEME_P); 715static int token (SCHEME_P);
687static void printslashstring (SCHEME_P_ char *s, int len); 716static void printslashstring (SCHEME_P_ char *s, int len);
688static 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);
689static void printatom (SCHEME_P_ pointer l, int f); 718static void printatom (SCHEME_P_ pointer l, int f);
690static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 719static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
856 } 885 }
857 886
858 return ret; 887 return ret;
859} 888}
860 889
861#if USE_MATH
862
863/* Round to nearest. Round to even if midway */
864static double
865round_per_R5RS (double x)
866{
867 double fl = floor (x);
868 double ce = ceil (x);
869 double dfl = x - fl;
870 double dce = ce - x;
871
872 if (dfl > dce)
873 return ce;
874 else if (dfl < dce)
875 return fl;
876 else
877 {
878 if (fmod (fl, 2) == 0) /* I imagine this holds */
879 return fl;
880 else
881 return ce;
882 }
883}
884#endif
885
886static int 890static int
887is_zero_rvalue (RVALUE x) 891is_zero_rvalue (RVALUE x)
888{ 892{
889 return x == 0; 893 return x == 0;
890#if 0 894#if 0
895#endif 899#endif
896#endif 900#endif
897} 901}
898 902
899/* allocate new cell segment */ 903/* allocate new cell segment */
900static int 904ecb_cold static int
901alloc_cellseg (SCHEME_P_ int n) 905alloc_cellseg (SCHEME_P)
902{ 906{
903 pointer newp; 907 struct cell *newp;
904 pointer last; 908 struct cell *last;
905 pointer p; 909 struct cell *p;
906 char *cp; 910 char *cp;
907 long i; 911 long i;
908 int k; 912 int k;
909 913
910 static int segsize = CELL_SEGSIZE >> 1; 914 static int segsize = CELL_SEGSIZE >> 1;
911 segsize <<= 1; 915 segsize <<= 1;
912 916
913 for (k = 0; k < n; k++)
914 {
915 if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1)
916 return k;
917
918 cp = malloc (segsize * sizeof (struct cell)); 917 cp = malloc (segsize * sizeof (struct cell));
919 918
920 if (!cp && USE_ERROR_CHECKING) 919 if (!cp && USE_ERROR_CHECKING)
921 return k; 920 return k;
922 921
923 i = ++SCHEME_V->last_cell_seg; 922 i = ++SCHEME_V->last_cell_seg;
924 SCHEME_V->alloc_seg[i] = cp;
925 923
926 newp = (pointer)cp; 924 newp = (struct cell *)cp;
927 SCHEME_V->cell_seg[i] = newp; 925 SCHEME_V->cell_seg[i] = newp;
928 SCHEME_V->cell_segsize[i] = segsize; 926 SCHEME_V->cell_segsize[i] = segsize;
929 SCHEME_V->fcells += segsize; 927 SCHEME_V->fcells += segsize;
930 last = newp + segsize - 1; 928 last = newp + segsize - 1;
931 929
932 for (p = newp; p <= last; p++) 930 for (p = newp; p <= last; p++)
933 { 931 {
932 pointer cp = POINTER (p);
934 set_typeflag (p, T_PAIR); 933 set_typeflag (cp, T_PAIR);
935 set_car (p, NIL); 934 set_car (cp, NIL);
936 set_cdr (p, p + 1); 935 set_cdr (cp, POINTER (p + 1));
937 } 936 }
938 937
939 set_cdr (last, SCHEME_V->free_cell); 938 set_cdr (POINTER (last), SCHEME_V->free_cell);
940 SCHEME_V->free_cell = newp; 939 SCHEME_V->free_cell = POINTER (newp);
941 }
942 940
943 return n; 941 return 1;
944} 942}
945 943
946/* get new cell. parameter a, b is marked by gc. */ 944/* get new cell. parameter a, b is marked by gc. */
947ecb_inline pointer 945ecb_inline pointer
948get_cell_x (SCHEME_P_ pointer a, pointer b) 946get_cell_x (SCHEME_P_ pointer a, pointer b)
952 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 950 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
953 return S_SINK; 951 return S_SINK;
954 952
955 if (SCHEME_V->free_cell == NIL) 953 if (SCHEME_V->free_cell == NIL)
956 { 954 {
957 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;
958 956
959 gc (SCHEME_A_ a, b); 957 gc (SCHEME_A_ a, b);
960 958
961 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)
962 { 960 {
963 /* 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 */
964 if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) 962 if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL)
965 { 963 {
966#if USE_ERROR_CHECKING 964#if USE_ERROR_CHECKING
967 SCHEME_V->no_memory = 1; 965 SCHEME_V->no_memory = 1;
968 return S_SINK; 966 return S_SINK;
969#endif 967#endif
981 } 979 }
982} 980}
983 981
984/* To retain recent allocs before interpreter knows about them - 982/* To retain recent allocs before interpreter knows about them -
985 Tehom */ 983 Tehom */
986 984ecb_hot static void
987static void
988push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 985push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
989{ 986{
990 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 987 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
991 988
992 set_typeflag (holder, T_PAIR); 989 set_typeflag (holder, T_PAIR);
994 set_car (holder, recent); 991 set_car (holder, recent);
995 set_cdr (holder, car (S_SINK)); 992 set_cdr (holder, car (S_SINK));
996 set_car (S_SINK, holder); 993 set_car (S_SINK, holder);
997} 994}
998 995
999static pointer 996ecb_hot static pointer
1000get_cell (SCHEME_P_ pointer a, pointer b) 997get_cell (SCHEME_P_ pointer a, pointer b)
1001{ 998{
1002 pointer cell = get_cell_x (SCHEME_A_ a, b); 999 pointer cell = get_cell_x (SCHEME_A_ a, b);
1003 1000
1004 /* 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
1013} 1010}
1014 1011
1015static pointer 1012static pointer
1016get_vector_object (SCHEME_P_ uint32_t len, pointer init) 1013get_vector_object (SCHEME_P_ uint32_t len, pointer init)
1017{ 1014{
1018 pointer v = get_cell_x (SCHEME_A_ 0, 0); 1015 pointer v = get_cell_x (SCHEME_A_ NIL, NIL);
1019 pointer *e = malloc (len * sizeof (pointer)); 1016 pointer *e = malloc (len * sizeof (pointer));
1020 1017
1021 if (!e && USE_ERROR_CHECKING) 1018 if (!e && USE_ERROR_CHECKING)
1022 return S_SINK; 1019 return S_SINK;
1023 1020
1024 /* Record it as a vector so that gc understands it. */ 1021 /* Record it as a vector so that gc understands it. */
1025 set_typeflag (v, T_VECTOR | T_ATOM); 1022 set_typeflag (v, T_VECTOR | T_ATOM);
1026 1023
1027 v->object.vector.vvalue = e; 1024 CELL(v)->object.vector.vvalue = e;
1028 v->object.vector.length = len; 1025 CELL(v)->object.vector.length = len;
1029 fill_vector (v, 0, init); 1026 fill_vector (v, 0, init);
1030 push_recent_alloc (SCHEME_A_ v, NIL); 1027 push_recent_alloc (SCHEME_A_ v, NIL);
1031 1028
1032 return v; 1029 return v;
1033} 1030}
1042static void 1039static void
1043check_cell_alloced (pointer p, int expect_alloced) 1040check_cell_alloced (pointer p, int expect_alloced)
1044{ 1041{
1045 /* 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. */
1046 if (typeflag (p) & !expect_alloced) 1043 if (typeflag (p) & !expect_alloced)
1047 xwrstr ("Cell is already allocated!\n"); 1044 putstr (SCHEME_A_ "Cell is already allocated!\n");
1048 1045
1049 if (!(typeflag (p)) & expect_alloced) 1046 if (!(typeflag (p)) & expect_alloced)
1050 xwrstr ("Cell is not allocated!\n"); 1047 putstr (SCHEME_A_ "Cell is not allocated!\n");
1051} 1048}
1052 1049
1053static void 1050static void
1054check_range_alloced (pointer p, int n, int expect_alloced) 1051check_range_alloced (pointer p, int n, int expect_alloced)
1055{ 1052{
1061#endif 1058#endif
1062 1059
1063/* Medium level cell allocation */ 1060/* Medium level cell allocation */
1064 1061
1065/* get new cons cell */ 1062/* get new cons cell */
1066pointer 1063ecb_hot static pointer
1067xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1064xcons (SCHEME_P_ pointer a, pointer b)
1068{ 1065{
1069 pointer x = get_cell (SCHEME_A_ a, b); 1066 pointer x = get_cell (SCHEME_A_ a, b);
1070 1067
1071 set_typeflag (x, T_PAIR); 1068 set_typeflag (x, T_PAIR);
1072
1073 if (immutable)
1074 setimmutable (x);
1075 1069
1076 set_car (x, a); 1070 set_car (x, a);
1077 set_cdr (x, b); 1071 set_cdr (x, b);
1078 1072
1079 return x; 1073 return x;
1080} 1074}
1081 1075
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
1088generate_symbol (SCHEME_P_ const char *name)
1089{
1090 pointer x = mk_string (SCHEME_A_ name);
1091 setimmutable (x);
1092 set_typeflag (x, T_SYMBOL | T_ATOM);
1093 return x;
1094}
1095
1082/* ========== oblist implementation ========== */ 1096/* ========== oblist implementation ========== */
1083 1097
1084#ifndef USE_OBJECT_LIST 1098#ifndef USE_OBJECT_LIST
1085 1099
1100static int
1086static int hash_fn (const char *key, int table_size); 1101hash_fn (const char *key, int table_size)
1102{
1103 const unsigned char *p = (unsigned char *)key;
1104 uint32_t hash = 2166136261U;
1087 1105
1106 while (*p)
1107 hash = (hash ^ *p++) * 16777619;
1108
1109 return hash % table_size;
1110}
1111
1088static pointer 1112ecb_cold static pointer
1089oblist_initial_value (SCHEME_P) 1113oblist_initial_value (SCHEME_P)
1090{ 1114{
1091 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1115 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1092} 1116}
1093 1117
1094/* returns the new symbol */ 1118/* returns the new symbol */
1095static pointer 1119ecb_cold static pointer
1096oblist_add_by_name (SCHEME_P_ const char *name) 1120oblist_add_by_name (SCHEME_P_ const char *name)
1097{ 1121{
1098 int location; 1122 pointer x = generate_symbol (SCHEME_A_ name);
1099
1100 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1101 set_typeflag (x, T_SYMBOL);
1102 setimmutable (car (x));
1103
1104 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1123 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1105 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)));
1106 return x; 1125 return x;
1107} 1126}
1108 1127
1109ecb_inline pointer 1128ecb_cold static pointer
1110oblist_find_by_name (SCHEME_P_ const char *name) 1129oblist_find_by_name (SCHEME_P_ const char *name)
1111{ 1130{
1112 int location; 1131 int location;
1113 pointer x; 1132 pointer x;
1114 char *s; 1133 char *s;
1125 } 1144 }
1126 1145
1127 return NIL; 1146 return NIL;
1128} 1147}
1129 1148
1130static pointer 1149ecb_cold static pointer
1131oblist_all_symbols (SCHEME_P) 1150oblist_all_symbols (SCHEME_P)
1132{ 1151{
1133 int i; 1152 int i;
1134 pointer x; 1153 pointer x;
1135 pointer ob_list = NIL; 1154 pointer ob_list = NIL;
1141 return ob_list; 1160 return ob_list;
1142} 1161}
1143 1162
1144#else 1163#else
1145 1164
1146static pointer 1165ecb_cold static pointer
1147oblist_initial_value (SCHEME_P) 1166oblist_initial_value (SCHEME_P)
1148{ 1167{
1149 return NIL; 1168 return NIL;
1150} 1169}
1151 1170
1152ecb_inline pointer 1171ecb_cold static pointer
1153oblist_find_by_name (SCHEME_P_ const char *name) 1172oblist_find_by_name (SCHEME_P_ const char *name)
1154{ 1173{
1155 pointer x; 1174 pointer x;
1156 char *s; 1175 char *s;
1157 1176
1166 1185
1167 return NIL; 1186 return NIL;
1168} 1187}
1169 1188
1170/* returns the new symbol */ 1189/* returns the new symbol */
1171static pointer 1190ecb_cold static pointer
1172oblist_add_by_name (SCHEME_P_ const char *name) 1191oblist_add_by_name (SCHEME_P_ const char *name)
1173{ 1192{
1174 pointer x; 1193 pointer x = generate_symbol (SCHEME_A_ name);
1175
1176 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1177 set_typeflag (x, T_SYMBOL);
1178 setimmutable (car (x));
1179 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1194 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1180 return x; 1195 return x;
1181} 1196}
1182 1197
1183static pointer 1198ecb_cold static pointer
1184oblist_all_symbols (SCHEME_P) 1199oblist_all_symbols (SCHEME_P)
1185{ 1200{
1186 return SCHEME_V->oblist; 1201 return SCHEME_V->oblist;
1187} 1202}
1188 1203
1189#endif 1204#endif
1190 1205
1191#if USE_PORTS
1192static pointer 1206ecb_cold static pointer
1193mk_port (SCHEME_P_ port *p) 1207mk_port (SCHEME_P_ port *p)
1194{ 1208{
1195 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1209 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1196 1210
1197 set_typeflag (x, T_PORT | T_ATOM); 1211 set_typeflag (x, T_PORT | T_ATOM);
1198 x->object.port = p; 1212 set_port (x, p);
1199 1213
1200 return x; 1214 return x;
1201} 1215}
1202#endif
1203 1216
1204pointer 1217ecb_cold pointer
1205mk_foreign_func (SCHEME_P_ foreign_func f) 1218mk_foreign_func (SCHEME_P_ foreign_func f)
1206{ 1219{
1207 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1220 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1208 1221
1209 set_typeflag (x, (T_FOREIGN | T_ATOM)); 1222 set_typeflag (x, T_FOREIGN | T_ATOM);
1210 x->object.ff = f; 1223 CELL(x)->object.ff = f;
1211 1224
1212 return x; 1225 return x;
1213} 1226}
1214 1227
1215INTERFACE pointer 1228INTERFACE pointer
1216mk_character (SCHEME_P_ int c) 1229mk_character (SCHEME_P_ int c)
1217{ 1230{
1218 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1231 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1219 1232
1220 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1233 set_typeflag (x, T_CHARACTER | T_ATOM);
1221 set_ivalue (x, c & 0xff); 1234 set_ivalue (x, c & 0xff);
1222 1235
1223 return x; 1236 return x;
1224} 1237}
1225 1238
1226/* get number atom (integer) */ 1239/* get number atom (integer) */
1227INTERFACE pointer 1240INTERFACE pointer
1228mk_integer (SCHEME_P_ long n) 1241mk_integer (SCHEME_P_ long n)
1229{ 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 {
1230 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1253 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1231 1254
1232 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 */
1233 set_ivalue (x, n); 1257 set_ivalue (x, n);
1234 1258
1259 *pp = x;
1260 }
1261
1235 return x; 1262 return *pp;
1236} 1263}
1237 1264
1238INTERFACE pointer 1265INTERFACE pointer
1239mk_real (SCHEME_P_ RVALUE n) 1266mk_real (SCHEME_P_ RVALUE n)
1240{ 1267{
1241#if USE_REAL 1268#if USE_REAL
1242 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1269 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1243 1270
1244 set_typeflag (x, (T_REAL | T_ATOM)); 1271 set_typeflag (x, T_REAL | T_ATOM);
1245 set_rvalue (x, n); 1272 set_rvalue (x, n);
1246 1273
1247 return x; 1274 return x;
1248#else 1275#else
1249 return mk_integer (SCHEME_A_ n); 1276 return mk_integer (SCHEME_A_ n);
1273 SCHEME_V->no_memory = 1; 1300 SCHEME_V->no_memory = 1;
1274 return SCHEME_V->strbuff; 1301 return SCHEME_V->strbuff;
1275 } 1302 }
1276 1303
1277 if (str) 1304 if (str)
1278 { 1305 memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */
1279 int l = strlen (str);
1280
1281 if (l > len_str)
1282 l = len_str;
1283
1284 memcpy (q, str, l);
1285 q[l] = 0;
1286 }
1287 else 1306 else
1288 {
1289 memset (q, fill, len_str); 1307 memset (q, fill, len_str);
1308
1290 q[len_str] = 0; 1309 q[len_str] = 0;
1291 }
1292 1310
1293 return q; 1311 return q;
1294} 1312}
1295 1313
1296INTERFACE pointer 1314INTERFACE pointer
1310 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1328 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1311 1329
1312 set_typeflag (x, T_STRING | T_ATOM); 1330 set_typeflag (x, T_STRING | T_ATOM);
1313 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1331 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1314 strlength (x) = len; 1332 strlength (x) = len;
1333
1315 return x; 1334 return x;
1316} 1335}
1317 1336
1318INTERFACE pointer 1337INTERFACE pointer
1319mk_string (SCHEME_P_ const char *str) 1338mk_string (SCHEME_P_ const char *str)
1332{ 1351{
1333 int i; 1352 int i;
1334 1353
1335 for (i = start; i < veclength (vec); i++) 1354 for (i = start; i < veclength (vec); i++)
1336 vecvalue (vec)[i] = obj; 1355 vecvalue (vec)[i] = obj;
1356}
1357
1358INTERFACE void
1359vector_resize (pointer vec, uint32_t newsize, pointer fill)
1360{
1361 uint32_t oldsize = veclength (vec);
1362 vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer));
1363 veclength (vec) = newsize;
1364 fill_vector (vec, oldsize, fill);
1337} 1365}
1338 1366
1339INTERFACE pointer 1367INTERFACE pointer
1340vector_get (pointer vec, uint32_t ielem) 1368vector_get (pointer vec, uint32_t ielem)
1341{ 1369{
1359 x = oblist_add_by_name (SCHEME_A_ name); 1387 x = oblist_add_by_name (SCHEME_A_ name);
1360 1388
1361 return x; 1389 return x;
1362} 1390}
1363 1391
1364INTERFACE pointer 1392ecb_cold INTERFACE pointer
1365gensym (SCHEME_P) 1393gensym (SCHEME_P)
1366{ 1394{
1367 pointer x; 1395 pointer x;
1368
1369 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1370 {
1371 char name[40] = "gensym-"; 1396 char name[40] = "gensym-";
1372 xnum (name + 7, SCHEME_V->gensym_cnt); 1397 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1373 1398
1374 /* first check oblist */ 1399 return generate_symbol (SCHEME_A_ name);
1375 x = oblist_find_by_name (SCHEME_A_ name); 1400}
1376 1401
1377 if (x == NIL) 1402static int
1378 { 1403is_gensym (SCHEME_P_ pointer x)
1379 x = oblist_add_by_name (SCHEME_A_ name); 1404{
1380 return x; 1405 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1381 }
1382 }
1383
1384 return NIL;
1385} 1406}
1386 1407
1387/* make symbol or number atom from string */ 1408/* make symbol or number atom from string */
1388static pointer 1409ecb_cold static pointer
1389mk_atom (SCHEME_P_ char *q) 1410mk_atom (SCHEME_P_ char *q)
1390{ 1411{
1391 char c, *p; 1412 char c, *p;
1392 int has_dec_point = 0; 1413 int has_dec_point = 0;
1393 int has_fp_exp = 0; 1414 int has_fp_exp = 0;
1464 1485
1465 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1486 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1466} 1487}
1467 1488
1468/* make constant */ 1489/* make constant */
1469static pointer 1490ecb_cold static pointer
1470mk_sharp_const (SCHEME_P_ char *name) 1491mk_sharp_const (SCHEME_P_ char *name)
1471{ 1492{
1472 if (!strcmp (name, "t")) 1493 if (!strcmp (name, "t"))
1473 return S_T; 1494 return S_T;
1474 else if (!strcmp (name, "f")) 1495 else if (!strcmp (name, "f"))
1475 return S_F; 1496 return S_F;
1476 else if (*name == '\\') /* #\w (character) */ 1497 else if (*name == '\\') /* #\w (character) */
1477 { 1498 {
1478 int c; 1499 int c;
1479 1500
1501 // TODO: optimise
1480 if (stricmp (name + 1, "space") == 0) 1502 if (stricmp (name + 1, "space") == 0)
1481 c = ' '; 1503 c = ' ';
1482 else if (stricmp (name + 1, "newline") == 0) 1504 else if (stricmp (name + 1, "newline") == 0)
1483 c = '\n'; 1505 c = '\n';
1484 else if (stricmp (name + 1, "return") == 0) 1506 else if (stricmp (name + 1, "return") == 0)
1485 c = '\r'; 1507 c = '\r';
1486 else if (stricmp (name + 1, "tab") == 0) 1508 else if (stricmp (name + 1, "tab") == 0)
1487 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;
1488 else if (name[1] == 'x' && name[2] != 0) 1520 else if (name[1] == 'x' && name[2] != 0)
1489 { 1521 {
1490 long c1 = strtol (name + 2, 0, 16); 1522 long c1 = strtol (name + 2, 0, 16);
1491 1523
1492 if (0 <= c1 && c1 <= UCHAR_MAX) 1524 if (0 <= c1 && c1 <= UCHAR_MAX)
1517 return NIL; 1549 return NIL;
1518 } 1550 }
1519} 1551}
1520 1552
1521/* ========== 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}
1522 1573
1523/*-- 1574/*--
1524 * 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,
1525 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1576 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1526 * for marking. 1577 * for marking.
1527 * 1578 *
1528 * The exception is vectors - vectors are currently marked recursively, 1579 * The exception is vectors - vectors are currently marked recursively,
1529 * 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
1530 * word of context in the vector 1581 * word of context in the vector
1531 */ 1582 */
1532static void 1583ecb_hot static void
1533mark (pointer a) 1584mark (pointer a)
1534{ 1585{
1535 pointer t, q, p; 1586 pointer t, q, p;
1536 1587
1537 t = 0; 1588 t = 0;
1594 p = q; 1645 p = q;
1595 goto E6; 1646 goto E6;
1596 } 1647 }
1597} 1648}
1598 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
1599/* garbage collection. parameter a, b is marked. */ 1692/* garbage collection. parameter a, b is marked. */
1600static void 1693ecb_cold static void
1601gc (SCHEME_P_ pointer a, pointer b) 1694gc (SCHEME_P_ pointer a, pointer b)
1602{ 1695{
1603 pointer p;
1604 int i; 1696 int i;
1605 1697
1606 if (SCHEME_V->gc_verbose) 1698 if (SCHEME_V->gc_verbose)
1607 putstr (SCHEME_A_ "gc..."); 1699 putstr (SCHEME_A_ "gc...");
1608 1700
1624 /* Mark recent objects the interpreter doesn't know about yet. */ 1716 /* Mark recent objects the interpreter doesn't know about yet. */
1625 mark (car (S_SINK)); 1717 mark (car (S_SINK));
1626 /* Mark any older stuff above nested C calls */ 1718 /* Mark any older stuff above nested C calls */
1627 mark (SCHEME_V->c_nest); 1719 mark (SCHEME_V->c_nest);
1628 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
1629 /* mark variables a, b */ 1728 /* mark variables a, b */
1630 mark (a); 1729 mark (a);
1631 mark (b); 1730 mark (b);
1632 1731
1633 /* garbage collect */ 1732 /* garbage collect */
1634 clrmark (NIL); 1733 clrmark (NIL);
1635 SCHEME_V->fcells = 0; 1734 SCHEME_V->fcells = 0;
1636 SCHEME_V->free_cell = NIL; 1735 SCHEME_V->free_cell = NIL;
1637 1736
1638 /* free-list is kept sorted by address so as to maintain consecutive
1639 ranges, if possible, for use with vectors. Here we scan the cells
1640 (which are also kept sorted by address) downwards to build the
1641 free-list in sorted order.
1642 */
1643 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1644 {
1645 p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1646
1647 while (--p >= SCHEME_V->cell_seg[i])
1648 {
1649 if (is_mark (p))
1650 clrmark (p);
1651 else
1652 {
1653 /* reclaim cell */
1654 if (typeflag (p) != T_PAIR)
1655 {
1656 finalize_cell (SCHEME_A_ p);
1657 set_typeflag (p, T_PAIR);
1658 set_car (p, NIL);
1659 }
1660
1661 ++SCHEME_V->fcells;
1662 set_cdr (p, SCHEME_V->free_cell);
1663 SCHEME_V->free_cell = p;
1664 }
1665 }
1666 }
1667
1668 if (SCHEME_V->gc_verbose) 1737 if (SCHEME_V->gc_verbose)
1669 { 1738 putstr (SCHEME_A_ "freeing...");
1670 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1671 }
1672}
1673 1739
1674static void 1740 gc_free (SCHEME_A);
1675finalize_cell (SCHEME_P_ pointer a)
1676{
1677 /* TODO, fast bitmap check? */
1678 if (is_string (a))
1679 free (strvalue (a));
1680 else if (is_vector (a))
1681 free (vecvalue (a));
1682#if USE_PORTS
1683 else if (is_port (a))
1684 {
1685 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit)
1686 port_close (SCHEME_A_ a, port_input | port_output);
1687
1688 free (a->object.port);
1689 }
1690#endif
1691} 1741}
1692 1742
1693/* ========== Routines for Reading ========== */ 1743/* ========== Routines for Reading ========== */
1694 1744
1695static int 1745ecb_cold static int
1696file_push (SCHEME_P_ const char *fname) 1746file_push (SCHEME_P_ const char *fname)
1697{ 1747{
1698#if USE_PORTS
1699 int fin; 1748 int fin;
1700 1749
1701 if (SCHEME_V->file_i == MAXFIL - 1) 1750 if (SCHEME_V->file_i == MAXFIL - 1)
1702 return 0; 1751 return 0;
1703 1752
1709 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; 1758 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1;
1710 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;
1711 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;
1712 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;
1713 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; 1762 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0;
1714 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);
1715 1764
1716#if SHOW_ERROR_LINE 1765#if SHOW_ERROR_LINE
1717 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;
1718 1767
1719 if (fname) 1768 if (fname)
1720 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);
1721#endif 1770#endif
1722 } 1771 }
1723 1772
1724 return fin >= 0; 1773 return fin >= 0;
1725
1726#else
1727 return 1;
1728#endif
1729} 1774}
1730 1775
1731static void 1776ecb_cold static void
1732file_pop (SCHEME_P) 1777file_pop (SCHEME_P)
1733{ 1778{
1734 if (SCHEME_V->file_i != 0) 1779 if (SCHEME_V->file_i != 0)
1735 { 1780 {
1736 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1781 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1737#if USE_PORTS 1782#if USE_PORTS
1738 port_close (SCHEME_A_ SCHEME_V->loadport, port_input); 1783 port_close (SCHEME_A_ SCHEME_V->loadport, port_input);
1739#endif 1784#endif
1740 SCHEME_V->file_i--; 1785 SCHEME_V->file_i--;
1741 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);
1742 } 1787 }
1743} 1788}
1744 1789
1745static int 1790ecb_cold static int
1746file_interactive (SCHEME_P) 1791file_interactive (SCHEME_P)
1747{ 1792{
1748#if USE_PORTS 1793#if USE_PORTS
1749 return SCHEME_V->file_i == 0 1794 return SCHEME_V->file_i == 0
1750 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1795 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1751 && (SCHEME_V->inport->object.port->kind & port_file); 1796 && (port (SCHEME_V->inport)->kind & port_file);
1752#else 1797#else
1753 return 0; 1798 return 0;
1754#endif 1799#endif
1755} 1800}
1756 1801
1757#if USE_PORTS 1802#if USE_PORTS
1758static port * 1803ecb_cold static port *
1759port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1804port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1760{ 1805{
1761 int fd; 1806 int fd;
1762 int flags; 1807 int flags;
1763 char *rw; 1808 char *rw;
1786# endif 1831# endif
1787 1832
1788 return pt; 1833 return pt;
1789} 1834}
1790 1835
1791static pointer 1836ecb_cold static pointer
1792port_from_filename (SCHEME_P_ const char *fn, int prop) 1837port_from_filename (SCHEME_P_ const char *fn, int prop)
1793{ 1838{
1794 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1839 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1795 1840
1796 if (!pt && USE_ERROR_CHECKING) 1841 if (!pt && USE_ERROR_CHECKING)
1797 return NIL; 1842 return NIL;
1798 1843
1799 return mk_port (SCHEME_A_ pt); 1844 return mk_port (SCHEME_A_ pt);
1800} 1845}
1801 1846
1802static port * 1847ecb_cold static port *
1803port_rep_from_file (SCHEME_P_ int f, int prop) 1848port_rep_from_file (SCHEME_P_ int f, int prop)
1804{ 1849{
1805 port *pt = malloc (sizeof *pt); 1850 port *pt = malloc (sizeof *pt);
1806 1851
1807 if (!pt && USE_ERROR_CHECKING) 1852 if (!pt && USE_ERROR_CHECKING)
1812 pt->rep.stdio.file = f; 1857 pt->rep.stdio.file = f;
1813 pt->rep.stdio.closeit = 0; 1858 pt->rep.stdio.closeit = 0;
1814 return pt; 1859 return pt;
1815} 1860}
1816 1861
1817static pointer 1862ecb_cold static pointer
1818port_from_file (SCHEME_P_ int f, int prop) 1863port_from_file (SCHEME_P_ int f, int prop)
1819{ 1864{
1820 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1865 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1821 1866
1822 if (!pt && USE_ERROR_CHECKING) 1867 if (!pt && USE_ERROR_CHECKING)
1823 return NIL; 1868 return NIL;
1824 1869
1825 return mk_port (SCHEME_A_ pt); 1870 return mk_port (SCHEME_A_ pt);
1826} 1871}
1827 1872
1828static port * 1873ecb_cold static port *
1829port_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)
1830{ 1875{
1831 port *pt = malloc (sizeof (port)); 1876 port *pt = malloc (sizeof (port));
1832 1877
1833 if (!pt && USE_ERROR_CHECKING) 1878 if (!pt && USE_ERROR_CHECKING)
1839 pt->rep.string.curr = start; 1884 pt->rep.string.curr = start;
1840 pt->rep.string.past_the_end = past_the_end; 1885 pt->rep.string.past_the_end = past_the_end;
1841 return pt; 1886 return pt;
1842} 1887}
1843 1888
1844static pointer 1889ecb_cold static pointer
1845port_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)
1846{ 1891{
1847 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);
1848 1893
1849 if (!pt && USE_ERROR_CHECKING) 1894 if (!pt && USE_ERROR_CHECKING)
1852 return mk_port (SCHEME_A_ pt); 1897 return mk_port (SCHEME_A_ pt);
1853} 1898}
1854 1899
1855# define BLOCK_SIZE 256 1900# define BLOCK_SIZE 256
1856 1901
1857static port * 1902ecb_cold static port *
1858port_rep_from_scratch (SCHEME_P) 1903port_rep_from_scratch (SCHEME_P)
1859{ 1904{
1860 char *start; 1905 char *start;
1861 port *pt = malloc (sizeof (port)); 1906 port *pt = malloc (sizeof (port));
1862 1907
1876 pt->rep.string.curr = start; 1921 pt->rep.string.curr = start;
1877 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1922 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1878 return pt; 1923 return pt;
1879} 1924}
1880 1925
1881static pointer 1926ecb_cold static pointer
1882port_from_scratch (SCHEME_P) 1927port_from_scratch (SCHEME_P)
1883{ 1928{
1884 port *pt = port_rep_from_scratch (SCHEME_A); 1929 port *pt = port_rep_from_scratch (SCHEME_A);
1885 1930
1886 if (!pt && USE_ERROR_CHECKING) 1931 if (!pt && USE_ERROR_CHECKING)
1887 return NIL; 1932 return NIL;
1888 1933
1889 return mk_port (SCHEME_A_ pt); 1934 return mk_port (SCHEME_A_ pt);
1890} 1935}
1891 1936
1892static void 1937ecb_cold static void
1893port_close (SCHEME_P_ pointer p, int flag) 1938port_close (SCHEME_P_ pointer p, int flag)
1894{ 1939{
1895 port *pt = p->object.port; 1940 port *pt = port (p);
1896 1941
1897 pt->kind &= ~flag; 1942 pt->kind &= ~flag;
1898 1943
1899 if ((pt->kind & (port_input | port_output)) == 0) 1944 if ((pt->kind & (port_input | port_output)) == 0)
1900 { 1945 {
1917 } 1962 }
1918} 1963}
1919#endif 1964#endif
1920 1965
1921/* get new character from input file */ 1966/* get new character from input file */
1922static int 1967ecb_cold static int
1923inchar (SCHEME_P) 1968inchar (SCHEME_P)
1924{ 1969{
1925 int c; 1970 int c;
1926 port *pt; 1971 port *pt = port (SCHEME_V->inport);
1927
1928 pt = SCHEME_V->inport->object.port;
1929 1972
1930 if (pt->kind & port_saw_EOF) 1973 if (pt->kind & port_saw_EOF)
1931 return EOF; 1974 return EOF;
1932 1975
1933 c = basic_inchar (pt); 1976 c = basic_inchar (pt);
1943 } 1986 }
1944 1987
1945 return c; 1988 return c;
1946} 1989}
1947 1990
1948static int ungot = -1; 1991ecb_cold static int
1949
1950static int
1951basic_inchar (port *pt) 1992basic_inchar (port *pt)
1952{ 1993{
1953#if USE_PORTS
1954 if (pt->unget != -1) 1994 if (pt->unget != -1)
1955 { 1995 {
1956 int r = pt->unget; 1996 int r = pt->unget;
1957 pt->unget = -1; 1997 pt->unget = -1;
1958 return r; 1998 return r;
1959 } 1999 }
1960 2000
2001#if USE_PORTS
1961 if (pt->kind & port_file) 2002 if (pt->kind & port_file)
1962 { 2003 {
1963 char c; 2004 char c;
1964 2005
1965 if (!read (pt->rep.stdio.file, &c, 1)) 2006 if (!read (pt->rep.stdio.file, &c, 1))
1973 return EOF; 2014 return EOF;
1974 else 2015 else
1975 return *pt->rep.string.curr++; 2016 return *pt->rep.string.curr++;
1976 } 2017 }
1977#else 2018#else
1978 if (ungot == -1)
1979 {
1980 char c; 2019 char c;
1981 if (!read (0, &c, 1)) 2020
2021 if (!read (pt->rep.stdio.file, &c, 1))
1982 return EOF; 2022 return EOF;
1983 2023
1984 ungot = c;
1985 }
1986
1987 {
1988 int r = ungot;
1989 ungot = -1;
1990 return r; 2024 return c;
1991 }
1992#endif 2025#endif
1993} 2026}
1994 2027
1995/* back character to input buffer */ 2028/* back character to input buffer */
1996static void 2029ecb_cold static void
1997backchar (SCHEME_P_ int c) 2030backchar (SCHEME_P_ int c)
1998{ 2031{
1999#if USE_PORTS 2032 port *pt = port (SCHEME_V->inport);
2000 port *pt;
2001 2033
2002 if (c == EOF) 2034 if (c == EOF)
2003 return; 2035 return;
2004 2036
2005 pt = SCHEME_V->inport->object.port;
2006 pt->unget = c; 2037 pt->unget = c;
2007#else
2008 if (c == EOF)
2009 return;
2010
2011 ungot = c;
2012#endif
2013} 2038}
2014 2039
2015#if USE_PORTS 2040#if USE_PORTS
2016static int 2041ecb_cold static int
2017realloc_port_string (SCHEME_P_ port *p) 2042realloc_port_string (SCHEME_P_ port *p)
2018{ 2043{
2019 char *start = p->rep.string.start; 2044 char *start = p->rep.string.start;
2020 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;
2021 char *str = malloc (new_size); 2046 char *str = malloc (new_size);
2034 else 2059 else
2035 return 0; 2060 return 0;
2036} 2061}
2037#endif 2062#endif
2038 2063
2039INTERFACE void 2064ecb_cold static void
2040putstr (SCHEME_P_ const char *s) 2065putchars (SCHEME_P_ const char *s, int len)
2041{ 2066{
2067 port *pt = port (SCHEME_V->outport);
2068
2042#if USE_PORTS 2069#if USE_PORTS
2043 port *pt = SCHEME_V->outport->object.port;
2044
2045 if (pt->kind & port_file)
2046 write (pt->rep.stdio.file, s, strlen (s));
2047 else
2048 for (; *s; s++)
2049 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2050 *pt->rep.string.curr++ = *s;
2051 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2052 *pt->rep.string.curr++ = *s;
2053
2054#else
2055 xwrstr (s);
2056#endif
2057}
2058
2059static void
2060putchars (SCHEME_P_ const char *s, int len)
2061{
2062#if USE_PORTS
2063 port *pt = SCHEME_V->outport->object.port;
2064
2065 if (pt->kind & port_file) 2070 if (pt->kind & port_file)
2066 write (pt->rep.stdio.file, s, len); 2071 write (pt->rep.stdio.file, s, len);
2067 else 2072 else
2068 { 2073 {
2069 for (; len; len--) 2074 for (; len; len--)
2074 *pt->rep.string.curr++ = *s++; 2079 *pt->rep.string.curr++ = *s++;
2075 } 2080 }
2076 } 2081 }
2077 2082
2078#else 2083#else
2079 write (1, s, len); 2084 write (1, s, len); // output not initialised
2080#endif 2085#endif
2086}
2087
2088INTERFACE void
2089putstr (SCHEME_P_ const char *s)
2090{
2091 putchars (SCHEME_A_ s, strlen (s));
2081} 2092}
2082 2093
2083INTERFACE void 2094INTERFACE void
2084putcharacter (SCHEME_P_ int c) 2095putcharacter (SCHEME_P_ int c)
2085{ 2096{
2086#if USE_PORTS
2087 port *pt = SCHEME_V->outport->object.port;
2088
2089 if (pt->kind & port_file)
2090 {
2091 char cc = c;
2092 write (pt->rep.stdio.file, &cc, 1);
2093 }
2094 else
2095 {
2096 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2097 *pt->rep.string.curr++ = c;
2098 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2099 *pt->rep.string.curr++ = c;
2100 }
2101
2102#else
2103 char cc = c; 2097 char cc = c;
2104 write (1, &c, 1); 2098
2105#endif 2099 putchars (SCHEME_A_ &cc, 1);
2106} 2100}
2107 2101
2108/* read characters up to delimiter, but cater to character constants */ 2102/* read characters up to delimiter, but cater to character constants */
2109static char * 2103ecb_cold static char *
2110readstr_upto (SCHEME_P_ char *delim) 2104readstr_upto (SCHEME_P_ int skip, const char *delim)
2111{ 2105{
2112 char *p = SCHEME_V->strbuff; 2106 char *p = SCHEME_V->strbuff + skip;
2113 2107
2114 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))));
2115 2109
2116 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2110 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2117 *p = 0; 2111 *p = 0;
2123 2117
2124 return SCHEME_V->strbuff; 2118 return SCHEME_V->strbuff;
2125} 2119}
2126 2120
2127/* read string expression "xxx...xxx" */ 2121/* read string expression "xxx...xxx" */
2128static pointer 2122ecb_cold static pointer
2129readstrexp (SCHEME_P) 2123readstrexp (SCHEME_P_ char delim)
2130{ 2124{
2131 char *p = SCHEME_V->strbuff; 2125 char *p = SCHEME_V->strbuff;
2132 int c; 2126 int c;
2133 int c1 = 0; 2127 int c1 = 0;
2134 enum
2135 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2128 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2136 2129
2137 for (;;) 2130 for (;;)
2138 { 2131 {
2139 c = inchar (SCHEME_A); 2132 c = inchar (SCHEME_A);
2140 2133
2142 return S_F; 2135 return S_F;
2143 2136
2144 switch (state) 2137 switch (state)
2145 { 2138 {
2146 case st_ok: 2139 case st_ok:
2147 switch (c) 2140 if (ecb_expect_false (c == delim))
2148 {
2149 case '\\':
2150 state = st_bsl;
2151 break;
2152
2153 case '"':
2154 *p = 0;
2155 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); 2141 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2156 2142
2157 default: 2143 if (ecb_expect_false (c == '\\'))
2144 state = st_bsl;
2145 else
2158 *p++ = c; 2146 *p++ = c;
2159 break;
2160 }
2161 2147
2162 break; 2148 break;
2163 2149
2164 case st_bsl: 2150 case st_bsl:
2165 switch (c) 2151 switch (c)
2174 case '7': 2160 case '7':
2175 state = st_oct1; 2161 state = st_oct1;
2176 c1 = c - '0'; 2162 c1 = c - '0';
2177 break; 2163 break;
2178 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
2179 case 'x': 2180 case 'x':
2180 case 'X': 2181 case 'X':
2181 state = st_x1; 2182 state = st_x1;
2182 c1 = 0; 2183 c1 = 0;
2183 break; 2184 break;
2184 2185
2185 case 'n':
2186 *p++ = '\n';
2187 state = st_ok;
2188 break;
2189
2190 case 't':
2191 *p++ = '\t';
2192 state = st_ok;
2193 break;
2194
2195 case 'r':
2196 *p++ = '\r';
2197 state = st_ok;
2198 break;
2199
2200 case '"':
2201 *p++ = '"';
2202 state = st_ok;
2203 break;
2204
2205 default: 2186 default:
2206 *p++ = c; 2187 *p++ = c;
2207 state = st_ok; 2188 state = st_ok;
2208 break; 2189 break;
2209 } 2190 }
2210 2191
2211 break; 2192 break;
2212 2193
2213 case st_x1: 2194 case st_x1:
2214 case st_x2: 2195 case st_x2:
2215 c = toupper (c); 2196 c = tolower (c);
2216 2197
2217 if (c >= '0' && c <= 'F') 2198 if (c >= '0' && c <= '9')
2218 {
2219 if (c <= '9')
2220 c1 = (c1 << 4) + c - '0'; 2199 c1 = (c1 << 4) + c - '0';
2221 else 2200 else if (c >= 'a' && c <= 'f')
2222 c1 = (c1 << 4) + c - 'A' + 10; 2201 c1 = (c1 << 4) + c - 'a' + 10;
2223
2224 if (state == st_x1)
2225 state = st_x2;
2226 else
2227 {
2228 *p++ = c1;
2229 state = st_ok;
2230 }
2231 }
2232 else 2202 else
2233 return S_F; 2203 return S_F;
2204
2205 if (state == st_x1)
2206 state = st_x2;
2207 else
2208 {
2209 *p++ = c1;
2210 state = st_ok;
2211 }
2234 2212
2235 break; 2213 break;
2236 2214
2237 case st_oct1: 2215 case st_oct1:
2238 case st_oct2: 2216 case st_oct2:
2242 backchar (SCHEME_A_ c); 2220 backchar (SCHEME_A_ c);
2243 state = st_ok; 2221 state = st_ok;
2244 } 2222 }
2245 else 2223 else
2246 { 2224 {
2247 if (state == st_oct2 && c1 >= 32) 2225 if (state == st_oct2 && c1 >= ' ')
2248 return S_F; 2226 return S_F;
2249 2227
2250 c1 = (c1 << 3) + (c - '0'); 2228 c1 = (c1 << 3) + (c - '0');
2251 2229
2252 if (state == st_oct1) 2230 if (state == st_oct1)
2257 state = st_ok; 2235 state = st_ok;
2258 } 2236 }
2259 } 2237 }
2260 2238
2261 break; 2239 break;
2262
2263 } 2240 }
2264 } 2241 }
2265} 2242}
2266 2243
2267/* check c is in chars */ 2244/* check c is in chars */
2268ecb_inline int 2245ecb_cold int
2269is_one_of (char *s, int c) 2246is_one_of (const char *s, int c)
2270{ 2247{
2271 if (c == EOF)
2272 return 1;
2273
2274 return !!strchr (s, c); 2248 return c == EOF || !!strchr (s, c);
2275} 2249}
2276 2250
2277/* skip white characters */ 2251/* skip white characters */
2278ecb_inline int 2252ecb_cold int
2279skipspace (SCHEME_P) 2253skipspace (SCHEME_P)
2280{ 2254{
2281 int c, curr_line = 0; 2255 int c, curr_line = 0;
2282 2256
2283 do 2257 do
2284 { 2258 {
2285 c = inchar (SCHEME_A); 2259 c = inchar (SCHEME_A);
2260
2286#if SHOW_ERROR_LINE 2261#if SHOW_ERROR_LINE
2287 if (c == '\n') 2262 if (ecb_expect_false (c == '\n'))
2288 curr_line++; 2263 curr_line++;
2289#endif 2264#endif
2265
2266 if (ecb_expect_false (c == EOF))
2267 return c;
2290 } 2268 }
2291 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2269 while (is_one_of (WHITESPACE, c));
2292 2270
2293 /* record it */ 2271 /* record it */
2294#if SHOW_ERROR_LINE 2272#if SHOW_ERROR_LINE
2295 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 2273 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file)
2296 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; 2274 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line;
2297#endif 2275#endif
2298 2276
2299 if (c != EOF)
2300 {
2301 backchar (SCHEME_A_ c); 2277 backchar (SCHEME_A_ c);
2302 return 1; 2278 return 1;
2303 }
2304 else
2305 return EOF;
2306} 2279}
2307 2280
2308/* get token */ 2281/* get token */
2309static int 2282ecb_cold static int
2310token (SCHEME_P) 2283token (SCHEME_P)
2311{ 2284{
2312 int c = skipspace (SCHEME_A); 2285 int c = skipspace (SCHEME_A);
2313 2286
2314 if (c == EOF) 2287 if (c == EOF)
2326 return TOK_RPAREN; 2299 return TOK_RPAREN;
2327 2300
2328 case '.': 2301 case '.':
2329 c = inchar (SCHEME_A); 2302 c = inchar (SCHEME_A);
2330 2303
2331 if (is_one_of (" \n\t", c)) 2304 if (is_one_of (WHITESPACE, c))
2332 return TOK_DOT; 2305 return TOK_DOT;
2333 else 2306 else
2334 { 2307 {
2335 //TODO: ungetc twice in a row is not supported in C
2336 backchar (SCHEME_A_ c); 2308 backchar (SCHEME_A_ c);
2337 backchar (SCHEME_A_ '.');
2338 return TOK_ATOM; 2309 return TOK_DOTATOM;
2339 } 2310 }
2311
2312 case '|':
2313 return TOK_STRATOM;
2340 2314
2341 case '\'': 2315 case '\'':
2342 return TOK_QUOTE; 2316 return TOK_QUOTE;
2343 2317
2344 case ';': 2318 case ';':
2411} 2385}
2412 2386
2413/* ========== Routines for Printing ========== */ 2387/* ========== Routines for Printing ========== */
2414#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2388#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2415 2389
2416static void 2390ecb_cold static void
2417printslashstring (SCHEME_P_ char *p, int len) 2391printslashstring (SCHEME_P_ char *p, int len)
2418{ 2392{
2419 int i; 2393 int i;
2420 unsigned char *s = (unsigned char *) p; 2394 unsigned char *s = (unsigned char *) p;
2421 2395
2476 } 2450 }
2477 2451
2478 putcharacter (SCHEME_A_ '"'); 2452 putcharacter (SCHEME_A_ '"');
2479} 2453}
2480 2454
2481
2482/* print atoms */ 2455/* print atoms */
2483static void 2456ecb_cold static void
2484printatom (SCHEME_P_ pointer l, int f) 2457printatom (SCHEME_P_ pointer l, int f)
2485{ 2458{
2486 char *p; 2459 char *p;
2487 int len; 2460 int len;
2488 2461
2489 atom2str (SCHEME_A_ l, f, &p, &len); 2462 atom2str (SCHEME_A_ l, f, &p, &len);
2490 putchars (SCHEME_A_ p, len); 2463 putchars (SCHEME_A_ p, len);
2491} 2464}
2492 2465
2493
2494/* Uses internal buffer unless string pointer is already available */ 2466/* Uses internal buffer unless string pointer is already available */
2495static void 2467ecb_cold static void
2496atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2468atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2497{ 2469{
2498 char *p; 2470 char *p;
2499 2471
2500 if (l == NIL) 2472 if (l == NIL)
2658#endif 2630#endif
2659 } 2631 }
2660 else if (is_continuation (l)) 2632 else if (is_continuation (l))
2661 p = "#<CONTINUATION>"; 2633 p = "#<CONTINUATION>";
2662 else 2634 else
2635 {
2636#if USE_PRINTF
2637 p = SCHEME_V->strbuff;
2638 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2639#else
2663 p = "#<ERROR>"; 2640 p = "#<ERROR>";
2641#endif
2642 }
2664 2643
2665 *pp = p; 2644 *pp = p;
2666 *plen = strlen (p); 2645 *plen = strlen (p);
2667} 2646}
2668 2647
2700 return car (d); 2679 return car (d);
2701 2680
2702 p = cons (car (d), cdr (d)); 2681 p = cons (car (d), cdr (d));
2703 q = p; 2682 q = p;
2704 2683
2705 while (cdr (cdr (p)) != NIL) 2684 while (cddr (p) != NIL)
2706 { 2685 {
2707 d = cons (car (p), cdr (p)); 2686 d = cons (car (p), cdr (p));
2708 2687
2709 if (cdr (cdr (p)) != NIL) 2688 if (cddr (p) != NIL)
2710 p = cdr (d); 2689 p = cdr (d);
2711 } 2690 }
2712 2691
2713 set_cdr (p, car (cdr (p))); 2692 set_cdr (p, cadr (p));
2714 return q; 2693 return q;
2715} 2694}
2716 2695
2717/* reverse list -- produce new list */ 2696/* reverse list -- produce new list */
2718static pointer 2697ecb_hot static pointer
2719reverse (SCHEME_P_ pointer a) 2698reverse (SCHEME_P_ pointer a)
2720{ 2699{
2721 /* a must be checked by gc */ 2700 /* a must be checked by gc */
2722 pointer p = NIL; 2701 pointer p = NIL;
2723 2702
2726 2705
2727 return p; 2706 return p;
2728} 2707}
2729 2708
2730/* reverse list --- in-place */ 2709/* reverse list --- in-place */
2731static pointer 2710ecb_hot static pointer
2732reverse_in_place (SCHEME_P_ pointer term, pointer list) 2711reverse_in_place (SCHEME_P_ pointer term, pointer list)
2733{ 2712{
2734 pointer result = term; 2713 pointer result = term;
2735 pointer p = list; 2714 pointer p = list;
2736 2715
2744 2723
2745 return result; 2724 return result;
2746} 2725}
2747 2726
2748/* append list -- produce new list (in reverse order) */ 2727/* append list -- produce new list (in reverse order) */
2749static pointer 2728ecb_hot static pointer
2750revappend (SCHEME_P_ pointer a, pointer b) 2729revappend (SCHEME_P_ pointer a, pointer b)
2751{ 2730{
2752 pointer result = a; 2731 pointer result = a;
2753 pointer p = b; 2732 pointer p = b;
2754 2733
2763 2742
2764 return S_F; /* signal an error */ 2743 return S_F; /* signal an error */
2765} 2744}
2766 2745
2767/* equivalence of atoms */ 2746/* equivalence of atoms */
2768int 2747ecb_hot int
2769eqv (pointer a, pointer b) 2748eqv (pointer a, pointer b)
2770{ 2749{
2771 if (is_string (a)) 2750 if (is_string (a))
2772 { 2751 {
2773 if (is_string (b)) 2752 if (is_string (b))
2812/* () is #t in R5RS */ 2791/* () is #t in R5RS */
2813#define is_true(p) ((p) != S_F) 2792#define is_true(p) ((p) != S_F)
2814#define is_false(p) ((p) == S_F) 2793#define is_false(p) ((p) == S_F)
2815 2794
2816/* ========== Environment implementation ========== */ 2795/* ========== Environment implementation ========== */
2817
2818#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
2819
2820static int
2821hash_fn (const char *key, int table_size)
2822{
2823 const unsigned char *p = key;
2824 uint32_t hash = 2166136261;
2825
2826 while (*p)
2827 hash = (hash ^ *p++) * 16777619;
2828
2829 return hash % table_size;
2830}
2831#endif
2832 2796
2833#ifndef USE_ALIST_ENV 2797#ifndef USE_ALIST_ENV
2834 2798
2835/* 2799/*
2836 * In this implementation, each frame of the environment may be 2800 * In this implementation, each frame of the environment may be
2853 2817
2854 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2818 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2855 setenvironment (SCHEME_V->envir); 2819 setenvironment (SCHEME_V->envir);
2856} 2820}
2857 2821
2822static uint32_t
2823sym_hash (pointer sym, uint32_t size)
2824{
2825 uintptr_t ptr = (uintptr_t)sym;
2826
2827#if 0
2828 /* table size is prime, so why mix */
2829 ptr += ptr >> 32;
2830 ptr += ptr >> 16;
2831 ptr += ptr >> 8;
2832#endif
2833
2834 return ptr % size;
2835}
2836
2858ecb_inline void 2837ecb_inline void
2859new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2838new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2860{ 2839{
2861 pointer slot = immutable_cons (variable, value); 2840 pointer slot = immutable_cons (variable, value);
2862 2841
2863 if (is_vector (car (env))) 2842 if (is_vector (car (env)))
2864 { 2843 {
2865 int location = hash_fn (symname (variable), veclength (car (env))); 2844 int location = sym_hash (variable, veclength (car (env)));
2866
2867 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); 2845 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2868 } 2846 }
2869 else 2847 else
2870 set_car (env, immutable_cons (slot, car (env))); 2848 set_car (env, immutable_cons (slot, car (env)));
2871} 2849}
2872 2850
2873static pointer 2851ecb_hot static pointer
2874find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2852find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2875{ 2853{
2876 pointer x, y; 2854 pointer x, y;
2877 int location;
2878 2855
2879 for (x = env; x != NIL; x = cdr (x)) 2856 for (x = env; x != NIL; x = cdr (x))
2880 { 2857 {
2881 if (is_vector (car (x))) 2858 if (is_vector (car (x)))
2882 { 2859 {
2883 location = hash_fn (symname (hdl), veclength (car (x))); 2860 int location = sym_hash (hdl, veclength (car (x)));
2884 y = vector_get (car (x), location); 2861 y = vector_get (car (x), location);
2885 } 2862 }
2886 else 2863 else
2887 y = car (x); 2864 y = car (x);
2888 2865
2889 for (; y != NIL; y = cdr (y)) 2866 for (; y != NIL; y = cdr (y))
2890 if (caar (y) == hdl) 2867 if (caar (y) == hdl)
2891 break; 2868 break;
2892 2869
2893 if (y != NIL) 2870 if (y != NIL)
2871 return car (y);
2872
2873 if (!all)
2894 break; 2874 break;
2895
2896 if (!all)
2897 return NIL;
2898 } 2875 }
2899
2900 if (x != NIL)
2901 return car (y);
2902 2876
2903 return NIL; 2877 return NIL;
2904} 2878}
2905 2879
2906#else /* USE_ALIST_ENV */ 2880#else /* USE_ALIST_ENV */
2907 2881
2908ecb_inline void 2882static void
2909new_frame_in_env (SCHEME_P_ pointer old_env) 2883new_frame_in_env (SCHEME_P_ pointer old_env)
2910{ 2884{
2911 SCHEME_V->envir = immutable_cons (NIL, old_env); 2885 SCHEME_V->envir = immutable_cons (NIL, old_env);
2912 setenvironment (SCHEME_V->envir); 2886 setenvironment (SCHEME_V->envir);
2913} 2887}
2914 2888
2915ecb_inline void 2889static void
2916new_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)
2917{ 2891{
2918 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2892 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2919} 2893}
2920 2894
2921static pointer 2895ecb_hot static pointer
2922find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2896find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2923{ 2897{
2924 pointer x, y; 2898 pointer x, y;
2925 2899
2926 for (x = env; x != NIL; x = cdr (x)) 2900 for (x = env; x != NIL; x = cdr (x))
2928 for (y = car (x); y != NIL; y = cdr (y)) 2902 for (y = car (x); y != NIL; y = cdr (y))
2929 if (caar (y) == hdl) 2903 if (caar (y) == hdl)
2930 break; 2904 break;
2931 2905
2932 if (y != NIL) 2906 if (y != NIL)
2907 return car (y);
2933 break; 2908 break;
2934 2909
2935 if (!all) 2910 if (!all)
2936 return NIL; 2911 break;
2937 } 2912 }
2938
2939 if (x != NIL)
2940 return car (y);
2941 2913
2942 return NIL; 2914 return NIL;
2943} 2915}
2944 2916
2945#endif /* USE_ALIST_ENV else */ 2917#endif /* USE_ALIST_ENV else */
2946 2918
2947ecb_inline void 2919static void
2948new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2920new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2949{ 2921{
2922 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2950 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);
2951} 2924}
2952 2925
2953ecb_inline void 2926static void
2954set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2927set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2955{ 2928{
2956 set_cdr (slot, value); 2929 set_cdr (slot, value);
2957} 2930}
2958 2931
2959ecb_inline pointer 2932static pointer
2960slot_value_in_env (pointer slot) 2933slot_value_in_env (pointer slot)
2961{ 2934{
2962 return cdr (slot); 2935 return cdr (slot);
2963} 2936}
2964 2937
2965/* ========== Evaluation Cycle ========== */ 2938/* ========== Evaluation Cycle ========== */
2966 2939
2967static int 2940ecb_cold static int
2968xError_1 (SCHEME_P_ const char *s, pointer a) 2941xError_1 (SCHEME_P_ const char *s, pointer a)
2969{ 2942{
2970#if USE_ERROR_HOOK
2971 pointer x;
2972 pointer hdl = SCHEME_V->ERROR_HOOK;
2973#endif
2974
2975#if USE_PRINTF 2943#if USE_PRINTF
2976#if SHOW_ERROR_LINE 2944#if SHOW_ERROR_LINE
2977 char sbuf[STRBUFFSIZE]; 2945 char sbuf[STRBUFFSIZE];
2978 2946
2979 /* make sure error is not in REPL */ 2947 /* make sure error is not in REPL */
2994 } 2962 }
2995#endif 2963#endif
2996#endif 2964#endif
2997 2965
2998#if USE_ERROR_HOOK 2966#if USE_ERROR_HOOK
2999 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);
3000 2968
3001 if (x != NIL) 2969 if (x != NIL)
3002 { 2970 {
3003 pointer code = a 2971 pointer code = a
3004 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) 2972 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL)
3048 pointer code; 3016 pointer code;
3049}; 3017};
3050 3018
3051# define STACK_GROWTH 3 3019# define STACK_GROWTH 3
3052 3020
3053static void 3021ecb_hot static void
3054s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3022s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3055{ 3023{
3056 int nframes = (uintptr_t)SCHEME_V->dump; 3024 int nframes = (uintptr_t)SCHEME_V->dump;
3057 struct dump_stack_frame *next_frame; 3025 struct dump_stack_frame *next_frame;
3058 3026
3059 /* enough room for the next frame? */ 3027 /* enough room for the next frame? */
3060 if (nframes >= SCHEME_V->dump_size) 3028 if (ecb_expect_false (nframes >= SCHEME_V->dump_size))
3061 { 3029 {
3062 SCHEME_V->dump_size += STACK_GROWTH; 3030 SCHEME_V->dump_size += STACK_GROWTH;
3063 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);
3064 } 3032 }
3065 3033
3071 next_frame->code = code; 3039 next_frame->code = code;
3072 3040
3073 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3041 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3074} 3042}
3075 3043
3076static int 3044static ecb_hot int
3077xs_return (SCHEME_P_ pointer a) 3045xs_return (SCHEME_P_ pointer a)
3078{ 3046{
3079 int nframes = (uintptr_t)SCHEME_V->dump; 3047 int nframes = (uintptr_t)SCHEME_V->dump;
3080 struct dump_stack_frame *frame; 3048 struct dump_stack_frame *frame;
3081 3049
3092 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3060 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3093 3061
3094 return 0; 3062 return 0;
3095} 3063}
3096 3064
3097ecb_inline void 3065ecb_cold void
3098dump_stack_reset (SCHEME_P) 3066dump_stack_reset (SCHEME_P)
3099{ 3067{
3100 /* 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 */
3101 SCHEME_V->dump = (pointer)+0; 3069 SCHEME_V->dump = (pointer)+0;
3102} 3070}
3103 3071
3104ecb_inline void 3072ecb_cold void
3105dump_stack_initialize (SCHEME_P) 3073dump_stack_initialize (SCHEME_P)
3106{ 3074{
3107 SCHEME_V->dump_size = 0; 3075 SCHEME_V->dump_size = 0;
3108 SCHEME_V->dump_base = 0; 3076 SCHEME_V->dump_base = 0;
3109 dump_stack_reset (SCHEME_A); 3077 dump_stack_reset (SCHEME_A);
3110} 3078}
3111 3079
3112static void 3080ecb_cold static void
3113dump_stack_free (SCHEME_P) 3081dump_stack_free (SCHEME_P)
3114{ 3082{
3115 free (SCHEME_V->dump_base); 3083 free (SCHEME_V->dump_base);
3116 SCHEME_V->dump_base = 0; 3084 SCHEME_V->dump_base = 0;
3117 SCHEME_V->dump = (pointer)0; 3085 SCHEME_V->dump = (pointer)0;
3118 SCHEME_V->dump_size = 0; 3086 SCHEME_V->dump_size = 0;
3119} 3087}
3120 3088
3121static void 3089ecb_cold static void
3122dump_stack_mark (SCHEME_P) 3090dump_stack_mark (SCHEME_P)
3123{ 3091{
3124 int nframes = (uintptr_t)SCHEME_V->dump; 3092 int nframes = (uintptr_t)SCHEME_V->dump;
3125 int i; 3093 int i;
3126 3094
3132 mark (frame->envir); 3100 mark (frame->envir);
3133 mark (frame->code); 3101 mark (frame->code);
3134 } 3102 }
3135} 3103}
3136 3104
3137static pointer 3105ecb_cold static pointer
3138ss_get_cont (SCHEME_P) 3106ss_get_cont (SCHEME_P)
3139{ 3107{
3140 int nframes = (uintptr_t)SCHEME_V->dump; 3108 int nframes = (uintptr_t)SCHEME_V->dump;
3141 int i; 3109 int i;
3142 3110
3154 } 3122 }
3155 3123
3156 return cont; 3124 return cont;
3157} 3125}
3158 3126
3159static void 3127ecb_cold static void
3160ss_set_cont (SCHEME_P_ pointer cont) 3128ss_set_cont (SCHEME_P_ pointer cont)
3161{ 3129{
3162 int i = 0; 3130 int i = 0;
3163 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3131 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3164 3132
3176 SCHEME_V->dump = (pointer)(uintptr_t)i; 3144 SCHEME_V->dump = (pointer)(uintptr_t)i;
3177} 3145}
3178 3146
3179#else 3147#else
3180 3148
3181ecb_inline void 3149ecb_cold void
3182dump_stack_reset (SCHEME_P) 3150dump_stack_reset (SCHEME_P)
3183{ 3151{
3184 SCHEME_V->dump = NIL; 3152 SCHEME_V->dump = NIL;
3185} 3153}
3186 3154
3187ecb_inline void 3155ecb_cold void
3188dump_stack_initialize (SCHEME_P) 3156dump_stack_initialize (SCHEME_P)
3189{ 3157{
3190 dump_stack_reset (SCHEME_A); 3158 dump_stack_reset (SCHEME_A);
3191} 3159}
3192 3160
3193static void 3161ecb_cold static void
3194dump_stack_free (SCHEME_P) 3162dump_stack_free (SCHEME_P)
3195{ 3163{
3196 SCHEME_V->dump = NIL; 3164 SCHEME_V->dump = NIL;
3197} 3165}
3198 3166
3199static int 3167ecb_hot static int
3200xs_return (SCHEME_P_ pointer a) 3168xs_return (SCHEME_P_ pointer a)
3201{ 3169{
3202 pointer dump = SCHEME_V->dump; 3170 pointer dump = SCHEME_V->dump;
3203 3171
3204 SCHEME_V->value = a; 3172 SCHEME_V->value = a;
3214 SCHEME_V->dump = dump; 3182 SCHEME_V->dump = dump;
3215 3183
3216 return 0; 3184 return 0;
3217} 3185}
3218 3186
3219static void 3187ecb_hot static void
3220s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3188s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3221{ 3189{
3222 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3190 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3223 cons (args, 3191 cons (args,
3224 cons (SCHEME_V->envir, 3192 cons (SCHEME_V->envir,
3225 cons (code, 3193 cons (code,
3226 SCHEME_V->dump)))); 3194 SCHEME_V->dump))));
3227} 3195}
3228 3196
3197ecb_cold static void
3198dump_stack_mark (SCHEME_P)
3199{
3200 mark (SCHEME_V->dump);
3201}
3202
3203ecb_cold static pointer
3204ss_get_cont (SCHEME_P)
3205{
3206 return SCHEME_V->dump;
3207}
3208
3209ecb_cold static void
3210ss_set_cont (SCHEME_P_ pointer cont)
3211{
3212 SCHEME_V->dump = cont;
3213}
3214
3215#endif
3216
3217#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3218
3219#if EXPERIMENT
3220
3221static int
3222dtree (SCHEME_P_ int indent, pointer x)
3223{
3224 int c;
3225
3226 if (is_syntax (x))
3227 {
3228 printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x));
3229 return 8 + 8;
3230 }
3231
3232 if (x == NIL)
3233 {
3234 printf ("%*sNIL\n", indent, "");
3235 return 3;
3236 }
3237
3238 switch (type (x))
3239 {
3240 case T_INTEGER:
3241 printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x);
3242 return 32+8;
3243
3244 case T_SYMBOL:
3245 printf ("%*sS<%s>\n", indent, "", symname (x));
3246 return 24+8;
3247
3248 case T_CLOSURE:
3249 printf ("%*sS<%s>\n", indent, "", "closure");
3250 dtree (SCHEME_A_ indent + 3, cdr(x));
3251 return 32 + dtree (SCHEME_A_ indent + 3, car (x));
3252
3253 case T_PAIR:
3254 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3255 c = dtree (SCHEME_A_ indent + 3, car (x));
3256 c += dtree (SCHEME_A_ indent + 3, cdr (x));
3257 return c + 1;
3258
3259 case T_PORT:
3260 printf ("%*sS<%s>\n", indent, "", "port");
3261 return 24+8;
3262
3263 case T_VECTOR:
3264 printf ("%*sS<%s>\n", indent, "", "vector");
3265 return 24+8;
3266
3267 case T_ENVIRONMENT:
3268 printf ("%*sS<%s>\n", indent, "", "environment");
3269 return 0 + dtree (SCHEME_A_ indent + 3, car (x));
3270
3271 default:
3272 printf ("unhandled type %d\n", type (x));
3273 break;
3274 }
3275}
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
3229static void 3400static void
3230dump_stack_mark (SCHEME_P) 3401compile_if (SCHEME_P_ stream s, pointer cond, pointer ift, pointer iff)
3231{ 3402{
3232 mark (SCHEME_V->dump); 3403 //TODO: borked
3233} 3404 stream sift = stream_init (); compile_expr (SCHEME_A_ sift, ift);
3234 3405
3235static pointer 3406 stream_put (s, BOP_BIFF);
3236ss_get_cont (SCHEME_P) 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)
3237{ 3423{
3238 return SCHEME_V->dump; 3424 stream_put (s, 0);
3425 stream_put (s, 0);
3426
3427 return stream_size (s);
3239} 3428}
3240 3429
3241static void 3430static void
3242ss_set_cont (SCHEME_P_ pointer cont) 3431stream_fix_fixup (stream s, stream_fixup fixup, uint32_t target)
3243{ 3432{
3244 SCHEME_V->dump = cont; 3433 target -= fixup;
3434 assert (target < (1 << 14));
3435 stream_data (s)[fixup - 2] = target | 0x80;
3436 stream_data (s)[fixup - 1] = target >> 7;
3245} 3437}
3246 3438
3247#endif
3248
3249#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3250
3251static int 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
3252opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3645opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3253{ 3646{
3254 pointer args = SCHEME_V->args; 3647 pointer args = SCHEME_V->args;
3255 pointer x, y; 3648 pointer x, y;
3256 3649
3257 switch (op) 3650 switch (op)
3258 { 3651 {
3652#if EXPERIMENT //D
3653 case OP_DEBUG:
3654 {
3655 uint32_t len = compile_closure (SCHEME_A_ car (args));
3656 printf ("len = %d\n", len);
3657 printf ("\n");
3658 s_return (S_T);
3659 }
3660
3661 case OP_DEBUG2:
3662 return -1;
3663#endif
3664
3259 case OP_LOAD: /* load */ 3665 case OP_LOAD: /* load */
3260 if (file_interactive (SCHEME_A)) 3666 if (file_interactive (SCHEME_A))
3261 { 3667 {
3262 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3668 putstr (SCHEME_A_ "Loading ");
3263 //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');
3264 } 3671 }
3265 3672
3266 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3673 if (!file_push (SCHEME_A_ strvalue (car (args))))
3267 Error_1 ("unable to open", car (args)); 3674 Error_1 ("unable to open", car (args));
3268 else 3675
3269 {
3270 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3676 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3271 s_goto (OP_T0LVL); 3677 s_goto (OP_T0LVL);
3272 }
3273 3678
3274 case OP_T0LVL: /* top level */ 3679 case OP_T0LVL: /* top level */
3275 3680
3276 /* If we reached the end of file, this loop is done. */ 3681 /* If we reached the end of file, this loop is done. */
3277 if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) 3682 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3278 { 3683 {
3279 if (SCHEME_V->file_i == 0) 3684 if (SCHEME_V->file_i == 0)
3280 { 3685 {
3281 SCHEME_V->args = NIL; 3686 SCHEME_V->args = NIL;
3282 s_goto (OP_QUIT); 3687 s_goto (OP_QUIT);
3293 /* If interactive, be nice to user. */ 3698 /* If interactive, be nice to user. */
3294 if (file_interactive (SCHEME_A)) 3699 if (file_interactive (SCHEME_A))
3295 { 3700 {
3296 SCHEME_V->envir = SCHEME_V->global_env; 3701 SCHEME_V->envir = SCHEME_V->global_env;
3297 dump_stack_reset (SCHEME_A); 3702 dump_stack_reset (SCHEME_A);
3298 putstr (SCHEME_A_ "\n"); 3703 putcharacter (SCHEME_A_ '\n');
3704#if EXPERIMENT
3705 system ("ps v $PPID");
3706#endif
3299 putstr (SCHEME_A_ prompt); 3707 putstr (SCHEME_A_ prompt);
3300 } 3708 }
3301 3709
3302 /* Set up another iteration of REPL */ 3710 /* Set up another iteration of REPL */
3303 SCHEME_V->nesting = 0; 3711 SCHEME_V->nesting = 0;
3338 { 3746 {
3339 SCHEME_V->print_flag = 1; 3747 SCHEME_V->print_flag = 1;
3340 SCHEME_V->args = SCHEME_V->value; 3748 SCHEME_V->args = SCHEME_V->value;
3341 s_goto (OP_P0LIST); 3749 s_goto (OP_P0LIST);
3342 } 3750 }
3343 else 3751
3344 s_return (SCHEME_V->value); 3752 s_return (SCHEME_V->value);
3345 3753
3346 case OP_EVAL: /* main part of evaluation */ 3754 case OP_EVAL: /* main part of evaluation */
3347#if USE_TRACING 3755#if USE_TRACING
3348 if (SCHEME_V->tracing) 3756 if (SCHEME_V->tracing)
3349 { 3757 {
3360#endif 3768#endif
3361 if (is_symbol (SCHEME_V->code)) /* symbol */ 3769 if (is_symbol (SCHEME_V->code)) /* symbol */
3362 { 3770 {
3363 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);
3364 3772
3365 if (x != NIL) 3773 if (x == NIL)
3366 s_return (slot_value_in_env (x));
3367 else
3368 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));
3369 } 3777 }
3370 else if (is_pair (SCHEME_V->code)) 3778 else if (is_pair (SCHEME_V->code))
3371 { 3779 {
3372 x = car (SCHEME_V->code); 3780 x = car (SCHEME_V->code);
3373 3781
3382 /* 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)); */
3383 SCHEME_V->code = x; 3791 SCHEME_V->code = x;
3384 s_goto (OP_EVAL); 3792 s_goto (OP_EVAL);
3385 } 3793 }
3386 } 3794 }
3387 else 3795
3388 s_return (SCHEME_V->code); 3796 s_return (SCHEME_V->code);
3389 3797
3390 case OP_E0ARGS: /* eval arguments */ 3798 case OP_E0ARGS: /* eval arguments */
3391 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3799 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3392 { 3800 {
3393 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3801 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3394 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3802 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3395 SCHEME_V->code = SCHEME_V->value; 3803 SCHEME_V->code = SCHEME_V->value;
3396 s_goto (OP_APPLY); 3804 s_goto (OP_APPLY);
3397 } 3805 }
3398 else 3806
3399 {
3400 SCHEME_V->code = cdr (SCHEME_V->code); 3807 SCHEME_V->code = cdr (SCHEME_V->code);
3401 s_goto (OP_E1ARGS); 3808 s_goto (OP_E1ARGS);
3402 }
3403 3809
3404 case OP_E1ARGS: /* eval arguments */ 3810 case OP_E1ARGS: /* eval arguments */
3405 args = cons (SCHEME_V->value, args); 3811 args = cons (SCHEME_V->value, args);
3406 3812
3407 if (is_pair (SCHEME_V->code)) /* continue */ 3813 if (is_pair (SCHEME_V->code)) /* continue */
3418 SCHEME_V->args = cdr (args); 3824 SCHEME_V->args = cdr (args);
3419 s_goto (OP_APPLY); 3825 s_goto (OP_APPLY);
3420 } 3826 }
3421 3827
3422#if USE_TRACING 3828#if USE_TRACING
3423
3424 case OP_TRACING: 3829 case OP_TRACING:
3425 { 3830 {
3426 int tr = SCHEME_V->tracing; 3831 int tr = SCHEME_V->tracing;
3427 3832
3428 SCHEME_V->tracing = ivalue_unchecked (car (args)); 3833 SCHEME_V->tracing = ivalue_unchecked (car (args));
3429 s_return (mk_integer (SCHEME_A_ tr)); 3834 s_return (mk_integer (SCHEME_A_ tr));
3430 } 3835 }
3431
3432#endif 3836#endif
3433 3837
3434 case OP_APPLY: /* apply 'code' to 'args' */ 3838 case OP_APPLY: /* apply 'code' to 'args' */
3435#if USE_TRACING 3839#if USE_TRACING
3436 if (SCHEME_V->tracing) 3840 if (SCHEME_V->tracing)
3450 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3854 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3451 else if (is_foreign (SCHEME_V->code)) 3855 else if (is_foreign (SCHEME_V->code))
3452 { 3856 {
3453 /* Keep nested calls from GC'ing the arglist */ 3857 /* Keep nested calls from GC'ing the arglist */
3454 push_recent_alloc (SCHEME_A_ args, NIL); 3858 push_recent_alloc (SCHEME_A_ args, NIL);
3455 x = SCHEME_V->code->object.ff (SCHEME_A_ args); 3859 x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args);
3456 3860
3457 s_return (x); 3861 s_return (x);
3458 } 3862 }
3459 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 */
3460 { 3864 {
3490 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3894 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3491 { 3895 {
3492 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3896 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3493 s_return (args != NIL ? car (args) : NIL); 3897 s_return (args != NIL ? car (args) : NIL);
3494 } 3898 }
3495 else 3899
3496 Error_0 ("illegal function"); 3900 Error_0 ("illegal function");
3497 3901
3498 case OP_DOMACRO: /* do macro */ 3902 case OP_DOMACRO: /* do macro */
3499 SCHEME_V->code = SCHEME_V->value; 3903 SCHEME_V->code = SCHEME_V->value;
3500 s_goto (OP_EVAL); 3904 s_goto (OP_EVAL);
3501
3502#if 1
3503 3905
3504 case OP_LAMBDA: /* lambda */ 3906 case OP_LAMBDA: /* lambda */
3505 /* 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
3506 set SCHEME_V->value fall thru */ 3908 set SCHEME_V->value fall thru */
3507 { 3909 {
3514 SCHEME_V->code = slot_value_in_env (f); 3916 SCHEME_V->code = slot_value_in_env (f);
3515 s_goto (OP_APPLY); 3917 s_goto (OP_APPLY);
3516 } 3918 }
3517 3919
3518 SCHEME_V->value = SCHEME_V->code; 3920 SCHEME_V->value = SCHEME_V->code;
3519 /* Fallthru */
3520 } 3921 }
3922 /* Fallthru */
3521 3923
3522 case OP_LAMBDA1: 3924 case OP_LAMBDA1:
3523 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));
3524
3525#else
3526
3527 case OP_LAMBDA: /* lambda */
3528 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3529
3530#endif
3531 3926
3532 case OP_MKCLOSURE: /* make-closure */ 3927 case OP_MKCLOSURE: /* make-closure */
3533 x = car (args); 3928 x = car (args);
3534 3929
3535 if (car (x) == SCHEME_V->LAMBDA) 3930 if (car (x) == SCHEME_V->LAMBDA)
3574 else 3969 else
3575 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);
3576 3971
3577 s_return (SCHEME_V->code); 3972 s_return (SCHEME_V->code);
3578 3973
3579
3580 case OP_DEFP: /* defined? */ 3974 case OP_DEFP: /* defined? */
3581 x = SCHEME_V->envir; 3975 x = SCHEME_V->envir;
3582 3976
3583 if (cdr (args) != NIL) 3977 if (cdr (args) != NIL)
3584 x = cadr (args); 3978 x = cadr (args);
3602 s_return (SCHEME_V->value); 3996 s_return (SCHEME_V->value);
3603 } 3997 }
3604 else 3998 else
3605 Error_1 ("set!: unbound variable:", SCHEME_V->code); 3999 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3606 4000
3607
3608 case OP_BEGIN: /* begin */ 4001 case OP_BEGIN: /* begin */
3609 if (!is_pair (SCHEME_V->code)) 4002 if (!is_pair (SCHEME_V->code))
3610 s_return (SCHEME_V->code); 4003 s_return (SCHEME_V->code);
3611 4004
3612 if (cdr (SCHEME_V->code) != NIL) 4005 if (cdr (SCHEME_V->code) != NIL)
3623 case OP_IF1: /* if */ 4016 case OP_IF1: /* if */
3624 if (is_true (SCHEME_V->value)) 4017 if (is_true (SCHEME_V->value))
3625 SCHEME_V->code = car (SCHEME_V->code); 4018 SCHEME_V->code = car (SCHEME_V->code);
3626 else 4019 else
3627 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
3628 s_goto (OP_EVAL); 4022 s_goto (OP_EVAL);
3629 4023
3630 case OP_LET0: /* let */ 4024 case OP_LET0: /* let */
3631 SCHEME_V->args = NIL; 4025 SCHEME_V->args = NIL;
3632 SCHEME_V->value = SCHEME_V->code; 4026 SCHEME_V->value = SCHEME_V->code;
3633 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);
3634 s_goto (OP_LET1); 4028 s_goto (OP_LET1);
3635 4029
3636 case OP_LET1: /* let (calculate parameters) */ 4030 case OP_LET1: /* let (calculate parameters) */
4031 case OP_LET1REC: /* letrec (calculate parameters) */
3637 args = cons (SCHEME_V->value, args); 4032 args = cons (SCHEME_V->value, args);
3638 4033
3639 if (is_pair (SCHEME_V->code)) /* continue */ 4034 if (is_pair (SCHEME_V->code)) /* continue */
3640 { 4035 {
3641 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)))
3642 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));
3643 4038
3644 s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); 4039 s_save (SCHEME_A_ op, args, cdr (SCHEME_V->code));
3645 SCHEME_V->code = cadar (SCHEME_V->code); 4040 SCHEME_V->code = cadar (SCHEME_V->code);
3646 SCHEME_V->args = NIL; 4041 SCHEME_V->args = NIL;
3647 s_goto (OP_EVAL); 4042 s_goto (OP_EVAL);
3648 } 4043 }
3649 else /* end */ 4044
3650 { 4045 /* end */
3651 args = reverse_in_place (SCHEME_A_ NIL, args); 4046 args = reverse_in_place (SCHEME_A_ NIL, args);
3652 SCHEME_V->code = car (args); 4047 SCHEME_V->code = car (args);
3653 SCHEME_V->args = cdr (args); 4048 SCHEME_V->args = cdr (args);
3654 s_goto (OP_LET2); 4049 s_goto (op == OP_LET1 ? OP_LET2 : OP_LET2REC);
3655 }
3656 4050
3657 case OP_LET2: /* let */ 4051 case OP_LET2: /* let */
3658 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4052 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3659 4053
3660 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;
3664 if (is_symbol (car (SCHEME_V->code))) /* named let */ 4058 if (is_symbol (car (SCHEME_V->code))) /* named let */
3665 { 4059 {
3666 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))
3667 { 4061 {
3668 if (!is_pair (x)) 4062 if (!is_pair (x))
3669 Error_1 ("Bad syntax of binding in let :", x); 4063 Error_1 ("Bad syntax of binding in let:", x);
3670 4064
3671 if (!is_list (SCHEME_A_ car (x))) 4065 if (!is_list (SCHEME_A_ car (x)))
3672 Error_1 ("Bad syntax of binding in let :", car (x)); 4066 Error_1 ("Bad syntax of binding in let:", car (x));
3673 4067
3674 args = cons (caar (x), args); 4068 args = cons (caar (x), args);
3675 } 4069 }
3676 4070
3677 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)),
3694 SCHEME_V->code = cdr (SCHEME_V->code); 4088 SCHEME_V->code = cdr (SCHEME_V->code);
3695 s_goto (OP_BEGIN); 4089 s_goto (OP_BEGIN);
3696 } 4090 }
3697 4091
3698 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)))
3699 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));
3700 4094
3701 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));
3702 SCHEME_V->code = car (cdaar (SCHEME_V->code)); 4096 SCHEME_V->code = car (cdaar (SCHEME_V->code));
3703 s_goto (OP_EVAL); 4097 s_goto (OP_EVAL);
3704 4098
3715 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); 4109 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3716 SCHEME_V->code = cadar (SCHEME_V->code); 4110 SCHEME_V->code = cadar (SCHEME_V->code);
3717 SCHEME_V->args = NIL; 4111 SCHEME_V->args = NIL;
3718 s_goto (OP_EVAL); 4112 s_goto (OP_EVAL);
3719 } 4113 }
3720 else /* end */ 4114
4115 /* end */
3721 { 4116
3722 SCHEME_V->code = args; 4117 SCHEME_V->code = args;
3723 SCHEME_V->args = NIL; 4118 SCHEME_V->args = NIL;
3724 s_goto (OP_BEGIN); 4119 s_goto (OP_BEGIN);
3725 }
3726 4120
3727 case OP_LET0REC: /* letrec */ 4121 case OP_LET0REC: /* letrec */
3728 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4122 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3729 SCHEME_V->args = NIL; 4123 SCHEME_V->args = NIL;
3730 SCHEME_V->value = SCHEME_V->code; 4124 SCHEME_V->value = SCHEME_V->code;
3731 SCHEME_V->code = car (SCHEME_V->code); 4125 SCHEME_V->code = car (SCHEME_V->code);
3732 s_goto (OP_LET1REC); 4126 s_goto (OP_LET1REC);
3733 4127
3734 case OP_LET1REC: /* letrec (calculate parameters) */ 4128 /* OP_LET1REC handled by OP_LET1 */
3735 args = cons (SCHEME_V->value, args);
3736
3737 if (is_pair (SCHEME_V->code)) /* continue */
3738 {
3739 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3740 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3741
3742 s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code));
3743 SCHEME_V->code = cadar (SCHEME_V->code);
3744 SCHEME_V->args = NIL;
3745 s_goto (OP_EVAL);
3746 }
3747 else /* end */
3748 {
3749 args = reverse_in_place (SCHEME_A_ NIL, args);
3750 SCHEME_V->code = car (args);
3751 SCHEME_V->args = cdr (args);
3752 s_goto (OP_LET2REC);
3753 }
3754 4129
3755 case OP_LET2REC: /* letrec */ 4130 case OP_LET2REC: /* letrec */
3756 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))
3757 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 4132 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3758 4133
3788 } 4163 }
3789 else 4164 else
3790 { 4165 {
3791 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 4166 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
3792 s_return (NIL); 4167 s_return (NIL);
3793 else 4168
3794 {
3795 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 4169 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
3796 SCHEME_V->code = caar (SCHEME_V->code); 4170 SCHEME_V->code = caar (SCHEME_V->code);
3797 s_goto (OP_EVAL); 4171 s_goto (OP_EVAL);
3798 }
3799 } 4172 }
3800 4173
3801 case OP_DELAY: /* delay */ 4174 case OP_DELAY: /* delay */
3802 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);
3803 set_typeflag (x, T_PROMISE); 4176 set_typeflag (x, T_PROMISE);
3814 case OP_AND1: /* and */ 4187 case OP_AND1: /* and */
3815 if (is_false (SCHEME_V->value)) 4188 if (is_false (SCHEME_V->value))
3816 s_return (SCHEME_V->value); 4189 s_return (SCHEME_V->value);
3817 else if (SCHEME_V->code == NIL) 4190 else if (SCHEME_V->code == NIL)
3818 s_return (SCHEME_V->value); 4191 s_return (SCHEME_V->value);
3819 else 4192
3820 {
3821 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 4193 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
3822 SCHEME_V->code = car (SCHEME_V->code); 4194 SCHEME_V->code = car (SCHEME_V->code);
3823 s_goto (OP_EVAL); 4195 s_goto (OP_EVAL);
3824 }
3825 4196
3826 case OP_OR0: /* or */ 4197 case OP_OR0: /* or */
3827 if (SCHEME_V->code == NIL) 4198 if (SCHEME_V->code == NIL)
3828 s_return (S_F); 4199 s_return (S_F);
3829 4200
3834 case OP_OR1: /* or */ 4205 case OP_OR1: /* or */
3835 if (is_true (SCHEME_V->value)) 4206 if (is_true (SCHEME_V->value))
3836 s_return (SCHEME_V->value); 4207 s_return (SCHEME_V->value);
3837 else if (SCHEME_V->code == NIL) 4208 else if (SCHEME_V->code == NIL)
3838 s_return (SCHEME_V->value); 4209 s_return (SCHEME_V->value);
3839 else 4210
3840 {
3841 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 4211 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
3842 SCHEME_V->code = car (SCHEME_V->code); 4212 SCHEME_V->code = car (SCHEME_V->code);
3843 s_goto (OP_EVAL); 4213 s_goto (OP_EVAL);
3844 }
3845 4214
3846 case OP_C0STREAM: /* cons-stream */ 4215 case OP_C0STREAM: /* cons-stream */
3847 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 4216 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3848 SCHEME_V->code = car (SCHEME_V->code); 4217 SCHEME_V->code = car (SCHEME_V->code);
3849 s_goto (OP_EVAL); 4218 s_goto (OP_EVAL);
3914 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); 4283 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x));
3915 SCHEME_V->code = caar (x); 4284 SCHEME_V->code = caar (x);
3916 s_goto (OP_EVAL); 4285 s_goto (OP_EVAL);
3917 } 4286 }
3918 } 4287 }
3919 else 4288
3920 s_return (NIL); 4289 s_return (NIL);
3921 4290
3922 case OP_CASE2: /* case */ 4291 case OP_CASE2: /* case */
3923 if (is_true (SCHEME_V->value)) 4292 if (is_true (SCHEME_V->value))
3924 s_goto (OP_BEGIN); 4293 s_goto (OP_BEGIN);
3925 else 4294
3926 s_return (NIL); 4295 s_return (NIL);
3927 4296
3928 case OP_PAPPLY: /* apply */ 4297 case OP_PAPPLY: /* apply */
3929 SCHEME_V->code = car (args); 4298 SCHEME_V->code = car (args);
3930 SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); 4299 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3931 /*SCHEME_V->args = cadr(args); */ 4300 /*SCHEME_V->args = cadr(args); */
3945 } 4314 }
3946 4315
3947 if (USE_ERROR_CHECKING) abort (); 4316 if (USE_ERROR_CHECKING) abort ();
3948} 4317}
3949 4318
3950static int 4319/* math, cxr */
4320ecb_hot static int
3951opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4321opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3952{ 4322{
3953 pointer args = SCHEME_V->args; 4323 pointer args = SCHEME_V->args;
3954 pointer x = car (args); 4324 pointer x = car (args);
3955 num v; 4325 num v;
3956 4326
3957 switch (op) 4327 switch (op)
3958 { 4328 {
3959#if USE_MATH 4329#if USE_MATH
3960 case OP_INEX2EX: /* inexact->exact */ 4330 case OP_INEX2EX: /* inexact->exact */
3961 {
3962 if (is_integer (x)) 4331 if (!is_integer (x))
3963 s_return (x); 4332 {
3964
3965 RVALUE r = rvalue_unchecked (x); 4333 RVALUE r = rvalue_unchecked (x);
3966 4334
3967 if (r == (RVALUE)(IVALUE)r) 4335 if (r == (RVALUE)(IVALUE)r)
3968 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); 4336 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
3969 else 4337 else
3970 Error_1 ("inexact->exact: not integral:", x); 4338 Error_1 ("inexact->exact: not integral:", x);
3971 } 4339 }
3972 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))));
3973 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))));
3974 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))))));
3975 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))));
3976 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))));
3977 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))));
3978 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))));
3979 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))));
3980 4357
3981 case OP_ATAN: 4358 case OP_ATAN:
4359 s_return (mk_real (SCHEME_A_
3982 if (cdr (args) == NIL) 4360 cdr (args) == NIL
3983 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4361 ? atan (rvalue (x))
3984 else 4362 : atan2 (rvalue (x), rvalue (cadr (args)))));
3985 {
3986 pointer y = cadr (args);
3987 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
3988 }
3989
3990 case OP_SQRT:
3991 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
3992 4363
3993 case OP_EXPT: 4364 case OP_EXPT:
3994 { 4365 {
3995 RVALUE result; 4366 RVALUE result;
3996 int real_result = 1; 4367 int real_result = 1;
4019 if (real_result) 4390 if (real_result)
4020 s_return (mk_real (SCHEME_A_ result)); 4391 s_return (mk_real (SCHEME_A_ result));
4021 else 4392 else
4022 s_return (mk_integer (SCHEME_A_ result)); 4393 s_return (mk_integer (SCHEME_A_ result));
4023 } 4394 }
4024
4025 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4026 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4027
4028 case OP_TRUNCATE:
4029 {
4030 RVALUE n = rvalue (x);
4031 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4032 }
4033
4034 case OP_ROUND:
4035 if (is_integer (x))
4036 s_return (x);
4037
4038 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4039#endif 4395#endif
4040 4396
4041 case OP_ADD: /* + */ 4397 case OP_ADD: /* + */
4042 v = num_zero; 4398 v = num_zero;
4043 4399
4131 else 4487 else
4132 Error_0 ("modulo: division by zero"); 4488 Error_0 ("modulo: division by zero");
4133 4489
4134 s_return (mk_number (SCHEME_A_ v)); 4490 s_return (mk_number (SCHEME_A_ v));
4135 4491
4136 case OP_CAR: /* car */ 4492 /* the compiler will optimize this mess... */
4137 s_return (caar (args)); 4493 case OP_CAR: op_car: s_return (car (x));
4138 4494 case OP_CDR: op_cdr: s_return (cdr (x));
4139 case OP_CDR: /* cdr */ 4495 case OP_CAAR: op_caar: x = car (x); goto op_car;
4140 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;
4141 4523
4142 case OP_CONS: /* cons */ 4524 case OP_CONS: /* cons */
4143 set_cdr (args, cadr (args)); 4525 set_cdr (args, cadr (args));
4144 s_return (args); 4526 s_return (args);
4145 4527
4319 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4701 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4320 4702
4321 s_return (newstr); 4703 s_return (newstr);
4322 } 4704 }
4323 4705
4324 case OP_SUBSTR: /* substring */ 4706 case OP_STRING_COPY: /* substring/string-copy */
4325 { 4707 {
4326 char *str = strvalue (x); 4708 char *str = strvalue (x);
4327 int index0 = ivalue_unchecked (cadr (args)); 4709 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4328 int index1; 4710 int index1;
4329 int len; 4711 int len;
4330 4712
4331 if (index0 > strlength (x)) 4713 if (index0 > strlength (x))
4332 Error_1 ("substring: start out of bounds:", cadr (args)); 4714 Error_1 ("string->copy: start out of bounds:", cadr (args));
4333 4715
4334 if (cddr (args) != NIL) 4716 if (cddr (args) != NIL)
4335 { 4717 {
4336 index1 = ivalue_unchecked (caddr (args)); 4718 index1 = ivalue_unchecked (caddr (args));
4337 4719
4338 if (index1 > strlength (x) || index1 < index0) 4720 if (index1 > strlength (x) || index1 < index0)
4339 Error_1 ("substring: end out of bounds:", caddr (args)); 4721 Error_1 ("string->copy: end out of bounds:", caddr (args));
4340 } 4722 }
4341 else 4723 else
4342 index1 = strlength (x); 4724 index1 = strlength (x);
4343 4725
4344 len = index1 - index0; 4726 len = index1 - index0;
4345 x = mk_empty_string (SCHEME_A_ len, ' '); 4727 x = mk_counted_string (SCHEME_A_ str + index0, len);
4346 memcpy (strvalue (x), str + index0, len);
4347 strvalue (x)[len] = 0;
4348 4728
4349 s_return (x); 4729 s_return (x);
4350 } 4730 }
4351 4731
4352 case OP_VECTOR: /* vector */ 4732 case OP_VECTOR: /* vector */
4394 } 4774 }
4395 4775
4396 case OP_VECLEN: /* vector-length */ 4776 case OP_VECLEN: /* vector-length */
4397 s_return (mk_integer (SCHEME_A_ veclength (x))); 4777 s_return (mk_integer (SCHEME_A_ veclength (x)));
4398 4778
4779 case OP_VECRESIZE:
4780 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4781 s_return (x);
4782
4399 case OP_VECREF: /* vector-ref */ 4783 case OP_VECREF: /* vector-ref */
4400 { 4784 {
4401 int index = ivalue_unchecked (cadr (args)); 4785 int index = ivalue_unchecked (cadr (args));
4402 4786
4403 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4787 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4422 } 4806 }
4423 4807
4424 if (USE_ERROR_CHECKING) abort (); 4808 if (USE_ERROR_CHECKING) abort ();
4425} 4809}
4426 4810
4427static int 4811/* relational ops */
4812ecb_hot static int
4428opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4813opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4429{ 4814{
4430 pointer x = SCHEME_V->args; 4815 pointer x = SCHEME_V->args;
4431 4816
4432 for (;;) 4817 for (;;)
4453 } 4838 }
4454 4839
4455 s_return (S_T); 4840 s_return (S_T);
4456} 4841}
4457 4842
4458static int 4843/* predicates */
4844ecb_hot static int
4459opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4845opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4460{ 4846{
4461 pointer args = SCHEME_V->args; 4847 pointer args = SCHEME_V->args;
4462 pointer a = car (args); 4848 pointer a = car (args);
4463 pointer d = cdr (args); 4849 pointer d = cdr (args);
4464 int r; 4850 int r;
4465 4851
4466 switch (op) 4852 switch (op)
4467 { 4853 {
4468 case OP_NOT: /* not */ r = is_false (a) ; break; 4854 case OP_NOT: /* not */ r = is_false (a) ; break;
4469 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; 4855 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break;
4470 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; 4856 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4471 case OP_NULLP: /* null? */ r = a == NIL ; break; 4857 case OP_NULLP: /* null? */ r = a == NIL ; break;
4472 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; 4858 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4859 case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break;
4473 case OP_NUMBERP: /* number? */ r = is_number (a) ; break; 4860 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4474 case OP_STRINGP: /* string? */ r = is_string (a) ; break; 4861 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4475 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4862 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4476 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4863 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4477 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4864 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4478 4865
4479#if USE_CHAR_CLASSIFIERS 4866#if USE_CHAR_CLASSIFIERS
4480 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; 4867 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4481 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; 4868 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4482 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; 4869 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4509 } 4896 }
4510 4897
4511 s_retbool (r); 4898 s_retbool (r);
4512} 4899}
4513 4900
4514static int 4901/* promises, list ops, ports */
4902ecb_hot static int
4515opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4903opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4516{ 4904{
4517 pointer args = SCHEME_V->args; 4905 pointer args = SCHEME_V->args;
4518 pointer a = car (args); 4906 pointer a = car (args);
4519 pointer x, y; 4907 pointer x, y;
4532 } 4920 }
4533 else 4921 else
4534 s_return (SCHEME_V->code); 4922 s_return (SCHEME_V->code);
4535 4923
4536 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4924 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4537 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4925 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4538 s_return (SCHEME_V->value); 4926 s_return (SCHEME_V->value);
4539 4927
4540#if USE_PORTS 4928#if USE_PORTS
4929
4930 case OP_EOF_OBJECT: /* eof-object */
4931 s_return (S_EOF);
4541 4932
4542 case OP_WRITE: /* write */ 4933 case OP_WRITE: /* write */
4543 case OP_DISPLAY: /* display */ 4934 case OP_DISPLAY: /* display */
4544 case OP_WRITE_CHAR: /* write-char */ 4935 case OP_WRITE_CHAR: /* write-char */
4545 if (is_pair (cdr (SCHEME_V->args))) 4936 if (is_pair (cdr (SCHEME_V->args)))
4559 else 4950 else
4560 SCHEME_V->print_flag = 0; 4951 SCHEME_V->print_flag = 0;
4561 4952
4562 s_goto (OP_P0LIST); 4953 s_goto (OP_P0LIST);
4563 4954
4955 //TODO: move to scheme
4564 case OP_NEWLINE: /* newline */ 4956 case OP_NEWLINE: /* newline */
4565 if (is_pair (args)) 4957 if (is_pair (args))
4566 { 4958 {
4567 if (a != SCHEME_V->outport) 4959 if (a != SCHEME_V->outport)
4568 { 4960 {
4570 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4962 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4571 SCHEME_V->outport = a; 4963 SCHEME_V->outport = a;
4572 } 4964 }
4573 } 4965 }
4574 4966
4575 putstr (SCHEME_A_ "\n"); 4967 putcharacter (SCHEME_A_ '\n');
4576 s_return (S_T); 4968 s_return (S_T);
4577#endif 4969#endif
4578 4970
4579 case OP_ERR0: /* error */ 4971 case OP_ERR0: /* error */
4580 SCHEME_V->retcode = -1; 4972 SCHEME_V->retcode = -1;
4589 putstr (SCHEME_A_ strvalue (car (args))); 4981 putstr (SCHEME_A_ strvalue (car (args)));
4590 SCHEME_V->args = cdr (args); 4982 SCHEME_V->args = cdr (args);
4591 s_goto (OP_ERR1); 4983 s_goto (OP_ERR1);
4592 4984
4593 case OP_ERR1: /* error */ 4985 case OP_ERR1: /* error */
4594 putstr (SCHEME_A_ " "); 4986 putcharacter (SCHEME_A_ ' ');
4595 4987
4596 if (args != NIL) 4988 if (args != NIL)
4597 { 4989 {
4598 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); 4990 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4599 SCHEME_V->args = a; 4991 SCHEME_V->args = a;
4600 SCHEME_V->print_flag = 1; 4992 SCHEME_V->print_flag = 1;
4601 s_goto (OP_P0LIST); 4993 s_goto (OP_P0LIST);
4602 } 4994 }
4603 else 4995 else
4604 { 4996 {
4605 putstr (SCHEME_A_ "\n"); 4997 putcharacter (SCHEME_A_ '\n');
4606 4998
4607 if (SCHEME_V->interactive_repl) 4999 if (SCHEME_V->interactive_repl)
4608 s_goto (OP_T0LVL); 5000 s_goto (OP_T0LVL);
4609 else 5001 else
4610 return -1; 5002 return -1;
4687 SCHEME_V->gc_verbose = (a != S_F); 5079 SCHEME_V->gc_verbose = (a != S_F);
4688 s_retbool (was); 5080 s_retbool (was);
4689 } 5081 }
4690 5082
4691 case OP_NEWSEGMENT: /* new-segment */ 5083 case OP_NEWSEGMENT: /* new-segment */
5084#if 0
4692 if (!is_pair (args) || !is_number (a)) 5085 if (!is_pair (args) || !is_number (a))
4693 Error_0 ("new-segment: argument must be a number"); 5086 Error_0 ("new-segment: argument must be a number");
4694 5087#endif
4695 alloc_cellseg (SCHEME_A_ ivalue (a)); 5088 s_retbool (alloc_cellseg (SCHEME_A));
4696
4697 s_return (S_T);
4698 5089
4699 case OP_OBLIST: /* oblist */ 5090 case OP_OBLIST: /* oblist */
4700 s_return (oblist_all_symbols (SCHEME_A)); 5091 s_return (oblist_all_symbols (SCHEME_A));
4701 5092
4702#if USE_PORTS 5093#if USE_PORTS
4772 s_return (p == NIL ? S_F : p); 5163 s_return (p == NIL ? S_F : p);
4773 } 5164 }
4774 5165
4775 case OP_GET_OUTSTRING: /* get-output-string */ 5166 case OP_GET_OUTSTRING: /* get-output-string */
4776 { 5167 {
4777 port *p; 5168 port *p = port (a);
4778 5169
4779 if ((p = a->object.port)->kind & port_string) 5170 if (p->kind & port_string)
4780 { 5171 {
4781 off_t size; 5172 off_t size;
4782 char *str; 5173 char *str;
4783 5174
4784 size = p->rep.string.curr - p->rep.string.start + 1; 5175 size = p->rep.string.curr - p->rep.string.start + 1;
4819 } 5210 }
4820 5211
4821 if (USE_ERROR_CHECKING) abort (); 5212 if (USE_ERROR_CHECKING) abort ();
4822} 5213}
4823 5214
4824static int 5215/* reading */
5216ecb_cold static int
4825opexe_5 (SCHEME_P_ enum scheme_opcodes op) 5217opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4826{ 5218{
4827 pointer args = SCHEME_V->args; 5219 pointer args = SCHEME_V->args;
4828 pointer x; 5220 pointer x;
4829 5221
4889 int res; 5281 int res;
4890 5282
4891 if (is_pair (args)) 5283 if (is_pair (args))
4892 p = car (args); 5284 p = car (args);
4893 5285
4894 res = p->object.port->kind & port_string; 5286 res = port (p)->kind & port_string;
4895 5287
4896 s_retbool (res); 5288 s_retbool (res);
4897 } 5289 }
4898 5290
4899 case OP_SET_INPORT: /* set-input-port */ 5291 case OP_SET_INPORT: /* set-input-port */
4908 case OP_RDSEXPR: 5300 case OP_RDSEXPR:
4909 switch (SCHEME_V->tok) 5301 switch (SCHEME_V->tok)
4910 { 5302 {
4911 case TOK_EOF: 5303 case TOK_EOF:
4912 s_return (S_EOF); 5304 s_return (S_EOF);
4913 /* NOTREACHED */
4914 5305
4915 case TOK_VEC: 5306 case TOK_VEC:
4916 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); 5307 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL);
4917 /* fall through */ 5308 /* fall through */
4918 5309
4921 5312
4922 if (SCHEME_V->tok == TOK_RPAREN) 5313 if (SCHEME_V->tok == TOK_RPAREN)
4923 s_return (NIL); 5314 s_return (NIL);
4924 else if (SCHEME_V->tok == TOK_DOT) 5315 else if (SCHEME_V->tok == TOK_DOT)
4925 Error_0 ("syntax error: illegal dot expression"); 5316 Error_0 ("syntax error: illegal dot expression");
4926 else 5317
4927 {
4928 SCHEME_V->nesting_stack[SCHEME_V->file_i]++; 5318 SCHEME_V->nesting_stack[SCHEME_V->file_i]++;
4929 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); 5319 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL);
4930 s_goto (OP_RDSEXPR); 5320 s_goto (OP_RDSEXPR);
4931 }
4932 5321
4933 case TOK_QUOTE: 5322 case TOK_QUOTE:
4934 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); 5323 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL);
4935 SCHEME_V->tok = token (SCHEME_A); 5324 SCHEME_V->tok = token (SCHEME_A);
4936 s_goto (OP_RDSEXPR); 5325 s_goto (OP_RDSEXPR);
4942 { 5331 {
4943 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); 5332 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL);
4944 SCHEME_V->tok = TOK_LPAREN; 5333 SCHEME_V->tok = TOK_LPAREN;
4945 s_goto (OP_RDSEXPR); 5334 s_goto (OP_RDSEXPR);
4946 } 5335 }
4947 else 5336
4948 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); 5337 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL);
4949
4950 s_goto (OP_RDSEXPR); 5338 s_goto (OP_RDSEXPR);
4951 5339
4952 case TOK_COMMA: 5340 case TOK_COMMA:
4953 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); 5341 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL);
4954 SCHEME_V->tok = token (SCHEME_A); 5342 SCHEME_V->tok = token (SCHEME_A);
4958 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 5346 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
4959 SCHEME_V->tok = token (SCHEME_A); 5347 SCHEME_V->tok = token (SCHEME_A);
4960 s_goto (OP_RDSEXPR); 5348 s_goto (OP_RDSEXPR);
4961 5349
4962 case TOK_ATOM: 5350 case TOK_ATOM:
4963 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 5351 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
5352
5353 case TOK_DOTATOM:
5354 SCHEME_V->strbuff[0] = '.';
5355 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5356
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
5360 x = readstrexp (SCHEME_A_ '|');
5361 s_return (mk_atom (SCHEME_A_ strvalue (x)));
4964 5362
4965 case TOK_DQUOTE: 5363 case TOK_DQUOTE:
4966 x = readstrexp (SCHEME_A); 5364 x = readstrexp (SCHEME_A_ '"');
4967 5365
4968 if (x == S_F) 5366 if (x == S_F)
4969 Error_0 ("Error reading string"); 5367 Error_0 ("Error reading string");
4970 5368
4971 setimmutable (x); 5369 setimmutable (x);
4975 { 5373 {
4976 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);
4977 5375
4978 if (f == NIL) 5376 if (f == NIL)
4979 Error_0 ("undefined sharp expression"); 5377 Error_0 ("undefined sharp expression");
4980 else 5378
4981 {
4982 SCHEME_V->code = cons (slot_value_in_env (f), NIL); 5379 SCHEME_V->code = cons (slot_value_in_env (f), NIL);
4983 s_goto (OP_EVAL); 5380 s_goto (OP_EVAL);
4984 }
4985 } 5381 }
4986 5382
4987 case TOK_SHARP_CONST: 5383 case TOK_SHARP_CONST:
4988 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 5384 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
4989 Error_0 ("undefined sharp expression"); 5385 Error_0 ("undefined sharp expression");
4990 else 5386
4991 s_return (x); 5387 s_return (x);
4992 5388
4993 default: 5389 default:
4994 Error_0 ("syntax error: illegal token"); 5390 Error_0 ("syntax error: illegal token");
4995 } 5391 }
4996 5392
5089 pointer b = cdr (args); 5485 pointer b = cdr (args);
5090 int ok_abbr = ok_abbrev (b); 5486 int ok_abbr = ok_abbrev (b);
5091 SCHEME_V->args = car (b); 5487 SCHEME_V->args = car (b);
5092 5488
5093 if (a == SCHEME_V->QUOTE && ok_abbr) 5489 if (a == SCHEME_V->QUOTE && ok_abbr)
5094 putstr (SCHEME_A_ "'"); 5490 putcharacter (SCHEME_A_ '\'');
5095 else if (a == SCHEME_V->QQUOTE && ok_abbr) 5491 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5096 putstr (SCHEME_A_ "`"); 5492 putcharacter (SCHEME_A_ '`');
5097 else if (a == SCHEME_V->UNQUOTE && ok_abbr) 5493 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5098 putstr (SCHEME_A_ ","); 5494 putcharacter (SCHEME_A_ ',');
5099 else if (a == SCHEME_V->UNQUOTESP && ok_abbr) 5495 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5100 putstr (SCHEME_A_ ",@"); 5496 putstr (SCHEME_A_ ",@");
5101 else 5497 else
5102 { 5498 {
5103 putstr (SCHEME_A_ "("); 5499 putcharacter (SCHEME_A_ '(');
5104 s_save (SCHEME_A_ OP_P1LIST, b, NIL); 5500 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5105 SCHEME_V->args = a; 5501 SCHEME_V->args = a;
5106 } 5502 }
5107 5503
5108 s_goto (OP_P0LIST); 5504 s_goto (OP_P0LIST);
5110 5506
5111 case OP_P1LIST: 5507 case OP_P1LIST:
5112 if (is_pair (args)) 5508 if (is_pair (args))
5113 { 5509 {
5114 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); 5510 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5115 putstr (SCHEME_A_ " "); 5511 putcharacter (SCHEME_A_ ' ');
5116 SCHEME_V->args = car (args); 5512 SCHEME_V->args = car (args);
5117 s_goto (OP_P0LIST); 5513 s_goto (OP_P0LIST);
5118 } 5514 }
5119 else if (is_vector (args)) 5515 else if (is_vector (args))
5120 { 5516 {
5128 { 5524 {
5129 putstr (SCHEME_A_ " . "); 5525 putstr (SCHEME_A_ " . ");
5130 printatom (SCHEME_A_ args, SCHEME_V->print_flag); 5526 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5131 } 5527 }
5132 5528
5133 putstr (SCHEME_A_ ")"); 5529 putcharacter (SCHEME_A_ ')');
5134 s_return (S_T); 5530 s_return (S_T);
5135 } 5531 }
5136 5532
5137 case OP_PVECFROM: 5533 case OP_PVECFROM:
5138 { 5534 {
5140 pointer vec = car (args); 5536 pointer vec = car (args);
5141 int len = veclength (vec); 5537 int len = veclength (vec);
5142 5538
5143 if (i == len) 5539 if (i == len)
5144 { 5540 {
5145 putstr (SCHEME_A_ ")"); 5541 putcharacter (SCHEME_A_ ')');
5146 s_return (S_T); 5542 s_return (S_T);
5147 } 5543 }
5148 else 5544 else
5149 { 5545 {
5150 pointer elem = vector_get (vec, i); 5546 pointer elem = vector_get (vec, i);
5152 ivalue_unchecked (cdr (args)) = i + 1; 5548 ivalue_unchecked (cdr (args)) = i + 1;
5153 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5549 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5154 SCHEME_V->args = elem; 5550 SCHEME_V->args = elem;
5155 5551
5156 if (i > 0) 5552 if (i > 0)
5157 putstr (SCHEME_A_ " "); 5553 putcharacter (SCHEME_A_ ' ');
5158 5554
5159 s_goto (OP_P0LIST); 5555 s_goto (OP_P0LIST);
5160 } 5556 }
5161 } 5557 }
5162 } 5558 }
5163 5559
5164 if (USE_ERROR_CHECKING) abort (); 5560 if (USE_ERROR_CHECKING) abort ();
5165} 5561}
5166 5562
5167static int 5563/* list ops */
5564ecb_hot static int
5168opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5565opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5169{ 5566{
5170 pointer args = SCHEME_V->args; 5567 pointer args = SCHEME_V->args;
5171 pointer a = car (args); 5568 pointer a = car (args);
5172 pointer x, y; 5569 pointer x, y;
5195 break; 5592 break;
5196 } 5593 }
5197 5594
5198 if (is_pair (y)) 5595 if (is_pair (y))
5199 s_return (car (y)); 5596 s_return (car (y));
5200 else 5597
5201 s_return (S_F); 5598 s_return (S_F);
5202
5203 5599
5204 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5600 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5205 SCHEME_V->args = a; 5601 SCHEME_V->args = a;
5206 5602
5207 if (SCHEME_V->args == NIL) 5603 if (SCHEME_V->args == NIL)
5208 s_return (S_F); 5604 s_return (S_F);
5209 else if (is_closure (SCHEME_V->args)) 5605 else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args))
5210 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5606 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5211 else if (is_macro (SCHEME_V->args)) 5607
5212 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5213 else
5214 s_return (S_F); 5608 s_return (S_F);
5215 5609
5216 case OP_CLOSUREP: /* closure? */ 5610 case OP_CLOSUREP: /* closure? */
5217 /* 5611 /*
5218 * Note, macro object is also a closure. 5612 * Note, macro object is also a closure.
5219 * Therefore, (closure? <#MACRO>) ==> #t 5613 * Therefore, (closure? <#MACRO>) ==> #t
5614 * (schmorp) well, obviously not, fix? TODO
5220 */ 5615 */
5221 s_retbool (is_closure (a)); 5616 s_retbool (is_closure (a));
5222 5617
5223 case OP_MACROP: /* macro? */ 5618 case OP_MACROP: /* macro? */
5224 s_retbool (is_macro (a)); 5619 s_retbool (is_macro (a));
5229 5624
5230/* 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 */
5231typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5626typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5232 5627
5233typedef int (*test_predicate)(pointer); 5628typedef int (*test_predicate)(pointer);
5234static int 5629
5630ecb_hot static int
5235tst_any (pointer p) 5631tst_any (pointer p)
5236{ 5632{
5237 return 1; 5633 return 1;
5238} 5634}
5239 5635
5240static int 5636ecb_hot static int
5241tst_inonneg (pointer p) 5637tst_inonneg (pointer p)
5242{ 5638{
5243 return is_integer (p) && ivalue_unchecked (p) >= 0; 5639 return is_integer (p) && ivalue_unchecked (p) >= 0;
5244} 5640}
5245 5641
5246static int 5642ecb_hot static int
5247tst_is_list (SCHEME_P_ pointer p) 5643tst_is_list (SCHEME_P_ pointer p)
5248{ 5644{
5249 return p == NIL || is_pair (p); 5645 return p == NIL || is_pair (p);
5250} 5646}
5251 5647
5294#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" 5690#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5295#include "opdefines.h" 5691#include "opdefines.h"
5296#undef OP_DEF 5692#undef OP_DEF
5297; 5693;
5298 5694
5299static const char * 5695ecb_cold static const char *
5300opname (int idx) 5696opname (int idx)
5301{ 5697{
5302 const char *name = opnames; 5698 const char *name = opnames;
5303 5699
5304 /* 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? */
5306 name += strlen (name) + 1; 5702 name += strlen (name) + 1;
5307 5703
5308 return *name ? name : "ILLEGAL"; 5704 return *name ? name : "ILLEGAL";
5309} 5705}
5310 5706
5311static const char * 5707ecb_cold static const char *
5312procname (pointer x) 5708procname (pointer x)
5313{ 5709{
5314 return opname (procnum (x)); 5710 return opname (procnum (x));
5315} 5711}
5316 5712
5336#undef OP_DEF 5732#undef OP_DEF
5337 {0} 5733 {0}
5338}; 5734};
5339 5735
5340/* kernel of this interpreter */ 5736/* kernel of this interpreter */
5341static void ecb_hot 5737ecb_hot static void
5342Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5738Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5343{ 5739{
5344 SCHEME_V->op = op; 5740 SCHEME_V->op = op;
5345 5741
5346 for (;;) 5742 for (;;)
5429 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))
5430 return; 5826 return;
5431 5827
5432 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5828 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5433 { 5829 {
5434 xwrstr ("No memory!\n"); 5830 putstr (SCHEME_A_ "No memory!\n");
5435 return; 5831 return;
5436 } 5832 }
5437 } 5833 }
5438} 5834}
5439 5835
5440/* ========== Initialization of internal keywords ========== */ 5836/* ========== Initialization of internal keywords ========== */
5441 5837
5442static void 5838ecb_cold static void
5443assign_syntax (SCHEME_P_ const char *name) 5839assign_syntax (SCHEME_P_ const char *name)
5444{ 5840{
5445 pointer x = oblist_add_by_name (SCHEME_A_ name); 5841 pointer x = oblist_add_by_name (SCHEME_A_ name);
5446 set_typeflag (x, typeflag (x) | T_SYNTAX); 5842 set_typeflag (x, typeflag (x) | T_SYNTAX);
5447} 5843}
5448 5844
5449static void 5845ecb_cold static void
5450assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5846assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5451{ 5847{
5452 pointer x = mk_symbol (SCHEME_A_ name); 5848 pointer x = mk_symbol (SCHEME_A_ name);
5453 pointer y = mk_proc (SCHEME_A_ op); 5849 pointer y = mk_proc (SCHEME_A_ op);
5454 new_slot_in_env (SCHEME_A_ x, y); 5850 new_slot_in_env (SCHEME_A_ x, y);
5462 ivalue_unchecked (y) = op; 5858 ivalue_unchecked (y) = op;
5463 return y; 5859 return y;
5464} 5860}
5465 5861
5466/* 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! */
5467static int 5863ecb_hot static int
5468syntaxnum (pointer p) 5864syntaxnum (pointer p)
5469{ 5865{
5470 const char *s = strvalue (car (p)); 5866 const char *s = strvalue (p);
5471 5867
5472 switch (strlength (car (p))) 5868 switch (strlength (p))
5473 { 5869 {
5474 case 2: 5870 case 2:
5475 if (s[0] == 'i') 5871 if (s[0] == 'i')
5476 return OP_IF0; /* if */ 5872 return OP_IF0; /* if */
5477 else 5873 else
5552ecb_cold int 5948ecb_cold int
5553scheme_init (SCHEME_P) 5949scheme_init (SCHEME_P)
5554{ 5950{
5555 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5951 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5556 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));
5557 5959
5558 num_set_fixnum (num_zero, 1); 5960 num_set_fixnum (num_zero, 1);
5559 num_set_ivalue (num_zero, 0); 5961 num_set_ivalue (num_zero, 0);
5560 num_set_fixnum (num_one, 1); 5962 num_set_fixnum (num_one, 1);
5561 num_set_ivalue (num_one, 1); 5963 num_set_ivalue (num_one, 1);
5573 SCHEME_V->save_inport = NIL; 5975 SCHEME_V->save_inport = NIL;
5574 SCHEME_V->loadport = NIL; 5976 SCHEME_V->loadport = NIL;
5575 SCHEME_V->nesting = 0; 5977 SCHEME_V->nesting = 0;
5576 SCHEME_V->interactive_repl = 0; 5978 SCHEME_V->interactive_repl = 0;
5577 5979
5578 if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) 5980 if (!alloc_cellseg (SCHEME_A))
5579 { 5981 {
5580#if USE_ERROR_CHECKING 5982#if USE_ERROR_CHECKING
5581 SCHEME_V->no_memory = 1; 5983 SCHEME_V->no_memory = 1;
5582 return 0; 5984 return 0;
5583#endif 5985#endif
5584 } 5986 }
5585 5987
5586 SCHEME_V->gc_verbose = 0; 5988 SCHEME_V->gc_verbose = 0;
5587 dump_stack_initialize (SCHEME_A); 5989 dump_stack_initialize (SCHEME_A);
5588 SCHEME_V->code = NIL; 5990 SCHEME_V->code = NIL;
5589 SCHEME_V->args = NIL; 5991 SCHEME_V->args = NIL;
5590 SCHEME_V->envir = NIL; 5992 SCHEME_V->envir = NIL;
5993 SCHEME_V->value = NIL;
5591 SCHEME_V->tracing = 0; 5994 SCHEME_V->tracing = 0;
5592 5995
5593 /* init NIL */ 5996 /* init NIL */
5594 set_typeflag (NIL, T_ATOM | T_MARK); 5997 set_typeflag (NIL, T_ATOM | T_MARK);
5595 set_car (NIL, NIL); 5998 set_car (NIL, NIL);
5651 6054
5652 return !SCHEME_V->no_memory; 6055 return !SCHEME_V->no_memory;
5653} 6056}
5654 6057
5655#if USE_PORTS 6058#if USE_PORTS
5656void 6059ecb_cold void
5657scheme_set_input_port_file (SCHEME_P_ int fin) 6060scheme_set_input_port_file (SCHEME_P_ int fin)
5658{ 6061{
5659 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 6062 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5660} 6063}
5661 6064
5662void 6065ecb_cold void
5663scheme_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)
5664{ 6067{
5665 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);
5666} 6069}
5667 6070
5668void 6071ecb_cold void
5669scheme_set_output_port_file (SCHEME_P_ int fout) 6072scheme_set_output_port_file (SCHEME_P_ int fout)
5670{ 6073{
5671 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 6074 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5672} 6075}
5673 6076
5674void 6077ecb_cold void
5675scheme_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)
5676{ 6079{
5677 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);
5678} 6081}
5679#endif 6082#endif
5680 6083
5681void 6084ecb_cold void
5682scheme_set_external_data (SCHEME_P_ void *p) 6085scheme_set_external_data (SCHEME_P_ void *p)
5683{ 6086{
5684 SCHEME_V->ext_data = p; 6087 SCHEME_V->ext_data = p;
5685} 6088}
5686 6089
5718 SCHEME_V->loadport = NIL; 6121 SCHEME_V->loadport = NIL;
5719 SCHEME_V->gc_verbose = 0; 6122 SCHEME_V->gc_verbose = 0;
5720 gc (SCHEME_A_ NIL, NIL); 6123 gc (SCHEME_A_ NIL, NIL);
5721 6124
5722 for (i = 0; i <= SCHEME_V->last_cell_seg; i++) 6125 for (i = 0; i <= SCHEME_V->last_cell_seg; i++)
5723 free (SCHEME_V->alloc_seg[i]); 6126 free (SCHEME_V->cell_seg[i]);
5724 6127
5725#if SHOW_ERROR_LINE 6128#if SHOW_ERROR_LINE
5726 for (i = 0; i <= SCHEME_V->file_i; i++) 6129 for (i = 0; i <= SCHEME_V->file_i; i++)
5727 {
5728 if (SCHEME_V->load_stack[i].kind & port_file) 6130 if (SCHEME_V->load_stack[i].kind & port_file)
5729 { 6131 {
5730 fname = SCHEME_V->load_stack[i].rep.stdio.filename; 6132 fname = SCHEME_V->load_stack[i].rep.stdio.filename;
5731 6133
5732 if (fname) 6134 if (fname)
5733 free (fname); 6135 free (fname);
5734 } 6136 }
5735 }
5736#endif 6137#endif
5737} 6138}
5738 6139
5739void 6140ecb_cold void
5740scheme_load_file (SCHEME_P_ int fin) 6141scheme_load_file (SCHEME_P_ int fin)
5741{ 6142{
5742 scheme_load_named_file (SCHEME_A_ fin, 0); 6143 scheme_load_named_file (SCHEME_A_ fin, 0);
5743} 6144}
5744 6145
5745void 6146ecb_cold void
5746scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 6147scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5747{ 6148{
5748 dump_stack_reset (SCHEME_A); 6149 dump_stack_reset (SCHEME_A);
5749 SCHEME_V->envir = SCHEME_V->global_env; 6150 SCHEME_V->envir = SCHEME_V->global_env;
5750 SCHEME_V->file_i = 0; 6151 SCHEME_V->file_i = 0;
5751 SCHEME_V->load_stack[0].unget = -1; 6152 SCHEME_V->load_stack[0].unget = -1;
5752 SCHEME_V->load_stack[0].kind = port_input | port_file; 6153 SCHEME_V->load_stack[0].kind = port_input | port_file;
5753 SCHEME_V->load_stack[0].rep.stdio.file = fin; 6154 SCHEME_V->load_stack[0].rep.stdio.file = fin;
5754#if USE_PORTS
5755 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6155 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5756#endif
5757 SCHEME_V->retcode = 0; 6156 SCHEME_V->retcode = 0;
5758 6157
5759#if USE_PORTS
5760 if (fin == STDIN_FILENO) 6158 if (fin == STDIN_FILENO)
5761 SCHEME_V->interactive_repl = 1; 6159 SCHEME_V->interactive_repl = 1;
5762#endif
5763 6160
5764#if USE_PORTS 6161#if USE_PORTS
5765#if SHOW_ERROR_LINE 6162#if SHOW_ERROR_LINE
5766 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; 6163 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0;
5767 6164
5771#endif 6168#endif
5772 6169
5773 SCHEME_V->inport = SCHEME_V->loadport; 6170 SCHEME_V->inport = SCHEME_V->loadport;
5774 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6171 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5775 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6172 Eval_Cycle (SCHEME_A_ OP_T0LVL);
6173
5776 set_typeflag (SCHEME_V->loadport, T_ATOM); 6174 set_typeflag (SCHEME_V->loadport, T_ATOM);
5777 6175
5778 if (SCHEME_V->retcode == 0) 6176 if (SCHEME_V->retcode == 0)
5779 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6177 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5780} 6178}
5781 6179
5782void 6180ecb_cold void
5783scheme_load_string (SCHEME_P_ const char *cmd) 6181scheme_load_string (SCHEME_P_ const char *cmd)
5784{ 6182{
6183#if USE_PORTs
5785 dump_stack_reset (SCHEME_A); 6184 dump_stack_reset (SCHEME_A);
5786 SCHEME_V->envir = SCHEME_V->global_env; 6185 SCHEME_V->envir = SCHEME_V->global_env;
5787 SCHEME_V->file_i = 0; 6186 SCHEME_V->file_i = 0;
5788 SCHEME_V->load_stack[0].kind = port_input | port_string; 6187 SCHEME_V->load_stack[0].kind = port_input | port_string;
5789 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 */
5790 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);
5791 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; 6190 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5792#if USE_PORTS
5793 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6191 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5794#endif
5795 SCHEME_V->retcode = 0; 6192 SCHEME_V->retcode = 0;
5796 SCHEME_V->interactive_repl = 0; 6193 SCHEME_V->interactive_repl = 0;
5797 SCHEME_V->inport = SCHEME_V->loadport; 6194 SCHEME_V->inport = SCHEME_V->loadport;
5798 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6195 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5799 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6196 Eval_Cycle (SCHEME_A_ OP_T0LVL);
5800 set_typeflag (SCHEME_V->loadport, T_ATOM); 6197 set_typeflag (SCHEME_V->loadport, T_ATOM);
5801 6198
5802 if (SCHEME_V->retcode == 0) 6199 if (SCHEME_V->retcode == 0)
5803 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6200 SCHEME_V->retcode = SCHEME_V->nesting != 0;
6201#else
6202 abort ();
6203#endif
5804} 6204}
5805 6205
5806void 6206ecb_cold void
5807scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 6207scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5808{ 6208{
5809 pointer x; 6209 pointer x;
5810 6210
5811 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6211 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5816 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6216 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5817} 6217}
5818 6218
5819#if !STANDALONE 6219#if !STANDALONE
5820 6220
5821void 6221ecb_cold void
5822scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6222scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5823{ 6223{
5824 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));
5825} 6225}
5826 6226
5827void 6227ecb_cold void
5828scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6228scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5829{ 6229{
5830 int i; 6230 int i;
5831 6231
5832 for (i = 0; i < count; i++) 6232 for (i = 0; i < count; i++)
5833 scheme_register_foreign_func (SCHEME_A_ list + i); 6233 scheme_register_foreign_func (SCHEME_A_ list + i);
5834} 6234}
5835 6235
5836pointer 6236ecb_cold pointer
5837scheme_apply0 (SCHEME_P_ const char *procname) 6237scheme_apply0 (SCHEME_P_ const char *procname)
5838{ 6238{
5839 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));
5840} 6240}
5841 6241
5842void 6242ecb_cold void
5843save_from_C_call (SCHEME_P) 6243save_from_C_call (SCHEME_P)
5844{ 6244{
5845 pointer saved_data = cons (car (S_SINK), 6245 pointer saved_data = cons (car (S_SINK),
5846 cons (SCHEME_V->envir, 6246 cons (SCHEME_V->envir,
5847 SCHEME_V->dump)); 6247 SCHEME_V->dump));
5851 /* 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
5852 directly resume pre-C-call operations. */ 6252 directly resume pre-C-call operations. */
5853 dump_stack_reset (SCHEME_A); 6253 dump_stack_reset (SCHEME_A);
5854} 6254}
5855 6255
5856void 6256ecb_cold void
5857restore_from_C_call (SCHEME_P) 6257restore_from_C_call (SCHEME_P)
5858{ 6258{
5859 set_car (S_SINK, caar (SCHEME_V->c_nest)); 6259 set_car (S_SINK, caar (SCHEME_V->c_nest));
5860 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 6260 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
5861 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 6261 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
5862 /* Pop */ 6262 /* Pop */
5863 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 6263 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
5864} 6264}
5865 6265
5866/* "func" and "args" are assumed to be already eval'ed. */ 6266/* "func" and "args" are assumed to be already eval'ed. */
5867pointer 6267ecb_cold pointer
5868scheme_call (SCHEME_P_ pointer func, pointer args) 6268scheme_call (SCHEME_P_ pointer func, pointer args)
5869{ 6269{
5870 int old_repl = SCHEME_V->interactive_repl; 6270 int old_repl = SCHEME_V->interactive_repl;
5871 6271
5872 SCHEME_V->interactive_repl = 0; 6272 SCHEME_V->interactive_repl = 0;
5879 SCHEME_V->interactive_repl = old_repl; 6279 SCHEME_V->interactive_repl = old_repl;
5880 restore_from_C_call (SCHEME_A); 6280 restore_from_C_call (SCHEME_A);
5881 return SCHEME_V->value; 6281 return SCHEME_V->value;
5882} 6282}
5883 6283
5884pointer 6284ecb_cold pointer
5885scheme_eval (SCHEME_P_ pointer obj) 6285scheme_eval (SCHEME_P_ pointer obj)
5886{ 6286{
5887 int old_repl = SCHEME_V->interactive_repl; 6287 int old_repl = SCHEME_V->interactive_repl;
5888 6288
5889 SCHEME_V->interactive_repl = 0; 6289 SCHEME_V->interactive_repl = 0;
5901 6301
5902/* ========== Main ========== */ 6302/* ========== Main ========== */
5903 6303
5904#if STANDALONE 6304#if STANDALONE
5905 6305
5906int 6306ecb_cold int
5907main (int argc, char **argv) 6307main (int argc, char **argv)
5908{ 6308{
5909# if USE_MULTIPLICITY 6309# if USE_MULTIPLICITY
5910 scheme ssc; 6310 scheme ssc;
5911 scheme *const SCHEME_V = &ssc; 6311 scheme *const SCHEME_V = &ssc;
5913# endif 6313# endif
5914 int fin; 6314 int fin;
5915 char *file_name = InitFile; 6315 char *file_name = InitFile;
5916 int retcode; 6316 int retcode;
5917 int isfile = 1; 6317 int isfile = 1;
6318#if EXPERIMENT
6319 system ("ps v $PPID");
6320#endif
5918 6321
5919 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6322 if (argc == 2 && strcmp (argv[1], "-?") == 0)
5920 { 6323 {
5921 xwrstr ("Usage: tinyscheme -?\n"); 6324 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
5922 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6325 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
5923 xwrstr ("followed by\n"); 6326 putstr (SCHEME_A_ "followed by\n");
5924 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6327 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
5925 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6328 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
5926 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6329 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
5927 xwrstr ("Use - as filename for stdin.\n"); 6330 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
5928 return 1; 6331 return 1;
5929 } 6332 }
5930 6333
5931 if (!scheme_init (SCHEME_A)) 6334 if (!scheme_init (SCHEME_A))
5932 { 6335 {
5933 xwrstr ("Could not initialize!\n"); 6336 putstr (SCHEME_A_ "Could not initialize!\n");
5934 return 2; 6337 return 2;
5935 } 6338 }
5936 6339
5937# if USE_PORTS 6340# if USE_PORTS
5938 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6341 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
5951 } 6354 }
5952#endif 6355#endif
5953 6356
5954 do 6357 do
5955 { 6358 {
5956#if USE_PORTS
5957 if (strcmp (file_name, "-") == 0) 6359 if (strcmp (file_name, "-") == 0)
5958 fin = STDIN_FILENO; 6360 fin = STDIN_FILENO;
5959 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)
5960 { 6362 {
5961 pointer args = NIL; 6363 pointer args = NIL;
5979 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);
5980 6382
5981 } 6383 }
5982 else 6384 else
5983 fin = open (file_name, O_RDONLY); 6385 fin = open (file_name, O_RDONLY);
5984#endif
5985 6386
5986 if (isfile && fin < 0) 6387 if (isfile && fin < 0)
5987 { 6388 {
5988 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');
5989 } 6392 }
5990 else 6393 else
5991 { 6394 {
5992 if (isfile) 6395 if (isfile)
5993 scheme_load_named_file (SCHEME_A_ fin, file_name); 6396 scheme_load_named_file (SCHEME_A_ fin, file_name);
5994 else 6397 else
5995 scheme_load_string (SCHEME_A_ file_name); 6398 scheme_load_string (SCHEME_A_ file_name);
5996 6399
5997#if USE_PORTS
5998 if (!isfile || fin != STDIN_FILENO) 6400 if (!isfile || fin != STDIN_FILENO)
5999 { 6401 {
6000 if (SCHEME_V->retcode != 0) 6402 if (SCHEME_V->retcode != 0)
6001 { 6403 {
6002 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');
6003 } 6407 }
6004 6408
6005 if (isfile) 6409 if (isfile)
6006 close (fin); 6410 close (fin);
6007 } 6411 }
6008#endif
6009 } 6412 }
6010 6413
6011 file_name = *argv++; 6414 file_name = *argv++;
6012 } 6415 }
6013 while (file_name != 0); 6416 while (file_name != 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines