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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines