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.46 by root, Mon Nov 30 09:16:55 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#define symprop(p) cdr(p)
427SCHEME_EXPORT INLINE int 419SCHEME_EXPORT int
428hasprop (pointer p) 420hasprop (pointer p)
429{ 421{
430 return typeflag (p) & T_SYMBOL; 422 return typeflag (p) & T_SYMBOL;
431} 423}
432
433# define symprop(p) cdr(p)
434#endif 424#endif
435 425
436INTERFACE INLINE int 426INTERFACE int
437is_syntax (pointer p) 427is_syntax (pointer p)
438{ 428{
439 return typeflag (p) & T_SYNTAX; 429 return typeflag (p) & T_SYNTAX;
440} 430}
441 431
442INTERFACE INLINE int 432INTERFACE int
443is_proc (pointer p) 433is_proc (pointer p)
444{ 434{
445 return type (p) == T_PROC; 435 return type (p) == T_PROC;
446} 436}
447 437
448INTERFACE INLINE int 438INTERFACE int
449is_foreign (pointer p) 439is_foreign (pointer p)
450{ 440{
451 return type (p) == T_FOREIGN; 441 return type (p) == T_FOREIGN;
452} 442}
453 443
454INTERFACE INLINE char * 444INTERFACE char *
455syntaxname (pointer p) 445syntaxname (pointer p)
456{ 446{
457 return strvalue (car (p)); 447 return strvalue (p);
458} 448}
459 449
460#define procnum(p) ivalue (p) 450#define procnum(p) ivalue_unchecked (p)
461static const char *procname (pointer x); 451static const char *procname (pointer x);
462 452
463INTERFACE INLINE int 453INTERFACE int
464is_closure (pointer p) 454is_closure (pointer p)
465{ 455{
466 return type (p) == T_CLOSURE; 456 return type (p) == T_CLOSURE;
467} 457}
468 458
469INTERFACE INLINE int 459INTERFACE int
470is_macro (pointer p) 460is_macro (pointer p)
471{ 461{
472 return type (p) == T_MACRO; 462 return type (p) == T_MACRO;
473} 463}
474 464
475INTERFACE INLINE pointer 465INTERFACE pointer
476closure_code (pointer p) 466closure_code (pointer p)
477{ 467{
478 return car (p); 468 return car (p);
479} 469}
480 470
481INTERFACE INLINE pointer 471INTERFACE pointer
482closure_env (pointer p) 472closure_env (pointer p)
483{ 473{
484 return cdr (p); 474 return cdr (p);
485} 475}
486 476
487INTERFACE INLINE int 477INTERFACE int
488is_continuation (pointer p) 478is_continuation (pointer p)
489{ 479{
490 return type (p) == T_CONTINUATION; 480 return type (p) == T_CONTINUATION;
491} 481}
492 482
493#define cont_dump(p) cdr (p) 483#define cont_dump(p) cdr (p)
494#define set_cont_dump(p,v) set_cdr ((p), (v)) 484#define set_cont_dump(p,v) set_cdr ((p), (v))
495 485
496/* To do: promise should be forced ONCE only */ 486/* To do: promise should be forced ONCE only */
497INTERFACE INLINE int 487INTERFACE int
498is_promise (pointer p) 488is_promise (pointer p)
499{ 489{
500 return type (p) == T_PROMISE; 490 return type (p) == T_PROMISE;
501} 491}
502 492
503INTERFACE INLINE int 493INTERFACE int
504is_environment (pointer p) 494is_environment (pointer p)
505{ 495{
506 return type (p) == T_ENVIRONMENT; 496 return type (p) == T_ENVIRONMENT;
507} 497}
508 498
514 504
515#define is_mark(p) (typeflag (p) & T_MARK) 505#define is_mark(p) (typeflag (p) & T_MARK)
516#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 506#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
517#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 507#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
518 508
519INTERFACE INLINE int 509INTERFACE int
520is_immutable (pointer p) 510is_immutable (pointer p)
521{ 511{
522 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 512 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
523} 513}
524 514
525INTERFACE INLINE void 515INTERFACE void
526setimmutable (pointer p) 516setimmutable (pointer p)
527{ 517{
528#if USE_ERROR_CHECKING 518#if USE_ERROR_CHECKING
529 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 519 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
530#endif 520#endif
531} 521}
532 522
523/* Result is:
524 proper list: length
525 circular list: -1
526 not even a pair: -2
527 dotted list: -2 minus length before dot
528*/
529INTERFACE int
530list_length (SCHEME_P_ pointer a)
531{
532 int i = 0;
533 pointer slow, fast;
534
535 slow = fast = a;
536
537 while (1)
538 {
539 if (fast == NIL)
540 return i;
541
542 if (!is_pair (fast))
543 return -2 - i;
544
545 fast = cdr (fast);
546 ++i;
547
548 if (fast == NIL)
549 return i;
550
551 if (!is_pair (fast))
552 return -2 - i;
553
554 ++i;
555 fast = cdr (fast);
556
557 /* Safe because we would have already returned if `fast'
558 encountered a non-pair. */
559 slow = cdr (slow);
560
561 if (fast == slow)
562 {
563 /* the fast pointer has looped back around and caught up
564 with the slow pointer, hence the structure is circular,
565 not of finite length, and therefore not a list */
566 return -1;
567 }
568 }
569}
570
571INTERFACE int
572is_list (SCHEME_P_ pointer a)
573{
574 return list_length (SCHEME_A_ a) >= 0;
575}
576
533#if USE_CHAR_CLASSIFIERS 577#if USE_CHAR_CLASSIFIERS
534static INLINE int 578ecb_inline int
535Cisalpha (int c) 579Cisalpha (int c)
536{ 580{
537 return isascii (c) && isalpha (c); 581 return isascii (c) && isalpha (c);
538} 582}
539 583
540static INLINE int 584ecb_inline int
541Cisdigit (int c) 585Cisdigit (int c)
542{ 586{
543 return isascii (c) && isdigit (c); 587 return isascii (c) && isdigit (c);
544} 588}
545 589
546static INLINE int 590ecb_inline int
547Cisspace (int c) 591Cisspace (int c)
548{ 592{
549 return isascii (c) && isspace (c); 593 return isascii (c) && isspace (c);
550} 594}
551 595
552static INLINE int 596ecb_inline int
553Cisupper (int c) 597Cisupper (int c)
554{ 598{
555 return isascii (c) && isupper (c); 599 return isascii (c) && isupper (c);
556} 600}
557 601
558static INLINE int 602ecb_inline int
559Cislower (int c) 603Cislower (int c)
560{ 604{
561 return isascii (c) && islower (c); 605 return isascii (c) && islower (c);
562} 606}
563#endif 607#endif
624#endif 668#endif
625 669
626static int file_push (SCHEME_P_ const char *fname); 670static int file_push (SCHEME_P_ const char *fname);
627static void file_pop (SCHEME_P); 671static void file_pop (SCHEME_P);
628static int file_interactive (SCHEME_P); 672static int file_interactive (SCHEME_P);
629static INLINE int is_one_of (char *s, int c); 673ecb_inline int is_one_of (const char *s, int c);
630static int alloc_cellseg (SCHEME_P_ int n); 674static int alloc_cellseg (SCHEME_P_ int n);
631static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 675ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
632static void finalize_cell (SCHEME_P_ pointer a); 676static void finalize_cell (SCHEME_P_ pointer a);
633static int count_consecutive_cells (pointer x, int needed); 677static int count_consecutive_cells (pointer x, int needed);
634static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 678static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
635static pointer mk_number (SCHEME_P_ const num n); 679static pointer mk_number (SCHEME_P_ const num n);
636static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 680static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
651static void mark (pointer a); 695static void mark (pointer a);
652static void gc (SCHEME_P_ pointer a, pointer b); 696static void gc (SCHEME_P_ pointer a, pointer b);
653static int basic_inchar (port *pt); 697static int basic_inchar (port *pt);
654static int inchar (SCHEME_P); 698static int inchar (SCHEME_P);
655static void backchar (SCHEME_P_ int c); 699static void backchar (SCHEME_P_ int c);
656static char *readstr_upto (SCHEME_P_ char *delim); 700static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
657static pointer readstrexp (SCHEME_P); 701static pointer readstrexp (SCHEME_P_ char delim);
658static INLINE int skipspace (SCHEME_P); 702ecb_inline int skipspace (SCHEME_P);
659static int token (SCHEME_P); 703static int token (SCHEME_P);
660static void printslashstring (SCHEME_P_ char *s, int len); 704static void printslashstring (SCHEME_P_ char *s, int len);
661static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 705static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
662static void printatom (SCHEME_P_ pointer l, int f); 706static void printatom (SCHEME_P_ pointer l, int f);
663static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 707static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
679static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 723static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
680static void assign_syntax (SCHEME_P_ const char *name); 724static void assign_syntax (SCHEME_P_ const char *name);
681static int syntaxnum (pointer p); 725static int syntaxnum (pointer p);
682static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 726static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
683 727
728static IVALUE
729ivalue (pointer x)
730{
731 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
732}
733
734static RVALUE
735rvalue (pointer x)
736{
737 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
738}
739
740INTERFACE num
741nvalue (pointer x)
742{
743 num n;
744
745 num_set_fixnum (n, is_integer (x));
746
747 if (num_is_fixnum (n))
748 num_set_ivalue (n, ivalue_unchecked (x));
749 else
750 num_set_rvalue (n, rvalue_unchecked (x));
751
752 return n;
753}
754
684static num 755static num
685num_op (enum num_op op, num a, num b) 756num_op (enum num_op op, num a, num b)
686{ 757{
687 num ret; 758 num ret;
688 759
689 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 760 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
690 761
691 if (num_is_fixnum (ret)) 762 if (num_is_fixnum (ret))
692 { 763 {
693 IVALUE av = num_get_ivalue (a);
694 IVALUE bv = num_get_ivalue (b);
695
696 switch (op) 764 switch (op)
697 { 765 {
698 case NUM_ADD: av += bv; break; 766 case NUM_ADD: a.ivalue += b.ivalue; break;
699 case NUM_SUB: av -= bv; break; 767 case NUM_SUB: a.ivalue -= b.ivalue; break;
700 case NUM_MUL: av *= bv; break; 768 case NUM_MUL: a.ivalue *= b.ivalue; break;
701 case NUM_INTDIV: av /= bv; break; 769 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
702 } 770 }
703 771
704 num_set_ivalue (ret, av); 772 num_set_ivalue (ret, a.ivalue);
705 } 773 }
774#if USE_REAL
706 else 775 else
707 { 776 {
708 RVALUE av = num_get_rvalue (a);
709 RVALUE bv = num_get_rvalue (b);
710
711 switch (op) 777 switch (op)
712 { 778 {
713 case NUM_ADD: av += bv; break; 779 case NUM_ADD: a.rvalue += b.rvalue; break;
714 case NUM_SUB: av -= bv; break; 780 case NUM_SUB: a.rvalue -= b.rvalue; break;
715 case NUM_MUL: av *= bv; break; 781 case NUM_MUL: a.rvalue *= b.rvalue; break;
716 case NUM_INTDIV: av /= bv; break; 782 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
717 } 783 }
718 784
719 num_set_rvalue (ret, av); 785 num_set_rvalue (ret, a.rvalue);
720 } 786 }
787#endif
721 788
722 return ret; 789 return ret;
723} 790}
724 791
725static num 792static num
726num_div (num a, num b) 793num_div (num a, num b)
727{ 794{
728 num ret; 795 num ret;
729 796
730 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); 797 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
731 798
732 if (num_is_fixnum (ret)) 799 if (num_is_fixnum (ret))
733 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 800 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
734 else 801 else
735 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 802 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
736 803
737 return ret; 804 return ret;
738} 805}
739 806
740static num 807static num
742{ 809{
743 num ret; 810 num ret;
744 long e1, e2, res; 811 long e1, e2, res;
745 812
746 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 813 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
747 e1 = num_get_ivalue (a); 814 e1 = num_ivalue (a);
748 e2 = num_get_ivalue (b); 815 e2 = num_ivalue (b);
749 res = e1 % e2; 816 res = e1 % e2;
750 817
751 /* remainder should have same sign as second operand */ 818 /* remainder should have same sign as second operand */
752 if (res > 0) 819 if (res > 0)
753 { 820 {
769{ 836{
770 num ret; 837 num ret;
771 long e1, e2, res; 838 long e1, e2, res;
772 839
773 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 840 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
774 e1 = num_get_ivalue (a); 841 e1 = num_ivalue (a);
775 e2 = num_get_ivalue (b); 842 e2 = num_ivalue (b);
776 res = e1 % e2; 843 res = e1 % e2;
777 844
778 /* modulo should have same sign as second operand */ 845 /* modulo should have same sign as second operand */
779 if (res * e2 < 0) 846 if (res * e2 < 0)
780 res += e2; 847 res += e2;
781 848
782 num_set_ivalue (ret, res); 849 num_set_ivalue (ret, res);
783 return ret; 850 return ret;
784} 851}
785 852
786/* this completely disrespects NaNs */ 853/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
787static int 854static int
788num_cmp (num a, num b) 855num_cmp (num a, num b)
789{ 856{
790 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 857 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
791 int ret; 858 int ret;
792 859
793 if (is_fixnum) 860 if (is_fixnum)
794 { 861 {
795 IVALUE av = num_get_ivalue (a); 862 IVALUE av = num_ivalue (a);
796 IVALUE bv = num_get_ivalue (b); 863 IVALUE bv = num_ivalue (b);
797 864
798 ret = av == bv ? 0 : av < bv ? -1 : +1; 865 ret = av == bv ? 0 : av < bv ? -1 : +1;
799 } 866 }
800 else 867 else
801 { 868 {
802 RVALUE av = num_get_rvalue (a); 869 RVALUE av = num_rvalue (a);
803 RVALUE bv = num_get_rvalue (b); 870 RVALUE bv = num_rvalue (b);
804 871
805 ret = av == bv ? 0 : av < bv ? -1 : +1; 872 ret = av == bv ? 0 : av < bv ? -1 : +1;
806 } 873 }
807 874
808 return ret; 875 return ret;
834#endif 901#endif
835 902
836static int 903static int
837is_zero_rvalue (RVALUE x) 904is_zero_rvalue (RVALUE x)
838{ 905{
906 return x == 0;
907#if 0
839#if USE_REAL 908#if USE_REAL
840 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 909 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
841#else 910#else
842 return x == 0; 911 return x == 0;
912#endif
843#endif 913#endif
844} 914}
845 915
846/* allocate new cell segment */ 916/* allocate new cell segment */
847static int 917static int
868 return k; 938 return k;
869 939
870 i = ++SCHEME_V->last_cell_seg; 940 i = ++SCHEME_V->last_cell_seg;
871 SCHEME_V->alloc_seg[i] = cp; 941 SCHEME_V->alloc_seg[i] = cp;
872 942
873 /* insert new segment in address order */
874 newp = (pointer)cp; 943 newp = (pointer)cp;
875 SCHEME_V->cell_seg[i] = newp; 944 SCHEME_V->cell_seg[i] = newp;
876 SCHEME_V->cell_segsize[i] = segsize; 945 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; 946 SCHEME_V->fcells += segsize;
893 last = newp + segsize - 1; 947 last = newp + segsize - 1;
894 948
895 for (p = newp; p <= last; p++) 949 for (p = newp; p <= last; p++)
896 { 950 {
897 set_typeflag (p, T_FREE); 951 set_typeflag (p, T_PAIR);
898 set_car (p, NIL); 952 set_car (p, NIL);
899 set_cdr (p, p + 1); 953 set_cdr (p, p + 1);
900 } 954 }
901 955
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); 956 set_cdr (last, SCHEME_V->free_cell);
906 SCHEME_V->free_cell = newp; 957 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 } 958 }
919 959
920 return n; 960 return n;
921} 961}
922 962
923/* get new cell. parameter a, b is marked by gc. */ 963/* get new cell. parameter a, b is marked by gc. */
924static INLINE pointer 964ecb_inline pointer
925get_cell_x (SCHEME_P_ pointer a, pointer b) 965get_cell_x (SCHEME_P_ pointer a, pointer b)
926{ 966{
927 if (ecb_expect_false (SCHEME_V->free_cell == NIL)) 967 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
928 { 968 {
929 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 969 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
930 return S_SINK; 970 return S_SINK;
931 971
932 if (SCHEME_V->free_cell == NIL) 972 if (SCHEME_V->free_cell == NIL)
933 { 973 {
934 const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; 974 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1;
935 975
936 gc (SCHEME_A_ a, b); 976 gc (SCHEME_A_ a, b);
937 977
938 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 978 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
939 { 979 {
990} 1030}
991 1031
992static pointer 1032static pointer
993get_vector_object (SCHEME_P_ uint32_t len, pointer init) 1033get_vector_object (SCHEME_P_ uint32_t len, pointer init)
994{ 1034{
995 pointer v = get_cell_x (SCHEME_A_ 0, 0); 1035 pointer v = get_cell_x (SCHEME_A_ NIL, NIL);
996 pointer *e = malloc (len * sizeof (pointer)); 1036 pointer *e = malloc (len * sizeof (pointer));
997 1037
998 if (!e && USE_ERROR_CHECKING) 1038 if (!e && USE_ERROR_CHECKING)
999 return S_SINK; 1039 return S_SINK;
1000 1040
1001 /* Record it as a vector so that gc understands it. */ 1041 /* Record it as a vector so that gc understands it. */
1002 set_typeflag (v, T_VECTOR | T_ATOM); 1042 set_typeflag (v, T_VECTOR | T_ATOM);
1003 1043
1004 v->object.vector.vvalue = e; 1044 v->object.vector.vvalue = e;
1005 v->object.vector.length = len; 1045 v->object.vector.length = len;
1006 fill_vector (v, init); 1046 fill_vector (v, 0, init);
1007 push_recent_alloc (SCHEME_A_ v, NIL); 1047 push_recent_alloc (SCHEME_A_ v, NIL);
1008 1048
1009 return v; 1049 return v;
1010} 1050}
1011 1051
1012static INLINE void 1052ecb_inline void
1013ok_to_freely_gc (SCHEME_P) 1053ok_to_freely_gc (SCHEME_P)
1014{ 1054{
1015 set_car (S_SINK, NIL); 1055 set_car (S_SINK, NIL);
1016} 1056}
1017 1057
1054 set_cdr (x, b); 1094 set_cdr (x, b);
1055 1095
1056 return x; 1096 return x;
1057} 1097}
1058 1098
1099static pointer
1100generate_symbol (SCHEME_P_ const char *name)
1101{
1102 pointer x = mk_string (SCHEME_A_ name);
1103 setimmutable (x);
1104 set_typeflag (x, T_SYMBOL | T_ATOM);
1105 return x;
1106}
1107
1059/* ========== oblist implementation ========== */ 1108/* ========== oblist implementation ========== */
1060 1109
1061#ifndef USE_OBJECT_LIST 1110#ifndef USE_OBJECT_LIST
1062 1111
1112static int
1063static int hash_fn (const char *key, int table_size); 1113hash_fn (const char *key, int table_size)
1114{
1115 const unsigned char *p = key;
1116 uint32_t hash = 2166136261;
1117
1118 while (*p)
1119 hash = (hash ^ *p++) * 16777619;
1120
1121 return hash % table_size;
1122}
1064 1123
1065static pointer 1124static pointer
1066oblist_initial_value (SCHEME_P) 1125oblist_initial_value (SCHEME_P)
1067{ 1126{
1068 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1127 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1070 1129
1071/* returns the new symbol */ 1130/* returns the new symbol */
1072static pointer 1131static pointer
1073oblist_add_by_name (SCHEME_P_ const char *name) 1132oblist_add_by_name (SCHEME_P_ const char *name)
1074{ 1133{
1075 int location; 1134 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)); 1135 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))); 1136 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1083 return x; 1137 return x;
1084} 1138}
1085 1139
1086static INLINE pointer 1140ecb_inline pointer
1087oblist_find_by_name (SCHEME_P_ const char *name) 1141oblist_find_by_name (SCHEME_P_ const char *name)
1088{ 1142{
1089 int location; 1143 int location;
1090 pointer x; 1144 pointer x;
1091 char *s; 1145 char *s;
1092 1146
1093 location = hash_fn (name, veclength (SCHEME_V->oblist)); 1147 location = hash_fn (name, veclength (SCHEME_V->oblist));
1094 1148
1095 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1149 for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1096 { 1150 {
1097 s = symname (car (x)); 1151 s = symname (car (x));
1098 1152
1099 /* case-insensitive, per R5RS section 2 */ 1153 /* case-insensitive, per R5RS section 2 */
1100 if (stricmp (name, s) == 0) 1154 if (stricmp (name, s) == 0)
1110 int i; 1164 int i;
1111 pointer x; 1165 pointer x;
1112 pointer ob_list = NIL; 1166 pointer ob_list = NIL;
1113 1167
1114 for (i = 0; i < veclength (SCHEME_V->oblist); i++) 1168 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1115 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1169 for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1116 ob_list = cons (x, ob_list); 1170 ob_list = cons (x, ob_list);
1117 1171
1118 return ob_list; 1172 return ob_list;
1119} 1173}
1120 1174
1124oblist_initial_value (SCHEME_P) 1178oblist_initial_value (SCHEME_P)
1125{ 1179{
1126 return NIL; 1180 return NIL;
1127} 1181}
1128 1182
1129static INLINE pointer 1183ecb_inline pointer
1130oblist_find_by_name (SCHEME_P_ const char *name) 1184oblist_find_by_name (SCHEME_P_ const char *name)
1131{ 1185{
1132 pointer x; 1186 pointer x;
1133 char *s; 1187 char *s;
1134 1188
1146 1200
1147/* returns the new symbol */ 1201/* returns the new symbol */
1148static pointer 1202static pointer
1149oblist_add_by_name (SCHEME_P_ const char *name) 1203oblist_add_by_name (SCHEME_P_ const char *name)
1150{ 1204{
1151 pointer x; 1205 pointer x = mk_string (SCHEME_A_ name);
1152
1153 x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1154 set_typeflag (x, T_SYMBOL); 1206 set_typeflag (x, T_SYMBOL);
1155 setimmutable (car (x)); 1207 setimmutable (x);
1156 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1208 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1157 return x; 1209 return x;
1158} 1210}
1159 1211
1160static pointer 1212static pointer
1193mk_character (SCHEME_P_ int c) 1245mk_character (SCHEME_P_ int c)
1194{ 1246{
1195 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1247 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1196 1248
1197 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1249 set_typeflag (x, (T_CHARACTER | T_ATOM));
1198 ivalue_unchecked (x) = c & 0xff; 1250 set_ivalue (x, c & 0xff);
1199 set_num_integer (x); 1251
1200 return x; 1252 return x;
1201} 1253}
1202 1254
1203/* get number atom (integer) */ 1255/* get number atom (integer) */
1204INTERFACE pointer 1256INTERFACE pointer
1205mk_integer (SCHEME_P_ long num) 1257mk_integer (SCHEME_P_ long n)
1206{ 1258{
1207 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1259 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1208 1260
1209 set_typeflag (x, (T_NUMBER | T_ATOM)); 1261 set_typeflag (x, (T_INTEGER | T_ATOM));
1210 ivalue_unchecked (x) = num; 1262 set_ivalue (x, n);
1211 set_num_integer (x); 1263
1212 return x; 1264 return x;
1213} 1265}
1214 1266
1215INTERFACE pointer 1267INTERFACE pointer
1216mk_real (SCHEME_P_ RVALUE n) 1268mk_real (SCHEME_P_ RVALUE n)
1217{ 1269{
1270#if USE_REAL
1218 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1271 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1219 1272
1220 set_typeflag (x, (T_NUMBER | T_ATOM)); 1273 set_typeflag (x, (T_REAL | T_ATOM));
1221 rvalue_unchecked (x) = n; 1274 set_rvalue (x, n);
1222 set_num_real (x); 1275
1223 return x; 1276 return x;
1277#else
1278 return mk_integer (SCHEME_A_ n);
1279#endif
1224} 1280}
1225 1281
1226static pointer 1282static pointer
1227mk_number (SCHEME_P_ const num n) 1283mk_number (SCHEME_P_ const num n)
1228{ 1284{
1285#if USE_REAL
1229 if (num_is_fixnum (n)) 1286 return num_is_fixnum (n)
1287 ? mk_integer (SCHEME_A_ num_ivalue (n))
1288 : mk_real (SCHEME_A_ num_rvalue (n));
1289#else
1230 return mk_integer (SCHEME_A_ num_get_ivalue (n)); 1290 return mk_integer (SCHEME_A_ num_ivalue (n));
1231 else 1291#endif
1232 return mk_real (SCHEME_A_ num_get_rvalue (n));
1233} 1292}
1234 1293
1235/* allocate name to string area */ 1294/* allocate name to string area */
1236static char * 1295static char *
1237store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) 1296store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1243 SCHEME_V->no_memory = 1; 1302 SCHEME_V->no_memory = 1;
1244 return SCHEME_V->strbuff; 1303 return SCHEME_V->strbuff;
1245 } 1304 }
1246 1305
1247 if (str) 1306 if (str)
1248 { 1307 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 1308 else
1258 {
1259 memset (q, fill, len_str); 1309 memset (q, fill, len_str);
1310
1260 q[len_str] = 0; 1311 q[len_str] = 0;
1261 }
1262 1312
1263 return q; 1313 return q;
1264} 1314}
1265 1315
1266INTERFACE pointer 1316INTERFACE pointer
1280 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1330 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1281 1331
1282 set_typeflag (x, T_STRING | T_ATOM); 1332 set_typeflag (x, T_STRING | T_ATOM);
1283 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1333 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1284 strlength (x) = len; 1334 strlength (x) = len;
1335
1285 return x; 1336 return x;
1286} 1337}
1287 1338
1288INTERFACE pointer 1339INTERFACE pointer
1289mk_string (SCHEME_P_ const char *str) 1340mk_string (SCHEME_P_ const char *str)
1296{ 1347{
1297 return get_vector_object (SCHEME_A_ len, NIL); 1348 return get_vector_object (SCHEME_A_ len, NIL);
1298} 1349}
1299 1350
1300INTERFACE void 1351INTERFACE void
1301fill_vector (pointer vec, pointer obj) 1352fill_vector (pointer vec, uint32_t start, pointer obj)
1302{ 1353{
1303 int i; 1354 int i;
1304 1355
1305 for (i = 0; i < vec->object.vector.length; i++) 1356 for (i = start; i < veclength (vec); i++)
1306 vecvalue (vec)[i] = obj; 1357 vecvalue (vec)[i] = obj;
1307} 1358}
1308 1359
1360INTERFACE void
1361vector_resize (pointer vec, uint32_t newsize, pointer fill)
1362{
1363 uint32_t oldsize = veclength (vec);
1364 vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer));
1365 veclength (vec) = newsize;
1366 fill_vector (vec, oldsize, fill);
1367}
1368
1309INTERFACE pointer 1369INTERFACE pointer
1310vector_elem (pointer vec, uint32_t ielem) 1370vector_get (pointer vec, uint32_t ielem)
1311{ 1371{
1312 return vecvalue(vec)[ielem]; 1372 return vecvalue(vec)[ielem];
1313} 1373}
1314 1374
1315INTERFACE void 1375INTERFACE void
1316set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1376vector_set (pointer vec, uint32_t ielem, pointer a)
1317{ 1377{
1318 vecvalue(vec)[ielem] = a; 1378 vecvalue(vec)[ielem] = a;
1319} 1379}
1320 1380
1321/* get new symbol */ 1381/* get new symbol */
1333 1393
1334INTERFACE pointer 1394INTERFACE pointer
1335gensym (SCHEME_P) 1395gensym (SCHEME_P)
1336{ 1396{
1337 pointer x; 1397 pointer x;
1338
1339 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1340 {
1341 char name[40] = "gensym-"; 1398 char name[40] = "gensym-";
1342 xnum (name + 7, SCHEME_V->gensym_cnt); 1399 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1343 1400
1344 /* first check oblist */ 1401 return generate_symbol (SCHEME_A_ name);
1345 x = oblist_find_by_name (SCHEME_A_ name); 1402}
1346 1403
1347 if (x == NIL) 1404static int
1348 { 1405is_gensym (SCHEME_P_ pointer x)
1349 x = oblist_add_by_name (SCHEME_A_ name); 1406{
1350 return x; 1407 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1351 }
1352 }
1353
1354 return NIL;
1355} 1408}
1356 1409
1357/* make symbol or number atom from string */ 1410/* make symbol or number atom from string */
1358static pointer 1411static pointer
1359mk_atom (SCHEME_P_ char *q) 1412mk_atom (SCHEME_P_ char *q)
1413 } 1466 }
1414 else if ((c == 'e') || (c == 'E')) 1467 else if ((c == 'e') || (c == 'E'))
1415 { 1468 {
1416 if (!has_fp_exp) 1469 if (!has_fp_exp)
1417 { 1470 {
1418 has_dec_point = 1; /* decimal point illegal 1471 has_dec_point = 1; /* decimal point illegal from now on */
1419 from now on */
1420 p++; 1472 p++;
1421 1473
1422 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1474 if ((*p == '-') || (*p == '+') || isdigit (*p))
1423 continue; 1475 continue;
1424 } 1476 }
1512 1564
1513 if (ecb_expect_false (is_vector (p))) 1565 if (ecb_expect_false (is_vector (p)))
1514 { 1566 {
1515 int i; 1567 int i;
1516 1568
1517 for (i = 0; i < p->object.vector.length; i++) 1569 for (i = 0; i < veclength (p); i++)
1518 mark (vecvalue (p)[i]); 1570 mark (vecvalue (p)[i]);
1519 } 1571 }
1520 1572
1521 if (is_atom (p)) 1573 if (is_atom (p))
1522 goto E6; 1574 goto E6;
1604 /* garbage collect */ 1656 /* garbage collect */
1605 clrmark (NIL); 1657 clrmark (NIL);
1606 SCHEME_V->fcells = 0; 1658 SCHEME_V->fcells = 0;
1607 SCHEME_V->free_cell = NIL; 1659 SCHEME_V->free_cell = NIL;
1608 1660
1609 /* free-list is kept sorted by address so as to maintain consecutive 1661 if (SCHEME_V->gc_verbose)
1610 ranges, if possible, for use with vectors. Here we scan the cells 1662 xwrstr ("freeing...");
1611 (which are also kept sorted by address) downwards to build the 1663
1612 free-list in sorted order. 1664 uint32_t total = 0;
1613 */ 1665
1666 /* Here we scan the cells to build the free-list. */
1614 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1667 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1615 { 1668 {
1616 p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; 1669 pointer end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i];
1670 total += SCHEME_V->cell_segsize [i];
1617 1671
1618 while (--p >= SCHEME_V->cell_seg[i]) 1672 for (p = SCHEME_V->cell_seg[i]; p < end; ++p)
1619 { 1673 {
1620 if (is_mark (p)) 1674 if (is_mark (p))
1621 clrmark (p); 1675 clrmark (p);
1622 else 1676 else
1623 { 1677 {
1624 /* reclaim cell */ 1678 /* reclaim cell */
1625 if (typeflag (p) != T_FREE) 1679 if (typeflag (p) != T_PAIR)
1626 { 1680 {
1627 finalize_cell (SCHEME_A_ p); 1681 finalize_cell (SCHEME_A_ p);
1628 set_typeflag (p, T_FREE); 1682 set_typeflag (p, T_PAIR);
1629 set_car (p, NIL); 1683 set_car (p, NIL);
1630 } 1684 }
1631 1685
1632 ++SCHEME_V->fcells; 1686 ++SCHEME_V->fcells;
1633 set_cdr (p, SCHEME_V->free_cell); 1687 set_cdr (p, SCHEME_V->free_cell);
1635 } 1689 }
1636 } 1690 }
1637 } 1691 }
1638 1692
1639 if (SCHEME_V->gc_verbose) 1693 if (SCHEME_V->gc_verbose)
1694 {
1640 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1695 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" out of "); xwrnum (total); xwrstr (" cells were recovered.\n");
1696 }
1641} 1697}
1642 1698
1643static void 1699static void
1644finalize_cell (SCHEME_P_ pointer a) 1700finalize_cell (SCHEME_P_ pointer a)
1645{ 1701{
1646 /* TODO, fast bitmap check? */ 1702 /* TODO, fast bitmap check? */
1647 if (is_string (a)) 1703 if (is_string (a) || is_symbol (a))
1648 free (strvalue (a)); 1704 free (strvalue (a));
1649 else if (is_vector (a)) 1705 else if (is_vector (a))
1650 free (vecvalue (a)); 1706 free (vecvalue (a));
1651#if USE_PORTS 1707#if USE_PORTS
1652 else if (is_port (a)) 1708 else if (is_port (a))
2074#endif 2130#endif
2075} 2131}
2076 2132
2077/* read characters up to delimiter, but cater to character constants */ 2133/* read characters up to delimiter, but cater to character constants */
2078static char * 2134static char *
2079readstr_upto (SCHEME_P_ char *delim) 2135readstr_upto (SCHEME_P_ int skip, const char *delim)
2080{ 2136{
2081 char *p = SCHEME_V->strbuff; 2137 char *p = SCHEME_V->strbuff + skip;
2082 2138
2083 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2139 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2084 2140
2085 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2141 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2086 *p = 0; 2142 *p = 0;
2093 return SCHEME_V->strbuff; 2149 return SCHEME_V->strbuff;
2094} 2150}
2095 2151
2096/* read string expression "xxx...xxx" */ 2152/* read string expression "xxx...xxx" */
2097static pointer 2153static pointer
2098readstrexp (SCHEME_P) 2154readstrexp (SCHEME_P_ char delim)
2099{ 2155{
2100 char *p = SCHEME_V->strbuff; 2156 char *p = SCHEME_V->strbuff;
2101 int c; 2157 int c;
2102 int c1 = 0; 2158 int c1 = 0;
2103 enum
2104 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2159 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2105 2160
2106 for (;;) 2161 for (;;)
2107 { 2162 {
2108 c = inchar (SCHEME_A); 2163 c = inchar (SCHEME_A);
2109 2164
2111 return S_F; 2166 return S_F;
2112 2167
2113 switch (state) 2168 switch (state)
2114 { 2169 {
2115 case st_ok: 2170 case st_ok:
2116 switch (c) 2171 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); 2172 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2125 2173
2126 default: 2174 if (ecb_expect_false (c == '\\'))
2175 state = st_bsl;
2176 else
2127 *p++ = c; 2177 *p++ = c;
2128 break;
2129 }
2130 2178
2131 break; 2179 break;
2132 2180
2133 case st_bsl: 2181 case st_bsl:
2134 switch (c) 2182 switch (c)
2164 case 'r': 2212 case 'r':
2165 *p++ = '\r'; 2213 *p++ = '\r';
2166 state = st_ok; 2214 state = st_ok;
2167 break; 2215 break;
2168 2216
2169 case '"':
2170 *p++ = '"';
2171 state = st_ok;
2172 break;
2173
2174 default: 2217 default:
2175 *p++ = c; 2218 *p++ = c;
2176 state = st_ok; 2219 state = st_ok;
2177 break; 2220 break;
2178 } 2221 }
2179 2222
2180 break; 2223 break;
2181 2224
2182 case st_x1: 2225 case st_x1:
2183 case st_x2: 2226 case st_x2:
2184 c = toupper (c); 2227 c = tolower (c);
2185 2228
2186 if (c >= '0' && c <= 'F') 2229 if (c >= '0' && c <= '9')
2187 {
2188 if (c <= '9')
2189 c1 = (c1 << 4) + c - '0'; 2230 c1 = (c1 << 4) + c - '0';
2190 else 2231 else if (c >= 'a' && c <= 'f')
2191 c1 = (c1 << 4) + c - 'A' + 10; 2232 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 2233 else
2202 return S_F; 2234 return S_F;
2235
2236 if (state == st_x1)
2237 state = st_x2;
2238 else
2239 {
2240 *p++ = c1;
2241 state = st_ok;
2242 }
2203 2243
2204 break; 2244 break;
2205 2245
2206 case st_oct1: 2246 case st_oct1:
2207 case st_oct2: 2247 case st_oct2:
2211 backchar (SCHEME_A_ c); 2251 backchar (SCHEME_A_ c);
2212 state = st_ok; 2252 state = st_ok;
2213 } 2253 }
2214 else 2254 else
2215 { 2255 {
2216 if (state == st_oct2 && c1 >= 32) 2256 if (state == st_oct2 && c1 >= ' ')
2217 return S_F; 2257 return S_F;
2218 2258
2219 c1 = (c1 << 3) + (c - '0'); 2259 c1 = (c1 << 3) + (c - '0');
2220 2260
2221 if (state == st_oct1) 2261 if (state == st_oct1)
2226 state = st_ok; 2266 state = st_ok;
2227 } 2267 }
2228 } 2268 }
2229 2269
2230 break; 2270 break;
2231
2232 } 2271 }
2233 } 2272 }
2234} 2273}
2235 2274
2236/* check c is in chars */ 2275/* check c is in chars */
2237static INLINE int 2276ecb_inline int
2238is_one_of (char *s, int c) 2277is_one_of (const char *s, int c)
2239{ 2278{
2240 if (c == EOF)
2241 return 1;
2242
2243 return !!strchr (s, c); 2279 return c == EOF || !!strchr (s, c);
2244} 2280}
2245 2281
2246/* skip white characters */ 2282/* skip white characters */
2247static INLINE int 2283ecb_inline int
2248skipspace (SCHEME_P) 2284skipspace (SCHEME_P)
2249{ 2285{
2250 int c, curr_line = 0; 2286 int c, curr_line = 0;
2251 2287
2252 do 2288 do
2253 { 2289 {
2254 c = inchar (SCHEME_A); 2290 c = inchar (SCHEME_A);
2291
2255#if SHOW_ERROR_LINE 2292#if SHOW_ERROR_LINE
2256 if (c == '\n') 2293 if (ecb_expect_false (c == '\n'))
2257 curr_line++; 2294 curr_line++;
2258#endif 2295#endif
2296
2297 if (ecb_expect_false (c == EOF))
2298 return c;
2259 } 2299 }
2260 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2300 while (is_one_of (WHITESPACE, c));
2261 2301
2262 /* record it */ 2302 /* record it */
2263#if SHOW_ERROR_LINE 2303#if SHOW_ERROR_LINE
2264 if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 2304 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; 2305 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line;
2266#endif 2306#endif
2267 2307
2268 if (c != EOF)
2269 {
2270 backchar (SCHEME_A_ c); 2308 backchar (SCHEME_A_ c);
2271 return 1; 2309 return 1;
2272 }
2273 else
2274 return EOF;
2275} 2310}
2276 2311
2277/* get token */ 2312/* get token */
2278static int 2313static int
2279token (SCHEME_P) 2314token (SCHEME_P)
2295 return TOK_RPAREN; 2330 return TOK_RPAREN;
2296 2331
2297 case '.': 2332 case '.':
2298 c = inchar (SCHEME_A); 2333 c = inchar (SCHEME_A);
2299 2334
2300 if (is_one_of (" \n\t", c)) 2335 if (is_one_of (WHITESPACE, c))
2301 return TOK_DOT; 2336 return TOK_DOT;
2302 else 2337 else
2303 { 2338 {
2304 //TODO: ungetc twice in a row is not supported in C
2305 backchar (SCHEME_A_ c); 2339 backchar (SCHEME_A_ c);
2306 backchar (SCHEME_A_ '.');
2307 return TOK_ATOM; 2340 return TOK_DOTATOM;
2308 } 2341 }
2342
2343 case '|':
2344 return TOK_STRATOM;
2309 2345
2310 case '\'': 2346 case '\'':
2311 return TOK_QUOTE; 2347 return TOK_QUOTE;
2312 2348
2313 case ';': 2349 case ';':
2445 } 2481 }
2446 2482
2447 putcharacter (SCHEME_A_ '"'); 2483 putcharacter (SCHEME_A_ '"');
2448} 2484}
2449 2485
2450
2451/* print atoms */ 2486/* print atoms */
2452static void 2487static void
2453printatom (SCHEME_P_ pointer l, int f) 2488printatom (SCHEME_P_ pointer l, int f)
2454{ 2489{
2455 char *p; 2490 char *p;
2456 int len; 2491 int len;
2457 2492
2458 atom2str (SCHEME_A_ l, f, &p, &len); 2493 atom2str (SCHEME_A_ l, f, &p, &len);
2459 putchars (SCHEME_A_ p, len); 2494 putchars (SCHEME_A_ p, len);
2460} 2495}
2461
2462 2496
2463/* Uses internal buffer unless string pointer is already available */ 2497/* Uses internal buffer unless string pointer is already available */
2464static void 2498static void
2465atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2499atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2466{ 2500{
2480 { 2514 {
2481 p = SCHEME_V->strbuff; 2515 p = SCHEME_V->strbuff;
2482 2516
2483 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2517 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2484 { 2518 {
2485 if (num_is_integer (l)) 2519 if (is_integer (l))
2486 xnum (p, ivalue_unchecked (l)); 2520 xnum (p, ivalue_unchecked (l));
2487#if USE_REAL 2521#if USE_REAL
2488 else 2522 else
2489 { 2523 {
2490 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2524 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2627#endif 2661#endif
2628 } 2662 }
2629 else if (is_continuation (l)) 2663 else if (is_continuation (l))
2630 p = "#<CONTINUATION>"; 2664 p = "#<CONTINUATION>";
2631 else 2665 else
2666 {
2667#if USE_PRINTF
2668 p = SCHEME_V->strbuff;
2669 snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l));
2670#else
2632 p = "#<ERROR>"; 2671 p = "#<ERROR>";
2672#endif
2673 }
2633 2674
2634 *pp = p; 2675 *pp = p;
2635 *plen = strlen (p); 2676 *plen = strlen (p);
2636} 2677}
2637 2678
2745 return 0; 2786 return 0;
2746 } 2787 }
2747 else if (is_number (a)) 2788 else if (is_number (a))
2748 { 2789 {
2749 if (is_number (b)) 2790 if (is_number (b))
2750 if (num_is_integer (a) == num_is_integer (b))
2751 return num_cmp (nvalue (a), nvalue (b)) == 0; 2791 return num_cmp (nvalue (a), nvalue (b)) == 0;
2752 2792
2753 return 0; 2793 return 0;
2754 } 2794 }
2755 else if (is_character (a)) 2795 else if (is_character (a))
2756 { 2796 {
2782/* () is #t in R5RS */ 2822/* () is #t in R5RS */
2783#define is_true(p) ((p) != S_F) 2823#define is_true(p) ((p) != S_F)
2784#define is_false(p) ((p) == S_F) 2824#define is_false(p) ((p) == S_F)
2785 2825
2786/* ========== Environment implementation ========== */ 2826/* ========== 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 2827
2803#ifndef USE_ALIST_ENV 2828#ifndef USE_ALIST_ENV
2804 2829
2805/* 2830/*
2806 * In this implementation, each frame of the environment may be 2831 * In this implementation, each frame of the environment may be
2823 2848
2824 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2849 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2825 setenvironment (SCHEME_V->envir); 2850 setenvironment (SCHEME_V->envir);
2826} 2851}
2827 2852
2828static INLINE void 2853static uint32_t
2854sym_hash (pointer sym, uint32_t size)
2855{
2856 uintptr_t ptr = (uintptr_t)sym;
2857
2858#if 0
2859 /* table size is prime, so why mix */
2860 ptr += ptr >> 32;
2861 ptr += ptr >> 16;
2862 ptr += ptr >> 8;
2863#endif
2864
2865 return ptr % size;
2866}
2867
2868ecb_inline void
2829new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2869new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2830{ 2870{
2831 pointer slot = immutable_cons (variable, value); 2871 pointer slot = immutable_cons (variable, value);
2832 2872
2833 if (is_vector (car (env))) 2873 if (is_vector (car (env)))
2834 { 2874 {
2835 int location = hash_fn (symname (variable), veclength (car (env))); 2875 int location = sym_hash (variable, veclength (car (env)));
2836
2837 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2876 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2838 } 2877 }
2839 else 2878 else
2840 set_car (env, immutable_cons (slot, car (env))); 2879 set_car (env, immutable_cons (slot, car (env)));
2841} 2880}
2842 2881
2843static pointer 2882static pointer
2844find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2883find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2845{ 2884{
2846 pointer x, y; 2885 pointer x, y;
2847 int location;
2848 2886
2849 for (x = env; x != NIL; x = cdr (x)) 2887 for (x = env; x != NIL; x = cdr (x))
2850 { 2888 {
2851 if (is_vector (car (x))) 2889 if (is_vector (car (x)))
2852 { 2890 {
2853 location = hash_fn (symname (hdl), veclength (car (x))); 2891 int location = sym_hash (hdl, veclength (car (x)));
2854 y = vector_elem (car (x), location); 2892 y = vector_get (car (x), location);
2855 } 2893 }
2856 else 2894 else
2857 y = car (x); 2895 y = car (x);
2858 2896
2859 for (; y != NIL; y = cdr (y)) 2897 for (; y != NIL; y = cdr (y))
2860 if (caar (y) == hdl) 2898 if (caar (y) == hdl)
2861 break; 2899 break;
2862 2900
2863 if (y != NIL) 2901 if (y != NIL)
2902 return car (y);
2903
2904 if (!all)
2864 break; 2905 break;
2865
2866 if (!all)
2867 return NIL;
2868 } 2906 }
2869
2870 if (x != NIL)
2871 return car (y);
2872 2907
2873 return NIL; 2908 return NIL;
2874} 2909}
2875 2910
2876#else /* USE_ALIST_ENV */ 2911#else /* USE_ALIST_ENV */
2877 2912
2878static INLINE void 2913ecb_inline void
2879new_frame_in_env (SCHEME_P_ pointer old_env) 2914new_frame_in_env (SCHEME_P_ pointer old_env)
2880{ 2915{
2881 SCHEME_V->envir = immutable_cons (NIL, old_env); 2916 SCHEME_V->envir = immutable_cons (NIL, old_env);
2882 setenvironment (SCHEME_V->envir); 2917 setenvironment (SCHEME_V->envir);
2883} 2918}
2884 2919
2885static INLINE void 2920ecb_inline void
2886new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2921new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2887{ 2922{
2888 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2923 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2889} 2924}
2890 2925
2898 for (y = car (x); y != NIL; y = cdr (y)) 2933 for (y = car (x); y != NIL; y = cdr (y))
2899 if (caar (y) == hdl) 2934 if (caar (y) == hdl)
2900 break; 2935 break;
2901 2936
2902 if (y != NIL) 2937 if (y != NIL)
2938 return car (y);
2903 break; 2939 break;
2904 2940
2905 if (!all) 2941 if (!all)
2906 return NIL; 2942 break;
2907 } 2943 }
2908
2909 if (x != NIL)
2910 return car (y);
2911 2944
2912 return NIL; 2945 return NIL;
2913} 2946}
2914 2947
2915#endif /* USE_ALIST_ENV else */ 2948#endif /* USE_ALIST_ENV else */
2916 2949
2917static INLINE void 2950ecb_inline void
2918new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2951new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2919{ 2952{
2953 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2920 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2954 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2921} 2955}
2922 2956
2923static INLINE void 2957ecb_inline void
2924set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2958set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2925{ 2959{
2926 set_cdr (slot, value); 2960 set_cdr (slot, value);
2927} 2961}
2928 2962
2929static INLINE pointer 2963ecb_inline pointer
2930slot_value_in_env (pointer slot) 2964slot_value_in_env (pointer slot)
2931{ 2965{
2932 return cdr (slot); 2966 return cdr (slot);
2933} 2967}
2934 2968
3062 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3096 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3063 3097
3064 return 0; 3098 return 0;
3065} 3099}
3066 3100
3067static INLINE void 3101ecb_inline void
3068dump_stack_reset (SCHEME_P) 3102dump_stack_reset (SCHEME_P)
3069{ 3103{
3070 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3104 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3071 SCHEME_V->dump = (pointer)+0; 3105 SCHEME_V->dump = (pointer)+0;
3072} 3106}
3073 3107
3074static INLINE void 3108ecb_inline void
3075dump_stack_initialize (SCHEME_P) 3109dump_stack_initialize (SCHEME_P)
3076{ 3110{
3077 SCHEME_V->dump_size = 0; 3111 SCHEME_V->dump_size = 0;
3078 SCHEME_V->dump_base = 0; 3112 SCHEME_V->dump_base = 0;
3079 dump_stack_reset (SCHEME_A); 3113 dump_stack_reset (SCHEME_A);
3132 int i = 0; 3166 int i = 0;
3133 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3167 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3134 3168
3135 while (cont != NIL) 3169 while (cont != NIL)
3136 { 3170 {
3137 frame->op = ivalue (car (cont)); cont = cdr (cont); 3171 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3138 frame->args = car (cont) ; cont = cdr (cont); 3172 frame->args = car (cont) ; cont = cdr (cont);
3139 frame->envir = car (cont) ; cont = cdr (cont); 3173 frame->envir = car (cont) ; cont = cdr (cont);
3140 frame->code = car (cont) ; cont = cdr (cont); 3174 frame->code = car (cont) ; cont = cdr (cont);
3141 3175
3142 ++frame; 3176 ++frame;
3143 ++i; 3177 ++i;
3144 } 3178 }
3145 3179
3146 SCHEME_V->dump = (pointer)(uintptr_t)i; 3180 SCHEME_V->dump = (pointer)(uintptr_t)i;
3147} 3181}
3148 3182
3149#else 3183#else
3150 3184
3151static INLINE void 3185ecb_inline void
3152dump_stack_reset (SCHEME_P) 3186dump_stack_reset (SCHEME_P)
3153{ 3187{
3154 SCHEME_V->dump = NIL; 3188 SCHEME_V->dump = NIL;
3155} 3189}
3156 3190
3157static INLINE void 3191ecb_inline void
3158dump_stack_initialize (SCHEME_P) 3192dump_stack_initialize (SCHEME_P)
3159{ 3193{
3160 dump_stack_reset (SCHEME_A); 3194 dump_stack_reset (SCHEME_A);
3161} 3195}
3162 3196
3174 SCHEME_V->value = a; 3208 SCHEME_V->value = a;
3175 3209
3176 if (dump == NIL) 3210 if (dump == NIL)
3177 return -1; 3211 return -1;
3178 3212
3179 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3213 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3180 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3214 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3181 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3215 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3182 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3216 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3183 3217
3184 SCHEME_V->dump = dump; 3218 SCHEME_V->dump = dump;
3185 3219
3186 return 0; 3220 return 0;
3187} 3221}
3216 3250
3217#endif 3251#endif
3218 3252
3219#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3253#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3220 3254
3255#if EXPERIMENT
3256static int
3257debug (SCHEME_P_ int indent, pointer x)
3258{
3259 int c;
3260
3261 if (is_syntax (x))
3262 {
3263 printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x));
3264 return 8 + 8;
3265 }
3266
3267 if (x == NIL)
3268 {
3269 printf ("%*sNIL\n", indent, "");
3270 return 3;
3271 }
3272
3273 switch (type (x))
3274 {
3275 case T_INTEGER:
3276 printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x);
3277 return 32+8;
3278
3279 case T_SYMBOL:
3280 printf ("%*sS<%s>\n", indent, "", symname (x));
3281 return 24+8;
3282
3283 case T_CLOSURE:
3284 printf ("%*sS<%s>\n", indent, "", "closure");
3285 debug (SCHEME_A_ indent + 3, cdr(x));
3286 return 32 + debug (SCHEME_A_ indent + 3, car (x));
3287
3288 case T_PAIR:
3289 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3290 c = debug (SCHEME_A_ indent + 3, car (x));
3291 c += debug (SCHEME_A_ indent + 3, cdr (x));
3292 return c + 1;
3293
3294 case T_PORT:
3295 printf ("%*sS<%s>\n", indent, "", "port");
3296 return 24+8;
3297
3298 case T_VECTOR:
3299 printf ("%*sS<%s>\n", indent, "", "vector");
3300 return 24+8;
3301
3302 case T_ENVIRONMENT:
3303 printf ("%*sS<%s>\n", indent, "", "environment");
3304 return 0 + debug (SCHEME_A_ indent + 3, car (x));
3305
3306 default:
3307 printf ("unhandled type %d\n", type (x));
3308 break;
3309 }
3310}
3311#endif
3312
3221static int 3313static int
3222opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3314opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3223{ 3315{
3224 pointer args = SCHEME_V->args; 3316 pointer args = SCHEME_V->args;
3225 pointer x, y; 3317 pointer x, y;
3226 3318
3227 switch (op) 3319 switch (op)
3228 { 3320 {
3321#if EXPERIMENT //D
3322 case OP_DEBUG:
3323 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8);
3324 printf ("\n");
3325 s_return (S_T);
3326#endif
3229 case OP_LOAD: /* load */ 3327 case OP_LOAD: /* load */
3230 if (file_interactive (SCHEME_A)) 3328 if (file_interactive (SCHEME_A))
3231 { 3329 {
3232 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3330 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))); 3331 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args)));
3356 } 3454 }
3357 else 3455 else
3358 s_return (SCHEME_V->code); 3456 s_return (SCHEME_V->code);
3359 3457
3360 case OP_E0ARGS: /* eval arguments */ 3458 case OP_E0ARGS: /* eval arguments */
3361 if (is_macro (SCHEME_V->value)) /* macro expansion */ 3459 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3362 { 3460 {
3363 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3461 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3364 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3462 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3365 SCHEME_V->code = SCHEME_V->value; 3463 SCHEME_V->code = SCHEME_V->value;
3366 s_goto (OP_APPLY); 3464 s_goto (OP_APPLY);
3393 3491
3394 case OP_TRACING: 3492 case OP_TRACING:
3395 { 3493 {
3396 int tr = SCHEME_V->tracing; 3494 int tr = SCHEME_V->tracing;
3397 3495
3398 SCHEME_V->tracing = ivalue (car (args)); 3496 SCHEME_V->tracing = ivalue_unchecked (car (args));
3399 s_return (mk_integer (SCHEME_A_ tr)); 3497 s_return (mk_integer (SCHEME_A_ tr));
3400 } 3498 }
3401 3499
3402#endif 3500#endif
3403 3501
3912 SCHEME_V->code = car (args); 4010 SCHEME_V->code = car (args);
3913 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 4011 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3914 s_goto (OP_APPLY); 4012 s_goto (OP_APPLY);
3915 } 4013 }
3916 4014
3917 abort (); 4015 if (USE_ERROR_CHECKING) abort ();
3918} 4016}
3919 4017
3920static int 4018static int
3921opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4019opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3922{ 4020{
3923 pointer args = SCHEME_V->args; 4021 pointer args = SCHEME_V->args;
3924 pointer x = car (args); 4022 pointer x = car (args);
3925 num v; 4023 num v;
3926 4024
3927#if USE_MATH
3928 RVALUE dd;
3929#endif
3930
3931 switch (op) 4025 switch (op)
3932 { 4026 {
3933#if USE_MATH 4027#if USE_MATH
3934 case OP_INEX2EX: /* inexact->exact */ 4028 case OP_INEX2EX: /* inexact->exact */
4029 {
3935 if (num_is_integer (x)) 4030 if (is_integer (x))
3936 s_return (x); 4031 s_return (x);
3937 else if (modf (rvalue_unchecked (x), &dd) == 0) 4032
4033 RVALUE r = rvalue_unchecked (x);
4034
4035 if (r == (RVALUE)(IVALUE)r)
3938 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4036 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3939 else 4037 else
3940 Error_1 ("inexact->exact: not integral:", x); 4038 Error_1 ("inexact->exact: not integral:", x);
4039 }
3941 4040
3942 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4041 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)))); 4042 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)))); 4043 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)))); 4044 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3963 { 4062 {
3964 RVALUE result; 4063 RVALUE result;
3965 int real_result = 1; 4064 int real_result = 1;
3966 pointer y = cadr (args); 4065 pointer y = cadr (args);
3967 4066
3968 if (num_is_integer (x) && num_is_integer (y)) 4067 if (is_integer (x) && is_integer (y))
3969 real_result = 0; 4068 real_result = 0;
3970 4069
3971 /* This 'if' is an R5RS compatibility fix. */ 4070 /* This 'if' is an R5RS compatibility fix. */
3972 /* NOTE: Remove this 'if' fix for R6RS. */ 4071 /* NOTE: Remove this 'if' fix for R6RS. */
3973 if (rvalue (x) == 0 && rvalue (y) < 0) 4072 if (rvalue (x) == 0 && rvalue (y) < 0)
3979 /* If the test fails, result is too big for integer. */ 4078 /* If the test fails, result is too big for integer. */
3980 if (!real_result) 4079 if (!real_result)
3981 { 4080 {
3982 long result_as_long = result; 4081 long result_as_long = result;
3983 4082
3984 if (result != (RVALUE) result_as_long) 4083 if (result != result_as_long)
3985 real_result = 1; 4084 real_result = 1;
3986 } 4085 }
3987 4086
3988 if (real_result) 4087 if (real_result)
3989 s_return (mk_real (SCHEME_A_ result)); 4088 s_return (mk_real (SCHEME_A_ result));
3994 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4093 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)))); 4094 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3996 4095
3997 case OP_TRUNCATE: 4096 case OP_TRUNCATE:
3998 { 4097 {
3999 RVALUE rvalue_of_x; 4098 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))); 4099 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 } 4100 }
4008 4101
4009 case OP_ROUND: 4102 case OP_ROUND:
4010 if (num_is_integer (x)) 4103 if (is_integer (x))
4011 s_return (x); 4104 s_return (x);
4012 4105
4013 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4106 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4014#endif 4107#endif
4015 4108
4016 case OP_ADD: /* + */ 4109 case OP_ADD: /* + */
4017 v = num_zero; 4110 v = num_zero;
4018 4111
4019 for (x = args; x != NIL; x = cdr (x)) 4112 for (x = args; x != NIL; x = cdr (x))
4020 v = num_op ('+', v, nvalue (car (x))); 4113 v = num_op (NUM_ADD, v, nvalue (car (x)));
4021 4114
4022 s_return (mk_number (SCHEME_A_ v)); 4115 s_return (mk_number (SCHEME_A_ v));
4023 4116
4024 case OP_MUL: /* * */ 4117 case OP_MUL: /* * */
4025 v = num_one; 4118 v = num_one;
4026 4119
4027 for (x = args; x != NIL; x = cdr (x)) 4120 for (x = args; x != NIL; x = cdr (x))
4028 v = num_op ('*', v, nvalue (car (x))); 4121 v = num_op (NUM_MUL, v, nvalue (car (x)));
4029 4122
4030 s_return (mk_number (SCHEME_A_ v)); 4123 s_return (mk_number (SCHEME_A_ v));
4031 4124
4032 case OP_SUB: /* - */ 4125 case OP_SUB: /* - */
4033 if (cdr (args) == NIL) 4126 if (cdr (args) == NIL)
4040 x = cdr (args); 4133 x = cdr (args);
4041 v = nvalue (car (args)); 4134 v = nvalue (car (args));
4042 } 4135 }
4043 4136
4044 for (; x != NIL; x = cdr (x)) 4137 for (; x != NIL; x = cdr (x))
4045 v = num_op ('-', v, nvalue (car (x))); 4138 v = num_op (NUM_SUB, v, nvalue (car (x)));
4046 4139
4047 s_return (mk_number (SCHEME_A_ v)); 4140 s_return (mk_number (SCHEME_A_ v));
4048 4141
4049 case OP_DIV: /* / */ 4142 case OP_DIV: /* / */
4050 if (cdr (args) == NIL) 4143 if (cdr (args) == NIL)
4057 x = cdr (args); 4150 x = cdr (args);
4058 v = nvalue (car (args)); 4151 v = nvalue (car (args));
4059 } 4152 }
4060 4153
4061 for (; x != NIL; x = cdr (x)) 4154 for (; x != NIL; x = cdr (x))
4062 {
4063 if (!is_zero_rvalue (rvalue (car (x)))) 4155 if (!is_zero_rvalue (rvalue (car (x))))
4064 v = num_div (v, nvalue (car (x))); 4156 v = num_div (v, nvalue (car (x)));
4065 else 4157 else
4066 Error_0 ("/: division by zero"); 4158 Error_0 ("/: division by zero");
4067 }
4068 4159
4069 s_return (mk_number (SCHEME_A_ v)); 4160 s_return (mk_number (SCHEME_A_ v));
4070 4161
4071 case OP_INTDIV: /* quotient */ 4162 case OP_INTDIV: /* quotient */
4072 if (cdr (args) == NIL) 4163 if (cdr (args) == NIL)
4081 } 4172 }
4082 4173
4083 for (; x != NIL; x = cdr (x)) 4174 for (; x != NIL; x = cdr (x))
4084 { 4175 {
4085 if (ivalue (car (x)) != 0) 4176 if (ivalue (car (x)) != 0)
4086 v = num_op ('/', v, nvalue (car (x))); 4177 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4087 else 4178 else
4088 Error_0 ("quotient: division by zero"); 4179 Error_0 ("quotient: division by zero");
4089 } 4180 }
4090 4181
4091 s_return (mk_number (SCHEME_A_ v)); 4182 s_return (mk_number (SCHEME_A_ v));
4108 else 4199 else
4109 Error_0 ("modulo: division by zero"); 4200 Error_0 ("modulo: division by zero");
4110 4201
4111 s_return (mk_number (SCHEME_A_ v)); 4202 s_return (mk_number (SCHEME_A_ v));
4112 4203
4113 case OP_CAR: /* car */ 4204 /* the compiler will optimize this mess... */
4114 s_return (caar (args)); 4205 case OP_CAR: op_car: s_return (car (x));
4115 4206 case OP_CDR: op_cdr: s_return (cdr (x));
4116 case OP_CDR: /* cdr */ 4207 case OP_CAAR: op_caar: x = car (x); goto op_car;
4117 s_return (cdar (args)); 4208 case OP_CADR: op_cadr: x = cdr (x); goto op_car;
4209 case OP_CDAR: op_cdar: x = car (x); goto op_cdr;
4210 case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr;
4211 case OP_CAAAR: op_caaar: x = car (x); goto op_caar;
4212 case OP_CAADR: op_caadr: x = cdr (x); goto op_caar;
4213 case OP_CADAR: op_cadar: x = car (x); goto op_cadr;
4214 case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr;
4215 case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar;
4216 case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar;
4217 case OP_CDDAR: op_cddar: x = car (x); goto op_cddr;
4218 case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr;
4219 case OP_CAAAAR: x = car (x); goto op_caaar;
4220 case OP_CAAADR: x = cdr (x); goto op_caaar;
4221 case OP_CAADAR: x = car (x); goto op_caadr;
4222 case OP_CAADDR: x = cdr (x); goto op_caadr;
4223 case OP_CADAAR: x = car (x); goto op_cadar;
4224 case OP_CADADR: x = cdr (x); goto op_cadar;
4225 case OP_CADDAR: x = car (x); goto op_caddr;
4226 case OP_CADDDR: x = cdr (x); goto op_caddr;
4227 case OP_CDAAAR: x = car (x); goto op_cdaar;
4228 case OP_CDAADR: x = cdr (x); goto op_cdaar;
4229 case OP_CDADAR: x = car (x); goto op_cdadr;
4230 case OP_CDADDR: x = cdr (x); goto op_cdadr;
4231 case OP_CDDAAR: x = car (x); goto op_cddar;
4232 case OP_CDDADR: x = cdr (x); goto op_cddar;
4233 case OP_CDDDAR: x = car (x); goto op_cdddr;
4234 case OP_CDDDDR: x = cdr (x); goto op_cdddr;
4118 4235
4119 case OP_CONS: /* cons */ 4236 case OP_CONS: /* cons */
4120 set_cdr (args, cadr (args)); 4237 set_cdr (args, cadr (args));
4121 s_return (args); 4238 s_return (args);
4122 4239
4137 } 4254 }
4138 else 4255 else
4139 Error_0 ("set-cdr!: unable to alter immutable pair"); 4256 Error_0 ("set-cdr!: unable to alter immutable pair");
4140 4257
4141 case OP_CHAR2INT: /* char->integer */ 4258 case OP_CHAR2INT: /* char->integer */
4142 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4259 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4143 4260
4144 case OP_INT2CHAR: /* integer->char */ 4261 case OP_INT2CHAR: /* integer->char */
4145 s_return (mk_character (SCHEME_A_ ivalue (x))); 4262 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4146 4263
4147 case OP_CHARUPCASE: 4264 case OP_CHARUPCASE:
4148 { 4265 {
4149 unsigned char c = ivalue (x); 4266 unsigned char c = ivalue_unchecked (x);
4150 c = toupper (c); 4267 c = toupper (c);
4151 s_return (mk_character (SCHEME_A_ c)); 4268 s_return (mk_character (SCHEME_A_ c));
4152 } 4269 }
4153 4270
4154 case OP_CHARDNCASE: 4271 case OP_CHARDNCASE:
4155 { 4272 {
4156 unsigned char c = ivalue (x); 4273 unsigned char c = ivalue_unchecked (x);
4157 c = tolower (c); 4274 c = tolower (c);
4158 s_return (mk_character (SCHEME_A_ c)); 4275 s_return (mk_character (SCHEME_A_ c));
4159 } 4276 }
4160 4277
4161 case OP_STR2SYM: /* string->symbol */ 4278 case OP_STR2SYM: /* string->symbol */
4238 Error_1 ("atom->string: not an atom:", x); 4355 Error_1 ("atom->string: not an atom:", x);
4239 } 4356 }
4240 4357
4241 case OP_MKSTRING: /* make-string */ 4358 case OP_MKSTRING: /* make-string */
4242 { 4359 {
4243 int fill = ' '; 4360 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4244 int len;
4245
4246 len = ivalue (x); 4361 int len = ivalue_unchecked (x);
4247
4248 if (cdr (args) != NIL)
4249 fill = charvalue (cadr (args));
4250 4362
4251 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4363 s_return (mk_empty_string (SCHEME_A_ len, fill));
4252 } 4364 }
4253 4365
4254 case OP_STRLEN: /* string-length */ 4366 case OP_STRLEN: /* string-length */
4255 s_return (mk_integer (SCHEME_A_ strlength (x))); 4367 s_return (mk_integer (SCHEME_A_ strlength (x)));
4256 4368
4257 case OP_STRREF: /* string-ref */ 4369 case OP_STRREF: /* string-ref */
4258 { 4370 {
4259 char *str;
4260 int index;
4261
4262 str = strvalue (x); 4371 char *str = strvalue (x);
4263
4264 index = ivalue (cadr (args)); 4372 int index = ivalue_unchecked (cadr (args));
4265 4373
4266 if (index >= strlength (x)) 4374 if (index >= strlength (x))
4267 Error_1 ("string-ref: out of bounds:", cadr (args)); 4375 Error_1 ("string-ref: out of bounds:", cadr (args));
4268 4376
4269 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4377 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4270 } 4378 }
4271 4379
4272 case OP_STRSET: /* string-set! */ 4380 case OP_STRSET: /* string-set! */
4273 { 4381 {
4274 char *str; 4382 char *str = strvalue (x);
4275 int index; 4383 int index = ivalue_unchecked (cadr (args));
4276 int c; 4384 int c;
4277 4385
4278 if (is_immutable (x)) 4386 if (is_immutable (x))
4279 Error_1 ("string-set!: unable to alter immutable string:", x); 4387 Error_1 ("string-set!: unable to alter immutable string:", x);
4280
4281 str = strvalue (x);
4282
4283 index = ivalue (cadr (args));
4284 4388
4285 if (index >= strlength (x)) 4389 if (index >= strlength (x))
4286 Error_1 ("string-set!: out of bounds:", cadr (args)); 4390 Error_1 ("string-set!: out of bounds:", cadr (args));
4287 4391
4288 c = charvalue (caddr (args)); 4392 c = charvalue (caddr (args));
4311 s_return (newstr); 4415 s_return (newstr);
4312 } 4416 }
4313 4417
4314 case OP_SUBSTR: /* substring */ 4418 case OP_SUBSTR: /* substring */
4315 { 4419 {
4316 char *str; 4420 char *str = strvalue (x);
4317 int index0; 4421 int index0 = ivalue_unchecked (cadr (args));
4318 int index1; 4422 int index1;
4319 int len; 4423 int len;
4320 4424
4321 str = strvalue (x);
4322
4323 index0 = ivalue (cadr (args));
4324
4325 if (index0 > strlength (x)) 4425 if (index0 > strlength (x))
4326 Error_1 ("substring: start out of bounds:", cadr (args)); 4426 Error_1 ("substring: start out of bounds:", cadr (args));
4327 4427
4328 if (cddr (args) != NIL) 4428 if (cddr (args) != NIL)
4329 { 4429 {
4330 index1 = ivalue (caddr (args)); 4430 index1 = ivalue_unchecked (caddr (args));
4331 4431
4332 if (index1 > strlength (x) || index1 < index0) 4432 if (index1 > strlength (x) || index1 < index0)
4333 Error_1 ("substring: end out of bounds:", caddr (args)); 4433 Error_1 ("substring: end out of bounds:", caddr (args));
4334 } 4434 }
4335 else 4435 else
4358 if (SCHEME_V->no_memory) 4458 if (SCHEME_V->no_memory)
4359 s_return (S_SINK); 4459 s_return (S_SINK);
4360#endif 4460#endif
4361 4461
4362 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4462 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4363 set_vector_elem (vec, i, car (x)); 4463 vector_set (vec, i, car (x));
4364 4464
4365 s_return (vec); 4465 s_return (vec);
4366 } 4466 }
4367 4467
4368 case OP_MKVECTOR: /* make-vector */ 4468 case OP_MKVECTOR: /* make-vector */
4369 { 4469 {
4370 pointer fill = NIL; 4470 pointer fill = NIL;
4371 int len;
4372 pointer vec; 4471 pointer vec;
4373
4374 len = ivalue (x); 4472 int len = ivalue_unchecked (x);
4375 4473
4376 if (cdr (args) != NIL) 4474 if (cdr (args) != NIL)
4377 fill = cadr (args); 4475 fill = cadr (args);
4378 4476
4379 vec = mk_vector (SCHEME_A_ len); 4477 vec = mk_vector (SCHEME_A_ len);
4382 if (SCHEME_V->no_memory) 4480 if (SCHEME_V->no_memory)
4383 s_return (S_SINK); 4481 s_return (S_SINK);
4384#endif 4482#endif
4385 4483
4386 if (fill != NIL) 4484 if (fill != NIL)
4387 fill_vector (vec, fill); 4485 fill_vector (vec, 0, fill);
4388 4486
4389 s_return (vec); 4487 s_return (vec);
4390 } 4488 }
4391 4489
4392 case OP_VECLEN: /* vector-length */ 4490 case OP_VECLEN: /* vector-length */
4393 s_return (mk_integer (SCHEME_A_ veclength (x))); 4491 s_return (mk_integer (SCHEME_A_ veclength (x)));
4394 4492
4493 case OP_VECRESIZE:
4494 vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args));
4495 s_return (x);
4496
4395 case OP_VECREF: /* vector-ref */ 4497 case OP_VECREF: /* vector-ref */
4396 { 4498 {
4397 int index;
4398
4399 index = ivalue (cadr (args)); 4499 int index = ivalue_unchecked (cadr (args));
4400 4500
4401 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4501 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4402 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4502 Error_1 ("vector-ref: out of bounds:", cadr (args));
4403 4503
4404 s_return (vector_elem (x, index)); 4504 s_return (vector_get (x, index));
4405 } 4505 }
4406 4506
4407 case OP_VECSET: /* vector-set! */ 4507 case OP_VECSET: /* vector-set! */
4408 { 4508 {
4409 int index; 4509 int index = ivalue_unchecked (cadr (args));
4410 4510
4411 if (is_immutable (x)) 4511 if (is_immutable (x))
4412 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4512 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4413 4513
4414 index = ivalue (cadr (args));
4415
4416 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4514 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4417 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4515 Error_1 ("vector-set!: out of bounds:", cadr (args));
4418 4516
4419 set_vector_elem (x, index, caddr (args)); 4517 vector_set (x, index, caddr (args));
4420 s_return (x); 4518 s_return (x);
4421 } 4519 }
4422 } 4520 }
4423 4521
4424 abort (); 4522 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} 4523}
4480 4524
4481static int 4525static int
4482opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4526opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4483{ 4527{
4517 pointer d = cdr (args); 4561 pointer d = cdr (args);
4518 int r; 4562 int r;
4519 4563
4520 switch (op) 4564 switch (op)
4521 { 4565 {
4522 case OP_NOT: /* not */ r = is_false (a) ; break; 4566 case OP_NOT: /* not */ r = is_false (a) ; break;
4523 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; 4567 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break;
4524 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; 4568 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4525 case OP_NULLP: /* null? */ r = a == NIL ; break; 4569 case OP_NULLP: /* null? */ r = a == NIL ; break;
4526 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; 4570 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4571 case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break;
4527 case OP_NUMBERP: /* number? */ r = is_number (a) ; break; 4572 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4528 case OP_STRINGP: /* string? */ r = is_string (a) ; break; 4573 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4529 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4574 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4530 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4575 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4531 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4576 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4532 4577
4533#if USE_CHAR_CLASSIFIERS 4578#if USE_CHAR_CLASSIFIERS
4534 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4579 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4535 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4580 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4536 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4581 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4537 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4582 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4538 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4583 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4539#endif 4584#endif
4540 4585
4541#if USE_PORTS 4586#if USE_PORTS
4542 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4587 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4543 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4588 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4744 4789
4745 case OP_NEWSEGMENT: /* new-segment */ 4790 case OP_NEWSEGMENT: /* new-segment */
4746 if (!is_pair (args) || !is_number (a)) 4791 if (!is_pair (args) || !is_number (a))
4747 Error_0 ("new-segment: argument must be a number"); 4792 Error_0 ("new-segment: argument must be a number");
4748 4793
4749 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4794 alloc_cellseg (SCHEME_A_ ivalue (a));
4750 4795
4751 s_return (S_T); 4796 s_return (S_T);
4752 4797
4753 case OP_OBLIST: /* oblist */ 4798 case OP_OBLIST: /* oblist */
4754 s_return (oblist_all_symbols (SCHEME_A)); 4799 s_return (oblist_all_symbols (SCHEME_A));
4783 break; 4828 break;
4784 } 4829 }
4785 4830
4786 p = port_from_filename (SCHEME_A_ strvalue (a), prop); 4831 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4787 4832
4788 if (p == NIL) 4833 s_return (p == NIL ? S_F : p);
4789 s_return (S_F);
4790
4791 s_return (p);
4792 } 4834 }
4793 4835
4794# if USE_STRING_PORTS 4836# if USE_STRING_PORTS
4795 4837
4796 case OP_OPEN_INSTRING: /* open-input-string */ 4838 case OP_OPEN_INSTRING: /* open-input-string */
4811 } 4853 }
4812 4854
4813 p = port_from_string (SCHEME_A_ strvalue (a), 4855 p = port_from_string (SCHEME_A_ strvalue (a),
4814 strvalue (a) + strlength (a), prop); 4856 strvalue (a) + strlength (a), prop);
4815 4857
4816 if (p == NIL) 4858 s_return (p == NIL ? S_F : p);
4817 s_return (S_F);
4818
4819 s_return (p);
4820 } 4859 }
4821 4860
4822 case OP_OPEN_OUTSTRING: /* open-output-string */ 4861 case OP_OPEN_OUTSTRING: /* open-output-string */
4823 { 4862 {
4824 pointer p; 4863 pointer p;
4825 4864
4826 if (a == NIL) 4865 if (a == NIL)
4827 {
4828 p = port_from_scratch (SCHEME_A); 4866 p = port_from_scratch (SCHEME_A);
4829
4830 if (p == NIL)
4831 s_return (S_F);
4832 }
4833 else 4867 else
4834 {
4835 p = port_from_string (SCHEME_A_ strvalue (a), 4868 p = port_from_string (SCHEME_A_ strvalue (a),
4836 strvalue (a) + strlength (a), port_output); 4869 strvalue (a) + strlength (a), port_output);
4837 4870
4838 if (p == NIL) 4871 s_return (p == NIL ? S_F : p);
4839 s_return (S_F);
4840 }
4841
4842 s_return (p);
4843 } 4872 }
4844 4873
4845 case OP_GET_OUTSTRING: /* get-output-string */ 4874 case OP_GET_OUTSTRING: /* get-output-string */
4846 { 4875 {
4847 port *p; 4876 port *p;
4886 case OP_CURR_ENV: /* current-environment */ 4915 case OP_CURR_ENV: /* current-environment */
4887 s_return (SCHEME_V->envir); 4916 s_return (SCHEME_V->envir);
4888 4917
4889 } 4918 }
4890 4919
4891 abort (); 4920 if (USE_ERROR_CHECKING) abort ();
4892} 4921}
4893 4922
4894static int 4923static int
4895opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4924opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4896{ 4925{
5028 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 5057 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5029 SCHEME_V->tok = token (SCHEME_A); 5058 SCHEME_V->tok = token (SCHEME_A);
5030 s_goto (OP_RDSEXPR); 5059 s_goto (OP_RDSEXPR);
5031 5060
5032 case TOK_ATOM: 5061 case TOK_ATOM:
5033 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 5062 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
5063
5064 case TOK_DOTATOM:
5065 SCHEME_V->strbuff[0] = '.';
5066 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5067
5068 case TOK_STRATOM:
5069 x = readstrexp (SCHEME_A_ '|');
5070 //TODO: haven't checked whether the garbage collector could interfere
5071 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5034 5072
5035 case TOK_DQUOTE: 5073 case TOK_DQUOTE:
5036 x = readstrexp (SCHEME_A); 5074 x = readstrexp (SCHEME_A_ '"');
5037 5075
5038 if (x == S_F) 5076 if (x == S_F)
5039 Error_0 ("Error reading string"); 5077 Error_0 ("Error reading string");
5040 5078
5041 setimmutable (x); 5079 setimmutable (x);
5053 s_goto (OP_EVAL); 5091 s_goto (OP_EVAL);
5054 } 5092 }
5055 } 5093 }
5056 5094
5057 case TOK_SHARP_CONST: 5095 case TOK_SHARP_CONST:
5058 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 5096 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5059 Error_0 ("undefined sharp expression"); 5097 Error_0 ("undefined sharp expression");
5060 else 5098 else
5061 s_return (x); 5099 s_return (x);
5062 5100
5063 default: 5101 default:
5215 putstr (SCHEME_A_ ")"); 5253 putstr (SCHEME_A_ ")");
5216 s_return (S_T); 5254 s_return (S_T);
5217 } 5255 }
5218 else 5256 else
5219 { 5257 {
5220 pointer elem = vector_elem (vec, i); 5258 pointer elem = vector_get (vec, i);
5221 5259
5222 ivalue_unchecked (cdr (args)) = i + 1; 5260 ivalue_unchecked (cdr (args)) = i + 1;
5223 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5261 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5224 SCHEME_V->args = elem; 5262 SCHEME_V->args = elem;
5225 5263
5229 s_goto (OP_P0LIST); 5267 s_goto (OP_P0LIST);
5230 } 5268 }
5231 } 5269 }
5232 } 5270 }
5233 5271
5234 abort (); 5272 if (USE_ERROR_CHECKING) abort ();
5235} 5273}
5236 5274
5237static int 5275static int
5238opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5276opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5239{ 5277{
5285 5323
5286 case OP_CLOSUREP: /* closure? */ 5324 case OP_CLOSUREP: /* closure? */
5287 /* 5325 /*
5288 * Note, macro object is also a closure. 5326 * Note, macro object is also a closure.
5289 * Therefore, (closure? <#MACRO>) ==> #t 5327 * Therefore, (closure? <#MACRO>) ==> #t
5328 * (schmorp) well, obviously not, fix? TODO
5290 */ 5329 */
5291 s_retbool (is_closure (a)); 5330 s_retbool (is_closure (a));
5292 5331
5293 case OP_MACROP: /* macro? */ 5332 case OP_MACROP: /* macro? */
5294 s_retbool (is_macro (a)); 5333 s_retbool (is_macro (a));
5295 } 5334 }
5296 5335
5297 abort (); 5336 if (USE_ERROR_CHECKING) abort ();
5298} 5337}
5299 5338
5300/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5339/* 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); 5340typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5302 5341
5303typedef int (*test_predicate)(pointer); 5342typedef int (*test_predicate)(pointer);
5304static int 5343static int
5305is_any (pointer p) 5344tst_any (pointer p)
5306{ 5345{
5307 return 1; 5346 return 1;
5308} 5347}
5309 5348
5310static int 5349static int
5311is_nonneg (pointer p) 5350tst_inonneg (pointer p)
5312{ 5351{
5313 return ivalue (p) >= 0 && is_integer (p); 5352 return is_integer (p) && ivalue_unchecked (p) >= 0;
5314} 5353}
5315 5354
5316static int 5355static int
5317tst_is_list (pointer p) 5356tst_is_list (SCHEME_P_ pointer p)
5318{ 5357{
5319 return p == NIL || is_pair (p); 5358 return p == NIL || is_pair (p);
5320} 5359}
5321 5360
5322/* Correspond carefully with following defines! */ 5361/* Correspond carefully with following defines! */
5323static struct 5362static struct
5324{ 5363{
5325 test_predicate fct; 5364 test_predicate fct;
5326 const char *kind; 5365 const char *kind;
5327} tests[] = 5366} tests[] = {
5328{
5329 { is_any, 0 }, 5367 { tst_any , 0 },
5330 { is_string, "string" }, 5368 { is_string , "string" },
5331 { is_symbol, "symbol" }, 5369 { is_symbol , "symbol" },
5332 { is_port, "port" }, 5370 { is_port , "port" },
5333 { is_inport, "input port" }, 5371 { is_inport , "input port" },
5334 { is_outport, "output port" }, 5372 { is_outport , "output port" },
5335 { is_environment, "environment" }, 5373 { is_environment, "environment" },
5336 { is_pair, "pair" }, 5374 { is_pair , "pair" },
5337 { tst_is_list, "pair or '()" }, 5375 { 0 , "pair or '()" },
5338 { is_character, "character" }, 5376 { is_character , "character" },
5339 { is_vector, "vector" }, 5377 { is_vector , "vector" },
5340 { is_number, "number" }, 5378 { is_number , "number" },
5341 { is_integer, "integer" }, 5379 { is_integer , "integer" },
5342 { is_nonneg, "non-negative integer" } 5380 { tst_inonneg , "non-negative integer" }
5343}; 5381};
5344 5382
5345#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ 5383#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5346#define TST_ANY "\001" 5384#define TST_ANY "\001"
5347#define TST_STRING "\002" 5385#define TST_STRING "\002"
5388typedef struct 5426typedef struct
5389{ 5427{
5390 uint8_t func; 5428 uint8_t func;
5391 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ 5429 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5392 uint8_t builtin; 5430 uint8_t builtin;
5431#if USE_ERROR_CHECKING
5393 uint8_t min_arity; 5432 uint8_t min_arity;
5394 uint8_t max_arity; 5433 uint8_t max_arity;
5395 char arg_tests_encoding[3]; 5434 char arg_tests_encoding[3];
5435#endif
5396} op_code_info; 5436} op_code_info;
5397 5437
5398static const op_code_info dispatch_table[] = { 5438static const op_code_info dispatch_table[] = {
5439#if USE_ERROR_CHECKING
5399#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, 5440#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5441#else
5442#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5443#endif
5400#include "opdefines.h" 5444#include "opdefines.h"
5401#undef OP_DEF 5445#undef OP_DEF
5402 {0} 5446 {0}
5403}; 5447};
5404 5448
5405/* kernel of this interpreter */ 5449/* kernel of this interpreter */
5406static void 5450static void ecb_hot
5407Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5451Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5408{ 5452{
5409 SCHEME_V->op = op; 5453 SCHEME_V->op = op;
5410 5454
5411 for (;;) 5455 for (;;)
5446 { 5490 {
5447 pointer arg = car (arglist); 5491 pointer arg = car (arglist);
5448 5492
5449 j = t[0]; 5493 j = t[0];
5450 5494
5495 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5496 if (j == TST_LIST[0])
5497 {
5498 if (!tst_is_list (SCHEME_A_ arg))
5499 break;
5500 }
5501 else
5502 {
5451 if (!tests[j - 1].fct (arg)) 5503 if (!tests[j - 1].fct (arg))
5452 break; 5504 break;
5505 }
5453 5506
5454 if (t[1]) /* last test is replicated as necessary */ 5507 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5455 t++; 5508 t++;
5456 5509
5457 arglist = cdr (arglist); 5510 arglist = cdr (arglist);
5458 i++; 5511 i++;
5459 } 5512 }
5514mk_proc (SCHEME_P_ enum scheme_opcodes op) 5567mk_proc (SCHEME_P_ enum scheme_opcodes op)
5515{ 5568{
5516 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5569 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5517 set_typeflag (y, (T_PROC | T_ATOM)); 5570 set_typeflag (y, (T_PROC | T_ATOM));
5518 ivalue_unchecked (y) = op; 5571 ivalue_unchecked (y) = op;
5519 set_num_integer (y);
5520 return y; 5572 return y;
5521} 5573}
5522 5574
5523/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5575/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5524static int 5576static int
5525syntaxnum (pointer p) 5577syntaxnum (pointer p)
5526{ 5578{
5527 const char *s = strvalue (car (p)); 5579 const char *s = strvalue (p);
5528 5580
5529 switch (strlength (car (p))) 5581 switch (strlength (p))
5530 { 5582 {
5531 case 2: 5583 case 2:
5532 if (s[0] == 'i') 5584 if (s[0] == 'i')
5533 return OP_IF0; /* if */ 5585 return OP_IF0; /* if */
5534 else 5586 else
5589 return OP_C0STREAM; /* cons-stream */ 5641 return OP_C0STREAM; /* cons-stream */
5590 } 5642 }
5591} 5643}
5592 5644
5593#if USE_MULTIPLICITY 5645#if USE_MULTIPLICITY
5594scheme * 5646ecb_cold scheme *
5595scheme_init_new () 5647scheme_init_new ()
5596{ 5648{
5597 scheme *sc = malloc (sizeof (scheme)); 5649 scheme *sc = malloc (sizeof (scheme));
5598 5650
5599 if (!scheme_init (SCHEME_A)) 5651 if (!scheme_init (SCHEME_A))
5604 else 5656 else
5605 return sc; 5657 return sc;
5606} 5658}
5607#endif 5659#endif
5608 5660
5609int 5661ecb_cold int
5610scheme_init (SCHEME_P) 5662scheme_init (SCHEME_P)
5611{ 5663{
5612 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5664 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5613 pointer x; 5665 pointer x;
5614 5666
5739scheme_set_external_data (SCHEME_P_ void *p) 5791scheme_set_external_data (SCHEME_P_ void *p)
5740{ 5792{
5741 SCHEME_V->ext_data = p; 5793 SCHEME_V->ext_data = p;
5742} 5794}
5743 5795
5744void 5796ecb_cold void
5745scheme_deinit (SCHEME_P) 5797scheme_deinit (SCHEME_P)
5746{ 5798{
5747 int i; 5799 int i;
5748 5800
5749#if SHOW_ERROR_LINE 5801#if SHOW_ERROR_LINE
5970# endif 6022# endif
5971 int fin; 6023 int fin;
5972 char *file_name = InitFile; 6024 char *file_name = InitFile;
5973 int retcode; 6025 int retcode;
5974 int isfile = 1; 6026 int isfile = 1;
6027 system ("ps v $PPID");//D
5975 6028
5976 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6029 if (argc == 2 && strcmp (argv[1], "-?") == 0)
5977 { 6030 {
5978 xwrstr ("Usage: tinyscheme -?\n"); 6031 xwrstr ("Usage: tinyscheme -?\n");
5979 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6032 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines