ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/microscheme/scheme.c
(Generate patch)

Comparing cvsroot/microscheme/scheme.c (file contents):
Revision 1.1 by root, Wed Nov 25 05:02:56 2015 UTC vs.
Revision 1.53 by root, Tue Dec 1 02:21:49 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines