1 | |
1 | /* |
2 | /* T I N Y S C H E M E 1 . 4 1 |
2 | * µscheme |
|
|
3 | * |
|
|
4 | * Copyright (C) 2015 Marc Alexander Lehmann <uscheme@schmorp.de> |
|
|
5 | * do as you want with this, attribution appreciated. |
|
|
6 | * |
|
|
7 | * Based opn tinyscheme-1.41 (original credits follow) |
3 | * Dimitrios Souflis (dsouflis@acm.org) |
8 | * Dimitrios Souflis (dsouflis@acm.org) |
4 | * Based on MiniScheme (original credits follow) |
9 | * Based on MiniScheme (original credits follow) |
5 | * (MINISCM) coded by Atsushi Moriwaki (11/5/1989) |
10 | * (MINISCM) coded by Atsushi Moriwaki (11/5/1989) |
6 | * (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp |
11 | * (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp |
7 | * (MINISCM) This version has been modified by R.C. Secrist. |
12 | * (MINISCM) This version has been modified by R.C. Secrist. |
… | |
… | |
11 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
16 | * (MINISCM) This is a revised and modified version by Akira KIDA. |
12 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
17 | * (MINISCM) current version is 0.85k4 (15 May 1994) |
13 | * |
18 | * |
14 | */ |
19 | */ |
15 | |
20 | |
|
|
21 | #define EXPERIMENT 1 |
|
|
22 | |
|
|
23 | #if 1 |
16 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
24 | #define PAGE_SIZE 4096 /* does not work on sparc/alpha */ |
17 | #include "malloc.c" |
25 | #include "malloc.c" |
|
|
26 | #endif |
18 | |
27 | |
19 | #define SCHEME_SOURCE |
28 | #define SCHEME_SOURCE |
20 | #include "scheme-private.h" |
29 | #include "scheme-private.h" |
21 | #ifndef WIN32 |
30 | #ifndef WIN32 |
22 | # include <unistd.h> |
31 | # include <unistd.h> |
23 | #endif |
32 | #endif |
24 | #if USE_MATH |
33 | #if USE_MATH |
25 | # include <math.h> |
34 | # include <math.h> |
26 | #endif |
35 | #endif |
27 | |
36 | |
|
|
37 | #include "ecb.h" |
|
|
38 | |
28 | #include <sys/types.h> |
39 | #include <sys/types.h> |
29 | #include <sys/stat.h> |
40 | #include <sys/stat.h> |
30 | #include <fcntl.h> |
41 | #include <fcntl.h> |
31 | |
42 | |
|
|
43 | #if !USE_ERROR_CHECKING |
|
|
44 | # define NDEBUG |
|
|
45 | #endif |
|
|
46 | |
|
|
47 | #include <assert.h> |
|
|
48 | #include <stdlib.h> |
32 | #include <string.h> |
49 | #include <string.h> |
33 | #include <stdlib.h> |
|
|
34 | |
50 | |
35 | #include <limits.h> |
51 | #include <limits.h> |
36 | #include <inttypes.h> |
52 | #include <inttypes.h> |
37 | #include <float.h> |
53 | #include <float.h> |
38 | //#include <ctype.h> |
54 | //#include <ctype.h> |
|
|
55 | |
|
|
56 | #if '1' != '0' + 1 \ |
|
|
57 | || '2' != '0' + 2 || '3' != '0' + 3 || '4' != '0' + 4 || '5' != '0' + 5 \ |
|
|
58 | || '6' != '0' + 6 || '7' != '0' + 7 || '8' != '0' + 8 || '9' != '0' + 9 \ |
|
|
59 | || 'b' != 'a' + 1 || 'c' != 'a' + 2 || 'd' != 'a' + 3 || 'e' != 'a' + 4 \ |
|
|
60 | || 'f' != 'a' + 5 |
|
|
61 | # error "execution character set digits not consecutive" |
|
|
62 | #endif |
39 | |
63 | |
40 | enum { |
64 | enum { |
41 | TOK_EOF, |
65 | TOK_EOF, |
42 | TOK_LPAREN, |
66 | TOK_LPAREN, |
43 | TOK_RPAREN, |
67 | TOK_RPAREN, |
44 | TOK_DOT, |
68 | TOK_DOT, |
45 | TOK_ATOM, |
69 | TOK_ATOM, |
|
|
70 | TOK_DOTATOM, /* atom name starting with '.' */ |
|
|
71 | TOK_STRATOM, /* atom name enclosed in | */ |
46 | TOK_QUOTE, |
72 | TOK_QUOTE, |
47 | TOK_DQUOTE, |
73 | TOK_DQUOTE, |
48 | TOK_BQUOTE, |
74 | TOK_BQUOTE, |
49 | TOK_COMMA, |
75 | TOK_COMMA, |
50 | TOK_ATMARK, |
76 | TOK_ATMARK, |
… | |
… | |
52 | TOK_SHARP_CONST, |
78 | TOK_SHARP_CONST, |
53 | TOK_VEC |
79 | TOK_VEC |
54 | }; |
80 | }; |
55 | |
81 | |
56 | #define BACKQUOTE '`' |
82 | #define BACKQUOTE '`' |
57 | #define DELIMITERS "()\";\f\t\v\n\r " |
83 | #define WHITESPACE " \t\r\n\v\f" |
|
|
84 | #define DELIMITERS "()\";" WHITESPACE |
58 | |
85 | |
59 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
86 | #define NIL POINTER (&SCHEME_V->xNIL) |
60 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
87 | #define S_T POINTER (&SCHEME_V->xT) |
61 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
88 | #define S_F POINTER (&SCHEME_V->xF) |
62 | #define S_SINK (&SCHEME_V->xsink) |
89 | #define S_SINK POINTER (&SCHEME_V->xsink) |
63 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
90 | #define S_EOF POINTER (&SCHEME_V->xEOF_OBJ) |
64 | |
91 | |
65 | #if !USE_MULTIPLICITY |
92 | #if !USE_MULTIPLICITY |
66 | static scheme sc; |
93 | static scheme sc; |
67 | #endif |
94 | #endif |
68 | |
95 | |
… | |
… | |
76 | } |
103 | } |
77 | |
104 | |
78 | char *p = s; |
105 | char *p = s; |
79 | |
106 | |
80 | do { |
107 | do { |
81 | *p++ = '0' + n % base; |
108 | *p++ = "0123456789abcdef"[n % base]; |
82 | n /= base; |
109 | n /= base; |
83 | } while (n); |
110 | } while (n); |
84 | |
111 | |
85 | *p-- = 0; |
112 | *p-- = 0; |
86 | |
113 | |
… | |
… | |
96 | { |
123 | { |
97 | xbase (s, n, 10); |
124 | xbase (s, n, 10); |
98 | } |
125 | } |
99 | |
126 | |
100 | static void |
127 | static void |
101 | xwrstr (const char *s) |
128 | putnum (SCHEME_P_ long n) |
102 | { |
|
|
103 | write (1, s, strlen (s)); |
|
|
104 | } |
|
|
105 | |
|
|
106 | static void |
|
|
107 | xwrnum (long n) |
|
|
108 | { |
129 | { |
109 | char buf[64]; |
130 | char buf[64]; |
110 | |
131 | |
111 | xnum (buf, n); |
132 | xnum (buf, n); |
112 | xwrstr (buf); |
133 | putstr (SCHEME_A_ buf); |
113 | } |
134 | } |
114 | |
135 | |
115 | static char |
136 | static char |
116 | xtoupper (char c) |
137 | xtoupper (char c) |
117 | { |
138 | { |
… | |
… | |
128 | c += 'a' - 'A'; |
149 | c += 'a' - 'A'; |
129 | |
150 | |
130 | return c; |
151 | return c; |
131 | } |
152 | } |
132 | |
153 | |
133 | #if USE_STRLWR |
154 | static int |
|
|
155 | xisdigit (char c) |
|
|
156 | { |
|
|
157 | return c >= '0' && c <= '9'; |
|
|
158 | } |
|
|
159 | |
|
|
160 | #define toupper(c) xtoupper (c) |
|
|
161 | #define tolower(c) xtolower (c) |
|
|
162 | #define isdigit(c) xisdigit (c) |
|
|
163 | |
|
|
164 | #if USE_IGNORECASE |
134 | static const char * |
165 | static const char * |
135 | strlwr (char *s) |
166 | xstrlwr (char *s) |
136 | { |
167 | { |
137 | const char *p = s; |
168 | const char *p = s; |
138 | |
169 | |
139 | while (*s) |
170 | while (*s) |
140 | { |
171 | { |
… | |
… | |
142 | s++; |
173 | s++; |
143 | } |
174 | } |
144 | |
175 | |
145 | return p; |
176 | return p; |
146 | } |
177 | } |
147 | #endif |
|
|
148 | |
178 | |
|
|
179 | #define stricmp(a,b) strcasecmp (a, b) |
|
|
180 | #define strlwr(s) xstrlwr (s) |
|
|
181 | |
|
|
182 | #else |
149 | #define stricmp(a,b) strcmp (a, b) |
183 | # define stricmp(a,b) strcmp (a, b) |
150 | #define strlwr(s) (s) |
184 | # define strlwr(s) (s) |
151 | #define toupper(c) xtoupper(c) |
185 | #endif |
152 | #define tolower(c) xtolower(c) |
|
|
153 | |
186 | |
154 | #ifndef prompt |
187 | #ifndef prompt |
155 | # define prompt "ts> " |
188 | # define prompt "ts> " |
156 | #endif |
189 | #endif |
157 | |
190 | |
158 | #ifndef InitFile |
191 | #ifndef InitFile |
159 | # define InitFile "init.scm" |
192 | # define InitFile "init.scm" |
160 | #endif |
193 | #endif |
161 | |
194 | |
162 | #ifndef FIRST_CELLSEGS |
|
|
163 | # define FIRST_CELLSEGS 3 |
|
|
164 | #endif |
|
|
165 | |
|
|
166 | enum scheme_types |
195 | enum scheme_types |
167 | { |
196 | { |
|
|
197 | T_INTEGER, |
|
|
198 | T_CHARACTER, |
168 | T_FREE, |
199 | T_REAL, |
169 | T_STRING, |
200 | T_STRING, |
170 | T_NUMBER, |
|
|
171 | T_SYMBOL, |
201 | T_SYMBOL, |
172 | T_PROC, |
202 | T_PROC, |
173 | T_PAIR, |
203 | T_PAIR, /* also used for free cells */ |
174 | T_CLOSURE, |
204 | T_CLOSURE, |
|
|
205 | T_MACRO, |
175 | T_CONTINUATION, |
206 | T_CONTINUATION, |
176 | T_FOREIGN, |
207 | T_FOREIGN, |
177 | T_CHARACTER, |
|
|
178 | T_PORT, |
208 | T_PORT, |
179 | T_VECTOR, |
209 | T_VECTOR, |
180 | T_MACRO, |
|
|
181 | T_PROMISE, |
210 | T_PROMISE, |
182 | T_ENVIRONMENT, |
211 | T_ENVIRONMENT, |
183 | /* one more... */ |
212 | /* one more... */ |
184 | T_NUM_SYSTEM_TYPES |
213 | T_NUM_SYSTEM_TYPES |
185 | }; |
214 | }; |
… | |
… | |
188 | #define T_SYNTAX 0x0010 |
217 | #define T_SYNTAX 0x0010 |
189 | #define T_IMMUTABLE 0x0020 |
218 | #define T_IMMUTABLE 0x0020 |
190 | #define T_ATOM 0x0040 /* only for gc */ |
219 | #define T_ATOM 0x0040 /* only for gc */ |
191 | #define T_MARK 0x0080 /* only for gc */ |
220 | #define T_MARK 0x0080 /* only for gc */ |
192 | |
221 | |
193 | static num num_add (num a, num b); |
222 | /* num, for generic arithmetic */ |
194 | static num num_mul (num a, num b); |
223 | struct num |
195 | static num num_div (num a, num b); |
224 | { |
|
|
225 | IVALUE ivalue; |
|
|
226 | #if USE_REAL |
|
|
227 | RVALUE rvalue; |
|
|
228 | char is_fixnum; |
|
|
229 | #endif |
|
|
230 | }; |
|
|
231 | |
|
|
232 | #if USE_REAL |
|
|
233 | # define num_is_fixnum(n) (n).is_fixnum |
|
|
234 | # define num_set_fixnum(n,f) (n).is_fixnum = (f) |
|
|
235 | # define num_ivalue(n) (n).ivalue |
|
|
236 | # define num_rvalue(n) (n).rvalue |
|
|
237 | # define num_set_ivalue(n,i) (n).rvalue = (n).ivalue = (i) |
|
|
238 | # define num_set_rvalue(n,r) (n).rvalue = (r) |
|
|
239 | #else |
|
|
240 | # define num_is_fixnum(n) 1 |
|
|
241 | # define num_set_fixnum(n,f) 0 |
|
|
242 | # define num_ivalue(n) (n).ivalue |
|
|
243 | # define num_rvalue(n) (n).ivalue |
|
|
244 | # define num_set_ivalue(n,i) (n).ivalue = (i) |
|
|
245 | # define num_set_rvalue(n,r) (n).ivalue = (r) |
|
|
246 | #endif |
|
|
247 | |
|
|
248 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
|
|
249 | |
|
|
250 | static num num_op (enum num_op op, num a, num b); |
196 | static num num_intdiv (num a, num b); |
251 | static num num_intdiv (num a, num b); |
197 | static num num_sub (num a, num b); |
|
|
198 | static num num_rem (num a, num b); |
252 | static num num_rem (num a, num b); |
199 | static num num_mod (num a, num b); |
253 | static num num_mod (num a, num b); |
200 | static int num_eq (num a, num b); |
|
|
201 | static int num_gt (num a, num b); |
|
|
202 | static int num_ge (num a, num b); |
|
|
203 | static int num_lt (num a, num b); |
|
|
204 | static int num_le (num a, num b); |
|
|
205 | |
254 | |
206 | #if USE_MATH |
|
|
207 | static double round_per_R5RS (double x); |
|
|
208 | #endif |
|
|
209 | static int is_zero_rvalue (RVALUE x); |
255 | static int is_zero_rvalue (RVALUE x); |
210 | |
|
|
211 | static INLINE int |
|
|
212 | num_is_integer (pointer p) |
|
|
213 | { |
|
|
214 | return num_is_fixnum (p->object.number); |
|
|
215 | } |
|
|
216 | |
256 | |
217 | static num num_zero; |
257 | static num num_zero; |
218 | static num num_one; |
258 | static num num_one; |
219 | |
259 | |
|
|
260 | /* convert "pointer" to cell* / cell* to pointer */ |
|
|
261 | #define CELL(p) ((struct cell *)(p) + 0) |
|
|
262 | #define POINTER(c) ((void *)((c) - 0)) |
|
|
263 | |
220 | /* macros for cell operations */ |
264 | /* macros for cell operations */ |
221 | #define typeflag(p) ((p)->flag + 0) |
265 | #define typeflag(p) (CELL(p)->flag + 0) |
222 | #define set_typeflag(p,v) ((p)->flag = (v)) |
266 | #define set_typeflag(p,v) (CELL(p)->flag = (v)) |
223 | #define type(p) (typeflag (p) & T_MASKTYPE) |
267 | #define type(p) (typeflag (p) & T_MASKTYPE) |
224 | |
268 | |
225 | INTERFACE INLINE int |
269 | INTERFACE int |
226 | is_string (pointer p) |
270 | is_string (pointer p) |
227 | { |
271 | { |
228 | return type (p) == T_STRING; |
272 | return type (p) == T_STRING; |
229 | } |
273 | } |
230 | |
274 | |
231 | #define strvalue(p) ((p)->object.string.svalue) |
275 | #define strvalue(p) (CELL(p)->object.string.svalue) |
232 | #define strlength(p) ((p)->object.string.length) |
276 | #define strlength(p) (CELL(p)->object.string.length) |
233 | |
277 | |
234 | INTERFACE int is_list (SCHEME_P_ pointer p); |
|
|
235 | INTERFACE INLINE int |
278 | INTERFACE int |
236 | is_vector (pointer p) |
279 | is_vector (pointer p) |
237 | { |
280 | { |
238 | return type (p) == T_VECTOR; |
281 | return type (p) == T_VECTOR; |
239 | } |
282 | } |
240 | |
283 | |
|
|
284 | #define vecvalue(p) (CELL(p)->object.vector.vvalue) |
|
|
285 | #define veclength(p) (CELL(p)->object.vector.length) |
241 | INTERFACE void fill_vector (pointer vec, pointer obj); |
286 | INTERFACE void fill_vector (pointer vec, uint32_t start, pointer obj); |
242 | INTERFACE pointer vector_elem (pointer vec, int ielem); |
287 | INTERFACE pointer vector_get (pointer vec, uint32_t ielem); |
243 | INTERFACE void set_vector_elem (pointer vec, int ielem, pointer a); |
288 | INTERFACE void vector_set (pointer vec, uint32_t ielem, pointer a); |
244 | |
289 | |
245 | INTERFACE INLINE int |
290 | INTERFACE int |
|
|
291 | is_integer (pointer p) |
|
|
292 | { |
|
|
293 | return type (p) == T_INTEGER; |
|
|
294 | } |
|
|
295 | |
|
|
296 | /* not the same as in scheme, where integers are (correctly :) reals */ |
|
|
297 | INTERFACE int |
|
|
298 | is_real (pointer p) |
|
|
299 | { |
|
|
300 | return type (p) == T_REAL; |
|
|
301 | } |
|
|
302 | |
|
|
303 | INTERFACE int |
246 | is_number (pointer p) |
304 | is_number (pointer p) |
247 | { |
305 | { |
248 | return type (p) == T_NUMBER; |
306 | return is_integer (p) || is_real (p); |
249 | } |
307 | } |
250 | |
308 | |
251 | INTERFACE INLINE int |
309 | INTERFACE int |
252 | is_integer (pointer p) |
|
|
253 | { |
|
|
254 | if (!is_number (p)) |
|
|
255 | return 0; |
|
|
256 | |
|
|
257 | if (num_is_integer (p) || ivalue (p) == rvalue (p)) |
|
|
258 | return 1; |
|
|
259 | |
|
|
260 | return 0; |
|
|
261 | } |
|
|
262 | |
|
|
263 | INTERFACE INLINE int |
|
|
264 | is_real (pointer p) |
|
|
265 | { |
|
|
266 | return is_number (p) && !num_is_fixnum (p->object.number); |
|
|
267 | } |
|
|
268 | |
|
|
269 | INTERFACE INLINE int |
|
|
270 | is_character (pointer p) |
310 | is_character (pointer p) |
271 | { |
311 | { |
272 | return type (p) == T_CHARACTER; |
312 | return type (p) == T_CHARACTER; |
273 | } |
313 | } |
274 | |
314 | |
275 | INTERFACE INLINE char * |
315 | INTERFACE char * |
276 | string_value (pointer p) |
316 | string_value (pointer p) |
277 | { |
317 | { |
278 | return strvalue (p); |
318 | return strvalue (p); |
279 | } |
319 | } |
280 | |
320 | |
281 | INLINE num |
|
|
282 | nvalue (pointer p) |
|
|
283 | { |
|
|
284 | return (p)->object.number; |
|
|
285 | } |
|
|
286 | |
|
|
287 | static IVALUE |
|
|
288 | num_get_ivalue (const num n) |
|
|
289 | { |
|
|
290 | return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n); |
|
|
291 | } |
|
|
292 | |
|
|
293 | static RVALUE |
|
|
294 | num_get_rvalue (const num n) |
|
|
295 | { |
|
|
296 | return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n); |
|
|
297 | } |
|
|
298 | |
|
|
299 | INTERFACE IVALUE |
|
|
300 | ivalue (pointer p) |
|
|
301 | { |
|
|
302 | return num_get_ivalue (p->object.number); |
|
|
303 | } |
|
|
304 | |
|
|
305 | INTERFACE RVALUE |
|
|
306 | rvalue (pointer p) |
|
|
307 | { |
|
|
308 | return num_get_rvalue (p->object.number); |
|
|
309 | } |
|
|
310 | |
|
|
311 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
321 | #define ivalue_unchecked(p) CELL(p)->object.ivalue |
312 | #if USE_FLOAT |
322 | #define set_ivalue(p,v) CELL(p)->object.ivalue = (v) |
|
|
323 | |
|
|
324 | #if USE_REAL |
313 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
325 | #define rvalue_unchecked(p) CELL(p)->object.rvalue |
314 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
326 | #define set_rvalue(p,v) CELL(p)->object.rvalue = (v) |
315 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
|
|
316 | #else |
327 | #else |
317 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
328 | #define rvalue_unchecked(p) CELL(p)->object.ivalue |
318 | # define set_num_integer(p) 0 |
329 | #define set_rvalue(p,v) CELL(p)->object.ivalue = (v) |
319 | # define set_num_real(p) 0 |
|
|
320 | #endif |
330 | #endif |
|
|
331 | |
321 | INTERFACE long |
332 | INTERFACE long |
322 | charvalue (pointer p) |
333 | charvalue (pointer p) |
323 | { |
334 | { |
324 | return ivalue_unchecked (p); |
335 | return ivalue_unchecked (p); |
325 | } |
336 | } |
326 | |
337 | |
|
|
338 | #define port(p) CELL(p)->object.port |
|
|
339 | #define set_port(p,v) port(p) = (v) |
327 | INTERFACE INLINE int |
340 | INTERFACE int |
328 | is_port (pointer p) |
341 | is_port (pointer p) |
329 | { |
342 | { |
330 | return type (p) == T_PORT; |
343 | return type (p) == T_PORT; |
331 | } |
344 | } |
332 | |
345 | |
333 | INTERFACE INLINE int |
346 | INTERFACE int |
334 | is_inport (pointer p) |
347 | is_inport (pointer p) |
335 | { |
348 | { |
336 | return is_port (p) && p->object.port->kind & port_input; |
349 | return is_port (p) && port (p)->kind & port_input; |
337 | } |
350 | } |
338 | |
351 | |
339 | INTERFACE INLINE int |
352 | INTERFACE int |
340 | is_outport (pointer p) |
353 | is_outport (pointer p) |
341 | { |
354 | { |
342 | return is_port (p) && p->object.port->kind & port_output; |
355 | return is_port (p) && port (p)->kind & port_output; |
343 | } |
356 | } |
344 | |
357 | |
345 | INTERFACE INLINE int |
358 | INTERFACE int |
346 | is_pair (pointer p) |
359 | is_pair (pointer p) |
347 | { |
360 | { |
348 | return type (p) == T_PAIR; |
361 | return type (p) == T_PAIR; |
349 | } |
362 | } |
350 | |
363 | |
351 | #define car(p) ((p)->object.cons.car + 0) |
364 | #define car(p) (POINTER (CELL(p)->object.cons.car)) |
352 | #define cdr(p) ((p)->object.cons.cdr) /* find_consecutive_cells uses &cdr */ |
365 | #define cdr(p) (POINTER (CELL(p)->object.cons.cdr)) |
353 | |
366 | |
354 | #define caar(p) car (car (p)) |
367 | static pointer caar (pointer p) { return car (car (p)); } |
355 | #define cadr(p) car (cdr (p)) |
368 | static pointer cadr (pointer p) { return car (cdr (p)); } |
356 | #define cdar(p) cdr (car (p)) |
369 | static pointer cdar (pointer p) { return cdr (car (p)); } |
357 | #define cddr(p) cdr (cdr (p)) |
370 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
358 | |
371 | |
359 | #define cadar(p) car (cdr (car (p))) |
372 | static pointer cadar (pointer p) { return car (cdr (car (p))); } |
360 | #define caddr(p) car (cdr (cdr (p))) |
373 | static pointer caddr (pointer p) { return car (cdr (cdr (p))); } |
361 | #define cdaar(p) cdr (car (car (p))) |
374 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
362 | |
375 | |
363 | INTERFACE void |
376 | INTERFACE void |
364 | set_car (pointer p, pointer q) |
377 | set_car (pointer p, pointer q) |
365 | { |
378 | { |
366 | p->object.cons.car = q; |
379 | CELL(p)->object.cons.car = CELL (q); |
367 | } |
380 | } |
368 | |
381 | |
369 | INTERFACE void |
382 | INTERFACE void |
370 | set_cdr (pointer p, pointer q) |
383 | set_cdr (pointer p, pointer q) |
371 | { |
384 | { |
372 | p->object.cons.cdr = q; |
385 | CELL(p)->object.cons.cdr = CELL (q); |
373 | } |
386 | } |
374 | |
387 | |
375 | INTERFACE pointer |
388 | INTERFACE pointer |
376 | pair_car (pointer p) |
389 | pair_car (pointer p) |
377 | { |
390 | { |
… | |
… | |
382 | pair_cdr (pointer p) |
395 | pair_cdr (pointer p) |
383 | { |
396 | { |
384 | return cdr (p); |
397 | return cdr (p); |
385 | } |
398 | } |
386 | |
399 | |
387 | INTERFACE INLINE int |
400 | INTERFACE int |
388 | is_symbol (pointer p) |
401 | is_symbol (pointer p) |
389 | { |
402 | { |
390 | return type (p) == T_SYMBOL; |
403 | return type (p) == T_SYMBOL; |
391 | } |
404 | } |
392 | |
405 | |
393 | INTERFACE INLINE char * |
406 | INTERFACE char * |
394 | symname (pointer p) |
407 | symname (pointer p) |
395 | { |
408 | { |
396 | return strvalue (car (p)); |
409 | return strvalue (p); |
397 | } |
410 | } |
398 | |
411 | |
399 | #if USE_PLIST |
412 | #if USE_PLIST |
|
|
413 | #error plists are broken because symbols are no longer pairs |
|
|
414 | #define symprop(p) cdr(p) |
400 | SCHEME_EXPORT INLINE int |
415 | SCHEME_EXPORT int |
401 | hasprop (pointer p) |
416 | hasprop (pointer p) |
402 | { |
417 | { |
403 | return typeflag (p) & T_SYMBOL; |
418 | return typeflag (p) & T_SYMBOL; |
404 | } |
419 | } |
405 | |
|
|
406 | # define symprop(p) cdr(p) |
|
|
407 | #endif |
420 | #endif |
408 | |
421 | |
409 | INTERFACE INLINE int |
422 | INTERFACE int |
410 | is_syntax (pointer p) |
423 | is_syntax (pointer p) |
411 | { |
424 | { |
412 | return typeflag (p) & T_SYNTAX; |
425 | return typeflag (p) & T_SYNTAX; |
413 | } |
426 | } |
414 | |
427 | |
415 | INTERFACE INLINE int |
428 | INTERFACE int |
416 | is_proc (pointer p) |
429 | is_proc (pointer p) |
417 | { |
430 | { |
418 | return type (p) == T_PROC; |
431 | return type (p) == T_PROC; |
419 | } |
432 | } |
420 | |
433 | |
421 | INTERFACE INLINE int |
434 | INTERFACE int |
422 | is_foreign (pointer p) |
435 | is_foreign (pointer p) |
423 | { |
436 | { |
424 | return type (p) == T_FOREIGN; |
437 | return type (p) == T_FOREIGN; |
425 | } |
438 | } |
426 | |
439 | |
427 | INTERFACE INLINE char * |
440 | INTERFACE char * |
428 | syntaxname (pointer p) |
441 | syntaxname (pointer p) |
429 | { |
442 | { |
430 | return strvalue (car (p)); |
443 | return strvalue (p); |
431 | } |
444 | } |
432 | |
445 | |
433 | #define procnum(p) ivalue (p) |
446 | #define procnum(p) ivalue_unchecked (p) |
434 | static const char *procname (pointer x); |
447 | static const char *procname (pointer x); |
435 | |
448 | |
436 | INTERFACE INLINE int |
449 | INTERFACE int |
437 | is_closure (pointer p) |
450 | is_closure (pointer p) |
438 | { |
451 | { |
439 | return type (p) == T_CLOSURE; |
452 | return type (p) == T_CLOSURE; |
440 | } |
453 | } |
441 | |
454 | |
442 | INTERFACE INLINE int |
455 | INTERFACE int |
443 | is_macro (pointer p) |
456 | is_macro (pointer p) |
444 | { |
457 | { |
445 | return type (p) == T_MACRO; |
458 | return type (p) == T_MACRO; |
446 | } |
459 | } |
447 | |
460 | |
448 | INTERFACE INLINE pointer |
461 | INTERFACE pointer |
449 | closure_code (pointer p) |
462 | closure_code (pointer p) |
450 | { |
463 | { |
451 | return car (p); |
464 | return car (p); |
452 | } |
465 | } |
453 | |
466 | |
454 | INTERFACE INLINE pointer |
467 | INTERFACE pointer |
455 | closure_env (pointer p) |
468 | closure_env (pointer p) |
456 | { |
469 | { |
457 | return cdr (p); |
470 | return cdr (p); |
458 | } |
471 | } |
459 | |
472 | |
460 | INTERFACE INLINE int |
473 | INTERFACE int |
461 | is_continuation (pointer p) |
474 | is_continuation (pointer p) |
462 | { |
475 | { |
463 | return type (p) == T_CONTINUATION; |
476 | return type (p) == T_CONTINUATION; |
464 | } |
477 | } |
465 | |
478 | |
466 | #define cont_dump(p) cdr (p) |
479 | #define cont_dump(p) cdr (p) |
467 | #define set_cont_dump(p,v) set_cdr ((p), (v)) |
480 | #define set_cont_dump(p,v) set_cdr ((p), (v)) |
468 | |
481 | |
469 | /* To do: promise should be forced ONCE only */ |
482 | /* To do: promise should be forced ONCE only */ |
470 | INTERFACE INLINE int |
483 | INTERFACE int |
471 | is_promise (pointer p) |
484 | is_promise (pointer p) |
472 | { |
485 | { |
473 | return type (p) == T_PROMISE; |
486 | return type (p) == T_PROMISE; |
474 | } |
487 | } |
475 | |
488 | |
476 | INTERFACE INLINE int |
489 | INTERFACE int |
477 | is_environment (pointer p) |
490 | is_environment (pointer p) |
478 | { |
491 | { |
479 | return type (p) == T_ENVIRONMENT; |
492 | return type (p) == T_ENVIRONMENT; |
480 | } |
493 | } |
481 | |
494 | |
482 | #define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) |
495 | #define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) |
483 | |
496 | |
484 | #define is_atom1(p) (TYPESET_ATOM & (1U << type (p))) |
|
|
485 | #define is_atom(p) (typeflag (p) & T_ATOM) |
497 | #define is_atom(p) (typeflag (p) & T_ATOM) |
486 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
498 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
487 | #define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) |
499 | #define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) |
488 | |
500 | |
489 | #define is_mark(p) (typeflag (p) & T_MARK) |
501 | #define is_mark(p) (typeflag (p) & T_MARK) |
490 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
502 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
491 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
503 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
492 | |
504 | |
493 | #if 0 |
|
|
494 | static int |
|
|
495 | is_atom(pointer p) |
|
|
496 | { |
|
|
497 | if (!is_atom1(p) != !is_atom2(p)) |
|
|
498 | printf ("atoms disagree %x\n", typeflag(p)); |
|
|
499 | |
|
|
500 | return is_atom2(p); |
|
|
501 | } |
|
|
502 | #endif |
|
|
503 | |
|
|
504 | INTERFACE INLINE int |
505 | INTERFACE int |
505 | is_immutable (pointer p) |
506 | is_immutable (pointer p) |
506 | { |
507 | { |
507 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
508 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
508 | } |
509 | } |
509 | |
510 | |
510 | INTERFACE INLINE void |
511 | INTERFACE void |
511 | setimmutable (pointer p) |
512 | setimmutable (pointer p) |
512 | { |
513 | { |
513 | #if USE_ERROR_CHECKING |
514 | #if USE_ERROR_CHECKING |
514 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
515 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
515 | #endif |
516 | #endif |
516 | } |
517 | } |
517 | |
518 | |
|
|
519 | /* Result is: |
|
|
520 | proper list: length |
|
|
521 | circular list: -1 |
|
|
522 | not even a pair: -2 |
|
|
523 | dotted list: -2 minus length before dot |
|
|
524 | */ |
|
|
525 | INTERFACE int |
|
|
526 | list_length (SCHEME_P_ pointer a) |
|
|
527 | { |
|
|
528 | int i = 0; |
|
|
529 | pointer slow, fast; |
|
|
530 | |
|
|
531 | slow = fast = a; |
|
|
532 | |
|
|
533 | while (1) |
|
|
534 | { |
|
|
535 | if (fast == NIL) |
|
|
536 | return i; |
|
|
537 | |
|
|
538 | if (!is_pair (fast)) |
|
|
539 | return -2 - i; |
|
|
540 | |
|
|
541 | fast = cdr (fast); |
|
|
542 | ++i; |
|
|
543 | |
|
|
544 | if (fast == NIL) |
|
|
545 | return i; |
|
|
546 | |
|
|
547 | if (!is_pair (fast)) |
|
|
548 | return -2 - i; |
|
|
549 | |
|
|
550 | ++i; |
|
|
551 | fast = cdr (fast); |
|
|
552 | |
|
|
553 | /* Safe because we would have already returned if `fast' |
|
|
554 | encountered a non-pair. */ |
|
|
555 | slow = cdr (slow); |
|
|
556 | |
|
|
557 | if (fast == slow) |
|
|
558 | { |
|
|
559 | /* the fast pointer has looped back around and caught up |
|
|
560 | with the slow pointer, hence the structure is circular, |
|
|
561 | not of finite length, and therefore not a list */ |
|
|
562 | return -1; |
|
|
563 | } |
|
|
564 | } |
|
|
565 | } |
|
|
566 | |
|
|
567 | INTERFACE int |
|
|
568 | is_list (SCHEME_P_ pointer a) |
|
|
569 | { |
|
|
570 | return list_length (SCHEME_A_ a) >= 0; |
|
|
571 | } |
|
|
572 | |
518 | #if USE_CHAR_CLASSIFIERS |
573 | #if USE_CHAR_CLASSIFIERS |
519 | static INLINE int |
574 | ecb_inline int |
520 | Cisalpha (int c) |
575 | Cisalpha (int c) |
521 | { |
576 | { |
522 | return isascii (c) && isalpha (c); |
577 | return isascii (c) && isalpha (c); |
523 | } |
578 | } |
524 | |
579 | |
525 | static INLINE int |
580 | ecb_inline int |
526 | Cisdigit (int c) |
581 | Cisdigit (int c) |
527 | { |
582 | { |
528 | return isascii (c) && isdigit (c); |
583 | return isascii (c) && isdigit (c); |
529 | } |
584 | } |
530 | |
585 | |
531 | static INLINE int |
586 | ecb_inline int |
532 | Cisspace (int c) |
587 | Cisspace (int c) |
533 | { |
588 | { |
534 | return isascii (c) && isspace (c); |
589 | return isascii (c) && isspace (c); |
535 | } |
590 | } |
536 | |
591 | |
537 | static INLINE int |
592 | ecb_inline int |
538 | Cisupper (int c) |
593 | Cisupper (int c) |
539 | { |
594 | { |
540 | return isascii (c) && isupper (c); |
595 | return isascii (c) && isupper (c); |
541 | } |
596 | } |
542 | |
597 | |
543 | static INLINE int |
598 | ecb_inline int |
544 | Cislower (int c) |
599 | Cislower (int c) |
545 | { |
600 | { |
546 | return isascii (c) && islower (c); |
601 | return isascii (c) && islower (c); |
547 | } |
602 | } |
548 | #endif |
603 | #endif |
… | |
… | |
609 | #endif |
664 | #endif |
610 | |
665 | |
611 | static int file_push (SCHEME_P_ const char *fname); |
666 | static int file_push (SCHEME_P_ const char *fname); |
612 | static void file_pop (SCHEME_P); |
667 | static void file_pop (SCHEME_P); |
613 | static int file_interactive (SCHEME_P); |
668 | static int file_interactive (SCHEME_P); |
614 | static INLINE int is_one_of (char *s, int c); |
669 | ecb_inline int is_one_of (const char *s, int c); |
615 | static int alloc_cellseg (SCHEME_P_ int n); |
670 | static int alloc_cellseg (SCHEME_P); |
616 | static long binary_decode (const char *s); |
|
|
617 | static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); |
671 | ecb_inline pointer get_cell (SCHEME_P_ pointer a, pointer b); |
618 | static pointer reserve_cells (SCHEME_P_ int n); |
|
|
619 | static pointer get_consecutive_cells (SCHEME_P_ int n); |
|
|
620 | static pointer find_consecutive_cells (SCHEME_P_ int n); |
|
|
621 | static void finalize_cell (SCHEME_P_ pointer a); |
672 | static void finalize_cell (SCHEME_P_ pointer a); |
622 | static int count_consecutive_cells (pointer x, int needed); |
673 | static int count_consecutive_cells (pointer x, int needed); |
623 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
674 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
624 | static pointer mk_number (SCHEME_P_ const num n); |
675 | static pointer mk_number (SCHEME_P_ const num n); |
625 | static char *store_string (SCHEME_P_ int len, const char *str, char fill); |
676 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
626 | static pointer mk_vector (SCHEME_P_ int len); |
677 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
627 | static pointer mk_atom (SCHEME_P_ char *q); |
678 | static pointer mk_atom (SCHEME_P_ char *q); |
628 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
679 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
629 | |
680 | |
630 | #if USE_PORTS |
681 | #if USE_PORTS |
631 | static pointer mk_port (SCHEME_P_ port *p); |
682 | static pointer mk_port (SCHEME_P_ port *p); |
… | |
… | |
640 | static void mark (pointer a); |
691 | static void mark (pointer a); |
641 | static void gc (SCHEME_P_ pointer a, pointer b); |
692 | static void gc (SCHEME_P_ pointer a, pointer b); |
642 | static int basic_inchar (port *pt); |
693 | static int basic_inchar (port *pt); |
643 | static int inchar (SCHEME_P); |
694 | static int inchar (SCHEME_P); |
644 | static void backchar (SCHEME_P_ int c); |
695 | static void backchar (SCHEME_P_ int c); |
645 | static char *readstr_upto (SCHEME_P_ char *delim); |
696 | static char *readstr_upto (SCHEME_P_ int skip, const char *delim); |
646 | static pointer readstrexp (SCHEME_P); |
697 | static pointer readstrexp (SCHEME_P_ char delim); |
647 | static INLINE int skipspace (SCHEME_P); |
698 | ecb_inline int skipspace (SCHEME_P); |
648 | static int token (SCHEME_P); |
699 | static int token (SCHEME_P); |
649 | static void printslashstring (SCHEME_P_ char *s, int len); |
700 | static void printslashstring (SCHEME_P_ char *s, int len); |
650 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
701 | static void atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen); |
651 | static void printatom (SCHEME_P_ pointer l, int f); |
702 | static void printatom (SCHEME_P_ pointer l, int f); |
652 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
703 | static pointer mk_proc (SCHEME_P_ enum scheme_opcodes op); |
… | |
… | |
656 | static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); |
707 | static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); |
657 | static pointer revappend (SCHEME_P_ pointer a, pointer b); |
708 | static pointer revappend (SCHEME_P_ pointer a, pointer b); |
658 | static pointer ss_get_cont (SCHEME_P); |
709 | static pointer ss_get_cont (SCHEME_P); |
659 | static void ss_set_cont (SCHEME_P_ pointer cont); |
710 | static void ss_set_cont (SCHEME_P_ pointer cont); |
660 | static void dump_stack_mark (SCHEME_P); |
711 | static void dump_stack_mark (SCHEME_P); |
661 | static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); |
712 | static int opexe_0 (SCHEME_P_ enum scheme_opcodes op); |
|
|
713 | static int opexe_1 (SCHEME_P_ enum scheme_opcodes op); |
662 | static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); |
714 | static int opexe_2 (SCHEME_P_ enum scheme_opcodes op); |
663 | static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
715 | static int opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
664 | static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
716 | static int opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
665 | static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
717 | static int opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
666 | static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
718 | static int opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
667 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
719 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
668 | static void assign_syntax (SCHEME_P_ const char *name); |
720 | static void assign_syntax (SCHEME_P_ const char *name); |
669 | static int syntaxnum (pointer p); |
721 | static int syntaxnum (pointer p); |
670 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
722 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
671 | |
723 | |
|
|
724 | static IVALUE |
|
|
725 | ivalue (pointer x) |
|
|
726 | { |
|
|
727 | return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x); |
|
|
728 | } |
|
|
729 | |
|
|
730 | static RVALUE |
|
|
731 | rvalue (pointer x) |
|
|
732 | { |
|
|
733 | return is_integer (x) ? ivalue_unchecked (x) : rvalue_unchecked (x); |
|
|
734 | } |
|
|
735 | |
|
|
736 | INTERFACE num |
|
|
737 | nvalue (pointer x) |
|
|
738 | { |
|
|
739 | num n; |
|
|
740 | |
|
|
741 | num_set_fixnum (n, is_integer (x)); |
|
|
742 | |
|
|
743 | if (num_is_fixnum (n)) |
|
|
744 | num_set_ivalue (n, ivalue_unchecked (x)); |
|
|
745 | else |
|
|
746 | num_set_rvalue (n, rvalue_unchecked (x)); |
|
|
747 | |
|
|
748 | return n; |
|
|
749 | } |
|
|
750 | |
672 | static num |
751 | static num |
673 | num_add (num a, num b) |
752 | num_op (enum num_op op, num a, num b) |
674 | { |
753 | { |
675 | num ret; |
754 | num ret; |
676 | |
755 | |
677 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
756 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
678 | |
757 | |
679 | if (num_is_fixnum (ret)) |
758 | if (num_is_fixnum (ret)) |
680 | num_set_ivalue (ret, num_get_ivalue (a) + num_get_ivalue (b)); |
759 | { |
|
|
760 | switch (op) |
|
|
761 | { |
|
|
762 | case NUM_ADD: a.ivalue += b.ivalue; break; |
|
|
763 | case NUM_SUB: a.ivalue -= b.ivalue; break; |
|
|
764 | case NUM_MUL: a.ivalue *= b.ivalue; break; |
|
|
765 | case NUM_INTDIV: a.ivalue /= b.ivalue; break; |
|
|
766 | } |
|
|
767 | |
|
|
768 | num_set_ivalue (ret, a.ivalue); |
|
|
769 | } |
|
|
770 | #if USE_REAL |
681 | else |
771 | else |
682 | num_set_rvalue (ret, num_get_rvalue (a) + num_get_rvalue (b)); |
772 | { |
|
|
773 | switch (op) |
|
|
774 | { |
|
|
775 | case NUM_ADD: a.rvalue += b.rvalue; break; |
|
|
776 | case NUM_SUB: a.rvalue -= b.rvalue; break; |
|
|
777 | case NUM_MUL: a.rvalue *= b.rvalue; break; |
|
|
778 | case NUM_INTDIV: a.rvalue /= b.rvalue; break; |
|
|
779 | } |
683 | |
780 | |
684 | return ret; |
781 | num_set_rvalue (ret, a.rvalue); |
685 | } |
782 | } |
686 | |
783 | #endif |
687 | static num |
|
|
688 | num_mul (num a, num b) |
|
|
689 | { |
|
|
690 | num ret; |
|
|
691 | |
|
|
692 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
|
|
693 | |
|
|
694 | if (num_is_fixnum (ret)) |
|
|
695 | num_set_ivalue (ret, num_get_ivalue (a) * num_get_ivalue (b)); |
|
|
696 | else |
|
|
697 | num_set_rvalue (ret, num_get_rvalue (a) * num_get_rvalue (b)); |
|
|
698 | |
784 | |
699 | return ret; |
785 | return ret; |
700 | } |
786 | } |
701 | |
787 | |
702 | static num |
788 | static num |
703 | num_div (num a, num b) |
789 | num_div (num a, num b) |
704 | { |
790 | { |
705 | num ret; |
791 | num ret; |
706 | |
792 | |
707 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); |
793 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_ivalue (a) % num_ivalue (b) == 0); |
708 | |
794 | |
709 | if (num_is_fixnum (ret)) |
795 | if (num_is_fixnum (ret)) |
710 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
796 | num_set_ivalue (ret, num_ivalue (a) / num_ivalue (b)); |
711 | else |
797 | else |
712 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
798 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
713 | |
|
|
714 | return ret; |
|
|
715 | } |
|
|
716 | |
|
|
717 | static num |
|
|
718 | num_intdiv (num a, num b) |
|
|
719 | { |
|
|
720 | num ret; |
|
|
721 | |
|
|
722 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
|
|
723 | |
|
|
724 | if (num_is_fixnum (ret)) |
|
|
725 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
|
|
726 | else |
|
|
727 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
|
|
728 | |
|
|
729 | return ret; |
|
|
730 | } |
|
|
731 | |
|
|
732 | static num |
|
|
733 | num_sub (num a, num b) |
|
|
734 | { |
|
|
735 | num ret; |
|
|
736 | |
|
|
737 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
|
|
738 | |
|
|
739 | if (num_is_fixnum (ret)) |
|
|
740 | num_set_ivalue (ret, num_get_ivalue (a) - num_get_ivalue (b)); |
|
|
741 | else |
|
|
742 | num_set_rvalue (ret, num_get_rvalue (a) - num_get_rvalue (b)); |
|
|
743 | |
799 | |
744 | return ret; |
800 | return ret; |
745 | } |
801 | } |
746 | |
802 | |
747 | static num |
803 | static num |
… | |
… | |
749 | { |
805 | { |
750 | num ret; |
806 | num ret; |
751 | long e1, e2, res; |
807 | long e1, e2, res; |
752 | |
808 | |
753 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
809 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
754 | e1 = num_get_ivalue (a); |
810 | e1 = num_ivalue (a); |
755 | e2 = num_get_ivalue (b); |
811 | e2 = num_ivalue (b); |
756 | res = e1 % e2; |
812 | res = e1 % e2; |
757 | |
813 | |
758 | /* remainder should have same sign as second operand */ |
814 | /* remainder should have same sign as second operand */ |
759 | if (res > 0) |
815 | if (res > 0) |
760 | { |
816 | { |
… | |
… | |
776 | { |
832 | { |
777 | num ret; |
833 | num ret; |
778 | long e1, e2, res; |
834 | long e1, e2, res; |
779 | |
835 | |
780 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
836 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
781 | e1 = num_get_ivalue (a); |
837 | e1 = num_ivalue (a); |
782 | e2 = num_get_ivalue (b); |
838 | e2 = num_ivalue (b); |
783 | res = e1 % e2; |
839 | res = e1 % e2; |
784 | |
840 | |
785 | /* modulo should have same sign as second operand */ |
841 | /* modulo should have same sign as second operand */ |
786 | if (res * e2 < 0) |
842 | if (res * e2 < 0) |
787 | res += e2; |
843 | res += e2; |
788 | |
844 | |
789 | num_set_ivalue (ret, res); |
845 | num_set_ivalue (ret, res); |
790 | return ret; |
846 | return ret; |
791 | } |
847 | } |
792 | |
848 | |
|
|
849 | /* this completely disrespects NaNs, but r5rs doesn't even allow NaNs */ |
793 | static int |
850 | static int |
794 | num_eq (num a, num b) |
851 | num_cmp (num a, num b) |
795 | { |
852 | { |
|
|
853 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
796 | int ret; |
854 | int ret; |
797 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
|
|
798 | |
855 | |
799 | if (is_fixnum) |
856 | if (is_fixnum) |
800 | ret = num_get_ivalue (a) == num_get_ivalue (b); |
857 | { |
|
|
858 | IVALUE av = num_ivalue (a); |
|
|
859 | IVALUE bv = num_ivalue (b); |
|
|
860 | |
|
|
861 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
|
|
862 | } |
801 | else |
863 | else |
802 | ret = num_get_rvalue (a) == num_get_rvalue (b); |
864 | { |
|
|
865 | RVALUE av = num_rvalue (a); |
|
|
866 | RVALUE bv = num_rvalue (b); |
|
|
867 | |
|
|
868 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
|
|
869 | } |
803 | |
870 | |
804 | return ret; |
871 | return ret; |
805 | } |
872 | } |
806 | |
|
|
807 | |
|
|
808 | static int |
|
|
809 | num_gt (num a, num b) |
|
|
810 | { |
|
|
811 | int ret; |
|
|
812 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
|
|
813 | |
|
|
814 | if (is_fixnum) |
|
|
815 | ret = num_get_ivalue (a) > num_get_ivalue (b); |
|
|
816 | else |
|
|
817 | ret = num_get_rvalue (a) > num_get_rvalue (b); |
|
|
818 | |
|
|
819 | return ret; |
|
|
820 | } |
|
|
821 | |
|
|
822 | static int |
|
|
823 | num_ge (num a, num b) |
|
|
824 | { |
|
|
825 | return !num_lt (a, b); |
|
|
826 | } |
|
|
827 | |
|
|
828 | static int |
|
|
829 | num_lt (num a, num b) |
|
|
830 | { |
|
|
831 | int ret; |
|
|
832 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
|
|
833 | |
|
|
834 | if (is_fixnum) |
|
|
835 | ret = num_get_ivalue (a) < num_get_ivalue (b); |
|
|
836 | else |
|
|
837 | ret = num_get_rvalue (a) < num_get_rvalue (b); |
|
|
838 | |
|
|
839 | return ret; |
|
|
840 | } |
|
|
841 | |
|
|
842 | static int |
|
|
843 | num_le (num a, num b) |
|
|
844 | { |
|
|
845 | return !num_gt (a, b); |
|
|
846 | } |
|
|
847 | |
|
|
848 | #if USE_MATH |
|
|
849 | |
|
|
850 | /* Round to nearest. Round to even if midway */ |
|
|
851 | static double |
|
|
852 | round_per_R5RS (double x) |
|
|
853 | { |
|
|
854 | double fl = floor (x); |
|
|
855 | double ce = ceil (x); |
|
|
856 | double dfl = x - fl; |
|
|
857 | double dce = ce - x; |
|
|
858 | |
|
|
859 | if (dfl > dce) |
|
|
860 | return ce; |
|
|
861 | else if (dfl < dce) |
|
|
862 | return fl; |
|
|
863 | else |
|
|
864 | { |
|
|
865 | if (fmod (fl, 2.0) == 0.0) /* I imagine this holds */ |
|
|
866 | return fl; |
|
|
867 | else |
|
|
868 | return ce; |
|
|
869 | } |
|
|
870 | } |
|
|
871 | #endif |
|
|
872 | |
873 | |
873 | static int |
874 | static int |
874 | is_zero_rvalue (RVALUE x) |
875 | is_zero_rvalue (RVALUE x) |
875 | { |
876 | { |
876 | #if USE_FLOAT |
877 | return x == 0; |
|
|
878 | #if 0 |
|
|
879 | #if USE_REAL |
877 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
880 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
878 | #else |
881 | #else |
879 | return x == 0; |
882 | return x == 0; |
880 | #endif |
883 | #endif |
881 | } |
884 | #endif |
882 | |
|
|
883 | static long |
|
|
884 | binary_decode (const char *s) |
|
|
885 | { |
|
|
886 | long x = 0; |
|
|
887 | |
|
|
888 | while (*s != 0 && (*s == '1' || *s == '0')) |
|
|
889 | { |
|
|
890 | x <<= 1; |
|
|
891 | x += *s - '0'; |
|
|
892 | s++; |
|
|
893 | } |
|
|
894 | |
|
|
895 | return x; |
|
|
896 | } |
885 | } |
897 | |
886 | |
898 | /* allocate new cell segment */ |
887 | /* allocate new cell segment */ |
899 | static int |
888 | static int |
900 | alloc_cellseg (SCHEME_P_ int n) |
889 | alloc_cellseg (SCHEME_P) |
901 | { |
890 | { |
902 | pointer newp; |
891 | struct cell *newp; |
903 | pointer last; |
892 | struct cell *last; |
904 | pointer p; |
893 | struct cell *p; |
905 | char *cp; |
894 | char *cp; |
906 | long i; |
895 | long i; |
907 | int k; |
896 | int k; |
908 | |
897 | |
909 | static int segsize = CELL_SEGSIZE >> 1; |
898 | static int segsize = CELL_SEGSIZE >> 1; |
910 | segsize <<= 1; |
899 | segsize <<= 1; |
911 | |
900 | |
912 | for (k = 0; k < n; k++) |
|
|
913 | { |
|
|
914 | if (SCHEME_V->last_cell_seg >= CELL_NSEGMENT - 1) |
|
|
915 | return k; |
|
|
916 | |
|
|
917 | cp = malloc (segsize * sizeof (struct cell)); |
901 | cp = malloc (segsize * sizeof (struct cell)); |
918 | |
902 | |
919 | if (!cp && USE_ERROR_CHECKING) |
903 | if (!cp && USE_ERROR_CHECKING) |
920 | return k; |
904 | return k; |
921 | |
905 | |
922 | i = ++SCHEME_V->last_cell_seg; |
906 | i = ++SCHEME_V->last_cell_seg; |
923 | SCHEME_V->alloc_seg[i] = cp; |
907 | SCHEME_V->alloc_seg[i] = cp; |
924 | |
908 | |
925 | /* insert new segment in address order */ |
909 | newp = (struct cell *)cp; |
926 | newp = (pointer)cp; |
|
|
927 | SCHEME_V->cell_seg[i] = newp; |
910 | SCHEME_V->cell_seg[i] = newp; |
928 | SCHEME_V->cell_segsize[i] = segsize; |
911 | SCHEME_V->cell_segsize[i] = segsize; |
929 | |
|
|
930 | //TODO: insert, not swap |
|
|
931 | while (i > 0 && SCHEME_V->cell_seg[i - 1] > SCHEME_V->cell_seg[i]) |
|
|
932 | { |
|
|
933 | p = SCHEME_V->cell_seg[i]; |
|
|
934 | SCHEME_V->cell_seg[i] = SCHEME_V->cell_seg[i - 1]; |
|
|
935 | SCHEME_V->cell_seg[i - 1] = p; |
|
|
936 | |
|
|
937 | k = SCHEME_V->cell_segsize[i]; |
|
|
938 | SCHEME_V->cell_segsize[i] = SCHEME_V->cell_segsize[i - 1]; |
|
|
939 | SCHEME_V->cell_segsize[i - 1] = k; |
|
|
940 | |
|
|
941 | --i; |
|
|
942 | } |
|
|
943 | |
|
|
944 | SCHEME_V->fcells += segsize; |
912 | SCHEME_V->fcells += segsize; |
945 | last = newp + segsize - 1; |
913 | last = newp + segsize - 1; |
946 | |
914 | |
947 | for (p = newp; p <= last; p++) |
915 | for (p = newp; p <= last; p++) |
948 | { |
916 | { |
|
|
917 | pointer cp = POINTER (p); |
949 | set_typeflag (p, T_FREE); |
918 | set_typeflag (cp, T_PAIR); |
950 | set_car (p, NIL); |
919 | set_car (cp, NIL); |
951 | set_cdr (p, p + 1); |
920 | set_cdr (cp, POINTER (p + 1)); |
952 | } |
921 | } |
953 | |
922 | |
954 | /* insert new cells in address order on free list */ |
|
|
955 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
|
|
956 | { |
|
|
957 | set_cdr (last, SCHEME_V->free_cell); |
923 | set_cdr (POINTER (last), SCHEME_V->free_cell); |
958 | SCHEME_V->free_cell = newp; |
924 | SCHEME_V->free_cell = POINTER (newp); |
959 | } |
|
|
960 | else |
|
|
961 | { |
|
|
962 | p = SCHEME_V->free_cell; |
|
|
963 | |
925 | |
964 | while (cdr (p) != NIL && newp > cdr (p)) |
|
|
965 | p = cdr (p); |
|
|
966 | |
|
|
967 | set_cdr (last, cdr (p)); |
|
|
968 | set_cdr (p, newp); |
|
|
969 | } |
|
|
970 | } |
|
|
971 | |
|
|
972 | return n; |
926 | return 1; |
973 | } |
927 | } |
974 | |
928 | |
975 | /* get new cell. parameter a, b is marked by gc. */ |
929 | /* get new cell. parameter a, b is marked by gc. */ |
976 | static INLINE pointer |
930 | ecb_inline pointer |
977 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
931 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
978 | { |
932 | { |
979 | if (SCHEME_V->free_cell == NIL) |
933 | if (ecb_expect_false (SCHEME_V->free_cell == NIL)) |
980 | { |
934 | { |
981 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
935 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
982 | return S_SINK; |
936 | return S_SINK; |
983 | |
937 | |
984 | if (SCHEME_V->free_cell == NIL) |
938 | if (SCHEME_V->free_cell == NIL) |
985 | { |
939 | { |
986 | const int min_to_be_recovered = SCHEME_V->last_cell_seg < 128 ? 128 * 8 : SCHEME_V->last_cell_seg * 8; |
940 | const int min_to_be_recovered = SCHEME_V->cell_segsize [SCHEME_V->last_cell_seg] >> 1; |
987 | |
941 | |
988 | gc (SCHEME_A_ a, b); |
942 | gc (SCHEME_A_ a, b); |
989 | |
943 | |
990 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
944 | if (SCHEME_V->fcells < min_to_be_recovered || SCHEME_V->free_cell == NIL) |
991 | { |
945 | { |
992 | /* if only a few recovered, get more to avoid fruitless gc's */ |
946 | /* if only a few recovered, get more to avoid fruitless gc's */ |
993 | if (!alloc_cellseg (SCHEME_A_ 1) && SCHEME_V->free_cell == NIL) |
947 | if (!alloc_cellseg (SCHEME_A) && SCHEME_V->free_cell == NIL) |
994 | { |
948 | { |
995 | #if USE_ERROR_CHECKING |
949 | #if USE_ERROR_CHECKING |
996 | SCHEME_V->no_memory = 1; |
950 | SCHEME_V->no_memory = 1; |
997 | return S_SINK; |
951 | return S_SINK; |
998 | #endif |
952 | #endif |
… | |
… | |
1008 | --SCHEME_V->fcells; |
962 | --SCHEME_V->fcells; |
1009 | return x; |
963 | return x; |
1010 | } |
964 | } |
1011 | } |
965 | } |
1012 | |
966 | |
1013 | /* make sure that there is a given number of cells free */ |
|
|
1014 | static pointer |
|
|
1015 | reserve_cells (SCHEME_P_ int n) |
|
|
1016 | { |
|
|
1017 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
|
|
1018 | return NIL; |
|
|
1019 | |
|
|
1020 | /* Are there enough cells available? */ |
|
|
1021 | if (SCHEME_V->fcells < n) |
|
|
1022 | { |
|
|
1023 | /* If not, try gc'ing some */ |
|
|
1024 | gc (SCHEME_A_ NIL, NIL); |
|
|
1025 | |
|
|
1026 | if (SCHEME_V->fcells < n) |
|
|
1027 | { |
|
|
1028 | /* If there still aren't, try getting more heap */ |
|
|
1029 | if (!alloc_cellseg (SCHEME_A_ 1) && USE_ERROR_CHECKING) |
|
|
1030 | { |
|
|
1031 | SCHEME_V->no_memory = 1; |
|
|
1032 | return NIL; |
|
|
1033 | } |
|
|
1034 | } |
|
|
1035 | |
|
|
1036 | if (SCHEME_V->fcells < n && USE_ERROR_CHECKING) |
|
|
1037 | { |
|
|
1038 | /* If all fail, report failure */ |
|
|
1039 | SCHEME_V->no_memory = 1; |
|
|
1040 | return NIL; |
|
|
1041 | } |
|
|
1042 | } |
|
|
1043 | |
|
|
1044 | return S_T; |
|
|
1045 | } |
|
|
1046 | |
|
|
1047 | static pointer |
|
|
1048 | get_consecutive_cells (SCHEME_P_ int n) |
|
|
1049 | { |
|
|
1050 | pointer x; |
|
|
1051 | |
|
|
1052 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
|
|
1053 | return S_SINK; |
|
|
1054 | |
|
|
1055 | /* Are there any cells available? */ |
|
|
1056 | x = find_consecutive_cells (SCHEME_A_ n); |
|
|
1057 | |
|
|
1058 | if (x != NIL) |
|
|
1059 | return x; |
|
|
1060 | |
|
|
1061 | /* If not, try gc'ing some */ |
|
|
1062 | gc (SCHEME_A_ NIL, NIL); |
|
|
1063 | |
|
|
1064 | for (;;) |
|
|
1065 | { |
|
|
1066 | x = find_consecutive_cells (SCHEME_A_ n); |
|
|
1067 | |
|
|
1068 | if (x != NIL) |
|
|
1069 | return x; |
|
|
1070 | |
|
|
1071 | /* If there still aren't, try getting more heap */ |
|
|
1072 | if (!alloc_cellseg (SCHEME_A_ 1)) |
|
|
1073 | { |
|
|
1074 | #if USE_ERROR_CHECKING |
|
|
1075 | SCHEME_V->no_memory = 1; |
|
|
1076 | return S_SINK; |
|
|
1077 | #endif |
|
|
1078 | } |
|
|
1079 | } |
|
|
1080 | } |
|
|
1081 | |
|
|
1082 | static int |
|
|
1083 | count_consecutive_cells (pointer x, int needed) |
|
|
1084 | { |
|
|
1085 | int n = 1; |
|
|
1086 | |
|
|
1087 | while (cdr (x) == x + 1) |
|
|
1088 | { |
|
|
1089 | x = cdr (x); |
|
|
1090 | n++; |
|
|
1091 | |
|
|
1092 | if (n >= needed) |
|
|
1093 | break; |
|
|
1094 | } |
|
|
1095 | |
|
|
1096 | return n; |
|
|
1097 | } |
|
|
1098 | |
|
|
1099 | static pointer |
|
|
1100 | find_consecutive_cells (SCHEME_P_ int n) |
|
|
1101 | { |
|
|
1102 | pointer *pp = &SCHEME_V->free_cell; |
|
|
1103 | |
|
|
1104 | while (*pp != NIL) |
|
|
1105 | { |
|
|
1106 | int cnt = count_consecutive_cells (*pp, n); |
|
|
1107 | |
|
|
1108 | if (cnt >= n) |
|
|
1109 | { |
|
|
1110 | pointer x = *pp; |
|
|
1111 | |
|
|
1112 | *pp = cdr (*pp + n - 1); |
|
|
1113 | SCHEME_V->fcells -= n; |
|
|
1114 | return x; |
|
|
1115 | } |
|
|
1116 | |
|
|
1117 | pp = &cdr (*pp + cnt - 1); |
|
|
1118 | } |
|
|
1119 | |
|
|
1120 | return NIL; |
|
|
1121 | } |
|
|
1122 | |
|
|
1123 | /* To retain recent allocs before interpreter knows about them - |
967 | /* To retain recent allocs before interpreter knows about them - |
1124 | Tehom */ |
968 | Tehom */ |
1125 | |
|
|
1126 | static void |
969 | static void |
1127 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
970 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1128 | { |
971 | { |
1129 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
972 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1130 | |
973 | |
… | |
… | |
1150 | |
993 | |
1151 | return cell; |
994 | return cell; |
1152 | } |
995 | } |
1153 | |
996 | |
1154 | static pointer |
997 | static pointer |
1155 | get_vector_object (SCHEME_P_ int len, pointer init) |
998 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1156 | { |
999 | { |
1157 | pointer cells = get_consecutive_cells (SCHEME_A_ len / 2 + len % 2 + 1); |
1000 | pointer v = get_cell_x (SCHEME_A_ NIL, NIL); |
|
|
1001 | pointer *e = malloc (len * sizeof (pointer)); |
1158 | |
1002 | |
1159 | #if USE_ERROR_CHECKING |
1003 | if (!e && USE_ERROR_CHECKING) |
1160 | if (SCHEME_V->no_memory) |
|
|
1161 | return S_SINK; |
1004 | return S_SINK; |
1162 | #endif |
|
|
1163 | |
1005 | |
1164 | /* Record it as a vector so that gc understands it. */ |
1006 | /* Record it as a vector so that gc understands it. */ |
1165 | set_typeflag (cells, T_VECTOR | T_ATOM); |
1007 | set_typeflag (v, T_VECTOR | T_ATOM); |
1166 | ivalue_unchecked (cells) = len; |
1008 | |
1167 | set_num_integer (cells); |
1009 | CELL(v)->object.vector.vvalue = e; |
|
|
1010 | CELL(v)->object.vector.length = len; |
1168 | fill_vector (cells, init); |
1011 | fill_vector (v, 0, init); |
1169 | push_recent_alloc (SCHEME_A_ cells, NIL); |
1012 | push_recent_alloc (SCHEME_A_ v, NIL); |
1170 | |
1013 | |
1171 | return cells; |
1014 | return v; |
1172 | } |
1015 | } |
1173 | |
1016 | |
1174 | static INLINE void |
1017 | ecb_inline void |
1175 | ok_to_freely_gc (SCHEME_P) |
1018 | ok_to_freely_gc (SCHEME_P) |
1176 | { |
1019 | { |
1177 | set_car (S_SINK, NIL); |
1020 | set_car (S_SINK, NIL); |
1178 | } |
1021 | } |
1179 | |
1022 | |
… | |
… | |
1181 | static void |
1024 | static void |
1182 | check_cell_alloced (pointer p, int expect_alloced) |
1025 | check_cell_alloced (pointer p, int expect_alloced) |
1183 | { |
1026 | { |
1184 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1027 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1185 | if (typeflag (p) & !expect_alloced) |
1028 | if (typeflag (p) & !expect_alloced) |
1186 | xwrstr ("Cell is already allocated!\n"); |
1029 | putstr (SCHEME_A_ "Cell is already allocated!\n"); |
1187 | |
1030 | |
1188 | if (!(typeflag (p)) & expect_alloced) |
1031 | if (!(typeflag (p)) & expect_alloced) |
1189 | xwrstr ("Cell is not allocated!\n"); |
1032 | putstr (SCHEME_A_ "Cell is not allocated!\n"); |
1190 | } |
1033 | } |
1191 | |
1034 | |
1192 | static void |
1035 | static void |
1193 | check_range_alloced (pointer p, int n, int expect_alloced) |
1036 | check_range_alloced (pointer p, int n, int expect_alloced) |
1194 | { |
1037 | { |
… | |
… | |
1212 | if (immutable) |
1055 | if (immutable) |
1213 | setimmutable (x); |
1056 | setimmutable (x); |
1214 | |
1057 | |
1215 | set_car (x, a); |
1058 | set_car (x, a); |
1216 | set_cdr (x, b); |
1059 | set_cdr (x, b); |
|
|
1060 | |
1217 | return x; |
1061 | return x; |
1218 | } |
1062 | } |
1219 | |
1063 | |
|
|
1064 | static pointer |
|
|
1065 | generate_symbol (SCHEME_P_ const char *name) |
|
|
1066 | { |
|
|
1067 | pointer x = mk_string (SCHEME_A_ name); |
|
|
1068 | setimmutable (x); |
|
|
1069 | set_typeflag (x, T_SYMBOL | T_ATOM); |
|
|
1070 | return x; |
|
|
1071 | } |
|
|
1072 | |
1220 | /* ========== oblist implementation ========== */ |
1073 | /* ========== oblist implementation ========== */ |
1221 | |
1074 | |
1222 | #ifndef USE_OBJECT_LIST |
1075 | #ifndef USE_OBJECT_LIST |
1223 | |
1076 | |
|
|
1077 | static int |
1224 | static int hash_fn (const char *key, int table_size); |
1078 | hash_fn (const char *key, int table_size) |
|
|
1079 | { |
|
|
1080 | const unsigned char *p = key; |
|
|
1081 | uint32_t hash = 2166136261; |
|
|
1082 | |
|
|
1083 | while (*p) |
|
|
1084 | hash = (hash ^ *p++) * 16777619; |
|
|
1085 | |
|
|
1086 | return hash % table_size; |
|
|
1087 | } |
1225 | |
1088 | |
1226 | static pointer |
1089 | static pointer |
1227 | oblist_initial_value (SCHEME_P) |
1090 | oblist_initial_value (SCHEME_P) |
1228 | { |
1091 | { |
1229 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
1092 | return mk_vector (SCHEME_A_ 461); /* probably should be bigger */ |
… | |
… | |
1231 | |
1094 | |
1232 | /* returns the new symbol */ |
1095 | /* returns the new symbol */ |
1233 | static pointer |
1096 | static pointer |
1234 | oblist_add_by_name (SCHEME_P_ const char *name) |
1097 | oblist_add_by_name (SCHEME_P_ const char *name) |
1235 | { |
1098 | { |
1236 | int location; |
1099 | pointer x = generate_symbol (SCHEME_A_ name); |
1237 | |
|
|
1238 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1239 | set_typeflag (x, T_SYMBOL); |
|
|
1240 | setimmutable (car (x)); |
|
|
1241 | |
|
|
1242 | location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); |
1100 | int location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1243 | set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); |
1101 | vector_set (SCHEME_V->oblist, location, immutable_cons (x, vector_get (SCHEME_V->oblist, location))); |
1244 | return x; |
1102 | return x; |
1245 | } |
1103 | } |
1246 | |
1104 | |
1247 | static INLINE pointer |
1105 | ecb_inline pointer |
1248 | oblist_find_by_name (SCHEME_P_ const char *name) |
1106 | oblist_find_by_name (SCHEME_P_ const char *name) |
1249 | { |
1107 | { |
1250 | int location; |
1108 | int location; |
1251 | pointer x; |
1109 | pointer x; |
1252 | char *s; |
1110 | char *s; |
1253 | |
1111 | |
1254 | location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); |
1112 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1255 | |
1113 | |
1256 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1114 | for (x = vector_get (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1257 | { |
1115 | { |
1258 | s = symname (car (x)); |
1116 | s = symname (car (x)); |
1259 | |
1117 | |
1260 | /* case-insensitive, per R5RS section 2 */ |
1118 | /* case-insensitive, per R5RS section 2 */ |
1261 | if (stricmp (name, s) == 0) |
1119 | if (stricmp (name, s) == 0) |
… | |
… | |
1270 | { |
1128 | { |
1271 | int i; |
1129 | int i; |
1272 | pointer x; |
1130 | pointer x; |
1273 | pointer ob_list = NIL; |
1131 | pointer ob_list = NIL; |
1274 | |
1132 | |
1275 | for (i = 0; i < ivalue_unchecked (SCHEME_V->oblist); i++) |
1133 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1276 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1134 | for (x = vector_get (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1277 | ob_list = cons (x, ob_list); |
1135 | ob_list = cons (x, ob_list); |
1278 | |
1136 | |
1279 | return ob_list; |
1137 | return ob_list; |
1280 | } |
1138 | } |
1281 | |
1139 | |
… | |
… | |
1285 | oblist_initial_value (SCHEME_P) |
1143 | oblist_initial_value (SCHEME_P) |
1286 | { |
1144 | { |
1287 | return NIL; |
1145 | return NIL; |
1288 | } |
1146 | } |
1289 | |
1147 | |
1290 | static INLINE pointer |
1148 | ecb_inline pointer |
1291 | oblist_find_by_name (SCHEME_P_ const char *name) |
1149 | oblist_find_by_name (SCHEME_P_ const char *name) |
1292 | { |
1150 | { |
1293 | pointer x; |
1151 | pointer x; |
1294 | char *s; |
1152 | char *s; |
1295 | |
1153 | |
… | |
… | |
1307 | |
1165 | |
1308 | /* returns the new symbol */ |
1166 | /* returns the new symbol */ |
1309 | static pointer |
1167 | static pointer |
1310 | oblist_add_by_name (SCHEME_P_ const char *name) |
1168 | oblist_add_by_name (SCHEME_P_ const char *name) |
1311 | { |
1169 | { |
1312 | pointer x; |
1170 | pointer x = generate_symbol (SCHEME_A_ name); |
1313 | |
|
|
1314 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
|
|
1315 | set_typeflag (x, T_SYMBOL); |
|
|
1316 | setimmutable (car (x)); |
|
|
1317 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1171 | SCHEME_V->oblist = immutable_cons (x, SCHEME_V->oblist); |
1318 | return x; |
1172 | return x; |
1319 | } |
1173 | } |
1320 | |
1174 | |
1321 | static pointer |
1175 | static pointer |
… | |
… | |
1331 | mk_port (SCHEME_P_ port *p) |
1185 | mk_port (SCHEME_P_ port *p) |
1332 | { |
1186 | { |
1333 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1187 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1334 | |
1188 | |
1335 | set_typeflag (x, T_PORT | T_ATOM); |
1189 | set_typeflag (x, T_PORT | T_ATOM); |
1336 | x->object.port = p; |
1190 | set_port (x, p); |
1337 | |
1191 | |
1338 | return x; |
1192 | return x; |
1339 | } |
1193 | } |
1340 | #endif |
1194 | #endif |
1341 | |
1195 | |
1342 | pointer |
1196 | pointer |
1343 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1197 | mk_foreign_func (SCHEME_P_ foreign_func f) |
1344 | { |
1198 | { |
1345 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1199 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1346 | |
1200 | |
1347 | set_typeflag (x, (T_FOREIGN | T_ATOM)); |
1201 | set_typeflag (x, T_FOREIGN | T_ATOM); |
1348 | x->object.ff = f; |
1202 | CELL(x)->object.ff = f; |
1349 | |
1203 | |
1350 | return x; |
1204 | return x; |
1351 | } |
1205 | } |
1352 | |
1206 | |
1353 | INTERFACE pointer |
1207 | INTERFACE pointer |
1354 | mk_character (SCHEME_P_ int c) |
1208 | mk_character (SCHEME_P_ int c) |
1355 | { |
1209 | { |
1356 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1210 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1357 | |
1211 | |
1358 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1212 | set_typeflag (x, T_CHARACTER | T_ATOM); |
1359 | ivalue_unchecked (x) = c & 0xff; |
1213 | set_ivalue (x, c & 0xff); |
1360 | set_num_integer (x); |
1214 | |
1361 | return x; |
1215 | return x; |
1362 | } |
1216 | } |
1363 | |
1217 | |
1364 | /* get number atom (integer) */ |
1218 | /* get number atom (integer) */ |
1365 | INTERFACE pointer |
1219 | INTERFACE pointer |
1366 | mk_integer (SCHEME_P_ long num) |
1220 | mk_integer (SCHEME_P_ long n) |
1367 | { |
1221 | { |
|
|
1222 | pointer p = 0; |
|
|
1223 | pointer *pp = &p; |
|
|
1224 | |
|
|
1225 | #if USE_INTCACHE |
|
|
1226 | if (n >= INTCACHE_MIN && n <= INTCACHE_MAX) |
|
|
1227 | pp = &SCHEME_V->intcache[n - INTCACHE_MIN]; |
|
|
1228 | #endif |
|
|
1229 | |
|
|
1230 | if (!*pp) |
|
|
1231 | { |
1368 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1232 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1369 | |
1233 | |
1370 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1234 | set_typeflag (x, T_INTEGER | T_ATOM); |
1371 | ivalue_unchecked (x) = num; |
1235 | setimmutable (x); /* shouldn't do anythi9ng, doesn't cost anything */ |
1372 | set_num_integer (x); |
1236 | set_ivalue (x, n); |
|
|
1237 | |
|
|
1238 | *pp = x; |
|
|
1239 | } |
|
|
1240 | |
1373 | return x; |
1241 | return *pp; |
1374 | } |
1242 | } |
1375 | |
1243 | |
1376 | INTERFACE pointer |
1244 | INTERFACE pointer |
1377 | mk_real (SCHEME_P_ RVALUE n) |
1245 | mk_real (SCHEME_P_ RVALUE n) |
1378 | { |
1246 | { |
|
|
1247 | #if USE_REAL |
1379 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1248 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1380 | |
1249 | |
1381 | set_typeflag (x, (T_NUMBER | T_ATOM)); |
1250 | set_typeflag (x, T_REAL | T_ATOM); |
1382 | rvalue_unchecked (x) = n; |
1251 | set_rvalue (x, n); |
1383 | set_num_real (x); |
1252 | |
1384 | return x; |
1253 | return x; |
|
|
1254 | #else |
|
|
1255 | return mk_integer (SCHEME_A_ n); |
|
|
1256 | #endif |
1385 | } |
1257 | } |
1386 | |
1258 | |
1387 | static pointer |
1259 | static pointer |
1388 | mk_number (SCHEME_P_ const num n) |
1260 | mk_number (SCHEME_P_ const num n) |
1389 | { |
1261 | { |
|
|
1262 | #if USE_REAL |
1390 | if (num_is_fixnum (n)) |
1263 | return num_is_fixnum (n) |
|
|
1264 | ? mk_integer (SCHEME_A_ num_ivalue (n)) |
|
|
1265 | : mk_real (SCHEME_A_ num_rvalue (n)); |
|
|
1266 | #else |
1391 | return mk_integer (SCHEME_A_ num_get_ivalue (n)); |
1267 | return mk_integer (SCHEME_A_ num_ivalue (n)); |
1392 | else |
1268 | #endif |
1393 | return mk_real (SCHEME_A_ num_get_rvalue (n)); |
|
|
1394 | } |
1269 | } |
1395 | |
1270 | |
1396 | /* allocate name to string area */ |
1271 | /* allocate name to string area */ |
1397 | static char * |
1272 | static char * |
1398 | store_string (SCHEME_P_ int len_str, const char *str, char fill) |
1273 | store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) |
1399 | { |
1274 | { |
1400 | char *q = malloc (len_str + 1); |
1275 | char *q = malloc (len_str + 1); |
1401 | |
1276 | |
1402 | if (q == 0 && USE_ERROR_CHECKING) |
1277 | if (q == 0 && USE_ERROR_CHECKING) |
1403 | { |
1278 | { |
1404 | SCHEME_V->no_memory = 1; |
1279 | SCHEME_V->no_memory = 1; |
1405 | return SCHEME_V->strbuff; |
1280 | return SCHEME_V->strbuff; |
1406 | } |
1281 | } |
1407 | |
1282 | |
1408 | if (str) |
1283 | if (str) |
1409 | { |
1284 | memcpy (q, str , len_str); /* caller must ensure that *str has length len_str */ |
1410 | int l = strlen (str); |
|
|
1411 | |
|
|
1412 | if (l > len_str) |
|
|
1413 | l = len_str; |
|
|
1414 | |
|
|
1415 | memcpy (q, str, l); |
|
|
1416 | q[l] = 0; |
|
|
1417 | } |
|
|
1418 | else |
1285 | else |
1419 | { |
|
|
1420 | memset (q, fill, len_str); |
1286 | memset (q, fill, len_str); |
|
|
1287 | |
1421 | q[len_str] = 0; |
1288 | q[len_str] = 0; |
1422 | } |
|
|
1423 | |
1289 | |
1424 | return q; |
1290 | return q; |
1425 | } |
1291 | } |
1426 | |
1292 | |
1427 | /* get new string */ |
|
|
1428 | INTERFACE pointer |
1293 | INTERFACE pointer |
1429 | mk_string (SCHEME_P_ const char *str) |
|
|
1430 | { |
|
|
1431 | return mk_counted_string (SCHEME_A_ str, strlen (str)); |
|
|
1432 | } |
|
|
1433 | |
|
|
1434 | INTERFACE pointer |
|
|
1435 | mk_counted_string (SCHEME_P_ const char *str, int len) |
|
|
1436 | { |
|
|
1437 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
|
|
1438 | |
|
|
1439 | set_typeflag (x, T_STRING | T_ATOM); |
|
|
1440 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
|
|
1441 | strlength (x) = len; |
|
|
1442 | return x; |
|
|
1443 | } |
|
|
1444 | |
|
|
1445 | INTERFACE pointer |
|
|
1446 | mk_empty_string (SCHEME_P_ int len, char fill) |
1294 | mk_empty_string (SCHEME_P_ uint32_t len, char fill) |
1447 | { |
1295 | { |
1448 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1296 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1449 | |
1297 | |
1450 | set_typeflag (x, T_STRING | T_ATOM); |
1298 | set_typeflag (x, T_STRING | T_ATOM); |
1451 | strvalue (x) = store_string (SCHEME_A_ len, 0, fill); |
1299 | strvalue (x) = store_string (SCHEME_A_ len, 0, fill); |
1452 | strlength (x) = len; |
1300 | strlength (x) = len; |
1453 | return x; |
1301 | return x; |
1454 | } |
1302 | } |
1455 | |
1303 | |
1456 | INTERFACE pointer |
1304 | INTERFACE pointer |
|
|
1305 | mk_counted_string (SCHEME_P_ const char *str, uint32_t len) |
|
|
1306 | { |
|
|
1307 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
|
|
1308 | |
|
|
1309 | set_typeflag (x, T_STRING | T_ATOM); |
|
|
1310 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
|
|
1311 | strlength (x) = len; |
|
|
1312 | |
|
|
1313 | return x; |
|
|
1314 | } |
|
|
1315 | |
|
|
1316 | INTERFACE pointer |
|
|
1317 | mk_string (SCHEME_P_ const char *str) |
|
|
1318 | { |
|
|
1319 | return mk_counted_string (SCHEME_A_ str, strlen (str)); |
|
|
1320 | } |
|
|
1321 | |
|
|
1322 | INTERFACE pointer |
1457 | mk_vector (SCHEME_P_ int len) |
1323 | mk_vector (SCHEME_P_ uint32_t len) |
1458 | { |
1324 | { |
1459 | return get_vector_object (SCHEME_A_ len, NIL); |
1325 | return get_vector_object (SCHEME_A_ len, NIL); |
1460 | } |
1326 | } |
1461 | |
1327 | |
1462 | INTERFACE void |
1328 | INTERFACE void |
1463 | fill_vector (pointer vec, pointer obj) |
1329 | fill_vector (pointer vec, uint32_t start, pointer obj) |
1464 | { |
1330 | { |
1465 | int i; |
1331 | int i; |
1466 | int num = ivalue (vec) / 2 + ivalue (vec) % 2; |
|
|
1467 | |
1332 | |
1468 | for (i = 0; i < num; i++) |
1333 | for (i = start; i < veclength (vec); i++) |
1469 | { |
1334 | vecvalue (vec)[i] = obj; |
1470 | set_typeflag (vec + 1 + i, T_PAIR); |
1335 | } |
1471 | setimmutable (vec + 1 + i); |
1336 | |
1472 | set_car (vec + 1 + i, obj); |
1337 | INTERFACE void |
1473 | set_cdr (vec + 1 + i, obj); |
1338 | vector_resize (pointer vec, uint32_t newsize, pointer fill) |
1474 | } |
1339 | { |
|
|
1340 | uint32_t oldsize = veclength (vec); |
|
|
1341 | vecvalue (vec) = realloc (vecvalue (vec), newsize * sizeof (pointer)); |
|
|
1342 | veclength (vec) = newsize; |
|
|
1343 | fill_vector (vec, oldsize, fill); |
1475 | } |
1344 | } |
1476 | |
1345 | |
1477 | INTERFACE pointer |
1346 | INTERFACE pointer |
1478 | vector_elem (pointer vec, int ielem) |
1347 | vector_get (pointer vec, uint32_t ielem) |
1479 | { |
1348 | { |
1480 | int n = ielem / 2; |
1349 | return vecvalue(vec)[ielem]; |
1481 | |
|
|
1482 | if (ielem % 2 == 0) |
|
|
1483 | return car (vec + 1 + n); |
|
|
1484 | else |
|
|
1485 | return cdr (vec + 1 + n); |
|
|
1486 | } |
1350 | } |
1487 | |
1351 | |
1488 | INTERFACE void |
1352 | INTERFACE void |
1489 | set_vector_elem (pointer vec, int ielem, pointer a) |
1353 | vector_set (pointer vec, uint32_t ielem, pointer a) |
1490 | { |
1354 | { |
1491 | int n = ielem / 2; |
1355 | vecvalue(vec)[ielem] = a; |
1492 | |
|
|
1493 | if (ielem % 2 == 0) |
|
|
1494 | set_car (vec + 1 + n, a); |
|
|
1495 | else |
|
|
1496 | set_cdr (vec + 1 + n, a); |
|
|
1497 | } |
1356 | } |
1498 | |
1357 | |
1499 | /* get new symbol */ |
1358 | /* get new symbol */ |
1500 | INTERFACE pointer |
1359 | INTERFACE pointer |
1501 | mk_symbol (SCHEME_P_ const char *name) |
1360 | mk_symbol (SCHEME_P_ const char *name) |
… | |
… | |
1511 | |
1370 | |
1512 | INTERFACE pointer |
1371 | INTERFACE pointer |
1513 | gensym (SCHEME_P) |
1372 | gensym (SCHEME_P) |
1514 | { |
1373 | { |
1515 | pointer x; |
1374 | pointer x; |
1516 | char name[40]; |
1375 | char name[40] = "gensym-"; |
1517 | |
|
|
1518 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
|
|
1519 | { |
|
|
1520 | strcpy (name, "gensym-"); |
|
|
1521 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1376 | xnum (name + 7, ++SCHEME_V->gensym_cnt); |
1522 | |
1377 | |
1523 | /* first check oblist */ |
1378 | return generate_symbol (SCHEME_A_ name); |
1524 | x = oblist_find_by_name (SCHEME_A_ name); |
1379 | } |
1525 | |
1380 | |
1526 | if (x != NIL) |
1381 | static int |
1527 | continue; |
1382 | is_gensym (SCHEME_P_ pointer x) |
1528 | else |
1383 | { |
1529 | { |
1384 | return is_symbol (x) && oblist_find_by_name (SCHEME_A_ strvalue (x)) != x; |
1530 | x = oblist_add_by_name (SCHEME_A_ name); |
|
|
1531 | return x; |
|
|
1532 | } |
|
|
1533 | } |
|
|
1534 | |
|
|
1535 | return NIL; |
|
|
1536 | } |
1385 | } |
1537 | |
1386 | |
1538 | /* make symbol or number atom from string */ |
1387 | /* make symbol or number atom from string */ |
1539 | static pointer |
1388 | static pointer |
1540 | mk_atom (SCHEME_P_ char *q) |
1389 | mk_atom (SCHEME_P_ char *q) |
… | |
… | |
1542 | char c, *p; |
1391 | char c, *p; |
1543 | int has_dec_point = 0; |
1392 | int has_dec_point = 0; |
1544 | int has_fp_exp = 0; |
1393 | int has_fp_exp = 0; |
1545 | |
1394 | |
1546 | #if USE_COLON_HOOK |
1395 | #if USE_COLON_HOOK |
1547 | |
|
|
1548 | if ((p = strstr (q, "::")) != 0) |
1396 | if ((p = strstr (q, "::")) != 0) |
1549 | { |
1397 | { |
1550 | *p = 0; |
1398 | *p = 0; |
1551 | return cons (SCHEME_V->COLON_HOOK, |
1399 | return cons (SCHEME_V->COLON_HOOK, |
1552 | cons (cons (SCHEME_V->QUOTE, |
1400 | cons (cons (SCHEME_V->QUOTE, |
1553 | cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL))); |
1401 | cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL))); |
1554 | } |
1402 | } |
1555 | |
|
|
1556 | #endif |
1403 | #endif |
1557 | |
1404 | |
1558 | p = q; |
1405 | p = q; |
1559 | c = *p++; |
1406 | c = *p++; |
1560 | |
1407 | |
… | |
… | |
1596 | } |
1443 | } |
1597 | else if ((c == 'e') || (c == 'E')) |
1444 | else if ((c == 'e') || (c == 'E')) |
1598 | { |
1445 | { |
1599 | if (!has_fp_exp) |
1446 | if (!has_fp_exp) |
1600 | { |
1447 | { |
1601 | has_dec_point = 1; /* decimal point illegal |
1448 | has_dec_point = 1; /* decimal point illegal from now on */ |
1602 | from now on */ |
|
|
1603 | p++; |
1449 | p++; |
1604 | |
1450 | |
1605 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1451 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1606 | continue; |
1452 | continue; |
1607 | } |
1453 | } |
… | |
… | |
1609 | |
1455 | |
1610 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1456 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1611 | } |
1457 | } |
1612 | } |
1458 | } |
1613 | |
1459 | |
1614 | #if USE_FLOAT |
1460 | #if USE_REAL |
1615 | if (has_dec_point) |
1461 | if (has_dec_point) |
1616 | return mk_real (SCHEME_A_ atof (q)); |
1462 | return mk_real (SCHEME_A_ atof (q)); |
1617 | #endif |
1463 | #endif |
1618 | |
1464 | |
1619 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1465 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
… | |
… | |
1621 | |
1467 | |
1622 | /* make constant */ |
1468 | /* make constant */ |
1623 | static pointer |
1469 | static pointer |
1624 | mk_sharp_const (SCHEME_P_ char *name) |
1470 | mk_sharp_const (SCHEME_P_ char *name) |
1625 | { |
1471 | { |
1626 | long x; |
|
|
1627 | char tmp[STRBUFFSIZE]; |
|
|
1628 | |
|
|
1629 | if (!strcmp (name, "t")) |
1472 | if (!strcmp (name, "t")) |
1630 | return S_T; |
1473 | return S_T; |
1631 | else if (!strcmp (name, "f")) |
1474 | else if (!strcmp (name, "f")) |
1632 | return S_F; |
1475 | return S_F; |
1633 | else if (*name == 'o') /* #o (octal) */ |
|
|
1634 | { |
|
|
1635 | x = strtol (name + 1, 0, 8); |
|
|
1636 | return mk_integer (SCHEME_A_ x); |
|
|
1637 | } |
|
|
1638 | else if (*name == 'd') /* #d (decimal) */ |
|
|
1639 | { |
|
|
1640 | x = strtol (name + 1, 0, 10); |
|
|
1641 | return mk_integer (SCHEME_A_ x); |
|
|
1642 | } |
|
|
1643 | else if (*name == 'x') /* #x (hex) */ |
|
|
1644 | { |
|
|
1645 | x = strtol (name + 1, 0, 16); |
|
|
1646 | return mk_integer (SCHEME_A_ x); |
|
|
1647 | } |
|
|
1648 | else if (*name == 'b') /* #b (binary) */ |
|
|
1649 | { |
|
|
1650 | x = binary_decode (name + 1); |
|
|
1651 | return mk_integer (SCHEME_A_ x); |
|
|
1652 | } |
|
|
1653 | else if (*name == '\\') /* #\w (character) */ |
1476 | else if (*name == '\\') /* #\w (character) */ |
1654 | { |
1477 | { |
1655 | int c = 0; |
1478 | int c; |
1656 | |
1479 | |
|
|
1480 | // TODO: optimise |
1657 | if (stricmp (name + 1, "space") == 0) |
1481 | if (stricmp (name + 1, "space") == 0) |
1658 | c = ' '; |
1482 | c = ' '; |
1659 | else if (stricmp (name + 1, "newline") == 0) |
1483 | else if (stricmp (name + 1, "newline") == 0) |
1660 | c = '\n'; |
1484 | c = '\n'; |
1661 | else if (stricmp (name + 1, "return") == 0) |
1485 | else if (stricmp (name + 1, "return") == 0) |
1662 | c = '\r'; |
1486 | c = '\r'; |
1663 | else if (stricmp (name + 1, "tab") == 0) |
1487 | else if (stricmp (name + 1, "tab") == 0) |
1664 | c = '\t'; |
1488 | c = '\t'; |
|
|
1489 | else if (stricmp (name + 1, "alarm") == 0) |
|
|
1490 | c = 0x07; |
|
|
1491 | else if (stricmp (name + 1, "backspace") == 0) |
|
|
1492 | c = 0x08; |
|
|
1493 | else if (stricmp (name + 1, "escape") == 0) |
|
|
1494 | c = 0x1b; |
|
|
1495 | else if (stricmp (name + 1, "delete") == 0) |
|
|
1496 | c = 0x7f; |
|
|
1497 | else if (stricmp (name + 1, "null") == 0) |
|
|
1498 | c = 0; |
1665 | else if (name[1] == 'x' && name[2] != 0) |
1499 | else if (name[1] == 'x' && name[2] != 0) |
1666 | { |
1500 | { |
1667 | int c1 = strtol (name + 2, 0, 16); |
1501 | long c1 = strtol (name + 2, 0, 16); |
1668 | |
1502 | |
1669 | if (c1 <= UCHAR_MAX) |
1503 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1670 | c = c1; |
1504 | c = c1; |
1671 | else |
1505 | else |
1672 | return NIL; |
1506 | return NIL; |
1673 | |
1507 | } |
1674 | #if USE_ASCII_NAMES |
1508 | #if USE_ASCII_NAMES |
1675 | } |
|
|
1676 | else if (is_ascii_name (name + 1, &c)) |
1509 | else if (is_ascii_name (name + 1, &c)) |
1677 | { |
|
|
1678 | /* nothing */ |
1510 | /* nothing */; |
1679 | #endif |
1511 | #endif |
1680 | } |
|
|
1681 | else if (name[2] == 0) |
1512 | else if (name[2] == 0) |
1682 | c = name[1]; |
1513 | c = name[1]; |
1683 | else |
1514 | else |
1684 | return NIL; |
1515 | return NIL; |
1685 | |
1516 | |
1686 | return mk_character (SCHEME_A_ c); |
1517 | return mk_character (SCHEME_A_ c); |
1687 | } |
1518 | } |
1688 | else |
1519 | else |
|
|
1520 | { |
|
|
1521 | /* identify base by string index */ |
|
|
1522 | const char baseidx[17] = "ffbf" "ffff" "ofdf" "ffff" "x"; |
|
|
1523 | char *base = strchr (baseidx, *name); |
|
|
1524 | |
|
|
1525 | if (base) |
|
|
1526 | return mk_integer (SCHEME_A_ strtol (name + 1, 0, base - baseidx)); |
|
|
1527 | |
1689 | return NIL; |
1528 | return NIL; |
|
|
1529 | } |
1690 | } |
1530 | } |
1691 | |
1531 | |
1692 | /* ========== garbage collector ========== */ |
1532 | /* ========== garbage collector ========== */ |
1693 | |
1533 | |
1694 | /*-- |
1534 | /*-- |
1695 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1535 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1696 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1536 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1697 | * for marking. |
1537 | * for marking. |
|
|
1538 | * |
|
|
1539 | * The exception is vectors - vectors are currently marked recursively, |
|
|
1540 | * which is inherited form tinyscheme and could be fixed by having another |
|
|
1541 | * word of context in the vector |
1698 | */ |
1542 | */ |
1699 | static void |
1543 | static void |
1700 | mark (pointer a) |
1544 | mark (pointer a) |
1701 | { |
1545 | { |
1702 | pointer t, q, p; |
1546 | pointer t, q, p; |
… | |
… | |
1704 | t = 0; |
1548 | t = 0; |
1705 | p = a; |
1549 | p = a; |
1706 | E2: |
1550 | E2: |
1707 | setmark (p); |
1551 | setmark (p); |
1708 | |
1552 | |
1709 | if (is_vector (p)) |
1553 | if (ecb_expect_false (is_vector (p))) |
1710 | { |
1554 | { |
1711 | int i; |
1555 | int i; |
1712 | int num = ivalue_unchecked (p) / 2 + ivalue_unchecked (p) % 2; |
|
|
1713 | |
1556 | |
1714 | for (i = 0; i < num; i++) |
1557 | for (i = 0; i < veclength (p); i++) |
1715 | { |
1558 | mark (vecvalue (p)[i]); |
1716 | /* Vector cells will be treated like ordinary cells */ |
|
|
1717 | mark (p + 1 + i); |
|
|
1718 | } |
|
|
1719 | } |
1559 | } |
1720 | |
1560 | |
1721 | if (is_atom (p)) |
1561 | if (is_atom (p)) |
1722 | goto E6; |
1562 | goto E6; |
1723 | |
1563 | |
… | |
… | |
1769 | |
1609 | |
1770 | /* garbage collection. parameter a, b is marked. */ |
1610 | /* garbage collection. parameter a, b is marked. */ |
1771 | static void |
1611 | static void |
1772 | gc (SCHEME_P_ pointer a, pointer b) |
1612 | gc (SCHEME_P_ pointer a, pointer b) |
1773 | { |
1613 | { |
1774 | pointer p; |
|
|
1775 | int i; |
1614 | int i; |
1776 | |
1615 | |
1777 | if (SCHEME_V->gc_verbose) |
1616 | if (SCHEME_V->gc_verbose) |
1778 | putstr (SCHEME_A_ "gc..."); |
1617 | putstr (SCHEME_A_ "gc..."); |
1779 | |
1618 | |
… | |
… | |
1795 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1634 | /* Mark recent objects the interpreter doesn't know about yet. */ |
1796 | mark (car (S_SINK)); |
1635 | mark (car (S_SINK)); |
1797 | /* Mark any older stuff above nested C calls */ |
1636 | /* Mark any older stuff above nested C calls */ |
1798 | mark (SCHEME_V->c_nest); |
1637 | mark (SCHEME_V->c_nest); |
1799 | |
1638 | |
|
|
1639 | #if USE_INTCACHE |
|
|
1640 | /* mark intcache */ |
|
|
1641 | for (i = INTCACHE_MIN; i <= INTCACHE_MAX; ++i) |
|
|
1642 | if (SCHEME_V->intcache[i - INTCACHE_MIN]) |
|
|
1643 | mark (SCHEME_V->intcache[i - INTCACHE_MIN]); |
|
|
1644 | #endif |
|
|
1645 | |
1800 | /* mark variables a, b */ |
1646 | /* mark variables a, b */ |
1801 | mark (a); |
1647 | mark (a); |
1802 | mark (b); |
1648 | mark (b); |
1803 | |
1649 | |
1804 | /* garbage collect */ |
1650 | /* garbage collect */ |
1805 | clrmark (NIL); |
1651 | clrmark (NIL); |
1806 | SCHEME_V->fcells = 0; |
1652 | SCHEME_V->fcells = 0; |
1807 | SCHEME_V->free_cell = NIL; |
1653 | SCHEME_V->free_cell = NIL; |
1808 | |
1654 | |
1809 | /* free-list is kept sorted by address so as to maintain consecutive |
1655 | if (SCHEME_V->gc_verbose) |
1810 | ranges, if possible, for use with vectors. Here we scan the cells |
1656 | putstr (SCHEME_A_ "freeing..."); |
1811 | (which are also kept sorted by address) downwards to build the |
1657 | |
1812 | free-list in sorted order. |
1658 | uint32_t total = 0; |
1813 | */ |
1659 | |
|
|
1660 | /* Here we scan the cells to build the free-list. */ |
1814 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1661 | for (i = SCHEME_V->last_cell_seg; i >= 0; i--) |
1815 | { |
1662 | { |
1816 | p = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
1663 | struct cell *end = SCHEME_V->cell_seg[i] + SCHEME_V->cell_segsize [i]; |
|
|
1664 | struct cell *p; |
|
|
1665 | total += SCHEME_V->cell_segsize [i]; |
1817 | |
1666 | |
1818 | while (--p >= SCHEME_V->cell_seg[i]) |
1667 | for (p = SCHEME_V->cell_seg[i]; p < end; ++p) |
1819 | { |
1668 | { |
|
|
1669 | pointer c = POINTER (p); |
|
|
1670 | |
1820 | if (is_mark (p)) |
1671 | if (is_mark (c)) |
1821 | clrmark (p); |
1672 | clrmark (c); |
1822 | else |
1673 | else |
1823 | { |
1674 | { |
1824 | /* reclaim cell */ |
1675 | /* reclaim cell */ |
1825 | if (typeflag (p) != T_FREE) |
1676 | if (typeflag (c) != T_PAIR) |
1826 | { |
1677 | { |
1827 | finalize_cell (SCHEME_A_ p); |
1678 | finalize_cell (SCHEME_A_ c); |
1828 | set_typeflag (p, T_FREE); |
1679 | set_typeflag (c, T_PAIR); |
1829 | set_car (p, NIL); |
1680 | set_car (c, NIL); |
1830 | } |
1681 | } |
1831 | |
1682 | |
1832 | ++SCHEME_V->fcells; |
1683 | ++SCHEME_V->fcells; |
1833 | set_cdr (p, SCHEME_V->free_cell); |
1684 | set_cdr (c, SCHEME_V->free_cell); |
1834 | SCHEME_V->free_cell = p; |
1685 | SCHEME_V->free_cell = c; |
1835 | } |
1686 | } |
1836 | } |
1687 | } |
1837 | } |
1688 | } |
1838 | |
1689 | |
1839 | if (SCHEME_V->gc_verbose) |
1690 | if (SCHEME_V->gc_verbose) |
1840 | xwrstr ("done: "); xwrnum (SCHEME_V->fcells); xwrstr (" cells were recovered.\n"); |
1691 | { |
|
|
1692 | 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"); |
|
|
1693 | } |
1841 | } |
1694 | } |
1842 | |
1695 | |
1843 | static void |
1696 | static void |
1844 | finalize_cell (SCHEME_P_ pointer a) |
1697 | finalize_cell (SCHEME_P_ pointer a) |
1845 | { |
1698 | { |
1846 | if (is_string (a)) |
1699 | /* TODO, fast bitmap check? */ |
|
|
1700 | if (is_string (a) || is_symbol (a)) |
1847 | free (strvalue (a)); |
1701 | free (strvalue (a)); |
|
|
1702 | else if (is_vector (a)) |
|
|
1703 | free (vecvalue (a)); |
1848 | #if USE_PORTS |
1704 | #if USE_PORTS |
1849 | else if (is_port (a)) |
1705 | else if (is_port (a)) |
1850 | { |
1706 | { |
1851 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1707 | if (port(a)->kind & port_file && port (a)->rep.stdio.closeit) |
1852 | port_close (SCHEME_A_ a, port_input | port_output); |
1708 | port_close (SCHEME_A_ a, port_input | port_output); |
1853 | |
1709 | |
1854 | free (a->object.port); |
1710 | free (port (a)); |
1855 | } |
1711 | } |
1856 | #endif |
1712 | #endif |
1857 | } |
1713 | } |
1858 | |
1714 | |
1859 | /* ========== Routines for Reading ========== */ |
1715 | /* ========== Routines for Reading ========== */ |
… | |
… | |
1875 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1731 | SCHEME_V->load_stack[SCHEME_V->file_i].unget = -1; |
1876 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1732 | SCHEME_V->load_stack[SCHEME_V->file_i].kind = port_file | port_input; |
1877 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1733 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.file = fin; |
1878 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1734 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.closeit = 1; |
1879 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1735 | SCHEME_V->nesting_stack[SCHEME_V->file_i] = 0; |
1880 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1736 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1881 | |
1737 | |
1882 | #if SHOW_ERROR_LINE |
1738 | #if SHOW_ERROR_LINE |
1883 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1739 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line = 0; |
1884 | |
1740 | |
1885 | if (fname) |
1741 | if (fname) |
… | |
… | |
1902 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1758 | SCHEME_V->nesting = SCHEME_V->nesting_stack[SCHEME_V->file_i]; |
1903 | #if USE_PORTS |
1759 | #if USE_PORTS |
1904 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1760 | port_close (SCHEME_A_ SCHEME_V->loadport, port_input); |
1905 | #endif |
1761 | #endif |
1906 | SCHEME_V->file_i--; |
1762 | SCHEME_V->file_i--; |
1907 | SCHEME_V->loadport->object.port = SCHEME_V->load_stack + SCHEME_V->file_i; |
1763 | set_port (SCHEME_V->loadport, SCHEME_V->load_stack + SCHEME_V->file_i); |
1908 | } |
1764 | } |
1909 | } |
1765 | } |
1910 | |
1766 | |
1911 | static int |
1767 | static int |
1912 | file_interactive (SCHEME_P) |
1768 | file_interactive (SCHEME_P) |
1913 | { |
1769 | { |
1914 | #if USE_PORTS |
1770 | #if USE_PORTS |
1915 | return SCHEME_V->file_i == 0 |
1771 | return SCHEME_V->file_i == 0 |
1916 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1772 | && SCHEME_V->load_stack[0].rep.stdio.file == STDIN_FILENO |
1917 | && (SCHEME_V->inport->object.port->kind & port_file); |
1773 | && (port (SCHEME_V->inport)->kind & port_file); |
1918 | #else |
1774 | #else |
1919 | return 0; |
1775 | return 0; |
1920 | #endif |
1776 | #endif |
1921 | } |
1777 | } |
1922 | |
1778 | |
… | |
… | |
2056 | } |
1912 | } |
2057 | |
1913 | |
2058 | static void |
1914 | static void |
2059 | port_close (SCHEME_P_ pointer p, int flag) |
1915 | port_close (SCHEME_P_ pointer p, int flag) |
2060 | { |
1916 | { |
2061 | port *pt = p->object.port; |
1917 | port *pt = port (p); |
2062 | |
1918 | |
2063 | pt->kind &= ~flag; |
1919 | pt->kind &= ~flag; |
2064 | |
1920 | |
2065 | if ((pt->kind & (port_input | port_output)) == 0) |
1921 | if ((pt->kind & (port_input | port_output)) == 0) |
2066 | { |
1922 | { |
… | |
… | |
2087 | /* get new character from input file */ |
1943 | /* get new character from input file */ |
2088 | static int |
1944 | static int |
2089 | inchar (SCHEME_P) |
1945 | inchar (SCHEME_P) |
2090 | { |
1946 | { |
2091 | int c; |
1947 | int c; |
2092 | port *pt; |
1948 | port *pt = port (SCHEME_V->inport); |
2093 | |
|
|
2094 | pt = SCHEME_V->inport->object.port; |
|
|
2095 | |
1949 | |
2096 | if (pt->kind & port_saw_EOF) |
1950 | if (pt->kind & port_saw_EOF) |
2097 | return EOF; |
1951 | return EOF; |
2098 | |
1952 | |
2099 | c = basic_inchar (pt); |
1953 | c = basic_inchar (pt); |
… | |
… | |
2166 | port *pt; |
2020 | port *pt; |
2167 | |
2021 | |
2168 | if (c == EOF) |
2022 | if (c == EOF) |
2169 | return; |
2023 | return; |
2170 | |
2024 | |
2171 | pt = SCHEME_V->inport->object.port; |
2025 | pt = port (SCHEME_V->inport); |
2172 | pt->unget = c; |
2026 | pt->unget = c; |
2173 | #else |
2027 | #else |
2174 | if (c == EOF) |
2028 | if (c == EOF) |
2175 | return; |
2029 | return; |
2176 | |
2030 | |
… | |
… | |
2204 | |
2058 | |
2205 | INTERFACE void |
2059 | INTERFACE void |
2206 | putstr (SCHEME_P_ const char *s) |
2060 | putstr (SCHEME_P_ const char *s) |
2207 | { |
2061 | { |
2208 | #if USE_PORTS |
2062 | #if USE_PORTS |
2209 | port *pt = SCHEME_V->outport->object.port; |
2063 | port *pt = port (SCHEME_V->outport); |
2210 | |
2064 | |
2211 | if (pt->kind & port_file) |
2065 | if (pt->kind & port_file) |
2212 | write (pt->rep.stdio.file, s, strlen (s)); |
2066 | write (pt->rep.stdio.file, s, strlen (s)); |
2213 | else |
2067 | else |
2214 | for (; *s; s++) |
2068 | for (; *s; s++) |
… | |
… | |
2216 | *pt->rep.string.curr++ = *s; |
2070 | *pt->rep.string.curr++ = *s; |
2217 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2071 | else if (pt->kind & port_srfi6 && realloc_port_string (SCHEME_A_ pt)) |
2218 | *pt->rep.string.curr++ = *s; |
2072 | *pt->rep.string.curr++ = *s; |
2219 | |
2073 | |
2220 | #else |
2074 | #else |
2221 | xwrstr (s); |
2075 | write (pt->rep.stdio.file, s, strlen (s)); |
2222 | #endif |
2076 | #endif |
2223 | } |
2077 | } |
2224 | |
2078 | |
2225 | static void |
2079 | static void |
2226 | putchars (SCHEME_P_ const char *s, int len) |
2080 | putchars (SCHEME_P_ const char *s, int len) |
2227 | { |
2081 | { |
2228 | #if USE_PORTS |
2082 | #if USE_PORTS |
2229 | port *pt = SCHEME_V->outport->object.port; |
2083 | port *pt = port (SCHEME_V->outport); |
2230 | |
2084 | |
2231 | if (pt->kind & port_file) |
2085 | if (pt->kind & port_file) |
2232 | write (pt->rep.stdio.file, s, len); |
2086 | write (pt->rep.stdio.file, s, len); |
2233 | else |
2087 | else |
2234 | { |
2088 | { |
… | |
… | |
2248 | |
2102 | |
2249 | INTERFACE void |
2103 | INTERFACE void |
2250 | putcharacter (SCHEME_P_ int c) |
2104 | putcharacter (SCHEME_P_ int c) |
2251 | { |
2105 | { |
2252 | #if USE_PORTS |
2106 | #if USE_PORTS |
2253 | port *pt = SCHEME_V->outport->object.port; |
2107 | port *pt = port (SCHEME_V->outport); |
2254 | |
2108 | |
2255 | if (pt->kind & port_file) |
2109 | if (pt->kind & port_file) |
2256 | { |
2110 | { |
2257 | char cc = c; |
2111 | char cc = c; |
2258 | write (pt->rep.stdio.file, &cc, 1); |
2112 | write (pt->rep.stdio.file, &cc, 1); |
… | |
… | |
2271 | #endif |
2125 | #endif |
2272 | } |
2126 | } |
2273 | |
2127 | |
2274 | /* read characters up to delimiter, but cater to character constants */ |
2128 | /* read characters up to delimiter, but cater to character constants */ |
2275 | static char * |
2129 | static char * |
2276 | readstr_upto (SCHEME_P_ char *delim) |
2130 | readstr_upto (SCHEME_P_ int skip, const char *delim) |
2277 | { |
2131 | { |
2278 | char *p = SCHEME_V->strbuff; |
2132 | char *p = SCHEME_V->strbuff + skip; |
2279 | |
2133 | |
2280 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2134 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2281 | |
2135 | |
2282 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2136 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2283 | *p = 0; |
2137 | *p = 0; |
… | |
… | |
2290 | return SCHEME_V->strbuff; |
2144 | return SCHEME_V->strbuff; |
2291 | } |
2145 | } |
2292 | |
2146 | |
2293 | /* read string expression "xxx...xxx" */ |
2147 | /* read string expression "xxx...xxx" */ |
2294 | static pointer |
2148 | static pointer |
2295 | readstrexp (SCHEME_P) |
2149 | readstrexp (SCHEME_P_ char delim) |
2296 | { |
2150 | { |
2297 | char *p = SCHEME_V->strbuff; |
2151 | char *p = SCHEME_V->strbuff; |
2298 | int c; |
2152 | int c; |
2299 | int c1 = 0; |
2153 | int c1 = 0; |
2300 | enum |
|
|
2301 | { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2154 | enum { st_ok, st_bsl, st_x1, st_x2, st_oct1, st_oct2 } state = st_ok; |
2302 | |
2155 | |
2303 | for (;;) |
2156 | for (;;) |
2304 | { |
2157 | { |
2305 | c = inchar (SCHEME_A); |
2158 | c = inchar (SCHEME_A); |
2306 | |
2159 | |
… | |
… | |
2308 | return S_F; |
2161 | return S_F; |
2309 | |
2162 | |
2310 | switch (state) |
2163 | switch (state) |
2311 | { |
2164 | { |
2312 | case st_ok: |
2165 | case st_ok: |
2313 | switch (c) |
2166 | if (ecb_expect_false (c == delim)) |
2314 | { |
|
|
2315 | case '\\': |
|
|
2316 | state = st_bsl; |
|
|
2317 | break; |
|
|
2318 | |
|
|
2319 | case '"': |
|
|
2320 | *p = 0; |
|
|
2321 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2167 | return mk_counted_string (SCHEME_A_ SCHEME_V->strbuff, p - SCHEME_V->strbuff); |
2322 | |
2168 | |
2323 | default: |
2169 | if (ecb_expect_false (c == '\\')) |
|
|
2170 | state = st_bsl; |
|
|
2171 | else |
2324 | *p++ = c; |
2172 | *p++ = c; |
2325 | break; |
|
|
2326 | } |
|
|
2327 | |
2173 | |
2328 | break; |
2174 | break; |
2329 | |
2175 | |
2330 | case st_bsl: |
2176 | case st_bsl: |
2331 | switch (c) |
2177 | switch (c) |
… | |
… | |
2340 | case '7': |
2186 | case '7': |
2341 | state = st_oct1; |
2187 | state = st_oct1; |
2342 | c1 = c - '0'; |
2188 | c1 = c - '0'; |
2343 | break; |
2189 | break; |
2344 | |
2190 | |
|
|
2191 | case 'a': *p++ = '\a'; state = st_ok; break; |
|
|
2192 | case 'n': *p++ = '\n'; state = st_ok; break; |
|
|
2193 | case 'r': *p++ = '\r'; state = st_ok; break; |
|
|
2194 | case 't': *p++ = '\t'; state = st_ok; break; |
|
|
2195 | |
|
|
2196 | //TODO: \whitespace eol whitespace |
|
|
2197 | |
|
|
2198 | //TODO: x should end in ;, not two-digit hex |
2345 | case 'x': |
2199 | case 'x': |
2346 | case 'X': |
2200 | case 'X': |
2347 | state = st_x1; |
2201 | state = st_x1; |
2348 | c1 = 0; |
2202 | c1 = 0; |
2349 | break; |
2203 | break; |
2350 | |
2204 | |
2351 | case 'n': |
|
|
2352 | *p++ = '\n'; |
|
|
2353 | state = st_ok; |
|
|
2354 | break; |
|
|
2355 | |
|
|
2356 | case 't': |
|
|
2357 | *p++ = '\t'; |
|
|
2358 | state = st_ok; |
|
|
2359 | break; |
|
|
2360 | |
|
|
2361 | case 'r': |
|
|
2362 | *p++ = '\r'; |
|
|
2363 | state = st_ok; |
|
|
2364 | break; |
|
|
2365 | |
|
|
2366 | case '"': |
|
|
2367 | *p++ = '"'; |
|
|
2368 | state = st_ok; |
|
|
2369 | break; |
|
|
2370 | |
|
|
2371 | default: |
2205 | default: |
2372 | *p++ = c; |
2206 | *p++ = c; |
2373 | state = st_ok; |
2207 | state = st_ok; |
2374 | break; |
2208 | break; |
2375 | } |
2209 | } |
2376 | |
2210 | |
2377 | break; |
2211 | break; |
2378 | |
2212 | |
2379 | case st_x1: |
2213 | case st_x1: |
2380 | case st_x2: |
2214 | case st_x2: |
2381 | c = toupper (c); |
2215 | c = tolower (c); |
2382 | |
2216 | |
2383 | if (c >= '0' && c <= 'F') |
2217 | if (c >= '0' && c <= '9') |
2384 | { |
|
|
2385 | if (c <= '9') |
|
|
2386 | c1 = (c1 << 4) + c - '0'; |
2218 | c1 = (c1 << 4) + c - '0'; |
2387 | else |
2219 | else if (c >= 'a' && c <= 'f') |
2388 | c1 = (c1 << 4) + c - 'A' + 10; |
2220 | c1 = (c1 << 4) + c - 'a' + 10; |
2389 | |
|
|
2390 | if (state == st_x1) |
|
|
2391 | state = st_x2; |
|
|
2392 | else |
|
|
2393 | { |
|
|
2394 | *p++ = c1; |
|
|
2395 | state = st_ok; |
|
|
2396 | } |
|
|
2397 | } |
|
|
2398 | else |
2221 | else |
2399 | return S_F; |
2222 | return S_F; |
|
|
2223 | |
|
|
2224 | if (state == st_x1) |
|
|
2225 | state = st_x2; |
|
|
2226 | else |
|
|
2227 | { |
|
|
2228 | *p++ = c1; |
|
|
2229 | state = st_ok; |
|
|
2230 | } |
2400 | |
2231 | |
2401 | break; |
2232 | break; |
2402 | |
2233 | |
2403 | case st_oct1: |
2234 | case st_oct1: |
2404 | case st_oct2: |
2235 | case st_oct2: |
… | |
… | |
2408 | backchar (SCHEME_A_ c); |
2239 | backchar (SCHEME_A_ c); |
2409 | state = st_ok; |
2240 | state = st_ok; |
2410 | } |
2241 | } |
2411 | else |
2242 | else |
2412 | { |
2243 | { |
2413 | if (state == st_oct2 && c1 >= 32) |
2244 | if (state == st_oct2 && c1 >= ' ') |
2414 | return S_F; |
2245 | return S_F; |
2415 | |
2246 | |
2416 | c1 = (c1 << 3) + (c - '0'); |
2247 | c1 = (c1 << 3) + (c - '0'); |
2417 | |
2248 | |
2418 | if (state == st_oct1) |
2249 | if (state == st_oct1) |
… | |
… | |
2423 | state = st_ok; |
2254 | state = st_ok; |
2424 | } |
2255 | } |
2425 | } |
2256 | } |
2426 | |
2257 | |
2427 | break; |
2258 | break; |
2428 | |
|
|
2429 | } |
2259 | } |
2430 | } |
2260 | } |
2431 | } |
2261 | } |
2432 | |
2262 | |
2433 | /* check c is in chars */ |
2263 | /* check c is in chars */ |
2434 | static INLINE int |
2264 | ecb_inline int |
2435 | is_one_of (char *s, int c) |
2265 | is_one_of (const char *s, int c) |
2436 | { |
2266 | { |
2437 | if (c == EOF) |
|
|
2438 | return 1; |
|
|
2439 | |
|
|
2440 | return !!strchr (s, c); |
2267 | return c == EOF || !!strchr (s, c); |
2441 | } |
2268 | } |
2442 | |
2269 | |
2443 | /* skip white characters */ |
2270 | /* skip white characters */ |
2444 | static INLINE int |
2271 | ecb_inline int |
2445 | skipspace (SCHEME_P) |
2272 | skipspace (SCHEME_P) |
2446 | { |
2273 | { |
2447 | int c, curr_line = 0; |
2274 | int c, curr_line = 0; |
2448 | |
2275 | |
2449 | do |
2276 | do |
2450 | { |
2277 | { |
2451 | c = inchar (SCHEME_A); |
2278 | c = inchar (SCHEME_A); |
|
|
2279 | |
2452 | #if SHOW_ERROR_LINE |
2280 | #if SHOW_ERROR_LINE |
2453 | if (c == '\n') |
2281 | if (ecb_expect_false (c == '\n')) |
2454 | curr_line++; |
2282 | curr_line++; |
2455 | #endif |
2283 | #endif |
|
|
2284 | |
|
|
2285 | if (ecb_expect_false (c == EOF)) |
|
|
2286 | return c; |
2456 | } |
2287 | } |
2457 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2288 | while (is_one_of (WHITESPACE, c)); |
2458 | |
2289 | |
2459 | /* record it */ |
2290 | /* record it */ |
2460 | #if SHOW_ERROR_LINE |
2291 | #if SHOW_ERROR_LINE |
2461 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2292 | if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
2462 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2293 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line += curr_line; |
2463 | #endif |
2294 | #endif |
2464 | |
2295 | |
2465 | if (c != EOF) |
|
|
2466 | { |
|
|
2467 | backchar (SCHEME_A_ c); |
2296 | backchar (SCHEME_A_ c); |
2468 | return 1; |
2297 | return 1; |
2469 | } |
|
|
2470 | else |
|
|
2471 | return EOF; |
|
|
2472 | } |
2298 | } |
2473 | |
2299 | |
2474 | /* get token */ |
2300 | /* get token */ |
2475 | static int |
2301 | static int |
2476 | token (SCHEME_P) |
2302 | token (SCHEME_P) |
… | |
… | |
2492 | return TOK_RPAREN; |
2318 | return TOK_RPAREN; |
2493 | |
2319 | |
2494 | case '.': |
2320 | case '.': |
2495 | c = inchar (SCHEME_A); |
2321 | c = inchar (SCHEME_A); |
2496 | |
2322 | |
2497 | if (is_one_of (" \n\t", c)) |
2323 | if (is_one_of (WHITESPACE, c)) |
2498 | return TOK_DOT; |
2324 | return TOK_DOT; |
2499 | else |
2325 | else |
2500 | { |
2326 | { |
2501 | //TODO: ungetc twice in a row is not supported in C |
|
|
2502 | backchar (SCHEME_A_ c); |
2327 | backchar (SCHEME_A_ c); |
2503 | backchar (SCHEME_A_ '.'); |
|
|
2504 | return TOK_ATOM; |
2328 | return TOK_DOTATOM; |
2505 | } |
2329 | } |
|
|
2330 | |
|
|
2331 | case '|': |
|
|
2332 | return TOK_STRATOM; |
2506 | |
2333 | |
2507 | case '\'': |
2334 | case '\'': |
2508 | return TOK_QUOTE; |
2335 | return TOK_QUOTE; |
2509 | |
2336 | |
2510 | case ';': |
2337 | case ';': |
… | |
… | |
2642 | } |
2469 | } |
2643 | |
2470 | |
2644 | putcharacter (SCHEME_A_ '"'); |
2471 | putcharacter (SCHEME_A_ '"'); |
2645 | } |
2472 | } |
2646 | |
2473 | |
2647 | |
|
|
2648 | /* print atoms */ |
2474 | /* print atoms */ |
2649 | static void |
2475 | static void |
2650 | printatom (SCHEME_P_ pointer l, int f) |
2476 | printatom (SCHEME_P_ pointer l, int f) |
2651 | { |
2477 | { |
2652 | char *p; |
2478 | char *p; |
2653 | int len; |
2479 | int len; |
2654 | |
2480 | |
2655 | atom2str (SCHEME_A_ l, f, &p, &len); |
2481 | atom2str (SCHEME_A_ l, f, &p, &len); |
2656 | putchars (SCHEME_A_ p, len); |
2482 | putchars (SCHEME_A_ p, len); |
2657 | } |
2483 | } |
2658 | |
|
|
2659 | |
2484 | |
2660 | /* Uses internal buffer unless string pointer is already available */ |
2485 | /* Uses internal buffer unless string pointer is already available */ |
2661 | static void |
2486 | static void |
2662 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2487 | atom2str (SCHEME_P_ pointer l, int f, char **pp, int *plen) |
2663 | { |
2488 | { |
… | |
… | |
2677 | { |
2502 | { |
2678 | p = SCHEME_V->strbuff; |
2503 | p = SCHEME_V->strbuff; |
2679 | |
2504 | |
2680 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2505 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2681 | { |
2506 | { |
2682 | if (num_is_integer (l)) |
2507 | if (is_integer (l)) |
2683 | xnum (p, ivalue_unchecked (l)); |
2508 | xnum (p, ivalue_unchecked (l)); |
2684 | #if USE_FLOAT |
2509 | #if USE_REAL |
2685 | else |
2510 | else |
2686 | { |
2511 | { |
2687 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2512 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2688 | /* r5rs says there must be a '.' (unless 'e'?) */ |
2513 | /* r5rs says there must be a '.' (unless 'e'?) */ |
2689 | f = strcspn (p, ".e"); |
2514 | f = strcspn (p, ".e"); |
… | |
… | |
2824 | #endif |
2649 | #endif |
2825 | } |
2650 | } |
2826 | else if (is_continuation (l)) |
2651 | else if (is_continuation (l)) |
2827 | p = "#<CONTINUATION>"; |
2652 | p = "#<CONTINUATION>"; |
2828 | else |
2653 | else |
|
|
2654 | { |
|
|
2655 | #if USE_PRINTF |
|
|
2656 | p = SCHEME_V->strbuff; |
|
|
2657 | snprintf (p, STRBUFFSIZE, "#<ERROR %x>", (int)typeflag (l)); |
|
|
2658 | #else |
2829 | p = "#<ERROR>"; |
2659 | p = "#<ERROR>"; |
|
|
2660 | #endif |
|
|
2661 | } |
2830 | |
2662 | |
2831 | *pp = p; |
2663 | *pp = p; |
2832 | *plen = strlen (p); |
2664 | *plen = strlen (p); |
2833 | } |
2665 | } |
2834 | |
2666 | |
… | |
… | |
2942 | return 0; |
2774 | return 0; |
2943 | } |
2775 | } |
2944 | else if (is_number (a)) |
2776 | else if (is_number (a)) |
2945 | { |
2777 | { |
2946 | if (is_number (b)) |
2778 | if (is_number (b)) |
2947 | if (num_is_integer (a) == num_is_integer (b)) |
|
|
2948 | return num_eq (nvalue (a), nvalue (b)); |
2779 | return num_cmp (nvalue (a), nvalue (b)) == 0; |
2949 | |
2780 | |
2950 | return 0; |
2781 | return 0; |
2951 | } |
2782 | } |
2952 | else if (is_character (a)) |
2783 | else if (is_character (a)) |
2953 | { |
2784 | { |
… | |
… | |
2979 | /* () is #t in R5RS */ |
2810 | /* () is #t in R5RS */ |
2980 | #define is_true(p) ((p) != S_F) |
2811 | #define is_true(p) ((p) != S_F) |
2981 | #define is_false(p) ((p) == S_F) |
2812 | #define is_false(p) ((p) == S_F) |
2982 | |
2813 | |
2983 | /* ========== Environment implementation ========== */ |
2814 | /* ========== Environment implementation ========== */ |
2984 | |
|
|
2985 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
|
|
2986 | |
|
|
2987 | static int |
|
|
2988 | hash_fn (const char *key, int table_size) |
|
|
2989 | { |
|
|
2990 | unsigned int hashed = 0; |
|
|
2991 | const char *c; |
|
|
2992 | int bits_per_int = sizeof (unsigned int) * 8; |
|
|
2993 | |
|
|
2994 | for (c = key; *c; c++) |
|
|
2995 | { |
|
|
2996 | /* letters have about 5 bits in them */ |
|
|
2997 | hashed = (hashed << 5) | (hashed >> (bits_per_int - 5)); |
|
|
2998 | hashed ^= *c; |
|
|
2999 | } |
|
|
3000 | |
|
|
3001 | return hashed % table_size; |
|
|
3002 | } |
|
|
3003 | #endif |
|
|
3004 | |
2815 | |
3005 | #ifndef USE_ALIST_ENV |
2816 | #ifndef USE_ALIST_ENV |
3006 | |
2817 | |
3007 | /* |
2818 | /* |
3008 | * In this implementation, each frame of the environment may be |
2819 | * In this implementation, each frame of the environment may be |
… | |
… | |
3025 | |
2836 | |
3026 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
2837 | SCHEME_V->envir = immutable_cons (new_frame, old_env); |
3027 | setenvironment (SCHEME_V->envir); |
2838 | setenvironment (SCHEME_V->envir); |
3028 | } |
2839 | } |
3029 | |
2840 | |
3030 | static INLINE void |
2841 | static uint32_t |
|
|
2842 | sym_hash (pointer sym, uint32_t size) |
|
|
2843 | { |
|
|
2844 | uintptr_t ptr = (uintptr_t)sym; |
|
|
2845 | |
|
|
2846 | #if 0 |
|
|
2847 | /* table size is prime, so why mix */ |
|
|
2848 | ptr += ptr >> 32; |
|
|
2849 | ptr += ptr >> 16; |
|
|
2850 | ptr += ptr >> 8; |
|
|
2851 | #endif |
|
|
2852 | |
|
|
2853 | return ptr % size; |
|
|
2854 | } |
|
|
2855 | |
|
|
2856 | ecb_inline void |
3031 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2857 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
3032 | { |
2858 | { |
3033 | pointer slot = immutable_cons (variable, value); |
2859 | pointer slot = immutable_cons (variable, value); |
3034 | |
2860 | |
3035 | if (is_vector (car (env))) |
2861 | if (is_vector (car (env))) |
3036 | { |
2862 | { |
3037 | int location = hash_fn (symname (variable), ivalue_unchecked (car (env))); |
2863 | int location = sym_hash (variable, veclength (car (env))); |
3038 | |
|
|
3039 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2864 | vector_set (car (env), location, immutable_cons (slot, vector_get (car (env), location))); |
3040 | } |
2865 | } |
3041 | else |
2866 | else |
3042 | set_car (env, immutable_cons (slot, car (env))); |
2867 | set_car (env, immutable_cons (slot, car (env))); |
3043 | } |
2868 | } |
3044 | |
2869 | |
3045 | static pointer |
2870 | static pointer |
3046 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
2871 | find_slot_in_env (SCHEME_P_ pointer env, pointer hdl, int all) |
3047 | { |
2872 | { |
3048 | pointer x, y; |
2873 | pointer x, y; |
3049 | int location; |
|
|
3050 | |
2874 | |
3051 | for (x = env; x != NIL; x = cdr (x)) |
2875 | for (x = env; x != NIL; x = cdr (x)) |
3052 | { |
2876 | { |
3053 | if (is_vector (car (x))) |
2877 | if (is_vector (car (x))) |
3054 | { |
2878 | { |
3055 | location = hash_fn (symname (hdl), ivalue_unchecked (car (x))); |
2879 | int location = sym_hash (hdl, veclength (car (x))); |
3056 | y = vector_elem (car (x), location); |
2880 | y = vector_get (car (x), location); |
3057 | } |
2881 | } |
3058 | else |
2882 | else |
3059 | y = car (x); |
2883 | y = car (x); |
3060 | |
2884 | |
3061 | for (; y != NIL; y = cdr (y)) |
2885 | for (; y != NIL; y = cdr (y)) |
3062 | if (caar (y) == hdl) |
2886 | if (caar (y) == hdl) |
3063 | break; |
2887 | break; |
3064 | |
2888 | |
3065 | if (y != NIL) |
2889 | if (y != NIL) |
|
|
2890 | return car (y); |
|
|
2891 | |
|
|
2892 | if (!all) |
3066 | break; |
2893 | break; |
3067 | |
|
|
3068 | if (!all) |
|
|
3069 | return NIL; |
|
|
3070 | } |
2894 | } |
3071 | |
|
|
3072 | if (x != NIL) |
|
|
3073 | return car (y); |
|
|
3074 | |
2895 | |
3075 | return NIL; |
2896 | return NIL; |
3076 | } |
2897 | } |
3077 | |
2898 | |
3078 | #else /* USE_ALIST_ENV */ |
2899 | #else /* USE_ALIST_ENV */ |
3079 | |
2900 | |
3080 | static INLINE void |
2901 | ecb_inline void |
3081 | new_frame_in_env (SCHEME_P_ pointer old_env) |
2902 | new_frame_in_env (SCHEME_P_ pointer old_env) |
3082 | { |
2903 | { |
3083 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
2904 | SCHEME_V->envir = immutable_cons (NIL, old_env); |
3084 | setenvironment (SCHEME_V->envir); |
2905 | setenvironment (SCHEME_V->envir); |
3085 | } |
2906 | } |
3086 | |
2907 | |
3087 | static INLINE void |
2908 | ecb_inline void |
3088 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
2909 | new_slot_spec_in_env (SCHEME_P_ pointer env, pointer variable, pointer value) |
3089 | { |
2910 | { |
3090 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
2911 | set_car (env, immutable_cons (immutable_cons (variable, value), car (env))); |
3091 | } |
2912 | } |
3092 | |
2913 | |
… | |
… | |
3100 | for (y = car (x); y != NIL; y = cdr (y)) |
2921 | for (y = car (x); y != NIL; y = cdr (y)) |
3101 | if (caar (y) == hdl) |
2922 | if (caar (y) == hdl) |
3102 | break; |
2923 | break; |
3103 | |
2924 | |
3104 | if (y != NIL) |
2925 | if (y != NIL) |
|
|
2926 | return car (y); |
3105 | break; |
2927 | break; |
3106 | |
2928 | |
3107 | if (!all) |
2929 | if (!all) |
3108 | return NIL; |
2930 | break; |
3109 | } |
2931 | } |
3110 | |
|
|
3111 | if (x != NIL) |
|
|
3112 | return car (y); |
|
|
3113 | |
2932 | |
3114 | return NIL; |
2933 | return NIL; |
3115 | } |
2934 | } |
3116 | |
2935 | |
3117 | #endif /* USE_ALIST_ENV else */ |
2936 | #endif /* USE_ALIST_ENV else */ |
3118 | |
2937 | |
3119 | static INLINE void |
2938 | ecb_inline void |
3120 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
2939 | new_slot_in_env (SCHEME_P_ pointer variable, pointer value) |
3121 | { |
2940 | { |
|
|
2941 | assert (is_symbol (variable));//TODO: bug in current-ws/OP_LET2 |
3122 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
2942 | new_slot_spec_in_env (SCHEME_A_ SCHEME_V->envir, variable, value); |
3123 | } |
2943 | } |
3124 | |
2944 | |
3125 | static INLINE void |
2945 | ecb_inline void |
3126 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
2946 | set_slot_in_env (SCHEME_P_ pointer slot, pointer value) |
3127 | { |
2947 | { |
3128 | set_cdr (slot, value); |
2948 | set_cdr (slot, value); |
3129 | } |
2949 | } |
3130 | |
2950 | |
3131 | static INLINE pointer |
2951 | ecb_inline pointer |
3132 | slot_value_in_env (pointer slot) |
2952 | slot_value_in_env (pointer slot) |
3133 | { |
2953 | { |
3134 | return cdr (slot); |
2954 | return cdr (slot); |
3135 | } |
2955 | } |
3136 | |
2956 | |
3137 | /* ========== Evaluation Cycle ========== */ |
2957 | /* ========== Evaluation Cycle ========== */ |
3138 | |
2958 | |
3139 | static pointer |
2959 | static int |
3140 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2960 | xError_1 (SCHEME_P_ const char *s, pointer a) |
3141 | { |
2961 | { |
3142 | #if USE_ERROR_HOOK |
2962 | #if USE_ERROR_HOOK |
3143 | pointer x; |
2963 | pointer x; |
3144 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2964 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3170 | #if USE_ERROR_HOOK |
2990 | #if USE_ERROR_HOOK |
3171 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); |
2991 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); |
3172 | |
2992 | |
3173 | if (x != NIL) |
2993 | if (x != NIL) |
3174 | { |
2994 | { |
3175 | if (a) |
2995 | pointer code = a |
3176 | SCHEME_V->code = cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL); |
2996 | ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) |
3177 | else |
2997 | : NIL; |
3178 | SCHEME_V->code = NIL; |
|
|
3179 | |
2998 | |
3180 | SCHEME_V->code = cons (mk_string (SCHEME_A_ s), SCHEME_V->code); |
2999 | code = cons (mk_string (SCHEME_A_ s), code); |
3181 | setimmutable (car (SCHEME_V->code)); |
3000 | setimmutable (car (code)); |
3182 | SCHEME_V->code = cons (slot_value_in_env (x), SCHEME_V->code); |
3001 | SCHEME_V->code = cons (slot_value_in_env (x), code); |
3183 | SCHEME_V->op = OP_EVAL; |
3002 | SCHEME_V->op = OP_EVAL; |
3184 | |
3003 | |
3185 | return S_T; |
3004 | return 0; |
3186 | } |
3005 | } |
3187 | #endif |
3006 | #endif |
3188 | |
3007 | |
3189 | if (a) |
3008 | if (a) |
3190 | SCHEME_V->args = cons (a, NIL); |
3009 | SCHEME_V->args = cons (a, NIL); |
… | |
… | |
3192 | SCHEME_V->args = NIL; |
3011 | SCHEME_V->args = NIL; |
3193 | |
3012 | |
3194 | SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); |
3013 | SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); |
3195 | setimmutable (car (SCHEME_V->args)); |
3014 | setimmutable (car (SCHEME_V->args)); |
3196 | SCHEME_V->op = OP_ERR0; |
3015 | SCHEME_V->op = OP_ERR0; |
|
|
3016 | |
3197 | return S_T; |
3017 | return 0; |
3198 | } |
3018 | } |
3199 | |
3019 | |
3200 | #define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) |
3020 | #define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) |
3201 | #define Error_0(s) Error_1 (s, 0) |
3021 | #define Error_0(s) Error_1 (s, 0) |
3202 | |
3022 | |
3203 | /* Too small to turn into function */ |
3023 | /* Too small to turn into function */ |
3204 | #define BEGIN do { |
3024 | #define BEGIN do { |
3205 | #define END } while (0) |
3025 | #define END } while (0) |
3206 | #define s_goto(a) BEGIN \ |
3026 | #define s_goto(a) BEGIN \ |
3207 | SCHEME_V->op = a; \ |
3027 | SCHEME_V->op = a; \ |
3208 | return S_T; END |
3028 | return 0; END |
3209 | |
3029 | |
3210 | #define s_return(a) return xs_return (SCHEME_A_ a) |
3030 | #define s_return(a) return xs_return (SCHEME_A_ a) |
3211 | |
3031 | |
3212 | #ifndef USE_SCHEME_STACK |
3032 | #ifndef USE_SCHEME_STACK |
3213 | |
3033 | |
… | |
… | |
3227 | { |
3047 | { |
3228 | int nframes = (uintptr_t)SCHEME_V->dump; |
3048 | int nframes = (uintptr_t)SCHEME_V->dump; |
3229 | struct dump_stack_frame *next_frame; |
3049 | struct dump_stack_frame *next_frame; |
3230 | |
3050 | |
3231 | /* enough room for the next frame? */ |
3051 | /* enough room for the next frame? */ |
3232 | if (nframes >= SCHEME_V->dump_size) |
3052 | if (ecb_expect_false (nframes >= SCHEME_V->dump_size)) |
3233 | { |
3053 | { |
3234 | SCHEME_V->dump_size += STACK_GROWTH; |
3054 | SCHEME_V->dump_size += STACK_GROWTH; |
3235 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3055 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3236 | } |
3056 | } |
3237 | |
3057 | |
3238 | next_frame = SCHEME_V->dump_base + nframes; |
3058 | next_frame = SCHEME_V->dump_base + nframes; |
3239 | |
3059 | |
3240 | next_frame->op = op; |
3060 | next_frame->op = op; |
3241 | next_frame->args = args; |
3061 | next_frame->args = args; |
3242 | next_frame->envir = SCHEME_V->envir; |
3062 | next_frame->envir = SCHEME_V->envir; |
3243 | next_frame->code = code; |
3063 | next_frame->code = code; |
3244 | |
3064 | |
3245 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3065 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3246 | } |
3066 | } |
3247 | |
3067 | |
3248 | static pointer |
3068 | static int |
3249 | xs_return (SCHEME_P_ pointer a) |
3069 | xs_return (SCHEME_P_ pointer a) |
3250 | { |
3070 | { |
3251 | int nframes = (uintptr_t)SCHEME_V->dump; |
3071 | int nframes = (uintptr_t)SCHEME_V->dump; |
3252 | struct dump_stack_frame *frame; |
3072 | struct dump_stack_frame *frame; |
3253 | |
3073 | |
3254 | SCHEME_V->value = a; |
3074 | SCHEME_V->value = a; |
3255 | |
3075 | |
3256 | if (nframes <= 0) |
3076 | if (nframes <= 0) |
3257 | return NIL; |
3077 | return -1; |
3258 | |
3078 | |
3259 | frame = &SCHEME_V->dump_base[--nframes]; |
3079 | frame = &SCHEME_V->dump_base[--nframes]; |
3260 | SCHEME_V->op = frame->op; |
3080 | SCHEME_V->op = frame->op; |
3261 | SCHEME_V->args = frame->args; |
3081 | SCHEME_V->args = frame->args; |
3262 | SCHEME_V->envir = frame->envir; |
3082 | SCHEME_V->envir = frame->envir; |
3263 | SCHEME_V->code = frame->code; |
3083 | SCHEME_V->code = frame->code; |
3264 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3084 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3265 | |
3085 | |
3266 | return S_T; |
3086 | return 0; |
3267 | } |
3087 | } |
3268 | |
3088 | |
3269 | static INLINE void |
3089 | ecb_inline void |
3270 | dump_stack_reset (SCHEME_P) |
3090 | dump_stack_reset (SCHEME_P) |
3271 | { |
3091 | { |
3272 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3092 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3273 | SCHEME_V->dump = (pointer)+0; |
3093 | SCHEME_V->dump = (pointer)+0; |
3274 | } |
3094 | } |
3275 | |
3095 | |
3276 | static INLINE void |
3096 | ecb_inline void |
3277 | dump_stack_initialize (SCHEME_P) |
3097 | dump_stack_initialize (SCHEME_P) |
3278 | { |
3098 | { |
3279 | SCHEME_V->dump_size = 0; |
3099 | SCHEME_V->dump_size = 0; |
3280 | SCHEME_V->dump_base = 0; |
3100 | SCHEME_V->dump_base = 0; |
3281 | dump_stack_reset (SCHEME_A); |
3101 | dump_stack_reset (SCHEME_A); |
… | |
… | |
3334 | int i = 0; |
3154 | int i = 0; |
3335 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3155 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3336 | |
3156 | |
3337 | while (cont != NIL) |
3157 | while (cont != NIL) |
3338 | { |
3158 | { |
3339 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3159 | frame->op = ivalue_unchecked (car (cont)); cont = cdr (cont); |
3340 | frame->args = car (cont) ; cont = cdr (cont); |
3160 | frame->args = car (cont) ; cont = cdr (cont); |
3341 | frame->envir = car (cont) ; cont = cdr (cont); |
3161 | frame->envir = car (cont) ; cont = cdr (cont); |
3342 | frame->code = car (cont) ; cont = cdr (cont); |
3162 | frame->code = car (cont) ; cont = cdr (cont); |
3343 | |
3163 | |
3344 | ++frame; |
3164 | ++frame; |
3345 | ++i; |
3165 | ++i; |
3346 | } |
3166 | } |
3347 | |
3167 | |
3348 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3168 | SCHEME_V->dump = (pointer)(uintptr_t)i; |
3349 | } |
3169 | } |
3350 | |
3170 | |
3351 | #else |
3171 | #else |
3352 | |
3172 | |
3353 | static INLINE void |
3173 | ecb_inline void |
3354 | dump_stack_reset (SCHEME_P) |
3174 | dump_stack_reset (SCHEME_P) |
3355 | { |
3175 | { |
3356 | SCHEME_V->dump = NIL; |
3176 | SCHEME_V->dump = NIL; |
3357 | } |
3177 | } |
3358 | |
3178 | |
3359 | static INLINE void |
3179 | ecb_inline void |
3360 | dump_stack_initialize (SCHEME_P) |
3180 | dump_stack_initialize (SCHEME_P) |
3361 | { |
3181 | { |
3362 | dump_stack_reset (SCHEME_A); |
3182 | dump_stack_reset (SCHEME_A); |
3363 | } |
3183 | } |
3364 | |
3184 | |
… | |
… | |
3366 | dump_stack_free (SCHEME_P) |
3186 | dump_stack_free (SCHEME_P) |
3367 | { |
3187 | { |
3368 | SCHEME_V->dump = NIL; |
3188 | SCHEME_V->dump = NIL; |
3369 | } |
3189 | } |
3370 | |
3190 | |
3371 | static pointer |
3191 | static int |
3372 | xs_return (SCHEME_P_ pointer a) |
3192 | xs_return (SCHEME_P_ pointer a) |
3373 | { |
3193 | { |
3374 | pointer dump = SCHEME_V->dump; |
3194 | pointer dump = SCHEME_V->dump; |
3375 | |
3195 | |
3376 | SCHEME_V->value = a; |
3196 | SCHEME_V->value = a; |
3377 | |
3197 | |
3378 | if (dump == NIL) |
3198 | if (dump == NIL) |
3379 | return NIL; |
3199 | return -1; |
3380 | |
3200 | |
3381 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3201 | SCHEME_V->op = ivalue_unchecked (car (dump)); dump = cdr (dump); |
3382 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3202 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3383 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3203 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3384 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3204 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3385 | |
3205 | |
3386 | SCHEME_V->dump = dump; |
3206 | SCHEME_V->dump = dump; |
3387 | |
3207 | |
3388 | return S_T; |
3208 | return 0; |
3389 | } |
3209 | } |
3390 | |
3210 | |
3391 | static void |
3211 | static void |
3392 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3212 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3393 | { |
3213 | { |
… | |
… | |
3418 | |
3238 | |
3419 | #endif |
3239 | #endif |
3420 | |
3240 | |
3421 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3241 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3422 | |
3242 | |
|
|
3243 | #if EXPERIMENT |
3423 | static pointer |
3244 | static int |
|
|
3245 | debug (SCHEME_P_ int indent, pointer x) |
|
|
3246 | { |
|
|
3247 | int c; |
|
|
3248 | |
|
|
3249 | if (is_syntax (x)) |
|
|
3250 | { |
|
|
3251 | printf ("%*ssyntax<%s,%d>\n", indent, "", syntaxname(x),syntaxnum(x)); |
|
|
3252 | return 8 + 8; |
|
|
3253 | } |
|
|
3254 | |
|
|
3255 | if (x == NIL) |
|
|
3256 | { |
|
|
3257 | printf ("%*sNIL\n", indent, ""); |
|
|
3258 | return 3; |
|
|
3259 | } |
|
|
3260 | |
|
|
3261 | switch (type (x)) |
|
|
3262 | { |
|
|
3263 | case T_INTEGER: |
|
|
3264 | printf ("%*sI<%d>%p\n", indent, "", (int)ivalue_unchecked (x), x); |
|
|
3265 | return 32+8; |
|
|
3266 | |
|
|
3267 | case T_SYMBOL: |
|
|
3268 | printf ("%*sS<%s>\n", indent, "", symname (x)); |
|
|
3269 | return 24+8; |
|
|
3270 | |
|
|
3271 | case T_CLOSURE: |
|
|
3272 | printf ("%*sS<%s>\n", indent, "", "closure"); |
|
|
3273 | debug (SCHEME_A_ indent + 3, cdr(x)); |
|
|
3274 | return 32 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3275 | |
|
|
3276 | case T_PAIR: |
|
|
3277 | printf ("%*spair %p %p\n", indent, "", car(x),cdr(x)); |
|
|
3278 | c = debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3279 | c += debug (SCHEME_A_ indent + 3, cdr (x)); |
|
|
3280 | return c + 1; |
|
|
3281 | |
|
|
3282 | case T_PORT: |
|
|
3283 | printf ("%*sS<%s>\n", indent, "", "port"); |
|
|
3284 | return 24+8; |
|
|
3285 | |
|
|
3286 | case T_VECTOR: |
|
|
3287 | printf ("%*sS<%s>\n", indent, "", "vector"); |
|
|
3288 | return 24+8; |
|
|
3289 | |
|
|
3290 | case T_ENVIRONMENT: |
|
|
3291 | printf ("%*sS<%s>\n", indent, "", "environment"); |
|
|
3292 | return 0 + debug (SCHEME_A_ indent + 3, car (x)); |
|
|
3293 | |
|
|
3294 | default: |
|
|
3295 | printf ("unhandled type %d\n", type (x)); |
|
|
3296 | break; |
|
|
3297 | } |
|
|
3298 | } |
|
|
3299 | #endif |
|
|
3300 | |
|
|
3301 | static int |
3424 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3302 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3425 | { |
3303 | { |
|
|
3304 | pointer args = SCHEME_V->args; |
3426 | pointer x, y; |
3305 | pointer x, y; |
3427 | |
3306 | |
3428 | switch (op) |
3307 | switch (op) |
3429 | { |
3308 | { |
|
|
3309 | #if EXPERIMENT //D |
|
|
3310 | case OP_DEBUG: |
|
|
3311 | printf ("len = %d\n", debug (SCHEME_A_ 0, args) / 8); |
|
|
3312 | printf ("\n"); |
|
|
3313 | s_return (S_T); |
|
|
3314 | #endif |
3430 | case OP_LOAD: /* load */ |
3315 | case OP_LOAD: /* load */ |
3431 | if (file_interactive (SCHEME_A)) |
3316 | if (file_interactive (SCHEME_A)) |
3432 | { |
3317 | { |
3433 | xwrstr ("Loading "); xwrstr (strvalue (car (SCHEME_V->args))); xwrstr ("\n"); |
3318 | putstr (SCHEME_A_ "Loading "); putstr (SCHEME_A_ strvalue (car (args))); putstr (SCHEME_A_ "\n"); |
3434 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (SCHEME_V->args))); |
3319 | //D fprintf (port (SCHEME_V->outport)->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3435 | } |
3320 | } |
3436 | |
3321 | |
3437 | if (!file_push (SCHEME_A_ strvalue (car (SCHEME_V->args)))) |
3322 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3438 | Error_1 ("unable to open", car (SCHEME_V->args)); |
3323 | Error_1 ("unable to open", car (args)); |
3439 | else |
3324 | else |
3440 | { |
3325 | { |
3441 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3326 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3442 | s_goto (OP_T0LVL); |
3327 | s_goto (OP_T0LVL); |
3443 | } |
3328 | } |
3444 | |
3329 | |
3445 | case OP_T0LVL: /* top level */ |
3330 | case OP_T0LVL: /* top level */ |
3446 | |
3331 | |
3447 | /* If we reached the end of file, this loop is done. */ |
3332 | /* If we reached the end of file, this loop is done. */ |
3448 | if (SCHEME_V->loadport->object.port->kind & port_saw_EOF) |
3333 | if (port (SCHEME_V->loadport)->kind & port_saw_EOF) |
3449 | { |
3334 | { |
3450 | if (SCHEME_V->file_i == 0) |
3335 | if (SCHEME_V->file_i == 0) |
3451 | { |
3336 | { |
3452 | SCHEME_V->args = NIL; |
3337 | SCHEME_V->args = NIL; |
3453 | s_goto (OP_QUIT); |
3338 | s_goto (OP_QUIT); |
… | |
… | |
3478 | s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); |
3363 | s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); |
3479 | s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); |
3364 | s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); |
3480 | s_goto (OP_READ_INTERNAL); |
3365 | s_goto (OP_READ_INTERNAL); |
3481 | |
3366 | |
3482 | case OP_T1LVL: /* top level */ |
3367 | case OP_T1LVL: /* top level */ |
3483 | SCHEME_V->code = SCHEME_V->value; |
3368 | SCHEME_V->code = SCHEME_V->value; |
3484 | SCHEME_V->inport = SCHEME_V->save_inport; |
3369 | SCHEME_V->inport = SCHEME_V->save_inport; |
3485 | s_goto (OP_EVAL); |
3370 | s_goto (OP_EVAL); |
3486 | |
3371 | |
3487 | case OP_READ_INTERNAL: /* internal read */ |
3372 | case OP_READ_INTERNAL: /* internal read */ |
3488 | SCHEME_V->tok = token (SCHEME_A); |
3373 | SCHEME_V->tok = token (SCHEME_A); |
… | |
… | |
3517 | case OP_EVAL: /* main part of evaluation */ |
3402 | case OP_EVAL: /* main part of evaluation */ |
3518 | #if USE_TRACING |
3403 | #if USE_TRACING |
3519 | if (SCHEME_V->tracing) |
3404 | if (SCHEME_V->tracing) |
3520 | { |
3405 | { |
3521 | /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ |
3406 | /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ |
3522 | s_save (SCHEME_A_ OP_REAL_EVAL, SCHEME_V->args, SCHEME_V->code); |
3407 | s_save (SCHEME_A_ OP_REAL_EVAL, args, SCHEME_V->code); |
3523 | SCHEME_V->args = SCHEME_V->code; |
3408 | SCHEME_V->args = SCHEME_V->code; |
3524 | putstr (SCHEME_A_ "\nEval: "); |
3409 | putstr (SCHEME_A_ "\nEval: "); |
3525 | s_goto (OP_P0LIST); |
3410 | s_goto (OP_P0LIST); |
3526 | } |
3411 | } |
3527 | |
3412 | |
… | |
… | |
3531 | #endif |
3416 | #endif |
3532 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3417 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3533 | { |
3418 | { |
3534 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3419 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3535 | |
3420 | |
3536 | if (x != NIL) |
3421 | if (x == NIL) |
3537 | s_return (slot_value_in_env (x)); |
|
|
3538 | else |
|
|
3539 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3422 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
|
|
3423 | |
|
|
3424 | s_return (slot_value_in_env (x)); |
3540 | } |
3425 | } |
3541 | else if (is_pair (SCHEME_V->code)) |
3426 | else if (is_pair (SCHEME_V->code)) |
3542 | { |
3427 | { |
|
|
3428 | x = car (SCHEME_V->code); |
|
|
3429 | |
3543 | if (is_syntax (x = car (SCHEME_V->code))) /* SYNTAX */ |
3430 | if (is_syntax (x)) /* SYNTAX */ |
3544 | { |
3431 | { |
3545 | SCHEME_V->code = cdr (SCHEME_V->code); |
3432 | SCHEME_V->code = cdr (SCHEME_V->code); |
3546 | s_goto (syntaxnum (x)); |
3433 | s_goto (syntaxnum (x)); |
3547 | } |
3434 | } |
3548 | else /* first, eval top element and eval arguments */ |
3435 | else /* first, eval top element and eval arguments */ |
3549 | { |
3436 | { |
3550 | s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code); |
3437 | s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code); |
3551 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3438 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3552 | SCHEME_V->code = car (SCHEME_V->code); |
3439 | SCHEME_V->code = x; |
3553 | s_goto (OP_EVAL); |
3440 | s_goto (OP_EVAL); |
3554 | } |
3441 | } |
3555 | } |
3442 | } |
3556 | else |
3443 | else |
3557 | s_return (SCHEME_V->code); |
3444 | s_return (SCHEME_V->code); |
3558 | |
3445 | |
3559 | case OP_E0ARGS: /* eval arguments */ |
3446 | case OP_E0ARGS: /* eval arguments */ |
3560 | if (is_macro (SCHEME_V->value)) /* macro expansion */ |
3447 | if (ecb_expect_false (is_macro (SCHEME_V->value))) /* macro expansion */ |
3561 | { |
3448 | { |
3562 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3449 | s_save (SCHEME_A_ OP_DOMACRO, NIL, NIL); |
3563 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3450 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3564 | SCHEME_V->code = SCHEME_V->value; |
3451 | SCHEME_V->code = SCHEME_V->value; |
3565 | s_goto (OP_APPLY); |
3452 | s_goto (OP_APPLY); |
… | |
… | |
3569 | SCHEME_V->code = cdr (SCHEME_V->code); |
3456 | SCHEME_V->code = cdr (SCHEME_V->code); |
3570 | s_goto (OP_E1ARGS); |
3457 | s_goto (OP_E1ARGS); |
3571 | } |
3458 | } |
3572 | |
3459 | |
3573 | case OP_E1ARGS: /* eval arguments */ |
3460 | case OP_E1ARGS: /* eval arguments */ |
3574 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3461 | args = cons (SCHEME_V->value, args); |
3575 | |
3462 | |
3576 | if (is_pair (SCHEME_V->code)) /* continue */ |
3463 | if (is_pair (SCHEME_V->code)) /* continue */ |
3577 | { |
3464 | { |
3578 | s_save (SCHEME_A_ OP_E1ARGS, SCHEME_V->args, cdr (SCHEME_V->code)); |
3465 | s_save (SCHEME_A_ OP_E1ARGS, args, cdr (SCHEME_V->code)); |
3579 | SCHEME_V->code = car (SCHEME_V->code); |
3466 | SCHEME_V->code = car (SCHEME_V->code); |
3580 | SCHEME_V->args = NIL; |
3467 | SCHEME_V->args = NIL; |
3581 | s_goto (OP_EVAL); |
3468 | s_goto (OP_EVAL); |
3582 | } |
3469 | } |
3583 | else /* end */ |
3470 | else /* end */ |
3584 | { |
3471 | { |
3585 | SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); |
3472 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3586 | SCHEME_V->code = car (SCHEME_V->args); |
3473 | SCHEME_V->code = car (args); |
3587 | SCHEME_V->args = cdr (SCHEME_V->args); |
3474 | SCHEME_V->args = cdr (args); |
3588 | s_goto (OP_APPLY); |
3475 | s_goto (OP_APPLY); |
3589 | } |
3476 | } |
3590 | |
3477 | |
3591 | #if USE_TRACING |
3478 | #if USE_TRACING |
3592 | |
3479 | |
3593 | case OP_TRACING: |
3480 | case OP_TRACING: |
3594 | { |
3481 | { |
3595 | int tr = SCHEME_V->tracing; |
3482 | int tr = SCHEME_V->tracing; |
3596 | |
3483 | |
3597 | SCHEME_V->tracing = ivalue (car (SCHEME_V->args)); |
3484 | SCHEME_V->tracing = ivalue_unchecked (car (args)); |
3598 | s_return (mk_integer (SCHEME_A_ tr)); |
3485 | s_return (mk_integer (SCHEME_A_ tr)); |
3599 | } |
3486 | } |
3600 | |
3487 | |
3601 | #endif |
3488 | #endif |
3602 | |
3489 | |
3603 | case OP_APPLY: /* apply 'code' to 'args' */ |
3490 | case OP_APPLY: /* apply 'code' to 'args' */ |
3604 | #if USE_TRACING |
3491 | #if USE_TRACING |
3605 | if (SCHEME_V->tracing) |
3492 | if (SCHEME_V->tracing) |
3606 | { |
3493 | { |
3607 | s_save (SCHEME_A_ OP_REAL_APPLY, SCHEME_V->args, SCHEME_V->code); |
3494 | s_save (SCHEME_A_ OP_REAL_APPLY, args, SCHEME_V->code); |
3608 | SCHEME_V->print_flag = 1; |
3495 | SCHEME_V->print_flag = 1; |
3609 | /* SCHEME_V->args=cons(SCHEME_V->code,SCHEME_V->args); */ |
3496 | /* args=cons(SCHEME_V->code,args); */ |
3610 | putstr (SCHEME_A_ "\nApply to: "); |
3497 | putstr (SCHEME_A_ "\nApply to: "); |
3611 | s_goto (OP_P0LIST); |
3498 | s_goto (OP_P0LIST); |
3612 | } |
3499 | } |
3613 | |
3500 | |
3614 | /* fall through */ |
3501 | /* fall through */ |
3615 | |
3502 | |
3616 | case OP_REAL_APPLY: |
3503 | case OP_REAL_APPLY: |
3617 | #endif |
3504 | #endif |
3618 | if (is_proc (SCHEME_V->code)) |
3505 | if (is_proc (SCHEME_V->code)) |
3619 | { |
|
|
3620 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3506 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3621 | } |
|
|
3622 | else if (is_foreign (SCHEME_V->code)) |
3507 | else if (is_foreign (SCHEME_V->code)) |
3623 | { |
3508 | { |
3624 | /* Keep nested calls from GC'ing the arglist */ |
3509 | /* Keep nested calls from GC'ing the arglist */ |
3625 | push_recent_alloc (SCHEME_A_ SCHEME_V->args, NIL); |
3510 | push_recent_alloc (SCHEME_A_ args, NIL); |
3626 | x = SCHEME_V->code->object.ff (SCHEME_A_ SCHEME_V->args); |
3511 | x = CELL(SCHEME_V->code)->object.ff (SCHEME_A_ args); |
3627 | |
3512 | |
3628 | s_return (x); |
3513 | s_return (x); |
3629 | } |
3514 | } |
3630 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3515 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3631 | { |
3516 | { |
3632 | /* Should not accept promise */ |
3517 | /* Should not accept promise */ |
3633 | /* make environment */ |
3518 | /* make environment */ |
3634 | new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); |
3519 | new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); |
3635 | |
3520 | |
3636 | for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) |
3521 | for (x = car (closure_code (SCHEME_V->code)), y = args; is_pair (x); x = cdr (x), y = cdr (y)) |
3637 | { |
3522 | { |
3638 | if (y == NIL) |
3523 | if (y == NIL) |
3639 | Error_0 ("not enough arguments"); |
3524 | Error_0 ("not enough arguments"); |
3640 | else |
3525 | else |
3641 | new_slot_in_env (SCHEME_A_ car (x), car (y)); |
3526 | new_slot_in_env (SCHEME_A_ car (x), car (y)); |
… | |
… | |
3659 | s_goto (OP_BEGIN); |
3544 | s_goto (OP_BEGIN); |
3660 | } |
3545 | } |
3661 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3546 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3662 | { |
3547 | { |
3663 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3548 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3664 | s_return (SCHEME_V->args != NIL ? car (SCHEME_V->args) : NIL); |
3549 | s_return (args != NIL ? car (args) : NIL); |
3665 | } |
3550 | } |
3666 | else |
3551 | else |
3667 | Error_0 ("illegal function"); |
3552 | Error_0 ("illegal function"); |
3668 | |
3553 | |
3669 | case OP_DOMACRO: /* do macro */ |
3554 | case OP_DOMACRO: /* do macro */ |
3670 | SCHEME_V->code = SCHEME_V->value; |
3555 | SCHEME_V->code = SCHEME_V->value; |
3671 | s_goto (OP_EVAL); |
3556 | s_goto (OP_EVAL); |
3672 | |
|
|
3673 | #if 1 |
|
|
3674 | |
3557 | |
3675 | case OP_LAMBDA: /* lambda */ |
3558 | case OP_LAMBDA: /* lambda */ |
3676 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3559 | /* If the hook is defined, apply it to SCHEME_V->code, otherwise |
3677 | set SCHEME_V->value fall thru */ |
3560 | set SCHEME_V->value fall thru */ |
3678 | { |
3561 | { |
3679 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3562 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3680 | |
3563 | |
3681 | if (f != NIL) |
3564 | if (f != NIL) |
3682 | { |
3565 | { |
3683 | s_save (SCHEME_A_ OP_LAMBDA1, SCHEME_V->args, SCHEME_V->code); |
3566 | s_save (SCHEME_A_ OP_LAMBDA1, args, SCHEME_V->code); |
3684 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3567 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3685 | SCHEME_V->code = slot_value_in_env (f); |
3568 | SCHEME_V->code = slot_value_in_env (f); |
3686 | s_goto (OP_APPLY); |
3569 | s_goto (OP_APPLY); |
3687 | } |
3570 | } |
3688 | |
3571 | |
3689 | SCHEME_V->value = SCHEME_V->code; |
3572 | SCHEME_V->value = SCHEME_V->code; |
3690 | /* Fallthru */ |
|
|
3691 | } |
3573 | } |
|
|
3574 | /* Fallthru */ |
3692 | |
3575 | |
3693 | case OP_LAMBDA1: |
3576 | case OP_LAMBDA1: |
3694 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3577 | s_return (mk_closure (SCHEME_A_ SCHEME_V->value, SCHEME_V->envir)); |
3695 | |
3578 | |
3696 | #else |
|
|
3697 | |
|
|
3698 | case OP_LAMBDA: /* lambda */ |
|
|
3699 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
|
|
3700 | |
|
|
3701 | #endif |
|
|
3702 | |
|
|
3703 | case OP_MKCLOSURE: /* make-closure */ |
3579 | case OP_MKCLOSURE: /* make-closure */ |
3704 | x = car (SCHEME_V->args); |
3580 | x = car (args); |
3705 | |
3581 | |
3706 | if (car (x) == SCHEME_V->LAMBDA) |
3582 | if (car (x) == SCHEME_V->LAMBDA) |
3707 | x = cdr (x); |
3583 | x = cdr (x); |
3708 | |
3584 | |
3709 | if (cdr (SCHEME_V->args) == NIL) |
3585 | if (cdr (args) == NIL) |
3710 | y = SCHEME_V->envir; |
3586 | y = SCHEME_V->envir; |
3711 | else |
3587 | else |
3712 | y = cadr (SCHEME_V->args); |
3588 | y = cadr (args); |
3713 | |
3589 | |
3714 | s_return (mk_closure (SCHEME_A_ x, y)); |
3590 | s_return (mk_closure (SCHEME_A_ x, y)); |
3715 | |
3591 | |
3716 | case OP_QUOTE: /* quote */ |
3592 | case OP_QUOTE: /* quote */ |
3717 | s_return (car (SCHEME_V->code)); |
3593 | s_return (car (SCHEME_V->code)); |
… | |
… | |
3749 | |
3625 | |
3750 | |
3626 | |
3751 | case OP_DEFP: /* defined? */ |
3627 | case OP_DEFP: /* defined? */ |
3752 | x = SCHEME_V->envir; |
3628 | x = SCHEME_V->envir; |
3753 | |
3629 | |
3754 | if (cdr (SCHEME_V->args) != NIL) |
3630 | if (cdr (args) != NIL) |
3755 | x = cadr (SCHEME_V->args); |
3631 | x = cadr (args); |
3756 | |
3632 | |
3757 | s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); |
3633 | s_retbool (find_slot_in_env (SCHEME_A_ x, car (args), 1) != NIL); |
3758 | |
3634 | |
3759 | case OP_SET0: /* set! */ |
3635 | case OP_SET0: /* set! */ |
3760 | if (is_immutable (car (SCHEME_V->code))) |
3636 | if (is_immutable (car (SCHEME_V->code))) |
3761 | Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); |
3637 | Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); |
3762 | |
3638 | |
… | |
… | |
3793 | |
3669 | |
3794 | case OP_IF1: /* if */ |
3670 | case OP_IF1: /* if */ |
3795 | if (is_true (SCHEME_V->value)) |
3671 | if (is_true (SCHEME_V->value)) |
3796 | SCHEME_V->code = car (SCHEME_V->code); |
3672 | SCHEME_V->code = car (SCHEME_V->code); |
3797 | else |
3673 | else |
3798 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because |
3674 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
3799 | |
|
|
3800 | * car(NIL) = NIL */ |
|
|
3801 | s_goto (OP_EVAL); |
3675 | s_goto (OP_EVAL); |
3802 | |
3676 | |
3803 | case OP_LET0: /* let */ |
3677 | case OP_LET0: /* let */ |
3804 | SCHEME_V->args = NIL; |
3678 | SCHEME_V->args = NIL; |
3805 | SCHEME_V->value = SCHEME_V->code; |
3679 | SCHEME_V->value = SCHEME_V->code; |
3806 | SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); |
3680 | SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); |
3807 | s_goto (OP_LET1); |
3681 | s_goto (OP_LET1); |
3808 | |
3682 | |
3809 | case OP_LET1: /* let (calculate parameters) */ |
3683 | case OP_LET1: /* let (calculate parameters) */ |
3810 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3684 | args = cons (SCHEME_V->value, args); |
3811 | |
3685 | |
3812 | if (is_pair (SCHEME_V->code)) /* continue */ |
3686 | if (is_pair (SCHEME_V->code)) /* continue */ |
3813 | { |
3687 | { |
3814 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3688 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3815 | Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); |
3689 | Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); |
3816 | |
3690 | |
3817 | s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); |
3691 | s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); |
3818 | SCHEME_V->code = cadar (SCHEME_V->code); |
3692 | SCHEME_V->code = cadar (SCHEME_V->code); |
3819 | SCHEME_V->args = NIL; |
3693 | SCHEME_V->args = NIL; |
3820 | s_goto (OP_EVAL); |
3694 | s_goto (OP_EVAL); |
3821 | } |
3695 | } |
3822 | else /* end */ |
3696 | else /* end */ |
3823 | { |
3697 | { |
3824 | SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); |
3698 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3825 | SCHEME_V->code = car (SCHEME_V->args); |
3699 | SCHEME_V->code = car (args); |
3826 | SCHEME_V->args = cdr (SCHEME_V->args); |
3700 | SCHEME_V->args = cdr (args); |
3827 | s_goto (OP_LET2); |
3701 | s_goto (OP_LET2); |
3828 | } |
3702 | } |
3829 | |
3703 | |
3830 | case OP_LET2: /* let */ |
3704 | case OP_LET2: /* let */ |
3831 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3705 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3832 | |
3706 | |
3833 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; |
3707 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; |
3834 | y != NIL; x = cdr (x), y = cdr (y)) |
3708 | y != NIL; x = cdr (x), y = cdr (y)) |
3835 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3709 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3836 | |
3710 | |
3837 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3711 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3838 | { |
3712 | { |
3839 | for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) |
3713 | for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) |
3840 | { |
3714 | { |
3841 | if (!is_pair (x)) |
3715 | if (!is_pair (x)) |
3842 | Error_1 ("Bad syntax of binding in let :", x); |
3716 | Error_1 ("Bad syntax of binding in let :", x); |
3843 | |
3717 | |
3844 | if (!is_list (SCHEME_A_ car (x))) |
3718 | if (!is_list (SCHEME_A_ car (x))) |
3845 | Error_1 ("Bad syntax of binding in let :", car (x)); |
3719 | Error_1 ("Bad syntax of binding in let :", car (x)); |
3846 | |
3720 | |
3847 | SCHEME_V->args = cons (caar (x), SCHEME_V->args); |
3721 | args = cons (caar (x), args); |
3848 | } |
3722 | } |
3849 | |
3723 | |
3850 | x = |
|
|
3851 | mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args), cddr (SCHEME_V->code)), |
3724 | x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)), |
3852 | SCHEME_V->envir); |
3725 | SCHEME_V->envir); |
3853 | new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); |
3726 | new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); |
3854 | SCHEME_V->code = cddr (SCHEME_V->code); |
3727 | SCHEME_V->code = cddr (SCHEME_V->code); |
3855 | SCHEME_V->args = NIL; |
|
|
3856 | } |
3728 | } |
3857 | else |
3729 | else |
3858 | { |
3730 | { |
3859 | SCHEME_V->code = cdr (SCHEME_V->code); |
3731 | SCHEME_V->code = cdr (SCHEME_V->code); |
|
|
3732 | } |
|
|
3733 | |
3860 | SCHEME_V->args = NIL; |
3734 | SCHEME_V->args = NIL; |
3861 | } |
|
|
3862 | |
|
|
3863 | s_goto (OP_BEGIN); |
3735 | s_goto (OP_BEGIN); |
3864 | |
3736 | |
3865 | case OP_LET0AST: /* let* */ |
3737 | case OP_LET0AST: /* let* */ |
3866 | if (car (SCHEME_V->code) == NIL) |
3738 | if (car (SCHEME_V->code) == NIL) |
3867 | { |
3739 | { |
… | |
… | |
3885 | new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); |
3757 | new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); |
3886 | SCHEME_V->code = cdr (SCHEME_V->code); |
3758 | SCHEME_V->code = cdr (SCHEME_V->code); |
3887 | |
3759 | |
3888 | if (is_pair (SCHEME_V->code)) /* continue */ |
3760 | if (is_pair (SCHEME_V->code)) /* continue */ |
3889 | { |
3761 | { |
3890 | s_save (SCHEME_A_ OP_LET2AST, SCHEME_V->args, SCHEME_V->code); |
3762 | s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); |
3891 | SCHEME_V->code = cadar (SCHEME_V->code); |
3763 | SCHEME_V->code = cadar (SCHEME_V->code); |
3892 | SCHEME_V->args = NIL; |
3764 | SCHEME_V->args = NIL; |
3893 | s_goto (OP_EVAL); |
3765 | s_goto (OP_EVAL); |
3894 | } |
3766 | } |
3895 | else /* end */ |
3767 | else /* end */ |
3896 | { |
3768 | { |
3897 | SCHEME_V->code = SCHEME_V->args; |
3769 | SCHEME_V->code = args; |
3898 | SCHEME_V->args = NIL; |
3770 | SCHEME_V->args = NIL; |
3899 | s_goto (OP_BEGIN); |
3771 | s_goto (OP_BEGIN); |
3900 | } |
3772 | } |
3901 | |
3773 | |
3902 | case OP_LET0REC: /* letrec */ |
3774 | case OP_LET0REC: /* letrec */ |
… | |
… | |
3905 | SCHEME_V->value = SCHEME_V->code; |
3777 | SCHEME_V->value = SCHEME_V->code; |
3906 | SCHEME_V->code = car (SCHEME_V->code); |
3778 | SCHEME_V->code = car (SCHEME_V->code); |
3907 | s_goto (OP_LET1REC); |
3779 | s_goto (OP_LET1REC); |
3908 | |
3780 | |
3909 | case OP_LET1REC: /* letrec (calculate parameters) */ |
3781 | case OP_LET1REC: /* letrec (calculate parameters) */ |
3910 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3782 | args = cons (SCHEME_V->value, args); |
3911 | |
3783 | |
3912 | if (is_pair (SCHEME_V->code)) /* continue */ |
3784 | if (is_pair (SCHEME_V->code)) /* continue */ |
3913 | { |
3785 | { |
3914 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3786 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3915 | Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); |
3787 | Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); |
3916 | |
3788 | |
3917 | s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); |
3789 | s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code)); |
3918 | SCHEME_V->code = cadar (SCHEME_V->code); |
3790 | SCHEME_V->code = cadar (SCHEME_V->code); |
3919 | SCHEME_V->args = NIL; |
3791 | SCHEME_V->args = NIL; |
3920 | s_goto (OP_EVAL); |
3792 | s_goto (OP_EVAL); |
3921 | } |
3793 | } |
3922 | else /* end */ |
3794 | else /* end */ |
3923 | { |
3795 | { |
3924 | SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); |
3796 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3925 | SCHEME_V->code = car (SCHEME_V->args); |
3797 | SCHEME_V->code = car (args); |
3926 | SCHEME_V->args = cdr (SCHEME_V->args); |
3798 | SCHEME_V->args = cdr (args); |
3927 | s_goto (OP_LET2REC); |
3799 | s_goto (OP_LET2REC); |
3928 | } |
3800 | } |
3929 | |
3801 | |
3930 | case OP_LET2REC: /* letrec */ |
3802 | case OP_LET2REC: /* letrec */ |
3931 | for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) |
3803 | for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y)) |
3932 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3804 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3933 | |
3805 | |
3934 | SCHEME_V->code = cdr (SCHEME_V->code); |
3806 | SCHEME_V->code = cdr (SCHEME_V->code); |
3935 | SCHEME_V->args = NIL; |
3807 | SCHEME_V->args = NIL; |
3936 | s_goto (OP_BEGIN); |
3808 | s_goto (OP_BEGIN); |
… | |
… | |
4022 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3894 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
4023 | SCHEME_V->code = car (SCHEME_V->code); |
3895 | SCHEME_V->code = car (SCHEME_V->code); |
4024 | s_goto (OP_EVAL); |
3896 | s_goto (OP_EVAL); |
4025 | |
3897 | |
4026 | case OP_C1STREAM: /* cons-stream */ |
3898 | case OP_C1STREAM: /* cons-stream */ |
4027 | SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register SCHEME_V->args for gc */ |
3899 | SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register args for gc */ |
4028 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3900 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
4029 | set_typeflag (x, T_PROMISE); |
3901 | set_typeflag (x, T_PROMISE); |
4030 | s_return (cons (SCHEME_V->args, x)); |
3902 | s_return (cons (args, x)); |
4031 | |
3903 | |
4032 | case OP_MACRO0: /* macro */ |
3904 | case OP_MACRO0: /* macro */ |
4033 | if (is_pair (car (SCHEME_V->code))) |
3905 | if (is_pair (car (SCHEME_V->code))) |
4034 | { |
3906 | { |
4035 | x = caar (SCHEME_V->code); |
3907 | x = caar (SCHEME_V->code); |
… | |
… | |
4068 | { |
3940 | { |
4069 | if (!is_pair (y = caar (x))) |
3941 | if (!is_pair (y = caar (x))) |
4070 | break; |
3942 | break; |
4071 | |
3943 | |
4072 | for (; y != NIL; y = cdr (y)) |
3944 | for (; y != NIL; y = cdr (y)) |
4073 | { |
|
|
4074 | if (eqv (car (y), SCHEME_V->value)) |
3945 | if (eqv (car (y), SCHEME_V->value)) |
4075 | break; |
3946 | break; |
4076 | } |
|
|
4077 | |
3947 | |
4078 | if (y != NIL) |
3948 | if (y != NIL) |
4079 | break; |
3949 | break; |
4080 | } |
3950 | } |
4081 | |
3951 | |
… | |
… | |
4101 | s_goto (OP_BEGIN); |
3971 | s_goto (OP_BEGIN); |
4102 | else |
3972 | else |
4103 | s_return (NIL); |
3973 | s_return (NIL); |
4104 | |
3974 | |
4105 | case OP_PAPPLY: /* apply */ |
3975 | case OP_PAPPLY: /* apply */ |
4106 | SCHEME_V->code = car (SCHEME_V->args); |
3976 | SCHEME_V->code = car (args); |
4107 | SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); |
3977 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
4108 | /*SCHEME_V->args = cadr(SCHEME_V->args); */ |
3978 | /*SCHEME_V->args = cadr(args); */ |
4109 | s_goto (OP_APPLY); |
3979 | s_goto (OP_APPLY); |
4110 | |
3980 | |
4111 | case OP_PEVAL: /* eval */ |
3981 | case OP_PEVAL: /* eval */ |
4112 | if (cdr (SCHEME_V->args) != NIL) |
3982 | if (cdr (args) != NIL) |
4113 | SCHEME_V->envir = cadr (SCHEME_V->args); |
3983 | SCHEME_V->envir = cadr (args); |
4114 | |
3984 | |
4115 | SCHEME_V->code = car (SCHEME_V->args); |
3985 | SCHEME_V->code = car (args); |
4116 | s_goto (OP_EVAL); |
3986 | s_goto (OP_EVAL); |
4117 | |
3987 | |
4118 | case OP_CONTINUATION: /* call-with-current-continuation */ |
3988 | case OP_CONTINUATION: /* call-with-current-continuation */ |
4119 | SCHEME_V->code = car (SCHEME_V->args); |
3989 | SCHEME_V->code = car (args); |
4120 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_V)), NIL); |
3990 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
4121 | s_goto (OP_APPLY); |
3991 | s_goto (OP_APPLY); |
4122 | } |
3992 | } |
4123 | |
3993 | |
4124 | return S_T; |
3994 | if (USE_ERROR_CHECKING) abort (); |
4125 | } |
3995 | } |
4126 | |
3996 | |
4127 | static pointer |
3997 | static int |
4128 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
3998 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4129 | { |
3999 | { |
4130 | pointer x; |
4000 | pointer args = SCHEME_V->args; |
|
|
4001 | pointer x = car (args); |
4131 | num v; |
4002 | num v; |
4132 | |
4003 | |
|
|
4004 | switch (op) |
|
|
4005 | { |
4133 | #if USE_MATH |
4006 | #if USE_MATH |
4134 | RVALUE dd; |
|
|
4135 | #endif |
|
|
4136 | |
|
|
4137 | switch (op) |
|
|
4138 | { |
|
|
4139 | #if USE_MATH |
|
|
4140 | |
|
|
4141 | case OP_INEX2EX: /* inexact->exact */ |
4007 | case OP_INEX2EX: /* inexact->exact */ |
4142 | x = car (SCHEME_V->args); |
|
|
4143 | |
|
|
4144 | if (num_is_integer (x)) |
4008 | if (!is_integer (x)) |
4145 | s_return (x); |
4009 | { |
4146 | else if (modf (rvalue_unchecked (x), &dd) == 0.0) |
4010 | RVALUE r = rvalue_unchecked (x); |
4147 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4011 | |
|
|
4012 | if (r == (RVALUE)(IVALUE)r) |
|
|
4013 | x = mk_integer (SCHEME_A_ rvalue_unchecked (x)); |
4148 | else |
4014 | else |
4149 | Error_1 ("inexact->exact: not integral:", x); |
4015 | Error_1 ("inexact->exact: not integral:", x); |
|
|
4016 | } |
4150 | |
4017 | |
4151 | case OP_EXP: |
4018 | s_return (x); |
4152 | x = car (SCHEME_V->args); |
4019 | |
|
|
4020 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4021 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4022 | case OP_TRUNCATE: s_return (mk_real (SCHEME_A_ trunc (rvalue (x)))); |
|
|
4023 | case OP_ROUND: s_return (mk_real (SCHEME_A_ nearbyint (rvalue (x)))); |
|
|
4024 | |
|
|
4025 | case OP_SQRT: s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
4153 | s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4026 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4154 | |
|
|
4155 | case OP_LOG: |
|
|
4156 | x = car (SCHEME_V->args); |
|
|
4157 | s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4027 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)) |
4158 | |
4028 | / (cadr (args) == NIL ? 1 : log (rvalue (cadr (args)))))); |
4159 | case OP_SIN: |
|
|
4160 | x = car (SCHEME_V->args); |
|
|
4161 | s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4029 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4162 | |
|
|
4163 | case OP_COS: |
|
|
4164 | x = car (SCHEME_V->args); |
|
|
4165 | s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4030 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4166 | |
|
|
4167 | case OP_TAN: |
|
|
4168 | x = car (SCHEME_V->args); |
|
|
4169 | s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4031 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4170 | |
|
|
4171 | case OP_ASIN: |
|
|
4172 | x = car (SCHEME_V->args); |
|
|
4173 | s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4032 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4174 | |
|
|
4175 | case OP_ACOS: |
|
|
4176 | x = car (SCHEME_V->args); |
|
|
4177 | s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4033 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4178 | |
4034 | |
4179 | case OP_ATAN: |
4035 | case OP_ATAN: |
4180 | x = car (SCHEME_V->args); |
|
|
4181 | |
|
|
4182 | if (cdr (SCHEME_V->args) == NIL) |
|
|
4183 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
|
|
4184 | else |
|
|
4185 | { |
|
|
4186 | pointer y = cadr (SCHEME_V->args); |
|
|
4187 | |
|
|
4188 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
|
|
4189 | } |
|
|
4190 | |
|
|
4191 | case OP_SQRT: |
|
|
4192 | x = car (SCHEME_V->args); |
|
|
4193 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
4036 | s_return (mk_real (SCHEME_A_ |
|
|
4037 | cdr (args) == NIL |
|
|
4038 | ? atan (rvalue (x)) |
|
|
4039 | : atan2 (rvalue (x), rvalue (cadr (args))))); |
4194 | |
4040 | |
4195 | case OP_EXPT: |
4041 | case OP_EXPT: |
4196 | { |
4042 | { |
4197 | RVALUE result; |
4043 | RVALUE result; |
4198 | int real_result = 1; |
4044 | int real_result = 1; |
4199 | pointer y = cadr (SCHEME_V->args); |
4045 | pointer y = cadr (args); |
4200 | |
4046 | |
4201 | x = car (SCHEME_V->args); |
|
|
4202 | |
|
|
4203 | if (num_is_integer (x) && num_is_integer (y)) |
4047 | if (is_integer (x) && is_integer (y)) |
4204 | real_result = 0; |
4048 | real_result = 0; |
4205 | |
4049 | |
4206 | /* This 'if' is an R5RS compatibility fix. */ |
4050 | /* This 'if' is an R5RS compatibility fix. */ |
4207 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4051 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4208 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4052 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4209 | result = 0.0; |
4053 | result = 0; |
4210 | else |
4054 | else |
4211 | result = pow (rvalue (x), rvalue (y)); |
4055 | result = pow (rvalue (x), rvalue (y)); |
4212 | |
4056 | |
4213 | /* Before returning integer result make sure we can. */ |
4057 | /* Before returning integer result make sure we can. */ |
4214 | /* If the test fails, result is too big for integer. */ |
4058 | /* If the test fails, result is too big for integer. */ |
4215 | if (!real_result) |
4059 | if (!real_result) |
4216 | { |
4060 | { |
4217 | long result_as_long = (long) result; |
4061 | long result_as_long = result; |
4218 | |
4062 | |
4219 | if (result != (RVALUE) result_as_long) |
4063 | if (result != result_as_long) |
4220 | real_result = 1; |
4064 | real_result = 1; |
4221 | } |
4065 | } |
4222 | |
4066 | |
4223 | if (real_result) |
4067 | if (real_result) |
4224 | s_return (mk_real (SCHEME_A_ result)); |
4068 | s_return (mk_real (SCHEME_A_ result)); |
4225 | else |
4069 | else |
4226 | s_return (mk_integer (SCHEME_A_ result)); |
4070 | s_return (mk_integer (SCHEME_A_ result)); |
4227 | } |
4071 | } |
4228 | |
|
|
4229 | case OP_FLOOR: |
|
|
4230 | x = car (SCHEME_V->args); |
|
|
4231 | s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
|
|
4232 | |
|
|
4233 | case OP_CEILING: |
|
|
4234 | x = car (SCHEME_V->args); |
|
|
4235 | s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
|
|
4236 | |
|
|
4237 | case OP_TRUNCATE: |
|
|
4238 | { |
|
|
4239 | RVALUE rvalue_of_x; |
|
|
4240 | |
|
|
4241 | x = car (SCHEME_V->args); |
|
|
4242 | rvalue_of_x = rvalue (x); |
|
|
4243 | |
|
|
4244 | if (rvalue_of_x > 0) |
|
|
4245 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
|
|
4246 | else |
|
|
4247 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
|
|
4248 | } |
|
|
4249 | |
|
|
4250 | case OP_ROUND: |
|
|
4251 | x = car (SCHEME_V->args); |
|
|
4252 | |
|
|
4253 | if (num_is_integer (x)) |
|
|
4254 | s_return (x); |
|
|
4255 | |
|
|
4256 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
|
|
4257 | #endif |
4072 | #endif |
4258 | |
4073 | |
4259 | case OP_ADD: /* + */ |
4074 | case OP_ADD: /* + */ |
4260 | v = num_zero; |
4075 | v = num_zero; |
4261 | |
4076 | |
4262 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4077 | for (x = args; x != NIL; x = cdr (x)) |
4263 | v = num_add (v, nvalue (car (x))); |
4078 | v = num_op (NUM_ADD, v, nvalue (car (x))); |
4264 | |
4079 | |
4265 | s_return (mk_number (SCHEME_A_ v)); |
4080 | s_return (mk_number (SCHEME_A_ v)); |
4266 | |
4081 | |
4267 | case OP_MUL: /* * */ |
4082 | case OP_MUL: /* * */ |
4268 | v = num_one; |
4083 | v = num_one; |
4269 | |
4084 | |
4270 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4085 | for (x = args; x != NIL; x = cdr (x)) |
4271 | v = num_mul (v, nvalue (car (x))); |
4086 | v = num_op (NUM_MUL, v, nvalue (car (x))); |
4272 | |
4087 | |
4273 | s_return (mk_number (SCHEME_A_ v)); |
4088 | s_return (mk_number (SCHEME_A_ v)); |
4274 | |
4089 | |
4275 | case OP_SUB: /* - */ |
4090 | case OP_SUB: /* - */ |
4276 | if (cdr (SCHEME_V->args) == NIL) |
4091 | if (cdr (args) == NIL) |
4277 | { |
4092 | { |
4278 | x = SCHEME_V->args; |
4093 | x = args; |
4279 | v = num_zero; |
4094 | v = num_zero; |
4280 | } |
4095 | } |
4281 | else |
4096 | else |
4282 | { |
4097 | { |
4283 | x = cdr (SCHEME_V->args); |
4098 | x = cdr (args); |
4284 | v = nvalue (car (SCHEME_V->args)); |
4099 | v = nvalue (car (args)); |
4285 | } |
4100 | } |
4286 | |
4101 | |
4287 | for (; x != NIL; x = cdr (x)) |
4102 | for (; x != NIL; x = cdr (x)) |
4288 | v = num_sub (v, nvalue (car (x))); |
4103 | v = num_op (NUM_SUB, v, nvalue (car (x))); |
4289 | |
4104 | |
4290 | s_return (mk_number (SCHEME_A_ v)); |
4105 | s_return (mk_number (SCHEME_A_ v)); |
4291 | |
4106 | |
4292 | case OP_DIV: /* / */ |
4107 | case OP_DIV: /* / */ |
4293 | if (cdr (SCHEME_V->args) == NIL) |
4108 | if (cdr (args) == NIL) |
4294 | { |
4109 | { |
4295 | x = SCHEME_V->args; |
4110 | x = args; |
4296 | v = num_one; |
4111 | v = num_one; |
4297 | } |
4112 | } |
4298 | else |
4113 | else |
4299 | { |
4114 | { |
4300 | x = cdr (SCHEME_V->args); |
4115 | x = cdr (args); |
4301 | v = nvalue (car (SCHEME_V->args)); |
4116 | v = nvalue (car (args)); |
4302 | } |
4117 | } |
4303 | |
4118 | |
4304 | for (; x != NIL; x = cdr (x)) |
4119 | for (; x != NIL; x = cdr (x)) |
4305 | { |
|
|
4306 | if (!is_zero_rvalue (rvalue (car (x)))) |
4120 | if (!is_zero_rvalue (rvalue (car (x)))) |
4307 | v = num_div (v, nvalue (car (x))); |
4121 | v = num_div (v, nvalue (car (x))); |
4308 | else |
4122 | else |
4309 | Error_0 ("/: division by zero"); |
4123 | Error_0 ("/: division by zero"); |
4310 | } |
|
|
4311 | |
4124 | |
4312 | s_return (mk_number (SCHEME_A_ v)); |
4125 | s_return (mk_number (SCHEME_A_ v)); |
4313 | |
4126 | |
4314 | case OP_INTDIV: /* quotient */ |
4127 | case OP_INTDIV: /* quotient */ |
4315 | if (cdr (SCHEME_V->args) == NIL) |
4128 | if (cdr (args) == NIL) |
4316 | { |
4129 | { |
4317 | x = SCHEME_V->args; |
4130 | x = args; |
4318 | v = num_one; |
4131 | v = num_one; |
4319 | } |
4132 | } |
4320 | else |
4133 | else |
4321 | { |
4134 | { |
4322 | x = cdr (SCHEME_V->args); |
4135 | x = cdr (args); |
4323 | v = nvalue (car (SCHEME_V->args)); |
4136 | v = nvalue (car (args)); |
4324 | } |
4137 | } |
4325 | |
4138 | |
4326 | for (; x != NIL; x = cdr (x)) |
4139 | for (; x != NIL; x = cdr (x)) |
4327 | { |
4140 | { |
4328 | if (ivalue (car (x)) != 0) |
4141 | if (ivalue (car (x)) != 0) |
4329 | v = num_intdiv (v, nvalue (car (x))); |
4142 | v = num_op (NUM_INTDIV, v, nvalue (car (x))); |
4330 | else |
4143 | else |
4331 | Error_0 ("quotient: division by zero"); |
4144 | Error_0 ("quotient: division by zero"); |
4332 | } |
4145 | } |
4333 | |
4146 | |
4334 | s_return (mk_number (SCHEME_A_ v)); |
4147 | s_return (mk_number (SCHEME_A_ v)); |
4335 | |
4148 | |
4336 | case OP_REM: /* remainder */ |
4149 | case OP_REM: /* remainder */ |
4337 | v = nvalue (car (SCHEME_V->args)); |
4150 | v = nvalue (x); |
4338 | |
4151 | |
4339 | if (ivalue (cadr (SCHEME_V->args)) != 0) |
4152 | if (ivalue (cadr (args)) != 0) |
4340 | v = num_rem (v, nvalue (cadr (SCHEME_V->args))); |
4153 | v = num_rem (v, nvalue (cadr (args))); |
4341 | else |
4154 | else |
4342 | Error_0 ("remainder: division by zero"); |
4155 | Error_0 ("remainder: division by zero"); |
4343 | |
4156 | |
4344 | s_return (mk_number (SCHEME_A_ v)); |
4157 | s_return (mk_number (SCHEME_A_ v)); |
4345 | |
4158 | |
4346 | case OP_MOD: /* modulo */ |
4159 | case OP_MOD: /* modulo */ |
4347 | v = nvalue (car (SCHEME_V->args)); |
4160 | v = nvalue (x); |
4348 | |
4161 | |
4349 | if (ivalue (cadr (SCHEME_V->args)) != 0) |
4162 | if (ivalue (cadr (args)) != 0) |
4350 | v = num_mod (v, nvalue (cadr (SCHEME_V->args))); |
4163 | v = num_mod (v, nvalue (cadr (args))); |
4351 | else |
4164 | else |
4352 | Error_0 ("modulo: division by zero"); |
4165 | Error_0 ("modulo: division by zero"); |
4353 | |
4166 | |
4354 | s_return (mk_number (SCHEME_A_ v)); |
4167 | s_return (mk_number (SCHEME_A_ v)); |
4355 | |
4168 | |
4356 | case OP_CAR: /* car */ |
4169 | /* the compiler will optimize this mess... */ |
4357 | s_return (caar (SCHEME_V->args)); |
4170 | case OP_CAR: op_car: s_return (car (x)); |
4358 | |
4171 | case OP_CDR: op_cdr: s_return (cdr (x)); |
4359 | case OP_CDR: /* cdr */ |
4172 | case OP_CAAR: op_caar: x = car (x); goto op_car; |
4360 | s_return (cdar (SCHEME_V->args)); |
4173 | case OP_CADR: op_cadr: x = cdr (x); goto op_car; |
|
|
4174 | case OP_CDAR: op_cdar: x = car (x); goto op_cdr; |
|
|
4175 | case OP_CDDR: op_cddr: x = cdr (x); goto op_cdr; |
|
|
4176 | case OP_CAAAR: op_caaar: x = car (x); goto op_caar; |
|
|
4177 | case OP_CAADR: op_caadr: x = cdr (x); goto op_caar; |
|
|
4178 | case OP_CADAR: op_cadar: x = car (x); goto op_cadr; |
|
|
4179 | case OP_CADDR: op_caddr: x = cdr (x); goto op_cadr; |
|
|
4180 | case OP_CDAAR: op_cdaar: x = car (x); goto op_cdar; |
|
|
4181 | case OP_CDADR: op_cdadr: x = cdr (x); goto op_cdar; |
|
|
4182 | case OP_CDDAR: op_cddar: x = car (x); goto op_cddr; |
|
|
4183 | case OP_CDDDR: op_cdddr: x = cdr (x); goto op_cddr; |
|
|
4184 | case OP_CAAAAR: x = car (x); goto op_caaar; |
|
|
4185 | case OP_CAAADR: x = cdr (x); goto op_caaar; |
|
|
4186 | case OP_CAADAR: x = car (x); goto op_caadr; |
|
|
4187 | case OP_CAADDR: x = cdr (x); goto op_caadr; |
|
|
4188 | case OP_CADAAR: x = car (x); goto op_cadar; |
|
|
4189 | case OP_CADADR: x = cdr (x); goto op_cadar; |
|
|
4190 | case OP_CADDAR: x = car (x); goto op_caddr; |
|
|
4191 | case OP_CADDDR: x = cdr (x); goto op_caddr; |
|
|
4192 | case OP_CDAAAR: x = car (x); goto op_cdaar; |
|
|
4193 | case OP_CDAADR: x = cdr (x); goto op_cdaar; |
|
|
4194 | case OP_CDADAR: x = car (x); goto op_cdadr; |
|
|
4195 | case OP_CDADDR: x = cdr (x); goto op_cdadr; |
|
|
4196 | case OP_CDDAAR: x = car (x); goto op_cddar; |
|
|
4197 | case OP_CDDADR: x = cdr (x); goto op_cddar; |
|
|
4198 | case OP_CDDDAR: x = car (x); goto op_cdddr; |
|
|
4199 | case OP_CDDDDR: x = cdr (x); goto op_cdddr; |
4361 | |
4200 | |
4362 | case OP_CONS: /* cons */ |
4201 | case OP_CONS: /* cons */ |
4363 | set_cdr (SCHEME_V->args, cadr (SCHEME_V->args)); |
4202 | set_cdr (args, cadr (args)); |
4364 | s_return (SCHEME_V->args); |
4203 | s_return (args); |
4365 | |
4204 | |
4366 | case OP_SETCAR: /* set-car! */ |
4205 | case OP_SETCAR: /* set-car! */ |
4367 | if (!is_immutable (car (SCHEME_V->args))) |
4206 | if (!is_immutable (x)) |
4368 | { |
4207 | { |
4369 | set_car (car (SCHEME_V->args), cadr (SCHEME_V->args)); |
4208 | set_car (x, cadr (args)); |
4370 | s_return (car (SCHEME_V->args)); |
4209 | s_return (car (args)); |
4371 | } |
4210 | } |
4372 | else |
4211 | else |
4373 | Error_0 ("set-car!: unable to alter immutable pair"); |
4212 | Error_0 ("set-car!: unable to alter immutable pair"); |
4374 | |
4213 | |
4375 | case OP_SETCDR: /* set-cdr! */ |
4214 | case OP_SETCDR: /* set-cdr! */ |
4376 | if (!is_immutable (car (SCHEME_V->args))) |
4215 | if (!is_immutable (x)) |
4377 | { |
4216 | { |
4378 | set_cdr (car (SCHEME_V->args), cadr (SCHEME_V->args)); |
4217 | set_cdr (x, cadr (args)); |
4379 | s_return (car (SCHEME_V->args)); |
4218 | s_return (car (args)); |
4380 | } |
4219 | } |
4381 | else |
4220 | else |
4382 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4221 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4383 | |
4222 | |
4384 | case OP_CHAR2INT: /* char->integer */ |
4223 | case OP_CHAR2INT: /* char->integer */ |
4385 | s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4224 | s_return (mk_integer (SCHEME_A_ ivalue_unchecked (x))); |
4386 | |
4225 | |
4387 | case OP_INT2CHAR: /* integer->char */ |
4226 | case OP_INT2CHAR: /* integer->char */ |
4388 | s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4227 | s_return (mk_character (SCHEME_A_ ivalue_unchecked (x))); |
4389 | |
4228 | |
4390 | case OP_CHARUPCASE: |
4229 | case OP_CHARUPCASE: |
4391 | { |
4230 | { |
4392 | unsigned char c = ivalue (car (SCHEME_V->args)); |
4231 | unsigned char c = ivalue_unchecked (x); |
4393 | c = toupper (c); |
4232 | c = toupper (c); |
4394 | s_return (mk_character (SCHEME_A_ c)); |
4233 | s_return (mk_character (SCHEME_A_ c)); |
4395 | } |
4234 | } |
4396 | |
4235 | |
4397 | case OP_CHARDNCASE: |
4236 | case OP_CHARDNCASE: |
4398 | { |
4237 | { |
4399 | unsigned char c = ivalue (car (SCHEME_V->args)); |
4238 | unsigned char c = ivalue_unchecked (x); |
4400 | c = tolower (c); |
4239 | c = tolower (c); |
4401 | s_return (mk_character (SCHEME_A_ c)); |
4240 | s_return (mk_character (SCHEME_A_ c)); |
4402 | } |
4241 | } |
4403 | |
4242 | |
4404 | case OP_STR2SYM: /* string->symbol */ |
4243 | case OP_STR2SYM: /* string->symbol */ |
4405 | s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); |
4244 | s_return (mk_symbol (SCHEME_A_ strvalue (x))); |
4406 | |
4245 | |
4407 | case OP_STR2ATOM: /* string->atom */ |
4246 | case OP_STR2ATOM: /* string->atom */ |
4408 | { |
4247 | { |
4409 | char *s = strvalue (car (SCHEME_V->args)); |
4248 | char *s = strvalue (x); |
4410 | long pf = 0; |
4249 | long pf = 0; |
4411 | |
4250 | |
4412 | if (cdr (SCHEME_V->args) != NIL) |
4251 | if (cdr (args) != NIL) |
4413 | { |
4252 | { |
4414 | /* we know cadr(SCHEME_V->args) is a natural number */ |
4253 | /* we know cadr(args) is a natural number */ |
4415 | /* see if it is 2, 8, 10, or 16, or error */ |
4254 | /* see if it is 2, 8, 10, or 16, or error */ |
4416 | pf = ivalue_unchecked (cadr (SCHEME_V->args)); |
4255 | pf = ivalue_unchecked (cadr (args)); |
4417 | |
4256 | |
4418 | if (pf == 16 || pf == 10 || pf == 8 || pf == 2) |
4257 | if (pf == 16 || pf == 10 || pf == 8 || pf == 2) |
4419 | { |
4258 | { |
4420 | /* base is OK */ |
4259 | /* base is OK */ |
4421 | } |
4260 | } |
4422 | else |
4261 | else |
4423 | pf = -1; |
4262 | pf = -1; |
4424 | } |
4263 | } |
4425 | |
4264 | |
4426 | if (pf < 0) |
4265 | if (pf < 0) |
4427 | Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); |
4266 | Error_1 ("string->atom: bad base:", cadr (args)); |
4428 | else if (*s == '#') /* no use of base! */ |
4267 | else if (*s == '#') /* no use of base! */ |
4429 | s_return (mk_sharp_const (SCHEME_A_ s + 1)); |
4268 | s_return (mk_sharp_const (SCHEME_A_ s + 1)); |
4430 | else |
4269 | else |
4431 | { |
4270 | { |
4432 | if (pf == 0 || pf == 10) |
4271 | if (pf == 0 || pf == 10) |
… | |
… | |
4443 | } |
4282 | } |
4444 | } |
4283 | } |
4445 | } |
4284 | } |
4446 | |
4285 | |
4447 | case OP_SYM2STR: /* symbol->string */ |
4286 | case OP_SYM2STR: /* symbol->string */ |
4448 | x = mk_string (SCHEME_A_ symname (car (SCHEME_V->args))); |
4287 | x = mk_string (SCHEME_A_ symname (x)); |
4449 | setimmutable (x); |
4288 | setimmutable (x); |
4450 | s_return (x); |
4289 | s_return (x); |
4451 | |
4290 | |
4452 | case OP_ATOM2STR: /* atom->string */ |
4291 | case OP_ATOM2STR: /* atom->string */ |
4453 | { |
4292 | { |
4454 | long pf = 0; |
4293 | long pf = 0; |
4455 | |
4294 | |
4456 | x = car (SCHEME_V->args); |
|
|
4457 | |
|
|
4458 | if (cdr (SCHEME_V->args) != NIL) |
4295 | if (cdr (args) != NIL) |
4459 | { |
4296 | { |
4460 | /* we know cadr(SCHEME_V->args) is a natural number */ |
4297 | /* we know cadr(args) is a natural number */ |
4461 | /* see if it is 2, 8, 10, or 16, or error */ |
4298 | /* see if it is 2, 8, 10, or 16, or error */ |
4462 | pf = ivalue_unchecked (cadr (SCHEME_V->args)); |
4299 | pf = ivalue_unchecked (cadr (args)); |
4463 | |
4300 | |
4464 | if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) |
4301 | if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) |
4465 | { |
4302 | { |
4466 | /* base is OK */ |
4303 | /* base is OK */ |
4467 | } |
4304 | } |
4468 | else |
4305 | else |
4469 | pf = -1; |
4306 | pf = -1; |
4470 | } |
4307 | } |
4471 | |
4308 | |
4472 | if (pf < 0) |
4309 | if (pf < 0) |
4473 | Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); |
4310 | Error_1 ("atom->string: bad base:", cadr (args)); |
4474 | else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) |
4311 | else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) |
4475 | { |
4312 | { |
4476 | char *p; |
4313 | char *p; |
4477 | int len; |
4314 | int len; |
4478 | |
4315 | |
… | |
… | |
4483 | Error_1 ("atom->string: not an atom:", x); |
4320 | Error_1 ("atom->string: not an atom:", x); |
4484 | } |
4321 | } |
4485 | |
4322 | |
4486 | case OP_MKSTRING: /* make-string */ |
4323 | case OP_MKSTRING: /* make-string */ |
4487 | { |
4324 | { |
4488 | int fill = ' '; |
4325 | int fill = cdr (args) != NIL ? charvalue (cadr (args)) : ' '; |
4489 | int len; |
4326 | int len = ivalue_unchecked (x); |
4490 | |
4327 | |
4491 | len = ivalue (car (SCHEME_V->args)); |
|
|
4492 | |
|
|
4493 | if (cdr (SCHEME_V->args) != NIL) |
|
|
4494 | fill = charvalue (cadr (SCHEME_V->args)); |
|
|
4495 | |
|
|
4496 | s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); |
4328 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4497 | } |
4329 | } |
4498 | |
4330 | |
4499 | case OP_STRLEN: /* string-length */ |
4331 | case OP_STRLEN: /* string-length */ |
4500 | s_return (mk_integer (SCHEME_A_ strlength (car (SCHEME_V->args)))); |
4332 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4501 | |
4333 | |
4502 | case OP_STRREF: /* string-ref */ |
4334 | case OP_STRREF: /* string-ref */ |
4503 | { |
4335 | { |
4504 | char *str; |
4336 | char *str = strvalue (x); |
4505 | int index; |
|
|
4506 | |
|
|
4507 | str = strvalue (car (SCHEME_V->args)); |
|
|
4508 | |
|
|
4509 | index = ivalue (cadr (SCHEME_V->args)); |
4337 | int index = ivalue_unchecked (cadr (args)); |
4510 | |
4338 | |
4511 | if (index >= strlength (car (SCHEME_V->args))) |
4339 | if (index >= strlength (x)) |
4512 | Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); |
4340 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4513 | |
4341 | |
4514 | s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); |
4342 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4515 | } |
4343 | } |
4516 | |
4344 | |
4517 | case OP_STRSET: /* string-set! */ |
4345 | case OP_STRSET: /* string-set! */ |
4518 | { |
4346 | { |
4519 | char *str; |
4347 | char *str = strvalue (x); |
4520 | int index; |
4348 | int index = ivalue_unchecked (cadr (args)); |
4521 | int c; |
4349 | int c; |
4522 | |
4350 | |
4523 | if (is_immutable (car (SCHEME_V->args))) |
4351 | if (is_immutable (x)) |
4524 | Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); |
4352 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4525 | |
4353 | |
4526 | str = strvalue (car (SCHEME_V->args)); |
|
|
4527 | |
|
|
4528 | index = ivalue (cadr (SCHEME_V->args)); |
|
|
4529 | |
|
|
4530 | if (index >= strlength (car (SCHEME_V->args))) |
4354 | if (index >= strlength (x)) |
4531 | Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); |
4355 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4532 | |
4356 | |
4533 | c = charvalue (caddr (SCHEME_V->args)); |
4357 | c = charvalue (caddr (args)); |
4534 | |
4358 | |
4535 | str[index] = (char) c; |
4359 | str[index] = c; |
4536 | s_return (car (SCHEME_V->args)); |
4360 | s_return (car (args)); |
4537 | } |
4361 | } |
4538 | |
4362 | |
4539 | case OP_STRAPPEND: /* string-append */ |
4363 | case OP_STRAPPEND: /* string-append */ |
4540 | { |
4364 | { |
4541 | /* in 1.29 string-append was in Scheme in init.scm but was too slow */ |
4365 | /* in 1.29 string-append was in Scheme in init.scm but was too slow */ |
4542 | int len = 0; |
4366 | int len = 0; |
4543 | pointer newstr; |
4367 | pointer newstr; |
4544 | char *pos; |
4368 | char *pos; |
4545 | |
4369 | |
4546 | /* compute needed length for new string */ |
4370 | /* compute needed length for new string */ |
4547 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4371 | for (x = args; x != NIL; x = cdr (x)) |
4548 | len += strlength (car (x)); |
4372 | len += strlength (car (x)); |
4549 | |
4373 | |
4550 | newstr = mk_empty_string (SCHEME_A_ len, ' '); |
4374 | newstr = mk_empty_string (SCHEME_A_ len, ' '); |
4551 | |
4375 | |
4552 | /* store the contents of the argument strings into the new string */ |
4376 | /* store the contents of the argument strings into the new string */ |
4553 | for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) |
4377 | for (pos = strvalue (newstr), x = args; x != NIL; pos += strlength (car (x)), x = cdr (x)) |
4554 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4378 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4555 | |
4379 | |
4556 | s_return (newstr); |
4380 | s_return (newstr); |
4557 | } |
4381 | } |
4558 | |
4382 | |
4559 | case OP_SUBSTR: /* substring */ |
4383 | case OP_STRING_COPY: /* substring/string-copy */ |
4560 | { |
4384 | { |
4561 | char *str; |
4385 | char *str = strvalue (x); |
4562 | int index0; |
4386 | int index0 = cadr (args) == NIL ? 0 : ivalue_unchecked (cadr (args)); |
4563 | int index1; |
4387 | int index1; |
4564 | int len; |
4388 | int len; |
4565 | |
4389 | |
4566 | str = strvalue (car (SCHEME_V->args)); |
|
|
4567 | |
|
|
4568 | index0 = ivalue (cadr (SCHEME_V->args)); |
|
|
4569 | |
|
|
4570 | if (index0 > strlength (car (SCHEME_V->args))) |
4390 | if (index0 > strlength (x)) |
4571 | Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); |
4391 | Error_1 ("string->copy: start out of bounds:", cadr (args)); |
4572 | |
4392 | |
4573 | if (cddr (SCHEME_V->args) != NIL) |
4393 | if (cddr (args) != NIL) |
4574 | { |
4394 | { |
4575 | index1 = ivalue (caddr (SCHEME_V->args)); |
4395 | index1 = ivalue_unchecked (caddr (args)); |
4576 | |
4396 | |
4577 | if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) |
4397 | if (index1 > strlength (x) || index1 < index0) |
4578 | Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); |
4398 | Error_1 ("string->copy: end out of bounds:", caddr (args)); |
4579 | } |
4399 | } |
4580 | else |
4400 | else |
4581 | index1 = strlength (car (SCHEME_V->args)); |
4401 | index1 = strlength (x); |
4582 | |
4402 | |
4583 | len = index1 - index0; |
4403 | len = index1 - index0; |
4584 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4404 | x = mk_counted_string (SCHEME_A_ str + index0, len); |
4585 | memcpy (strvalue (x), str + index0, len); |
|
|
4586 | strvalue (x)[len] = 0; |
|
|
4587 | |
4405 | |
4588 | s_return (x); |
4406 | s_return (x); |
4589 | } |
4407 | } |
4590 | |
4408 | |
4591 | case OP_VECTOR: /* vector */ |
4409 | case OP_VECTOR: /* vector */ |
4592 | { |
4410 | { |
4593 | int i; |
4411 | int i; |
4594 | pointer vec; |
4412 | pointer vec; |
4595 | int len = list_length (SCHEME_A_ SCHEME_V->args); |
4413 | int len = list_length (SCHEME_A_ args); |
4596 | |
4414 | |
4597 | if (len < 0) |
4415 | if (len < 0) |
4598 | Error_1 ("vector: not a proper list:", SCHEME_V->args); |
4416 | Error_1 ("vector: not a proper list:", args); |
4599 | |
4417 | |
4600 | vec = mk_vector (SCHEME_A_ len); |
4418 | vec = mk_vector (SCHEME_A_ len); |
4601 | |
4419 | |
4602 | #if USE_ERROR_CHECKING |
4420 | #if USE_ERROR_CHECKING |
4603 | if (SCHEME_V->no_memory) |
4421 | if (SCHEME_V->no_memory) |
4604 | s_return (S_SINK); |
4422 | s_return (S_SINK); |
4605 | #endif |
4423 | #endif |
4606 | |
4424 | |
4607 | for (x = SCHEME_V->args, i = 0; is_pair (x); x = cdr (x), i++) |
4425 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4608 | set_vector_elem (vec, i, car (x)); |
4426 | vector_set (vec, i, car (x)); |
4609 | |
4427 | |
4610 | s_return (vec); |
4428 | s_return (vec); |
4611 | } |
4429 | } |
4612 | |
4430 | |
4613 | case OP_MKVECTOR: /* make-vector */ |
4431 | case OP_MKVECTOR: /* make-vector */ |
4614 | { |
4432 | { |
4615 | pointer fill = NIL; |
4433 | pointer fill = NIL; |
4616 | int len; |
|
|
4617 | pointer vec; |
4434 | pointer vec; |
|
|
4435 | int len = ivalue_unchecked (x); |
4618 | |
4436 | |
4619 | len = ivalue (car (SCHEME_V->args)); |
|
|
4620 | |
|
|
4621 | if (cdr (SCHEME_V->args) != NIL) |
4437 | if (cdr (args) != NIL) |
4622 | fill = cadr (SCHEME_V->args); |
4438 | fill = cadr (args); |
4623 | |
4439 | |
4624 | vec = mk_vector (SCHEME_A_ len); |
4440 | vec = mk_vector (SCHEME_A_ len); |
4625 | |
4441 | |
4626 | #if USE_ERROR_CHECKING |
4442 | #if USE_ERROR_CHECKING |
4627 | if (SCHEME_V->no_memory) |
4443 | if (SCHEME_V->no_memory) |
4628 | s_return (S_SINK); |
4444 | s_return (S_SINK); |
4629 | #endif |
4445 | #endif |
4630 | |
4446 | |
4631 | if (fill != NIL) |
4447 | if (fill != NIL) |
4632 | fill_vector (vec, fill); |
4448 | fill_vector (vec, 0, fill); |
4633 | |
4449 | |
4634 | s_return (vec); |
4450 | s_return (vec); |
4635 | } |
4451 | } |
4636 | |
4452 | |
4637 | case OP_VECLEN: /* vector-length */ |
4453 | case OP_VECLEN: /* vector-length */ |
4638 | s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4454 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
|
|
4455 | |
|
|
4456 | case OP_VECRESIZE: |
|
|
4457 | vector_resize (x, ivalue_unchecked (cadr (args)), caddr (args)); |
|
|
4458 | s_return (x); |
4639 | |
4459 | |
4640 | case OP_VECREF: /* vector-ref */ |
4460 | case OP_VECREF: /* vector-ref */ |
4641 | { |
4461 | { |
4642 | int index; |
|
|
4643 | |
|
|
4644 | index = ivalue (cadr (SCHEME_V->args)); |
4462 | int index = ivalue_unchecked (cadr (args)); |
4645 | |
4463 | |
4646 | if (index >= ivalue (car (SCHEME_V->args))) |
4464 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4647 | Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); |
4465 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4648 | |
4466 | |
4649 | s_return (vector_elem (car (SCHEME_V->args), index)); |
4467 | s_return (vector_get (x, index)); |
4650 | } |
4468 | } |
4651 | |
4469 | |
4652 | case OP_VECSET: /* vector-set! */ |
4470 | case OP_VECSET: /* vector-set! */ |
4653 | { |
4471 | { |
4654 | int index; |
4472 | int index = ivalue_unchecked (cadr (args)); |
4655 | |
4473 | |
4656 | if (is_immutable (car (SCHEME_V->args))) |
4474 | if (is_immutable (x)) |
4657 | Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); |
4475 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4658 | |
4476 | |
4659 | index = ivalue (cadr (SCHEME_V->args)); |
4477 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4660 | |
|
|
4661 | if (index >= ivalue (car (SCHEME_V->args))) |
|
|
4662 | Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); |
4478 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4663 | |
4479 | |
4664 | set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); |
4480 | vector_set (x, index, caddr (args)); |
4665 | s_return (car (SCHEME_V->args)); |
4481 | s_return (x); |
4666 | } |
4482 | } |
4667 | } |
4483 | } |
4668 | |
4484 | |
4669 | return S_T; |
4485 | if (USE_ERROR_CHECKING) abort (); |
4670 | } |
4486 | } |
4671 | |
4487 | |
4672 | INTERFACE int |
4488 | static int |
4673 | is_list (SCHEME_P_ pointer a) |
4489 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4674 | { |
4490 | { |
4675 | return list_length (SCHEME_A_ a) >= 0; |
4491 | pointer x = SCHEME_V->args; |
4676 | } |
|
|
4677 | |
4492 | |
4678 | /* Result is: |
4493 | for (;;) |
4679 | proper list: length |
|
|
4680 | circular list: -1 |
|
|
4681 | not even a pair: -2 |
|
|
4682 | dotted list: -2 minus length before dot |
|
|
4683 | */ |
|
|
4684 | INTERFACE int |
|
|
4685 | list_length (SCHEME_P_ pointer a) |
|
|
4686 | { |
|
|
4687 | int i = 0; |
|
|
4688 | pointer slow, fast; |
|
|
4689 | |
|
|
4690 | slow = fast = a; |
|
|
4691 | |
|
|
4692 | while (1) |
|
|
4693 | { |
4494 | { |
|
|
4495 | num v = nvalue (car (x)); |
|
|
4496 | x = cdr (x); |
|
|
4497 | |
4694 | if (fast == NIL) |
4498 | if (x == NIL) |
4695 | return i; |
4499 | break; |
4696 | |
4500 | |
4697 | if (!is_pair (fast)) |
4501 | int r = num_cmp (v, nvalue (car (x))); |
4698 | return -2 - i; |
|
|
4699 | |
4502 | |
4700 | fast = cdr (fast); |
4503 | switch (op) |
4701 | ++i; |
|
|
4702 | |
|
|
4703 | if (fast == NIL) |
|
|
4704 | return i; |
|
|
4705 | |
|
|
4706 | if (!is_pair (fast)) |
|
|
4707 | return -2 - i; |
|
|
4708 | |
|
|
4709 | ++i; |
|
|
4710 | fast = cdr (fast); |
|
|
4711 | |
|
|
4712 | /* Safe because we would have already returned if `fast' |
|
|
4713 | encountered a non-pair. */ |
|
|
4714 | slow = cdr (slow); |
|
|
4715 | |
|
|
4716 | if (fast == slow) |
|
|
4717 | { |
4504 | { |
4718 | /* the fast pointer has looped back around and caught up |
4505 | case OP_NUMEQ: r = r == 0; break; |
4719 | with the slow pointer, hence the structure is circular, |
4506 | case OP_LESS: r = r < 0; break; |
4720 | not of finite length, and therefore not a list */ |
4507 | case OP_GRE: r = r > 0; break; |
4721 | return -1; |
4508 | case OP_LEQ: r = r <= 0; break; |
|
|
4509 | case OP_GEQ: r = r >= 0; break; |
4722 | } |
4510 | } |
4723 | } |
|
|
4724 | } |
|
|
4725 | |
4511 | |
|
|
4512 | if (!r) |
|
|
4513 | s_return (S_F); |
|
|
4514 | } |
|
|
4515 | |
|
|
4516 | s_return (S_T); |
|
|
4517 | } |
|
|
4518 | |
4726 | static pointer |
4519 | static int |
4727 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4520 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4728 | { |
4521 | { |
4729 | pointer x; |
4522 | pointer args = SCHEME_V->args; |
4730 | num v; |
4523 | pointer a = car (args); |
4731 | int (*comp_func) (num, num); |
4524 | pointer d = cdr (args); |
|
|
4525 | int r; |
4732 | |
4526 | |
4733 | switch (op) |
4527 | switch (op) |
4734 | { |
4528 | { |
4735 | case OP_NOT: /* not */ |
4529 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4736 | s_retbool (is_false (car (SCHEME_V->args))); |
4530 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T ; break; |
|
|
4531 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
|
|
4532 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
|
|
4533 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4534 | case OP_GENSYMP: /* gensym? */ r = is_gensym (SCHEME_A_ a); break; |
|
|
4535 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
|
|
4536 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
|
|
4537 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
|
|
4538 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
|
|
4539 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4737 | |
4540 | |
4738 | case OP_BOOLP: /* boolean? */ |
|
|
4739 | s_retbool (car (SCHEME_V->args) == S_F || car (SCHEME_V->args) == S_T); |
|
|
4740 | |
|
|
4741 | case OP_EOFOBJP: /* boolean? */ |
|
|
4742 | s_retbool (car (SCHEME_V->args) == S_EOF); |
|
|
4743 | |
|
|
4744 | case OP_NULLP: /* null? */ |
|
|
4745 | s_retbool (car (SCHEME_V->args) == NIL); |
|
|
4746 | |
|
|
4747 | case OP_NUMEQ: /* = */ |
|
|
4748 | case OP_LESS: /* < */ |
|
|
4749 | case OP_GRE: /* > */ |
|
|
4750 | case OP_LEQ: /* <= */ |
|
|
4751 | case OP_GEQ: /* >= */ |
|
|
4752 | switch (op) |
|
|
4753 | { |
|
|
4754 | case OP_NUMEQ: |
|
|
4755 | comp_func = num_eq; |
|
|
4756 | break; |
|
|
4757 | |
|
|
4758 | case OP_LESS: |
|
|
4759 | comp_func = num_lt; |
|
|
4760 | break; |
|
|
4761 | |
|
|
4762 | case OP_GRE: |
|
|
4763 | comp_func = num_gt; |
|
|
4764 | break; |
|
|
4765 | |
|
|
4766 | case OP_LEQ: |
|
|
4767 | comp_func = num_le; |
|
|
4768 | break; |
|
|
4769 | |
|
|
4770 | case OP_GEQ: |
|
|
4771 | comp_func = num_ge; |
|
|
4772 | break; |
|
|
4773 | } |
|
|
4774 | |
|
|
4775 | x = SCHEME_V->args; |
|
|
4776 | v = nvalue (car (x)); |
|
|
4777 | x = cdr (x); |
|
|
4778 | |
|
|
4779 | for (; x != NIL; x = cdr (x)) |
|
|
4780 | { |
|
|
4781 | if (!comp_func (v, nvalue (car (x)))) |
|
|
4782 | s_retbool (0); |
|
|
4783 | |
|
|
4784 | v = nvalue (car (x)); |
|
|
4785 | } |
|
|
4786 | |
|
|
4787 | s_retbool (1); |
|
|
4788 | |
|
|
4789 | case OP_SYMBOLP: /* symbol? */ |
|
|
4790 | s_retbool (is_symbol (car (SCHEME_V->args))); |
|
|
4791 | |
|
|
4792 | case OP_NUMBERP: /* number? */ |
|
|
4793 | s_retbool (is_number (car (SCHEME_V->args))); |
|
|
4794 | |
|
|
4795 | case OP_STRINGP: /* string? */ |
|
|
4796 | s_retbool (is_string (car (SCHEME_V->args))); |
|
|
4797 | |
|
|
4798 | case OP_INTEGERP: /* integer? */ |
|
|
4799 | s_retbool (is_integer (car (SCHEME_V->args))); |
|
|
4800 | |
|
|
4801 | case OP_REALP: /* real? */ |
|
|
4802 | s_retbool (is_number (car (SCHEME_V->args))); /* All numbers are real */ |
|
|
4803 | |
|
|
4804 | case OP_CHARP: /* char? */ |
|
|
4805 | s_retbool (is_character (car (SCHEME_V->args))); |
|
|
4806 | #if USE_CHAR_CLASSIFIERS |
4541 | #if USE_CHAR_CLASSIFIERS |
4807 | |
4542 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue_unchecked (a)); break; |
4808 | case OP_CHARAP: /* char-alphabetic? */ |
4543 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue_unchecked (a)); break; |
4809 | s_retbool (Cisalpha (ivalue (car (SCHEME_V->args)))); |
4544 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue_unchecked (a)); break; |
4810 | |
4545 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue_unchecked (a)); break; |
4811 | case OP_CHARNP: /* char-numeric? */ |
4546 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue_unchecked (a)); break; |
4812 | s_retbool (Cisdigit (ivalue (car (SCHEME_V->args)))); |
|
|
4813 | |
|
|
4814 | case OP_CHARWP: /* char-whitespace? */ |
|
|
4815 | s_retbool (Cisspace (ivalue (car (SCHEME_V->args)))); |
|
|
4816 | |
|
|
4817 | case OP_CHARUP: /* char-upper-case? */ |
|
|
4818 | s_retbool (Cisupper (ivalue (car (SCHEME_V->args)))); |
|
|
4819 | |
|
|
4820 | case OP_CHARLP: /* char-lower-case? */ |
|
|
4821 | s_retbool (Cislower (ivalue (car (SCHEME_V->args)))); |
|
|
4822 | #endif |
4547 | #endif |
|
|
4548 | |
4823 | #if USE_PORTS |
4549 | #if USE_PORTS |
4824 | |
4550 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4825 | case OP_PORTP: /* port? */ |
|
|
4826 | s_retbool (is_port (car (SCHEME_V->args))); |
|
|
4827 | |
|
|
4828 | case OP_INPORTP: /* input-port? */ |
4551 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
4829 | s_retbool (is_inport (car (SCHEME_V->args))); |
|
|
4830 | |
|
|
4831 | case OP_OUTPORTP: /* output-port? */ |
4552 | case OP_OUTPORTP: /* output-port? */ r = is_outport (a); break; |
4832 | s_retbool (is_outport (car (SCHEME_V->args))); |
|
|
4833 | #endif |
4553 | #endif |
4834 | |
4554 | |
4835 | case OP_PROCP: /* procedure? */ |
4555 | case OP_PROCP: /* procedure? */ |
4836 | |
4556 | |
4837 | /*-- |
4557 | /*-- |
4838 | * continuation should be procedure by the example |
4558 | * continuation should be procedure by the example |
4839 | * (call-with-current-continuation procedure?) ==> #t |
4559 | * (call-with-current-continuation procedure?) ==> #t |
4840 | * in R^3 report sec. 6.9 |
4560 | * in R^3 report sec. 6.9 |
4841 | */ |
4561 | */ |
4842 | s_retbool (is_proc (car (SCHEME_V->args)) || is_closure (car (SCHEME_V->args)) |
4562 | r = is_proc (a) || is_closure (a) || is_continuation (a) || is_foreign (a); |
4843 | || is_continuation (car (SCHEME_V->args)) || is_foreign (car (SCHEME_V->args))); |
4563 | break; |
4844 | |
4564 | |
4845 | case OP_PAIRP: /* pair? */ |
4565 | case OP_PAIRP: /* pair? */ r = is_pair (a) ; break; |
4846 | s_retbool (is_pair (car (SCHEME_V->args))); |
4566 | case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break; |
4847 | |
4567 | case OP_ENVP: /* environment? */ r = is_environment (a) ; break; |
4848 | case OP_LISTP: /* list? */ |
4568 | case OP_VECTORP: /* vector? */ r = is_vector (a) ; break; |
4849 | s_retbool (list_length (SCHEME_A_ car (SCHEME_V->args)) >= 0); |
4569 | case OP_EQ: /* eq? */ r = a == cadr (args) ; break; |
4850 | |
4570 | case OP_EQV: /* eqv? */ r = eqv (a, cadr (args)) ; break; |
4851 | case OP_ENVP: /* environment? */ |
|
|
4852 | s_retbool (is_environment (car (SCHEME_V->args))); |
|
|
4853 | |
|
|
4854 | case OP_VECTORP: /* vector? */ |
|
|
4855 | s_retbool (is_vector (car (SCHEME_V->args))); |
|
|
4856 | |
|
|
4857 | case OP_EQ: /* eq? */ |
|
|
4858 | s_retbool (car (SCHEME_V->args) == cadr (SCHEME_V->args)); |
|
|
4859 | |
|
|
4860 | case OP_EQV: /* eqv? */ |
|
|
4861 | s_retbool (eqv (car (SCHEME_V->args), cadr (SCHEME_V->args))); |
|
|
4862 | } |
4571 | } |
4863 | |
4572 | |
4864 | return S_T; |
4573 | s_retbool (r); |
4865 | } |
4574 | } |
4866 | |
4575 | |
4867 | static pointer |
4576 | static int |
4868 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4577 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4869 | { |
4578 | { |
|
|
4579 | pointer args = SCHEME_V->args; |
|
|
4580 | pointer a = car (args); |
4870 | pointer x, y; |
4581 | pointer x, y; |
4871 | |
4582 | |
4872 | switch (op) |
4583 | switch (op) |
4873 | { |
4584 | { |
4874 | case OP_FORCE: /* force */ |
4585 | case OP_FORCE: /* force */ |
4875 | SCHEME_V->code = car (SCHEME_V->args); |
4586 | SCHEME_V->code = a; |
4876 | |
4587 | |
4877 | if (is_promise (SCHEME_V->code)) |
4588 | if (is_promise (SCHEME_V->code)) |
4878 | { |
4589 | { |
4879 | /* Should change type to closure here */ |
4590 | /* Should change type to closure here */ |
4880 | s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); |
4591 | s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); |
… | |
… | |
4883 | } |
4594 | } |
4884 | else |
4595 | else |
4885 | s_return (SCHEME_V->code); |
4596 | s_return (SCHEME_V->code); |
4886 | |
4597 | |
4887 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4598 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4888 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4599 | *CELL (SCHEME_V->code) = *CELL (SCHEME_V->value); |
4889 | s_return (SCHEME_V->value); |
4600 | s_return (SCHEME_V->value); |
4890 | |
4601 | |
4891 | #if USE_PORTS |
4602 | #if USE_PORTS |
|
|
4603 | |
|
|
4604 | case OP_EOF_OBJECT: /* eof-object */ |
|
|
4605 | s_return (S_EOF); |
4892 | |
4606 | |
4893 | case OP_WRITE: /* write */ |
4607 | case OP_WRITE: /* write */ |
4894 | case OP_DISPLAY: /* display */ |
4608 | case OP_DISPLAY: /* display */ |
4895 | case OP_WRITE_CHAR: /* write-char */ |
4609 | case OP_WRITE_CHAR: /* write-char */ |
4896 | if (is_pair (cdr (SCHEME_V->args))) |
4610 | if (is_pair (cdr (SCHEME_V->args))) |
… | |
… | |
4901 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4615 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4902 | SCHEME_V->outport = cadr (SCHEME_V->args); |
4616 | SCHEME_V->outport = cadr (SCHEME_V->args); |
4903 | } |
4617 | } |
4904 | } |
4618 | } |
4905 | |
4619 | |
4906 | SCHEME_V->args = car (SCHEME_V->args); |
4620 | SCHEME_V->args = a; |
4907 | |
4621 | |
4908 | if (op == OP_WRITE) |
4622 | if (op == OP_WRITE) |
4909 | SCHEME_V->print_flag = 1; |
4623 | SCHEME_V->print_flag = 1; |
4910 | else |
4624 | else |
4911 | SCHEME_V->print_flag = 0; |
4625 | SCHEME_V->print_flag = 0; |
4912 | |
4626 | |
4913 | s_goto (OP_P0LIST); |
4627 | s_goto (OP_P0LIST); |
4914 | |
4628 | |
4915 | case OP_NEWLINE: /* newline */ |
4629 | case OP_NEWLINE: /* newline */ |
4916 | if (is_pair (SCHEME_V->args)) |
4630 | if (is_pair (args)) |
4917 | { |
4631 | { |
4918 | if (car (SCHEME_V->args) != SCHEME_V->outport) |
4632 | if (a != SCHEME_V->outport) |
4919 | { |
4633 | { |
4920 | x = cons (SCHEME_V->outport, NIL); |
4634 | x = cons (SCHEME_V->outport, NIL); |
4921 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4635 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4922 | SCHEME_V->outport = car (SCHEME_V->args); |
4636 | SCHEME_V->outport = a; |
4923 | } |
4637 | } |
4924 | } |
4638 | } |
4925 | |
4639 | |
4926 | putstr (SCHEME_A_ "\n"); |
4640 | putstr (SCHEME_A_ "\n"); |
4927 | s_return (S_T); |
4641 | s_return (S_T); |
4928 | #endif |
4642 | #endif |
4929 | |
4643 | |
4930 | case OP_ERR0: /* error */ |
4644 | case OP_ERR0: /* error */ |
4931 | SCHEME_V->retcode = -1; |
4645 | SCHEME_V->retcode = -1; |
4932 | |
4646 | |
4933 | if (!is_string (car (SCHEME_V->args))) |
4647 | if (!is_string (a)) |
4934 | { |
4648 | { |
4935 | SCHEME_V->args = cons (mk_string (SCHEME_A_ " -- "), SCHEME_V->args); |
4649 | args = cons (mk_string (SCHEME_A_ " -- "), args); |
4936 | setimmutable (car (SCHEME_V->args)); |
4650 | setimmutable (car (args)); |
4937 | } |
4651 | } |
4938 | |
4652 | |
4939 | putstr (SCHEME_A_ "Error: "); |
4653 | putstr (SCHEME_A_ "Error: "); |
4940 | putstr (SCHEME_A_ strvalue (car (SCHEME_V->args))); |
4654 | putstr (SCHEME_A_ strvalue (car (args))); |
4941 | SCHEME_V->args = cdr (SCHEME_V->args); |
4655 | SCHEME_V->args = cdr (args); |
4942 | s_goto (OP_ERR1); |
4656 | s_goto (OP_ERR1); |
4943 | |
4657 | |
4944 | case OP_ERR1: /* error */ |
4658 | case OP_ERR1: /* error */ |
4945 | putstr (SCHEME_A_ " "); |
4659 | putstr (SCHEME_A_ " "); |
4946 | |
4660 | |
4947 | if (SCHEME_V->args != NIL) |
4661 | if (args != NIL) |
4948 | { |
4662 | { |
4949 | s_save (SCHEME_A_ OP_ERR1, cdr (SCHEME_V->args), NIL); |
4663 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4950 | SCHEME_V->args = car (SCHEME_V->args); |
4664 | SCHEME_V->args = a; |
4951 | SCHEME_V->print_flag = 1; |
4665 | SCHEME_V->print_flag = 1; |
4952 | s_goto (OP_P0LIST); |
4666 | s_goto (OP_P0LIST); |
4953 | } |
4667 | } |
4954 | else |
4668 | else |
4955 | { |
4669 | { |
4956 | putstr (SCHEME_A_ "\n"); |
4670 | putstr (SCHEME_A_ "\n"); |
4957 | |
4671 | |
4958 | if (SCHEME_V->interactive_repl) |
4672 | if (SCHEME_V->interactive_repl) |
4959 | s_goto (OP_T0LVL); |
4673 | s_goto (OP_T0LVL); |
4960 | else |
4674 | else |
4961 | return NIL; |
4675 | return -1; |
4962 | } |
4676 | } |
4963 | |
4677 | |
4964 | case OP_REVERSE: /* reverse */ |
4678 | case OP_REVERSE: /* reverse */ |
4965 | s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); |
4679 | s_return (reverse (SCHEME_A_ a)); |
4966 | |
4680 | |
4967 | case OP_LIST_STAR: /* list* */ |
4681 | case OP_LIST_STAR: /* list* */ |
4968 | s_return (list_star (SCHEME_A_ SCHEME_V->args)); |
4682 | s_return (list_star (SCHEME_A_ SCHEME_V->args)); |
4969 | |
4683 | |
4970 | case OP_APPEND: /* append */ |
4684 | case OP_APPEND: /* append */ |
4971 | x = NIL; |
4685 | x = NIL; |
4972 | y = SCHEME_V->args; |
4686 | y = args; |
4973 | |
4687 | |
4974 | if (y == x) |
4688 | if (y == x) |
4975 | s_return (x); |
4689 | s_return (x); |
4976 | |
4690 | |
4977 | /* cdr() in the while condition is not a typo. If car() */ |
4691 | /* cdr() in the while condition is not a typo. If car() */ |
… | |
… | |
4988 | s_return (reverse_in_place (SCHEME_A_ car (y), x)); |
4702 | s_return (reverse_in_place (SCHEME_A_ car (y), x)); |
4989 | |
4703 | |
4990 | #if USE_PLIST |
4704 | #if USE_PLIST |
4991 | |
4705 | |
4992 | case OP_PUT: /* put */ |
4706 | case OP_PUT: /* put */ |
4993 | if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) |
4707 | if (!hasprop (a) || !hasprop (cadr (args))) |
4994 | Error_0 ("illegal use of put"); |
4708 | Error_0 ("illegal use of put"); |
4995 | |
4709 | |
4996 | for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) |
4710 | for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x)) |
4997 | { |
4711 | { |
4998 | if (caar (x) == y) |
4712 | if (caar (x) == y) |
4999 | break; |
4713 | break; |
5000 | } |
4714 | } |
5001 | |
4715 | |
5002 | if (x != NIL) |
4716 | if (x != NIL) |
5003 | cdar (x) = caddr (SCHEME_V->args); |
4717 | cdar (x) = caddr (args); |
5004 | else |
4718 | else |
5005 | symprop (car (SCHEME_V->args)) = cons (cons (y, caddr (SCHEME_V->args)), symprop (car (SCHEME_V->args))); |
4719 | symprop (a) = cons (cons (y, caddr (args)), symprop (a)); |
5006 | |
4720 | |
5007 | s_return (S_T); |
4721 | s_return (S_T); |
5008 | |
4722 | |
5009 | case OP_GET: /* get */ |
4723 | case OP_GET: /* get */ |
5010 | if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) |
4724 | if (!hasprop (a) || !hasprop (cadr (args))) |
5011 | Error_0 ("illegal use of get"); |
4725 | Error_0 ("illegal use of get"); |
5012 | |
4726 | |
5013 | for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) |
4727 | for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x)) |
5014 | if (caar (x) == y) |
4728 | if (caar (x) == y) |
5015 | break; |
4729 | break; |
5016 | |
4730 | |
5017 | if (x != NIL) |
4731 | if (x != NIL) |
5018 | s_return (cdar (x)); |
4732 | s_return (cdar (x)); |
… | |
… | |
5020 | s_return (NIL); |
4734 | s_return (NIL); |
5021 | |
4735 | |
5022 | #endif /* USE_PLIST */ |
4736 | #endif /* USE_PLIST */ |
5023 | |
4737 | |
5024 | case OP_QUIT: /* quit */ |
4738 | case OP_QUIT: /* quit */ |
5025 | if (is_pair (SCHEME_V->args)) |
4739 | if (is_pair (args)) |
5026 | SCHEME_V->retcode = ivalue (car (SCHEME_V->args)); |
4740 | SCHEME_V->retcode = ivalue (a); |
5027 | |
4741 | |
5028 | return NIL; |
4742 | return -1; |
5029 | |
4743 | |
5030 | case OP_GC: /* gc */ |
4744 | case OP_GC: /* gc */ |
5031 | gc (SCHEME_A_ NIL, NIL); |
4745 | gc (SCHEME_A_ NIL, NIL); |
5032 | s_return (S_T); |
4746 | s_return (S_T); |
5033 | |
4747 | |
5034 | case OP_GCVERB: /* gc-verbose */ |
4748 | case OP_GCVERB: /* gc-verbose */ |
5035 | { |
4749 | { |
5036 | int was = SCHEME_V->gc_verbose; |
4750 | int was = SCHEME_V->gc_verbose; |
5037 | |
4751 | |
5038 | SCHEME_V->gc_verbose = (car (SCHEME_V->args) != S_F); |
4752 | SCHEME_V->gc_verbose = (a != S_F); |
5039 | s_retbool (was); |
4753 | s_retbool (was); |
5040 | } |
4754 | } |
5041 | |
4755 | |
5042 | case OP_NEWSEGMENT: /* new-segment */ |
4756 | case OP_NEWSEGMENT: /* new-segment */ |
|
|
4757 | #if 0 |
5043 | if (!is_pair (SCHEME_V->args) || !is_number (car (SCHEME_V->args))) |
4758 | if (!is_pair (args) || !is_number (a)) |
5044 | Error_0 ("new-segment: argument must be a number"); |
4759 | Error_0 ("new-segment: argument must be a number"); |
5045 | |
4760 | #endif |
5046 | alloc_cellseg (SCHEME_A_ (int)ivalue (car (SCHEME_V->args))); |
4761 | s_retbool (alloc_cellseg (SCHEME_A)); |
5047 | |
|
|
5048 | s_return (S_T); |
|
|
5049 | |
4762 | |
5050 | case OP_OBLIST: /* oblist */ |
4763 | case OP_OBLIST: /* oblist */ |
5051 | s_return (oblist_all_symbols (SCHEME_A)); |
4764 | s_return (oblist_all_symbols (SCHEME_A)); |
5052 | |
4765 | |
5053 | #if USE_PORTS |
4766 | #if USE_PORTS |
… | |
… | |
5078 | case OP_OPEN_INOUTFILE: |
4791 | case OP_OPEN_INOUTFILE: |
5079 | prop = port_input | port_output; |
4792 | prop = port_input | port_output; |
5080 | break; |
4793 | break; |
5081 | } |
4794 | } |
5082 | |
4795 | |
5083 | p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); |
4796 | p = port_from_filename (SCHEME_A_ strvalue (a), prop); |
5084 | |
4797 | |
5085 | if (p == NIL) |
4798 | s_return (p == NIL ? S_F : p); |
5086 | s_return (S_F); |
|
|
5087 | |
|
|
5088 | s_return (p); |
|
|
5089 | } |
4799 | } |
5090 | |
4800 | |
5091 | # if USE_STRING_PORTS |
4801 | # if USE_STRING_PORTS |
5092 | |
4802 | |
5093 | case OP_OPEN_INSTRING: /* open-input-string */ |
4803 | case OP_OPEN_INSTRING: /* open-input-string */ |
… | |
… | |
5105 | case OP_OPEN_INOUTSTRING: |
4815 | case OP_OPEN_INOUTSTRING: |
5106 | prop = port_input | port_output; |
4816 | prop = port_input | port_output; |
5107 | break; |
4817 | break; |
5108 | } |
4818 | } |
5109 | |
4819 | |
5110 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
4820 | p = port_from_string (SCHEME_A_ strvalue (a), |
5111 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); |
4821 | strvalue (a) + strlength (a), prop); |
5112 | |
4822 | |
5113 | if (p == NIL) |
4823 | s_return (p == NIL ? S_F : p); |
5114 | s_return (S_F); |
|
|
5115 | |
|
|
5116 | s_return (p); |
|
|
5117 | } |
4824 | } |
5118 | |
4825 | |
5119 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4826 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
5120 | { |
4827 | { |
5121 | pointer p; |
4828 | pointer p; |
5122 | |
4829 | |
5123 | if (car (SCHEME_V->args) == NIL) |
4830 | if (a == NIL) |
5124 | { |
|
|
5125 | p = port_from_scratch (SCHEME_A); |
4831 | p = port_from_scratch (SCHEME_A); |
5126 | |
|
|
5127 | if (p == NIL) |
|
|
5128 | s_return (S_F); |
|
|
5129 | } |
|
|
5130 | else |
4832 | else |
5131 | { |
|
|
5132 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
4833 | p = port_from_string (SCHEME_A_ strvalue (a), |
5133 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); |
4834 | strvalue (a) + strlength (a), port_output); |
5134 | |
4835 | |
5135 | if (p == NIL) |
4836 | s_return (p == NIL ? S_F : p); |
5136 | s_return (S_F); |
|
|
5137 | } |
|
|
5138 | |
|
|
5139 | s_return (p); |
|
|
5140 | } |
4837 | } |
5141 | |
4838 | |
5142 | case OP_GET_OUTSTRING: /* get-output-string */ |
4839 | case OP_GET_OUTSTRING: /* get-output-string */ |
5143 | { |
4840 | { |
5144 | port *p; |
4841 | port *p = port (a); |
5145 | |
4842 | |
5146 | if ((p = car (SCHEME_V->args)->object.port)->kind & port_string) |
4843 | if (p->kind & port_string) |
5147 | { |
4844 | { |
5148 | off_t size; |
4845 | off_t size; |
5149 | char *str; |
4846 | char *str; |
5150 | |
4847 | |
5151 | size = p->rep.string.curr - p->rep.string.start + 1; |
4848 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
5167 | } |
4864 | } |
5168 | |
4865 | |
5169 | # endif |
4866 | # endif |
5170 | |
4867 | |
5171 | case OP_CLOSE_INPORT: /* close-input-port */ |
4868 | case OP_CLOSE_INPORT: /* close-input-port */ |
5172 | port_close (SCHEME_A_ car (SCHEME_V->args), port_input); |
4869 | port_close (SCHEME_A_ a, port_input); |
5173 | s_return (S_T); |
4870 | s_return (S_T); |
5174 | |
4871 | |
5175 | case OP_CLOSE_OUTPORT: /* close-output-port */ |
4872 | case OP_CLOSE_OUTPORT: /* close-output-port */ |
5176 | port_close (SCHEME_A_ car (SCHEME_V->args), port_output); |
4873 | port_close (SCHEME_A_ a, port_output); |
5177 | s_return (S_T); |
4874 | s_return (S_T); |
5178 | #endif |
4875 | #endif |
5179 | |
4876 | |
5180 | case OP_INT_ENV: /* interaction-environment */ |
4877 | case OP_INT_ENV: /* interaction-environment */ |
5181 | s_return (SCHEME_V->global_env); |
4878 | s_return (SCHEME_V->global_env); |
… | |
… | |
5183 | case OP_CURR_ENV: /* current-environment */ |
4880 | case OP_CURR_ENV: /* current-environment */ |
5184 | s_return (SCHEME_V->envir); |
4881 | s_return (SCHEME_V->envir); |
5185 | |
4882 | |
5186 | } |
4883 | } |
5187 | |
4884 | |
5188 | return S_T; |
4885 | if (USE_ERROR_CHECKING) abort (); |
5189 | } |
4886 | } |
5190 | |
4887 | |
5191 | static pointer |
4888 | static int |
5192 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4889 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5193 | { |
4890 | { |
|
|
4891 | pointer args = SCHEME_V->args; |
5194 | pointer x; |
4892 | pointer x; |
5195 | |
4893 | |
5196 | if (SCHEME_V->nesting != 0) |
4894 | if (SCHEME_V->nesting != 0) |
5197 | { |
4895 | { |
5198 | int n = SCHEME_V->nesting; |
4896 | int n = SCHEME_V->nesting; |
… | |
… | |
5205 | switch (op) |
4903 | switch (op) |
5206 | { |
4904 | { |
5207 | /* ========== reading part ========== */ |
4905 | /* ========== reading part ========== */ |
5208 | #if USE_PORTS |
4906 | #if USE_PORTS |
5209 | case OP_READ: |
4907 | case OP_READ: |
5210 | if (!is_pair (SCHEME_V->args)) |
4908 | if (!is_pair (args)) |
5211 | s_goto (OP_READ_INTERNAL); |
4909 | s_goto (OP_READ_INTERNAL); |
5212 | |
4910 | |
5213 | if (!is_inport (car (SCHEME_V->args))) |
4911 | if (!is_inport (car (args))) |
5214 | Error_1 ("read: not an input port:", car (SCHEME_V->args)); |
4912 | Error_1 ("read: not an input port:", car (args)); |
5215 | |
4913 | |
5216 | if (car (SCHEME_V->args) == SCHEME_V->inport) |
4914 | if (car (args) == SCHEME_V->inport) |
5217 | s_goto (OP_READ_INTERNAL); |
4915 | s_goto (OP_READ_INTERNAL); |
5218 | |
4916 | |
5219 | x = SCHEME_V->inport; |
4917 | x = SCHEME_V->inport; |
5220 | SCHEME_V->inport = car (SCHEME_V->args); |
4918 | SCHEME_V->inport = car (args); |
5221 | x = cons (x, NIL); |
4919 | x = cons (x, NIL); |
5222 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
4920 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
5223 | s_goto (OP_READ_INTERNAL); |
4921 | s_goto (OP_READ_INTERNAL); |
5224 | |
4922 | |
5225 | case OP_READ_CHAR: /* read-char */ |
4923 | case OP_READ_CHAR: /* read-char */ |
5226 | case OP_PEEK_CHAR: /* peek-char */ |
4924 | case OP_PEEK_CHAR: /* peek-char */ |
5227 | { |
4925 | { |
5228 | int c; |
4926 | int c; |
5229 | |
4927 | |
5230 | if (is_pair (SCHEME_V->args)) |
4928 | if (is_pair (args)) |
5231 | { |
4929 | { |
5232 | if (car (SCHEME_V->args) != SCHEME_V->inport) |
4930 | if (car (args) != SCHEME_V->inport) |
5233 | { |
4931 | { |
5234 | x = SCHEME_V->inport; |
4932 | x = SCHEME_V->inport; |
5235 | x = cons (x, NIL); |
4933 | x = cons (x, NIL); |
5236 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
4934 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
5237 | SCHEME_V->inport = car (SCHEME_V->args); |
4935 | SCHEME_V->inport = car (args); |
5238 | } |
4936 | } |
5239 | } |
4937 | } |
5240 | |
4938 | |
5241 | c = inchar (SCHEME_A); |
4939 | c = inchar (SCHEME_A); |
5242 | |
4940 | |
… | |
… | |
5252 | case OP_CHAR_READY: /* char-ready? */ |
4950 | case OP_CHAR_READY: /* char-ready? */ |
5253 | { |
4951 | { |
5254 | pointer p = SCHEME_V->inport; |
4952 | pointer p = SCHEME_V->inport; |
5255 | int res; |
4953 | int res; |
5256 | |
4954 | |
5257 | if (is_pair (SCHEME_V->args)) |
4955 | if (is_pair (args)) |
5258 | p = car (SCHEME_V->args); |
4956 | p = car (args); |
5259 | |
4957 | |
5260 | res = p->object.port->kind & port_string; |
4958 | res = port (p)->kind & port_string; |
5261 | |
4959 | |
5262 | s_retbool (res); |
4960 | s_retbool (res); |
5263 | } |
4961 | } |
5264 | |
4962 | |
5265 | case OP_SET_INPORT: /* set-input-port */ |
4963 | case OP_SET_INPORT: /* set-input-port */ |
5266 | SCHEME_V->inport = car (SCHEME_V->args); |
4964 | SCHEME_V->inport = car (args); |
5267 | s_return (SCHEME_V->value); |
4965 | s_return (SCHEME_V->value); |
5268 | |
4966 | |
5269 | case OP_SET_OUTPORT: /* set-output-port */ |
4967 | case OP_SET_OUTPORT: /* set-output-port */ |
5270 | SCHEME_V->outport = car (SCHEME_V->args); |
4968 | SCHEME_V->outport = car (args); |
5271 | s_return (SCHEME_V->value); |
4969 | s_return (SCHEME_V->value); |
5272 | #endif |
4970 | #endif |
5273 | |
4971 | |
5274 | case OP_RDSEXPR: |
4972 | case OP_RDSEXPR: |
5275 | switch (SCHEME_V->tok) |
4973 | switch (SCHEME_V->tok) |
… | |
… | |
5324 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5022 | s_save (SCHEME_A_ OP_RDUQTSP, NIL, NIL); |
5325 | SCHEME_V->tok = token (SCHEME_A); |
5023 | SCHEME_V->tok = token (SCHEME_A); |
5326 | s_goto (OP_RDSEXPR); |
5024 | s_goto (OP_RDSEXPR); |
5327 | |
5025 | |
5328 | case TOK_ATOM: |
5026 | case TOK_ATOM: |
5329 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))); |
5027 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))); |
|
|
5028 | |
|
|
5029 | case TOK_DOTATOM: |
|
|
5030 | SCHEME_V->strbuff[0] = '.'; |
|
|
5031 | s_return (mk_atom (SCHEME_A_ readstr_upto (SCHEME_A_ 1, DELIMITERS))); |
|
|
5032 | |
|
|
5033 | case TOK_STRATOM: |
|
|
5034 | x = readstrexp (SCHEME_A_ '|'); |
|
|
5035 | //TODO: haven't checked whether the garbage collector could interfere |
|
|
5036 | s_return (mk_atom (SCHEME_A_ strvalue (x))); |
5330 | |
5037 | |
5331 | case TOK_DQUOTE: |
5038 | case TOK_DQUOTE: |
5332 | x = readstrexp (SCHEME_A); |
5039 | x = readstrexp (SCHEME_A_ '"'); |
5333 | |
5040 | |
5334 | if (x == S_F) |
5041 | if (x == S_F) |
5335 | Error_0 ("Error reading string"); |
5042 | Error_0 ("Error reading string"); |
5336 | |
5043 | |
5337 | setimmutable (x); |
5044 | setimmutable (x); |
… | |
… | |
5349 | s_goto (OP_EVAL); |
5056 | s_goto (OP_EVAL); |
5350 | } |
5057 | } |
5351 | } |
5058 | } |
5352 | |
5059 | |
5353 | case TOK_SHARP_CONST: |
5060 | case TOK_SHARP_CONST: |
5354 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ DELIMITERS))) == NIL) |
5061 | if ((x = mk_sharp_const (SCHEME_A_ readstr_upto (SCHEME_A_ 0, DELIMITERS))) == NIL) |
5355 | Error_0 ("undefined sharp expression"); |
5062 | Error_0 ("undefined sharp expression"); |
5356 | else |
5063 | else |
5357 | s_return (x); |
5064 | s_return (x); |
5358 | |
5065 | |
5359 | default: |
5066 | default: |
… | |
… | |
5361 | } |
5068 | } |
5362 | |
5069 | |
5363 | break; |
5070 | break; |
5364 | |
5071 | |
5365 | case OP_RDLIST: |
5072 | case OP_RDLIST: |
5366 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
5073 | SCHEME_V->args = cons (SCHEME_V->value, args); |
5367 | SCHEME_V->tok = token (SCHEME_A); |
5074 | SCHEME_V->tok = token (SCHEME_A); |
5368 | |
5075 | |
5369 | switch (SCHEME_V->tok) |
5076 | switch (SCHEME_V->tok) |
5370 | { |
5077 | { |
5371 | case TOK_EOF: |
5078 | case TOK_EOF: |
… | |
… | |
5399 | case OP_RDDOT: |
5106 | case OP_RDDOT: |
5400 | if (token (SCHEME_A) != TOK_RPAREN) |
5107 | if (token (SCHEME_A) != TOK_RPAREN) |
5401 | Error_0 ("syntax error: illegal dot expression"); |
5108 | Error_0 ("syntax error: illegal dot expression"); |
5402 | |
5109 | |
5403 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5110 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5404 | s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); |
5111 | s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args)); |
5405 | |
5112 | |
5406 | case OP_RDQUOTE: |
5113 | case OP_RDQUOTE: |
5407 | s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); |
5114 | s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); |
5408 | |
5115 | |
5409 | case OP_RDQQUOTE: |
5116 | case OP_RDQQUOTE: |
… | |
… | |
5431 | SCHEME_V->args = SCHEME_V->value; |
5138 | SCHEME_V->args = SCHEME_V->value; |
5432 | s_goto (OP_VECTOR); |
5139 | s_goto (OP_VECTOR); |
5433 | |
5140 | |
5434 | /* ========== printing part ========== */ |
5141 | /* ========== printing part ========== */ |
5435 | case OP_P0LIST: |
5142 | case OP_P0LIST: |
5436 | if (is_vector (SCHEME_V->args)) |
5143 | if (is_vector (args)) |
5437 | { |
5144 | { |
5438 | putstr (SCHEME_A_ "#("); |
5145 | putstr (SCHEME_A_ "#("); |
5439 | SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); |
5146 | SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0)); |
5440 | s_goto (OP_PVECFROM); |
5147 | s_goto (OP_PVECFROM); |
5441 | } |
5148 | } |
5442 | else if (is_environment (SCHEME_V->args)) |
5149 | else if (is_environment (args)) |
5443 | { |
5150 | { |
5444 | putstr (SCHEME_A_ "#<ENVIRONMENT>"); |
5151 | putstr (SCHEME_A_ "#<ENVIRONMENT>"); |
5445 | s_return (S_T); |
5152 | s_return (S_T); |
5446 | } |
5153 | } |
5447 | else if (!is_pair (SCHEME_V->args)) |
5154 | else if (!is_pair (args)) |
5448 | { |
5155 | { |
5449 | printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); |
5156 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5450 | s_return (S_T); |
5157 | s_return (S_T); |
5451 | } |
5158 | } |
5452 | else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) |
5159 | else |
5453 | { |
5160 | { |
|
|
5161 | pointer a = car (args); |
|
|
5162 | pointer b = cdr (args); |
|
|
5163 | int ok_abbr = ok_abbrev (b); |
|
|
5164 | SCHEME_V->args = car (b); |
|
|
5165 | |
|
|
5166 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5454 | putstr (SCHEME_A_ "'"); |
5167 | putstr (SCHEME_A_ "'"); |
5455 | SCHEME_V->args = cadr (SCHEME_V->args); |
5168 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
|
|
5169 | putstr (SCHEME_A_ "`"); |
|
|
5170 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
|
|
5171 | putstr (SCHEME_A_ ","); |
|
|
5172 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
|
|
5173 | putstr (SCHEME_A_ ",@"); |
|
|
5174 | else |
|
|
5175 | { |
|
|
5176 | putstr (SCHEME_A_ "("); |
|
|
5177 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
|
|
5178 | SCHEME_V->args = a; |
|
|
5179 | } |
|
|
5180 | |
5456 | s_goto (OP_P0LIST); |
5181 | s_goto (OP_P0LIST); |
5457 | } |
5182 | } |
5458 | else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) |
5183 | |
|
|
5184 | case OP_P1LIST: |
|
|
5185 | if (is_pair (args)) |
5459 | { |
5186 | { |
|
|
5187 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5460 | putstr (SCHEME_A_ "`"); |
5188 | putstr (SCHEME_A_ " "); |
5461 | SCHEME_V->args = cadr (SCHEME_V->args); |
5189 | SCHEME_V->args = car (args); |
5462 | s_goto (OP_P0LIST); |
5190 | s_goto (OP_P0LIST); |
5463 | } |
5191 | } |
5464 | else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTE && ok_abbrev (cdr (SCHEME_V->args))) |
|
|
5465 | { |
|
|
5466 | putstr (SCHEME_A_ ","); |
|
|
5467 | SCHEME_V->args = cadr (SCHEME_V->args); |
|
|
5468 | s_goto (OP_P0LIST); |
|
|
5469 | } |
|
|
5470 | else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTESP && ok_abbrev (cdr (SCHEME_V->args))) |
|
|
5471 | { |
|
|
5472 | putstr (SCHEME_A_ ",@"); |
|
|
5473 | SCHEME_V->args = cadr (SCHEME_V->args); |
|
|
5474 | s_goto (OP_P0LIST); |
|
|
5475 | } |
|
|
5476 | else |
|
|
5477 | { |
|
|
5478 | putstr (SCHEME_A_ "("); |
|
|
5479 | s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL); |
|
|
5480 | SCHEME_V->args = car (SCHEME_V->args); |
|
|
5481 | s_goto (OP_P0LIST); |
|
|
5482 | } |
|
|
5483 | |
|
|
5484 | case OP_P1LIST: |
|
|
5485 | if (is_pair (SCHEME_V->args)) |
|
|
5486 | { |
|
|
5487 | s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL); |
|
|
5488 | putstr (SCHEME_A_ " "); |
|
|
5489 | SCHEME_V->args = car (SCHEME_V->args); |
|
|
5490 | s_goto (OP_P0LIST); |
|
|
5491 | } |
|
|
5492 | else if (is_vector (SCHEME_V->args)) |
5192 | else if (is_vector (args)) |
5493 | { |
5193 | { |
5494 | s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); |
5194 | s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); |
5495 | putstr (SCHEME_A_ " . "); |
5195 | putstr (SCHEME_A_ " . "); |
5496 | s_goto (OP_P0LIST); |
5196 | s_goto (OP_P0LIST); |
5497 | } |
5197 | } |
5498 | else |
5198 | else |
5499 | { |
5199 | { |
5500 | if (SCHEME_V->args != NIL) |
5200 | if (args != NIL) |
5501 | { |
5201 | { |
5502 | putstr (SCHEME_A_ " . "); |
5202 | putstr (SCHEME_A_ " . "); |
5503 | printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); |
5203 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5504 | } |
5204 | } |
5505 | |
5205 | |
5506 | putstr (SCHEME_A_ ")"); |
5206 | putstr (SCHEME_A_ ")"); |
5507 | s_return (S_T); |
5207 | s_return (S_T); |
5508 | } |
5208 | } |
5509 | |
5209 | |
5510 | case OP_PVECFROM: |
5210 | case OP_PVECFROM: |
5511 | { |
5211 | { |
5512 | int i = ivalue_unchecked (cdr (SCHEME_V->args)); |
5212 | int i = ivalue_unchecked (cdr (args)); |
5513 | pointer vec = car (SCHEME_V->args); |
5213 | pointer vec = car (args); |
5514 | int len = ivalue_unchecked (vec); |
5214 | int len = veclength (vec); |
5515 | |
5215 | |
5516 | if (i == len) |
5216 | if (i == len) |
5517 | { |
5217 | { |
5518 | putstr (SCHEME_A_ ")"); |
5218 | putstr (SCHEME_A_ ")"); |
5519 | s_return (S_T); |
5219 | s_return (S_T); |
5520 | } |
5220 | } |
5521 | else |
5221 | else |
5522 | { |
5222 | { |
5523 | pointer elem = vector_elem (vec, i); |
5223 | pointer elem = vector_get (vec, i); |
5524 | |
5224 | |
5525 | ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; |
5225 | ivalue_unchecked (cdr (args)) = i + 1; |
5526 | s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); |
5226 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5527 | SCHEME_V->args = elem; |
5227 | SCHEME_V->args = elem; |
5528 | |
5228 | |
5529 | if (i > 0) |
5229 | if (i > 0) |
5530 | putstr (SCHEME_A_ " "); |
5230 | putstr (SCHEME_A_ " "); |
5531 | |
5231 | |
5532 | s_goto (OP_P0LIST); |
5232 | s_goto (OP_P0LIST); |
5533 | } |
5233 | } |
5534 | } |
5234 | } |
5535 | } |
5235 | } |
5536 | |
5236 | |
5537 | return S_T; |
5237 | if (USE_ERROR_CHECKING) abort (); |
5538 | } |
5238 | } |
5539 | |
5239 | |
5540 | static pointer |
5240 | static int |
5541 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5241 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5542 | { |
5242 | { |
|
|
5243 | pointer args = SCHEME_V->args; |
|
|
5244 | pointer a = car (args); |
5543 | pointer x, y; |
5245 | pointer x, y; |
5544 | |
5246 | |
5545 | switch (op) |
5247 | switch (op) |
5546 | { |
5248 | { |
5547 | case OP_LIST_LENGTH: /* length *//* a.k */ |
5249 | case OP_LIST_LENGTH: /* length *//* a.k */ |
5548 | { |
5250 | { |
5549 | long v = list_length (SCHEME_A_ car (SCHEME_V->args)); |
5251 | long v = list_length (SCHEME_A_ a); |
5550 | |
5252 | |
5551 | if (v < 0) |
5253 | if (v < 0) |
5552 | Error_1 ("length: not a list:", car (SCHEME_V->args)); |
5254 | Error_1 ("length: not a list:", a); |
5553 | |
5255 | |
5554 | s_return (mk_integer (SCHEME_A_ v)); |
5256 | s_return (mk_integer (SCHEME_A_ v)); |
5555 | } |
5257 | } |
5556 | |
5258 | |
5557 | case OP_ASSQ: /* assq *//* a.k */ |
5259 | case OP_ASSQ: /* assq *//* a.k */ |
5558 | x = car (SCHEME_V->args); |
5260 | x = a; |
5559 | |
5261 | |
5560 | for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) |
5262 | for (y = cadr (args); is_pair (y); y = cdr (y)) |
5561 | { |
5263 | { |
5562 | if (!is_pair (car (y))) |
5264 | if (!is_pair (car (y))) |
5563 | Error_0 ("unable to handle non pair element"); |
5265 | Error_0 ("unable to handle non pair element"); |
5564 | |
5266 | |
5565 | if (x == caar (y)) |
5267 | if (x == caar (y)) |
… | |
… | |
5571 | else |
5273 | else |
5572 | s_return (S_F); |
5274 | s_return (S_F); |
5573 | |
5275 | |
5574 | |
5276 | |
5575 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5277 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5576 | SCHEME_V->args = car (SCHEME_V->args); |
5278 | SCHEME_V->args = a; |
5577 | |
5279 | |
5578 | if (SCHEME_V->args == NIL) |
5280 | if (SCHEME_V->args == NIL) |
5579 | s_return (S_F); |
5281 | s_return (S_F); |
5580 | else if (is_closure (SCHEME_V->args)) |
5282 | else if (is_closure (SCHEME_V->args)) |
5581 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5283 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
… | |
… | |
5586 | |
5288 | |
5587 | case OP_CLOSUREP: /* closure? */ |
5289 | case OP_CLOSUREP: /* closure? */ |
5588 | /* |
5290 | /* |
5589 | * Note, macro object is also a closure. |
5291 | * Note, macro object is also a closure. |
5590 | * Therefore, (closure? <#MACRO>) ==> #t |
5292 | * Therefore, (closure? <#MACRO>) ==> #t |
|
|
5293 | * (schmorp) well, obviously not, fix? TODO |
5591 | */ |
5294 | */ |
5592 | s_retbool (is_closure (car (SCHEME_V->args))); |
5295 | s_retbool (is_closure (a)); |
5593 | |
5296 | |
5594 | case OP_MACROP: /* macro? */ |
5297 | case OP_MACROP: /* macro? */ |
5595 | s_retbool (is_macro (car (SCHEME_V->args))); |
5298 | s_retbool (is_macro (a)); |
5596 | } |
5299 | } |
5597 | |
5300 | |
5598 | return S_T; /* NOTREACHED */ |
5301 | if (USE_ERROR_CHECKING) abort (); |
5599 | } |
5302 | } |
5600 | |
5303 | |
|
|
5304 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5601 | typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); |
5305 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5602 | |
5306 | |
5603 | typedef int (*test_predicate) (pointer); |
5307 | typedef int (*test_predicate)(pointer); |
5604 | static int |
5308 | static int |
5605 | is_any (pointer p) |
5309 | tst_any (pointer p) |
5606 | { |
5310 | { |
5607 | return 1; |
5311 | return 1; |
5608 | } |
5312 | } |
5609 | |
5313 | |
5610 | static int |
5314 | static int |
5611 | is_nonneg (pointer p) |
5315 | tst_inonneg (pointer p) |
5612 | { |
5316 | { |
5613 | return ivalue (p) >= 0 && is_integer (p); |
5317 | return is_integer (p) && ivalue_unchecked (p) >= 0; |
|
|
5318 | } |
|
|
5319 | |
|
|
5320 | static int |
|
|
5321 | tst_is_list (SCHEME_P_ pointer p) |
|
|
5322 | { |
|
|
5323 | return p == NIL || is_pair (p); |
5614 | } |
5324 | } |
5615 | |
5325 | |
5616 | /* Correspond carefully with following defines! */ |
5326 | /* Correspond carefully with following defines! */ |
5617 | static struct |
5327 | static struct |
5618 | { |
5328 | { |
5619 | test_predicate fct; |
5329 | test_predicate fct; |
5620 | const char *kind; |
5330 | const char *kind; |
5621 | } tests[] = |
5331 | } tests[] = { |
5622 | { |
5332 | { tst_any , 0 }, |
5623 | { 0, 0}, /* unused */ |
5333 | { is_string , "string" }, |
5624 | { is_any, 0}, |
5334 | { is_symbol , "symbol" }, |
5625 | { is_string, "string" }, |
5335 | { is_port , "port" }, |
5626 | { is_symbol, "symbol" }, |
|
|
5627 | { is_port, "port" }, |
|
|
5628 | { is_inport, "input port" }, |
5336 | { is_inport , "input port" }, |
5629 | { is_outport, "output port" }, |
5337 | { is_outport , "output port" }, |
5630 | { is_environment, "environment" }, |
5338 | { is_environment, "environment" }, |
5631 | { is_pair, "pair" }, |
5339 | { is_pair , "pair" }, |
5632 | { 0, "pair or '()" }, |
5340 | { 0 , "pair or '()" }, |
5633 | { is_character, "character" }, |
5341 | { is_character , "character" }, |
5634 | { is_vector, "vector" }, |
5342 | { is_vector , "vector" }, |
5635 | { is_number, "number" }, |
5343 | { is_number , "number" }, |
5636 | { is_integer, "integer" }, |
5344 | { is_integer , "integer" }, |
5637 | { is_nonneg, "non-negative integer" } |
5345 | { tst_inonneg , "non-negative integer" } |
5638 | }; |
5346 | }; |
5639 | |
5347 | |
5640 | #define TST_NONE 0 |
5348 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5641 | #define TST_ANY "\001" |
5349 | #define TST_ANY "\001" |
5642 | #define TST_STRING "\002" |
5350 | #define TST_STRING "\002" |
5643 | #define TST_SYMBOL "\003" |
5351 | #define TST_SYMBOL "\003" |
5644 | #define TST_PORT "\004" |
5352 | #define TST_PORT "\004" |
5645 | #define TST_INPORT "\005" |
5353 | #define TST_INPORT "\005" |
5646 | #define TST_OUTPORT "\006" |
5354 | #define TST_OUTPORT "\006" |
5647 | #define TST_ENVIRONMENT "\007" |
5355 | #define TST_ENVIRONMENT "\007" |
5648 | #define TST_PAIR "\010" |
5356 | #define TST_PAIR "\010" |
5649 | #define TST_LIST "\011" |
5357 | #define TST_LIST "\011" |
5650 | #define TST_CHAR "\012" |
5358 | #define TST_CHAR "\012" |
5651 | #define TST_VECTOR "\013" |
5359 | #define TST_VECTOR "\013" |
5652 | #define TST_NUMBER "\014" |
5360 | #define TST_NUMBER "\014" |
5653 | #define TST_INTEGER "\015" |
5361 | #define TST_INTEGER "\015" |
5654 | #define TST_NATURAL "\016" |
5362 | #define TST_NATURAL "\016" |
|
|
5363 | |
|
|
5364 | #define INF_ARG 0xff |
|
|
5365 | #define UNNAMED_OP "" |
|
|
5366 | |
|
|
5367 | static const char opnames[] = |
|
|
5368 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
|
|
5369 | #include "opdefines.h" |
|
|
5370 | #undef OP_DEF |
|
|
5371 | ; |
|
|
5372 | |
|
|
5373 | static const char * |
|
|
5374 | opname (int idx) |
|
|
5375 | { |
|
|
5376 | const char *name = opnames; |
|
|
5377 | |
|
|
5378 | /* should do this at compile time, but would require external program, right? */ |
|
|
5379 | while (idx--) |
|
|
5380 | name += strlen (name) + 1; |
|
|
5381 | |
|
|
5382 | return *name ? name : "ILLEGAL"; |
|
|
5383 | } |
|
|
5384 | |
|
|
5385 | static const char * |
|
|
5386 | procname (pointer x) |
|
|
5387 | { |
|
|
5388 | return opname (procnum (x)); |
|
|
5389 | } |
5655 | |
5390 | |
5656 | typedef struct |
5391 | typedef struct |
5657 | { |
5392 | { |
5658 | dispatch_func func; |
5393 | uint8_t func; |
5659 | char *name; |
5394 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
|
|
5395 | uint8_t builtin; |
|
|
5396 | #if USE_ERROR_CHECKING |
5660 | int min_arity; |
5397 | uint8_t min_arity; |
5661 | int max_arity; |
5398 | uint8_t max_arity; |
5662 | char *arg_tests_encoding; |
5399 | char arg_tests_encoding[3]; |
|
|
5400 | #endif |
5663 | } op_code_info; |
5401 | } op_code_info; |
5664 | |
5402 | |
5665 | #define INF_ARG 0xffff |
|
|
5666 | |
|
|
5667 | static op_code_info dispatch_table[] = { |
5403 | static const op_code_info dispatch_table[] = { |
5668 | #define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, |
5404 | #if USE_ERROR_CHECKING |
|
|
5405 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
|
|
5406 | #else |
|
|
5407 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1 }, |
|
|
5408 | #endif |
5669 | #include "opdefines.h" |
5409 | #include "opdefines.h" |
|
|
5410 | #undef OP_DEF |
5670 | {0} |
5411 | {0} |
5671 | }; |
5412 | }; |
5672 | |
5413 | |
5673 | static const char * |
|
|
5674 | procname (pointer x) |
|
|
5675 | { |
|
|
5676 | int n = procnum (x); |
|
|
5677 | const char *name = dispatch_table[n].name; |
|
|
5678 | |
|
|
5679 | if (name == 0) |
|
|
5680 | name = "ILLEGAL!"; |
|
|
5681 | |
|
|
5682 | return name; |
|
|
5683 | } |
|
|
5684 | |
|
|
5685 | /* kernel of this interpreter */ |
5414 | /* kernel of this interpreter */ |
5686 | static void |
5415 | static void ecb_hot |
5687 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5416 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5688 | { |
5417 | { |
5689 | SCHEME_V->op = op; |
5418 | SCHEME_V->op = op; |
5690 | |
5419 | |
5691 | for (;;) |
5420 | for (;;) |
5692 | { |
5421 | { |
5693 | op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5422 | const op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5694 | |
5423 | |
|
|
5424 | #if USE_ERROR_CHECKING |
5695 | if (pcd->name) /* if built-in function, check arguments */ |
5425 | if (pcd->builtin) /* if built-in function, check arguments */ |
5696 | { |
5426 | { |
5697 | #if USE_ERROR_CHECKING |
|
|
5698 | char msg[STRBUFFSIZE]; |
5427 | char msg[STRBUFFSIZE]; |
5699 | int ok = 1; |
|
|
5700 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5428 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5701 | |
5429 | |
5702 | /* Check number of arguments */ |
5430 | /* Check number of arguments */ |
5703 | if (n < pcd->min_arity) |
5431 | if (ecb_expect_false (n < pcd->min_arity)) |
5704 | { |
5432 | { |
5705 | ok = 0; |
|
|
5706 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5433 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5707 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
5434 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
|
|
5435 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5436 | continue; |
5708 | } |
5437 | } |
5709 | |
5438 | else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG)) |
5710 | if (ok && n > pcd->max_arity) |
|
|
5711 | { |
5439 | { |
5712 | ok = 0; |
|
|
5713 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5440 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5714 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
5441 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
|
|
5442 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5443 | continue; |
5715 | } |
5444 | } |
5716 | #endif |
5445 | else |
5717 | |
|
|
5718 | if (ok) |
|
|
5719 | { |
5446 | { |
5720 | if (pcd->arg_tests_encoding && USE_ERROR_CHECKING) |
5447 | if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */ |
5721 | { |
5448 | { |
5722 | int i = 0; |
5449 | int i = 0; |
5723 | int j; |
5450 | int j; |
5724 | const char *t = pcd->arg_tests_encoding; |
5451 | const char *t = pcd->arg_tests_encoding; |
5725 | pointer arglist = SCHEME_V->args; |
5452 | pointer arglist = SCHEME_V->args; |
5726 | |
5453 | |
5727 | do |
5454 | do |
5728 | { |
5455 | { |
5729 | pointer arg = car (arglist); |
5456 | pointer arg = car (arglist); |
5730 | |
5457 | |
5731 | j = (int) t[0]; |
5458 | j = t[0]; |
5732 | |
5459 | |
|
|
5460 | /*TODO: tst_is_list has different prototype - fix if other tests acquire same prototype */ |
5733 | if (j == TST_LIST[0]) |
5461 | if (j == TST_LIST[0]) |
5734 | { |
5462 | { |
5735 | if (arg != NIL && !is_pair (arg)) |
5463 | if (!tst_is_list (SCHEME_A_ arg)) |
5736 | break; |
5464 | break; |
5737 | } |
5465 | } |
5738 | else |
5466 | else |
5739 | { |
5467 | { |
5740 | if (!tests[j].fct (arg)) |
5468 | if (!tests[j - 1].fct (arg)) |
5741 | break; |
5469 | break; |
5742 | } |
5470 | } |
5743 | |
5471 | |
5744 | if (t[1] != 0) /* last test is replicated as necessary */ |
5472 | if (t < pcd->arg_tests_encoding + sizeof (pcd->arg_tests_encoding) - 1 && t[1]) /* last test is replicated as necessary */ |
5745 | t++; |
5473 | t++; |
5746 | |
5474 | |
5747 | arglist = cdr (arglist); |
5475 | arglist = cdr (arglist); |
5748 | i++; |
5476 | i++; |
5749 | } |
5477 | } |
5750 | while (i < n); |
5478 | while (i < n); |
5751 | |
5479 | |
5752 | if (i < n) |
5480 | if (i < n) |
5753 | { |
5481 | { |
5754 | ok = 0; |
|
|
5755 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); |
5482 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind); |
|
|
5483 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5484 | continue; |
5756 | } |
5485 | } |
5757 | } |
5486 | } |
5758 | } |
5487 | } |
5759 | |
|
|
5760 | if (!ok) |
|
|
5761 | { |
|
|
5762 | if (xError_1 (SCHEME_A_ msg, 0) == NIL) |
|
|
5763 | return; |
|
|
5764 | |
|
|
5765 | pcd = dispatch_table + SCHEME_V->op; |
|
|
5766 | } |
|
|
5767 | } |
5488 | } |
|
|
5489 | #endif |
5768 | |
5490 | |
5769 | ok_to_freely_gc (SCHEME_A); |
5491 | ok_to_freely_gc (SCHEME_A); |
5770 | |
5492 | |
5771 | if (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL) |
5493 | static const dispatch_func dispatch_funcs[] = { |
|
|
5494 | opexe_0, |
|
|
5495 | opexe_1, |
|
|
5496 | opexe_2, |
|
|
5497 | opexe_3, |
|
|
5498 | opexe_4, |
|
|
5499 | opexe_5, |
|
|
5500 | opexe_6, |
|
|
5501 | }; |
|
|
5502 | |
|
|
5503 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5772 | return; |
5504 | return; |
5773 | |
5505 | |
5774 | #if USE_ERROR_CHECKING |
5506 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5775 | if (SCHEME_V->no_memory) |
|
|
5776 | { |
5507 | { |
5777 | xwrstr ("No memory!\n"); |
5508 | putstr (SCHEME_A_ "No memory!\n"); |
5778 | return; |
5509 | return; |
5779 | } |
5510 | } |
5780 | #endif |
|
|
5781 | } |
5511 | } |
5782 | } |
5512 | } |
5783 | |
5513 | |
5784 | /* ========== Initialization of internal keywords ========== */ |
5514 | /* ========== Initialization of internal keywords ========== */ |
5785 | |
5515 | |
… | |
… | |
5802 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5532 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5803 | { |
5533 | { |
5804 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5534 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5805 | set_typeflag (y, (T_PROC | T_ATOM)); |
5535 | set_typeflag (y, (T_PROC | T_ATOM)); |
5806 | ivalue_unchecked (y) = op; |
5536 | ivalue_unchecked (y) = op; |
5807 | set_num_integer (y); |
|
|
5808 | return y; |
5537 | return y; |
5809 | } |
5538 | } |
5810 | |
5539 | |
5811 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5540 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5812 | static int |
5541 | static int |
5813 | syntaxnum (pointer p) |
5542 | syntaxnum (pointer p) |
5814 | { |
5543 | { |
5815 | const char *s = strvalue (car (p)); |
5544 | const char *s = strvalue (p); |
5816 | |
5545 | |
5817 | switch (strlength (car (p))) |
5546 | switch (strlength (p)) |
5818 | { |
5547 | { |
5819 | case 2: |
5548 | case 2: |
5820 | if (s[0] == 'i') |
5549 | if (s[0] == 'i') |
5821 | return OP_IF0; /* if */ |
5550 | return OP_IF0; /* if */ |
5822 | else |
5551 | else |
… | |
… | |
5836 | |
5565 | |
5837 | case 'd': |
5566 | case 'd': |
5838 | return OP_COND0; /* cond */ |
5567 | return OP_COND0; /* cond */ |
5839 | |
5568 | |
5840 | case '*': |
5569 | case '*': |
5841 | return OP_LET0AST; /* let* */ |
5570 | return OP_LET0AST;/* let* */ |
5842 | |
5571 | |
5843 | default: |
5572 | default: |
5844 | return OP_SET0; /* set! */ |
5573 | return OP_SET0; /* set! */ |
5845 | } |
5574 | } |
5846 | |
5575 | |
… | |
… | |
5868 | |
5597 | |
5869 | case 'f': |
5598 | case 'f': |
5870 | return OP_DEF0; /* define */ |
5599 | return OP_DEF0; /* define */ |
5871 | |
5600 | |
5872 | default: |
5601 | default: |
5873 | return OP_LET0REC; /* letrec */ |
5602 | return OP_LET0REC;/* letrec */ |
5874 | } |
5603 | } |
5875 | |
5604 | |
5876 | default: |
5605 | default: |
5877 | return OP_C0STREAM; /* cons-stream */ |
5606 | return OP_C0STREAM; /* cons-stream */ |
5878 | } |
5607 | } |
5879 | } |
5608 | } |
5880 | |
5609 | |
5881 | #if USE_MULTIPLICITY |
5610 | #if USE_MULTIPLICITY |
5882 | scheme * |
5611 | ecb_cold scheme * |
5883 | scheme_init_new () |
5612 | scheme_init_new () |
5884 | { |
5613 | { |
5885 | scheme *sc = malloc (sizeof (scheme)); |
5614 | scheme *sc = malloc (sizeof (scheme)); |
5886 | |
5615 | |
5887 | if (!scheme_init (SCHEME_A)) |
5616 | if (!scheme_init (SCHEME_A)) |
… | |
… | |
5892 | else |
5621 | else |
5893 | return sc; |
5622 | return sc; |
5894 | } |
5623 | } |
5895 | #endif |
5624 | #endif |
5896 | |
5625 | |
5897 | int |
5626 | ecb_cold int |
5898 | scheme_init (SCHEME_P) |
5627 | scheme_init (SCHEME_P) |
5899 | { |
5628 | { |
5900 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5629 | int i, n = sizeof (dispatch_table) / sizeof (dispatch_table[0]); |
5901 | pointer x; |
5630 | pointer x; |
|
|
5631 | |
|
|
5632 | /* this memset is not strictly correct, as we assume (intcache) |
|
|
5633 | * that memset 0 will also set pointers to 0, but memset does |
|
|
5634 | * of course not guarantee that. screw such systems. |
|
|
5635 | */ |
|
|
5636 | memset (SCHEME_V, 0, sizeof (*SCHEME_V)); |
5902 | |
5637 | |
5903 | num_set_fixnum (num_zero, 1); |
5638 | num_set_fixnum (num_zero, 1); |
5904 | num_set_ivalue (num_zero, 0); |
5639 | num_set_ivalue (num_zero, 0); |
5905 | num_set_fixnum (num_one, 1); |
5640 | num_set_fixnum (num_one, 1); |
5906 | num_set_ivalue (num_one, 1); |
5641 | num_set_ivalue (num_one, 1); |
… | |
… | |
5918 | SCHEME_V->save_inport = NIL; |
5653 | SCHEME_V->save_inport = NIL; |
5919 | SCHEME_V->loadport = NIL; |
5654 | SCHEME_V->loadport = NIL; |
5920 | SCHEME_V->nesting = 0; |
5655 | SCHEME_V->nesting = 0; |
5921 | SCHEME_V->interactive_repl = 0; |
5656 | SCHEME_V->interactive_repl = 0; |
5922 | |
5657 | |
5923 | if (alloc_cellseg (SCHEME_A_ FIRST_CELLSEGS) != FIRST_CELLSEGS) |
5658 | if (!alloc_cellseg (SCHEME_A)) |
5924 | { |
5659 | { |
5925 | #if USE_ERROR_CHECKING |
5660 | #if USE_ERROR_CHECKING |
5926 | SCHEME_V->no_memory = 1; |
5661 | SCHEME_V->no_memory = 1; |
5927 | return 0; |
5662 | return 0; |
5928 | #endif |
5663 | #endif |
… | |
… | |
5945 | set_cdr (S_T, S_T); |
5680 | set_cdr (S_T, S_T); |
5946 | /* init F */ |
5681 | /* init F */ |
5947 | set_typeflag (S_F, T_ATOM | T_MARK); |
5682 | set_typeflag (S_F, T_ATOM | T_MARK); |
5948 | set_car (S_F, S_F); |
5683 | set_car (S_F, S_F); |
5949 | set_cdr (S_F, S_F); |
5684 | set_cdr (S_F, S_F); |
|
|
5685 | /* init EOF_OBJ */ |
|
|
5686 | set_typeflag (S_EOF, T_ATOM | T_MARK); |
|
|
5687 | set_car (S_EOF, S_EOF); |
|
|
5688 | set_cdr (S_EOF, S_EOF); |
5950 | /* init sink */ |
5689 | /* init sink */ |
5951 | set_typeflag (S_SINK, T_PAIR | T_MARK); |
5690 | set_typeflag (S_SINK, T_PAIR | T_MARK); |
5952 | set_car (S_SINK, NIL); |
5691 | set_car (S_SINK, NIL); |
|
|
5692 | |
5953 | /* init c_nest */ |
5693 | /* init c_nest */ |
5954 | SCHEME_V->c_nest = NIL; |
5694 | SCHEME_V->c_nest = NIL; |
5955 | |
5695 | |
5956 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
5696 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
5957 | /* init global_env */ |
5697 | /* init global_env */ |
… | |
… | |
5970 | |
5710 | |
5971 | for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) |
5711 | for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) |
5972 | assign_syntax (SCHEME_A_ syntax_names[i]); |
5712 | assign_syntax (SCHEME_A_ syntax_names[i]); |
5973 | } |
5713 | } |
5974 | |
5714 | |
|
|
5715 | // TODO: should iterate via strlen, to avoid n² complexity |
5975 | for (i = 0; i < n; i++) |
5716 | for (i = 0; i < n; i++) |
5976 | if (dispatch_table[i].name != 0) |
5717 | if (dispatch_table[i].builtin) |
5977 | assign_proc (SCHEME_A_ i, dispatch_table[i].name); |
5718 | assign_proc (SCHEME_A_ i, opname (i)); |
5978 | |
5719 | |
5979 | /* initialization of global pointers to special symbols */ |
5720 | /* initialization of global pointers to special symbols */ |
5980 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
5721 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
5981 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
5722 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
5982 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
5723 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
5983 | SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); |
5724 | SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); |
5984 | SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); |
5725 | SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); |
5985 | SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); |
5726 | SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); |
5986 | SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); |
5727 | SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); |
5987 | SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); |
5728 | SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); |
5988 | SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); |
5729 | SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); |
5989 | SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); |
5730 | SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); |
5990 | |
5731 | |
5991 | return !SCHEME_V->no_memory; |
5732 | return !SCHEME_V->no_memory; |
5992 | } |
5733 | } |
5993 | |
5734 | |
… | |
… | |
6021 | scheme_set_external_data (SCHEME_P_ void *p) |
5762 | scheme_set_external_data (SCHEME_P_ void *p) |
6022 | { |
5763 | { |
6023 | SCHEME_V->ext_data = p; |
5764 | SCHEME_V->ext_data = p; |
6024 | } |
5765 | } |
6025 | |
5766 | |
6026 | void |
5767 | ecb_cold void |
6027 | scheme_deinit (SCHEME_P) |
5768 | scheme_deinit (SCHEME_P) |
6028 | { |
5769 | { |
6029 | int i; |
5770 | int i; |
6030 | |
5771 | |
6031 | #if SHOW_ERROR_LINE |
5772 | #if SHOW_ERROR_LINE |
… | |
… | |
6123 | { |
5864 | { |
6124 | dump_stack_reset (SCHEME_A); |
5865 | dump_stack_reset (SCHEME_A); |
6125 | SCHEME_V->envir = SCHEME_V->global_env; |
5866 | SCHEME_V->envir = SCHEME_V->global_env; |
6126 | SCHEME_V->file_i = 0; |
5867 | SCHEME_V->file_i = 0; |
6127 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5868 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
6128 | SCHEME_V->load_stack[0].rep.string.start = (char *) cmd; /* This func respects const */ |
5869 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
6129 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *) cmd + strlen (cmd); |
5870 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
6130 | SCHEME_V->load_stack[0].rep.string.curr = (char *) cmd; |
5871 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
6131 | #if USE_PORTS |
5872 | #if USE_PORTS |
6132 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5873 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
6133 | #endif |
5874 | #endif |
6134 | SCHEME_V->retcode = 0; |
5875 | SCHEME_V->retcode = 0; |
6135 | SCHEME_V->interactive_repl = 0; |
5876 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
6240 | |
5981 | |
6241 | /* ========== Main ========== */ |
5982 | /* ========== Main ========== */ |
6242 | |
5983 | |
6243 | #if STANDALONE |
5984 | #if STANDALONE |
6244 | |
5985 | |
6245 | # if defined(__APPLE__) && !defined (OSX) |
|
|
6246 | int |
|
|
6247 | main () |
|
|
6248 | { |
|
|
6249 | extern MacTS_main (int argc, char **argv); |
|
|
6250 | char **argv; |
|
|
6251 | int argc = ccommand (&argv); |
|
|
6252 | |
|
|
6253 | MacTS_main (argc, argv); |
|
|
6254 | return 0; |
|
|
6255 | } |
|
|
6256 | |
|
|
6257 | int |
|
|
6258 | MacTS_main (int argc, char **argv) |
|
|
6259 | { |
|
|
6260 | # else |
|
|
6261 | int |
5986 | int |
6262 | main (int argc, char **argv) |
5987 | main (int argc, char **argv) |
6263 | { |
5988 | { |
6264 | # endif |
|
|
6265 | # if USE_MULTIPLICITY |
5989 | # if USE_MULTIPLICITY |
6266 | scheme ssc; |
5990 | scheme ssc; |
6267 | scheme *const SCHEME_V = &ssc; |
5991 | scheme *const SCHEME_V = &ssc; |
6268 | # else |
5992 | # else |
6269 | # endif |
5993 | # endif |
6270 | int fin; |
5994 | int fin; |
6271 | char *file_name = InitFile; |
5995 | char *file_name = InitFile; |
6272 | int retcode; |
5996 | int retcode; |
6273 | int isfile = 1; |
5997 | int isfile = 1; |
|
|
5998 | system ("ps v $PPID");//D |
6274 | |
5999 | |
6275 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6000 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6276 | { |
6001 | { |
6277 | xwrstr ("Usage: tinyscheme -?\n"); |
6002 | putstr (SCHEME_A_ "Usage: tinyscheme -?\n"); |
6278 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6003 | putstr (SCHEME_A_ "or: tinyscheme [<file1> <file2> ...]\n"); |
6279 | xwrstr ("followed by\n"); |
6004 | putstr (SCHEME_A_ "followed by\n"); |
6280 | xwrstr (" -1 <file> [<arg1> <arg2> ...]\n"); |
6005 | putstr (SCHEME_A_ " -1 <file> [<arg1> <arg2> ...]\n"); |
6281 | xwrstr (" -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6006 | putstr (SCHEME_A_ " -c <Scheme commands> [<arg1> <arg2> ...]\n"); |
6282 | xwrstr ("assuming that the executable is named tinyscheme.\n"); |
6007 | putstr (SCHEME_A_ "assuming that the executable is named tinyscheme.\n"); |
6283 | xwrstr ("Use - as filename for stdin.\n"); |
6008 | putstr (SCHEME_A_ "Use - as filename for stdin.\n"); |
6284 | return 1; |
6009 | return 1; |
6285 | } |
6010 | } |
6286 | |
6011 | |
6287 | if (!scheme_init (SCHEME_A)) |
6012 | if (!scheme_init (SCHEME_A)) |
6288 | { |
6013 | { |
6289 | xwrstr ("Could not initialize!\n"); |
6014 | putstr (SCHEME_A_ "Could not initialize!\n"); |
6290 | return 2; |
6015 | return 2; |
6291 | } |
6016 | } |
6292 | |
6017 | |
6293 | # if USE_PORTS |
6018 | # if USE_PORTS |
6294 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
6019 | scheme_set_input_port_file (SCHEME_A_ STDIN_FILENO); |
… | |
… | |
6339 | fin = open (file_name, O_RDONLY); |
6064 | fin = open (file_name, O_RDONLY); |
6340 | #endif |
6065 | #endif |
6341 | |
6066 | |
6342 | if (isfile && fin < 0) |
6067 | if (isfile && fin < 0) |
6343 | { |
6068 | { |
6344 | xwrstr ("Could not open file "); xwrstr (file_name); xwrstr ("\n"); |
6069 | putstr (SCHEME_A_ "Could not open file "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6345 | } |
6070 | } |
6346 | else |
6071 | else |
6347 | { |
6072 | { |
6348 | if (isfile) |
6073 | if (isfile) |
6349 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
6074 | scheme_load_named_file (SCHEME_A_ fin, file_name); |
… | |
… | |
6353 | #if USE_PORTS |
6078 | #if USE_PORTS |
6354 | if (!isfile || fin != STDIN_FILENO) |
6079 | if (!isfile || fin != STDIN_FILENO) |
6355 | { |
6080 | { |
6356 | if (SCHEME_V->retcode != 0) |
6081 | if (SCHEME_V->retcode != 0) |
6357 | { |
6082 | { |
6358 | xwrstr ("Errors encountered reading "); xwrstr (file_name); xwrstr ("\n"); |
6083 | putstr (SCHEME_A_ "Errors encountered reading "); putstr (SCHEME_A_ file_name); putstr (SCHEME_A_ "\n"); |
6359 | } |
6084 | } |
6360 | |
6085 | |
6361 | if (isfile) |
6086 | if (isfile) |
6362 | close (fin); |
6087 | close (fin); |
6363 | } |
6088 | } |