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

Comparing cvsroot/microscheme/scheme.c (file contents):
Revision 1.19 by root, Thu Nov 26 21:36:11 2015 UTC vs.
Revision 1.40 by root, Mon Nov 30 05:19:01 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines