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.1 by root, Wed Nov 25 05:02:56 2015 UTC vs.
Revision 1.69 by root, Mon Dec 7 22:12:53 2015 UTC

1 1/*
2/* T I N Y S C H E M E 1 . 4 1 2 * µscheme
3 *
4 * Copyright (C) 2015 Marc Alexander Lehmann <uscheme@schmorp.de>
5 * do as you want with this, attribution appreciated.
6 *
7 * Based opn tinyscheme-1.41 (original credits follow)
3 * Dimitrios Souflis (dsouflis@acm.org) 8 * Dimitrios Souflis (dsouflis@acm.org)
4 * Based on MiniScheme (original credits follow) 9 * Based on MiniScheme (original credits follow)
5 * (MINISCM) coded by Atsushi Moriwaki (11/5/1989) 10 * (MINISCM) coded by Atsushi Moriwaki (11/5/1989)
6 * (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp 11 * (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp
7 * (MINISCM) This version has been modified by R.C. Secrist. 12 * (MINISCM) This version has been modified by R.C. Secrist.
11 * (MINISCM) This is a revised and modified version by Akira KIDA. 16 * (MINISCM) This is a revised and modified version by Akira KIDA.
12 * (MINISCM) current version is 0.85k4 (15 May 1994) 17 * (MINISCM) current version is 0.85k4 (15 May 1994)
13 * 18 *
14 */ 19 */
15 20
16#define PAGE_SIZE 4096 /* does not work on sparc/alpha */ 21#define _POSIX_C_SOURCE 200201
17#include "malloc.c" 22#define _XOPEN_SOURCE 600
23#define _GNU_SOURCE 1 /* for malloc mremap */
18 24
19#define SCHEME_SOURCE 25#define SCHEME_SOURCE
20#include "scheme-private.h" 26#include "scheme-private.h"
21#ifndef WIN32 27#ifndef WIN32
22# include <unistd.h> 28# include <unistd.h>
23#endif 29#endif
24#if USE_MATH 30#if USE_MATH
25# include <math.h> 31# include <math.h>
26#endif 32#endif
27 33
34#define ECB_NO_THREADS 1
35#include "ecb.h"
36
28#include <sys/types.h> 37#include <sys/types.h>
29#include <sys/stat.h> 38#include <sys/stat.h>
30#include <fcntl.h> 39#include <fcntl.h>
31 40
41#if !USE_ERROR_CHECKING
42# define NDEBUG
43#endif
44
45#include <assert.h>
46#include <stdlib.h>
47#include <string.h>
48
32#include <limits.h> 49#include <limits.h>
33#include <inttypes.h> 50#include <inttypes.h>
34#include <float.h> 51#include <float.h>
35//#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
36 68
37enum { 69enum {
38 TOK_EOF, 70 TOK_EOF,
39 TOK_LPAREN, 71 TOK_LPAREN,
40 TOK_RPAREN, 72 TOK_RPAREN,
41 TOK_DOT, 73 TOK_DOT,
42 TOK_ATOM, 74 TOK_ATOM,
75 TOK_DOTATOM, /* atom name starting with '.' */
76 TOK_STRATOM, /* atom name enclosed in | */
43 TOK_QUOTE, 77 TOK_QUOTE,
44 TOK_DQUOTE, 78 TOK_DQUOTE,
45 TOK_BQUOTE, 79 TOK_BQUOTE,
46 TOK_COMMA, 80 TOK_COMMA,
47 TOK_ATMARK, 81 TOK_ATMARK,
49 TOK_SHARP_CONST, 83 TOK_SHARP_CONST,
50 TOK_VEC 84 TOK_VEC
51}; 85};
52 86
53#define BACKQUOTE '`' 87#define BACKQUOTE '`'
54#define DELIMITERS "()\";\f\t\v\n\r " 88#define WHITESPACE " \t\r\n\v\f"
89#define DELIMITERS "()\";" WHITESPACE
55 90
56#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 91#define NIL POINTER (&SCHEME_V->xNIL)
57#define S_T (&SCHEME_V->xT) 92#define S_T POINTER (&SCHEME_V->xT)
58#define S_F (&SCHEME_V->xF) 93#define S_F POINTER (&SCHEME_V->xF)
59#define S_SINK (&SCHEME_V->xsink) 94#define S_SINK POINTER (&SCHEME_V->xsink)
60#define S_EOF (&SCHEME_V->xEOF_OBJ) 95#define S_EOF POINTER (&SCHEME_V->xEOF_OBJ)
61
62/*
63 * Basic memory allocation units
64 */
65
66#define banner "TinyScheme 1.41-s"
67
68#include <string.h>
69#include <stdlib.h>
70 96
71#if !USE_MULTIPLICITY 97#if !USE_MULTIPLICITY
72static scheme sc; 98static scheme sc;
73#endif 99#endif
74 100
75static void 101ecb_cold static void
76xbase (char *s, long n, int base) 102xbase (char *s, long n, int base)
77{ 103{
78 if (n < 0) 104 if (n < 0)
79 { 105 {
80 *s++ = '-'; 106 *s++ = '-';
82 } 108 }
83 109
84 char *p = s; 110 char *p = s;
85 111
86 do { 112 do {
87 *p++ = '0' + n % base; 113 *p++ = "0123456789abcdef"[n % base];
88 n /= base; 114 n /= base;
89 } while (n); 115 } while (n);
90 116
91 *p-- = 0; 117 *p-- = 0;
92 118
95 char x = *s; *s = *p; *p = x; 121 char x = *s; *s = *p; *p = x;
96 --p; ++s; 122 --p; ++s;
97 } 123 }
98} 124}
99 125
100static void 126ecb_cold static void
101xnum (char *s, long n) 127xnum (char *s, long n)
102{ 128{
103 xbase (s, n, 10); 129 xbase (s, n, 10);
104} 130}
105 131
106static void 132ecb_cold static void
107xwrstr (const char *s) 133putnum (SCHEME_P_ long n)
108{
109 write (1, s, strlen (s));
110}
111
112static void
113xwrnum (long n)
114{ 134{
115 char buf[64]; 135 char buf[64];
116 136
117 xnum (buf, n); 137 xnum (buf, n);
118 xwrstr (buf); 138 putstr (SCHEME_A_ buf);
119} 139}
140
141#if USE_CHAR_CLASSIFIERS
142#include <ctype.h>
143#else
120 144
121static char 145static char
122xtoupper (char c) 146xtoupper (char c)
123{ 147{
124 if (c >= 'a' && c <= 'z') 148 if (c >= 'a' && c <= 'z')
134 c += 'a' - 'A'; 158 c += 'a' - 'A';
135 159
136 return c; 160 return c;
137} 161}
138 162
139#if USE_STRLWR 163static int
164xisdigit (char c)
165{
166 return c >= '0' && c <= '9';
167}
168
169#define toupper(c) xtoupper (c)
170#define tolower(c) xtolower (c)
171#define isdigit(c) xisdigit (c)
172
173#endif
174
175#if USE_IGNORECASE
140static const char * 176ecb_cold static const char *
141strlwr (char *s) 177xstrlwr (char *s)
142{ 178{
143 const char *p = s; 179 const char *p = s;
144 180
145 while (*s) 181 while (*s)
146 { 182 {
148 s++; 184 s++;
149 } 185 }
150 186
151 return p; 187 return p;
152} 188}
153#endif
154 189
190#define stricmp(a,b) strcasecmp (a, b)
191#define strlwr(s) xstrlwr (s)
192
193#else
155#define stricmp(a,b) strcmp (a, b) 194# define stricmp(a,b) strcmp (a, b)
156#define strlwr(s) (s) 195# define strlwr(s) (s)
157#define toupper(c) xtoupper(c) 196#endif
158#define tolower(c) xtolower(c)
159 197
160#ifndef prompt 198#ifndef prompt
161# define prompt "ts> " 199# define prompt "ms> "
162#endif 200#endif
163 201
164#ifndef InitFile 202#ifndef InitFile
165# define InitFile "init.scm" 203# define InitFile "init.scm"
166#endif 204#endif
167 205
168#ifndef FIRST_CELLSEGS
169# define FIRST_CELLSEGS 3
170#endif
171
172enum scheme_types 206enum scheme_types
173{ 207{
174 T_NULL, 208 T_INTEGER,
209 T_CHARACTER,
210 T_REAL,
175 T_STRING, 211 T_STRING,
176 T_NUMBER,
177 T_SYMBOL, 212 T_SYMBOL,
178 T_PROC, 213 T_PROC,
179 T_PAIR, 214 T_PAIR, /* also used for free cells */
180 T_CLOSURE, 215 T_CLOSURE,
216 T_BYTECODE, // temp
217 T_MACRO,
181 T_CONTINUATION, 218 T_CONTINUATION,
182 T_FOREIGN, 219 T_FOREIGN,
183 T_CHARACTER,
184 T_PORT, 220 T_PORT,
185 T_VECTOR, 221 T_VECTOR,
186 T_MACRO,
187 T_PROMISE, 222 T_PROMISE,
188 T_ENVIRONMENT, 223 T_ENVIRONMENT,
224 T_SPECIAL, // #t, #f, '(), eof-object
225
189 T_NUM_SYSTEM_TYPES 226 T_NUM_SYSTEM_TYPES
190}; 227};
191 228
192#define T_MASKTYPE 31 /* 0000000000011111 */ 229#define T_MASKTYPE 0x001f
193#define T_SYNTAX 4096 /* 0001000000000000 */ 230#define T_SYNTAX 0x0020
194#define T_IMMUTABLE 8192 /* 0010000000000000 */ 231#define T_IMMUTABLE 0x0040
195#define T_ATOM 16384 /* 0100000000000000 */ /* only for gc */ 232#define T_ATOM 0x0080 /* only for gc */
196#define CLRATOM 49151 /* 1011111111111111 */ /* only for gc */ 233//#define T_MARK 0x0080 /* only for gc */
197#define MARK 32768 /* 1000000000000000 */
198#define UNMARK 32767 /* 0111111111111111 */
199 234
235/* num, for generic arithmetic */
236struct num
237{
238 IVALUE ivalue;
239#if USE_REAL
240 RVALUE rvalue;
241 char is_fixnum;
242#endif
243};
200 244
201static num num_add (num a, num b); 245#if USE_REAL
202static num num_mul (num a, num b); 246# define num_is_fixnum(n) (n).is_fixnum
203static num num_div (num a, num b); 247# define num_set_fixnum(n,f) (n).is_fixnum = (f)
248# define num_ivalue(n) (n).ivalue
249# define num_rvalue(n) (n).rvalue
250# define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i)
251# define num_set_rvalue(n,r) (n).rvalue = (r)
252#else
253# define num_is_fixnum(n) 1
254# define num_set_fixnum(n,f) 0
255# define num_ivalue(n) (n).ivalue
256# define num_rvalue(n) (n).ivalue
257# define num_set_ivalue(n,i) (n).ivalue = (i)
258# define num_set_rvalue(n,r) (n).ivalue = (r)
259#endif
260
261enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
262
263static num num_op (enum num_op op, num a, num b);
204static num num_intdiv (num a, num b); 264static num num_intdiv (num a, num b);
205static num num_sub (num a, num b);
206static num num_rem (num a, num b); 265static num num_rem (num a, num b);
207static num num_mod (num a, num b); 266static num num_mod (num a, num b);
208static int num_eq (num a, num b);
209static int num_gt (num a, num b);
210static int num_ge (num a, num b);
211static int num_lt (num a, num b);
212static int num_le (num a, num b);
213 267
214#if USE_MATH
215static double round_per_R5RS (double x);
216#endif
217static int is_zero_rvalue (RVALUE x); 268static int is_zero_rvalue (RVALUE x);
218
219static INLINE int
220num_is_integer (pointer p)
221{
222 return num_is_fixnum (p->object.number);
223}
224 269
225static num num_zero; 270static num num_zero;
226static num num_one; 271static num num_one;
227 272
273/* convert "pointer" to cell* / cell* to pointer */
274#define CELL(p) ((struct cell *)(p) + 0)
275#define POINTER(c) ((void *)((c) - 0))
276
228/* macros for cell operations */ 277/* macros for cell operations */
229#define typeflag(p) ((p)->flag + 0) 278#define typeflag(p) (CELL(p)->flag + 0)
230#define set_typeflag(p,v) ((p)->flag = (v)) 279#define set_typeflag(p,v) (CELL(p)->flag = (v))
231#define type(p) (typeflag (p) & T_MASKTYPE) 280#define type(p) (typeflag (p) & T_MASKTYPE)
232 281
233INTERFACE INLINE int 282INTERFACE int
234is_string (pointer p) 283is_string (pointer p)
235{ 284{
236 return type (p) == T_STRING; 285 return type (p) == T_STRING;
237} 286}
238 287
239#define strvalue(p) ((p)->object.string.svalue) 288#define strvalue(p) (CELL(p)->object.string.svalue)
240#define strlength(p) ((p)->object.string.length) 289#define strlength(p) (CELL(p)->object.string.length)
241 290
242INTERFACE int is_list (SCHEME_P_ pointer p);
243INTERFACE INLINE int 291INTERFACE int
244is_vector (pointer p) 292is_vector (pointer p)
245{ 293{
246 return type (p) == T_VECTOR; 294 return type (p) == T_VECTOR;
247} 295}
248 296
297#define vecvalue(p) (CELL(p)->object.vector.vvalue)
298#define veclength(p) (CELL(p)->object.vector.length)
249INTERFACE void fill_vector (pointer vec, pointer obj); 299INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
250INTERFACE pointer vector_elem (pointer vec, int ielem); 300INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
251INTERFACE void set_vector_elem (pointer vec, int ielem, pointer a); 301INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
252 302
253INTERFACE INLINE int 303INTERFACE int
304is_integer (pointer p)
305{
306 return type (p) == T_INTEGER;
307}
308
309/* not the same as in scheme, where integers are (correctly :) reals */
310INTERFACE int
311is_real (pointer p)
312{
313 return type (p) == T_REAL;
314}
315
316INTERFACE int
254is_number (pointer p) 317is_number (pointer p)
255{ 318{
256 return type (p) == T_NUMBER; 319 return is_integer (p) || is_real (p);
257} 320}
258 321
259INTERFACE INLINE int 322INTERFACE int
260is_integer (pointer p)
261{
262 if (!is_number (p))
263 return 0;
264
265 if (num_is_integer (p) || ivalue (p) == rvalue (p))
266 return 1;
267
268 return 0;
269}
270
271INTERFACE INLINE int
272is_real (pointer p)
273{
274 return is_number (p) && !num_is_fixnum (p->object.number);
275}
276
277INTERFACE INLINE int
278is_character (pointer p) 323is_character (pointer p)
279{ 324{
280 return type (p) == T_CHARACTER; 325 return type (p) == T_CHARACTER;
281} 326}
282 327
283INTERFACE INLINE char * 328INTERFACE char *
284string_value (pointer p) 329string_value (pointer p)
285{ 330{
286 return strvalue (p); 331 return strvalue (p);
287} 332}
288 333
289INLINE num
290nvalue (pointer p)
291{
292 return (p)->object.number;
293}
294
295INTERFACE long
296ivalue (pointer p)
297{
298 return num_is_integer (p) ? num_ivalue ((p)->object.number) : (long) num_rvalue ((p)->object.number);
299}
300
301INTERFACE RVALUE
302rvalue (pointer p)
303{
304 return num_is_integer (p) ? (RVALUE) num_ivalue ((p)->object.number) : num_rvalue ((p)->object.number);
305}
306
307#define ivalue_unchecked(p) ((p)->object.number.value.ivalue) 334#define ivalue_unchecked(p) (CELL(p)->object.ivalue + 0)
308#if USE_FLOAT 335#define set_ivalue(p,v) CELL(p)->object.ivalue = (v)
336
337#if USE_REAL
309# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 338#define rvalue_unchecked(p) CELL(p)->object.rvalue
310# define set_num_integer(p) (p)->object.number.is_fixnum=1; 339#define set_rvalue(p,v) CELL(p)->object.rvalue = (v)
311# define set_num_real(p) (p)->object.number.is_fixnum=0;
312#else 340#else
313# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 341#define rvalue_unchecked(p) CELL(p)->object.ivalue
314# define set_num_integer(p) 0 342#define set_rvalue(p,v) CELL(p)->object.ivalue = (v)
315# define set_num_real(p) 0
316#endif 343#endif
344
317INTERFACE long 345INTERFACE long
318charvalue (pointer p) 346charvalue (pointer p)
319{ 347{
320 return ivalue_unchecked (p); 348 return ivalue_unchecked (p);
321} 349}
322 350
351#define port(p) CELL(p)->object.port
352#define set_port(p,v) port(p) = (v)
323INTERFACE INLINE int 353INTERFACE int
324is_port (pointer p) 354is_port (pointer p)
325{ 355{
326 return type (p) == T_PORT; 356 return type (p) == T_PORT;
327} 357}
328 358
329INTERFACE INLINE int 359INTERFACE int
330is_inport (pointer p) 360is_inport (pointer p)
331{ 361{
332 return is_port (p) && p->object.port->kind & port_input; 362 return is_port (p) && port (p)->kind & port_input;
333} 363}
334 364
335INTERFACE INLINE int 365INTERFACE int
336is_outport (pointer p) 366is_outport (pointer p)
337{ 367{
338 return is_port (p) && p->object.port->kind & port_output; 368 return is_port (p) && port (p)->kind & port_output;
339} 369}
340 370
341INTERFACE INLINE int 371INTERFACE int
342is_pair (pointer p) 372is_pair (pointer p)
343{ 373{
344 return type (p) == T_PAIR; 374 return type (p) == T_PAIR;
345} 375}
346 376
347#define car(p) ((p)->object.cons.car + 0) 377#define car(p) (POINTER (CELL(p)->object.cons.car))
348#define cdr(p) ((p)->object.cons.cdr) /* find_consecutive_cells uses &cdr */ 378#define cdr(p) (POINTER (CELL(p)->object.cons.cdr))
349 379
350#define caar(p) car (car (p)) 380static pointer caar (pointer p) { return car (car (p)); }
351#define cadr(p) car (cdr (p)) 381static pointer cadr (pointer p) { return car (cdr (p)); }
352#define cdar(p) cdr (car (p)) 382static pointer cdar (pointer p) { return cdr (car (p)); }
353#define cddr(p) cdr (cdr (p)) 383static pointer cddr (pointer p) { return cdr (cdr (p)); }
354 384
355#define cadar(p) car (cdr (car (p))) 385static pointer cadar (pointer p) { return car (cdr (car (p))); }
356#define caddr(p) car (cdr (cdr (p))) 386static pointer caddr (pointer p) { return car (cdr (cdr (p))); }
357#define cdaar(p) cdr (car (car (p))) 387static pointer cdaar (pointer p) { return cdr (car (car (p))); }
388
389static pointer cadddr (pointer p) { return car (car (car (cdr (p)))); }
358 390
359INTERFACE void 391INTERFACE void
360set_car (pointer p, pointer q) 392set_car (pointer p, pointer q)
361{ 393{
362 p->object.cons.car = q; 394 CELL(p)->object.cons.car = CELL (q);
363} 395}
364 396
365INTERFACE void 397INTERFACE void
366set_cdr (pointer p, pointer q) 398set_cdr (pointer p, pointer q)
367{ 399{
368 p->object.cons.cdr = q; 400 CELL(p)->object.cons.cdr = CELL (q);
369} 401}
370 402
371INTERFACE pointer 403INTERFACE pointer
372pair_car (pointer p) 404pair_car (pointer p)
373{ 405{
378pair_cdr (pointer p) 410pair_cdr (pointer p)
379{ 411{
380 return cdr (p); 412 return cdr (p);
381} 413}
382 414
383INTERFACE INLINE int 415INTERFACE int
384is_symbol (pointer p) 416is_symbol (pointer p)
385{ 417{
386 return type (p) == T_SYMBOL; 418 return type (p) == T_SYMBOL;
387} 419}
388 420
389INTERFACE INLINE char * 421INTERFACE char *
390symname (pointer p) 422symname (pointer p)
391{ 423{
392 return strvalue (car (p)); 424 return strvalue (p);
393} 425}
394 426
395#if USE_PLIST 427#if USE_PLIST
428#error plists are broken because symbols are no longer pairs
429#define symprop(p) cdr(p)
396SCHEME_EXPORT INLINE int 430SCHEME_EXPORT int
397hasprop (pointer p) 431hasprop (pointer p)
398{ 432{
399 return typeflag (p) & T_SYMBOL; 433 return typeflag (p) & T_SYMBOL;
400} 434}
401
402# define symprop(p) cdr(p)
403#endif 435#endif
404 436
405INTERFACE INLINE int 437INTERFACE int
406is_syntax (pointer p) 438is_syntax (pointer p)
407{ 439{
408 return typeflag (p) & T_SYNTAX; 440 return typeflag (p) & T_SYNTAX;
409} 441}
410 442
411INTERFACE INLINE int 443INTERFACE int
412is_proc (pointer p) 444is_proc (pointer p)
413{ 445{
414 return type (p) == T_PROC; 446 return type (p) == T_PROC;
415} 447}
416 448
417INTERFACE INLINE int 449INTERFACE int
418is_foreign (pointer p) 450is_foreign (pointer p)
419{ 451{
420 return type (p) == T_FOREIGN; 452 return type (p) == T_FOREIGN;
421} 453}
422 454
423INTERFACE INLINE char * 455INTERFACE char *
424syntaxname (pointer p) 456syntaxname (pointer p)
425{ 457{
426 return strvalue (car (p)); 458 return strvalue (p);
427} 459}
428 460
429#define procnum(p) ivalue(p) 461#define procnum(p) ivalue_unchecked (p)
430static const char *procname (pointer x); 462static const char *procname (pointer x);
431 463
432INTERFACE INLINE int 464INTERFACE int
433is_closure (pointer p) 465is_closure (pointer p)
434{ 466{
435 return type (p) == T_CLOSURE; 467 return type (p) == T_CLOSURE;
436} 468}
437 469
438INTERFACE INLINE int 470INTERFACE int
439is_macro (pointer p) 471is_macro (pointer p)
440{ 472{
441 return type (p) == T_MACRO; 473 return type (p) == T_MACRO;
442} 474}
443 475
444INTERFACE INLINE pointer 476INTERFACE pointer
445closure_code (pointer p) 477closure_code (pointer p)
446{ 478{
447 return car (p); 479 return car (p);
448} 480}
449 481
450INTERFACE INLINE pointer 482INTERFACE pointer
451closure_env (pointer p) 483closure_env (pointer p)
452{ 484{
453 return cdr (p); 485 return cdr (p);
454} 486}
455 487
456INTERFACE INLINE int 488INTERFACE int
457is_continuation (pointer p) 489is_continuation (pointer p)
458{ 490{
459 return type (p) == T_CONTINUATION; 491 return type (p) == T_CONTINUATION;
460} 492}
461 493
462#define cont_dump(p) cdr (p) 494#define cont_dump(p) cdr (p)
463#define set_cont_dump(p,v) set_cdr ((p), (v)) 495#define set_cont_dump(p,v) set_cdr ((p), (v))
464 496
465/* To do: promise should be forced ONCE only */ 497/* To do: promise should be forced ONCE only */
466INTERFACE INLINE int 498INTERFACE int
467is_promise (pointer p) 499is_promise (pointer p)
468{ 500{
469 return type (p) == T_PROMISE; 501 return type (p) == T_PROMISE;
470} 502}
471 503
472INTERFACE INLINE int 504INTERFACE int
473is_environment (pointer p) 505is_environment (pointer p)
474{ 506{
475 return type (p) == T_ENVIRONMENT; 507 return type (p) == T_ENVIRONMENT;
476} 508}
477 509
478#define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) 510#define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT)
479 511
480#define is_atom(p) (typeflag (p) & T_ATOM) 512#define is_atom(p) (typeflag (p) & T_ATOM)
481#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) 513#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM)
482#define clratom(p) set_typeflag ((p), typeflag (p) & CLRATOM) 514#define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM)
483 515
516#if 1
517#define is_mark(p) (CELL(p)->mark)
518#define setmark(p) (CELL(p)->mark = 1)
519#define clrmark(p) (CELL(p)->mark = 0)
520#else
484#define is_mark(p) (typeflag (p) & MARK) 521#define is_mark(p) (typeflag (p) & T_MARK)
485#define setmark(p) set_typeflag ((p), typeflag (p) | MARK) 522#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
486#define clrmark(p) set_typeflag ((p), typeflag (p) & UNMARK) 523#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
524#endif
487 525
488INTERFACE INLINE int 526INTERFACE int
489is_immutable (pointer p) 527is_immutable (pointer p)
490{ 528{
491 return typeflag (p) & T_IMMUTABLE; 529 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
492} 530}
493 531
494INTERFACE INLINE void 532INTERFACE void
495setimmutable (pointer p) 533setimmutable (pointer p)
496{ 534{
535#if USE_ERROR_CHECKING
497 set_typeflag (p, T_IMMUTABLE); 536 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
537#endif
538}
539
540/* Result is:
541 proper list: length
542 circular list: -1
543 not even a pair: -2
544 dotted list: -2 minus length before dot
545*/
546ecb_hot INTERFACE int
547list_length (SCHEME_P_ pointer a)
548{
549 int i = 0;
550 pointer slow, fast;
551
552 slow = fast = a;
553
554 while (1)
555 {
556 if (fast == NIL)
557 return i;
558
559 if (!is_pair (fast))
560 return -2 - i;
561
562 fast = cdr (fast);
563 ++i;
564
565 if (fast == NIL)
566 return i;
567
568 if (!is_pair (fast))
569 return -2 - i;
570
571 ++i;
572 fast = cdr (fast);
573
574 /* Safe because we would have already returned if `fast'
575 encountered a non-pair. */
576 slow = cdr (slow);
577
578 if (fast == slow)
579 {
580 /* the fast pointer has looped back around and caught up
581 with the slow pointer, hence the structure is circular,
582 not of finite length, and therefore not a list */
583 return -1;
584 }
585 }
586}
587
588INTERFACE int
589is_list (SCHEME_P_ pointer a)
590{
591 return list_length (SCHEME_A_ a) >= 0;
498} 592}
499 593
500#if USE_CHAR_CLASSIFIERS 594#if USE_CHAR_CLASSIFIERS
501static INLINE int 595
596ecb_inline int
502Cisalpha (int c) 597Cisalpha (int c)
503{ 598{
504 return isascii (c) && isalpha (c); 599 return isascii (c) && isalpha (c);
505} 600}
506 601
507static INLINE int 602ecb_inline int
508Cisdigit (int c) 603Cisdigit (int c)
509{ 604{
510 return isascii (c) && isdigit (c); 605 return isascii (c) && isdigit (c);
511} 606}
512 607
513static INLINE int 608ecb_inline int
514Cisspace (int c) 609Cisspace (int c)
515{ 610{
516 return isascii (c) && isspace (c); 611 return isascii (c) && isspace (c);
517} 612}
518 613
519static INLINE int 614ecb_inline int
520Cisupper (int c) 615Cisupper (int c)
521{ 616{
522 return isascii (c) && isupper (c); 617 return isascii (c) && isupper (c);
523} 618}
524 619
525static INLINE int 620ecb_inline int
526Cislower (int c) 621Cislower (int c)
527{ 622{
528 return isascii (c) && islower (c); 623 return isascii (c) && islower (c);
529} 624}
530#endif 625#endif
563 "gs", 658 "gs",
564 "rs", 659 "rs",
565 "us" 660 "us"
566}; 661};
567 662
568static int 663ecb_cold static int
569is_ascii_name (const char *name, int *pc) 664is_ascii_name (const char *name, int *pc)
570{ 665{
571 int i; 666 int i;
572 667
573 for (i = 0; i < 32; i++) 668 for (i = 0; i < 32; i++)
591#endif 686#endif
592 687
593static int file_push (SCHEME_P_ const char *fname); 688static int file_push (SCHEME_P_ const char *fname);
594static void file_pop (SCHEME_P); 689static void file_pop (SCHEME_P);
595static int file_interactive (SCHEME_P); 690static int file_interactive (SCHEME_P);
596static INLINE int is_one_of (char *s, int c); 691ecb_inline int is_one_of (const char *s, int c);
597static int alloc_cellseg (SCHEME_P_ int n); 692static int alloc_cellseg (SCHEME_P);
598static long binary_decode (const char *s);
599static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b);
600static pointer xget_cell (SCHEME_P_ pointer a, pointer b); 693ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
601static pointer reserve_cells (SCHEME_P_ int n);
602static pointer get_consecutive_cells (SCHEME_P_ int n);
603static pointer find_consecutive_cells (SCHEME_P_ int n);
604static void finalize_cell (SCHEME_P_ pointer a); 694static void finalize_cell (SCHEME_P_ pointer a);
605static int count_consecutive_cells (pointer x, int needed);
606static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 695static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
607static pointer mk_number (SCHEME_P_ num n); 696static pointer mk_number (SCHEME_P_ const num n);
608static char *store_string (SCHEME_P_ int len, const char *str, char fill); 697static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
609static pointer mk_vector (SCHEME_P_ int len); 698static pointer mk_vector (SCHEME_P_ uint32_t len);
610static pointer mk_atom (SCHEME_P_ char *q); 699static pointer mk_atom (SCHEME_P_ char *q);
611static pointer mk_sharp_const (SCHEME_P_ char *name); 700static pointer mk_sharp_const (SCHEME_P_ char *name);
612 701
702static pointer mk_port (SCHEME_P_ port *p);
703
613#if USE_PORTS 704#if USE_PORTS
614static pointer mk_port (SCHEME_P_ port *p);
615static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); 705static pointer port_from_filename (SCHEME_P_ const char *fn, int prop);
616static pointer port_from_file (SCHEME_P_ int, int prop); 706static pointer port_from_file (SCHEME_P_ int, int prop);
617static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 707static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
618static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); 708static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop);
619static port *port_rep_from_file (SCHEME_P_ int, int prop); 709static port *port_rep_from_file (SCHEME_P_ int, int prop);
620static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 710static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
621static void port_close (SCHEME_P_ pointer p, int flag); 711static void port_close (SCHEME_P_ pointer p, int flag);
622#endif 712#endif
713
623static void mark (pointer a); 714static void mark (pointer a);
624static void gc (SCHEME_P_ pointer a, pointer b); 715static void gc (SCHEME_P_ pointer a, pointer b);
625static int basic_inchar (port *pt); 716static int basic_inchar (port *pt);
626static int inchar (SCHEME_P); 717static int inchar (SCHEME_P);
627static void backchar (SCHEME_P_ int c); 718static void backchar (SCHEME_P_ int c);
628static char *readstr_upto (SCHEME_P_ char *delim); 719static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
629static pointer readstrexp (SCHEME_P); 720static pointer readstrexp (SCHEME_P_ char delim);
630static INLINE int skipspace (SCHEME_P); 721static int skipspace (SCHEME_P);
631static int token (SCHEME_P); 722static int token (SCHEME_P);
632static void printslashstring (SCHEME_P_ char *s, int len); 723static void printslashstring (SCHEME_P_ char *s, int len);
633static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 724static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
634static void printatom (SCHEME_P_ pointer l, int f); 725static void printatom (SCHEME_P_ pointer l, int f);
635static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 726static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
639static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); 730static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list);
640static pointer revappend (SCHEME_P_ pointer a, pointer b); 731static pointer revappend (SCHEME_P_ pointer a, pointer b);
641static pointer ss_get_cont (SCHEME_P); 732static pointer ss_get_cont (SCHEME_P);
642static void ss_set_cont (SCHEME_P_ pointer cont); 733static void ss_set_cont (SCHEME_P_ pointer cont);
643static void dump_stack_mark (SCHEME_P); 734static void dump_stack_mark (SCHEME_P);
644static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); 735static int opexe_0 (SCHEME_P_ enum scheme_opcodes op);
736static int opexe_1 (SCHEME_P_ enum scheme_opcodes op);
645static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); 737static int opexe_2 (SCHEME_P_ enum scheme_opcodes op);
646static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); 738static int opexe_3 (SCHEME_P_ enum scheme_opcodes op);
647static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 739static int opexe_4 (SCHEME_P_ enum scheme_opcodes op);
648static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 740static int opexe_5 (SCHEME_P_ enum scheme_opcodes op);
649static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 741static int opexe_6 (SCHEME_P_ enum scheme_opcodes op);
650static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 742static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
651static void assign_syntax (SCHEME_P_ char *name); 743static void assign_syntax (SCHEME_P_ const char *name);
652static int syntaxnum (pointer p); 744static int syntaxnum (pointer p);
653static void assign_proc (SCHEME_P_ enum scheme_opcodes, char *name); 745static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
746
747static IVALUE
748ivalue (pointer x)
749{
750 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
751}
752
753static RVALUE
754rvalue (pointer x)
755{
756 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
757}
758
759INTERFACE num
760nvalue (pointer x)
761{
762 num n;
763
764 num_set_fixnum (n, is_integer (x));
765
766 if (num_is_fixnum (n))
767 num_set_ivalue (n, ivalue_unchecked (x));
768 else
769 num_set_rvalue (n, rvalue_unchecked (x));
770
771 return n;
772}
654 773
655static num 774static num
656num_add (num a, num b) 775num_op (enum num_op op, num a, num b)
657{ 776{
658 num ret; 777 num ret;
659 778
660 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 779 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
661 780
662 if (num_is_fixnum (ret)) 781 if (num_is_fixnum (ret))
663 num_set_ivalue (ret, a.value.ivalue + b.value.ivalue); 782 {
783 switch (op)
784 {
785 case NUM_ADD: a.ivalue += b.ivalue; break;
786 case NUM_SUB: a.ivalue -= b.ivalue; break;
787 case NUM_MUL: a.ivalue *= b.ivalue; break;
788 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
789 }
790
791 num_set_ivalue (ret, a.ivalue);
792 }
793#if USE_REAL
664 else 794 else
665 num_set_rvalue (ret, num_rvalue (a) + num_rvalue (b)); 795 {
796 switch (op)
797 {
798 case NUM_ADD: a.rvalue += b.rvalue; break;
799 case NUM_SUB: a.rvalue -= b.rvalue; break;
800 case NUM_MUL: a.rvalue *= b.rvalue; break;
801 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
802 }
666 803
667 return ret; 804 num_set_rvalue (ret, a.rvalue);
668} 805 }
669 806#endif
670static num
671num_mul (num a, num b)
672{
673 num ret;
674
675 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
676
677 if (num_is_fixnum (ret))
678 num_set_ivalue (ret, a.value.ivalue * b.value.ivalue);
679 else
680 num_set_rvalue (ret, num_rvalue (a) * num_rvalue (b));
681 807
682 return ret; 808 return ret;
683} 809}
684 810
685static num 811static num
686num_div (num a, num b) 812num_div (num a, num b)
687{ 813{
688 num ret; 814 num ret;
689 815
690 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && a.value.ivalue % b.value.ivalue == 0); 816 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
691 817
692 if (num_is_fixnum (ret)) 818 if (num_is_fixnum (ret))
693 num_set_ivalue (ret, a.value.ivalue / b.value.ivalue); 819 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
694 else 820 else
695 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); 821 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
696
697 return ret;
698}
699
700static num
701num_intdiv (num a, num b)
702{
703 num ret;
704
705 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
706
707 if (num_is_fixnum (ret))
708 num_set_ivalue (ret, a.value.ivalue / b.value.ivalue);
709 else
710 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
711
712 return ret;
713}
714
715static num
716num_sub (num a, num b)
717{
718 num ret;
719
720 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
721
722 if (num_is_fixnum (ret))
723 num_set_ivalue (ret, a.value.ivalue - b.value.ivalue);
724 else
725 num_set_rvalue (ret, num_rvalue (a) - num_rvalue (b));
726 822
727 return ret; 823 return ret;
728} 824}
729 825
730static num 826static num
771 867
772 num_set_ivalue (ret, res); 868 num_set_ivalue (ret, res);
773 return ret; 869 return ret;
774} 870}
775 871
872/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
776static int 873static int
777num_eq (num a, num b) 874num_cmp (num a, num b)
778{ 875{
876 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
779 int ret; 877 int ret;
780 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
781 878
782 if (is_fixnum) 879 if (is_fixnum)
783 ret = a.value.ivalue == b.value.ivalue; 880 {
881 IVALUE av = num_ivalue (a);
882 IVALUE bv = num_ivalue (b);
883
884 ret = av == bv ? 0 : av < bv ? -1 : +1;
885 }
784 else 886 else
785 ret = num_rvalue (a) == num_rvalue (b); 887 {
888 RVALUE av = num_rvalue (a);
889 RVALUE bv = num_rvalue (b);
890
891 ret = av == bv ? 0 : av < bv ? -1 : +1;
892 }
786 893
787 return ret; 894 return ret;
788} 895}
789
790
791static int
792num_gt (num a, num b)
793{
794 int ret;
795 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
796
797 if (is_fixnum)
798 ret = a.value.ivalue > b.value.ivalue;
799 else
800 ret = num_rvalue (a) > num_rvalue (b);
801
802 return ret;
803}
804
805static int
806num_ge (num a, num b)
807{
808 return !num_lt (a, b);
809}
810
811static int
812num_lt (num a, num b)
813{
814 int ret;
815 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
816
817 if (is_fixnum)
818 ret = a.value.ivalue < b.value.ivalue;
819 else
820 ret = num_rvalue (a) < num_rvalue (b);
821
822 return ret;
823}
824
825static int
826num_le (num a, num b)
827{
828 return !num_gt (a, b);
829}
830
831#if USE_MATH
832
833/* Round to nearest. Round to even if midway */
834static double
835round_per_R5RS (double x)
836{
837 double fl = floor (x);
838 double ce = ceil (x);
839 double dfl = x - fl;
840 double dce = ce - x;
841
842 if (dfl > dce)
843 return ce;
844 else if (dfl < dce)
845 return fl;
846 else
847 {
848 if (fmod (fl, 2.0) == 0.0) /* I imagine this holds */
849 return fl;
850 else
851 return ce;
852 }
853}
854#endif
855 896
856static int 897static int
857is_zero_rvalue (RVALUE x) 898is_zero_rvalue (RVALUE x)
858{ 899{
859#if USE_FLOAT 900 return x == 0;
901#if 0
902#if USE_REAL
860 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 903 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
861#else 904#else
862 return x == 0; 905 return x == 0;
863#endif 906#endif
864} 907#endif
865
866static long
867binary_decode (const char *s)
868{
869 long x = 0;
870
871 while (*s != 0 && (*s == '1' || *s == '0'))
872 {
873 x <<= 1;
874 x += *s - '0';
875 s++;
876 }
877
878 return x;
879} 908}
880 909
881/* allocate new cell segment */ 910/* allocate new cell segment */
882static int 911ecb_cold static int
883alloc_cellseg (SCHEME_P_ int n) 912alloc_cellseg (SCHEME_P)
884{ 913{
885 pointer newp; 914 struct cell *newp;
886 pointer last; 915 struct cell *last;
887 pointer p; 916 struct cell *p;
888 char *cp; 917 char *cp;
889 long i; 918 long i;
890 int k; 919 int k;
891 920
892 static int segsize = CELL_SEGSIZE >> 1; 921 static int segsize = CELL_SEGSIZE >> 1;
893 segsize <<= 1; 922 segsize <<= 1;
894 923
895 for (k = 0; k < n; k++)
896 {
897 if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1)
898 return k;
899
900 cp = malloc (segsize * sizeof (struct cell)); 924 cp = malloc (segsize * sizeof (struct cell));
901 925
902 if (!cp && USE_ERROR_CHECKING) 926 if (!cp && USE_ERROR_CHECKING)
903 return k; 927 return k;
904 928
905 i = ++SCHEME_V->last_cell_seg; 929 i = ++SCHEME_V->last_cell_seg;
906 SCHEME_V->alloc_seg[i] = cp;
907 930
908 /* insert new segment in address order */ 931 newp = (struct cell *)cp;
909 newp = (pointer)cp;
910 SCHEME_V->cell_seg[i] = newp; 932 SCHEME_V->cell_seg[i] = newp;
911 SCHEME_V->cell_segsize[i] = segsize; 933 SCHEME_V->cell_segsize[i] = segsize;
912
913 //TODO: insert, not swap
914 while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i])
915 {
916 p = SCHEME_V->cell_seg[i];
917 SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1];
918 SCHEME_V->cell_seg[i - 1] = p;
919
920 k = SCHEME_V->cell_segsize[i];
921 SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1];
922 SCHEME_V->cell_segsize[i - 1] = k;
923
924 --i;
925 }
926
927 SCHEME_V->fcells += segsize; 934 SCHEME_V->fcells += segsize;
928 last = newp + segsize - 1; 935 last = newp + segsize - 1;
929 936
930 for (p = newp; p <= last; p++) 937 for (p = newp; p <= last; p++)
931 { 938 {
939 pointer cp = POINTER (p);
940 clrmark (cp);
932 set_typeflag (p, 0); 941 set_typeflag (cp, T_PAIR);
933 set_cdr (p, p + 1);
934 set_car (p, NIL); 942 set_car (cp, NIL);
935 } 943 set_cdr (cp, POINTER (p + 1));
944 }
936 945
937 /* insert new cells in address order on free list */
938 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
939 {
940 set_cdr (last, SCHEME_V->free_cell); 946 set_cdr (POINTER (last), SCHEME_V->free_cell);
941 SCHEME_V->free_cell = newp; 947 SCHEME_V->free_cell = POINTER (newp);
942 }
943 else
944 {
945 p = SCHEME_V->free_cell;
946 948
947 while (cdr (p) != NIL && newp > cdr (p))
948 p = cdr (p);
949
950 set_cdr (last, cdr (p));
951 set_cdr (p, newp);
952 }
953 }
954
955 return n; 949 return 1;
956} 950}
957 951
958/* get new cell. parameter a, b is marked by gc. */ 952/* get new cell. parameter a, b is marked by gc. */
959static INLINE pointer 953ecb_inline pointer
960get_cell_x (SCHEME_P_ pointer a, pointer b) 954get_cell_x (SCHEME_P_ pointer a, pointer b)
961{ 955{
962 if (SCHEME_V->free_cell == NIL) 956 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
963 { 957 {
964 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 958 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
965 return S_SINK; 959 return S_SINK;
966 960
967 if (SCHEME_V->free_cell == NIL) 961 if (SCHEME_V->free_cell == NIL)
968 { 962 {
969 const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; 963 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2;
970 964
971 gc (SCHEME_A_ a, b); 965 gc (SCHEME_A_ a, b);
972 966
973 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 967 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
974 { 968 {
975 /* if only a few recovered, get more to avoid fruitless gc's */ 969 /* if only a few recovered, get more to avoid fruitless gc's */
976 if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) 970 if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL)
977 { 971 {
978#if USE_ERROR_CHECKING 972#if USE_ERROR_CHECKING
979 SCHEME_V->no_memory = 1; 973 SCHEME_V->no_memory = 1;
980 return S_SINK; 974 return S_SINK;
981#endif 975#endif
991 --SCHEME_V->fcells; 985 --SCHEME_V->fcells;
992 return x; 986 return x;
993 } 987 }
994} 988}
995 989
996/* make sure that there is a given number of cells free */
997static pointer
998reserve_cells (SCHEME_P_ int n)
999{
1000 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1001 return NIL;
1002
1003 /* Are there enough cells available? */
1004 if (SCHEME_V->fcells < n)
1005 {
1006 /* If not, try gc'ing some */
1007 gc (SCHEME_A_ NIL, NIL);
1008
1009 if (SCHEME_V->fcells < n)
1010 {
1011 /* If there still aren't, try getting more heap */
1012 if (!alloc_cellseg (SCHEME_A_ 1) && USE_ERROR_CHECKING)
1013 {
1014 SCHEME_V->no_memory = 1;
1015 return NIL;
1016 }
1017 }
1018
1019 if (SCHEME_V->fcells < n && USE_ERROR_CHECKING)
1020 {
1021 /* If all fail, report failure */
1022 SCHEME_V->no_memory = 1;
1023 return NIL;
1024 }
1025 }
1026
1027 return S_T;
1028}
1029
1030static pointer
1031get_consecutive_cells (SCHEME_P_ int n)
1032{
1033 pointer x;
1034
1035 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1036 return S_SINK;
1037
1038 /* Are there any cells available? */
1039 x = find_consecutive_cells (SCHEME_A_ n);
1040
1041 if (x != NIL)
1042 return x;
1043
1044 /* If not, try gc'ing some */
1045 gc (SCHEME_A_ NIL, NIL);
1046
1047 for (;;)
1048 {
1049 x = find_consecutive_cells (SCHEME_A_ n);
1050
1051 if (x != NIL)
1052 return x;
1053
1054 /* If there still aren't, try getting more heap */
1055 if (!alloc_cellseg (SCHEME_A_ 1))
1056 {
1057#if USE_ERROR_CHECKING
1058 SCHEME_V->no_memory = 1;
1059 return S_SINK;
1060#endif
1061 }
1062 }
1063}
1064
1065static int
1066count_consecutive_cells (pointer x, int needed)
1067{
1068 int n = 1;
1069
1070 while (cdr (x) == x + 1)
1071 {
1072 x = cdr (x);
1073 n++;
1074
1075 if (n > needed)
1076 return n;
1077 }
1078
1079 return n;
1080}
1081
1082static pointer
1083find_consecutive_cells (SCHEME_P_ int n)
1084{
1085 pointer *pp;
1086 int cnt;
1087
1088 pp = &SCHEME_V->free_cell;
1089
1090 while (*pp != NIL)
1091 {
1092 cnt = count_consecutive_cells (*pp, n);
1093
1094 if (cnt >= n)
1095 {
1096 pointer x = *pp;
1097
1098 *pp = cdr (*pp + n - 1);
1099 SCHEME_V->fcells -= n;
1100 return x;
1101 }
1102
1103 pp = &cdr (*pp + cnt - 1);
1104 }
1105
1106 return NIL;
1107}
1108
1109/* To retain recent allocs before interpreter knows about them - 990/* To retain recent allocs before interpreter knows about them -
1110 Tehom */ 991 Tehom */
1111 992ecb_hot static void
1112static void
1113push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 993push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
1114{ 994{
1115 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 995 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1116 996
1117 set_typeflag (holder, T_PAIR | T_IMMUTABLE); 997 set_typeflag (holder, T_PAIR);
998 setimmutable (holder);
1118 set_car (holder, recent); 999 set_car (holder, recent);
1119 set_cdr (holder, car (S_SINK)); 1000 set_cdr (holder, car (S_SINK));
1120 set_car (S_SINK, holder); 1001 set_car (S_SINK, holder);
1121} 1002}
1122 1003
1123static pointer 1004ecb_hot static pointer
1124get_cell (SCHEME_P_ pointer a, pointer b) 1005get_cell (SCHEME_P_ pointer a, pointer b)
1125{ 1006{
1126 pointer cell = get_cell_x (SCHEME_A_ a, b); 1007 pointer cell = get_cell_x (SCHEME_A_ a, b);
1127 1008
1128 /* For right now, include "a" and "b" in "cell" so that gc doesn't 1009 /* For right now, include "a" and "b" in "cell" so that gc doesn't
1135 1016
1136 return cell; 1017 return cell;
1137} 1018}
1138 1019
1139static pointer 1020static pointer
1140get_vector_object (SCHEME_P_ int len, pointer init) 1021get_vector_object (SCHEME_P_ uint32_t len, pointer init)
1141{ 1022{
1142 pointer cells = get_consecutive_cells (SCHEME_A_ len / 2 + len % 2 + 1); 1023 pointer v = get_cell_x (SCHEME_A_ NIL, NIL);
1024 pointer *e = malloc (len * sizeof (pointer));
1143 1025
1144#if USE_ERROR_CHECKING 1026 if (!e && USE_ERROR_CHECKING)
1145 if (SCHEME_V->no_memory)
1146 return S_SINK; 1027 return S_SINK;
1147#endif
1148 1028
1149 /* Record it as a vector so that gc understands it. */ 1029 /* Record it as a vector so that gc understands it. */
1150 set_typeflag (cells, T_VECTOR | T_ATOM); 1030 set_typeflag (v, T_VECTOR | T_ATOM);
1151 ivalue_unchecked (cells) = len; 1031
1152 set_num_integer (cells); 1032 CELL(v)->object.vector.vvalue = e;
1033 CELL(v)->object.vector.length = len;
1153 fill_vector (cells, init); 1034 fill_vector (v, 0, init);
1154 push_recent_alloc (SCHEME_A_ cells, NIL); 1035 push_recent_alloc (SCHEME_A_ v, NIL);
1155 1036
1156 return cells; 1037 return v;
1157} 1038}
1158 1039
1159static INLINE void 1040ecb_inline void
1160ok_to_freely_gc (SCHEME_P) 1041ok_to_freely_gc (SCHEME_P)
1161{ 1042{
1162 set_car (S_SINK, NIL); 1043 set_car (S_SINK, NIL);
1163} 1044}
1164 1045
1165#if defined TSGRIND 1046#if defined TSGRIND
1166static void 1047static void
1167check_cell_alloced (pointer p, int expect_alloced) 1048check_cell_alloced (pointer p, int expect_alloced)
1168{ 1049{
1169 /* Can't use putstr(SCHEME_A_ str) because callers have no access to 1050 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */
1170 sc. */
1171 if (typeflag (p) & !expect_alloced) 1051 if (typeflag (p) & !expect_alloced)
1172 xwrstr ("Cell is already allocated!\n"); 1052 putstr (SCHEME_A_ "Cell is already allocated!\n");
1173 1053
1174 if (!(typeflag (p)) & expect_alloced) 1054 if (!(typeflag (p)) & expect_alloced)
1175 xwrstr ("Cell is not allocated!\n"); 1055 putstr (SCHEME_A_ "Cell is not allocated!\n");
1176} 1056}
1177 1057
1178static void 1058static void
1179check_range_alloced (pointer p, int n, int expect_alloced) 1059check_range_alloced (pointer p, int n, int expect_alloced)
1180{ 1060{
1186#endif 1066#endif
1187 1067
1188/* Medium level cell allocation */ 1068/* Medium level cell allocation */
1189 1069
1190/* get new cons cell */ 1070/* get new cons cell */
1191pointer 1071ecb_hot static pointer
1192xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1072xcons (SCHEME_P_ pointer a, pointer b)
1193{ 1073{
1194 pointer x = get_cell (SCHEME_A_ a, b); 1074 pointer x = get_cell (SCHEME_A_ a, b);
1195 1075
1196 set_typeflag (x, T_PAIR); 1076 set_typeflag (x, T_PAIR);
1197
1198 if (immutable)
1199 setimmutable (x);
1200 1077
1201 set_car (x, a); 1078 set_car (x, a);
1202 set_cdr (x, b); 1079 set_cdr (x, b);
1080
1203 return x; 1081 return x;
1204} 1082}
1205 1083
1084ecb_hot static pointer
1085ximmutable_cons (SCHEME_P_ pointer a, pointer b)
1086{
1087 pointer x = xcons (SCHEME_A_ a, b);
1088 setimmutable (x);
1089 return x;
1090}
1091
1092#define cons(a,b) xcons (SCHEME_A_ a, b)
1093#define immutable_cons(a,b) ximmutable_cons (SCHEME_A_ a, b)
1094
1095ecb_cold static pointer
1096generate_symbol (SCHEME_P_ const char *name)
1097{
1098 pointer x = mk_string (SCHEME_A_ name);
1099 setimmutable (x);
1100 set_typeflag (x, T_SYMBOL | T_ATOM);
1101 return x;
1102}
1103
1206/* ========== oblist implementation ========== */ 1104/* ========== oblist implementation ========== */
1207 1105
1208#ifndef USE_OBJECT_LIST 1106#ifndef USE_OBJECT_LIST
1209 1107
1108static int
1210static int hash_fn (const char *key, int table_size); 1109hash_fn (const char *key, int table_size)
1110{
1111 const unsigned char *p = (unsigned char *)key;
1112 uint32_t hash = 2166136261U;
1211 1113
1114 while (*p)
1115 hash = (hash ^ *p++) * 16777619;
1116
1117 return hash % table_size;
1118}
1119
1212static pointer 1120ecb_cold static pointer
1213oblist_initial_value (SCHEME_P) 1121oblist_initial_value (SCHEME_P)
1214{ 1122{
1215 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1123 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1216} 1124}
1217 1125
1218/* returns the new symbol */ 1126/* returns the new symbol */
1219static pointer 1127ecb_cold static pointer
1220oblist_add_by_name (SCHEME_P_ const char *name) 1128oblist_add_by_name (SCHEME_P_ const char *name)
1221{ 1129{
1222 pointer x; 1130 pointer x = generate_symbol (SCHEME_A_ name);
1223 int location;
1224
1225 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1226 set_typeflag (x, T_SYMBOL);
1227 setimmutable (car (x));
1228
1229 location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); 1131 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1230 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1132 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1231 return x; 1133 return x;
1232} 1134}
1233 1135
1234static INLINE pointer 1136ecb_cold static pointer
1235oblist_find_by_name (SCHEME_P_ const char *name) 1137oblist_find_by_name (SCHEME_P_ const char *name)
1236{ 1138{
1237 int location; 1139 int location;
1238 pointer x; 1140 pointer x;
1239 char *s; 1141 char *s;
1240 1142
1241 location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); 1143 location = hash_fn (name, veclength (SCHEME_V->oblist));
1242 1144
1243 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1145 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1244 { 1146 {
1245 s = symname (car (x)); 1147 s = symname (car (x));
1246 1148
1247 /* case-insensitive, per R5RS section 2 */ 1149 /* case-insensitive, per R5RS section 2 */
1248 if (stricmp (name, s) == 0) 1150 if (stricmp (name, s) == 0)
1250 } 1152 }
1251 1153
1252 return NIL; 1154 return NIL;
1253} 1155}
1254 1156
1255static pointer 1157ecb_cold static pointer
1256oblist_all_symbols (SCHEME_P) 1158oblist_all_symbols (SCHEME_P)
1257{ 1159{
1258 int i; 1160 int i;
1259 pointer x; 1161 pointer x;
1260 pointer ob_list = NIL; 1162 pointer ob_list = NIL;
1261 1163
1262 for (i = 0; i < ivalue_unchecked (SCHEME_V->oblist); i++) 1164 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1263 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1165 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1264 ob_list = cons (x, ob_list); 1166 ob_list = cons (x, ob_list);
1265 1167
1266 return ob_list; 1168 return ob_list;
1267} 1169}
1268 1170
1269#else 1171#else
1270 1172
1271static pointer 1173ecb_cold static pointer
1272oblist_initial_value (SCHEME_P) 1174oblist_initial_value (SCHEME_P)
1273{ 1175{
1274 return NIL; 1176 return NIL;
1275} 1177}
1276 1178
1277static INLINE pointer 1179ecb_cold static pointer
1278oblist_find_by_name (SCHEME_P_ const char *name) 1180oblist_find_by_name (SCHEME_P_ const char *name)
1279{ 1181{
1280 pointer x; 1182 pointer x;
1281 char *s; 1183 char *s;
1282 1184
1291 1193
1292 return NIL; 1194 return NIL;
1293} 1195}
1294 1196
1295/* returns the new symbol */ 1197/* returns the new symbol */
1296static pointer 1198ecb_cold static pointer
1297oblist_add_by_name (SCHEME_P_ const char *name) 1199oblist_add_by_name (SCHEME_P_ const char *name)
1298{ 1200{
1299 pointer x; 1201 pointer x = generate_symbol (SCHEME_A_ name);
1300
1301 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1302 set_typeflag (x, T_SYMBOL);
1303 setimmutable (car (x));
1304 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1202 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1305 return x; 1203 return x;
1306} 1204}
1307 1205
1308static pointer 1206ecb_cold static pointer
1309oblist_all_symbols (SCHEME_P) 1207oblist_all_symbols (SCHEME_P)
1310{ 1208{
1311 return SCHEME_V->oblist; 1209 return SCHEME_V->oblist;
1312} 1210}
1313 1211
1314#endif 1212#endif
1315 1213
1316#if USE_PORTS
1317static pointer 1214ecb_cold static pointer
1318mk_port (SCHEME_P_ port *p) 1215mk_port (SCHEME_P_ port *p)
1319{ 1216{
1320 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1217 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1321 1218
1322 set_typeflag (x, T_PORT | T_ATOM); 1219 set_typeflag (x, T_PORT | T_ATOM);
1323 x->object.port = p; 1220 set_port (x, p);
1324 1221
1325 return x; 1222 return x;
1326} 1223}
1327#endif
1328 1224
1329pointer 1225ecb_cold pointer
1330mk_foreign_func (SCHEME_P_ foreign_func f) 1226mk_foreign_func (SCHEME_P_ foreign_func f)
1331{ 1227{
1332 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1228 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1333 1229
1334 set_typeflag (x, (T_FOREIGN | T_ATOM)); 1230 set_typeflag (x, T_FOREIGN | T_ATOM);
1335 x->object.ff = f; 1231 CELL(x)->object.ff = f;
1336 1232
1337 return x; 1233 return x;
1338} 1234}
1339 1235
1340INTERFACE pointer 1236INTERFACE pointer
1341mk_character (SCHEME_P_ int c) 1237mk_character (SCHEME_P_ int c)
1342{ 1238{
1343 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1239 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1344 1240
1345 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1241 set_typeflag (x, T_CHARACTER | T_ATOM);
1346 ivalue_unchecked (x) = c; 1242 set_ivalue (x, c & 0xff);
1347 set_num_integer (x); 1243
1348 return x; 1244 return x;
1349} 1245}
1350 1246
1351/* get number atom (integer) */ 1247/* get number atom (integer) */
1352INTERFACE pointer 1248INTERFACE pointer
1353mk_integer (SCHEME_P_ long num) 1249mk_integer (SCHEME_P_ long n)
1354{ 1250{
1251 pointer p = 0;
1252 pointer *pp = &p;
1253
1254#if USE_INTCACHE
1255 if (n >= INTCACHE_MIN && n <= INTCACHE_MAX)
1256 pp = &SCHEME_V->intcache[n - INTCACHE_MIN];
1257#endif
1258
1259 if (!*pp)
1260 {
1355 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1261 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1356 1262
1357 set_typeflag (x, (T_NUMBER | T_ATOM)); 1263 set_typeflag (x, T_INTEGER | T_ATOM);
1358 ivalue_unchecked (x) = num; 1264 setimmutable (x); /* shouldn't do anything, doesn't cost anything */
1359 set_num_integer (x); 1265 set_ivalue (x, n);
1266
1267 *pp = x;
1268 }
1269
1360 return x; 1270 return *pp;
1361} 1271}
1362 1272
1363INTERFACE pointer 1273INTERFACE pointer
1364mk_real (SCHEME_P_ RVALUE n) 1274mk_real (SCHEME_P_ RVALUE n)
1365{ 1275{
1276#if USE_REAL
1366 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1277 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1367 1278
1368 set_typeflag (x, (T_NUMBER | T_ATOM)); 1279 set_typeflag (x, T_REAL | T_ATOM);
1369 rvalue_unchecked (x) = n; 1280 set_rvalue (x, n);
1370 set_num_real (x); 1281
1371 return x; 1282 return x;
1283#else
1284 return mk_integer (SCHEME_A_ n);
1285#endif
1372} 1286}
1373 1287
1374static pointer 1288static pointer
1375mk_number (SCHEME_P_ num n) 1289mk_number (SCHEME_P_ const num n)
1376{ 1290{
1291#if USE_REAL
1377 if (num_is_fixnum (n)) 1292 return num_is_fixnum (n)
1378 { 1293 ? mk_integer (SCHEME_A_ num_ivalue (n))
1294 : mk_real (SCHEME_A_ num_rvalue (n));
1295#else
1379 return mk_integer (SCHEME_A_ num_ivalue (n)); 1296 return mk_integer (SCHEME_A_ num_ivalue (n));
1380 } 1297#endif
1381 else
1382 {
1383 return mk_real (SCHEME_A_ num_rvalue (n));
1384 }
1385} 1298}
1386 1299
1387/* allocate name to string area */ 1300/* allocate name to string area */
1388static char * 1301static char *
1389store_string (SCHEME_P_ int len_str, const char *str, char fill) 1302store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1390{ 1303{
1391 char *q;
1392
1393 q = (char *)malloc (len_str + 1); 1304 char *q = malloc (len_str + 1);
1394 1305
1395 if (q == 0) 1306 if (q == 0 && USE_ERROR_CHECKING)
1396 { 1307 {
1397#if USE_ERROR_CHECKING
1398 SCHEME_V->no_memory = 1; 1308 SCHEME_V->no_memory = 1;
1399 return SCHEME_V->strbuff; 1309 return SCHEME_V->strbuff;
1400#endif
1401 } 1310 }
1402 1311
1403 if (str) 1312 if (str)
1404 { 1313 memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */
1405 int l = strlen (str);
1406
1407 if (l > len_str)
1408 l = len_str;
1409
1410 memcpy (q, str, l + 1);
1411 q[l + 1] = 0;
1412 }
1413 else 1314 else
1414 {
1415 memset (q, fill, len_str); 1315 memset (q, fill, len_str);
1316
1416 q[len_str] = 0; 1317 q[len_str] = 0;
1417 }
1418 1318
1419 return q; 1319 return q;
1420} 1320}
1421 1321
1422/* get new string */
1423INTERFACE pointer 1322INTERFACE pointer
1424mk_string (SCHEME_P_ const char *str)
1425{
1426 return mk_counted_string (SCHEME_A_ str, strlen (str));
1427}
1428
1429INTERFACE pointer
1430mk_counted_string (SCHEME_P_ const char *str, int len)
1431{
1432 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1433
1434 set_typeflag (x, T_STRING | T_ATOM);
1435 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1436 strlength (x) = len;
1437 return x;
1438}
1439
1440INTERFACE pointer
1441mk_empty_string (SCHEME_P_ int len, char fill) 1323mk_empty_string (SCHEME_P_ uint32_t len, char fill)
1442{ 1324{
1443 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1325 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1444 1326
1445 set_typeflag (x, T_STRING | T_ATOM); 1327 set_typeflag (x, T_STRING | T_ATOM);
1446 strvalue (x) = store_string (SCHEME_A_ len, 0, fill); 1328 strvalue (x) = store_string (SCHEME_A_ len, 0, fill);
1447 strlength (x) = len; 1329 strlength (x) = len;
1448 return x; 1330 return x;
1449} 1331}
1450 1332
1451INTERFACE pointer 1333INTERFACE pointer
1334mk_counted_string (SCHEME_P_ const char *str, uint32_t len)
1335{
1336 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1337
1338 set_typeflag (x, T_STRING | T_ATOM);
1339 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1340 strlength (x) = len;
1341
1342 return x;
1343}
1344
1345INTERFACE pointer
1346mk_string (SCHEME_P_ const char *str)
1347{
1348 return mk_counted_string (SCHEME_A_ str, strlen (str));
1349}
1350
1351INTERFACE pointer
1452mk_vector (SCHEME_P_ int len) 1352mk_vector (SCHEME_P_ uint32_t len)
1453{ 1353{
1454 return get_vector_object (SCHEME_A_ len, NIL); 1354 return get_vector_object (SCHEME_A_ len, NIL);
1455} 1355}
1456 1356
1457INTERFACE void 1357INTERFACE void
1458fill_vector (pointer vec, pointer obj) 1358fill_vector (pointer vec, uint32_t start, pointer obj)
1459{ 1359{
1460 int i; 1360 int i;
1461 int num = ivalue (vec) / 2 + ivalue (vec) % 2;
1462 1361
1463 for (i = 0; i < num; i++) 1362 for (i = start; i < veclength (vec); i++)
1464 { 1363 vecvalue (vec)[i] = obj;
1465 set_typeflag (vec + 1 + i, T_PAIR); 1364}
1466 setimmutable (vec + 1 + i); 1365
1467 set_car (vec + 1 + i, obj); 1366INTERFACE void
1468 set_cdr (vec + 1 + i, obj); 1367vector_resize (pointer vec, uint32_t newsize, pointer fill)
1469 } 1368{
1369 uint32_t oldsize = veclength (vec);
1370 vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer));
1371 veclength (vec) = newsize;
1372 fill_vector (vec, oldsize, fill);
1470} 1373}
1471 1374
1472INTERFACE pointer 1375INTERFACE pointer
1473vector_elem (pointer vec, int ielem) 1376vector_get (pointer vec, uint32_t ielem)
1474{ 1377{
1475 int n = ielem / 2; 1378 return vecvalue(vec)[ielem];
1476
1477 if (ielem % 2 == 0)
1478 return car (vec + 1 + n);
1479 else
1480 return cdr (vec + 1 + n);
1481} 1379}
1482 1380
1483INTERFACE void 1381INTERFACE void
1484set_vector_elem (pointer vec, int ielem, pointer a) 1382vector_set (pointer vec, uint32_t ielem, pointer a)
1485{ 1383{
1486 int n = ielem / 2; 1384 vecvalue(vec)[ielem] = a;
1487
1488 if (ielem % 2 == 0)
1489 set_car (vec + 1 + n, a);
1490 else
1491 set_cdr (vec + 1 + n, a);
1492} 1385}
1493 1386
1494/* get new symbol */ 1387/* get new symbol */
1495INTERFACE pointer 1388INTERFACE pointer
1496mk_symbol (SCHEME_P_ const char *name) 1389mk_symbol (SCHEME_P_ const char *name)
1497{ 1390{
1391 /* first check oblist */
1392 pointer x = oblist_find_by_name (SCHEME_A_ name);
1393
1394 if (x == NIL)
1395 x = oblist_add_by_name (SCHEME_A_ name);
1396
1397 return x;
1398}
1399
1400ecb_cold INTERFACE pointer
1401gensym (SCHEME_P)
1402{
1498 pointer x; 1403 pointer x;
1499 1404 char name[40] = "gensym-";
1500 /* first check oblist */
1501 x = oblist_find_by_name (SCHEME_A_ name);
1502
1503 if (x != NIL)
1504 return x;
1505 else
1506 {
1507 x = oblist_add_by_name (SCHEME_A_ name);
1508 return x;
1509 }
1510}
1511
1512INTERFACE pointer
1513gensym (SCHEME_P)
1514{
1515 pointer x;
1516 char name[40];
1517
1518 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1519 {
1520 strcpy (name, "gensym-");
1521 xnum (name + 7, SCHEME_V->gensym_cnt); 1405 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1522 1406
1523 /* first check oblist */ 1407 return generate_symbol (SCHEME_A_ name);
1524 x = oblist_find_by_name (SCHEME_A_ name); 1408}
1525 1409
1526 if (x != NIL) 1410static int
1527 continue; 1411is_gensym (SCHEME_P_ pointer x)
1528 else 1412{
1529 { 1413 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1530 x = oblist_add_by_name (SCHEME_A_ name);
1531 return x;
1532 }
1533 }
1534
1535 return NIL;
1536} 1414}
1537 1415
1538/* make symbol or number atom from string */ 1416/* make symbol or number atom from string */
1539static pointer 1417ecb_cold static pointer
1540mk_atom (SCHEME_P_ char *q) 1418mk_atom (SCHEME_P_ char *q)
1541{ 1419{
1542 char c, *p; 1420 char c, *p;
1543 int has_dec_point = 0; 1421 int has_dec_point = 0;
1544 int has_fp_exp = 0; 1422 int has_fp_exp = 0;
1545 1423
1546#if USE_COLON_HOOK 1424#if USE_COLON_HOOK
1547
1548 if ((p = strstr (q, "::")) != 0) 1425 if ((p = strstr (q, "::")) != 0)
1549 { 1426 {
1550 *p = 0; 1427 *p = 0;
1551 return cons (SCHEME_V->COLON_HOOK, 1428 return cons (SCHEME_V->COLON_HOOK,
1552 cons (cons (SCHEME_V->QUOTE, 1429 cons (cons (SCHEME_V->QUOTE,
1553 cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL))); 1430 cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL)));
1554 } 1431 }
1555
1556#endif 1432#endif
1557 1433
1558 p = q; 1434 p = q;
1559 c = *p++; 1435 c = *p++;
1560 1436
1567 has_dec_point = 1; 1443 has_dec_point = 1;
1568 c = *p++; 1444 c = *p++;
1569 } 1445 }
1570 1446
1571 if (!isdigit (c)) 1447 if (!isdigit (c))
1572 {
1573 return mk_symbol (SCHEME_A_ strlwr (q)); 1448 return mk_symbol (SCHEME_A_ strlwr (q));
1574 }
1575 } 1449 }
1576 else if (c == '.') 1450 else if (c == '.')
1577 { 1451 {
1578 has_dec_point = 1; 1452 has_dec_point = 1;
1579 c = *p++; 1453 c = *p++;
1580 1454
1581 if (!isdigit (c)) 1455 if (!isdigit (c))
1582 {
1583 return mk_symbol (SCHEME_A_ strlwr (q)); 1456 return mk_symbol (SCHEME_A_ strlwr (q));
1584 }
1585 } 1457 }
1586 else if (!isdigit (c)) 1458 else if (!isdigit (c))
1587 {
1588 return mk_symbol (SCHEME_A_ strlwr (q)); 1459 return mk_symbol (SCHEME_A_ strlwr (q));
1589 }
1590 1460
1591 for (; (c = *p) != 0; ++p) 1461 for (; (c = *p) != 0; ++p)
1592 { 1462 {
1593 if (!isdigit (c)) 1463 if (!isdigit (c))
1594 { 1464 {
1602 } 1472 }
1603 else if ((c == 'e') || (c == 'E')) 1473 else if ((c == 'e') || (c == 'E'))
1604 { 1474 {
1605 if (!has_fp_exp) 1475 if (!has_fp_exp)
1606 { 1476 {
1607 has_dec_point = 1; /* decimal point illegal 1477 has_dec_point = 1; /* decimal point illegal from now on */
1608 from now on */
1609 p++; 1478 p++;
1610 1479
1611 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1480 if ((*p == '-') || (*p == '+') || isdigit (*p))
1612 {
1613 continue; 1481 continue;
1614 }
1615 } 1482 }
1616 } 1483 }
1617 1484
1618 return mk_symbol (SCHEME_A_ strlwr (q)); 1485 return mk_symbol (SCHEME_A_ strlwr (q));
1619 } 1486 }
1620 } 1487 }
1621 1488
1622#if USE_FLOAT 1489#if USE_REAL
1623 if (has_dec_point) 1490 if (has_dec_point)
1624 return mk_real (SCHEME_A_ atof (q)); 1491 return mk_real (SCHEME_A_ atof (q));
1625#endif 1492#endif
1626 1493
1627 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1494 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1628} 1495}
1629 1496
1630/* make constant */ 1497/* make constant */
1631static pointer 1498ecb_cold static pointer
1632mk_sharp_const (SCHEME_P_ char *name) 1499mk_sharp_const (SCHEME_P_ char *name)
1633{ 1500{
1634 long x;
1635 char tmp[STRBUFFSIZE];
1636
1637 if (!strcmp (name, "t")) 1501 if (!strcmp (name, "t"))
1638 return S_T; 1502 return S_T;
1639 else if (!strcmp (name, "f")) 1503 else if (!strcmp (name, "f"))
1640 return S_F; 1504 return S_F;
1641 else if (*name == 'o') /* #o (octal) */
1642 {
1643 x = strtol (name + 1, 0, 8);
1644 return mk_integer (SCHEME_A_ x);
1645 }
1646 else if (*name == 'd') /* #d (decimal) */
1647 {
1648 x = strtol (name + 1, 0, 10);
1649 return mk_integer (SCHEME_A_ x);
1650 }
1651 else if (*name == 'x') /* #x (hex) */
1652 {
1653 x = strtol (name + 1, 0, 16);
1654 return mk_integer (SCHEME_A_ x);
1655 }
1656 else if (*name == 'b') /* #b (binary) */
1657 {
1658 x = binary_decode (name + 1);
1659 return mk_integer (SCHEME_A_ x);
1660 }
1661 else if (*name == '\\') /* #\w (character) */ 1505 else if (*name == '\\') /* #\w (character) */
1662 { 1506 {
1663 int c = 0; 1507 int c;
1664 1508
1509 // TODO: optimise
1665 if (stricmp (name + 1, "space") == 0) 1510 if (stricmp (name + 1, "space") == 0)
1666 c = ' '; 1511 c = ' ';
1667 else if (stricmp (name + 1, "newline") == 0) 1512 else if (stricmp (name + 1, "newline") == 0)
1668 c = '\n'; 1513 c = '\n';
1669 else if (stricmp (name + 1, "return") == 0) 1514 else if (stricmp (name + 1, "return") == 0)
1670 c = '\r'; 1515 c = '\r';
1671 else if (stricmp (name + 1, "tab") == 0) 1516 else if (stricmp (name + 1, "tab") == 0)
1672 c = '\t'; 1517 c = '\t';
1518 else if (stricmp (name + 1, "alarm") == 0)
1519 c = 0x07;
1520 else if (stricmp (name + 1, "backspace") == 0)
1521 c = 0x08;
1522 else if (stricmp (name + 1, "escape") == 0)
1523 c = 0x1b;
1524 else if (stricmp (name + 1, "delete") == 0)
1525 c = 0x7f;
1526 else if (stricmp (name + 1, "null") == 0)
1527 c = 0;
1673 else if (name[1] == 'x' && name[2] != 0) 1528 else if (name[1] == 'x' && name[2] != 0)
1674 { 1529 {
1675 int c1 = strtol (name + 2, 0, 16); 1530 long c1 = strtol (name + 2, 0, 16);
1676 1531
1677 if (c1 <= UCHAR_MAX) 1532 if (0 <= c1 && c1 <= UCHAR_MAX)
1678 c = c1; 1533 c = c1;
1679 else 1534 else
1680 return NIL; 1535 return NIL;
1681 1536 }
1682#if USE_ASCII_NAMES 1537#if USE_ASCII_NAMES
1683 }
1684 else if (is_ascii_name (name + 1, &c)) 1538 else if (is_ascii_name (name + 1, &c))
1685 {
1686 /* nothing */ 1539 /* nothing */;
1687#endif 1540#endif
1688 }
1689 else if (name[2] == 0) 1541 else if (name[2] == 0)
1690 c = name[1]; 1542 c = name[1];
1691 else 1543 else
1692 return NIL; 1544 return NIL;
1693 1545
1694 return mk_character (SCHEME_A_ c); 1546 return mk_character (SCHEME_A_ c);
1695 } 1547 }
1696 else 1548 else
1549 {
1550 /* identify base by string index */
1551 const char baseidx[18] = "ffbf" "ffff" "ofdf" "ffff" "x";
1552 char *base = strchr (baseidx, *name);
1553
1554 if (base && *base)
1555 return mk_integer (SCHEME_A_ strtol (name + 1, 0, base - baseidx));
1556
1697 return NIL; 1557 return NIL;
1558 }
1698} 1559}
1699 1560
1700/* ========== garbage collector ========== */ 1561/* ========== garbage collector ========== */
1562
1563static void
1564finalize_cell (SCHEME_P_ pointer a)
1565{
1566 /* TODO, fast bitmap check? */
1567 if (is_string (a) || is_symbol (a))
1568 free (strvalue (a));
1569 else if (is_vector (a))
1570 free (vecvalue (a));
1571#if USE_PORTS
1572 else if (is_port (a))
1573 {
1574 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1575 port_close (SCHEME_A_ a, port_input | port_output);
1576
1577 free (port (a));
1578 }
1579#endif
1580}
1701 1581
1702/*-- 1582/*--
1703 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, 1583 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1,
1704 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1584 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1705 * for marking. 1585 * for marking.
1586 *
1587 * The exception is vectors - vectors are currently marked recursively,
1588 * which is inherited form tinyscheme and could be fixed by having another
1589 * word of context in the vector
1706 */ 1590 */
1707static void 1591ecb_hot static void
1708mark (pointer a) 1592mark (pointer a)
1709{ 1593{
1710 pointer t, q, p; 1594 pointer t, q, p;
1711 1595
1712 t = (pointer) 0; 1596 t = 0;
1713 p = a; 1597 p = a;
1714E2: 1598E2:
1715 setmark (p); 1599 setmark (p);
1716 printf ("mark %p %x\n", p, p->flag);//D
1717 1600
1718 if (is_vector (p)) 1601 if (ecb_expect_false (is_vector (p)))
1719 { 1602 {
1720 int i; 1603 int i;
1721 int num = ivalue_unchecked (p) / 2 + ivalue_unchecked (p) % 2;
1722 1604
1723 for (i = 0; i < num; i++) 1605 for (i = 0; i < veclength (p); i++)
1724 { 1606 mark (vecvalue (p)[i]);
1725 /* Vector cells will be treated like ordinary cells */
1726 mark (p + 1 + i);
1727 }
1728 } 1607 }
1729 1608
1730 if (is_atom (p)) 1609 if (is_atom (p))
1731 goto E6; 1610 goto E6;
1732 1611
1742 goto E2; 1621 goto E2;
1743 } 1622 }
1744 1623
1745E5: 1624E5:
1746 q = cdr (p); /* down cdr */ 1625 q = cdr (p); /* down cdr */
1747 printf ("mark+ %p\n", q, q->flag);//D
1748 1626
1749 if (q && !is_mark (q)) 1627 if (q && !is_mark (q))
1750 { 1628 {
1751 set_cdr (p, t); 1629 set_cdr (p, t);
1752 t = p; 1630 t = p;
1753 p = q; 1631 p = q;
1754 goto E2; 1632 goto E2;
1755 } 1633 }
1756 1634
1757E6: /* up. Undo the link switching from steps E4 and E5. */ 1635E6: /* up. Undo the link switching from steps E4 and E5. */
1758
1759 if (!t) 1636 if (!t)
1760 return; 1637 return;
1761 1638
1762 q = t; 1639 q = t;
1763 1640
1776 p = q; 1653 p = q;
1777 goto E6; 1654 goto E6;
1778 } 1655 }
1779} 1656}
1780 1657
1658ecb_hot static void
1659gc_free (SCHEME_P)
1660{
1661 int i;
1662 uint32_t total = 0;
1663
1664 /* Here we scan the cells to build the free-list. */
1665 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1666 {
1667 struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1668 struct cell *p;
1669 total += SCHEME_V->cell_segsize [i];
1670
1671 for (p = SCHEME_V->cell_seg[i]; p < end; ++p)
1672 {
1673 pointer c = POINTER (p);
1674
1675 if (is_mark (c))
1676 clrmark (c);
1677 else
1678 {
1679 /* reclaim cell */
1680 if (typeflag (c) != T_PAIR)
1681 {
1682 finalize_cell (SCHEME_A_ c);
1683 set_typeflag (c, T_PAIR);
1684 set_car (c, NIL);
1685 }
1686
1687 ++SCHEME_V->fcells;
1688 set_cdr (c, SCHEME_V->free_cell);
1689 SCHEME_V->free_cell = c;
1690 }
1691 }
1692 }
1693
1694 if (SCHEME_V->gc_verbose)
1695 {
1696 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");
1697 }
1698}
1699
1781/* garbage collection. parameter a, b is marked. */ 1700/* garbage collection. parameter a, b is marked. */
1782static void 1701ecb_cold static void
1783gc (SCHEME_P_ pointer a, pointer b) 1702gc (SCHEME_P_ pointer a, pointer b)
1784{ 1703{
1785 pointer p;
1786 int i; 1704 int i;
1787 1705
1788 if (SCHEME_V->gc_verbose) 1706 if (SCHEME_V->gc_verbose)
1789 putstr (SCHEME_A_ "gc..."); 1707 putstr (SCHEME_A_ "gc...");
1790 1708
1806 /* Mark recent objects the interpreter doesn't know about yet. */ 1724 /* Mark recent objects the interpreter doesn't know about yet. */
1807 mark (car (S_SINK)); 1725 mark (car (S_SINK));
1808 /* Mark any older stuff above nested C calls */ 1726 /* Mark any older stuff above nested C calls */
1809 mark (SCHEME_V->c_nest); 1727 mark (SCHEME_V->c_nest);
1810 1728
1729#if USE_INTCACHE
1730 /* mark intcache */
1731 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1732 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1733 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1734#endif
1735
1811 /* mark variables a, b */ 1736 /* mark variables a, b */
1812 mark (a); 1737 mark (a);
1813 mark (b); 1738 mark (b);
1814 1739
1815 /* garbage collect */ 1740 /* garbage collect */
1816 clrmark (NIL); 1741 clrmark (NIL);
1817 SCHEME_V->fcells = 0; 1742 SCHEME_V->fcells = 0;
1818 SCHEME_V->free_cell = NIL; 1743 SCHEME_V->free_cell = NIL;
1819 1744
1820 /* free-list is kept sorted by address so as to maintain consecutive
1821 ranges, if possible, for use with vectors. Here we scan the cells
1822 (which are also kept sorted by address) downwards to build the
1823 free-list in sorted order.
1824 */
1825 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1826 {
1827 p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1828
1829 while (--p >= SCHEME_V->cell_seg[i])
1830 {
1831 if (is_mark (p))
1832 clrmark (p);
1833 else
1834 {
1835 /* reclaim cell */
1836 if (typeflag (p) != 0)
1837 {
1838 finalize_cell (SCHEME_A_ p);
1839 set_typeflag (p, 0);
1840 set_car (p, NIL);
1841 }
1842
1843 ++SCHEME_V->fcells;
1844 set_cdr (p, SCHEME_V->free_cell);
1845 SCHEME_V->free_cell = p;
1846 }
1847 }
1848 }
1849
1850 if (SCHEME_V->gc_verbose) 1745 if (SCHEME_V->gc_verbose)
1851 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1746 putstr (SCHEME_A_ "freeing...");
1852}
1853 1747
1854static void 1748 gc_free (SCHEME_A);
1855finalize_cell (SCHEME_P_ pointer a)
1856{
1857 if (is_string (a))
1858 free (strvalue (a));
1859#if USE_PORTS
1860 else if (is_port (a))
1861 {
1862 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit)
1863 port_close (SCHEME_A_ a, port_input | port_output);
1864
1865 free (a->object.port);
1866 }
1867#endif
1868} 1749}
1869 1750
1870/* ========== Routines for Reading ========== */ 1751/* ========== Routines for Reading ========== */
1871 1752
1872static int 1753ecb_cold static int
1873file_push (SCHEME_P_ const char *fname) 1754file_push (SCHEME_P_ const char *fname)
1874{ 1755{
1875#if USE_PORTS
1876 int fin; 1756 int fin;
1877 1757
1878 if (SCHEME_V->file_i == MAXFIL - 1) 1758 if (SCHEME_V->file_i == MAXFIL - 1)
1879 return 0; 1759 return 0;
1880 1760
1886 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; 1766 SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1;
1887 SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; 1767 SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input;
1888 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; 1768 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin;
1889 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; 1769 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1;
1890 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; 1770 SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0;
1891 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1771 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1892 1772
1893#if SHOW_ERROR_LINE 1773#if SHOW_ERROR_LINE
1894 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; 1774 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0;
1895 1775
1896 if (fname) 1776 if (fname)
1897 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); 1777 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0);
1898#endif 1778#endif
1899 } 1779 }
1900 1780
1901 return fin >= 0; 1781 return fin >= 0;
1902
1903#else
1904 return 1;
1905#endif
1906} 1782}
1907 1783
1908static void 1784ecb_cold static void
1909file_pop (SCHEME_P) 1785file_pop (SCHEME_P)
1910{ 1786{
1911 if (SCHEME_V->file_i != 0) 1787 if (SCHEME_V->file_i != 0)
1912 { 1788 {
1913 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1789 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1914#if USE_PORTS 1790#if USE_PORTS
1915 port_close (SCHEME_A_ SCHEME_V->loadport, port_input); 1791 port_close (SCHEME_A_ SCHEME_V->loadport, port_input);
1916#endif 1792#endif
1917 SCHEME_V->file_i--; 1793 SCHEME_V->file_i--;
1918 SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; 1794 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1919 } 1795 }
1920} 1796}
1921 1797
1922static int 1798ecb_cold static int
1923file_interactive (SCHEME_P) 1799file_interactive (SCHEME_P)
1924{ 1800{
1925#if USE_PORTS 1801#if USE_PORTS
1926 return SCHEME_V->file_i == 0 1802 return SCHEME_V->file_i == 0
1927 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1803 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1928 && (SCHEME_V->inport->object.port->kind & port_file); 1804 && (port (SCHEME_V->inport)->kind & port_file);
1929#else 1805#else
1930 return 0; 1806 return 0;
1931#endif 1807#endif
1932} 1808}
1933 1809
1934#if USE_PORTS 1810#if USE_PORTS
1935static port * 1811ecb_cold static port *
1936port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1812port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1937{ 1813{
1938 int fd; 1814 int fd;
1939 int flags; 1815 int flags;
1940 char *rw; 1816 char *rw;
1963# endif 1839# endif
1964 1840
1965 return pt; 1841 return pt;
1966} 1842}
1967 1843
1968static pointer 1844ecb_cold static pointer
1969port_from_filename (SCHEME_P_ const char *fn, int prop) 1845port_from_filename (SCHEME_P_ const char *fn, int prop)
1970{ 1846{
1971 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1847 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1972 1848
1973 if (!pt && USE_ERROR_CHECKING) 1849 if (!pt && USE_ERROR_CHECKING)
1974 return NIL; 1850 return NIL;
1975 1851
1976 return mk_port (SCHEME_A_ pt); 1852 return mk_port (SCHEME_A_ pt);
1977} 1853}
1978 1854
1979static port * 1855ecb_cold static port *
1980port_rep_from_file (SCHEME_P_ int f, int prop) 1856port_rep_from_file (SCHEME_P_ int f, int prop)
1981{ 1857{
1982 port *pt = malloc (sizeof *pt); 1858 port *pt = malloc (sizeof *pt);
1983 1859
1984 if (!pt && USE_ERROR_CHECKING) 1860 if (!pt && USE_ERROR_CHECKING)
1989 pt->rep.stdio.file = f; 1865 pt->rep.stdio.file = f;
1990 pt->rep.stdio.closeit = 0; 1866 pt->rep.stdio.closeit = 0;
1991 return pt; 1867 return pt;
1992} 1868}
1993 1869
1994static pointer 1870ecb_cold static pointer
1995port_from_file (SCHEME_P_ int f, int prop) 1871port_from_file (SCHEME_P_ int f, int prop)
1996{ 1872{
1997 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1873 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1998 1874
1999 if (!pt && USE_ERROR_CHECKING) 1875 if (!pt && USE_ERROR_CHECKING)
2000 return NIL; 1876 return NIL;
2001 1877
2002 return mk_port (SCHEME_A_ pt); 1878 return mk_port (SCHEME_A_ pt);
2003} 1879}
2004 1880
2005static port * 1881ecb_cold static port *
2006port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1882port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
2007{ 1883{
2008 port *pt = malloc (sizeof (port)); 1884 port *pt = malloc (sizeof (port));
2009 1885
2010 if (!pt && USE_ERROR_CHECKING) 1886 if (!pt && USE_ERROR_CHECKING)
2016 pt->rep.string.curr = start; 1892 pt->rep.string.curr = start;
2017 pt->rep.string.past_the_end = past_the_end; 1893 pt->rep.string.past_the_end = past_the_end;
2018 return pt; 1894 return pt;
2019} 1895}
2020 1896
2021static pointer 1897ecb_cold static pointer
2022port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1898port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
2023{ 1899{
2024 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); 1900 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop);
2025 1901
2026 if (!pt && USE_ERROR_CHECKING) 1902 if (!pt && USE_ERROR_CHECKING)
2029 return mk_port (SCHEME_A_ pt); 1905 return mk_port (SCHEME_A_ pt);
2030} 1906}
2031 1907
2032# define BLOCK_SIZE 256 1908# define BLOCK_SIZE 256
2033 1909
2034static port * 1910ecb_cold static port *
2035port_rep_from_scratch (SCHEME_P) 1911port_rep_from_scratch (SCHEME_P)
2036{ 1912{
2037 char *start; 1913 char *start;
2038 port *pt = malloc (sizeof (port)); 1914 port *pt = malloc (sizeof (port));
2039 1915
2053 pt->rep.string.curr = start; 1929 pt->rep.string.curr = start;
2054 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1930 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
2055 return pt; 1931 return pt;
2056} 1932}
2057 1933
2058static pointer 1934ecb_cold static pointer
2059port_from_scratch (SCHEME_P) 1935port_from_scratch (SCHEME_P)
2060{ 1936{
2061 port *pt = port_rep_from_scratch (SCHEME_A); 1937 port *pt = port_rep_from_scratch (SCHEME_A);
2062 1938
2063 if (!pt && USE_ERROR_CHECKING) 1939 if (!pt && USE_ERROR_CHECKING)
2064 return NIL; 1940 return NIL;
2065 1941
2066 return mk_port (SCHEME_A_ pt); 1942 return mk_port (SCHEME_A_ pt);
2067} 1943}
2068 1944
2069static void 1945ecb_cold static void
2070port_close (SCHEME_P_ pointer p, int flag) 1946port_close (SCHEME_P_ pointer p, int flag)
2071{ 1947{
2072 port *pt = p->object.port; 1948 port *pt = port (p);
2073 1949
2074 pt->kind &= ~flag; 1950 pt->kind &= ~flag;
2075 1951
2076 if ((pt->kind & (port_input | port_output)) == 0) 1952 if ((pt->kind & (port_input | port_output)) == 0)
2077 { 1953 {
2094 } 1970 }
2095} 1971}
2096#endif 1972#endif
2097 1973
2098/* get new character from input file */ 1974/* get new character from input file */
2099static int 1975ecb_cold static int
2100inchar (SCHEME_P) 1976inchar (SCHEME_P)
2101{ 1977{
2102 int c; 1978 int c;
2103 port *pt; 1979 port *pt = port (SCHEME_V->inport);
2104
2105 pt = SCHEME_V->inport->object.port;
2106 1980
2107 if (pt->kind & port_saw_EOF) 1981 if (pt->kind & port_saw_EOF)
2108 return EOF; 1982 return EOF;
2109 1983
2110 c = basic_inchar (pt); 1984 c = basic_inchar (pt);
2120 } 1994 }
2121 1995
2122 return c; 1996 return c;
2123} 1997}
2124 1998
2125static int ungot = -1; 1999ecb_cold static int
2126
2127static int
2128basic_inchar (port *pt) 2000basic_inchar (port *pt)
2129{ 2001{
2130#if USE_PORTS
2131 if (pt->unget != -1) 2002 if (pt->unget != -1)
2132 { 2003 {
2133 int r = pt->unget; 2004 int r = pt->unget;
2134 pt->unget = -1; 2005 pt->unget = -1;
2135 return r; 2006 return r;
2136 } 2007 }
2137 2008
2009#if USE_PORTS
2138 if (pt->kind & port_file) 2010 if (pt->kind & port_file)
2139 { 2011 {
2140 char c; 2012 char c;
2141 2013
2142 if (!read (pt->rep.stdio.file, &c, 1)) 2014 if (!read (pt->rep.stdio.file, &c, 1))
2150 return EOF; 2022 return EOF;
2151 else 2023 else
2152 return *pt->rep.string.curr++; 2024 return *pt->rep.string.curr++;
2153 } 2025 }
2154#else 2026#else
2155 if (ungot == -1)
2156 {
2157 char c; 2027 char c;
2158 if (!read (0, &c, 1)) 2028
2029 if (!read (pt->rep.stdio.file, &c, 1))
2159 return EOF; 2030 return EOF;
2160 2031
2161 ungot = c;
2162 }
2163
2164 {
2165 int r = ungot;
2166 ungot = -1;
2167 return r; 2032 return c;
2168 }
2169#endif 2033#endif
2170} 2034}
2171 2035
2172/* back character to input buffer */ 2036/* back character to input buffer */
2173static void 2037ecb_cold static void
2174backchar (SCHEME_P_ int c) 2038backchar (SCHEME_P_ int c)
2175{ 2039{
2176#if USE_PORTS 2040 port *pt = port (SCHEME_V->inport);
2177 port *pt;
2178 2041
2179 if (c == EOF) 2042 if (c == EOF)
2180 return; 2043 return;
2181 2044
2182 pt = SCHEME_V->inport->object.port;
2183 pt->unget = c; 2045 pt->unget = c;
2184#else
2185 if (c == EOF)
2186 return;
2187
2188 ungot = c;
2189#endif
2190} 2046}
2191 2047
2192#if USE_PORTS 2048#if USE_PORTS
2193static int 2049ecb_cold static int
2194realloc_port_string (SCHEME_P_ port *p) 2050realloc_port_string (SCHEME_P_ port *p)
2195{ 2051{
2196 char *start = p->rep.string.start; 2052 char *start = p->rep.string.start;
2197 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; 2053 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE;
2198 char *str = malloc (new_size); 2054 char *str = malloc (new_size);
2211 else 2067 else
2212 return 0; 2068 return 0;
2213} 2069}
2214#endif 2070#endif
2215 2071
2216INTERFACE void 2072ecb_cold static void
2217putstr (SCHEME_P_ const char *s) 2073putchars (SCHEME_P_ const char *s, int len)
2218{ 2074{
2075 port *pt = port (SCHEME_V->outport);
2076
2219#if USE_PORTS 2077#if USE_PORTS
2220 port *pt = SCHEME_V->outport->object.port;
2221
2222 if (pt->kind & port_file)
2223 write (pt->rep.stdio.file, s, strlen (s));
2224 else
2225 for (; *s; s++)
2226 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2227 *pt->rep.string.curr++ = *s;
2228 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2229 *pt->rep.string.curr++ = *s;
2230
2231#else
2232 xwrstr (s);
2233#endif
2234}
2235
2236static void
2237putchars (SCHEME_P_ const char *s, int len)
2238{
2239#if USE_PORTS
2240 port *pt = SCHEME_V->outport->object.port;
2241
2242 if (pt->kind & port_file) 2078 if (pt->kind & port_file)
2243 write (pt->rep.stdio.file, s, len); 2079 write (pt->rep.stdio.file, s, len);
2244 else 2080 else
2245 { 2081 {
2246 for (; len; len--) 2082 for (; len; len--)
2251 *pt->rep.string.curr++ = *s++; 2087 *pt->rep.string.curr++ = *s++;
2252 } 2088 }
2253 } 2089 }
2254 2090
2255#else 2091#else
2256 write (1, s, len); 2092 write (1, s, len); // output not initialised
2257#endif 2093#endif
2094}
2095
2096INTERFACE void
2097putstr (SCHEME_P_ const char *s)
2098{
2099 putchars (SCHEME_A_ s, strlen (s));
2258} 2100}
2259 2101
2260INTERFACE void 2102INTERFACE void
2261putcharacter (SCHEME_P_ int c) 2103putcharacter (SCHEME_P_ int c)
2262{ 2104{
2263#if USE_PORTS
2264 port *pt = SCHEME_V->outport->object.port;
2265
2266 if (pt->kind & port_file)
2267 {
2268 char cc = c;
2269 write (pt->rep.stdio.file, &cc, 1);
2270 }
2271 else
2272 {
2273 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2274 *pt->rep.string.curr++ = c;
2275 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2276 *pt->rep.string.curr++ = c;
2277 }
2278
2279#else
2280 char cc = c; 2105 char cc = c;
2281 write (1, &c, 1); 2106
2282#endif 2107 putchars (SCHEME_A_ &cc, 1);
2283} 2108}
2284 2109
2285/* read characters up to delimiter, but cater to character constants */ 2110/* read characters up to delimiter, but cater to character constants */
2286static char * 2111ecb_cold static char *
2287readstr_upto (SCHEME_P_ char *delim) 2112readstr_upto (SCHEME_P_ int skip, const char *delim)
2288{ 2113{
2289 char *p = SCHEME_V->strbuff; 2114 char *p = SCHEME_V->strbuff + skip;
2290 2115
2291 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2116 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2292 2117
2293 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2118 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2294 {
2295 *p = 0; 2119 *p = 0;
2296 }
2297 else 2120 else
2298 { 2121 {
2299 backchar (SCHEME_A_ p[-1]); 2122 backchar (SCHEME_A_ p[-1]);
2300 *--p = '\0'; 2123 *--p = '\0';
2301 } 2124 }
2302 2125
2303 return SCHEME_V->strbuff; 2126 return SCHEME_V->strbuff;
2304} 2127}
2305 2128
2306/* read string expression "xxx...xxx" */ 2129/* read string expression "xxx...xxx" */
2307static pointer 2130ecb_cold static pointer
2308readstrexp (SCHEME_P) 2131readstrexp (SCHEME_P_ char delim)
2309{ 2132{
2310 char *p = SCHEME_V->strbuff; 2133 char *p = SCHEME_V->strbuff;
2311 int c; 2134 int c;
2312 int c1 = 0; 2135 int c1 = 0;
2313 enum
2314 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2136 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2315 2137
2316 for (;;) 2138 for (;;)
2317 { 2139 {
2318 c = inchar (SCHEME_A); 2140 c = inchar (SCHEME_A);
2319 2141
2320 if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1) 2142 if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1)
2321 {
2322 return S_F; 2143 return S_F;
2323 }
2324 2144
2325 switch (state) 2145 switch (state)
2326 { 2146 {
2327 case st_ok: 2147 case st_ok:
2328 switch (c) 2148 if (ecb_expect_false (c == delim))
2329 {
2330 case '\\':
2331 state = st_bsl;
2332 break;
2333
2334 case '"':
2335 *p = 0;
2336 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); 2149 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2337 2150
2338 default: 2151 if (ecb_expect_false (c == '\\'))
2152 state = st_bsl;
2153 else
2339 *p++ = c; 2154 *p++ = c;
2340 break;
2341 }
2342 2155
2343 break; 2156 break;
2344 2157
2345 case st_bsl: 2158 case st_bsl:
2346 switch (c) 2159 switch (c)
2355 case '7': 2168 case '7':
2356 state = st_oct1; 2169 state = st_oct1;
2357 c1 = c - '0'; 2170 c1 = c - '0';
2358 break; 2171 break;
2359 2172
2173 case 'a': *p++ = '\a'; state = st_ok; break;
2174 case 'n': *p++ = '\n'; state = st_ok; break;
2175 case 'r': *p++ = '\r'; state = st_ok; break;
2176 case 't': *p++ = '\t'; state = st_ok; break;
2177
2178 // this overshoots the minimum requirements of r7rs
2179 case ' ':
2180 case '\t':
2181 case '\r':
2182 case '\n':
2183 skipspace (SCHEME_A);
2184 state = st_ok;
2185 break;
2186
2187 //TODO: x should end in ;, not two-digit hex
2360 case 'x': 2188 case 'x':
2361 case 'X': 2189 case 'X':
2362 state = st_x1; 2190 state = st_x1;
2363 c1 = 0; 2191 c1 = 0;
2364 break; 2192 break;
2365 2193
2366 case 'n':
2367 *p++ = '\n';
2368 state = st_ok;
2369 break;
2370
2371 case 't':
2372 *p++ = '\t';
2373 state = st_ok;
2374 break;
2375
2376 case 'r':
2377 *p++ = '\r';
2378 state = st_ok;
2379 break;
2380
2381 case '"':
2382 *p++ = '"';
2383 state = st_ok;
2384 break;
2385
2386 default: 2194 default:
2387 *p++ = c; 2195 *p++ = c;
2388 state = st_ok; 2196 state = st_ok;
2389 break; 2197 break;
2390 } 2198 }
2391 2199
2392 break; 2200 break;
2393 2201
2394 case st_x1: 2202 case st_x1:
2395 case st_x2: 2203 case st_x2:
2396 c = toupper (c); 2204 c = tolower (c);
2397 2205
2398 if (c >= '0' && c <= 'F') 2206 if (c >= '0' && c <= '9')
2399 {
2400 if (c <= '9')
2401 {
2402 c1 = (c1 << 4) + c - '0'; 2207 c1 = (c1 << 4) + c - '0';
2403 } 2208 else if (c >= 'a' && c <= 'f')
2404 else
2405 {
2406 c1 = (c1 << 4) + c - 'A' + 10; 2209 c1 = (c1 << 4) + c - 'a' + 10;
2407 } 2210 else
2211 return S_F;
2408 2212
2409 if (state == st_x1) 2213 if (state == st_x1)
2410 {
2411 state = st_x2; 2214 state = st_x2;
2412 }
2413 else
2414 {
2415 *p++ = c1;
2416 state = st_ok;
2417 }
2418 }
2419 else 2215 else
2420 { 2216 {
2421 return S_F; 2217 *p++ = c1;
2218 state = st_ok;
2422 } 2219 }
2423 2220
2424 break; 2221 break;
2425 2222
2426 case st_oct1: 2223 case st_oct1:
2431 backchar (SCHEME_A_ c); 2228 backchar (SCHEME_A_ c);
2432 state = st_ok; 2229 state = st_ok;
2433 } 2230 }
2434 else 2231 else
2435 { 2232 {
2436 if (state == st_oct2 && c1 >= 32) 2233 if (state == st_oct2 && c1 >= ' ')
2437 return S_F; 2234 return S_F;
2438 2235
2439 c1 = (c1 << 3) + (c - '0'); 2236 c1 = (c1 << 3) + (c - '0');
2440 2237
2441 if (state == st_oct1) 2238 if (state == st_oct1)
2446 state = st_ok; 2243 state = st_ok;
2447 } 2244 }
2448 } 2245 }
2449 2246
2450 break; 2247 break;
2451
2452 } 2248 }
2453 } 2249 }
2454} 2250}
2455 2251
2456/* check c is in chars */ 2252/* check c is in chars */
2457static INLINE int 2253ecb_cold int
2458is_one_of (char *s, int c) 2254is_one_of (const char *s, int c)
2459{ 2255{
2460 if (c == EOF) 2256 return c == EOF || !!strchr (s, c);
2461 return 1;
2462
2463 while (*s)
2464 if (*s++ == c)
2465 return 1;
2466
2467 return 0;
2468} 2257}
2469 2258
2470/* skip white characters */ 2259/* skip white characters */
2471static INLINE int 2260ecb_cold int
2472skipspace (SCHEME_P) 2261skipspace (SCHEME_P)
2473{ 2262{
2474 int c, curr_line = 0; 2263 int c, curr_line = 0;
2475 2264
2476 do 2265 do
2477 { 2266 {
2478 c = inchar (SCHEME_A); 2267 c = inchar (SCHEME_A);
2268
2479#if SHOW_ERROR_LINE 2269#if SHOW_ERROR_LINE
2480 if (c == '\n') 2270 if (ecb_expect_false (c == '\n'))
2481 curr_line++; 2271 curr_line++;
2482
2483#endif 2272#endif
2273
2274 if (ecb_expect_false (c == EOF))
2275 return c;
2484 } 2276 }
2485 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2277 while (is_one_of (WHITESPACE, c));
2486 2278
2487 /* record it */ 2279 /* record it */
2488#if SHOW_ERROR_LINE 2280#if SHOW_ERROR_LINE
2489 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 2281 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file)
2490 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; 2282 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line;
2491#endif 2283#endif
2492 2284
2493 if (c != EOF)
2494 {
2495 backchar (SCHEME_A_ c); 2285 backchar (SCHEME_A_ c);
2496 return 1; 2286 return 1;
2497 }
2498 else
2499 return EOF;
2500} 2287}
2501 2288
2502/* get token */ 2289/* get token */
2503static int 2290ecb_cold static int
2504token (SCHEME_P) 2291token (SCHEME_P)
2505{ 2292{
2506 int c = skipspace (SCHEME_A); 2293 int c = skipspace (SCHEME_A);
2507 2294
2508 if (c == EOF) 2295 if (c == EOF)
2520 return TOK_RPAREN; 2307 return TOK_RPAREN;
2521 2308
2522 case '.': 2309 case '.':
2523 c = inchar (SCHEME_A); 2310 c = inchar (SCHEME_A);
2524 2311
2525 if (is_one_of (" \n\t", c)) 2312 if (is_one_of (WHITESPACE, c))
2526 return TOK_DOT; 2313 return TOK_DOT;
2527 else 2314 else
2528 { 2315 {
2529 backchar (SCHEME_A_ c); 2316 backchar (SCHEME_A_ c);
2530 backchar (SCHEME_A_ '.');
2531 return TOK_ATOM; 2317 return TOK_DOTATOM;
2532 } 2318 }
2319
2320 case '|':
2321 return TOK_STRATOM;
2533 2322
2534 case '\'': 2323 case '\'':
2535 return TOK_QUOTE; 2324 return TOK_QUOTE;
2536 2325
2537 case ';': 2326 case ';':
2604} 2393}
2605 2394
2606/* ========== Routines for Printing ========== */ 2395/* ========== Routines for Printing ========== */
2607#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2396#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2608 2397
2609static void 2398ecb_cold static void
2610printslashstring (SCHEME_P_ char *p, int len) 2399printslashstring (SCHEME_P_ char *p, int len)
2611{ 2400{
2612 int i; 2401 int i;
2613 unsigned char *s = (unsigned char *) p; 2402 unsigned char *s = (unsigned char *) p;
2614 2403
2647 int d = *s / 16; 2436 int d = *s / 16;
2648 2437
2649 putcharacter (SCHEME_A_ 'x'); 2438 putcharacter (SCHEME_A_ 'x');
2650 2439
2651 if (d < 10) 2440 if (d < 10)
2652 {
2653 putcharacter (SCHEME_A_ d + '0'); 2441 putcharacter (SCHEME_A_ d + '0');
2654 }
2655 else 2442 else
2656 {
2657 putcharacter (SCHEME_A_ d - 10 + 'A'); 2443 putcharacter (SCHEME_A_ d - 10 + 'A');
2658 }
2659 2444
2660 d = *s % 16; 2445 d = *s % 16;
2661 2446
2662 if (d < 10) 2447 if (d < 10)
2663 {
2664 putcharacter (SCHEME_A_ d + '0'); 2448 putcharacter (SCHEME_A_ d + '0');
2665 }
2666 else 2449 else
2667 {
2668 putcharacter (SCHEME_A_ d - 10 + 'A'); 2450 putcharacter (SCHEME_A_ d - 10 + 'A');
2669 }
2670 } 2451 }
2671 } 2452 }
2672 } 2453 }
2673 else 2454 else
2674 {
2675 putcharacter (SCHEME_A_ * s); 2455 putcharacter (SCHEME_A_ * s);
2676 }
2677 2456
2678 s++; 2457 s++;
2679 } 2458 }
2680 2459
2681 putcharacter (SCHEME_A_ '"'); 2460 putcharacter (SCHEME_A_ '"');
2682} 2461}
2683 2462
2684
2685/* print atoms */ 2463/* print atoms */
2686static void 2464ecb_cold static void
2687printatom (SCHEME_P_ pointer l, int f) 2465printatom (SCHEME_P_ pointer l, int f)
2688{ 2466{
2689 char *p; 2467 char *p;
2690 int len; 2468 int len;
2691 2469
2692 atom2str (SCHEME_A_ l, f, &p, &len); 2470 atom2str (SCHEME_A_ l, f, &p, &len);
2693 putchars (SCHEME_A_ p, len); 2471 putchars (SCHEME_A_ p, len);
2694} 2472}
2695 2473
2696
2697/* Uses internal buffer unless string pointer is already available */ 2474/* Uses internal buffer unless string pointer is already available */
2698static void 2475ecb_cold static void
2699atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2476atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2700{ 2477{
2701 char *p; 2478 char *p;
2702 2479
2703 if (l == NIL) 2480 if (l == NIL)
2714 { 2491 {
2715 p = SCHEME_V->strbuff; 2492 p = SCHEME_V->strbuff;
2716 2493
2717 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2494 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2718 { 2495 {
2719 if (num_is_integer (l)) 2496 if (is_integer (l))
2720 xnum (p, ivalue_unchecked (l)); 2497 xnum (p, ivalue_unchecked (l));
2721#if USE_FLOAT 2498#if USE_REAL
2722 else 2499 else
2723 { 2500 {
2724 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2501 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2725 /* r5rs says there must be a '.' (unless 'e'?) */ 2502 /* r5rs says there must be a '.' (unless 'e'?) */
2726 f = strcspn (p, ".e"); 2503 f = strcspn (p, ".e");
2836 } 2613 }
2837 else if (is_symbol (l)) 2614 else if (is_symbol (l))
2838 p = symname (l); 2615 p = symname (l);
2839 else if (is_proc (l)) 2616 else if (is_proc (l))
2840 { 2617 {
2618 p = (char *)procname (l); // ok with r7rs display, but not r7rs write
2619#if 0
2841#if USE_PRINTF 2620#if USE_PRINTF
2842 p = SCHEME_V->strbuff; 2621 p = SCHEME_V->strbuff;
2843 snprintf (p, STRBUFFSIZE, "#<%s PROCEDURE %ld>", procname (l), procnum (l)); 2622 snprintf (p, STRBUFFSIZE, " PROCEDURE %ld>", procname (l), procnum (l));
2844#else 2623#else
2845 p = "#<PROCEDURE>"; 2624 p = "#<PROCEDURE>";
2625#endif
2846#endif 2626#endif
2847 } 2627 }
2848 else if (is_macro (l)) 2628 else if (is_macro (l))
2849 p = "#<MACRO>"; 2629 p = "#<MACRO>";
2850 else if (is_closure (l)) 2630 else if (is_closure (l))
2861#endif 2641#endif
2862 } 2642 }
2863 else if (is_continuation (l)) 2643 else if (is_continuation (l))
2864 p = "#<CONTINUATION>"; 2644 p = "#<CONTINUATION>";
2865 else 2645 else
2646 {
2647#if USE_PRINTF
2648 p = SCHEME_V->strbuff;
2649 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2650#else
2866 p = "#<ERROR>"; 2651 p = "#<ERROR>";
2652#endif
2653 }
2867 2654
2868 *pp = p; 2655 *pp = p;
2869 *plen = strlen (p); 2656 *plen = strlen (p);
2870} 2657}
2871 2658
2898list_star (SCHEME_P_ pointer d) 2685list_star (SCHEME_P_ pointer d)
2899{ 2686{
2900 pointer p, q; 2687 pointer p, q;
2901 2688
2902 if (cdr (d) == NIL) 2689 if (cdr (d) == NIL)
2903 {
2904 return car (d); 2690 return car (d);
2905 }
2906 2691
2907 p = cons (car (d), cdr (d)); 2692 p = cons (car (d), cdr (d));
2908 q = p; 2693 q = p;
2909 2694
2910 while (cdr (cdr (p)) != NIL) 2695 while (cddr (p) != NIL)
2911 { 2696 {
2912 d = cons (car (p), cdr (p)); 2697 d = cons (car (p), cdr (p));
2913 2698
2914 if (cdr (cdr (p)) != NIL) 2699 if (cddr (p) != NIL)
2915 {
2916 p = cdr (d); 2700 p = cdr (d);
2917 }
2918 } 2701 }
2919 2702
2920 set_cdr (p, car (cdr (p))); 2703 set_cdr (p, cadr (p));
2921 return q; 2704 return q;
2922} 2705}
2923 2706
2924/* reverse list -- produce new list */ 2707/* reverse list -- produce new list */
2925static pointer 2708ecb_hot static pointer
2926reverse (SCHEME_P_ pointer a) 2709reverse (SCHEME_P_ pointer a)
2927{ 2710{
2928 /* a must be checked by gc */ 2711 /* a must be checked by gc */
2929 pointer p = NIL; 2712 pointer p = NIL;
2930 2713
2933 2716
2934 return p; 2717 return p;
2935} 2718}
2936 2719
2937/* reverse list --- in-place */ 2720/* reverse list --- in-place */
2938static pointer 2721ecb_hot static pointer
2939reverse_in_place (SCHEME_P_ pointer term, pointer list) 2722reverse_in_place (SCHEME_P_ pointer term, pointer list)
2940{ 2723{
2941 pointer p = list, result = term, q; 2724 pointer result = term;
2725 pointer p = list;
2942 2726
2943 while (p != NIL) 2727 while (p != NIL)
2944 { 2728 {
2945 q = cdr (p); 2729 pointer q = cdr (p);
2946 set_cdr (p, result); 2730 set_cdr (p, result);
2947 result = p; 2731 result = p;
2948 p = q; 2732 p = q;
2949 } 2733 }
2950 2734
2951 return result; 2735 return result;
2952} 2736}
2953 2737
2954/* append list -- produce new list (in reverse order) */ 2738/* append list -- produce new list (in reverse order) */
2955static pointer 2739ecb_hot static pointer
2956revappend (SCHEME_P_ pointer a, pointer b) 2740revappend (SCHEME_P_ pointer a, pointer b)
2957{ 2741{
2958 pointer result = a; 2742 pointer result = a;
2959 pointer p = b; 2743 pointer p = b;
2960 2744
2969 2753
2970 return S_F; /* signal an error */ 2754 return S_F; /* signal an error */
2971} 2755}
2972 2756
2973/* equivalence of atoms */ 2757/* equivalence of atoms */
2974int 2758ecb_hot int
2975eqv (pointer a, pointer b) 2759eqv (pointer a, pointer b)
2976{ 2760{
2977 if (is_string (a)) 2761 if (is_string (a))
2978 { 2762 {
2979 if (is_string (b)) 2763 if (is_string (b))
2982 return 0; 2766 return 0;
2983 } 2767 }
2984 else if (is_number (a)) 2768 else if (is_number (a))
2985 { 2769 {
2986 if (is_number (b)) 2770 if (is_number (b))
2987 if (num_is_integer (a) == num_is_integer (b))
2988 return num_eq (nvalue (a), nvalue (b)); 2771 return num_cmp (nvalue (a), nvalue (b)) == 0;
2989 2772
2990 return 0; 2773 return 0;
2991 } 2774 }
2992 else if (is_character (a)) 2775 else if (is_character (a))
2993 { 2776 {
3019/* () is #t in R5RS */ 2802/* () is #t in R5RS */
3020#define is_true(p) ((p) != S_F) 2803#define is_true(p) ((p) != S_F)
3021#define is_false(p) ((p) == S_F) 2804#define is_false(p) ((p) == S_F)
3022 2805
3023/* ========== Environment implementation ========== */ 2806/* ========== Environment implementation ========== */
3024
3025#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
3026
3027static int
3028hash_fn (const char *key, int table_size)
3029{
3030 unsigned int hashed = 0;
3031 const char *c;
3032 int bits_per_int = sizeof (unsigned int) * 8;
3033
3034 for (c = key; *c; c++)
3035 {
3036 /* letters have about 5 bits in them */
3037 hashed = (hashed << 5) | (hashed >> (bits_per_int - 5));
3038 hashed ^= *c;
3039 }
3040
3041 return hashed % table_size;
3042}
3043#endif
3044 2807
3045#ifndef USE_ALIST_ENV 2808#ifndef USE_ALIST_ENV
3046 2809
3047/* 2810/*
3048 * In this implementation, each frame of the environment may be 2811 * In this implementation, each frame of the environment may be
3057{ 2820{
3058 pointer new_frame; 2821 pointer new_frame;
3059 2822
3060 /* The interaction-environment has about 300 variables in it. */ 2823 /* The interaction-environment has about 300 variables in it. */
3061 if (old_env == NIL) 2824 if (old_env == NIL)
3062 new_frame = mk_vector (SCHEME_A_ 461); 2825 new_frame = mk_vector (SCHEME_A_ 29); // was 461
3063 else 2826 else
3064 new_frame = NIL; 2827 new_frame = NIL;
3065 2828
3066 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2829 SCHEME_V->envir = immutable_cons (new_frame, old_env);
3067 setenvironment (SCHEME_V->envir); 2830 setenvironment (SCHEME_V->envir);
3068} 2831}
3069 2832
3070static INLINE void 2833static uint32_t
2834sym_hash (pointer sym, uint32_t size)
2835{
2836 uintptr_t ptr = (uintptr_t)sym;
2837
2838#if 0
2839 /* table size is prime, so why mix */
2840 ptr += ptr >> 32;
2841 ptr += ptr >> 16;
2842 ptr += ptr >> 8;
2843#endif
2844
2845 return ptr % size;
2846}
2847
2848ecb_inline void
3071new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2849new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
3072{ 2850{
3073 pointer slot = immutable_cons (variable, value); 2851 pointer slot = immutable_cons (variable, value);
3074 2852
3075 if (is_vector (car (env))) 2853 if (is_vector (car (env)))
3076 { 2854 {
3077 int location = hash_fn (symname (variable), ivalue_unchecked (car (env))); 2855 int location = sym_hash (variable, veclength (car (env)));
3078
3079 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2856 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
3080 } 2857 }
3081 else 2858 else
3082 set_car (env, immutable_cons (slot, car (env))); 2859 set_car (env, immutable_cons (slot, car (env)));
3083} 2860}
3084 2861
3085static pointer 2862ecb_hot static pointer
3086find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2863find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
3087{ 2864{
3088 pointer x, y; 2865 pointer x, y;
3089 int location;
3090 2866
3091 for (x = env; x != NIL; x = cdr (x)) 2867 for (x = env; x != NIL; x = cdr (x))
3092 { 2868 {
3093 if (is_vector (car (x))) 2869 if (is_vector (car (x)))
3094 { 2870 {
3095 location = hash_fn (symname (hdl), ivalue_unchecked (car (x))); 2871 int location = sym_hash (hdl, veclength (car (x)));
3096 y = vector_elem (car (x), location); 2872 y = vector_get (car (x), location);
3097 } 2873 }
3098 else 2874 else
3099 y = car (x); 2875 y = car (x);
3100 2876
3101 for (; y != NIL; y = cdr (y)) 2877 for (; y != NIL; y = cdr (y))
3102 if (caar (y) == hdl) 2878 if (caar (y) == hdl)
3103 break; 2879 break;
3104 2880
3105 if (y != NIL) 2881 if (y != NIL)
2882 return car (y);
2883
2884 if (!all)
3106 break; 2885 break;
3107
3108 if (!all)
3109 return NIL;
3110 } 2886 }
3111
3112 if (x != NIL)
3113 return car (y);
3114 2887
3115 return NIL; 2888 return NIL;
3116} 2889}
3117 2890
3118#else /* USE_ALIST_ENV */ 2891#else /* USE_ALIST_ENV */
3119 2892
3120static INLINE void 2893static void
3121new_frame_in_env (SCHEME_P_ pointer old_env) 2894new_frame_in_env (SCHEME_P_ pointer old_env)
3122{ 2895{
3123 SCHEME_V->envir = immutable_cons (NIL, old_env); 2896 SCHEME_V->envir = immutable_cons (NIL, old_env);
3124 setenvironment (SCHEME_V->envir); 2897 setenvironment (SCHEME_V->envir);
3125} 2898}
3126 2899
3127static INLINE void 2900static void
3128new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2901new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
3129{ 2902{
3130 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2903 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
3131} 2904}
3132 2905
3133static pointer 2906ecb_hot static pointer
3134find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2907find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
3135{ 2908{
3136 pointer x, y; 2909 pointer x, y;
3137 2910
3138 for (x = env; x != NIL; x = cdr (x)) 2911 for (x = env; x != NIL; x = cdr (x))
3140 for (y = car (x); y != NIL; y = cdr (y)) 2913 for (y = car (x); y != NIL; y = cdr (y))
3141 if (caar (y) == hdl) 2914 if (caar (y) == hdl)
3142 break; 2915 break;
3143 2916
3144 if (y != NIL) 2917 if (y != NIL)
2918 return car (y);
3145 break; 2919 break;
3146 2920
3147 if (!all) 2921 if (!all)
3148 return NIL; 2922 break;
3149 } 2923 }
3150
3151 if (x != NIL)
3152 return car (y);
3153 2924
3154 return NIL; 2925 return NIL;
3155} 2926}
3156 2927
3157#endif /* USE_ALIST_ENV else */ 2928#endif /* USE_ALIST_ENV else */
3158 2929
3159static INLINE void 2930static void
3160new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2931new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
3161{ 2932{
2933 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
3162 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2934 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
3163} 2935}
3164 2936
3165static INLINE void 2937static void
3166set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2938set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
3167{ 2939{
3168 set_cdr (slot, value); 2940 set_cdr (slot, value);
3169} 2941}
3170 2942
3171static INLINE pointer 2943static pointer
3172slot_value_in_env (pointer slot) 2944slot_value_in_env (pointer slot)
3173{ 2945{
3174 return cdr (slot); 2946 return cdr (slot);
3175} 2947}
3176 2948
3177/* ========== Evaluation Cycle ========== */ 2949/* ========== Evaluation Cycle ========== */
3178 2950
3179static pointer 2951ecb_cold static int
3180xError_1 (SCHEME_P_ const char *s, pointer a) 2952xError_1 (SCHEME_P_ const char *s, pointer a)
3181{ 2953{
3182#if USE_ERROR_HOOK
3183 pointer x;
3184 pointer hdl = SCHEME_V->ERROR_HOOK;
3185#endif
3186
3187#if USE_PRINTF 2954#if USE_PRINTF
3188#if SHOW_ERROR_LINE 2955#if SHOW_ERROR_LINE
3189 char sbuf[STRBUFFSIZE]; 2956 char sbuf[STRBUFFSIZE];
3190 2957
3191 /* make sure error is not in REPL */ 2958 /* make sure error is not in REPL */
3206 } 2973 }
3207#endif 2974#endif
3208#endif 2975#endif
3209 2976
3210#if USE_ERROR_HOOK 2977#if USE_ERROR_HOOK
3211 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); 2978 pointer x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->ERROR_HOOK, 1);
3212 2979
3213 if (x != NIL) 2980 if (x != NIL)
3214 { 2981 {
3215 if (a) 2982 pointer code = a
3216 SCHEME_V->code = cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL); 2983 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL)
3217 else 2984 : NIL;
3218 SCHEME_V->code = NIL;
3219 2985
3220 SCHEME_V->code = cons (mk_string (SCHEME_A_ s), SCHEME_V->code); 2986 code = cons (mk_string (SCHEME_A_ s), code);
3221 setimmutable (car (SCHEME_V->code)); 2987 setimmutable (car (code));
3222 SCHEME_V->code = cons (slot_value_in_env (x), SCHEME_V->code); 2988 SCHEME_V->code = cons (slot_value_in_env (x), code);
3223 SCHEME_V->op = OP_EVAL; 2989 SCHEME_V->op = OP_EVAL;
3224 2990
3225 return S_T; 2991 return 0;
3226 } 2992 }
3227#endif 2993#endif
3228 2994
3229 if (a) 2995 if (a)
3230 SCHEME_V->args = cons (a, NIL); 2996 SCHEME_V->args = cons (a, NIL);
3231 else 2997 else
3232 SCHEME_V->args = NIL; 2998 SCHEME_V->args = NIL;
3233 2999
3234 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 3000 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
3235 setimmutable (car (SCHEME_V->args)); 3001 setimmutable (car (SCHEME_V->args));
3236 SCHEME_V->op = (int)OP_ERR0; 3002 SCHEME_V->op = OP_ERR0;
3003
3237 return S_T; 3004 return 0;
3238} 3005}
3239 3006
3240#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 3007#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
3241#define Error_0(s) Error_1 (s, 0) 3008#define Error_0(s) Error_1 (s, 0)
3242 3009
3243/* Too small to turn into function */ 3010/* Too small to turn into function */
3244#define BEGIN do { 3011#define BEGIN do {
3245#define END } while (0) 3012#define END } while (0)
3246#define s_goto(a) BEGIN \ 3013#define s_goto(a) BEGIN \
3247 SCHEME_V->op = (int)(a); \ 3014 SCHEME_V->op = a; \
3248 return S_T; END 3015 return 0; END
3249 3016
3250#define s_return(a) return xs_return(SCHEME_A_ a) 3017#define s_return(a) return xs_return (SCHEME_A_ a)
3251 3018
3252#ifndef USE_SCHEME_STACK 3019#ifndef USE_SCHEME_STACK
3253 3020
3254/* this structure holds all the interpreter's registers */ 3021/* this structure holds all the interpreter's registers */
3255struct dump_stack_frame 3022struct dump_stack_frame
3260 pointer code; 3027 pointer code;
3261}; 3028};
3262 3029
3263# define STACK_GROWTH 3 3030# define STACK_GROWTH 3
3264 3031
3265static void 3032ecb_hot static void
3266s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3033s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3267{ 3034{
3268 int nframes = (uintptr_t)SCHEME_V->dump; 3035 int nframes = (uintptr_t)SCHEME_V->dump;
3269 struct dump_stack_frame *next_frame; 3036 struct dump_stack_frame *next_frame;
3270 3037
3271 /* enough room for the next frame? */ 3038 /* enough room for the next frame? */
3272 if (nframes >= SCHEME_V->dump_size) 3039 if (ecb_expect_false (nframes >= SCHEME_V->dump_size))
3273 { 3040 {
3274 SCHEME_V->dump_size += STACK_GROWTH; 3041 SCHEME_V->dump_size += STACK_GROWTH;
3275 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); 3042 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size);
3276 } 3043 }
3277 3044
3278 next_frame = SCHEME_V->dump_base + nframes; 3045 next_frame = SCHEME_V->dump_base + nframes;
3046
3279 next_frame->op = op; 3047 next_frame->op = op;
3280 next_frame->args = args; 3048 next_frame->args = args;
3281 next_frame->envir = SCHEME_V->envir; 3049 next_frame->envir = SCHEME_V->envir;
3282 next_frame->code = code; 3050 next_frame->code = code;
3051
3283 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3052 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3284} 3053}
3285 3054
3286static pointer 3055static ecb_hot int
3287xs_return (SCHEME_P_ pointer a) 3056xs_return (SCHEME_P_ pointer a)
3288{ 3057{
3289 int nframes = (uintptr_t)SCHEME_V->dump; 3058 int nframes = (uintptr_t)SCHEME_V->dump;
3290 struct dump_stack_frame *frame; 3059 struct dump_stack_frame *frame;
3291 3060
3292 SCHEME_V->value = a; 3061 SCHEME_V->value = a;
3293 3062
3294 if (nframes <= 0) 3063 if (nframes <= 0)
3295 return NIL; 3064 return -1;
3296 3065
3297 nframes--;
3298 frame = SCHEME_V->dump_base + nframes; 3066 frame = &SCHEME_V->dump_base[--nframes];
3299 SCHEME_V->op = frame->op; 3067 SCHEME_V->op = frame->op;
3300 SCHEME_V->args = frame->args; 3068 SCHEME_V->args = frame->args;
3301 SCHEME_V->envir = frame->envir; 3069 SCHEME_V->envir = frame->envir;
3302 SCHEME_V->code = frame->code; 3070 SCHEME_V->code = frame->code;
3303 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3071 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3304 3072
3305 return S_T; 3073 return 0;
3306} 3074}
3307 3075
3308static INLINE void 3076ecb_cold void
3309dump_stack_reset (SCHEME_P) 3077dump_stack_reset (SCHEME_P)
3310{ 3078{
3311 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3079 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3312 SCHEME_V->dump = (pointer)0; 3080 SCHEME_V->dump = (pointer)+0;
3313} 3081}
3314 3082
3315static INLINE void 3083ecb_cold void
3316dump_stack_initialize (SCHEME_P) 3084dump_stack_initialize (SCHEME_P)
3317{ 3085{
3318 SCHEME_V->dump_size = 0; 3086 SCHEME_V->dump_size = 0;
3319 SCHEME_V->dump_base = NULL; 3087 SCHEME_V->dump_base = 0;
3320 dump_stack_reset (SCHEME_A); 3088 dump_stack_reset (SCHEME_A);
3321} 3089}
3322 3090
3323static void 3091ecb_cold static void
3324dump_stack_free (SCHEME_P) 3092dump_stack_free (SCHEME_P)
3325{ 3093{
3326 free (SCHEME_V->dump_base); 3094 free (SCHEME_V->dump_base);
3327 SCHEME_V->dump_base = NULL; 3095 SCHEME_V->dump_base = 0;
3328 SCHEME_V->dump = (pointer)0; 3096 SCHEME_V->dump = (pointer)0;
3329 SCHEME_V->dump_size = 0; 3097 SCHEME_V->dump_size = 0;
3330} 3098}
3331 3099
3332static void 3100ecb_cold static void
3333dump_stack_mark (SCHEME_P) 3101dump_stack_mark (SCHEME_P)
3334{ 3102{
3335 int nframes = (uintptr_t)SCHEME_V->dump; 3103 int nframes = (uintptr_t)SCHEME_V->dump;
3336 int i; 3104 int i;
3337 3105
3343 mark (frame->envir); 3111 mark (frame->envir);
3344 mark (frame->code); 3112 mark (frame->code);
3345 } 3113 }
3346} 3114}
3347 3115
3348static pointer 3116ecb_cold static pointer
3349ss_get_cont (SCHEME_P) 3117ss_get_cont (SCHEME_P)
3350{ 3118{
3351 int nframes = (uintptr_t)SCHEME_V->dump; 3119 int nframes = (uintptr_t)SCHEME_V->dump;
3352 int i; 3120 int i;
3353 3121
3365 } 3133 }
3366 3134
3367 return cont; 3135 return cont;
3368} 3136}
3369 3137
3370static void 3138ecb_cold static void
3371ss_set_cont (SCHEME_P_ pointer cont) 3139ss_set_cont (SCHEME_P_ pointer cont)
3372{ 3140{
3373 int i = 0; 3141 int i = 0;
3374 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3142 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3375 3143
3376 while (cont != NIL) 3144 while (cont != NIL)
3377 { 3145 {
3378 frame->op = ivalue (car (cont)); cont = cdr (cont); 3146 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3379 frame->args = car (cont) ; cont = cdr (cont); 3147 frame->args = car (cont) ; cont = cdr (cont);
3380 frame->envir = car (cont) ; cont = cdr (cont); 3148 frame->envir = car (cont) ; cont = cdr (cont);
3381 frame->code = car (cont) ; cont = cdr (cont); 3149 frame->code = car (cont) ; cont = cdr (cont);
3382 3150
3383 ++frame; 3151 ++frame;
3384 ++i; 3152 ++i;
3385 } 3153 }
3386 3154
3387 SCHEME_V->dump = (pointer)(uintptr_t)i; 3155 SCHEME_V->dump = (pointer)(uintptr_t)i;
3388} 3156}
3389 3157
3390#else 3158#else
3391 3159
3392static INLINE void 3160ecb_cold void
3393dump_stack_reset (SCHEME_P) 3161dump_stack_reset (SCHEME_P)
3394{ 3162{
3395 SCHEME_V->dump = NIL; 3163 SCHEME_V->dump = NIL;
3396} 3164}
3397 3165
3398static INLINE void 3166ecb_cold void
3399dump_stack_initialize (SCHEME_P) 3167dump_stack_initialize (SCHEME_P)
3400{ 3168{
3401 dump_stack_reset (SCHEME_A); 3169 dump_stack_reset (SCHEME_A);
3402} 3170}
3403 3171
3404static void 3172ecb_cold static void
3405dump_stack_free (SCHEME_P) 3173dump_stack_free (SCHEME_P)
3406{ 3174{
3407 SCHEME_V->dump = NIL; 3175 SCHEME_V->dump = NIL;
3408} 3176}
3409 3177
3410static pointer 3178ecb_hot static int
3411xs_return (SCHEME_P_ pointer a) 3179xs_return (SCHEME_P_ pointer a)
3412{ 3180{
3413 pointer dump = SCHEME_V->dump; 3181 pointer dump = SCHEME_V->dump;
3414 3182
3415 SCHEME_V->value = a; 3183 SCHEME_V->value = a;
3416 3184
3417 if (dump == NIL) 3185 if (dump == NIL)
3418 return NIL; 3186 return -1;
3419 3187
3420 SCHEME_V->op = ivalue (car (dump)); 3188 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3421 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3189 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3422 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3190 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3423 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3191 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3424 3192
3425 SCHEME_V->dump = dump; 3193 SCHEME_V->dump = dump;
3426 3194
3427 return S_T; 3195 return 0;
3428} 3196}
3429 3197
3430static void 3198ecb_hot static void
3431s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3199s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3432{ 3200{
3433 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3201 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3434 cons (args, 3202 cons (args,
3435 cons (SCHEME_V->envir, 3203 cons (SCHEME_V->envir,
3436 cons (code, 3204 cons (code,
3437 SCHEME_V->dump)))); 3205 SCHEME_V->dump))));
3438} 3206}
3439 3207
3208ecb_cold static void
3209dump_stack_mark (SCHEME_P)
3210{
3211 mark (SCHEME_V->dump);
3212}
3213
3214ecb_cold static pointer
3215ss_get_cont (SCHEME_P)
3216{
3217 return SCHEME_V->dump;
3218}
3219
3220ecb_cold static void
3221ss_set_cont (SCHEME_P_ pointer cont)
3222{
3223 SCHEME_V->dump = cont;
3224}
3225
3226#endif
3227
3228#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3229
3230#if EXPERIMENT
3231
3232static int
3233dtree (SCHEME_P_ int indent, pointer x)
3234{
3235 int c;
3236
3237 if (is_syntax (x))
3238 {
3239 printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x));
3240 return 8 + 8;
3241 }
3242
3243 if (x == NIL)
3244 {
3245 printf ("%*sNIL\n", indent, "");
3246 return 3;
3247 }
3248
3249 switch (type (x))
3250 {
3251 case T_INTEGER:
3252 printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x);
3253 return 32+8;
3254
3255 case T_SYMBOL:
3256 printf ("%*sS<%s>\n", indent, "", symname (x));
3257 return 24+8;
3258
3259 case T_CLOSURE:
3260 printf ("%*sS<%s>\n", indent, "", "closure");
3261 dtree (SCHEME_A_ indent + 3, cdr(x));
3262 return 32 + dtree (SCHEME_A_ indent + 3, car (x));
3263
3264 case T_PAIR:
3265 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3266 c = dtree (SCHEME_A_ indent + 3, car (x));
3267 c += dtree (SCHEME_A_ indent + 3, cdr (x));
3268 return c + 1;
3269
3270 case T_PORT:
3271 printf ("%*sS<%s>\n", indent, "", "port");
3272 return 24+8;
3273
3274 case T_VECTOR:
3275 printf ("%*sS<%s>\n", indent, "", "vector");
3276 return 24+8;
3277
3278 case T_ENVIRONMENT:
3279 printf ("%*sS<%s>\n", indent, "", "environment");
3280 return 0 + dtree (SCHEME_A_ indent + 3, car (x));
3281
3282 default:
3283 printf ("unhandled type %d\n", type (x));
3284 break;
3285 }
3286}
3287
3288#define DUMP(t) do { printf ("DUMP %s:%d\n", __FILE__, __LINE__); dtree (SCHEME_A_ 0, (t)); } while (0)
3289
3290typedef void *stream[1];
3291
3292#define stream_init() { 0 }
3293#define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2)
3294#define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2)
3295#define stream_free(s) free (s[0])
3296
3297ecb_cold static void
3298stream_put (stream s, uint8_t byte)
3299{
3300 uint32_t *sp = *s;
3301 uint32_t size = sizeof (uint32_t) * 2;
3302 uint32_t offs = size;
3303
3304 if (ecb_expect_true (sp))
3305 {
3306 offs = sp[0];
3307 size = sp[1];
3308 }
3309
3310 if (ecb_expect_false (offs == size))
3311 {
3312 size *= 2;
3313 sp = realloc (sp, size);
3314 *s = sp;
3315 sp[1] = size;
3316
3317 }
3318
3319 ((uint8_t *)sp)[offs++] = byte;
3320 sp[0] = offs;
3321}
3322
3323ecb_cold static void
3324stream_put_v (stream s, uint32_t v)
3325{
3326 while (v > 0x7f)
3327 {
3328 stream_put (s, v | 0x80);
3329 v >>= 7;
3330 }
3331
3332 stream_put (s, v);
3333}
3334
3335ecb_cold static void
3336stream_put_tv (stream s, int bop, uint32_t v)
3337{
3338 printf ("put tv %d %d\n", bop, v);//D
3339 stream_put (s, bop);
3340 stream_put_v (s, v);
3341}
3342
3343ecb_cold static void
3344stream_put_stream (stream s, stream o)
3345{
3346 uint32_t i;
3347
3348 for (i = 0; i < stream_size (o); ++i)
3349 stream_put (s, stream_data (o)[i]);
3350
3351 stream_free (o);
3352}
3353
3354ecb_cold static uint32_t
3355cell_id (SCHEME_P_ pointer x)
3356{
3357 struct cell *p = CELL (x);
3358 int i;
3359
3360 for (i = SCHEME_V->last_cell_seg; i >= 0; --i)
3361 if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i])
3362 return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG);
3363
3364 abort ();
3365}
3366
3367// calculates a (preferably small) integer that makes it possible to find
3368// the symbol again. if pointers were offsets into a memory area... until
3369// then, we return segment number in the low bits, and offset in the high
3370// bits.
3371// also, this function must never return 0.
3372ecb_cold static uint32_t
3373symbol_id (SCHEME_P_ pointer sym)
3374{
3375 return cell_id (SCHEME_A_ sym);
3376}
3377
3378enum byteop
3379{
3380 BOP_NIL,
3381 BOP_INTEGER,
3382 BOP_SYMBOL,
3383 BOP_DATUM,
3384 BOP_LIST_BEG,
3385 BOP_LIST_END,
3386 BOP_IF,
3387 BOP_AND,
3388 BOP_OR,
3389 BOP_CASE,
3390 BOP_COND,
3391 BOP_LET,
3392 BOP_LETAST,
3393 BOP_LETREC,
3394 BOP_DEFINE,
3395 BOP_MACRO,
3396 BOP_SET,
3397 BOP_BEGIN,
3398 BOP_LAMBDA,
3399 BOP_DELAY,
3400 BOP_OP,
3401};
3402
3403ecb_cold static void compile_expr (SCHEME_P_ stream s, pointer x);
3404
3405ecb_cold static void
3406compile_list (SCHEME_P_ stream s, pointer x)
3407{
3408 // TODO: improper list
3409
3410 for (; x != NIL; x = cdr (x))
3411 {
3412 stream t = stream_init ();
3413 compile_expr (SCHEME_A_ t, car (x));
3414 stream_put_v (s, stream_size (t));
3415 stream_put_stream (s, t);
3416 }
3417
3418 stream_put_v (s, 0);
3419}
3420
3440static void 3421static void
3441dump_stack_mark (SCHEME_P) 3422compile_if (SCHEME_P_ stream s, pointer cond, pointer ift, pointer iff)
3442{ 3423{
3443 mark (SCHEME_V->dump); 3424 stream sift = stream_init (); compile_expr (SCHEME_A_ sift, ift);
3425
3426 stream_put (s, BOP_IF);
3427 compile_expr (SCHEME_A_ s, cond);
3428 stream_put_v (s, stream_size (sift));
3429 stream_put_stream (s, sift);
3430 compile_expr (SCHEME_A_ s, iff);
3431}
3432
3433typedef uint32_t stream_fixup;
3434
3435static stream_fixup
3436stream_put_fixup (stream s)
3437{
3438 stream_put (s, 0);
3439 stream_put (s, 0);
3440
3441 return stream_size (s);
3442}
3443
3444static void
3445stream_fix_fixup (stream s, stream_fixup fixup, uint32_t target)
3446{
3447 target -= fixup;
3448 assert (target < (1 << 14));
3449 stream_data (s)[fixup - 2] = target | 0x80;
3450 stream_data (s)[fixup - 1] = target >> 7;
3451}
3452
3453static void
3454compile_and_or (SCHEME_P_ stream s, int and, pointer x)
3455{
3456 for (; cdr (x) != NIL; x = cdr (x))
3457 {
3458 stream t = stream_init ();
3459 compile_expr (SCHEME_A_ t, car (x));
3460 stream_put_v (s, stream_size (t));
3461 stream_put_stream (s, t);
3462 }
3463
3464 stream_put_v (s, 0);
3465}
3466
3467static void
3468compile_case (SCHEME_P_ stream s, pointer x)
3469{
3470 compile_expr (SCHEME_A_ s, caar (x));
3471
3472 for (;;)
3473 {
3474 x = cdr (x);
3475
3476 if (x == NIL)
3477 break;
3478
3479 compile_expr (SCHEME_A_ s, caar (x));
3480 stream t = stream_init (); compile_expr (SCHEME_A_ t, cdar (x));
3481 stream_put_v (s, stream_size (t));
3482 stream_put_stream (s, t);
3483 }
3484
3485 stream_put_v (s, 0);
3486}
3487
3488static void
3489compile_cond (SCHEME_P_ stream s, pointer x)
3490{
3491 for ( ; x != NIL; x = cdr (x))
3492 {
3493 compile_expr (SCHEME_A_ s, caar (x));
3494 stream t = stream_init (); compile_expr (SCHEME_A_ t, cdar (x));
3495 stream_put_v (s, stream_size (t));
3496 stream_put_stream (s, t);
3497 }
3498
3499 stream_put_v (s, 0);
3444} 3500}
3445 3501
3446static pointer 3502static pointer
3447ss_get_cont (SCHEME_P) 3503lookup (SCHEME_P_ pointer x)
3448{ 3504{
3449 return SCHEME_V->dump; 3505 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, x, 1);
3450}
3451 3506
3452static void 3507 if (x != NIL)
3453ss_set_cont (SCHEME_P_ pointer cont) 3508 x = slot_value_in_env (x);
3454{
3455 SCHEME_V->dump = cont;
3456}
3457 3509
3458#endif 3510 return x;
3511}
3459 3512
3460#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3513ecb_cold static void
3514compile_expr (SCHEME_P_ stream s, pointer x)
3515{
3516 if (x == NIL)
3517 {
3518 stream_put (s, BOP_NIL);
3519 return;
3520 }
3461 3521
3462static pointer 3522 if (is_pair (x))
3523 {
3524 pointer head = car (x);
3525
3526 if (is_syntax (head))
3527 {
3528 int syn = syntaxnum (head);
3529 x = cdr (x);
3530
3531 switch (syntaxnum (head))
3532 {
3533 case OP_IF0: /* if */
3534 stream_put_v (s, BOP_IF);
3535 compile_if (SCHEME_A_ s, car (x), cadr (x), caddr (x));
3536 break;
3537
3538 case OP_OR0: /* or */
3539 stream_put_v (s, BOP_OR);
3540 compile_and_or (SCHEME_A_ s, 0, x);
3541 break;
3542
3543 case OP_AND0: /* and */
3544 stream_put_v (s, BOP_AND);
3545 compile_and_or (SCHEME_A_ s, 1, x);
3546 break;
3547
3548 case OP_CASE0: /* case */
3549 stream_put_v (s, BOP_CASE);
3550 compile_case (SCHEME_A_ s, x);
3551 break;
3552
3553 case OP_COND0: /* cond */
3554 stream_put_v (s, BOP_COND);
3555 compile_cond (SCHEME_A_ s, x);
3556 break;
3557
3558 case OP_LET0: /* let */
3559 case OP_LET0AST: /* let* */
3560 case OP_LET0REC: /* letrec */
3561 switch (syn)
3562 {
3563 case OP_LET0: stream_put (s, BOP_LET ); break;
3564 case OP_LET0AST: stream_put (s, BOP_LETAST); break;
3565 case OP_LET0REC: stream_put (s, BOP_LETREC); break;
3566 }
3567
3568 {
3569 pointer bindings = car (x);
3570 pointer body = cadr (x);
3571
3572 for (x = bindings; x != NIL; x = cdr (x))
3573 {
3574 pointer init = NIL;
3575 pointer var = car (x);
3576
3577 if (is_pair (var))
3578 {
3579 init = cdr (var);
3580 var = car (var);
3581 }
3582
3583 stream_put_v (s, symbol_id (SCHEME_A_ var));
3584 compile_expr (SCHEME_A_ s, init);
3585 }
3586
3587 stream_put_v (s, 0);
3588 compile_expr (SCHEME_A_ s, body);
3589 }
3590 break;
3591
3592 case OP_DEF0: /* define */
3593 case OP_MACRO0: /* macro */
3594 stream_put (s, syntaxnum (head) == OP_DEF0 ? BOP_DEFINE : BOP_MACRO);
3595 stream_put_v (s, cell_id (SCHEME_A_ car (x)));
3596 compile_expr (SCHEME_A_ s, cadr (x));
3597 break;
3598
3599 case OP_SET0: /* set! */
3600 stream_put (s, BOP_SET);
3601 stream_put_v (s, symbol_id (SCHEME_A_ car (x)));
3602 compile_expr (SCHEME_A_ s, cadr (x));
3603 break;
3604
3605 case OP_BEGIN: /* begin */
3606 stream_put (s, BOP_BEGIN);
3607 compile_list (SCHEME_A_ s, x);
3608 return;
3609
3610 case OP_QUOTE: /* quote */
3611 stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x));
3612 break;
3613
3614 case OP_DELAY: /* delay */
3615 stream_put (s, BOP_DELAY);
3616 compile_expr (SCHEME_A_ s, x);
3617 break;
3618
3619 case OP_LAMBDA: /* lambda */
3620 {
3621 pointer formals = car (x);
3622 pointer body = cadr (x);
3623
3624 stream_put (s, BOP_LAMBDA);
3625
3626 for (; is_pair (formals); formals = cdr (formals))
3627 stream_put_v (s, symbol_id (SCHEME_A_ car (formals)));
3628
3629 stream_put_v (s, 0);
3630 stream_put_v (s, formals == NIL ? 0 : symbol_id (SCHEME_A_ formals));
3631
3632 compile_expr (SCHEME_A_ s, body);
3633 }
3634 break;
3635
3636 case OP_C0STREAM:/* cons-stream */
3637 abort ();
3638 break;
3639 }
3640
3641 return;
3642 }
3643
3644 pointer m = lookup (SCHEME_A_ head);
3645
3646 if (is_macro (m))
3647 {
3648 s_save (SCHEME_A_ OP_DEBUG2, SCHEME_V->args, SCHEME_V->code);
3649 SCHEME_V->code = m;
3650 SCHEME_V->args = cons (x, NIL);
3651 Eval_Cycle (SCHEME_A_ OP_APPLY);
3652 x = SCHEME_V->value;
3653 compile_expr (SCHEME_A_ s, SCHEME_V->value);
3654 return;
3655 }
3656
3657 stream_put (s, BOP_LIST_BEG);
3658
3659 for (; x != NIL; x = cdr (x))
3660 compile_expr (SCHEME_A_ s, car (x));
3661
3662 stream_put (s, BOP_LIST_END);
3663 return;
3664 }
3665
3666 switch (type (x))
3667 {
3668 case T_INTEGER:
3669 {
3670 IVALUE iv = ivalue_unchecked (x);
3671 iv = iv < 0 ? ((~(uint32_t)iv) << 1) | 1 : (uint32_t)iv << 1;
3672 stream_put_tv (s, BOP_INTEGER, iv);
3673 }
3674 return;
3675
3676 case T_SYMBOL:
3677 if (0)
3678 {
3679 // no can do without more analysis
3680 pointer m = lookup (SCHEME_A_ x);
3681
3682 if (is_proc (m))
3683 {
3684 printf ("compile proc %s %d\n", procname(m), procnum(m));
3685 stream_put_tv (s, BOP_SYMBOL, BOP_OP + procnum (m));
3686 }
3687 else
3688 stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x));
3689 }
3690
3691 stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x));
3692 return;
3693
3694 default:
3695 stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x));
3696 break;
3697 }
3698}
3699
3700ecb_cold static int
3701compile_closure (SCHEME_P_ pointer p)
3702{
3703 stream s = stream_init ();
3704
3705 compile_list (SCHEME_A_ s, cdar (p));
3706
3707 FILE *xxd = popen ("xxd", "we");
3708 fwrite (stream_data (s), 1, stream_size (s), xxd);
3709 fclose (xxd);
3710
3711 return stream_size (s);
3712}
3713
3714#endif
3715
3716/* syntax, eval, core, ... */
3717ecb_hot static int
3463opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3718opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3464{ 3719{
3720 pointer args = SCHEME_V->args;
3465 pointer x, y; 3721 pointer x, y;
3466 3722
3467 switch (op) 3723 switch (op)
3468 { 3724 {
3725#if EXPERIMENT //D
3726 case OP_DEBUG:
3727 {
3728 uint32_t len = compile_closure (SCHEME_A_ car (args));
3729 printf ("len = %d\n", len);
3730 printf ("\n");
3731 s_return (S_T);
3732 }
3733
3734 case OP_DEBUG2:
3735 return -1;
3736#endif
3737
3469 case OP_LOAD: /* load */ 3738 case OP_LOAD: /* load */
3470 if (file_interactive (SCHEME_A)) 3739 if (file_interactive (SCHEME_A))
3471 { 3740 {
3472 xwrstr ("Loading "); xwrstr (strvalue (car (SCHEME_V->args))); xwrstr ("\n"); 3741 putstr (SCHEME_A_ "Loading ");
3473 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (SCHEME_V->args))); 3742 putstr (SCHEME_A_ strvalue (car (args)));
3743 putcharacter (SCHEME_A_ '\n');
3474 } 3744 }
3475 3745
3476 if (!file_push (SCHEME_A_ strvalue (car (SCHEME_V->args)))) 3746 if (!file_push (SCHEME_A_ strvalue (car (args))))
3477 Error_1 ("unable to open", car (SCHEME_V->args)); 3747 Error_1 ("unable to open", car (args));
3478 else 3748
3479 {
3480 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3749 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3481 s_goto (OP_T0LVL); 3750 s_goto (OP_T0LVL);
3482 }
3483 3751
3484 case OP_T0LVL: /* top level */ 3752 case OP_T0LVL: /* top level */
3485 3753
3486 /* If we reached the end of file, this loop is done. */ 3754 /* If we reached the end of file, this loop is done. */
3487 if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) 3755 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3488 { 3756 {
3489 if (SCHEME_V->file_i == 0) 3757 if (SCHEME_V->file_i == 0)
3490 { 3758 {
3491 SCHEME_V->args = NIL; 3759 SCHEME_V->args = NIL;
3492 s_goto (OP_QUIT); 3760 s_goto (OP_QUIT);
3503 /* If interactive, be nice to user. */ 3771 /* If interactive, be nice to user. */
3504 if (file_interactive (SCHEME_A)) 3772 if (file_interactive (SCHEME_A))
3505 { 3773 {
3506 SCHEME_V->envir = SCHEME_V->global_env; 3774 SCHEME_V->envir = SCHEME_V->global_env;
3507 dump_stack_reset (SCHEME_A); 3775 dump_stack_reset (SCHEME_A);
3508 putstr (SCHEME_A_ "\n"); 3776 putcharacter (SCHEME_A_ '\n');
3777#if EXPERIMENT
3778 system ("ps v $PPID");
3779#endif
3509 putstr (SCHEME_A_ prompt); 3780 putstr (SCHEME_A_ prompt);
3510 } 3781 }
3511 3782
3512 /* Set up another iteration of REPL */ 3783 /* Set up another iteration of REPL */
3513 SCHEME_V->nesting = 0; 3784 SCHEME_V->nesting = 0;
3517 s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); 3788 s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL);
3518 s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); 3789 s_save (SCHEME_A_ OP_T1LVL, NIL, NIL);
3519 s_goto (OP_READ_INTERNAL); 3790 s_goto (OP_READ_INTERNAL);
3520 3791
3521 case OP_T1LVL: /* top level */ 3792 case OP_T1LVL: /* top level */
3522 SCHEME_V->code = SCHEME_V->value; 3793 SCHEME_V->code = SCHEME_V->value;
3523 SCHEME_V->inport = SCHEME_V->save_inport; 3794 SCHEME_V->inport = SCHEME_V->save_inport;
3524 s_goto (OP_EVAL); 3795 s_goto (OP_EVAL);
3525 3796
3526 case OP_READ_INTERNAL: /* internal read */ 3797 case OP_READ_INTERNAL: /* internal read */
3527 SCHEME_V->tok = token (SCHEME_A); 3798 SCHEME_V->tok = token (SCHEME_A);
3528 3799
3529 if (SCHEME_V->tok == TOK_EOF) 3800 if (SCHEME_V->tok == TOK_EOF)
3530 {
3531 s_return (S_EOF); 3801 s_return (S_EOF);
3532 }
3533 3802
3534 s_goto (OP_RDSEXPR); 3803 s_goto (OP_RDSEXPR);
3535 3804
3536 case OP_GENSYM: 3805 case OP_GENSYM:
3537 s_return (gensym (SCHEME_A)); 3806 s_return (gensym (SCHEME_A));
3541 /* OP_VALUEPRINT is always pushed, because when changing from 3810 /* OP_VALUEPRINT is always pushed, because when changing from
3542 non-interactive to interactive mode, it needs to be 3811 non-interactive to interactive mode, it needs to be
3543 already on the stack */ 3812 already on the stack */
3544#if USE_TRACING 3813#if USE_TRACING
3545 if (SCHEME_V->tracing) 3814 if (SCHEME_V->tracing)
3546 {
3547 putstr (SCHEME_A_ "\nGives: "); 3815 putstr (SCHEME_A_ "\nGives: ");
3548 }
3549#endif 3816#endif
3550 3817
3551 if (file_interactive (SCHEME_A)) 3818 if (file_interactive (SCHEME_A))
3552 { 3819 {
3553 SCHEME_V->print_flag = 1; 3820 SCHEME_V->print_flag = 1;
3554 SCHEME_V->args = SCHEME_V->value; 3821 SCHEME_V->args = SCHEME_V->value;
3555 s_goto (OP_P0LIST); 3822 s_goto (OP_P0LIST);
3556 } 3823 }
3557 else 3824
3558 {
3559 s_return (SCHEME_V->value); 3825 s_return (SCHEME_V->value);
3560 }
3561 3826
3562 case OP_EVAL: /* main part of evaluation */ 3827 case OP_EVAL: /* main part of evaluation */
3563#if USE_TRACING 3828#if USE_TRACING
3564 if (SCHEME_V->tracing) 3829 if (SCHEME_V->tracing)
3565 { 3830 {
3566 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ 3831 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */
3567 s_save (SCHEME_A_ OP_REAL_EVAL, SCHEME_V->args, SCHEME_V->code); 3832 s_save (SCHEME_A_ OP_REAL_EVAL, args, SCHEME_V->code);
3568 SCHEME_V->args = SCHEME_V->code; 3833 SCHEME_V->args = SCHEME_V->code;
3569 putstr (SCHEME_A_ "\nEval: "); 3834 putstr (SCHEME_A_ "\nEval: ");
3570 s_goto (OP_P0LIST); 3835 s_goto (OP_P0LIST);
3571 } 3836 }
3572 3837
3573 /* fall through */ 3838 /* fall through */
3839
3574 case OP_REAL_EVAL: 3840 case OP_REAL_EVAL:
3575#endif 3841#endif
3576 if (is_symbol (SCHEME_V->code)) /* symbol */ 3842 if (is_symbol (SCHEME_V->code)) /* symbol */
3577 { 3843 {
3578 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); 3844 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1);
3579 3845
3580 if (x != NIL) 3846 if (x == NIL)
3581 s_return (slot_value_in_env (x));
3582 else
3583 Error_1 ("eval: unbound variable:", SCHEME_V->code); 3847 Error_1 ("eval: unbound variable:", SCHEME_V->code);
3848
3849 s_return (slot_value_in_env (x));
3584 } 3850 }
3585 else if (is_pair (SCHEME_V->code)) 3851 else if (is_pair (SCHEME_V->code))
3586 { 3852 {
3853 x = car (SCHEME_V->code);
3854
3587 if (is_syntax (x = car (SCHEME_V->code))) /* SYNTAX */ 3855 if (is_syntax (x)) /* SYNTAX */
3588 { 3856 {
3589 SCHEME_V->code = cdr (SCHEME_V->code); 3857 SCHEME_V->code = cdr (SCHEME_V->code);
3590 s_goto (syntaxnum (x)); 3858 s_goto (syntaxnum (x));
3591 } 3859 }
3592 else /* first, eval top element and eval arguments */ 3860 else /* first, eval top element and eval arguments */
3593 { 3861 {
3594 s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code); 3862 s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code);
3595 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ 3863 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */
3596 SCHEME_V->code = car (SCHEME_V->code); 3864 SCHEME_V->code = x;
3597 s_goto (OP_EVAL); 3865 s_goto (OP_EVAL);
3598 } 3866 }
3599 } 3867 }
3600 else 3868
3601 s_return (SCHEME_V->code); 3869 s_return (SCHEME_V->code);
3602 3870
3603 case OP_E0ARGS: /* eval arguments */ 3871 case OP_E0ARGS: /* eval arguments */
3604 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3872 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3605 { 3873 {
3606 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3874 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3607 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3875 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3608 SCHEME_V->code = SCHEME_V->value; 3876 SCHEME_V->code = SCHEME_V->value;
3609 s_goto (OP_APPLY); 3877 s_goto (OP_APPLY);
3610 } 3878 }
3611 else 3879
3612 {
3613 SCHEME_V->code = cdr (SCHEME_V->code); 3880 SCHEME_V->code = cdr (SCHEME_V->code);
3614 s_goto (OP_E1ARGS); 3881 s_goto (OP_E1ARGS);
3615 }
3616 3882
3617 case OP_E1ARGS: /* eval arguments */ 3883 case OP_E1ARGS: /* eval arguments */
3618 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3884 args = cons (SCHEME_V->value, args);
3619 3885
3620 if (is_pair (SCHEME_V->code)) /* continue */ 3886 if (is_pair (SCHEME_V->code)) /* continue */
3621 { 3887 {
3622 s_save (SCHEME_A_ OP_E1ARGS, SCHEME_V->args, cdr (SCHEME_V->code)); 3888 s_save (SCHEME_A_ OP_E1ARGS, args, cdr (SCHEME_V->code));
3623 SCHEME_V->code = car (SCHEME_V->code); 3889 SCHEME_V->code = car (SCHEME_V->code);
3624 SCHEME_V->args = NIL; 3890 SCHEME_V->args = NIL;
3625 s_goto (OP_EVAL); 3891 s_goto (OP_EVAL);
3626 } 3892 }
3627 else /* end */ 3893 else /* end */
3628 { 3894 {
3629 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3895 args = reverse_in_place (SCHEME_A_ NIL, args);
3630 SCHEME_V->code = car (SCHEME_V->args); 3896 SCHEME_V->code = car (args);
3631 SCHEME_V->args = cdr (SCHEME_V->args); 3897 SCHEME_V->args = cdr (args);
3632 s_goto (OP_APPLY); 3898 s_goto (OP_APPLY);
3633 } 3899 }
3634 3900
3635#if USE_TRACING 3901#if USE_TRACING
3636
3637 case OP_TRACING: 3902 case OP_TRACING:
3638 { 3903 {
3639 int tr = SCHEME_V->tracing; 3904 int tr = SCHEME_V->tracing;
3640 3905
3641 SCHEME_V->tracing = ivalue (car (SCHEME_V->args)); 3906 SCHEME_V->tracing = ivalue_unchecked (car (args));
3642 s_return (mk_integer (SCHEME_A_ tr)); 3907 s_return (mk_integer (SCHEME_A_ tr));
3643 } 3908 }
3644
3645#endif 3909#endif
3646 3910
3647 case OP_APPLY: /* apply 'code' to 'args' */ 3911 case OP_APPLY: /* apply 'code' to 'args' */
3648#if USE_TRACING 3912#if USE_TRACING
3649 if (SCHEME_V->tracing) 3913 if (SCHEME_V->tracing)
3650 { 3914 {
3651 s_save (SCHEME_A_ OP_REAL_APPLY, SCHEME_V->args, SCHEME_V->code); 3915 s_save (SCHEME_A_ OP_REAL_APPLY, args, SCHEME_V->code);
3652 SCHEME_V->print_flag = 1; 3916 SCHEME_V->print_flag = 1;
3653 /* SCHEME_V->args=cons(SCHEME_V->code,SCHEME_V->args); */ 3917 /* args=cons(SCHEME_V->code,args); */
3654 putstr (SCHEME_A_ "\nApply to: "); 3918 putstr (SCHEME_A_ "\nApply to: ");
3655 s_goto (OP_P0LIST); 3919 s_goto (OP_P0LIST);
3656 } 3920 }
3657 3921
3658 /* fall through */ 3922 /* fall through */
3923
3659 case OP_REAL_APPLY: 3924 case OP_REAL_APPLY:
3660#endif 3925#endif
3661 if (is_proc (SCHEME_V->code)) 3926 if (is_proc (SCHEME_V->code))
3662 {
3663 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3927 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3664 }
3665 else if (is_foreign (SCHEME_V->code)) 3928 else if (is_foreign (SCHEME_V->code))
3666 { 3929 {
3667 /* Keep nested calls from GC'ing the arglist */ 3930 /* Keep nested calls from GC'ing the arglist */
3668 push_recent_alloc (SCHEME_A_ SCHEME_V->args, NIL); 3931 push_recent_alloc (SCHEME_A_ args, NIL);
3669 x = SCHEME_V->code->object.ff (SCHEME_A_ SCHEME_V->args); 3932 x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args);
3670 3933
3671 s_return (x); 3934 s_return (x);
3672 } 3935 }
3673 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ 3936 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */
3674 { 3937 {
3675 /* Should not accept promise */ 3938 /* Should not accept promise */
3676 /* make environment */ 3939 /* make environment */
3677 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); 3940 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code));
3678 3941
3679 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) 3942 for (x = car (closure_code (SCHEME_V->code)), y = args; is_pair (x); x = cdr (x), y = cdr (y))
3680 { 3943 {
3681 if (y == NIL) 3944 if (y == NIL)
3682 {
3683 Error_0 ("not enough arguments"); 3945 Error_0 ("not enough arguments");
3684 }
3685 else 3946 else
3686 {
3687 new_slot_in_env (SCHEME_A_ car (x), car (y)); 3947 new_slot_in_env (SCHEME_A_ car (x), car (y));
3688 }
3689 } 3948 }
3690 3949
3691 if (x == NIL) 3950 if (x == NIL)
3692 { 3951 {
3693
3694 /*-- 3952 /*--
3695 * if (y != NIL) { 3953 * if (y != NIL) {
3696 * Error_0("too many arguments"); 3954 * Error_0("too many arguments");
3697 * } 3955 * }
3698 */ 3956 */
3699 } 3957 }
3700 else if (is_symbol (x)) 3958 else if (is_symbol (x))
3701 new_slot_in_env (SCHEME_A_ x, y); 3959 new_slot_in_env (SCHEME_A_ x, y);
3702 else 3960 else
3703 {
3704 Error_1 ("syntax error in closure: not a symbol:", x); 3961 Error_1 ("syntax error in closure: not a symbol:", x);
3705 }
3706 3962
3707 SCHEME_V->code = cdr (closure_code (SCHEME_V->code)); 3963 SCHEME_V->code = cdr (closure_code (SCHEME_V->code));
3708 SCHEME_V->args = NIL; 3964 SCHEME_V->args = NIL;
3709 s_goto (OP_BEGIN); 3965 s_goto (OP_BEGIN);
3710 } 3966 }
3711 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3967 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3712 { 3968 {
3713 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3969 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3714 s_return (SCHEME_V->args != NIL ? car (SCHEME_V->args) : NIL); 3970 s_return (args != NIL ? car (args) : NIL);
3715 } 3971 }
3716 else 3972
3717 Error_0 ("illegal function"); 3973 Error_0 ("illegal function");
3718 3974
3719 case OP_DOMACRO: /* do macro */ 3975 case OP_DOMACRO: /* do macro */
3720 SCHEME_V->code = SCHEME_V->value; 3976 SCHEME_V->code = SCHEME_V->value;
3721 s_goto (OP_EVAL); 3977 s_goto (OP_EVAL);
3722
3723#if 1
3724 3978
3725 case OP_LAMBDA: /* lambda */ 3979 case OP_LAMBDA: /* lambda */
3726 /* If the hook is defined, apply it to SCHEME_V->code, otherwise 3980 /* If the hook is defined, apply it to SCHEME_V->code, otherwise
3727 set SCHEME_V->value fall thru */ 3981 set SCHEME_V->value fall thru */
3728 { 3982 {
3729 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); 3983 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1);
3730 3984
3731 if (f != NIL) 3985 if (f != NIL)
3732 { 3986 {
3733 s_save (SCHEME_A_ OP_LAMBDA1, SCHEME_V->args, SCHEME_V->code); 3987 s_save (SCHEME_A_ OP_LAMBDA1, args, SCHEME_V->code);
3734 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3988 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3735 SCHEME_V->code = slot_value_in_env (f); 3989 SCHEME_V->code = slot_value_in_env (f);
3736 s_goto (OP_APPLY); 3990 s_goto (OP_APPLY);
3737 } 3991 }
3738 3992
3739 SCHEME_V->value = SCHEME_V->code; 3993 SCHEME_V->value = SCHEME_V->code;
3740 /* Fallthru */
3741 } 3994 }
3995 /* Fallthru */
3742 3996
3743 case OP_LAMBDA1: 3997 case OP_LAMBDA1:
3744 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); 3998 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir));
3745 3999
3746#else
3747
3748 case OP_LAMBDA: /* lambda */
3749 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3750
3751#endif
3752
3753 case OP_MKCLOSURE: /* make-closure */ 4000 case OP_MKCLOSURE: /* make-closure */
3754 x = car (SCHEME_V->args); 4001 x = car (args);
3755 4002
3756 if (car (x) == SCHEME_V->LAMBDA) 4003 if (car (x) == SCHEME_V->LAMBDA)
3757 x = cdr (x); 4004 x = cdr (x);
3758 4005
3759 if (cdr (SCHEME_V->args) == NIL) 4006 if (cdr (args) == NIL)
3760 y = SCHEME_V->envir; 4007 y = SCHEME_V->envir;
3761 else 4008 else
3762 y = cadr (SCHEME_V->args); 4009 y = cadr (args);
3763 4010
3764 s_return (mk_closure (SCHEME_A_ x, y)); 4011 s_return (mk_closure (SCHEME_A_ x, y));
3765 4012
3766 case OP_QUOTE: /* quote */ 4013 case OP_QUOTE: /* quote */
3767 s_return (car (SCHEME_V->code)); 4014 s_return (car (SCHEME_V->code));
3780 x = car (SCHEME_V->code); 4027 x = car (SCHEME_V->code);
3781 SCHEME_V->code = cadr (SCHEME_V->code); 4028 SCHEME_V->code = cadr (SCHEME_V->code);
3782 } 4029 }
3783 4030
3784 if (!is_symbol (x)) 4031 if (!is_symbol (x))
3785 {
3786 Error_0 ("variable is not a symbol"); 4032 Error_0 ("variable is not a symbol");
3787 }
3788 4033
3789 s_save (SCHEME_A_ OP_DEF1, NIL, x); 4034 s_save (SCHEME_A_ OP_DEF1, NIL, x);
3790 s_goto (OP_EVAL); 4035 s_goto (OP_EVAL);
3791 4036
3792 case OP_DEF1: /* define */ 4037 case OP_DEF1: /* define */
3793 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); 4038 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0);
3794 4039
3795 if (x != NIL) 4040 if (x != NIL)
3796 {
3797 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); 4041 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value);
3798 }
3799 else 4042 else
3800 {
3801 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 4043 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
3802 }
3803 4044
3804 s_return (SCHEME_V->code); 4045 s_return (SCHEME_V->code);
3805
3806 4046
3807 case OP_DEFP: /* defined? */ 4047 case OP_DEFP: /* defined? */
3808 x = SCHEME_V->envir; 4048 x = SCHEME_V->envir;
3809 4049
3810 if (cdr (SCHEME_V->args) != NIL) 4050 if (cdr (args) != NIL)
3811 {
3812 x = cadr (SCHEME_V->args); 4051 x = cadr (args);
3813 }
3814 4052
3815 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); 4053 s_retbool (find_slot_in_env (SCHEME_A_ x, car (args), 1) != NIL);
3816 4054
3817 case OP_SET0: /* set! */ 4055 case OP_SET0: /* set! */
3818 if (is_immutable (car (SCHEME_V->code))) 4056 if (is_immutable (car (SCHEME_V->code)))
3819 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); 4057 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code));
3820 4058
3829 { 4067 {
3830 set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); 4068 set_slot_in_env (SCHEME_A_ y, SCHEME_V->value);
3831 s_return (SCHEME_V->value); 4069 s_return (SCHEME_V->value);
3832 } 4070 }
3833 else 4071 else
3834 {
3835 Error_1 ("set!: unbound variable:", SCHEME_V->code); 4072 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3836 }
3837
3838 4073
3839 case OP_BEGIN: /* begin */ 4074 case OP_BEGIN: /* begin */
3840 if (!is_pair (SCHEME_V->code)) 4075 if (!is_pair (SCHEME_V->code))
3841 {
3842 s_return (SCHEME_V->code); 4076 s_return (SCHEME_V->code);
3843 }
3844 4077
3845 if (cdr (SCHEME_V->code) != NIL) 4078 if (cdr (SCHEME_V->code) != NIL)
3846 {
3847 s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code)); 4079 s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code));
3848 }
3849 4080
3850 SCHEME_V->code = car (SCHEME_V->code); 4081 SCHEME_V->code = car (SCHEME_V->code);
3851 s_goto (OP_EVAL); 4082 s_goto (OP_EVAL);
3852 4083
3853 case OP_IF0: /* if */ 4084 case OP_IF0: /* if */
3857 4088
3858 case OP_IF1: /* if */ 4089 case OP_IF1: /* if */
3859 if (is_true (SCHEME_V->value)) 4090 if (is_true (SCHEME_V->value))
3860 SCHEME_V->code = car (SCHEME_V->code); 4091 SCHEME_V->code = car (SCHEME_V->code);
3861 else 4092 else
3862 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because 4093 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
3863 4094
3864 * car(NIL) = NIL */
3865 s_goto (OP_EVAL); 4095 s_goto (OP_EVAL);
3866 4096
3867 case OP_LET0: /* let */ 4097 case OP_LET0: /* let */
3868 SCHEME_V->args = NIL; 4098 SCHEME_V->args = NIL;
3869 SCHEME_V->value = SCHEME_V->code; 4099 SCHEME_V->value = SCHEME_V->code;
3870 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); 4100 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code);
3871 s_goto (OP_LET1); 4101 s_goto (OP_LET1);
3872 4102
3873 case OP_LET1: /* let (calculate parameters) */ 4103 case OP_LET1: /* let (calculate parameters) */
4104 case OP_LET1REC: /* letrec (calculate parameters) */
3874 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 4105 args = cons (SCHEME_V->value, args);
3875 4106
3876 if (is_pair (SCHEME_V->code)) /* continue */ 4107 if (is_pair (SCHEME_V->code)) /* continue */
3877 { 4108 {
3878 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 4109 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3879 {
3880 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); 4110 Error_1 ("Bad syntax of binding spec in let/letrec:", car (SCHEME_V->code));
3881 }
3882 4111
3883 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); 4112 s_save (SCHEME_A_ op, args, cdr (SCHEME_V->code));
3884 SCHEME_V->code = cadar (SCHEME_V->code); 4113 SCHEME_V->code = cadar (SCHEME_V->code);
3885 SCHEME_V->args = NIL; 4114 SCHEME_V->args = NIL;
3886 s_goto (OP_EVAL); 4115 s_goto (OP_EVAL);
3887 } 4116 }
3888 else /* end */ 4117
3889 { 4118 /* end */
3890 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 4119 args = reverse_in_place (SCHEME_A_ NIL, args);
3891 SCHEME_V->code = car (SCHEME_V->args); 4120 SCHEME_V->code = car (args);
3892 SCHEME_V->args = cdr (SCHEME_V->args); 4121 SCHEME_V->args = cdr (args);
3893 s_goto (OP_LET2); 4122 s_goto (op == OP_LET1 ? OP_LET2 : OP_LET2REC);
3894 }
3895 4123
3896 case OP_LET2: /* let */ 4124 case OP_LET2: /* let */
3897 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4125 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3898 4126
3899 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; 4127 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args;
3900 y != NIL; x = cdr (x), y = cdr (y)) 4128 y != NIL; x = cdr (x), y = cdr (y))
3901 {
3902 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 4129 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3903 }
3904 4130
3905 if (is_symbol (car (SCHEME_V->code))) /* named let */ 4131 if (is_symbol (car (SCHEME_V->code))) /* named let */
3906 { 4132 {
3907 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) 4133 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3908 { 4134 {
3909 if (!is_pair (x)) 4135 if (!is_pair (x))
3910 Error_1 ("Bad syntax of binding in let :", x); 4136 Error_1 ("Bad syntax of binding in let:", x);
3911 4137
3912 if (!is_list (SCHEME_A_ car (x))) 4138 if (!is_list (SCHEME_A_ car (x)))
3913 Error_1 ("Bad syntax of binding in let :", car (x)); 4139 Error_1 ("Bad syntax of binding in let:", car (x));
3914 4140
3915 SCHEME_V->args = cons (caar (x), SCHEME_V->args); 4141 args = cons (caar (x), args);
3916 } 4142 }
3917 4143
3918 x =
3919 mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args), cddr (SCHEME_V->code)), 4144 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)),
3920 SCHEME_V->envir); 4145 SCHEME_V->envir);
3921 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); 4146 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x);
3922 SCHEME_V->code = cddr (SCHEME_V->code); 4147 SCHEME_V->code = cddr (SCHEME_V->code);
3923 SCHEME_V->args = NIL;
3924 } 4148 }
3925 else 4149 else
3926 { 4150 {
3927 SCHEME_V->code = cdr (SCHEME_V->code); 4151 SCHEME_V->code = cdr (SCHEME_V->code);
3928 SCHEME_V->args = NIL;
3929 } 4152 }
3930 4153
4154 SCHEME_V->args = NIL;
3931 s_goto (OP_BEGIN); 4155 s_goto (OP_BEGIN);
3932 4156
3933 case OP_LET0AST: /* let* */ 4157 case OP_LET0AST: /* let* */
3934 if (car (SCHEME_V->code) == NIL) 4158 if (car (SCHEME_V->code) == NIL)
3935 { 4159 {
3937 SCHEME_V->code = cdr (SCHEME_V->code); 4161 SCHEME_V->code = cdr (SCHEME_V->code);
3938 s_goto (OP_BEGIN); 4162 s_goto (OP_BEGIN);
3939 } 4163 }
3940 4164
3941 if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code))) 4165 if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code)))
3942 Error_1 ("Bad syntax of binding spec in let* :", car (SCHEME_V->code)); 4166 Error_1 ("Bad syntax of binding spec in let*:", car (SCHEME_V->code));
3943 4167
3944 s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code)); 4168 s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code));
3945 SCHEME_V->code = car (cdaar (SCHEME_V->code)); 4169 SCHEME_V->code = car (cdaar (SCHEME_V->code));
3946 s_goto (OP_EVAL); 4170 s_goto (OP_EVAL);
3947 4171
3953 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); 4177 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value);
3954 SCHEME_V->code = cdr (SCHEME_V->code); 4178 SCHEME_V->code = cdr (SCHEME_V->code);
3955 4179
3956 if (is_pair (SCHEME_V->code)) /* continue */ 4180 if (is_pair (SCHEME_V->code)) /* continue */
3957 { 4181 {
3958 s_save (SCHEME_A_ OP_LET2AST, SCHEME_V->args, SCHEME_V->code); 4182 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3959 SCHEME_V->code = cadar (SCHEME_V->code); 4183 SCHEME_V->code = cadar (SCHEME_V->code);
3960 SCHEME_V->args = NIL; 4184 SCHEME_V->args = NIL;
3961 s_goto (OP_EVAL); 4185 s_goto (OP_EVAL);
3962 } 4186 }
3963 else /* end */ 4187
4188 /* end */
3964 { 4189
3965 SCHEME_V->code = SCHEME_V->args; 4190 SCHEME_V->code = args;
3966 SCHEME_V->args = NIL; 4191 SCHEME_V->args = NIL;
3967 s_goto (OP_BEGIN); 4192 s_goto (OP_BEGIN);
3968 }
3969 4193
3970 case OP_LET0REC: /* letrec */ 4194 case OP_LET0REC: /* letrec */
3971 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4195 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3972 SCHEME_V->args = NIL; 4196 SCHEME_V->args = NIL;
3973 SCHEME_V->value = SCHEME_V->code; 4197 SCHEME_V->value = SCHEME_V->code;
3974 SCHEME_V->code = car (SCHEME_V->code); 4198 SCHEME_V->code = car (SCHEME_V->code);
3975 s_goto (OP_LET1REC); 4199 s_goto (OP_LET1REC);
3976 4200
3977 case OP_LET1REC: /* letrec (calculate parameters) */ 4201 /* OP_LET1REC handled by OP_LET1 */
3978 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args);
3979
3980 if (is_pair (SCHEME_V->code)) /* continue */
3981 {
3982 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3983 {
3984 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3985 }
3986
3987 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code));
3988 SCHEME_V->code = cadar (SCHEME_V->code);
3989 SCHEME_V->args = NIL;
3990 s_goto (OP_EVAL);
3991 }
3992 else /* end */
3993 {
3994 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args);
3995 SCHEME_V->code = car (SCHEME_V->args);
3996 SCHEME_V->args = cdr (SCHEME_V->args);
3997 s_goto (OP_LET2REC);
3998 }
3999 4202
4000 case OP_LET2REC: /* letrec */ 4203 case OP_LET2REC: /* letrec */
4001 for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) 4204 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y))
4002 {
4003 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 4205 new_slot_in_env (SCHEME_A_ caar (x), car (y));
4004 }
4005 4206
4006 SCHEME_V->code = cdr (SCHEME_V->code); 4207 SCHEME_V->code = cdr (SCHEME_V->code);
4007 SCHEME_V->args = NIL; 4208 SCHEME_V->args = NIL;
4008 s_goto (OP_BEGIN); 4209 s_goto (OP_BEGIN);
4009 4210
4010 case OP_COND0: /* cond */ 4211 case OP_COND0: /* cond */
4011 if (!is_pair (SCHEME_V->code)) 4212 if (!is_pair (SCHEME_V->code))
4012 {
4013 Error_0 ("syntax error in cond"); 4213 Error_0 ("syntax error in cond");
4014 }
4015 4214
4016 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 4215 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
4017 SCHEME_V->code = caar (SCHEME_V->code); 4216 SCHEME_V->code = caar (SCHEME_V->code);
4018 s_goto (OP_EVAL); 4217 s_goto (OP_EVAL);
4019 4218
4020 case OP_COND1: /* cond */ 4219 case OP_COND1: /* cond */
4021 if (is_true (SCHEME_V->value)) 4220 if (is_true (SCHEME_V->value))
4022 { 4221 {
4023 if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL) 4222 if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL)
4024 {
4025 s_return (SCHEME_V->value); 4223 s_return (SCHEME_V->value);
4026 }
4027 4224
4028 if (car (SCHEME_V->code) == SCHEME_V->FEED_TO) 4225 if (car (SCHEME_V->code) == SCHEME_V->FEED_TO)
4029 { 4226 {
4030 if (!is_pair (cdr (SCHEME_V->code))) 4227 if (!is_pair (cdr (SCHEME_V->code)))
4031 {
4032 Error_0 ("syntax error in cond"); 4228 Error_0 ("syntax error in cond");
4033 }
4034 4229
4035 x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)); 4230 x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL));
4036 SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL)); 4231 SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL));
4037 s_goto (OP_EVAL); 4232 s_goto (OP_EVAL);
4038 } 4233 }
4040 s_goto (OP_BEGIN); 4235 s_goto (OP_BEGIN);
4041 } 4236 }
4042 else 4237 else
4043 { 4238 {
4044 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 4239 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
4045 {
4046 s_return (NIL); 4240 s_return (NIL);
4047 } 4241
4048 else
4049 {
4050 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 4242 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
4051 SCHEME_V->code = caar (SCHEME_V->code); 4243 SCHEME_V->code = caar (SCHEME_V->code);
4052 s_goto (OP_EVAL); 4244 s_goto (OP_EVAL);
4053 }
4054 } 4245 }
4055 4246
4056 case OP_DELAY: /* delay */ 4247 case OP_DELAY: /* delay */
4057 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); 4248 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
4058 set_typeflag (x, T_PROMISE); 4249 set_typeflag (x, T_PROMISE);
4059 s_return (x); 4250 s_return (x);
4060 4251
4061 case OP_AND0: /* and */ 4252 case OP_AND0: /* and */
4062 if (SCHEME_V->code == NIL) 4253 if (SCHEME_V->code == NIL)
4063 {
4064 s_return (S_T); 4254 s_return (S_T);
4065 }
4066 4255
4067 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 4256 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
4068 SCHEME_V->code = car (SCHEME_V->code); 4257 SCHEME_V->code = car (SCHEME_V->code);
4069 s_goto (OP_EVAL); 4258 s_goto (OP_EVAL);
4070 4259
4071 case OP_AND1: /* and */ 4260 case OP_AND1: /* and */
4072 if (is_false (SCHEME_V->value)) 4261 if (is_false (SCHEME_V->value))
4073 {
4074 s_return (SCHEME_V->value); 4262 s_return (SCHEME_V->value);
4075 }
4076 else if (SCHEME_V->code == NIL) 4263 else if (SCHEME_V->code == NIL)
4077 {
4078 s_return (SCHEME_V->value); 4264 s_return (SCHEME_V->value);
4079 } 4265
4080 else
4081 {
4082 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 4266 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
4083 SCHEME_V->code = car (SCHEME_V->code); 4267 SCHEME_V->code = car (SCHEME_V->code);
4084 s_goto (OP_EVAL); 4268 s_goto (OP_EVAL);
4085 }
4086 4269
4087 case OP_OR0: /* or */ 4270 case OP_OR0: /* or */
4088 if (SCHEME_V->code == NIL) 4271 if (SCHEME_V->code == NIL)
4089 {
4090 s_return (S_F); 4272 s_return (S_F);
4091 }
4092 4273
4093 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 4274 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
4094 SCHEME_V->code = car (SCHEME_V->code); 4275 SCHEME_V->code = car (SCHEME_V->code);
4095 s_goto (OP_EVAL); 4276 s_goto (OP_EVAL);
4096 4277
4097 case OP_OR1: /* or */ 4278 case OP_OR1: /* or */
4098 if (is_true (SCHEME_V->value)) 4279 if (is_true (SCHEME_V->value))
4099 {
4100 s_return (SCHEME_V->value); 4280 s_return (SCHEME_V->value);
4101 }
4102 else if (SCHEME_V->code == NIL) 4281 else if (SCHEME_V->code == NIL)
4103 {
4104 s_return (SCHEME_V->value); 4282 s_return (SCHEME_V->value);
4105 } 4283
4106 else
4107 {
4108 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 4284 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
4109 SCHEME_V->code = car (SCHEME_V->code); 4285 SCHEME_V->code = car (SCHEME_V->code);
4110 s_goto (OP_EVAL); 4286 s_goto (OP_EVAL);
4111 }
4112 4287
4113 case OP_C0STREAM: /* cons-stream */ 4288 case OP_C0STREAM: /* cons-stream */
4114 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 4289 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
4115 SCHEME_V->code = car (SCHEME_V->code); 4290 SCHEME_V->code = car (SCHEME_V->code);
4116 s_goto (OP_EVAL); 4291 s_goto (OP_EVAL);
4117 4292
4118 case OP_C1STREAM: /* cons-stream */ 4293 case OP_C1STREAM: /* cons-stream */
4119 SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register SCHEME_V->args for gc */ 4294 SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register args for gc */
4120 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); 4295 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
4121 set_typeflag (x, T_PROMISE); 4296 set_typeflag (x, T_PROMISE);
4122 s_return (cons (SCHEME_V->args, x)); 4297 s_return (cons (args, x));
4123 4298
4124 case OP_MACRO0: /* macro */ 4299 case OP_MACRO0: /* macro */
4125 if (is_pair (car (SCHEME_V->code))) 4300 if (is_pair (car (SCHEME_V->code)))
4126 { 4301 {
4127 x = caar (SCHEME_V->code); 4302 x = caar (SCHEME_V->code);
4132 x = car (SCHEME_V->code); 4307 x = car (SCHEME_V->code);
4133 SCHEME_V->code = cadr (SCHEME_V->code); 4308 SCHEME_V->code = cadr (SCHEME_V->code);
4134 } 4309 }
4135 4310
4136 if (!is_symbol (x)) 4311 if (!is_symbol (x))
4137 {
4138 Error_0 ("variable is not a symbol"); 4312 Error_0 ("variable is not a symbol");
4139 }
4140 4313
4141 s_save (SCHEME_A_ OP_MACRO1, NIL, x); 4314 s_save (SCHEME_A_ OP_MACRO1, NIL, x);
4142 s_goto (OP_EVAL); 4315 s_goto (OP_EVAL);
4143 4316
4144 case OP_MACRO1: /* macro */ 4317 case OP_MACRO1: /* macro */
4145 set_typeflag (SCHEME_V->value, T_MACRO); 4318 set_typeflag (SCHEME_V->value, T_MACRO);
4146 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); 4319 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0);
4147 4320
4148 if (x != NIL) 4321 if (x != NIL)
4149 {
4150 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); 4322 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value);
4151 }
4152 else 4323 else
4153 {
4154 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 4324 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
4155 }
4156 4325
4157 s_return (SCHEME_V->code); 4326 s_return (SCHEME_V->code);
4158 4327
4159 case OP_CASE0: /* case */ 4328 case OP_CASE0: /* case */
4160 s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code)); 4329 s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code));
4163 4332
4164 case OP_CASE1: /* case */ 4333 case OP_CASE1: /* case */
4165 for (x = SCHEME_V->code; x != NIL; x = cdr (x)) 4334 for (x = SCHEME_V->code; x != NIL; x = cdr (x))
4166 { 4335 {
4167 if (!is_pair (y = caar (x))) 4336 if (!is_pair (y = caar (x)))
4168 {
4169 break; 4337 break;
4170 }
4171 4338
4172 for (; y != NIL; y = cdr (y)) 4339 for (; y != NIL; y = cdr (y))
4173 {
4174 if (eqv (car (y), SCHEME_V->value)) 4340 if (eqv (car (y), SCHEME_V->value))
4175 {
4176 break; 4341 break;
4177 }
4178 }
4179 4342
4180 if (y != NIL) 4343 if (y != NIL)
4181 {
4182 break; 4344 break;
4183 }
4184 } 4345 }
4185 4346
4186 if (x != NIL) 4347 if (x != NIL)
4187 { 4348 {
4188 if (is_pair (caar (x))) 4349 if (is_pair (caar (x)))
4195 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); 4356 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x));
4196 SCHEME_V->code = caar (x); 4357 SCHEME_V->code = caar (x);
4197 s_goto (OP_EVAL); 4358 s_goto (OP_EVAL);
4198 } 4359 }
4199 } 4360 }
4200 else 4361
4201 {
4202 s_return (NIL); 4362 s_return (NIL);
4203 }
4204 4363
4205 case OP_CASE2: /* case */ 4364 case OP_CASE2: /* case */
4206 if (is_true (SCHEME_V->value)) 4365 if (is_true (SCHEME_V->value))
4366 s_goto (OP_BEGIN);
4367
4368 s_return (NIL);
4369
4370 case OP_PAPPLY: /* apply */
4371 SCHEME_V->code = car (args);
4372 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
4373 /*SCHEME_V->args = cadr(args); */
4374 s_goto (OP_APPLY);
4375
4376 case OP_PEVAL: /* eval */
4377 if (cdr (args) != NIL)
4378 SCHEME_V->envir = cadr (args);
4379
4380 SCHEME_V->code = car (args);
4381 s_goto (OP_EVAL);
4382
4383 case OP_CONTINUATION: /* call-with-current-continuation */
4384 SCHEME_V->code = car (args);
4385 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
4386 s_goto (OP_APPLY);
4387 }
4388
4389 if (USE_ERROR_CHECKING) abort ();
4390}
4391
4392/* math, cxr */
4393ecb_hot static int
4394opexe_1 (SCHEME_P_ enum scheme_opcodes op)
4395{
4396 pointer args = SCHEME_V->args;
4397 pointer x = car (args);
4398 num v;
4399
4400 switch (op)
4401 {
4402#if USE_MATH
4403 case OP_INEX2EX: /* inexact->exact */
4404 if (!is_integer (x))
4207 { 4405 {
4208 s_goto (OP_BEGIN); 4406 RVALUE r = rvalue_unchecked (x);
4407
4408 if (r == (RVALUE)(IVALUE)r)
4409 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
4410 else
4411 Error_1 ("inexact->exact: not integral:", x);
4209 } 4412 }
4210 else
4211 {
4212 s_return (NIL);
4213 }
4214 4413
4215 case OP_PAPPLY: /* apply */
4216 SCHEME_V->code = car (SCHEME_V->args);
4217 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args));
4218 /*SCHEME_V->args = cadr(SCHEME_V->args); */
4219 s_goto (OP_APPLY);
4220
4221 case OP_PEVAL: /* eval */
4222 if (cdr (SCHEME_V->args) != NIL)
4223 {
4224 SCHEME_V->envir = cadr (SCHEME_V->args);
4225 }
4226
4227 SCHEME_V->code = car (SCHEME_V->args);
4228 s_goto (OP_EVAL);
4229
4230 case OP_CONTINUATION: /* call-with-current-continuation */
4231 SCHEME_V->code = car (SCHEME_V->args);
4232 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_V)), NIL);
4233 s_goto (OP_APPLY);
4234 }
4235
4236 return S_T;
4237}
4238
4239static pointer
4240opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4241{
4242 pointer x;
4243 num v;
4244
4245#if USE_MATH
4246 RVALUE dd;
4247#endif
4248
4249 switch (op)
4250 {
4251#if USE_MATH
4252
4253 case OP_INEX2EX: /* inexact->exact */
4254 x = car (SCHEME_V->args);
4255
4256 if (num_is_integer (x))
4257 {
4258 s_return (x); 4414 s_return (x);
4259 }
4260 else if (modf (rvalue_unchecked (x), &dd) == 0.0)
4261 {
4262 s_return (mk_integer (SCHEME_A_ ivalue (x)));
4263 }
4264 else
4265 {
4266 Error_1 ("inexact->exact: not integral:", x);
4267 }
4268 4415
4269 case OP_EXP: 4416 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4270 x = car (SCHEME_V->args); 4417 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4418 case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x))));
4419 case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x))));
4420
4421 case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4271 s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4422 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4272
4273 case OP_LOG:
4274 x = car (SCHEME_V->args);
4275 s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 4423 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
4276 4424 / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args))))));
4277 case OP_SIN:
4278 x = car (SCHEME_V->args);
4279 s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4425 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
4280
4281 case OP_COS:
4282 x = car (SCHEME_V->args);
4283 s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 4426 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
4284
4285 case OP_TAN:
4286 x = car (SCHEME_V->args);
4287 s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); 4427 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x))));
4288
4289 case OP_ASIN:
4290 x = car (SCHEME_V->args);
4291 s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); 4428 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x))));
4292
4293 case OP_ACOS:
4294 x = car (SCHEME_V->args);
4295 s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); 4429 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4296 4430
4297 case OP_ATAN: 4431 case OP_ATAN:
4298 x = car (SCHEME_V->args);
4299
4300 if (cdr (SCHEME_V->args) == NIL)
4301 {
4302 s_return (mk_real (SCHEME_A_ atan (rvalue (x))));
4303 }
4304 else
4305 {
4306 pointer y = cadr (SCHEME_V->args);
4307
4308 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4309 }
4310
4311 case OP_SQRT:
4312 x = car (SCHEME_V->args);
4313 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); 4432 s_return (mk_real (SCHEME_A_
4433 cdr (args) == NIL
4434 ? atan (rvalue (x))
4435 : atan2 (rvalue (x), rvalue (cadr (args)))));
4314 4436
4315 case OP_EXPT: 4437 case OP_EXPT:
4316 { 4438 {
4317 RVALUE result; 4439 RVALUE result;
4318 int real_result = 1; 4440 int real_result = 1;
4319 pointer y = cadr (SCHEME_V->args); 4441 pointer y = cadr (args);
4320 4442
4321 x = car (SCHEME_V->args);
4322
4323 if (num_is_integer (x) && num_is_integer (y)) 4443 if (is_integer (x) && is_integer (y))
4324 real_result = 0; 4444 real_result = 0;
4325 4445
4326 /* This 'if' is an R5RS compatibility fix. */ 4446 /* This 'if' is an R5RS compatibility fix. */
4327 /* NOTE: Remove this 'if' fix for R6RS. */ 4447 /* NOTE: Remove this 'if' fix for R6RS. */
4328 if (rvalue (x) == 0 && rvalue (y) < 0) 4448 if (rvalue (x) == 0 && rvalue (y) < 0)
4329 {
4330 result = 0.0; 4449 result = 0;
4331 }
4332 else 4450 else
4333 {
4334 result = pow (rvalue (x), rvalue (y)); 4451 result = pow (rvalue (x), rvalue (y));
4335 }
4336 4452
4337 /* Before returning integer result make sure we can. */ 4453 /* Before returning integer result make sure we can. */
4338 /* If the test fails, result is too big for integer. */ 4454 /* If the test fails, result is too big for integer. */
4339 if (!real_result) 4455 if (!real_result)
4340 { 4456 {
4341 long result_as_long = (long) result; 4457 long result_as_long = result;
4342 4458
4343 if (result != (RVALUE) result_as_long) 4459 if (result != result_as_long)
4344 real_result = 1; 4460 real_result = 1;
4345 } 4461 }
4346 4462
4347 if (real_result) 4463 if (real_result)
4348 {
4349 s_return (mk_real (SCHEME_A_ result)); 4464 s_return (mk_real (SCHEME_A_ result));
4350 }
4351 else 4465 else
4352 {
4353 s_return (mk_integer (SCHEME_A_ result)); 4466 s_return (mk_integer (SCHEME_A_ result));
4354 }
4355 } 4467 }
4356
4357 case OP_FLOOR:
4358 x = car (SCHEME_V->args);
4359 s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4360
4361 case OP_CEILING:
4362 x = car (SCHEME_V->args);
4363 s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4364
4365 case OP_TRUNCATE:
4366 {
4367 RVALUE rvalue_of_x;
4368
4369 x = car (SCHEME_V->args);
4370 rvalue_of_x = rvalue (x);
4371
4372 if (rvalue_of_x > 0)
4373 {
4374 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x)));
4375 }
4376 else
4377 {
4378 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4379 }
4380 }
4381
4382 case OP_ROUND:
4383 x = car (SCHEME_V->args);
4384
4385 if (num_is_integer (x))
4386 s_return (x);
4387
4388 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4389#endif 4468#endif
4390 4469
4391 case OP_ADD: /* + */ 4470 case OP_ADD: /* + */
4392 v = num_zero; 4471 v = num_zero;
4393 4472
4394 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4473 for (x = args; x != NIL; x = cdr (x))
4395 v = num_add (v, nvalue (car (x))); 4474 v = num_op (NUM_ADD, v, nvalue (car (x)));
4396 4475
4397 s_return (mk_number (SCHEME_A_ v)); 4476 s_return (mk_number (SCHEME_A_ v));
4398 4477
4399 case OP_MUL: /* * */ 4478 case OP_MUL: /* * */
4400 v = num_one; 4479 v = num_one;
4401 4480
4402 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4481 for (x = args; x != NIL; x = cdr (x))
4403 v = num_mul (v, nvalue (car (x))); 4482 v = num_op (NUM_MUL, v, nvalue (car (x)));
4404 4483
4405 s_return (mk_number (SCHEME_A_ v)); 4484 s_return (mk_number (SCHEME_A_ v));
4406 4485
4407 case OP_SUB: /* - */ 4486 case OP_SUB: /* - */
4408 if (cdr (SCHEME_V->args) == NIL) 4487 if (cdr (args) == NIL)
4409 { 4488 {
4410 x = SCHEME_V->args; 4489 x = args;
4411 v = num_zero; 4490 v = num_zero;
4412 } 4491 }
4413 else 4492 else
4414 { 4493 {
4415 x = cdr (SCHEME_V->args); 4494 x = cdr (args);
4416 v = nvalue (car (SCHEME_V->args)); 4495 v = nvalue (car (args));
4417 } 4496 }
4418 4497
4419 for (; x != NIL; x = cdr (x)) 4498 for (; x != NIL; x = cdr (x))
4420 v = num_sub (v, nvalue (car (x))); 4499 v = num_op (NUM_SUB, v, nvalue (car (x)));
4421 4500
4422 s_return (mk_number (SCHEME_A_ v)); 4501 s_return (mk_number (SCHEME_A_ v));
4423 4502
4424 case OP_DIV: /* / */ 4503 case OP_DIV: /* / */
4425 if (cdr (SCHEME_V->args) == NIL) 4504 if (cdr (args) == NIL)
4426 { 4505 {
4427 x = SCHEME_V->args; 4506 x = args;
4428 v = num_one; 4507 v = num_one;
4429 } 4508 }
4430 else 4509 else
4431 { 4510 {
4432 x = cdr (SCHEME_V->args); 4511 x = cdr (args);
4433 v = nvalue (car (SCHEME_V->args)); 4512 v = nvalue (car (args));
4434 } 4513 }
4435 4514
4436 for (; x != NIL; x = cdr (x)) 4515 for (; x != NIL; x = cdr (x))
4516 if (!is_zero_rvalue (rvalue (car (x))))
4517 v = num_div (v, nvalue (car (x)));
4518 else
4519 Error_0 ("/: division by zero");
4520
4521 s_return (mk_number (SCHEME_A_ v));
4522
4523 case OP_INTDIV: /* quotient */
4524 if (cdr (args) == NIL)
4437 { 4525 {
4438 if (!is_zero_rvalue (rvalue (car (x))))
4439 v = num_div (v, nvalue (car (x)));
4440 else
4441 Error_0 ("/: division by zero");
4442 }
4443
4444 s_return (mk_number (SCHEME_A_ v));
4445
4446 case OP_INTDIV: /* quotient */
4447 if (cdr (SCHEME_V->args) == NIL)
4448 {
4449 x = SCHEME_V->args; 4526 x = args;
4450 v = num_one; 4527 v = num_one;
4451 } 4528 }
4452 else 4529 else
4453 { 4530 {
4454 x = cdr (SCHEME_V->args); 4531 x = cdr (args);
4455 v = nvalue (car (SCHEME_V->args)); 4532 v = nvalue (car (args));
4456 } 4533 }
4457 4534
4458 for (; x != NIL; x = cdr (x)) 4535 for (; x != NIL; x = cdr (x))
4459 { 4536 {
4460 if (ivalue (car (x)) != 0) 4537 if (ivalue (car (x)) != 0)
4461 v = num_intdiv (v, nvalue (car (x))); 4538 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4462 else 4539 else
4463 Error_0 ("quotient: division by zero"); 4540 Error_0 ("quotient: division by zero");
4464 } 4541 }
4465 4542
4466 s_return (mk_number (SCHEME_A_ v)); 4543 s_return (mk_number (SCHEME_A_ v));
4467 4544
4468 case OP_REM: /* remainder */ 4545 case OP_REM: /* remainder */
4469 v = nvalue (car (SCHEME_V->args)); 4546 v = nvalue (x);
4470 4547
4471 if (ivalue (cadr (SCHEME_V->args)) != 0) 4548 if (ivalue (cadr (args)) != 0)
4472 v = num_rem (v, nvalue (cadr (SCHEME_V->args))); 4549 v = num_rem (v, nvalue (cadr (args)));
4473 else 4550 else
4474 Error_0 ("remainder: division by zero"); 4551 Error_0 ("remainder: division by zero");
4475 4552
4476 s_return (mk_number (SCHEME_A_ v)); 4553 s_return (mk_number (SCHEME_A_ v));
4477 4554
4478 case OP_MOD: /* modulo */ 4555 case OP_MOD: /* modulo */
4479 v = nvalue (car (SCHEME_V->args)); 4556 v = nvalue (x);
4480 4557
4481 if (ivalue (cadr (SCHEME_V->args)) != 0) 4558 if (ivalue (cadr (args)) != 0)
4482 v = num_mod (v, nvalue (cadr (SCHEME_V->args))); 4559 v = num_mod (v, nvalue (cadr (args)));
4483 else 4560 else
4484 Error_0 ("modulo: division by zero"); 4561 Error_0 ("modulo: division by zero");
4485 4562
4486 s_return (mk_number (SCHEME_A_ v)); 4563 s_return (mk_number (SCHEME_A_ v));
4487 4564
4488 case OP_CAR: /* car */ 4565 /* the compiler will optimize this mess... */
4489 s_return (caar (SCHEME_V->args)); 4566 case OP_CAR: op_car: s_return (car (x));
4490 4567 case OP_CDR: op_cdr: s_return (cdr (x));
4491 case OP_CDR: /* cdr */ 4568 case OP_CAAR: op_caar: x = car (x); goto op_car;
4492 s_return (cdar (SCHEME_V->args)); 4569 case OP_CADR: op_cadr: x = cdr (x); goto op_car;
4570 case OP_CDAR: op_cdar: x = car (x); goto op_cdr;
4571 case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr;
4572 case OP_CAAAR: op_caaar: x = car (x); goto op_caar;
4573 case OP_CAADR: op_caadr: x = cdr (x); goto op_caar;
4574 case OP_CADAR: op_cadar: x = car (x); goto op_cadr;
4575 case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr;
4576 case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar;
4577 case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar;
4578 case OP_CDDAR: op_cddar: x = car (x); goto op_cddr;
4579 case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr;
4580 case OP_CAAAAR: x = car (x); goto op_caaar;
4581 case OP_CAAADR: x = cdr (x); goto op_caaar;
4582 case OP_CAADAR: x = car (x); goto op_caadr;
4583 case OP_CAADDR: x = cdr (x); goto op_caadr;
4584 case OP_CADAAR: x = car (x); goto op_cadar;
4585 case OP_CADADR: x = cdr (x); goto op_cadar;
4586 case OP_CADDAR: x = car (x); goto op_caddr;
4587 case OP_CADDDR: x = cdr (x); goto op_caddr;
4588 case OP_CDAAAR: x = car (x); goto op_cdaar;
4589 case OP_CDAADR: x = cdr (x); goto op_cdaar;
4590 case OP_CDADAR: x = car (x); goto op_cdadr;
4591 case OP_CDADDR: x = cdr (x); goto op_cdadr;
4592 case OP_CDDAAR: x = car (x); goto op_cddar;
4593 case OP_CDDADR: x = cdr (x); goto op_cddar;
4594 case OP_CDDDAR: x = car (x); goto op_cdddr;
4595 case OP_CDDDDR: x = cdr (x); goto op_cdddr;
4493 4596
4494 case OP_CONS: /* cons */ 4597 case OP_CONS: /* cons */
4495 set_cdr (SCHEME_V->args, cadr (SCHEME_V->args)); 4598 set_cdr (args, cadr (args));
4496 s_return (SCHEME_V->args); 4599 s_return (args);
4497 4600
4498 case OP_SETCAR: /* set-car! */ 4601 case OP_SETCAR: /* set-car! */
4499 if (!is_immutable (car (SCHEME_V->args))) 4602 if (!is_immutable (x))
4500 { 4603 {
4501 set_car (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4604 set_car (x, cadr (args));
4502 s_return (car (SCHEME_V->args)); 4605 s_return (car (args));
4503 } 4606 }
4504 else 4607 else
4505 Error_0 ("set-car!: unable to alter immutable pair"); 4608 Error_0 ("set-car!: unable to alter immutable pair");
4506 4609
4507 case OP_SETCDR: /* set-cdr! */ 4610 case OP_SETCDR: /* set-cdr! */
4508 if (!is_immutable (car (SCHEME_V->args))) 4611 if (!is_immutable (x))
4509 { 4612 {
4510 set_cdr (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4613 set_cdr (x, cadr (args));
4511 s_return (car (SCHEME_V->args)); 4614 s_return (car (args));
4512 } 4615 }
4513 else 4616 else
4514 Error_0 ("set-cdr!: unable to alter immutable pair"); 4617 Error_0 ("set-cdr!: unable to alter immutable pair");
4515 4618
4516 case OP_CHAR2INT: /* char->integer */ 4619 case OP_CHAR2INT: /* char->integer */
4517 {
4518 char c;
4519
4520 c = (char) ivalue (car (SCHEME_V->args));
4521 s_return (mk_integer (SCHEME_A_ (unsigned char) c)); 4620 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4522 }
4523 4621
4524 case OP_INT2CHAR: /* integer->char */ 4622 case OP_INT2CHAR: /* integer->char */
4525 {
4526 unsigned char c;
4527
4528 c = (unsigned char) ivalue (car (SCHEME_V->args));
4529 s_return (mk_character (SCHEME_A_ (char) c)); 4623 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4530 }
4531 4624
4532 case OP_CHARUPCASE: 4625 case OP_CHARUPCASE:
4533 { 4626 {
4534 unsigned char c; 4627 unsigned char c = ivalue_unchecked (x);
4535
4536 c = (unsigned char) ivalue (car (SCHEME_V->args));
4537 c = toupper (c); 4628 c = toupper (c);
4538 s_return (mk_character (SCHEME_A_ (char) c)); 4629 s_return (mk_character (SCHEME_A_ c));
4539 } 4630 }
4540 4631
4541 case OP_CHARDNCASE: 4632 case OP_CHARDNCASE:
4542 { 4633 {
4543 unsigned char c; 4634 unsigned char c = ivalue_unchecked (x);
4544
4545 c = (unsigned char) ivalue (car (SCHEME_V->args));
4546 c = tolower (c); 4635 c = tolower (c);
4547 s_return (mk_character (SCHEME_A_ (char) c)); 4636 s_return (mk_character (SCHEME_A_ c));
4548 } 4637 }
4549 4638
4550 case OP_STR2SYM: /* string->symbol */ 4639 case OP_STR2SYM: /* string->symbol */
4551 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); 4640 s_return (mk_symbol (SCHEME_A_ strvalue (x)));
4552 4641
4553 case OP_STR2ATOM: /* string->atom */ 4642 case OP_STR2ATOM: /* string->atom */
4554 { 4643 {
4555 char *s = strvalue (car (SCHEME_V->args)); 4644 char *s = strvalue (x);
4556 long pf = 0; 4645 long pf = 0;
4557 4646
4558 if (cdr (SCHEME_V->args) != NIL) 4647 if (cdr (args) != NIL)
4559 { 4648 {
4560 /* we know cadr(SCHEME_V->args) is a natural number */ 4649 /* we know cadr(args) is a natural number */
4561 /* see if it is 2, 8, 10, or 16, or error */ 4650 /* see if it is 2, 8, 10, or 16, or error */
4562 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4651 pf = ivalue_unchecked (cadr (args));
4563 4652
4564 if (pf == 16 || pf == 10 || pf == 8 || pf == 2) 4653 if (pf == 16 || pf == 10 || pf == 8 || pf == 2)
4565 { 4654 {
4566 /* base is OK */ 4655 /* base is OK */
4567 } 4656 }
4568 else 4657 else
4569 {
4570 pf = -1; 4658 pf = -1;
4571 }
4572 } 4659 }
4573 4660
4574 if (pf < 0) 4661 if (pf < 0)
4575 {
4576 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); 4662 Error_1 ("string->atom: bad base:", cadr (args));
4577 }
4578 else if (*s == '#') /* no use of base! */ 4663 else if (*s == '#') /* no use of base! */
4579 {
4580 s_return (mk_sharp_const (SCHEME_A_ s + 1)); 4664 s_return (mk_sharp_const (SCHEME_A_ s + 1));
4581 }
4582 else 4665 else
4583 { 4666 {
4584 if (pf == 0 || pf == 10) 4667 if (pf == 0 || pf == 10)
4585 {
4586 s_return (mk_atom (SCHEME_A_ s)); 4668 s_return (mk_atom (SCHEME_A_ s));
4587 }
4588 else 4669 else
4589 { 4670 {
4590 char *ep; 4671 char *ep;
4591 long iv = strtol (s, &ep, (int) pf); 4672 long iv = strtol (s, &ep, (int) pf);
4592 4673
4593 if (*ep == 0) 4674 if (*ep == 0)
4594 {
4595 s_return (mk_integer (SCHEME_A_ iv)); 4675 s_return (mk_integer (SCHEME_A_ iv));
4596 }
4597 else 4676 else
4598 {
4599 s_return (S_F); 4677 s_return (S_F);
4600 }
4601 } 4678 }
4602 } 4679 }
4603 } 4680 }
4604 4681
4605 case OP_SYM2STR: /* symbol->string */ 4682 case OP_SYM2STR: /* symbol->string */
4606 x = mk_string (SCHEME_A_ symname (car (SCHEME_V->args))); 4683 x = mk_string (SCHEME_A_ symname (x));
4607 setimmutable (x); 4684 setimmutable (x);
4608 s_return (x); 4685 s_return (x);
4609 4686
4610 case OP_ATOM2STR: /* atom->string */ 4687 case OP_ATOM2STR: /* atom->string */
4611 { 4688 {
4612 long pf = 0; 4689 long pf = 0;
4613 4690
4614 x = car (SCHEME_V->args);
4615
4616 if (cdr (SCHEME_V->args) != NIL) 4691 if (cdr (args) != NIL)
4617 { 4692 {
4618 /* we know cadr(SCHEME_V->args) is a natural number */ 4693 /* we know cadr(args) is a natural number */
4619 /* see if it is 2, 8, 10, or 16, or error */ 4694 /* see if it is 2, 8, 10, or 16, or error */
4620 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4695 pf = ivalue_unchecked (cadr (args));
4621 4696
4622 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) 4697 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2))
4623 { 4698 {
4624 /* base is OK */ 4699 /* base is OK */
4625 } 4700 }
4626 else 4701 else
4627 {
4628 pf = -1; 4702 pf = -1;
4629 }
4630 } 4703 }
4631 4704
4632 if (pf < 0) 4705 if (pf < 0)
4633 {
4634 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); 4706 Error_1 ("atom->string: bad base:", cadr (args));
4635 }
4636 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) 4707 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x))
4637 { 4708 {
4638 char *p; 4709 char *p;
4639 int len; 4710 int len;
4640 4711
4641 atom2str (SCHEME_A_ x, (int) pf, &p, &len); 4712 atom2str (SCHEME_A_ x, pf, &p, &len);
4642 s_return (mk_counted_string (SCHEME_A_ p, len)); 4713 s_return (mk_counted_string (SCHEME_A_ p, len));
4643 } 4714 }
4644 else 4715 else
4645 {
4646 Error_1 ("atom->string: not an atom:", x); 4716 Error_1 ("atom->string: not an atom:", x);
4647 }
4648 } 4717 }
4649 4718
4650 case OP_MKSTRING: /* make-string */ 4719 case OP_MKSTRING: /* make-string */
4651 { 4720 {
4652 int fill = ' '; 4721 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4653 int len; 4722 int len = ivalue_unchecked (x);
4654 4723
4655 len = ivalue (car (SCHEME_V->args));
4656
4657 if (cdr (SCHEME_V->args) != NIL)
4658 {
4659 fill = charvalue (cadr (SCHEME_V->args));
4660 }
4661
4662 s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); 4724 s_return (mk_empty_string (SCHEME_A_ len, fill));
4663 } 4725 }
4664 4726
4665 case OP_STRLEN: /* string-length */ 4727 case OP_STRLEN: /* string-length */
4666 s_return (mk_integer (SCHEME_A_ strlength (car (SCHEME_V->args)))); 4728 s_return (mk_integer (SCHEME_A_ strlength (x)));
4667 4729
4668 case OP_STRREF: /* string-ref */ 4730 case OP_STRREF: /* string-ref */
4669 { 4731 {
4670 char *str; 4732 char *str = strvalue (x);
4671 int index;
4672
4673 str = strvalue (car (SCHEME_V->args));
4674
4675 index = ivalue (cadr (SCHEME_V->args)); 4733 int index = ivalue_unchecked (cadr (args));
4676 4734
4677 if (index >= strlength (car (SCHEME_V->args))) 4735 if (index >= strlength (x))
4678 {
4679 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); 4736 Error_1 ("string-ref: out of bounds:", cadr (args));
4680 }
4681 4737
4682 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); 4738 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4683 } 4739 }
4684 4740
4685 case OP_STRSET: /* string-set! */ 4741 case OP_STRSET: /* string-set! */
4686 { 4742 {
4687 char *str; 4743 char *str = strvalue (x);
4688 int index; 4744 int index = ivalue_unchecked (cadr (args));
4689 int c; 4745 int c;
4690 4746
4691 if (is_immutable (car (SCHEME_V->args))) 4747 if (is_immutable (x))
4692 {
4693 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); 4748 Error_1 ("string-set!: unable to alter immutable string:", x);
4694 }
4695 4749
4696 str = strvalue (car (SCHEME_V->args));
4697
4698 index = ivalue (cadr (SCHEME_V->args));
4699
4700 if (index >= strlength (car (SCHEME_V->args))) 4750 if (index >= strlength (x))
4701 {
4702 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); 4751 Error_1 ("string-set!: out of bounds:", cadr (args));
4703 }
4704 4752
4705 c = charvalue (caddr (SCHEME_V->args)); 4753 c = charvalue (caddr (args));
4706 4754
4707 str[index] = (char) c; 4755 str[index] = c;
4708 s_return (car (SCHEME_V->args)); 4756 s_return (car (args));
4709 } 4757 }
4710 4758
4711 case OP_STRAPPEND: /* string-append */ 4759 case OP_STRAPPEND: /* string-append */
4712 { 4760 {
4713 /* in 1.29 string-append was in Scheme in init.scm but was too slow */ 4761 /* in 1.29 string-append was in Scheme in init.scm but was too slow */
4714 int len = 0; 4762 int len = 0;
4715 pointer newstr; 4763 pointer newstr;
4716 char *pos; 4764 char *pos;
4717 4765
4718 /* compute needed length for new string */ 4766 /* compute needed length for new string */
4719 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4767 for (x = args; x != NIL; x = cdr (x))
4720 {
4721 len += strlength (car (x)); 4768 len += strlength (car (x));
4722 }
4723 4769
4724 newstr = mk_empty_string (SCHEME_A_ len, ' '); 4770 newstr = mk_empty_string (SCHEME_A_ len, ' ');
4725 4771
4726 /* store the contents of the argument strings into the new string */ 4772 /* store the contents of the argument strings into the new string */
4727 for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) 4773 for (pos = strvalue (newstr), x = args; x != NIL; pos += strlength (car (x)), x = cdr (x))
4728 {
4729 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4774 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4730 }
4731 4775
4732 s_return (newstr); 4776 s_return (newstr);
4733 } 4777 }
4734 4778
4735 case OP_SUBSTR: /* substring */ 4779 case OP_STRING_COPY: /* substring/string-copy */
4736 { 4780 {
4737 char *str; 4781 char *str = strvalue (x);
4738 int index0; 4782 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4739 int index1; 4783 int index1;
4740 int len; 4784 int len;
4741 4785
4742 str = strvalue (car (SCHEME_V->args));
4743
4744 index0 = ivalue (cadr (SCHEME_V->args));
4745
4746 if (index0 > strlength (car (SCHEME_V->args))) 4786 if (index0 > strlength (x))
4787 Error_1 ("string->copy: start out of bounds:", cadr (args));
4788
4789 if (cddr (args) != NIL)
4747 { 4790 {
4748 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args));
4749 }
4750
4751 if (cddr (SCHEME_V->args) != NIL)
4752 {
4753 index1 = ivalue (caddr (SCHEME_V->args)); 4791 index1 = ivalue_unchecked (caddr (args));
4754 4792
4755 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) 4793 if (index1 > strlength (x) || index1 < index0)
4756 {
4757 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); 4794 Error_1 ("string->copy: end out of bounds:", caddr (args));
4758 }
4759 } 4795 }
4760 else 4796 else
4761 { 4797 index1 = strlength (x);
4762 index1 = strlength (car (SCHEME_V->args));
4763 }
4764 4798
4765 len = index1 - index0; 4799 len = index1 - index0;
4766 x = mk_empty_string (SCHEME_A_ len, ' '); 4800 x = mk_counted_string (SCHEME_A_ str + index0, len);
4767 memcpy (strvalue (x), str + index0, len);
4768 strvalue (x)[len] = 0;
4769 4801
4770 s_return (x); 4802 s_return (x);
4771 } 4803 }
4772 4804
4773 case OP_VECTOR: /* vector */ 4805 case OP_VECTOR: /* vector */
4774 { 4806 {
4775 int i; 4807 int i;
4776 pointer vec; 4808 pointer vec;
4777 int len = list_length (SCHEME_A_ SCHEME_V->args); 4809 int len = list_length (SCHEME_A_ args);
4778 4810
4779 if (len < 0) 4811 if (len < 0)
4780 {
4781 Error_1 ("vector: not a proper list:", SCHEME_V->args); 4812 Error_1 ("vector: not a proper list:", args);
4782 }
4783 4813
4784 vec = mk_vector (SCHEME_A_ len); 4814 vec = mk_vector (SCHEME_A_ len);
4785 4815
4786#if USE_ERROR_CHECKING 4816#if USE_ERROR_CHECKING
4787 if (SCHEME_V->no_memory) 4817 if (SCHEME_V->no_memory)
4788 s_return (S_SINK); 4818 s_return (S_SINK);
4789#endif 4819#endif
4790 4820
4791 for (x = SCHEME_V->args, i = 0; is_pair (x); x = cdr (x), i++) 4821 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4792 set_vector_elem (vec, i, car (x)); 4822 vector_set (vec, i, car (x));
4793 4823
4794 s_return (vec); 4824 s_return (vec);
4795 } 4825 }
4796 4826
4797 case OP_MKVECTOR: /* make-vector */ 4827 case OP_MKVECTOR: /* make-vector */
4798 { 4828 {
4799 pointer fill = NIL; 4829 pointer fill = NIL;
4800 int len;
4801 pointer vec; 4830 pointer vec;
4831 int len = ivalue_unchecked (x);
4802 4832
4803 len = ivalue (car (SCHEME_V->args));
4804
4805 if (cdr (SCHEME_V->args) != NIL) 4833 if (cdr (args) != NIL)
4806 fill = cadr (SCHEME_V->args); 4834 fill = cadr (args);
4807 4835
4808 vec = mk_vector (SCHEME_A_ len); 4836 vec = mk_vector (SCHEME_A_ len);
4809 4837
4810#if USE_ERROR_CHECKING 4838#if USE_ERROR_CHECKING
4811 if (SCHEME_V->no_memory) 4839 if (SCHEME_V->no_memory)
4812 s_return (S_SINK); 4840 s_return (S_SINK);
4813#endif 4841#endif
4814 4842
4815 if (fill != NIL) 4843 if (fill != NIL)
4816 fill_vector (vec, fill); 4844 fill_vector (vec, 0, fill);
4817 4845
4818 s_return (vec); 4846 s_return (vec);
4819 } 4847 }
4820 4848
4821 case OP_VECLEN: /* vector-length */ 4849 case OP_VECLEN: /* vector-length */
4822 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4850 s_return (mk_integer (SCHEME_A_ veclength (x)));
4851
4852 case OP_VECRESIZE:
4853 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4854 s_return (x);
4823 4855
4824 case OP_VECREF: /* vector-ref */ 4856 case OP_VECREF: /* vector-ref */
4825 { 4857 {
4826 int index;
4827
4828 index = ivalue (cadr (SCHEME_V->args)); 4858 int index = ivalue_unchecked (cadr (args));
4829 4859
4830 if (index >= ivalue (car (SCHEME_V->args))) 4860 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4831 {
4832 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); 4861 Error_1 ("vector-ref: out of bounds:", cadr (args));
4833 }
4834 4862
4835 s_return (vector_elem (car (SCHEME_V->args), index)); 4863 s_return (vector_get (x, index));
4836 } 4864 }
4837 4865
4838 case OP_VECSET: /* vector-set! */ 4866 case OP_VECSET: /* vector-set! */
4839 { 4867 {
4840 int index; 4868 int index = ivalue_unchecked (cadr (args));
4841 4869
4842 if (is_immutable (car (SCHEME_V->args))) 4870 if (is_immutable (x))
4843 {
4844 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); 4871 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4845 }
4846 4872
4847 index = ivalue (cadr (SCHEME_V->args)); 4873 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4848
4849 if (index >= ivalue (car (SCHEME_V->args)))
4850 {
4851 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); 4874 Error_1 ("vector-set!: out of bounds:", cadr (args));
4852 }
4853 4875
4854 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); 4876 vector_set (x, index, caddr (args));
4855 s_return (car (SCHEME_V->args)); 4877 s_return (x);
4856 } 4878 }
4857 } 4879 }
4858 4880
4859 return S_T; 4881 if (USE_ERROR_CHECKING) abort ();
4860} 4882}
4861 4883
4862INTERFACE int 4884/* relational ops */
4863is_list (SCHEME_P_ pointer a) 4885ecb_hot static int
4886opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4864{ 4887{
4865 return list_length (SCHEME_A_ a) >= 0; 4888 pointer x = SCHEME_V->args;
4866}
4867 4889
4868/* Result is: 4890 for (;;)
4869 proper list: length
4870 circular list: -1
4871 not even a pair: -2
4872 dotted list: -2 minus length before dot
4873*/
4874INTERFACE int
4875list_length (SCHEME_P_ pointer a)
4876{
4877 int i = 0;
4878 pointer slow, fast;
4879
4880 slow = fast = a;
4881
4882 while (1)
4883 { 4891 {
4892 num v = nvalue (car (x));
4893 x = cdr (x);
4894
4884 if (fast == NIL) 4895 if (x == NIL)
4885 return i; 4896 break;
4886 4897
4887 if (!is_pair (fast)) 4898 int r = num_cmp (v, nvalue (car (x)));
4888 return -2 - i;
4889 4899
4890 fast = cdr (fast); 4900 switch (op)
4891 ++i;
4892
4893 if (fast == NIL)
4894 return i;
4895
4896 if (!is_pair (fast))
4897 return -2 - i;
4898
4899 ++i;
4900 fast = cdr (fast);
4901
4902 /* Safe because we would have already returned if `fast'
4903 encountered a non-pair. */
4904 slow = cdr (slow);
4905
4906 if (fast == slow)
4907 { 4901 {
4908 /* the fast pointer has looped back around and caught up 4902 case OP_NUMEQ: r = r == 0; break;
4909 with the slow pointer, hence the structure is circular, 4903 case OP_LESS: r = r < 0; break;
4910 not of finite length, and therefore not a list */ 4904 case OP_GRE: r = r > 0; break;
4911 return -1; 4905 case OP_LEQ: r = r <= 0; break;
4906 case OP_GEQ: r = r >= 0; break;
4912 } 4907 }
4913 }
4914}
4915 4908
4916static pointer 4909 if (!r)
4910 s_return (S_F);
4911 }
4912
4913 s_return (S_T);
4914}
4915
4916/* predicates */
4917ecb_hot static int
4917opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4918opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4918{ 4919{
4919 pointer x; 4920 pointer args = SCHEME_V->args;
4920 num v; 4921 pointer a = car (args);
4921 int (*comp_func) (num, num); 4922 pointer d = cdr (args);
4923 int r;
4922 4924
4923 switch (op) 4925 switch (op)
4924 { 4926 {
4925 case OP_NOT: /* not */ 4927 case OP_NOT: /* not */ r = is_false (a) ; break;
4926 s_retbool (is_false (car (SCHEME_V->args))); 4928 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break;
4929 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4930 case OP_NULLP: /* null? */ r = a == NIL ; break;
4931 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4932 case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break;
4933 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4934 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4935 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4936 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4937 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4927 4938
4928 case OP_BOOLP: /* boolean? */
4929 s_retbool (car (SCHEME_V->args) == S_F || car (SCHEME_V->args) == S_T);
4930
4931 case OP_EOFOBJP: /* boolean? */
4932 s_retbool (car (SCHEME_V->args) == S_EOF);
4933
4934 case OP_NULLP: /* null? */
4935 s_retbool (car (SCHEME_V->args) == NIL);
4936
4937 case OP_NUMEQ: /* = */
4938 case OP_LESS: /* < */
4939 case OP_GRE: /* > */
4940 case OP_LEQ: /* <= */
4941 case OP_GEQ: /* >= */
4942 switch (op)
4943 {
4944 case OP_NUMEQ:
4945 comp_func = num_eq;
4946 break;
4947
4948 case OP_LESS:
4949 comp_func = num_lt;
4950 break;
4951
4952 case OP_GRE:
4953 comp_func = num_gt;
4954 break;
4955
4956 case OP_LEQ:
4957 comp_func = num_le;
4958 break;
4959
4960 case OP_GEQ:
4961 comp_func = num_ge;
4962 break;
4963 }
4964
4965 x = SCHEME_V->args;
4966 v = nvalue (car (x));
4967 x = cdr (x);
4968
4969 for (; x != NIL; x = cdr (x))
4970 {
4971 if (!comp_func (v, nvalue (car (x))))
4972 {
4973 s_retbool (0);
4974 }
4975
4976 v = nvalue (car (x));
4977 }
4978
4979 s_retbool (1);
4980
4981 case OP_SYMBOLP: /* symbol? */
4982 s_retbool (is_symbol (car (SCHEME_V->args)));
4983
4984 case OP_NUMBERP: /* number? */
4985 s_retbool (is_number (car (SCHEME_V->args)));
4986
4987 case OP_STRINGP: /* string? */
4988 s_retbool (is_string (car (SCHEME_V->args)));
4989
4990 case OP_INTEGERP: /* integer? */
4991 s_retbool (is_integer (car (SCHEME_V->args)));
4992
4993 case OP_REALP: /* real? */
4994 s_retbool (is_number (car (SCHEME_V->args))); /* All numbers are real */
4995
4996 case OP_CHARP: /* char? */
4997 s_retbool (is_character (car (SCHEME_V->args)));
4998#if USE_CHAR_CLASSIFIERS 4939#if USE_CHAR_CLASSIFIERS
4999 4940 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
5000 case OP_CHARAP: /* char-alphabetic? */ 4941 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
5001 s_retbool (Cisalpha (ivalue (car (SCHEME_V->args)))); 4942 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
5002 4943 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
5003 case OP_CHARNP: /* char-numeric? */ 4944 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
5004 s_retbool (Cisdigit (ivalue (car (SCHEME_V->args))));
5005
5006 case OP_CHARWP: /* char-whitespace? */
5007 s_retbool (Cisspace (ivalue (car (SCHEME_V->args))));
5008
5009 case OP_CHARUP: /* char-upper-case? */
5010 s_retbool (Cisupper (ivalue (car (SCHEME_V->args))));
5011
5012 case OP_CHARLP: /* char-lower-case? */
5013 s_retbool (Cislower (ivalue (car (SCHEME_V->args))));
5014#endif 4945#endif
4946
5015#if USE_PORTS 4947#if USE_PORTS
5016 4948 case OP_PORTP: /* port? */ r = is_port (a) ; break;
5017 case OP_PORTP: /* port? */
5018 s_retbool (is_port (car (SCHEME_V->args)));
5019
5020 case OP_INPORTP: /* input-port? */ 4949 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
5021 s_retbool (is_inport (car (SCHEME_V->args)));
5022
5023 case OP_OUTPORTP: /* output-port? */ 4950 case OP_OUTPORTP: /* output-port? */ r = is_outport (a); break;
5024 s_retbool (is_outport (car (SCHEME_V->args)));
5025#endif 4951#endif
5026 4952
5027 case OP_PROCP: /* procedure? */ 4953 case OP_PROCP: /* procedure? */
5028 4954
5029 /*-- 4955 /*--
5030 * continuation should be procedure by the example 4956 * continuation should be procedure by the example
5031 * (call-with-current-continuation procedure?) ==> #t 4957 * (call-with-current-continuation procedure?) ==> #t
5032 * in R^3 report sec. 6.9 4958 * in R^3 report sec. 6.9
5033 */ 4959 */
5034 s_retbool (is_proc (car (SCHEME_V->args)) || is_closure (car (SCHEME_V->args)) 4960 r = is_proc (a) || is_closure (a) || is_continuation (a) || is_foreign (a);
5035 || is_continuation (car (SCHEME_V->args)) || is_foreign (car (SCHEME_V->args))); 4961 break;
5036 4962
5037 case OP_PAIRP: /* pair? */ 4963 case OP_PAIRP: /* pair? */ r = is_pair (a) ; break;
5038 s_retbool (is_pair (car (SCHEME_V->args))); 4964 case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break;
5039 4965 case OP_ENVP: /* environment? */ r = is_environment (a) ; break;
5040 case OP_LISTP: /* list? */ 4966 case OP_VECTORP: /* vector? */ r = is_vector (a) ; break;
5041 s_retbool (list_length (SCHEME_A_ car (SCHEME_V->args)) >= 0); 4967 case OP_EQ: /* eq? */ r = a == cadr (args) ; break;
5042 4968 case OP_EQV: /* eqv? */ r = eqv (a, cadr (args)) ; break;
5043 case OP_ENVP: /* environment? */
5044 s_retbool (is_environment (car (SCHEME_V->args)));
5045
5046 case OP_VECTORP: /* vector? */
5047 s_retbool (is_vector (car (SCHEME_V->args)));
5048
5049 case OP_EQ: /* eq? */
5050 s_retbool (car (SCHEME_V->args) == cadr (SCHEME_V->args));
5051
5052 case OP_EQV: /* eqv? */
5053 s_retbool (eqv (car (SCHEME_V->args), cadr (SCHEME_V->args)));
5054 } 4969 }
5055 4970
5056 return S_T; 4971 s_retbool (r);
5057} 4972}
5058 4973
5059static pointer 4974/* promises, list ops, ports */
4975ecb_hot static int
5060opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4976opexe_4 (SCHEME_P_ enum scheme_opcodes op)
5061{ 4977{
4978 pointer args = SCHEME_V->args;
4979 pointer a = car (args);
5062 pointer x, y; 4980 pointer x, y;
5063 4981
5064 switch (op) 4982 switch (op)
5065 { 4983 {
5066 case OP_FORCE: /* force */ 4984 case OP_FORCE: /* force */
5067 SCHEME_V->code = car (SCHEME_V->args); 4985 SCHEME_V->code = a;
5068 4986
5069 if (is_promise (SCHEME_V->code)) 4987 if (is_promise (SCHEME_V->code))
5070 { 4988 {
5071 /* Should change type to closure here */ 4989 /* Should change type to closure here */
5072 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); 4990 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code);
5073 SCHEME_V->args = NIL; 4991 SCHEME_V->args = NIL;
5074 s_goto (OP_APPLY); 4992 s_goto (OP_APPLY);
5075 } 4993 }
5076 else 4994 else
5077 {
5078 s_return (SCHEME_V->code); 4995 s_return (SCHEME_V->code);
5079 }
5080 4996
5081 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4997 case OP_SAVE_FORCED: /* Save forced value replacing promise */
5082 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4998 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
5083 s_return (SCHEME_V->value); 4999 s_return (SCHEME_V->value);
5084 5000
5085#if USE_PORTS 5001#if USE_PORTS
5002
5003 case OP_EOF_OBJECT: /* eof-object */
5004 s_return (S_EOF);
5086 5005
5087 case OP_WRITE: /* write */ 5006 case OP_WRITE: /* write */
5088 case OP_DISPLAY: /* display */ 5007 case OP_DISPLAY: /* display */
5089 case OP_WRITE_CHAR: /* write-char */ 5008 case OP_WRITE_CHAR: /* write-char */
5090 if (is_pair (cdr (SCHEME_V->args))) 5009 if (is_pair (cdr (SCHEME_V->args)))
5095 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 5014 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
5096 SCHEME_V->outport = cadr (SCHEME_V->args); 5015 SCHEME_V->outport = cadr (SCHEME_V->args);
5097 } 5016 }
5098 } 5017 }
5099 5018
5100 SCHEME_V->args = car (SCHEME_V->args); 5019 SCHEME_V->args = a;
5101 5020
5102 if (op == OP_WRITE) 5021 if (op == OP_WRITE)
5103 SCHEME_V->print_flag = 1; 5022 SCHEME_V->print_flag = 1;
5104 else 5023 else
5105 SCHEME_V->print_flag = 0; 5024 SCHEME_V->print_flag = 0;
5106 5025
5107 s_goto (OP_P0LIST); 5026 s_goto (OP_P0LIST);
5108 5027
5028 //TODO: move to scheme
5109 case OP_NEWLINE: /* newline */ 5029 case OP_NEWLINE: /* newline */
5110 if (is_pair (SCHEME_V->args)) 5030 if (is_pair (args))
5111 { 5031 {
5112 if (car (SCHEME_V->args) != SCHEME_V->outport) 5032 if (a != SCHEME_V->outport)
5113 { 5033 {
5114 x = cons (SCHEME_V->outport, NIL); 5034 x = cons (SCHEME_V->outport, NIL);
5115 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 5035 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
5116 SCHEME_V->outport = car (SCHEME_V->args); 5036 SCHEME_V->outport = a;
5117 } 5037 }
5118 } 5038 }
5119 5039
5120 putstr (SCHEME_A_ "\n"); 5040 putcharacter (SCHEME_A_ '\n');
5121 s_return (S_T); 5041 s_return (S_T);
5122#endif 5042#endif
5123 5043
5124 case OP_ERR0: /* error */ 5044 case OP_ERR0: /* error */
5125 SCHEME_V->retcode = -1; 5045 SCHEME_V->retcode = -1;
5126 5046
5127 if (!is_string (car (SCHEME_V->args))) 5047 if (!is_string (a))
5128 { 5048 {
5129 SCHEME_V->args = cons (mk_string (SCHEME_A_ " -- "), SCHEME_V->args); 5049 args = cons (mk_string (SCHEME_A_ " -- "), args);
5130 setimmutable (car (SCHEME_V->args)); 5050 setimmutable (car (args));
5131 } 5051 }
5132 5052
5133 putstr (SCHEME_A_ "Error: "); 5053 putstr (SCHEME_A_ "Error: ");
5134 putstr (SCHEME_A_ strvalue (car (SCHEME_V->args))); 5054 putstr (SCHEME_A_ strvalue (car (args)));
5135 SCHEME_V->args = cdr (SCHEME_V->args); 5055 SCHEME_V->args = cdr (args);
5136 s_goto (OP_ERR1); 5056 s_goto (OP_ERR1);
5137 5057
5138 case OP_ERR1: /* error */ 5058 case OP_ERR1: /* error */
5139 putstr (SCHEME_A_ " "); 5059 putcharacter (SCHEME_A_ ' ');
5140 5060
5141 if (SCHEME_V->args != NIL) 5061 if (args != NIL)
5142 { 5062 {
5143 s_save (SCHEME_A_ OP_ERR1, cdr (SCHEME_V->args), NIL); 5063 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
5144 SCHEME_V->args = car (SCHEME_V->args); 5064 SCHEME_V->args = a;
5145 SCHEME_V->print_flag = 1; 5065 SCHEME_V->print_flag = 1;
5146 s_goto (OP_P0LIST); 5066 s_goto (OP_P0LIST);
5147 } 5067 }
5148 else 5068 else
5149 { 5069 {
5150 putstr (SCHEME_A_ "\n"); 5070 putcharacter (SCHEME_A_ '\n');
5151 5071
5152 if (SCHEME_V->interactive_repl) 5072 if (SCHEME_V->interactive_repl)
5153 {
5154 s_goto (OP_T0LVL); 5073 s_goto (OP_T0LVL);
5155 }
5156 else 5074 else
5157 {
5158 return NIL; 5075 return -1;
5159 }
5160 } 5076 }
5161 5077
5162 case OP_REVERSE: /* reverse */ 5078 case OP_REVERSE: /* reverse */
5163 s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); 5079 s_return (reverse (SCHEME_A_ a));
5164 5080
5165 case OP_LIST_STAR: /* list* */ 5081 case OP_LIST_STAR: /* list* */
5166 s_return (list_star (SCHEME_A_ SCHEME_V->args)); 5082 s_return (list_star (SCHEME_A_ SCHEME_V->args));
5167 5083
5168 case OP_APPEND: /* append */ 5084 case OP_APPEND: /* append */
5169 x = NIL; 5085 x = NIL;
5170 y = SCHEME_V->args; 5086 y = args;
5171 5087
5172 if (y == x) 5088 if (y == x)
5173 s_return (x); 5089 s_return (x);
5174 5090
5175 /* cdr() in the while condition is not a typo. If car() */ 5091 /* cdr() in the while condition is not a typo. If car() */
5186 s_return (reverse_in_place (SCHEME_A_ car (y), x)); 5102 s_return (reverse_in_place (SCHEME_A_ car (y), x));
5187 5103
5188#if USE_PLIST 5104#if USE_PLIST
5189 5105
5190 case OP_PUT: /* put */ 5106 case OP_PUT: /* put */
5191 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 5107 if (!hasprop (a) || !hasprop (cadr (args)))
5192 {
5193 Error_0 ("illegal use of put"); 5108 Error_0 ("illegal use of put");
5194 }
5195 5109
5196 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 5110 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
5197 { 5111 {
5198 if (caar (x) == y) 5112 if (caar (x) == y)
5199 {
5200 break; 5113 break;
5201 }
5202 } 5114 }
5203 5115
5204 if (x != NIL) 5116 if (x != NIL)
5205 cdar (x) = caddr (SCHEME_V->args); 5117 cdar (x) = caddr (args);
5206 else 5118 else
5207 symprop (car (SCHEME_V->args)) = cons (cons (y, caddr (SCHEME_V->args)), symprop (car (SCHEME_V->args))); 5119 symprop (a) = cons (cons (y, caddr (args)), symprop (a));
5208 5120
5209 s_return (S_T); 5121 s_return (S_T);
5210 5122
5211 case OP_GET: /* get */ 5123 case OP_GET: /* get */
5212 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 5124 if (!hasprop (a) || !hasprop (cadr (args)))
5213 Error_0 ("illegal use of get"); 5125 Error_0 ("illegal use of get");
5214 5126
5215 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 5127 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
5216 if (caar (x) == y) 5128 if (caar (x) == y)
5217 break; 5129 break;
5218 5130
5219 if (x != NIL) 5131 if (x != NIL)
5220 s_return (cdar (x)); 5132 s_return (cdar (x));
5222 s_return (NIL); 5134 s_return (NIL);
5223 5135
5224#endif /* USE_PLIST */ 5136#endif /* USE_PLIST */
5225 5137
5226 case OP_QUIT: /* quit */ 5138 case OP_QUIT: /* quit */
5227 if (is_pair (SCHEME_V->args)) 5139 if (is_pair (args))
5228 SCHEME_V->retcode = ivalue (car (SCHEME_V->args)); 5140 SCHEME_V->retcode = ivalue (a);
5229 5141
5230 return NIL; 5142 return -1;
5231 5143
5232 case OP_GC: /* gc */ 5144 case OP_GC: /* gc */
5233 gc (SCHEME_A_ NIL, NIL); 5145 gc (SCHEME_A_ NIL, NIL);
5234 s_return (S_T); 5146 s_return (S_T);
5235 5147
5236 case OP_GCVERB: /* gc-verbose */ 5148 case OP_GCVERB: /* gc-verbose */
5237 { 5149 {
5238 int was = SCHEME_V->gc_verbose; 5150 int was = SCHEME_V->gc_verbose;
5239 5151
5240 SCHEME_V->gc_verbose = (car (SCHEME_V->args) != S_F); 5152 SCHEME_V->gc_verbose = (a != S_F);
5241 s_retbool (was); 5153 s_retbool (was);
5242 } 5154 }
5243 5155
5244 case OP_NEWSEGMENT: /* new-segment */ 5156 case OP_NEWSEGMENT: /* new-segment */
5157#if 0
5245 if (!is_pair (SCHEME_V->args) || !is_number (car (SCHEME_V->args))) 5158 if (!is_pair (args) || !is_number (a))
5246 Error_0 ("new-segment: argument must be a number"); 5159 Error_0 ("new-segment: argument must be a number");
5247 5160#endif
5248 alloc_cellseg (SCHEME_A_ (int)ivalue (car (SCHEME_V->args))); 5161 s_retbool (alloc_cellseg (SCHEME_A));
5249
5250 s_return (S_T);
5251 5162
5252 case OP_OBLIST: /* oblist */ 5163 case OP_OBLIST: /* oblist */
5253 s_return (oblist_all_symbols (SCHEME_A)); 5164 s_return (oblist_all_symbols (SCHEME_A));
5254 5165
5255#if USE_PORTS 5166#if USE_PORTS
5280 case OP_OPEN_INOUTFILE: 5191 case OP_OPEN_INOUTFILE:
5281 prop = port_input | port_output; 5192 prop = port_input | port_output;
5282 break; 5193 break;
5283 } 5194 }
5284 5195
5285 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); 5196 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
5286 5197
5287 if (p == NIL) 5198 s_return (p == NIL ? S_F : p);
5288 {
5289 s_return (S_F);
5290 }
5291
5292 s_return (p);
5293 } 5199 }
5294 5200
5295# if USE_STRING_PORTS 5201# if USE_STRING_PORTS
5296 5202
5297 case OP_OPEN_INSTRING: /* open-input-string */ 5203 case OP_OPEN_INSTRING: /* open-input-string */
5309 case OP_OPEN_INOUTSTRING: 5215 case OP_OPEN_INOUTSTRING:
5310 prop = port_input | port_output; 5216 prop = port_input | port_output;
5311 break; 5217 break;
5312 } 5218 }
5313 5219
5314 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 5220 p = port_from_string (SCHEME_A_ strvalue (a),
5315 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); 5221 strvalue (a) + strlength (a), prop);
5316 5222
5317 if (p == NIL) 5223 s_return (p == NIL ? S_F : p);
5318 {
5319 s_return (S_F);
5320 }
5321
5322 s_return (p);
5323 } 5224 }
5324 5225
5325 case OP_OPEN_OUTSTRING: /* open-output-string */ 5226 case OP_OPEN_OUTSTRING: /* open-output-string */
5326 { 5227 {
5327 pointer p; 5228 pointer p;
5328 5229
5329 if (car (SCHEME_V->args) == NIL) 5230 if (a == NIL)
5330 {
5331 p = port_from_scratch (SCHEME_A); 5231 p = port_from_scratch (SCHEME_A);
5332
5333 if (p == NIL)
5334 {
5335 s_return (S_F);
5336 }
5337 }
5338 else 5232 else
5339 {
5340 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 5233 p = port_from_string (SCHEME_A_ strvalue (a),
5341 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); 5234 strvalue (a) + strlength (a), port_output);
5342 5235
5343 if (p == NIL) 5236 s_return (p == NIL ? S_F : p);
5344 {
5345 s_return (S_F);
5346 }
5347 }
5348
5349 s_return (p);
5350 } 5237 }
5351 5238
5352 case OP_GET_OUTSTRING: /* get-output-string */ 5239 case OP_GET_OUTSTRING: /* get-output-string */
5353 { 5240 {
5354 port *p; 5241 port *p = port (a);
5355 5242
5356 if ((p = car (SCHEME_V->args)->object.port)->kind & port_string) 5243 if (p->kind & port_string)
5357 { 5244 {
5358 off_t size; 5245 off_t size;
5359 char *str; 5246 char *str;
5360 5247
5361 size = p->rep.string.curr - p->rep.string.start + 1; 5248 size = p->rep.string.curr - p->rep.string.start + 1;
5377 } 5264 }
5378 5265
5379# endif 5266# endif
5380 5267
5381 case OP_CLOSE_INPORT: /* close-input-port */ 5268 case OP_CLOSE_INPORT: /* close-input-port */
5382 port_close (SCHEME_A_ car (SCHEME_V->args), port_input); 5269 port_close (SCHEME_A_ a, port_input);
5383 s_return (S_T); 5270 s_return (S_T);
5384 5271
5385 case OP_CLOSE_OUTPORT: /* close-output-port */ 5272 case OP_CLOSE_OUTPORT: /* close-output-port */
5386 port_close (SCHEME_A_ car (SCHEME_V->args), port_output); 5273 port_close (SCHEME_A_ a, port_output);
5387 s_return (S_T); 5274 s_return (S_T);
5388#endif 5275#endif
5389 5276
5390 case OP_INT_ENV: /* interaction-environment */ 5277 case OP_INT_ENV: /* interaction-environment */
5391 s_return (SCHEME_V->global_env); 5278 s_return (SCHEME_V->global_env);
5393 case OP_CURR_ENV: /* current-environment */ 5280 case OP_CURR_ENV: /* current-environment */
5394 s_return (SCHEME_V->envir); 5281 s_return (SCHEME_V->envir);
5395 5282
5396 } 5283 }
5397 5284
5398 return S_T; 5285 if (USE_ERROR_CHECKING) abort ();
5399} 5286}
5400 5287
5401static pointer 5288/* reading */
5289ecb_cold static int
5402opexe_5 (SCHEME_P_ enum scheme_opcodes op) 5290opexe_5 (SCHEME_P_ enum scheme_opcodes op)
5403{ 5291{
5292 pointer args = SCHEME_V->args;
5404 pointer x; 5293 pointer x;
5405 5294
5406 if (SCHEME_V->nesting != 0) 5295 if (SCHEME_V->nesting != 0)
5407 { 5296 {
5408 int n = SCHEME_V->nesting; 5297 int n = SCHEME_V->nesting;
5415 switch (op) 5304 switch (op)
5416 { 5305 {
5417 /* ========== reading part ========== */ 5306 /* ========== reading part ========== */
5418#if USE_PORTS 5307#if USE_PORTS
5419 case OP_READ: 5308 case OP_READ:
5420 if (!is_pair (SCHEME_V->args)) 5309 if (!is_pair (args))
5421 {
5422 s_goto (OP_READ_INTERNAL); 5310 s_goto (OP_READ_INTERNAL);
5423 }
5424 5311
5425 if (!is_inport (car (SCHEME_V->args))) 5312 if (!is_inport (car (args)))
5426 {
5427 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 5313 Error_1 ("read: not an input port:", car (args));
5428 }
5429 5314
5430 if (car (SCHEME_V->args) == SCHEME_V->inport) 5315 if (car (args) == SCHEME_V->inport)
5431 {
5432 s_goto (OP_READ_INTERNAL); 5316 s_goto (OP_READ_INTERNAL);
5433 }
5434 5317
5435 x = SCHEME_V->inport; 5318 x = SCHEME_V->inport;
5436 SCHEME_V->inport = car (SCHEME_V->args); 5319 SCHEME_V->inport = car (args);
5437 x = cons (x, NIL); 5320 x = cons (x, NIL);
5438 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 5321 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5439 s_goto (OP_READ_INTERNAL); 5322 s_goto (OP_READ_INTERNAL);
5440 5323
5441 case OP_READ_CHAR: /* read-char */ 5324 case OP_READ_CHAR: /* read-char */
5442 case OP_PEEK_CHAR: /* peek-char */ 5325 case OP_PEEK_CHAR: /* peek-char */
5443 { 5326 {
5444 int c; 5327 int c;
5445 5328
5446 if (is_pair (SCHEME_V->args)) 5329 if (is_pair (args))
5447 { 5330 {
5448 if (car (SCHEME_V->args) != SCHEME_V->inport) 5331 if (car (args) != SCHEME_V->inport)
5449 { 5332 {
5450 x = SCHEME_V->inport; 5333 x = SCHEME_V->inport;
5451 x = cons (x, NIL); 5334 x = cons (x, NIL);
5452 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 5335 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5453 SCHEME_V->inport = car (SCHEME_V->args); 5336 SCHEME_V->inport = car (args);
5454 } 5337 }
5455 } 5338 }
5456 5339
5457 c = inchar (SCHEME_A); 5340 c = inchar (SCHEME_A);
5458 5341
5468 case OP_CHAR_READY: /* char-ready? */ 5351 case OP_CHAR_READY: /* char-ready? */
5469 { 5352 {
5470 pointer p = SCHEME_V->inport; 5353 pointer p = SCHEME_V->inport;
5471 int res; 5354 int res;
5472 5355
5473 if (is_pair (SCHEME_V->args)) 5356 if (is_pair (args))
5474 {
5475 p = car (SCHEME_V->args); 5357 p = car (args);
5476 }
5477 5358
5478 res = p->object.port->kind & port_string; 5359 res = port (p)->kind & port_string;
5479 5360
5480 s_retbool (res); 5361 s_retbool (res);
5481 } 5362 }
5482 5363
5483 case OP_SET_INPORT: /* set-input-port */ 5364 case OP_SET_INPORT: /* set-input-port */
5484 SCHEME_V->inport = car (SCHEME_V->args); 5365 SCHEME_V->inport = car (args);
5485 s_return (SCHEME_V->value); 5366 s_return (SCHEME_V->value);
5486 5367
5487 case OP_SET_OUTPORT: /* set-output-port */ 5368 case OP_SET_OUTPORT: /* set-output-port */
5488 SCHEME_V->outport = car (SCHEME_V->args); 5369 SCHEME_V->outport = car (args);
5489 s_return (SCHEME_V->value); 5370 s_return (SCHEME_V->value);
5490#endif 5371#endif
5491 5372
5492 case OP_RDSEXPR: 5373 case OP_RDSEXPR:
5493 switch (SCHEME_V->tok) 5374 switch (SCHEME_V->tok)
5494 { 5375 {
5495 case TOK_EOF: 5376 case TOK_EOF:
5496 s_return (S_EOF); 5377 s_return (S_EOF);
5497 /* NOTREACHED */
5498 5378
5499 case TOK_VEC: 5379 case TOK_VEC:
5500 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); 5380 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL);
5501
5502 /* fall through */ 5381 /* fall through */
5382
5503 case TOK_LPAREN: 5383 case TOK_LPAREN:
5504 SCHEME_V->tok = token (SCHEME_A); 5384 SCHEME_V->tok = token (SCHEME_A);
5505 5385
5506 if (SCHEME_V->tok == TOK_RPAREN) 5386 if (SCHEME_V->tok == TOK_RPAREN)
5507 s_return (NIL); 5387 s_return (NIL);
5508 else if (SCHEME_V->tok == TOK_DOT) 5388 else if (SCHEME_V->tok == TOK_DOT)
5509 Error_0 ("syntax error: illegal dot expression"); 5389 Error_0 ("syntax error: illegal dot expression");
5510 else 5390
5511 {
5512 SCHEME_V->nesting_stack[SCHEME_V->file_i]++; 5391 SCHEME_V->nesting_stack[SCHEME_V->file_i]++;
5513 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); 5392 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL);
5514 s_goto (OP_RDSEXPR); 5393 s_goto (OP_RDSEXPR);
5515 }
5516 5394
5517 case TOK_QUOTE: 5395 case TOK_QUOTE:
5518 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); 5396 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL);
5519 SCHEME_V->tok = token (SCHEME_A); 5397 SCHEME_V->tok = token (SCHEME_A);
5520 s_goto (OP_RDSEXPR); 5398 s_goto (OP_RDSEXPR);
5526 { 5404 {
5527 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); 5405 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL);
5528 SCHEME_V->tok = TOK_LPAREN; 5406 SCHEME_V->tok = TOK_LPAREN;
5529 s_goto (OP_RDSEXPR); 5407 s_goto (OP_RDSEXPR);
5530 } 5408 }
5531 else 5409
5532 {
5533 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); 5410 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL);
5534 }
5535
5536 s_goto (OP_RDSEXPR); 5411 s_goto (OP_RDSEXPR);
5537 5412
5538 case TOK_COMMA: 5413 case TOK_COMMA:
5539 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); 5414 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL);
5540 SCHEME_V->tok = token (SCHEME_A); 5415 SCHEME_V->tok = token (SCHEME_A);
5544 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 5419 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5545 SCHEME_V->tok = token (SCHEME_A); 5420 SCHEME_V->tok = token (SCHEME_A);
5546 s_goto (OP_RDSEXPR); 5421 s_goto (OP_RDSEXPR);
5547 5422
5548 case TOK_ATOM: 5423 case TOK_ATOM:
5549 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 5424 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
5425
5426 case TOK_DOTATOM:
5427 SCHEME_V->strbuff[0] = '.';
5428 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5429
5430 case TOK_STRATOM:
5431 //TODO: haven't checked whether the garbage collector could interfere and free x
5432 gc (SCHEME_A_ NIL, NIL); //TODO: superheavyhanded
5433 x = readstrexp (SCHEME_A_ '|');
5434 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5550 5435
5551 case TOK_DQUOTE: 5436 case TOK_DQUOTE:
5552 x = readstrexp (SCHEME_A); 5437 x = readstrexp (SCHEME_A_ '"');
5553 5438
5554 if (x == S_F) 5439 if (x == S_F)
5555 Error_0 ("Error reading string"); 5440 Error_0 ("Error reading string");
5556 5441
5557 setimmutable (x); 5442 setimmutable (x);
5561 { 5446 {
5562 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1); 5447 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1);
5563 5448
5564 if (f == NIL) 5449 if (f == NIL)
5565 Error_0 ("undefined sharp expression"); 5450 Error_0 ("undefined sharp expression");
5566 else 5451
5567 {
5568 SCHEME_V->code = cons (slot_value_in_env (f), NIL); 5452 SCHEME_V->code = cons (slot_value_in_env (f), NIL);
5569 s_goto (OP_EVAL); 5453 s_goto (OP_EVAL);
5570 }
5571 } 5454 }
5572 5455
5573 case TOK_SHARP_CONST: 5456 case TOK_SHARP_CONST:
5574 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 5457 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5575 Error_0 ("undefined sharp expression"); 5458 Error_0 ("undefined sharp expression");
5576 else 5459
5577 s_return (x); 5460 s_return (x);
5578 5461
5579 default: 5462 default:
5580 Error_0 ("syntax error: illegal token"); 5463 Error_0 ("syntax error: illegal token");
5581 } 5464 }
5582 5465
5583 break; 5466 break;
5584 5467
5585 case OP_RDLIST: 5468 case OP_RDLIST:
5586 {
5587 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5469 SCHEME_V->args = cons (SCHEME_V->value, args);
5588 SCHEME_V->tok = token (SCHEME_A); 5470 SCHEME_V->tok = token (SCHEME_A);
5589 5471
5590 if (SCHEME_V->tok == TOK_EOF) 5472 switch (SCHEME_V->tok)
5473 {
5474 case TOK_EOF:
5591 s_return (S_EOF); 5475 s_return (S_EOF);
5592 else if (SCHEME_V->tok == TOK_RPAREN) 5476
5477 case TOK_RPAREN:
5593 { 5478 {
5594 int c = inchar (SCHEME_A); 5479 int c = inchar (SCHEME_A);
5595 5480
5596 if (c != '\n') 5481 if (c != '\n')
5597 backchar (SCHEME_A_ c); 5482 backchar (SCHEME_A_ c);
5598
5599#if SHOW_ERROR_LINE 5483#if SHOW_ERROR_LINE
5600 else if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 5484 else if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file)
5601 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line++; 5485 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line++;
5602
5603#endif 5486#endif
5487
5604 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5488 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5605 s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args)); 5489 s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args));
5606 } 5490 }
5607 else if (SCHEME_V->tok == TOK_DOT) 5491
5608 { 5492 case TOK_DOT:
5609 s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL); 5493 s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL);
5610 SCHEME_V->tok = token (SCHEME_A); 5494 SCHEME_V->tok = token (SCHEME_A);
5611 s_goto (OP_RDSEXPR); 5495 s_goto (OP_RDSEXPR);
5612 } 5496
5613 else 5497 default:
5614 {
5615 s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL);; 5498 s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL);
5616 s_goto (OP_RDSEXPR); 5499 s_goto (OP_RDSEXPR);
5617 } 5500 }
5618 }
5619 5501
5620 case OP_RDDOT: 5502 case OP_RDDOT:
5621 if (token (SCHEME_A) != TOK_RPAREN) 5503 if (token (SCHEME_A) != TOK_RPAREN)
5622 Error_0 ("syntax error: illegal dot expression"); 5504 Error_0 ("syntax error: illegal dot expression");
5623 else 5505
5624 {
5625 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5506 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5626 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5507 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args));
5627 }
5628 5508
5629 case OP_RDQUOTE: 5509 case OP_RDQUOTE:
5630 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5510 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5631 5511
5632 case OP_RDQQUOTE: 5512 case OP_RDQQUOTE:
5654 SCHEME_V->args = SCHEME_V->value; 5534 SCHEME_V->args = SCHEME_V->value;
5655 s_goto (OP_VECTOR); 5535 s_goto (OP_VECTOR);
5656 5536
5657 /* ========== printing part ========== */ 5537 /* ========== printing part ========== */
5658 case OP_P0LIST: 5538 case OP_P0LIST:
5659 if (is_vector (SCHEME_V->args)) 5539 if (is_vector (args))
5660 { 5540 {
5661 putstr (SCHEME_A_ "#("); 5541 putstr (SCHEME_A_ "#(");
5662 SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); 5542 SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0));
5663 s_goto (OP_PVECFROM); 5543 s_goto (OP_PVECFROM);
5664 } 5544 }
5665 else if (is_environment (SCHEME_V->args)) 5545 else if (is_environment (args))
5666 { 5546 {
5667 putstr (SCHEME_A_ "#<ENVIRONMENT>"); 5547 putstr (SCHEME_A_ "#<ENVIRONMENT>");
5668 s_return (S_T); 5548 s_return (S_T);
5669 } 5549 }
5670 else if (!is_pair (SCHEME_V->args)) 5550 else if (!is_pair (args))
5671 { 5551 {
5672 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5552 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5673 s_return (S_T); 5553 s_return (S_T);
5674 }
5675 else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args)))
5676 {
5677 putstr (SCHEME_A_ "'");
5678 SCHEME_V->args = cadr (SCHEME_V->args);
5679 s_goto (OP_P0LIST);
5680 }
5681 else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args)))
5682 {
5683 putstr (SCHEME_A_ "`");
5684 SCHEME_V->args = cadr (SCHEME_V->args);
5685 s_goto (OP_P0LIST);
5686 }
5687 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTE && ok_abbrev (cdr (SCHEME_V->args)))
5688 {
5689 putstr (SCHEME_A_ ",");
5690 SCHEME_V->args = cadr (SCHEME_V->args);
5691 s_goto (OP_P0LIST);
5692 }
5693 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTESP && ok_abbrev (cdr (SCHEME_V->args)))
5694 {
5695 putstr (SCHEME_A_ ",@");
5696 SCHEME_V->args = cadr (SCHEME_V->args);
5697 s_goto (OP_P0LIST);
5698 } 5554 }
5699 else 5555 else
5700 { 5556 {
5557 pointer a = car (args);
5558 pointer b = cdr (args);
5559 int ok_abbr = ok_abbrev (b);
5560 SCHEME_V->args = car (b);
5561
5562 if (a == SCHEME_V->QUOTE && ok_abbr)
5563 putcharacter (SCHEME_A_ '\'');
5564 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5565 putcharacter (SCHEME_A_ '`');
5566 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5567 putcharacter (SCHEME_A_ ',');
5568 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5701 putstr (SCHEME_A_ "("); 5569 putstr (SCHEME_A_ ",@");
5570 else
5571 {
5572 putcharacter (SCHEME_A_ '(');
5702 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL); 5573 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5703 SCHEME_V->args = car (SCHEME_V->args); 5574 SCHEME_V->args = a;
5575 }
5576
5704 s_goto (OP_P0LIST); 5577 s_goto (OP_P0LIST);
5705 } 5578 }
5706 5579
5707 case OP_P1LIST: 5580 case OP_P1LIST:
5708 if (is_pair (SCHEME_V->args)) 5581 if (is_pair (args))
5709 { 5582 {
5710 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL); 5583 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5711 putstr (SCHEME_A_ " "); 5584 putcharacter (SCHEME_A_ ' ');
5712 SCHEME_V->args = car (SCHEME_V->args); 5585 SCHEME_V->args = car (args);
5713 s_goto (OP_P0LIST); 5586 s_goto (OP_P0LIST);
5714 } 5587 }
5715 else if (is_vector (SCHEME_V->args)) 5588 else if (is_vector (args))
5716 { 5589 {
5717 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); 5590 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL);
5718 putstr (SCHEME_A_ " . "); 5591 putstr (SCHEME_A_ " . ");
5719 s_goto (OP_P0LIST); 5592 s_goto (OP_P0LIST);
5720 } 5593 }
5721 else 5594 else
5722 { 5595 {
5723 if (SCHEME_V->args != NIL) 5596 if (args != NIL)
5724 { 5597 {
5725 putstr (SCHEME_A_ " . "); 5598 putstr (SCHEME_A_ " . ");
5726 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5599 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5727 } 5600 }
5728 5601
5729 putstr (SCHEME_A_ ")"); 5602 putcharacter (SCHEME_A_ ')');
5730 s_return (S_T); 5603 s_return (S_T);
5731 } 5604 }
5732 5605
5733 case OP_PVECFROM: 5606 case OP_PVECFROM:
5734 { 5607 {
5735 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5608 IVALUE i = ivalue_unchecked (cdr (args));
5736 pointer vec = car (SCHEME_V->args); 5609 pointer vec = car (args);
5737 int len = ivalue_unchecked (vec); 5610 uint32_t len = veclength (vec);
5738 5611
5739 if (i == len) 5612 if (i == len)
5740 { 5613 {
5741 putstr (SCHEME_A_ ")"); 5614 putcharacter (SCHEME_A_ ')');
5742 s_return (S_T); 5615 s_return (S_T);
5743 } 5616 }
5744 else 5617 else
5745 { 5618 {
5746 pointer elem = vector_elem (vec, i); 5619 pointer elem = vector_get (vec, i);
5747 5620
5748 ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; 5621 set_cdr (args, mk_integer (SCHEME_A_ i + 1));
5749 s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); 5622 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5750 SCHEME_V->args = elem; 5623 SCHEME_V->args = elem;
5751 5624
5752 if (i > 0) 5625 if (i > 0)
5753 putstr (SCHEME_A_ " "); 5626 putcharacter (SCHEME_A_ ' ');
5754 5627
5755 s_goto (OP_P0LIST); 5628 s_goto (OP_P0LIST);
5756 } 5629 }
5757 } 5630 }
5758 } 5631 }
5759 5632
5760 return S_T; 5633 if (USE_ERROR_CHECKING) abort ();
5761} 5634}
5762 5635
5763static pointer 5636/* list ops */
5637ecb_hot static int
5764opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5638opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5765{ 5639{
5640 pointer args = SCHEME_V->args;
5641 pointer a = car (args);
5766 pointer x, y; 5642 pointer x, y;
5767 5643
5768 switch (op) 5644 switch (op)
5769 { 5645 {
5770 case OP_LIST_LENGTH: /* length *//* a.k */ 5646 case OP_LIST_LENGTH: /* length *//* a.k */
5771 { 5647 {
5772 long v = list_length (SCHEME_A_ car (SCHEME_V->args)); 5648 long v = list_length (SCHEME_A_ a);
5773 5649
5774 if (v < 0) 5650 if (v < 0)
5775 Error_1 ("length: not a list:", car (SCHEME_V->args)); 5651 Error_1 ("length: not a list:", a);
5776 5652
5777 s_return (mk_integer (SCHEME_A_ v)); 5653 s_return (mk_integer (SCHEME_A_ v));
5778 } 5654 }
5779 5655
5780 case OP_ASSQ: /* assq *//* a.k */ 5656 case OP_ASSQ: /* assq *//* a.k */
5781 x = car (SCHEME_V->args); 5657 x = a;
5782 5658
5783 for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) 5659 for (y = cadr (args); is_pair (y); y = cdr (y))
5784 { 5660 {
5785 if (!is_pair (car (y))) 5661 if (!is_pair (car (y)))
5786 Error_0 ("unable to handle non pair element"); 5662 Error_0 ("unable to handle non pair element");
5787 5663
5788 if (x == caar (y)) 5664 if (x == caar (y))
5789 break; 5665 break;
5790 } 5666 }
5791 5667
5792 if (is_pair (y)) 5668 if (is_pair (y))
5793 s_return (car (y)); 5669 s_return (car (y));
5794 else 5670
5795 s_return (S_F); 5671 s_return (S_F);
5796
5797 5672
5798 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5673 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5799 SCHEME_V->args = car (SCHEME_V->args); 5674 SCHEME_V->args = a;
5800 5675
5801 if (SCHEME_V->args == NIL) 5676 if (SCHEME_V->args == NIL)
5802 s_return (S_F); 5677 s_return (S_F);
5803 else if (is_closure (SCHEME_V->args)) 5678 else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args))
5804 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5679 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5805 else if (is_macro (SCHEME_V->args)) 5680
5806 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5807 else
5808 s_return (S_F); 5681 s_return (S_F);
5809 5682
5810 case OP_CLOSUREP: /* closure? */ 5683 case OP_CLOSUREP: /* closure? */
5811 /* 5684 /*
5812 * Note, macro object is also a closure. 5685 * Note, macro object is also a closure.
5813 * Therefore, (closure? <#MACRO>) ==> #t 5686 * Therefore, (closure? <#MACRO>) ==> #t
5687 * (schmorp) well, obviously not, fix? TODO
5814 */ 5688 */
5815 s_retbool (is_closure (car (SCHEME_V->args))); 5689 s_retbool (is_closure (a));
5816 5690
5817 case OP_MACROP: /* macro? */ 5691 case OP_MACROP: /* macro? */
5818 s_retbool (is_macro (car (SCHEME_V->args))); 5692 s_retbool (is_macro (a));
5819 } 5693 }
5820 5694
5821 return S_T; /* NOTREACHED */ 5695 if (USE_ERROR_CHECKING) abort ();
5822} 5696}
5823 5697
5698/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5824typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); 5699typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5825 5700
5826typedef int (*test_predicate) (pointer); 5701typedef int (*test_predicate)(pointer);
5827static int 5702
5703ecb_hot static int
5828is_any (pointer p) 5704tst_any (pointer p)
5829{ 5705{
5830 return 1; 5706 return 1;
5831} 5707}
5832 5708
5833static int 5709ecb_hot static int
5834is_nonneg (pointer p) 5710tst_inonneg (pointer p)
5835{ 5711{
5836 return ivalue (p) >= 0 && is_integer (p); 5712 return is_integer (p) && ivalue_unchecked (p) >= 0;
5713}
5714
5715ecb_hot static int
5716tst_is_list (SCHEME_P_ pointer p)
5717{
5718 return p == NIL || is_pair (p);
5837} 5719}
5838 5720
5839/* Correspond carefully with following defines! */ 5721/* Correspond carefully with following defines! */
5840static struct 5722static struct
5841{ 5723{
5842 test_predicate fct; 5724 test_predicate fct;
5843 const char *kind; 5725 const char *kind;
5844} tests[] = 5726} tests[] = {
5845{ 5727 { tst_any , 0 },
5846 { 0, 0}, /* unused */ 5728 { is_string , "string" },
5847 { is_any, 0}, 5729 { is_symbol , "symbol" },
5848 { is_string, "string" }, 5730 { is_port , "port" },
5849 { is_symbol, "symbol" },
5850 { is_port, "port" },
5851 { is_inport, "input port" }, 5731 { is_inport , "input port" },
5852 { is_outport, "output port" }, 5732 { is_outport , "output port" },
5853 { is_environment, "environment" }, 5733 { is_environment, "environment" },
5854 { is_pair, "pair" }, 5734 { is_pair , "pair" },
5855 { 0, "pair or '()" }, 5735 { 0 , "pair or '()" },
5856 { is_character, "character" }, 5736 { is_character , "character" },
5857 { is_vector, "vector" }, 5737 { is_vector , "vector" },
5858 { is_number, "number" }, 5738 { is_number , "number" },
5859 { is_integer, "integer" }, 5739 { is_integer , "integer" },
5860 { is_nonneg, "non-negative integer" } 5740 { tst_inonneg , "non-negative integer" }
5861}; 5741};
5862 5742
5863#define TST_NONE 0 5743#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5864#define TST_ANY "\001" 5744#define TST_ANY "\001"
5865#define TST_STRING "\002" 5745#define TST_STRING "\002"
5866#define TST_SYMBOL "\003" 5746#define TST_SYMBOL "\003"
5867#define TST_PORT "\004" 5747#define TST_PORT "\004"
5868#define TST_INPORT "\005" 5748#define TST_INPORT "\005"
5869#define TST_OUTPORT "\006" 5749#define TST_OUTPORT "\006"
5870#define TST_ENVIRONMENT "\007" 5750#define TST_ENVIRONMENT "\007"
5871#define TST_PAIR "\010" 5751#define TST_PAIR "\010"
5872#define TST_LIST "\011" 5752#define TST_LIST "\011"
5873#define TST_CHAR "\012" 5753#define TST_CHAR "\012"
5874#define TST_VECTOR "\013" 5754#define TST_VECTOR "\013"
5875#define TST_NUMBER "\014" 5755#define TST_NUMBER "\014"
5876#define TST_INTEGER "\015" 5756#define TST_INTEGER "\015"
5877#define TST_NATURAL "\016" 5757#define TST_NATURAL "\016"
5758
5759#define INF_ARG 0xff
5760#define UNNAMED_OP ""
5761
5762static const char opnames[] =
5763#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5764#include "opdefines.h"
5765#undef OP_DEF
5766;
5767
5768ecb_cold static const char *
5769opname (int idx)
5770{
5771 const char *name = opnames;
5772
5773 /* should do this at compile time, but would require external program, right? */
5774 while (idx--)
5775 name += strlen (name) + 1;
5776
5777 return *name ? name : "ILLEGAL";
5778}
5779
5780ecb_cold static const char *
5781procname (pointer x)
5782{
5783 return opname (procnum (x));
5784}
5878 5785
5879typedef struct 5786typedef struct
5880{ 5787{
5881 dispatch_func func; 5788 uint8_t func;
5882 char *name; 5789 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5790 uint8_t builtin;
5791#if USE_ERROR_CHECKING
5883 int min_arity; 5792 uint8_t min_arity;
5884 int max_arity; 5793 uint8_t max_arity;
5885 char *arg_tests_encoding; 5794 char arg_tests_encoding[3];
5795#endif
5886} op_code_info; 5796} op_code_info;
5887 5797
5888#define INF_ARG 0xffff
5889
5890static op_code_info dispatch_table[] = { 5798static const op_code_info dispatch_table[] = {
5891#define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, 5799#if USE_ERROR_CHECKING
5800#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5801#else
5802#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5803#endif
5892#include "opdefines.h" 5804#include "opdefines.h"
5805#undef OP_DEF
5893 {0} 5806 {0}
5894}; 5807};
5895 5808
5896static const char *
5897procname (pointer x)
5898{
5899 int n = procnum (x);
5900 const char *name = dispatch_table[n].name;
5901
5902 if (name == 0)
5903 {
5904 name = "ILLEGAL!";
5905 }
5906
5907 return name;
5908}
5909
5910/* kernel of this interpreter */ 5809/* kernel of this interpreter */
5911static void 5810ecb_hot static void
5912Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5811Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5913{ 5812{
5914 SCHEME_V->op = op; 5813 SCHEME_V->op = op;
5915 5814
5916 for (;;) 5815 for (;;)
5917 { 5816 {
5918 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5817 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5919 5818
5819#if USE_ERROR_CHECKING
5920 if (pcd->name != 0) /* if built-in function, check arguments */ 5820 if (pcd->builtin) /* if built-in function, check arguments */
5921 { 5821 {
5922 char msg[STRBUFFSIZE]; 5822 char msg[STRBUFFSIZE];
5923 int ok = 1;
5924 int n = list_length (SCHEME_A_ SCHEME_V->args); 5823 int n = list_length (SCHEME_A_ SCHEME_V->args);
5925 5824
5926#if USE_ERROR_CHECKING
5927 /* Check number of arguments */ 5825 /* Check number of arguments */
5928 if (n < pcd->min_arity) 5826 if (ecb_expect_false (n < pcd->min_arity))
5929 { 5827 {
5930 ok = 0;
5931 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5828 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5932 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5829 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5830 xError_1 (SCHEME_A_ msg, 0);
5831 continue;
5933 } 5832 }
5934 5833 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5935 if (ok && n > pcd->max_arity)
5936 { 5834 {
5937 ok = 0;
5938 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5835 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5939 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5836 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5837 xError_1 (SCHEME_A_ msg, 0);
5838 continue;
5940 } 5839 }
5941#endif 5840 else
5942
5943 if (ok)
5944 { 5841 {
5945 if (pcd->arg_tests_encoding != 0) 5842 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5946 { 5843 {
5947 int i = 0; 5844 int i = 0;
5948 int j; 5845 int j;
5949 const char *t = pcd->arg_tests_encoding; 5846 const char *t = pcd->arg_tests_encoding;
5950 pointer arglist = SCHEME_V->args; 5847 pointer arglist = SCHEME_V->args;
5951 5848
5952 do 5849 do
5953 { 5850 {
5954 pointer arg = car (arglist); 5851 pointer arg = car (arglist);
5955 5852
5956 j = (int) t[0]; 5853 j = t[0];
5957 5854
5855 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5958 if (j == TST_LIST[0]) 5856 if (j == TST_LIST[0])
5959 { 5857 {
5960 if (arg != NIL && !is_pair (arg)) 5858 if (!tst_is_list (SCHEME_A_ arg))
5961 break; 5859 break;
5962 } 5860 }
5963 else 5861 else
5964 { 5862 {
5965 if (!tests[j].fct (arg)) 5863 if (!tests[j - 1].fct (arg))
5966 break; 5864 break;
5967 } 5865 }
5968 5866
5969 if (t[1] != 0) /* last test is replicated as necessary */ 5867 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5970 {
5971 t++; 5868 t++;
5972 }
5973 5869
5974 arglist = cdr (arglist); 5870 arglist = cdr (arglist);
5975 i++; 5871 i++;
5976 } 5872 }
5977 while (i < n); 5873 while (i < n);
5978 5874
5979#if USE_ERROR_CHECKING
5980 if (i < n) 5875 if (i < n)
5981 { 5876 {
5982 ok = 0;
5983 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5877 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind);
5878 xError_1 (SCHEME_A_ msg, 0);
5879 continue;
5984 } 5880 }
5985#endif
5986 } 5881 }
5987 } 5882 }
5988
5989 if (!ok)
5990 {
5991 if (xError_1 (SCHEME_A_ msg, 0) == NIL)
5992 return;
5993
5994 pcd = dispatch_table + SCHEME_V->op;
5995 }
5996 } 5883 }
5884#endif
5997 5885
5998 ok_to_freely_gc (SCHEME_A); 5886 ok_to_freely_gc (SCHEME_A);
5999 5887
6000 if (pcd->func (SCHEME_A_ (enum scheme_opcodes) SCHEME_V->op) == NIL) 5888 static const dispatch_func dispatch_funcs[] = {
5889 opexe_0,
5890 opexe_1,
5891 opexe_2,
5892 opexe_3,
5893 opexe_4,
5894 opexe_5,
5895 opexe_6,
5896 };
5897
5898 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
6001 return; 5899 return;
6002 5900
6003#if USE_ERROR_CHECKING 5901 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
6004 if (SCHEME_V->no_memory)
6005 { 5902 {
6006 xwrstr ("No memory!\n"); 5903 putstr (SCHEME_A_ "No memory!\n");
6007 return; 5904 return;
6008 } 5905 }
6009#endif
6010 } 5906 }
6011} 5907}
6012 5908
6013/* ========== Initialization of internal keywords ========== */ 5909/* ========== Initialization of internal keywords ========== */
6014 5910
6015static void 5911ecb_cold static void
6016assign_syntax (SCHEME_P_ char *name) 5912assign_syntax (SCHEME_P_ const char *name)
6017{ 5913{
6018 pointer x = oblist_add_by_name (SCHEME_A_ name); 5914 pointer x = oblist_add_by_name (SCHEME_A_ name);
6019 set_typeflag (x, typeflag (x) | T_SYNTAX); 5915 set_typeflag (x, typeflag (x) | T_SYNTAX);
6020} 5916}
6021 5917
6022static void 5918ecb_cold static void
6023assign_proc (SCHEME_P_ enum scheme_opcodes op, char *name) 5919assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
6024{ 5920{
6025 pointer x = mk_symbol (SCHEME_A_ name); 5921 pointer x = mk_symbol (SCHEME_A_ name);
6026 pointer y = mk_proc (SCHEME_A_ op); 5922 pointer y = mk_proc (SCHEME_A_ op);
6027 new_slot_in_env (SCHEME_A_ x, y); 5923 new_slot_in_env (SCHEME_A_ x, y);
6028} 5924}
6030static pointer 5926static pointer
6031mk_proc (SCHEME_P_ enum scheme_opcodes op) 5927mk_proc (SCHEME_P_ enum scheme_opcodes op)
6032{ 5928{
6033 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5929 pointer y = get_cell (SCHEME_A_ NIL, NIL);
6034 set_typeflag (y, (T_PROC | T_ATOM)); 5930 set_typeflag (y, (T_PROC | T_ATOM));
6035 ivalue_unchecked (y) = (long) op; 5931 set_ivalue (y, op);
6036 set_num_integer (y);
6037 return y; 5932 return y;
6038} 5933}
6039 5934
6040/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5935/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
6041static int 5936ecb_hot static int
6042syntaxnum (pointer p) 5937syntaxnum (pointer p)
6043{ 5938{
6044 const char *s = strvalue (car (p)); 5939 const char *s = strvalue (p);
6045 5940
6046 switch (strlength (car (p))) 5941 switch (strlength (p))
6047 { 5942 {
6048 case 2: 5943 case 2:
6049 if (s[0] == 'i') 5944 if (s[0] == 'i')
6050 return OP_IF0; /* if */ 5945 return OP_IF0; /* if */
6051 else 5946 else
6065 5960
6066 case 'd': 5961 case 'd':
6067 return OP_COND0; /* cond */ 5962 return OP_COND0; /* cond */
6068 5963
6069 case '*': 5964 case '*':
6070 return OP_LET0AST; /* let* */ 5965 return OP_LET0AST;/* let* */
6071 5966
6072 default: 5967 default:
6073 return OP_SET0; /* set! */ 5968 return OP_SET0; /* set! */
6074 } 5969 }
6075 5970
6097 5992
6098 case 'f': 5993 case 'f':
6099 return OP_DEF0; /* define */ 5994 return OP_DEF0; /* define */
6100 5995
6101 default: 5996 default:
6102 return OP_LET0REC; /* letrec */ 5997 return OP_LET0REC;/* letrec */
6103 } 5998 }
6104 5999
6105 default: 6000 default:
6106 return OP_C0STREAM; /* cons-stream */ 6001 return OP_C0STREAM; /* cons-stream */
6107 } 6002 }
6108} 6003}
6109 6004
6110#if USE_MULTIPLICITY 6005#if USE_MULTIPLICITY
6111scheme * 6006ecb_cold scheme *
6112scheme_init_new () 6007scheme_init_new ()
6113{ 6008{
6114 scheme *sc = malloc (sizeof (scheme)); 6009 scheme *sc = malloc (sizeof (scheme));
6115 6010
6116 if (!scheme_init (SCHEME_A)) 6011 if (!scheme_init (SCHEME_A))
6121 else 6016 else
6122 return sc; 6017 return sc;
6123} 6018}
6124#endif 6019#endif
6125 6020
6126int 6021ecb_cold int
6127scheme_init (SCHEME_P) 6022scheme_init (SCHEME_P)
6128{ 6023{
6129 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 6024 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
6130 pointer x; 6025
6026 /* this memset is not strictly correct, as we assume (intcache)
6027 * that memset 0 will also set pointers to 0, but memset does
6028 * of course not guarantee that. screw such systems.
6029 */
6030 memset (SCHEME_V, 0, sizeof (*SCHEME_V));
6131 6031
6132 num_set_fixnum (num_zero, 1); 6032 num_set_fixnum (num_zero, 1);
6133 num_set_ivalue (num_zero, 0); 6033 num_set_ivalue (num_zero, 0);
6134 num_set_fixnum (num_one, 1); 6034 num_set_fixnum (num_one, 1);
6135 num_set_ivalue (num_one, 1); 6035 num_set_ivalue (num_one, 1);
6147 SCHEME_V->save_inport = NIL; 6047 SCHEME_V->save_inport = NIL;
6148 SCHEME_V->loadport = NIL; 6048 SCHEME_V->loadport = NIL;
6149 SCHEME_V->nesting = 0; 6049 SCHEME_V->nesting = 0;
6150 SCHEME_V->interactive_repl = 0; 6050 SCHEME_V->interactive_repl = 0;
6151 6051
6152 if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) 6052 if (!alloc_cellseg (SCHEME_A))
6153 { 6053 {
6154#if USE_ERROR_CHECKING 6054#if USE_ERROR_CHECKING
6155 SCHEME_V->no_memory = 1; 6055 SCHEME_V->no_memory = 1;
6156 return 0; 6056 return 0;
6157#endif 6057#endif
6158 } 6058 }
6159 6059
6160 SCHEME_V->gc_verbose = 0; 6060 SCHEME_V->gc_verbose = 0;
6161 dump_stack_initialize (SCHEME_A); 6061 dump_stack_initialize (SCHEME_A);
6162 SCHEME_V->code = NIL; 6062 SCHEME_V->code = NIL;
6063 SCHEME_V->args = NIL;
6064 SCHEME_V->envir = NIL;
6065 SCHEME_V->value = NIL;
6163 SCHEME_V->tracing = 0; 6066 SCHEME_V->tracing = 0;
6164 6067
6165 /* init NIL */ 6068 /* init NIL */
6166 set_typeflag (NIL, T_ATOM | MARK); 6069 set_typeflag (NIL, T_SPECIAL | T_ATOM);
6167 set_car (NIL, NIL); 6070 set_car (NIL, NIL);
6168 set_cdr (NIL, NIL); 6071 set_cdr (NIL, NIL);
6169 /* init T */ 6072 /* init T */
6170 set_typeflag (S_T, T_ATOM | MARK); 6073 set_typeflag (S_T, T_SPECIAL | T_ATOM);
6171 set_car (S_T, S_T); 6074 set_car (S_T, S_T);
6172 set_cdr (S_T, S_T); 6075 set_cdr (S_T, S_T);
6173 /* init F */ 6076 /* init F */
6174 set_typeflag (S_F, T_ATOM | MARK); 6077 set_typeflag (S_F, T_SPECIAL | T_ATOM);
6175 set_car (S_F, S_F); 6078 set_car (S_F, S_F);
6176 set_cdr (S_F, S_F); 6079 set_cdr (S_F, S_F);
6080 /* init EOF_OBJ */
6081 set_typeflag (S_EOF, T_SPECIAL | T_ATOM);
6082 set_car (S_EOF, S_EOF);
6083 set_cdr (S_EOF, S_EOF);
6177 /* init sink */ 6084 /* init sink */
6178 set_typeflag (S_SINK, T_PAIR | MARK); 6085 set_typeflag (S_SINK, T_PAIR);
6179 set_car (S_SINK, NIL); 6086 set_car (S_SINK, NIL);
6087
6180 /* init c_nest */ 6088 /* init c_nest */
6181 SCHEME_V->c_nest = NIL; 6089 SCHEME_V->c_nest = NIL;
6182 6090
6183 SCHEME_V->oblist = oblist_initial_value (SCHEME_A); 6091 SCHEME_V->oblist = oblist_initial_value (SCHEME_A);
6184 /* init global_env */ 6092 /* init global_env */
6185 new_frame_in_env (SCHEME_A_ NIL); 6093 new_frame_in_env (SCHEME_A_ NIL);
6186 SCHEME_V->global_env = SCHEME_V->envir; 6094 SCHEME_V->global_env = SCHEME_V->envir;
6187 /* init else */ 6095 /* init else */
6188 x = mk_symbol (SCHEME_A_ "else"); 6096 new_slot_in_env (SCHEME_A_ mk_symbol (SCHEME_A_ "else"), S_T);
6189 new_slot_in_env (SCHEME_A_ x, S_T);
6190 6097
6191 assign_syntax (SCHEME_A_ "lambda"); 6098 {
6192 assign_syntax (SCHEME_A_ "quote"); 6099 static const char *syntax_names[] = {
6193 assign_syntax (SCHEME_A_ "define"); 6100 "lambda", "quote", "define", "if", "begin", "set!",
6194 assign_syntax (SCHEME_A_ "if"); 6101 "let", "let*", "letrec", "cond", "delay", "and",
6195 assign_syntax (SCHEME_A_ "begin"); 6102 "or", "cons-stream", "macro", "case"
6196 assign_syntax (SCHEME_A_ "set!"); 6103 };
6197 assign_syntax (SCHEME_A_ "let"); 6104
6198 assign_syntax (SCHEME_A_ "let*"); 6105 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
6199 assign_syntax (SCHEME_A_ "letrec");
6200 assign_syntax (SCHEME_A_ "cond");
6201 assign_syntax (SCHEME_A_ "delay");
6202 assign_syntax (SCHEME_A_ "and"); 6106 assign_syntax (SCHEME_A_ syntax_names[i]);
6203 assign_syntax (SCHEME_A_ "or"); 6107 }
6204 assign_syntax (SCHEME_A_ "cons-stream");
6205 assign_syntax (SCHEME_A_ "macro");
6206 assign_syntax (SCHEME_A_ "case");
6207 6108
6109 // TODO: should iterate via strlen, to avoid n² complexity
6208 for (i = 0; i < n; i++) 6110 for (i = 0; i < n; i++)
6209 {
6210 if (dispatch_table[i].name != 0) 6111 if (dispatch_table[i].builtin)
6211 { 6112 assign_proc (SCHEME_A_ i, opname (i));
6212 assign_proc (SCHEME_A_ (enum scheme_opcodes) i, dispatch_table[i].name);
6213 }
6214 }
6215 6113
6216 /* initialization of global pointers to special symbols */ 6114 /* initialization of global pointers to special symbols */
6217 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 6115 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
6218 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 6116 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
6219 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 6117 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
6220 SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); 6118 SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote");
6221 SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); 6119 SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing");
6222 SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); 6120 SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>");
6223 SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); 6121 SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*");
6224 SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); 6122 SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*");
6225 SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); 6123 SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*");
6226 SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); 6124 SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*");
6227 6125
6228 return !SCHEME_V->no_memory; 6126 return !SCHEME_V->no_memory;
6229} 6127}
6230 6128
6231#if USE_PORTS 6129#if USE_PORTS
6232void 6130ecb_cold void
6233scheme_set_input_port_file (SCHEME_P_ int fin) 6131scheme_set_input_port_file (SCHEME_P_ int fin)
6234{ 6132{
6235 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 6133 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
6236} 6134}
6237 6135
6238void 6136ecb_cold void
6239scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) 6137scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end)
6240{ 6138{
6241 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); 6139 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input);
6242} 6140}
6243 6141
6244void 6142ecb_cold void
6245scheme_set_output_port_file (SCHEME_P_ int fout) 6143scheme_set_output_port_file (SCHEME_P_ int fout)
6246{ 6144{
6247 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 6145 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
6248} 6146}
6249 6147
6250void 6148ecb_cold void
6251scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) 6149scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end)
6252{ 6150{
6253 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); 6151 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output);
6254} 6152}
6255#endif 6153#endif
6256 6154
6257void 6155ecb_cold void
6258scheme_set_external_data (SCHEME_P_ void *p) 6156scheme_set_external_data (SCHEME_P_ void *p)
6259{ 6157{
6260 SCHEME_V->ext_data = p; 6158 SCHEME_V->ext_data = p;
6261} 6159}
6262 6160
6263void 6161ecb_cold void
6264scheme_deinit (SCHEME_P) 6162scheme_deinit (SCHEME_P)
6265{ 6163{
6266 int i; 6164 int i;
6267 6165
6268#if SHOW_ERROR_LINE 6166#if SHOW_ERROR_LINE
6294 SCHEME_V->loadport = NIL; 6192 SCHEME_V->loadport = NIL;
6295 SCHEME_V->gc_verbose = 0; 6193 SCHEME_V->gc_verbose = 0;
6296 gc (SCHEME_A_ NIL, NIL); 6194 gc (SCHEME_A_ NIL, NIL);
6297 6195
6298 for (i = 0; i <= SCHEME_V->last_cell_seg; i++) 6196 for (i = 0; i <= SCHEME_V->last_cell_seg; i++)
6299 free (SCHEME_V->alloc_seg[i]); 6197 free (SCHEME_V->cell_seg[i]);
6300 6198
6301#if SHOW_ERROR_LINE 6199#if SHOW_ERROR_LINE
6302 for (i = 0; i <= SCHEME_V->file_i; i++) 6200 for (i = 0; i <= SCHEME_V->file_i; i++)
6303 {
6304 if (SCHEME_V->load_stack[i].kind & port_file) 6201 if (SCHEME_V->load_stack[i].kind & port_file)
6305 { 6202 {
6306 fname = SCHEME_V->load_stack[i].rep.stdio.filename; 6203 fname = SCHEME_V->load_stack[i].rep.stdio.filename;
6307 6204
6308 if (fname) 6205 if (fname)
6309 free (fname); 6206 free (fname);
6310 } 6207 }
6311 }
6312#endif 6208#endif
6313} 6209}
6314 6210
6315void 6211ecb_cold void
6316scheme_load_file (SCHEME_P_ int fin) 6212scheme_load_file (SCHEME_P_ int fin)
6317{ 6213{
6318 scheme_load_named_file (SCHEME_A_ fin, 0); 6214 scheme_load_named_file (SCHEME_A_ fin, 0);
6319} 6215}
6320 6216
6321void 6217ecb_cold void
6322scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 6218scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
6323{ 6219{
6324 dump_stack_reset (SCHEME_A); 6220 dump_stack_reset (SCHEME_A);
6325 SCHEME_V->envir = SCHEME_V->global_env; 6221 SCHEME_V->envir = SCHEME_V->global_env;
6326 SCHEME_V->file_i = 0; 6222 SCHEME_V->file_i = 0;
6327 SCHEME_V->load_stack[0].unget = -1; 6223 SCHEME_V->load_stack[0].unget = -1;
6328 SCHEME_V->load_stack[0].kind = port_input | port_file; 6224 SCHEME_V->load_stack[0].kind = port_input | port_file;
6329 SCHEME_V->load_stack[0].rep.stdio.file = fin; 6225 SCHEME_V->load_stack[0].rep.stdio.file = fin;
6330#if USE_PORTS
6331 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6226 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
6332#endif
6333 SCHEME_V->retcode = 0; 6227 SCHEME_V->retcode = 0;
6334 6228
6335#if USE_PORTS
6336 if (fin == STDIN_FILENO) 6229 if (fin == STDIN_FILENO)
6337 SCHEME_V->interactive_repl = 1; 6230 SCHEME_V->interactive_repl = 1;
6338#endif
6339 6231
6340#if USE_PORTS 6232#if USE_PORTS
6341#if SHOW_ERROR_LINE 6233#if SHOW_ERROR_LINE
6342 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; 6234 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0;
6343 6235
6347#endif 6239#endif
6348 6240
6349 SCHEME_V->inport = SCHEME_V->loadport; 6241 SCHEME_V->inport = SCHEME_V->loadport;
6350 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6242 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
6351 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6243 Eval_Cycle (SCHEME_A_ OP_T0LVL);
6244
6352 set_typeflag (SCHEME_V->loadport, T_ATOM); 6245 set_typeflag (SCHEME_V->loadport, T_ATOM);
6353 6246
6354 if (SCHEME_V->retcode == 0) 6247 if (SCHEME_V->retcode == 0)
6355 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6248 SCHEME_V->retcode = SCHEME_V->nesting != 0;
6356} 6249}
6357 6250
6358void 6251ecb_cold void
6359scheme_load_string (SCHEME_P_ const char *cmd) 6252scheme_load_string (SCHEME_P_ const char *cmd)
6360{ 6253{
6254#if USE_PORTs
6361 dump_stack_reset (SCHEME_A); 6255 dump_stack_reset (SCHEME_A);
6362 SCHEME_V->envir = SCHEME_V->global_env; 6256 SCHEME_V->envir = SCHEME_V->global_env;
6363 SCHEME_V->file_i = 0; 6257 SCHEME_V->file_i = 0;
6364 SCHEME_V->load_stack[0].kind = port_input | port_string; 6258 SCHEME_V->load_stack[0].kind = port_input | port_string;
6365 SCHEME_V->load_stack[0].rep.string.start = (char *) cmd; /* This func respects const */ 6259 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */
6366 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *) cmd + strlen (cmd); 6260 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd);
6367 SCHEME_V->load_stack[0].rep.string.curr = (char *) cmd; 6261 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
6368#if USE_PORTS
6369 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6262 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
6370#endif
6371 SCHEME_V->retcode = 0; 6263 SCHEME_V->retcode = 0;
6372 SCHEME_V->interactive_repl = 0; 6264 SCHEME_V->interactive_repl = 0;
6373 SCHEME_V->inport = SCHEME_V->loadport; 6265 SCHEME_V->inport = SCHEME_V->loadport;
6374 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6266 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
6375 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6267 Eval_Cycle (SCHEME_A_ OP_T0LVL);
6376 set_typeflag (SCHEME_V->loadport, T_ATOM); 6268 set_typeflag (SCHEME_V->loadport, T_ATOM);
6377 6269
6378 if (SCHEME_V->retcode == 0) 6270 if (SCHEME_V->retcode == 0)
6379 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6271 SCHEME_V->retcode = SCHEME_V->nesting != 0;
6272#else
6273 abort ();
6274#endif
6380} 6275}
6381 6276
6382void 6277ecb_cold void
6383scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 6278scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
6384{ 6279{
6385 pointer x; 6280 pointer x;
6386 6281
6387 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6282 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
6388 6283
6389 if (x != NIL) 6284 if (x != NIL)
6390 {
6391 set_slot_in_env (SCHEME_A_ x, value); 6285 set_slot_in_env (SCHEME_A_ x, value);
6392 }
6393 else 6286 else
6394 {
6395 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6287 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
6396 }
6397} 6288}
6398 6289
6399#if !STANDALONE 6290#if !STANDALONE
6400void 6291
6292ecb_cold void
6401scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6293scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
6402{ 6294{
6403 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); 6295 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f));
6404} 6296}
6405 6297
6406void 6298ecb_cold void
6407scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6299scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
6408{ 6300{
6409 int i; 6301 int i;
6410 6302
6411 for (i = 0; i < count; i++) 6303 for (i = 0; i < count; i++)
6412 {
6413 scheme_register_foreign_func (SCHEME_A_ list + i); 6304 scheme_register_foreign_func (SCHEME_A_ list + i);
6414 }
6415} 6305}
6416 6306
6417pointer 6307ecb_cold pointer
6418scheme_apply0 (SCHEME_P_ const char *procname) 6308scheme_apply0 (SCHEME_P_ const char *procname)
6419{ 6309{
6420 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); 6310 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL));
6421} 6311}
6422 6312
6423void 6313ecb_cold void
6424save_from_C_call (SCHEME_P) 6314save_from_C_call (SCHEME_P)
6425{ 6315{
6426 pointer saved_data = cons (car (S_SINK), 6316 pointer saved_data = cons (car (S_SINK),
6427 cons (SCHEME_V->envir, 6317 cons (SCHEME_V->envir,
6428 SCHEME_V->dump)); 6318 SCHEME_V->dump));
6432 /* Truncate the dump stack so TS will return here when done, not 6322 /* Truncate the dump stack so TS will return here when done, not
6433 directly resume pre-C-call operations. */ 6323 directly resume pre-C-call operations. */
6434 dump_stack_reset (SCHEME_A); 6324 dump_stack_reset (SCHEME_A);
6435} 6325}
6436 6326
6437void 6327ecb_cold void
6438restore_from_C_call (SCHEME_P) 6328restore_from_C_call (SCHEME_P)
6439{ 6329{
6440 set_car (S_SINK, caar (SCHEME_V->c_nest)); 6330 set_car (S_SINK, caar (SCHEME_V->c_nest));
6441 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 6331 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
6442 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 6332 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
6443 /* Pop */ 6333 /* Pop */
6444 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 6334 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
6445} 6335}
6446 6336
6447/* "func" and "args" are assumed to be already eval'ed. */ 6337/* "func" and "args" are assumed to be already eval'ed. */
6448pointer 6338ecb_cold pointer
6449scheme_call (SCHEME_P_ pointer func, pointer args) 6339scheme_call (SCHEME_P_ pointer func, pointer args)
6450{ 6340{
6451 int old_repl = SCHEME_V->interactive_repl; 6341 int old_repl = SCHEME_V->interactive_repl;
6452 6342
6453 SCHEME_V->interactive_repl = 0; 6343 SCHEME_V->interactive_repl = 0;
6460 SCHEME_V->interactive_repl = old_repl; 6350 SCHEME_V->interactive_repl = old_repl;
6461 restore_from_C_call (SCHEME_A); 6351 restore_from_C_call (SCHEME_A);
6462 return SCHEME_V->value; 6352 return SCHEME_V->value;
6463} 6353}
6464 6354
6465pointer 6355ecb_cold pointer
6466scheme_eval (SCHEME_P_ pointer obj) 6356scheme_eval (SCHEME_P_ pointer obj)
6467{ 6357{
6468 int old_repl = SCHEME_V->interactive_repl; 6358 int old_repl = SCHEME_V->interactive_repl;
6469 6359
6470 SCHEME_V->interactive_repl = 0; 6360 SCHEME_V->interactive_repl = 0;
6476 SCHEME_V->interactive_repl = old_repl; 6366 SCHEME_V->interactive_repl = old_repl;
6477 restore_from_C_call (SCHEME_A); 6367 restore_from_C_call (SCHEME_A);
6478 return SCHEME_V->value; 6368 return SCHEME_V->value;
6479} 6369}
6480 6370
6481
6482#endif 6371#endif
6483 6372
6484/* ========== Main ========== */ 6373/* ========== Main ========== */
6485 6374
6486#if STANDALONE 6375#if STANDALONE
6487 6376
6488# if defined(__APPLE__) && !defined (OSX) 6377ecb_cold int
6489int
6490main ()
6491{
6492 extern MacTS_main (int argc, char **argv);
6493 char **argv;
6494 int argc = ccommand (&argv);
6495
6496 MacTS_main (argc, argv);
6497 return 0;
6498}
6499
6500int
6501MacTS_main (int argc, char **argv)
6502{
6503# else
6504int
6505main (int argc, char **argv) 6378main (int argc, char **argv)
6506{ 6379{
6507# endif
6508# if USE_MULTIPLICITY 6380# if USE_MULTIPLICITY
6509 scheme ssc; 6381 scheme ssc;
6510 scheme *const sc = &ssc; 6382 scheme *const SCHEME_V = &ssc;
6511# else 6383# else
6512# endif 6384# endif
6513 int fin; 6385 int fin;
6514 char *file_name = InitFile; 6386 char *file_name = InitFile;
6515 int retcode; 6387 int retcode;
6516 int isfile = 1; 6388 int isfile = 1;
6517 6389#if EXPERIMENT
6518 if (argc == 1) 6390 system ("ps v $PPID");
6519 xwrstr (banner); 6391#endif
6520 6392
6521 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6393 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6522 { 6394 {
6523 xwrstr ("Usage: tinyscheme -?\n"); 6395 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
6524 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6396 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
6525 xwrstr ("followed by\n"); 6397 putstr (SCHEME_A_ "followed by\n");
6526 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6398 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
6527 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6399 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
6528 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6400 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
6529 xwrstr ("Use - as filename for stdin.\n"); 6401 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
6530 return 1; 6402 return 1;
6531 } 6403 }
6532 6404
6533 if (!scheme_init (SCHEME_A)) 6405 if (!scheme_init (SCHEME_A))
6534 { 6406 {
6535 xwrstr ("Could not initialize!\n"); 6407 putstr (SCHEME_A_ "Could not initialize!\n");
6536 return 2; 6408 return 2;
6537 } 6409 }
6538 6410
6539# if USE_PORTS 6411# if USE_PORTS
6540 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6412 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
6547 if (access (file_name, 0) != 0) 6419 if (access (file_name, 0) != 0)
6548 { 6420 {
6549 char *p = getenv ("TINYSCHEMEINIT"); 6421 char *p = getenv ("TINYSCHEMEINIT");
6550 6422
6551 if (p != 0) 6423 if (p != 0)
6552 {
6553 file_name = p; 6424 file_name = p;
6554 }
6555 } 6425 }
6556#endif 6426#endif
6557 6427
6558 do 6428 do
6559 { 6429 {
6560#if USE_PORTS
6561 if (strcmp (file_name, "-") == 0) 6430 if (strcmp (file_name, "-") == 0)
6562 fin = STDIN_FILENO; 6431 fin = STDIN_FILENO;
6563 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) 6432 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0)
6564 { 6433 {
6565 pointer args = NIL; 6434 pointer args = NIL;
6583 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); 6452 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args);
6584 6453
6585 } 6454 }
6586 else 6455 else
6587 fin = open (file_name, O_RDONLY); 6456 fin = open (file_name, O_RDONLY);
6588#endif
6589 6457
6590 if (isfile && fin < 0) 6458 if (isfile && fin < 0)
6591 { 6459 {
6592 xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); 6460 putstr (SCHEME_A_ "Could not open file ");
6461 putstr (SCHEME_A_ file_name);
6462 putcharacter (SCHEME_A_ '\n');
6593 } 6463 }
6594 else 6464 else
6595 { 6465 {
6596 if (isfile) 6466 if (isfile)
6597 scheme_load_named_file (SCHEME_A_ fin, file_name); 6467 scheme_load_named_file (SCHEME_A_ fin, file_name);
6598 else 6468 else
6599 scheme_load_string (SCHEME_A_ file_name); 6469 scheme_load_string (SCHEME_A_ file_name);
6600 6470
6601#if USE_PORTS
6602 if (!isfile || fin != STDIN_FILENO) 6471 if (!isfile || fin != STDIN_FILENO)
6603 { 6472 {
6604 if (SCHEME_V->retcode != 0) 6473 if (SCHEME_V->retcode != 0)
6605 { 6474 {
6606 xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); 6475 putstr (SCHEME_A_ "Errors encountered reading ");
6476 putstr (SCHEME_A_ file_name);
6477 putcharacter (SCHEME_A_ '\n');
6607 } 6478 }
6608 6479
6609 if (isfile) 6480 if (isfile)
6610 close (fin); 6481 close (fin);
6611 } 6482 }
6612#endif
6613 } 6483 }
6614 6484
6615 file_name = *argv++; 6485 file_name = *argv++;
6616 } 6486 }
6617 while (file_name != 0); 6487 while (file_name != 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines