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.51 by root, Tue Dec 1 01:54:27 2015 UTC vs.
Revision 1.66 by root, Mon Dec 7 18:10:57 2015 UTC

16 * (MINISCM) This is a revised and modified version by Akira KIDA. 16 * (MINISCM) This is a revised and modified version by Akira KIDA.
17 * (MINISCM) current version is 0.85k4 (15 May 1994) 17 * (MINISCM) current version is 0.85k4 (15 May 1994)
18 * 18 *
19 */ 19 */
20 20
21#define EXPERIMENT 1 21#define _POSIX_C_SOURCE 200201
22 22#define _XOPEN_SOURCE 600
23#define PAGE_SIZE 4096 /* does not work on sparc/alpha */ 23#define _GNU_SOURCE 1 /* for malloc mremap */
24#include "malloc.c"
25 24
26#define SCHEME_SOURCE 25#define SCHEME_SOURCE
27#include "scheme-private.h" 26#include "scheme-private.h"
28#ifndef WIN32 27#ifndef WIN32
29# include <unistd.h> 28# include <unistd.h>
30#endif 29#endif
31#if USE_MATH 30#if USE_MATH
32# include <math.h> 31# include <math.h>
33#endif 32#endif
34 33
34#define ECB_NO_THREADS 1
35#include "ecb.h" 35#include "ecb.h"
36 36
37#include <sys/types.h> 37#include <sys/types.h>
38#include <sys/stat.h> 38#include <sys/stat.h>
39#include <fcntl.h> 39#include <fcntl.h>
47#include <string.h> 47#include <string.h>
48 48
49#include <limits.h> 49#include <limits.h>
50#include <inttypes.h> 50#include <inttypes.h>
51#include <float.h> 51#include <float.h>
52//#include <ctype.h> 52
53#if !USE_SYSTEM_MALLOC
54# define PAGE_SIZE 4096 /* does not work on sparc/alpha */
55# include "malloc.c"
56# define malloc(n) tiny_malloc (n)
57# define realloc(p,n) tiny_realloc (p, n)
58# define free(p) tiny_free (p)
59#endif
53 60
54#if '1' != '0' + 1 \ 61#if '1' != '0' + 1 \
55 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ 62 || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \
56 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ 63 || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \
57 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ 64 || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \
89 96
90#if !USE_MULTIPLICITY 97#if !USE_MULTIPLICITY
91static scheme sc; 98static scheme sc;
92#endif 99#endif
93 100
94static void 101ecb_cold static void
95xbase (char *s, long n, int base) 102xbase (char *s, long n, int base)
96{ 103{
97 if (n < 0) 104 if (n < 0)
98 { 105 {
99 *s++ = '-'; 106 *s++ = '-';
101 } 108 }
102 109
103 char *p = s; 110 char *p = s;
104 111
105 do { 112 do {
106 *p++ = '0' + n % base; 113 *p++ = "0123456789abcdef"[n % base];
107 n /= base; 114 n /= base;
108 } while (n); 115 } while (n);
109 116
110 *p-- = 0; 117 *p-- = 0;
111 118
114 char x = *s; *s = *p; *p = x; 121 char x = *s; *s = *p; *p = x;
115 --p; ++s; 122 --p; ++s;
116 } 123 }
117} 124}
118 125
119static void 126ecb_cold static void
120xnum (char *s, long n) 127xnum (char *s, long n)
121{ 128{
122 xbase (s, n, 10); 129 xbase (s, n, 10);
123} 130}
124 131
125static void 132ecb_cold static void
126xwrstr (const char *s) 133putnum (SCHEME_P_ long n)
127{
128 write (1, s, strlen (s));
129}
130
131static void
132xwrnum (long n)
133{ 134{
134 char buf[64]; 135 char buf[64];
135 136
136 xnum (buf, n); 137 xnum (buf, n);
137 xwrstr (buf); 138 putstr (SCHEME_A_ buf);
138} 139}
140
141#if USE_CHAR_CLASSIFIERS
142#include <ctype.h>
143#else
139 144
140static char 145static char
141xtoupper (char c) 146xtoupper (char c)
142{ 147{
143 if (c >= 'a' && c <= 'z') 148 if (c >= 'a' && c <= 'z')
163 168
164#define toupper(c) xtoupper (c) 169#define toupper(c) xtoupper (c)
165#define tolower(c) xtolower (c) 170#define tolower(c) xtolower (c)
166#define isdigit(c) xisdigit (c) 171#define isdigit(c) xisdigit (c)
167 172
173#endif
174
168#if USE_IGNORECASE 175#if USE_IGNORECASE
169static const char * 176ecb_cold static const char *
170xstrlwr (char *s) 177xstrlwr (char *s)
171{ 178{
172 const char *p = s; 179 const char *p = s;
173 180
174 while (*s) 181 while (*s)
187# define stricmp(a,b) strcmp (a, b) 194# define stricmp(a,b) strcmp (a, b)
188# define strlwr(s) (s) 195# define strlwr(s) (s)
189#endif 196#endif
190 197
191#ifndef prompt 198#ifndef prompt
192# define prompt "ts> " 199# define prompt "ms> "
193#endif 200#endif
194 201
195#ifndef InitFile 202#ifndef InitFile
196# define InitFile "init.scm" 203# define InitFile "init.scm"
197#endif 204#endif
198 205
199enum scheme_types 206enum scheme_types
200{ 207{
201 T_INTEGER, 208 T_INTEGER,
209 T_CHARACTER,
202 T_REAL, 210 T_REAL,
203 T_STRING, 211 T_STRING,
204 T_SYMBOL, 212 T_SYMBOL,
205 T_PROC, 213 T_PROC,
206 T_PAIR, /* also used for free cells */ 214 T_PAIR, /* also used for free cells */
207 T_CLOSURE, 215 T_CLOSURE,
216 T_BYTECODE, // temp
217 T_MACRO,
208 T_CONTINUATION, 218 T_CONTINUATION,
209 T_FOREIGN, 219 T_FOREIGN,
210 T_CHARACTER,
211 T_PORT, 220 T_PORT,
212 T_VECTOR, 221 T_VECTOR,
213 T_MACRO,
214 T_PROMISE, 222 T_PROMISE,
215 T_ENVIRONMENT, 223 T_ENVIRONMENT,
216 /* one more... */ 224 T_SPECIAL, // #t, #f, '(), eof-object
225
217 T_NUM_SYSTEM_TYPES 226 T_NUM_SYSTEM_TYPES
218}; 227};
219 228
220#define T_MASKTYPE 0x000f 229#define T_MASKTYPE 0x001f
221#define T_SYNTAX 0x0010 230#define T_SYNTAX 0x0020
222#define T_IMMUTABLE 0x0020 231#define T_IMMUTABLE 0x0040
223#define T_ATOM 0x0040 /* only for gc */ 232#define T_ATOM 0x0080 /* only for gc */
224#define T_MARK 0x0080 /* only for gc */ 233//#define T_MARK 0x0080 /* only for gc */
225 234
226/* num, for generic arithmetic */ 235/* num, for generic arithmetic */
227struct num 236struct num
228{ 237{
229 IVALUE ivalue; 238 IVALUE ivalue;
254static num num_op (enum num_op op, num a, num b); 263static num num_op (enum num_op op, num a, num b);
255static num num_intdiv (num a, num b); 264static num num_intdiv (num a, num b);
256static num num_rem (num a, num b); 265static num num_rem (num a, num b);
257static num num_mod (num a, num b); 266static num num_mod (num a, num b);
258 267
259#if USE_MATH
260static double round_per_R5RS (double x);
261#endif
262static int is_zero_rvalue (RVALUE x); 268static int is_zero_rvalue (RVALUE x);
263 269
264static num num_zero; 270static num num_zero;
265static num num_one; 271static num num_one;
266 272
378 384
379static pointer cadar (pointer p) { return car (cdr (car (p))); } 385static pointer cadar (pointer p) { return car (cdr (car (p))); }
380static pointer caddr (pointer p) { return car (cdr (cdr (p))); } 386static pointer caddr (pointer p) { return car (cdr (cdr (p))); }
381static pointer cdaar (pointer p) { return cdr (car (car (p))); } 387static pointer cdaar (pointer p) { return cdr (car (car (p))); }
382 388
389static pointer cadddr (pointer p) { return car (car (car (cdr (p)))); }
390
383INTERFACE void 391INTERFACE void
384set_car (pointer p, pointer q) 392set_car (pointer p, pointer q)
385{ 393{
386 CELL(p)->object.cons.car = CELL (q); 394 CELL(p)->object.cons.car = CELL (q);
387} 395}
503 511
504#define is_atom(p) (typeflag (p) & T_ATOM) 512#define is_atom(p) (typeflag (p) & T_ATOM)
505#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) 513#define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM)
506#define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) 514#define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM)
507 515
516#if 1
517#define is_mark(p) (CELL(p)->mark)
518#define setmark(p) (CELL(p)->mark = 1)
519#define clrmark(p) (CELL(p)->mark = 0)
520#else
508#define is_mark(p) (typeflag (p) & T_MARK) 521#define is_mark(p) (typeflag (p) & T_MARK)
509#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) 522#define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK)
510#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) 523#define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK)
524#endif
511 525
512INTERFACE int 526INTERFACE int
513is_immutable (pointer p) 527is_immutable (pointer p)
514{ 528{
515 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; 529 return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING;
527 proper list: length 541 proper list: length
528 circular list: -1 542 circular list: -1
529 not even a pair: -2 543 not even a pair: -2
530 dotted list: -2 minus length before dot 544 dotted list: -2 minus length before dot
531*/ 545*/
532INTERFACE int 546ecb_hot INTERFACE int
533list_length (SCHEME_P_ pointer a) 547list_length (SCHEME_P_ pointer a)
534{ 548{
535 int i = 0; 549 int i = 0;
536 pointer slow, fast; 550 pointer slow, fast;
537 551
576{ 590{
577 return list_length (SCHEME_A_ a) >= 0; 591 return list_length (SCHEME_A_ a) >= 0;
578} 592}
579 593
580#if USE_CHAR_CLASSIFIERS 594#if USE_CHAR_CLASSIFIERS
595
581ecb_inline int 596ecb_inline int
582Cisalpha (int c) 597Cisalpha (int c)
583{ 598{
584 return isascii (c) && isalpha (c); 599 return isascii (c) && isalpha (c);
585} 600}
643 "gs", 658 "gs",
644 "rs", 659 "rs",
645 "us" 660 "us"
646}; 661};
647 662
648static int 663ecb_cold static int
649is_ascii_name (const char *name, int *pc) 664is_ascii_name (const char *name, int *pc)
650{ 665{
651 int i; 666 int i;
652 667
653 for (i = 0; i < 32; i++) 668 for (i = 0; i < 32; i++)
675static int file_interactive (SCHEME_P); 690static int file_interactive (SCHEME_P);
676ecb_inline int is_one_of (const char *s, int c); 691ecb_inline int is_one_of (const char *s, int c);
677static int alloc_cellseg (SCHEME_P); 692static int alloc_cellseg (SCHEME_P);
678ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); 693ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b);
679static void finalize_cell (SCHEME_P_ pointer a); 694static void finalize_cell (SCHEME_P_ pointer a);
680static int count_consecutive_cells (pointer x, int needed);
681static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); 695static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all);
682static pointer mk_number (SCHEME_P_ const num n); 696static pointer mk_number (SCHEME_P_ const num n);
683static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); 697static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill);
684static pointer mk_vector (SCHEME_P_ uint32_t len); 698static pointer mk_vector (SCHEME_P_ uint32_t len);
685static pointer mk_atom (SCHEME_P_ char *q); 699static pointer mk_atom (SCHEME_P_ char *q);
686static pointer mk_sharp_const (SCHEME_P_ char *name); 700static pointer mk_sharp_const (SCHEME_P_ char *name);
687 701
702static pointer mk_port (SCHEME_P_ port *p);
703
688#if USE_PORTS 704#if USE_PORTS
689static pointer mk_port (SCHEME_P_ port *p);
690static pointer port_from_filename (SCHEME_P_ const char *fn, int prop); 705static pointer port_from_filename (SCHEME_P_ const char *fn, int prop);
691static pointer port_from_file (SCHEME_P_ int, int prop); 706static pointer port_from_file (SCHEME_P_ int, int prop);
692static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 707static pointer port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
693static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop); 708static port *port_rep_from_filename (SCHEME_P_ const char *fn, int prop);
694static port *port_rep_from_file (SCHEME_P_ int, int prop); 709static port *port_rep_from_file (SCHEME_P_ int, int prop);
695static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop); 710static port *port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop);
696static void port_close (SCHEME_P_ pointer p, int flag); 711static void port_close (SCHEME_P_ pointer p, int flag);
697#endif 712#endif
713
698static void mark (pointer a); 714static void mark (pointer a);
699static void gc (SCHEME_P_ pointer a, pointer b); 715static void gc (SCHEME_P_ pointer a, pointer b);
700static int basic_inchar (port *pt); 716static int basic_inchar (port *pt);
701static int inchar (SCHEME_P); 717static int inchar (SCHEME_P);
702static void backchar (SCHEME_P_ int c); 718static void backchar (SCHEME_P_ int c);
703static char *readstr_upto (SCHEME_P_ int skip, const char *delim); 719static char *readstr_upto (SCHEME_P_ int skip, const char *delim);
704static pointer readstrexp (SCHEME_P_ char delim); 720static pointer readstrexp (SCHEME_P_ char delim);
705ecb_inline int skipspace (SCHEME_P); 721static int skipspace (SCHEME_P);
706static int token (SCHEME_P); 722static int token (SCHEME_P);
707static void printslashstring (SCHEME_P_ char *s, int len); 723static void printslashstring (SCHEME_P_ char *s, int len);
708static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); 724static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen);
709static void printatom (SCHEME_P_ pointer l, int f); 725static void printatom (SCHEME_P_ pointer l, int f);
710static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); 726static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op);
876 } 892 }
877 893
878 return ret; 894 return ret;
879} 895}
880 896
881#if USE_MATH
882
883/* Round to nearest. Round to even if midway */
884static double
885round_per_R5RS (double x)
886{
887 double fl = floor (x);
888 double ce = ceil (x);
889 double dfl = x - fl;
890 double dce = ce - x;
891
892 if (dfl > dce)
893 return ce;
894 else if (dfl < dce)
895 return fl;
896 else
897 {
898 if (fmod (fl, 2) == 0) /* I imagine this holds */
899 return fl;
900 else
901 return ce;
902 }
903}
904#endif
905
906static int 897static int
907is_zero_rvalue (RVALUE x) 898is_zero_rvalue (RVALUE x)
908{ 899{
909 return x == 0; 900 return x == 0;
910#if 0 901#if 0
915#endif 906#endif
916#endif 907#endif
917} 908}
918 909
919/* allocate new cell segment */ 910/* allocate new cell segment */
920static int 911ecb_cold static int
921alloc_cellseg (SCHEME_P) 912alloc_cellseg (SCHEME_P)
922{ 913{
923 struct cell *newp; 914 struct cell *newp;
924 struct cell *last; 915 struct cell *last;
925 struct cell *p; 916 struct cell *p;
934 925
935 if (!cp && USE_ERROR_CHECKING) 926 if (!cp && USE_ERROR_CHECKING)
936 return k; 927 return k;
937 928
938 i = ++SCHEME_V->last_cell_seg; 929 i = ++SCHEME_V->last_cell_seg;
939 SCHEME_V->alloc_seg[i] = cp;
940 930
941 newp = (struct cell *)cp; 931 newp = (struct cell *)cp;
942 SCHEME_V->cell_seg[i] = newp; 932 SCHEME_V->cell_seg[i] = newp;
943 SCHEME_V->cell_segsize[i] = segsize; 933 SCHEME_V->cell_segsize[i] = segsize;
944 SCHEME_V->fcells += segsize; 934 SCHEME_V->fcells += segsize;
945 last = newp + segsize - 1; 935 last = newp + segsize - 1;
946 936
947 for (p = newp; p <= last; p++) 937 for (p = newp; p <= last; p++)
948 { 938 {
949 pointer cp = POINTER (p); 939 pointer cp = POINTER (p);
940 clrmark (cp);
950 set_typeflag (cp, T_PAIR); 941 set_typeflag (cp, T_PAIR);
951 set_car (cp, NIL); 942 set_car (cp, NIL);
952 set_cdr (cp, POINTER (p + 1)); 943 set_cdr (cp, POINTER (p + 1));
953 } 944 }
954 945
967 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 958 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
968 return S_SINK; 959 return S_SINK;
969 960
970 if (SCHEME_V->free_cell == NIL) 961 if (SCHEME_V->free_cell == NIL)
971 { 962 {
972 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; 963 const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 2;
973 964
974 gc (SCHEME_A_ a, b); 965 gc (SCHEME_A_ a, b);
975 966
976 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) 967 if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL)
977 { 968 {
996 } 987 }
997} 988}
998 989
999/* To retain recent allocs before interpreter knows about them - 990/* To retain recent allocs before interpreter knows about them -
1000 Tehom */ 991 Tehom */
1001static void 992ecb_hot static void
1002push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) 993push_recent_alloc (SCHEME_P_ pointer recent, pointer extra)
1003{ 994{
1004 pointer holder = get_cell_x (SCHEME_A_ recent, extra); 995 pointer holder = get_cell_x (SCHEME_A_ recent, extra);
1005 996
1006 set_typeflag (holder, T_PAIR); 997 set_typeflag (holder, T_PAIR);
1008 set_car (holder, recent); 999 set_car (holder, recent);
1009 set_cdr (holder, car (S_SINK)); 1000 set_cdr (holder, car (S_SINK));
1010 set_car (S_SINK, holder); 1001 set_car (S_SINK, holder);
1011} 1002}
1012 1003
1013static pointer 1004ecb_hot static pointer
1014get_cell (SCHEME_P_ pointer a, pointer b) 1005get_cell (SCHEME_P_ pointer a, pointer b)
1015{ 1006{
1016 pointer cell = get_cell_x (SCHEME_A_ a, b); 1007 pointer cell = get_cell_x (SCHEME_A_ a, b);
1017 1008
1018 /* For right now, include "a" and "b" in "cell" so that gc doesn't 1009 /* For right now, include "a" and "b" in "cell" so that gc doesn't
1056static void 1047static void
1057check_cell_alloced (pointer p, int expect_alloced) 1048check_cell_alloced (pointer p, int expect_alloced)
1058{ 1049{
1059 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ 1050 /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */
1060 if (typeflag (p) & !expect_alloced) 1051 if (typeflag (p) & !expect_alloced)
1061 xwrstr ("Cell is already allocated!\n"); 1052 putstr (SCHEME_A_ "Cell is already allocated!\n");
1062 1053
1063 if (!(typeflag (p)) & expect_alloced) 1054 if (!(typeflag (p)) & expect_alloced)
1064 xwrstr ("Cell is not allocated!\n"); 1055 putstr (SCHEME_A_ "Cell is not allocated!\n");
1065} 1056}
1066 1057
1067static void 1058static void
1068check_range_alloced (pointer p, int n, int expect_alloced) 1059check_range_alloced (pointer p, int n, int expect_alloced)
1069{ 1060{
1075#endif 1066#endif
1076 1067
1077/* Medium level cell allocation */ 1068/* Medium level cell allocation */
1078 1069
1079/* get new cons cell */ 1070/* get new cons cell */
1080pointer 1071ecb_hot static pointer
1081xcons (SCHEME_P_ pointer a, pointer b, int immutable) 1072xcons (SCHEME_P_ pointer a, pointer b)
1082{ 1073{
1083 pointer x = get_cell (SCHEME_A_ a, b); 1074 pointer x = get_cell (SCHEME_A_ a, b);
1084 1075
1085 set_typeflag (x, T_PAIR); 1076 set_typeflag (x, T_PAIR);
1086
1087 if (immutable)
1088 setimmutable (x);
1089 1077
1090 set_car (x, a); 1078 set_car (x, a);
1091 set_cdr (x, b); 1079 set_cdr (x, b);
1092 1080
1093 return x; 1081 return x;
1094} 1082}
1095 1083
1096static pointer 1084ecb_hot static pointer
1085ximmutable_cons (SCHEME_P_ pointer a, pointer b)
1086{
1087 pointer x = xcons (SCHEME_A_ a, b);
1088 setimmutable (x);
1089 return x;
1090}
1091
1092#define cons(a,b) xcons (SCHEME_A_ a, b)
1093#define immutable_cons(a,b) ximmutable_cons (SCHEME_A_ a, b)
1094
1095ecb_cold static pointer
1097generate_symbol (SCHEME_P_ const char *name) 1096generate_symbol (SCHEME_P_ const char *name)
1098{ 1097{
1099 pointer x = mk_string (SCHEME_A_ name); 1098 pointer x = mk_string (SCHEME_A_ name);
1100 setimmutable (x); 1099 setimmutable (x);
1101 set_typeflag (x, T_SYMBOL | T_ATOM); 1100 set_typeflag (x, T_SYMBOL | T_ATOM);
1107#ifndef USE_OBJECT_LIST 1106#ifndef USE_OBJECT_LIST
1108 1107
1109static int 1108static int
1110hash_fn (const char *key, int table_size) 1109hash_fn (const char *key, int table_size)
1111{ 1110{
1112 const unsigned char *p = key; 1111 const unsigned char *p = (unsigned char *)key;
1113 uint32_t hash = 2166136261; 1112 uint32_t hash = 2166136261U;
1114 1113
1115 while (*p) 1114 while (*p)
1116 hash = (hash ^ *p++) * 16777619; 1115 hash = (hash ^ *p++) * 16777619;
1117 1116
1118 return hash % table_size; 1117 return hash % table_size;
1119} 1118}
1120 1119
1121static pointer 1120ecb_cold static pointer
1122oblist_initial_value (SCHEME_P) 1121oblist_initial_value (SCHEME_P)
1123{ 1122{
1124 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ 1123 return mk_vector (SCHEME_A_ 461); /* probably should be bigger */
1125} 1124}
1126 1125
1127/* returns the new symbol */ 1126/* returns the new symbol */
1128static pointer 1127ecb_cold static pointer
1129oblist_add_by_name (SCHEME_P_ const char *name) 1128oblist_add_by_name (SCHEME_P_ const char *name)
1130{ 1129{
1131 pointer x = generate_symbol (SCHEME_A_ name); 1130 pointer x = generate_symbol (SCHEME_A_ name);
1132 int location = hash_fn (name, veclength (SCHEME_V->oblist)); 1131 int location = hash_fn (name, veclength (SCHEME_V->oblist));
1133 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); 1132 vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location)));
1134 return x; 1133 return x;
1135} 1134}
1136 1135
1137ecb_inline pointer 1136ecb_cold static pointer
1138oblist_find_by_name (SCHEME_P_ const char *name) 1137oblist_find_by_name (SCHEME_P_ const char *name)
1139{ 1138{
1140 int location; 1139 int location;
1141 pointer x; 1140 pointer x;
1142 char *s; 1141 char *s;
1153 } 1152 }
1154 1153
1155 return NIL; 1154 return NIL;
1156} 1155}
1157 1156
1158static pointer 1157ecb_cold static pointer
1159oblist_all_symbols (SCHEME_P) 1158oblist_all_symbols (SCHEME_P)
1160{ 1159{
1161 int i; 1160 int i;
1162 pointer x; 1161 pointer x;
1163 pointer ob_list = NIL; 1162 pointer ob_list = NIL;
1169 return ob_list; 1168 return ob_list;
1170} 1169}
1171 1170
1172#else 1171#else
1173 1172
1174static pointer 1173ecb_cold static pointer
1175oblist_initial_value (SCHEME_P) 1174oblist_initial_value (SCHEME_P)
1176{ 1175{
1177 return NIL; 1176 return NIL;
1178} 1177}
1179 1178
1180ecb_inline pointer 1179ecb_cold static pointer
1181oblist_find_by_name (SCHEME_P_ const char *name) 1180oblist_find_by_name (SCHEME_P_ const char *name)
1182{ 1181{
1183 pointer x; 1182 pointer x;
1184 char *s; 1183 char *s;
1185 1184
1194 1193
1195 return NIL; 1194 return NIL;
1196} 1195}
1197 1196
1198/* returns the new symbol */ 1197/* returns the new symbol */
1199static pointer 1198ecb_cold static pointer
1200oblist_add_by_name (SCHEME_P_ const char *name) 1199oblist_add_by_name (SCHEME_P_ const char *name)
1201{ 1200{
1202 pointer x = generate_symbol (SCHEME_A_ name); 1201 pointer x = generate_symbol (SCHEME_A_ name);
1203 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); 1202 SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist);
1204 return x; 1203 return x;
1205} 1204}
1206 1205
1207static pointer 1206ecb_cold static pointer
1208oblist_all_symbols (SCHEME_P) 1207oblist_all_symbols (SCHEME_P)
1209{ 1208{
1210 return SCHEME_V->oblist; 1209 return SCHEME_V->oblist;
1211} 1210}
1212 1211
1213#endif 1212#endif
1214 1213
1215#if USE_PORTS
1216static pointer 1214ecb_cold static pointer
1217mk_port (SCHEME_P_ port *p) 1215mk_port (SCHEME_P_ port *p)
1218{ 1216{
1219 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1217 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1220 1218
1221 set_typeflag (x, T_PORT | T_ATOM); 1219 set_typeflag (x, T_PORT | T_ATOM);
1222 set_port (x, p); 1220 set_port (x, p);
1223 1221
1224 return x; 1222 return x;
1225} 1223}
1226#endif
1227 1224
1228pointer 1225ecb_cold pointer
1229mk_foreign_func (SCHEME_P_ foreign_func f) 1226mk_foreign_func (SCHEME_P_ foreign_func f)
1230{ 1227{
1231 pointer x = get_cell (SCHEME_A_ NIL, NIL); 1228 pointer x = get_cell (SCHEME_A_ NIL, NIL);
1232 1229
1233 set_typeflag (x, T_FOREIGN | T_ATOM); 1230 set_typeflag (x, T_FOREIGN | T_ATOM);
1398 x = oblist_add_by_name (SCHEME_A_ name); 1395 x = oblist_add_by_name (SCHEME_A_ name);
1399 1396
1400 return x; 1397 return x;
1401} 1398}
1402 1399
1403INTERFACE pointer 1400ecb_cold INTERFACE pointer
1404gensym (SCHEME_P) 1401gensym (SCHEME_P)
1405{ 1402{
1406 pointer x; 1403 pointer x;
1407 char name[40] = "gensym-"; 1404 char name[40] = "gensym-";
1408 xnum (name + 7, ++SCHEME_V->gensym_cnt); 1405 xnum (name + 7, ++SCHEME_V->gensym_cnt);
1415{ 1412{
1416 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; 1413 return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x;
1417} 1414}
1418 1415
1419/* make symbol or number atom from string */ 1416/* make symbol or number atom from string */
1420static pointer 1417ecb_cold static pointer
1421mk_atom (SCHEME_P_ char *q) 1418mk_atom (SCHEME_P_ char *q)
1422{ 1419{
1423 char c, *p; 1420 char c, *p;
1424 int has_dec_point = 0; 1421 int has_dec_point = 0;
1425 int has_fp_exp = 0; 1422 int has_fp_exp = 0;
1496 1493
1497 return mk_integer (SCHEME_A_ strtol (q, 0, 10)); 1494 return mk_integer (SCHEME_A_ strtol (q, 0, 10));
1498} 1495}
1499 1496
1500/* make constant */ 1497/* make constant */
1501static pointer 1498ecb_cold static pointer
1502mk_sharp_const (SCHEME_P_ char *name) 1499mk_sharp_const (SCHEME_P_ char *name)
1503{ 1500{
1504 if (!strcmp (name, "t")) 1501 if (!strcmp (name, "t"))
1505 return S_T; 1502 return S_T;
1506 else if (!strcmp (name, "f")) 1503 else if (!strcmp (name, "f"))
1507 return S_F; 1504 return S_F;
1508 else if (*name == '\\') /* #\w (character) */ 1505 else if (*name == '\\') /* #\w (character) */
1509 { 1506 {
1510 int c; 1507 int c;
1511 1508
1509 // TODO: optimise
1512 if (stricmp (name + 1, "space") == 0) 1510 if (stricmp (name + 1, "space") == 0)
1513 c = ' '; 1511 c = ' ';
1514 else if (stricmp (name + 1, "newline") == 0) 1512 else if (stricmp (name + 1, "newline") == 0)
1515 c = '\n'; 1513 c = '\n';
1516 else if (stricmp (name + 1, "return") == 0) 1514 else if (stricmp (name + 1, "return") == 0)
1517 c = '\r'; 1515 c = '\r';
1518 else if (stricmp (name + 1, "tab") == 0) 1516 else if (stricmp (name + 1, "tab") == 0)
1519 c = '\t'; 1517 c = '\t';
1518 else if (stricmp (name + 1, "alarm") == 0)
1519 c = 0x07;
1520 else if (stricmp (name + 1, "backspace") == 0)
1521 c = 0x08;
1522 else if (stricmp (name + 1, "escape") == 0)
1523 c = 0x1b;
1524 else if (stricmp (name + 1, "delete") == 0)
1525 c = 0x7f;
1526 else if (stricmp (name + 1, "null") == 0)
1527 c = 0;
1520 else if (name[1] == 'x' && name[2] != 0) 1528 else if (name[1] == 'x' && name[2] != 0)
1521 { 1529 {
1522 long c1 = strtol (name + 2, 0, 16); 1530 long c1 = strtol (name + 2, 0, 16);
1523 1531
1524 if (0 <= c1 && c1 <= UCHAR_MAX) 1532 if (0 <= c1 && c1 <= UCHAR_MAX)
1549 return NIL; 1557 return NIL;
1550 } 1558 }
1551} 1559}
1552 1560
1553/* ========== garbage collector ========== */ 1561/* ========== garbage collector ========== */
1562
1563static void
1564finalize_cell (SCHEME_P_ pointer a)
1565{
1566 /* TODO, fast bitmap check? */
1567 if (is_string (a) || is_symbol (a))
1568 free (strvalue (a));
1569 else if (is_vector (a))
1570 free (vecvalue (a));
1571#if USE_PORTS
1572 else if (is_port (a))
1573 {
1574 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit)
1575 port_close (SCHEME_A_ a, port_input | port_output);
1576
1577 free (port (a));
1578 }
1579#endif
1580}
1554 1581
1555/*-- 1582/*--
1556 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, 1583 * We use algorithm E (Knuth, The Art of Computer Programming Vol.1,
1557 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, 1584 * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm,
1558 * for marking. 1585 * for marking.
1559 * 1586 *
1560 * The exception is vectors - vectors are currently marked recursively, 1587 * The exception is vectors - vectors are currently marked recursively,
1561 * which is inherited form tinyscheme and could be fixed by having another 1588 * which is inherited form tinyscheme and could be fixed by having another
1562 * word of context in the vector 1589 * word of context in the vector
1563 */ 1590 */
1564static void 1591ecb_hot static void
1565mark (pointer a) 1592mark (pointer a)
1566{ 1593{
1567 pointer t, q, p; 1594 pointer t, q, p;
1568 1595
1569 t = 0; 1596 t = 0;
1626 p = q; 1653 p = q;
1627 goto E6; 1654 goto E6;
1628 } 1655 }
1629} 1656}
1630 1657
1631/* garbage collection. parameter a, b is marked. */ 1658ecb_hot static void
1632static void 1659gc_free (SCHEME_P)
1633gc (SCHEME_P_ pointer a, pointer b)
1634{ 1660{
1635 int i; 1661 int i;
1636
1637 if (SCHEME_V->gc_verbose)
1638 putstr (SCHEME_A_ "gc...");
1639
1640 /* mark system globals */
1641 mark (SCHEME_V->oblist);
1642 mark (SCHEME_V->global_env);
1643
1644 /* mark current registers */
1645 mark (SCHEME_V->args);
1646 mark (SCHEME_V->envir);
1647 mark (SCHEME_V->code);
1648 dump_stack_mark (SCHEME_A);
1649 mark (SCHEME_V->value);
1650 mark (SCHEME_V->inport);
1651 mark (SCHEME_V->save_inport);
1652 mark (SCHEME_V->outport);
1653 mark (SCHEME_V->loadport);
1654
1655 /* Mark recent objects the interpreter doesn't know about yet. */
1656 mark (car (S_SINK));
1657 /* Mark any older stuff above nested C calls */
1658 mark (SCHEME_V->c_nest);
1659
1660#if USE_INTCACHE
1661 /* mark intcache */
1662 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1663 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1664 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1665#endif
1666
1667 /* mark variables a, b */
1668 mark (a);
1669 mark (b);
1670
1671 /* garbage collect */
1672 clrmark (NIL);
1673 SCHEME_V->fcells = 0;
1674 SCHEME_V->free_cell = NIL;
1675
1676 if (SCHEME_V->gc_verbose)
1677 xwrstr ("freeing...");
1678
1679 uint32_t total = 0; 1662 uint32_t total = 0;
1680 1663
1681 /* Here we scan the cells to build the free-list. */ 1664 /* Here we scan the cells to build the free-list. */
1682 for (i = SCHEME_V->last_cell_seg; i >= 0; i--) 1665 for (i = SCHEME_V->last_cell_seg; i >= 0; i--)
1683 { 1666 {
1708 } 1691 }
1709 } 1692 }
1710 1693
1711 if (SCHEME_V->gc_verbose) 1694 if (SCHEME_V->gc_verbose)
1712 { 1695 {
1713 xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" out of "); xwrnum (total); xwrstr (" cells were recovered.\n"); 1696 putstr (SCHEME_A_ "done: "); putnum (SCHEME_A_ SCHEME_V->fcells); putstr (SCHEME_A_ " out of "); putnum (SCHEME_A_ total); putstr (SCHEME_A_ " cells were recovered.\n");
1714 }
1715}
1716
1717static void
1718finalize_cell (SCHEME_P_ pointer a)
1719{
1720 /* TODO, fast bitmap check? */
1721 if (is_string (a) || is_symbol (a))
1722 free (strvalue (a));
1723 else if (is_vector (a))
1724 free (vecvalue (a));
1725#if USE_PORTS
1726 else if (is_port (a))
1727 { 1697 }
1728 if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) 1698}
1729 port_close (SCHEME_A_ a, port_input | port_output);
1730 1699
1731 free (port (a)); 1700/* garbage collection. parameter a, b is marked. */
1732 } 1701ecb_cold static void
1702gc (SCHEME_P_ pointer a, pointer b)
1703{
1704 int i;
1705
1706 if (SCHEME_V->gc_verbose)
1707 putstr (SCHEME_A_ "gc...");
1708
1709 /* mark system globals */
1710 mark (SCHEME_V->oblist);
1711 mark (SCHEME_V->global_env);
1712
1713 /* mark current registers */
1714 mark (SCHEME_V->args);
1715 mark (SCHEME_V->envir);
1716 mark (SCHEME_V->code);
1717 dump_stack_mark (SCHEME_A);
1718 mark (SCHEME_V->value);
1719 mark (SCHEME_V->inport);
1720 mark (SCHEME_V->save_inport);
1721 mark (SCHEME_V->outport);
1722 mark (SCHEME_V->loadport);
1723
1724 /* Mark recent objects the interpreter doesn't know about yet. */
1725 mark (car (S_SINK));
1726 /* Mark any older stuff above nested C calls */
1727 mark (SCHEME_V->c_nest);
1728
1729#if USE_INTCACHE
1730 /* mark intcache */
1731 for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i)
1732 if (SCHEME_V->intcache[i - INTCACHE_MIN])
1733 mark (SCHEME_V->intcache[i - INTCACHE_MIN]);
1733#endif 1734#endif
1735
1736 /* mark variables a, b */
1737 mark (a);
1738 mark (b);
1739
1740 /* garbage collect */
1741 clrmark (NIL);
1742 SCHEME_V->fcells = 0;
1743 SCHEME_V->free_cell = NIL;
1744
1745 if (SCHEME_V->gc_verbose)
1746 putstr (SCHEME_A_ "freeing...");
1747
1748 gc_free (SCHEME_A);
1734} 1749}
1735 1750
1736/* ========== Routines for Reading ========== */ 1751/* ========== Routines for Reading ========== */
1737 1752
1738static int 1753ecb_cold static int
1739file_push (SCHEME_P_ const char *fname) 1754file_push (SCHEME_P_ const char *fname)
1740{ 1755{
1741#if USE_PORTS
1742 int fin; 1756 int fin;
1743 1757
1744 if (SCHEME_V->file_i == MAXFIL - 1) 1758 if (SCHEME_V->file_i == MAXFIL - 1)
1745 return 0; 1759 return 0;
1746 1760
1763 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0); 1777 SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.filename = store_string (SCHEME_A_ strlen (fname), fname, 0);
1764#endif 1778#endif
1765 } 1779 }
1766 1780
1767 return fin >= 0; 1781 return fin >= 0;
1768
1769#else
1770 return 1;
1771#endif
1772} 1782}
1773 1783
1774static void 1784ecb_cold static void
1775file_pop (SCHEME_P) 1785file_pop (SCHEME_P)
1776{ 1786{
1777 if (SCHEME_V->file_i != 0) 1787 if (SCHEME_V->file_i != 0)
1778 { 1788 {
1779 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; 1789 SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i];
1783 SCHEME_V->file_i--; 1793 SCHEME_V->file_i--;
1784 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); 1794 set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i);
1785 } 1795 }
1786} 1796}
1787 1797
1788static int 1798ecb_cold static int
1789file_interactive (SCHEME_P) 1799file_interactive (SCHEME_P)
1790{ 1800{
1791#if USE_PORTS 1801#if USE_PORTS
1792 return SCHEME_V->file_i == 0 1802 return SCHEME_V->file_i == 0
1793 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO 1803 && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO
1796 return 0; 1806 return 0;
1797#endif 1807#endif
1798} 1808}
1799 1809
1800#if USE_PORTS 1810#if USE_PORTS
1801static port * 1811ecb_cold static port *
1802port_rep_from_filename (SCHEME_P_ const char *fn, int prop) 1812port_rep_from_filename (SCHEME_P_ const char *fn, int prop)
1803{ 1813{
1804 int fd; 1814 int fd;
1805 int flags; 1815 int flags;
1806 char *rw; 1816 char *rw;
1829# endif 1839# endif
1830 1840
1831 return pt; 1841 return pt;
1832} 1842}
1833 1843
1834static pointer 1844ecb_cold static pointer
1835port_from_filename (SCHEME_P_ const char *fn, int prop) 1845port_from_filename (SCHEME_P_ const char *fn, int prop)
1836{ 1846{
1837 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop); 1847 port *pt = port_rep_from_filename (SCHEME_A_ fn, prop);
1838 1848
1839 if (!pt && USE_ERROR_CHECKING) 1849 if (!pt && USE_ERROR_CHECKING)
1840 return NIL; 1850 return NIL;
1841 1851
1842 return mk_port (SCHEME_A_ pt); 1852 return mk_port (SCHEME_A_ pt);
1843} 1853}
1844 1854
1845static port * 1855ecb_cold static port *
1846port_rep_from_file (SCHEME_P_ int f, int prop) 1856port_rep_from_file (SCHEME_P_ int f, int prop)
1847{ 1857{
1848 port *pt = malloc (sizeof *pt); 1858 port *pt = malloc (sizeof *pt);
1849 1859
1850 if (!pt && USE_ERROR_CHECKING) 1860 if (!pt && USE_ERROR_CHECKING)
1855 pt->rep.stdio.file = f; 1865 pt->rep.stdio.file = f;
1856 pt->rep.stdio.closeit = 0; 1866 pt->rep.stdio.closeit = 0;
1857 return pt; 1867 return pt;
1858} 1868}
1859 1869
1860static pointer 1870ecb_cold static pointer
1861port_from_file (SCHEME_P_ int f, int prop) 1871port_from_file (SCHEME_P_ int f, int prop)
1862{ 1872{
1863 port *pt = port_rep_from_file (SCHEME_A_ f, prop); 1873 port *pt = port_rep_from_file (SCHEME_A_ f, prop);
1864 1874
1865 if (!pt && USE_ERROR_CHECKING) 1875 if (!pt && USE_ERROR_CHECKING)
1866 return NIL; 1876 return NIL;
1867 1877
1868 return mk_port (SCHEME_A_ pt); 1878 return mk_port (SCHEME_A_ pt);
1869} 1879}
1870 1880
1871static port * 1881ecb_cold static port *
1872port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1882port_rep_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1873{ 1883{
1874 port *pt = malloc (sizeof (port)); 1884 port *pt = malloc (sizeof (port));
1875 1885
1876 if (!pt && USE_ERROR_CHECKING) 1886 if (!pt && USE_ERROR_CHECKING)
1882 pt->rep.string.curr = start; 1892 pt->rep.string.curr = start;
1883 pt->rep.string.past_the_end = past_the_end; 1893 pt->rep.string.past_the_end = past_the_end;
1884 return pt; 1894 return pt;
1885} 1895}
1886 1896
1887static pointer 1897ecb_cold static pointer
1888port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop) 1898port_from_string (SCHEME_P_ char *start, char *past_the_end, int prop)
1889{ 1899{
1890 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop); 1900 port *pt = port_rep_from_string (SCHEME_A_ start, past_the_end, prop);
1891 1901
1892 if (!pt && USE_ERROR_CHECKING) 1902 if (!pt && USE_ERROR_CHECKING)
1895 return mk_port (SCHEME_A_ pt); 1905 return mk_port (SCHEME_A_ pt);
1896} 1906}
1897 1907
1898# define BLOCK_SIZE 256 1908# define BLOCK_SIZE 256
1899 1909
1900static port * 1910ecb_cold static port *
1901port_rep_from_scratch (SCHEME_P) 1911port_rep_from_scratch (SCHEME_P)
1902{ 1912{
1903 char *start; 1913 char *start;
1904 port *pt = malloc (sizeof (port)); 1914 port *pt = malloc (sizeof (port));
1905 1915
1919 pt->rep.string.curr = start; 1929 pt->rep.string.curr = start;
1920 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1; 1930 pt->rep.string.past_the_end = start + BLOCK_SIZE - 1;
1921 return pt; 1931 return pt;
1922} 1932}
1923 1933
1924static pointer 1934ecb_cold static pointer
1925port_from_scratch (SCHEME_P) 1935port_from_scratch (SCHEME_P)
1926{ 1936{
1927 port *pt = port_rep_from_scratch (SCHEME_A); 1937 port *pt = port_rep_from_scratch (SCHEME_A);
1928 1938
1929 if (!pt && USE_ERROR_CHECKING) 1939 if (!pt && USE_ERROR_CHECKING)
1930 return NIL; 1940 return NIL;
1931 1941
1932 return mk_port (SCHEME_A_ pt); 1942 return mk_port (SCHEME_A_ pt);
1933} 1943}
1934 1944
1935static void 1945ecb_cold static void
1936port_close (SCHEME_P_ pointer p, int flag) 1946port_close (SCHEME_P_ pointer p, int flag)
1937{ 1947{
1938 port *pt = port (p); 1948 port *pt = port (p);
1939 1949
1940 pt->kind &= ~flag; 1950 pt->kind &= ~flag;
1960 } 1970 }
1961} 1971}
1962#endif 1972#endif
1963 1973
1964/* get new character from input file */ 1974/* get new character from input file */
1965static int 1975ecb_cold static int
1966inchar (SCHEME_P) 1976inchar (SCHEME_P)
1967{ 1977{
1968 int c; 1978 int c;
1969 port *pt = port (SCHEME_V->inport); 1979 port *pt = port (SCHEME_V->inport);
1970 1980
1984 } 1994 }
1985 1995
1986 return c; 1996 return c;
1987} 1997}
1988 1998
1989static int ungot = -1; 1999ecb_cold static int
1990
1991static int
1992basic_inchar (port *pt) 2000basic_inchar (port *pt)
1993{ 2001{
1994#if USE_PORTS
1995 if (pt->unget != -1) 2002 if (pt->unget != -1)
1996 { 2003 {
1997 int r = pt->unget; 2004 int r = pt->unget;
1998 pt->unget = -1; 2005 pt->unget = -1;
1999 return r; 2006 return r;
2000 } 2007 }
2001 2008
2009#if USE_PORTS
2002 if (pt->kind & port_file) 2010 if (pt->kind & port_file)
2003 { 2011 {
2004 char c; 2012 char c;
2005 2013
2006 if (!read (pt->rep.stdio.file, &c, 1)) 2014 if (!read (pt->rep.stdio.file, &c, 1))
2014 return EOF; 2022 return EOF;
2015 else 2023 else
2016 return *pt->rep.string.curr++; 2024 return *pt->rep.string.curr++;
2017 } 2025 }
2018#else 2026#else
2019 if (ungot == -1)
2020 {
2021 char c; 2027 char c;
2022 if (!read (0, &c, 1)) 2028
2029 if (!read (pt->rep.stdio.file, &c, 1))
2023 return EOF; 2030 return EOF;
2024 2031
2025 ungot = c;
2026 }
2027
2028 {
2029 int r = ungot;
2030 ungot = -1;
2031 return r; 2032 return c;
2032 }
2033#endif 2033#endif
2034} 2034}
2035 2035
2036/* back character to input buffer */ 2036/* back character to input buffer */
2037static void 2037ecb_cold static void
2038backchar (SCHEME_P_ int c) 2038backchar (SCHEME_P_ int c)
2039{ 2039{
2040#if USE_PORTS 2040 port *pt = port (SCHEME_V->inport);
2041 port *pt;
2042 2041
2043 if (c == EOF) 2042 if (c == EOF)
2044 return; 2043 return;
2045 2044
2046 pt = port (SCHEME_V->inport);
2047 pt->unget = c; 2045 pt->unget = c;
2048#else
2049 if (c == EOF)
2050 return;
2051
2052 ungot = c;
2053#endif
2054} 2046}
2055 2047
2056#if USE_PORTS 2048#if USE_PORTS
2057static int 2049ecb_cold static int
2058realloc_port_string (SCHEME_P_ port *p) 2050realloc_port_string (SCHEME_P_ port *p)
2059{ 2051{
2060 char *start = p->rep.string.start; 2052 char *start = p->rep.string.start;
2061 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE; 2053 size_t new_size = p->rep.string.past_the_end - start + 1 + BLOCK_SIZE;
2062 char *str = malloc (new_size); 2054 char *str = malloc (new_size);
2075 else 2067 else
2076 return 0; 2068 return 0;
2077} 2069}
2078#endif 2070#endif
2079 2071
2080INTERFACE void 2072ecb_cold static void
2081putstr (SCHEME_P_ const char *s) 2073putchars (SCHEME_P_ const char *s, int len)
2082{ 2074{
2075 port *pt = port (SCHEME_V->outport);
2076
2083#if USE_PORTS 2077#if USE_PORTS
2084 port *pt = port (SCHEME_V->outport);
2085
2086 if (pt->kind & port_file)
2087 write (pt->rep.stdio.file, s, strlen (s));
2088 else
2089 for (; *s; s++)
2090 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2091 *pt->rep.string.curr++ = *s;
2092 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2093 *pt->rep.string.curr++ = *s;
2094
2095#else
2096 xwrstr (s);
2097#endif
2098}
2099
2100static void
2101putchars (SCHEME_P_ const char *s, int len)
2102{
2103#if USE_PORTS
2104 port *pt = port (SCHEME_V->outport);
2105
2106 if (pt->kind & port_file) 2078 if (pt->kind & port_file)
2107 write (pt->rep.stdio.file, s, len); 2079 write (pt->rep.stdio.file, s, len);
2108 else 2080 else
2109 { 2081 {
2110 for (; len; len--) 2082 for (; len; len--)
2115 *pt->rep.string.curr++ = *s++; 2087 *pt->rep.string.curr++ = *s++;
2116 } 2088 }
2117 } 2089 }
2118 2090
2119#else 2091#else
2120 write (1, s, len); 2092 write (1, s, len); // output not initialised
2121#endif 2093#endif
2094}
2095
2096INTERFACE void
2097putstr (SCHEME_P_ const char *s)
2098{
2099 putchars (SCHEME_A_ s, strlen (s));
2122} 2100}
2123 2101
2124INTERFACE void 2102INTERFACE void
2125putcharacter (SCHEME_P_ int c) 2103putcharacter (SCHEME_P_ int c)
2126{ 2104{
2127#if USE_PORTS
2128 port *pt = port (SCHEME_V->outport);
2129
2130 if (pt->kind & port_file)
2131 {
2132 char cc = c;
2133 write (pt->rep.stdio.file, &cc, 1);
2134 }
2135 else
2136 {
2137 if (pt->rep.string.curr != pt->rep.string.past_the_end)
2138 *pt->rep.string.curr++ = c;
2139 else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt))
2140 *pt->rep.string.curr++ = c;
2141 }
2142
2143#else
2144 char cc = c; 2105 char cc = c;
2145 write (1, &c, 1); 2106
2146#endif 2107 putchars (SCHEME_A_ &cc, 1);
2147} 2108}
2148 2109
2149/* read characters up to delimiter, but cater to character constants */ 2110/* read characters up to delimiter, but cater to character constants */
2150static char * 2111ecb_cold static char *
2151readstr_upto (SCHEME_P_ int skip, const char *delim) 2112readstr_upto (SCHEME_P_ int skip, const char *delim)
2152{ 2113{
2153 char *p = SCHEME_V->strbuff + skip; 2114 char *p = SCHEME_V->strbuff + skip;
2154 2115
2155 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); 2116 while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A))));
2164 2125
2165 return SCHEME_V->strbuff; 2126 return SCHEME_V->strbuff;
2166} 2127}
2167 2128
2168/* read string expression "xxx...xxx" */ 2129/* read string expression "xxx...xxx" */
2169static pointer 2130ecb_cold static pointer
2170readstrexp (SCHEME_P_ char delim) 2131readstrexp (SCHEME_P_ char delim)
2171{ 2132{
2172 char *p = SCHEME_V->strbuff; 2133 char *p = SCHEME_V->strbuff;
2173 int c; 2134 int c;
2174 int c1 = 0; 2135 int c1 = 0;
2207 case '7': 2168 case '7':
2208 state = st_oct1; 2169 state = st_oct1;
2209 c1 = c - '0'; 2170 c1 = c - '0';
2210 break; 2171 break;
2211 2172
2173 case 'a': *p++ = '\a'; state = st_ok; break;
2174 case 'n': *p++ = '\n'; state = st_ok; break;
2175 case 'r': *p++ = '\r'; state = st_ok; break;
2176 case 't': *p++ = '\t'; state = st_ok; break;
2177
2178 // this overshoots the minimum requirements of r7rs
2179 case ' ':
2180 case '\t':
2181 case '\r':
2182 case '\n':
2183 skipspace (SCHEME_A);
2184 state = st_ok;
2185 break;
2186
2187 //TODO: x should end in ;, not two-digit hex
2212 case 'x': 2188 case 'x':
2213 case 'X': 2189 case 'X':
2214 state = st_x1; 2190 state = st_x1;
2215 c1 = 0; 2191 c1 = 0;
2216 break;
2217
2218 case 'n':
2219 *p++ = '\n';
2220 state = st_ok;
2221 break;
2222
2223 case 't':
2224 *p++ = '\t';
2225 state = st_ok;
2226 break;
2227
2228 case 'r':
2229 *p++ = '\r';
2230 state = st_ok;
2231 break; 2192 break;
2232 2193
2233 default: 2194 default:
2234 *p++ = c; 2195 *p++ = c;
2235 state = st_ok; 2196 state = st_ok;
2287 } 2248 }
2288 } 2249 }
2289} 2250}
2290 2251
2291/* check c is in chars */ 2252/* check c is in chars */
2292ecb_inline int 2253ecb_cold int
2293is_one_of (const char *s, int c) 2254is_one_of (const char *s, int c)
2294{ 2255{
2295 return c == EOF || !!strchr (s, c); 2256 return c == EOF || !!strchr (s, c);
2296} 2257}
2297 2258
2298/* skip white characters */ 2259/* skip white characters */
2299ecb_inline int 2260ecb_cold int
2300skipspace (SCHEME_P) 2261skipspace (SCHEME_P)
2301{ 2262{
2302 int c, curr_line = 0; 2263 int c, curr_line = 0;
2303 2264
2304 do 2265 do
2324 backchar (SCHEME_A_ c); 2285 backchar (SCHEME_A_ c);
2325 return 1; 2286 return 1;
2326} 2287}
2327 2288
2328/* get token */ 2289/* get token */
2329static int 2290ecb_cold static int
2330token (SCHEME_P) 2291token (SCHEME_P)
2331{ 2292{
2332 int c = skipspace (SCHEME_A); 2293 int c = skipspace (SCHEME_A);
2333 2294
2334 if (c == EOF) 2295 if (c == EOF)
2432} 2393}
2433 2394
2434/* ========== Routines for Printing ========== */ 2395/* ========== Routines for Printing ========== */
2435#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL) 2396#define ok_abbrev(x) (is_pair(x) && cdr(x) == NIL)
2436 2397
2437static void 2398ecb_cold static void
2438printslashstring (SCHEME_P_ char *p, int len) 2399printslashstring (SCHEME_P_ char *p, int len)
2439{ 2400{
2440 int i; 2401 int i;
2441 unsigned char *s = (unsigned char *) p; 2402 unsigned char *s = (unsigned char *) p;
2442 2403
2498 2459
2499 putcharacter (SCHEME_A_ '"'); 2460 putcharacter (SCHEME_A_ '"');
2500} 2461}
2501 2462
2502/* print atoms */ 2463/* print atoms */
2503static void 2464ecb_cold static void
2504printatom (SCHEME_P_ pointer l, int f) 2465printatom (SCHEME_P_ pointer l, int f)
2505{ 2466{
2506 char *p; 2467 char *p;
2507 int len; 2468 int len;
2508 2469
2509 atom2str (SCHEME_A_ l, f, &p, &len); 2470 atom2str (SCHEME_A_ l, f, &p, &len);
2510 putchars (SCHEME_A_ p, len); 2471 putchars (SCHEME_A_ p, len);
2511} 2472}
2512 2473
2513/* Uses internal buffer unless string pointer is already available */ 2474/* Uses internal buffer unless string pointer is already available */
2514static void 2475ecb_cold static void
2515atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) 2476atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen)
2516{ 2477{
2517 char *p; 2478 char *p;
2518 2479
2519 if (l == NIL) 2480 if (l == NIL)
2726 return car (d); 2687 return car (d);
2727 2688
2728 p = cons (car (d), cdr (d)); 2689 p = cons (car (d), cdr (d));
2729 q = p; 2690 q = p;
2730 2691
2731 while (cdr (cdr (p)) != NIL) 2692 while (cddr (p) != NIL)
2732 { 2693 {
2733 d = cons (car (p), cdr (p)); 2694 d = cons (car (p), cdr (p));
2734 2695
2735 if (cdr (cdr (p)) != NIL) 2696 if (cddr (p) != NIL)
2736 p = cdr (d); 2697 p = cdr (d);
2737 } 2698 }
2738 2699
2739 set_cdr (p, car (cdr (p))); 2700 set_cdr (p, cadr (p));
2740 return q; 2701 return q;
2741} 2702}
2742 2703
2743/* reverse list -- produce new list */ 2704/* reverse list -- produce new list */
2744static pointer 2705ecb_hot static pointer
2745reverse (SCHEME_P_ pointer a) 2706reverse (SCHEME_P_ pointer a)
2746{ 2707{
2747 /* a must be checked by gc */ 2708 /* a must be checked by gc */
2748 pointer p = NIL; 2709 pointer p = NIL;
2749 2710
2752 2713
2753 return p; 2714 return p;
2754} 2715}
2755 2716
2756/* reverse list --- in-place */ 2717/* reverse list --- in-place */
2757static pointer 2718ecb_hot static pointer
2758reverse_in_place (SCHEME_P_ pointer term, pointer list) 2719reverse_in_place (SCHEME_P_ pointer term, pointer list)
2759{ 2720{
2760 pointer result = term; 2721 pointer result = term;
2761 pointer p = list; 2722 pointer p = list;
2762 2723
2770 2731
2771 return result; 2732 return result;
2772} 2733}
2773 2734
2774/* append list -- produce new list (in reverse order) */ 2735/* append list -- produce new list (in reverse order) */
2775static pointer 2736ecb_hot static pointer
2776revappend (SCHEME_P_ pointer a, pointer b) 2737revappend (SCHEME_P_ pointer a, pointer b)
2777{ 2738{
2778 pointer result = a; 2739 pointer result = a;
2779 pointer p = b; 2740 pointer p = b;
2780 2741
2789 2750
2790 return S_F; /* signal an error */ 2751 return S_F; /* signal an error */
2791} 2752}
2792 2753
2793/* equivalence of atoms */ 2754/* equivalence of atoms */
2794int 2755ecb_hot int
2795eqv (pointer a, pointer b) 2756eqv (pointer a, pointer b)
2796{ 2757{
2797 if (is_string (a)) 2758 if (is_string (a))
2798 { 2759 {
2799 if (is_string (b)) 2760 if (is_string (b))
2893 } 2854 }
2894 else 2855 else
2895 set_car (env, immutable_cons (slot, car (env))); 2856 set_car (env, immutable_cons (slot, car (env)));
2896} 2857}
2897 2858
2898static pointer 2859ecb_hot static pointer
2899find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2860find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2900{ 2861{
2901 pointer x, y; 2862 pointer x, y;
2902 2863
2903 for (x = env; x != NIL; x = cdr (x)) 2864 for (x = env; x != NIL; x = cdr (x))
2924 return NIL; 2885 return NIL;
2925} 2886}
2926 2887
2927#else /* USE_ALIST_ENV */ 2888#else /* USE_ALIST_ENV */
2928 2889
2929ecb_inline void 2890static void
2930new_frame_in_env (SCHEME_P_ pointer old_env) 2891new_frame_in_env (SCHEME_P_ pointer old_env)
2931{ 2892{
2932 SCHEME_V->envir = immutable_cons (NIL, old_env); 2893 SCHEME_V->envir = immutable_cons (NIL, old_env);
2933 setenvironment (SCHEME_V->envir); 2894 setenvironment (SCHEME_V->envir);
2934} 2895}
2935 2896
2936ecb_inline void 2897static void
2937new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) 2898new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value)
2938{ 2899{
2939 set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); 2900 set_car (env, immutable_cons (immutable_cons (variable, value), car (env)));
2940} 2901}
2941 2902
2942static pointer 2903ecb_hot static pointer
2943find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) 2904find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all)
2944{ 2905{
2945 pointer x, y; 2906 pointer x, y;
2946 2907
2947 for (x = env; x != NIL; x = cdr (x)) 2908 for (x = env; x != NIL; x = cdr (x))
2961 return NIL; 2922 return NIL;
2962} 2923}
2963 2924
2964#endif /* USE_ALIST_ENV else */ 2925#endif /* USE_ALIST_ENV else */
2965 2926
2966ecb_inline void 2927static void
2967new_slot_in_env (SCHEME_P_ pointer variable, pointer value) 2928new_slot_in_env (SCHEME_P_ pointer variable, pointer value)
2968{ 2929{
2969 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 2930 assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2
2970 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); 2931 new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value);
2971} 2932}
2972 2933
2973ecb_inline void 2934static void
2974set_slot_in_env (SCHEME_P_ pointer slot, pointer value) 2935set_slot_in_env (SCHEME_P_ pointer slot, pointer value)
2975{ 2936{
2976 set_cdr (slot, value); 2937 set_cdr (slot, value);
2977} 2938}
2978 2939
2979ecb_inline pointer 2940static pointer
2980slot_value_in_env (pointer slot) 2941slot_value_in_env (pointer slot)
2981{ 2942{
2982 return cdr (slot); 2943 return cdr (slot);
2983} 2944}
2984 2945
2985/* ========== Evaluation Cycle ========== */ 2946/* ========== Evaluation Cycle ========== */
2986 2947
2987static int 2948ecb_cold static int
2988xError_1 (SCHEME_P_ const char *s, pointer a) 2949xError_1 (SCHEME_P_ const char *s, pointer a)
2989{ 2950{
2990#if USE_ERROR_HOOK
2991 pointer x;
2992 pointer hdl = SCHEME_V->ERROR_HOOK;
2993#endif
2994
2995#if USE_PRINTF 2951#if USE_PRINTF
2996#if SHOW_ERROR_LINE 2952#if SHOW_ERROR_LINE
2997 char sbuf[STRBUFFSIZE]; 2953 char sbuf[STRBUFFSIZE];
2998 2954
2999 /* make sure error is not in REPL */ 2955 /* make sure error is not in REPL */
3014 } 2970 }
3015#endif 2971#endif
3016#endif 2972#endif
3017 2973
3018#if USE_ERROR_HOOK 2974#if USE_ERROR_HOOK
3019 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); 2975 pointer x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->ERROR_HOOK, 1);
3020 2976
3021 if (x != NIL) 2977 if (x != NIL)
3022 { 2978 {
3023 pointer code = a 2979 pointer code = a
3024 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) 2980 ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL)
3068 pointer code; 3024 pointer code;
3069}; 3025};
3070 3026
3071# define STACK_GROWTH 3 3027# define STACK_GROWTH 3
3072 3028
3073static void 3029ecb_hot static void
3074s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3030s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3075{ 3031{
3076 int nframes = (uintptr_t)SCHEME_V->dump; 3032 int nframes = (uintptr_t)SCHEME_V->dump;
3077 struct dump_stack_frame *next_frame; 3033 struct dump_stack_frame *next_frame;
3078 3034
3091 next_frame->code = code; 3047 next_frame->code = code;
3092 3048
3093 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3049 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3094} 3050}
3095 3051
3096static int 3052static ecb_hot int
3097xs_return (SCHEME_P_ pointer a) 3053xs_return (SCHEME_P_ pointer a)
3098{ 3054{
3099 int nframes = (uintptr_t)SCHEME_V->dump; 3055 int nframes = (uintptr_t)SCHEME_V->dump;
3100 struct dump_stack_frame *frame; 3056 struct dump_stack_frame *frame;
3101 3057
3112 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3068 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3113 3069
3114 return 0; 3070 return 0;
3115} 3071}
3116 3072
3117ecb_inline void 3073ecb_cold void
3118dump_stack_reset (SCHEME_P) 3074dump_stack_reset (SCHEME_P)
3119{ 3075{
3120 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ 3076 /* in this implementation, SCHEME_V->dump is the number of frames on the stack */
3121 SCHEME_V->dump = (pointer)+0; 3077 SCHEME_V->dump = (pointer)+0;
3122} 3078}
3123 3079
3124ecb_inline void 3080ecb_cold void
3125dump_stack_initialize (SCHEME_P) 3081dump_stack_initialize (SCHEME_P)
3126{ 3082{
3127 SCHEME_V->dump_size = 0; 3083 SCHEME_V->dump_size = 0;
3128 SCHEME_V->dump_base = 0; 3084 SCHEME_V->dump_base = 0;
3129 dump_stack_reset (SCHEME_A); 3085 dump_stack_reset (SCHEME_A);
3130} 3086}
3131 3087
3132static void 3088ecb_cold static void
3133dump_stack_free (SCHEME_P) 3089dump_stack_free (SCHEME_P)
3134{ 3090{
3135 free (SCHEME_V->dump_base); 3091 free (SCHEME_V->dump_base);
3136 SCHEME_V->dump_base = 0; 3092 SCHEME_V->dump_base = 0;
3137 SCHEME_V->dump = (pointer)0; 3093 SCHEME_V->dump = (pointer)0;
3138 SCHEME_V->dump_size = 0; 3094 SCHEME_V->dump_size = 0;
3139} 3095}
3140 3096
3141static void 3097ecb_cold static void
3142dump_stack_mark (SCHEME_P) 3098dump_stack_mark (SCHEME_P)
3143{ 3099{
3144 int nframes = (uintptr_t)SCHEME_V->dump; 3100 int nframes = (uintptr_t)SCHEME_V->dump;
3145 int i; 3101 int i;
3146 3102
3152 mark (frame->envir); 3108 mark (frame->envir);
3153 mark (frame->code); 3109 mark (frame->code);
3154 } 3110 }
3155} 3111}
3156 3112
3157static pointer 3113ecb_cold static pointer
3158ss_get_cont (SCHEME_P) 3114ss_get_cont (SCHEME_P)
3159{ 3115{
3160 int nframes = (uintptr_t)SCHEME_V->dump; 3116 int nframes = (uintptr_t)SCHEME_V->dump;
3161 int i; 3117 int i;
3162 3118
3174 } 3130 }
3175 3131
3176 return cont; 3132 return cont;
3177} 3133}
3178 3134
3179static void 3135ecb_cold static void
3180ss_set_cont (SCHEME_P_ pointer cont) 3136ss_set_cont (SCHEME_P_ pointer cont)
3181{ 3137{
3182 int i = 0; 3138 int i = 0;
3183 struct dump_stack_frame *frame = SCHEME_V->dump_base; 3139 struct dump_stack_frame *frame = SCHEME_V->dump_base;
3184 3140
3196 SCHEME_V->dump = (pointer)(uintptr_t)i; 3152 SCHEME_V->dump = (pointer)(uintptr_t)i;
3197} 3153}
3198 3154
3199#else 3155#else
3200 3156
3201ecb_inline void 3157ecb_cold void
3202dump_stack_reset (SCHEME_P) 3158dump_stack_reset (SCHEME_P)
3203{ 3159{
3204 SCHEME_V->dump = NIL; 3160 SCHEME_V->dump = NIL;
3205} 3161}
3206 3162
3207ecb_inline void 3163ecb_cold void
3208dump_stack_initialize (SCHEME_P) 3164dump_stack_initialize (SCHEME_P)
3209{ 3165{
3210 dump_stack_reset (SCHEME_A); 3166 dump_stack_reset (SCHEME_A);
3211} 3167}
3212 3168
3213static void 3169ecb_cold static void
3214dump_stack_free (SCHEME_P) 3170dump_stack_free (SCHEME_P)
3215{ 3171{
3216 SCHEME_V->dump = NIL; 3172 SCHEME_V->dump = NIL;
3217} 3173}
3218 3174
3219static int 3175ecb_hot static int
3220xs_return (SCHEME_P_ pointer a) 3176xs_return (SCHEME_P_ pointer a)
3221{ 3177{
3222 pointer dump = SCHEME_V->dump; 3178 pointer dump = SCHEME_V->dump;
3223 3179
3224 SCHEME_V->value = a; 3180 SCHEME_V->value = a;
3234 SCHEME_V->dump = dump; 3190 SCHEME_V->dump = dump;
3235 3191
3236 return 0; 3192 return 0;
3237} 3193}
3238 3194
3239static void 3195ecb_hot static void
3240s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3196s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3241{ 3197{
3242 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op), 3198 SCHEME_V->dump = cons (mk_integer (SCHEME_A_ op),
3243 cons (args, 3199 cons (args,
3244 cons (SCHEME_V->envir, 3200 cons (SCHEME_V->envir,
3245 cons (code, 3201 cons (code,
3246 SCHEME_V->dump)))); 3202 SCHEME_V->dump))));
3247} 3203}
3248 3204
3249static void 3205ecb_cold static void
3250dump_stack_mark (SCHEME_P) 3206dump_stack_mark (SCHEME_P)
3251{ 3207{
3252 mark (SCHEME_V->dump); 3208 mark (SCHEME_V->dump);
3253} 3209}
3254 3210
3255static pointer 3211ecb_cold static pointer
3256ss_get_cont (SCHEME_P) 3212ss_get_cont (SCHEME_P)
3257{ 3213{
3258 return SCHEME_V->dump; 3214 return SCHEME_V->dump;
3259} 3215}
3260 3216
3261static void 3217ecb_cold static void
3262ss_set_cont (SCHEME_P_ pointer cont) 3218ss_set_cont (SCHEME_P_ pointer cont)
3263{ 3219{
3264 SCHEME_V->dump = cont; 3220 SCHEME_V->dump = cont;
3265} 3221}
3266 3222
3267#endif 3223#endif
3268 3224
3269#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3225#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3270 3226
3271#if EXPERIMENT 3227#if EXPERIMENT
3228
3272static int 3229static int
3273debug (SCHEME_P_ int indent, pointer x) 3230dtree (SCHEME_P_ int indent, pointer x)
3274{ 3231{
3275 int c; 3232 int c;
3276 3233
3277 if (is_syntax (x)) 3234 if (is_syntax (x))
3278 { 3235 {
3296 printf ("%*sS<%s>\n", indent, "", symname (x)); 3253 printf ("%*sS<%s>\n", indent, "", symname (x));
3297 return 24+8; 3254 return 24+8;
3298 3255
3299 case T_CLOSURE: 3256 case T_CLOSURE:
3300 printf ("%*sS<%s>\n", indent, "", "closure"); 3257 printf ("%*sS<%s>\n", indent, "", "closure");
3301 debug (SCHEME_A_ indent + 3, cdr(x)); 3258 dtree (SCHEME_A_ indent + 3, cdr(x));
3302 return 32 + debug (SCHEME_A_ indent + 3, car (x)); 3259 return 32 + dtree (SCHEME_A_ indent + 3, car (x));
3303 3260
3304 case T_PAIR: 3261 case T_PAIR:
3305 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); 3262 printf ("%*spair %p %p\n", indent, "", car(x),cdr(x));
3306 c = debug (SCHEME_A_ indent + 3, car (x)); 3263 c = dtree (SCHEME_A_ indent + 3, car (x));
3307 c += debug (SCHEME_A_ indent + 3, cdr (x)); 3264 c += dtree (SCHEME_A_ indent + 3, cdr (x));
3308 return c + 1; 3265 return c + 1;
3309 3266
3310 case T_PORT: 3267 case T_PORT:
3311 printf ("%*sS<%s>\n", indent, "", "port"); 3268 printf ("%*sS<%s>\n", indent, "", "port");
3312 return 24+8; 3269 return 24+8;
3315 printf ("%*sS<%s>\n", indent, "", "vector"); 3272 printf ("%*sS<%s>\n", indent, "", "vector");
3316 return 24+8; 3273 return 24+8;
3317 3274
3318 case T_ENVIRONMENT: 3275 case T_ENVIRONMENT:
3319 printf ("%*sS<%s>\n", indent, "", "environment"); 3276 printf ("%*sS<%s>\n", indent, "", "environment");
3320 return 0 + debug (SCHEME_A_ indent + 3, car (x)); 3277 return 0 + dtree (SCHEME_A_ indent + 3, car (x));
3321 3278
3322 default: 3279 default:
3323 printf ("unhandled type %d\n", type (x)); 3280 printf ("unhandled type %d\n", type (x));
3324 break; 3281 break;
3325 } 3282 }
3326} 3283}
3327#endif
3328 3284
3285#define DUMP(t) do { printf ("DUMP %s:%d\n", __FILE__, __LINE__); dtree (SCHEME_A_ 0, (t)); } while (0)
3286
3287typedef void *stream[1];
3288
3289#define stream_init() { 0 }
3290#define stream_data(s) ((char *)(s)[0] + sizeof (uint32_t) * 2)
3291#define stream_size(s) (((uint32_t *)(s)[0])[0] - sizeof (uint32_t) * 2)
3292#define stream_free(s) free (s[0])
3293
3294ecb_cold static void
3295stream_put (stream s, uint8_t byte)
3296{
3297 uint32_t *sp = *s;
3298 uint32_t size = sizeof (uint32_t) * 2;
3299 uint32_t offs = size;
3300
3301 if (ecb_expect_true (sp))
3302 {
3303 offs = sp[0];
3304 size = sp[1];
3305 }
3306
3307 if (ecb_expect_false (offs == size))
3308 {
3309 size *= 2;
3310 sp = realloc (sp, size);
3311 *s = sp;
3312 sp[1] = size;
3313
3314 }
3315
3316 ((uint8_t *)sp)[offs++] = byte;
3317 sp[0] = offs;
3318}
3319
3320ecb_cold static void
3321stream_put_v (stream s, uint32_t v)
3322{
3323 while (v > 0x7f)
3324 {
3325 stream_put (s, v | 0x80);
3326 v >>= 7;
3327 }
3328
3329 stream_put (s, v);
3330}
3331
3332ecb_cold static void
3333stream_put_tv (stream s, int bop, uint32_t v)
3334{
3335 printf ("put tv %d %d\n", bop, v);//D
3336 stream_put (s, bop);
3337 stream_put_v (s, v);
3338}
3339
3340ecb_cold static void
3341stream_put_stream (stream s, stream o)
3342{
3343 uint32_t i;
3344
3345 for (i = 0; i < stream_size (o); ++i)
3346 stream_put (s, stream_data (o)[i]);
3347
3348 stream_free (o);
3349}
3350
3351ecb_cold static uint32_t
3352cell_id (SCHEME_P_ pointer x)
3353{
3354 struct cell *p = CELL (x);
3355 int i;
3356
3357 for (i = SCHEME_V->last_cell_seg; i >= 0; --i)
3358 if (SCHEME_V->cell_seg[i] <= p && p < SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize[i])
3359 return i | ((p - SCHEME_V->cell_seg[i]) << CELL_NSEGMENT_LOG);
3360
3361 abort ();
3362}
3363
3364// calculates a (preferably small) integer that makes it possible to find
3365// the symbol again. if pointers were offsets into a memory area... until
3366// then, we return segment number in the low bits, and offset in the high
3367// bits.
3368// also, this function must never return 0.
3369ecb_cold static uint32_t
3370symbol_id (SCHEME_P_ pointer sym)
3371{
3372 return cell_id (SCHEME_A_ sym);
3373}
3374
3375enum byteop
3376{
3377 BOP_NIL,
3378 BOP_INTEGER,
3379 BOP_SYMBOL,
3380 BOP_DATUM,
3381 BOP_LIST_BEG,
3382 BOP_LIST_END,
3383 BOP_IF,
3384 BOP_AND,
3385 BOP_OR,
3386 BOP_CASE,
3387 BOP_COND,
3388 BOP_LET,
3389 BOP_LETAST,
3390 BOP_LETREC,
3391 BOP_DEFINE,
3392 BOP_MACRO,
3393 BOP_SET,
3394 BOP_BEGIN,
3395 BOP_LAMBDA,
3396 BOP_OP,
3397};
3398
3399ecb_cold static void compile_expr (SCHEME_P_ stream s, pointer x);
3400
3401ecb_cold static void
3402compile_list (SCHEME_P_ stream s, pointer x)
3403{
3404 // TODO: improper list
3405
3406 for (; x != NIL; x = cdr (x))
3407 {
3408 stream t = stream_init ();
3409 compile_expr (SCHEME_A_ t, car (x));
3410 stream_put_v (s, stream_size (t));
3411 stream_put_stream (s, t);
3412 }
3413
3414 stream_put_v (s, 0);
3415}
3416
3417static void
3418compile_if (SCHEME_P_ stream s, pointer cond, pointer ift, pointer iff)
3419{
3420 stream sift = stream_init (); compile_expr (SCHEME_A_ sift, ift);
3421
3422 stream_put (s, BOP_IF);
3423 compile_expr (SCHEME_A_ s, cond);
3424 stream_put_v (s, stream_size (sift));
3425 stream_put_stream (s, sift);
3426 compile_expr (SCHEME_A_ s, iff);
3427}
3428
3429typedef uint32_t stream_fixup;
3430
3431static stream_fixup
3432stream_put_fixup (stream s)
3433{
3434 stream_put (s, 0);
3435 stream_put (s, 0);
3436
3437 return stream_size (s);
3438}
3439
3440static void
3441stream_fix_fixup (stream s, stream_fixup fixup, uint32_t target)
3442{
3443 target -= fixup;
3444 assert (target < (1 << 14));
3445 stream_data (s)[fixup - 2] = target | 0x80;
3446 stream_data (s)[fixup - 1] = target >> 7;
3447}
3448
3449static void
3450compile_and_or (SCHEME_P_ stream s, int and, pointer x)
3451{
3452 for (; cdr (x) != NIL; x = cdr (x))
3453 {
3454 stream t = stream_init ();
3455 compile_expr (SCHEME_A_ t, car (x));
3456 stream_put_v (s, stream_size (t));
3457 stream_put_stream (s, t);
3458 }
3459
3460 stream_put_v (s, 0);
3461}
3462
3463static void
3464compile_case (SCHEME_P_ stream s, pointer x)
3465{
3466 compile_expr (SCHEME_A_ s, caar (x));
3467
3468 for (;;)
3469 {
3470 x = cdr (x);
3471
3472 if (x == NIL)
3473 break;
3474
3475 compile_expr (SCHEME_A_ s, caar (x));
3476 stream t = stream_init (); compile_expr (SCHEME_A_ t, cdar (x));
3477 stream_put_v (s, stream_size (t));
3478 stream_put_stream (s, t);
3479 }
3480
3481 stream_put_v (s, 0);
3482}
3483
3484static void
3485compile_cond (SCHEME_P_ stream s, pointer x)
3486{
3487 for ( ; x != NIL; x = cdr (x))
3488 {
3489 compile_expr (SCHEME_A_ s, caar (x));
3490 stream t = stream_init (); compile_expr (SCHEME_A_ t, cdar (x));
3491 stream_put_v (s, stream_size (t));
3492 stream_put_stream (s, t);
3493 }
3494
3495 stream_put_v (s, 0);
3496}
3497
3329static int 3498static pointer
3499lookup (SCHEME_P_ pointer x)
3500{
3501 x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, x, 1);
3502
3503 if (x != NIL)
3504 x = slot_value_in_env (x);
3505
3506 return x;
3507}
3508
3509ecb_cold static void
3510compile_expr (SCHEME_P_ stream s, pointer x)
3511{
3512 if (x == NIL)
3513 {
3514 stream_put (s, BOP_NIL);
3515 return;
3516 }
3517
3518 if (is_pair (x))
3519 {
3520 pointer head = car (x);
3521
3522 if (is_syntax (head))
3523 {
3524 x = cdr (x);
3525
3526 switch (syntaxnum (head))
3527 {
3528 case OP_IF0: /* if */
3529 stream_put_v (s, BOP_IF);
3530 compile_if (SCHEME_A_ s, car (x), cadr (x), caddr (x));
3531 break;
3532
3533 case OP_OR0: /* or */
3534 stream_put_v (s, BOP_OR);
3535 compile_and_or (SCHEME_A_ s, 0, x);
3536 break;
3537
3538 case OP_AND0: /* and */
3539 stream_put_v (s, BOP_AND);
3540 compile_and_or (SCHEME_A_ s, 1, x);
3541 break;
3542
3543 case OP_CASE0: /* case */
3544 stream_put_v (s, BOP_CASE);
3545 compile_case (SCHEME_A_ s, x);
3546 break;
3547
3548 case OP_COND0: /* cond */
3549 stream_put_v (s, BOP_COND);
3550 compile_cond (SCHEME_A_ s, x);
3551 break;
3552
3553 case OP_LET0: /* let */
3554 case OP_LET0AST: /* let* */
3555 case OP_LET0REC: /* letrec */
3556 switch (syntaxnum (head))
3557 {
3558 case OP_LET0: stream_put (s, BOP_LET ); break;
3559 case OP_LET0AST: stream_put (s, BOP_LETAST); break;
3560 case OP_LET0REC: stream_put (s, BOP_LETREC); break;
3561 }
3562
3563 {
3564 pointer bindings = car (x);
3565 pointer body = cadr (x);
3566
3567 for (x = bindings; x != NIL; x = cdr (x))
3568 {
3569 pointer init = NIL;
3570 pointer var = car (x);
3571
3572 if (is_pair (var))
3573 {
3574 init = cdr (var);
3575 var = car (var);
3576 }
3577
3578 stream_put_v (s, symbol_id (SCHEME_A_ var));
3579 compile_expr (SCHEME_A_ s, init);
3580 }
3581
3582 stream_put_v (s, 0);
3583 compile_expr (SCHEME_A_ s, body);
3584 }
3585 break;
3586
3587 case OP_DEF0: /* define */
3588 case OP_MACRO0: /* macro */
3589 stream_put (s, syntaxnum (head) == OP_DEF0 ? BOP_DEFINE : BOP_MACRO);
3590 stream_put_v (s, cell_id (SCHEME_A_ car (x)));
3591 compile_expr (SCHEME_A_ s, cadr (x));
3592 break;
3593
3594 case OP_SET0: /* set! */
3595 stream_put (s, BOP_SET);
3596 stream_put_v (s, symbol_id (SCHEME_A_ car (x)));
3597 compile_expr (SCHEME_A_ s, cadr (x));
3598 break;
3599
3600 case OP_BEGIN: /* begin */
3601 stream_put (s, BOP_BEGIN);
3602 compile_list (SCHEME_A_ s, x);
3603 return;
3604
3605 case OP_DELAY: /* delay */
3606 abort ();
3607 break;
3608
3609 case OP_QUOTE: /* quote */
3610 stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x));
3611 break;
3612
3613 case OP_LAMBDA: /* lambda */
3614 {
3615 pointer formals = car (x);
3616 pointer body = cadr (x);
3617
3618 stream_put (s, BOP_LAMBDA);
3619
3620 for (; is_pair (formals); formals = cdr (formals))
3621 stream_put_v (s, symbol_id (SCHEME_A_ car (formals)));
3622
3623 stream_put_v (s, 0);
3624 stream_put_v (s, formals == NIL ? 0 : symbol_id (SCHEME_A_ formals));
3625
3626 compile_expr (SCHEME_A_ s, body);
3627 }
3628 break;
3629
3630 case OP_C0STREAM:/* cons-stream */
3631 abort ();
3632 break;
3633 }
3634
3635 return;
3636 }
3637
3638 pointer m = lookup (SCHEME_A_ head);
3639
3640 if (is_macro (m))
3641 {
3642 s_save (SCHEME_A_ OP_DEBUG2, SCHEME_V->args, SCHEME_V->code);
3643 SCHEME_V->code = m;
3644 SCHEME_V->args = cons (x, NIL);
3645 Eval_Cycle (SCHEME_A_ OP_APPLY);
3646 x = SCHEME_V->value;
3647 compile_expr (SCHEME_A_ s, SCHEME_V->value);
3648 return;
3649 }
3650
3651 stream_put (s, BOP_LIST_BEG);
3652
3653 for (; x != NIL; x = cdr (x))
3654 compile_expr (SCHEME_A_ s, car (x));
3655
3656 stream_put (s, BOP_LIST_END);
3657 return;
3658 }
3659
3660 switch (type (x))
3661 {
3662 case T_INTEGER:
3663 {
3664 IVALUE iv = ivalue_unchecked (x);
3665 iv = iv < 0 ? ((~(uint32_t)iv) << 1) | 1 : (uint32_t)iv << 1;
3666 stream_put_tv (s, BOP_INTEGER, iv);
3667 }
3668 return;
3669
3670 case T_SYMBOL:
3671 if (0)
3672 {
3673 // no can do without more analysis
3674 pointer m = lookup (SCHEME_A_ x);
3675
3676 if (is_proc (m))
3677 {
3678 printf ("compile proc %s %d\n", procname(m), procnum(m));
3679 stream_put_tv (s, BOP_SYMBOL, BOP_OP + procnum (m));
3680 }
3681 else
3682 stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x));
3683 }
3684
3685 stream_put_tv (s, BOP_SYMBOL, symbol_id (SCHEME_A_ x));
3686 return;
3687
3688 default:
3689 stream_put_tv (s, BOP_DATUM, cell_id (SCHEME_A_ x));
3690 break;
3691 }
3692}
3693
3694ecb_cold static int
3695compile_closure (SCHEME_P_ pointer p)
3696{
3697 stream s = stream_init ();
3698
3699 compile_list (SCHEME_A_ s, cdar (p));
3700
3701 FILE *xxd = popen ("xxd", "we");
3702 fwrite (stream_data (s), 1, stream_size (s), xxd);
3703 fclose (xxd);
3704
3705 return stream_size (s);
3706}
3707
3708#endif
3709
3710/* syntax, eval, core, ... */
3711ecb_hot static int
3330opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3712opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3331{ 3713{
3332 pointer args = SCHEME_V->args; 3714 pointer args = SCHEME_V->args;
3333 pointer x, y; 3715 pointer x, y;
3334 3716
3335 switch (op) 3717 switch (op)
3336 { 3718 {
3337#if EXPERIMENT //D 3719#if EXPERIMENT //D
3338 case OP_DEBUG: 3720 case OP_DEBUG:
3339 printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); 3721 {
3722 uint32_t len = compile_closure (SCHEME_A_ car (args));
3723 printf ("len = %d\n", len);
3340 printf ("\n"); 3724 printf ("\n");
3341 s_return (S_T); 3725 s_return (S_T);
3726 }
3727
3728 case OP_DEBUG2:
3729 return -1;
3342#endif 3730#endif
3731
3343 case OP_LOAD: /* load */ 3732 case OP_LOAD: /* load */
3344 if (file_interactive (SCHEME_A)) 3733 if (file_interactive (SCHEME_A))
3345 { 3734 {
3346 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); 3735 putstr (SCHEME_A_ "Loading ");
3347 //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); 3736 putstr (SCHEME_A_ strvalue (car (args)));
3737 putcharacter (SCHEME_A_ '\n');
3348 } 3738 }
3349 3739
3350 if (!file_push (SCHEME_A_ strvalue (car (args)))) 3740 if (!file_push (SCHEME_A_ strvalue (car (args))))
3351 Error_1 ("unable to open", car (args)); 3741 Error_1 ("unable to open", car (args));
3352 else 3742
3353 {
3354 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3743 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3355 s_goto (OP_T0LVL); 3744 s_goto (OP_T0LVL);
3356 }
3357 3745
3358 case OP_T0LVL: /* top level */ 3746 case OP_T0LVL: /* top level */
3359 3747
3360 /* If we reached the end of file, this loop is done. */ 3748 /* If we reached the end of file, this loop is done. */
3361 if (port (SCHEME_V->loadport)->kind & port_saw_EOF) 3749 if (port (SCHEME_V->loadport)->kind & port_saw_EOF)
3377 /* If interactive, be nice to user. */ 3765 /* If interactive, be nice to user. */
3378 if (file_interactive (SCHEME_A)) 3766 if (file_interactive (SCHEME_A))
3379 { 3767 {
3380 SCHEME_V->envir = SCHEME_V->global_env; 3768 SCHEME_V->envir = SCHEME_V->global_env;
3381 dump_stack_reset (SCHEME_A); 3769 dump_stack_reset (SCHEME_A);
3382 putstr (SCHEME_A_ "\n"); 3770 putcharacter (SCHEME_A_ '\n');
3771#if EXPERIMENT
3772 system ("ps v $PPID");
3773#endif
3383 putstr (SCHEME_A_ prompt); 3774 putstr (SCHEME_A_ prompt);
3384 } 3775 }
3385 3776
3386 /* Set up another iteration of REPL */ 3777 /* Set up another iteration of REPL */
3387 SCHEME_V->nesting = 0; 3778 SCHEME_V->nesting = 0;
3422 { 3813 {
3423 SCHEME_V->print_flag = 1; 3814 SCHEME_V->print_flag = 1;
3424 SCHEME_V->args = SCHEME_V->value; 3815 SCHEME_V->args = SCHEME_V->value;
3425 s_goto (OP_P0LIST); 3816 s_goto (OP_P0LIST);
3426 } 3817 }
3427 else 3818
3428 s_return (SCHEME_V->value); 3819 s_return (SCHEME_V->value);
3429 3820
3430 case OP_EVAL: /* main part of evaluation */ 3821 case OP_EVAL: /* main part of evaluation */
3431#if USE_TRACING 3822#if USE_TRACING
3432 if (SCHEME_V->tracing) 3823 if (SCHEME_V->tracing)
3433 { 3824 {
3466 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ 3857 /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */
3467 SCHEME_V->code = x; 3858 SCHEME_V->code = x;
3468 s_goto (OP_EVAL); 3859 s_goto (OP_EVAL);
3469 } 3860 }
3470 } 3861 }
3471 else 3862
3472 s_return (SCHEME_V->code); 3863 s_return (SCHEME_V->code);
3473 3864
3474 case OP_E0ARGS: /* eval arguments */ 3865 case OP_E0ARGS: /* eval arguments */
3475 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ 3866 if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */
3476 { 3867 {
3477 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); 3868 s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL);
3478 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3869 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3479 SCHEME_V->code = SCHEME_V->value; 3870 SCHEME_V->code = SCHEME_V->value;
3480 s_goto (OP_APPLY); 3871 s_goto (OP_APPLY);
3481 } 3872 }
3482 else 3873
3483 {
3484 SCHEME_V->code = cdr (SCHEME_V->code); 3874 SCHEME_V->code = cdr (SCHEME_V->code);
3485 s_goto (OP_E1ARGS); 3875 s_goto (OP_E1ARGS);
3486 }
3487 3876
3488 case OP_E1ARGS: /* eval arguments */ 3877 case OP_E1ARGS: /* eval arguments */
3489 args = cons (SCHEME_V->value, args); 3878 args = cons (SCHEME_V->value, args);
3490 3879
3491 if (is_pair (SCHEME_V->code)) /* continue */ 3880 if (is_pair (SCHEME_V->code)) /* continue */
3502 SCHEME_V->args = cdr (args); 3891 SCHEME_V->args = cdr (args);
3503 s_goto (OP_APPLY); 3892 s_goto (OP_APPLY);
3504 } 3893 }
3505 3894
3506#if USE_TRACING 3895#if USE_TRACING
3507
3508 case OP_TRACING: 3896 case OP_TRACING:
3509 { 3897 {
3510 int tr = SCHEME_V->tracing; 3898 int tr = SCHEME_V->tracing;
3511 3899
3512 SCHEME_V->tracing = ivalue_unchecked (car (args)); 3900 SCHEME_V->tracing = ivalue_unchecked (car (args));
3513 s_return (mk_integer (SCHEME_A_ tr)); 3901 s_return (mk_integer (SCHEME_A_ tr));
3514 } 3902 }
3515
3516#endif 3903#endif
3517 3904
3518 case OP_APPLY: /* apply 'code' to 'args' */ 3905 case OP_APPLY: /* apply 'code' to 'args' */
3519#if USE_TRACING 3906#if USE_TRACING
3520 if (SCHEME_V->tracing) 3907 if (SCHEME_V->tracing)
3574 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3961 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3575 { 3962 {
3576 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3963 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3577 s_return (args != NIL ? car (args) : NIL); 3964 s_return (args != NIL ? car (args) : NIL);
3578 } 3965 }
3579 else 3966
3580 Error_0 ("illegal function"); 3967 Error_0 ("illegal function");
3581 3968
3582 case OP_DOMACRO: /* do macro */ 3969 case OP_DOMACRO: /* do macro */
3583 SCHEME_V->code = SCHEME_V->value; 3970 SCHEME_V->code = SCHEME_V->value;
3584 s_goto (OP_EVAL); 3971 s_goto (OP_EVAL);
3585 3972
3649 else 4036 else
3650 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); 4037 new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value);
3651 4038
3652 s_return (SCHEME_V->code); 4039 s_return (SCHEME_V->code);
3653 4040
3654
3655 case OP_DEFP: /* defined? */ 4041 case OP_DEFP: /* defined? */
3656 x = SCHEME_V->envir; 4042 x = SCHEME_V->envir;
3657 4043
3658 if (cdr (args) != NIL) 4044 if (cdr (args) != NIL)
3659 x = cadr (args); 4045 x = cadr (args);
3677 s_return (SCHEME_V->value); 4063 s_return (SCHEME_V->value);
3678 } 4064 }
3679 else 4065 else
3680 Error_1 ("set!: unbound variable:", SCHEME_V->code); 4066 Error_1 ("set!: unbound variable:", SCHEME_V->code);
3681 4067
3682
3683 case OP_BEGIN: /* begin */ 4068 case OP_BEGIN: /* begin */
3684 if (!is_pair (SCHEME_V->code)) 4069 if (!is_pair (SCHEME_V->code))
3685 s_return (SCHEME_V->code); 4070 s_return (SCHEME_V->code);
3686 4071
3687 if (cdr (SCHEME_V->code) != NIL) 4072 if (cdr (SCHEME_V->code) != NIL)
3698 case OP_IF1: /* if */ 4083 case OP_IF1: /* if */
3699 if (is_true (SCHEME_V->value)) 4084 if (is_true (SCHEME_V->value))
3700 SCHEME_V->code = car (SCHEME_V->code); 4085 SCHEME_V->code = car (SCHEME_V->code);
3701 else 4086 else
3702 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ 4087 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
4088
3703 s_goto (OP_EVAL); 4089 s_goto (OP_EVAL);
3704 4090
3705 case OP_LET0: /* let */ 4091 case OP_LET0: /* let */
3706 SCHEME_V->args = NIL; 4092 SCHEME_V->args = NIL;
3707 SCHEME_V->value = SCHEME_V->code; 4093 SCHEME_V->value = SCHEME_V->code;
3708 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); 4094 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code);
3709 s_goto (OP_LET1); 4095 s_goto (OP_LET1);
3710 4096
3711 case OP_LET1: /* let (calculate parameters) */ 4097 case OP_LET1: /* let (calculate parameters) */
4098 case OP_LET1REC: /* letrec (calculate parameters) */
3712 args = cons (SCHEME_V->value, args); 4099 args = cons (SCHEME_V->value, args);
3713 4100
3714 if (is_pair (SCHEME_V->code)) /* continue */ 4101 if (is_pair (SCHEME_V->code)) /* continue */
3715 { 4102 {
3716 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 4103 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3717 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); 4104 Error_1 ("Bad syntax of binding spec in let/letrec:", car (SCHEME_V->code));
3718 4105
3719 s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); 4106 s_save (SCHEME_A_ op, args, cdr (SCHEME_V->code));
3720 SCHEME_V->code = cadar (SCHEME_V->code); 4107 SCHEME_V->code = cadar (SCHEME_V->code);
3721 SCHEME_V->args = NIL; 4108 SCHEME_V->args = NIL;
3722 s_goto (OP_EVAL); 4109 s_goto (OP_EVAL);
3723 } 4110 }
3724 else /* end */ 4111
3725 { 4112 /* end */
3726 args = reverse_in_place (SCHEME_A_ NIL, args); 4113 args = reverse_in_place (SCHEME_A_ NIL, args);
3727 SCHEME_V->code = car (args); 4114 SCHEME_V->code = car (args);
3728 SCHEME_V->args = cdr (args); 4115 SCHEME_V->args = cdr (args);
3729 s_goto (OP_LET2); 4116 s_goto (op == OP_LET1 ? OP_LET2 : OP_LET2REC);
3730 }
3731 4117
3732 case OP_LET2: /* let */ 4118 case OP_LET2: /* let */
3733 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4119 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3734 4120
3735 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; 4121 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args;
3739 if (is_symbol (car (SCHEME_V->code))) /* named let */ 4125 if (is_symbol (car (SCHEME_V->code))) /* named let */
3740 { 4126 {
3741 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) 4127 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3742 { 4128 {
3743 if (!is_pair (x)) 4129 if (!is_pair (x))
3744 Error_1 ("Bad syntax of binding in let :", x); 4130 Error_1 ("Bad syntax of binding in let:", x);
3745 4131
3746 if (!is_list (SCHEME_A_ car (x))) 4132 if (!is_list (SCHEME_A_ car (x)))
3747 Error_1 ("Bad syntax of binding in let :", car (x)); 4133 Error_1 ("Bad syntax of binding in let:", car (x));
3748 4134
3749 args = cons (caar (x), args); 4135 args = cons (caar (x), args);
3750 } 4136 }
3751 4137
3752 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)), 4138 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)),
3769 SCHEME_V->code = cdr (SCHEME_V->code); 4155 SCHEME_V->code = cdr (SCHEME_V->code);
3770 s_goto (OP_BEGIN); 4156 s_goto (OP_BEGIN);
3771 } 4157 }
3772 4158
3773 if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code))) 4159 if (!is_pair (car (SCHEME_V->code)) || !is_pair (caar (SCHEME_V->code)) || !is_pair (cdaar (SCHEME_V->code)))
3774 Error_1 ("Bad syntax of binding spec in let* :", car (SCHEME_V->code)); 4160 Error_1 ("Bad syntax of binding spec in let*:", car (SCHEME_V->code));
3775 4161
3776 s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code)); 4162 s_save (SCHEME_A_ OP_LET1AST, cdr (SCHEME_V->code), car (SCHEME_V->code));
3777 SCHEME_V->code = car (cdaar (SCHEME_V->code)); 4163 SCHEME_V->code = car (cdaar (SCHEME_V->code));
3778 s_goto (OP_EVAL); 4164 s_goto (OP_EVAL);
3779 4165
3790 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); 4176 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3791 SCHEME_V->code = cadar (SCHEME_V->code); 4177 SCHEME_V->code = cadar (SCHEME_V->code);
3792 SCHEME_V->args = NIL; 4178 SCHEME_V->args = NIL;
3793 s_goto (OP_EVAL); 4179 s_goto (OP_EVAL);
3794 } 4180 }
3795 else /* end */ 4181
4182 /* end */
3796 { 4183
3797 SCHEME_V->code = args; 4184 SCHEME_V->code = args;
3798 SCHEME_V->args = NIL; 4185 SCHEME_V->args = NIL;
3799 s_goto (OP_BEGIN); 4186 s_goto (OP_BEGIN);
3800 }
3801 4187
3802 case OP_LET0REC: /* letrec */ 4188 case OP_LET0REC: /* letrec */
3803 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 4189 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3804 SCHEME_V->args = NIL; 4190 SCHEME_V->args = NIL;
3805 SCHEME_V->value = SCHEME_V->code; 4191 SCHEME_V->value = SCHEME_V->code;
3806 SCHEME_V->code = car (SCHEME_V->code); 4192 SCHEME_V->code = car (SCHEME_V->code);
3807 s_goto (OP_LET1REC); 4193 s_goto (OP_LET1REC);
3808 4194
3809 case OP_LET1REC: /* letrec (calculate parameters) */ 4195 /* OP_LET1REC handled by OP_LET1 */
3810 args = cons (SCHEME_V->value, args);
3811
3812 if (is_pair (SCHEME_V->code)) /* continue */
3813 {
3814 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3815 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3816
3817 s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code));
3818 SCHEME_V->code = cadar (SCHEME_V->code);
3819 SCHEME_V->args = NIL;
3820 s_goto (OP_EVAL);
3821 }
3822 else /* end */
3823 {
3824 args = reverse_in_place (SCHEME_A_ NIL, args);
3825 SCHEME_V->code = car (args);
3826 SCHEME_V->args = cdr (args);
3827 s_goto (OP_LET2REC);
3828 }
3829 4196
3830 case OP_LET2REC: /* letrec */ 4197 case OP_LET2REC: /* letrec */
3831 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y)) 4198 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y))
3832 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 4199 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3833 4200
3863 } 4230 }
3864 else 4231 else
3865 { 4232 {
3866 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) 4233 if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL)
3867 s_return (NIL); 4234 s_return (NIL);
3868 else 4235
3869 {
3870 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); 4236 s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code);
3871 SCHEME_V->code = caar (SCHEME_V->code); 4237 SCHEME_V->code = caar (SCHEME_V->code);
3872 s_goto (OP_EVAL); 4238 s_goto (OP_EVAL);
3873 }
3874 } 4239 }
3875 4240
3876 case OP_DELAY: /* delay */ 4241 case OP_DELAY: /* delay */
3877 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); 4242 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
3878 set_typeflag (x, T_PROMISE); 4243 set_typeflag (x, T_PROMISE);
3889 case OP_AND1: /* and */ 4254 case OP_AND1: /* and */
3890 if (is_false (SCHEME_V->value)) 4255 if (is_false (SCHEME_V->value))
3891 s_return (SCHEME_V->value); 4256 s_return (SCHEME_V->value);
3892 else if (SCHEME_V->code == NIL) 4257 else if (SCHEME_V->code == NIL)
3893 s_return (SCHEME_V->value); 4258 s_return (SCHEME_V->value);
3894 else 4259
3895 {
3896 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); 4260 s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code));
3897 SCHEME_V->code = car (SCHEME_V->code); 4261 SCHEME_V->code = car (SCHEME_V->code);
3898 s_goto (OP_EVAL); 4262 s_goto (OP_EVAL);
3899 }
3900 4263
3901 case OP_OR0: /* or */ 4264 case OP_OR0: /* or */
3902 if (SCHEME_V->code == NIL) 4265 if (SCHEME_V->code == NIL)
3903 s_return (S_F); 4266 s_return (S_F);
3904 4267
3909 case OP_OR1: /* or */ 4272 case OP_OR1: /* or */
3910 if (is_true (SCHEME_V->value)) 4273 if (is_true (SCHEME_V->value))
3911 s_return (SCHEME_V->value); 4274 s_return (SCHEME_V->value);
3912 else if (SCHEME_V->code == NIL) 4275 else if (SCHEME_V->code == NIL)
3913 s_return (SCHEME_V->value); 4276 s_return (SCHEME_V->value);
3914 else 4277
3915 {
3916 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); 4278 s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code));
3917 SCHEME_V->code = car (SCHEME_V->code); 4279 SCHEME_V->code = car (SCHEME_V->code);
3918 s_goto (OP_EVAL); 4280 s_goto (OP_EVAL);
3919 }
3920 4281
3921 case OP_C0STREAM: /* cons-stream */ 4282 case OP_C0STREAM: /* cons-stream */
3922 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 4283 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3923 SCHEME_V->code = car (SCHEME_V->code); 4284 SCHEME_V->code = car (SCHEME_V->code);
3924 s_goto (OP_EVAL); 4285 s_goto (OP_EVAL);
3989 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x)); 4350 s_save (SCHEME_A_ OP_CASE2, NIL, cdar (x));
3990 SCHEME_V->code = caar (x); 4351 SCHEME_V->code = caar (x);
3991 s_goto (OP_EVAL); 4352 s_goto (OP_EVAL);
3992 } 4353 }
3993 } 4354 }
3994 else 4355
3995 s_return (NIL); 4356 s_return (NIL);
3996 4357
3997 case OP_CASE2: /* case */ 4358 case OP_CASE2: /* case */
3998 if (is_true (SCHEME_V->value)) 4359 if (is_true (SCHEME_V->value))
3999 s_goto (OP_BEGIN); 4360 s_goto (OP_BEGIN);
4000 else 4361
4001 s_return (NIL); 4362 s_return (NIL);
4002 4363
4003 case OP_PAPPLY: /* apply */ 4364 case OP_PAPPLY: /* apply */
4004 SCHEME_V->code = car (args); 4365 SCHEME_V->code = car (args);
4005 SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); 4366 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
4006 /*SCHEME_V->args = cadr(args); */ 4367 /*SCHEME_V->args = cadr(args); */
4020 } 4381 }
4021 4382
4022 if (USE_ERROR_CHECKING) abort (); 4383 if (USE_ERROR_CHECKING) abort ();
4023} 4384}
4024 4385
4025static int 4386/* math, cxr */
4387ecb_hot static int
4026opexe_1 (SCHEME_P_ enum scheme_opcodes op) 4388opexe_1 (SCHEME_P_ enum scheme_opcodes op)
4027{ 4389{
4028 pointer args = SCHEME_V->args; 4390 pointer args = SCHEME_V->args;
4029 pointer x = car (args); 4391 pointer x = car (args);
4030 num v; 4392 num v;
4031 4393
4032 switch (op) 4394 switch (op)
4033 { 4395 {
4034#if USE_MATH 4396#if USE_MATH
4035 case OP_INEX2EX: /* inexact->exact */ 4397 case OP_INEX2EX: /* inexact->exact */
4036 {
4037 if (is_integer (x)) 4398 if (!is_integer (x))
4038 s_return (x); 4399 {
4039
4040 RVALUE r = rvalue_unchecked (x); 4400 RVALUE r = rvalue_unchecked (x);
4041 4401
4042 if (r == (RVALUE)(IVALUE)r) 4402 if (r == (RVALUE)(IVALUE)r)
4043 s_return (mk_integer (SCHEME_A_ rvalue_unchecked (x))); 4403 x = mk_integer (SCHEME_A_ rvalue_unchecked (x));
4044 else 4404 else
4045 Error_1 ("inexact->exact: not integral:", x); 4405 Error_1 ("inexact->exact: not integral:", x);
4046 } 4406 }
4047 4407
4408 s_return (x);
4409
4410 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4411 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4412 case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x))));
4413 case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x))));
4414
4415 case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4048 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 4416 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4049 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 4417 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))
4418 / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args))))));
4050 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 4419 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
4051 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 4420 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
4052 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); 4421 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x))));
4053 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); 4422 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x))));
4054 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); 4423 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4055 4424
4056 case OP_ATAN: 4425 case OP_ATAN:
4426 s_return (mk_real (SCHEME_A_
4057 if (cdr (args) == NIL) 4427 cdr (args) == NIL
4058 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 4428 ? atan (rvalue (x))
4059 else 4429 : atan2 (rvalue (x), rvalue (cadr (args)))));
4060 {
4061 pointer y = cadr (args);
4062 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4063 }
4064
4065 case OP_SQRT:
4066 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4067 4430
4068 case OP_EXPT: 4431 case OP_EXPT:
4069 { 4432 {
4070 RVALUE result; 4433 RVALUE result;
4071 int real_result = 1; 4434 int real_result = 1;
4094 if (real_result) 4457 if (real_result)
4095 s_return (mk_real (SCHEME_A_ result)); 4458 s_return (mk_real (SCHEME_A_ result));
4096 else 4459 else
4097 s_return (mk_integer (SCHEME_A_ result)); 4460 s_return (mk_integer (SCHEME_A_ result));
4098 } 4461 }
4099
4100 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4101 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4102
4103 case OP_TRUNCATE:
4104 {
4105 RVALUE n = rvalue (x);
4106 s_return (mk_real (SCHEME_A_ n > 0 ? floor (n) : ceil (n)));
4107 }
4108
4109 case OP_ROUND:
4110 if (is_integer (x))
4111 s_return (x);
4112
4113 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4114#endif 4462#endif
4115 4463
4116 case OP_ADD: /* + */ 4464 case OP_ADD: /* + */
4117 v = num_zero; 4465 v = num_zero;
4118 4466
4420 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4768 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4421 4769
4422 s_return (newstr); 4770 s_return (newstr);
4423 } 4771 }
4424 4772
4425 case OP_SUBSTR: /* substring */ 4773 case OP_STRING_COPY: /* substring/string-copy */
4426 { 4774 {
4427 char *str = strvalue (x); 4775 char *str = strvalue (x);
4428 int index0 = ivalue_unchecked (cadr (args)); 4776 int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args));
4429 int index1; 4777 int index1;
4430 int len; 4778 int len;
4431 4779
4432 if (index0 > strlength (x)) 4780 if (index0 > strlength (x))
4433 Error_1 ("substring: start out of bounds:", cadr (args)); 4781 Error_1 ("string->copy: start out of bounds:", cadr (args));
4434 4782
4435 if (cddr (args) != NIL) 4783 if (cddr (args) != NIL)
4436 { 4784 {
4437 index1 = ivalue_unchecked (caddr (args)); 4785 index1 = ivalue_unchecked (caddr (args));
4438 4786
4439 if (index1 > strlength (x) || index1 < index0) 4787 if (index1 > strlength (x) || index1 < index0)
4440 Error_1 ("substring: end out of bounds:", caddr (args)); 4788 Error_1 ("string->copy: end out of bounds:", caddr (args));
4441 } 4789 }
4442 else 4790 else
4443 index1 = strlength (x); 4791 index1 = strlength (x);
4444 4792
4445 len = index1 - index0; 4793 len = index1 - index0;
4446 x = mk_empty_string (SCHEME_A_ len, ' '); 4794 x = mk_counted_string (SCHEME_A_ str + index0, len);
4447 memcpy (strvalue (x), str + index0, len);
4448 strvalue (x)[len] = 0;
4449 4795
4450 s_return (x); 4796 s_return (x);
4451 } 4797 }
4452 4798
4453 case OP_VECTOR: /* vector */ 4799 case OP_VECTOR: /* vector */
4527 } 4873 }
4528 4874
4529 if (USE_ERROR_CHECKING) abort (); 4875 if (USE_ERROR_CHECKING) abort ();
4530} 4876}
4531 4877
4532static int 4878/* relational ops */
4879ecb_hot static int
4533opexe_2 (SCHEME_P_ enum scheme_opcodes op) 4880opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4534{ 4881{
4535 pointer x = SCHEME_V->args; 4882 pointer x = SCHEME_V->args;
4536 4883
4537 for (;;) 4884 for (;;)
4558 } 4905 }
4559 4906
4560 s_return (S_T); 4907 s_return (S_T);
4561} 4908}
4562 4909
4563static int 4910/* predicates */
4911ecb_hot static int
4564opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4912opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4565{ 4913{
4566 pointer args = SCHEME_V->args; 4914 pointer args = SCHEME_V->args;
4567 pointer a = car (args); 4915 pointer a = car (args);
4568 pointer d = cdr (args); 4916 pointer d = cdr (args);
4615 } 4963 }
4616 4964
4617 s_retbool (r); 4965 s_retbool (r);
4618} 4966}
4619 4967
4620static int 4968/* promises, list ops, ports */
4969ecb_hot static int
4621opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4970opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4622{ 4971{
4623 pointer args = SCHEME_V->args; 4972 pointer args = SCHEME_V->args;
4624 pointer a = car (args); 4973 pointer a = car (args);
4625 pointer x, y; 4974 pointer x, y;
4642 case OP_SAVE_FORCED: /* Save forced value replacing promise */ 4991 case OP_SAVE_FORCED: /* Save forced value replacing promise */
4643 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); 4992 *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value);
4644 s_return (SCHEME_V->value); 4993 s_return (SCHEME_V->value);
4645 4994
4646#if USE_PORTS 4995#if USE_PORTS
4996
4997 case OP_EOF_OBJECT: /* eof-object */
4998 s_return (S_EOF);
4647 4999
4648 case OP_WRITE: /* write */ 5000 case OP_WRITE: /* write */
4649 case OP_DISPLAY: /* display */ 5001 case OP_DISPLAY: /* display */
4650 case OP_WRITE_CHAR: /* write-char */ 5002 case OP_WRITE_CHAR: /* write-char */
4651 if (is_pair (cdr (SCHEME_V->args))) 5003 if (is_pair (cdr (SCHEME_V->args)))
4665 else 5017 else
4666 SCHEME_V->print_flag = 0; 5018 SCHEME_V->print_flag = 0;
4667 5019
4668 s_goto (OP_P0LIST); 5020 s_goto (OP_P0LIST);
4669 5021
5022 //TODO: move to scheme
4670 case OP_NEWLINE: /* newline */ 5023 case OP_NEWLINE: /* newline */
4671 if (is_pair (args)) 5024 if (is_pair (args))
4672 { 5025 {
4673 if (a != SCHEME_V->outport) 5026 if (a != SCHEME_V->outport)
4674 { 5027 {
4676 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 5029 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4677 SCHEME_V->outport = a; 5030 SCHEME_V->outport = a;
4678 } 5031 }
4679 } 5032 }
4680 5033
4681 putstr (SCHEME_A_ "\n"); 5034 putcharacter (SCHEME_A_ '\n');
4682 s_return (S_T); 5035 s_return (S_T);
4683#endif 5036#endif
4684 5037
4685 case OP_ERR0: /* error */ 5038 case OP_ERR0: /* error */
4686 SCHEME_V->retcode = -1; 5039 SCHEME_V->retcode = -1;
4695 putstr (SCHEME_A_ strvalue (car (args))); 5048 putstr (SCHEME_A_ strvalue (car (args)));
4696 SCHEME_V->args = cdr (args); 5049 SCHEME_V->args = cdr (args);
4697 s_goto (OP_ERR1); 5050 s_goto (OP_ERR1);
4698 5051
4699 case OP_ERR1: /* error */ 5052 case OP_ERR1: /* error */
4700 putstr (SCHEME_A_ " "); 5053 putcharacter (SCHEME_A_ ' ');
4701 5054
4702 if (args != NIL) 5055 if (args != NIL)
4703 { 5056 {
4704 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); 5057 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4705 SCHEME_V->args = a; 5058 SCHEME_V->args = a;
4706 SCHEME_V->print_flag = 1; 5059 SCHEME_V->print_flag = 1;
4707 s_goto (OP_P0LIST); 5060 s_goto (OP_P0LIST);
4708 } 5061 }
4709 else 5062 else
4710 { 5063 {
4711 putstr (SCHEME_A_ "\n"); 5064 putcharacter (SCHEME_A_ '\n');
4712 5065
4713 if (SCHEME_V->interactive_repl) 5066 if (SCHEME_V->interactive_repl)
4714 s_goto (OP_T0LVL); 5067 s_goto (OP_T0LVL);
4715 else 5068 else
4716 return -1; 5069 return -1;
4924 } 5277 }
4925 5278
4926 if (USE_ERROR_CHECKING) abort (); 5279 if (USE_ERROR_CHECKING) abort ();
4927} 5280}
4928 5281
4929static int 5282/* reading */
5283ecb_cold static int
4930opexe_5 (SCHEME_P_ enum scheme_opcodes op) 5284opexe_5 (SCHEME_P_ enum scheme_opcodes op)
4931{ 5285{
4932 pointer args = SCHEME_V->args; 5286 pointer args = SCHEME_V->args;
4933 pointer x; 5287 pointer x;
4934 5288
5013 case OP_RDSEXPR: 5367 case OP_RDSEXPR:
5014 switch (SCHEME_V->tok) 5368 switch (SCHEME_V->tok)
5015 { 5369 {
5016 case TOK_EOF: 5370 case TOK_EOF:
5017 s_return (S_EOF); 5371 s_return (S_EOF);
5018 /* NOTREACHED */
5019 5372
5020 case TOK_VEC: 5373 case TOK_VEC:
5021 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); 5374 s_save (SCHEME_A_ OP_RDVEC, NIL, NIL);
5022 /* fall through */ 5375 /* fall through */
5023 5376
5026 5379
5027 if (SCHEME_V->tok == TOK_RPAREN) 5380 if (SCHEME_V->tok == TOK_RPAREN)
5028 s_return (NIL); 5381 s_return (NIL);
5029 else if (SCHEME_V->tok == TOK_DOT) 5382 else if (SCHEME_V->tok == TOK_DOT)
5030 Error_0 ("syntax error: illegal dot expression"); 5383 Error_0 ("syntax error: illegal dot expression");
5031 else 5384
5032 {
5033 SCHEME_V->nesting_stack[SCHEME_V->file_i]++; 5385 SCHEME_V->nesting_stack[SCHEME_V->file_i]++;
5034 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL); 5386 s_save (SCHEME_A_ OP_RDLIST, NIL, NIL);
5035 s_goto (OP_RDSEXPR); 5387 s_goto (OP_RDSEXPR);
5036 }
5037 5388
5038 case TOK_QUOTE: 5389 case TOK_QUOTE:
5039 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL); 5390 s_save (SCHEME_A_ OP_RDQUOTE, NIL, NIL);
5040 SCHEME_V->tok = token (SCHEME_A); 5391 SCHEME_V->tok = token (SCHEME_A);
5041 s_goto (OP_RDSEXPR); 5392 s_goto (OP_RDSEXPR);
5047 { 5398 {
5048 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); 5399 s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL);
5049 SCHEME_V->tok = TOK_LPAREN; 5400 SCHEME_V->tok = TOK_LPAREN;
5050 s_goto (OP_RDSEXPR); 5401 s_goto (OP_RDSEXPR);
5051 } 5402 }
5052 else 5403
5053 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); 5404 s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL);
5054
5055 s_goto (OP_RDSEXPR); 5405 s_goto (OP_RDSEXPR);
5056 5406
5057 case TOK_COMMA: 5407 case TOK_COMMA:
5058 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); 5408 s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL);
5059 SCHEME_V->tok = token (SCHEME_A); 5409 SCHEME_V->tok = token (SCHEME_A);
5070 case TOK_DOTATOM: 5420 case TOK_DOTATOM:
5071 SCHEME_V->strbuff[0] = '.'; 5421 SCHEME_V->strbuff[0] = '.';
5072 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); 5422 s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS)));
5073 5423
5074 case TOK_STRATOM: 5424 case TOK_STRATOM:
5425 //TODO: haven't checked whether the garbage collector could interfere and free x
5426 gc (SCHEME_A_ NIL, NIL); //TODO: superheavyhanded
5075 x = readstrexp (SCHEME_A_ '|'); 5427 x = readstrexp (SCHEME_A_ '|');
5076 //TODO: haven't checked whether the garbage collector could interfere
5077 s_return (mk_atom (SCHEME_A_ strvalue (x))); 5428 s_return (mk_atom (SCHEME_A_ strvalue (x)));
5078 5429
5079 case TOK_DQUOTE: 5430 case TOK_DQUOTE:
5080 x = readstrexp (SCHEME_A_ '"'); 5431 x = readstrexp (SCHEME_A_ '"');
5081 5432
5089 { 5440 {
5090 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1); 5441 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->SHARP_HOOK, 1);
5091 5442
5092 if (f == NIL) 5443 if (f == NIL)
5093 Error_0 ("undefined sharp expression"); 5444 Error_0 ("undefined sharp expression");
5094 else 5445
5095 {
5096 SCHEME_V->code = cons (slot_value_in_env (f), NIL); 5446 SCHEME_V->code = cons (slot_value_in_env (f), NIL);
5097 s_goto (OP_EVAL); 5447 s_goto (OP_EVAL);
5098 }
5099 } 5448 }
5100 5449
5101 case TOK_SHARP_CONST: 5450 case TOK_SHARP_CONST:
5102 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) 5451 if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL)
5103 Error_0 ("undefined sharp expression"); 5452 Error_0 ("undefined sharp expression");
5104 else 5453
5105 s_return (x); 5454 s_return (x);
5106 5455
5107 default: 5456 default:
5108 Error_0 ("syntax error: illegal token"); 5457 Error_0 ("syntax error: illegal token");
5109 } 5458 }
5110 5459
5203 pointer b = cdr (args); 5552 pointer b = cdr (args);
5204 int ok_abbr = ok_abbrev (b); 5553 int ok_abbr = ok_abbrev (b);
5205 SCHEME_V->args = car (b); 5554 SCHEME_V->args = car (b);
5206 5555
5207 if (a == SCHEME_V->QUOTE && ok_abbr) 5556 if (a == SCHEME_V->QUOTE && ok_abbr)
5208 putstr (SCHEME_A_ "'"); 5557 putcharacter (SCHEME_A_ '\'');
5209 else if (a == SCHEME_V->QQUOTE && ok_abbr) 5558 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5210 putstr (SCHEME_A_ "`"); 5559 putcharacter (SCHEME_A_ '`');
5211 else if (a == SCHEME_V->UNQUOTE && ok_abbr) 5560 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5212 putstr (SCHEME_A_ ","); 5561 putcharacter (SCHEME_A_ ',');
5213 else if (a == SCHEME_V->UNQUOTESP && ok_abbr) 5562 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5214 putstr (SCHEME_A_ ",@"); 5563 putstr (SCHEME_A_ ",@");
5215 else 5564 else
5216 { 5565 {
5217 putstr (SCHEME_A_ "("); 5566 putcharacter (SCHEME_A_ '(');
5218 s_save (SCHEME_A_ OP_P1LIST, b, NIL); 5567 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5219 SCHEME_V->args = a; 5568 SCHEME_V->args = a;
5220 } 5569 }
5221 5570
5222 s_goto (OP_P0LIST); 5571 s_goto (OP_P0LIST);
5224 5573
5225 case OP_P1LIST: 5574 case OP_P1LIST:
5226 if (is_pair (args)) 5575 if (is_pair (args))
5227 { 5576 {
5228 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); 5577 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5229 putstr (SCHEME_A_ " "); 5578 putcharacter (SCHEME_A_ ' ');
5230 SCHEME_V->args = car (args); 5579 SCHEME_V->args = car (args);
5231 s_goto (OP_P0LIST); 5580 s_goto (OP_P0LIST);
5232 } 5581 }
5233 else if (is_vector (args)) 5582 else if (is_vector (args))
5234 { 5583 {
5242 { 5591 {
5243 putstr (SCHEME_A_ " . "); 5592 putstr (SCHEME_A_ " . ");
5244 printatom (SCHEME_A_ args, SCHEME_V->print_flag); 5593 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5245 } 5594 }
5246 5595
5247 putstr (SCHEME_A_ ")"); 5596 putcharacter (SCHEME_A_ ')');
5248 s_return (S_T); 5597 s_return (S_T);
5249 } 5598 }
5250 5599
5251 case OP_PVECFROM: 5600 case OP_PVECFROM:
5252 { 5601 {
5254 pointer vec = car (args); 5603 pointer vec = car (args);
5255 int len = veclength (vec); 5604 int len = veclength (vec);
5256 5605
5257 if (i == len) 5606 if (i == len)
5258 { 5607 {
5259 putstr (SCHEME_A_ ")"); 5608 putcharacter (SCHEME_A_ ')');
5260 s_return (S_T); 5609 s_return (S_T);
5261 } 5610 }
5262 else 5611 else
5263 { 5612 {
5264 pointer elem = vector_get (vec, i); 5613 pointer elem = vector_get (vec, i);
5266 ivalue_unchecked (cdr (args)) = i + 1; 5615 ivalue_unchecked (cdr (args)) = i + 1;
5267 s_save (SCHEME_A_ OP_PVECFROM, args, NIL); 5616 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5268 SCHEME_V->args = elem; 5617 SCHEME_V->args = elem;
5269 5618
5270 if (i > 0) 5619 if (i > 0)
5271 putstr (SCHEME_A_ " "); 5620 putcharacter (SCHEME_A_ ' ');
5272 5621
5273 s_goto (OP_P0LIST); 5622 s_goto (OP_P0LIST);
5274 } 5623 }
5275 } 5624 }
5276 } 5625 }
5277 5626
5278 if (USE_ERROR_CHECKING) abort (); 5627 if (USE_ERROR_CHECKING) abort ();
5279} 5628}
5280 5629
5281static int 5630/* list ops */
5631ecb_hot static int
5282opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5632opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5283{ 5633{
5284 pointer args = SCHEME_V->args; 5634 pointer args = SCHEME_V->args;
5285 pointer a = car (args); 5635 pointer a = car (args);
5286 pointer x, y; 5636 pointer x, y;
5309 break; 5659 break;
5310 } 5660 }
5311 5661
5312 if (is_pair (y)) 5662 if (is_pair (y))
5313 s_return (car (y)); 5663 s_return (car (y));
5314 else 5664
5315 s_return (S_F); 5665 s_return (S_F);
5316
5317 5666
5318 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5667 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5319 SCHEME_V->args = a; 5668 SCHEME_V->args = a;
5320 5669
5321 if (SCHEME_V->args == NIL) 5670 if (SCHEME_V->args == NIL)
5322 s_return (S_F); 5671 s_return (S_F);
5323 else if (is_closure (SCHEME_V->args)) 5672 else if (is_closure (SCHEME_V->args) || is_macro (SCHEME_V->args))
5324 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5673 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5325 else if (is_macro (SCHEME_V->args)) 5674
5326 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5327 else
5328 s_return (S_F); 5675 s_return (S_F);
5329 5676
5330 case OP_CLOSUREP: /* closure? */ 5677 case OP_CLOSUREP: /* closure? */
5331 /* 5678 /*
5332 * Note, macro object is also a closure. 5679 * Note, macro object is also a closure.
5333 * Therefore, (closure? <#MACRO>) ==> #t 5680 * Therefore, (closure? <#MACRO>) ==> #t
5344 5691
5345/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ 5692/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5346typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); 5693typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5347 5694
5348typedef int (*test_predicate)(pointer); 5695typedef int (*test_predicate)(pointer);
5349static int 5696
5697ecb_hot static int
5350tst_any (pointer p) 5698tst_any (pointer p)
5351{ 5699{
5352 return 1; 5700 return 1;
5353} 5701}
5354 5702
5355static int 5703ecb_hot static int
5356tst_inonneg (pointer p) 5704tst_inonneg (pointer p)
5357{ 5705{
5358 return is_integer (p) && ivalue_unchecked (p) >= 0; 5706 return is_integer (p) && ivalue_unchecked (p) >= 0;
5359} 5707}
5360 5708
5361static int 5709ecb_hot static int
5362tst_is_list (SCHEME_P_ pointer p) 5710tst_is_list (SCHEME_P_ pointer p)
5363{ 5711{
5364 return p == NIL || is_pair (p); 5712 return p == NIL || is_pair (p);
5365} 5713}
5366 5714
5409#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" 5757#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5410#include "opdefines.h" 5758#include "opdefines.h"
5411#undef OP_DEF 5759#undef OP_DEF
5412; 5760;
5413 5761
5414static const char * 5762ecb_cold static const char *
5415opname (int idx) 5763opname (int idx)
5416{ 5764{
5417 const char *name = opnames; 5765 const char *name = opnames;
5418 5766
5419 /* should do this at compile time, but would require external program, right? */ 5767 /* should do this at compile time, but would require external program, right? */
5421 name += strlen (name) + 1; 5769 name += strlen (name) + 1;
5422 5770
5423 return *name ? name : "ILLEGAL"; 5771 return *name ? name : "ILLEGAL";
5424} 5772}
5425 5773
5426static const char * 5774ecb_cold static const char *
5427procname (pointer x) 5775procname (pointer x)
5428{ 5776{
5429 return opname (procnum (x)); 5777 return opname (procnum (x));
5430} 5778}
5431 5779
5451#undef OP_DEF 5799#undef OP_DEF
5452 {0} 5800 {0}
5453}; 5801};
5454 5802
5455/* kernel of this interpreter */ 5803/* kernel of this interpreter */
5456static void ecb_hot 5804ecb_hot static void
5457Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5805Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5458{ 5806{
5459 SCHEME_V->op = op; 5807 SCHEME_V->op = op;
5460 5808
5461 for (;;) 5809 for (;;)
5544 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) 5892 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5545 return; 5893 return;
5546 5894
5547 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5895 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5548 { 5896 {
5549 xwrstr ("No memory!\n"); 5897 putstr (SCHEME_A_ "No memory!\n");
5550 return; 5898 return;
5551 } 5899 }
5552 } 5900 }
5553} 5901}
5554 5902
5555/* ========== Initialization of internal keywords ========== */ 5903/* ========== Initialization of internal keywords ========== */
5556 5904
5557static void 5905ecb_cold static void
5558assign_syntax (SCHEME_P_ const char *name) 5906assign_syntax (SCHEME_P_ const char *name)
5559{ 5907{
5560 pointer x = oblist_add_by_name (SCHEME_A_ name); 5908 pointer x = oblist_add_by_name (SCHEME_A_ name);
5561 set_typeflag (x, typeflag (x) | T_SYNTAX); 5909 set_typeflag (x, typeflag (x) | T_SYNTAX);
5562} 5910}
5563 5911
5564static void 5912ecb_cold static void
5565assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) 5913assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name)
5566{ 5914{
5567 pointer x = mk_symbol (SCHEME_A_ name); 5915 pointer x = mk_symbol (SCHEME_A_ name);
5568 pointer y = mk_proc (SCHEME_A_ op); 5916 pointer y = mk_proc (SCHEME_A_ op);
5569 new_slot_in_env (SCHEME_A_ x, y); 5917 new_slot_in_env (SCHEME_A_ x, y);
5577 ivalue_unchecked (y) = op; 5925 ivalue_unchecked (y) = op;
5578 return y; 5926 return y;
5579} 5927}
5580 5928
5581/* Hard-coded for the given keywords. Remember to rewrite if more are added! */ 5929/* Hard-coded for the given keywords. Remember to rewrite if more are added! */
5582static int 5930ecb_hot static int
5583syntaxnum (pointer p) 5931syntaxnum (pointer p)
5584{ 5932{
5585 const char *s = strvalue (p); 5933 const char *s = strvalue (p);
5586 5934
5587 switch (strlength (p)) 5935 switch (strlength (p))
5666 6014
5667ecb_cold int 6015ecb_cold int
5668scheme_init (SCHEME_P) 6016scheme_init (SCHEME_P)
5669{ 6017{
5670 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); 6018 int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]);
5671 pointer x;
5672 6019
5673 /* this memset is not strictly correct, as we assume (intcache) 6020 /* this memset is not strictly correct, as we assume (intcache)
5674 * that memset 0 will also set pointers to 0, but memset does 6021 * that memset 0 will also set pointers to 0, but memset does
5675 * of course not guarantee that. screw such systems. 6022 * of course not guarantee that. screw such systems.
5676 */ 6023 */
5704#endif 6051#endif
5705 } 6052 }
5706 6053
5707 SCHEME_V->gc_verbose = 0; 6054 SCHEME_V->gc_verbose = 0;
5708 dump_stack_initialize (SCHEME_A); 6055 dump_stack_initialize (SCHEME_A);
5709 SCHEME_V->code = NIL; 6056 SCHEME_V->code = NIL;
5710 SCHEME_V->args = NIL; 6057 SCHEME_V->args = NIL;
5711 SCHEME_V->envir = NIL; 6058 SCHEME_V->envir = NIL;
6059 SCHEME_V->value = NIL;
5712 SCHEME_V->tracing = 0; 6060 SCHEME_V->tracing = 0;
5713 6061
5714 /* init NIL */ 6062 /* init NIL */
5715 set_typeflag (NIL, T_ATOM | T_MARK); 6063 set_typeflag (NIL, T_SPECIAL | T_ATOM);
5716 set_car (NIL, NIL); 6064 set_car (NIL, NIL);
5717 set_cdr (NIL, NIL); 6065 set_cdr (NIL, NIL);
5718 /* init T */ 6066 /* init T */
5719 set_typeflag (S_T, T_ATOM | T_MARK); 6067 set_typeflag (S_T, T_SPECIAL | T_ATOM);
5720 set_car (S_T, S_T); 6068 set_car (S_T, S_T);
5721 set_cdr (S_T, S_T); 6069 set_cdr (S_T, S_T);
5722 /* init F */ 6070 /* init F */
5723 set_typeflag (S_F, T_ATOM | T_MARK); 6071 set_typeflag (S_F, T_SPECIAL | T_ATOM);
5724 set_car (S_F, S_F); 6072 set_car (S_F, S_F);
5725 set_cdr (S_F, S_F); 6073 set_cdr (S_F, S_F);
5726 /* init EOF_OBJ */ 6074 /* init EOF_OBJ */
5727 set_typeflag (S_EOF, T_ATOM | T_MARK); 6075 set_typeflag (S_EOF, T_SPECIAL | T_ATOM);
5728 set_car (S_EOF, S_EOF); 6076 set_car (S_EOF, S_EOF);
5729 set_cdr (S_EOF, S_EOF); 6077 set_cdr (S_EOF, S_EOF);
5730 /* init sink */ 6078 /* init sink */
5731 set_typeflag (S_SINK, T_PAIR | T_MARK); 6079 set_typeflag (S_SINK, T_PAIR);
5732 set_car (S_SINK, NIL); 6080 set_car (S_SINK, NIL);
5733 6081
5734 /* init c_nest */ 6082 /* init c_nest */
5735 SCHEME_V->c_nest = NIL; 6083 SCHEME_V->c_nest = NIL;
5736 6084
5737 SCHEME_V->oblist = oblist_initial_value (SCHEME_A); 6085 SCHEME_V->oblist = oblist_initial_value (SCHEME_A);
5738 /* init global_env */ 6086 /* init global_env */
5739 new_frame_in_env (SCHEME_A_ NIL); 6087 new_frame_in_env (SCHEME_A_ NIL);
5740 SCHEME_V->global_env = SCHEME_V->envir; 6088 SCHEME_V->global_env = SCHEME_V->envir;
5741 /* init else */ 6089 /* init else */
5742 x = mk_symbol (SCHEME_A_ "else"); 6090 new_slot_in_env (SCHEME_A_ mk_symbol (SCHEME_A_ "else"), S_T);
5743 new_slot_in_env (SCHEME_A_ x, S_T);
5744 6091
5745 { 6092 {
5746 static const char *syntax_names[] = { 6093 static const char *syntax_names[] = {
5747 "lambda", "quote", "define", "if", "begin", "set!", 6094 "lambda", "quote", "define", "if", "begin", "set!",
5748 "let", "let*", "letrec", "cond", "delay", "and", 6095 "let", "let*", "letrec", "cond", "delay", "and",
5772 6119
5773 return !SCHEME_V->no_memory; 6120 return !SCHEME_V->no_memory;
5774} 6121}
5775 6122
5776#if USE_PORTS 6123#if USE_PORTS
5777void 6124ecb_cold void
5778scheme_set_input_port_file (SCHEME_P_ int fin) 6125scheme_set_input_port_file (SCHEME_P_ int fin)
5779{ 6126{
5780 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input); 6127 SCHEME_V->inport = port_from_file (SCHEME_A_ fin, port_input);
5781} 6128}
5782 6129
5783void 6130ecb_cold void
5784scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end) 6131scheme_set_input_port_string (SCHEME_P_ char *start, char *past_the_end)
5785{ 6132{
5786 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input); 6133 SCHEME_V->inport = port_from_string (SCHEME_A_ start, past_the_end, port_input);
5787} 6134}
5788 6135
5789void 6136ecb_cold void
5790scheme_set_output_port_file (SCHEME_P_ int fout) 6137scheme_set_output_port_file (SCHEME_P_ int fout)
5791{ 6138{
5792 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output); 6139 SCHEME_V->outport = port_from_file (SCHEME_A_ fout, port_output);
5793} 6140}
5794 6141
5795void 6142ecb_cold void
5796scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end) 6143scheme_set_output_port_string (SCHEME_P_ char *start, char *past_the_end)
5797{ 6144{
5798 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output); 6145 SCHEME_V->outport = port_from_string (SCHEME_A_ start, past_the_end, port_output);
5799} 6146}
5800#endif 6147#endif
5801 6148
5802void 6149ecb_cold void
5803scheme_set_external_data (SCHEME_P_ void *p) 6150scheme_set_external_data (SCHEME_P_ void *p)
5804{ 6151{
5805 SCHEME_V->ext_data = p; 6152 SCHEME_V->ext_data = p;
5806} 6153}
5807 6154
5839 SCHEME_V->loadport = NIL; 6186 SCHEME_V->loadport = NIL;
5840 SCHEME_V->gc_verbose = 0; 6187 SCHEME_V->gc_verbose = 0;
5841 gc (SCHEME_A_ NIL, NIL); 6188 gc (SCHEME_A_ NIL, NIL);
5842 6189
5843 for (i = 0; i <= SCHEME_V->last_cell_seg; i++) 6190 for (i = 0; i <= SCHEME_V->last_cell_seg; i++)
5844 free (SCHEME_V->alloc_seg[i]); 6191 free (SCHEME_V->cell_seg[i]);
5845 6192
5846#if SHOW_ERROR_LINE 6193#if SHOW_ERROR_LINE
5847 for (i = 0; i <= SCHEME_V->file_i; i++) 6194 for (i = 0; i <= SCHEME_V->file_i; i++)
5848 {
5849 if (SCHEME_V->load_stack[i].kind & port_file) 6195 if (SCHEME_V->load_stack[i].kind & port_file)
5850 { 6196 {
5851 fname = SCHEME_V->load_stack[i].rep.stdio.filename; 6197 fname = SCHEME_V->load_stack[i].rep.stdio.filename;
5852 6198
5853 if (fname) 6199 if (fname)
5854 free (fname); 6200 free (fname);
5855 } 6201 }
5856 }
5857#endif 6202#endif
5858} 6203}
5859 6204
5860void 6205ecb_cold void
5861scheme_load_file (SCHEME_P_ int fin) 6206scheme_load_file (SCHEME_P_ int fin)
5862{ 6207{
5863 scheme_load_named_file (SCHEME_A_ fin, 0); 6208 scheme_load_named_file (SCHEME_A_ fin, 0);
5864} 6209}
5865 6210
5866void 6211ecb_cold void
5867scheme_load_named_file (SCHEME_P_ int fin, const char *filename) 6212scheme_load_named_file (SCHEME_P_ int fin, const char *filename)
5868{ 6213{
5869 dump_stack_reset (SCHEME_A); 6214 dump_stack_reset (SCHEME_A);
5870 SCHEME_V->envir = SCHEME_V->global_env; 6215 SCHEME_V->envir = SCHEME_V->global_env;
5871 SCHEME_V->file_i = 0; 6216 SCHEME_V->file_i = 0;
5872 SCHEME_V->load_stack[0].unget = -1; 6217 SCHEME_V->load_stack[0].unget = -1;
5873 SCHEME_V->load_stack[0].kind = port_input | port_file; 6218 SCHEME_V->load_stack[0].kind = port_input | port_file;
5874 SCHEME_V->load_stack[0].rep.stdio.file = fin; 6219 SCHEME_V->load_stack[0].rep.stdio.file = fin;
5875#if USE_PORTS
5876 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6220 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5877#endif
5878 SCHEME_V->retcode = 0; 6221 SCHEME_V->retcode = 0;
5879 6222
5880#if USE_PORTS
5881 if (fin == STDIN_FILENO) 6223 if (fin == STDIN_FILENO)
5882 SCHEME_V->interactive_repl = 1; 6224 SCHEME_V->interactive_repl = 1;
5883#endif
5884 6225
5885#if USE_PORTS 6226#if USE_PORTS
5886#if SHOW_ERROR_LINE 6227#if SHOW_ERROR_LINE
5887 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0; 6228 SCHEME_V->load_stack[0].rep.stdio.curr_line = 0;
5888 6229
5892#endif 6233#endif
5893 6234
5894 SCHEME_V->inport = SCHEME_V->loadport; 6235 SCHEME_V->inport = SCHEME_V->loadport;
5895 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6236 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5896 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6237 Eval_Cycle (SCHEME_A_ OP_T0LVL);
6238
5897 set_typeflag (SCHEME_V->loadport, T_ATOM); 6239 set_typeflag (SCHEME_V->loadport, T_ATOM);
5898 6240
5899 if (SCHEME_V->retcode == 0) 6241 if (SCHEME_V->retcode == 0)
5900 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6242 SCHEME_V->retcode = SCHEME_V->nesting != 0;
5901} 6243}
5902 6244
5903void 6245ecb_cold void
5904scheme_load_string (SCHEME_P_ const char *cmd) 6246scheme_load_string (SCHEME_P_ const char *cmd)
5905{ 6247{
6248#if USE_PORTs
5906 dump_stack_reset (SCHEME_A); 6249 dump_stack_reset (SCHEME_A);
5907 SCHEME_V->envir = SCHEME_V->global_env; 6250 SCHEME_V->envir = SCHEME_V->global_env;
5908 SCHEME_V->file_i = 0; 6251 SCHEME_V->file_i = 0;
5909 SCHEME_V->load_stack[0].kind = port_input | port_string; 6252 SCHEME_V->load_stack[0].kind = port_input | port_string;
5910 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ 6253 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */
5911 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); 6254 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd);
5912 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; 6255 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5913#if USE_PORTS
5914 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 6256 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5915#endif
5916 SCHEME_V->retcode = 0; 6257 SCHEME_V->retcode = 0;
5917 SCHEME_V->interactive_repl = 0; 6258 SCHEME_V->interactive_repl = 0;
5918 SCHEME_V->inport = SCHEME_V->loadport; 6259 SCHEME_V->inport = SCHEME_V->loadport;
5919 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 6260 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
5920 Eval_Cycle (SCHEME_A_ OP_T0LVL); 6261 Eval_Cycle (SCHEME_A_ OP_T0LVL);
5921 set_typeflag (SCHEME_V->loadport, T_ATOM); 6262 set_typeflag (SCHEME_V->loadport, T_ATOM);
5922 6263
5923 if (SCHEME_V->retcode == 0) 6264 if (SCHEME_V->retcode == 0)
5924 SCHEME_V->retcode = SCHEME_V->nesting != 0; 6265 SCHEME_V->retcode = SCHEME_V->nesting != 0;
6266#else
6267 abort ();
6268#endif
5925} 6269}
5926 6270
5927void 6271ecb_cold void
5928scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value) 6272scheme_define (SCHEME_P_ pointer envir, pointer symbol, pointer value)
5929{ 6273{
5930 pointer x; 6274 pointer x;
5931 6275
5932 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); 6276 x = find_slot_in_env (SCHEME_A_ envir, symbol, 0);
5937 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); 6281 new_slot_spec_in_env (SCHEME_A_ envir, symbol, value);
5938} 6282}
5939 6283
5940#if !STANDALONE 6284#if !STANDALONE
5941 6285
5942void 6286ecb_cold void
5943scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) 6287scheme_register_foreign_func (scheme * sc, scheme_registerable * sr)
5944{ 6288{
5945 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); 6289 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f));
5946} 6290}
5947 6291
5948void 6292ecb_cold void
5949scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) 6293scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count)
5950{ 6294{
5951 int i; 6295 int i;
5952 6296
5953 for (i = 0; i < count; i++) 6297 for (i = 0; i < count; i++)
5954 scheme_register_foreign_func (SCHEME_A_ list + i); 6298 scheme_register_foreign_func (SCHEME_A_ list + i);
5955} 6299}
5956 6300
5957pointer 6301ecb_cold pointer
5958scheme_apply0 (SCHEME_P_ const char *procname) 6302scheme_apply0 (SCHEME_P_ const char *procname)
5959{ 6303{
5960 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL)); 6304 return scheme_eval (SCHEME_A_ cons (mk_symbol (SCHEME_A_ procname), NIL));
5961} 6305}
5962 6306
5963void 6307ecb_cold void
5964save_from_C_call (SCHEME_P) 6308save_from_C_call (SCHEME_P)
5965{ 6309{
5966 pointer saved_data = cons (car (S_SINK), 6310 pointer saved_data = cons (car (S_SINK),
5967 cons (SCHEME_V->envir, 6311 cons (SCHEME_V->envir,
5968 SCHEME_V->dump)); 6312 SCHEME_V->dump));
5972 /* Truncate the dump stack so TS will return here when done, not 6316 /* Truncate the dump stack so TS will return here when done, not
5973 directly resume pre-C-call operations. */ 6317 directly resume pre-C-call operations. */
5974 dump_stack_reset (SCHEME_A); 6318 dump_stack_reset (SCHEME_A);
5975} 6319}
5976 6320
5977void 6321ecb_cold void
5978restore_from_C_call (SCHEME_P) 6322restore_from_C_call (SCHEME_P)
5979{ 6323{
5980 set_car (S_SINK, caar (SCHEME_V->c_nest)); 6324 set_car (S_SINK, caar (SCHEME_V->c_nest));
5981 SCHEME_V->envir = cadar (SCHEME_V->c_nest); 6325 SCHEME_V->envir = cadar (SCHEME_V->c_nest);
5982 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest)); 6326 SCHEME_V->dump = cdr (cdar (SCHEME_V->c_nest));
5983 /* Pop */ 6327 /* Pop */
5984 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest); 6328 SCHEME_V->c_nest = cdr (SCHEME_V->c_nest);
5985} 6329}
5986 6330
5987/* "func" and "args" are assumed to be already eval'ed. */ 6331/* "func" and "args" are assumed to be already eval'ed. */
5988pointer 6332ecb_cold pointer
5989scheme_call (SCHEME_P_ pointer func, pointer args) 6333scheme_call (SCHEME_P_ pointer func, pointer args)
5990{ 6334{
5991 int old_repl = SCHEME_V->interactive_repl; 6335 int old_repl = SCHEME_V->interactive_repl;
5992 6336
5993 SCHEME_V->interactive_repl = 0; 6337 SCHEME_V->interactive_repl = 0;
6000 SCHEME_V->interactive_repl = old_repl; 6344 SCHEME_V->interactive_repl = old_repl;
6001 restore_from_C_call (SCHEME_A); 6345 restore_from_C_call (SCHEME_A);
6002 return SCHEME_V->value; 6346 return SCHEME_V->value;
6003} 6347}
6004 6348
6005pointer 6349ecb_cold pointer
6006scheme_eval (SCHEME_P_ pointer obj) 6350scheme_eval (SCHEME_P_ pointer obj)
6007{ 6351{
6008 int old_repl = SCHEME_V->interactive_repl; 6352 int old_repl = SCHEME_V->interactive_repl;
6009 6353
6010 SCHEME_V->interactive_repl = 0; 6354 SCHEME_V->interactive_repl = 0;
6022 6366
6023/* ========== Main ========== */ 6367/* ========== Main ========== */
6024 6368
6025#if STANDALONE 6369#if STANDALONE
6026 6370
6027int 6371ecb_cold int
6028main (int argc, char **argv) 6372main (int argc, char **argv)
6029{ 6373{
6030# if USE_MULTIPLICITY 6374# if USE_MULTIPLICITY
6031 scheme ssc; 6375 scheme ssc;
6032 scheme *const SCHEME_V = &ssc; 6376 scheme *const SCHEME_V = &ssc;
6034# endif 6378# endif
6035 int fin; 6379 int fin;
6036 char *file_name = InitFile; 6380 char *file_name = InitFile;
6037 int retcode; 6381 int retcode;
6038 int isfile = 1; 6382 int isfile = 1;
6383#if EXPERIMENT
6039 system ("ps v $PPID");//D 6384 system ("ps v $PPID");
6385#endif
6040 6386
6041 if (argc == 2 && strcmp (argv[1], "-?") == 0) 6387 if (argc == 2 && strcmp (argv[1], "-?") == 0)
6042 { 6388 {
6043 xwrstr ("Usage: tinyscheme -?\n"); 6389 putstr (SCHEME_A_ "Usage: tinyscheme -?\n");
6044 xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); 6390 putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n");
6045 xwrstr ("followed by\n"); 6391 putstr (SCHEME_A_ "followed by\n");
6046 xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); 6392 putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n");
6047 xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); 6393 putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n");
6048 xwrstr ("assuming that the executable is named tinyscheme.\n"); 6394 putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n");
6049 xwrstr ("Use - as filename for stdin.\n"); 6395 putstr (SCHEME_A_ "Use - as filename for stdin.\n");
6050 return 1; 6396 return 1;
6051 } 6397 }
6052 6398
6053 if (!scheme_init (SCHEME_A)) 6399 if (!scheme_init (SCHEME_A))
6054 { 6400 {
6055 xwrstr ("Could not initialize!\n"); 6401 putstr (SCHEME_A_ "Could not initialize!\n");
6056 return 2; 6402 return 2;
6057 } 6403 }
6058 6404
6059# if USE_PORTS 6405# if USE_PORTS
6060 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); 6406 scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO);
6073 } 6419 }
6074#endif 6420#endif
6075 6421
6076 do 6422 do
6077 { 6423 {
6078#if USE_PORTS
6079 if (strcmp (file_name, "-") == 0) 6424 if (strcmp (file_name, "-") == 0)
6080 fin = STDIN_FILENO; 6425 fin = STDIN_FILENO;
6081 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0) 6426 else if (strcmp (file_name, "-1") == 0 || strcmp (file_name, "-c") == 0)
6082 { 6427 {
6083 pointer args = NIL; 6428 pointer args = NIL;
6101 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args); 6446 scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ "*args*"), args);
6102 6447
6103 } 6448 }
6104 else 6449 else
6105 fin = open (file_name, O_RDONLY); 6450 fin = open (file_name, O_RDONLY);
6106#endif
6107 6451
6108 if (isfile && fin < 0) 6452 if (isfile && fin < 0)
6109 { 6453 {
6110 xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); 6454 putstr (SCHEME_A_ "Could not open file ");
6455 putstr (SCHEME_A_ file_name);
6456 putcharacter (SCHEME_A_ '\n');
6111 } 6457 }
6112 else 6458 else
6113 { 6459 {
6114 if (isfile) 6460 if (isfile)
6115 scheme_load_named_file (SCHEME_A_ fin, file_name); 6461 scheme_load_named_file (SCHEME_A_ fin, file_name);
6116 else 6462 else
6117 scheme_load_string (SCHEME_A_ file_name); 6463 scheme_load_string (SCHEME_A_ file_name);
6118 6464
6119#if USE_PORTS
6120 if (!isfile || fin != STDIN_FILENO) 6465 if (!isfile || fin != STDIN_FILENO)
6121 { 6466 {
6122 if (SCHEME_V->retcode != 0) 6467 if (SCHEME_V->retcode != 0)
6123 { 6468 {
6124 xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); 6469 putstr (SCHEME_A_ "Errors encountered reading ");
6470 putstr (SCHEME_A_ file_name);
6471 putcharacter (SCHEME_A_ '\n');
6125 } 6472 }
6126 6473
6127 if (isfile) 6474 if (isfile)
6128 close (fin); 6475 close (fin);
6129 } 6476 }
6130#endif
6131 } 6477 }
6132 6478
6133 file_name = *argv++; 6479 file_name = *argv++;
6134 } 6480 }
6135 while (file_name != 0); 6481 while (file_name != 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines