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.42 by root, Mon Nov 30 06:19:18 2015 UTC

28#endif 28#endif
29#if USE_MATH 29#if USE_MATH
30# include <math.h> 30# include <math.h>
31#endif 31#endif
32 32
33#include "ecb.h"
34
33#include <sys/types.h> 35#include <sys/types.h>
34#include <sys/stat.h> 36#include <sys/stat.h>
35#include <fcntl.h> 37#include <fcntl.h>
36 38
39#if !USE_ERROR_CHECKING
40# define NDEBUG
41#endif
42
43#include <assert.h>
44#include <stdlib.h>
37#include <string.h> 45#include <string.h>
38#include <stdlib.h>
39 46
40#include <limits.h> 47#include <limits.h>
41#include <inttypes.h> 48#include <inttypes.h>
42#include <float.h> 49#include <float.h>
43//#include <ctype.h> 50//#include <ctype.h>
51
52#if '1' != '0' + 1 \
53 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \
54 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \
55 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \
56 || 'f' != 'a' + 5
57# error "execution character set digits not consecutive"
58#endif
44 59
45enum { 60enum {
46 TOK_EOF, 61 TOK_EOF,
47 TOK_LPAREN, 62 TOK_LPAREN,
48 TOK_RPAREN, 63 TOK_RPAREN,
49 TOK_DOT, 64 TOK_DOT,
50 TOK_ATOM, 65 TOK_ATOM,
66 TOK_DOTATOM, /* atom name starting with '.' */
67 TOK_STRATOM, /* atom name enclosed in | */
51 TOK_QUOTE, 68 TOK_QUOTE,
52 TOK_DQUOTE, 69 TOK_DQUOTE,
53 TOK_BQUOTE, 70 TOK_BQUOTE,
54 TOK_COMMA, 71 TOK_COMMA,
55 TOK_ATMARK, 72 TOK_ATMARK,
57 TOK_SHARP_CONST, 74 TOK_SHARP_CONST,
58 TOK_VEC 75 TOK_VEC
59}; 76};
60 77
61#define BACKQUOTE '`' 78#define BACKQUOTE '`'
62#define DELIMITERS "()\";\f\t\v\n\r " 79#define WHITESPACE " \t\r\n\v\f"
80#define DELIMITERS "()\";" WHITESPACE
63 81
64#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 82#define NIL (&SCHEME_V->xNIL) //TODO: make this 0?
65#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 83#define S_T (&SCHEME_V->xT) //TODO: magic ptr value?
66#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 84#define S_F (&SCHEME_V->xF) //TODO: magic ptr value?
67#define S_SINK (&SCHEME_V->xsink) 85#define S_SINK (&SCHEME_V->xsink)
68#define S_EOF (&SCHEME_V->xEOF_OBJ) 86#define S_EOF (&SCHEME_V->xEOF_OBJ)
69 87
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 88#if !USE_MULTIPLICITY
81static scheme sc; 89static scheme sc;
82#endif 90#endif
83 91
84static void 92static void
153 161
154#define toupper(c) xtoupper (c) 162#define toupper(c) xtoupper (c)
155#define tolower(c) xtolower (c) 163#define tolower(c) xtolower (c)
156#define isdigit(c) xisdigit (c) 164#define isdigit(c) xisdigit (c)
157 165
158#if USE_STRLWR 166#if USE_IGNORECASE
159static const char * 167static const char *
160strlwr (char *s) 168xstrlwr (char *s)
161{ 169{
162 const char *p = s; 170 const char *p = s;
163 171
164 while (*s) 172 while (*s)
165 { 173 {
167 s++; 175 s++;
168 } 176 }
169 177
170 return p; 178 return p;
171} 179}
172#endif
173 180
181#define stricmp(a,b) strcasecmp (a, b)
182#define strlwr(s) xstrlwr (s)
183
184#else
174#define stricmp(a,b) strcmp (a, b) 185# define stricmp(a,b) strcmp (a, b)
175#define strlwr(s) (s) 186# define strlwr(s) (s)
187#endif
176 188
177#ifndef prompt 189#ifndef prompt
178# define prompt "ts> " 190# define prompt "ts> "
179#endif 191#endif
180 192
186# define FIRST_CELLSEGS 3 198# define FIRST_CELLSEGS 3
187#endif 199#endif
188 200
189enum scheme_types 201enum scheme_types
190{ 202{
203 T_INTEGER,
191 T_FREE, 204 T_REAL,
192 T_STRING, 205 T_STRING,
193 T_NUMBER,
194 T_SYMBOL, 206 T_SYMBOL,
195 T_PROC, 207 T_PROC,
196 T_PAIR, 208 T_PAIR, /* also used for free cells */
197 T_CLOSURE, 209 T_CLOSURE,
198 T_CONTINUATION, 210 T_CONTINUATION,
199 T_FOREIGN, 211 T_FOREIGN,
200 T_CHARACTER, 212 T_CHARACTER,
201 T_PORT, 213 T_PORT,
211#define T_SYNTAX 0x0010 223#define T_SYNTAX 0x0010
212#define T_IMMUTABLE 0x0020 224#define T_IMMUTABLE 0x0020
213#define T_ATOM 0x0040 /* only for gc */ 225#define T_ATOM 0x0040 /* only for gc */
214#define T_MARK 0x0080 /* only for gc */ 226#define T_MARK 0x0080 /* only for gc */
215 227
228/* num, for generic arithmetic */
229struct num
230{
231 IVALUE ivalue;
232#if USE_REAL
233 RVALUE rvalue;
234 char is_fixnum;
235#endif
236};
237
238#if USE_REAL
239# define num_is_fixnum(n) (n).is_fixnum
240# define num_set_fixnum(n,f) (n).is_fixnum = (f)
241# define num_ivalue(n) (n).ivalue
242# define num_rvalue(n) (n).rvalue
243# define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i)
244# define num_set_rvalue(n,r) (n).rvalue = (r)
245#else
246# define num_is_fixnum(n) 1
247# define num_set_fixnum(n,f) 0
248# define num_ivalue(n) (n).ivalue
249# define num_rvalue(n) (n).ivalue
250# define num_set_ivalue(n,i) (n).ivalue = (i)
251# define num_set_rvalue(n,r) (n).ivalue = (r)
252#endif
253
216enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; 254enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
217 255
218static num num_op (enum num_op op, num a, num b); 256static num num_op (enum num_op op, num a, num b);
219static num num_intdiv (num a, num b); 257static num num_intdiv (num a, num b);
220static num num_rem (num a, num b); 258static num num_rem (num a, num b);
223#if USE_MATH 261#if USE_MATH
224static double round_per_R5RS (double x); 262static double round_per_R5RS (double x);
225#endif 263#endif
226static int is_zero_rvalue (RVALUE x); 264static int is_zero_rvalue (RVALUE x);
227 265
228static INLINE int
229num_is_integer (pointer p)
230{
231 return num_is_fixnum (p->object.number);
232}
233
234static num num_zero; 266static num num_zero;
235static num num_one; 267static num num_one;
236 268
237/* macros for cell operations */ 269/* macros for cell operations */
238#define typeflag(p) ((p)->flag + 0) 270#define typeflag(p) ((p)->flag + 0)
239#define set_typeflag(p,v) ((p)->flag = (v)) 271#define set_typeflag(p,v) ((p)->flag = (v))
240#define type(p) (typeflag (p) & T_MASKTYPE) 272#define type(p) (typeflag (p) & T_MASKTYPE)
241 273
242INTERFACE INLINE int 274INTERFACE int
243is_string (pointer p) 275is_string (pointer p)
244{ 276{
245 return type (p) == T_STRING; 277 return type (p) == T_STRING;
246} 278}
247 279
248#define strvalue(p) ((p)->object.string.svalue) 280#define strvalue(p) ((p)->object.string.svalue)
249#define strlength(p) ((p)->object.string.length) 281#define strlength(p) ((p)->object.string.length)
250 282
251INTERFACE int is_list (SCHEME_P_ pointer p);
252
253INTERFACE INLINE int 283INTERFACE int
254is_vector (pointer p) 284is_vector (pointer p)
255{ 285{
256 return type (p) == T_VECTOR; 286 return type (p) == T_VECTOR;
257} 287}
258 288
259#define vecvalue(p) ((p)->object.vector.vvalue) 289#define vecvalue(p) ((p)->object.vector.vvalue)
260#define veclength(p) ((p)->object.vector.length) 290#define veclength(p) ((p)->object.vector.length)
261INTERFACE void fill_vector (pointer vec, pointer obj); 291INTERFACE 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); 292INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
264INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 293INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
265 294
266INTERFACE uint32_t 295INTERFACE int
267vector_length (pointer vec) 296is_integer (pointer p)
268{ 297{
269 return vec->object.vector.length; 298 return type (p) == T_INTEGER;
270} 299}
271 300
301/* not the same as in scheme, where integers are (correctly :) reals */
272INTERFACE INLINE int 302INTERFACE int
303is_real (pointer p)
304{
305 return type (p) == T_REAL;
306}
307
308INTERFACE int
273is_number (pointer p) 309is_number (pointer p)
274{ 310{
275 return type (p) == T_NUMBER; 311 return is_integer (p) || is_real (p);
276} 312}
277 313
278INTERFACE INLINE int 314INTERFACE 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) 315is_character (pointer p)
298{ 316{
299 return type (p) == T_CHARACTER; 317 return type (p) == T_CHARACTER;
300} 318}
301 319
302INTERFACE INLINE char * 320INTERFACE char *
303string_value (pointer p) 321string_value (pointer p)
304{ 322{
305 return strvalue (p); 323 return strvalue (p);
306} 324}
307 325
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) 326#define ivalue_unchecked(p) (p)->object.ivalue
327#define set_ivalue(p,v) (p)->object.ivalue = (v)
328
339#if USE_REAL 329#if USE_REAL
340# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 330#define rvalue_unchecked(p) (p)->object.rvalue
341# define set_num_integer(p) (p)->object.number.is_fixnum=1; 331#define set_rvalue(p,v) (p)->object.rvalue = (v)
342# define set_num_real(p) (p)->object.number.is_fixnum=0;
343#else 332#else
344# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 333#define rvalue_unchecked(p) (p)->object.ivalue
345# define set_num_integer(p) 0 334#define set_rvalue(p,v) (p)->object.ivalue = (v)
346# define set_num_real(p) 0
347#endif 335#endif
336
348INTERFACE long 337INTERFACE long
349charvalue (pointer p) 338charvalue (pointer p)
350{ 339{
351 return ivalue_unchecked (p); 340 return ivalue_unchecked (p);
352} 341}
353 342
354INTERFACE INLINE int 343INTERFACE int
355is_port (pointer p) 344is_port (pointer p)
356{ 345{
357 return type (p) == T_PORT; 346 return type (p) == T_PORT;
358} 347}
359 348
360INTERFACE INLINE int 349INTERFACE int
361is_inport (pointer p) 350is_inport (pointer p)
362{ 351{
363 return is_port (p) && p->object.port->kind & port_input; 352 return is_port (p) && p->object.port->kind & port_input;
364} 353}
365 354
366INTERFACE INLINE int 355INTERFACE int
367is_outport (pointer p) 356is_outport (pointer p)
368{ 357{
369 return is_port (p) && p->object.port->kind & port_output; 358 return is_port (p) && p->object.port->kind & port_output;
370} 359}
371 360
372INTERFACE INLINE int 361INTERFACE int
373is_pair (pointer p) 362is_pair (pointer p)
374{ 363{
375 return type (p) == T_PAIR; 364 return type (p) == T_PAIR;
376} 365}
377 366
409pair_cdr (pointer p) 398pair_cdr (pointer p)
410{ 399{
411 return cdr (p); 400 return cdr (p);
412} 401}
413 402
414INTERFACE INLINE int 403INTERFACE int
415is_symbol (pointer p) 404is_symbol (pointer p)
416{ 405{
417 return type (p) == T_SYMBOL; 406 return type (p) == T_SYMBOL;
418} 407}
419 408
420INTERFACE INLINE char * 409INTERFACE char *
421symname (pointer p) 410symname (pointer p)
422{ 411{
423 return strvalue (car (p)); 412 return strvalue (p);
424} 413}
425 414
426#if USE_PLIST 415#if USE_PLIST
427SCHEME_EXPORT INLINE int 416SCHEME_EXPORT int
428hasprop (pointer p) 417hasprop (pointer p)
429{ 418{
430 return typeflag (p) & T_SYMBOL; 419 return typeflag (p) & T_SYMBOL;
431} 420}
432 421
433# define symprop(p) cdr(p) 422# define symprop(p) cdr(p)
434#endif 423#endif
435 424
436INTERFACE INLINE int 425INTERFACE int
437is_syntax (pointer p) 426is_syntax (pointer p)
438{ 427{
439 return typeflag (p) & T_SYNTAX; 428 return typeflag (p) & T_SYNTAX;
440} 429}
441 430
442INTERFACE INLINE int 431INTERFACE int
443is_proc (pointer p) 432is_proc (pointer p)
444{ 433{
445 return type (p) == T_PROC; 434 return type (p) == T_PROC;
446} 435}
447 436
448INTERFACE INLINE int 437INTERFACE int
449is_foreign (pointer p) 438is_foreign (pointer p)
450{ 439{
451 return type (p) == T_FOREIGN; 440 return type (p) == T_FOREIGN;
452} 441}
453 442
454INTERFACE INLINE char * 443INTERFACE char *
455syntaxname (pointer p) 444syntaxname (pointer p)
456{ 445{
457 return strvalue (car (p)); 446 return strvalue (p);
458} 447}
459 448
460#define procnum(p) ivalue (p) 449#define procnum(p) ivalue_unchecked (p)
461static const char *procname (pointer x); 450static const char *procname (pointer x);
462 451
463INTERFACE INLINE int 452INTERFACE int
464is_closure (pointer p) 453is_closure (pointer p)
465{ 454{
466 return type (p) == T_CLOSURE; 455 return type (p) == T_CLOSURE;
467} 456}
468 457
469INTERFACE INLINE int 458INTERFACE int
470is_macro (pointer p) 459is_macro (pointer p)
471{ 460{
472 return type (p) == T_MACRO; 461 return type (p) == T_MACRO;
473} 462}
474 463
475INTERFACE INLINE pointer 464INTERFACE pointer
476closure_code (pointer p) 465closure_code (pointer p)
477{ 466{
478 return car (p); 467 return car (p);
479} 468}
480 469
481INTERFACE INLINE pointer 470INTERFACE pointer
482closure_env (pointer p) 471closure_env (pointer p)
483{ 472{
484 return cdr (p); 473 return cdr (p);
485} 474}
486 475
487INTERFACE INLINE int 476INTERFACE int
488is_continuation (pointer p) 477is_continuation (pointer p)
489{ 478{
490 return type (p) == T_CONTINUATION; 479 return type (p) == T_CONTINUATION;
491} 480}
492 481
493#define cont_dump(p) cdr (p) 482#define cont_dump(p) cdr (p)
494#define set_cont_dump(p,v) set_cdr ((p), (v)) 483#define set_cont_dump(p,v) set_cdr ((p), (v))
495 484
496/* To do: promise should be forced ONCE only */ 485/* To do: promise should be forced ONCE only */
497INTERFACE INLINE int 486INTERFACE int
498is_promise (pointer p) 487is_promise (pointer p)
499{ 488{
500 return type (p) == T_PROMISE; 489 return type (p) == T_PROMISE;
501} 490}
502 491
503INTERFACE INLINE int 492INTERFACE int
504is_environment (pointer p) 493is_environment (pointer p)
505{ 494{
506 return type (p) == T_ENVIRONMENT; 495 return type (p) == T_ENVIRONMENT;
507} 496}
508 497
514 503
515#define is_mark(p) (typeflag (p) & T_MARK) 504#define is_mark(p) (typeflag (p) & T_MARK)
516#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 505#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
517#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 506#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
518 507
519INTERFACE INLINE int 508INTERFACE int
520is_immutable (pointer p) 509is_immutable (pointer p)
521{ 510{
522 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 511 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
523} 512}
524 513
525INTERFACE INLINE void 514INTERFACE void
526setimmutable (pointer p) 515setimmutable (pointer p)
527{ 516{
528#if USE_ERROR_CHECKING 517#if USE_ERROR_CHECKING
529 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 518 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
530#endif 519#endif
531} 520}
532 521
522/* Result is:
523 proper list: length
524 circular list: -1
525 not even a pair: -2
526 dotted list: -2 minus length before dot
527*/
528INTERFACE int
529list_length (SCHEME_P_ pointer a)
530{
531 int i = 0;
532 pointer slow, fast;
533
534 slow = fast = a;
535
536 while (1)
537 {
538 if (fast == NIL)
539 return i;
540
541 if (!is_pair (fast))
542 return -2 - i;
543
544 fast = cdr (fast);
545 ++i;
546
547 if (fast == NIL)
548 return i;
549
550 if (!is_pair (fast))
551 return -2 - i;
552
553 ++i;
554 fast = cdr (fast);
555
556 /* Safe because we would have already returned if `fast'
557 encountered a non-pair. */
558 slow = cdr (slow);
559
560 if (fast == slow)
561 {
562 /* the fast pointer has looped back around and caught up
563 with the slow pointer, hence the structure is circular,
564 not of finite length, and therefore not a list */
565 return -1;
566 }
567 }
568}
569
570INTERFACE int
571is_list (SCHEME_P_ pointer a)
572{
573 return list_length (SCHEME_A_ a) >= 0;
574}
575
533#if USE_CHAR_CLASSIFIERS 576#if USE_CHAR_CLASSIFIERS
534static INLINE int 577ecb_inline int
535Cisalpha (int c) 578Cisalpha (int c)
536{ 579{
537 return isascii (c) && isalpha (c); 580 return isascii (c) && isalpha (c);
538} 581}
539 582
540static INLINE int 583ecb_inline int
541Cisdigit (int c) 584Cisdigit (int c)
542{ 585{
543 return isascii (c) && isdigit (c); 586 return isascii (c) && isdigit (c);
544} 587}
545 588
546static INLINE int 589ecb_inline int
547Cisspace (int c) 590Cisspace (int c)
548{ 591{
549 return isascii (c) && isspace (c); 592 return isascii (c) && isspace (c);
550} 593}
551 594
552static INLINE int 595ecb_inline int
553Cisupper (int c) 596Cisupper (int c)
554{ 597{
555 return isascii (c) && isupper (c); 598 return isascii (c) && isupper (c);
556} 599}
557 600
558static INLINE int 601ecb_inline int
559Cislower (int c) 602Cislower (int c)
560{ 603{
561 return isascii (c) && islower (c); 604 return isascii (c) && islower (c);
562} 605}
563#endif 606#endif
624#endif 667#endif
625 668
626static int file_push (SCHEME_P_ const char *fname); 669static int file_push (SCHEME_P_ const char *fname);
627static void file_pop (SCHEME_P); 670static void file_pop (SCHEME_P);
628static int file_interactive (SCHEME_P); 671static int file_interactive (SCHEME_P);
629static INLINE int is_one_of (char *s, int c); 672ecb_inline int is_one_of (const char *s, int c);
630static int alloc_cellseg (SCHEME_P_ int n); 673static int alloc_cellseg (SCHEME_P_ int n);
631static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 674ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
632static void finalize_cell (SCHEME_P_ pointer a); 675static void finalize_cell (SCHEME_P_ pointer a);
633static int count_consecutive_cells (pointer x, int needed); 676static int count_consecutive_cells (pointer x, int needed);
634static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 677static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
635static pointer mk_number (SCHEME_P_ const num n); 678static pointer mk_number (SCHEME_P_ const num n);
636static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 679static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
651static void mark (pointer a); 694static void mark (pointer a);
652static void gc (SCHEME_P_ pointer a, pointer b); 695static void gc (SCHEME_P_ pointer a, pointer b);
653static int basic_inchar (port *pt); 696static int basic_inchar (port *pt);
654static int inchar (SCHEME_P); 697static int inchar (SCHEME_P);
655static void backchar (SCHEME_P_ int c); 698static void backchar (SCHEME_P_ int c);
656static char *readstr_upto (SCHEME_P_ char *delim); 699static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
657static pointer readstrexp (SCHEME_P); 700static pointer readstrexp (SCHEME_P_ char delim);
658static INLINE int skipspace (SCHEME_P); 701ecb_inline int skipspace (SCHEME_P);
659static int token (SCHEME_P); 702static int token (SCHEME_P);
660static void printslashstring (SCHEME_P_ char *s, int len); 703static void printslashstring (SCHEME_P_ char *s, int len);
661static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 704static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
662static void printatom (SCHEME_P_ pointer l, int f); 705static void printatom (SCHEME_P_ pointer l, int f);
663static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 706static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
679static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 722static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
680static void assign_syntax (SCHEME_P_ const char *name); 723static void assign_syntax (SCHEME_P_ const char *name);
681static int syntaxnum (pointer p); 724static int syntaxnum (pointer p);
682static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 725static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
683 726
727static IVALUE
728ivalue (pointer x)
729{
730 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
731}
732
733static RVALUE
734rvalue (pointer x)
735{
736 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
737}
738
739INTERFACE num
740nvalue (pointer x)
741{
742 num n;
743
744 num_set_fixnum (n, is_integer (x));
745
746 if (num_is_fixnum (n))
747 num_set_ivalue (n, ivalue_unchecked (x));
748 else
749 num_set_rvalue (n, rvalue_unchecked (x));
750
751 return n;
752}
753
684static num 754static num
685num_op (enum num_op op, num a, num b) 755num_op (enum num_op op, num a, num b)
686{ 756{
687 num ret; 757 num ret;
688 758
689 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 759 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
690 760
691 if (num_is_fixnum (ret)) 761 if (num_is_fixnum (ret))
692 { 762 {
693 IVALUE av = num_get_ivalue (a);
694 IVALUE bv = num_get_ivalue (b);
695
696 switch (op) 763 switch (op)
697 { 764 {
698 case NUM_ADD: av += bv; break; 765 case NUM_ADD: a.ivalue += b.ivalue; break;
699 case NUM_SUB: av -= bv; break; 766 case NUM_SUB: a.ivalue -= b.ivalue; break;
700 case NUM_MUL: av *= bv; break; 767 case NUM_MUL: a.ivalue *= b.ivalue; break;
701 case NUM_INTDIV: av /= bv; break; 768 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
702 } 769 }
703 770
704 num_set_ivalue (ret, av); 771 num_set_ivalue (ret, a.ivalue);
705 } 772 }
773#if USE_REAL
706 else 774 else
707 { 775 {
708 RVALUE av = num_get_rvalue (a);
709 RVALUE bv = num_get_rvalue (b);
710
711 switch (op) 776 switch (op)
712 { 777 {
713 case NUM_ADD: av += bv; break; 778 case NUM_ADD: a.rvalue += b.rvalue; break;
714 case NUM_SUB: av -= bv; break; 779 case NUM_SUB: a.rvalue -= b.rvalue; break;
715 case NUM_MUL: av *= bv; break; 780 case NUM_MUL: a.rvalue *= b.rvalue; break;
716 case NUM_INTDIV: av /= bv; break; 781 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
717 } 782 }
718 783
719 num_set_rvalue (ret, av); 784 num_set_rvalue (ret, a.rvalue);
720 } 785 }
786#endif
721 787
722 return ret; 788 return ret;
723} 789}
724 790
725static num 791static num
726num_div (num a, num b) 792num_div (num a, num b)
727{ 793{
728 num ret; 794 num ret;
729 795
730 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); 796 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
731 797
732 if (num_is_fixnum (ret)) 798 if (num_is_fixnum (ret))
733 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 799 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
734 else 800 else
735 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 801 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
736 802
737 return ret; 803 return ret;
738} 804}
739 805
740static num 806static num
742{ 808{
743 num ret; 809 num ret;
744 long e1, e2, res; 810 long e1, e2, res;
745 811
746 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 812 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
747 e1 = num_get_ivalue (a); 813 e1 = num_ivalue (a);
748 e2 = num_get_ivalue (b); 814 e2 = num_ivalue (b);
749 res = e1 % e2; 815 res = e1 % e2;
750 816
751 /* remainder should have same sign as second operand */ 817 /* remainder should have same sign as second operand */
752 if (res > 0) 818 if (res > 0)
753 { 819 {
769{ 835{
770 num ret; 836 num ret;
771 long e1, e2, res; 837 long e1, e2, res;
772 838
773 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 839 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
774 e1 = num_get_ivalue (a); 840 e1 = num_ivalue (a);
775 e2 = num_get_ivalue (b); 841 e2 = num_ivalue (b);
776 res = e1 % e2; 842 res = e1 % e2;
777 843
778 /* modulo should have same sign as second operand */ 844 /* modulo should have same sign as second operand */
779 if (res * e2 < 0) 845 if (res * e2 < 0)
780 res += e2; 846 res += e2;
781 847
782 num_set_ivalue (ret, res); 848 num_set_ivalue (ret, res);
783 return ret; 849 return ret;
784} 850}
785 851
786/* this completely disrespects NaNs */ 852/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
787static int 853static int
788num_cmp (num a, num b) 854num_cmp (num a, num b)
789{ 855{
790 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 856 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
791 int ret; 857 int ret;
792 858
793 if (is_fixnum) 859 if (is_fixnum)
794 { 860 {
795 IVALUE av = num_get_ivalue (a); 861 IVALUE av = num_ivalue (a);
796 IVALUE bv = num_get_ivalue (b); 862 IVALUE bv = num_ivalue (b);
797 863
798 ret = av == bv ? 0 : av < bv ? -1 : +1; 864 ret = av == bv ? 0 : av < bv ? -1 : +1;
799 } 865 }
800 else 866 else
801 { 867 {
802 RVALUE av = num_get_rvalue (a); 868 RVALUE av = num_rvalue (a);
803 RVALUE bv = num_get_rvalue (b); 869 RVALUE bv = num_rvalue (b);
804 870
805 ret = av == bv ? 0 : av < bv ? -1 : +1; 871 ret = av == bv ? 0 : av < bv ? -1 : +1;
806 } 872 }
807 873
808 return ret; 874 return ret;
834#endif 900#endif
835 901
836static int 902static int
837is_zero_rvalue (RVALUE x) 903is_zero_rvalue (RVALUE x)
838{ 904{
905 return x == 0;
906#if 0
839#if USE_REAL 907#if USE_REAL
840 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 908 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
841#else 909#else
842 return x == 0; 910 return x == 0;
911#endif
843#endif 912#endif
844} 913}
845 914
846/* allocate new cell segment */ 915/* allocate new cell segment */
847static int 916static int
868 return k; 937 return k;
869 938
870 i = ++SCHEME_V->last_cell_seg; 939 i = ++SCHEME_V->last_cell_seg;
871 SCHEME_V->alloc_seg[i] = cp; 940 SCHEME_V->alloc_seg[i] = cp;
872 941
873 /* insert new segment in address order */
874 newp = (pointer)cp; 942 newp = (pointer)cp;
875 SCHEME_V->cell_seg[i] = newp; 943 SCHEME_V->cell_seg[i] = newp;
876 SCHEME_V->cell_segsize[i] = segsize; 944 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; 945 SCHEME_V->fcells += segsize;
893 last = newp + segsize - 1; 946 last = newp + segsize - 1;
894 947
895 for (p = newp; p <= last; p++) 948 for (p = newp; p <= last; p++)
896 { 949 {
897 set_typeflag (p, T_FREE); 950 set_typeflag (p, T_PAIR);
898 set_car (p, NIL); 951 set_car (p, NIL);
899 set_cdr (p, p + 1); 952 set_cdr (p, p + 1);
900 } 953 }
901 954
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); 955 set_cdr (last, SCHEME_V->free_cell);
906 SCHEME_V->free_cell = newp; 956 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 } 957 }
919 958
920 return n; 959 return n;
921} 960}
922 961
923/* get new cell. parameter a, b is marked by gc. */ 962/* get new cell. parameter a, b is marked by gc. */
924static INLINE pointer 963ecb_inline pointer
925get_cell_x (SCHEME_P_ pointer a, pointer b) 964get_cell_x (SCHEME_P_ pointer a, pointer b)
926{ 965{
927 if (ecb_expect_false (SCHEME_V->free_cell == NIL)) 966 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
928 { 967 {
929 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 968 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1001 /* Record it as a vector so that gc understands it. */ 1040 /* Record it as a vector so that gc understands it. */
1002 set_typeflag (v, T_VECTOR | T_ATOM); 1041 set_typeflag (v, T_VECTOR | T_ATOM);
1003 1042
1004 v->object.vector.vvalue = e; 1043 v->object.vector.vvalue = e;
1005 v->object.vector.length = len; 1044 v->object.vector.length = len;
1006 fill_vector (v, init); 1045 fill_vector (v, 0, init);
1007 push_recent_alloc (SCHEME_A_ v, NIL); 1046 push_recent_alloc (SCHEME_A_ v, NIL);
1008 1047
1009 return v; 1048 return v;
1010} 1049}
1011 1050
1012static INLINE void 1051ecb_inline void
1013ok_to_freely_gc (SCHEME_P) 1052ok_to_freely_gc (SCHEME_P)
1014{ 1053{
1015 set_car (S_SINK, NIL); 1054 set_car (S_SINK, NIL);
1016} 1055}
1017 1056
1056 return x; 1095 return x;
1057} 1096}
1058 1097
1059/* ========== oblist implementation ========== */ 1098/* ========== oblist implementation ========== */
1060 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
1061#ifndef USE_OBJECT_LIST 1109#ifndef USE_OBJECT_LIST
1062 1110
1111static int
1063static int hash_fn (const char *key, int table_size); 1112hash_fn (const char *key, int table_size)
1113{
1114 const unsigned char *p = key;
1115 uint32_t hash = 2166136261;
1116
1117 while (*p)
1118 hash = (hash ^ *p++) * 16777619;
1119
1120 return hash % table_size;
1121}
1064 1122
1065static pointer 1123static pointer
1066oblist_initial_value (SCHEME_P) 1124oblist_initial_value (SCHEME_P)
1067{ 1125{
1068 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1126 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1070 1128
1071/* returns the new symbol */ 1129/* returns the new symbol */
1072static pointer 1130static pointer
1073oblist_add_by_name (SCHEME_P_ const char *name) 1131oblist_add_by_name (SCHEME_P_ const char *name)
1074{ 1132{
1075 int location; 1133 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)); 1134 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))); 1135 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1083 return x; 1136 return x;
1084} 1137}
1085 1138
1086static INLINE pointer 1139ecb_inline pointer
1087oblist_find_by_name (SCHEME_P_ const char *name) 1140oblist_find_by_name (SCHEME_P_ const char *name)
1088{ 1141{
1089 int location; 1142 int location;
1090 pointer x; 1143 pointer x;
1091 char *s; 1144 char *s;
1092 1145
1093 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1146 location = hash_fn (name, veclength (SCHEME_V->oblist));
1094 1147
1095 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1148 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1096 { 1149 {
1097 s = symname (car (x)); 1150 s = symname (car (x));
1098 1151
1099 /* case-insensitive, per R5RS section 2 */ 1152 /* case-insensitive, per R5RS section 2 */
1100 if (stricmp (name, s) == 0) 1153 if (stricmp (name, s) == 0)
1110 int i; 1163 int i;
1111 pointer x; 1164 pointer x;
1112 pointer ob_list = NIL; 1165 pointer ob_list = NIL;
1113 1166
1114 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1167 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1115 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1168 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1116 ob_list = cons (x, ob_list); 1169 ob_list = cons (x, ob_list);
1117 1170
1118 return ob_list; 1171 return ob_list;
1119} 1172}
1120 1173
1124oblist_initial_value (SCHEME_P) 1177oblist_initial_value (SCHEME_P)
1125{ 1178{
1126 return NIL; 1179 return NIL;
1127} 1180}
1128 1181
1129static INLINE pointer 1182ecb_inline pointer
1130oblist_find_by_name (SCHEME_P_ const char *name) 1183oblist_find_by_name (SCHEME_P_ const char *name)
1131{ 1184{
1132 pointer x; 1185 pointer x;
1133 char *s; 1186 char *s;
1134 1187
1146 1199
1147/* returns the new symbol */ 1200/* returns the new symbol */
1148static pointer 1201static pointer
1149oblist_add_by_name (SCHEME_P_ const char *name) 1202oblist_add_by_name (SCHEME_P_ const char *name)
1150{ 1203{
1151 pointer x; 1204 pointer x = mk_string (SCHEME_A_ name);
1152
1153 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1154 set_typeflag (x, T_SYMBOL); 1205 set_typeflag (x, T_SYMBOL);
1155 setimmutable (car (x)); 1206 setimmutable (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
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{
1207 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1258 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1208 1259
1209 set_typeflag (x, (T_NUMBER | T_ATOM)); 1260 set_typeflag (x, (T_INTEGER | T_ATOM));
1210 ivalue_unchecked (x) = num; 1261 set_ivalue (x, n);
1211 set_num_integer (x); 1262
1212 return x; 1263 return x;
1213} 1264}
1214 1265
1215INTERFACE pointer 1266INTERFACE pointer
1216mk_real (SCHEME_P_ RVALUE n) 1267mk_real (SCHEME_P_ RVALUE n)
1217{ 1268{
1269#if USE_REAL
1218 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1270 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1219 1271
1220 set_typeflag (x, (T_NUMBER | T_ATOM)); 1272 set_typeflag (x, (T_REAL | T_ATOM));
1221 rvalue_unchecked (x) = n; 1273 set_rvalue (x, n);
1222 set_num_real (x); 1274
1223 return x; 1275 return x;
1276#else
1277 return mk_integer (SCHEME_A_ n);
1278#endif
1224} 1279}
1225 1280
1226static pointer 1281static pointer
1227mk_number (SCHEME_P_ const num n) 1282mk_number (SCHEME_P_ const num n)
1228{ 1283{
1284#if USE_REAL
1229 if (num_is_fixnum (n)) 1285 return num_is_fixnum (n)
1286 ? mk_integer (SCHEME_A_ num_ivalue (n))
1287 : mk_real (SCHEME_A_ num_rvalue (n));
1288#else
1230 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1289 return mk_integer (SCHEME_A_ num_ivalue (n));
1231 else 1290#endif
1232 return mk_real (SCHEME_A_ num_get_rvalue (n));
1233} 1291}
1234 1292
1235/* allocate name to string area */ 1293/* allocate name to string area */
1236static char * 1294static char *
1237store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1295store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1243 SCHEME_V->no_memory = 1; 1301 SCHEME_V->no_memory = 1;
1244 return SCHEME_V->strbuff; 1302 return SCHEME_V->strbuff;
1245 } 1303 }
1246 1304
1247 if (str) 1305 if (str)
1248 { 1306 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 1307 else
1258 {
1259 memset (q, fill, len_str); 1308 memset (q, fill, len_str);
1309
1260 q[len_str] = 0; 1310 q[len_str] = 0;
1261 }
1262 1311
1263 return q; 1312 return q;
1264} 1313}
1265 1314
1266INTERFACE pointer 1315INTERFACE pointer
1280 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1329 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1281 1330
1282 set_typeflag (x, T_STRING | T_ATOM); 1331 set_typeflag (x, T_STRING | T_ATOM);
1283 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1332 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1284 strlength (x) = len; 1333 strlength (x) = len;
1334
1285 return x; 1335 return x;
1286} 1336}
1287 1337
1288INTERFACE pointer 1338INTERFACE pointer
1289mk_string (SCHEME_P_ const char *str) 1339mk_string (SCHEME_P_ const char *str)
1296{ 1346{
1297 return get_vector_object (SCHEME_A_ len, NIL); 1347 return get_vector_object (SCHEME_A_ len, NIL);
1298} 1348}
1299 1349
1300INTERFACE void 1350INTERFACE void
1301fill_vector (pointer vec, pointer obj) 1351fill_vector (pointer vec, uint32_t start, pointer obj)
1302{ 1352{
1303 int i; 1353 int i;
1304 1354
1305 for (i = 0; i < vec->object.vector.length; i++) 1355 for (i = start; i < veclength (vec); i++)
1306 vecvalue (vec)[i] = obj; 1356 vecvalue (vec)[i] = obj;
1307} 1357}
1308 1358
1359INTERFACE void
1360vector_resize (pointer vec, uint32_t newsize, pointer fill)
1361{
1362 uint32_t oldsize = veclength (vec);
1363 vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer));
1364 veclength (vec) = newsize;
1365 fill_vector (vec, oldsize, fill);
1366}
1367
1309INTERFACE pointer 1368INTERFACE pointer
1310vector_elem (pointer vec, uint32_t ielem) 1369vector_get (pointer vec, uint32_t ielem)
1311{ 1370{
1312 return vecvalue(vec)[ielem]; 1371 return vecvalue(vec)[ielem];
1313} 1372}
1314 1373
1315INTERFACE void 1374INTERFACE void
1316set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1375vector_set (pointer vec, uint32_t ielem, pointer a)
1317{ 1376{
1318 vecvalue(vec)[ielem] = a; 1377 vecvalue(vec)[ielem] = a;
1319} 1378}
1320 1379
1321/* get new symbol */ 1380/* get new symbol */
1333 1392
1334INTERFACE pointer 1393INTERFACE pointer
1335gensym (SCHEME_P) 1394gensym (SCHEME_P)
1336{ 1395{
1337 pointer x; 1396 pointer x;
1338
1339 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1340 {
1341 char name[40] = "gensym-"; 1397 char name[40] = "gensym-";
1342 xnum (name + 7, SCHEME_V->gensym_cnt); 1398 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1343 1399
1344 /* first check oblist */ 1400 return generate_symbol (SCHEME_A_ name);
1345 x = oblist_find_by_name (SCHEME_A_ name);
1346
1347 if (x == NIL)
1348 {
1349 x = oblist_add_by_name (SCHEME_A_ name);
1350 return x;
1351 }
1352 }
1353
1354 return NIL;
1355} 1401}
1356 1402
1357/* make symbol or number atom from string */ 1403/* make symbol or number atom from string */
1358static pointer 1404static pointer
1359mk_atom (SCHEME_P_ char *q) 1405mk_atom (SCHEME_P_ char *q)
1413 } 1459 }
1414 else if ((c == 'e') || (c == 'E')) 1460 else if ((c == 'e') || (c == 'E'))
1415 { 1461 {
1416 if (!has_fp_exp) 1462 if (!has_fp_exp)
1417 { 1463 {
1418 has_dec_point = 1; /* decimal point illegal 1464 has_dec_point = 1; /* decimal point illegal from now on */
1419 from now on */
1420 p++; 1465 p++;
1421 1466
1422 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1467 if ((*p == '-') || (*p == '+') || isdigit (*p))
1423 continue; 1468 continue;
1424 } 1469 }
1512 1557
1513 if (ecb_expect_false (is_vector (p))) 1558 if (ecb_expect_false (is_vector (p)))
1514 { 1559 {
1515 int i; 1560 int i;
1516 1561
1517 for (i = 0; i < p->object.vector.length; i++) 1562 for (i = 0; i < veclength (p); i++)
1518 mark (vecvalue (p)[i]); 1563 mark (vecvalue (p)[i]);
1519 } 1564 }
1520 1565
1521 if (is_atom (p)) 1566 if (is_atom (p))
1522 goto E6; 1567 goto E6;
1620 if (is_mark (p)) 1665 if (is_mark (p))
1621 clrmark (p); 1666 clrmark (p);
1622 else 1667 else
1623 { 1668 {
1624 /* reclaim cell */ 1669 /* reclaim cell */
1625 if (typeflag (p) != T_FREE) 1670 if (typeflag (p) != T_PAIR)
1626 { 1671 {
1627 finalize_cell (SCHEME_A_ p); 1672 finalize_cell (SCHEME_A_ p);
1628 set_typeflag (p, T_FREE); 1673 set_typeflag (p, T_PAIR);
1629 set_car (p, NIL); 1674 set_car (p, NIL);
1630 } 1675 }
1631 1676
1632 ++SCHEME_V->fcells; 1677 ++SCHEME_V->fcells;
1633 set_cdr (p, SCHEME_V->free_cell); 1678 set_cdr (p, SCHEME_V->free_cell);
1635 } 1680 }
1636 } 1681 }
1637 } 1682 }
1638 1683
1639 if (SCHEME_V->gc_verbose) 1684 if (SCHEME_V->gc_verbose)
1685 {
1640 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1686 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1687 }
1641} 1688}
1642 1689
1643static void 1690static void
1644finalize_cell (SCHEME_P_ pointer a) 1691finalize_cell (SCHEME_P_ pointer a)
1645{ 1692{
1646 /* TODO, fast bitmap check? */ 1693 /* TODO, fast bitmap check? */
1647 if (is_string (a)) 1694 if (is_string (a) || is_symbol (a))
1648 free (strvalue (a)); 1695 free (strvalue (a));
1649 else if (is_vector (a)) 1696 else if (is_vector (a))
1650 free (vecvalue (a)); 1697 free (vecvalue (a));
1651#if USE_PORTS 1698#if USE_PORTS
1652 else if (is_port (a)) 1699 else if (is_port (a))
2074#endif 2121#endif
2075} 2122}
2076 2123
2077/* read characters up to delimiter, but cater to character constants */ 2124/* read characters up to delimiter, but cater to character constants */
2078static char * 2125static char *
2079readstr_upto (SCHEME_P_ char *delim) 2126readstr_upto (SCHEME_P_ int skip, const char *delim)
2080{ 2127{
2081 char *p = SCHEME_V->strbuff; 2128 char *p = SCHEME_V->strbuff + skip;
2082 2129
2083 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2130 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2084 2131
2085 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2132 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2086 *p = 0; 2133 *p = 0;
2093 return SCHEME_V->strbuff; 2140 return SCHEME_V->strbuff;
2094} 2141}
2095 2142
2096/* read string expression "xxx...xxx" */ 2143/* read string expression "xxx...xxx" */
2097static pointer 2144static pointer
2098readstrexp (SCHEME_P) 2145readstrexp (SCHEME_P_ char delim)
2099{ 2146{
2100 char *p = SCHEME_V->strbuff; 2147 char *p = SCHEME_V->strbuff;
2101 int c; 2148 int c;
2102 int c1 = 0; 2149 int c1 = 0;
2103 enum
2104 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2150 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2105 2151
2106 for (;;) 2152 for (;;)
2107 { 2153 {
2108 c = inchar (SCHEME_A); 2154 c = inchar (SCHEME_A);
2109 2155
2111 return S_F; 2157 return S_F;
2112 2158
2113 switch (state) 2159 switch (state)
2114 { 2160 {
2115 case st_ok: 2161 case st_ok:
2116 switch (c) 2162 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); 2163 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2125 2164
2126 default: 2165 if (ecb_expect_false (c == '\\'))
2166 state = st_bsl;
2167 else
2127 *p++ = c; 2168 *p++ = c;
2128 break;
2129 }
2130 2169
2131 break; 2170 break;
2132 2171
2133 case st_bsl: 2172 case st_bsl:
2134 switch (c) 2173 switch (c)
2164 case 'r': 2203 case 'r':
2165 *p++ = '\r'; 2204 *p++ = '\r';
2166 state = st_ok; 2205 state = st_ok;
2167 break; 2206 break;
2168 2207
2169 case '"':
2170 *p++ = '"';
2171 state = st_ok;
2172 break;
2173
2174 default: 2208 default:
2175 *p++ = c; 2209 *p++ = c;
2176 state = st_ok; 2210 state = st_ok;
2177 break; 2211 break;
2178 } 2212 }
2179 2213
2180 break; 2214 break;
2181 2215
2182 case st_x1: 2216 case st_x1:
2183 case st_x2: 2217 case st_x2:
2184 c = toupper (c); 2218 c = tolower (c);
2185 2219
2186 if (c >= '0' && c <= 'F') 2220 if (c >= '0' && c <= '9')
2187 {
2188 if (c <= '9')
2189 c1 = (c1 << 4) + c - '0'; 2221 c1 = (c1 << 4) + c - '0';
2190 else 2222 else if (c >= 'a' && c <= 'f')
2191 c1 = (c1 << 4) + c - 'A' + 10; 2223 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 2224 else
2202 return S_F; 2225 return S_F;
2226
2227 if (state == st_x1)
2228 state = st_x2;
2229 else
2230 {
2231 *p++ = c1;
2232 state = st_ok;
2233 }
2203 2234
2204 break; 2235 break;
2205 2236
2206 case st_oct1: 2237 case st_oct1:
2207 case st_oct2: 2238 case st_oct2:
2211 backchar (SCHEME_A_ c); 2242 backchar (SCHEME_A_ c);
2212 state = st_ok; 2243 state = st_ok;
2213 } 2244 }
2214 else 2245 else
2215 { 2246 {
2216 if (state == st_oct2 && c1 >= 32) 2247 if (state == st_oct2 && c1 >= ' ')
2217 return S_F; 2248 return S_F;
2218 2249
2219 c1 = (c1 << 3) + (c - '0'); 2250 c1 = (c1 << 3) + (c - '0');
2220 2251
2221 if (state == st_oct1) 2252 if (state == st_oct1)
2226 state = st_ok; 2257 state = st_ok;
2227 } 2258 }
2228 } 2259 }
2229 2260
2230 break; 2261 break;
2231
2232 } 2262 }
2233 } 2263 }
2234} 2264}
2235 2265
2236/* check c is in chars */ 2266/* check c is in chars */
2237static INLINE int 2267ecb_inline int
2238is_one_of (char *s, int c) 2268is_one_of (const char *s, int c)
2239{ 2269{
2240 if (c == EOF)
2241 return 1;
2242
2243 return !!strchr (s, c); 2270 return c == EOF || !!strchr (s, c);
2244} 2271}
2245 2272
2246/* skip white characters */ 2273/* skip white characters */
2247static INLINE int 2274ecb_inline int
2248skipspace (SCHEME_P) 2275skipspace (SCHEME_P)
2249{ 2276{
2250 int c, curr_line = 0; 2277 int c, curr_line = 0;
2251 2278
2252 do 2279 do
2253 { 2280 {
2254 c = inchar (SCHEME_A); 2281 c = inchar (SCHEME_A);
2282
2255#if SHOW_ERROR_LINE 2283#if SHOW_ERROR_LINE
2256 if (c == '\n') 2284 if (ecb_expect_false (c == '\n'))
2257 curr_line++; 2285 curr_line++;
2258#endif 2286#endif
2287
2288 if (ecb_expect_false (c == EOF))
2289 return c;
2259 } 2290 }
2260 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2291 while (is_one_of (WHITESPACE, c));
2261 2292
2262 /* record it */ 2293 /* record it */
2263#if SHOW_ERROR_LINE 2294#if SHOW_ERROR_LINE
2264 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 2295 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; 2296 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line;
2266#endif 2297#endif
2267 2298
2268 if (c != EOF)
2269 {
2270 backchar (SCHEME_A_ c); 2299 backchar (SCHEME_A_ c);
2271 return 1; 2300 return 1;
2272 }
2273 else
2274 return EOF;
2275} 2301}
2276 2302
2277/* get token */ 2303/* get token */
2278static int 2304static int
2279token (SCHEME_P) 2305token (SCHEME_P)
2295 return TOK_RPAREN; 2321 return TOK_RPAREN;
2296 2322
2297 case '.': 2323 case '.':
2298 c = inchar (SCHEME_A); 2324 c = inchar (SCHEME_A);
2299 2325
2300 if (is_one_of (" \n\t", c)) 2326 if (is_one_of (WHITESPACE, c))
2301 return TOK_DOT; 2327 return TOK_DOT;
2302 else 2328 else
2303 { 2329 {
2304 //TODO: ungetc twice in a row is not supported in C
2305 backchar (SCHEME_A_ c); 2330 backchar (SCHEME_A_ c);
2306 backchar (SCHEME_A_ '.');
2307 return TOK_ATOM; 2331 return TOK_DOTATOM;
2308 } 2332 }
2333
2334 case '|':
2335 return TOK_STRATOM;
2309 2336
2310 case '\'': 2337 case '\'':
2311 return TOK_QUOTE; 2338 return TOK_QUOTE;
2312 2339
2313 case ';': 2340 case ';':
2445 } 2472 }
2446 2473
2447 putcharacter (SCHEME_A_ '"'); 2474 putcharacter (SCHEME_A_ '"');
2448} 2475}
2449 2476
2450
2451/* print atoms */ 2477/* print atoms */
2452static void 2478static void
2453printatom (SCHEME_P_ pointer l, int f) 2479printatom (SCHEME_P_ pointer l, int f)
2454{ 2480{
2455 char *p; 2481 char *p;
2456 int len; 2482 int len;
2457 2483
2458 atom2str (SCHEME_A_ l, f, &p, &len); 2484 atom2str (SCHEME_A_ l, f, &p, &len);
2459 putchars (SCHEME_A_ p, len); 2485 putchars (SCHEME_A_ p, len);
2460} 2486}
2461
2462 2487
2463/* Uses internal buffer unless string pointer is already available */ 2488/* Uses internal buffer unless string pointer is already available */
2464static void 2489static void
2465atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2490atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2466{ 2491{
2480 { 2505 {
2481 p = SCHEME_V->strbuff; 2506 p = SCHEME_V->strbuff;
2482 2507
2483 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2508 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2484 { 2509 {
2485 if (num_is_integer (l)) 2510 if (is_integer (l))
2486 xnum (p, ivalue_unchecked (l)); 2511 xnum (p, ivalue_unchecked (l));
2487#if USE_REAL 2512#if USE_REAL
2488 else 2513 else
2489 { 2514 {
2490 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2515 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2627#endif 2652#endif
2628 } 2653 }
2629 else if (is_continuation (l)) 2654 else if (is_continuation (l))
2630 p = "#<CONTINUATION>"; 2655 p = "#<CONTINUATION>";
2631 else 2656 else
2657 {
2658#if USE_PRINTF
2659 p = SCHEME_V->strbuff;
2660 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2661#else
2632 p = "#<ERROR>"; 2662 p = "#<ERROR>";
2663#endif
2664 }
2633 2665
2634 *pp = p; 2666 *pp = p;
2635 *plen = strlen (p); 2667 *plen = strlen (p);
2636} 2668}
2637 2669
2745 return 0; 2777 return 0;
2746 } 2778 }
2747 else if (is_number (a)) 2779 else if (is_number (a))
2748 { 2780 {
2749 if (is_number (b)) 2781 if (is_number (b))
2750 if (num_is_integer (a) == num_is_integer (b))
2751 return num_cmp (nvalue (a), nvalue (b)) == 0; 2782 return num_cmp (nvalue (a), nvalue (b)) == 0;
2752 2783
2753 return 0; 2784 return 0;
2754 } 2785 }
2755 else if (is_character (a)) 2786 else if (is_character (a))
2756 { 2787 {
2782/* () is #t in R5RS */ 2813/* () is #t in R5RS */
2783#define is_true(p) ((p) != S_F) 2814#define is_true(p) ((p) != S_F)
2784#define is_false(p) ((p) == S_F) 2815#define is_false(p) ((p) == S_F)
2785 2816
2786/* ========== Environment implementation ========== */ 2817/* ========== 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 2818
2803#ifndef USE_ALIST_ENV 2819#ifndef USE_ALIST_ENV
2804 2820
2805/* 2821/*
2806 * In this implementation, each frame of the environment may be 2822 * In this implementation, each frame of the environment may be
2823 2839
2824 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2840 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2825 setenvironment (SCHEME_V->envir); 2841 setenvironment (SCHEME_V->envir);
2826} 2842}
2827 2843
2828static INLINE void 2844static uint32_t
2845sym_hash (pointer sym, uint32_t size)
2846{
2847 uintptr_t ptr = (uintptr_t)sym;
2848
2849#if 0
2850 /* table size is prime, so why mix */
2851 ptr += ptr >> 32;
2852 ptr += ptr >> 16;
2853 ptr += ptr >> 8;
2854#endif
2855
2856 return ptr % size;
2857}
2858
2859ecb_inline void
2829new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2860new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2830{ 2861{
2831 pointer slot = immutable_cons (variable, value); 2862 pointer slot = immutable_cons (variable, value);
2832 2863
2833 if (is_vector (car (env))) 2864 if (is_vector (car (env)))
2834 { 2865 {
2835 int location = hash_fn (symname (variable), veclength (car (env))); 2866 int location = sym_hash (variable, veclength (car (env)));
2836
2837 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2867 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2838 } 2868 }
2839 else 2869 else
2840 set_car (env, immutable_cons (slot, car (env))); 2870 set_car (env, immutable_cons (slot, car (env)));
2841} 2871}
2842 2872
2843static pointer 2873static pointer
2844find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2874find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2845{ 2875{
2846 pointer x, y; 2876 pointer x, y;
2847 int location;
2848 2877
2849 for (x = env; x != NIL; x = cdr (x)) 2878 for (x = env; x != NIL; x = cdr (x))
2850 { 2879 {
2851 if (is_vector (car (x))) 2880 if (is_vector (car (x)))
2852 { 2881 {
2853 location = hash_fn (symname (hdl), veclength (car (x))); 2882 int location = sym_hash (hdl, veclength (car (x)));
2854 y = vector_elem (car (x), location); 2883 y = vector_get (car (x), location);
2855 } 2884 }
2856 else 2885 else
2857 y = car (x); 2886 y = car (x);
2858 2887
2859 for (; y != NIL; y = cdr (y)) 2888 for (; y != NIL; y = cdr (y))
2860 if (caar (y) == hdl) 2889 if (caar (y) == hdl)
2861 break; 2890 break;
2862 2891
2863 if (y != NIL) 2892 if (y != NIL)
2893 return car (y);
2894
2895 if (!all)
2864 break; 2896 break;
2865
2866 if (!all)
2867 return NIL;
2868 } 2897 }
2869
2870 if (x != NIL)
2871 return car (y);
2872 2898
2873 return NIL; 2899 return NIL;
2874} 2900}
2875 2901
2876#else /* USE_ALIST_ENV */ 2902#else /* USE_ALIST_ENV */
2877 2903
2878static INLINE void 2904ecb_inline void
2879new_frame_in_env (SCHEME_P_ pointer old_env) 2905new_frame_in_env (SCHEME_P_ pointer old_env)
2880{ 2906{
2881 SCHEME_V->envir = immutable_cons (NIL, old_env); 2907 SCHEME_V->envir = immutable_cons (NIL, old_env);
2882 setenvironment (SCHEME_V->envir); 2908 setenvironment (SCHEME_V->envir);
2883} 2909}
2884 2910
2885static INLINE void 2911ecb_inline void
2886new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2912new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2887{ 2913{
2888 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2914 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2889} 2915}
2890 2916
2898 for (y = car (x); y != NIL; y = cdr (y)) 2924 for (y = car (x); y != NIL; y = cdr (y))
2899 if (caar (y) == hdl) 2925 if (caar (y) == hdl)
2900 break; 2926 break;
2901 2927
2902 if (y != NIL) 2928 if (y != NIL)
2929 return car (y);
2903 break; 2930 break;
2904 2931
2905 if (!all) 2932 if (!all)
2906 return NIL; 2933 break;
2907 } 2934 }
2908
2909 if (x != NIL)
2910 return car (y);
2911 2935
2912 return NIL; 2936 return NIL;
2913} 2937}
2914 2938
2915#endif /* USE_ALIST_ENV else */ 2939#endif /* USE_ALIST_ENV else */
2916 2940
2917static INLINE void 2941ecb_inline void
2918new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2942new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2919{ 2943{
2944 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2920 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2945 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2921} 2946}
2922 2947
2923static INLINE void 2948ecb_inline void
2924set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2949set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2925{ 2950{
2926 set_cdr (slot, value); 2951 set_cdr (slot, value);
2927} 2952}
2928 2953
2929static INLINE pointer 2954ecb_inline pointer
2930slot_value_in_env (pointer slot) 2955slot_value_in_env (pointer slot)
2931{ 2956{
2932 return cdr (slot); 2957 return cdr (slot);
2933} 2958}
2934 2959
3062 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3087 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3063 3088
3064 return 0; 3089 return 0;
3065} 3090}
3066 3091
3067static INLINE void 3092ecb_inline void
3068dump_stack_reset (SCHEME_P) 3093dump_stack_reset (SCHEME_P)
3069{ 3094{
3070 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3095 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3071 SCHEME_V->dump = (pointer)+0; 3096 SCHEME_V->dump = (pointer)+0;
3072} 3097}
3073 3098
3074static INLINE void 3099ecb_inline void
3075dump_stack_initialize (SCHEME_P) 3100dump_stack_initialize (SCHEME_P)
3076{ 3101{
3077 SCHEME_V->dump_size = 0; 3102 SCHEME_V->dump_size = 0;
3078 SCHEME_V->dump_base = 0; 3103 SCHEME_V->dump_base = 0;
3079 dump_stack_reset (SCHEME_A); 3104 dump_stack_reset (SCHEME_A);
3132 int i = 0; 3157 int i = 0;
3133 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3158 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3134 3159
3135 while (cont != NIL) 3160 while (cont != NIL)
3136 { 3161 {
3137 frame->op = ivalue (car (cont)); cont = cdr (cont); 3162 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3138 frame->args = car (cont) ; cont = cdr (cont); 3163 frame->args = car (cont) ; cont = cdr (cont);
3139 frame->envir = car (cont) ; cont = cdr (cont); 3164 frame->envir = car (cont) ; cont = cdr (cont);
3140 frame->code = car (cont) ; cont = cdr (cont); 3165 frame->code = car (cont) ; cont = cdr (cont);
3141 3166
3142 ++frame; 3167 ++frame;
3143 ++i; 3168 ++i;
3144 } 3169 }
3145 3170
3146 SCHEME_V->dump = (pointer)(uintptr_t)i; 3171 SCHEME_V->dump = (pointer)(uintptr_t)i;
3147} 3172}
3148 3173
3149#else 3174#else
3150 3175
3151static INLINE void 3176ecb_inline void
3152dump_stack_reset (SCHEME_P) 3177dump_stack_reset (SCHEME_P)
3153{ 3178{
3154 SCHEME_V->dump = NIL; 3179 SCHEME_V->dump = NIL;
3155} 3180}
3156 3181
3157static INLINE void 3182ecb_inline void
3158dump_stack_initialize (SCHEME_P) 3183dump_stack_initialize (SCHEME_P)
3159{ 3184{
3160 dump_stack_reset (SCHEME_A); 3185 dump_stack_reset (SCHEME_A);
3161} 3186}
3162 3187
3174 SCHEME_V->value = a; 3199 SCHEME_V->value = a;
3175 3200
3176 if (dump == NIL) 3201 if (dump == NIL)
3177 return -1; 3202 return -1;
3178 3203
3179 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3204 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3180 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3205 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3181 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3206 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3182 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3207 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3183 3208
3184 SCHEME_V->dump = dump; 3209 SCHEME_V->dump = dump;
3185 3210
3186 return 0; 3211 return 0;
3187} 3212}
3216 3241
3217#endif 3242#endif
3218 3243
3219#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3244#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3220 3245
3246#if 1
3247static int
3248debug (SCHEME_P_ int indent, pointer x)
3249{
3250 int c;
3251
3252 if (is_syntax (x))
3253 {
3254 printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x));
3255 return 8 + 8;
3256 }
3257
3258 if (x == NIL)
3259 {
3260 printf ("%*sNIL\n", indent, "");
3261 return 3;
3262 }
3263
3264 switch (type (x))
3265 {
3266 case T_INTEGER:
3267 printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x);
3268 return 32+8;
3269
3270 case T_SYMBOL:
3271 printf ("%*sS<%s>\n", indent, "", symname (x));
3272 return 24+8;
3273
3274 case T_CLOSURE:
3275 printf ("%*sS<%s>\n", indent, "", "closure");
3276 debug (SCHEME_A_ indent + 3, cdr(x));
3277 return 32 + debug (SCHEME_A_ indent + 3, car (x));
3278
3279 case T_PAIR:
3280 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3281 c = debug (SCHEME_A_ indent + 3, car (x));
3282 c += debug (SCHEME_A_ indent + 3, cdr (x));
3283 return c + 1;
3284
3285 case T_PORT:
3286 printf ("%*sS<%s>\n", indent, "", "port");
3287 return 24+8;
3288
3289 case T_VECTOR:
3290 printf ("%*sS<%s>\n", indent, "", "vector");
3291 return 24+8;
3292
3293 case T_ENVIRONMENT:
3294 printf ("%*sS<%s>\n", indent, "", "environment");
3295 return 0 + debug (SCHEME_A_ indent + 3, car (x));
3296
3297 default:
3298 printf ("unhandled type %d\n", type (x));
3299 break;
3300 }
3301}
3302#endif
3303
3221static int 3304static int
3222opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3305opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3223{ 3306{
3224 pointer args = SCHEME_V->args; 3307 pointer args = SCHEME_V->args;
3225 pointer x, y; 3308 pointer x, y;
3226 3309
3227 switch (op) 3310 switch (op)
3228 { 3311 {
3312#if 1 //D
3313 case OP_DEBUG:
3314 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8);
3315 printf ("\n");
3316 s_return (S_T);
3317#endif
3229 case OP_LOAD: /* load */ 3318 case OP_LOAD: /* load */
3230 if (file_interactive (SCHEME_A)) 3319 if (file_interactive (SCHEME_A))
3231 { 3320 {
3232 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3321 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))); 3322 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args)));
3356 } 3445 }
3357 else 3446 else
3358 s_return (SCHEME_V->code); 3447 s_return (SCHEME_V->code);
3359 3448
3360 case OP_E0ARGS: /* eval arguments */ 3449 case OP_E0ARGS: /* eval arguments */
3361 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3450 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3362 { 3451 {
3363 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3452 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3364 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3453 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3365 SCHEME_V->code = SCHEME_V->value; 3454 SCHEME_V->code = SCHEME_V->value;
3366 s_goto (OP_APPLY); 3455 s_goto (OP_APPLY);
3393 3482
3394 case OP_TRACING: 3483 case OP_TRACING:
3395 { 3484 {
3396 int tr = SCHEME_V->tracing; 3485 int tr = SCHEME_V->tracing;
3397 3486
3398 SCHEME_V->tracing = ivalue (car (args)); 3487 SCHEME_V->tracing = ivalue_unchecked (car (args));
3399 s_return (mk_integer (SCHEME_A_ tr)); 3488 s_return (mk_integer (SCHEME_A_ tr));
3400 } 3489 }
3401 3490
3402#endif 3491#endif
3403 3492
3912 SCHEME_V->code = car (args); 4001 SCHEME_V->code = car (args);
3913 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 4002 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3914 s_goto (OP_APPLY); 4003 s_goto (OP_APPLY);
3915 } 4004 }
3916 4005
3917 abort (); 4006 if (USE_ERROR_CHECKING) abort ();
3918} 4007}
3919 4008
3920static int 4009static int
3921opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4010opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3922{ 4011{
3923 pointer args = SCHEME_V->args; 4012 pointer args = SCHEME_V->args;
3924 pointer x = car (args); 4013 pointer x = car (args);
3925 num v; 4014 num v;
3926 4015
3927#if USE_MATH
3928 RVALUE dd;
3929#endif
3930
3931 switch (op) 4016 switch (op)
3932 { 4017 {
3933#if USE_MATH 4018#if USE_MATH
3934 case OP_INEX2EX: /* inexact->exact */ 4019 case OP_INEX2EX: /* inexact->exact */
4020 {
3935 if (num_is_integer (x)) 4021 if (is_integer (x))
3936 s_return (x); 4022 s_return (x);
3937 else if (modf (rvalue_unchecked (x), &dd) == 0) 4023
4024 RVALUE r = rvalue_unchecked (x);
4025
4026 if (r == (RVALUE)(IVALUE)r)
3938 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4027 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3939 else 4028 else
3940 Error_1 ("inexact->exact: not integral:", x); 4029 Error_1 ("inexact->exact: not integral:", x);
4030 }
3941 4031
3942 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4032 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)))); 4033 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)))); 4034 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)))); 4035 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3963 { 4053 {
3964 RVALUE result; 4054 RVALUE result;
3965 int real_result = 1; 4055 int real_result = 1;
3966 pointer y = cadr (args); 4056 pointer y = cadr (args);
3967 4057
3968 if (num_is_integer (x) && num_is_integer (y)) 4058 if (is_integer (x) && is_integer (y))
3969 real_result = 0; 4059 real_result = 0;
3970 4060
3971 /* This 'if' is an R5RS compatibility fix. */ 4061 /* This 'if' is an R5RS compatibility fix. */
3972 /* NOTE: Remove this 'if' fix for R6RS. */ 4062 /* NOTE: Remove this 'if' fix for R6RS. */
3973 if (rvalue (x) == 0 && rvalue (y) < 0) 4063 if (rvalue (x) == 0 && rvalue (y) < 0)
3979 /* If the test fails, result is too big for integer. */ 4069 /* If the test fails, result is too big for integer. */
3980 if (!real_result) 4070 if (!real_result)
3981 { 4071 {
3982 long result_as_long = result; 4072 long result_as_long = result;
3983 4073
3984 if (result != (RVALUE) result_as_long) 4074 if (result != result_as_long)
3985 real_result = 1; 4075 real_result = 1;
3986 } 4076 }
3987 4077
3988 if (real_result) 4078 if (real_result)
3989 s_return (mk_real (SCHEME_A_ result)); 4079 s_return (mk_real (SCHEME_A_ result));
3994 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4084 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)))); 4085 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3996 4086
3997 case OP_TRUNCATE: 4087 case OP_TRUNCATE:
3998 { 4088 {
3999 RVALUE rvalue_of_x; 4089 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))); 4090 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 } 4091 }
4008 4092
4009 case OP_ROUND: 4093 case OP_ROUND:
4010 if (num_is_integer (x)) 4094 if (is_integer (x))
4011 s_return (x); 4095 s_return (x);
4012 4096
4013 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4097 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4014#endif 4098#endif
4015 4099
4016 case OP_ADD: /* + */ 4100 case OP_ADD: /* + */
4017 v = num_zero; 4101 v = num_zero;
4018 4102
4019 for (x = args; x != NIL; x = cdr (x)) 4103 for (x = args; x != NIL; x = cdr (x))
4020 v = num_op ('+', v, nvalue (car (x))); 4104 v = num_op (NUM_ADD, v, nvalue (car (x)));
4021 4105
4022 s_return (mk_number (SCHEME_A_ v)); 4106 s_return (mk_number (SCHEME_A_ v));
4023 4107
4024 case OP_MUL: /* * */ 4108 case OP_MUL: /* * */
4025 v = num_one; 4109 v = num_one;
4026 4110
4027 for (x = args; x != NIL; x = cdr (x)) 4111 for (x = args; x != NIL; x = cdr (x))
4028 v = num_op ('*', v, nvalue (car (x))); 4112 v = num_op (NUM_MUL, v, nvalue (car (x)));
4029 4113
4030 s_return (mk_number (SCHEME_A_ v)); 4114 s_return (mk_number (SCHEME_A_ v));
4031 4115
4032 case OP_SUB: /* - */ 4116 case OP_SUB: /* - */
4033 if (cdr (args) == NIL) 4117 if (cdr (args) == NIL)
4040 x = cdr (args); 4124 x = cdr (args);
4041 v = nvalue (car (args)); 4125 v = nvalue (car (args));
4042 } 4126 }
4043 4127
4044 for (; x != NIL; x = cdr (x)) 4128 for (; x != NIL; x = cdr (x))
4045 v = num_op ('-', v, nvalue (car (x))); 4129 v = num_op (NUM_SUB, v, nvalue (car (x)));
4046 4130
4047 s_return (mk_number (SCHEME_A_ v)); 4131 s_return (mk_number (SCHEME_A_ v));
4048 4132
4049 case OP_DIV: /* / */ 4133 case OP_DIV: /* / */
4050 if (cdr (args) == NIL) 4134 if (cdr (args) == NIL)
4057 x = cdr (args); 4141 x = cdr (args);
4058 v = nvalue (car (args)); 4142 v = nvalue (car (args));
4059 } 4143 }
4060 4144
4061 for (; x != NIL; x = cdr (x)) 4145 for (; x != NIL; x = cdr (x))
4062 {
4063 if (!is_zero_rvalue (rvalue (car (x)))) 4146 if (!is_zero_rvalue (rvalue (car (x))))
4064 v = num_div (v, nvalue (car (x))); 4147 v = num_div (v, nvalue (car (x)));
4065 else 4148 else
4066 Error_0 ("/: division by zero"); 4149 Error_0 ("/: division by zero");
4067 }
4068 4150
4069 s_return (mk_number (SCHEME_A_ v)); 4151 s_return (mk_number (SCHEME_A_ v));
4070 4152
4071 case OP_INTDIV: /* quotient */ 4153 case OP_INTDIV: /* quotient */
4072 if (cdr (args) == NIL) 4154 if (cdr (args) == NIL)
4081 } 4163 }
4082 4164
4083 for (; x != NIL; x = cdr (x)) 4165 for (; x != NIL; x = cdr (x))
4084 { 4166 {
4085 if (ivalue (car (x)) != 0) 4167 if (ivalue (car (x)) != 0)
4086 v = num_op ('/', v, nvalue (car (x))); 4168 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4087 else 4169 else
4088 Error_0 ("quotient: division by zero"); 4170 Error_0 ("quotient: division by zero");
4089 } 4171 }
4090 4172
4091 s_return (mk_number (SCHEME_A_ v)); 4173 s_return (mk_number (SCHEME_A_ v));
4137 } 4219 }
4138 else 4220 else
4139 Error_0 ("set-cdr!: unable to alter immutable pair"); 4221 Error_0 ("set-cdr!: unable to alter immutable pair");
4140 4222
4141 case OP_CHAR2INT: /* char->integer */ 4223 case OP_CHAR2INT: /* char->integer */
4142 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4224 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4143 4225
4144 case OP_INT2CHAR: /* integer->char */ 4226 case OP_INT2CHAR: /* integer->char */
4145 s_return (mk_character (SCHEME_A_ ivalue (x))); 4227 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4146 4228
4147 case OP_CHARUPCASE: 4229 case OP_CHARUPCASE:
4148 { 4230 {
4149 unsigned char c = ivalue (x); 4231 unsigned char c = ivalue_unchecked (x);
4150 c = toupper (c); 4232 c = toupper (c);
4151 s_return (mk_character (SCHEME_A_ c)); 4233 s_return (mk_character (SCHEME_A_ c));
4152 } 4234 }
4153 4235
4154 case OP_CHARDNCASE: 4236 case OP_CHARDNCASE:
4155 { 4237 {
4156 unsigned char c = ivalue (x); 4238 unsigned char c = ivalue_unchecked (x);
4157 c = tolower (c); 4239 c = tolower (c);
4158 s_return (mk_character (SCHEME_A_ c)); 4240 s_return (mk_character (SCHEME_A_ c));
4159 } 4241 }
4160 4242
4161 case OP_STR2SYM: /* string->symbol */ 4243 case OP_STR2SYM: /* string->symbol */
4238 Error_1 ("atom->string: not an atom:", x); 4320 Error_1 ("atom->string: not an atom:", x);
4239 } 4321 }
4240 4322
4241 case OP_MKSTRING: /* make-string */ 4323 case OP_MKSTRING: /* make-string */
4242 { 4324 {
4243 int fill = ' '; 4325 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4244 int len;
4245
4246 len = ivalue (x); 4326 int len = ivalue_unchecked (x);
4247
4248 if (cdr (args) != NIL)
4249 fill = charvalue (cadr (args));
4250 4327
4251 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4328 s_return (mk_empty_string (SCHEME_A_ len, fill));
4252 } 4329 }
4253 4330
4254 case OP_STRLEN: /* string-length */ 4331 case OP_STRLEN: /* string-length */
4255 s_return (mk_integer (SCHEME_A_ strlength (x))); 4332 s_return (mk_integer (SCHEME_A_ strlength (x)));
4256 4333
4257 case OP_STRREF: /* string-ref */ 4334 case OP_STRREF: /* string-ref */
4258 { 4335 {
4259 char *str;
4260 int index;
4261
4262 str = strvalue (x); 4336 char *str = strvalue (x);
4263
4264 index = ivalue (cadr (args)); 4337 int index = ivalue_unchecked (cadr (args));
4265 4338
4266 if (index >= strlength (x)) 4339 if (index >= strlength (x))
4267 Error_1 ("string-ref: out of bounds:", cadr (args)); 4340 Error_1 ("string-ref: out of bounds:", cadr (args));
4268 4341
4269 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4342 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4270 } 4343 }
4271 4344
4272 case OP_STRSET: /* string-set! */ 4345 case OP_STRSET: /* string-set! */
4273 { 4346 {
4274 char *str; 4347 char *str = strvalue (x);
4275 int index; 4348 int index = ivalue_unchecked (cadr (args));
4276 int c; 4349 int c;
4277 4350
4278 if (is_immutable (x)) 4351 if (is_immutable (x))
4279 Error_1 ("string-set!: unable to alter immutable string:", x); 4352 Error_1 ("string-set!: unable to alter immutable string:", x);
4280
4281 str = strvalue (x);
4282
4283 index = ivalue (cadr (args));
4284 4353
4285 if (index >= strlength (x)) 4354 if (index >= strlength (x))
4286 Error_1 ("string-set!: out of bounds:", cadr (args)); 4355 Error_1 ("string-set!: out of bounds:", cadr (args));
4287 4356
4288 c = charvalue (caddr (args)); 4357 c = charvalue (caddr (args));
4311 s_return (newstr); 4380 s_return (newstr);
4312 } 4381 }
4313 4382
4314 case OP_SUBSTR: /* substring */ 4383 case OP_SUBSTR: /* substring */
4315 { 4384 {
4316 char *str; 4385 char *str = strvalue (x);
4317 int index0; 4386 int index0 = ivalue_unchecked (cadr (args));
4318 int index1; 4387 int index1;
4319 int len; 4388 int len;
4320 4389
4321 str = strvalue (x);
4322
4323 index0 = ivalue (cadr (args));
4324
4325 if (index0 > strlength (x)) 4390 if (index0 > strlength (x))
4326 Error_1 ("substring: start out of bounds:", cadr (args)); 4391 Error_1 ("substring: start out of bounds:", cadr (args));
4327 4392
4328 if (cddr (args) != NIL) 4393 if (cddr (args) != NIL)
4329 { 4394 {
4330 index1 = ivalue (caddr (args)); 4395 index1 = ivalue_unchecked (caddr (args));
4331 4396
4332 if (index1 > strlength (x) || index1 < index0) 4397 if (index1 > strlength (x) || index1 < index0)
4333 Error_1 ("substring: end out of bounds:", caddr (args)); 4398 Error_1 ("substring: end out of bounds:", caddr (args));
4334 } 4399 }
4335 else 4400 else
4358 if (SCHEME_V->no_memory) 4423 if (SCHEME_V->no_memory)
4359 s_return (S_SINK); 4424 s_return (S_SINK);
4360#endif 4425#endif
4361 4426
4362 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4427 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4363 set_vector_elem (vec, i, car (x)); 4428 vector_set (vec, i, car (x));
4364 4429
4365 s_return (vec); 4430 s_return (vec);
4366 } 4431 }
4367 4432
4368 case OP_MKVECTOR: /* make-vector */ 4433 case OP_MKVECTOR: /* make-vector */
4369 { 4434 {
4370 pointer fill = NIL; 4435 pointer fill = NIL;
4371 int len;
4372 pointer vec; 4436 pointer vec;
4373
4374 len = ivalue (x); 4437 int len = ivalue_unchecked (x);
4375 4438
4376 if (cdr (args) != NIL) 4439 if (cdr (args) != NIL)
4377 fill = cadr (args); 4440 fill = cadr (args);
4378 4441
4379 vec = mk_vector (SCHEME_A_ len); 4442 vec = mk_vector (SCHEME_A_ len);
4382 if (SCHEME_V->no_memory) 4445 if (SCHEME_V->no_memory)
4383 s_return (S_SINK); 4446 s_return (S_SINK);
4384#endif 4447#endif
4385 4448
4386 if (fill != NIL) 4449 if (fill != NIL)
4387 fill_vector (vec, fill); 4450 fill_vector (vec, 0, fill);
4388 4451
4389 s_return (vec); 4452 s_return (vec);
4390 } 4453 }
4391 4454
4392 case OP_VECLEN: /* vector-length */ 4455 case OP_VECLEN: /* vector-length */
4393 s_return (mk_integer (SCHEME_A_ veclength (x))); 4456 s_return (mk_integer (SCHEME_A_ veclength (x)));
4394 4457
4458 case OP_VECRESIZE:
4459 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4460 s_return (x);
4461
4395 case OP_VECREF: /* vector-ref */ 4462 case OP_VECREF: /* vector-ref */
4396 { 4463 {
4397 int index;
4398
4399 index = ivalue (cadr (args)); 4464 int index = ivalue_unchecked (cadr (args));
4400 4465
4401 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4466 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4402 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4467 Error_1 ("vector-ref: out of bounds:", cadr (args));
4403 4468
4404 s_return (vector_elem (x, index)); 4469 s_return (vector_get (x, index));
4405 } 4470 }
4406 4471
4407 case OP_VECSET: /* vector-set! */ 4472 case OP_VECSET: /* vector-set! */
4408 { 4473 {
4409 int index; 4474 int index = ivalue_unchecked (cadr (args));
4410 4475
4411 if (is_immutable (x)) 4476 if (is_immutable (x))
4412 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4477 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4413 4478
4414 index = ivalue (cadr (args));
4415
4416 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4479 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4417 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4480 Error_1 ("vector-set!: out of bounds:", cadr (args));
4418 4481
4419 set_vector_elem (x, index, caddr (args)); 4482 vector_set (x, index, caddr (args));
4420 s_return (x); 4483 s_return (x);
4421 } 4484 }
4422 } 4485 }
4423 4486
4424 abort (); 4487 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} 4488}
4480 4489
4481static int 4490static int
4482opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4491opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4483{ 4492{
4529 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4538 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4530 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4539 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4531 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4540 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4532 4541
4533#if USE_CHAR_CLASSIFIERS 4542#if USE_CHAR_CLASSIFIERS
4534 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4543 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4535 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4544 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4536 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4545 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4537 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4546 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4538 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4547 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4539#endif 4548#endif
4540 4549
4541#if USE_PORTS 4550#if USE_PORTS
4542 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4551 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4543 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4552 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4744 4753
4745 case OP_NEWSEGMENT: /* new-segment */ 4754 case OP_NEWSEGMENT: /* new-segment */
4746 if (!is_pair (args) || !is_number (a)) 4755 if (!is_pair (args) || !is_number (a))
4747 Error_0 ("new-segment: argument must be a number"); 4756 Error_0 ("new-segment: argument must be a number");
4748 4757
4749 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4758 alloc_cellseg (SCHEME_A_ ivalue (a));
4750 4759
4751 s_return (S_T); 4760 s_return (S_T);
4752 4761
4753 case OP_OBLIST: /* oblist */ 4762 case OP_OBLIST: /* oblist */
4754 s_return (oblist_all_symbols (SCHEME_A)); 4763 s_return (oblist_all_symbols (SCHEME_A));
4783 break; 4792 break;
4784 } 4793 }
4785 4794
4786 p = port_from_filename (SCHEME_A_ strvalue (a), prop); 4795 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4787 4796
4788 if (p == NIL) 4797 s_return (p == NIL ? S_F : p);
4789 s_return (S_F);
4790
4791 s_return (p);
4792 } 4798 }
4793 4799
4794# if USE_STRING_PORTS 4800# if USE_STRING_PORTS
4795 4801
4796 case OP_OPEN_INSTRING: /* open-input-string */ 4802 case OP_OPEN_INSTRING: /* open-input-string */
4811 } 4817 }
4812 4818
4813 p = port_from_string (SCHEME_A_ strvalue (a), 4819 p = port_from_string (SCHEME_A_ strvalue (a),
4814 strvalue (a) + strlength (a), prop); 4820 strvalue (a) + strlength (a), prop);
4815 4821
4816 if (p == NIL) 4822 s_return (p == NIL ? S_F : p);
4817 s_return (S_F);
4818
4819 s_return (p);
4820 } 4823 }
4821 4824
4822 case OP_OPEN_OUTSTRING: /* open-output-string */ 4825 case OP_OPEN_OUTSTRING: /* open-output-string */
4823 { 4826 {
4824 pointer p; 4827 pointer p;
4825 4828
4826 if (a == NIL) 4829 if (a == NIL)
4827 {
4828 p = port_from_scratch (SCHEME_A); 4830 p = port_from_scratch (SCHEME_A);
4829
4830 if (p == NIL)
4831 s_return (S_F);
4832 }
4833 else 4831 else
4834 {
4835 p = port_from_string (SCHEME_A_ strvalue (a), 4832 p = port_from_string (SCHEME_A_ strvalue (a),
4836 strvalue (a) + strlength (a), port_output); 4833 strvalue (a) + strlength (a), port_output);
4837 4834
4838 if (p == NIL) 4835 s_return (p == NIL ? S_F : p);
4839 s_return (S_F);
4840 }
4841
4842 s_return (p);
4843 } 4836 }
4844 4837
4845 case OP_GET_OUTSTRING: /* get-output-string */ 4838 case OP_GET_OUTSTRING: /* get-output-string */
4846 { 4839 {
4847 port *p; 4840 port *p;
4886 case OP_CURR_ENV: /* current-environment */ 4879 case OP_CURR_ENV: /* current-environment */
4887 s_return (SCHEME_V->envir); 4880 s_return (SCHEME_V->envir);
4888 4881
4889 } 4882 }
4890 4883
4891 abort (); 4884 if (USE_ERROR_CHECKING) abort ();
4892} 4885}
4893 4886
4894static int 4887static int
4895opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4888opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4896{ 4889{
5028 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 5021 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5029 SCHEME_V->tok = token (SCHEME_A); 5022 SCHEME_V->tok = token (SCHEME_A);
5030 s_goto (OP_RDSEXPR); 5023 s_goto (OP_RDSEXPR);
5031 5024
5032 case TOK_ATOM: 5025 case TOK_ATOM:
5033 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 5026 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
5027
5028 case TOK_DOTATOM:
5029 SCHEME_V->strbuff[0] = '.';
5030 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5031
5032 case TOK_STRATOM:
5033 x = readstrexp (SCHEME_A_ '|');
5034 //TODO: haven't checked whether the garbage collector could interfere
5035 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5034 5036
5035 case TOK_DQUOTE: 5037 case TOK_DQUOTE:
5036 x = readstrexp (SCHEME_A); 5038 x = readstrexp (SCHEME_A_ '"');
5037 5039
5038 if (x == S_F) 5040 if (x == S_F)
5039 Error_0 ("Error reading string"); 5041 Error_0 ("Error reading string");
5040 5042
5041 setimmutable (x); 5043 setimmutable (x);
5053 s_goto (OP_EVAL); 5055 s_goto (OP_EVAL);
5054 } 5056 }
5055 } 5057 }
5056 5058
5057 case TOK_SHARP_CONST: 5059 case TOK_SHARP_CONST:
5058 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 5060 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5059 Error_0 ("undefined sharp expression"); 5061 Error_0 ("undefined sharp expression");
5060 else 5062 else
5061 s_return (x); 5063 s_return (x);
5062 5064
5063 default: 5065 default:
5215 putstr (SCHEME_A_ ")"); 5217 putstr (SCHEME_A_ ")");
5216 s_return (S_T); 5218 s_return (S_T);
5217 } 5219 }
5218 else 5220 else
5219 { 5221 {
5220 pointer elem = vector_elem (vec, i); 5222 pointer elem = vector_get (vec, i);
5221 5223
5222 ivalue_unchecked (cdr (args)) = i + 1; 5224 ivalue_unchecked (cdr (args)) = i + 1;
5223 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5225 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5224 SCHEME_V->args = elem; 5226 SCHEME_V->args = elem;
5225 5227
5229 s_goto (OP_P0LIST); 5231 s_goto (OP_P0LIST);
5230 } 5232 }
5231 } 5233 }
5232 } 5234 }
5233 5235
5234 abort (); 5236 if (USE_ERROR_CHECKING) abort ();
5235} 5237}
5236 5238
5237static int 5239static int
5238opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5240opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5239{ 5241{
5285 5287
5286 case OP_CLOSUREP: /* closure? */ 5288 case OP_CLOSUREP: /* closure? */
5287 /* 5289 /*
5288 * Note, macro object is also a closure. 5290 * Note, macro object is also a closure.
5289 * Therefore, (closure? <#MACRO>) ==> #t 5291 * Therefore, (closure? <#MACRO>) ==> #t
5292 * (schmorp) well, obviously not, fix? TODO
5290 */ 5293 */
5291 s_retbool (is_closure (a)); 5294 s_retbool (is_closure (a));
5292 5295
5293 case OP_MACROP: /* macro? */ 5296 case OP_MACROP: /* macro? */
5294 s_retbool (is_macro (a)); 5297 s_retbool (is_macro (a));
5295 } 5298 }
5296 5299
5297 abort (); 5300 if (USE_ERROR_CHECKING) abort ();
5298} 5301}
5299 5302
5300/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5303/* 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); 5304typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5302 5305
5303typedef int (*test_predicate)(pointer); 5306typedef int (*test_predicate)(pointer);
5304static int 5307static int
5305is_any (pointer p) 5308tst_any (pointer p)
5306{ 5309{
5307 return 1; 5310 return 1;
5308} 5311}
5309 5312
5310static int 5313static int
5311is_nonneg (pointer p) 5314tst_inonneg (pointer p)
5312{ 5315{
5313 return ivalue (p) >= 0 && is_integer (p); 5316 return is_integer (p) && ivalue_unchecked (p) >= 0;
5314} 5317}
5315 5318
5316static int 5319static int
5317tst_is_list (pointer p) 5320tst_is_list (SCHEME_P_ pointer p)
5318{ 5321{
5319 return p == NIL || is_pair (p); 5322 return p == NIL || is_pair (p);
5320} 5323}
5321 5324
5322/* Correspond carefully with following defines! */ 5325/* Correspond carefully with following defines! */
5323static struct 5326static struct
5324{ 5327{
5325 test_predicate fct; 5328 test_predicate fct;
5326 const char *kind; 5329 const char *kind;
5327} tests[] = 5330} tests[] = {
5328{
5329 { is_any, 0 }, 5331 { tst_any , 0 },
5330 { is_string, "string" }, 5332 { is_string , "string" },
5331 { is_symbol, "symbol" }, 5333 { is_symbol , "symbol" },
5332 { is_port, "port" }, 5334 { is_port , "port" },
5333 { is_inport, "input port" }, 5335 { is_inport , "input port" },
5334 { is_outport, "output port" }, 5336 { is_outport , "output port" },
5335 { is_environment, "environment" }, 5337 { is_environment, "environment" },
5336 { is_pair, "pair" }, 5338 { is_pair , "pair" },
5337 { tst_is_list, "pair or '()" }, 5339 { 0 , "pair or '()" },
5338 { is_character, "character" }, 5340 { is_character , "character" },
5339 { is_vector, "vector" }, 5341 { is_vector , "vector" },
5340 { is_number, "number" }, 5342 { is_number , "number" },
5341 { is_integer, "integer" }, 5343 { is_integer , "integer" },
5342 { is_nonneg, "non-negative integer" } 5344 { tst_inonneg , "non-negative integer" }
5343}; 5345};
5344 5346
5345#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ 5347#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5346#define TST_ANY "\001" 5348#define TST_ANY "\001"
5347#define TST_STRING "\002" 5349#define TST_STRING "\002"
5388typedef struct 5390typedef struct
5389{ 5391{
5390 uint8_t func; 5392 uint8_t func;
5391 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ 5393 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5392 uint8_t builtin; 5394 uint8_t builtin;
5395#if USE_ERROR_CHECKING
5393 uint8_t min_arity; 5396 uint8_t min_arity;
5394 uint8_t max_arity; 5397 uint8_t max_arity;
5395 char arg_tests_encoding[3]; 5398 char arg_tests_encoding[3];
5399#endif
5396} op_code_info; 5400} op_code_info;
5397 5401
5398static const op_code_info dispatch_table[] = { 5402static const op_code_info dispatch_table[] = {
5403#if USE_ERROR_CHECKING
5399#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, 5404#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5405#else
5406#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5407#endif
5400#include "opdefines.h" 5408#include "opdefines.h"
5401#undef OP_DEF 5409#undef OP_DEF
5402 {0} 5410 {0}
5403}; 5411};
5404 5412
5405/* kernel of this interpreter */ 5413/* kernel of this interpreter */
5406static void 5414static void ecb_hot
5407Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5415Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5408{ 5416{
5409 SCHEME_V->op = op; 5417 SCHEME_V->op = op;
5410 5418
5411 for (;;) 5419 for (;;)
5446 { 5454 {
5447 pointer arg = car (arglist); 5455 pointer arg = car (arglist);
5448 5456
5449 j = t[0]; 5457 j = t[0];
5450 5458
5459 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5460 if (j == TST_LIST[0])
5461 {
5462 if (!tst_is_list (SCHEME_A_ arg))
5463 break;
5464 }
5465 else
5466 {
5451 if (!tests[j - 1].fct (arg)) 5467 if (!tests[j - 1].fct (arg))
5452 break; 5468 break;
5469 }
5453 5470
5454 if (t[1]) /* last test is replicated as necessary */ 5471 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5455 t++; 5472 t++;
5456 5473
5457 arglist = cdr (arglist); 5474 arglist = cdr (arglist);
5458 i++; 5475 i++;
5459 } 5476 }
5514mk_proc (SCHEME_P_ enum scheme_opcodes op) 5531mk_proc (SCHEME_P_ enum scheme_opcodes op)
5515{ 5532{
5516 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5533 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5517 set_typeflag (y, (T_PROC | T_ATOM)); 5534 set_typeflag (y, (T_PROC | T_ATOM));
5518 ivalue_unchecked (y) = op; 5535 ivalue_unchecked (y) = op;
5519 set_num_integer (y);
5520 return y; 5536 return y;
5521} 5537}
5522 5538
5523/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5539/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5524static int 5540static int
5525syntaxnum (pointer p) 5541syntaxnum (pointer p)
5526{ 5542{
5527 const char *s = strvalue (car (p)); 5543 const char *s = strvalue (p);
5528 5544
5529 switch (strlength (car (p))) 5545 switch (strlength (p))
5530 { 5546 {
5531 case 2: 5547 case 2:
5532 if (s[0] == 'i') 5548 if (s[0] == 'i')
5533 return OP_IF0; /* if */ 5549 return OP_IF0; /* if */
5534 else 5550 else
5589 return OP_C0STREAM; /* cons-stream */ 5605 return OP_C0STREAM; /* cons-stream */
5590 } 5606 }
5591} 5607}
5592 5608
5593#if USE_MULTIPLICITY 5609#if USE_MULTIPLICITY
5594scheme * 5610ecb_cold scheme *
5595scheme_init_new () 5611scheme_init_new ()
5596{ 5612{
5597 scheme *sc = malloc (sizeof (scheme)); 5613 scheme *sc = malloc (sizeof (scheme));
5598 5614
5599 if (!scheme_init (SCHEME_A)) 5615 if (!scheme_init (SCHEME_A))
5604 else 5620 else
5605 return sc; 5621 return sc;
5606} 5622}
5607#endif 5623#endif
5608 5624
5609int 5625ecb_cold int
5610scheme_init (SCHEME_P) 5626scheme_init (SCHEME_P)
5611{ 5627{
5612 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5628 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5613 pointer x; 5629 pointer x;
5614 5630
5739scheme_set_external_data (SCHEME_P_ void *p) 5755scheme_set_external_data (SCHEME_P_ void *p)
5740{ 5756{
5741 SCHEME_V->ext_data = p; 5757 SCHEME_V->ext_data = p;
5742} 5758}
5743 5759
5744void 5760ecb_cold void
5745scheme_deinit (SCHEME_P) 5761scheme_deinit (SCHEME_P)
5746{ 5762{
5747 int i; 5763 int i;
5748 5764
5749#if SHOW_ERROR_LINE 5765#if SHOW_ERROR_LINE
5970# endif 5986# endif
5971 int fin; 5987 int fin;
5972 char *file_name = InitFile; 5988 char *file_name = InitFile;
5973 int retcode; 5989 int retcode;
5974 int isfile = 1; 5990 int isfile = 1;
5991 system ("ps v $PPID");//D
5975 5992
5976 if (argc == 2 && strcmp (argv[1], "-?") == 0) 5993 if (argc == 2 && strcmp (argv[1], "-?") == 0)
5977 { 5994 {
5978 xwrstr ("Usage: tinyscheme -?\n"); 5995 xwrstr ("Usage: tinyscheme -?\n");
5979 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 5996 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines