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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines