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.2 by root, Wed Nov 25 10:01:39 2015 UTC vs.
Revision 1.54 by root, Tue Dec 1 02:42:35 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines