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

Comparing microscheme/scheme.c (file contents):
Revision 1.22 by root, Thu Nov 26 23:26:00 2015 UTC vs.
Revision 1.48 by root, Mon Nov 30 13:07:34 2015 UTC

16 * (MINISCM) This is a revised and modified version by Akira KIDA. 16 * (MINISCM) This is a revised and modified version by Akira KIDA.
17 * (MINISCM) current version is 0.85k4 (15 May 1994) 17 * (MINISCM) current version is 0.85k4 (15 May 1994)
18 * 18 *
19 */ 19 */
20 20
21#define EXPERIMENT 1
22
21#define PAGE_SIZE 4096 /* does not work on sparc/alpha */ 23#define PAGE_SIZE 4096 /* does not work on sparc/alpha */
22#include "malloc.c" 24#include "malloc.c"
23 25
24#define SCHEME_SOURCE 26#define SCHEME_SOURCE
25#include "scheme-private.h" 27#include "scheme-private.h"
28#endif 30#endif
29#if USE_MATH 31#if USE_MATH
30# include <math.h> 32# include <math.h>
31#endif 33#endif
32 34
35#include "ecb.h"
36
33#include <sys/types.h> 37#include <sys/types.h>
34#include <sys/stat.h> 38#include <sys/stat.h>
35#include <fcntl.h> 39#include <fcntl.h>
36 40
41#if !USE_ERROR_CHECKING
42# define NDEBUG
43#endif
44
45#include <assert.h>
46#include <stdlib.h>
37#include <string.h> 47#include <string.h>
38#include <stdlib.h>
39 48
40#include <limits.h> 49#include <limits.h>
41#include <inttypes.h> 50#include <inttypes.h>
42#include <float.h> 51#include <float.h>
43//#include <ctype.h> 52//#include <ctype.h>
53
54#if '1' != '0' + 1 \
55 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \
56 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \
57 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \
58 || 'f' != 'a' + 5
59# error "execution character set digits not consecutive"
60#endif
44 61
45enum { 62enum {
46 TOK_EOF, 63 TOK_EOF,
47 TOK_LPAREN, 64 TOK_LPAREN,
48 TOK_RPAREN, 65 TOK_RPAREN,
49 TOK_DOT, 66 TOK_DOT,
50 TOK_ATOM, 67 TOK_ATOM,
68 TOK_DOTATOM, /* atom name starting with '.' */
69 TOK_STRATOM, /* atom name enclosed in | */
51 TOK_QUOTE, 70 TOK_QUOTE,
52 TOK_DQUOTE, 71 TOK_DQUOTE,
53 TOK_BQUOTE, 72 TOK_BQUOTE,
54 TOK_COMMA, 73 TOK_COMMA,
55 TOK_ATMARK, 74 TOK_ATMARK,
57 TOK_SHARP_CONST, 76 TOK_SHARP_CONST,
58 TOK_VEC 77 TOK_VEC
59}; 78};
60 79
61#define BACKQUOTE '`' 80#define BACKQUOTE '`'
62#define DELIMITERS "()\";\f\t\v\n\r " 81#define WHITESPACE " \t\r\n\v\f"
82#define DELIMITERS "()\";" WHITESPACE
63 83
64#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 84#define NIL (&SCHEME_V->xNIL) //TODO: make this 0?
65#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 85#define S_T (&SCHEME_V->xT) //TODO: magic ptr value?
66#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 86#define S_F (&SCHEME_V->xF) //TODO: magic ptr value?
67#define S_SINK (&SCHEME_V->xsink) 87#define S_SINK (&SCHEME_V->xsink)
68#define S_EOF (&SCHEME_V->xEOF_OBJ) 88#define S_EOF (&SCHEME_V->xEOF_OBJ)
69 89
70/* should use libecb */
71#if __GNUC__ >= 4
72# define ecb_expect(expr,value) __builtin_expect ((expr),(value))
73# define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
74# define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
75#else
76# define ecb_expect_false(expr) !!(expr)
77# define ecb_expect_true(expr) !!(expr)
78#endif
79
80#if !USE_MULTIPLICITY 90#if !USE_MULTIPLICITY
81static scheme sc; 91static scheme sc;
82#endif 92#endif
83 93
84static void 94static void
153 163
154#define toupper(c) xtoupper (c) 164#define toupper(c) xtoupper (c)
155#define tolower(c) xtolower (c) 165#define tolower(c) xtolower (c)
156#define isdigit(c) xisdigit (c) 166#define isdigit(c) xisdigit (c)
157 167
158#if USE_STRLWR 168#if USE_IGNORECASE
159static const char * 169static const char *
160strlwr (char *s) 170xstrlwr (char *s)
161{ 171{
162 const char *p = s; 172 const char *p = s;
163 173
164 while (*s) 174 while (*s)
165 { 175 {
167 s++; 177 s++;
168 } 178 }
169 179
170 return p; 180 return p;
171} 181}
172#endif
173 182
183#define stricmp(a,b) strcasecmp (a, b)
184#define strlwr(s) xstrlwr (s)
185
186#else
174#define stricmp(a,b) strcmp (a, b) 187# define stricmp(a,b) strcmp (a, b)
175#define strlwr(s) (s) 188# define strlwr(s) (s)
189#endif
176 190
177#ifndef prompt 191#ifndef prompt
178# define prompt "ts> " 192# define prompt "ts> "
179#endif 193#endif
180 194
186# define FIRST_CELLSEGS 3 200# define FIRST_CELLSEGS 3
187#endif 201#endif
188 202
189enum scheme_types 203enum scheme_types
190{ 204{
205 T_INTEGER,
191 T_FREE, 206 T_REAL,
192 T_STRING, 207 T_STRING,
193 T_NUMBER,
194 T_SYMBOL, 208 T_SYMBOL,
195 T_PROC, 209 T_PROC,
196 T_PAIR, 210 T_PAIR, /* also used for free cells */
197 T_CLOSURE, 211 T_CLOSURE,
198 T_CONTINUATION, 212 T_CONTINUATION,
199 T_FOREIGN, 213 T_FOREIGN,
200 T_CHARACTER, 214 T_CHARACTER,
201 T_PORT, 215 T_PORT,
211#define T_SYNTAX 0x0010 225#define T_SYNTAX 0x0010
212#define T_IMMUTABLE 0x0020 226#define T_IMMUTABLE 0x0020
213#define T_ATOM 0x0040 /* only for gc */ 227#define T_ATOM 0x0040 /* only for gc */
214#define T_MARK 0x0080 /* only for gc */ 228#define T_MARK 0x0080 /* only for gc */
215 229
230/* num, for generic arithmetic */
231struct num
232{
233 IVALUE ivalue;
234#if USE_REAL
235 RVALUE rvalue;
236 char is_fixnum;
237#endif
238};
239
240#if USE_REAL
241# define num_is_fixnum(n) (n).is_fixnum
242# define num_set_fixnum(n,f) (n).is_fixnum = (f)
243# define num_ivalue(n) (n).ivalue
244# define num_rvalue(n) (n).rvalue
245# define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i)
246# define num_set_rvalue(n,r) (n).rvalue = (r)
247#else
248# define num_is_fixnum(n) 1
249# define num_set_fixnum(n,f) 0
250# define num_ivalue(n) (n).ivalue
251# define num_rvalue(n) (n).ivalue
252# define num_set_ivalue(n,i) (n).ivalue = (i)
253# define num_set_rvalue(n,r) (n).ivalue = (r)
254#endif
255
216enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; 256enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
217 257
218static num num_op (enum num_op op, num a, num b); 258static num num_op (enum num_op op, num a, num b);
219static num num_intdiv (num a, num b); 259static num num_intdiv (num a, num b);
220static num num_rem (num a, num b); 260static num num_rem (num a, num b);
223#if USE_MATH 263#if USE_MATH
224static double round_per_R5RS (double x); 264static double round_per_R5RS (double x);
225#endif 265#endif
226static int is_zero_rvalue (RVALUE x); 266static int is_zero_rvalue (RVALUE x);
227 267
228static INLINE int
229num_is_integer (pointer p)
230{
231 return num_is_fixnum (p->object.number);
232}
233
234static num num_zero; 268static num num_zero;
235static num num_one; 269static num num_one;
236 270
237/* macros for cell operations */ 271/* macros for cell operations */
238#define typeflag(p) ((p)->flag + 0) 272#define typeflag(p) ((p)->flag + 0)
239#define set_typeflag(p,v) ((p)->flag = (v)) 273#define set_typeflag(p,v) ((p)->flag = (v))
240#define type(p) (typeflag (p) & T_MASKTYPE) 274#define type(p) (typeflag (p) & T_MASKTYPE)
241 275
242INTERFACE INLINE int 276INTERFACE int
243is_string (pointer p) 277is_string (pointer p)
244{ 278{
245 return type (p) == T_STRING; 279 return type (p) == T_STRING;
246} 280}
247 281
248#define strvalue(p) ((p)->object.string.svalue) 282#define strvalue(p) ((p)->object.string.svalue)
249#define strlength(p) ((p)->object.string.length) 283#define strlength(p) ((p)->object.string.length)
250 284
251INTERFACE int is_list (SCHEME_P_ pointer p);
252
253INTERFACE INLINE int 285INTERFACE int
254is_vector (pointer p) 286is_vector (pointer p)
255{ 287{
256 return type (p) == T_VECTOR; 288 return type (p) == T_VECTOR;
257} 289}
258 290
259#define vecvalue(p) ((p)->object.vector.vvalue) 291#define vecvalue(p) ((p)->object.vector.vvalue)
260#define veclength(p) ((p)->object.vector.length) 292#define veclength(p) ((p)->object.vector.length)
261INTERFACE void fill_vector (pointer vec, pointer obj); 293INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj);
262INTERFACE uint32_t vector_length (pointer vec);
263INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); 294INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
264INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 295INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
265 296
266INTERFACE uint32_t 297INTERFACE int
267vector_length (pointer vec) 298is_integer (pointer p)
268{ 299{
269 return vec->object.vector.length; 300 return type (p) == T_INTEGER;
270} 301}
271 302
303/* not the same as in scheme, where integers are (correctly :) reals */
272INTERFACE INLINE int 304INTERFACE int
305is_real (pointer p)
306{
307 return type (p) == T_REAL;
308}
309
310INTERFACE int
273is_number (pointer p) 311is_number (pointer p)
274{ 312{
275 return type (p) == T_NUMBER; 313 return is_integer (p) || is_real (p);
276} 314}
277 315
278INTERFACE INLINE int 316INTERFACE int
279is_integer (pointer p)
280{
281 if (!is_number (p))
282 return 0;
283
284 if (num_is_integer (p) || ivalue (p) == rvalue (p))
285 return 1;
286
287 return 0;
288}
289
290INTERFACE INLINE int
291is_real (pointer p)
292{
293 return is_number (p) && !num_is_fixnum (p->object.number);
294}
295
296INTERFACE INLINE int
297is_character (pointer p) 317is_character (pointer p)
298{ 318{
299 return type (p) == T_CHARACTER; 319 return type (p) == T_CHARACTER;
300} 320}
301 321
302INTERFACE INLINE char * 322INTERFACE char *
303string_value (pointer p) 323string_value (pointer p)
304{ 324{
305 return strvalue (p); 325 return strvalue (p);
306} 326}
307 327
308INLINE num
309nvalue (pointer p)
310{
311 return (p)->object.number;
312}
313
314static IVALUE
315num_get_ivalue (const num n)
316{
317 return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n);
318}
319
320static RVALUE
321num_get_rvalue (const num n)
322{
323 return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n);
324}
325
326INTERFACE IVALUE
327ivalue (pointer p)
328{
329 return num_get_ivalue (p->object.number);
330}
331
332INTERFACE RVALUE
333rvalue (pointer p)
334{
335 return num_get_rvalue (p->object.number);
336}
337
338#define ivalue_unchecked(p) ((p)->object.number.value.ivalue) 328#define ivalue_unchecked(p) (p)->object.ivalue
329#define set_ivalue(p,v) (p)->object.ivalue = (v)
330
339#if USE_REAL 331#if USE_REAL
340# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 332#define rvalue_unchecked(p) (p)->object.rvalue
341# define set_num_integer(p) (p)->object.number.is_fixnum=1; 333#define set_rvalue(p,v) (p)->object.rvalue = (v)
342# define set_num_real(p) (p)->object.number.is_fixnum=0;
343#else 334#else
344# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 335#define rvalue_unchecked(p) (p)->object.ivalue
345# define set_num_integer(p) 0 336#define set_rvalue(p,v) (p)->object.ivalue = (v)
346# define set_num_real(p) 0
347#endif 337#endif
338
348INTERFACE long 339INTERFACE long
349charvalue (pointer p) 340charvalue (pointer p)
350{ 341{
351 return ivalue_unchecked (p); 342 return ivalue_unchecked (p);
352} 343}
353 344
354INTERFACE INLINE int 345INTERFACE int
355is_port (pointer p) 346is_port (pointer p)
356{ 347{
357 return type (p) == T_PORT; 348 return type (p) == T_PORT;
358} 349}
359 350
360INTERFACE INLINE int 351INTERFACE int
361is_inport (pointer p) 352is_inport (pointer p)
362{ 353{
363 return is_port (p) && p->object.port->kind & port_input; 354 return is_port (p) && p->object.port->kind & port_input;
364} 355}
365 356
366INTERFACE INLINE int 357INTERFACE int
367is_outport (pointer p) 358is_outport (pointer p)
368{ 359{
369 return is_port (p) && p->object.port->kind & port_output; 360 return is_port (p) && p->object.port->kind & port_output;
370} 361}
371 362
372INTERFACE INLINE int 363INTERFACE int
373is_pair (pointer p) 364is_pair (pointer p)
374{ 365{
375 return type (p) == T_PAIR; 366 return type (p) == T_PAIR;
376} 367}
377 368
409pair_cdr (pointer p) 400pair_cdr (pointer p)
410{ 401{
411 return cdr (p); 402 return cdr (p);
412} 403}
413 404
414INTERFACE INLINE int 405INTERFACE int
415is_symbol (pointer p) 406is_symbol (pointer p)
416{ 407{
417 return type (p) == T_SYMBOL; 408 return type (p) == T_SYMBOL;
418} 409}
419 410
420INTERFACE INLINE char * 411INTERFACE char *
421symname (pointer p) 412symname (pointer p)
422{ 413{
423 return strvalue (car (p)); 414 return strvalue (p);
424} 415}
425 416
426#if USE_PLIST 417#if USE_PLIST
418#error plists are broken because symbols are no longer pairs
419#define symprop(p) cdr(p)
427SCHEME_EXPORT INLINE int 420SCHEME_EXPORT int
428hasprop (pointer p) 421hasprop (pointer p)
429{ 422{
430 return typeflag (p) & T_SYMBOL; 423 return typeflag (p) & T_SYMBOL;
431} 424}
432
433# define symprop(p) cdr(p)
434#endif 425#endif
435 426
436INTERFACE INLINE int 427INTERFACE int
437is_syntax (pointer p) 428is_syntax (pointer p)
438{ 429{
439 return typeflag (p) & T_SYNTAX; 430 return typeflag (p) & T_SYNTAX;
440} 431}
441 432
442INTERFACE INLINE int 433INTERFACE int
443is_proc (pointer p) 434is_proc (pointer p)
444{ 435{
445 return type (p) == T_PROC; 436 return type (p) == T_PROC;
446} 437}
447 438
448INTERFACE INLINE int 439INTERFACE int
449is_foreign (pointer p) 440is_foreign (pointer p)
450{ 441{
451 return type (p) == T_FOREIGN; 442 return type (p) == T_FOREIGN;
452} 443}
453 444
454INTERFACE INLINE char * 445INTERFACE char *
455syntaxname (pointer p) 446syntaxname (pointer p)
456{ 447{
457 return strvalue (car (p)); 448 return strvalue (p);
458} 449}
459 450
460#define procnum(p) ivalue (p) 451#define procnum(p) ivalue_unchecked (p)
461static const char *procname (pointer x); 452static const char *procname (pointer x);
462 453
463INTERFACE INLINE int 454INTERFACE int
464is_closure (pointer p) 455is_closure (pointer p)
465{ 456{
466 return type (p) == T_CLOSURE; 457 return type (p) == T_CLOSURE;
467} 458}
468 459
469INTERFACE INLINE int 460INTERFACE int
470is_macro (pointer p) 461is_macro (pointer p)
471{ 462{
472 return type (p) == T_MACRO; 463 return type (p) == T_MACRO;
473} 464}
474 465
475INTERFACE INLINE pointer 466INTERFACE pointer
476closure_code (pointer p) 467closure_code (pointer p)
477{ 468{
478 return car (p); 469 return car (p);
479} 470}
480 471
481INTERFACE INLINE pointer 472INTERFACE pointer
482closure_env (pointer p) 473closure_env (pointer p)
483{ 474{
484 return cdr (p); 475 return cdr (p);
485} 476}
486 477
487INTERFACE INLINE int 478INTERFACE int
488is_continuation (pointer p) 479is_continuation (pointer p)
489{ 480{
490 return type (p) == T_CONTINUATION; 481 return type (p) == T_CONTINUATION;
491} 482}
492 483
493#define cont_dump(p) cdr (p) 484#define cont_dump(p) cdr (p)
494#define set_cont_dump(p,v) set_cdr ((p), (v)) 485#define set_cont_dump(p,v) set_cdr ((p), (v))
495 486
496/* To do: promise should be forced ONCE only */ 487/* To do: promise should be forced ONCE only */
497INTERFACE INLINE int 488INTERFACE int
498is_promise (pointer p) 489is_promise (pointer p)
499{ 490{
500 return type (p) == T_PROMISE; 491 return type (p) == T_PROMISE;
501} 492}
502 493
503INTERFACE INLINE int 494INTERFACE int
504is_environment (pointer p) 495is_environment (pointer p)
505{ 496{
506 return type (p) == T_ENVIRONMENT; 497 return type (p) == T_ENVIRONMENT;
507} 498}
508 499
514 505
515#define is_mark(p) (typeflag (p) & T_MARK) 506#define is_mark(p) (typeflag (p) & T_MARK)
516#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 507#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
517#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 508#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
518 509
519INTERFACE INLINE int 510INTERFACE int
520is_immutable (pointer p) 511is_immutable (pointer p)
521{ 512{
522 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 513 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
523} 514}
524 515
525INTERFACE INLINE void 516INTERFACE void
526setimmutable (pointer p) 517setimmutable (pointer p)
527{ 518{
528#if USE_ERROR_CHECKING 519#if USE_ERROR_CHECKING
529 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 520 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
530#endif 521#endif
531} 522}
532 523
524/* Result is:
525 proper list: length
526 circular list: -1
527 not even a pair: -2
528 dotted list: -2 minus length before dot
529*/
530INTERFACE int
531list_length (SCHEME_P_ pointer a)
532{
533 int i = 0;
534 pointer slow, fast;
535
536 slow = fast = a;
537
538 while (1)
539 {
540 if (fast == NIL)
541 return i;
542
543 if (!is_pair (fast))
544 return -2 - i;
545
546 fast = cdr (fast);
547 ++i;
548
549 if (fast == NIL)
550 return i;
551
552 if (!is_pair (fast))
553 return -2 - i;
554
555 ++i;
556 fast = cdr (fast);
557
558 /* Safe because we would have already returned if `fast'
559 encountered a non-pair. */
560 slow = cdr (slow);
561
562 if (fast == slow)
563 {
564 /* the fast pointer has looped back around and caught up
565 with the slow pointer, hence the structure is circular,
566 not of finite length, and therefore not a list */
567 return -1;
568 }
569 }
570}
571
572INTERFACE int
573is_list (SCHEME_P_ pointer a)
574{
575 return list_length (SCHEME_A_ a) >= 0;
576}
577
533#if USE_CHAR_CLASSIFIERS 578#if USE_CHAR_CLASSIFIERS
534static INLINE int 579ecb_inline int
535Cisalpha (int c) 580Cisalpha (int c)
536{ 581{
537 return isascii (c) && isalpha (c); 582 return isascii (c) && isalpha (c);
538} 583}
539 584
540static INLINE int 585ecb_inline int
541Cisdigit (int c) 586Cisdigit (int c)
542{ 587{
543 return isascii (c) && isdigit (c); 588 return isascii (c) && isdigit (c);
544} 589}
545 590
546static INLINE int 591ecb_inline int
547Cisspace (int c) 592Cisspace (int c)
548{ 593{
549 return isascii (c) && isspace (c); 594 return isascii (c) && isspace (c);
550} 595}
551 596
552static INLINE int 597ecb_inline int
553Cisupper (int c) 598Cisupper (int c)
554{ 599{
555 return isascii (c) && isupper (c); 600 return isascii (c) && isupper (c);
556} 601}
557 602
558static INLINE int 603ecb_inline int
559Cislower (int c) 604Cislower (int c)
560{ 605{
561 return isascii (c) && islower (c); 606 return isascii (c) && islower (c);
562} 607}
563#endif 608#endif
624#endif 669#endif
625 670
626static int file_push (SCHEME_P_ const char *fname); 671static int file_push (SCHEME_P_ const char *fname);
627static void file_pop (SCHEME_P); 672static void file_pop (SCHEME_P);
628static int file_interactive (SCHEME_P); 673static int file_interactive (SCHEME_P);
629static INLINE int is_one_of (char *s, int c); 674ecb_inline int is_one_of (const char *s, int c);
630static int alloc_cellseg (SCHEME_P_ int n); 675static int alloc_cellseg (SCHEME_P_ int n);
631static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 676ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
632static void finalize_cell (SCHEME_P_ pointer a); 677static void finalize_cell (SCHEME_P_ pointer a);
633static int count_consecutive_cells (pointer x, int needed); 678static int count_consecutive_cells (pointer x, int needed);
634static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 679static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
635static pointer mk_number (SCHEME_P_ const num n); 680static pointer mk_number (SCHEME_P_ const num n);
636static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 681static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
651static void mark (pointer a); 696static void mark (pointer a);
652static void gc (SCHEME_P_ pointer a, pointer b); 697static void gc (SCHEME_P_ pointer a, pointer b);
653static int basic_inchar (port *pt); 698static int basic_inchar (port *pt);
654static int inchar (SCHEME_P); 699static int inchar (SCHEME_P);
655static void backchar (SCHEME_P_ int c); 700static void backchar (SCHEME_P_ int c);
656static char *readstr_upto (SCHEME_P_ char *delim); 701static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
657static pointer readstrexp (SCHEME_P); 702static pointer readstrexp (SCHEME_P_ char delim);
658static INLINE int skipspace (SCHEME_P); 703ecb_inline int skipspace (SCHEME_P);
659static int token (SCHEME_P); 704static int token (SCHEME_P);
660static void printslashstring (SCHEME_P_ char *s, int len); 705static void printslashstring (SCHEME_P_ char *s, int len);
661static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 706static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
662static void printatom (SCHEME_P_ pointer l, int f); 707static void printatom (SCHEME_P_ pointer l, int f);
663static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 708static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
679static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 724static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
680static void assign_syntax (SCHEME_P_ const char *name); 725static void assign_syntax (SCHEME_P_ const char *name);
681static int syntaxnum (pointer p); 726static int syntaxnum (pointer p);
682static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 727static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
683 728
729static IVALUE
730ivalue (pointer x)
731{
732 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
733}
734
735static RVALUE
736rvalue (pointer x)
737{
738 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
739}
740
741INTERFACE num
742nvalue (pointer x)
743{
744 num n;
745
746 num_set_fixnum (n, is_integer (x));
747
748 if (num_is_fixnum (n))
749 num_set_ivalue (n, ivalue_unchecked (x));
750 else
751 num_set_rvalue (n, rvalue_unchecked (x));
752
753 return n;
754}
755
684static num 756static num
685num_op (enum num_op op, num a, num b) 757num_op (enum num_op op, num a, num b)
686{ 758{
687 num ret; 759 num ret;
688 760
689 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 761 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
690 762
691 if (num_is_fixnum (ret)) 763 if (num_is_fixnum (ret))
692 { 764 {
693 IVALUE av = num_get_ivalue (a);
694 IVALUE bv = num_get_ivalue (b);
695
696 switch (op) 765 switch (op)
697 { 766 {
698 case NUM_ADD: av += bv; break; 767 case NUM_ADD: a.ivalue += b.ivalue; break;
699 case NUM_SUB: av -= bv; break; 768 case NUM_SUB: a.ivalue -= b.ivalue; break;
700 case NUM_MUL: av *= bv; break; 769 case NUM_MUL: a.ivalue *= b.ivalue; break;
701 case NUM_INTDIV: av /= bv; break; 770 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
702 } 771 }
703 772
704 num_set_ivalue (ret, av); 773 num_set_ivalue (ret, a.ivalue);
705 } 774 }
775#if USE_REAL
706 else 776 else
707 { 777 {
708 RVALUE av = num_get_rvalue (a);
709 RVALUE bv = num_get_rvalue (b);
710
711 switch (op) 778 switch (op)
712 { 779 {
713 case NUM_ADD: av += bv; break; 780 case NUM_ADD: a.rvalue += b.rvalue; break;
714 case NUM_SUB: av -= bv; break; 781 case NUM_SUB: a.rvalue -= b.rvalue; break;
715 case NUM_MUL: av *= bv; break; 782 case NUM_MUL: a.rvalue *= b.rvalue; break;
716 case NUM_INTDIV: av /= bv; break; 783 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
717 } 784 }
718 785
719 num_set_rvalue (ret, av); 786 num_set_rvalue (ret, a.rvalue);
720 } 787 }
788#endif
721 789
722 return ret; 790 return ret;
723} 791}
724 792
725static num 793static num
726num_div (num a, num b) 794num_div (num a, num b)
727{ 795{
728 num ret; 796 num ret;
729 797
730 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); 798 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
731 799
732 if (num_is_fixnum (ret)) 800 if (num_is_fixnum (ret))
733 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 801 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
734 else 802 else
735 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 803 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
736 804
737 return ret; 805 return ret;
738} 806}
739 807
740static num 808static num
742{ 810{
743 num ret; 811 num ret;
744 long e1, e2, res; 812 long e1, e2, res;
745 813
746 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 814 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
747 e1 = num_get_ivalue (a); 815 e1 = num_ivalue (a);
748 e2 = num_get_ivalue (b); 816 e2 = num_ivalue (b);
749 res = e1 % e2; 817 res = e1 % e2;
750 818
751 /* remainder should have same sign as second operand */ 819 /* remainder should have same sign as second operand */
752 if (res > 0) 820 if (res > 0)
753 { 821 {
769{ 837{
770 num ret; 838 num ret;
771 long e1, e2, res; 839 long e1, e2, res;
772 840
773 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 841 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
774 e1 = num_get_ivalue (a); 842 e1 = num_ivalue (a);
775 e2 = num_get_ivalue (b); 843 e2 = num_ivalue (b);
776 res = e1 % e2; 844 res = e1 % e2;
777 845
778 /* modulo should have same sign as second operand */ 846 /* modulo should have same sign as second operand */
779 if (res * e2 < 0) 847 if (res * e2 < 0)
780 res += e2; 848 res += e2;
781 849
782 num_set_ivalue (ret, res); 850 num_set_ivalue (ret, res);
783 return ret; 851 return ret;
784} 852}
785 853
786/* this completely disrespects NaNs */ 854/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
787static int 855static int
788num_cmp (num a, num b) 856num_cmp (num a, num b)
789{ 857{
790 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 858 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
791 int ret; 859 int ret;
792 860
793 if (is_fixnum) 861 if (is_fixnum)
794 { 862 {
795 IVALUE av = num_get_ivalue (a); 863 IVALUE av = num_ivalue (a);
796 IVALUE bv = num_get_ivalue (b); 864 IVALUE bv = num_ivalue (b);
797 865
798 ret = av == bv ? 0 : av < bv ? -1 : +1; 866 ret = av == bv ? 0 : av < bv ? -1 : +1;
799 } 867 }
800 else 868 else
801 { 869 {
802 RVALUE av = num_get_rvalue (a); 870 RVALUE av = num_rvalue (a);
803 RVALUE bv = num_get_rvalue (b); 871 RVALUE bv = num_rvalue (b);
804 872
805 ret = av == bv ? 0 : av < bv ? -1 : +1; 873 ret = av == bv ? 0 : av < bv ? -1 : +1;
806 } 874 }
807 875
808 return ret; 876 return ret;
834#endif 902#endif
835 903
836static int 904static int
837is_zero_rvalue (RVALUE x) 905is_zero_rvalue (RVALUE x)
838{ 906{
907 return x == 0;
908#if 0
839#if USE_REAL 909#if USE_REAL
840 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 910 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
841#else 911#else
842 return x == 0; 912 return x == 0;
913#endif
843#endif 914#endif
844} 915}
845 916
846/* allocate new cell segment */ 917/* allocate new cell segment */
847static int 918static int
868 return k; 939 return k;
869 940
870 i = ++SCHEME_V->last_cell_seg; 941 i = ++SCHEME_V->last_cell_seg;
871 SCHEME_V->alloc_seg[i] = cp; 942 SCHEME_V->alloc_seg[i] = cp;
872 943
873 /* insert new segment in address order */
874 newp = (pointer)cp; 944 newp = (pointer)cp;
875 SCHEME_V->cell_seg[i] = newp; 945 SCHEME_V->cell_seg[i] = newp;
876 SCHEME_V->cell_segsize[i] = segsize; 946 SCHEME_V->cell_segsize[i] = segsize;
877
878 //TODO: insert, not swap
879 while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i])
880 {
881 p = SCHEME_V->cell_seg[i];
882 SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1];
883 SCHEME_V->cell_seg[i - 1] = p;
884
885 k = SCHEME_V->cell_segsize[i];
886 SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1];
887 SCHEME_V->cell_segsize[i - 1] = k;
888
889 --i;
890 }
891
892 SCHEME_V->fcells += segsize; 947 SCHEME_V->fcells += segsize;
893 last = newp + segsize - 1; 948 last = newp + segsize - 1;
894 949
895 for (p = newp; p <= last; p++) 950 for (p = newp; p <= last; p++)
896 { 951 {
897 set_typeflag (p, T_FREE); 952 set_typeflag (p, T_PAIR);
898 set_car (p, NIL); 953 set_car (p, NIL);
899 set_cdr (p, p + 1); 954 set_cdr (p, p + 1);
900 } 955 }
901 956
902 /* insert new cells in address order on free list */
903 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
904 {
905 set_cdr (last, SCHEME_V->free_cell); 957 set_cdr (last, SCHEME_V->free_cell);
906 SCHEME_V->free_cell = newp; 958 SCHEME_V->free_cell = newp;
907 }
908 else
909 {
910 p = SCHEME_V->free_cell;
911
912 while (cdr (p) != NIL && newp > cdr (p))
913 p = cdr (p);
914
915 set_cdr (last, cdr (p));
916 set_cdr (p, newp);
917 }
918 } 959 }
919 960
920 return n; 961 return n;
921} 962}
922 963
923/* get new cell. parameter a, b is marked by gc. */ 964/* get new cell. parameter a, b is marked by gc. */
924static INLINE pointer 965ecb_inline pointer
925get_cell_x (SCHEME_P_ pointer a, pointer b) 966get_cell_x (SCHEME_P_ pointer a, pointer b)
926{ 967{
927 if (ecb_expect_false (SCHEME_V->free_cell == NIL)) 968 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
928 { 969 {
929 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 970 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
930 return S_SINK; 971 return S_SINK;
931 972
932 if (SCHEME_V->free_cell == NIL) 973 if (SCHEME_V->free_cell == NIL)
933 { 974 {
934 const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; 975 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1;
935 976
936 gc (SCHEME_A_ a, b); 977 gc (SCHEME_A_ a, b);
937 978
938 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 979 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
939 { 980 {
990} 1031}
991 1032
992static pointer 1033static pointer
993get_vector_object (SCHEME_P_ uint32_t len, pointer init) 1034get_vector_object (SCHEME_P_ uint32_t len, pointer init)
994{ 1035{
995 pointer v = get_cell_x (SCHEME_A_ 0, 0); 1036 pointer v = get_cell_x (SCHEME_A_ NIL, NIL);
996 pointer *e = malloc (len * sizeof (pointer)); 1037 pointer *e = malloc (len * sizeof (pointer));
997 1038
998 if (!e && USE_ERROR_CHECKING) 1039 if (!e && USE_ERROR_CHECKING)
999 return S_SINK; 1040 return S_SINK;
1000 1041
1001 /* Record it as a vector so that gc understands it. */ 1042 /* Record it as a vector so that gc understands it. */
1002 set_typeflag (v, T_VECTOR | T_ATOM); 1043 set_typeflag (v, T_VECTOR | T_ATOM);
1003 1044
1004 v->object.vector.vvalue = e; 1045 v->object.vector.vvalue = e;
1005 v->object.vector.length = len; 1046 v->object.vector.length = len;
1006 fill_vector (v, init); 1047 fill_vector (v, 0, init);
1007 push_recent_alloc (SCHEME_A_ v, NIL); 1048 push_recent_alloc (SCHEME_A_ v, NIL);
1008 1049
1009 return v; 1050 return v;
1010} 1051}
1011 1052
1012static INLINE void 1053ecb_inline void
1013ok_to_freely_gc (SCHEME_P) 1054ok_to_freely_gc (SCHEME_P)
1014{ 1055{
1015 set_car (S_SINK, NIL); 1056 set_car (S_SINK, NIL);
1016} 1057}
1017 1058
1054 set_cdr (x, b); 1095 set_cdr (x, b);
1055 1096
1056 return x; 1097 return x;
1057} 1098}
1058 1099
1100static pointer
1101generate_symbol (SCHEME_P_ const char *name)
1102{
1103 pointer x = mk_string (SCHEME_A_ name);
1104 setimmutable (x);
1105 set_typeflag (x, T_SYMBOL | T_ATOM);
1106 return x;
1107}
1108
1059/* ========== oblist implementation ========== */ 1109/* ========== oblist implementation ========== */
1060 1110
1061#ifndef USE_OBJECT_LIST 1111#ifndef USE_OBJECT_LIST
1062 1112
1113static int
1063static int hash_fn (const char *key, int table_size); 1114hash_fn (const char *key, int table_size)
1115{
1116 const unsigned char *p = key;
1117 uint32_t hash = 2166136261;
1118
1119 while (*p)
1120 hash = (hash ^ *p++) * 16777619;
1121
1122 return hash % table_size;
1123}
1064 1124
1065static pointer 1125static pointer
1066oblist_initial_value (SCHEME_P) 1126oblist_initial_value (SCHEME_P)
1067{ 1127{
1068 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1128 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1070 1130
1071/* returns the new symbol */ 1131/* returns the new symbol */
1072static pointer 1132static pointer
1073oblist_add_by_name (SCHEME_P_ const char *name) 1133oblist_add_by_name (SCHEME_P_ const char *name)
1074{ 1134{
1075 int location; 1135 pointer x = generate_symbol (SCHEME_A_ name);
1076
1077 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1078 set_typeflag (x, T_SYMBOL);
1079 setimmutable (car (x));
1080
1081 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1136 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1082 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1137 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1083 return x; 1138 return x;
1084} 1139}
1085 1140
1086static INLINE pointer 1141ecb_inline pointer
1087oblist_find_by_name (SCHEME_P_ const char *name) 1142oblist_find_by_name (SCHEME_P_ const char *name)
1088{ 1143{
1089 int location; 1144 int location;
1090 pointer x; 1145 pointer x;
1091 char *s; 1146 char *s;
1092 1147
1093 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1148 location = hash_fn (name, veclength (SCHEME_V->oblist));
1094 1149
1095 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1150 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1096 { 1151 {
1097 s = symname (car (x)); 1152 s = symname (car (x));
1098 1153
1099 /* case-insensitive, per R5RS section 2 */ 1154 /* case-insensitive, per R5RS section 2 */
1100 if (stricmp (name, s) == 0) 1155 if (stricmp (name, s) == 0)
1110 int i; 1165 int i;
1111 pointer x; 1166 pointer x;
1112 pointer ob_list = NIL; 1167 pointer ob_list = NIL;
1113 1168
1114 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1169 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1115 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1170 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1116 ob_list = cons (x, ob_list); 1171 ob_list = cons (x, ob_list);
1117 1172
1118 return ob_list; 1173 return ob_list;
1119} 1174}
1120 1175
1124oblist_initial_value (SCHEME_P) 1179oblist_initial_value (SCHEME_P)
1125{ 1180{
1126 return NIL; 1181 return NIL;
1127} 1182}
1128 1183
1129static INLINE pointer 1184ecb_inline pointer
1130oblist_find_by_name (SCHEME_P_ const char *name) 1185oblist_find_by_name (SCHEME_P_ const char *name)
1131{ 1186{
1132 pointer x; 1187 pointer x;
1133 char *s; 1188 char *s;
1134 1189
1146 1201
1147/* returns the new symbol */ 1202/* returns the new symbol */
1148static pointer 1203static pointer
1149oblist_add_by_name (SCHEME_P_ const char *name) 1204oblist_add_by_name (SCHEME_P_ const char *name)
1150{ 1205{
1151 pointer x; 1206 pointer x = generate_symbol (SCHEME_A_ name);
1152
1153 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1154 set_typeflag (x, T_SYMBOL);
1155 setimmutable (car (x));
1156 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1207 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1157 return x; 1208 return x;
1158} 1209}
1159 1210
1160static pointer 1211static pointer
1181pointer 1232pointer
1182mk_foreign_func (SCHEME_P_ foreign_func f) 1233mk_foreign_func (SCHEME_P_ foreign_func f)
1183{ 1234{
1184 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1235 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1185 1236
1186 set_typeflag (x, (T_FOREIGN | T_ATOM)); 1237 set_typeflag (x, T_FOREIGN | T_ATOM);
1187 x->object.ff = f; 1238 x->object.ff = f;
1188 1239
1189 return x; 1240 return x;
1190} 1241}
1191 1242
1192INTERFACE pointer 1243INTERFACE pointer
1193mk_character (SCHEME_P_ int c) 1244mk_character (SCHEME_P_ int c)
1194{ 1245{
1195 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1246 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1196 1247
1197 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1248 set_typeflag (x, T_CHARACTER | T_ATOM);
1198 ivalue_unchecked (x) = c & 0xff; 1249 set_ivalue (x, c & 0xff);
1199 set_num_integer (x); 1250
1200 return x; 1251 return x;
1201} 1252}
1202 1253
1203/* get number atom (integer) */ 1254/* get number atom (integer) */
1204INTERFACE pointer 1255INTERFACE pointer
1205mk_integer (SCHEME_P_ long num) 1256mk_integer (SCHEME_P_ long n)
1206{ 1257{
1258 pointer p = 0;
1259 pointer *pp = &p;
1260
1261#if USE_INTCACHE
1262 if (n >= INTCACHE_MIN && n <= INTCACHE_MAX)
1263 pp = &SCHEME_V->intcache[n - INTCACHE_MIN];
1264#endif
1265
1266 if (!*pp)
1267 {
1207 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1268 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1208 1269
1209 set_typeflag (x, (T_NUMBER | T_ATOM)); 1270 set_typeflag (x, T_INTEGER | T_ATOM);
1210 ivalue_unchecked (x) = num; 1271 setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */
1211 set_num_integer (x); 1272 set_ivalue (x, n);
1273
1274 *pp = x;
1275 }
1276
1212 return x; 1277 return *pp;
1213} 1278}
1214 1279
1215INTERFACE pointer 1280INTERFACE pointer
1216mk_real (SCHEME_P_ RVALUE n) 1281mk_real (SCHEME_P_ RVALUE n)
1217{ 1282{
1283#if USE_REAL
1218 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1284 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1219 1285
1220 set_typeflag (x, (T_NUMBER | T_ATOM)); 1286 set_typeflag (x, T_REAL | T_ATOM);
1221 rvalue_unchecked (x) = n; 1287 set_rvalue (x, n);
1222 set_num_real (x); 1288
1223 return x; 1289 return x;
1290#else
1291 return mk_integer (SCHEME_A_ n);
1292#endif
1224} 1293}
1225 1294
1226static pointer 1295static pointer
1227mk_number (SCHEME_P_ const num n) 1296mk_number (SCHEME_P_ const num n)
1228{ 1297{
1298#if USE_REAL
1229 if (num_is_fixnum (n)) 1299 return num_is_fixnum (n)
1300 ? mk_integer (SCHEME_A_ num_ivalue (n))
1301 : mk_real (SCHEME_A_ num_rvalue (n));
1302#else
1230 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1303 return mk_integer (SCHEME_A_ num_ivalue (n));
1231 else 1304#endif
1232 return mk_real (SCHEME_A_ num_get_rvalue (n));
1233} 1305}
1234 1306
1235/* allocate name to string area */ 1307/* allocate name to string area */
1236static char * 1308static char *
1237store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1309store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1243 SCHEME_V->no_memory = 1; 1315 SCHEME_V->no_memory = 1;
1244 return SCHEME_V->strbuff; 1316 return SCHEME_V->strbuff;
1245 } 1317 }
1246 1318
1247 if (str) 1319 if (str)
1248 { 1320 memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */
1249 int l = strlen (str);
1250
1251 if (l > len_str)
1252 l = len_str;
1253
1254 memcpy (q, str, l);
1255 q[l] = 0;
1256 }
1257 else 1321 else
1258 {
1259 memset (q, fill, len_str); 1322 memset (q, fill, len_str);
1323
1260 q[len_str] = 0; 1324 q[len_str] = 0;
1261 }
1262 1325
1263 return q; 1326 return q;
1264} 1327}
1265 1328
1266INTERFACE pointer 1329INTERFACE pointer
1280 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1343 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1281 1344
1282 set_typeflag (x, T_STRING | T_ATOM); 1345 set_typeflag (x, T_STRING | T_ATOM);
1283 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1346 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1284 strlength (x) = len; 1347 strlength (x) = len;
1348
1285 return x; 1349 return x;
1286} 1350}
1287 1351
1288INTERFACE pointer 1352INTERFACE pointer
1289mk_string (SCHEME_P_ const char *str) 1353mk_string (SCHEME_P_ const char *str)
1296{ 1360{
1297 return get_vector_object (SCHEME_A_ len, NIL); 1361 return get_vector_object (SCHEME_A_ len, NIL);
1298} 1362}
1299 1363
1300INTERFACE void 1364INTERFACE void
1301fill_vector (pointer vec, pointer obj) 1365fill_vector (pointer vec, uint32_t start, pointer obj)
1302{ 1366{
1303 int i; 1367 int i;
1304 1368
1305 for (i = 0; i < vec->object.vector.length; i++) 1369 for (i = start; i < veclength (vec); i++)
1306 vecvalue (vec)[i] = obj; 1370 vecvalue (vec)[i] = obj;
1307} 1371}
1308 1372
1373INTERFACE void
1374vector_resize (pointer vec, uint32_t newsize, pointer fill)
1375{
1376 uint32_t oldsize = veclength (vec);
1377 vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer));
1378 veclength (vec) = newsize;
1379 fill_vector (vec, oldsize, fill);
1380}
1381
1309INTERFACE pointer 1382INTERFACE pointer
1310vector_elem (pointer vec, uint32_t ielem) 1383vector_get (pointer vec, uint32_t ielem)
1311{ 1384{
1312 return vecvalue(vec)[ielem]; 1385 return vecvalue(vec)[ielem];
1313} 1386}
1314 1387
1315INTERFACE void 1388INTERFACE void
1316set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1389vector_set (pointer vec, uint32_t ielem, pointer a)
1317{ 1390{
1318 vecvalue(vec)[ielem] = a; 1391 vecvalue(vec)[ielem] = a;
1319} 1392}
1320 1393
1321/* get new symbol */ 1394/* get new symbol */
1333 1406
1334INTERFACE pointer 1407INTERFACE pointer
1335gensym (SCHEME_P) 1408gensym (SCHEME_P)
1336{ 1409{
1337 pointer x; 1410 pointer x;
1338
1339 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1340 {
1341 char name[40] = "gensym-"; 1411 char name[40] = "gensym-";
1342 xnum (name + 7, SCHEME_V->gensym_cnt); 1412 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1343 1413
1344 /* first check oblist */ 1414 return generate_symbol (SCHEME_A_ name);
1345 x = oblist_find_by_name (SCHEME_A_ name); 1415}
1346 1416
1347 if (x == NIL) 1417static int
1348 { 1418is_gensym (SCHEME_P_ pointer x)
1349 x = oblist_add_by_name (SCHEME_A_ name); 1419{
1350 return x; 1420 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1351 }
1352 }
1353
1354 return NIL;
1355} 1421}
1356 1422
1357/* make symbol or number atom from string */ 1423/* make symbol or number atom from string */
1358static pointer 1424static pointer
1359mk_atom (SCHEME_P_ char *q) 1425mk_atom (SCHEME_P_ char *q)
1413 } 1479 }
1414 else if ((c == 'e') || (c == 'E')) 1480 else if ((c == 'e') || (c == 'E'))
1415 { 1481 {
1416 if (!has_fp_exp) 1482 if (!has_fp_exp)
1417 { 1483 {
1418 has_dec_point = 1; /* decimal point illegal 1484 has_dec_point = 1; /* decimal point illegal from now on */
1419 from now on */
1420 p++; 1485 p++;
1421 1486
1422 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1487 if ((*p == '-') || (*p == '+') || isdigit (*p))
1423 continue; 1488 continue;
1424 } 1489 }
1512 1577
1513 if (ecb_expect_false (is_vector (p))) 1578 if (ecb_expect_false (is_vector (p)))
1514 { 1579 {
1515 int i; 1580 int i;
1516 1581
1517 for (i = 0; i < p->object.vector.length; i++) 1582 for (i = 0; i < veclength (p); i++)
1518 mark (vecvalue (p)[i]); 1583 mark (vecvalue (p)[i]);
1519 } 1584 }
1520 1585
1521 if (is_atom (p)) 1586 if (is_atom (p))
1522 goto E6; 1587 goto E6;
1595 /* Mark recent objects the interpreter doesn't know about yet. */ 1660 /* Mark recent objects the interpreter doesn't know about yet. */
1596 mark (car (S_SINK)); 1661 mark (car (S_SINK));
1597 /* Mark any older stuff above nested C calls */ 1662 /* Mark any older stuff above nested C calls */
1598 mark (SCHEME_V->c_nest); 1663 mark (SCHEME_V->c_nest);
1599 1664
1665#if USE_INTCACHE
1666 /* mark intcache */
1667 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1668 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1669 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1670#endif
1671
1600 /* mark variables a, b */ 1672 /* mark variables a, b */
1601 mark (a); 1673 mark (a);
1602 mark (b); 1674 mark (b);
1603 1675
1604 /* garbage collect */ 1676 /* garbage collect */
1605 clrmark (NIL); 1677 clrmark (NIL);
1606 SCHEME_V->fcells = 0; 1678 SCHEME_V->fcells = 0;
1607 SCHEME_V->free_cell = NIL; 1679 SCHEME_V->free_cell = NIL;
1608 1680
1609 /* free-list is kept sorted by address so as to maintain consecutive 1681 if (SCHEME_V->gc_verbose)
1610 ranges, if possible, for use with vectors. Here we scan the cells 1682 xwrstr ("freeing...");
1611 (which are also kept sorted by address) downwards to build the 1683
1612 free-list in sorted order. 1684 uint32_t total = 0;
1613 */ 1685
1686 /* Here we scan the cells to build the free-list. */
1614 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1687 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1615 { 1688 {
1616 p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; 1689 pointer end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1690 total += SCHEME_V->cell_segsize [i];
1617 1691
1618 while (--p >= SCHEME_V->cell_seg[i]) 1692 for (p = SCHEME_V->cell_seg[i]; p < end; ++p)
1619 { 1693 {
1620 if (is_mark (p)) 1694 if (is_mark (p))
1621 clrmark (p); 1695 clrmark (p);
1622 else 1696 else
1623 { 1697 {
1624 /* reclaim cell */ 1698 /* reclaim cell */
1625 if (typeflag (p) != T_FREE) 1699 if (typeflag (p) != T_PAIR)
1626 { 1700 {
1627 finalize_cell (SCHEME_A_ p); 1701 finalize_cell (SCHEME_A_ p);
1628 set_typeflag (p, T_FREE); 1702 set_typeflag (p, T_PAIR);
1629 set_car (p, NIL); 1703 set_car (p, NIL);
1630 } 1704 }
1631 1705
1632 ++SCHEME_V->fcells; 1706 ++SCHEME_V->fcells;
1633 set_cdr (p, SCHEME_V->free_cell); 1707 set_cdr (p, SCHEME_V->free_cell);
1635 } 1709 }
1636 } 1710 }
1637 } 1711 }
1638 1712
1639 if (SCHEME_V->gc_verbose) 1713 if (SCHEME_V->gc_verbose)
1714 {
1640 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1715 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" out of "); xwrnum (total); xwrstr (" cells were recovered.\n");
1716 }
1641} 1717}
1642 1718
1643static void 1719static void
1644finalize_cell (SCHEME_P_ pointer a) 1720finalize_cell (SCHEME_P_ pointer a)
1645{ 1721{
1646 /* TODO, fast bitmap check? */ 1722 /* TODO, fast bitmap check? */
1647 if (is_string (a)) 1723 if (is_string (a) || is_symbol (a))
1648 free (strvalue (a)); 1724 free (strvalue (a));
1649 else if (is_vector (a)) 1725 else if (is_vector (a))
1650 free (vecvalue (a)); 1726 free (vecvalue (a));
1651#if USE_PORTS 1727#if USE_PORTS
1652 else if (is_port (a)) 1728 else if (is_port (a))
2074#endif 2150#endif
2075} 2151}
2076 2152
2077/* read characters up to delimiter, but cater to character constants */ 2153/* read characters up to delimiter, but cater to character constants */
2078static char * 2154static char *
2079readstr_upto (SCHEME_P_ char *delim) 2155readstr_upto (SCHEME_P_ int skip, const char *delim)
2080{ 2156{
2081 char *p = SCHEME_V->strbuff; 2157 char *p = SCHEME_V->strbuff + skip;
2082 2158
2083 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2159 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2084 2160
2085 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2161 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2086 *p = 0; 2162 *p = 0;
2093 return SCHEME_V->strbuff; 2169 return SCHEME_V->strbuff;
2094} 2170}
2095 2171
2096/* read string expression "xxx...xxx" */ 2172/* read string expression "xxx...xxx" */
2097static pointer 2173static pointer
2098readstrexp (SCHEME_P) 2174readstrexp (SCHEME_P_ char delim)
2099{ 2175{
2100 char *p = SCHEME_V->strbuff; 2176 char *p = SCHEME_V->strbuff;
2101 int c; 2177 int c;
2102 int c1 = 0; 2178 int c1 = 0;
2103 enum
2104 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2179 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2105 2180
2106 for (;;) 2181 for (;;)
2107 { 2182 {
2108 c = inchar (SCHEME_A); 2183 c = inchar (SCHEME_A);
2109 2184
2111 return S_F; 2186 return S_F;
2112 2187
2113 switch (state) 2188 switch (state)
2114 { 2189 {
2115 case st_ok: 2190 case st_ok:
2116 switch (c) 2191 if (ecb_expect_false (c == delim))
2117 {
2118 case '\\':
2119 state = st_bsl;
2120 break;
2121
2122 case '"':
2123 *p = 0;
2124 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); 2192 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2125 2193
2126 default: 2194 if (ecb_expect_false (c == '\\'))
2195 state = st_bsl;
2196 else
2127 *p++ = c; 2197 *p++ = c;
2128 break;
2129 }
2130 2198
2131 break; 2199 break;
2132 2200
2133 case st_bsl: 2201 case st_bsl:
2134 switch (c) 2202 switch (c)
2164 case 'r': 2232 case 'r':
2165 *p++ = '\r'; 2233 *p++ = '\r';
2166 state = st_ok; 2234 state = st_ok;
2167 break; 2235 break;
2168 2236
2169 case '"':
2170 *p++ = '"';
2171 state = st_ok;
2172 break;
2173
2174 default: 2237 default:
2175 *p++ = c; 2238 *p++ = c;
2176 state = st_ok; 2239 state = st_ok;
2177 break; 2240 break;
2178 } 2241 }
2179 2242
2180 break; 2243 break;
2181 2244
2182 case st_x1: 2245 case st_x1:
2183 case st_x2: 2246 case st_x2:
2184 c = toupper (c); 2247 c = tolower (c);
2185 2248
2186 if (c >= '0' && c <= 'F') 2249 if (c >= '0' && c <= '9')
2187 {
2188 if (c <= '9')
2189 c1 = (c1 << 4) + c - '0'; 2250 c1 = (c1 << 4) + c - '0';
2190 else 2251 else if (c >= 'a' && c <= 'f')
2191 c1 = (c1 << 4) + c - 'A' + 10; 2252 c1 = (c1 << 4) + c - 'a' + 10;
2192
2193 if (state == st_x1)
2194 state = st_x2;
2195 else
2196 {
2197 *p++ = c1;
2198 state = st_ok;
2199 }
2200 }
2201 else 2253 else
2202 return S_F; 2254 return S_F;
2255
2256 if (state == st_x1)
2257 state = st_x2;
2258 else
2259 {
2260 *p++ = c1;
2261 state = st_ok;
2262 }
2203 2263
2204 break; 2264 break;
2205 2265
2206 case st_oct1: 2266 case st_oct1:
2207 case st_oct2: 2267 case st_oct2:
2211 backchar (SCHEME_A_ c); 2271 backchar (SCHEME_A_ c);
2212 state = st_ok; 2272 state = st_ok;
2213 } 2273 }
2214 else 2274 else
2215 { 2275 {
2216 if (state == st_oct2 && c1 >= 32) 2276 if (state == st_oct2 && c1 >= ' ')
2217 return S_F; 2277 return S_F;
2218 2278
2219 c1 = (c1 << 3) + (c - '0'); 2279 c1 = (c1 << 3) + (c - '0');
2220 2280
2221 if (state == st_oct1) 2281 if (state == st_oct1)
2226 state = st_ok; 2286 state = st_ok;
2227 } 2287 }
2228 } 2288 }
2229 2289
2230 break; 2290 break;
2231
2232 } 2291 }
2233 } 2292 }
2234} 2293}
2235 2294
2236/* check c is in chars */ 2295/* check c is in chars */
2237static INLINE int 2296ecb_inline int
2238is_one_of (char *s, int c) 2297is_one_of (const char *s, int c)
2239{ 2298{
2240 if (c == EOF)
2241 return 1;
2242
2243 return !!strchr (s, c); 2299 return c == EOF || !!strchr (s, c);
2244} 2300}
2245 2301
2246/* skip white characters */ 2302/* skip white characters */
2247static INLINE int 2303ecb_inline int
2248skipspace (SCHEME_P) 2304skipspace (SCHEME_P)
2249{ 2305{
2250 int c, curr_line = 0; 2306 int c, curr_line = 0;
2251 2307
2252 do 2308 do
2253 { 2309 {
2254 c = inchar (SCHEME_A); 2310 c = inchar (SCHEME_A);
2311
2255#if SHOW_ERROR_LINE 2312#if SHOW_ERROR_LINE
2256 if (c == '\n') 2313 if (ecb_expect_false (c == '\n'))
2257 curr_line++; 2314 curr_line++;
2258#endif 2315#endif
2316
2317 if (ecb_expect_false (c == EOF))
2318 return c;
2259 } 2319 }
2260 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2320 while (is_one_of (WHITESPACE, c));
2261 2321
2262 /* record it */ 2322 /* record it */
2263#if SHOW_ERROR_LINE 2323#if SHOW_ERROR_LINE
2264 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 2324 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file)
2265 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; 2325 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line;
2266#endif 2326#endif
2267 2327
2268 if (c != EOF)
2269 {
2270 backchar (SCHEME_A_ c); 2328 backchar (SCHEME_A_ c);
2271 return 1; 2329 return 1;
2272 }
2273 else
2274 return EOF;
2275} 2330}
2276 2331
2277/* get token */ 2332/* get token */
2278static int 2333static int
2279token (SCHEME_P) 2334token (SCHEME_P)
2295 return TOK_RPAREN; 2350 return TOK_RPAREN;
2296 2351
2297 case '.': 2352 case '.':
2298 c = inchar (SCHEME_A); 2353 c = inchar (SCHEME_A);
2299 2354
2300 if (is_one_of (" \n\t", c)) 2355 if (is_one_of (WHITESPACE, c))
2301 return TOK_DOT; 2356 return TOK_DOT;
2302 else 2357 else
2303 { 2358 {
2304 //TODO: ungetc twice in a row is not supported in C
2305 backchar (SCHEME_A_ c); 2359 backchar (SCHEME_A_ c);
2306 backchar (SCHEME_A_ '.');
2307 return TOK_ATOM; 2360 return TOK_DOTATOM;
2308 } 2361 }
2362
2363 case '|':
2364 return TOK_STRATOM;
2309 2365
2310 case '\'': 2366 case '\'':
2311 return TOK_QUOTE; 2367 return TOK_QUOTE;
2312 2368
2313 case ';': 2369 case ';':
2445 } 2501 }
2446 2502
2447 putcharacter (SCHEME_A_ '"'); 2503 putcharacter (SCHEME_A_ '"');
2448} 2504}
2449 2505
2450
2451/* print atoms */ 2506/* print atoms */
2452static void 2507static void
2453printatom (SCHEME_P_ pointer l, int f) 2508printatom (SCHEME_P_ pointer l, int f)
2454{ 2509{
2455 char *p; 2510 char *p;
2456 int len; 2511 int len;
2457 2512
2458 atom2str (SCHEME_A_ l, f, &p, &len); 2513 atom2str (SCHEME_A_ l, f, &p, &len);
2459 putchars (SCHEME_A_ p, len); 2514 putchars (SCHEME_A_ p, len);
2460} 2515}
2461
2462 2516
2463/* Uses internal buffer unless string pointer is already available */ 2517/* Uses internal buffer unless string pointer is already available */
2464static void 2518static void
2465atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2519atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2466{ 2520{
2480 { 2534 {
2481 p = SCHEME_V->strbuff; 2535 p = SCHEME_V->strbuff;
2482 2536
2483 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2537 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2484 { 2538 {
2485 if (num_is_integer (l)) 2539 if (is_integer (l))
2486 xnum (p, ivalue_unchecked (l)); 2540 xnum (p, ivalue_unchecked (l));
2487#if USE_REAL 2541#if USE_REAL
2488 else 2542 else
2489 { 2543 {
2490 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2544 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2627#endif 2681#endif
2628 } 2682 }
2629 else if (is_continuation (l)) 2683 else if (is_continuation (l))
2630 p = "#<CONTINUATION>"; 2684 p = "#<CONTINUATION>";
2631 else 2685 else
2686 {
2687#if USE_PRINTF
2688 p = SCHEME_V->strbuff;
2689 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2690#else
2632 p = "#<ERROR>"; 2691 p = "#<ERROR>";
2692#endif
2693 }
2633 2694
2634 *pp = p; 2695 *pp = p;
2635 *plen = strlen (p); 2696 *plen = strlen (p);
2636} 2697}
2637 2698
2745 return 0; 2806 return 0;
2746 } 2807 }
2747 else if (is_number (a)) 2808 else if (is_number (a))
2748 { 2809 {
2749 if (is_number (b)) 2810 if (is_number (b))
2750 if (num_is_integer (a) == num_is_integer (b))
2751 return num_cmp (nvalue (a), nvalue (b)) == 0; 2811 return num_cmp (nvalue (a), nvalue (b)) == 0;
2752 2812
2753 return 0; 2813 return 0;
2754 } 2814 }
2755 else if (is_character (a)) 2815 else if (is_character (a))
2756 { 2816 {
2782/* () is #t in R5RS */ 2842/* () is #t in R5RS */
2783#define is_true(p) ((p) != S_F) 2843#define is_true(p) ((p) != S_F)
2784#define is_false(p) ((p) == S_F) 2844#define is_false(p) ((p) == S_F)
2785 2845
2786/* ========== Environment implementation ========== */ 2846/* ========== Environment implementation ========== */
2787
2788#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
2789
2790static int
2791hash_fn (const char *key, int table_size)
2792{
2793 const unsigned char *p = key;
2794 uint32_t hash = 2166136261;
2795
2796 while (*p)
2797 hash = (hash ^ *p++) * 16777619;
2798
2799 return hash % table_size;
2800}
2801#endif
2802 2847
2803#ifndef USE_ALIST_ENV 2848#ifndef USE_ALIST_ENV
2804 2849
2805/* 2850/*
2806 * In this implementation, each frame of the environment may be 2851 * In this implementation, each frame of the environment may be
2823 2868
2824 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2869 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2825 setenvironment (SCHEME_V->envir); 2870 setenvironment (SCHEME_V->envir);
2826} 2871}
2827 2872
2828static INLINE void 2873static uint32_t
2874sym_hash (pointer sym, uint32_t size)
2875{
2876 uintptr_t ptr = (uintptr_t)sym;
2877
2878#if 0
2879 /* table size is prime, so why mix */
2880 ptr += ptr >> 32;
2881 ptr += ptr >> 16;
2882 ptr += ptr >> 8;
2883#endif
2884
2885 return ptr % size;
2886}
2887
2888ecb_inline void
2829new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2889new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2830{ 2890{
2831 pointer slot = immutable_cons (variable, value); 2891 pointer slot = immutable_cons (variable, value);
2832 2892
2833 if (is_vector (car (env))) 2893 if (is_vector (car (env)))
2834 { 2894 {
2835 int location = hash_fn (symname (variable), veclength (car (env))); 2895 int location = sym_hash (variable, veclength (car (env)));
2836
2837 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2896 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2838 } 2897 }
2839 else 2898 else
2840 set_car (env, immutable_cons (slot, car (env))); 2899 set_car (env, immutable_cons (slot, car (env)));
2841} 2900}
2842 2901
2843static pointer 2902static pointer
2844find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2903find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2845{ 2904{
2846 pointer x, y; 2905 pointer x, y;
2847 int location;
2848 2906
2849 for (x = env; x != NIL; x = cdr (x)) 2907 for (x = env; x != NIL; x = cdr (x))
2850 { 2908 {
2851 if (is_vector (car (x))) 2909 if (is_vector (car (x)))
2852 { 2910 {
2853 location = hash_fn (symname (hdl), veclength (car (x))); 2911 int location = sym_hash (hdl, veclength (car (x)));
2854 y = vector_elem (car (x), location); 2912 y = vector_get (car (x), location);
2855 } 2913 }
2856 else 2914 else
2857 y = car (x); 2915 y = car (x);
2858 2916
2859 for (; y != NIL; y = cdr (y)) 2917 for (; y != NIL; y = cdr (y))
2860 if (caar (y) == hdl) 2918 if (caar (y) == hdl)
2861 break; 2919 break;
2862 2920
2863 if (y != NIL) 2921 if (y != NIL)
2922 return car (y);
2923
2924 if (!all)
2864 break; 2925 break;
2865
2866 if (!all)
2867 return NIL;
2868 } 2926 }
2869
2870 if (x != NIL)
2871 return car (y);
2872 2927
2873 return NIL; 2928 return NIL;
2874} 2929}
2875 2930
2876#else /* USE_ALIST_ENV */ 2931#else /* USE_ALIST_ENV */
2877 2932
2878static INLINE void 2933ecb_inline void
2879new_frame_in_env (SCHEME_P_ pointer old_env) 2934new_frame_in_env (SCHEME_P_ pointer old_env)
2880{ 2935{
2881 SCHEME_V->envir = immutable_cons (NIL, old_env); 2936 SCHEME_V->envir = immutable_cons (NIL, old_env);
2882 setenvironment (SCHEME_V->envir); 2937 setenvironment (SCHEME_V->envir);
2883} 2938}
2884 2939
2885static INLINE void 2940ecb_inline void
2886new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2941new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2887{ 2942{
2888 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2943 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2889} 2944}
2890 2945
2898 for (y = car (x); y != NIL; y = cdr (y)) 2953 for (y = car (x); y != NIL; y = cdr (y))
2899 if (caar (y) == hdl) 2954 if (caar (y) == hdl)
2900 break; 2955 break;
2901 2956
2902 if (y != NIL) 2957 if (y != NIL)
2958 return car (y);
2903 break; 2959 break;
2904 2960
2905 if (!all) 2961 if (!all)
2906 return NIL; 2962 break;
2907 } 2963 }
2908
2909 if (x != NIL)
2910 return car (y);
2911 2964
2912 return NIL; 2965 return NIL;
2913} 2966}
2914 2967
2915#endif /* USE_ALIST_ENV else */ 2968#endif /* USE_ALIST_ENV else */
2916 2969
2917static INLINE void 2970ecb_inline void
2918new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2971new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2919{ 2972{
2973 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2920 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2974 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2921} 2975}
2922 2976
2923static INLINE void 2977ecb_inline void
2924set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2978set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2925{ 2979{
2926 set_cdr (slot, value); 2980 set_cdr (slot, value);
2927} 2981}
2928 2982
2929static INLINE pointer 2983ecb_inline pointer
2930slot_value_in_env (pointer slot) 2984slot_value_in_env (pointer slot)
2931{ 2985{
2932 return cdr (slot); 2986 return cdr (slot);
2933} 2987}
2934 2988
3062 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3116 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3063 3117
3064 return 0; 3118 return 0;
3065} 3119}
3066 3120
3067static INLINE void 3121ecb_inline void
3068dump_stack_reset (SCHEME_P) 3122dump_stack_reset (SCHEME_P)
3069{ 3123{
3070 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3124 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3071 SCHEME_V->dump = (pointer)+0; 3125 SCHEME_V->dump = (pointer)+0;
3072} 3126}
3073 3127
3074static INLINE void 3128ecb_inline void
3075dump_stack_initialize (SCHEME_P) 3129dump_stack_initialize (SCHEME_P)
3076{ 3130{
3077 SCHEME_V->dump_size = 0; 3131 SCHEME_V->dump_size = 0;
3078 SCHEME_V->dump_base = 0; 3132 SCHEME_V->dump_base = 0;
3079 dump_stack_reset (SCHEME_A); 3133 dump_stack_reset (SCHEME_A);
3132 int i = 0; 3186 int i = 0;
3133 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3187 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3134 3188
3135 while (cont != NIL) 3189 while (cont != NIL)
3136 { 3190 {
3137 frame->op = ivalue (car (cont)); cont = cdr (cont); 3191 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3138 frame->args = car (cont) ; cont = cdr (cont); 3192 frame->args = car (cont) ; cont = cdr (cont);
3139 frame->envir = car (cont) ; cont = cdr (cont); 3193 frame->envir = car (cont) ; cont = cdr (cont);
3140 frame->code = car (cont) ; cont = cdr (cont); 3194 frame->code = car (cont) ; cont = cdr (cont);
3141 3195
3142 ++frame; 3196 ++frame;
3143 ++i; 3197 ++i;
3144 } 3198 }
3145 3199
3146 SCHEME_V->dump = (pointer)(uintptr_t)i; 3200 SCHEME_V->dump = (pointer)(uintptr_t)i;
3147} 3201}
3148 3202
3149#else 3203#else
3150 3204
3151static INLINE void 3205ecb_inline void
3152dump_stack_reset (SCHEME_P) 3206dump_stack_reset (SCHEME_P)
3153{ 3207{
3154 SCHEME_V->dump = NIL; 3208 SCHEME_V->dump = NIL;
3155} 3209}
3156 3210
3157static INLINE void 3211ecb_inline void
3158dump_stack_initialize (SCHEME_P) 3212dump_stack_initialize (SCHEME_P)
3159{ 3213{
3160 dump_stack_reset (SCHEME_A); 3214 dump_stack_reset (SCHEME_A);
3161} 3215}
3162 3216
3174 SCHEME_V->value = a; 3228 SCHEME_V->value = a;
3175 3229
3176 if (dump == NIL) 3230 if (dump == NIL)
3177 return -1; 3231 return -1;
3178 3232
3179 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3233 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3180 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3234 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3181 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3235 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3182 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3236 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3183 3237
3184 SCHEME_V->dump = dump; 3238 SCHEME_V->dump = dump;
3185 3239
3186 return 0; 3240 return 0;
3187} 3241}
3216 3270
3217#endif 3271#endif
3218 3272
3219#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3273#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3220 3274
3275#if EXPERIMENT
3276static int
3277debug (SCHEME_P_ int indent, pointer x)
3278{
3279 int c;
3280
3281 if (is_syntax (x))
3282 {
3283 printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x));
3284 return 8 + 8;
3285 }
3286
3287 if (x == NIL)
3288 {
3289 printf ("%*sNIL\n", indent, "");
3290 return 3;
3291 }
3292
3293 switch (type (x))
3294 {
3295 case T_INTEGER:
3296 printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x);
3297 return 32+8;
3298
3299 case T_SYMBOL:
3300 printf ("%*sS<%s>\n", indent, "", symname (x));
3301 return 24+8;
3302
3303 case T_CLOSURE:
3304 printf ("%*sS<%s>\n", indent, "", "closure");
3305 debug (SCHEME_A_ indent + 3, cdr(x));
3306 return 32 + debug (SCHEME_A_ indent + 3, car (x));
3307
3308 case T_PAIR:
3309 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3310 c = debug (SCHEME_A_ indent + 3, car (x));
3311 c += debug (SCHEME_A_ indent + 3, cdr (x));
3312 return c + 1;
3313
3314 case T_PORT:
3315 printf ("%*sS<%s>\n", indent, "", "port");
3316 return 24+8;
3317
3318 case T_VECTOR:
3319 printf ("%*sS<%s>\n", indent, "", "vector");
3320 return 24+8;
3321
3322 case T_ENVIRONMENT:
3323 printf ("%*sS<%s>\n", indent, "", "environment");
3324 return 0 + debug (SCHEME_A_ indent + 3, car (x));
3325
3326 default:
3327 printf ("unhandled type %d\n", type (x));
3328 break;
3329 }
3330}
3331#endif
3332
3221static int 3333static int
3222opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3334opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3223{ 3335{
3224 pointer args = SCHEME_V->args; 3336 pointer args = SCHEME_V->args;
3225 pointer x, y; 3337 pointer x, y;
3226 3338
3227 switch (op) 3339 switch (op)
3228 { 3340 {
3341#if EXPERIMENT //D
3342 case OP_DEBUG:
3343 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8);
3344 printf ("\n");
3345 s_return (S_T);
3346#endif
3229 case OP_LOAD: /* load */ 3347 case OP_LOAD: /* load */
3230 if (file_interactive (SCHEME_A)) 3348 if (file_interactive (SCHEME_A))
3231 { 3349 {
3232 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3350 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n");
3233 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); 3351 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args)));
3356 } 3474 }
3357 else 3475 else
3358 s_return (SCHEME_V->code); 3476 s_return (SCHEME_V->code);
3359 3477
3360 case OP_E0ARGS: /* eval arguments */ 3478 case OP_E0ARGS: /* eval arguments */
3361 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3479 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3362 { 3480 {
3363 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3481 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3364 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3482 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3365 SCHEME_V->code = SCHEME_V->value; 3483 SCHEME_V->code = SCHEME_V->value;
3366 s_goto (OP_APPLY); 3484 s_goto (OP_APPLY);
3393 3511
3394 case OP_TRACING: 3512 case OP_TRACING:
3395 { 3513 {
3396 int tr = SCHEME_V->tracing; 3514 int tr = SCHEME_V->tracing;
3397 3515
3398 SCHEME_V->tracing = ivalue (car (args)); 3516 SCHEME_V->tracing = ivalue_unchecked (car (args));
3399 s_return (mk_integer (SCHEME_A_ tr)); 3517 s_return (mk_integer (SCHEME_A_ tr));
3400 } 3518 }
3401 3519
3402#endif 3520#endif
3403 3521
3467 3585
3468 case OP_DOMACRO: /* do macro */ 3586 case OP_DOMACRO: /* do macro */
3469 SCHEME_V->code = SCHEME_V->value; 3587 SCHEME_V->code = SCHEME_V->value;
3470 s_goto (OP_EVAL); 3588 s_goto (OP_EVAL);
3471 3589
3472#if 1
3473
3474 case OP_LAMBDA: /* lambda */ 3590 case OP_LAMBDA: /* lambda */
3475 /* If the hook is defined, apply it to SCHEME_V->code, otherwise 3591 /* If the hook is defined, apply it to SCHEME_V->code, otherwise
3476 set SCHEME_V->value fall thru */ 3592 set SCHEME_V->value fall thru */
3477 { 3593 {
3478 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); 3594 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1);
3484 SCHEME_V->code = slot_value_in_env (f); 3600 SCHEME_V->code = slot_value_in_env (f);
3485 s_goto (OP_APPLY); 3601 s_goto (OP_APPLY);
3486 } 3602 }
3487 3603
3488 SCHEME_V->value = SCHEME_V->code; 3604 SCHEME_V->value = SCHEME_V->code;
3489 /* Fallthru */
3490 } 3605 }
3606 /* Fallthru */
3491 3607
3492 case OP_LAMBDA1: 3608 case OP_LAMBDA1:
3493 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); 3609 s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir));
3494
3495#else
3496
3497 case OP_LAMBDA: /* lambda */
3498 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3499
3500#endif
3501 3610
3502 case OP_MKCLOSURE: /* make-closure */ 3611 case OP_MKCLOSURE: /* make-closure */
3503 x = car (args); 3612 x = car (args);
3504 3613
3505 if (car (x) == SCHEME_V->LAMBDA) 3614 if (car (x) == SCHEME_V->LAMBDA)
3912 SCHEME_V->code = car (args); 4021 SCHEME_V->code = car (args);
3913 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 4022 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3914 s_goto (OP_APPLY); 4023 s_goto (OP_APPLY);
3915 } 4024 }
3916 4025
3917 abort (); 4026 if (USE_ERROR_CHECKING) abort ();
3918} 4027}
3919 4028
3920static int 4029static int
3921opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4030opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3922{ 4031{
3923 pointer args = SCHEME_V->args; 4032 pointer args = SCHEME_V->args;
3924 pointer x = car (args); 4033 pointer x = car (args);
3925 num v; 4034 num v;
3926 4035
3927#if USE_MATH
3928 RVALUE dd;
3929#endif
3930
3931 switch (op) 4036 switch (op)
3932 { 4037 {
3933#if USE_MATH 4038#if USE_MATH
3934 case OP_INEX2EX: /* inexact->exact */ 4039 case OP_INEX2EX: /* inexact->exact */
4040 {
3935 if (num_is_integer (x)) 4041 if (is_integer (x))
3936 s_return (x); 4042 s_return (x);
3937 else if (modf (rvalue_unchecked (x), &dd) == 0) 4043
4044 RVALUE r = rvalue_unchecked (x);
4045
4046 if (r == (RVALUE)(IVALUE)r)
3938 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4047 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3939 else 4048 else
3940 Error_1 ("inexact->exact: not integral:", x); 4049 Error_1 ("inexact->exact: not integral:", x);
4050 }
3941 4051
3942 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4052 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
3943 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 4053 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))));
3944 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4054 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
3945 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 4055 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3963 { 4073 {
3964 RVALUE result; 4074 RVALUE result;
3965 int real_result = 1; 4075 int real_result = 1;
3966 pointer y = cadr (args); 4076 pointer y = cadr (args);
3967 4077
3968 if (num_is_integer (x) && num_is_integer (y)) 4078 if (is_integer (x) && is_integer (y))
3969 real_result = 0; 4079 real_result = 0;
3970 4080
3971 /* This 'if' is an R5RS compatibility fix. */ 4081 /* This 'if' is an R5RS compatibility fix. */
3972 /* NOTE: Remove this 'if' fix for R6RS. */ 4082 /* NOTE: Remove this 'if' fix for R6RS. */
3973 if (rvalue (x) == 0 && rvalue (y) < 0) 4083 if (rvalue (x) == 0 && rvalue (y) < 0)
3979 /* If the test fails, result is too big for integer. */ 4089 /* If the test fails, result is too big for integer. */
3980 if (!real_result) 4090 if (!real_result)
3981 { 4091 {
3982 long result_as_long = result; 4092 long result_as_long = result;
3983 4093
3984 if (result != (RVALUE) result_as_long) 4094 if (result != result_as_long)
3985 real_result = 1; 4095 real_result = 1;
3986 } 4096 }
3987 4097
3988 if (real_result) 4098 if (real_result)
3989 s_return (mk_real (SCHEME_A_ result)); 4099 s_return (mk_real (SCHEME_A_ result));
3994 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4104 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
3995 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); 4105 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3996 4106
3997 case OP_TRUNCATE: 4107 case OP_TRUNCATE:
3998 { 4108 {
3999 RVALUE rvalue_of_x; 4109 RVALUE n = rvalue (x);
4000
4001 rvalue_of_x = rvalue (x);
4002
4003 if (rvalue_of_x > 0)
4004 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); 4110 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4005 else
4006 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4007 } 4111 }
4008 4112
4009 case OP_ROUND: 4113 case OP_ROUND:
4010 if (num_is_integer (x)) 4114 if (is_integer (x))
4011 s_return (x); 4115 s_return (x);
4012 4116
4013 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4117 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4014#endif 4118#endif
4015 4119
4016 case OP_ADD: /* + */ 4120 case OP_ADD: /* + */
4017 v = num_zero; 4121 v = num_zero;
4018 4122
4019 for (x = args; x != NIL; x = cdr (x)) 4123 for (x = args; x != NIL; x = cdr (x))
4020 v = num_op ('+', v, nvalue (car (x))); 4124 v = num_op (NUM_ADD, v, nvalue (car (x)));
4021 4125
4022 s_return (mk_number (SCHEME_A_ v)); 4126 s_return (mk_number (SCHEME_A_ v));
4023 4127
4024 case OP_MUL: /* * */ 4128 case OP_MUL: /* * */
4025 v = num_one; 4129 v = num_one;
4026 4130
4027 for (x = args; x != NIL; x = cdr (x)) 4131 for (x = args; x != NIL; x = cdr (x))
4028 v = num_op ('*', v, nvalue (car (x))); 4132 v = num_op (NUM_MUL, v, nvalue (car (x)));
4029 4133
4030 s_return (mk_number (SCHEME_A_ v)); 4134 s_return (mk_number (SCHEME_A_ v));
4031 4135
4032 case OP_SUB: /* - */ 4136 case OP_SUB: /* - */
4033 if (cdr (args) == NIL) 4137 if (cdr (args) == NIL)
4040 x = cdr (args); 4144 x = cdr (args);
4041 v = nvalue (car (args)); 4145 v = nvalue (car (args));
4042 } 4146 }
4043 4147
4044 for (; x != NIL; x = cdr (x)) 4148 for (; x != NIL; x = cdr (x))
4045 v = num_op ('-', v, nvalue (car (x))); 4149 v = num_op (NUM_SUB, v, nvalue (car (x)));
4046 4150
4047 s_return (mk_number (SCHEME_A_ v)); 4151 s_return (mk_number (SCHEME_A_ v));
4048 4152
4049 case OP_DIV: /* / */ 4153 case OP_DIV: /* / */
4050 if (cdr (args) == NIL) 4154 if (cdr (args) == NIL)
4057 x = cdr (args); 4161 x = cdr (args);
4058 v = nvalue (car (args)); 4162 v = nvalue (car (args));
4059 } 4163 }
4060 4164
4061 for (; x != NIL; x = cdr (x)) 4165 for (; x != NIL; x = cdr (x))
4062 {
4063 if (!is_zero_rvalue (rvalue (car (x)))) 4166 if (!is_zero_rvalue (rvalue (car (x))))
4064 v = num_div (v, nvalue (car (x))); 4167 v = num_div (v, nvalue (car (x)));
4065 else 4168 else
4066 Error_0 ("/: division by zero"); 4169 Error_0 ("/: division by zero");
4067 }
4068 4170
4069 s_return (mk_number (SCHEME_A_ v)); 4171 s_return (mk_number (SCHEME_A_ v));
4070 4172
4071 case OP_INTDIV: /* quotient */ 4173 case OP_INTDIV: /* quotient */
4072 if (cdr (args) == NIL) 4174 if (cdr (args) == NIL)
4081 } 4183 }
4082 4184
4083 for (; x != NIL; x = cdr (x)) 4185 for (; x != NIL; x = cdr (x))
4084 { 4186 {
4085 if (ivalue (car (x)) != 0) 4187 if (ivalue (car (x)) != 0)
4086 v = num_op ('/', v, nvalue (car (x))); 4188 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4087 else 4189 else
4088 Error_0 ("quotient: division by zero"); 4190 Error_0 ("quotient: division by zero");
4089 } 4191 }
4090 4192
4091 s_return (mk_number (SCHEME_A_ v)); 4193 s_return (mk_number (SCHEME_A_ v));
4108 else 4210 else
4109 Error_0 ("modulo: division by zero"); 4211 Error_0 ("modulo: division by zero");
4110 4212
4111 s_return (mk_number (SCHEME_A_ v)); 4213 s_return (mk_number (SCHEME_A_ v));
4112 4214
4113 case OP_CAR: /* car */ 4215 /* the compiler will optimize this mess... */
4114 s_return (caar (args)); 4216 case OP_CAR: op_car: s_return (car (x));
4115 4217 case OP_CDR: op_cdr: s_return (cdr (x));
4116 case OP_CDR: /* cdr */ 4218 case OP_CAAR: op_caar: x = car (x); goto op_car;
4117 s_return (cdar (args)); 4219 case OP_CADR: op_cadr: x = cdr (x); goto op_car;
4220 case OP_CDAR: op_cdar: x = car (x); goto op_cdr;
4221 case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr;
4222 case OP_CAAAR: op_caaar: x = car (x); goto op_caar;
4223 case OP_CAADR: op_caadr: x = cdr (x); goto op_caar;
4224 case OP_CADAR: op_cadar: x = car (x); goto op_cadr;
4225 case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr;
4226 case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar;
4227 case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar;
4228 case OP_CDDAR: op_cddar: x = car (x); goto op_cddr;
4229 case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr;
4230 case OP_CAAAAR: x = car (x); goto op_caaar;
4231 case OP_CAAADR: x = cdr (x); goto op_caaar;
4232 case OP_CAADAR: x = car (x); goto op_caadr;
4233 case OP_CAADDR: x = cdr (x); goto op_caadr;
4234 case OP_CADAAR: x = car (x); goto op_cadar;
4235 case OP_CADADR: x = cdr (x); goto op_cadar;
4236 case OP_CADDAR: x = car (x); goto op_caddr;
4237 case OP_CADDDR: x = cdr (x); goto op_caddr;
4238 case OP_CDAAAR: x = car (x); goto op_cdaar;
4239 case OP_CDAADR: x = cdr (x); goto op_cdaar;
4240 case OP_CDADAR: x = car (x); goto op_cdadr;
4241 case OP_CDADDR: x = cdr (x); goto op_cdadr;
4242 case OP_CDDAAR: x = car (x); goto op_cddar;
4243 case OP_CDDADR: x = cdr (x); goto op_cddar;
4244 case OP_CDDDAR: x = car (x); goto op_cdddr;
4245 case OP_CDDDDR: x = cdr (x); goto op_cdddr;
4118 4246
4119 case OP_CONS: /* cons */ 4247 case OP_CONS: /* cons */
4120 set_cdr (args, cadr (args)); 4248 set_cdr (args, cadr (args));
4121 s_return (args); 4249 s_return (args);
4122 4250
4137 } 4265 }
4138 else 4266 else
4139 Error_0 ("set-cdr!: unable to alter immutable pair"); 4267 Error_0 ("set-cdr!: unable to alter immutable pair");
4140 4268
4141 case OP_CHAR2INT: /* char->integer */ 4269 case OP_CHAR2INT: /* char->integer */
4142 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4270 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4143 4271
4144 case OP_INT2CHAR: /* integer->char */ 4272 case OP_INT2CHAR: /* integer->char */
4145 s_return (mk_character (SCHEME_A_ ivalue (x))); 4273 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4146 4274
4147 case OP_CHARUPCASE: 4275 case OP_CHARUPCASE:
4148 { 4276 {
4149 unsigned char c = ivalue (x); 4277 unsigned char c = ivalue_unchecked (x);
4150 c = toupper (c); 4278 c = toupper (c);
4151 s_return (mk_character (SCHEME_A_ c)); 4279 s_return (mk_character (SCHEME_A_ c));
4152 } 4280 }
4153 4281
4154 case OP_CHARDNCASE: 4282 case OP_CHARDNCASE:
4155 { 4283 {
4156 unsigned char c = ivalue (x); 4284 unsigned char c = ivalue_unchecked (x);
4157 c = tolower (c); 4285 c = tolower (c);
4158 s_return (mk_character (SCHEME_A_ c)); 4286 s_return (mk_character (SCHEME_A_ c));
4159 } 4287 }
4160 4288
4161 case OP_STR2SYM: /* string->symbol */ 4289 case OP_STR2SYM: /* string->symbol */
4238 Error_1 ("atom->string: not an atom:", x); 4366 Error_1 ("atom->string: not an atom:", x);
4239 } 4367 }
4240 4368
4241 case OP_MKSTRING: /* make-string */ 4369 case OP_MKSTRING: /* make-string */
4242 { 4370 {
4243 int fill = ' '; 4371 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4244 int len;
4245
4246 len = ivalue (x); 4372 int len = ivalue_unchecked (x);
4247
4248 if (cdr (args) != NIL)
4249 fill = charvalue (cadr (args));
4250 4373
4251 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4374 s_return (mk_empty_string (SCHEME_A_ len, fill));
4252 } 4375 }
4253 4376
4254 case OP_STRLEN: /* string-length */ 4377 case OP_STRLEN: /* string-length */
4255 s_return (mk_integer (SCHEME_A_ strlength (x))); 4378 s_return (mk_integer (SCHEME_A_ strlength (x)));
4256 4379
4257 case OP_STRREF: /* string-ref */ 4380 case OP_STRREF: /* string-ref */
4258 { 4381 {
4259 char *str;
4260 int index;
4261
4262 str = strvalue (x); 4382 char *str = strvalue (x);
4263
4264 index = ivalue (cadr (args)); 4383 int index = ivalue_unchecked (cadr (args));
4265 4384
4266 if (index >= strlength (x)) 4385 if (index >= strlength (x))
4267 Error_1 ("string-ref: out of bounds:", cadr (args)); 4386 Error_1 ("string-ref: out of bounds:", cadr (args));
4268 4387
4269 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4388 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4270 } 4389 }
4271 4390
4272 case OP_STRSET: /* string-set! */ 4391 case OP_STRSET: /* string-set! */
4273 { 4392 {
4274 char *str; 4393 char *str = strvalue (x);
4275 int index; 4394 int index = ivalue_unchecked (cadr (args));
4276 int c; 4395 int c;
4277 4396
4278 if (is_immutable (x)) 4397 if (is_immutable (x))
4279 Error_1 ("string-set!: unable to alter immutable string:", x); 4398 Error_1 ("string-set!: unable to alter immutable string:", x);
4280
4281 str = strvalue (x);
4282
4283 index = ivalue (cadr (args));
4284 4399
4285 if (index >= strlength (x)) 4400 if (index >= strlength (x))
4286 Error_1 ("string-set!: out of bounds:", cadr (args)); 4401 Error_1 ("string-set!: out of bounds:", cadr (args));
4287 4402
4288 c = charvalue (caddr (args)); 4403 c = charvalue (caddr (args));
4311 s_return (newstr); 4426 s_return (newstr);
4312 } 4427 }
4313 4428
4314 case OP_SUBSTR: /* substring */ 4429 case OP_SUBSTR: /* substring */
4315 { 4430 {
4316 char *str; 4431 char *str = strvalue (x);
4317 int index0; 4432 int index0 = ivalue_unchecked (cadr (args));
4318 int index1; 4433 int index1;
4319 int len; 4434 int len;
4320 4435
4321 str = strvalue (x);
4322
4323 index0 = ivalue (cadr (args));
4324
4325 if (index0 > strlength (x)) 4436 if (index0 > strlength (x))
4326 Error_1 ("substring: start out of bounds:", cadr (args)); 4437 Error_1 ("substring: start out of bounds:", cadr (args));
4327 4438
4328 if (cddr (args) != NIL) 4439 if (cddr (args) != NIL)
4329 { 4440 {
4330 index1 = ivalue (caddr (args)); 4441 index1 = ivalue_unchecked (caddr (args));
4331 4442
4332 if (index1 > strlength (x) || index1 < index0) 4443 if (index1 > strlength (x) || index1 < index0)
4333 Error_1 ("substring: end out of bounds:", caddr (args)); 4444 Error_1 ("substring: end out of bounds:", caddr (args));
4334 } 4445 }
4335 else 4446 else
4358 if (SCHEME_V->no_memory) 4469 if (SCHEME_V->no_memory)
4359 s_return (S_SINK); 4470 s_return (S_SINK);
4360#endif 4471#endif
4361 4472
4362 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4473 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4363 set_vector_elem (vec, i, car (x)); 4474 vector_set (vec, i, car (x));
4364 4475
4365 s_return (vec); 4476 s_return (vec);
4366 } 4477 }
4367 4478
4368 case OP_MKVECTOR: /* make-vector */ 4479 case OP_MKVECTOR: /* make-vector */
4369 { 4480 {
4370 pointer fill = NIL; 4481 pointer fill = NIL;
4371 int len;
4372 pointer vec; 4482 pointer vec;
4373
4374 len = ivalue (x); 4483 int len = ivalue_unchecked (x);
4375 4484
4376 if (cdr (args) != NIL) 4485 if (cdr (args) != NIL)
4377 fill = cadr (args); 4486 fill = cadr (args);
4378 4487
4379 vec = mk_vector (SCHEME_A_ len); 4488 vec = mk_vector (SCHEME_A_ len);
4382 if (SCHEME_V->no_memory) 4491 if (SCHEME_V->no_memory)
4383 s_return (S_SINK); 4492 s_return (S_SINK);
4384#endif 4493#endif
4385 4494
4386 if (fill != NIL) 4495 if (fill != NIL)
4387 fill_vector (vec, fill); 4496 fill_vector (vec, 0, fill);
4388 4497
4389 s_return (vec); 4498 s_return (vec);
4390 } 4499 }
4391 4500
4392 case OP_VECLEN: /* vector-length */ 4501 case OP_VECLEN: /* vector-length */
4393 s_return (mk_integer (SCHEME_A_ veclength (x))); 4502 s_return (mk_integer (SCHEME_A_ veclength (x)));
4394 4503
4504 case OP_VECRESIZE:
4505 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4506 s_return (x);
4507
4395 case OP_VECREF: /* vector-ref */ 4508 case OP_VECREF: /* vector-ref */
4396 { 4509 {
4397 int index;
4398
4399 index = ivalue (cadr (args)); 4510 int index = ivalue_unchecked (cadr (args));
4400 4511
4401 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4512 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4402 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4513 Error_1 ("vector-ref: out of bounds:", cadr (args));
4403 4514
4404 s_return (vector_elem (x, index)); 4515 s_return (vector_get (x, index));
4405 } 4516 }
4406 4517
4407 case OP_VECSET: /* vector-set! */ 4518 case OP_VECSET: /* vector-set! */
4408 { 4519 {
4409 int index; 4520 int index = ivalue_unchecked (cadr (args));
4410 4521
4411 if (is_immutable (x)) 4522 if (is_immutable (x))
4412 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4523 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4413 4524
4414 index = ivalue (cadr (args));
4415
4416 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4525 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4417 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4526 Error_1 ("vector-set!: out of bounds:", cadr (args));
4418 4527
4419 set_vector_elem (x, index, caddr (args)); 4528 vector_set (x, index, caddr (args));
4420 s_return (x); 4529 s_return (x);
4421 } 4530 }
4422 } 4531 }
4423 4532
4424 abort (); 4533 if (USE_ERROR_CHECKING) abort ();
4425}
4426
4427INTERFACE int
4428is_list (SCHEME_P_ pointer a)
4429{
4430 return list_length (SCHEME_A_ a) >= 0;
4431}
4432
4433/* Result is:
4434 proper list: length
4435 circular list: -1
4436 not even a pair: -2
4437 dotted list: -2 minus length before dot
4438*/
4439INTERFACE int
4440list_length (SCHEME_P_ pointer a)
4441{
4442 int i = 0;
4443 pointer slow, fast;
4444
4445 slow = fast = a;
4446
4447 while (1)
4448 {
4449 if (fast == NIL)
4450 return i;
4451
4452 if (!is_pair (fast))
4453 return -2 - i;
4454
4455 fast = cdr (fast);
4456 ++i;
4457
4458 if (fast == NIL)
4459 return i;
4460
4461 if (!is_pair (fast))
4462 return -2 - i;
4463
4464 ++i;
4465 fast = cdr (fast);
4466
4467 /* Safe because we would have already returned if `fast'
4468 encountered a non-pair. */
4469 slow = cdr (slow);
4470
4471 if (fast == slow)
4472 {
4473 /* the fast pointer has looped back around and caught up
4474 with the slow pointer, hence the structure is circular,
4475 not of finite length, and therefore not a list */
4476 return -1;
4477 }
4478 }
4479} 4534}
4480 4535
4481static int 4536static int
4482opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4537opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4483{ 4538{
4517 pointer d = cdr (args); 4572 pointer d = cdr (args);
4518 int r; 4573 int r;
4519 4574
4520 switch (op) 4575 switch (op)
4521 { 4576 {
4522 case OP_NOT: /* not */ r = is_false (a) ; break; 4577 case OP_NOT: /* not */ r = is_false (a) ; break;
4523 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; 4578 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break;
4524 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; 4579 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4525 case OP_NULLP: /* null? */ r = a == NIL ; break; 4580 case OP_NULLP: /* null? */ r = a == NIL ; break;
4526 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; 4581 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4582 case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break;
4527 case OP_NUMBERP: /* number? */ r = is_number (a) ; break; 4583 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4528 case OP_STRINGP: /* string? */ r = is_string (a) ; break; 4584 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4529 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4585 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4530 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4586 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4531 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4587 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4532 4588
4533#if USE_CHAR_CLASSIFIERS 4589#if USE_CHAR_CLASSIFIERS
4534 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4590 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4535 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4591 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4536 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4592 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4537 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4593 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4538 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4594 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4539#endif 4595#endif
4540 4596
4541#if USE_PORTS 4597#if USE_PORTS
4542 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4598 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4543 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4599 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4744 4800
4745 case OP_NEWSEGMENT: /* new-segment */ 4801 case OP_NEWSEGMENT: /* new-segment */
4746 if (!is_pair (args) || !is_number (a)) 4802 if (!is_pair (args) || !is_number (a))
4747 Error_0 ("new-segment: argument must be a number"); 4803 Error_0 ("new-segment: argument must be a number");
4748 4804
4749 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4805 alloc_cellseg (SCHEME_A_ ivalue (a));
4750 4806
4751 s_return (S_T); 4807 s_return (S_T);
4752 4808
4753 case OP_OBLIST: /* oblist */ 4809 case OP_OBLIST: /* oblist */
4754 s_return (oblist_all_symbols (SCHEME_A)); 4810 s_return (oblist_all_symbols (SCHEME_A));
4783 break; 4839 break;
4784 } 4840 }
4785 4841
4786 p = port_from_filename (SCHEME_A_ strvalue (a), prop); 4842 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4787 4843
4788 if (p == NIL) 4844 s_return (p == NIL ? S_F : p);
4789 s_return (S_F);
4790
4791 s_return (p);
4792 } 4845 }
4793 4846
4794# if USE_STRING_PORTS 4847# if USE_STRING_PORTS
4795 4848
4796 case OP_OPEN_INSTRING: /* open-input-string */ 4849 case OP_OPEN_INSTRING: /* open-input-string */
4811 } 4864 }
4812 4865
4813 p = port_from_string (SCHEME_A_ strvalue (a), 4866 p = port_from_string (SCHEME_A_ strvalue (a),
4814 strvalue (a) + strlength (a), prop); 4867 strvalue (a) + strlength (a), prop);
4815 4868
4816 if (p == NIL) 4869 s_return (p == NIL ? S_F : p);
4817 s_return (S_F);
4818
4819 s_return (p);
4820 } 4870 }
4821 4871
4822 case OP_OPEN_OUTSTRING: /* open-output-string */ 4872 case OP_OPEN_OUTSTRING: /* open-output-string */
4823 { 4873 {
4824 pointer p; 4874 pointer p;
4825 4875
4826 if (a == NIL) 4876 if (a == NIL)
4827 {
4828 p = port_from_scratch (SCHEME_A); 4877 p = port_from_scratch (SCHEME_A);
4829
4830 if (p == NIL)
4831 s_return (S_F);
4832 }
4833 else 4878 else
4834 {
4835 p = port_from_string (SCHEME_A_ strvalue (a), 4879 p = port_from_string (SCHEME_A_ strvalue (a),
4836 strvalue (a) + strlength (a), port_output); 4880 strvalue (a) + strlength (a), port_output);
4837 4881
4838 if (p == NIL) 4882 s_return (p == NIL ? S_F : p);
4839 s_return (S_F);
4840 }
4841
4842 s_return (p);
4843 } 4883 }
4844 4884
4845 case OP_GET_OUTSTRING: /* get-output-string */ 4885 case OP_GET_OUTSTRING: /* get-output-string */
4846 { 4886 {
4847 port *p; 4887 port *p;
4886 case OP_CURR_ENV: /* current-environment */ 4926 case OP_CURR_ENV: /* current-environment */
4887 s_return (SCHEME_V->envir); 4927 s_return (SCHEME_V->envir);
4888 4928
4889 } 4929 }
4890 4930
4891 abort (); 4931 if (USE_ERROR_CHECKING) abort ();
4892} 4932}
4893 4933
4894static int 4934static int
4895opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4935opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4896{ 4936{
5028 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 5068 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5029 SCHEME_V->tok = token (SCHEME_A); 5069 SCHEME_V->tok = token (SCHEME_A);
5030 s_goto (OP_RDSEXPR); 5070 s_goto (OP_RDSEXPR);
5031 5071
5032 case TOK_ATOM: 5072 case TOK_ATOM:
5033 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 5073 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
5074
5075 case TOK_DOTATOM:
5076 SCHEME_V->strbuff[0] = '.';
5077 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5078
5079 case TOK_STRATOM:
5080 x = readstrexp (SCHEME_A_ '|');
5081 //TODO: haven't checked whether the garbage collector could interfere
5082 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5034 5083
5035 case TOK_DQUOTE: 5084 case TOK_DQUOTE:
5036 x = readstrexp (SCHEME_A); 5085 x = readstrexp (SCHEME_A_ '"');
5037 5086
5038 if (x == S_F) 5087 if (x == S_F)
5039 Error_0 ("Error reading string"); 5088 Error_0 ("Error reading string");
5040 5089
5041 setimmutable (x); 5090 setimmutable (x);
5053 s_goto (OP_EVAL); 5102 s_goto (OP_EVAL);
5054 } 5103 }
5055 } 5104 }
5056 5105
5057 case TOK_SHARP_CONST: 5106 case TOK_SHARP_CONST:
5058 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 5107 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5059 Error_0 ("undefined sharp expression"); 5108 Error_0 ("undefined sharp expression");
5060 else 5109 else
5061 s_return (x); 5110 s_return (x);
5062 5111
5063 default: 5112 default:
5215 putstr (SCHEME_A_ ")"); 5264 putstr (SCHEME_A_ ")");
5216 s_return (S_T); 5265 s_return (S_T);
5217 } 5266 }
5218 else 5267 else
5219 { 5268 {
5220 pointer elem = vector_elem (vec, i); 5269 pointer elem = vector_get (vec, i);
5221 5270
5222 ivalue_unchecked (cdr (args)) = i + 1; 5271 ivalue_unchecked (cdr (args)) = i + 1;
5223 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5272 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5224 SCHEME_V->args = elem; 5273 SCHEME_V->args = elem;
5225 5274
5229 s_goto (OP_P0LIST); 5278 s_goto (OP_P0LIST);
5230 } 5279 }
5231 } 5280 }
5232 } 5281 }
5233 5282
5234 abort (); 5283 if (USE_ERROR_CHECKING) abort ();
5235} 5284}
5236 5285
5237static int 5286static int
5238opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5287opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5239{ 5288{
5285 5334
5286 case OP_CLOSUREP: /* closure? */ 5335 case OP_CLOSUREP: /* closure? */
5287 /* 5336 /*
5288 * Note, macro object is also a closure. 5337 * Note, macro object is also a closure.
5289 * Therefore, (closure? <#MACRO>) ==> #t 5338 * Therefore, (closure? <#MACRO>) ==> #t
5339 * (schmorp) well, obviously not, fix? TODO
5290 */ 5340 */
5291 s_retbool (is_closure (a)); 5341 s_retbool (is_closure (a));
5292 5342
5293 case OP_MACROP: /* macro? */ 5343 case OP_MACROP: /* macro? */
5294 s_retbool (is_macro (a)); 5344 s_retbool (is_macro (a));
5295 } 5345 }
5296 5346
5297 abort (); 5347 if (USE_ERROR_CHECKING) abort ();
5298} 5348}
5299 5349
5300/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5350/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5301typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5351typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5302 5352
5303typedef int (*test_predicate)(pointer); 5353typedef int (*test_predicate)(pointer);
5304static int 5354static int
5305is_any (pointer p) 5355tst_any (pointer p)
5306{ 5356{
5307 return 1; 5357 return 1;
5308} 5358}
5309 5359
5310static int 5360static int
5311is_nonneg (pointer p) 5361tst_inonneg (pointer p)
5312{ 5362{
5313 return ivalue (p) >= 0 && is_integer (p); 5363 return is_integer (p) && ivalue_unchecked (p) >= 0;
5314} 5364}
5315 5365
5316static int 5366static int
5317tst_is_list (pointer p) 5367tst_is_list (SCHEME_P_ pointer p)
5318{ 5368{
5319 return p == NIL || is_pair (p); 5369 return p == NIL || is_pair (p);
5320} 5370}
5321 5371
5322/* Correspond carefully with following defines! */ 5372/* Correspond carefully with following defines! */
5323static struct 5373static struct
5324{ 5374{
5325 test_predicate fct; 5375 test_predicate fct;
5326 const char *kind; 5376 const char *kind;
5327} tests[] = 5377} tests[] = {
5328{
5329 { is_any, 0 }, 5378 { tst_any , 0 },
5330 { is_string, "string" }, 5379 { is_string , "string" },
5331 { is_symbol, "symbol" }, 5380 { is_symbol , "symbol" },
5332 { is_port, "port" }, 5381 { is_port , "port" },
5333 { is_inport, "input port" }, 5382 { is_inport , "input port" },
5334 { is_outport, "output port" }, 5383 { is_outport , "output port" },
5335 { is_environment, "environment" }, 5384 { is_environment, "environment" },
5336 { is_pair, "pair" }, 5385 { is_pair , "pair" },
5337 { tst_is_list, "pair or '()" }, 5386 { 0 , "pair or '()" },
5338 { is_character, "character" }, 5387 { is_character , "character" },
5339 { is_vector, "vector" }, 5388 { is_vector , "vector" },
5340 { is_number, "number" }, 5389 { is_number , "number" },
5341 { is_integer, "integer" }, 5390 { is_integer , "integer" },
5342 { is_nonneg, "non-negative integer" } 5391 { tst_inonneg , "non-negative integer" }
5343}; 5392};
5344 5393
5345#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ 5394#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5346#define TST_ANY "\001" 5395#define TST_ANY "\001"
5347#define TST_STRING "\002" 5396#define TST_STRING "\002"
5388typedef struct 5437typedef struct
5389{ 5438{
5390 uint8_t func; 5439 uint8_t func;
5391 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ 5440 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5392 uint8_t builtin; 5441 uint8_t builtin;
5442#if USE_ERROR_CHECKING
5393 uint8_t min_arity; 5443 uint8_t min_arity;
5394 uint8_t max_arity; 5444 uint8_t max_arity;
5395 char arg_tests_encoding[3]; 5445 char arg_tests_encoding[3];
5446#endif
5396} op_code_info; 5447} op_code_info;
5397 5448
5398static const op_code_info dispatch_table[] = { 5449static const op_code_info dispatch_table[] = {
5450#if USE_ERROR_CHECKING
5399#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, 5451#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5452#else
5453#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5454#endif
5400#include "opdefines.h" 5455#include "opdefines.h"
5401#undef OP_DEF 5456#undef OP_DEF
5402 {0} 5457 {0}
5403}; 5458};
5404 5459
5405/* kernel of this interpreter */ 5460/* kernel of this interpreter */
5406static void 5461static void ecb_hot
5407Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5462Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5408{ 5463{
5409 SCHEME_V->op = op; 5464 SCHEME_V->op = op;
5410 5465
5411 for (;;) 5466 for (;;)
5446 { 5501 {
5447 pointer arg = car (arglist); 5502 pointer arg = car (arglist);
5448 5503
5449 j = t[0]; 5504 j = t[0];
5450 5505
5506 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5507 if (j == TST_LIST[0])
5508 {
5509 if (!tst_is_list (SCHEME_A_ arg))
5510 break;
5511 }
5512 else
5513 {
5451 if (!tests[j - 1].fct (arg)) 5514 if (!tests[j - 1].fct (arg))
5452 break; 5515 break;
5516 }
5453 5517
5454 if (t[1]) /* last test is replicated as necessary */ 5518 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5455 t++; 5519 t++;
5456 5520
5457 arglist = cdr (arglist); 5521 arglist = cdr (arglist);
5458 i++; 5522 i++;
5459 } 5523 }
5514mk_proc (SCHEME_P_ enum scheme_opcodes op) 5578mk_proc (SCHEME_P_ enum scheme_opcodes op)
5515{ 5579{
5516 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5580 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5517 set_typeflag (y, (T_PROC | T_ATOM)); 5581 set_typeflag (y, (T_PROC | T_ATOM));
5518 ivalue_unchecked (y) = op; 5582 ivalue_unchecked (y) = op;
5519 set_num_integer (y);
5520 return y; 5583 return y;
5521} 5584}
5522 5585
5523/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5586/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5524static int 5587static int
5525syntaxnum (pointer p) 5588syntaxnum (pointer p)
5526{ 5589{
5527 const char *s = strvalue (car (p)); 5590 const char *s = strvalue (p);
5528 5591
5529 switch (strlength (car (p))) 5592 switch (strlength (p))
5530 { 5593 {
5531 case 2: 5594 case 2:
5532 if (s[0] == 'i') 5595 if (s[0] == 'i')
5533 return OP_IF0; /* if */ 5596 return OP_IF0; /* if */
5534 else 5597 else
5589 return OP_C0STREAM; /* cons-stream */ 5652 return OP_C0STREAM; /* cons-stream */
5590 } 5653 }
5591} 5654}
5592 5655
5593#if USE_MULTIPLICITY 5656#if USE_MULTIPLICITY
5594scheme * 5657ecb_cold scheme *
5595scheme_init_new () 5658scheme_init_new ()
5596{ 5659{
5597 scheme *sc = malloc (sizeof (scheme)); 5660 scheme *sc = malloc (sizeof (scheme));
5598 5661
5599 if (!scheme_init (SCHEME_A)) 5662 if (!scheme_init (SCHEME_A))
5604 else 5667 else
5605 return sc; 5668 return sc;
5606} 5669}
5607#endif 5670#endif
5608 5671
5609int 5672ecb_cold int
5610scheme_init (SCHEME_P) 5673scheme_init (SCHEME_P)
5611{ 5674{
5612 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5675 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5613 pointer x; 5676 pointer x;
5677
5678 memset (SCHEME_V, 0, sizeof (*SCHEME_V));//TODO !iso c
5614 5679
5615 num_set_fixnum (num_zero, 1); 5680 num_set_fixnum (num_zero, 1);
5616 num_set_ivalue (num_zero, 0); 5681 num_set_ivalue (num_zero, 0);
5617 num_set_fixnum (num_one, 1); 5682 num_set_fixnum (num_one, 1);
5618 num_set_ivalue (num_one, 1); 5683 num_set_ivalue (num_one, 1);
5739scheme_set_external_data (SCHEME_P_ void *p) 5804scheme_set_external_data (SCHEME_P_ void *p)
5740{ 5805{
5741 SCHEME_V->ext_data = p; 5806 SCHEME_V->ext_data = p;
5742} 5807}
5743 5808
5744void 5809ecb_cold void
5745scheme_deinit (SCHEME_P) 5810scheme_deinit (SCHEME_P)
5746{ 5811{
5747 int i; 5812 int i;
5748 5813
5749#if SHOW_ERROR_LINE 5814#if SHOW_ERROR_LINE
5970# endif 6035# endif
5971 int fin; 6036 int fin;
5972 char *file_name = InitFile; 6037 char *file_name = InitFile;
5973 int retcode; 6038 int retcode;
5974 int isfile = 1; 6039 int isfile = 1;
6040 system ("ps v $PPID");//D
5975 6041
5976 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6042 if (argc == 2 && strcmp (argv[1], "-?") == 0)
5977 { 6043 {
5978 xwrstr ("Usage: tinyscheme -?\n"); 6044 xwrstr ("Usage: tinyscheme -?\n");
5979 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6045 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines