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.21 by root, Thu Nov 26 22:55:39 2015 UTC vs.
Revision 1.35 by root, Sun Nov 29 00:02:21 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,
65#define S_T (&SCHEME_V->xT) //TODO: magic ptr value? 82#define S_T (&SCHEME_V->xT) //TODO: magic ptr value?
66#define S_F (&SCHEME_V->xF) //TODO: magic ptr value? 83#define S_F (&SCHEME_V->xF) //TODO: magic ptr value?
67#define S_SINK (&SCHEME_V->xsink) 84#define S_SINK (&SCHEME_V->xsink)
68#define S_EOF (&SCHEME_V->xEOF_OBJ) 85#define S_EOF (&SCHEME_V->xEOF_OBJ)
69 86
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 87#if !USE_MULTIPLICITY
81static scheme sc; 88static scheme sc;
82#endif 89#endif
83 90
84static void 91static void
153 160
154#define toupper(c) xtoupper (c) 161#define toupper(c) xtoupper (c)
155#define tolower(c) xtolower (c) 162#define tolower(c) xtolower (c)
156#define isdigit(c) xisdigit (c) 163#define isdigit(c) xisdigit (c)
157 164
158#if USE_STRLWR 165#if USE_IGNORECASE
159static const char * 166static const char *
160strlwr (char *s) 167xstrlwr (char *s)
161{ 168{
162 const char *p = s; 169 const char *p = s;
163 170
164 while (*s) 171 while (*s)
165 { 172 {
167 s++; 174 s++;
168 } 175 }
169 176
170 return p; 177 return p;
171} 178}
172#endif
173 179
180#define stricmp(a,b) strcasecmp (a, b)
181#define strlwr(s) xstrlwr (s)
182
183#else
174#define stricmp(a,b) strcmp (a, b) 184# define stricmp(a,b) strcmp (a, b)
175#define strlwr(s) (s) 185# define strlwr(s) (s)
186#endif
176 187
177#ifndef prompt 188#ifndef prompt
178# define prompt "ts> " 189# define prompt "ts> "
179#endif 190#endif
180 191
186# define FIRST_CELLSEGS 3 197# define FIRST_CELLSEGS 3
187#endif 198#endif
188 199
189enum scheme_types 200enum scheme_types
190{ 201{
202 T_INTEGER,
191 T_FREE, 203 T_REAL,
192 T_STRING, 204 T_STRING,
193 T_NUMBER,
194 T_SYMBOL, 205 T_SYMBOL,
195 T_PROC, 206 T_PROC,
196 T_PAIR, 207 T_PAIR, /* also used for free cells */
197 T_CLOSURE, 208 T_CLOSURE,
198 T_CONTINUATION, 209 T_CONTINUATION,
199 T_FOREIGN, 210 T_FOREIGN,
200 T_CHARACTER, 211 T_CHARACTER,
201 T_PORT, 212 T_PORT,
211#define T_SYNTAX 0x0010 222#define T_SYNTAX 0x0010
212#define T_IMMUTABLE 0x0020 223#define T_IMMUTABLE 0x0020
213#define T_ATOM 0x0040 /* only for gc */ 224#define T_ATOM 0x0040 /* only for gc */
214#define T_MARK 0x0080 /* only for gc */ 225#define T_MARK 0x0080 /* only for gc */
215 226
227/* num, for generic arithmetic */
228struct num
229{
230 IVALUE ivalue;
231#if USE_REAL
232 RVALUE rvalue;
233 char is_fixnum;
234#endif
235};
236
237#if USE_REAL
238# define num_is_fixnum(n) (n).is_fixnum
239# define num_set_fixnum(n,f) (n).is_fixnum = (f)
240# define num_ivalue(n) (n).ivalue
241# define num_rvalue(n) (n).rvalue
242# define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i)
243# define num_set_rvalue(n,r) (n).rvalue = (r)
244#else
245# define num_is_fixnum(n) 1
246# define num_set_fixnum(n,f) 0
247# define num_ivalue(n) (n).ivalue
248# define num_rvalue(n) (n).ivalue
249# define num_set_ivalue(n,i) (n).ivalue = (i)
250# define num_set_rvalue(n,r) (n).ivalue = (r)
251#endif
252
216enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; 253enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
217 254
218static num num_op (enum num_op op, num a, num b); 255static num num_op (enum num_op op, num a, num b);
219static num num_intdiv (num a, num b); 256static num num_intdiv (num a, num b);
220static num num_rem (num a, num b); 257static num num_rem (num a, num b);
223#if USE_MATH 260#if USE_MATH
224static double round_per_R5RS (double x); 261static double round_per_R5RS (double x);
225#endif 262#endif
226static int is_zero_rvalue (RVALUE x); 263static int is_zero_rvalue (RVALUE x);
227 264
228static INLINE int
229num_is_integer (pointer p)
230{
231 return num_is_fixnum (p->object.number);
232}
233
234static num num_zero; 265static num num_zero;
235static num num_one; 266static num num_one;
236 267
237/* macros for cell operations */ 268/* macros for cell operations */
238#define typeflag(p) ((p)->flag + 0) 269#define typeflag(p) ((p)->flag + 0)
239#define set_typeflag(p,v) ((p)->flag = (v)) 270#define set_typeflag(p,v) ((p)->flag = (v))
240#define type(p) (typeflag (p) & T_MASKTYPE) 271#define type(p) (typeflag (p) & T_MASKTYPE)
241 272
242INTERFACE INLINE int 273INTERFACE int
243is_string (pointer p) 274is_string (pointer p)
244{ 275{
245 return type (p) == T_STRING; 276 return type (p) == T_STRING;
246} 277}
247 278
248#define strvalue(p) ((p)->object.string.svalue) 279#define strvalue(p) ((p)->object.string.svalue)
249#define strlength(p) ((p)->object.string.length) 280#define strlength(p) ((p)->object.string.length)
250 281
251INTERFACE int is_list (SCHEME_P_ pointer p);
252
253INTERFACE INLINE int 282INTERFACE int
254is_vector (pointer p) 283is_vector (pointer p)
255{ 284{
256 return type (p) == T_VECTOR; 285 return type (p) == T_VECTOR;
257} 286}
258 287
259#define vecvalue(p) ((p)->object.vector.vvalue) 288#define vecvalue(p) ((p)->object.vector.vvalue)
260#define veclength(p) ((p)->object.vector.length) 289#define veclength(p) ((p)->object.vector.length)
261INTERFACE void fill_vector (pointer vec, pointer obj); 290INTERFACE 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); 291INTERFACE pointer vector_get (pointer vec, uint32_t ielem);
264INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); 292INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a);
265 293
266INTERFACE uint32_t 294INTERFACE int
267vector_length (pointer vec) 295is_integer (pointer p)
268{ 296{
269 return vec->object.vector.length; 297 return type (p) == T_INTEGER;
270} 298}
271 299
300/* not the same as in scheme, where integers are (correctly :) reals */
272INTERFACE INLINE int 301INTERFACE int
302is_real (pointer p)
303{
304 return type (p) == T_REAL;
305}
306
307INTERFACE int
273is_number (pointer p) 308is_number (pointer p)
274{ 309{
275 return type (p) == T_NUMBER; 310 return is_integer (p) || is_real (p);
276} 311}
277 312
278INTERFACE INLINE int 313INTERFACE 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) 314is_character (pointer p)
298{ 315{
299 return type (p) == T_CHARACTER; 316 return type (p) == T_CHARACTER;
300} 317}
301 318
302INTERFACE INLINE char * 319INTERFACE char *
303string_value (pointer p) 320string_value (pointer p)
304{ 321{
305 return strvalue (p); 322 return strvalue (p);
306} 323}
307 324
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) 325#define ivalue_unchecked(p) (p)->object.ivalue
326#define set_ivalue(p,v) (p)->object.ivalue = (v)
327
339#if USE_REAL 328#if USE_REAL
340# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 329#define rvalue_unchecked(p) (p)->object.rvalue
341# define set_num_integer(p) (p)->object.number.is_fixnum=1; 330#define set_rvalue(p,v) (p)->object.rvalue = (v)
342# define set_num_real(p) (p)->object.number.is_fixnum=0;
343#else 331#else
344# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 332#define rvalue_unchecked(p) (p)->object.ivalue
345# define set_num_integer(p) 0 333#define set_rvalue(p,v) (p)->object.ivalue = (v)
346# define set_num_real(p) 0
347#endif 334#endif
335
348INTERFACE long 336INTERFACE long
349charvalue (pointer p) 337charvalue (pointer p)
350{ 338{
351 return ivalue_unchecked (p); 339 return ivalue_unchecked (p);
352} 340}
353 341
354INTERFACE INLINE int 342INTERFACE int
355is_port (pointer p) 343is_port (pointer p)
356{ 344{
357 return type (p) == T_PORT; 345 return type (p) == T_PORT;
358} 346}
359 347
360INTERFACE INLINE int 348INTERFACE int
361is_inport (pointer p) 349is_inport (pointer p)
362{ 350{
363 return is_port (p) && p->object.port->kind & port_input; 351 return is_port (p) && p->object.port->kind & port_input;
364} 352}
365 353
366INTERFACE INLINE int 354INTERFACE int
367is_outport (pointer p) 355is_outport (pointer p)
368{ 356{
369 return is_port (p) && p->object.port->kind & port_output; 357 return is_port (p) && p->object.port->kind & port_output;
370} 358}
371 359
372INTERFACE INLINE int 360INTERFACE int
373is_pair (pointer p) 361is_pair (pointer p)
374{ 362{
375 return type (p) == T_PAIR; 363 return type (p) == T_PAIR;
376} 364}
377 365
409pair_cdr (pointer p) 397pair_cdr (pointer p)
410{ 398{
411 return cdr (p); 399 return cdr (p);
412} 400}
413 401
414INTERFACE INLINE int 402INTERFACE int
415is_symbol (pointer p) 403is_symbol (pointer p)
416{ 404{
417 return type (p) == T_SYMBOL; 405 return type (p) == T_SYMBOL;
418} 406}
419 407
420INTERFACE INLINE char * 408INTERFACE char *
421symname (pointer p) 409symname (pointer p)
422{ 410{
423 return strvalue (car (p)); 411 return strvalue (car (p));
424} 412}
425 413
426#if USE_PLIST 414#if USE_PLIST
427SCHEME_EXPORT INLINE int 415SCHEME_EXPORT int
428hasprop (pointer p) 416hasprop (pointer p)
429{ 417{
430 return typeflag (p) & T_SYMBOL; 418 return typeflag (p) & T_SYMBOL;
431} 419}
432 420
433# define symprop(p) cdr(p) 421# define symprop(p) cdr(p)
434#endif 422#endif
435 423
436INTERFACE INLINE int 424INTERFACE int
437is_syntax (pointer p) 425is_syntax (pointer p)
438{ 426{
439 return typeflag (p) & T_SYNTAX; 427 return typeflag (p) & T_SYNTAX;
440} 428}
441 429
442INTERFACE INLINE int 430INTERFACE int
443is_proc (pointer p) 431is_proc (pointer p)
444{ 432{
445 return type (p) == T_PROC; 433 return type (p) == T_PROC;
446} 434}
447 435
448INTERFACE INLINE int 436INTERFACE int
449is_foreign (pointer p) 437is_foreign (pointer p)
450{ 438{
451 return type (p) == T_FOREIGN; 439 return type (p) == T_FOREIGN;
452} 440}
453 441
454INTERFACE INLINE char * 442INTERFACE char *
455syntaxname (pointer p) 443syntaxname (pointer p)
456{ 444{
457 return strvalue (car (p)); 445 return strvalue (car (p));
458} 446}
459 447
460#define procnum(p) ivalue (p) 448#define procnum(p) ivalue_unchecked (p)
461static const char *procname (pointer x); 449static const char *procname (pointer x);
462 450
463INTERFACE INLINE int 451INTERFACE int
464is_closure (pointer p) 452is_closure (pointer p)
465{ 453{
466 return type (p) == T_CLOSURE; 454 return type (p) == T_CLOSURE;
467} 455}
468 456
469INTERFACE INLINE int 457INTERFACE int
470is_macro (pointer p) 458is_macro (pointer p)
471{ 459{
472 return type (p) == T_MACRO; 460 return type (p) == T_MACRO;
473} 461}
474 462
475INTERFACE INLINE pointer 463INTERFACE pointer
476closure_code (pointer p) 464closure_code (pointer p)
477{ 465{
478 return car (p); 466 return car (p);
479} 467}
480 468
481INTERFACE INLINE pointer 469INTERFACE pointer
482closure_env (pointer p) 470closure_env (pointer p)
483{ 471{
484 return cdr (p); 472 return cdr (p);
485} 473}
486 474
487INTERFACE INLINE int 475INTERFACE int
488is_continuation (pointer p) 476is_continuation (pointer p)
489{ 477{
490 return type (p) == T_CONTINUATION; 478 return type (p) == T_CONTINUATION;
491} 479}
492 480
493#define cont_dump(p) cdr (p) 481#define cont_dump(p) cdr (p)
494#define set_cont_dump(p,v) set_cdr ((p), (v)) 482#define set_cont_dump(p,v) set_cdr ((p), (v))
495 483
496/* To do: promise should be forced ONCE only */ 484/* To do: promise should be forced ONCE only */
497INTERFACE INLINE int 485INTERFACE int
498is_promise (pointer p) 486is_promise (pointer p)
499{ 487{
500 return type (p) == T_PROMISE; 488 return type (p) == T_PROMISE;
501} 489}
502 490
503INTERFACE INLINE int 491INTERFACE int
504is_environment (pointer p) 492is_environment (pointer p)
505{ 493{
506 return type (p) == T_ENVIRONMENT; 494 return type (p) == T_ENVIRONMENT;
507} 495}
508 496
514 502
515#define is_mark(p) (typeflag (p) & T_MARK) 503#define is_mark(p) (typeflag (p) & T_MARK)
516#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 504#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
517#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 505#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
518 506
519INTERFACE INLINE int 507INTERFACE int
520is_immutable (pointer p) 508is_immutable (pointer p)
521{ 509{
522 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 510 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
523} 511}
524 512
525INTERFACE INLINE void 513INTERFACE void
526setimmutable (pointer p) 514setimmutable (pointer p)
527{ 515{
528#if USE_ERROR_CHECKING 516#if USE_ERROR_CHECKING
529 set_typeflag (p, typeflag (p) | T_IMMUTABLE); 517 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
530#endif 518#endif
531} 519}
532 520
521/* Result is:
522 proper list: length
523 circular list: -1
524 not even a pair: -2
525 dotted list: -2 minus length before dot
526*/
527INTERFACE int
528list_length (SCHEME_P_ pointer a)
529{
530 int i = 0;
531 pointer slow, fast;
532
533 slow = fast = a;
534
535 while (1)
536 {
537 if (fast == NIL)
538 return i;
539
540 if (!is_pair (fast))
541 return -2 - i;
542
543 fast = cdr (fast);
544 ++i;
545
546 if (fast == NIL)
547 return i;
548
549 if (!is_pair (fast))
550 return -2 - i;
551
552 ++i;
553 fast = cdr (fast);
554
555 /* Safe because we would have already returned if `fast'
556 encountered a non-pair. */
557 slow = cdr (slow);
558
559 if (fast == slow)
560 {
561 /* the fast pointer has looped back around and caught up
562 with the slow pointer, hence the structure is circular,
563 not of finite length, and therefore not a list */
564 return -1;
565 }
566 }
567}
568
569INTERFACE int
570is_list (SCHEME_P_ pointer a)
571{
572 return list_length (SCHEME_A_ a) >= 0;
573}
574
533#if USE_CHAR_CLASSIFIERS 575#if USE_CHAR_CLASSIFIERS
534static INLINE int 576ecb_inline int
535Cisalpha (int c) 577Cisalpha (int c)
536{ 578{
537 return isascii (c) && isalpha (c); 579 return isascii (c) && isalpha (c);
538} 580}
539 581
540static INLINE int 582ecb_inline int
541Cisdigit (int c) 583Cisdigit (int c)
542{ 584{
543 return isascii (c) && isdigit (c); 585 return isascii (c) && isdigit (c);
544} 586}
545 587
546static INLINE int 588ecb_inline int
547Cisspace (int c) 589Cisspace (int c)
548{ 590{
549 return isascii (c) && isspace (c); 591 return isascii (c) && isspace (c);
550} 592}
551 593
552static INLINE int 594ecb_inline int
553Cisupper (int c) 595Cisupper (int c)
554{ 596{
555 return isascii (c) && isupper (c); 597 return isascii (c) && isupper (c);
556} 598}
557 599
558static INLINE int 600ecb_inline int
559Cislower (int c) 601Cislower (int c)
560{ 602{
561 return isascii (c) && islower (c); 603 return isascii (c) && islower (c);
562} 604}
563#endif 605#endif
624#endif 666#endif
625 667
626static int file_push (SCHEME_P_ const char *fname); 668static int file_push (SCHEME_P_ const char *fname);
627static void file_pop (SCHEME_P); 669static void file_pop (SCHEME_P);
628static int file_interactive (SCHEME_P); 670static int file_interactive (SCHEME_P);
629static INLINE int is_one_of (char *s, int c); 671ecb_inline int is_one_of (const char *s, int c);
630static int alloc_cellseg (SCHEME_P_ int n); 672static int alloc_cellseg (SCHEME_P_ int n);
631static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 673ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
632static void finalize_cell (SCHEME_P_ pointer a); 674static void finalize_cell (SCHEME_P_ pointer a);
633static int count_consecutive_cells (pointer x, int needed); 675static int count_consecutive_cells (pointer x, int needed);
634static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 676static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
635static pointer mk_number (SCHEME_P_ const num n); 677static pointer mk_number (SCHEME_P_ const num n);
636static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 678static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
651static void mark (pointer a); 693static void mark (pointer a);
652static void gc (SCHEME_P_ pointer a, pointer b); 694static void gc (SCHEME_P_ pointer a, pointer b);
653static int basic_inchar (port *pt); 695static int basic_inchar (port *pt);
654static int inchar (SCHEME_P); 696static int inchar (SCHEME_P);
655static void backchar (SCHEME_P_ int c); 697static void backchar (SCHEME_P_ int c);
656static char *readstr_upto (SCHEME_P_ char *delim); 698static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
657static pointer readstrexp (SCHEME_P); 699static pointer readstrexp (SCHEME_P_ char delim);
658static INLINE int skipspace (SCHEME_P); 700ecb_inline int skipspace (SCHEME_P);
659static int token (SCHEME_P); 701static int token (SCHEME_P);
660static void printslashstring (SCHEME_P_ char *s, int len); 702static void printslashstring (SCHEME_P_ char *s, int len);
661static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 703static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
662static void printatom (SCHEME_P_ pointer l, int f); 704static void printatom (SCHEME_P_ pointer l, int f);
663static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 705static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
679static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 721static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
680static void assign_syntax (SCHEME_P_ const char *name); 722static void assign_syntax (SCHEME_P_ const char *name);
681static int syntaxnum (pointer p); 723static int syntaxnum (pointer p);
682static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 724static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
683 725
726static IVALUE
727ivalue (pointer x)
728{
729 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
730}
731
732static RVALUE
733rvalue (pointer x)
734{
735 return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x);
736}
737
738INTERFACE num
739nvalue (pointer x)
740{
741 num n;
742
743 num_set_fixnum (n, is_integer (x));
744
745 if (num_is_fixnum (n))
746 num_set_ivalue (n, ivalue_unchecked (x));
747 else
748 num_set_rvalue (n, rvalue_unchecked (x));
749
750 return n;
751}
752
684static num 753static num
685num_op (enum num_op op, num a, num b) 754num_op (enum num_op op, num a, num b)
686{ 755{
687 num ret; 756 num ret;
688 757
689 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 758 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
690 759
691 if (num_is_fixnum (ret)) 760 if (num_is_fixnum (ret))
692 { 761 {
693 IVALUE av = num_get_ivalue (a);
694 IVALUE bv = num_get_ivalue (b);
695
696 switch (op) 762 switch (op)
697 { 763 {
698 case NUM_ADD: av += bv; break; 764 case NUM_ADD: a.ivalue += b.ivalue; break;
699 case NUM_SUB: av -= bv; break; 765 case NUM_SUB: a.ivalue -= b.ivalue; break;
700 case NUM_MUL: av *= bv; break; 766 case NUM_MUL: a.ivalue *= b.ivalue; break;
701 case NUM_INTDIV: av /= bv; break; 767 case NUM_INTDIV: a.ivalue /= b.ivalue; break;
702 } 768 }
703 769
704 num_set_ivalue (ret, av); 770 num_set_ivalue (ret, a.ivalue);
705 } 771 }
772#if USE_REAL
706 else 773 else
707 { 774 {
708 RVALUE av = num_get_rvalue (a);
709 RVALUE bv = num_get_rvalue (b);
710
711 switch (op) 775 switch (op)
712 { 776 {
713 case NUM_ADD: av += bv; break; 777 case NUM_ADD: a.rvalue += b.rvalue; break;
714 case NUM_SUB: av -= bv; break; 778 case NUM_SUB: a.rvalue -= b.rvalue; break;
715 case NUM_MUL: av *= bv; break; 779 case NUM_MUL: a.rvalue *= b.rvalue; break;
716 case NUM_INTDIV: av /= bv; break; 780 case NUM_INTDIV: a.rvalue /= b.rvalue; break;
717 } 781 }
718 782
719 num_set_rvalue (ret, av); 783 num_set_rvalue (ret, a.rvalue);
720 } 784 }
785#endif
721 786
722 return ret; 787 return ret;
723} 788}
724 789
725static num 790static num
726num_div (num a, num b) 791num_div (num a, num b)
727{ 792{
728 num ret; 793 num ret;
729 794
730 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); 795 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0);
731 796
732 if (num_is_fixnum (ret)) 797 if (num_is_fixnum (ret))
733 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); 798 num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b));
734 else 799 else
735 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); 800 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b));
736 801
737 return ret; 802 return ret;
738} 803}
739 804
740static num 805static num
742{ 807{
743 num ret; 808 num ret;
744 long e1, e2, res; 809 long e1, e2, res;
745 810
746 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 811 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
747 e1 = num_get_ivalue (a); 812 e1 = num_ivalue (a);
748 e2 = num_get_ivalue (b); 813 e2 = num_ivalue (b);
749 res = e1 % e2; 814 res = e1 % e2;
750 815
751 /* remainder should have same sign as second operand */ 816 /* remainder should have same sign as second operand */
752 if (res > 0) 817 if (res > 0)
753 { 818 {
769{ 834{
770 num ret; 835 num ret;
771 long e1, e2, res; 836 long e1, e2, res;
772 837
773 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 838 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
774 e1 = num_get_ivalue (a); 839 e1 = num_ivalue (a);
775 e2 = num_get_ivalue (b); 840 e2 = num_ivalue (b);
776 res = e1 % e2; 841 res = e1 % e2;
777 842
778 /* modulo should have same sign as second operand */ 843 /* modulo should have same sign as second operand */
779 if (res * e2 < 0) 844 if (res * e2 < 0)
780 res += e2; 845 res += e2;
781 846
782 num_set_ivalue (ret, res); 847 num_set_ivalue (ret, res);
783 return ret; 848 return ret;
784} 849}
785 850
786/* this completely disrespects NaNs */ 851/* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */
787static int 852static int
788num_cmp (num a, num b) 853num_cmp (num a, num b)
789{ 854{
790 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 855 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
791 int ret; 856 int ret;
792 857
793 if (is_fixnum) 858 if (is_fixnum)
794 { 859 {
795 IVALUE av = num_get_ivalue (a); 860 IVALUE av = num_ivalue (a);
796 IVALUE bv = num_get_ivalue (b); 861 IVALUE bv = num_ivalue (b);
797 862
798 ret = av == bv ? 0 : av < bv ? -1 : +1; 863 ret = av == bv ? 0 : av < bv ? -1 : +1;
799 } 864 }
800 else 865 else
801 { 866 {
802 RVALUE av = num_get_rvalue (a); 867 RVALUE av = num_rvalue (a);
803 RVALUE bv = num_get_rvalue (b); 868 RVALUE bv = num_rvalue (b);
804 869
805 ret = av == bv ? 0 : av < bv ? -1 : +1; 870 ret = av == bv ? 0 : av < bv ? -1 : +1;
806 } 871 }
807 872
808 return ret; 873 return ret;
834#endif 899#endif
835 900
836static int 901static int
837is_zero_rvalue (RVALUE x) 902is_zero_rvalue (RVALUE x)
838{ 903{
904 return x == 0;
905#if 0
839#if USE_REAL 906#if USE_REAL
840 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 907 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
841#else 908#else
842 return x == 0; 909 return x == 0;
910#endif
843#endif 911#endif
844} 912}
845 913
846/* allocate new cell segment */ 914/* allocate new cell segment */
847static int 915static int
868 return k; 936 return k;
869 937
870 i = ++SCHEME_V->last_cell_seg; 938 i = ++SCHEME_V->last_cell_seg;
871 SCHEME_V->alloc_seg[i] = cp; 939 SCHEME_V->alloc_seg[i] = cp;
872 940
873 /* insert new segment in address order */
874 newp = (pointer)cp; 941 newp = (pointer)cp;
875 SCHEME_V->cell_seg[i] = newp; 942 SCHEME_V->cell_seg[i] = newp;
876 SCHEME_V->cell_segsize[i] = segsize; 943 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; 944 SCHEME_V->fcells += segsize;
893 last = newp + segsize - 1; 945 last = newp + segsize - 1;
894 946
895 for (p = newp; p <= last; p++) 947 for (p = newp; p <= last; p++)
896 { 948 {
897 set_typeflag (p, T_FREE); 949 set_typeflag (p, T_PAIR);
898 set_car (p, NIL); 950 set_car (p, NIL);
899 set_cdr (p, p + 1); 951 set_cdr (p, p + 1);
900 } 952 }
901 953
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); 954 set_cdr (last, SCHEME_V->free_cell);
906 SCHEME_V->free_cell = newp; 955 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 } 956 }
919 957
920 return n; 958 return n;
921} 959}
922 960
923/* get new cell. parameter a, b is marked by gc. */ 961/* get new cell. parameter a, b is marked by gc. */
924static INLINE pointer 962ecb_inline pointer
925get_cell_x (SCHEME_P_ pointer a, pointer b) 963get_cell_x (SCHEME_P_ pointer a, pointer b)
926{ 964{
927 if (ecb_expect_false (SCHEME_V->free_cell == NIL)) 965 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
928 { 966 {
929 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 967 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1001 /* Record it as a vector so that gc understands it. */ 1039 /* Record it as a vector so that gc understands it. */
1002 set_typeflag (v, T_VECTOR | T_ATOM); 1040 set_typeflag (v, T_VECTOR | T_ATOM);
1003 1041
1004 v->object.vector.vvalue = e; 1042 v->object.vector.vvalue = e;
1005 v->object.vector.length = len; 1043 v->object.vector.length = len;
1006 fill_vector (v, init); 1044 fill_vector (v, 0, init);
1007 push_recent_alloc (SCHEME_A_ v, NIL); 1045 push_recent_alloc (SCHEME_A_ v, NIL);
1008 1046
1009 return v; 1047 return v;
1010} 1048}
1011 1049
1012static INLINE void 1050ecb_inline void
1013ok_to_freely_gc (SCHEME_P) 1051ok_to_freely_gc (SCHEME_P)
1014{ 1052{
1015 set_car (S_SINK, NIL); 1053 set_car (S_SINK, NIL);
1016} 1054}
1017 1055
1056 return x; 1094 return x;
1057} 1095}
1058 1096
1059/* ========== oblist implementation ========== */ 1097/* ========== oblist implementation ========== */
1060 1098
1099static pointer
1100generate_symbol (SCHEME_P_ const char *name)
1101{
1102 pointer x = mk_string (SCHEME_A_ name);
1103 setimmutable (x);
1104 x = immutable_cons (x, NIL);
1105 set_typeflag (x, T_SYMBOL);
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
1309INTERFACE pointer 1359INTERFACE pointer
1310vector_elem (pointer vec, uint32_t ielem) 1360vector_get (pointer vec, uint32_t ielem)
1311{ 1361{
1312 return vecvalue(vec)[ielem]; 1362 return vecvalue(vec)[ielem];
1313} 1363}
1314 1364
1315INTERFACE void 1365INTERFACE void
1316set_vector_elem (pointer vec, uint32_t ielem, pointer a) 1366vector_set (pointer vec, uint32_t ielem, pointer a)
1317{ 1367{
1318 vecvalue(vec)[ielem] = a; 1368 vecvalue(vec)[ielem] = a;
1319} 1369}
1320 1370
1321/* get new symbol */ 1371/* get new symbol */
1333 1383
1334INTERFACE pointer 1384INTERFACE pointer
1335gensym (SCHEME_P) 1385gensym (SCHEME_P)
1336{ 1386{
1337 pointer x; 1387 pointer x;
1338
1339 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1340 {
1341 char name[40] = "gensym-"; 1388 char name[40] = "gensym-";
1342 xnum (name + 7, SCHEME_V->gensym_cnt); 1389 xnum (name + 7, SCHEME_V->gensym_cnt);
1343 1390
1344 /* first check oblist */ 1391 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} 1392}
1356 1393
1357/* make symbol or number atom from string */ 1394/* make symbol or number atom from string */
1358static pointer 1395static pointer
1359mk_atom (SCHEME_P_ char *q) 1396mk_atom (SCHEME_P_ char *q)
1413 } 1450 }
1414 else if ((c == 'e') || (c == 'E')) 1451 else if ((c == 'e') || (c == 'E'))
1415 { 1452 {
1416 if (!has_fp_exp) 1453 if (!has_fp_exp)
1417 { 1454 {
1418 has_dec_point = 1; /* decimal point illegal 1455 has_dec_point = 1; /* decimal point illegal from now on */
1419 from now on */
1420 p++; 1456 p++;
1421 1457
1422 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1458 if ((*p == '-') || (*p == '+') || isdigit (*p))
1423 continue; 1459 continue;
1424 } 1460 }
1512 1548
1513 if (ecb_expect_false (is_vector (p))) 1549 if (ecb_expect_false (is_vector (p)))
1514 { 1550 {
1515 int i; 1551 int i;
1516 1552
1517 for (i = 0; i < p->object.vector.length; i++) 1553 for (i = 0; i < veclength (p); i++)
1518 mark (vecvalue (p)[i]); 1554 mark (vecvalue (p)[i]);
1519 } 1555 }
1520 1556
1521 if (is_atom (p)) 1557 if (is_atom (p))
1522 goto E6; 1558 goto E6;
1620 if (is_mark (p)) 1656 if (is_mark (p))
1621 clrmark (p); 1657 clrmark (p);
1622 else 1658 else
1623 { 1659 {
1624 /* reclaim cell */ 1660 /* reclaim cell */
1625 if (typeflag (p) != T_FREE) 1661 if (typeflag (p) != T_PAIR)
1626 { 1662 {
1627 finalize_cell (SCHEME_A_ p); 1663 finalize_cell (SCHEME_A_ p);
1628 set_typeflag (p, T_FREE); 1664 set_typeflag (p, T_PAIR);
1629 set_car (p, NIL); 1665 set_car (p, NIL);
1630 } 1666 }
1631 1667
1632 ++SCHEME_V->fcells; 1668 ++SCHEME_V->fcells;
1633 set_cdr (p, SCHEME_V->free_cell); 1669 set_cdr (p, SCHEME_V->free_cell);
1635 } 1671 }
1636 } 1672 }
1637 } 1673 }
1638 1674
1639 if (SCHEME_V->gc_verbose) 1675 if (SCHEME_V->gc_verbose)
1676 {
1640 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); 1677 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n");
1678 }
1641} 1679}
1642 1680
1643static void 1681static void
1644finalize_cell (SCHEME_P_ pointer a) 1682finalize_cell (SCHEME_P_ pointer a)
1645{ 1683{
2074#endif 2112#endif
2075} 2113}
2076 2114
2077/* read characters up to delimiter, but cater to character constants */ 2115/* read characters up to delimiter, but cater to character constants */
2078static char * 2116static char *
2079readstr_upto (SCHEME_P_ char *delim) 2117readstr_upto (SCHEME_P_ int skip, const char *delim)
2080{ 2118{
2081 char *p = SCHEME_V->strbuff; 2119 char *p = SCHEME_V->strbuff + skip;
2082 2120
2083 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2121 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2084 2122
2085 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2123 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2086 *p = 0; 2124 *p = 0;
2093 return SCHEME_V->strbuff; 2131 return SCHEME_V->strbuff;
2094} 2132}
2095 2133
2096/* read string expression "xxx...xxx" */ 2134/* read string expression "xxx...xxx" */
2097static pointer 2135static pointer
2098readstrexp (SCHEME_P) 2136readstrexp (SCHEME_P_ char delim)
2099{ 2137{
2100 char *p = SCHEME_V->strbuff; 2138 char *p = SCHEME_V->strbuff;
2101 int c; 2139 int c;
2102 int c1 = 0; 2140 int c1 = 0;
2103 enum
2104 { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; 2141 enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok;
2105 2142
2106 for (;;) 2143 for (;;)
2107 { 2144 {
2108 c = inchar (SCHEME_A); 2145 c = inchar (SCHEME_A);
2109 2146
2111 return S_F; 2148 return S_F;
2112 2149
2113 switch (state) 2150 switch (state)
2114 { 2151 {
2115 case st_ok: 2152 case st_ok:
2116 switch (c) 2153 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); 2154 return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff);
2125 2155
2126 default: 2156 if (ecb_expect_false (c == '\\'))
2157 state = st_bsl;
2158 else
2127 *p++ = c; 2159 *p++ = c;
2128 break;
2129 }
2130 2160
2131 break; 2161 break;
2132 2162
2133 case st_bsl: 2163 case st_bsl:
2134 switch (c) 2164 switch (c)
2164 case 'r': 2194 case 'r':
2165 *p++ = '\r'; 2195 *p++ = '\r';
2166 state = st_ok; 2196 state = st_ok;
2167 break; 2197 break;
2168 2198
2169 case '"':
2170 *p++ = '"';
2171 state = st_ok;
2172 break;
2173
2174 default: 2199 default:
2175 *p++ = c; 2200 *p++ = c;
2176 state = st_ok; 2201 state = st_ok;
2177 break; 2202 break;
2178 } 2203 }
2179 2204
2180 break; 2205 break;
2181 2206
2182 case st_x1: 2207 case st_x1:
2183 case st_x2: 2208 case st_x2:
2184 c = toupper (c); 2209 c = tolower (c);
2185 2210
2186 if (c >= '0' && c <= 'F') 2211 if (c >= '0' && c <= '9')
2187 {
2188 if (c <= '9')
2189 c1 = (c1 << 4) + c - '0'; 2212 c1 = (c1 << 4) + c - '0';
2190 else 2213 else if (c >= 'a' && c <= 'f')
2191 c1 = (c1 << 4) + c - 'A' + 10; 2214 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 2215 else
2202 return S_F; 2216 return S_F;
2217
2218 if (state == st_x1)
2219 state = st_x2;
2220 else
2221 {
2222 *p++ = c1;
2223 state = st_ok;
2224 }
2203 2225
2204 break; 2226 break;
2205 2227
2206 case st_oct1: 2228 case st_oct1:
2207 case st_oct2: 2229 case st_oct2:
2211 backchar (SCHEME_A_ c); 2233 backchar (SCHEME_A_ c);
2212 state = st_ok; 2234 state = st_ok;
2213 } 2235 }
2214 else 2236 else
2215 { 2237 {
2216 if (state == st_oct2 && c1 >= 32) 2238 if (state == st_oct2 && c1 >= ' ')
2217 return S_F; 2239 return S_F;
2218 2240
2219 c1 = (c1 << 3) + (c - '0'); 2241 c1 = (c1 << 3) + (c - '0');
2220 2242
2221 if (state == st_oct1) 2243 if (state == st_oct1)
2226 state = st_ok; 2248 state = st_ok;
2227 } 2249 }
2228 } 2250 }
2229 2251
2230 break; 2252 break;
2231
2232 } 2253 }
2233 } 2254 }
2234} 2255}
2235 2256
2236/* check c is in chars */ 2257/* check c is in chars */
2237static INLINE int 2258ecb_inline int
2238is_one_of (char *s, int c) 2259is_one_of (const char *s, int c)
2239{ 2260{
2240 if (c == EOF) 2261 if (c == EOF)
2241 return 1; 2262 return 1;
2242 2263
2243 return !!strchr (s, c); 2264 return !!strchr (s, c);
2244} 2265}
2245 2266
2246/* skip white characters */ 2267/* skip white characters */
2247static INLINE int 2268ecb_inline int
2248skipspace (SCHEME_P) 2269skipspace (SCHEME_P)
2249{ 2270{
2250 int c, curr_line = 0; 2271 int c, curr_line = 0;
2251 2272
2252 do 2273 do
2299 2320
2300 if (is_one_of (" \n\t", c)) 2321 if (is_one_of (" \n\t", c))
2301 return TOK_DOT; 2322 return TOK_DOT;
2302 else 2323 else
2303 { 2324 {
2304 //TODO: ungetc twice in a row is not supported in C
2305 backchar (SCHEME_A_ c); 2325 backchar (SCHEME_A_ c);
2306 backchar (SCHEME_A_ '.');
2307 return TOK_ATOM; 2326 return TOK_DOTATOM;
2308 } 2327 }
2328
2329 case '|':
2330 return TOK_STRATOM;
2309 2331
2310 case '\'': 2332 case '\'':
2311 return TOK_QUOTE; 2333 return TOK_QUOTE;
2312 2334
2313 case ';': 2335 case ';':
2480 { 2502 {
2481 p = SCHEME_V->strbuff; 2503 p = SCHEME_V->strbuff;
2482 2504
2483 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2505 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2484 { 2506 {
2485 if (num_is_integer (l)) 2507 if (is_integer (l))
2486 xnum (p, ivalue_unchecked (l)); 2508 xnum (p, ivalue_unchecked (l));
2487#if USE_REAL 2509#if USE_REAL
2488 else 2510 else
2489 { 2511 {
2490 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2512 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2745 return 0; 2767 return 0;
2746 } 2768 }
2747 else if (is_number (a)) 2769 else if (is_number (a))
2748 { 2770 {
2749 if (is_number (b)) 2771 if (is_number (b))
2750 if (num_is_integer (a) == num_is_integer (b))
2751 return num_cmp (nvalue (a), nvalue (b)) == 0; 2772 return num_cmp (nvalue (a), nvalue (b)) == 0;
2752 2773
2753 return 0; 2774 return 0;
2754 } 2775 }
2755 else if (is_character (a)) 2776 else if (is_character (a))
2756 { 2777 {
2782/* () is #t in R5RS */ 2803/* () is #t in R5RS */
2783#define is_true(p) ((p) != S_F) 2804#define is_true(p) ((p) != S_F)
2784#define is_false(p) ((p) == S_F) 2805#define is_false(p) ((p) == S_F)
2785 2806
2786/* ========== Environment implementation ========== */ 2807/* ========== 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 2808
2803#ifndef USE_ALIST_ENV 2809#ifndef USE_ALIST_ENV
2804 2810
2805/* 2811/*
2806 * In this implementation, each frame of the environment may be 2812 * In this implementation, each frame of the environment may be
2823 2829
2824 SCHEME_V->envir = immutable_cons (new_frame, old_env); 2830 SCHEME_V->envir = immutable_cons (new_frame, old_env);
2825 setenvironment (SCHEME_V->envir); 2831 setenvironment (SCHEME_V->envir);
2826} 2832}
2827 2833
2828static INLINE void 2834static uint32_t
2835sym_hash (pointer sym, uint32_t size)
2836{
2837 uintptr_t ptr = (uintptr_t)sym;
2838
2839#if 0
2840 /* table size is prime, so why mix */
2841 ptr += ptr >> 32;
2842 ptr += ptr >> 16;
2843 ptr += ptr >> 8;
2844#endif
2845
2846 return ptr % size;
2847}
2848
2849ecb_inline void
2829new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2850new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2830{ 2851{
2831 pointer slot = immutable_cons (variable, value); 2852 pointer slot = immutable_cons (variable, value);
2832 2853
2833 if (is_vector (car (env))) 2854 if (is_vector (car (env)))
2834 { 2855 {
2835 int location = hash_fn (symname (variable), veclength (car (env))); 2856 int location = sym_hash (variable, veclength (car (env)));
2836
2837 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2857 vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location)));
2838 } 2858 }
2839 else 2859 else
2840 set_car (env, immutable_cons (slot, car (env))); 2860 set_car (env, immutable_cons (slot, car (env)));
2841} 2861}
2842 2862
2843static pointer 2863static pointer
2844find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2864find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2845{ 2865{
2846 pointer x, y; 2866 pointer x, y;
2847 int location;
2848 2867
2849 for (x = env; x != NIL; x = cdr (x)) 2868 for (x = env; x != NIL; x = cdr (x))
2850 { 2869 {
2851 if (is_vector (car (x))) 2870 if (is_vector (car (x)))
2852 { 2871 {
2853 location = hash_fn (symname (hdl), veclength (car (x))); 2872 int location = sym_hash (hdl, veclength (car (x)));
2854 y = vector_elem (car (x), location); 2873 y = vector_get (car (x), location);
2855 } 2874 }
2856 else 2875 else
2857 y = car (x); 2876 y = car (x);
2858 2877
2859 for (; y != NIL; y = cdr (y)) 2878 for (; y != NIL; y = cdr (y))
2860 if (caar (y) == hdl) 2879 if (caar (y) == hdl)
2861 break; 2880 break;
2862 2881
2863 if (y != NIL) 2882 if (y != NIL)
2883 return car (y);
2884
2885 if (!all)
2864 break; 2886 break;
2865
2866 if (!all)
2867 return NIL;
2868 } 2887 }
2869
2870 if (x != NIL)
2871 return car (y);
2872 2888
2873 return NIL; 2889 return NIL;
2874} 2890}
2875 2891
2876#else /* USE_ALIST_ENV */ 2892#else /* USE_ALIST_ENV */
2877 2893
2878static INLINE void 2894ecb_inline void
2879new_frame_in_env (SCHEME_P_ pointer old_env) 2895new_frame_in_env (SCHEME_P_ pointer old_env)
2880{ 2896{
2881 SCHEME_V->envir = immutable_cons (NIL, old_env); 2897 SCHEME_V->envir = immutable_cons (NIL, old_env);
2882 setenvironment (SCHEME_V->envir); 2898 setenvironment (SCHEME_V->envir);
2883} 2899}
2884 2900
2885static INLINE void 2901ecb_inline void
2886new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2902new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2887{ 2903{
2888 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2904 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2889} 2905}
2890 2906
2898 for (y = car (x); y != NIL; y = cdr (y)) 2914 for (y = car (x); y != NIL; y = cdr (y))
2899 if (caar (y) == hdl) 2915 if (caar (y) == hdl)
2900 break; 2916 break;
2901 2917
2902 if (y != NIL) 2918 if (y != NIL)
2919 return car (y);
2903 break; 2920 break;
2904 2921
2905 if (!all) 2922 if (!all)
2906 return NIL; 2923 break;
2907 } 2924 }
2908
2909 if (x != NIL)
2910 return car (y);
2911 2925
2912 return NIL; 2926 return NIL;
2913} 2927}
2914 2928
2915#endif /* USE_ALIST_ENV else */ 2929#endif /* USE_ALIST_ENV else */
2916 2930
2917static INLINE void 2931ecb_inline void
2918new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2932new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2919{ 2933{
2920 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2934 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2921} 2935}
2922 2936
2923static INLINE void 2937ecb_inline void
2924set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2938set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2925{ 2939{
2926 set_cdr (slot, value); 2940 set_cdr (slot, value);
2927} 2941}
2928 2942
2929static INLINE pointer 2943ecb_inline pointer
2930slot_value_in_env (pointer slot) 2944slot_value_in_env (pointer slot)
2931{ 2945{
2932 return cdr (slot); 2946 return cdr (slot);
2933} 2947}
2934 2948
3062 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3076 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3063 3077
3064 return 0; 3078 return 0;
3065} 3079}
3066 3080
3067static INLINE void 3081ecb_inline void
3068dump_stack_reset (SCHEME_P) 3082dump_stack_reset (SCHEME_P)
3069{ 3083{
3070 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3084 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3071 SCHEME_V->dump = (pointer)+0; 3085 SCHEME_V->dump = (pointer)+0;
3072} 3086}
3073 3087
3074static INLINE void 3088ecb_inline void
3075dump_stack_initialize (SCHEME_P) 3089dump_stack_initialize (SCHEME_P)
3076{ 3090{
3077 SCHEME_V->dump_size = 0; 3091 SCHEME_V->dump_size = 0;
3078 SCHEME_V->dump_base = 0; 3092 SCHEME_V->dump_base = 0;
3079 dump_stack_reset (SCHEME_A); 3093 dump_stack_reset (SCHEME_A);
3132 int i = 0; 3146 int i = 0;
3133 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3147 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3134 3148
3135 while (cont != NIL) 3149 while (cont != NIL)
3136 { 3150 {
3137 frame->op = ivalue (car (cont)); cont = cdr (cont); 3151 frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont);
3138 frame->args = car (cont) ; cont = cdr (cont); 3152 frame->args = car (cont) ; cont = cdr (cont);
3139 frame->envir = car (cont) ; cont = cdr (cont); 3153 frame->envir = car (cont) ; cont = cdr (cont);
3140 frame->code = car (cont) ; cont = cdr (cont); 3154 frame->code = car (cont) ; cont = cdr (cont);
3141 3155
3142 ++frame; 3156 ++frame;
3143 ++i; 3157 ++i;
3144 } 3158 }
3145 3159
3146 SCHEME_V->dump = (pointer)(uintptr_t)i; 3160 SCHEME_V->dump = (pointer)(uintptr_t)i;
3147} 3161}
3148 3162
3149#else 3163#else
3150 3164
3151static INLINE void 3165ecb_inline void
3152dump_stack_reset (SCHEME_P) 3166dump_stack_reset (SCHEME_P)
3153{ 3167{
3154 SCHEME_V->dump = NIL; 3168 SCHEME_V->dump = NIL;
3155} 3169}
3156 3170
3157static INLINE void 3171ecb_inline void
3158dump_stack_initialize (SCHEME_P) 3172dump_stack_initialize (SCHEME_P)
3159{ 3173{
3160 dump_stack_reset (SCHEME_A); 3174 dump_stack_reset (SCHEME_A);
3161} 3175}
3162 3176
3174 SCHEME_V->value = a; 3188 SCHEME_V->value = a;
3175 3189
3176 if (dump == NIL) 3190 if (dump == NIL)
3177 return -1; 3191 return -1;
3178 3192
3179 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3193 SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump);
3180 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3194 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3181 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3195 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3182 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3196 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3183 3197
3184 SCHEME_V->dump = dump; 3198 SCHEME_V->dump = dump;
3185 3199
3186 return 0; 3200 return 0;
3187} 3201}
3393 3407
3394 case OP_TRACING: 3408 case OP_TRACING:
3395 { 3409 {
3396 int tr = SCHEME_V->tracing; 3410 int tr = SCHEME_V->tracing;
3397 3411
3398 SCHEME_V->tracing = ivalue (car (args)); 3412 SCHEME_V->tracing = ivalue_unchecked (car (args));
3399 s_return (mk_integer (SCHEME_A_ tr)); 3413 s_return (mk_integer (SCHEME_A_ tr));
3400 } 3414 }
3401 3415
3402#endif 3416#endif
3403 3417
3912 SCHEME_V->code = car (args); 3926 SCHEME_V->code = car (args);
3913 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 3927 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3914 s_goto (OP_APPLY); 3928 s_goto (OP_APPLY);
3915 } 3929 }
3916 3930
3917 abort (); 3931 if (USE_ERROR_CHECKING) abort ();
3918} 3932}
3919 3933
3920static int 3934static int
3921opexe_1 (SCHEME_P_ enum scheme_opcodes op) 3935opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3922{ 3936{
3923 pointer args = SCHEME_V->args; 3937 pointer args = SCHEME_V->args;
3924 pointer x = car (args); 3938 pointer x = car (args);
3925 num v; 3939 num v;
3926 3940
3927#if USE_MATH
3928 RVALUE dd;
3929#endif
3930
3931 switch (op) 3941 switch (op)
3932 { 3942 {
3933#if USE_MATH 3943#if USE_MATH
3934 case OP_INEX2EX: /* inexact->exact */ 3944 case OP_INEX2EX: /* inexact->exact */
3945 {
3935 if (num_is_integer (x)) 3946 if (is_integer (x))
3936 s_return (x); 3947 s_return (x);
3937 else if (modf (rvalue_unchecked (x), &dd) == 0) 3948
3949 RVALUE r = rvalue_unchecked (x);
3950
3951 if (r == (RVALUE)(IVALUE)r)
3938 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3952 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x)));
3939 else 3953 else
3940 Error_1 ("inexact->exact: not integral:", x); 3954 Error_1 ("inexact->exact: not integral:", x);
3955 }
3941 3956
3942 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3957 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
3943 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 3958 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))));
3944 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 3959 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
3945 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 3960 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
3963 { 3978 {
3964 RVALUE result; 3979 RVALUE result;
3965 int real_result = 1; 3980 int real_result = 1;
3966 pointer y = cadr (args); 3981 pointer y = cadr (args);
3967 3982
3968 if (num_is_integer (x) && num_is_integer (y)) 3983 if (is_integer (x) && is_integer (y))
3969 real_result = 0; 3984 real_result = 0;
3970 3985
3971 /* This 'if' is an R5RS compatibility fix. */ 3986 /* This 'if' is an R5RS compatibility fix. */
3972 /* NOTE: Remove this 'if' fix for R6RS. */ 3987 /* NOTE: Remove this 'if' fix for R6RS. */
3973 if (rvalue (x) == 0 && rvalue (y) < 0) 3988 if (rvalue (x) == 0 && rvalue (y) < 0)
3979 /* If the test fails, result is too big for integer. */ 3994 /* If the test fails, result is too big for integer. */
3980 if (!real_result) 3995 if (!real_result)
3981 { 3996 {
3982 long result_as_long = result; 3997 long result_as_long = result;
3983 3998
3984 if (result != (RVALUE) result_as_long) 3999 if (result != result_as_long)
3985 real_result = 1; 4000 real_result = 1;
3986 } 4001 }
3987 4002
3988 if (real_result) 4003 if (real_result)
3989 s_return (mk_real (SCHEME_A_ result)); 4004 s_return (mk_real (SCHEME_A_ result));
3994 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4009 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
3995 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); 4010 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
3996 4011
3997 case OP_TRUNCATE: 4012 case OP_TRUNCATE:
3998 { 4013 {
3999 RVALUE rvalue_of_x; 4014 RVALUE n = rvalue (x);
4000
4001 rvalue_of_x = rvalue (x);
4002
4003 if (rvalue_of_x > 0)
4004 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); 4015 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4005 else
4006 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4007 } 4016 }
4008 4017
4009 case OP_ROUND: 4018 case OP_ROUND:
4010 if (num_is_integer (x)) 4019 if (is_integer (x))
4011 s_return (x); 4020 s_return (x);
4012 4021
4013 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4022 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4014#endif 4023#endif
4015 4024
4016 case OP_ADD: /* + */ 4025 case OP_ADD: /* + */
4017 v = num_zero; 4026 v = num_zero;
4018 4027
4019 for (x = args; x != NIL; x = cdr (x)) 4028 for (x = args; x != NIL; x = cdr (x))
4020 v = num_op ('+', v, nvalue (car (x))); 4029 v = num_op (NUM_ADD, v, nvalue (car (x)));
4021 4030
4022 s_return (mk_number (SCHEME_A_ v)); 4031 s_return (mk_number (SCHEME_A_ v));
4023 4032
4024 case OP_MUL: /* * */ 4033 case OP_MUL: /* * */
4025 v = num_one; 4034 v = num_one;
4026 4035
4027 for (x = args; x != NIL; x = cdr (x)) 4036 for (x = args; x != NIL; x = cdr (x))
4028 v = num_op ('+', v, nvalue (car (x))); 4037 v = num_op (NUM_MUL, v, nvalue (car (x)));
4029 4038
4030 s_return (mk_number (SCHEME_A_ v)); 4039 s_return (mk_number (SCHEME_A_ v));
4031 4040
4032 case OP_SUB: /* - */ 4041 case OP_SUB: /* - */
4033 if (cdr (args) == NIL) 4042 if (cdr (args) == NIL)
4040 x = cdr (args); 4049 x = cdr (args);
4041 v = nvalue (car (args)); 4050 v = nvalue (car (args));
4042 } 4051 }
4043 4052
4044 for (; x != NIL; x = cdr (x)) 4053 for (; x != NIL; x = cdr (x))
4045 v = num_op ('+', v, nvalue (car (x))); 4054 v = num_op (NUM_SUB, v, nvalue (car (x)));
4046 4055
4047 s_return (mk_number (SCHEME_A_ v)); 4056 s_return (mk_number (SCHEME_A_ v));
4048 4057
4049 case OP_DIV: /* / */ 4058 case OP_DIV: /* / */
4050 if (cdr (args) == NIL) 4059 if (cdr (args) == NIL)
4057 x = cdr (args); 4066 x = cdr (args);
4058 v = nvalue (car (args)); 4067 v = nvalue (car (args));
4059 } 4068 }
4060 4069
4061 for (; x != NIL; x = cdr (x)) 4070 for (; x != NIL; x = cdr (x))
4062 {
4063 if (!is_zero_rvalue (rvalue (car (x)))) 4071 if (!is_zero_rvalue (rvalue (car (x))))
4064 v = num_div (v, nvalue (car (x))); 4072 v = num_div (v, nvalue (car (x)));
4065 else 4073 else
4066 Error_0 ("/: division by zero"); 4074 Error_0 ("/: division by zero");
4067 }
4068 4075
4069 s_return (mk_number (SCHEME_A_ v)); 4076 s_return (mk_number (SCHEME_A_ v));
4070 4077
4071 case OP_INTDIV: /* quotient */ 4078 case OP_INTDIV: /* quotient */
4072 if (cdr (args) == NIL) 4079 if (cdr (args) == NIL)
4081 } 4088 }
4082 4089
4083 for (; x != NIL; x = cdr (x)) 4090 for (; x != NIL; x = cdr (x))
4084 { 4091 {
4085 if (ivalue (car (x)) != 0) 4092 if (ivalue (car (x)) != 0)
4086 v = num_op ('/', v, nvalue (car (x))); 4093 v = num_op (NUM_INTDIV, v, nvalue (car (x)));
4087 else 4094 else
4088 Error_0 ("quotient: division by zero"); 4095 Error_0 ("quotient: division by zero");
4089 } 4096 }
4090 4097
4091 s_return (mk_number (SCHEME_A_ v)); 4098 s_return (mk_number (SCHEME_A_ v));
4137 } 4144 }
4138 else 4145 else
4139 Error_0 ("set-cdr!: unable to alter immutable pair"); 4146 Error_0 ("set-cdr!: unable to alter immutable pair");
4140 4147
4141 case OP_CHAR2INT: /* char->integer */ 4148 case OP_CHAR2INT: /* char->integer */
4142 s_return (mk_integer (SCHEME_A_ ivalue (x))); 4149 s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x)));
4143 4150
4144 case OP_INT2CHAR: /* integer->char */ 4151 case OP_INT2CHAR: /* integer->char */
4145 s_return (mk_character (SCHEME_A_ ivalue (x))); 4152 s_return (mk_character (SCHEME_A_ ivalue_unchecked (x)));
4146 4153
4147 case OP_CHARUPCASE: 4154 case OP_CHARUPCASE:
4148 { 4155 {
4149 unsigned char c = ivalue (x); 4156 unsigned char c = ivalue_unchecked (x);
4150 c = toupper (c); 4157 c = toupper (c);
4151 s_return (mk_character (SCHEME_A_ c)); 4158 s_return (mk_character (SCHEME_A_ c));
4152 } 4159 }
4153 4160
4154 case OP_CHARDNCASE: 4161 case OP_CHARDNCASE:
4155 { 4162 {
4156 unsigned char c = ivalue (x); 4163 unsigned char c = ivalue_unchecked (x);
4157 c = tolower (c); 4164 c = tolower (c);
4158 s_return (mk_character (SCHEME_A_ c)); 4165 s_return (mk_character (SCHEME_A_ c));
4159 } 4166 }
4160 4167
4161 case OP_STR2SYM: /* string->symbol */ 4168 case OP_STR2SYM: /* string->symbol */
4238 Error_1 ("atom->string: not an atom:", x); 4245 Error_1 ("atom->string: not an atom:", x);
4239 } 4246 }
4240 4247
4241 case OP_MKSTRING: /* make-string */ 4248 case OP_MKSTRING: /* make-string */
4242 { 4249 {
4243 int fill = ' '; 4250 int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' ';
4244 int len;
4245
4246 len = ivalue (x); 4251 int len = ivalue_unchecked (x);
4247
4248 if (cdr (args) != NIL)
4249 fill = charvalue (cadr (args));
4250 4252
4251 s_return (mk_empty_string (SCHEME_A_ len, fill)); 4253 s_return (mk_empty_string (SCHEME_A_ len, fill));
4252 } 4254 }
4253 4255
4254 case OP_STRLEN: /* string-length */ 4256 case OP_STRLEN: /* string-length */
4255 s_return (mk_integer (SCHEME_A_ strlength (x))); 4257 s_return (mk_integer (SCHEME_A_ strlength (x)));
4256 4258
4257 case OP_STRREF: /* string-ref */ 4259 case OP_STRREF: /* string-ref */
4258 { 4260 {
4259 char *str;
4260 int index;
4261
4262 str = strvalue (x); 4261 char *str = strvalue (x);
4263
4264 index = ivalue (cadr (args)); 4262 int index = ivalue_unchecked (cadr (args));
4265 4263
4266 if (index >= strlength (x)) 4264 if (index >= strlength (x))
4267 Error_1 ("string-ref: out of bounds:", cadr (args)); 4265 Error_1 ("string-ref: out of bounds:", cadr (args));
4268 4266
4269 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); 4267 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4270 } 4268 }
4271 4269
4272 case OP_STRSET: /* string-set! */ 4270 case OP_STRSET: /* string-set! */
4273 { 4271 {
4274 char *str; 4272 char *str = strvalue (x);
4275 int index; 4273 int index = ivalue_unchecked (cadr (args));
4276 int c; 4274 int c;
4277 4275
4278 if (is_immutable (x)) 4276 if (is_immutable (x))
4279 Error_1 ("string-set!: unable to alter immutable string:", x); 4277 Error_1 ("string-set!: unable to alter immutable string:", x);
4280
4281 str = strvalue (x);
4282
4283 index = ivalue (cadr (args));
4284 4278
4285 if (index >= strlength (x)) 4279 if (index >= strlength (x))
4286 Error_1 ("string-set!: out of bounds:", cadr (args)); 4280 Error_1 ("string-set!: out of bounds:", cadr (args));
4287 4281
4288 c = charvalue (caddr (args)); 4282 c = charvalue (caddr (args));
4311 s_return (newstr); 4305 s_return (newstr);
4312 } 4306 }
4313 4307
4314 case OP_SUBSTR: /* substring */ 4308 case OP_SUBSTR: /* substring */
4315 { 4309 {
4316 char *str; 4310 char *str = strvalue (x);
4317 int index0; 4311 int index0 = ivalue_unchecked (cadr (args));
4318 int index1; 4312 int index1;
4319 int len; 4313 int len;
4320 4314
4321 str = strvalue (x);
4322
4323 index0 = ivalue (cadr (args));
4324
4325 if (index0 > strlength (x)) 4315 if (index0 > strlength (x))
4326 Error_1 ("substring: start out of bounds:", cadr (args)); 4316 Error_1 ("substring: start out of bounds:", cadr (args));
4327 4317
4328 if (cddr (args) != NIL) 4318 if (cddr (args) != NIL)
4329 { 4319 {
4330 index1 = ivalue (caddr (args)); 4320 index1 = ivalue_unchecked (caddr (args));
4331 4321
4332 if (index1 > strlength (x) || index1 < index0) 4322 if (index1 > strlength (x) || index1 < index0)
4333 Error_1 ("substring: end out of bounds:", caddr (args)); 4323 Error_1 ("substring: end out of bounds:", caddr (args));
4334 } 4324 }
4335 else 4325 else
4358 if (SCHEME_V->no_memory) 4348 if (SCHEME_V->no_memory)
4359 s_return (S_SINK); 4349 s_return (S_SINK);
4360#endif 4350#endif
4361 4351
4362 for (x = args, i = 0; is_pair (x); x = cdr (x), i++) 4352 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4363 set_vector_elem (vec, i, car (x)); 4353 vector_set (vec, i, car (x));
4364 4354
4365 s_return (vec); 4355 s_return (vec);
4366 } 4356 }
4367 4357
4368 case OP_MKVECTOR: /* make-vector */ 4358 case OP_MKVECTOR: /* make-vector */
4369 { 4359 {
4370 pointer fill = NIL; 4360 pointer fill = NIL;
4371 int len;
4372 pointer vec; 4361 pointer vec;
4373
4374 len = ivalue (x); 4362 int len = ivalue_unchecked (x);
4375 4363
4376 if (cdr (args) != NIL) 4364 if (cdr (args) != NIL)
4377 fill = cadr (args); 4365 fill = cadr (args);
4378 4366
4379 vec = mk_vector (SCHEME_A_ len); 4367 vec = mk_vector (SCHEME_A_ len);
4382 if (SCHEME_V->no_memory) 4370 if (SCHEME_V->no_memory)
4383 s_return (S_SINK); 4371 s_return (S_SINK);
4384#endif 4372#endif
4385 4373
4386 if (fill != NIL) 4374 if (fill != NIL)
4387 fill_vector (vec, fill); 4375 fill_vector (vec, 0, fill);
4388 4376
4389 s_return (vec); 4377 s_return (vec);
4390 } 4378 }
4391 4379
4392 case OP_VECLEN: /* vector-length */ 4380 case OP_VECLEN: /* vector-length */
4393 s_return (mk_integer (SCHEME_A_ veclength (x))); 4381 s_return (mk_integer (SCHEME_A_ veclength (x)));
4394 4382
4395 case OP_VECREF: /* vector-ref */ 4383 case OP_VECREF: /* vector-ref */
4396 { 4384 {
4397 int index;
4398
4399 index = ivalue (cadr (args)); 4385 int index = ivalue_unchecked (cadr (args));
4400 4386
4401 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4387 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4402 Error_1 ("vector-ref: out of bounds:", cadr (args)); 4388 Error_1 ("vector-ref: out of bounds:", cadr (args));
4403 4389
4404 s_return (vector_elem (x, index)); 4390 s_return (vector_get (x, index));
4405 } 4391 }
4406 4392
4407 case OP_VECSET: /* vector-set! */ 4393 case OP_VECSET: /* vector-set! */
4408 { 4394 {
4409 int index; 4395 int index = ivalue_unchecked (cadr (args));
4410 4396
4411 if (is_immutable (x)) 4397 if (is_immutable (x))
4412 Error_1 ("vector-set!: unable to alter immutable vector:", x); 4398 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4413 4399
4414 index = ivalue (cadr (args));
4415
4416 if (index >= veclength (car (args)) && USE_ERROR_CHECKING) 4400 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4417 Error_1 ("vector-set!: out of bounds:", cadr (args)); 4401 Error_1 ("vector-set!: out of bounds:", cadr (args));
4418 4402
4419 set_vector_elem (x, index, caddr (args)); 4403 vector_set (x, index, caddr (args));
4420 s_return (x); 4404 s_return (x);
4421 } 4405 }
4422 } 4406 }
4423 4407
4424 abort (); 4408 if (USE_ERROR_CHECKING) abort ();
4425}
4426
4427INTERFACE int
4428is_list (SCHEME_P_ pointer a)
4429{
4430 return list_length (SCHEME_A_ a) >= 0;
4431}
4432
4433/* Result is:
4434 proper list: length
4435 circular list: -1
4436 not even a pair: -2
4437 dotted list: -2 minus length before dot
4438*/
4439INTERFACE int
4440list_length (SCHEME_P_ pointer a)
4441{
4442 int i = 0;
4443 pointer slow, fast;
4444
4445 slow = fast = a;
4446
4447 while (1)
4448 {
4449 if (fast == NIL)
4450 return i;
4451
4452 if (!is_pair (fast))
4453 return -2 - i;
4454
4455 fast = cdr (fast);
4456 ++i;
4457
4458 if (fast == NIL)
4459 return i;
4460
4461 if (!is_pair (fast))
4462 return -2 - i;
4463
4464 ++i;
4465 fast = cdr (fast);
4466
4467 /* Safe because we would have already returned if `fast'
4468 encountered a non-pair. */
4469 slow = cdr (slow);
4470
4471 if (fast == slow)
4472 {
4473 /* the fast pointer has looped back around and caught up
4474 with the slow pointer, hence the structure is circular,
4475 not of finite length, and therefore not a list */
4476 return -1;
4477 }
4478 }
4479} 4409}
4480 4410
4481static int 4411static int
4482opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4412opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4483{ 4413{
4529 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; 4459 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4530 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ 4460 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4531 case OP_CHARP: /* char? */ r = is_character (a) ; break; 4461 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4532 4462
4533#if USE_CHAR_CLASSIFIERS 4463#if USE_CHAR_CLASSIFIERS
4534 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; 4464 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break;
4535 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; 4465 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break;
4536 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; 4466 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break;
4537 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; 4467 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break;
4538 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; 4468 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break;
4539#endif 4469#endif
4540 4470
4541#if USE_PORTS 4471#if USE_PORTS
4542 case OP_PORTP: /* port? */ r = is_port (a) ; break; 4472 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4543 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; 4473 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4744 4674
4745 case OP_NEWSEGMENT: /* new-segment */ 4675 case OP_NEWSEGMENT: /* new-segment */
4746 if (!is_pair (args) || !is_number (a)) 4676 if (!is_pair (args) || !is_number (a))
4747 Error_0 ("new-segment: argument must be a number"); 4677 Error_0 ("new-segment: argument must be a number");
4748 4678
4749 alloc_cellseg (SCHEME_A_ (int)ivalue (a)); 4679 alloc_cellseg (SCHEME_A_ ivalue (a));
4750 4680
4751 s_return (S_T); 4681 s_return (S_T);
4752 4682
4753 case OP_OBLIST: /* oblist */ 4683 case OP_OBLIST: /* oblist */
4754 s_return (oblist_all_symbols (SCHEME_A)); 4684 s_return (oblist_all_symbols (SCHEME_A));
4783 break; 4713 break;
4784 } 4714 }
4785 4715
4786 p = port_from_filename (SCHEME_A_ strvalue (a), prop); 4716 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4787 4717
4788 if (p == NIL) 4718 s_return (p == NIL ? S_F : p);
4789 s_return (S_F);
4790
4791 s_return (p);
4792 } 4719 }
4793 4720
4794# if USE_STRING_PORTS 4721# if USE_STRING_PORTS
4795 4722
4796 case OP_OPEN_INSTRING: /* open-input-string */ 4723 case OP_OPEN_INSTRING: /* open-input-string */
4811 } 4738 }
4812 4739
4813 p = port_from_string (SCHEME_A_ strvalue (a), 4740 p = port_from_string (SCHEME_A_ strvalue (a),
4814 strvalue (a) + strlength (a), prop); 4741 strvalue (a) + strlength (a), prop);
4815 4742
4816 if (p == NIL) 4743 s_return (p == NIL ? S_F : p);
4817 s_return (S_F);
4818
4819 s_return (p);
4820 } 4744 }
4821 4745
4822 case OP_OPEN_OUTSTRING: /* open-output-string */ 4746 case OP_OPEN_OUTSTRING: /* open-output-string */
4823 { 4747 {
4824 pointer p; 4748 pointer p;
4825 4749
4826 if (a == NIL) 4750 if (a == NIL)
4827 {
4828 p = port_from_scratch (SCHEME_A); 4751 p = port_from_scratch (SCHEME_A);
4829
4830 if (p == NIL)
4831 s_return (S_F);
4832 }
4833 else 4752 else
4834 {
4835 p = port_from_string (SCHEME_A_ strvalue (a), 4753 p = port_from_string (SCHEME_A_ strvalue (a),
4836 strvalue (a) + strlength (a), port_output); 4754 strvalue (a) + strlength (a), port_output);
4837 4755
4838 if (p == NIL) 4756 s_return (p == NIL ? S_F : p);
4839 s_return (S_F);
4840 }
4841
4842 s_return (p);
4843 } 4757 }
4844 4758
4845 case OP_GET_OUTSTRING: /* get-output-string */ 4759 case OP_GET_OUTSTRING: /* get-output-string */
4846 { 4760 {
4847 port *p; 4761 port *p;
4886 case OP_CURR_ENV: /* current-environment */ 4800 case OP_CURR_ENV: /* current-environment */
4887 s_return (SCHEME_V->envir); 4801 s_return (SCHEME_V->envir);
4888 4802
4889 } 4803 }
4890 4804
4891 abort (); 4805 if (USE_ERROR_CHECKING) abort ();
4892} 4806}
4893 4807
4894static int 4808static int
4895opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4809opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4896{ 4810{
5028 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); 4942 s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL);
5029 SCHEME_V->tok = token (SCHEME_A); 4943 SCHEME_V->tok = token (SCHEME_A);
5030 s_goto (OP_RDSEXPR); 4944 s_goto (OP_RDSEXPR);
5031 4945
5032 case TOK_ATOM: 4946 case TOK_ATOM:
5033 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); 4947 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS)));
4948
4949 case TOK_DOTATOM:
4950 SCHEME_V->strbuff[0] = '.';
4951 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5034 4952
5035 case TOK_DQUOTE: 4953 case TOK_DQUOTE:
5036 x = readstrexp (SCHEME_A); 4954 x = readstrexp (SCHEME_A_ '"');
5037 4955
5038 if (x == S_F) 4956 if (x == S_F)
5039 Error_0 ("Error reading string"); 4957 Error_0 ("Error reading string");
5040 4958
5041 setimmutable (x); 4959 setimmutable (x);
5053 s_goto (OP_EVAL); 4971 s_goto (OP_EVAL);
5054 } 4972 }
5055 } 4973 }
5056 4974
5057 case TOK_SHARP_CONST: 4975 case TOK_SHARP_CONST:
5058 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) 4976 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5059 Error_0 ("undefined sharp expression"); 4977 Error_0 ("undefined sharp expression");
5060 else 4978 else
5061 s_return (x); 4979 s_return (x);
5062 4980
5063 default: 4981 default:
5215 putstr (SCHEME_A_ ")"); 5133 putstr (SCHEME_A_ ")");
5216 s_return (S_T); 5134 s_return (S_T);
5217 } 5135 }
5218 else 5136 else
5219 { 5137 {
5220 pointer elem = vector_elem (vec, i); 5138 pointer elem = vector_get (vec, i);
5221 5139
5222 ivalue_unchecked (cdr (args)) = i + 1; 5140 ivalue_unchecked (cdr (args)) = i + 1;
5223 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5141 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5224 SCHEME_V->args = elem; 5142 SCHEME_V->args = elem;
5225 5143
5229 s_goto (OP_P0LIST); 5147 s_goto (OP_P0LIST);
5230 } 5148 }
5231 } 5149 }
5232 } 5150 }
5233 5151
5234 abort (); 5152 if (USE_ERROR_CHECKING) abort ();
5235} 5153}
5236 5154
5237static int 5155static int
5238opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5156opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5239{ 5157{
5292 5210
5293 case OP_MACROP: /* macro? */ 5211 case OP_MACROP: /* macro? */
5294 s_retbool (is_macro (a)); 5212 s_retbool (is_macro (a));
5295 } 5213 }
5296 5214
5297 abort (); 5215 if (USE_ERROR_CHECKING) abort ();
5298} 5216}
5299 5217
5300/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5218/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5301typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5219typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5302 5220
5303typedef int (*test_predicate)(pointer); 5221typedef int (*test_predicate)(pointer);
5304static int 5222static int
5305is_any (pointer p) 5223tst_any (pointer p)
5306{ 5224{
5307 return 1; 5225 return 1;
5308} 5226}
5309 5227
5310static int 5228static int
5311is_nonneg (pointer p) 5229tst_inonneg (pointer p)
5312{ 5230{
5313 return ivalue (p) >= 0 && is_integer (p); 5231 return is_integer (p) && ivalue_unchecked (p) >= 0;
5314} 5232}
5315 5233
5316static int 5234static int
5317tst_is_list (pointer p) 5235tst_is_list (SCHEME_P_ pointer p)
5318{ 5236{
5319 return p == NIL || is_pair (p); 5237 return p == NIL || is_pair (p);
5320} 5238}
5321 5239
5322/* Correspond carefully with following defines! */ 5240/* Correspond carefully with following defines! */
5323static struct 5241static struct
5324{ 5242{
5325 test_predicate fct; 5243 test_predicate fct;
5326 const char *kind; 5244 const char *kind;
5327} tests[] = 5245} tests[] = {
5328{
5329 { is_any, 0 }, 5246 { tst_any , 0 },
5330 { is_string, "string" }, 5247 { is_string , "string" },
5331 { is_symbol, "symbol" }, 5248 { is_symbol , "symbol" },
5332 { is_port, "port" }, 5249 { is_port , "port" },
5333 { is_inport, "input port" }, 5250 { is_inport , "input port" },
5334 { is_outport, "output port" }, 5251 { is_outport , "output port" },
5335 { is_environment, "environment" }, 5252 { is_environment, "environment" },
5336 { is_pair, "pair" }, 5253 { is_pair , "pair" },
5337 { tst_is_list, "pair or '()" }, 5254 { 0 , "pair or '()" },
5338 { is_character, "character" }, 5255 { is_character , "character" },
5339 { is_vector, "vector" }, 5256 { is_vector , "vector" },
5340 { is_number, "number" }, 5257 { is_number , "number" },
5341 { is_integer, "integer" }, 5258 { is_integer , "integer" },
5342 { is_nonneg, "non-negative integer" } 5259 { tst_inonneg , "non-negative integer" }
5343}; 5260};
5344 5261
5345#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ 5262#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5346#define TST_ANY "\001" 5263#define TST_ANY "\001"
5347#define TST_STRING "\002" 5264#define TST_STRING "\002"
5388typedef struct 5305typedef struct
5389{ 5306{
5390 uint8_t func; 5307 uint8_t func;
5391 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ 5308 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5392 uint8_t builtin; 5309 uint8_t builtin;
5310#if USE_ERROR_CHECKING
5393 uint8_t min_arity; 5311 uint8_t min_arity;
5394 uint8_t max_arity; 5312 uint8_t max_arity;
5395 char arg_tests_encoding[3]; 5313 char arg_tests_encoding[3];
5314#endif
5396} op_code_info; 5315} op_code_info;
5397 5316
5398static const op_code_info dispatch_table[] = { 5317static const op_code_info dispatch_table[] = {
5318#if USE_ERROR_CHECKING
5399#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, 5319#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5320#else
5321#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 },
5322#endif
5400#include "opdefines.h" 5323#include "opdefines.h"
5401#undef OP_DEF 5324#undef OP_DEF
5402 {0} 5325 {0}
5403}; 5326};
5404 5327
5405/* kernel of this interpreter */ 5328/* kernel of this interpreter */
5406static void 5329static void ecb_hot
5407Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5330Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5408{ 5331{
5409 SCHEME_V->op = op; 5332 SCHEME_V->op = op;
5410 5333
5411 for (;;) 5334 for (;;)
5446 { 5369 {
5447 pointer arg = car (arglist); 5370 pointer arg = car (arglist);
5448 5371
5449 j = t[0]; 5372 j = t[0];
5450 5373
5374 /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */
5375 if (j == TST_LIST[0])
5376 {
5377 if (!tst_is_list (SCHEME_A_ arg))
5378 break;
5379 }
5380 else
5381 {
5451 if (!tests[j - 1].fct (arg)) 5382 if (!tests[j - 1].fct (arg))
5452 break; 5383 break;
5384 }
5453 5385
5454 if (t[1]) /* last test is replicated as necessary */ 5386 if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */
5455 t++; 5387 t++;
5456 5388
5457 arglist = cdr (arglist); 5389 arglist = cdr (arglist);
5458 i++; 5390 i++;
5459 } 5391 }
5514mk_proc (SCHEME_P_ enum scheme_opcodes op) 5446mk_proc (SCHEME_P_ enum scheme_opcodes op)
5515{ 5447{
5516 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5448 pointer y = get_cell (SCHEME_A_ NIL, NIL);
5517 set_typeflag (y, (T_PROC | T_ATOM)); 5449 set_typeflag (y, (T_PROC | T_ATOM));
5518 ivalue_unchecked (y) = op; 5450 ivalue_unchecked (y) = op;
5519 set_num_integer (y);
5520 return y; 5451 return y;
5521} 5452}
5522 5453
5523/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5454/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5524static int 5455static int
5589 return OP_C0STREAM; /* cons-stream */ 5520 return OP_C0STREAM; /* cons-stream */
5590 } 5521 }
5591} 5522}
5592 5523
5593#if USE_MULTIPLICITY 5524#if USE_MULTIPLICITY
5594scheme * 5525ecb_cold scheme *
5595scheme_init_new () 5526scheme_init_new ()
5596{ 5527{
5597 scheme *sc = malloc (sizeof (scheme)); 5528 scheme *sc = malloc (sizeof (scheme));
5598 5529
5599 if (!scheme_init (SCHEME_A)) 5530 if (!scheme_init (SCHEME_A))
5604 else 5535 else
5605 return sc; 5536 return sc;
5606} 5537}
5607#endif 5538#endif
5608 5539
5609int 5540ecb_cold int
5610scheme_init (SCHEME_P) 5541scheme_init (SCHEME_P)
5611{ 5542{
5612 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 5543 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5613 pointer x; 5544 pointer x;
5614 5545
5739scheme_set_external_data (SCHEME_P_ void *p) 5670scheme_set_external_data (SCHEME_P_ void *p)
5740{ 5671{
5741 SCHEME_V->ext_data = p; 5672 SCHEME_V->ext_data = p;
5742} 5673}
5743 5674
5744void 5675ecb_cold void
5745scheme_deinit (SCHEME_P) 5676scheme_deinit (SCHEME_P)
5746{ 5677{
5747 int i; 5678 int i;
5748 5679
5749#if SHOW_ERROR_LINE 5680#if SHOW_ERROR_LINE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines