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