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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines