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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines