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.1 by root, Wed Nov 25 05:02:56 2015 UTC vs.
Revision 1.11 by root, Thu Nov 26 00:05:20 2015 UTC

1 1/*
2/* T I N Y S C H E M E 1 . 4 1 2 * µscheme
3 *
4 * Copyright (C) 2015 Marc Alexander Lehmann <uscheme@schmorp.de>
5 * do as you want with this, attribution appreciated.
6 *
7 * Based opn tinyscheme-1.41 (original credits follow)
3 * Dimitrios Souflis (dsouflis@acm.org) 8 * Dimitrios Souflis (dsouflis@acm.org)
4 * Based on MiniScheme (original credits follow) 9 * Based on MiniScheme (original credits follow)
5 * (MINISCM) coded by Atsushi Moriwaki (11/5/1989) 10 * (MINISCM) coded by Atsushi Moriwaki (11/5/1989)
6 * (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp 11 * (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp
7 * (MINISCM) This version has been modified by R.C. Secrist. 12 * (MINISCM) This version has been modified by R.C. Secrist.
26#endif 31#endif
27 32
28#include <sys/types.h> 33#include <sys/types.h>
29#include <sys/stat.h> 34#include <sys/stat.h>
30#include <fcntl.h> 35#include <fcntl.h>
36
37#include <string.h>
38#include <stdlib.h>
31 39
32#include <limits.h> 40#include <limits.h>
33#include <inttypes.h> 41#include <inttypes.h>
34#include <float.h> 42#include <float.h>
35//#include <ctype.h> 43//#include <ctype.h>
52 60
53#define BACKQUOTE '`' 61#define BACKQUOTE '`'
54#define DELIMITERS "()\";\f\t\v\n\r " 62#define DELIMITERS "()\";\f\t\v\n\r "
55 63
56#define NIL (&SCHEME_V->xNIL) //TODO: make this 0? 64#define NIL (&SCHEME_V->xNIL) //TODO: make this 0?
57#define S_T (&SCHEME_V->xT) 65#define S_T (&SCHEME_V->xT) //TODO: magic ptr value?
58#define S_F (&SCHEME_V->xF) 66#define S_F (&SCHEME_V->xF) //TODO: magic ptr value?
59#define S_SINK (&SCHEME_V->xsink) 67#define S_SINK (&SCHEME_V->xsink)
60#define S_EOF (&SCHEME_V->xEOF_OBJ) 68#define S_EOF (&SCHEME_V->xEOF_OBJ)
61 69
62/* 70/* should use libecb */
63 * Basic memory allocation units 71#if __GNUC__ >= 4
64 */ 72# define ecb_expect(expr,value) __builtin_expect ((expr),(value))
65 73# define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
66#define banner "TinyScheme 1.41-s" 74# define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
67 75#endif
68#include <string.h>
69#include <stdlib.h>
70 76
71#if !USE_MULTIPLICITY 77#if !USE_MULTIPLICITY
72static scheme sc; 78static scheme sc;
73#endif 79#endif
74 80
134 c += 'a' - 'A'; 140 c += 'a' - 'A';
135 141
136 return c; 142 return c;
137} 143}
138 144
145static int
146xisdigit (char c)
147{
148 return c >= '0' && c <= '9';
149}
150
151#define toupper(c) xtoupper (c)
152#define tolower(c) xtolower (c)
153#define isdigit(c) xisdigit (c)
154
139#if USE_STRLWR 155#if USE_STRLWR
140static const char * 156static const char *
141strlwr (char *s) 157strlwr (char *s)
142{ 158{
143 const char *p = s; 159 const char *p = s;
152} 168}
153#endif 169#endif
154 170
155#define stricmp(a,b) strcmp (a, b) 171#define stricmp(a,b) strcmp (a, b)
156#define strlwr(s) (s) 172#define strlwr(s) (s)
157#define toupper(c) xtoupper(c)
158#define tolower(c) xtolower(c)
159 173
160#ifndef prompt 174#ifndef prompt
161# define prompt "ts> " 175# define prompt "ts> "
162#endif 176#endif
163 177
169# define FIRST_CELLSEGS 3 183# define FIRST_CELLSEGS 3
170#endif 184#endif
171 185
172enum scheme_types 186enum scheme_types
173{ 187{
174 T_NULL, 188 T_FREE,
175 T_STRING, 189 T_STRING,
176 T_NUMBER, 190 T_NUMBER,
177 T_SYMBOL, 191 T_SYMBOL,
178 T_PROC, 192 T_PROC,
179 T_PAIR, 193 T_PAIR,
184 T_PORT, 198 T_PORT,
185 T_VECTOR, 199 T_VECTOR,
186 T_MACRO, 200 T_MACRO,
187 T_PROMISE, 201 T_PROMISE,
188 T_ENVIRONMENT, 202 T_ENVIRONMENT,
203 /* one more... */
189 T_NUM_SYSTEM_TYPES 204 T_NUM_SYSTEM_TYPES
190}; 205};
191 206
192#define T_MASKTYPE 31 /* 0000000000011111 */ 207#define T_MASKTYPE 0x000f
193#define T_SYNTAX 4096 /* 0001000000000000 */ 208#define T_SYNTAX 0x0010
194#define T_IMMUTABLE 8192 /* 0010000000000000 */ 209#define T_IMMUTABLE 0x0020
195#define T_ATOM 16384 /* 0100000000000000 */ /* only for gc */ 210#define T_ATOM 0x0040 /* only for gc */
196#define CLRATOM 49151 /* 1011111111111111 */ /* only for gc */ 211#define T_MARK 0x0080 /* only for gc */
197#define MARK 32768 /* 1000000000000000 */
198#define UNMARK 32767 /* 0111111111111111 */
199
200 212
201static num num_add (num a, num b); 213static num num_add (num a, num b);
202static num num_mul (num a, num b); 214static num num_mul (num a, num b);
203static num num_div (num a, num b); 215static num num_div (num a, num b);
204static num num_intdiv (num a, num b); 216static num num_intdiv (num a, num b);
244is_vector (pointer p) 256is_vector (pointer p)
245{ 257{
246 return type (p) == T_VECTOR; 258 return type (p) == T_VECTOR;
247} 259}
248 260
261#define vecvalue(p) ((p)->object.vector.vvalue)
262#define veclength(p) ((p)->object.vector.length)
249INTERFACE void fill_vector (pointer vec, pointer obj); 263INTERFACE void fill_vector (pointer vec, pointer obj);
264INTERFACE uint32_t vector_length (pointer vec);
250INTERFACE pointer vector_elem (pointer vec, int ielem); 265INTERFACE pointer vector_elem (pointer vec, uint32_t ielem);
251INTERFACE void set_vector_elem (pointer vec, int ielem, pointer a); 266INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a);
267
268INTERFACE uint32_t
269vector_length (pointer vec)
270{
271 return vec->object.vector.length;
272}
252 273
253INTERFACE INLINE int 274INTERFACE INLINE int
254is_number (pointer p) 275is_number (pointer p)
255{ 276{
256 return type (p) == T_NUMBER; 277 return type (p) == T_NUMBER;
290nvalue (pointer p) 311nvalue (pointer p)
291{ 312{
292 return (p)->object.number; 313 return (p)->object.number;
293} 314}
294 315
295INTERFACE long 316static IVALUE
317num_get_ivalue (const num n)
318{
319 return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n);
320}
321
322static RVALUE
323num_get_rvalue (const num n)
324{
325 return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n);
326}
327
328INTERFACE IVALUE
296ivalue (pointer p) 329ivalue (pointer p)
297{ 330{
298 return num_is_integer (p) ? num_ivalue ((p)->object.number) : (long) num_rvalue ((p)->object.number); 331 return num_get_ivalue (p->object.number);
299} 332}
300 333
301INTERFACE RVALUE 334INTERFACE RVALUE
302rvalue (pointer p) 335rvalue (pointer p)
303{ 336{
304 return num_is_integer (p) ? (RVALUE) num_ivalue ((p)->object.number) : num_rvalue ((p)->object.number); 337 return num_get_rvalue (p->object.number);
305} 338}
306 339
307#define ivalue_unchecked(p) ((p)->object.number.value.ivalue) 340#define ivalue_unchecked(p) ((p)->object.number.value.ivalue)
308#if USE_FLOAT 341#if USE_REAL
309# define rvalue_unchecked(p) ((p)->object.number.value.rvalue) 342# define rvalue_unchecked(p) ((p)->object.number.value.rvalue)
310# define set_num_integer(p) (p)->object.number.is_fixnum=1; 343# define set_num_integer(p) (p)->object.number.is_fixnum=1;
311# define set_num_real(p) (p)->object.number.is_fixnum=0; 344# define set_num_real(p) (p)->object.number.is_fixnum=0;
312#else 345#else
313# define rvalue_unchecked(p) ((p)->object.number.value.ivalue) 346# define rvalue_unchecked(p) ((p)->object.number.value.ivalue)
343{ 376{
344 return type (p) == T_PAIR; 377 return type (p) == T_PAIR;
345} 378}
346 379
347#define car(p) ((p)->object.cons.car + 0) 380#define car(p) ((p)->object.cons.car + 0)
348#define cdr(p) ((p)->object.cons.cdr) /* find_consecutive_cells uses &cdr */ 381#define cdr(p) ((p)->object.cons.cdr + 0)
349 382
350#define caar(p) car (car (p)) 383#define caar(p) car (car (p))
351#define cadr(p) car (cdr (p)) 384#define cadr(p) car (cdr (p))
352#define cdar(p) cdr (car (p)) 385#define cdar(p) cdr (car (p))
353#define cddr(p) cdr (cdr (p)) 386#define cddr(p) cdr (cdr (p))
424syntaxname (pointer p) 457syntaxname (pointer p)
425{ 458{
426 return strvalue (car (p)); 459 return strvalue (car (p));
427} 460}
428 461
429#define procnum(p) ivalue(p) 462#define procnum(p) ivalue (p)
430static const char *procname (pointer x); 463static const char *procname (pointer x);
431 464
432INTERFACE INLINE int 465INTERFACE INLINE int
433is_closure (pointer p) 466is_closure (pointer p)
434{ 467{
477 510
478#define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) 511#define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT)
479 512
480#define is_atom(p) (typeflag (p) & T_ATOM) 513#define is_atom(p) (typeflag (p) & T_ATOM)
481#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) 514#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM)
482#define clratom(p) set_typeflag ((p), typeflag (p) & CLRATOM) 515#define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM)
483 516
484#define is_mark(p) (typeflag (p) & MARK) 517#define is_mark(p) (typeflag (p) & T_MARK)
485#define setmark(p) set_typeflag ((p), typeflag (p) | MARK) 518#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
486#define clrmark(p) set_typeflag ((p), typeflag (p) & UNMARK) 519#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
487 520
488INTERFACE INLINE int 521INTERFACE INLINE int
489is_immutable (pointer p) 522is_immutable (pointer p)
490{ 523{
491 return typeflag (p) & T_IMMUTABLE; 524 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
492} 525}
493 526
494INTERFACE INLINE void 527INTERFACE INLINE void
495setimmutable (pointer p) 528setimmutable (pointer p)
496{ 529{
530#if USE_ERROR_CHECKING
497 set_typeflag (p, T_IMMUTABLE); 531 set_typeflag (p, typeflag (p) | T_IMMUTABLE);
532#endif
498} 533}
499 534
500#if USE_CHAR_CLASSIFIERS 535#if USE_CHAR_CLASSIFIERS
501static INLINE int 536static INLINE int
502Cisalpha (int c) 537Cisalpha (int c)
593static int file_push (SCHEME_P_ const char *fname); 628static int file_push (SCHEME_P_ const char *fname);
594static void file_pop (SCHEME_P); 629static void file_pop (SCHEME_P);
595static int file_interactive (SCHEME_P); 630static int file_interactive (SCHEME_P);
596static INLINE int is_one_of (char *s, int c); 631static INLINE int is_one_of (char *s, int c);
597static int alloc_cellseg (SCHEME_P_ int n); 632static int alloc_cellseg (SCHEME_P_ int n);
598static long binary_decode (const char *s);
599static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); 633static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b);
600static pointer xget_cell (SCHEME_P_ pointer a, pointer b);
601static pointer reserve_cells (SCHEME_P_ int n);
602static pointer get_consecutive_cells (SCHEME_P_ int n);
603static pointer find_consecutive_cells (SCHEME_P_ int n);
604static void finalize_cell (SCHEME_P_ pointer a); 634static void finalize_cell (SCHEME_P_ pointer a);
605static int count_consecutive_cells (pointer x, int needed); 635static int count_consecutive_cells (pointer x, int needed);
606static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 636static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
607static pointer mk_number (SCHEME_P_ num n); 637static pointer mk_number (SCHEME_P_ const num n);
608static char *store_string (SCHEME_P_ int len, const char *str, char fill); 638static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
609static pointer mk_vector (SCHEME_P_ int len); 639static pointer mk_vector (SCHEME_P_ uint32_t len);
610static pointer mk_atom (SCHEME_P_ char *q); 640static pointer mk_atom (SCHEME_P_ char *q);
611static pointer mk_sharp_const (SCHEME_P_ char *name); 641static pointer mk_sharp_const (SCHEME_P_ char *name);
612 642
613#if USE_PORTS 643#if USE_PORTS
614static pointer mk_port (SCHEME_P_ port *p); 644static pointer mk_port (SCHEME_P_ port *p);
646static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); 676static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op);
647static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 677static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op);
648static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 678static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op);
649static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 679static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op);
650static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 680static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
651static void assign_syntax (SCHEME_P_ char *name); 681static void assign_syntax (SCHEME_P_ const char *name);
652static int syntaxnum (pointer p); 682static int syntaxnum (pointer p);
653static void assign_proc (SCHEME_P_ enum scheme_opcodes, char *name); 683static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
654 684
655static num 685static num
656num_add (num a, num b) 686num_add (num a, num b)
657{ 687{
658 num ret; 688 num ret;
659 689
660 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 690 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
661 691
662 if (num_is_fixnum (ret)) 692 if (num_is_fixnum (ret))
663 num_set_ivalue (ret, a.value.ivalue + b.value.ivalue); 693 num_set_ivalue (ret, num_get_ivalue (a) + num_get_ivalue (b));
664 else 694 else
665 num_set_rvalue (ret, num_rvalue (a) + num_rvalue (b)); 695 num_set_rvalue (ret, num_get_rvalue (a) + num_get_rvalue (b));
666 696
667 return ret; 697 return ret;
668} 698}
669 699
670static num 700static num
673 num ret; 703 num ret;
674 704
675 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 705 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
676 706
677 if (num_is_fixnum (ret)) 707 if (num_is_fixnum (ret))
678 num_set_ivalue (ret, a.value.ivalue * b.value.ivalue); 708 num_set_ivalue (ret, num_get_ivalue (a) * num_get_ivalue (b));
679 else 709 else
680 num_set_rvalue (ret, num_rvalue (a) * num_rvalue (b)); 710 num_set_rvalue (ret, num_get_rvalue (a) * num_get_rvalue (b));
681 711
682 return ret; 712 return ret;
683} 713}
684 714
685static num 715static num
686num_div (num a, num b) 716num_div (num a, num b)
687{ 717{
688 num ret; 718 num ret;
689 719
690 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && a.value.ivalue % b.value.ivalue == 0); 720 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0);
691 721
692 if (num_is_fixnum (ret)) 722 if (num_is_fixnum (ret))
693 num_set_ivalue (ret, a.value.ivalue / b.value.ivalue); 723 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b));
694 else 724 else
695 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); 725 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b));
696 726
697 return ret; 727 return ret;
698} 728}
699 729
700static num 730static num
703 num ret; 733 num ret;
704 734
705 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 735 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
706 736
707 if (num_is_fixnum (ret)) 737 if (num_is_fixnum (ret))
708 num_set_ivalue (ret, a.value.ivalue / b.value.ivalue); 738 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b));
709 else 739 else
710 num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); 740 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b));
711 741
712 return ret; 742 return ret;
713} 743}
714 744
715static num 745static num
718 num ret; 748 num ret;
719 749
720 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 750 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
721 751
722 if (num_is_fixnum (ret)) 752 if (num_is_fixnum (ret))
723 num_set_ivalue (ret, a.value.ivalue - b.value.ivalue); 753 num_set_ivalue (ret, num_get_ivalue (a) - num_get_ivalue (b));
724 else 754 else
725 num_set_rvalue (ret, num_rvalue (a) - num_rvalue (b)); 755 num_set_rvalue (ret, num_get_rvalue (a) - num_get_rvalue (b));
726 756
727 return ret; 757 return ret;
728} 758}
729 759
730static num 760static num
732{ 762{
733 num ret; 763 num ret;
734 long e1, e2, res; 764 long e1, e2, res;
735 765
736 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 766 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
737 e1 = num_ivalue (a); 767 e1 = num_get_ivalue (a);
738 e2 = num_ivalue (b); 768 e2 = num_get_ivalue (b);
739 res = e1 % e2; 769 res = e1 % e2;
740 770
741 /* remainder should have same sign as second operand */ 771 /* remainder should have same sign as second operand */
742 if (res > 0) 772 if (res > 0)
743 { 773 {
759{ 789{
760 num ret; 790 num ret;
761 long e1, e2, res; 791 long e1, e2, res;
762 792
763 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 793 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
764 e1 = num_ivalue (a); 794 e1 = num_get_ivalue (a);
765 e2 = num_ivalue (b); 795 e2 = num_get_ivalue (b);
766 res = e1 % e2; 796 res = e1 % e2;
767 797
768 /* modulo should have same sign as second operand */ 798 /* modulo should have same sign as second operand */
769 if (res * e2 < 0) 799 if (res * e2 < 0)
770 res += e2; 800 res += e2;
778{ 808{
779 int ret; 809 int ret;
780 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 810 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
781 811
782 if (is_fixnum) 812 if (is_fixnum)
783 ret = a.value.ivalue == b.value.ivalue; 813 ret = num_get_ivalue (a) == num_get_ivalue (b);
784 else 814 else
785 ret = num_rvalue (a) == num_rvalue (b); 815 ret = num_get_rvalue (a) == num_get_rvalue (b);
786 816
787 return ret; 817 return ret;
788} 818}
789 819
790 820
793{ 823{
794 int ret; 824 int ret;
795 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 825 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
796 826
797 if (is_fixnum) 827 if (is_fixnum)
798 ret = a.value.ivalue > b.value.ivalue; 828 ret = num_get_ivalue (a) > num_get_ivalue (b);
799 else 829 else
800 ret = num_rvalue (a) > num_rvalue (b); 830 ret = num_get_rvalue (a) > num_get_rvalue (b);
801 831
802 return ret; 832 return ret;
803} 833}
804 834
805static int 835static int
813{ 843{
814 int ret; 844 int ret;
815 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); 845 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
816 846
817 if (is_fixnum) 847 if (is_fixnum)
818 ret = a.value.ivalue < b.value.ivalue; 848 ret = num_get_ivalue (a) < num_get_ivalue (b);
819 else 849 else
820 ret = num_rvalue (a) < num_rvalue (b); 850 ret = num_get_rvalue (a) < num_get_rvalue (b);
821 851
822 return ret; 852 return ret;
823} 853}
824 854
825static int 855static int
854#endif 884#endif
855 885
856static int 886static int
857is_zero_rvalue (RVALUE x) 887is_zero_rvalue (RVALUE x)
858{ 888{
859#if USE_FLOAT 889#if USE_REAL
860 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ 890 return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */
861#else 891#else
862 return x == 0; 892 return x == 0;
863#endif 893#endif
864}
865
866static long
867binary_decode (const char *s)
868{
869 long x = 0;
870
871 while (*s != 0 && (*s == '1' || *s == '0'))
872 {
873 x <<= 1;
874 x += *s - '0';
875 s++;
876 }
877
878 return x;
879} 894}
880 895
881/* allocate new cell segment */ 896/* allocate new cell segment */
882static int 897static int
883alloc_cellseg (SCHEME_P_ int n) 898alloc_cellseg (SCHEME_P_ int n)
927 SCHEME_V->fcells += segsize; 942 SCHEME_V->fcells += segsize;
928 last = newp + segsize - 1; 943 last = newp + segsize - 1;
929 944
930 for (p = newp; p <= last; p++) 945 for (p = newp; p <= last; p++)
931 { 946 {
932 set_typeflag (p, 0); 947 set_typeflag (p, T_FREE);
948 set_car (p, NIL);
933 set_cdr (p, p + 1); 949 set_cdr (p, p + 1);
934 set_car (p, NIL);
935 } 950 }
936 951
937 /* insert new cells in address order on free list */ 952 /* insert new cells in address order on free list */
938 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) 953 if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell)
939 { 954 {
953 } 968 }
954 969
955 return n; 970 return n;
956} 971}
957 972
958/* get new cell. parameter a, b is marked by gc. */ 973/* get new cell. parameter a, b is marked by gc. */
959static INLINE pointer 974static INLINE pointer
960get_cell_x (SCHEME_P_ pointer a, pointer b) 975get_cell_x (SCHEME_P_ pointer a, pointer b)
961{ 976{
962 if (SCHEME_V->free_cell == NIL) 977 if (ecb_expect_false (SCHEME_V->free_cell == NIL))
963 { 978 {
964 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 979 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
965 return S_SINK; 980 return S_SINK;
966 981
967 if (SCHEME_V->free_cell == NIL) 982 if (SCHEME_V->free_cell == NIL)
991 --SCHEME_V->fcells; 1006 --SCHEME_V->fcells;
992 return x; 1007 return x;
993 } 1008 }
994} 1009}
995 1010
996/* make sure that there is a given number of cells free */
997static pointer
998reserve_cells (SCHEME_P_ int n)
999{
1000 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1001 return NIL;
1002
1003 /* Are there enough cells available? */
1004 if (SCHEME_V->fcells < n)
1005 {
1006 /* If not, try gc'ing some */
1007 gc (SCHEME_A_ NIL, NIL);
1008
1009 if (SCHEME_V->fcells < n)
1010 {
1011 /* If there still aren't, try getting more heap */
1012 if (!alloc_cellseg (SCHEME_A_ 1) && USE_ERROR_CHECKING)
1013 {
1014 SCHEME_V->no_memory = 1;
1015 return NIL;
1016 }
1017 }
1018
1019 if (SCHEME_V->fcells < n && USE_ERROR_CHECKING)
1020 {
1021 /* If all fail, report failure */
1022 SCHEME_V->no_memory = 1;
1023 return NIL;
1024 }
1025 }
1026
1027 return S_T;
1028}
1029
1030static pointer
1031get_consecutive_cells (SCHEME_P_ int n)
1032{
1033 pointer x;
1034
1035 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
1036 return S_SINK;
1037
1038 /* Are there any cells available? */
1039 x = find_consecutive_cells (SCHEME_A_ n);
1040
1041 if (x != NIL)
1042 return x;
1043
1044 /* If not, try gc'ing some */
1045 gc (SCHEME_A_ NIL, NIL);
1046
1047 for (;;)
1048 {
1049 x = find_consecutive_cells (SCHEME_A_ n);
1050
1051 if (x != NIL)
1052 return x;
1053
1054 /* If there still aren't, try getting more heap */
1055 if (!alloc_cellseg (SCHEME_A_ 1))
1056 {
1057#if USE_ERROR_CHECKING
1058 SCHEME_V->no_memory = 1;
1059 return S_SINK;
1060#endif
1061 }
1062 }
1063}
1064
1065static int
1066count_consecutive_cells (pointer x, int needed)
1067{
1068 int n = 1;
1069
1070 while (cdr (x) == x + 1)
1071 {
1072 x = cdr (x);
1073 n++;
1074
1075 if (n > needed)
1076 return n;
1077 }
1078
1079 return n;
1080}
1081
1082static pointer
1083find_consecutive_cells (SCHEME_P_ int n)
1084{
1085 pointer *pp;
1086 int cnt;
1087
1088 pp = &SCHEME_V->free_cell;
1089
1090 while (*pp != NIL)
1091 {
1092 cnt = count_consecutive_cells (*pp, n);
1093
1094 if (cnt >= n)
1095 {
1096 pointer x = *pp;
1097
1098 *pp = cdr (*pp + n - 1);
1099 SCHEME_V->fcells -= n;
1100 return x;
1101 }
1102
1103 pp = &cdr (*pp + cnt - 1);
1104 }
1105
1106 return NIL;
1107}
1108
1109/* To retain recent allocs before interpreter knows about them - 1011/* To retain recent allocs before interpreter knows about them -
1110 Tehom */ 1012 Tehom */
1111 1013
1112static void 1014static void
1113push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 1015push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
1114{ 1016{
1115 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 1017 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1116 1018
1117 set_typeflag (holder, T_PAIR | T_IMMUTABLE); 1019 set_typeflag (holder, T_PAIR);
1020 setimmutable (holder);
1118 set_car (holder, recent); 1021 set_car (holder, recent);
1119 set_cdr (holder, car (S_SINK)); 1022 set_cdr (holder, car (S_SINK));
1120 set_car (S_SINK, holder); 1023 set_car (S_SINK, holder);
1121} 1024}
1122 1025
1135 1038
1136 return cell; 1039 return cell;
1137} 1040}
1138 1041
1139static pointer 1042static pointer
1140get_vector_object (SCHEME_P_ int len, pointer init) 1043get_vector_object (SCHEME_P_ uint32_t len, pointer init)
1141{ 1044{
1142 pointer cells = get_consecutive_cells (SCHEME_A_ len / 2 + len % 2 + 1); 1045 pointer v = get_cell_x (SCHEME_A_ 0, 0);
1046 pointer *e = malloc (len * sizeof (pointer));
1143 1047
1144#if USE_ERROR_CHECKING 1048 if (!e && USE_ERROR_CHECKING)
1145 if (SCHEME_V->no_memory)
1146 return S_SINK; 1049 return S_SINK;
1147#endif
1148 1050
1149 /* Record it as a vector so that gc understands it. */ 1051 /* Record it as a vector so that gc understands it. */
1150 set_typeflag (cells, T_VECTOR | T_ATOM); 1052 set_typeflag (v, T_VECTOR | T_ATOM);
1151 ivalue_unchecked (cells) = len; 1053
1152 set_num_integer (cells); 1054 v->object.vector.vvalue = e;
1055 v->object.vector.length = len;
1153 fill_vector (cells, init); 1056 fill_vector (v, init);
1154 push_recent_alloc (SCHEME_A_ cells, NIL); 1057 push_recent_alloc (SCHEME_A_ v, NIL);
1155 1058
1156 return cells; 1059 return v;
1157} 1060}
1158 1061
1159static INLINE void 1062static INLINE void
1160ok_to_freely_gc (SCHEME_P) 1063ok_to_freely_gc (SCHEME_P)
1161{ 1064{
1164 1067
1165#if defined TSGRIND 1068#if defined TSGRIND
1166static void 1069static void
1167check_cell_alloced (pointer p, int expect_alloced) 1070check_cell_alloced (pointer p, int expect_alloced)
1168{ 1071{
1169 /* Can't use putstr(SCHEME_A_ str) because callers have no access to 1072 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */
1170 sc. */
1171 if (typeflag (p) & !expect_alloced) 1073 if (typeflag (p) & !expect_alloced)
1172 xwrstr ("Cell is already allocated!\n"); 1074 xwrstr ("Cell is already allocated!\n");
1173 1075
1174 if (!(typeflag (p)) & expect_alloced) 1076 if (!(typeflag (p)) & expect_alloced)
1175 xwrstr ("Cell is not allocated!\n"); 1077 xwrstr ("Cell is not allocated!\n");
1198 if (immutable) 1100 if (immutable)
1199 setimmutable (x); 1101 setimmutable (x);
1200 1102
1201 set_car (x, a); 1103 set_car (x, a);
1202 set_cdr (x, b); 1104 set_cdr (x, b);
1105
1203 return x; 1106 return x;
1204} 1107}
1205 1108
1206/* ========== oblist implementation ========== */ 1109/* ========== oblist implementation ========== */
1207 1110
1217 1120
1218/* returns the new symbol */ 1121/* returns the new symbol */
1219static pointer 1122static pointer
1220oblist_add_by_name (SCHEME_P_ const char *name) 1123oblist_add_by_name (SCHEME_P_ const char *name)
1221{ 1124{
1222 pointer x;
1223 int location; 1125 int location;
1224 1126
1225 x = immutable_cons (mk_string (SCHEME_A_ name), NIL); 1127 pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL);
1226 set_typeflag (x, T_SYMBOL); 1128 set_typeflag (x, T_SYMBOL);
1227 setimmutable (car (x)); 1129 setimmutable (car (x));
1228 1130
1229 location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); 1131 location = hash_fn (name, veclength (SCHEME_V->oblist));
1230 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); 1132 set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location)));
1231 return x; 1133 return x;
1232} 1134}
1233 1135
1234static INLINE pointer 1136static INLINE pointer
1236{ 1138{
1237 int location; 1139 int location;
1238 pointer x; 1140 pointer x;
1239 char *s; 1141 char *s;
1240 1142
1241 location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); 1143 location = hash_fn (name, veclength (SCHEME_V->oblist));
1242 1144
1243 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) 1145 for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x))
1244 { 1146 {
1245 s = symname (car (x)); 1147 s = symname (car (x));
1246 1148
1257{ 1159{
1258 int i; 1160 int i;
1259 pointer x; 1161 pointer x;
1260 pointer ob_list = NIL; 1162 pointer ob_list = NIL;
1261 1163
1262 for (i = 0; i < ivalue_unchecked (SCHEME_V->oblist); i++) 1164 for (i = 0; i < veclength (SCHEME_V->oblist); i++)
1263 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) 1165 for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x))
1264 ob_list = cons (x, ob_list); 1166 ob_list = cons (x, ob_list);
1265 1167
1266 return ob_list; 1168 return ob_list;
1267} 1169}
1341mk_character (SCHEME_P_ int c) 1243mk_character (SCHEME_P_ int c)
1342{ 1244{
1343 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1245 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1344 1246
1345 set_typeflag (x, (T_CHARACTER | T_ATOM)); 1247 set_typeflag (x, (T_CHARACTER | T_ATOM));
1346 ivalue_unchecked (x) = c; 1248 ivalue_unchecked (x) = c & 0xff;
1347 set_num_integer (x); 1249 set_num_integer (x);
1348 return x; 1250 return x;
1349} 1251}
1350 1252
1351/* get number atom (integer) */ 1253/* get number atom (integer) */
1370 set_num_real (x); 1272 set_num_real (x);
1371 return x; 1273 return x;
1372} 1274}
1373 1275
1374static pointer 1276static pointer
1375mk_number (SCHEME_P_ num n) 1277mk_number (SCHEME_P_ const num n)
1376{ 1278{
1377 if (num_is_fixnum (n)) 1279 if (num_is_fixnum (n))
1378 {
1379 return mk_integer (SCHEME_A_ num_ivalue (n)); 1280 return mk_integer (SCHEME_A_ num_get_ivalue (n));
1380 }
1381 else 1281 else
1382 {
1383 return mk_real (SCHEME_A_ num_rvalue (n)); 1282 return mk_real (SCHEME_A_ num_get_rvalue (n));
1384 }
1385} 1283}
1386 1284
1387/* allocate name to string area */ 1285/* allocate name to string area */
1388static char * 1286static char *
1389store_string (SCHEME_P_ int len_str, const char *str, char fill) 1287store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill)
1390{ 1288{
1391 char *q;
1392
1393 q = (char *)malloc (len_str + 1); 1289 char *q = malloc (len_str + 1);
1394 1290
1395 if (q == 0) 1291 if (q == 0 && USE_ERROR_CHECKING)
1396 { 1292 {
1397#if USE_ERROR_CHECKING
1398 SCHEME_V->no_memory = 1; 1293 SCHEME_V->no_memory = 1;
1399 return SCHEME_V->strbuff; 1294 return SCHEME_V->strbuff;
1400#endif
1401 } 1295 }
1402 1296
1403 if (str) 1297 if (str)
1404 { 1298 {
1405 int l = strlen (str); 1299 int l = strlen (str);
1406 1300
1407 if (l > len_str) 1301 if (l > len_str)
1408 l = len_str; 1302 l = len_str;
1409 1303
1410 memcpy (q, str, l + 1); 1304 memcpy (q, str, l);
1411 q[l + 1] = 0; 1305 q[l] = 0;
1412 } 1306 }
1413 else 1307 else
1414 { 1308 {
1415 memset (q, fill, len_str); 1309 memset (q, fill, len_str);
1416 q[len_str] = 0; 1310 q[len_str] = 0;
1417 } 1311 }
1418 1312
1419 return q; 1313 return q;
1420} 1314}
1421 1315
1422/* get new string */
1423INTERFACE pointer 1316INTERFACE pointer
1424mk_string (SCHEME_P_ const char *str) 1317mk_empty_string (SCHEME_P_ uint32_t len, char fill)
1425{ 1318{
1426 return mk_counted_string (SCHEME_A_ str, strlen (str)); 1319 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1320
1321 set_typeflag (x, T_STRING | T_ATOM);
1322 strvalue (x) = store_string (SCHEME_A_ len, 0, fill);
1323 strlength (x) = len;
1324 return x;
1427} 1325}
1428 1326
1429INTERFACE pointer 1327INTERFACE pointer
1430mk_counted_string (SCHEME_P_ const char *str, int len) 1328mk_counted_string (SCHEME_P_ const char *str, uint32_t len)
1431{ 1329{
1432 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1330 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1433 1331
1434 set_typeflag (x, T_STRING | T_ATOM); 1332 set_typeflag (x, T_STRING | T_ATOM);
1435 strvalue (x) = store_string (SCHEME_A_ len, str, 0); 1333 strvalue (x) = store_string (SCHEME_A_ len, str, 0);
1436 strlength (x) = len; 1334 strlength (x) = len;
1437 return x; 1335 return x;
1438} 1336}
1439 1337
1440INTERFACE pointer 1338INTERFACE pointer
1441mk_empty_string (SCHEME_P_ int len, char fill) 1339mk_string (SCHEME_P_ const char *str)
1442{ 1340{
1443 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1341 return mk_counted_string (SCHEME_A_ str, strlen (str));
1444
1445 set_typeflag (x, T_STRING | T_ATOM);
1446 strvalue (x) = store_string (SCHEME_A_ len, 0, fill);
1447 strlength (x) = len;
1448 return x;
1449} 1342}
1450 1343
1451INTERFACE pointer 1344INTERFACE pointer
1452mk_vector (SCHEME_P_ int len) 1345mk_vector (SCHEME_P_ uint32_t len)
1453{ 1346{
1454 return get_vector_object (SCHEME_A_ len, NIL); 1347 return get_vector_object (SCHEME_A_ len, NIL);
1455} 1348}
1456 1349
1457INTERFACE void 1350INTERFACE void
1458fill_vector (pointer vec, pointer obj) 1351fill_vector (pointer vec, pointer obj)
1459{ 1352{
1460 int i; 1353 int i;
1461 int num = ivalue (vec) / 2 + ivalue (vec) % 2;
1462 1354
1463 for (i = 0; i < num; i++) 1355 for (i = 0; i < vec->object.vector.length; i++)
1464 { 1356 vecvalue (vec)[i] = obj;
1465 set_typeflag (vec + 1 + i, T_PAIR);
1466 setimmutable (vec + 1 + i);
1467 set_car (vec + 1 + i, obj);
1468 set_cdr (vec + 1 + i, obj);
1469 }
1470} 1357}
1471 1358
1472INTERFACE pointer 1359INTERFACE pointer
1473vector_elem (pointer vec, int ielem) 1360vector_elem (pointer vec, uint32_t ielem)
1474{ 1361{
1475 int n = ielem / 2; 1362 return vecvalue(vec)[ielem];
1476
1477 if (ielem % 2 == 0)
1478 return car (vec + 1 + n);
1479 else
1480 return cdr (vec + 1 + n);
1481} 1363}
1482 1364
1483INTERFACE void 1365INTERFACE void
1484set_vector_elem (pointer vec, int ielem, pointer a) 1366set_vector_elem (pointer vec, uint32_t ielem, pointer a)
1485{ 1367{
1486 int n = ielem / 2; 1368 vecvalue(vec)[ielem] = a;
1487
1488 if (ielem % 2 == 0)
1489 set_car (vec + 1 + n, a);
1490 else
1491 set_cdr (vec + 1 + n, a);
1492} 1369}
1493 1370
1494/* get new symbol */ 1371/* get new symbol */
1495INTERFACE pointer 1372INTERFACE pointer
1496mk_symbol (SCHEME_P_ const char *name) 1373mk_symbol (SCHEME_P_ const char *name)
1497{ 1374{
1498 pointer x;
1499
1500 /* first check oblist */ 1375 /* first check oblist */
1501 x = oblist_find_by_name (SCHEME_A_ name); 1376 pointer x = oblist_find_by_name (SCHEME_A_ name);
1502 1377
1503 if (x != NIL) 1378 if (x == NIL)
1504 return x;
1505 else
1506 {
1507 x = oblist_add_by_name (SCHEME_A_ name); 1379 x = oblist_add_by_name (SCHEME_A_ name);
1380
1508 return x; 1381 return x;
1509 }
1510} 1382}
1511 1383
1512INTERFACE pointer 1384INTERFACE pointer
1513gensym (SCHEME_P) 1385gensym (SCHEME_P)
1514{ 1386{
1515 pointer x; 1387 pointer x;
1516 char name[40];
1517 1388
1518 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) 1389 for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++)
1519 { 1390 {
1520 strcpy (name, "gensym-"); 1391 char name[40] = "gensym-";
1521 xnum (name + 7, SCHEME_V->gensym_cnt); 1392 xnum (name + 7, SCHEME_V->gensym_cnt);
1522 1393
1523 /* first check oblist */ 1394 /* first check oblist */
1524 x = oblist_find_by_name (SCHEME_A_ name); 1395 x = oblist_find_by_name (SCHEME_A_ name);
1525 1396
1526 if (x != NIL) 1397 if (x == NIL)
1527 continue;
1528 else
1529 { 1398 {
1530 x = oblist_add_by_name (SCHEME_A_ name); 1399 x = oblist_add_by_name (SCHEME_A_ name);
1531 return x; 1400 return x;
1532 } 1401 }
1533 } 1402 }
1542 char c, *p; 1411 char c, *p;
1543 int has_dec_point = 0; 1412 int has_dec_point = 0;
1544 int has_fp_exp = 0; 1413 int has_fp_exp = 0;
1545 1414
1546#if USE_COLON_HOOK 1415#if USE_COLON_HOOK
1547
1548 if ((p = strstr (q, "::")) != 0) 1416 if ((p = strstr (q, "::")) != 0)
1549 { 1417 {
1550 *p = 0; 1418 *p = 0;
1551 return cons (SCHEME_V->COLON_HOOK, 1419 return cons (SCHEME_V->COLON_HOOK,
1552 cons (cons (SCHEME_V->QUOTE, 1420 cons (cons (SCHEME_V->QUOTE,
1553 cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL))); 1421 cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL)));
1554 } 1422 }
1555
1556#endif 1423#endif
1557 1424
1558 p = q; 1425 p = q;
1559 c = *p++; 1426 c = *p++;
1560 1427
1567 has_dec_point = 1; 1434 has_dec_point = 1;
1568 c = *p++; 1435 c = *p++;
1569 } 1436 }
1570 1437
1571 if (!isdigit (c)) 1438 if (!isdigit (c))
1572 {
1573 return mk_symbol (SCHEME_A_ strlwr (q)); 1439 return mk_symbol (SCHEME_A_ strlwr (q));
1574 }
1575 } 1440 }
1576 else if (c == '.') 1441 else if (c == '.')
1577 { 1442 {
1578 has_dec_point = 1; 1443 has_dec_point = 1;
1579 c = *p++; 1444 c = *p++;
1580 1445
1581 if (!isdigit (c)) 1446 if (!isdigit (c))
1582 {
1583 return mk_symbol (SCHEME_A_ strlwr (q)); 1447 return mk_symbol (SCHEME_A_ strlwr (q));
1584 }
1585 } 1448 }
1586 else if (!isdigit (c)) 1449 else if (!isdigit (c))
1587 {
1588 return mk_symbol (SCHEME_A_ strlwr (q)); 1450 return mk_symbol (SCHEME_A_ strlwr (q));
1589 }
1590 1451
1591 for (; (c = *p) != 0; ++p) 1452 for (; (c = *p) != 0; ++p)
1592 { 1453 {
1593 if (!isdigit (c)) 1454 if (!isdigit (c))
1594 { 1455 {
1607 has_dec_point = 1; /* decimal point illegal 1468 has_dec_point = 1; /* decimal point illegal
1608 from now on */ 1469 from now on */
1609 p++; 1470 p++;
1610 1471
1611 if ((*p == '-') || (*p == '+') || isdigit (*p)) 1472 if ((*p == '-') || (*p == '+') || isdigit (*p))
1612 {
1613 continue; 1473 continue;
1614 }
1615 } 1474 }
1616 } 1475 }
1617 1476
1618 return mk_symbol (SCHEME_A_ strlwr (q)); 1477 return mk_symbol (SCHEME_A_ strlwr (q));
1619 } 1478 }
1620 } 1479 }
1621 1480
1622#if USE_FLOAT 1481#if USE_REAL
1623 if (has_dec_point) 1482 if (has_dec_point)
1624 return mk_real (SCHEME_A_ atof (q)); 1483 return mk_real (SCHEME_A_ atof (q));
1625#endif 1484#endif
1626 1485
1627 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1486 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1629 1488
1630/* make constant */ 1489/* make constant */
1631static pointer 1490static pointer
1632mk_sharp_const (SCHEME_P_ char *name) 1491mk_sharp_const (SCHEME_P_ char *name)
1633{ 1492{
1634 long x;
1635 char tmp[STRBUFFSIZE];
1636
1637 if (!strcmp (name, "t")) 1493 if (!strcmp (name, "t"))
1638 return S_T; 1494 return S_T;
1639 else if (!strcmp (name, "f")) 1495 else if (!strcmp (name, "f"))
1640 return S_F; 1496 return S_F;
1641 else if (*name == 'o') /* #o (octal) */
1642 {
1643 x = strtol (name + 1, 0, 8);
1644 return mk_integer (SCHEME_A_ x);
1645 }
1646 else if (*name == 'd') /* #d (decimal) */
1647 {
1648 x = strtol (name + 1, 0, 10);
1649 return mk_integer (SCHEME_A_ x);
1650 }
1651 else if (*name == 'x') /* #x (hex) */
1652 {
1653 x = strtol (name + 1, 0, 16);
1654 return mk_integer (SCHEME_A_ x);
1655 }
1656 else if (*name == 'b') /* #b (binary) */
1657 {
1658 x = binary_decode (name + 1);
1659 return mk_integer (SCHEME_A_ x);
1660 }
1661 else if (*name == '\\') /* #\w (character) */ 1497 else if (*name == '\\') /* #\w (character) */
1662 { 1498 {
1663 int c = 0; 1499 int c;
1664 1500
1665 if (stricmp (name + 1, "space") == 0) 1501 if (stricmp (name + 1, "space") == 0)
1666 c = ' '; 1502 c = ' ';
1667 else if (stricmp (name + 1, "newline") == 0) 1503 else if (stricmp (name + 1, "newline") == 0)
1668 c = '\n'; 1504 c = '\n';
1670 c = '\r'; 1506 c = '\r';
1671 else if (stricmp (name + 1, "tab") == 0) 1507 else if (stricmp (name + 1, "tab") == 0)
1672 c = '\t'; 1508 c = '\t';
1673 else if (name[1] == 'x' && name[2] != 0) 1509 else if (name[1] == 'x' && name[2] != 0)
1674 { 1510 {
1675 int c1 = strtol (name + 2, 0, 16); 1511 long c1 = strtol (name + 2, 0, 16);
1676 1512
1677 if (c1 <= UCHAR_MAX) 1513 if (0 <= c1 && c1 <= UCHAR_MAX)
1678 c = c1; 1514 c = c1;
1679 else 1515 else
1680 return NIL; 1516 return NIL;
1681 1517 }
1682#if USE_ASCII_NAMES 1518#if USE_ASCII_NAMES
1683 }
1684 else if (is_ascii_name (name + 1, &c)) 1519 else if (is_ascii_name (name + 1, &c))
1685 {
1686 /* nothing */ 1520 /* nothing */;
1687#endif 1521#endif
1688 }
1689 else if (name[2] == 0) 1522 else if (name[2] == 0)
1690 c = name[1]; 1523 c = name[1];
1691 else 1524 else
1692 return NIL; 1525 return NIL;
1693 1526
1694 return mk_character (SCHEME_A_ c); 1527 return mk_character (SCHEME_A_ c);
1695 } 1528 }
1696 else 1529 else
1530 {
1531 /* identify base by string index */
1532 const char baseidx[17] = "ffbf" "ffff" "ofdf" "ffff" "x";
1533 char *base = strchr (baseidx, *name);
1534
1535 if (base)
1536 return mk_integer (SCHEME_A_ strtol (name + 1, 0, base - baseidx));
1537
1697 return NIL; 1538 return NIL;
1539 }
1698} 1540}
1699 1541
1700/* ========== garbage collector ========== */ 1542/* ========== garbage collector ========== */
1701 1543
1702/*-- 1544/*--
1703 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, 1545 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1,
1704 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1546 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1705 * for marking. 1547 * for marking.
1548 *
1549 * The exception is vectors - vectors are currently marked recursively,
1550 * which is inherited form tinyscheme and could be fixed by having another
1551 * word of context in the vector
1706 */ 1552 */
1707static void 1553static void
1708mark (pointer a) 1554mark (pointer a)
1709{ 1555{
1710 pointer t, q, p; 1556 pointer t, q, p;
1711 1557
1712 t = (pointer) 0; 1558 t = 0;
1713 p = a; 1559 p = a;
1714E2: 1560E2:
1715 setmark (p); 1561 setmark (p);
1716 printf ("mark %p %x\n", p, p->flag);//D
1717 1562
1718 if (is_vector (p)) 1563 if (ecb_expect_false (is_vector (p)))
1719 { 1564 {
1720 int i; 1565 int i;
1721 int num = ivalue_unchecked (p) / 2 + ivalue_unchecked (p) % 2;
1722 1566
1723 for (i = 0; i < num; i++) 1567 for (i = 0; i < p->object.vector.length; i++)
1724 { 1568 mark (vecvalue (p)[i]);
1725 /* Vector cells will be treated like ordinary cells */
1726 mark (p + 1 + i);
1727 }
1728 } 1569 }
1729 1570
1730 if (is_atom (p)) 1571 if (is_atom (p))
1731 goto E6; 1572 goto E6;
1732 1573
1742 goto E2; 1583 goto E2;
1743 } 1584 }
1744 1585
1745E5: 1586E5:
1746 q = cdr (p); /* down cdr */ 1587 q = cdr (p); /* down cdr */
1747 printf ("mark+ %p\n", q, q->flag);//D
1748 1588
1749 if (q && !is_mark (q)) 1589 if (q && !is_mark (q))
1750 { 1590 {
1751 set_cdr (p, t); 1591 set_cdr (p, t);
1752 t = p; 1592 t = p;
1753 p = q; 1593 p = q;
1754 goto E2; 1594 goto E2;
1755 } 1595 }
1756 1596
1757E6: /* up. Undo the link switching from steps E4 and E5. */ 1597E6: /* up. Undo the link switching from steps E4 and E5. */
1758
1759 if (!t) 1598 if (!t)
1760 return; 1599 return;
1761 1600
1762 q = t; 1601 q = t;
1763 1602
1831 if (is_mark (p)) 1670 if (is_mark (p))
1832 clrmark (p); 1671 clrmark (p);
1833 else 1672 else
1834 { 1673 {
1835 /* reclaim cell */ 1674 /* reclaim cell */
1836 if (typeflag (p) != 0) 1675 if (typeflag (p) != T_FREE)
1837 { 1676 {
1838 finalize_cell (SCHEME_A_ p); 1677 finalize_cell (SCHEME_A_ p);
1839 set_typeflag (p, 0); 1678 set_typeflag (p, T_FREE);
1840 set_car (p, NIL); 1679 set_car (p, NIL);
1841 } 1680 }
1842 1681
1843 ++SCHEME_V->fcells; 1682 ++SCHEME_V->fcells;
1844 set_cdr (p, SCHEME_V->free_cell); 1683 set_cdr (p, SCHEME_V->free_cell);
1852} 1691}
1853 1692
1854static void 1693static void
1855finalize_cell (SCHEME_P_ pointer a) 1694finalize_cell (SCHEME_P_ pointer a)
1856{ 1695{
1696 /* TODO, fast bitmap check? */
1857 if (is_string (a)) 1697 if (is_string (a))
1858 free (strvalue (a)); 1698 free (strvalue (a));
1699 else if (is_vector (a))
1700 free (vecvalue (a));
1859#if USE_PORTS 1701#if USE_PORTS
1860 else if (is_port (a)) 1702 else if (is_port (a))
1861 { 1703 {
1862 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) 1704 if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit)
1863 port_close (SCHEME_A_ a, port_input | port_output); 1705 port_close (SCHEME_A_ a, port_input | port_output);
2289 char *p = SCHEME_V->strbuff; 2131 char *p = SCHEME_V->strbuff;
2290 2132
2291 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2133 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2292 2134
2293 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') 2135 if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\')
2294 {
2295 *p = 0; 2136 *p = 0;
2296 }
2297 else 2137 else
2298 { 2138 {
2299 backchar (SCHEME_A_ p[-1]); 2139 backchar (SCHEME_A_ p[-1]);
2300 *--p = '\0'; 2140 *--p = '\0';
2301 } 2141 }
2316 for (;;) 2156 for (;;)
2317 { 2157 {
2318 c = inchar (SCHEME_A); 2158 c = inchar (SCHEME_A);
2319 2159
2320 if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1) 2160 if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1)
2321 {
2322 return S_F; 2161 return S_F;
2323 }
2324 2162
2325 switch (state) 2163 switch (state)
2326 { 2164 {
2327 case st_ok: 2165 case st_ok:
2328 switch (c) 2166 switch (c)
2396 c = toupper (c); 2234 c = toupper (c);
2397 2235
2398 if (c >= '0' && c <= 'F') 2236 if (c >= '0' && c <= 'F')
2399 { 2237 {
2400 if (c <= '9') 2238 if (c <= '9')
2401 {
2402 c1 = (c1 << 4) + c - '0'; 2239 c1 = (c1 << 4) + c - '0';
2403 }
2404 else 2240 else
2405 {
2406 c1 = (c1 << 4) + c - 'A' + 10; 2241 c1 = (c1 << 4) + c - 'A' + 10;
2407 }
2408 2242
2409 if (state == st_x1) 2243 if (state == st_x1)
2410 {
2411 state = st_x2; 2244 state = st_x2;
2412 }
2413 else 2245 else
2414 { 2246 {
2415 *p++ = c1; 2247 *p++ = c1;
2416 state = st_ok; 2248 state = st_ok;
2417 } 2249 }
2418 } 2250 }
2419 else 2251 else
2420 {
2421 return S_F; 2252 return S_F;
2422 }
2423 2253
2424 break; 2254 break;
2425 2255
2426 case st_oct1: 2256 case st_oct1:
2427 case st_oct2: 2257 case st_oct2:
2458is_one_of (char *s, int c) 2288is_one_of (char *s, int c)
2459{ 2289{
2460 if (c == EOF) 2290 if (c == EOF)
2461 return 1; 2291 return 1;
2462 2292
2463 while (*s) 2293 return !!strchr (s, c);
2464 if (*s++ == c)
2465 return 1;
2466
2467 return 0;
2468} 2294}
2469 2295
2470/* skip white characters */ 2296/* skip white characters */
2471static INLINE int 2297static INLINE int
2472skipspace (SCHEME_P) 2298skipspace (SCHEME_P)
2477 { 2303 {
2478 c = inchar (SCHEME_A); 2304 c = inchar (SCHEME_A);
2479#if SHOW_ERROR_LINE 2305#if SHOW_ERROR_LINE
2480 if (c == '\n') 2306 if (c == '\n')
2481 curr_line++; 2307 curr_line++;
2482
2483#endif 2308#endif
2484 } 2309 }
2485 while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); 2310 while (c == ' ' || c == '\n' || c == '\r' || c == '\t');
2486 2311
2487 /* record it */ 2312 /* record it */
2524 2349
2525 if (is_one_of (" \n\t", c)) 2350 if (is_one_of (" \n\t", c))
2526 return TOK_DOT; 2351 return TOK_DOT;
2527 else 2352 else
2528 { 2353 {
2354 //TODO: ungetc twice in a row is not supported in C
2529 backchar (SCHEME_A_ c); 2355 backchar (SCHEME_A_ c);
2530 backchar (SCHEME_A_ '.'); 2356 backchar (SCHEME_A_ '.');
2531 return TOK_ATOM; 2357 return TOK_ATOM;
2532 } 2358 }
2533 2359
2647 int d = *s / 16; 2473 int d = *s / 16;
2648 2474
2649 putcharacter (SCHEME_A_ 'x'); 2475 putcharacter (SCHEME_A_ 'x');
2650 2476
2651 if (d < 10) 2477 if (d < 10)
2652 {
2653 putcharacter (SCHEME_A_ d + '0'); 2478 putcharacter (SCHEME_A_ d + '0');
2654 }
2655 else 2479 else
2656 {
2657 putcharacter (SCHEME_A_ d - 10 + 'A'); 2480 putcharacter (SCHEME_A_ d - 10 + 'A');
2658 }
2659 2481
2660 d = *s % 16; 2482 d = *s % 16;
2661 2483
2662 if (d < 10) 2484 if (d < 10)
2663 {
2664 putcharacter (SCHEME_A_ d + '0'); 2485 putcharacter (SCHEME_A_ d + '0');
2665 }
2666 else 2486 else
2667 {
2668 putcharacter (SCHEME_A_ d - 10 + 'A'); 2487 putcharacter (SCHEME_A_ d - 10 + 'A');
2669 }
2670 } 2488 }
2671 } 2489 }
2672 } 2490 }
2673 else 2491 else
2674 {
2675 putcharacter (SCHEME_A_ * s); 2492 putcharacter (SCHEME_A_ * s);
2676 }
2677 2493
2678 s++; 2494 s++;
2679 } 2495 }
2680 2496
2681 putcharacter (SCHEME_A_ '"'); 2497 putcharacter (SCHEME_A_ '"');
2716 2532
2717 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ 2533 if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */
2718 { 2534 {
2719 if (num_is_integer (l)) 2535 if (num_is_integer (l))
2720 xnum (p, ivalue_unchecked (l)); 2536 xnum (p, ivalue_unchecked (l));
2721#if USE_FLOAT 2537#if USE_REAL
2722 else 2538 else
2723 { 2539 {
2724 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); 2540 snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l));
2725 /* r5rs says there must be a '.' (unless 'e'?) */ 2541 /* r5rs says there must be a '.' (unless 'e'?) */
2726 f = strcspn (p, ".e"); 2542 f = strcspn (p, ".e");
2898list_star (SCHEME_P_ pointer d) 2714list_star (SCHEME_P_ pointer d)
2899{ 2715{
2900 pointer p, q; 2716 pointer p, q;
2901 2717
2902 if (cdr (d) == NIL) 2718 if (cdr (d) == NIL)
2903 {
2904 return car (d); 2719 return car (d);
2905 }
2906 2720
2907 p = cons (car (d), cdr (d)); 2721 p = cons (car (d), cdr (d));
2908 q = p; 2722 q = p;
2909 2723
2910 while (cdr (cdr (p)) != NIL) 2724 while (cdr (cdr (p)) != NIL)
2911 { 2725 {
2912 d = cons (car (p), cdr (p)); 2726 d = cons (car (p), cdr (p));
2913 2727
2914 if (cdr (cdr (p)) != NIL) 2728 if (cdr (cdr (p)) != NIL)
2915 {
2916 p = cdr (d); 2729 p = cdr (d);
2917 }
2918 } 2730 }
2919 2731
2920 set_cdr (p, car (cdr (p))); 2732 set_cdr (p, car (cdr (p)));
2921 return q; 2733 return q;
2922} 2734}
2936 2748
2937/* reverse list --- in-place */ 2749/* reverse list --- in-place */
2938static pointer 2750static pointer
2939reverse_in_place (SCHEME_P_ pointer term, pointer list) 2751reverse_in_place (SCHEME_P_ pointer term, pointer list)
2940{ 2752{
2941 pointer p = list, result = term, q; 2753 pointer result = term;
2754 pointer p = list;
2942 2755
2943 while (p != NIL) 2756 while (p != NIL)
2944 { 2757 {
2945 q = cdr (p); 2758 pointer q = cdr (p);
2946 set_cdr (p, result); 2759 set_cdr (p, result);
2947 result = p; 2760 result = p;
2948 p = q; 2761 p = q;
2949 } 2762 }
2950 2763
3025#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) 2838#if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST)
3026 2839
3027static int 2840static int
3028hash_fn (const char *key, int table_size) 2841hash_fn (const char *key, int table_size)
3029{ 2842{
3030 unsigned int hashed = 0; 2843 const unsigned char *p = key;
3031 const char *c; 2844 uint32_t hash = 2166136261;
3032 int bits_per_int = sizeof (unsigned int) * 8;
3033 2845
3034 for (c = key; *c; c++) 2846 while (*p)
3035 { 2847 hash = (hash ^ *p++) * 16777619;
3036 /* letters have about 5 bits in them */
3037 hashed = (hashed << 5) | (hashed >> (bits_per_int - 5));
3038 hashed ^= *c;
3039 }
3040 2848
3041 return hashed % table_size; 2849 return hash % table_size;
3042} 2850}
3043#endif 2851#endif
3044 2852
3045#ifndef USE_ALIST_ENV 2853#ifndef USE_ALIST_ENV
3046 2854
3072{ 2880{
3073 pointer slot = immutable_cons (variable, value); 2881 pointer slot = immutable_cons (variable, value);
3074 2882
3075 if (is_vector (car (env))) 2883 if (is_vector (car (env)))
3076 { 2884 {
3077 int location = hash_fn (symname (variable), ivalue_unchecked (car (env))); 2885 int location = hash_fn (symname (variable), veclength (car (env)));
3078 2886
3079 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); 2887 set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location)));
3080 } 2888 }
3081 else 2889 else
3082 set_car (env, immutable_cons (slot, car (env))); 2890 set_car (env, immutable_cons (slot, car (env)));
3090 2898
3091 for (x = env; x != NIL; x = cdr (x)) 2899 for (x = env; x != NIL; x = cdr (x))
3092 { 2900 {
3093 if (is_vector (car (x))) 2901 if (is_vector (car (x)))
3094 { 2902 {
3095 location = hash_fn (symname (hdl), ivalue_unchecked (car (x))); 2903 location = hash_fn (symname (hdl), veclength (car (x)));
3096 y = vector_elem (car (x), location); 2904 y = vector_elem (car (x), location);
3097 } 2905 }
3098 else 2906 else
3099 y = car (x); 2907 y = car (x);
3100 2908
3210#if USE_ERROR_HOOK 3018#if USE_ERROR_HOOK
3211 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); 3019 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1);
3212 3020
3213 if (x != NIL) 3021 if (x != NIL)
3214 { 3022 {
3215 if (a) 3023 pointer code = a
3216 SCHEME_V->code = cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL); 3024 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL)
3217 else 3025 : NIL;
3218 SCHEME_V->code = NIL;
3219 3026
3220 SCHEME_V->code = cons (mk_string (SCHEME_A_ s), SCHEME_V->code); 3027 code = cons (mk_string (SCHEME_A_ s), code);
3221 setimmutable (car (SCHEME_V->code)); 3028 setimmutable (car (code));
3222 SCHEME_V->code = cons (slot_value_in_env (x), SCHEME_V->code); 3029 SCHEME_V->code = cons (slot_value_in_env (x), code);
3223 SCHEME_V->op = OP_EVAL; 3030 SCHEME_V->op = OP_EVAL;
3224 3031
3225 return S_T; 3032 return S_T;
3226 } 3033 }
3227#endif 3034#endif
3231 else 3038 else
3232 SCHEME_V->args = NIL; 3039 SCHEME_V->args = NIL;
3233 3040
3234 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 3041 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
3235 setimmutable (car (SCHEME_V->args)); 3042 setimmutable (car (SCHEME_V->args));
3236 SCHEME_V->op = (int)OP_ERR0; 3043 SCHEME_V->op = OP_ERR0;
3237 return S_T; 3044 return S_T;
3238} 3045}
3239 3046
3240#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 3047#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
3241#define Error_0(s) Error_1 (s, 0) 3048#define Error_0(s) Error_1 (s, 0)
3242 3049
3243/* Too small to turn into function */ 3050/* Too small to turn into function */
3244#define BEGIN do { 3051#define BEGIN do {
3245#define END } while (0) 3052#define END } while (0)
3246#define s_goto(a) BEGIN \ 3053#define s_goto(a) BEGIN \
3247 SCHEME_V->op = (int)(a); \ 3054 SCHEME_V->op = a; \
3248 return S_T; END 3055 return S_T; END
3249 3056
3250#define s_return(a) return xs_return(SCHEME_A_ a) 3057#define s_return(a) return xs_return (SCHEME_A_ a)
3251 3058
3252#ifndef USE_SCHEME_STACK 3059#ifndef USE_SCHEME_STACK
3253 3060
3254/* this structure holds all the interpreter's registers */ 3061/* this structure holds all the interpreter's registers */
3255struct dump_stack_frame 3062struct dump_stack_frame
3274 SCHEME_V->dump_size += STACK_GROWTH; 3081 SCHEME_V->dump_size += STACK_GROWTH;
3275 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); 3082 SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size);
3276 } 3083 }
3277 3084
3278 next_frame = SCHEME_V->dump_base + nframes; 3085 next_frame = SCHEME_V->dump_base + nframes;
3086
3279 next_frame->op = op; 3087 next_frame->op = op;
3280 next_frame->args = args; 3088 next_frame->args = args;
3281 next_frame->envir = SCHEME_V->envir; 3089 next_frame->envir = SCHEME_V->envir;
3282 next_frame->code = code; 3090 next_frame->code = code;
3091
3283 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3092 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3284} 3093}
3285 3094
3286static pointer 3095static pointer
3287xs_return (SCHEME_P_ pointer a) 3096xs_return (SCHEME_P_ pointer a)
3292 SCHEME_V->value = a; 3101 SCHEME_V->value = a;
3293 3102
3294 if (nframes <= 0) 3103 if (nframes <= 0)
3295 return NIL; 3104 return NIL;
3296 3105
3297 nframes--;
3298 frame = SCHEME_V->dump_base + nframes; 3106 frame = &SCHEME_V->dump_base[--nframes];
3299 SCHEME_V->op = frame->op; 3107 SCHEME_V->op = frame->op;
3300 SCHEME_V->args = frame->args; 3108 SCHEME_V->args = frame->args;
3301 SCHEME_V->envir = frame->envir; 3109 SCHEME_V->envir = frame->envir;
3302 SCHEME_V->code = frame->code; 3110 SCHEME_V->code = frame->code;
3303 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3111 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3304 3112
3305 return S_T; 3113 return S_T;
3306} 3114}
3307 3115
3308static INLINE void 3116static INLINE void
3309dump_stack_reset (SCHEME_P) 3117dump_stack_reset (SCHEME_P)
3310{ 3118{
3311 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3119 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3312 SCHEME_V->dump = (pointer)0; 3120 SCHEME_V->dump = (pointer)+0;
3313} 3121}
3314 3122
3315static INLINE void 3123static INLINE void
3316dump_stack_initialize (SCHEME_P) 3124dump_stack_initialize (SCHEME_P)
3317{ 3125{
3318 SCHEME_V->dump_size = 0; 3126 SCHEME_V->dump_size = 0;
3319 SCHEME_V->dump_base = NULL; 3127 SCHEME_V->dump_base = 0;
3320 dump_stack_reset (SCHEME_A); 3128 dump_stack_reset (SCHEME_A);
3321} 3129}
3322 3130
3323static void 3131static void
3324dump_stack_free (SCHEME_P) 3132dump_stack_free (SCHEME_P)
3325{ 3133{
3326 free (SCHEME_V->dump_base); 3134 free (SCHEME_V->dump_base);
3327 SCHEME_V->dump_base = NULL; 3135 SCHEME_V->dump_base = 0;
3328 SCHEME_V->dump = (pointer)0; 3136 SCHEME_V->dump = (pointer)0;
3329 SCHEME_V->dump_size = 0; 3137 SCHEME_V->dump_size = 0;
3330} 3138}
3331 3139
3332static void 3140static void
3374 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3182 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3375 3183
3376 while (cont != NIL) 3184 while (cont != NIL)
3377 { 3185 {
3378 frame->op = ivalue (car (cont)); cont = cdr (cont); 3186 frame->op = ivalue (car (cont)); cont = cdr (cont);
3379 frame->args = car (cont) ; cont = cdr (cont); 3187 frame->args = car (cont) ; cont = cdr (cont);
3380 frame->envir = car (cont) ; cont = cdr (cont); 3188 frame->envir = car (cont) ; cont = cdr (cont);
3381 frame->code = car (cont) ; cont = cdr (cont); 3189 frame->code = car (cont) ; cont = cdr (cont);
3382 3190
3383 ++frame; 3191 ++frame;
3384 ++i; 3192 ++i;
3385 } 3193 }
3386 3194
3415 SCHEME_V->value = a; 3223 SCHEME_V->value = a;
3416 3224
3417 if (dump == NIL) 3225 if (dump == NIL)
3418 return NIL; 3226 return NIL;
3419 3227
3420 SCHEME_V->op = ivalue (car (dump)); 3228 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump);
3421 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3229 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3422 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3230 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3423 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3231 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3424 3232
3425 SCHEME_V->dump = dump; 3233 SCHEME_V->dump = dump;
3426 3234
3427 return S_T; 3235 return S_T;
3428} 3236}
3517 s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); 3325 s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL);
3518 s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); 3326 s_save (SCHEME_A_ OP_T1LVL, NIL, NIL);
3519 s_goto (OP_READ_INTERNAL); 3327 s_goto (OP_READ_INTERNAL);
3520 3328
3521 case OP_T1LVL: /* top level */ 3329 case OP_T1LVL: /* top level */
3522 SCHEME_V->code = SCHEME_V->value; 3330 SCHEME_V->code = SCHEME_V->value;
3523 SCHEME_V->inport = SCHEME_V->save_inport; 3331 SCHEME_V->inport = SCHEME_V->save_inport;
3524 s_goto (OP_EVAL); 3332 s_goto (OP_EVAL);
3525 3333
3526 case OP_READ_INTERNAL: /* internal read */ 3334 case OP_READ_INTERNAL: /* internal read */
3527 SCHEME_V->tok = token (SCHEME_A); 3335 SCHEME_V->tok = token (SCHEME_A);
3528 3336
3529 if (SCHEME_V->tok == TOK_EOF) 3337 if (SCHEME_V->tok == TOK_EOF)
3530 {
3531 s_return (S_EOF); 3338 s_return (S_EOF);
3532 }
3533 3339
3534 s_goto (OP_RDSEXPR); 3340 s_goto (OP_RDSEXPR);
3535 3341
3536 case OP_GENSYM: 3342 case OP_GENSYM:
3537 s_return (gensym (SCHEME_A)); 3343 s_return (gensym (SCHEME_A));
3541 /* OP_VALUEPRINT is always pushed, because when changing from 3347 /* OP_VALUEPRINT is always pushed, because when changing from
3542 non-interactive to interactive mode, it needs to be 3348 non-interactive to interactive mode, it needs to be
3543 already on the stack */ 3349 already on the stack */
3544#if USE_TRACING 3350#if USE_TRACING
3545 if (SCHEME_V->tracing) 3351 if (SCHEME_V->tracing)
3546 {
3547 putstr (SCHEME_A_ "\nGives: "); 3352 putstr (SCHEME_A_ "\nGives: ");
3548 }
3549#endif 3353#endif
3550 3354
3551 if (file_interactive (SCHEME_A)) 3355 if (file_interactive (SCHEME_A))
3552 { 3356 {
3553 SCHEME_V->print_flag = 1; 3357 SCHEME_V->print_flag = 1;
3554 SCHEME_V->args = SCHEME_V->value; 3358 SCHEME_V->args = SCHEME_V->value;
3555 s_goto (OP_P0LIST); 3359 s_goto (OP_P0LIST);
3556 } 3360 }
3557 else 3361 else
3558 {
3559 s_return (SCHEME_V->value); 3362 s_return (SCHEME_V->value);
3560 }
3561 3363
3562 case OP_EVAL: /* main part of evaluation */ 3364 case OP_EVAL: /* main part of evaluation */
3563#if USE_TRACING 3365#if USE_TRACING
3564 if (SCHEME_V->tracing) 3366 if (SCHEME_V->tracing)
3565 { 3367 {
3569 putstr (SCHEME_A_ "\nEval: "); 3371 putstr (SCHEME_A_ "\nEval: ");
3570 s_goto (OP_P0LIST); 3372 s_goto (OP_P0LIST);
3571 } 3373 }
3572 3374
3573 /* fall through */ 3375 /* fall through */
3376
3574 case OP_REAL_EVAL: 3377 case OP_REAL_EVAL:
3575#endif 3378#endif
3576 if (is_symbol (SCHEME_V->code)) /* symbol */ 3379 if (is_symbol (SCHEME_V->code)) /* symbol */
3577 { 3380 {
3578 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); 3381 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1);
3582 else 3385 else
3583 Error_1 ("eval: unbound variable:", SCHEME_V->code); 3386 Error_1 ("eval: unbound variable:", SCHEME_V->code);
3584 } 3387 }
3585 else if (is_pair (SCHEME_V->code)) 3388 else if (is_pair (SCHEME_V->code))
3586 { 3389 {
3390 x = car (SCHEME_V->code);
3391
3587 if (is_syntax (x = car (SCHEME_V->code))) /* SYNTAX */ 3392 if (is_syntax (x)) /* SYNTAX */
3588 { 3393 {
3589 SCHEME_V->code = cdr (SCHEME_V->code); 3394 SCHEME_V->code = cdr (SCHEME_V->code);
3590 s_goto (syntaxnum (x)); 3395 s_goto (syntaxnum (x));
3591 } 3396 }
3592 else /* first, eval top element and eval arguments */ 3397 else /* first, eval top element and eval arguments */
3593 { 3398 {
3594 s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code); 3399 s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code);
3595 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ 3400 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */
3596 SCHEME_V->code = car (SCHEME_V->code); 3401 SCHEME_V->code = x;
3597 s_goto (OP_EVAL); 3402 s_goto (OP_EVAL);
3598 } 3403 }
3599 } 3404 }
3600 else 3405 else
3601 s_return (SCHEME_V->code); 3406 s_return (SCHEME_V->code);
3654 putstr (SCHEME_A_ "\nApply to: "); 3459 putstr (SCHEME_A_ "\nApply to: ");
3655 s_goto (OP_P0LIST); 3460 s_goto (OP_P0LIST);
3656 } 3461 }
3657 3462
3658 /* fall through */ 3463 /* fall through */
3464
3659 case OP_REAL_APPLY: 3465 case OP_REAL_APPLY:
3660#endif 3466#endif
3661 if (is_proc (SCHEME_V->code)) 3467 if (is_proc (SCHEME_V->code))
3662 { 3468 {
3663 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3469 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3677 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); 3483 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code));
3678 3484
3679 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) 3485 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y))
3680 { 3486 {
3681 if (y == NIL) 3487 if (y == NIL)
3682 {
3683 Error_0 ("not enough arguments"); 3488 Error_0 ("not enough arguments");
3684 }
3685 else 3489 else
3686 {
3687 new_slot_in_env (SCHEME_A_ car (x), car (y)); 3490 new_slot_in_env (SCHEME_A_ car (x), car (y));
3688 }
3689 } 3491 }
3690 3492
3691 if (x == NIL) 3493 if (x == NIL)
3692 { 3494 {
3693
3694 /*-- 3495 /*--
3695 * if (y != NIL) { 3496 * if (y != NIL) {
3696 * Error_0("too many arguments"); 3497 * Error_0("too many arguments");
3697 * } 3498 * }
3698 */ 3499 */
3699 } 3500 }
3700 else if (is_symbol (x)) 3501 else if (is_symbol (x))
3701 new_slot_in_env (SCHEME_A_ x, y); 3502 new_slot_in_env (SCHEME_A_ x, y);
3702 else 3503 else
3703 {
3704 Error_1 ("syntax error in closure: not a symbol:", x); 3504 Error_1 ("syntax error in closure: not a symbol:", x);
3705 }
3706 3505
3707 SCHEME_V->code = cdr (closure_code (SCHEME_V->code)); 3506 SCHEME_V->code = cdr (closure_code (SCHEME_V->code));
3708 SCHEME_V->args = NIL; 3507 SCHEME_V->args = NIL;
3709 s_goto (OP_BEGIN); 3508 s_goto (OP_BEGIN);
3710 } 3509 }
3780 x = car (SCHEME_V->code); 3579 x = car (SCHEME_V->code);
3781 SCHEME_V->code = cadr (SCHEME_V->code); 3580 SCHEME_V->code = cadr (SCHEME_V->code);
3782 } 3581 }
3783 3582
3784 if (!is_symbol (x)) 3583 if (!is_symbol (x))
3785 {
3786 Error_0 ("variable is not a symbol"); 3584 Error_0 ("variable is not a symbol");
3787 }
3788 3585
3789 s_save (SCHEME_A_ OP_DEF1, NIL, x); 3586 s_save (SCHEME_A_ OP_DEF1, NIL, x);
3790 s_goto (OP_EVAL); 3587 s_goto (OP_EVAL);
3791 3588
3792 case OP_DEF1: /* define */ 3589 case OP_DEF1: /* define */
3793 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); 3590 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0);
3794 3591
3795 if (x != NIL) 3592 if (x != NIL)
3796 {
3797 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); 3593 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value);
3798 }
3799 else 3594 else
3800 {
3801 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 3595 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
3802 }
3803 3596
3804 s_return (SCHEME_V->code); 3597 s_return (SCHEME_V->code);
3805 3598
3806 3599
3807 case OP_DEFP: /* defined? */ 3600 case OP_DEFP: /* defined? */
3808 x = SCHEME_V->envir; 3601 x = SCHEME_V->envir;
3809 3602
3810 if (cdr (SCHEME_V->args) != NIL) 3603 if (cdr (SCHEME_V->args) != NIL)
3811 {
3812 x = cadr (SCHEME_V->args); 3604 x = cadr (SCHEME_V->args);
3813 }
3814 3605
3815 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); 3606 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL);
3816 3607
3817 case OP_SET0: /* set! */ 3608 case OP_SET0: /* set! */
3818 if (is_immutable (car (SCHEME_V->code))) 3609 if (is_immutable (car (SCHEME_V->code)))
3829 { 3620 {
3830 set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); 3621 set_slot_in_env (SCHEME_A_ y, SCHEME_V->value);
3831 s_return (SCHEME_V->value); 3622 s_return (SCHEME_V->value);
3832 } 3623 }
3833 else 3624 else
3834 {
3835 Error_1 ("set!: unbound variable:", SCHEME_V->code); 3625 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3836 }
3837 3626
3838 3627
3839 case OP_BEGIN: /* begin */ 3628 case OP_BEGIN: /* begin */
3840 if (!is_pair (SCHEME_V->code)) 3629 if (!is_pair (SCHEME_V->code))
3841 {
3842 s_return (SCHEME_V->code); 3630 s_return (SCHEME_V->code);
3843 }
3844 3631
3845 if (cdr (SCHEME_V->code) != NIL) 3632 if (cdr (SCHEME_V->code) != NIL)
3846 {
3847 s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code)); 3633 s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code));
3848 }
3849 3634
3850 SCHEME_V->code = car (SCHEME_V->code); 3635 SCHEME_V->code = car (SCHEME_V->code);
3851 s_goto (OP_EVAL); 3636 s_goto (OP_EVAL);
3852 3637
3853 case OP_IF0: /* if */ 3638 case OP_IF0: /* if */
3874 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3659 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args);
3875 3660
3876 if (is_pair (SCHEME_V->code)) /* continue */ 3661 if (is_pair (SCHEME_V->code)) /* continue */
3877 { 3662 {
3878 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3663 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3879 {
3880 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); 3664 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code));
3881 }
3882 3665
3883 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); 3666 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code));
3884 SCHEME_V->code = cadar (SCHEME_V->code); 3667 SCHEME_V->code = cadar (SCHEME_V->code);
3885 SCHEME_V->args = NIL; 3668 SCHEME_V->args = NIL;
3886 s_goto (OP_EVAL); 3669 s_goto (OP_EVAL);
3896 case OP_LET2: /* let */ 3679 case OP_LET2: /* let */
3897 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 3680 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3898 3681
3899 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; 3682 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args;
3900 y != NIL; x = cdr (x), y = cdr (y)) 3683 y != NIL; x = cdr (x), y = cdr (y))
3901 {
3902 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3684 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3903 }
3904 3685
3905 if (is_symbol (car (SCHEME_V->code))) /* named let */ 3686 if (is_symbol (car (SCHEME_V->code))) /* named let */
3906 { 3687 {
3907 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) 3688 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x))
3908 { 3689 {
3978 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3759 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args);
3979 3760
3980 if (is_pair (SCHEME_V->code)) /* continue */ 3761 if (is_pair (SCHEME_V->code)) /* continue */
3981 { 3762 {
3982 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3763 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3983 {
3984 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); 3764 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3985 }
3986 3765
3987 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); 3766 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code));
3988 SCHEME_V->code = cadar (SCHEME_V->code); 3767 SCHEME_V->code = cadar (SCHEME_V->code);
3989 SCHEME_V->args = NIL; 3768 SCHEME_V->args = NIL;
3990 s_goto (OP_EVAL); 3769 s_goto (OP_EVAL);
3997 s_goto (OP_LET2REC); 3776 s_goto (OP_LET2REC);
3998 } 3777 }
3999 3778
4000 case OP_LET2REC: /* letrec */ 3779 case OP_LET2REC: /* letrec */
4001 for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) 3780 for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y))
4002 {
4003 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3781 new_slot_in_env (SCHEME_A_ caar (x), car (y));
4004 }
4005 3782
4006 SCHEME_V->code = cdr (SCHEME_V->code); 3783 SCHEME_V->code = cdr (SCHEME_V->code);
4007 SCHEME_V->args = NIL; 3784 SCHEME_V->args = NIL;
4008 s_goto (OP_BEGIN); 3785 s_goto (OP_BEGIN);
4009 3786
4010 case OP_COND0: /* cond */ 3787 case OP_COND0: /* cond */
4011 if (!is_pair (SCHEME_V->code)) 3788 if (!is_pair (SCHEME_V->code))
4012 {
4013 Error_0 ("syntax error in cond"); 3789 Error_0 ("syntax error in cond");
4014 }
4015 3790
4016 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 3791 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
4017 SCHEME_V->code = caar (SCHEME_V->code); 3792 SCHEME_V->code = caar (SCHEME_V->code);
4018 s_goto (OP_EVAL); 3793 s_goto (OP_EVAL);
4019 3794
4020 case OP_COND1: /* cond */ 3795 case OP_COND1: /* cond */
4021 if (is_true (SCHEME_V->value)) 3796 if (is_true (SCHEME_V->value))
4022 { 3797 {
4023 if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL) 3798 if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL)
4024 {
4025 s_return (SCHEME_V->value); 3799 s_return (SCHEME_V->value);
4026 }
4027 3800
4028 if (car (SCHEME_V->code) == SCHEME_V->FEED_TO) 3801 if (car (SCHEME_V->code) == SCHEME_V->FEED_TO)
4029 { 3802 {
4030 if (!is_pair (cdr (SCHEME_V->code))) 3803 if (!is_pair (cdr (SCHEME_V->code)))
4031 {
4032 Error_0 ("syntax error in cond"); 3804 Error_0 ("syntax error in cond");
4033 }
4034 3805
4035 x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)); 3806 x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL));
4036 SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL)); 3807 SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL));
4037 s_goto (OP_EVAL); 3808 s_goto (OP_EVAL);
4038 } 3809 }
4040 s_goto (OP_BEGIN); 3811 s_goto (OP_BEGIN);
4041 } 3812 }
4042 else 3813 else
4043 { 3814 {
4044 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 3815 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
4045 {
4046 s_return (NIL); 3816 s_return (NIL);
4047 }
4048 else 3817 else
4049 { 3818 {
4050 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 3819 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
4051 SCHEME_V->code = caar (SCHEME_V->code); 3820 SCHEME_V->code = caar (SCHEME_V->code);
4052 s_goto (OP_EVAL); 3821 s_goto (OP_EVAL);
4058 set_typeflag (x, T_PROMISE); 3827 set_typeflag (x, T_PROMISE);
4059 s_return (x); 3828 s_return (x);
4060 3829
4061 case OP_AND0: /* and */ 3830 case OP_AND0: /* and */
4062 if (SCHEME_V->code == NIL) 3831 if (SCHEME_V->code == NIL)
4063 {
4064 s_return (S_T); 3832 s_return (S_T);
4065 }
4066 3833
4067 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 3834 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
4068 SCHEME_V->code = car (SCHEME_V->code); 3835 SCHEME_V->code = car (SCHEME_V->code);
4069 s_goto (OP_EVAL); 3836 s_goto (OP_EVAL);
4070 3837
4071 case OP_AND1: /* and */ 3838 case OP_AND1: /* and */
4072 if (is_false (SCHEME_V->value)) 3839 if (is_false (SCHEME_V->value))
4073 {
4074 s_return (SCHEME_V->value); 3840 s_return (SCHEME_V->value);
4075 }
4076 else if (SCHEME_V->code == NIL) 3841 else if (SCHEME_V->code == NIL)
4077 {
4078 s_return (SCHEME_V->value); 3842 s_return (SCHEME_V->value);
4079 }
4080 else 3843 else
4081 { 3844 {
4082 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 3845 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
4083 SCHEME_V->code = car (SCHEME_V->code); 3846 SCHEME_V->code = car (SCHEME_V->code);
4084 s_goto (OP_EVAL); 3847 s_goto (OP_EVAL);
4085 } 3848 }
4086 3849
4087 case OP_OR0: /* or */ 3850 case OP_OR0: /* or */
4088 if (SCHEME_V->code == NIL) 3851 if (SCHEME_V->code == NIL)
4089 {
4090 s_return (S_F); 3852 s_return (S_F);
4091 }
4092 3853
4093 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 3854 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
4094 SCHEME_V->code = car (SCHEME_V->code); 3855 SCHEME_V->code = car (SCHEME_V->code);
4095 s_goto (OP_EVAL); 3856 s_goto (OP_EVAL);
4096 3857
4097 case OP_OR1: /* or */ 3858 case OP_OR1: /* or */
4098 if (is_true (SCHEME_V->value)) 3859 if (is_true (SCHEME_V->value))
4099 {
4100 s_return (SCHEME_V->value); 3860 s_return (SCHEME_V->value);
4101 }
4102 else if (SCHEME_V->code == NIL) 3861 else if (SCHEME_V->code == NIL)
4103 {
4104 s_return (SCHEME_V->value); 3862 s_return (SCHEME_V->value);
4105 }
4106 else 3863 else
4107 { 3864 {
4108 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 3865 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
4109 SCHEME_V->code = car (SCHEME_V->code); 3866 SCHEME_V->code = car (SCHEME_V->code);
4110 s_goto (OP_EVAL); 3867 s_goto (OP_EVAL);
4132 x = car (SCHEME_V->code); 3889 x = car (SCHEME_V->code);
4133 SCHEME_V->code = cadr (SCHEME_V->code); 3890 SCHEME_V->code = cadr (SCHEME_V->code);
4134 } 3891 }
4135 3892
4136 if (!is_symbol (x)) 3893 if (!is_symbol (x))
4137 {
4138 Error_0 ("variable is not a symbol"); 3894 Error_0 ("variable is not a symbol");
4139 }
4140 3895
4141 s_save (SCHEME_A_ OP_MACRO1, NIL, x); 3896 s_save (SCHEME_A_ OP_MACRO1, NIL, x);
4142 s_goto (OP_EVAL); 3897 s_goto (OP_EVAL);
4143 3898
4144 case OP_MACRO1: /* macro */ 3899 case OP_MACRO1: /* macro */
4145 set_typeflag (SCHEME_V->value, T_MACRO); 3900 set_typeflag (SCHEME_V->value, T_MACRO);
4146 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); 3901 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0);
4147 3902
4148 if (x != NIL) 3903 if (x != NIL)
4149 {
4150 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); 3904 set_slot_in_env (SCHEME_A_ x, SCHEME_V->value);
4151 }
4152 else 3905 else
4153 {
4154 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 3906 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
4155 }
4156 3907
4157 s_return (SCHEME_V->code); 3908 s_return (SCHEME_V->code);
4158 3909
4159 case OP_CASE0: /* case */ 3910 case OP_CASE0: /* case */
4160 s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code)); 3911 s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code));
4163 3914
4164 case OP_CASE1: /* case */ 3915 case OP_CASE1: /* case */
4165 for (x = SCHEME_V->code; x != NIL; x = cdr (x)) 3916 for (x = SCHEME_V->code; x != NIL; x = cdr (x))
4166 { 3917 {
4167 if (!is_pair (y = caar (x))) 3918 if (!is_pair (y = caar (x)))
4168 {
4169 break; 3919 break;
4170 }
4171 3920
4172 for (; y != NIL; y = cdr (y)) 3921 for (; y != NIL; y = cdr (y))
4173 { 3922 {
4174 if (eqv (car (y), SCHEME_V->value)) 3923 if (eqv (car (y), SCHEME_V->value))
4175 {
4176 break; 3924 break;
4177 }
4178 } 3925 }
4179 3926
4180 if (y != NIL) 3927 if (y != NIL)
4181 {
4182 break; 3928 break;
4183 }
4184 } 3929 }
4185 3930
4186 if (x != NIL) 3931 if (x != NIL)
4187 { 3932 {
4188 if (is_pair (caar (x))) 3933 if (is_pair (caar (x)))
4196 SCHEME_V->code = caar (x); 3941 SCHEME_V->code = caar (x);
4197 s_goto (OP_EVAL); 3942 s_goto (OP_EVAL);
4198 } 3943 }
4199 } 3944 }
4200 else 3945 else
4201 {
4202 s_return (NIL); 3946 s_return (NIL);
4203 }
4204 3947
4205 case OP_CASE2: /* case */ 3948 case OP_CASE2: /* case */
4206 if (is_true (SCHEME_V->value)) 3949 if (is_true (SCHEME_V->value))
4207 {
4208 s_goto (OP_BEGIN); 3950 s_goto (OP_BEGIN);
4209 }
4210 else 3951 else
4211 {
4212 s_return (NIL); 3952 s_return (NIL);
4213 }
4214 3953
4215 case OP_PAPPLY: /* apply */ 3954 case OP_PAPPLY: /* apply */
4216 SCHEME_V->code = car (SCHEME_V->args); 3955 SCHEME_V->code = car (SCHEME_V->args);
4217 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); 3956 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args));
4218 /*SCHEME_V->args = cadr(SCHEME_V->args); */ 3957 /*SCHEME_V->args = cadr(SCHEME_V->args); */
4219 s_goto (OP_APPLY); 3958 s_goto (OP_APPLY);
4220 3959
4221 case OP_PEVAL: /* eval */ 3960 case OP_PEVAL: /* eval */
4222 if (cdr (SCHEME_V->args) != NIL) 3961 if (cdr (SCHEME_V->args) != NIL)
4223 {
4224 SCHEME_V->envir = cadr (SCHEME_V->args); 3962 SCHEME_V->envir = cadr (SCHEME_V->args);
4225 }
4226 3963
4227 SCHEME_V->code = car (SCHEME_V->args); 3964 SCHEME_V->code = car (SCHEME_V->args);
4228 s_goto (OP_EVAL); 3965 s_goto (OP_EVAL);
4229 3966
4230 case OP_CONTINUATION: /* call-with-current-continuation */ 3967 case OP_CONTINUATION: /* call-with-current-continuation */
4231 SCHEME_V->code = car (SCHEME_V->args); 3968 SCHEME_V->code = car (SCHEME_V->args);
4232 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_V)), NIL); 3969 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
4233 s_goto (OP_APPLY); 3970 s_goto (OP_APPLY);
4234 } 3971 }
4235 3972
4236 return S_T; 3973 return S_T;
4237} 3974}
4252 3989
4253 case OP_INEX2EX: /* inexact->exact */ 3990 case OP_INEX2EX: /* inexact->exact */
4254 x = car (SCHEME_V->args); 3991 x = car (SCHEME_V->args);
4255 3992
4256 if (num_is_integer (x)) 3993 if (num_is_integer (x))
4257 {
4258 s_return (x); 3994 s_return (x);
4259 }
4260 else if (modf (rvalue_unchecked (x), &dd) == 0.0) 3995 else if (modf (rvalue_unchecked (x), &dd) == 0.0)
4261 {
4262 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3996 s_return (mk_integer (SCHEME_A_ ivalue (x)));
4263 }
4264 else 3997 else
4265 {
4266 Error_1 ("inexact->exact: not integral:", x); 3998 Error_1 ("inexact->exact: not integral:", x);
4267 }
4268 3999
4269 case OP_EXP: 4000 case OP_EXP:
4270 x = car (SCHEME_V->args); 4001 x = car (SCHEME_V->args);
4271 s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4002 s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4272 4003
4296 4027
4297 case OP_ATAN: 4028 case OP_ATAN:
4298 x = car (SCHEME_V->args); 4029 x = car (SCHEME_V->args);
4299 4030
4300 if (cdr (SCHEME_V->args) == NIL) 4031 if (cdr (SCHEME_V->args) == NIL)
4301 {
4302 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4032 s_return (mk_real (SCHEME_A_ atan (rvalue (x))));
4303 }
4304 else 4033 else
4305 { 4034 {
4306 pointer y = cadr (SCHEME_V->args); 4035 pointer y = cadr (SCHEME_V->args);
4307 4036
4308 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); 4037 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4324 real_result = 0; 4053 real_result = 0;
4325 4054
4326 /* This 'if' is an R5RS compatibility fix. */ 4055 /* This 'if' is an R5RS compatibility fix. */
4327 /* NOTE: Remove this 'if' fix for R6RS. */ 4056 /* NOTE: Remove this 'if' fix for R6RS. */
4328 if (rvalue (x) == 0 && rvalue (y) < 0) 4057 if (rvalue (x) == 0 && rvalue (y) < 0)
4329 {
4330 result = 0.0; 4058 result = 0.0;
4331 }
4332 else 4059 else
4333 {
4334 result = pow (rvalue (x), rvalue (y)); 4060 result = pow (rvalue (x), rvalue (y));
4335 }
4336 4061
4337 /* Before returning integer result make sure we can. */ 4062 /* Before returning integer result make sure we can. */
4338 /* If the test fails, result is too big for integer. */ 4063 /* If the test fails, result is too big for integer. */
4339 if (!real_result) 4064 if (!real_result)
4340 { 4065 {
4343 if (result != (RVALUE) result_as_long) 4068 if (result != (RVALUE) result_as_long)
4344 real_result = 1; 4069 real_result = 1;
4345 } 4070 }
4346 4071
4347 if (real_result) 4072 if (real_result)
4348 {
4349 s_return (mk_real (SCHEME_A_ result)); 4073 s_return (mk_real (SCHEME_A_ result));
4350 }
4351 else 4074 else
4352 {
4353 s_return (mk_integer (SCHEME_A_ result)); 4075 s_return (mk_integer (SCHEME_A_ result));
4354 }
4355 } 4076 }
4356 4077
4357 case OP_FLOOR: 4078 case OP_FLOOR:
4358 x = car (SCHEME_V->args); 4079 x = car (SCHEME_V->args);
4359 s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 4080 s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4368 4089
4369 x = car (SCHEME_V->args); 4090 x = car (SCHEME_V->args);
4370 rvalue_of_x = rvalue (x); 4091 rvalue_of_x = rvalue (x);
4371 4092
4372 if (rvalue_of_x > 0) 4093 if (rvalue_of_x > 0)
4373 {
4374 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); 4094 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x)));
4375 }
4376 else 4095 else
4377 {
4378 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); 4096 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4379 }
4380 } 4097 }
4381 4098
4382 case OP_ROUND: 4099 case OP_ROUND:
4383 x = car (SCHEME_V->args); 4100 x = car (SCHEME_V->args);
4384 4101
4512 } 4229 }
4513 else 4230 else
4514 Error_0 ("set-cdr!: unable to alter immutable pair"); 4231 Error_0 ("set-cdr!: unable to alter immutable pair");
4515 4232
4516 case OP_CHAR2INT: /* char->integer */ 4233 case OP_CHAR2INT: /* char->integer */
4517 { 4234 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args))));
4518 char c;
4519
4520 c = (char) ivalue (car (SCHEME_V->args));
4521 s_return (mk_integer (SCHEME_A_ (unsigned char) c));
4522 }
4523 4235
4524 case OP_INT2CHAR: /* integer->char */ 4236 case OP_INT2CHAR: /* integer->char */
4525 {
4526 unsigned char c;
4527
4528 c = (unsigned char) ivalue (car (SCHEME_V->args));
4529 s_return (mk_character (SCHEME_A_ (char) c)); 4237 s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args))));
4530 }
4531 4238
4532 case OP_CHARUPCASE: 4239 case OP_CHARUPCASE:
4533 { 4240 {
4534 unsigned char c;
4535
4536 c = (unsigned char) ivalue (car (SCHEME_V->args)); 4241 unsigned char c = ivalue (car (SCHEME_V->args));
4537 c = toupper (c); 4242 c = toupper (c);
4538 s_return (mk_character (SCHEME_A_ (char) c)); 4243 s_return (mk_character (SCHEME_A_ c));
4539 } 4244 }
4540 4245
4541 case OP_CHARDNCASE: 4246 case OP_CHARDNCASE:
4542 { 4247 {
4543 unsigned char c;
4544
4545 c = (unsigned char) ivalue (car (SCHEME_V->args)); 4248 unsigned char c = ivalue (car (SCHEME_V->args));
4546 c = tolower (c); 4249 c = tolower (c);
4547 s_return (mk_character (SCHEME_A_ (char) c)); 4250 s_return (mk_character (SCHEME_A_ c));
4548 } 4251 }
4549 4252
4550 case OP_STR2SYM: /* string->symbol */ 4253 case OP_STR2SYM: /* string->symbol */
4551 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); 4254 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args))));
4552 4255
4564 if (pf == 16 || pf == 10 || pf == 8 || pf == 2) 4267 if (pf == 16 || pf == 10 || pf == 8 || pf == 2)
4565 { 4268 {
4566 /* base is OK */ 4269 /* base is OK */
4567 } 4270 }
4568 else 4271 else
4569 {
4570 pf = -1; 4272 pf = -1;
4571 }
4572 } 4273 }
4573 4274
4574 if (pf < 0) 4275 if (pf < 0)
4575 {
4576 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); 4276 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args));
4577 }
4578 else if (*s == '#') /* no use of base! */ 4277 else if (*s == '#') /* no use of base! */
4579 {
4580 s_return (mk_sharp_const (SCHEME_A_ s + 1)); 4278 s_return (mk_sharp_const (SCHEME_A_ s + 1));
4581 }
4582 else 4279 else
4583 { 4280 {
4584 if (pf == 0 || pf == 10) 4281 if (pf == 0 || pf == 10)
4585 {
4586 s_return (mk_atom (SCHEME_A_ s)); 4282 s_return (mk_atom (SCHEME_A_ s));
4587 }
4588 else 4283 else
4589 { 4284 {
4590 char *ep; 4285 char *ep;
4591 long iv = strtol (s, &ep, (int) pf); 4286 long iv = strtol (s, &ep, (int) pf);
4592 4287
4593 if (*ep == 0) 4288 if (*ep == 0)
4594 {
4595 s_return (mk_integer (SCHEME_A_ iv)); 4289 s_return (mk_integer (SCHEME_A_ iv));
4596 }
4597 else 4290 else
4598 {
4599 s_return (S_F); 4291 s_return (S_F);
4600 }
4601 } 4292 }
4602 } 4293 }
4603 } 4294 }
4604 4295
4605 case OP_SYM2STR: /* symbol->string */ 4296 case OP_SYM2STR: /* symbol->string */
4622 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) 4313 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2))
4623 { 4314 {
4624 /* base is OK */ 4315 /* base is OK */
4625 } 4316 }
4626 else 4317 else
4627 {
4628 pf = -1; 4318 pf = -1;
4629 }
4630 } 4319 }
4631 4320
4632 if (pf < 0) 4321 if (pf < 0)
4633 {
4634 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); 4322 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args));
4635 }
4636 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) 4323 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x))
4637 { 4324 {
4638 char *p; 4325 char *p;
4639 int len; 4326 int len;
4640 4327
4641 atom2str (SCHEME_A_ x, (int) pf, &p, &len); 4328 atom2str (SCHEME_A_ x, pf, &p, &len);
4642 s_return (mk_counted_string (SCHEME_A_ p, len)); 4329 s_return (mk_counted_string (SCHEME_A_ p, len));
4643 } 4330 }
4644 else 4331 else
4645 {
4646 Error_1 ("atom->string: not an atom:", x); 4332 Error_1 ("atom->string: not an atom:", x);
4647 }
4648 } 4333 }
4649 4334
4650 case OP_MKSTRING: /* make-string */ 4335 case OP_MKSTRING: /* make-string */
4651 { 4336 {
4652 int fill = ' '; 4337 int fill = ' ';
4653 int len; 4338 int len;
4654 4339
4655 len = ivalue (car (SCHEME_V->args)); 4340 len = ivalue (car (SCHEME_V->args));
4656 4341
4657 if (cdr (SCHEME_V->args) != NIL) 4342 if (cdr (SCHEME_V->args) != NIL)
4658 {
4659 fill = charvalue (cadr (SCHEME_V->args)); 4343 fill = charvalue (cadr (SCHEME_V->args));
4660 }
4661 4344
4662 s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); 4345 s_return (mk_empty_string (SCHEME_A_ len, (char) fill));
4663 } 4346 }
4664 4347
4665 case OP_STRLEN: /* string-length */ 4348 case OP_STRLEN: /* string-length */
4673 str = strvalue (car (SCHEME_V->args)); 4356 str = strvalue (car (SCHEME_V->args));
4674 4357
4675 index = ivalue (cadr (SCHEME_V->args)); 4358 index = ivalue (cadr (SCHEME_V->args));
4676 4359
4677 if (index >= strlength (car (SCHEME_V->args))) 4360 if (index >= strlength (car (SCHEME_V->args)))
4678 {
4679 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); 4361 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args));
4680 }
4681 4362
4682 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); 4363 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index]));
4683 } 4364 }
4684 4365
4685 case OP_STRSET: /* string-set! */ 4366 case OP_STRSET: /* string-set! */
4687 char *str; 4368 char *str;
4688 int index; 4369 int index;
4689 int c; 4370 int c;
4690 4371
4691 if (is_immutable (car (SCHEME_V->args))) 4372 if (is_immutable (car (SCHEME_V->args)))
4692 {
4693 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); 4373 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args));
4694 }
4695 4374
4696 str = strvalue (car (SCHEME_V->args)); 4375 str = strvalue (car (SCHEME_V->args));
4697 4376
4698 index = ivalue (cadr (SCHEME_V->args)); 4377 index = ivalue (cadr (SCHEME_V->args));
4699 4378
4700 if (index >= strlength (car (SCHEME_V->args))) 4379 if (index >= strlength (car (SCHEME_V->args)))
4701 {
4702 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); 4380 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args));
4703 }
4704 4381
4705 c = charvalue (caddr (SCHEME_V->args)); 4382 c = charvalue (caddr (SCHEME_V->args));
4706 4383
4707 str[index] = (char) c; 4384 str[index] = (char) c;
4708 s_return (car (SCHEME_V->args)); 4385 s_return (car (SCHEME_V->args));
4715 pointer newstr; 4392 pointer newstr;
4716 char *pos; 4393 char *pos;
4717 4394
4718 /* compute needed length for new string */ 4395 /* compute needed length for new string */
4719 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4396 for (x = SCHEME_V->args; x != NIL; x = cdr (x))
4720 {
4721 len += strlength (car (x)); 4397 len += strlength (car (x));
4722 }
4723 4398
4724 newstr = mk_empty_string (SCHEME_A_ len, ' '); 4399 newstr = mk_empty_string (SCHEME_A_ len, ' ');
4725 4400
4726 /* store the contents of the argument strings into the new string */ 4401 /* store the contents of the argument strings into the new string */
4727 for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) 4402 for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x))
4728 {
4729 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4403 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4730 }
4731 4404
4732 s_return (newstr); 4405 s_return (newstr);
4733 } 4406 }
4734 4407
4735 case OP_SUBSTR: /* substring */ 4408 case OP_SUBSTR: /* substring */
4742 str = strvalue (car (SCHEME_V->args)); 4415 str = strvalue (car (SCHEME_V->args));
4743 4416
4744 index0 = ivalue (cadr (SCHEME_V->args)); 4417 index0 = ivalue (cadr (SCHEME_V->args));
4745 4418
4746 if (index0 > strlength (car (SCHEME_V->args))) 4419 if (index0 > strlength (car (SCHEME_V->args)))
4747 {
4748 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); 4420 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args));
4749 }
4750 4421
4751 if (cddr (SCHEME_V->args) != NIL) 4422 if (cddr (SCHEME_V->args) != NIL)
4752 { 4423 {
4753 index1 = ivalue (caddr (SCHEME_V->args)); 4424 index1 = ivalue (caddr (SCHEME_V->args));
4754 4425
4755 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) 4426 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0)
4756 {
4757 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); 4427 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args));
4758 }
4759 } 4428 }
4760 else 4429 else
4761 {
4762 index1 = strlength (car (SCHEME_V->args)); 4430 index1 = strlength (car (SCHEME_V->args));
4763 }
4764 4431
4765 len = index1 - index0; 4432 len = index1 - index0;
4766 x = mk_empty_string (SCHEME_A_ len, ' '); 4433 x = mk_empty_string (SCHEME_A_ len, ' ');
4767 memcpy (strvalue (x), str + index0, len); 4434 memcpy (strvalue (x), str + index0, len);
4768 strvalue (x)[len] = 0; 4435 strvalue (x)[len] = 0;
4775 int i; 4442 int i;
4776 pointer vec; 4443 pointer vec;
4777 int len = list_length (SCHEME_A_ SCHEME_V->args); 4444 int len = list_length (SCHEME_A_ SCHEME_V->args);
4778 4445
4779 if (len < 0) 4446 if (len < 0)
4780 {
4781 Error_1 ("vector: not a proper list:", SCHEME_V->args); 4447 Error_1 ("vector: not a proper list:", SCHEME_V->args);
4782 }
4783 4448
4784 vec = mk_vector (SCHEME_A_ len); 4449 vec = mk_vector (SCHEME_A_ len);
4785 4450
4786#if USE_ERROR_CHECKING 4451#if USE_ERROR_CHECKING
4787 if (SCHEME_V->no_memory) 4452 if (SCHEME_V->no_memory)
4817 4482
4818 s_return (vec); 4483 s_return (vec);
4819 } 4484 }
4820 4485
4821 case OP_VECLEN: /* vector-length */ 4486 case OP_VECLEN: /* vector-length */
4822 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4487 s_return (mk_integer (SCHEME_A_ veclength (car (SCHEME_V->args))));
4823 4488
4824 case OP_VECREF: /* vector-ref */ 4489 case OP_VECREF: /* vector-ref */
4825 { 4490 {
4826 int index; 4491 int index;
4827 4492
4828 index = ivalue (cadr (SCHEME_V->args)); 4493 index = ivalue (cadr (SCHEME_V->args));
4829 4494
4830 if (index >= ivalue (car (SCHEME_V->args))) 4495 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING)
4831 {
4832 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); 4496 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args));
4833 }
4834 4497
4835 s_return (vector_elem (car (SCHEME_V->args), index)); 4498 s_return (vector_elem (car (SCHEME_V->args), index));
4836 } 4499 }
4837 4500
4838 case OP_VECSET: /* vector-set! */ 4501 case OP_VECSET: /* vector-set! */
4839 { 4502 {
4840 int index; 4503 int index;
4841 4504
4842 if (is_immutable (car (SCHEME_V->args))) 4505 if (is_immutable (car (SCHEME_V->args)))
4843 {
4844 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); 4506 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args));
4845 }
4846 4507
4847 index = ivalue (cadr (SCHEME_V->args)); 4508 index = ivalue (cadr (SCHEME_V->args));
4848 4509
4849 if (index >= ivalue (car (SCHEME_V->args))) 4510 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING)
4850 {
4851 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); 4511 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args));
4852 }
4853 4512
4854 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); 4513 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args));
4855 s_return (car (SCHEME_V->args)); 4514 s_return (car (SCHEME_V->args));
4856 } 4515 }
4857 } 4516 }
4967 x = cdr (x); 4626 x = cdr (x);
4968 4627
4969 for (; x != NIL; x = cdr (x)) 4628 for (; x != NIL; x = cdr (x))
4970 { 4629 {
4971 if (!comp_func (v, nvalue (car (x)))) 4630 if (!comp_func (v, nvalue (car (x))))
4972 {
4973 s_retbool (0); 4631 s_retbool (0);
4974 }
4975 4632
4976 v = nvalue (car (x)); 4633 v = nvalue (car (x));
4977 } 4634 }
4978 4635
4979 s_retbool (1); 4636 s_retbool (1);
5072 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); 4729 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code);
5073 SCHEME_V->args = NIL; 4730 SCHEME_V->args = NIL;
5074 s_goto (OP_APPLY); 4731 s_goto (OP_APPLY);
5075 } 4732 }
5076 else 4733 else
5077 {
5078 s_return (SCHEME_V->code); 4734 s_return (SCHEME_V->code);
5079 }
5080 4735
5081 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4736 case OP_SAVE_FORCED: /* Save forced value replacing promise */
5082 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); 4737 memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell));
5083 s_return (SCHEME_V->value); 4738 s_return (SCHEME_V->value);
5084 4739
5148 else 4803 else
5149 { 4804 {
5150 putstr (SCHEME_A_ "\n"); 4805 putstr (SCHEME_A_ "\n");
5151 4806
5152 if (SCHEME_V->interactive_repl) 4807 if (SCHEME_V->interactive_repl)
5153 {
5154 s_goto (OP_T0LVL); 4808 s_goto (OP_T0LVL);
5155 }
5156 else 4809 else
5157 {
5158 return NIL; 4810 return NIL;
5159 }
5160 } 4811 }
5161 4812
5162 case OP_REVERSE: /* reverse */ 4813 case OP_REVERSE: /* reverse */
5163 s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); 4814 s_return (reverse (SCHEME_A_ car (SCHEME_V->args)));
5164 4815
5187 4838
5188#if USE_PLIST 4839#if USE_PLIST
5189 4840
5190 case OP_PUT: /* put */ 4841 case OP_PUT: /* put */
5191 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4842 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args)))
5192 {
5193 Error_0 ("illegal use of put"); 4843 Error_0 ("illegal use of put");
5194 }
5195 4844
5196 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4845 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x))
5197 { 4846 {
5198 if (caar (x) == y) 4847 if (caar (x) == y)
5199 {
5200 break; 4848 break;
5201 }
5202 } 4849 }
5203 4850
5204 if (x != NIL) 4851 if (x != NIL)
5205 cdar (x) = caddr (SCHEME_V->args); 4852 cdar (x) = caddr (SCHEME_V->args);
5206 else 4853 else
5283 } 4930 }
5284 4931
5285 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); 4932 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop);
5286 4933
5287 if (p == NIL) 4934 if (p == NIL)
5288 {
5289 s_return (S_F); 4935 s_return (S_F);
5290 }
5291 4936
5292 s_return (p); 4937 s_return (p);
5293 } 4938 }
5294 4939
5295# if USE_STRING_PORTS 4940# if USE_STRING_PORTS
5313 4958
5314 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4959 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)),
5315 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); 4960 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop);
5316 4961
5317 if (p == NIL) 4962 if (p == NIL)
5318 {
5319 s_return (S_F); 4963 s_return (S_F);
5320 }
5321 4964
5322 s_return (p); 4965 s_return (p);
5323 } 4966 }
5324 4967
5325 case OP_OPEN_OUTSTRING: /* open-output-string */ 4968 case OP_OPEN_OUTSTRING: /* open-output-string */
5329 if (car (SCHEME_V->args) == NIL) 4972 if (car (SCHEME_V->args) == NIL)
5330 { 4973 {
5331 p = port_from_scratch (SCHEME_A); 4974 p = port_from_scratch (SCHEME_A);
5332 4975
5333 if (p == NIL) 4976 if (p == NIL)
5334 {
5335 s_return (S_F); 4977 s_return (S_F);
5336 }
5337 } 4978 }
5338 else 4979 else
5339 { 4980 {
5340 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4981 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)),
5341 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); 4982 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output);
5342 4983
5343 if (p == NIL) 4984 if (p == NIL)
5344 {
5345 s_return (S_F); 4985 s_return (S_F);
5346 }
5347 } 4986 }
5348 4987
5349 s_return (p); 4988 s_return (p);
5350 } 4989 }
5351 4990
5416 { 5055 {
5417 /* ========== reading part ========== */ 5056 /* ========== reading part ========== */
5418#if USE_PORTS 5057#if USE_PORTS
5419 case OP_READ: 5058 case OP_READ:
5420 if (!is_pair (SCHEME_V->args)) 5059 if (!is_pair (SCHEME_V->args))
5421 {
5422 s_goto (OP_READ_INTERNAL); 5060 s_goto (OP_READ_INTERNAL);
5423 }
5424 5061
5425 if (!is_inport (car (SCHEME_V->args))) 5062 if (!is_inport (car (SCHEME_V->args)))
5426 {
5427 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 5063 Error_1 ("read: not an input port:", car (SCHEME_V->args));
5428 }
5429 5064
5430 if (car (SCHEME_V->args) == SCHEME_V->inport) 5065 if (car (SCHEME_V->args) == SCHEME_V->inport)
5431 {
5432 s_goto (OP_READ_INTERNAL); 5066 s_goto (OP_READ_INTERNAL);
5433 }
5434 5067
5435 x = SCHEME_V->inport; 5068 x = SCHEME_V->inport;
5436 SCHEME_V->inport = car (SCHEME_V->args); 5069 SCHEME_V->inport = car (SCHEME_V->args);
5437 x = cons (x, NIL); 5070 x = cons (x, NIL);
5438 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 5071 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5469 { 5102 {
5470 pointer p = SCHEME_V->inport; 5103 pointer p = SCHEME_V->inport;
5471 int res; 5104 int res;
5472 5105
5473 if (is_pair (SCHEME_V->args)) 5106 if (is_pair (SCHEME_V->args))
5474 {
5475 p = car (SCHEME_V->args); 5107 p = car (SCHEME_V->args);
5476 }
5477 5108
5478 res = p->object.port->kind & port_string; 5109 res = p->object.port->kind & port_string;
5479 5110
5480 s_retbool (res); 5111 s_retbool (res);
5481 } 5112 }
5496 s_return (S_EOF); 5127 s_return (S_EOF);
5497 /* NOTREACHED */ 5128 /* NOTREACHED */
5498 5129
5499 case TOK_VEC: 5130 case TOK_VEC:
5500 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); 5131 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL);
5501
5502 /* fall through */ 5132 /* fall through */
5133
5503 case TOK_LPAREN: 5134 case TOK_LPAREN:
5504 SCHEME_V->tok = token (SCHEME_A); 5135 SCHEME_V->tok = token (SCHEME_A);
5505 5136
5506 if (SCHEME_V->tok == TOK_RPAREN) 5137 if (SCHEME_V->tok == TOK_RPAREN)
5507 s_return (NIL); 5138 s_return (NIL);
5527 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); 5158 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL);
5528 SCHEME_V->tok = TOK_LPAREN; 5159 SCHEME_V->tok = TOK_LPAREN;
5529 s_goto (OP_RDSEXPR); 5160 s_goto (OP_RDSEXPR);
5530 } 5161 }
5531 else 5162 else
5532 {
5533 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); 5163 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL);
5534 }
5535 5164
5536 s_goto (OP_RDSEXPR); 5165 s_goto (OP_RDSEXPR);
5537 5166
5538 case TOK_COMMA: 5167 case TOK_COMMA:
5539 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); 5168 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL);
5581 } 5210 }
5582 5211
5583 break; 5212 break;
5584 5213
5585 case OP_RDLIST: 5214 case OP_RDLIST:
5586 {
5587 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5215 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args);
5588 SCHEME_V->tok = token (SCHEME_A); 5216 SCHEME_V->tok = token (SCHEME_A);
5589 5217
5590 if (SCHEME_V->tok == TOK_EOF) 5218 switch (SCHEME_V->tok)
5219 {
5220 case TOK_EOF:
5591 s_return (S_EOF); 5221 s_return (S_EOF);
5592 else if (SCHEME_V->tok == TOK_RPAREN) 5222
5223 case TOK_RPAREN:
5593 { 5224 {
5594 int c = inchar (SCHEME_A); 5225 int c = inchar (SCHEME_A);
5595 5226
5596 if (c != '\n') 5227 if (c != '\n')
5597 backchar (SCHEME_A_ c); 5228 backchar (SCHEME_A_ c);
5598
5599#if SHOW_ERROR_LINE 5229#if SHOW_ERROR_LINE
5600 else if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) 5230 else if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file)
5601 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line++; 5231 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line++;
5602
5603#endif 5232#endif
5233
5604 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5234 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5605 s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args)); 5235 s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args));
5606 } 5236 }
5607 else if (SCHEME_V->tok == TOK_DOT) 5237
5608 { 5238 case TOK_DOT:
5609 s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL); 5239 s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL);
5610 SCHEME_V->tok = token (SCHEME_A); 5240 SCHEME_V->tok = token (SCHEME_A);
5611 s_goto (OP_RDSEXPR); 5241 s_goto (OP_RDSEXPR);
5612 } 5242
5613 else 5243 default:
5614 {
5615 s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL);; 5244 s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL);
5616 s_goto (OP_RDSEXPR); 5245 s_goto (OP_RDSEXPR);
5617 } 5246 }
5618 }
5619 5247
5620 case OP_RDDOT: 5248 case OP_RDDOT:
5621 if (token (SCHEME_A) != TOK_RPAREN) 5249 if (token (SCHEME_A) != TOK_RPAREN)
5622 Error_0 ("syntax error: illegal dot expression"); 5250 Error_0 ("syntax error: illegal dot expression");
5623 else 5251
5624 {
5625 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5252 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5626 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5253 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args));
5627 }
5628 5254
5629 case OP_RDQUOTE: 5255 case OP_RDQUOTE:
5630 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5256 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5631 5257
5632 case OP_RDQQUOTE: 5258 case OP_RDQQUOTE:
5732 5358
5733 case OP_PVECFROM: 5359 case OP_PVECFROM:
5734 { 5360 {
5735 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5361 int i = ivalue_unchecked (cdr (SCHEME_V->args));
5736 pointer vec = car (SCHEME_V->args); 5362 pointer vec = car (SCHEME_V->args);
5737 int len = ivalue_unchecked (vec); 5363 int len = veclength (vec);
5738 5364
5739 if (i == len) 5365 if (i == len)
5740 { 5366 {
5741 putstr (SCHEME_A_ ")"); 5367 putstr (SCHEME_A_ ")");
5742 s_return (S_T); 5368 s_return (S_T);
5898{ 5524{
5899 int n = procnum (x); 5525 int n = procnum (x);
5900 const char *name = dispatch_table[n].name; 5526 const char *name = dispatch_table[n].name;
5901 5527
5902 if (name == 0) 5528 if (name == 0)
5903 {
5904 name = "ILLEGAL!"; 5529 name = "ILLEGAL!";
5905 }
5906 5530
5907 return name; 5531 return name;
5908} 5532}
5909 5533
5910/* kernel of this interpreter */ 5534/* kernel of this interpreter */
5915 5539
5916 for (;;) 5540 for (;;)
5917 { 5541 {
5918 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5542 op_code_info *pcd = dispatch_table + SCHEME_V->op;
5919 5543
5544#if USE_ERROR_CHECKING
5920 if (pcd->name != 0) /* if built-in function, check arguments */ 5545 if (pcd->name) /* if built-in function, check arguments */
5921 { 5546 {
5547 int ok = 1;
5922 char msg[STRBUFFSIZE]; 5548 char msg[STRBUFFSIZE];
5923 int ok = 1;
5924 int n = list_length (SCHEME_A_ SCHEME_V->args); 5549 int n = list_length (SCHEME_A_ SCHEME_V->args);
5925 5550
5926#if USE_ERROR_CHECKING
5927 /* Check number of arguments */ 5551 /* Check number of arguments */
5928 if (n < pcd->min_arity) 5552 if (ecb_expect_false (n < pcd->min_arity))
5929 { 5553 {
5930 ok = 0; 5554 ok = 0;
5931 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5555 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5932 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5556 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5933 } 5557 }
5934 5558 else if (ecb_excpect_false (n > pcd->max_arity))
5935 if (ok && n > pcd->max_arity)
5936 { 5559 {
5937 ok = 0; 5560 ok = 0;
5938 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5561 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5939 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5562 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5940 } 5563 }
5941#endif
5942 5564
5943 if (ok) 5565 if (ecb_expect_false (ok))
5944 { 5566 {
5945 if (pcd->arg_tests_encoding != 0) 5567 if (pcd->arg_tests_encoding)
5946 { 5568 {
5947 int i = 0; 5569 int i = 0;
5948 int j; 5570 int j;
5949 const char *t = pcd->arg_tests_encoding; 5571 const char *t = pcd->arg_tests_encoding;
5950 pointer arglist = SCHEME_V->args; 5572 pointer arglist = SCHEME_V->args;
5965 if (!tests[j].fct (arg)) 5587 if (!tests[j].fct (arg))
5966 break; 5588 break;
5967 } 5589 }
5968 5590
5969 if (t[1] != 0) /* last test is replicated as necessary */ 5591 if (t[1] != 0) /* last test is replicated as necessary */
5970 {
5971 t++; 5592 t++;
5972 }
5973 5593
5974 arglist = cdr (arglist); 5594 arglist = cdr (arglist);
5975 i++; 5595 i++;
5976 } 5596 }
5977 while (i < n); 5597 while (i < n);
5978 5598
5979#if USE_ERROR_CHECKING
5980 if (i < n) 5599 if (i < n)
5981 { 5600 {
5982 ok = 0; 5601 ok = 0;
5983 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5602 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind);
5984 } 5603 }
5985#endif
5986 } 5604 }
5987 } 5605 }
5988 5606
5989 if (!ok) 5607 if (!ok)
5990 { 5608 {
5992 return; 5610 return;
5993 5611
5994 pcd = dispatch_table + SCHEME_V->op; 5612 pcd = dispatch_table + SCHEME_V->op;
5995 } 5613 }
5996 } 5614 }
5615#endif
5997 5616
5998 ok_to_freely_gc (SCHEME_A); 5617 ok_to_freely_gc (SCHEME_A);
5999 5618
6000 if (pcd->func (SCHEME_A_ (enum scheme_opcodes) SCHEME_V->op) == NIL) 5619 if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL))
6001 return; 5620 return;
6002 5621
6003#if USE_ERROR_CHECKING 5622 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
6004 if (SCHEME_V->no_memory)
6005 { 5623 {
6006 xwrstr ("No memory!\n"); 5624 xwrstr ("No memory!\n");
6007 return; 5625 return;
6008 } 5626 }
6009#endif
6010 } 5627 }
6011} 5628}
6012 5629
6013/* ========== Initialization of internal keywords ========== */ 5630/* ========== Initialization of internal keywords ========== */
6014 5631
6015static void 5632static void
6016assign_syntax (SCHEME_P_ char *name) 5633assign_syntax (SCHEME_P_ const char *name)
6017{ 5634{
6018 pointer x = oblist_add_by_name (SCHEME_A_ name); 5635 pointer x = oblist_add_by_name (SCHEME_A_ name);
6019 set_typeflag (x, typeflag (x) | T_SYNTAX); 5636 set_typeflag (x, typeflag (x) | T_SYNTAX);
6020} 5637}
6021 5638
6022static void 5639static void
6023assign_proc (SCHEME_P_ enum scheme_opcodes op, char *name) 5640assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
6024{ 5641{
6025 pointer x = mk_symbol (SCHEME_A_ name); 5642 pointer x = mk_symbol (SCHEME_A_ name);
6026 pointer y = mk_proc (SCHEME_A_ op); 5643 pointer y = mk_proc (SCHEME_A_ op);
6027 new_slot_in_env (SCHEME_A_ x, y); 5644 new_slot_in_env (SCHEME_A_ x, y);
6028} 5645}
6030static pointer 5647static pointer
6031mk_proc (SCHEME_P_ enum scheme_opcodes op) 5648mk_proc (SCHEME_P_ enum scheme_opcodes op)
6032{ 5649{
6033 pointer y = get_cell (SCHEME_A_ NIL, NIL); 5650 pointer y = get_cell (SCHEME_A_ NIL, NIL);
6034 set_typeflag (y, (T_PROC | T_ATOM)); 5651 set_typeflag (y, (T_PROC | T_ATOM));
6035 ivalue_unchecked (y) = (long) op; 5652 ivalue_unchecked (y) = op;
6036 set_num_integer (y); 5653 set_num_integer (y);
6037 return y; 5654 return y;
6038} 5655}
6039 5656
6040/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5657/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
6065 5682
6066 case 'd': 5683 case 'd':
6067 return OP_COND0; /* cond */ 5684 return OP_COND0; /* cond */
6068 5685
6069 case '*': 5686 case '*':
6070 return OP_LET0AST; /* let* */ 5687 return OP_LET0AST;/* let* */
6071 5688
6072 default: 5689 default:
6073 return OP_SET0; /* set! */ 5690 return OP_SET0; /* set! */
6074 } 5691 }
6075 5692
6097 5714
6098 case 'f': 5715 case 'f':
6099 return OP_DEF0; /* define */ 5716 return OP_DEF0; /* define */
6100 5717
6101 default: 5718 default:
6102 return OP_LET0REC; /* letrec */ 5719 return OP_LET0REC;/* letrec */
6103 } 5720 }
6104 5721
6105 default: 5722 default:
6106 return OP_C0STREAM; /* cons-stream */ 5723 return OP_C0STREAM; /* cons-stream */
6107 } 5724 }
6158 } 5775 }
6159 5776
6160 SCHEME_V->gc_verbose = 0; 5777 SCHEME_V->gc_verbose = 0;
6161 dump_stack_initialize (SCHEME_A); 5778 dump_stack_initialize (SCHEME_A);
6162 SCHEME_V->code = NIL; 5779 SCHEME_V->code = NIL;
5780 SCHEME_V->args = NIL;
5781 SCHEME_V->envir = NIL;
6163 SCHEME_V->tracing = 0; 5782 SCHEME_V->tracing = 0;
6164 5783
6165 /* init NIL */ 5784 /* init NIL */
6166 set_typeflag (NIL, T_ATOM | MARK); 5785 set_typeflag (NIL, T_ATOM | T_MARK);
6167 set_car (NIL, NIL); 5786 set_car (NIL, NIL);
6168 set_cdr (NIL, NIL); 5787 set_cdr (NIL, NIL);
6169 /* init T */ 5788 /* init T */
6170 set_typeflag (S_T, T_ATOM | MARK); 5789 set_typeflag (S_T, T_ATOM | T_MARK);
6171 set_car (S_T, S_T); 5790 set_car (S_T, S_T);
6172 set_cdr (S_T, S_T); 5791 set_cdr (S_T, S_T);
6173 /* init F */ 5792 /* init F */
6174 set_typeflag (S_F, T_ATOM | MARK); 5793 set_typeflag (S_F, T_ATOM | T_MARK);
6175 set_car (S_F, S_F); 5794 set_car (S_F, S_F);
6176 set_cdr (S_F, S_F); 5795 set_cdr (S_F, S_F);
5796 /* init EOF_OBJ */
5797 set_typeflag (S_EOF, T_ATOM | T_MARK);
5798 set_car (S_EOF, S_EOF);
5799 set_cdr (S_EOF, S_EOF);
6177 /* init sink */ 5800 /* init sink */
6178 set_typeflag (S_SINK, T_PAIR | MARK); 5801 set_typeflag (S_SINK, T_PAIR | T_MARK);
6179 set_car (S_SINK, NIL); 5802 set_car (S_SINK, NIL);
5803
6180 /* init c_nest */ 5804 /* init c_nest */
6181 SCHEME_V->c_nest = NIL; 5805 SCHEME_V->c_nest = NIL;
6182 5806
6183 SCHEME_V->oblist = oblist_initial_value (SCHEME_A); 5807 SCHEME_V->oblist = oblist_initial_value (SCHEME_A);
6184 /* init global_env */ 5808 /* init global_env */
6186 SCHEME_V->global_env = SCHEME_V->envir; 5810 SCHEME_V->global_env = SCHEME_V->envir;
6187 /* init else */ 5811 /* init else */
6188 x = mk_symbol (SCHEME_A_ "else"); 5812 x = mk_symbol (SCHEME_A_ "else");
6189 new_slot_in_env (SCHEME_A_ x, S_T); 5813 new_slot_in_env (SCHEME_A_ x, S_T);
6190 5814
6191 assign_syntax (SCHEME_A_ "lambda"); 5815 {
6192 assign_syntax (SCHEME_A_ "quote"); 5816 static const char *syntax_names[] = {
6193 assign_syntax (SCHEME_A_ "define"); 5817 "lambda", "quote", "define", "if", "begin", "set!",
6194 assign_syntax (SCHEME_A_ "if"); 5818 "let", "let*", "letrec", "cond", "delay", "and",
6195 assign_syntax (SCHEME_A_ "begin"); 5819 "or", "cons-stream", "macro", "case"
6196 assign_syntax (SCHEME_A_ "set!"); 5820 };
6197 assign_syntax (SCHEME_A_ "let"); 5821
6198 assign_syntax (SCHEME_A_ "let*"); 5822 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
6199 assign_syntax (SCHEME_A_ "letrec");
6200 assign_syntax (SCHEME_A_ "cond");
6201 assign_syntax (SCHEME_A_ "delay");
6202 assign_syntax (SCHEME_A_ "and"); 5823 assign_syntax (SCHEME_A_ syntax_names[i]);
6203 assign_syntax (SCHEME_A_ "or"); 5824 }
6204 assign_syntax (SCHEME_A_ "cons-stream");
6205 assign_syntax (SCHEME_A_ "macro");
6206 assign_syntax (SCHEME_A_ "case");
6207 5825
6208 for (i = 0; i < n; i++) 5826 for (i = 0; i < n; i++)
6209 {
6210 if (dispatch_table[i].name != 0) 5827 if (dispatch_table[i].name != 0)
6211 {
6212 assign_proc (SCHEME_A_ (enum scheme_opcodes) i, dispatch_table[i].name); 5828 assign_proc (SCHEME_A_ i, dispatch_table[i].name);
6213 }
6214 }
6215 5829
6216 /* initialization of global pointers to special symbols */ 5830 /* initialization of global pointers to special symbols */
6217 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5831 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
6218 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5832 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
6219 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5833 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
6220 SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); 5834 SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote");
6221 SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); 5835 SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing");
6222 SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); 5836 SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>");
6223 SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); 5837 SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*");
6224 SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); 5838 SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*");
6225 SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); 5839 SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*");
6226 SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); 5840 SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*");
6227 5841
6228 return !SCHEME_V->no_memory; 5842 return !SCHEME_V->no_memory;
6229} 5843}
6230 5844
6385 pointer x; 5999 pointer x;
6386 6000
6387 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6001 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
6388 6002
6389 if (x != NIL) 6003 if (x != NIL)
6390 {
6391 set_slot_in_env (SCHEME_A_ x, value); 6004 set_slot_in_env (SCHEME_A_ x, value);
6392 }
6393 else 6005 else
6394 {
6395 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6006 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
6396 }
6397} 6007}
6398 6008
6399#if !STANDALONE 6009#if !STANDALONE
6010
6400void 6011void
6401scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6012scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
6402{ 6013{
6403 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); 6014 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f));
6404} 6015}
6407scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6018scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
6408{ 6019{
6409 int i; 6020 int i;
6410 6021
6411 for (i = 0; i < count; i++) 6022 for (i = 0; i < count; i++)
6412 {
6413 scheme_register_foreign_func (SCHEME_A_ list + i); 6023 scheme_register_foreign_func (SCHEME_A_ list + i);
6414 }
6415} 6024}
6416 6025
6417pointer 6026pointer
6418scheme_apply0 (SCHEME_P_ const char *procname) 6027scheme_apply0 (SCHEME_P_ const char *procname)
6419{ 6028{
6476 SCHEME_V->interactive_repl = old_repl; 6085 SCHEME_V->interactive_repl = old_repl;
6477 restore_from_C_call (SCHEME_A); 6086 restore_from_C_call (SCHEME_A);
6478 return SCHEME_V->value; 6087 return SCHEME_V->value;
6479} 6088}
6480 6089
6481
6482#endif 6090#endif
6483 6091
6484/* ========== Main ========== */ 6092/* ========== Main ========== */
6485 6093
6486#if STANDALONE 6094#if STANDALONE
6487 6095
6488# if defined(__APPLE__) && !defined (OSX)
6489int
6490main ()
6491{
6492 extern MacTS_main (int argc, char **argv);
6493 char **argv;
6494 int argc = ccommand (&argv);
6495
6496 MacTS_main (argc, argv);
6497 return 0;
6498}
6499
6500int
6501MacTS_main (int argc, char **argv)
6502{
6503# else
6504int 6096int
6505main (int argc, char **argv) 6097main (int argc, char **argv)
6506{ 6098{
6507# endif
6508# if USE_MULTIPLICITY 6099# if USE_MULTIPLICITY
6509 scheme ssc; 6100 scheme ssc;
6510 scheme *const sc = &ssc; 6101 scheme *const SCHEME_V = &ssc;
6511# else 6102# else
6512# endif 6103# endif
6513 int fin; 6104 int fin;
6514 char *file_name = InitFile; 6105 char *file_name = InitFile;
6515 int retcode; 6106 int retcode;
6516 int isfile = 1; 6107 int isfile = 1;
6517
6518 if (argc == 1)
6519 xwrstr (banner);
6520 6108
6521 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6109 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6522 { 6110 {
6523 xwrstr ("Usage: tinyscheme -?\n"); 6111 xwrstr ("Usage: tinyscheme -?\n");
6524 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6112 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n");
6547 if (access (file_name, 0) != 0) 6135 if (access (file_name, 0) != 0)
6548 { 6136 {
6549 char *p = getenv ("TINYSCHEMEINIT"); 6137 char *p = getenv ("TINYSCHEMEINIT");
6550 6138
6551 if (p != 0) 6139 if (p != 0)
6552 {
6553 file_name = p; 6140 file_name = p;
6554 }
6555 } 6141 }
6556#endif 6142#endif
6557 6143
6558 do 6144 do
6559 { 6145 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines