1 | |
1 | /* |
2 | /* T I N Y S C H E M E 1 . 4 1 |
2 | * µscheme |
|
|
3 | * |
|
|
4 | * Copyright (C) 2015 Marc Alexander Lehmann <uscheme@schmorp.de> |
|
|
5 | * do as you want with this, attribution appreciated. |
|
|
6 | * |
|
|
7 | * Based opn tinyscheme-1.41 (original credits follow) |
3 | * Dimitrios Souflis (dsouflis@acm.org) |
8 | * Dimitrios Souflis (dsouflis@acm.org) |
4 | * Based on MiniScheme (original credits follow) |
9 | * Based on MiniScheme (original credits follow) |
5 | * (MINISCM) coded by Atsushi Moriwaki (11/5/1989) |
10 | * (MINISCM) coded by Atsushi Moriwaki (11/5/1989) |
6 | * (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp |
11 | * (MINISCM) E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp |
7 | * (MINISCM) This version has been modified by R.C. Secrist. |
12 | * (MINISCM) This version has been modified by R.C. Secrist. |
… | |
… | |
26 | #endif |
31 | #endif |
27 | |
32 | |
28 | #include <sys/types.h> |
33 | #include <sys/types.h> |
29 | #include <sys/stat.h> |
34 | #include <sys/stat.h> |
30 | #include <fcntl.h> |
35 | #include <fcntl.h> |
|
|
36 | |
|
|
37 | #include <string.h> |
|
|
38 | #include <stdlib.h> |
31 | |
39 | |
32 | #include <limits.h> |
40 | #include <limits.h> |
33 | #include <inttypes.h> |
41 | #include <inttypes.h> |
34 | #include <float.h> |
42 | #include <float.h> |
35 | //#include <ctype.h> |
43 | //#include <ctype.h> |
… | |
… | |
52 | |
60 | |
53 | #define BACKQUOTE '`' |
61 | #define BACKQUOTE '`' |
54 | #define DELIMITERS "()\";\f\t\v\n\r " |
62 | #define DELIMITERS "()\";\f\t\v\n\r " |
55 | |
63 | |
56 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
64 | #define NIL (&SCHEME_V->xNIL) //TODO: make this 0? |
57 | #define S_T (&SCHEME_V->xT) |
65 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
58 | #define S_F (&SCHEME_V->xF) |
66 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
59 | #define S_SINK (&SCHEME_V->xsink) |
67 | #define S_SINK (&SCHEME_V->xsink) |
60 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
68 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
61 | |
69 | |
62 | /* |
70 | /* should use libecb */ |
63 | * Basic memory allocation units |
71 | #if __GNUC__ >= 4 |
64 | */ |
72 | # define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
65 | |
73 | # define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
66 | #define banner "TinyScheme 1.41-s" |
74 | # define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
67 | |
75 | #endif |
68 | #include <string.h> |
|
|
69 | #include <stdlib.h> |
|
|
70 | |
76 | |
71 | #if !USE_MULTIPLICITY |
77 | #if !USE_MULTIPLICITY |
72 | static scheme sc; |
78 | static scheme sc; |
73 | #endif |
79 | #endif |
74 | |
80 | |
… | |
… | |
134 | c += 'a' - 'A'; |
140 | c += 'a' - 'A'; |
135 | |
141 | |
136 | return c; |
142 | return c; |
137 | } |
143 | } |
138 | |
144 | |
|
|
145 | static int |
|
|
146 | xisdigit (char c) |
|
|
147 | { |
|
|
148 | return c >= '0' && c <= '9'; |
|
|
149 | } |
|
|
150 | |
|
|
151 | #define toupper(c) xtoupper (c) |
|
|
152 | #define tolower(c) xtolower (c) |
|
|
153 | #define isdigit(c) xisdigit (c) |
|
|
154 | |
139 | #if USE_STRLWR |
155 | #if USE_STRLWR |
140 | static const char * |
156 | static const char * |
141 | strlwr (char *s) |
157 | strlwr (char *s) |
142 | { |
158 | { |
143 | const char *p = s; |
159 | const char *p = s; |
… | |
… | |
152 | } |
168 | } |
153 | #endif |
169 | #endif |
154 | |
170 | |
155 | #define stricmp(a,b) strcmp (a, b) |
171 | #define stricmp(a,b) strcmp (a, b) |
156 | #define strlwr(s) (s) |
172 | #define strlwr(s) (s) |
157 | #define toupper(c) xtoupper(c) |
|
|
158 | #define tolower(c) xtolower(c) |
|
|
159 | |
173 | |
160 | #ifndef prompt |
174 | #ifndef prompt |
161 | # define prompt "ts> " |
175 | # define prompt "ts> " |
162 | #endif |
176 | #endif |
163 | |
177 | |
… | |
… | |
169 | # define FIRST_CELLSEGS 3 |
183 | # define FIRST_CELLSEGS 3 |
170 | #endif |
184 | #endif |
171 | |
185 | |
172 | enum scheme_types |
186 | enum scheme_types |
173 | { |
187 | { |
174 | T_NULL, |
188 | T_FREE, |
175 | T_STRING, |
189 | T_STRING, |
176 | T_NUMBER, |
190 | T_NUMBER, |
177 | T_SYMBOL, |
191 | T_SYMBOL, |
178 | T_PROC, |
192 | T_PROC, |
179 | T_PAIR, |
193 | T_PAIR, |
… | |
… | |
184 | T_PORT, |
198 | T_PORT, |
185 | T_VECTOR, |
199 | T_VECTOR, |
186 | T_MACRO, |
200 | T_MACRO, |
187 | T_PROMISE, |
201 | T_PROMISE, |
188 | T_ENVIRONMENT, |
202 | T_ENVIRONMENT, |
|
|
203 | /* one more... */ |
189 | T_NUM_SYSTEM_TYPES |
204 | T_NUM_SYSTEM_TYPES |
190 | }; |
205 | }; |
191 | |
206 | |
192 | #define T_MASKTYPE 31 /* 0000000000011111 */ |
207 | #define T_MASKTYPE 0x000f |
193 | #define T_SYNTAX 4096 /* 0001000000000000 */ |
208 | #define T_SYNTAX 0x0010 |
194 | #define T_IMMUTABLE 8192 /* 0010000000000000 */ |
209 | #define T_IMMUTABLE 0x0020 |
195 | #define T_ATOM 16384 /* 0100000000000000 */ /* only for gc */ |
210 | #define T_ATOM 0x0040 /* only for gc */ |
196 | #define CLRATOM 49151 /* 1011111111111111 */ /* only for gc */ |
211 | #define T_MARK 0x0080 /* only for gc */ |
197 | #define MARK 32768 /* 1000000000000000 */ |
|
|
198 | #define UNMARK 32767 /* 0111111111111111 */ |
|
|
199 | |
212 | |
|
|
213 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
200 | |
214 | |
201 | static num num_add (num a, num b); |
215 | static num num_op (enum num_op op, num a, num b); |
202 | static num num_mul (num a, num b); |
|
|
203 | static num num_div (num a, num b); |
|
|
204 | static num num_intdiv (num a, num b); |
216 | 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); |
217 | static num num_rem (num a, num b); |
207 | static num num_mod (num a, num b); |
218 | 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 | |
219 | |
214 | #if USE_MATH |
220 | #if USE_MATH |
215 | static double round_per_R5RS (double x); |
221 | static double round_per_R5RS (double x); |
216 | #endif |
222 | #endif |
217 | static int is_zero_rvalue (RVALUE x); |
223 | static int is_zero_rvalue (RVALUE x); |
… | |
… | |
244 | is_vector (pointer p) |
250 | is_vector (pointer p) |
245 | { |
251 | { |
246 | return type (p) == T_VECTOR; |
252 | return type (p) == T_VECTOR; |
247 | } |
253 | } |
248 | |
254 | |
|
|
255 | #define vecvalue(p) ((p)->object.vector.vvalue) |
|
|
256 | #define veclength(p) ((p)->object.vector.length) |
249 | INTERFACE void fill_vector (pointer vec, pointer obj); |
257 | INTERFACE void fill_vector (pointer vec, pointer obj); |
|
|
258 | INTERFACE uint32_t vector_length (pointer vec); |
250 | INTERFACE pointer vector_elem (pointer vec, int ielem); |
259 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
251 | INTERFACE void set_vector_elem (pointer vec, int ielem, pointer a); |
260 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
|
|
261 | |
|
|
262 | INTERFACE uint32_t |
|
|
263 | vector_length (pointer vec) |
|
|
264 | { |
|
|
265 | return vec->object.vector.length; |
|
|
266 | } |
252 | |
267 | |
253 | INTERFACE INLINE int |
268 | INTERFACE INLINE int |
254 | is_number (pointer p) |
269 | is_number (pointer p) |
255 | { |
270 | { |
256 | return type (p) == T_NUMBER; |
271 | return type (p) == T_NUMBER; |
… | |
… | |
290 | nvalue (pointer p) |
305 | nvalue (pointer p) |
291 | { |
306 | { |
292 | return (p)->object.number; |
307 | return (p)->object.number; |
293 | } |
308 | } |
294 | |
309 | |
295 | INTERFACE long |
310 | static IVALUE |
|
|
311 | num_get_ivalue (const num n) |
|
|
312 | { |
|
|
313 | return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n); |
|
|
314 | } |
|
|
315 | |
|
|
316 | static RVALUE |
|
|
317 | num_get_rvalue (const num n) |
|
|
318 | { |
|
|
319 | return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n); |
|
|
320 | } |
|
|
321 | |
|
|
322 | INTERFACE IVALUE |
296 | ivalue (pointer p) |
323 | ivalue (pointer p) |
297 | { |
324 | { |
298 | return num_is_integer (p) ? num_ivalue ((p)->object.number) : (long) num_rvalue ((p)->object.number); |
325 | return num_get_ivalue (p->object.number); |
299 | } |
326 | } |
300 | |
327 | |
301 | INTERFACE RVALUE |
328 | INTERFACE RVALUE |
302 | rvalue (pointer p) |
329 | rvalue (pointer p) |
303 | { |
330 | { |
304 | return num_is_integer (p) ? (RVALUE) num_ivalue ((p)->object.number) : num_rvalue ((p)->object.number); |
331 | return num_get_rvalue (p->object.number); |
305 | } |
332 | } |
306 | |
333 | |
307 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
334 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
308 | #if USE_FLOAT |
335 | #if USE_REAL |
309 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
336 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
310 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
337 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
311 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
338 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
312 | #else |
339 | #else |
313 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
340 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
… | |
… | |
343 | { |
370 | { |
344 | return type (p) == T_PAIR; |
371 | return type (p) == T_PAIR; |
345 | } |
372 | } |
346 | |
373 | |
347 | #define car(p) ((p)->object.cons.car + 0) |
374 | #define car(p) ((p)->object.cons.car + 0) |
348 | #define cdr(p) ((p)->object.cons.cdr) /* find_consecutive_cells uses &cdr */ |
375 | #define cdr(p) ((p)->object.cons.cdr + 0) |
349 | |
376 | |
350 | #define caar(p) car (car (p)) |
377 | static pointer caar (pointer p) { return car (car (p)); } |
351 | #define cadr(p) car (cdr (p)) |
378 | static pointer cadr (pointer p) { return car (cdr (p)); } |
352 | #define cdar(p) cdr (car (p)) |
379 | static pointer cdar (pointer p) { return cdr (car (p)); } |
353 | #define cddr(p) cdr (cdr (p)) |
380 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
354 | |
381 | |
355 | #define cadar(p) car (cdr (car (p))) |
382 | static pointer cadar (pointer p) { return car (cdr (car (p))); } |
356 | #define caddr(p) car (cdr (cdr (p))) |
383 | static pointer caddr (pointer p) { return car (cdr (cdr (p))); } |
357 | #define cdaar(p) cdr (car (car (p))) |
384 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
358 | |
385 | |
359 | INTERFACE void |
386 | INTERFACE void |
360 | set_car (pointer p, pointer q) |
387 | set_car (pointer p, pointer q) |
361 | { |
388 | { |
362 | p->object.cons.car = q; |
389 | p->object.cons.car = q; |
… | |
… | |
424 | syntaxname (pointer p) |
451 | syntaxname (pointer p) |
425 | { |
452 | { |
426 | return strvalue (car (p)); |
453 | return strvalue (car (p)); |
427 | } |
454 | } |
428 | |
455 | |
429 | #define procnum(p) ivalue(p) |
456 | #define procnum(p) ivalue (p) |
430 | static const char *procname (pointer x); |
457 | static const char *procname (pointer x); |
431 | |
458 | |
432 | INTERFACE INLINE int |
459 | INTERFACE INLINE int |
433 | is_closure (pointer p) |
460 | is_closure (pointer p) |
434 | { |
461 | { |
… | |
… | |
477 | |
504 | |
478 | #define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) |
505 | #define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) |
479 | |
506 | |
480 | #define is_atom(p) (typeflag (p) & T_ATOM) |
507 | #define is_atom(p) (typeflag (p) & T_ATOM) |
481 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
508 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
482 | #define clratom(p) set_typeflag ((p), typeflag (p) & CLRATOM) |
509 | #define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) |
483 | |
510 | |
484 | #define is_mark(p) (typeflag (p) & MARK) |
511 | #define is_mark(p) (typeflag (p) & T_MARK) |
485 | #define setmark(p) set_typeflag ((p), typeflag (p) | MARK) |
512 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
486 | #define clrmark(p) set_typeflag ((p), typeflag (p) & UNMARK) |
513 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
487 | |
514 | |
488 | INTERFACE INLINE int |
515 | INTERFACE INLINE int |
489 | is_immutable (pointer p) |
516 | is_immutable (pointer p) |
490 | { |
517 | { |
491 | return typeflag (p) & T_IMMUTABLE; |
518 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
492 | } |
519 | } |
493 | |
520 | |
494 | INTERFACE INLINE void |
521 | INTERFACE INLINE void |
495 | setimmutable (pointer p) |
522 | setimmutable (pointer p) |
496 | { |
523 | { |
|
|
524 | #if USE_ERROR_CHECKING |
497 | set_typeflag (p, T_IMMUTABLE); |
525 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
|
|
526 | #endif |
498 | } |
527 | } |
499 | |
528 | |
500 | #if USE_CHAR_CLASSIFIERS |
529 | #if USE_CHAR_CLASSIFIERS |
501 | static INLINE int |
530 | static INLINE int |
502 | Cisalpha (int c) |
531 | Cisalpha (int c) |
… | |
… | |
593 | static int file_push (SCHEME_P_ const char *fname); |
622 | static int file_push (SCHEME_P_ const char *fname); |
594 | static void file_pop (SCHEME_P); |
623 | static void file_pop (SCHEME_P); |
595 | static int file_interactive (SCHEME_P); |
624 | static int file_interactive (SCHEME_P); |
596 | static INLINE int is_one_of (char *s, int c); |
625 | static INLINE int is_one_of (char *s, int c); |
597 | static int alloc_cellseg (SCHEME_P_ int n); |
626 | 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); |
627 | static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); |
600 | static pointer xget_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); |
628 | static void finalize_cell (SCHEME_P_ pointer a); |
605 | static int count_consecutive_cells (pointer x, int needed); |
629 | static int count_consecutive_cells (pointer x, int needed); |
606 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
630 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
607 | static pointer mk_number (SCHEME_P_ num n); |
631 | static pointer mk_number (SCHEME_P_ const num n); |
608 | static char *store_string (SCHEME_P_ int len, const char *str, char fill); |
632 | static char *store_string (SCHEME_P_ uint32_t len, const char *str, char fill); |
609 | static pointer mk_vector (SCHEME_P_ int len); |
633 | static pointer mk_vector (SCHEME_P_ uint32_t len); |
610 | static pointer mk_atom (SCHEME_P_ char *q); |
634 | static pointer mk_atom (SCHEME_P_ char *q); |
611 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
635 | static pointer mk_sharp_const (SCHEME_P_ char *name); |
612 | |
636 | |
613 | #if USE_PORTS |
637 | #if USE_PORTS |
614 | static pointer mk_port (SCHEME_P_ port *p); |
638 | static pointer mk_port (SCHEME_P_ port *p); |
… | |
… | |
641 | static pointer ss_get_cont (SCHEME_P); |
665 | static pointer ss_get_cont (SCHEME_P); |
642 | static void ss_set_cont (SCHEME_P_ pointer cont); |
666 | static void ss_set_cont (SCHEME_P_ pointer cont); |
643 | static void dump_stack_mark (SCHEME_P); |
667 | static void dump_stack_mark (SCHEME_P); |
644 | static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); |
668 | static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); |
645 | static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); |
669 | static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); |
|
|
670 | static pointer opexe_r (SCHEME_P_ enum scheme_opcodes op); |
646 | static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
671 | static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
647 | static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
672 | static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
648 | static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
673 | static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
649 | static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
674 | static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
650 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
675 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
651 | static void assign_syntax (SCHEME_P_ char *name); |
676 | static void assign_syntax (SCHEME_P_ const char *name); |
652 | static int syntaxnum (pointer p); |
677 | static int syntaxnum (pointer p); |
653 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, char *name); |
678 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
654 | |
679 | |
655 | static num |
680 | static num |
656 | num_add (num a, num b) |
681 | num_op (enum num_op op, num a, num b) |
657 | { |
682 | { |
658 | num ret; |
683 | num ret; |
659 | |
684 | |
660 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
685 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
661 | |
686 | |
662 | if (num_is_fixnum (ret)) |
687 | if (num_is_fixnum (ret)) |
663 | num_set_ivalue (ret, a.value.ivalue + b.value.ivalue); |
688 | { |
|
|
689 | IVALUE av = num_get_ivalue (a); |
|
|
690 | IVALUE bv = num_get_ivalue (b); |
|
|
691 | |
|
|
692 | switch (op) |
|
|
693 | { |
|
|
694 | case NUM_ADD: av += bv; break; |
|
|
695 | case NUM_SUB: av -= bv; break; |
|
|
696 | case NUM_MUL: av *= bv; break; |
|
|
697 | case NUM_INTDIV: av /= bv; break; |
|
|
698 | } |
|
|
699 | |
|
|
700 | num_set_ivalue (ret, av); |
|
|
701 | } |
664 | else |
702 | else |
665 | num_set_rvalue (ret, num_rvalue (a) + num_rvalue (b)); |
703 | { |
|
|
704 | RVALUE av = num_get_rvalue (a); |
|
|
705 | RVALUE bv = num_get_rvalue (b); |
666 | |
706 | |
667 | return ret; |
707 | switch (op) |
668 | } |
708 | { |
|
|
709 | case NUM_ADD: av += bv; break; |
|
|
710 | case NUM_SUB: av -= bv; break; |
|
|
711 | case NUM_MUL: av *= bv; break; |
|
|
712 | case NUM_INTDIV: av /= bv; break; |
|
|
713 | } |
669 | |
714 | |
670 | static num |
715 | num_set_rvalue (ret, av); |
671 | num_mul (num a, num b) |
716 | } |
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 | |
717 | |
682 | return ret; |
718 | return ret; |
683 | } |
719 | } |
684 | |
720 | |
685 | static num |
721 | static num |
686 | num_div (num a, num b) |
722 | num_div (num a, num b) |
687 | { |
723 | { |
688 | num ret; |
724 | num ret; |
689 | |
725 | |
690 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && a.value.ivalue % b.value.ivalue == 0); |
726 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); |
691 | |
727 | |
692 | if (num_is_fixnum (ret)) |
728 | if (num_is_fixnum (ret)) |
693 | num_set_ivalue (ret, a.value.ivalue / b.value.ivalue); |
729 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
694 | else |
730 | else |
695 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
731 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_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 | |
732 | |
727 | return ret; |
733 | return ret; |
728 | } |
734 | } |
729 | |
735 | |
730 | static num |
736 | static num |
… | |
… | |
732 | { |
738 | { |
733 | num ret; |
739 | num ret; |
734 | long e1, e2, res; |
740 | long e1, e2, res; |
735 | |
741 | |
736 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
742 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
737 | e1 = num_ivalue (a); |
743 | e1 = num_get_ivalue (a); |
738 | e2 = num_ivalue (b); |
744 | e2 = num_get_ivalue (b); |
739 | res = e1 % e2; |
745 | res = e1 % e2; |
740 | |
746 | |
741 | /* remainder should have same sign as second operand */ |
747 | /* remainder should have same sign as second operand */ |
742 | if (res > 0) |
748 | if (res > 0) |
743 | { |
749 | { |
… | |
… | |
759 | { |
765 | { |
760 | num ret; |
766 | num ret; |
761 | long e1, e2, res; |
767 | long e1, e2, res; |
762 | |
768 | |
763 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
769 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
764 | e1 = num_ivalue (a); |
770 | e1 = num_get_ivalue (a); |
765 | e2 = num_ivalue (b); |
771 | e2 = num_get_ivalue (b); |
766 | res = e1 % e2; |
772 | res = e1 % e2; |
767 | |
773 | |
768 | /* modulo should have same sign as second operand */ |
774 | /* modulo should have same sign as second operand */ |
769 | if (res * e2 < 0) |
775 | if (res * e2 < 0) |
770 | res += e2; |
776 | res += e2; |
771 | |
777 | |
772 | num_set_ivalue (ret, res); |
778 | num_set_ivalue (ret, res); |
773 | return ret; |
779 | return ret; |
774 | } |
780 | } |
775 | |
781 | |
|
|
782 | /* this completely disrespects NaNs */ |
776 | static int |
783 | static int |
777 | num_eq (num a, num b) |
784 | num_cmp (num a, num b) |
778 | { |
785 | { |
|
|
786 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
779 | int ret; |
787 | int ret; |
780 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
|
|
781 | |
788 | |
782 | if (is_fixnum) |
789 | if (is_fixnum) |
783 | ret = a.value.ivalue == b.value.ivalue; |
790 | { |
|
|
791 | IVALUE av = num_get_ivalue (a); |
|
|
792 | IVALUE bv = num_get_ivalue (b); |
|
|
793 | |
|
|
794 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
|
|
795 | } |
784 | else |
796 | else |
785 | ret = num_rvalue (a) == num_rvalue (b); |
797 | { |
|
|
798 | RVALUE av = num_get_rvalue (a); |
|
|
799 | RVALUE bv = num_get_rvalue (b); |
|
|
800 | |
|
|
801 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
|
|
802 | } |
786 | |
803 | |
787 | return ret; |
804 | 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 | } |
805 | } |
830 | |
806 | |
831 | #if USE_MATH |
807 | #if USE_MATH |
832 | |
808 | |
833 | /* Round to nearest. Round to even if midway */ |
809 | /* Round to nearest. Round to even if midway */ |
… | |
… | |
854 | #endif |
830 | #endif |
855 | |
831 | |
856 | static int |
832 | static int |
857 | is_zero_rvalue (RVALUE x) |
833 | is_zero_rvalue (RVALUE x) |
858 | { |
834 | { |
859 | #if USE_FLOAT |
835 | #if USE_REAL |
860 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
836 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
861 | #else |
837 | #else |
862 | return x == 0; |
838 | return x == 0; |
863 | #endif |
839 | #endif |
864 | } |
|
|
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 | } |
840 | } |
880 | |
841 | |
881 | /* allocate new cell segment */ |
842 | /* allocate new cell segment */ |
882 | static int |
843 | static int |
883 | alloc_cellseg (SCHEME_P_ int n) |
844 | alloc_cellseg (SCHEME_P_ int n) |
… | |
… | |
927 | SCHEME_V->fcells += segsize; |
888 | SCHEME_V->fcells += segsize; |
928 | last = newp + segsize - 1; |
889 | last = newp + segsize - 1; |
929 | |
890 | |
930 | for (p = newp; p <= last; p++) |
891 | for (p = newp; p <= last; p++) |
931 | { |
892 | { |
932 | set_typeflag (p, 0); |
893 | set_typeflag (p, T_FREE); |
|
|
894 | set_car (p, NIL); |
933 | set_cdr (p, p + 1); |
895 | set_cdr (p, p + 1); |
934 | set_car (p, NIL); |
|
|
935 | } |
896 | } |
936 | |
897 | |
937 | /* insert new cells in address order on free list */ |
898 | /* insert new cells in address order on free list */ |
938 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
899 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
939 | { |
900 | { |
… | |
… | |
953 | } |
914 | } |
954 | |
915 | |
955 | return n; |
916 | return n; |
956 | } |
917 | } |
957 | |
918 | |
958 | /* get new cell. parameter a, b is marked by gc. */ |
919 | /* get new cell. parameter a, b is marked by gc. */ |
959 | static INLINE pointer |
920 | static INLINE pointer |
960 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
921 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
961 | { |
922 | { |
962 | if (SCHEME_V->free_cell == NIL) |
923 | if (ecb_expect_false (SCHEME_V->free_cell == NIL)) |
963 | { |
924 | { |
964 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
925 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
965 | return S_SINK; |
926 | return S_SINK; |
966 | |
927 | |
967 | if (SCHEME_V->free_cell == NIL) |
928 | if (SCHEME_V->free_cell == NIL) |
… | |
… | |
991 | --SCHEME_V->fcells; |
952 | --SCHEME_V->fcells; |
992 | return x; |
953 | return x; |
993 | } |
954 | } |
994 | } |
955 | } |
995 | |
956 | |
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 - |
957 | /* To retain recent allocs before interpreter knows about them - |
1110 | Tehom */ |
958 | Tehom */ |
1111 | |
959 | |
1112 | static void |
960 | static void |
1113 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
961 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1114 | { |
962 | { |
1115 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
963 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1116 | |
964 | |
1117 | set_typeflag (holder, T_PAIR | T_IMMUTABLE); |
965 | set_typeflag (holder, T_PAIR); |
|
|
966 | setimmutable (holder); |
1118 | set_car (holder, recent); |
967 | set_car (holder, recent); |
1119 | set_cdr (holder, car (S_SINK)); |
968 | set_cdr (holder, car (S_SINK)); |
1120 | set_car (S_SINK, holder); |
969 | set_car (S_SINK, holder); |
1121 | } |
970 | } |
1122 | |
971 | |
… | |
… | |
1135 | |
984 | |
1136 | return cell; |
985 | return cell; |
1137 | } |
986 | } |
1138 | |
987 | |
1139 | static pointer |
988 | static pointer |
1140 | get_vector_object (SCHEME_P_ int len, pointer init) |
989 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1141 | { |
990 | { |
1142 | pointer cells = get_consecutive_cells (SCHEME_A_ len / 2 + len % 2 + 1); |
991 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
|
|
992 | pointer *e = malloc (len * sizeof (pointer)); |
1143 | |
993 | |
1144 | #if USE_ERROR_CHECKING |
994 | if (!e && USE_ERROR_CHECKING) |
1145 | if (SCHEME_V->no_memory) |
|
|
1146 | return S_SINK; |
995 | return S_SINK; |
1147 | #endif |
|
|
1148 | |
996 | |
1149 | /* Record it as a vector so that gc understands it. */ |
997 | /* Record it as a vector so that gc understands it. */ |
1150 | set_typeflag (cells, T_VECTOR | T_ATOM); |
998 | set_typeflag (v, T_VECTOR | T_ATOM); |
1151 | ivalue_unchecked (cells) = len; |
999 | |
1152 | set_num_integer (cells); |
1000 | v->object.vector.vvalue = e; |
|
|
1001 | v->object.vector.length = len; |
1153 | fill_vector (cells, init); |
1002 | fill_vector (v, init); |
1154 | push_recent_alloc (SCHEME_A_ cells, NIL); |
1003 | push_recent_alloc (SCHEME_A_ v, NIL); |
1155 | |
1004 | |
1156 | return cells; |
1005 | return v; |
1157 | } |
1006 | } |
1158 | |
1007 | |
1159 | static INLINE void |
1008 | static INLINE void |
1160 | ok_to_freely_gc (SCHEME_P) |
1009 | ok_to_freely_gc (SCHEME_P) |
1161 | { |
1010 | { |
… | |
… | |
1164 | |
1013 | |
1165 | #if defined TSGRIND |
1014 | #if defined TSGRIND |
1166 | static void |
1015 | static void |
1167 | check_cell_alloced (pointer p, int expect_alloced) |
1016 | check_cell_alloced (pointer p, int expect_alloced) |
1168 | { |
1017 | { |
1169 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to |
1018 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1170 | sc. */ |
|
|
1171 | if (typeflag (p) & !expect_alloced) |
1019 | if (typeflag (p) & !expect_alloced) |
1172 | xwrstr ("Cell is already allocated!\n"); |
1020 | xwrstr ("Cell is already allocated!\n"); |
1173 | |
1021 | |
1174 | if (!(typeflag (p)) & expect_alloced) |
1022 | if (!(typeflag (p)) & expect_alloced) |
1175 | xwrstr ("Cell is not allocated!\n"); |
1023 | xwrstr ("Cell is not allocated!\n"); |
… | |
… | |
1198 | if (immutable) |
1046 | if (immutable) |
1199 | setimmutable (x); |
1047 | setimmutable (x); |
1200 | |
1048 | |
1201 | set_car (x, a); |
1049 | set_car (x, a); |
1202 | set_cdr (x, b); |
1050 | set_cdr (x, b); |
|
|
1051 | |
1203 | return x; |
1052 | return x; |
1204 | } |
1053 | } |
1205 | |
1054 | |
1206 | /* ========== oblist implementation ========== */ |
1055 | /* ========== oblist implementation ========== */ |
1207 | |
1056 | |
… | |
… | |
1217 | |
1066 | |
1218 | /* returns the new symbol */ |
1067 | /* returns the new symbol */ |
1219 | static pointer |
1068 | static pointer |
1220 | oblist_add_by_name (SCHEME_P_ const char *name) |
1069 | oblist_add_by_name (SCHEME_P_ const char *name) |
1221 | { |
1070 | { |
1222 | pointer x; |
|
|
1223 | int location; |
1071 | int location; |
1224 | |
1072 | |
1225 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
1073 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
1226 | set_typeflag (x, T_SYMBOL); |
1074 | set_typeflag (x, T_SYMBOL); |
1227 | setimmutable (car (x)); |
1075 | setimmutable (car (x)); |
1228 | |
1076 | |
1229 | location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); |
1077 | 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))); |
1078 | set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); |
1231 | return x; |
1079 | return x; |
1232 | } |
1080 | } |
1233 | |
1081 | |
1234 | static INLINE pointer |
1082 | static INLINE pointer |
… | |
… | |
1236 | { |
1084 | { |
1237 | int location; |
1085 | int location; |
1238 | pointer x; |
1086 | pointer x; |
1239 | char *s; |
1087 | char *s; |
1240 | |
1088 | |
1241 | location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); |
1089 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1242 | |
1090 | |
1243 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1091 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1244 | { |
1092 | { |
1245 | s = symname (car (x)); |
1093 | s = symname (car (x)); |
1246 | |
1094 | |
… | |
… | |
1257 | { |
1105 | { |
1258 | int i; |
1106 | int i; |
1259 | pointer x; |
1107 | pointer x; |
1260 | pointer ob_list = NIL; |
1108 | pointer ob_list = NIL; |
1261 | |
1109 | |
1262 | for (i = 0; i < ivalue_unchecked (SCHEME_V->oblist); i++) |
1110 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1263 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1111 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1264 | ob_list = cons (x, ob_list); |
1112 | ob_list = cons (x, ob_list); |
1265 | |
1113 | |
1266 | return ob_list; |
1114 | return ob_list; |
1267 | } |
1115 | } |
… | |
… | |
1341 | mk_character (SCHEME_P_ int c) |
1189 | mk_character (SCHEME_P_ int c) |
1342 | { |
1190 | { |
1343 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1191 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1344 | |
1192 | |
1345 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1193 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1346 | ivalue_unchecked (x) = c; |
1194 | ivalue_unchecked (x) = c & 0xff; |
1347 | set_num_integer (x); |
1195 | set_num_integer (x); |
1348 | return x; |
1196 | return x; |
1349 | } |
1197 | } |
1350 | |
1198 | |
1351 | /* get number atom (integer) */ |
1199 | /* get number atom (integer) */ |
… | |
… | |
1370 | set_num_real (x); |
1218 | set_num_real (x); |
1371 | return x; |
1219 | return x; |
1372 | } |
1220 | } |
1373 | |
1221 | |
1374 | static pointer |
1222 | static pointer |
1375 | mk_number (SCHEME_P_ num n) |
1223 | mk_number (SCHEME_P_ const num n) |
1376 | { |
1224 | { |
1377 | if (num_is_fixnum (n)) |
1225 | if (num_is_fixnum (n)) |
1378 | { |
|
|
1379 | return mk_integer (SCHEME_A_ num_ivalue (n)); |
1226 | return mk_integer (SCHEME_A_ num_get_ivalue (n)); |
1380 | } |
|
|
1381 | else |
1227 | else |
1382 | { |
|
|
1383 | return mk_real (SCHEME_A_ num_rvalue (n)); |
1228 | return mk_real (SCHEME_A_ num_get_rvalue (n)); |
1384 | } |
|
|
1385 | } |
1229 | } |
1386 | |
1230 | |
1387 | /* allocate name to string area */ |
1231 | /* allocate name to string area */ |
1388 | static char * |
1232 | static char * |
1389 | store_string (SCHEME_P_ int len_str, const char *str, char fill) |
1233 | store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) |
1390 | { |
1234 | { |
1391 | char *q; |
|
|
1392 | |
|
|
1393 | q = (char *)malloc (len_str + 1); |
1235 | char *q = malloc (len_str + 1); |
1394 | |
1236 | |
1395 | if (q == 0) |
1237 | if (q == 0 && USE_ERROR_CHECKING) |
1396 | { |
1238 | { |
1397 | #if USE_ERROR_CHECKING |
|
|
1398 | SCHEME_V->no_memory = 1; |
1239 | SCHEME_V->no_memory = 1; |
1399 | return SCHEME_V->strbuff; |
1240 | return SCHEME_V->strbuff; |
1400 | #endif |
|
|
1401 | } |
1241 | } |
1402 | |
1242 | |
1403 | if (str) |
1243 | if (str) |
1404 | { |
1244 | { |
1405 | int l = strlen (str); |
1245 | int l = strlen (str); |
1406 | |
1246 | |
1407 | if (l > len_str) |
1247 | if (l > len_str) |
1408 | l = len_str; |
1248 | l = len_str; |
1409 | |
1249 | |
1410 | memcpy (q, str, l + 1); |
1250 | memcpy (q, str, l); |
1411 | q[l + 1] = 0; |
1251 | q[l] = 0; |
1412 | } |
1252 | } |
1413 | else |
1253 | else |
1414 | { |
1254 | { |
1415 | memset (q, fill, len_str); |
1255 | memset (q, fill, len_str); |
1416 | q[len_str] = 0; |
1256 | q[len_str] = 0; |
1417 | } |
1257 | } |
1418 | |
1258 | |
1419 | return q; |
1259 | return q; |
1420 | } |
1260 | } |
1421 | |
1261 | |
1422 | /* get new string */ |
|
|
1423 | INTERFACE pointer |
1262 | INTERFACE pointer |
1424 | mk_string (SCHEME_P_ const char *str) |
1263 | mk_empty_string (SCHEME_P_ uint32_t len, char fill) |
1425 | { |
1264 | { |
1426 | return mk_counted_string (SCHEME_A_ str, strlen (str)); |
1265 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
|
|
1266 | |
|
|
1267 | set_typeflag (x, T_STRING | T_ATOM); |
|
|
1268 | strvalue (x) = store_string (SCHEME_A_ len, 0, fill); |
|
|
1269 | strlength (x) = len; |
|
|
1270 | return x; |
1427 | } |
1271 | } |
1428 | |
1272 | |
1429 | INTERFACE pointer |
1273 | INTERFACE pointer |
1430 | mk_counted_string (SCHEME_P_ const char *str, int len) |
1274 | mk_counted_string (SCHEME_P_ const char *str, uint32_t len) |
1431 | { |
1275 | { |
1432 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1276 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1433 | |
1277 | |
1434 | set_typeflag (x, T_STRING | T_ATOM); |
1278 | set_typeflag (x, T_STRING | T_ATOM); |
1435 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1279 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1436 | strlength (x) = len; |
1280 | strlength (x) = len; |
1437 | return x; |
1281 | return x; |
1438 | } |
1282 | } |
1439 | |
1283 | |
1440 | INTERFACE pointer |
1284 | INTERFACE pointer |
1441 | mk_empty_string (SCHEME_P_ int len, char fill) |
1285 | mk_string (SCHEME_P_ const char *str) |
1442 | { |
1286 | { |
1443 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1287 | return mk_counted_string (SCHEME_A_ str, strlen (str)); |
1444 | |
|
|
1445 | set_typeflag (x, T_STRING | T_ATOM); |
|
|
1446 | strvalue (x) = store_string (SCHEME_A_ len, 0, fill); |
|
|
1447 | strlength (x) = len; |
|
|
1448 | return x; |
|
|
1449 | } |
1288 | } |
1450 | |
1289 | |
1451 | INTERFACE pointer |
1290 | INTERFACE pointer |
1452 | mk_vector (SCHEME_P_ int len) |
1291 | mk_vector (SCHEME_P_ uint32_t len) |
1453 | { |
1292 | { |
1454 | return get_vector_object (SCHEME_A_ len, NIL); |
1293 | return get_vector_object (SCHEME_A_ len, NIL); |
1455 | } |
1294 | } |
1456 | |
1295 | |
1457 | INTERFACE void |
1296 | INTERFACE void |
1458 | fill_vector (pointer vec, pointer obj) |
1297 | fill_vector (pointer vec, pointer obj) |
1459 | { |
1298 | { |
1460 | int i; |
1299 | int i; |
1461 | int num = ivalue (vec) / 2 + ivalue (vec) % 2; |
|
|
1462 | |
1300 | |
1463 | for (i = 0; i < num; i++) |
1301 | for (i = 0; i < vec->object.vector.length; i++) |
1464 | { |
1302 | vecvalue (vec)[i] = obj; |
1465 | set_typeflag (vec + 1 + i, T_PAIR); |
|
|
1466 | setimmutable (vec + 1 + i); |
|
|
1467 | set_car (vec + 1 + i, obj); |
|
|
1468 | set_cdr (vec + 1 + i, obj); |
|
|
1469 | } |
|
|
1470 | } |
1303 | } |
1471 | |
1304 | |
1472 | INTERFACE pointer |
1305 | INTERFACE pointer |
1473 | vector_elem (pointer vec, int ielem) |
1306 | vector_elem (pointer vec, uint32_t ielem) |
1474 | { |
1307 | { |
1475 | int n = ielem / 2; |
1308 | 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 | } |
1309 | } |
1482 | |
1310 | |
1483 | INTERFACE void |
1311 | INTERFACE void |
1484 | set_vector_elem (pointer vec, int ielem, pointer a) |
1312 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1485 | { |
1313 | { |
1486 | int n = ielem / 2; |
1314 | 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 | } |
1315 | } |
1493 | |
1316 | |
1494 | /* get new symbol */ |
1317 | /* get new symbol */ |
1495 | INTERFACE pointer |
1318 | INTERFACE pointer |
1496 | mk_symbol (SCHEME_P_ const char *name) |
1319 | mk_symbol (SCHEME_P_ const char *name) |
1497 | { |
1320 | { |
1498 | pointer x; |
|
|
1499 | |
|
|
1500 | /* first check oblist */ |
1321 | /* first check oblist */ |
1501 | x = oblist_find_by_name (SCHEME_A_ name); |
1322 | pointer x = oblist_find_by_name (SCHEME_A_ name); |
1502 | |
1323 | |
1503 | if (x != NIL) |
1324 | if (x == NIL) |
1504 | return x; |
|
|
1505 | else |
|
|
1506 | { |
|
|
1507 | x = oblist_add_by_name (SCHEME_A_ name); |
1325 | x = oblist_add_by_name (SCHEME_A_ name); |
|
|
1326 | |
1508 | return x; |
1327 | return x; |
1509 | } |
|
|
1510 | } |
1328 | } |
1511 | |
1329 | |
1512 | INTERFACE pointer |
1330 | INTERFACE pointer |
1513 | gensym (SCHEME_P) |
1331 | gensym (SCHEME_P) |
1514 | { |
1332 | { |
1515 | pointer x; |
1333 | pointer x; |
1516 | char name[40]; |
|
|
1517 | |
1334 | |
1518 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
1335 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
1519 | { |
1336 | { |
1520 | strcpy (name, "gensym-"); |
1337 | char name[40] = "gensym-"; |
1521 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1338 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1522 | |
1339 | |
1523 | /* first check oblist */ |
1340 | /* first check oblist */ |
1524 | x = oblist_find_by_name (SCHEME_A_ name); |
1341 | x = oblist_find_by_name (SCHEME_A_ name); |
1525 | |
1342 | |
1526 | if (x != NIL) |
1343 | if (x == NIL) |
1527 | continue; |
|
|
1528 | else |
|
|
1529 | { |
1344 | { |
1530 | x = oblist_add_by_name (SCHEME_A_ name); |
1345 | x = oblist_add_by_name (SCHEME_A_ name); |
1531 | return x; |
1346 | return x; |
1532 | } |
1347 | } |
1533 | } |
1348 | } |
… | |
… | |
1542 | char c, *p; |
1357 | char c, *p; |
1543 | int has_dec_point = 0; |
1358 | int has_dec_point = 0; |
1544 | int has_fp_exp = 0; |
1359 | int has_fp_exp = 0; |
1545 | |
1360 | |
1546 | #if USE_COLON_HOOK |
1361 | #if USE_COLON_HOOK |
1547 | |
|
|
1548 | if ((p = strstr (q, "::")) != 0) |
1362 | if ((p = strstr (q, "::")) != 0) |
1549 | { |
1363 | { |
1550 | *p = 0; |
1364 | *p = 0; |
1551 | return cons (SCHEME_V->COLON_HOOK, |
1365 | return cons (SCHEME_V->COLON_HOOK, |
1552 | cons (cons (SCHEME_V->QUOTE, |
1366 | cons (cons (SCHEME_V->QUOTE, |
1553 | cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL))); |
1367 | cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL))); |
1554 | } |
1368 | } |
1555 | |
|
|
1556 | #endif |
1369 | #endif |
1557 | |
1370 | |
1558 | p = q; |
1371 | p = q; |
1559 | c = *p++; |
1372 | c = *p++; |
1560 | |
1373 | |
… | |
… | |
1567 | has_dec_point = 1; |
1380 | has_dec_point = 1; |
1568 | c = *p++; |
1381 | c = *p++; |
1569 | } |
1382 | } |
1570 | |
1383 | |
1571 | if (!isdigit (c)) |
1384 | if (!isdigit (c)) |
1572 | { |
|
|
1573 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1385 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1574 | } |
|
|
1575 | } |
1386 | } |
1576 | else if (c == '.') |
1387 | else if (c == '.') |
1577 | { |
1388 | { |
1578 | has_dec_point = 1; |
1389 | has_dec_point = 1; |
1579 | c = *p++; |
1390 | c = *p++; |
1580 | |
1391 | |
1581 | if (!isdigit (c)) |
1392 | if (!isdigit (c)) |
1582 | { |
|
|
1583 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1393 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1584 | } |
|
|
1585 | } |
1394 | } |
1586 | else if (!isdigit (c)) |
1395 | else if (!isdigit (c)) |
1587 | { |
|
|
1588 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1396 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1589 | } |
|
|
1590 | |
1397 | |
1591 | for (; (c = *p) != 0; ++p) |
1398 | for (; (c = *p) != 0; ++p) |
1592 | { |
1399 | { |
1593 | if (!isdigit (c)) |
1400 | if (!isdigit (c)) |
1594 | { |
1401 | { |
… | |
… | |
1607 | has_dec_point = 1; /* decimal point illegal |
1414 | has_dec_point = 1; /* decimal point illegal |
1608 | from now on */ |
1415 | from now on */ |
1609 | p++; |
1416 | p++; |
1610 | |
1417 | |
1611 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1418 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1612 | { |
|
|
1613 | continue; |
1419 | continue; |
1614 | } |
|
|
1615 | } |
1420 | } |
1616 | } |
1421 | } |
1617 | |
1422 | |
1618 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1423 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1619 | } |
1424 | } |
1620 | } |
1425 | } |
1621 | |
1426 | |
1622 | #if USE_FLOAT |
1427 | #if USE_REAL |
1623 | if (has_dec_point) |
1428 | if (has_dec_point) |
1624 | return mk_real (SCHEME_A_ atof (q)); |
1429 | return mk_real (SCHEME_A_ atof (q)); |
1625 | #endif |
1430 | #endif |
1626 | |
1431 | |
1627 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1432 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
… | |
… | |
1629 | |
1434 | |
1630 | /* make constant */ |
1435 | /* make constant */ |
1631 | static pointer |
1436 | static pointer |
1632 | mk_sharp_const (SCHEME_P_ char *name) |
1437 | mk_sharp_const (SCHEME_P_ char *name) |
1633 | { |
1438 | { |
1634 | long x; |
|
|
1635 | char tmp[STRBUFFSIZE]; |
|
|
1636 | |
|
|
1637 | if (!strcmp (name, "t")) |
1439 | if (!strcmp (name, "t")) |
1638 | return S_T; |
1440 | return S_T; |
1639 | else if (!strcmp (name, "f")) |
1441 | else if (!strcmp (name, "f")) |
1640 | return S_F; |
1442 | 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) */ |
1443 | else if (*name == '\\') /* #\w (character) */ |
1662 | { |
1444 | { |
1663 | int c = 0; |
1445 | int c; |
1664 | |
1446 | |
1665 | if (stricmp (name + 1, "space") == 0) |
1447 | if (stricmp (name + 1, "space") == 0) |
1666 | c = ' '; |
1448 | c = ' '; |
1667 | else if (stricmp (name + 1, "newline") == 0) |
1449 | else if (stricmp (name + 1, "newline") == 0) |
1668 | c = '\n'; |
1450 | c = '\n'; |
… | |
… | |
1670 | c = '\r'; |
1452 | c = '\r'; |
1671 | else if (stricmp (name + 1, "tab") == 0) |
1453 | else if (stricmp (name + 1, "tab") == 0) |
1672 | c = '\t'; |
1454 | c = '\t'; |
1673 | else if (name[1] == 'x' && name[2] != 0) |
1455 | else if (name[1] == 'x' && name[2] != 0) |
1674 | { |
1456 | { |
1675 | int c1 = strtol (name + 2, 0, 16); |
1457 | long c1 = strtol (name + 2, 0, 16); |
1676 | |
1458 | |
1677 | if (c1 <= UCHAR_MAX) |
1459 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1678 | c = c1; |
1460 | c = c1; |
1679 | else |
1461 | else |
1680 | return NIL; |
1462 | return NIL; |
1681 | |
1463 | } |
1682 | #if USE_ASCII_NAMES |
1464 | #if USE_ASCII_NAMES |
1683 | } |
|
|
1684 | else if (is_ascii_name (name + 1, &c)) |
1465 | else if (is_ascii_name (name + 1, &c)) |
1685 | { |
|
|
1686 | /* nothing */ |
1466 | /* nothing */; |
1687 | #endif |
1467 | #endif |
1688 | } |
|
|
1689 | else if (name[2] == 0) |
1468 | else if (name[2] == 0) |
1690 | c = name[1]; |
1469 | c = name[1]; |
1691 | else |
1470 | else |
1692 | return NIL; |
1471 | return NIL; |
1693 | |
1472 | |
1694 | return mk_character (SCHEME_A_ c); |
1473 | return mk_character (SCHEME_A_ c); |
1695 | } |
1474 | } |
1696 | else |
1475 | else |
|
|
1476 | { |
|
|
1477 | /* identify base by string index */ |
|
|
1478 | const char baseidx[17] = "ffbf" "ffff" "ofdf" "ffff" "x"; |
|
|
1479 | char *base = strchr (baseidx, *name); |
|
|
1480 | |
|
|
1481 | if (base) |
|
|
1482 | return mk_integer (SCHEME_A_ strtol (name + 1, 0, base - baseidx)); |
|
|
1483 | |
1697 | return NIL; |
1484 | return NIL; |
|
|
1485 | } |
1698 | } |
1486 | } |
1699 | |
1487 | |
1700 | /* ========== garbage collector ========== */ |
1488 | /* ========== garbage collector ========== */ |
1701 | |
1489 | |
1702 | /*-- |
1490 | /*-- |
1703 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1491 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1704 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1492 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1705 | * for marking. |
1493 | * for marking. |
|
|
1494 | * |
|
|
1495 | * The exception is vectors - vectors are currently marked recursively, |
|
|
1496 | * which is inherited form tinyscheme and could be fixed by having another |
|
|
1497 | * word of context in the vector |
1706 | */ |
1498 | */ |
1707 | static void |
1499 | static void |
1708 | mark (pointer a) |
1500 | mark (pointer a) |
1709 | { |
1501 | { |
1710 | pointer t, q, p; |
1502 | pointer t, q, p; |
1711 | |
1503 | |
1712 | t = (pointer) 0; |
1504 | t = 0; |
1713 | p = a; |
1505 | p = a; |
1714 | E2: |
1506 | E2: |
1715 | setmark (p); |
1507 | setmark (p); |
1716 | printf ("mark %p %x\n", p, p->flag);//D |
|
|
1717 | |
1508 | |
1718 | if (is_vector (p)) |
1509 | if (ecb_expect_false (is_vector (p))) |
1719 | { |
1510 | { |
1720 | int i; |
1511 | int i; |
1721 | int num = ivalue_unchecked (p) / 2 + ivalue_unchecked (p) % 2; |
|
|
1722 | |
1512 | |
1723 | for (i = 0; i < num; i++) |
1513 | for (i = 0; i < p->object.vector.length; i++) |
1724 | { |
1514 | mark (vecvalue (p)[i]); |
1725 | /* Vector cells will be treated like ordinary cells */ |
|
|
1726 | mark (p + 1 + i); |
|
|
1727 | } |
|
|
1728 | } |
1515 | } |
1729 | |
1516 | |
1730 | if (is_atom (p)) |
1517 | if (is_atom (p)) |
1731 | goto E6; |
1518 | goto E6; |
1732 | |
1519 | |
… | |
… | |
1742 | goto E2; |
1529 | goto E2; |
1743 | } |
1530 | } |
1744 | |
1531 | |
1745 | E5: |
1532 | E5: |
1746 | q = cdr (p); /* down cdr */ |
1533 | q = cdr (p); /* down cdr */ |
1747 | printf ("mark+ %p\n", q, q->flag);//D |
|
|
1748 | |
1534 | |
1749 | if (q && !is_mark (q)) |
1535 | if (q && !is_mark (q)) |
1750 | { |
1536 | { |
1751 | set_cdr (p, t); |
1537 | set_cdr (p, t); |
1752 | t = p; |
1538 | t = p; |
1753 | p = q; |
1539 | p = q; |
1754 | goto E2; |
1540 | goto E2; |
1755 | } |
1541 | } |
1756 | |
1542 | |
1757 | E6: /* up. Undo the link switching from steps E4 and E5. */ |
1543 | E6: /* up. Undo the link switching from steps E4 and E5. */ |
1758 | |
|
|
1759 | if (!t) |
1544 | if (!t) |
1760 | return; |
1545 | return; |
1761 | |
1546 | |
1762 | q = t; |
1547 | q = t; |
1763 | |
1548 | |
… | |
… | |
1831 | if (is_mark (p)) |
1616 | if (is_mark (p)) |
1832 | clrmark (p); |
1617 | clrmark (p); |
1833 | else |
1618 | else |
1834 | { |
1619 | { |
1835 | /* reclaim cell */ |
1620 | /* reclaim cell */ |
1836 | if (typeflag (p) != 0) |
1621 | if (typeflag (p) != T_FREE) |
1837 | { |
1622 | { |
1838 | finalize_cell (SCHEME_A_ p); |
1623 | finalize_cell (SCHEME_A_ p); |
1839 | set_typeflag (p, 0); |
1624 | set_typeflag (p, T_FREE); |
1840 | set_car (p, NIL); |
1625 | set_car (p, NIL); |
1841 | } |
1626 | } |
1842 | |
1627 | |
1843 | ++SCHEME_V->fcells; |
1628 | ++SCHEME_V->fcells; |
1844 | set_cdr (p, SCHEME_V->free_cell); |
1629 | set_cdr (p, SCHEME_V->free_cell); |
… | |
… | |
1852 | } |
1637 | } |
1853 | |
1638 | |
1854 | static void |
1639 | static void |
1855 | finalize_cell (SCHEME_P_ pointer a) |
1640 | finalize_cell (SCHEME_P_ pointer a) |
1856 | { |
1641 | { |
|
|
1642 | /* TODO, fast bitmap check? */ |
1857 | if (is_string (a)) |
1643 | if (is_string (a)) |
1858 | free (strvalue (a)); |
1644 | free (strvalue (a)); |
|
|
1645 | else if (is_vector (a)) |
|
|
1646 | free (vecvalue (a)); |
1859 | #if USE_PORTS |
1647 | #if USE_PORTS |
1860 | else if (is_port (a)) |
1648 | else if (is_port (a)) |
1861 | { |
1649 | { |
1862 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1650 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1863 | port_close (SCHEME_A_ a, port_input | port_output); |
1651 | port_close (SCHEME_A_ a, port_input | port_output); |
… | |
… | |
2289 | char *p = SCHEME_V->strbuff; |
2077 | char *p = SCHEME_V->strbuff; |
2290 | |
2078 | |
2291 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2079 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2292 | |
2080 | |
2293 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2081 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2294 | { |
|
|
2295 | *p = 0; |
2082 | *p = 0; |
2296 | } |
|
|
2297 | else |
2083 | else |
2298 | { |
2084 | { |
2299 | backchar (SCHEME_A_ p[-1]); |
2085 | backchar (SCHEME_A_ p[-1]); |
2300 | *--p = '\0'; |
2086 | *--p = '\0'; |
2301 | } |
2087 | } |
… | |
… | |
2316 | for (;;) |
2102 | for (;;) |
2317 | { |
2103 | { |
2318 | c = inchar (SCHEME_A); |
2104 | c = inchar (SCHEME_A); |
2319 | |
2105 | |
2320 | if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1) |
2106 | if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1) |
2321 | { |
|
|
2322 | return S_F; |
2107 | return S_F; |
2323 | } |
|
|
2324 | |
2108 | |
2325 | switch (state) |
2109 | switch (state) |
2326 | { |
2110 | { |
2327 | case st_ok: |
2111 | case st_ok: |
2328 | switch (c) |
2112 | switch (c) |
… | |
… | |
2396 | c = toupper (c); |
2180 | c = toupper (c); |
2397 | |
2181 | |
2398 | if (c >= '0' && c <= 'F') |
2182 | if (c >= '0' && c <= 'F') |
2399 | { |
2183 | { |
2400 | if (c <= '9') |
2184 | if (c <= '9') |
2401 | { |
|
|
2402 | c1 = (c1 << 4) + c - '0'; |
2185 | c1 = (c1 << 4) + c - '0'; |
2403 | } |
|
|
2404 | else |
2186 | else |
2405 | { |
|
|
2406 | c1 = (c1 << 4) + c - 'A' + 10; |
2187 | c1 = (c1 << 4) + c - 'A' + 10; |
2407 | } |
|
|
2408 | |
2188 | |
2409 | if (state == st_x1) |
2189 | if (state == st_x1) |
2410 | { |
|
|
2411 | state = st_x2; |
2190 | state = st_x2; |
2412 | } |
|
|
2413 | else |
2191 | else |
2414 | { |
2192 | { |
2415 | *p++ = c1; |
2193 | *p++ = c1; |
2416 | state = st_ok; |
2194 | state = st_ok; |
2417 | } |
2195 | } |
2418 | } |
2196 | } |
2419 | else |
2197 | else |
2420 | { |
|
|
2421 | return S_F; |
2198 | return S_F; |
2422 | } |
|
|
2423 | |
2199 | |
2424 | break; |
2200 | break; |
2425 | |
2201 | |
2426 | case st_oct1: |
2202 | case st_oct1: |
2427 | case st_oct2: |
2203 | case st_oct2: |
… | |
… | |
2458 | is_one_of (char *s, int c) |
2234 | is_one_of (char *s, int c) |
2459 | { |
2235 | { |
2460 | if (c == EOF) |
2236 | if (c == EOF) |
2461 | return 1; |
2237 | return 1; |
2462 | |
2238 | |
2463 | while (*s) |
2239 | return !!strchr (s, c); |
2464 | if (*s++ == c) |
|
|
2465 | return 1; |
|
|
2466 | |
|
|
2467 | return 0; |
|
|
2468 | } |
2240 | } |
2469 | |
2241 | |
2470 | /* skip white characters */ |
2242 | /* skip white characters */ |
2471 | static INLINE int |
2243 | static INLINE int |
2472 | skipspace (SCHEME_P) |
2244 | skipspace (SCHEME_P) |
… | |
… | |
2477 | { |
2249 | { |
2478 | c = inchar (SCHEME_A); |
2250 | c = inchar (SCHEME_A); |
2479 | #if SHOW_ERROR_LINE |
2251 | #if SHOW_ERROR_LINE |
2480 | if (c == '\n') |
2252 | if (c == '\n') |
2481 | curr_line++; |
2253 | curr_line++; |
2482 | |
|
|
2483 | #endif |
2254 | #endif |
2484 | } |
2255 | } |
2485 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2256 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2486 | |
2257 | |
2487 | /* record it */ |
2258 | /* record it */ |
… | |
… | |
2524 | |
2295 | |
2525 | if (is_one_of (" \n\t", c)) |
2296 | if (is_one_of (" \n\t", c)) |
2526 | return TOK_DOT; |
2297 | return TOK_DOT; |
2527 | else |
2298 | else |
2528 | { |
2299 | { |
|
|
2300 | //TODO: ungetc twice in a row is not supported in C |
2529 | backchar (SCHEME_A_ c); |
2301 | backchar (SCHEME_A_ c); |
2530 | backchar (SCHEME_A_ '.'); |
2302 | backchar (SCHEME_A_ '.'); |
2531 | return TOK_ATOM; |
2303 | return TOK_ATOM; |
2532 | } |
2304 | } |
2533 | |
2305 | |
… | |
… | |
2647 | int d = *s / 16; |
2419 | int d = *s / 16; |
2648 | |
2420 | |
2649 | putcharacter (SCHEME_A_ 'x'); |
2421 | putcharacter (SCHEME_A_ 'x'); |
2650 | |
2422 | |
2651 | if (d < 10) |
2423 | if (d < 10) |
2652 | { |
|
|
2653 | putcharacter (SCHEME_A_ d + '0'); |
2424 | putcharacter (SCHEME_A_ d + '0'); |
2654 | } |
|
|
2655 | else |
2425 | else |
2656 | { |
|
|
2657 | putcharacter (SCHEME_A_ d - 10 + 'A'); |
2426 | putcharacter (SCHEME_A_ d - 10 + 'A'); |
2658 | } |
|
|
2659 | |
2427 | |
2660 | d = *s % 16; |
2428 | d = *s % 16; |
2661 | |
2429 | |
2662 | if (d < 10) |
2430 | if (d < 10) |
2663 | { |
|
|
2664 | putcharacter (SCHEME_A_ d + '0'); |
2431 | putcharacter (SCHEME_A_ d + '0'); |
2665 | } |
|
|
2666 | else |
2432 | else |
2667 | { |
|
|
2668 | putcharacter (SCHEME_A_ d - 10 + 'A'); |
2433 | putcharacter (SCHEME_A_ d - 10 + 'A'); |
2669 | } |
|
|
2670 | } |
2434 | } |
2671 | } |
2435 | } |
2672 | } |
2436 | } |
2673 | else |
2437 | else |
2674 | { |
|
|
2675 | putcharacter (SCHEME_A_ * s); |
2438 | putcharacter (SCHEME_A_ * s); |
2676 | } |
|
|
2677 | |
2439 | |
2678 | s++; |
2440 | s++; |
2679 | } |
2441 | } |
2680 | |
2442 | |
2681 | putcharacter (SCHEME_A_ '"'); |
2443 | putcharacter (SCHEME_A_ '"'); |
… | |
… | |
2716 | |
2478 | |
2717 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2479 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2718 | { |
2480 | { |
2719 | if (num_is_integer (l)) |
2481 | if (num_is_integer (l)) |
2720 | xnum (p, ivalue_unchecked (l)); |
2482 | xnum (p, ivalue_unchecked (l)); |
2721 | #if USE_FLOAT |
2483 | #if USE_REAL |
2722 | else |
2484 | else |
2723 | { |
2485 | { |
2724 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2486 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2725 | /* r5rs says there must be a '.' (unless 'e'?) */ |
2487 | /* r5rs says there must be a '.' (unless 'e'?) */ |
2726 | f = strcspn (p, ".e"); |
2488 | f = strcspn (p, ".e"); |
… | |
… | |
2898 | list_star (SCHEME_P_ pointer d) |
2660 | list_star (SCHEME_P_ pointer d) |
2899 | { |
2661 | { |
2900 | pointer p, q; |
2662 | pointer p, q; |
2901 | |
2663 | |
2902 | if (cdr (d) == NIL) |
2664 | if (cdr (d) == NIL) |
2903 | { |
|
|
2904 | return car (d); |
2665 | return car (d); |
2905 | } |
|
|
2906 | |
2666 | |
2907 | p = cons (car (d), cdr (d)); |
2667 | p = cons (car (d), cdr (d)); |
2908 | q = p; |
2668 | q = p; |
2909 | |
2669 | |
2910 | while (cdr (cdr (p)) != NIL) |
2670 | while (cdr (cdr (p)) != NIL) |
2911 | { |
2671 | { |
2912 | d = cons (car (p), cdr (p)); |
2672 | d = cons (car (p), cdr (p)); |
2913 | |
2673 | |
2914 | if (cdr (cdr (p)) != NIL) |
2674 | if (cdr (cdr (p)) != NIL) |
2915 | { |
|
|
2916 | p = cdr (d); |
2675 | p = cdr (d); |
2917 | } |
|
|
2918 | } |
2676 | } |
2919 | |
2677 | |
2920 | set_cdr (p, car (cdr (p))); |
2678 | set_cdr (p, car (cdr (p))); |
2921 | return q; |
2679 | return q; |
2922 | } |
2680 | } |
… | |
… | |
2936 | |
2694 | |
2937 | /* reverse list --- in-place */ |
2695 | /* reverse list --- in-place */ |
2938 | static pointer |
2696 | static pointer |
2939 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2697 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2940 | { |
2698 | { |
2941 | pointer p = list, result = term, q; |
2699 | pointer result = term; |
|
|
2700 | pointer p = list; |
2942 | |
2701 | |
2943 | while (p != NIL) |
2702 | while (p != NIL) |
2944 | { |
2703 | { |
2945 | q = cdr (p); |
2704 | pointer q = cdr (p); |
2946 | set_cdr (p, result); |
2705 | set_cdr (p, result); |
2947 | result = p; |
2706 | result = p; |
2948 | p = q; |
2707 | p = q; |
2949 | } |
2708 | } |
2950 | |
2709 | |
… | |
… | |
2983 | } |
2742 | } |
2984 | else if (is_number (a)) |
2743 | else if (is_number (a)) |
2985 | { |
2744 | { |
2986 | if (is_number (b)) |
2745 | if (is_number (b)) |
2987 | if (num_is_integer (a) == num_is_integer (b)) |
2746 | if (num_is_integer (a) == num_is_integer (b)) |
2988 | return num_eq (nvalue (a), nvalue (b)); |
2747 | return num_cmp (nvalue (a), nvalue (b)) == 0; |
2989 | |
2748 | |
2990 | return 0; |
2749 | return 0; |
2991 | } |
2750 | } |
2992 | else if (is_character (a)) |
2751 | else if (is_character (a)) |
2993 | { |
2752 | { |
… | |
… | |
3025 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
2784 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
3026 | |
2785 | |
3027 | static int |
2786 | static int |
3028 | hash_fn (const char *key, int table_size) |
2787 | hash_fn (const char *key, int table_size) |
3029 | { |
2788 | { |
3030 | unsigned int hashed = 0; |
2789 | const unsigned char *p = key; |
3031 | const char *c; |
2790 | uint32_t hash = 2166136261; |
3032 | int bits_per_int = sizeof (unsigned int) * 8; |
|
|
3033 | |
2791 | |
3034 | for (c = key; *c; c++) |
2792 | while (*p) |
3035 | { |
2793 | hash = (hash ^ *p++) * 16777619; |
3036 | /* letters have about 5 bits in them */ |
|
|
3037 | hashed = (hashed << 5) | (hashed >> (bits_per_int - 5)); |
|
|
3038 | hashed ^= *c; |
|
|
3039 | } |
|
|
3040 | |
2794 | |
3041 | return hashed % table_size; |
2795 | return hash % table_size; |
3042 | } |
2796 | } |
3043 | #endif |
2797 | #endif |
3044 | |
2798 | |
3045 | #ifndef USE_ALIST_ENV |
2799 | #ifndef USE_ALIST_ENV |
3046 | |
2800 | |
… | |
… | |
3072 | { |
2826 | { |
3073 | pointer slot = immutable_cons (variable, value); |
2827 | pointer slot = immutable_cons (variable, value); |
3074 | |
2828 | |
3075 | if (is_vector (car (env))) |
2829 | if (is_vector (car (env))) |
3076 | { |
2830 | { |
3077 | int location = hash_fn (symname (variable), ivalue_unchecked (car (env))); |
2831 | int location = hash_fn (symname (variable), veclength (car (env))); |
3078 | |
2832 | |
3079 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2833 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
3080 | } |
2834 | } |
3081 | else |
2835 | else |
3082 | set_car (env, immutable_cons (slot, car (env))); |
2836 | set_car (env, immutable_cons (slot, car (env))); |
… | |
… | |
3090 | |
2844 | |
3091 | for (x = env; x != NIL; x = cdr (x)) |
2845 | for (x = env; x != NIL; x = cdr (x)) |
3092 | { |
2846 | { |
3093 | if (is_vector (car (x))) |
2847 | if (is_vector (car (x))) |
3094 | { |
2848 | { |
3095 | location = hash_fn (symname (hdl), ivalue_unchecked (car (x))); |
2849 | location = hash_fn (symname (hdl), veclength (car (x))); |
3096 | y = vector_elem (car (x), location); |
2850 | y = vector_elem (car (x), location); |
3097 | } |
2851 | } |
3098 | else |
2852 | else |
3099 | y = car (x); |
2853 | y = car (x); |
3100 | |
2854 | |
… | |
… | |
3210 | #if USE_ERROR_HOOK |
2964 | #if USE_ERROR_HOOK |
3211 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); |
2965 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); |
3212 | |
2966 | |
3213 | if (x != NIL) |
2967 | if (x != NIL) |
3214 | { |
2968 | { |
3215 | if (a) |
2969 | pointer code = a |
3216 | SCHEME_V->code = cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL); |
2970 | ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) |
3217 | else |
2971 | : NIL; |
3218 | SCHEME_V->code = NIL; |
|
|
3219 | |
2972 | |
3220 | SCHEME_V->code = cons (mk_string (SCHEME_A_ s), SCHEME_V->code); |
2973 | code = cons (mk_string (SCHEME_A_ s), code); |
3221 | setimmutable (car (SCHEME_V->code)); |
2974 | setimmutable (car (code)); |
3222 | SCHEME_V->code = cons (slot_value_in_env (x), SCHEME_V->code); |
2975 | SCHEME_V->code = cons (slot_value_in_env (x), code); |
3223 | SCHEME_V->op = OP_EVAL; |
2976 | SCHEME_V->op = OP_EVAL; |
3224 | |
2977 | |
3225 | return S_T; |
2978 | return S_T; |
3226 | } |
2979 | } |
3227 | #endif |
2980 | #endif |
… | |
… | |
3231 | else |
2984 | else |
3232 | SCHEME_V->args = NIL; |
2985 | SCHEME_V->args = NIL; |
3233 | |
2986 | |
3234 | SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); |
2987 | SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); |
3235 | setimmutable (car (SCHEME_V->args)); |
2988 | setimmutable (car (SCHEME_V->args)); |
3236 | SCHEME_V->op = (int)OP_ERR0; |
2989 | SCHEME_V->op = OP_ERR0; |
3237 | return S_T; |
2990 | return S_T; |
3238 | } |
2991 | } |
3239 | |
2992 | |
3240 | #define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) |
2993 | #define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) |
3241 | #define Error_0(s) Error_1 (s, 0) |
2994 | #define Error_0(s) Error_1 (s, 0) |
3242 | |
2995 | |
3243 | /* Too small to turn into function */ |
2996 | /* Too small to turn into function */ |
3244 | #define BEGIN do { |
2997 | #define BEGIN do { |
3245 | #define END } while (0) |
2998 | #define END } while (0) |
3246 | #define s_goto(a) BEGIN \ |
2999 | #define s_goto(a) BEGIN \ |
3247 | SCHEME_V->op = (int)(a); \ |
3000 | SCHEME_V->op = a; \ |
3248 | return S_T; END |
3001 | return S_T; END |
3249 | |
3002 | |
3250 | #define s_return(a) return xs_return(SCHEME_A_ a) |
3003 | #define s_return(a) return xs_return (SCHEME_A_ a) |
3251 | |
3004 | |
3252 | #ifndef USE_SCHEME_STACK |
3005 | #ifndef USE_SCHEME_STACK |
3253 | |
3006 | |
3254 | /* this structure holds all the interpreter's registers */ |
3007 | /* this structure holds all the interpreter's registers */ |
3255 | struct dump_stack_frame |
3008 | struct dump_stack_frame |
… | |
… | |
3274 | SCHEME_V->dump_size += STACK_GROWTH; |
3027 | 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); |
3028 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3276 | } |
3029 | } |
3277 | |
3030 | |
3278 | next_frame = SCHEME_V->dump_base + nframes; |
3031 | next_frame = SCHEME_V->dump_base + nframes; |
|
|
3032 | |
3279 | next_frame->op = op; |
3033 | next_frame->op = op; |
3280 | next_frame->args = args; |
3034 | next_frame->args = args; |
3281 | next_frame->envir = SCHEME_V->envir; |
3035 | next_frame->envir = SCHEME_V->envir; |
3282 | next_frame->code = code; |
3036 | next_frame->code = code; |
|
|
3037 | |
3283 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3038 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3284 | } |
3039 | } |
3285 | |
3040 | |
3286 | static pointer |
3041 | static pointer |
3287 | xs_return (SCHEME_P_ pointer a) |
3042 | xs_return (SCHEME_P_ pointer a) |
… | |
… | |
3292 | SCHEME_V->value = a; |
3047 | SCHEME_V->value = a; |
3293 | |
3048 | |
3294 | if (nframes <= 0) |
3049 | if (nframes <= 0) |
3295 | return NIL; |
3050 | return NIL; |
3296 | |
3051 | |
3297 | nframes--; |
|
|
3298 | frame = SCHEME_V->dump_base + nframes; |
3052 | frame = &SCHEME_V->dump_base[--nframes]; |
3299 | SCHEME_V->op = frame->op; |
3053 | SCHEME_V->op = frame->op; |
3300 | SCHEME_V->args = frame->args; |
3054 | SCHEME_V->args = frame->args; |
3301 | SCHEME_V->envir = frame->envir; |
3055 | SCHEME_V->envir = frame->envir; |
3302 | SCHEME_V->code = frame->code; |
3056 | SCHEME_V->code = frame->code; |
3303 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3057 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3304 | |
3058 | |
3305 | return S_T; |
3059 | return S_T; |
3306 | } |
3060 | } |
3307 | |
3061 | |
3308 | static INLINE void |
3062 | static INLINE void |
3309 | dump_stack_reset (SCHEME_P) |
3063 | dump_stack_reset (SCHEME_P) |
3310 | { |
3064 | { |
3311 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3065 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3312 | SCHEME_V->dump = (pointer)0; |
3066 | SCHEME_V->dump = (pointer)+0; |
3313 | } |
3067 | } |
3314 | |
3068 | |
3315 | static INLINE void |
3069 | static INLINE void |
3316 | dump_stack_initialize (SCHEME_P) |
3070 | dump_stack_initialize (SCHEME_P) |
3317 | { |
3071 | { |
3318 | SCHEME_V->dump_size = 0; |
3072 | SCHEME_V->dump_size = 0; |
3319 | SCHEME_V->dump_base = NULL; |
3073 | SCHEME_V->dump_base = 0; |
3320 | dump_stack_reset (SCHEME_A); |
3074 | dump_stack_reset (SCHEME_A); |
3321 | } |
3075 | } |
3322 | |
3076 | |
3323 | static void |
3077 | static void |
3324 | dump_stack_free (SCHEME_P) |
3078 | dump_stack_free (SCHEME_P) |
3325 | { |
3079 | { |
3326 | free (SCHEME_V->dump_base); |
3080 | free (SCHEME_V->dump_base); |
3327 | SCHEME_V->dump_base = NULL; |
3081 | SCHEME_V->dump_base = 0; |
3328 | SCHEME_V->dump = (pointer)0; |
3082 | SCHEME_V->dump = (pointer)0; |
3329 | SCHEME_V->dump_size = 0; |
3083 | SCHEME_V->dump_size = 0; |
3330 | } |
3084 | } |
3331 | |
3085 | |
3332 | static void |
3086 | static void |
… | |
… | |
3374 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3128 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3375 | |
3129 | |
3376 | while (cont != NIL) |
3130 | while (cont != NIL) |
3377 | { |
3131 | { |
3378 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3132 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3379 | frame->args = car (cont) ; cont = cdr (cont); |
3133 | frame->args = car (cont) ; cont = cdr (cont); |
3380 | frame->envir = car (cont) ; cont = cdr (cont); |
3134 | frame->envir = car (cont) ; cont = cdr (cont); |
3381 | frame->code = car (cont) ; cont = cdr (cont); |
3135 | frame->code = car (cont) ; cont = cdr (cont); |
3382 | |
3136 | |
3383 | ++frame; |
3137 | ++frame; |
3384 | ++i; |
3138 | ++i; |
3385 | } |
3139 | } |
3386 | |
3140 | |
… | |
… | |
3415 | SCHEME_V->value = a; |
3169 | SCHEME_V->value = a; |
3416 | |
3170 | |
3417 | if (dump == NIL) |
3171 | if (dump == NIL) |
3418 | return NIL; |
3172 | return NIL; |
3419 | |
3173 | |
3420 | SCHEME_V->op = ivalue (car (dump)); |
3174 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3421 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3175 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3422 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3176 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3423 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3177 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3424 | |
3178 | |
3425 | SCHEME_V->dump = dump; |
3179 | SCHEME_V->dump = dump; |
3426 | |
3180 | |
3427 | return S_T; |
3181 | return S_T; |
3428 | } |
3182 | } |
… | |
… | |
3517 | s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); |
3271 | s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); |
3518 | s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); |
3272 | s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); |
3519 | s_goto (OP_READ_INTERNAL); |
3273 | s_goto (OP_READ_INTERNAL); |
3520 | |
3274 | |
3521 | case OP_T1LVL: /* top level */ |
3275 | case OP_T1LVL: /* top level */ |
3522 | SCHEME_V->code = SCHEME_V->value; |
3276 | SCHEME_V->code = SCHEME_V->value; |
3523 | SCHEME_V->inport = SCHEME_V->save_inport; |
3277 | SCHEME_V->inport = SCHEME_V->save_inport; |
3524 | s_goto (OP_EVAL); |
3278 | s_goto (OP_EVAL); |
3525 | |
3279 | |
3526 | case OP_READ_INTERNAL: /* internal read */ |
3280 | case OP_READ_INTERNAL: /* internal read */ |
3527 | SCHEME_V->tok = token (SCHEME_A); |
3281 | SCHEME_V->tok = token (SCHEME_A); |
3528 | |
3282 | |
3529 | if (SCHEME_V->tok == TOK_EOF) |
3283 | if (SCHEME_V->tok == TOK_EOF) |
3530 | { |
|
|
3531 | s_return (S_EOF); |
3284 | s_return (S_EOF); |
3532 | } |
|
|
3533 | |
3285 | |
3534 | s_goto (OP_RDSEXPR); |
3286 | s_goto (OP_RDSEXPR); |
3535 | |
3287 | |
3536 | case OP_GENSYM: |
3288 | case OP_GENSYM: |
3537 | s_return (gensym (SCHEME_A)); |
3289 | s_return (gensym (SCHEME_A)); |
… | |
… | |
3541 | /* OP_VALUEPRINT is always pushed, because when changing from |
3293 | /* OP_VALUEPRINT is always pushed, because when changing from |
3542 | non-interactive to interactive mode, it needs to be |
3294 | non-interactive to interactive mode, it needs to be |
3543 | already on the stack */ |
3295 | already on the stack */ |
3544 | #if USE_TRACING |
3296 | #if USE_TRACING |
3545 | if (SCHEME_V->tracing) |
3297 | if (SCHEME_V->tracing) |
3546 | { |
|
|
3547 | putstr (SCHEME_A_ "\nGives: "); |
3298 | putstr (SCHEME_A_ "\nGives: "); |
3548 | } |
|
|
3549 | #endif |
3299 | #endif |
3550 | |
3300 | |
3551 | if (file_interactive (SCHEME_A)) |
3301 | if (file_interactive (SCHEME_A)) |
3552 | { |
3302 | { |
3553 | SCHEME_V->print_flag = 1; |
3303 | SCHEME_V->print_flag = 1; |
3554 | SCHEME_V->args = SCHEME_V->value; |
3304 | SCHEME_V->args = SCHEME_V->value; |
3555 | s_goto (OP_P0LIST); |
3305 | s_goto (OP_P0LIST); |
3556 | } |
3306 | } |
3557 | else |
3307 | else |
3558 | { |
|
|
3559 | s_return (SCHEME_V->value); |
3308 | s_return (SCHEME_V->value); |
3560 | } |
|
|
3561 | |
3309 | |
3562 | case OP_EVAL: /* main part of evaluation */ |
3310 | case OP_EVAL: /* main part of evaluation */ |
3563 | #if USE_TRACING |
3311 | #if USE_TRACING |
3564 | if (SCHEME_V->tracing) |
3312 | if (SCHEME_V->tracing) |
3565 | { |
3313 | { |
… | |
… | |
3569 | putstr (SCHEME_A_ "\nEval: "); |
3317 | putstr (SCHEME_A_ "\nEval: "); |
3570 | s_goto (OP_P0LIST); |
3318 | s_goto (OP_P0LIST); |
3571 | } |
3319 | } |
3572 | |
3320 | |
3573 | /* fall through */ |
3321 | /* fall through */ |
|
|
3322 | |
3574 | case OP_REAL_EVAL: |
3323 | case OP_REAL_EVAL: |
3575 | #endif |
3324 | #endif |
3576 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3325 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3577 | { |
3326 | { |
3578 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3327 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
… | |
… | |
3582 | else |
3331 | else |
3583 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3332 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3584 | } |
3333 | } |
3585 | else if (is_pair (SCHEME_V->code)) |
3334 | else if (is_pair (SCHEME_V->code)) |
3586 | { |
3335 | { |
|
|
3336 | x = car (SCHEME_V->code); |
|
|
3337 | |
3587 | if (is_syntax (x = car (SCHEME_V->code))) /* SYNTAX */ |
3338 | if (is_syntax (x)) /* SYNTAX */ |
3588 | { |
3339 | { |
3589 | SCHEME_V->code = cdr (SCHEME_V->code); |
3340 | SCHEME_V->code = cdr (SCHEME_V->code); |
3590 | s_goto (syntaxnum (x)); |
3341 | s_goto (syntaxnum (x)); |
3591 | } |
3342 | } |
3592 | else /* first, eval top element and eval arguments */ |
3343 | else /* first, eval top element and eval arguments */ |
3593 | { |
3344 | { |
3594 | s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code); |
3345 | 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)); */ |
3346 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3596 | SCHEME_V->code = car (SCHEME_V->code); |
3347 | SCHEME_V->code = x; |
3597 | s_goto (OP_EVAL); |
3348 | s_goto (OP_EVAL); |
3598 | } |
3349 | } |
3599 | } |
3350 | } |
3600 | else |
3351 | else |
3601 | s_return (SCHEME_V->code); |
3352 | s_return (SCHEME_V->code); |
… | |
… | |
3654 | putstr (SCHEME_A_ "\nApply to: "); |
3405 | putstr (SCHEME_A_ "\nApply to: "); |
3655 | s_goto (OP_P0LIST); |
3406 | s_goto (OP_P0LIST); |
3656 | } |
3407 | } |
3657 | |
3408 | |
3658 | /* fall through */ |
3409 | /* fall through */ |
|
|
3410 | |
3659 | case OP_REAL_APPLY: |
3411 | case OP_REAL_APPLY: |
3660 | #endif |
3412 | #endif |
3661 | if (is_proc (SCHEME_V->code)) |
3413 | if (is_proc (SCHEME_V->code)) |
3662 | { |
3414 | { |
3663 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3415 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
… | |
… | |
3677 | new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); |
3429 | new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); |
3678 | |
3430 | |
3679 | for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) |
3431 | for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) |
3680 | { |
3432 | { |
3681 | if (y == NIL) |
3433 | if (y == NIL) |
3682 | { |
|
|
3683 | Error_0 ("not enough arguments"); |
3434 | Error_0 ("not enough arguments"); |
3684 | } |
|
|
3685 | else |
3435 | else |
3686 | { |
|
|
3687 | new_slot_in_env (SCHEME_A_ car (x), car (y)); |
3436 | new_slot_in_env (SCHEME_A_ car (x), car (y)); |
3688 | } |
|
|
3689 | } |
3437 | } |
3690 | |
3438 | |
3691 | if (x == NIL) |
3439 | if (x == NIL) |
3692 | { |
3440 | { |
3693 | |
|
|
3694 | /*-- |
3441 | /*-- |
3695 | * if (y != NIL) { |
3442 | * if (y != NIL) { |
3696 | * Error_0("too many arguments"); |
3443 | * Error_0("too many arguments"); |
3697 | * } |
3444 | * } |
3698 | */ |
3445 | */ |
3699 | } |
3446 | } |
3700 | else if (is_symbol (x)) |
3447 | else if (is_symbol (x)) |
3701 | new_slot_in_env (SCHEME_A_ x, y); |
3448 | new_slot_in_env (SCHEME_A_ x, y); |
3702 | else |
3449 | else |
3703 | { |
|
|
3704 | Error_1 ("syntax error in closure: not a symbol:", x); |
3450 | Error_1 ("syntax error in closure: not a symbol:", x); |
3705 | } |
|
|
3706 | |
3451 | |
3707 | SCHEME_V->code = cdr (closure_code (SCHEME_V->code)); |
3452 | SCHEME_V->code = cdr (closure_code (SCHEME_V->code)); |
3708 | SCHEME_V->args = NIL; |
3453 | SCHEME_V->args = NIL; |
3709 | s_goto (OP_BEGIN); |
3454 | s_goto (OP_BEGIN); |
3710 | } |
3455 | } |
… | |
… | |
3780 | x = car (SCHEME_V->code); |
3525 | x = car (SCHEME_V->code); |
3781 | SCHEME_V->code = cadr (SCHEME_V->code); |
3526 | SCHEME_V->code = cadr (SCHEME_V->code); |
3782 | } |
3527 | } |
3783 | |
3528 | |
3784 | if (!is_symbol (x)) |
3529 | if (!is_symbol (x)) |
3785 | { |
|
|
3786 | Error_0 ("variable is not a symbol"); |
3530 | Error_0 ("variable is not a symbol"); |
3787 | } |
|
|
3788 | |
3531 | |
3789 | s_save (SCHEME_A_ OP_DEF1, NIL, x); |
3532 | s_save (SCHEME_A_ OP_DEF1, NIL, x); |
3790 | s_goto (OP_EVAL); |
3533 | s_goto (OP_EVAL); |
3791 | |
3534 | |
3792 | case OP_DEF1: /* define */ |
3535 | case OP_DEF1: /* define */ |
3793 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); |
3536 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); |
3794 | |
3537 | |
3795 | if (x != NIL) |
3538 | if (x != NIL) |
3796 | { |
|
|
3797 | set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); |
3539 | set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); |
3798 | } |
|
|
3799 | else |
3540 | else |
3800 | { |
|
|
3801 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3541 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3802 | } |
|
|
3803 | |
3542 | |
3804 | s_return (SCHEME_V->code); |
3543 | s_return (SCHEME_V->code); |
3805 | |
3544 | |
3806 | |
3545 | |
3807 | case OP_DEFP: /* defined? */ |
3546 | case OP_DEFP: /* defined? */ |
3808 | x = SCHEME_V->envir; |
3547 | x = SCHEME_V->envir; |
3809 | |
3548 | |
3810 | if (cdr (SCHEME_V->args) != NIL) |
3549 | if (cdr (SCHEME_V->args) != NIL) |
3811 | { |
|
|
3812 | x = cadr (SCHEME_V->args); |
3550 | x = cadr (SCHEME_V->args); |
3813 | } |
|
|
3814 | |
3551 | |
3815 | s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); |
3552 | s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); |
3816 | |
3553 | |
3817 | case OP_SET0: /* set! */ |
3554 | case OP_SET0: /* set! */ |
3818 | if (is_immutable (car (SCHEME_V->code))) |
3555 | if (is_immutable (car (SCHEME_V->code))) |
… | |
… | |
3829 | { |
3566 | { |
3830 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3567 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3831 | s_return (SCHEME_V->value); |
3568 | s_return (SCHEME_V->value); |
3832 | } |
3569 | } |
3833 | else |
3570 | else |
3834 | { |
|
|
3835 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3571 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3836 | } |
|
|
3837 | |
3572 | |
3838 | |
3573 | |
3839 | case OP_BEGIN: /* begin */ |
3574 | case OP_BEGIN: /* begin */ |
3840 | if (!is_pair (SCHEME_V->code)) |
3575 | if (!is_pair (SCHEME_V->code)) |
3841 | { |
|
|
3842 | s_return (SCHEME_V->code); |
3576 | s_return (SCHEME_V->code); |
3843 | } |
|
|
3844 | |
3577 | |
3845 | if (cdr (SCHEME_V->code) != NIL) |
3578 | if (cdr (SCHEME_V->code) != NIL) |
3846 | { |
|
|
3847 | s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code)); |
3579 | s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code)); |
3848 | } |
|
|
3849 | |
3580 | |
3850 | SCHEME_V->code = car (SCHEME_V->code); |
3581 | SCHEME_V->code = car (SCHEME_V->code); |
3851 | s_goto (OP_EVAL); |
3582 | s_goto (OP_EVAL); |
3852 | |
3583 | |
3853 | case OP_IF0: /* if */ |
3584 | case OP_IF0: /* if */ |
… | |
… | |
3874 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3605 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3875 | |
3606 | |
3876 | if (is_pair (SCHEME_V->code)) /* continue */ |
3607 | if (is_pair (SCHEME_V->code)) /* continue */ |
3877 | { |
3608 | { |
3878 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3609 | 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)); |
3610 | Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); |
3881 | } |
|
|
3882 | |
3611 | |
3883 | s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); |
3612 | s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); |
3884 | SCHEME_V->code = cadar (SCHEME_V->code); |
3613 | SCHEME_V->code = cadar (SCHEME_V->code); |
3885 | SCHEME_V->args = NIL; |
3614 | SCHEME_V->args = NIL; |
3886 | s_goto (OP_EVAL); |
3615 | s_goto (OP_EVAL); |
… | |
… | |
3896 | case OP_LET2: /* let */ |
3625 | case OP_LET2: /* let */ |
3897 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3626 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3898 | |
3627 | |
3899 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; |
3628 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; |
3900 | y != NIL; x = cdr (x), y = cdr (y)) |
3629 | y != NIL; x = cdr (x), y = cdr (y)) |
3901 | { |
|
|
3902 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3630 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3903 | } |
|
|
3904 | |
3631 | |
3905 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3632 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3906 | { |
3633 | { |
3907 | for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) |
3634 | for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) |
3908 | { |
3635 | { |
… | |
… | |
3978 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3705 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3979 | |
3706 | |
3980 | if (is_pair (SCHEME_V->code)) /* continue */ |
3707 | if (is_pair (SCHEME_V->code)) /* continue */ |
3981 | { |
3708 | { |
3982 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3709 | 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)); |
3710 | Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); |
3985 | } |
|
|
3986 | |
3711 | |
3987 | s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); |
3712 | s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); |
3988 | SCHEME_V->code = cadar (SCHEME_V->code); |
3713 | SCHEME_V->code = cadar (SCHEME_V->code); |
3989 | SCHEME_V->args = NIL; |
3714 | SCHEME_V->args = NIL; |
3990 | s_goto (OP_EVAL); |
3715 | s_goto (OP_EVAL); |
… | |
… | |
3997 | s_goto (OP_LET2REC); |
3722 | s_goto (OP_LET2REC); |
3998 | } |
3723 | } |
3999 | |
3724 | |
4000 | case OP_LET2REC: /* letrec */ |
3725 | case OP_LET2REC: /* letrec */ |
4001 | for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) |
3726 | for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) |
4002 | { |
|
|
4003 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3727 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
4004 | } |
|
|
4005 | |
3728 | |
4006 | SCHEME_V->code = cdr (SCHEME_V->code); |
3729 | SCHEME_V->code = cdr (SCHEME_V->code); |
4007 | SCHEME_V->args = NIL; |
3730 | SCHEME_V->args = NIL; |
4008 | s_goto (OP_BEGIN); |
3731 | s_goto (OP_BEGIN); |
4009 | |
3732 | |
4010 | case OP_COND0: /* cond */ |
3733 | case OP_COND0: /* cond */ |
4011 | if (!is_pair (SCHEME_V->code)) |
3734 | if (!is_pair (SCHEME_V->code)) |
4012 | { |
|
|
4013 | Error_0 ("syntax error in cond"); |
3735 | Error_0 ("syntax error in cond"); |
4014 | } |
|
|
4015 | |
3736 | |
4016 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3737 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
4017 | SCHEME_V->code = caar (SCHEME_V->code); |
3738 | SCHEME_V->code = caar (SCHEME_V->code); |
4018 | s_goto (OP_EVAL); |
3739 | s_goto (OP_EVAL); |
4019 | |
3740 | |
4020 | case OP_COND1: /* cond */ |
3741 | case OP_COND1: /* cond */ |
4021 | if (is_true (SCHEME_V->value)) |
3742 | if (is_true (SCHEME_V->value)) |
4022 | { |
3743 | { |
4023 | if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL) |
3744 | if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL) |
4024 | { |
|
|
4025 | s_return (SCHEME_V->value); |
3745 | s_return (SCHEME_V->value); |
4026 | } |
|
|
4027 | |
3746 | |
4028 | if (car (SCHEME_V->code) == SCHEME_V->FEED_TO) |
3747 | if (car (SCHEME_V->code) == SCHEME_V->FEED_TO) |
4029 | { |
3748 | { |
4030 | if (!is_pair (cdr (SCHEME_V->code))) |
3749 | if (!is_pair (cdr (SCHEME_V->code))) |
4031 | { |
|
|
4032 | Error_0 ("syntax error in cond"); |
3750 | Error_0 ("syntax error in cond"); |
4033 | } |
|
|
4034 | |
3751 | |
4035 | x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)); |
3752 | x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)); |
4036 | SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL)); |
3753 | SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL)); |
4037 | s_goto (OP_EVAL); |
3754 | s_goto (OP_EVAL); |
4038 | } |
3755 | } |
… | |
… | |
4040 | s_goto (OP_BEGIN); |
3757 | s_goto (OP_BEGIN); |
4041 | } |
3758 | } |
4042 | else |
3759 | else |
4043 | { |
3760 | { |
4044 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3761 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
4045 | { |
|
|
4046 | s_return (NIL); |
3762 | s_return (NIL); |
4047 | } |
|
|
4048 | else |
3763 | else |
4049 | { |
3764 | { |
4050 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3765 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
4051 | SCHEME_V->code = caar (SCHEME_V->code); |
3766 | SCHEME_V->code = caar (SCHEME_V->code); |
4052 | s_goto (OP_EVAL); |
3767 | s_goto (OP_EVAL); |
… | |
… | |
4058 | set_typeflag (x, T_PROMISE); |
3773 | set_typeflag (x, T_PROMISE); |
4059 | s_return (x); |
3774 | s_return (x); |
4060 | |
3775 | |
4061 | case OP_AND0: /* and */ |
3776 | case OP_AND0: /* and */ |
4062 | if (SCHEME_V->code == NIL) |
3777 | if (SCHEME_V->code == NIL) |
4063 | { |
|
|
4064 | s_return (S_T); |
3778 | s_return (S_T); |
4065 | } |
|
|
4066 | |
3779 | |
4067 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3780 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
4068 | SCHEME_V->code = car (SCHEME_V->code); |
3781 | SCHEME_V->code = car (SCHEME_V->code); |
4069 | s_goto (OP_EVAL); |
3782 | s_goto (OP_EVAL); |
4070 | |
3783 | |
4071 | case OP_AND1: /* and */ |
3784 | case OP_AND1: /* and */ |
4072 | if (is_false (SCHEME_V->value)) |
3785 | if (is_false (SCHEME_V->value)) |
4073 | { |
|
|
4074 | s_return (SCHEME_V->value); |
3786 | s_return (SCHEME_V->value); |
4075 | } |
|
|
4076 | else if (SCHEME_V->code == NIL) |
3787 | else if (SCHEME_V->code == NIL) |
4077 | { |
|
|
4078 | s_return (SCHEME_V->value); |
3788 | s_return (SCHEME_V->value); |
4079 | } |
|
|
4080 | else |
3789 | else |
4081 | { |
3790 | { |
4082 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3791 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
4083 | SCHEME_V->code = car (SCHEME_V->code); |
3792 | SCHEME_V->code = car (SCHEME_V->code); |
4084 | s_goto (OP_EVAL); |
3793 | s_goto (OP_EVAL); |
4085 | } |
3794 | } |
4086 | |
3795 | |
4087 | case OP_OR0: /* or */ |
3796 | case OP_OR0: /* or */ |
4088 | if (SCHEME_V->code == NIL) |
3797 | if (SCHEME_V->code == NIL) |
4089 | { |
|
|
4090 | s_return (S_F); |
3798 | s_return (S_F); |
4091 | } |
|
|
4092 | |
3799 | |
4093 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3800 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
4094 | SCHEME_V->code = car (SCHEME_V->code); |
3801 | SCHEME_V->code = car (SCHEME_V->code); |
4095 | s_goto (OP_EVAL); |
3802 | s_goto (OP_EVAL); |
4096 | |
3803 | |
4097 | case OP_OR1: /* or */ |
3804 | case OP_OR1: /* or */ |
4098 | if (is_true (SCHEME_V->value)) |
3805 | if (is_true (SCHEME_V->value)) |
4099 | { |
|
|
4100 | s_return (SCHEME_V->value); |
3806 | s_return (SCHEME_V->value); |
4101 | } |
|
|
4102 | else if (SCHEME_V->code == NIL) |
3807 | else if (SCHEME_V->code == NIL) |
4103 | { |
|
|
4104 | s_return (SCHEME_V->value); |
3808 | s_return (SCHEME_V->value); |
4105 | } |
|
|
4106 | else |
3809 | else |
4107 | { |
3810 | { |
4108 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3811 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
4109 | SCHEME_V->code = car (SCHEME_V->code); |
3812 | SCHEME_V->code = car (SCHEME_V->code); |
4110 | s_goto (OP_EVAL); |
3813 | s_goto (OP_EVAL); |
… | |
… | |
4132 | x = car (SCHEME_V->code); |
3835 | x = car (SCHEME_V->code); |
4133 | SCHEME_V->code = cadr (SCHEME_V->code); |
3836 | SCHEME_V->code = cadr (SCHEME_V->code); |
4134 | } |
3837 | } |
4135 | |
3838 | |
4136 | if (!is_symbol (x)) |
3839 | if (!is_symbol (x)) |
4137 | { |
|
|
4138 | Error_0 ("variable is not a symbol"); |
3840 | Error_0 ("variable is not a symbol"); |
4139 | } |
|
|
4140 | |
3841 | |
4141 | s_save (SCHEME_A_ OP_MACRO1, NIL, x); |
3842 | s_save (SCHEME_A_ OP_MACRO1, NIL, x); |
4142 | s_goto (OP_EVAL); |
3843 | s_goto (OP_EVAL); |
4143 | |
3844 | |
4144 | case OP_MACRO1: /* macro */ |
3845 | case OP_MACRO1: /* macro */ |
4145 | set_typeflag (SCHEME_V->value, T_MACRO); |
3846 | set_typeflag (SCHEME_V->value, T_MACRO); |
4146 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); |
3847 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); |
4147 | |
3848 | |
4148 | if (x != NIL) |
3849 | if (x != NIL) |
4149 | { |
|
|
4150 | set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); |
3850 | set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); |
4151 | } |
|
|
4152 | else |
3851 | else |
4153 | { |
|
|
4154 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3852 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
4155 | } |
|
|
4156 | |
3853 | |
4157 | s_return (SCHEME_V->code); |
3854 | s_return (SCHEME_V->code); |
4158 | |
3855 | |
4159 | case OP_CASE0: /* case */ |
3856 | case OP_CASE0: /* case */ |
4160 | s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code)); |
3857 | s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code)); |
… | |
… | |
4163 | |
3860 | |
4164 | case OP_CASE1: /* case */ |
3861 | case OP_CASE1: /* case */ |
4165 | for (x = SCHEME_V->code; x != NIL; x = cdr (x)) |
3862 | for (x = SCHEME_V->code; x != NIL; x = cdr (x)) |
4166 | { |
3863 | { |
4167 | if (!is_pair (y = caar (x))) |
3864 | if (!is_pair (y = caar (x))) |
4168 | { |
|
|
4169 | break; |
3865 | break; |
4170 | } |
|
|
4171 | |
3866 | |
4172 | for (; y != NIL; y = cdr (y)) |
3867 | for (; y != NIL; y = cdr (y)) |
4173 | { |
3868 | { |
4174 | if (eqv (car (y), SCHEME_V->value)) |
3869 | if (eqv (car (y), SCHEME_V->value)) |
4175 | { |
|
|
4176 | break; |
3870 | break; |
4177 | } |
|
|
4178 | } |
3871 | } |
4179 | |
3872 | |
4180 | if (y != NIL) |
3873 | if (y != NIL) |
4181 | { |
|
|
4182 | break; |
3874 | break; |
4183 | } |
|
|
4184 | } |
3875 | } |
4185 | |
3876 | |
4186 | if (x != NIL) |
3877 | if (x != NIL) |
4187 | { |
3878 | { |
4188 | if (is_pair (caar (x))) |
3879 | if (is_pair (caar (x))) |
… | |
… | |
4196 | SCHEME_V->code = caar (x); |
3887 | SCHEME_V->code = caar (x); |
4197 | s_goto (OP_EVAL); |
3888 | s_goto (OP_EVAL); |
4198 | } |
3889 | } |
4199 | } |
3890 | } |
4200 | else |
3891 | else |
4201 | { |
|
|
4202 | s_return (NIL); |
3892 | s_return (NIL); |
4203 | } |
|
|
4204 | |
3893 | |
4205 | case OP_CASE2: /* case */ |
3894 | case OP_CASE2: /* case */ |
4206 | if (is_true (SCHEME_V->value)) |
3895 | if (is_true (SCHEME_V->value)) |
4207 | { |
|
|
4208 | s_goto (OP_BEGIN); |
3896 | s_goto (OP_BEGIN); |
4209 | } |
|
|
4210 | else |
3897 | else |
4211 | { |
|
|
4212 | s_return (NIL); |
3898 | s_return (NIL); |
4213 | } |
|
|
4214 | |
3899 | |
4215 | case OP_PAPPLY: /* apply */ |
3900 | case OP_PAPPLY: /* apply */ |
4216 | SCHEME_V->code = car (SCHEME_V->args); |
3901 | SCHEME_V->code = car (SCHEME_V->args); |
4217 | SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); |
3902 | SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); |
4218 | /*SCHEME_V->args = cadr(SCHEME_V->args); */ |
3903 | /*SCHEME_V->args = cadr(SCHEME_V->args); */ |
4219 | s_goto (OP_APPLY); |
3904 | s_goto (OP_APPLY); |
4220 | |
3905 | |
4221 | case OP_PEVAL: /* eval */ |
3906 | case OP_PEVAL: /* eval */ |
4222 | if (cdr (SCHEME_V->args) != NIL) |
3907 | if (cdr (SCHEME_V->args) != NIL) |
4223 | { |
|
|
4224 | SCHEME_V->envir = cadr (SCHEME_V->args); |
3908 | SCHEME_V->envir = cadr (SCHEME_V->args); |
4225 | } |
|
|
4226 | |
3909 | |
4227 | SCHEME_V->code = car (SCHEME_V->args); |
3910 | SCHEME_V->code = car (SCHEME_V->args); |
4228 | s_goto (OP_EVAL); |
3911 | s_goto (OP_EVAL); |
4229 | |
3912 | |
4230 | case OP_CONTINUATION: /* call-with-current-continuation */ |
3913 | case OP_CONTINUATION: /* call-with-current-continuation */ |
4231 | SCHEME_V->code = car (SCHEME_V->args); |
3914 | SCHEME_V->code = car (SCHEME_V->args); |
4232 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_V)), NIL); |
3915 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
4233 | s_goto (OP_APPLY); |
3916 | s_goto (OP_APPLY); |
4234 | } |
3917 | } |
4235 | |
3918 | |
4236 | return S_T; |
3919 | abort (); |
4237 | } |
3920 | } |
4238 | |
3921 | |
4239 | static pointer |
3922 | static pointer |
4240 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
3923 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4241 | { |
3924 | { |
… | |
… | |
4252 | |
3935 | |
4253 | case OP_INEX2EX: /* inexact->exact */ |
3936 | case OP_INEX2EX: /* inexact->exact */ |
4254 | x = car (SCHEME_V->args); |
3937 | x = car (SCHEME_V->args); |
4255 | |
3938 | |
4256 | if (num_is_integer (x)) |
3939 | if (num_is_integer (x)) |
4257 | { |
|
|
4258 | s_return (x); |
3940 | s_return (x); |
4259 | } |
|
|
4260 | else if (modf (rvalue_unchecked (x), &dd) == 0.0) |
3941 | else if (modf (rvalue_unchecked (x), &dd) == 0.0) |
4261 | { |
|
|
4262 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
3942 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4263 | } |
|
|
4264 | else |
3943 | else |
4265 | { |
|
|
4266 | Error_1 ("inexact->exact: not integral:", x); |
3944 | Error_1 ("inexact->exact: not integral:", x); |
4267 | } |
|
|
4268 | |
3945 | |
4269 | case OP_EXP: |
3946 | case OP_EXP: |
4270 | x = car (SCHEME_V->args); |
3947 | x = car (SCHEME_V->args); |
4271 | s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
3948 | s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4272 | |
3949 | |
… | |
… | |
4296 | |
3973 | |
4297 | case OP_ATAN: |
3974 | case OP_ATAN: |
4298 | x = car (SCHEME_V->args); |
3975 | x = car (SCHEME_V->args); |
4299 | |
3976 | |
4300 | if (cdr (SCHEME_V->args) == NIL) |
3977 | if (cdr (SCHEME_V->args) == NIL) |
4301 | { |
|
|
4302 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
3978 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4303 | } |
|
|
4304 | else |
3979 | else |
4305 | { |
3980 | { |
4306 | pointer y = cadr (SCHEME_V->args); |
3981 | pointer y = cadr (SCHEME_V->args); |
4307 | |
3982 | |
4308 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
3983 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
… | |
… | |
4324 | real_result = 0; |
3999 | real_result = 0; |
4325 | |
4000 | |
4326 | /* This 'if' is an R5RS compatibility fix. */ |
4001 | /* This 'if' is an R5RS compatibility fix. */ |
4327 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4002 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4328 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4003 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4329 | { |
|
|
4330 | result = 0.0; |
4004 | result = 0.0; |
4331 | } |
|
|
4332 | else |
4005 | else |
4333 | { |
|
|
4334 | result = pow (rvalue (x), rvalue (y)); |
4006 | result = pow (rvalue (x), rvalue (y)); |
4335 | } |
|
|
4336 | |
4007 | |
4337 | /* Before returning integer result make sure we can. */ |
4008 | /* Before returning integer result make sure we can. */ |
4338 | /* If the test fails, result is too big for integer. */ |
4009 | /* If the test fails, result is too big for integer. */ |
4339 | if (!real_result) |
4010 | if (!real_result) |
4340 | { |
4011 | { |
… | |
… | |
4343 | if (result != (RVALUE) result_as_long) |
4014 | if (result != (RVALUE) result_as_long) |
4344 | real_result = 1; |
4015 | real_result = 1; |
4345 | } |
4016 | } |
4346 | |
4017 | |
4347 | if (real_result) |
4018 | if (real_result) |
4348 | { |
|
|
4349 | s_return (mk_real (SCHEME_A_ result)); |
4019 | s_return (mk_real (SCHEME_A_ result)); |
4350 | } |
|
|
4351 | else |
4020 | else |
4352 | { |
|
|
4353 | s_return (mk_integer (SCHEME_A_ result)); |
4021 | s_return (mk_integer (SCHEME_A_ result)); |
4354 | } |
|
|
4355 | } |
4022 | } |
4356 | |
4023 | |
4357 | case OP_FLOOR: |
4024 | case OP_FLOOR: |
4358 | x = car (SCHEME_V->args); |
4025 | x = car (SCHEME_V->args); |
4359 | s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
4026 | s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
… | |
… | |
4368 | |
4035 | |
4369 | x = car (SCHEME_V->args); |
4036 | x = car (SCHEME_V->args); |
4370 | rvalue_of_x = rvalue (x); |
4037 | rvalue_of_x = rvalue (x); |
4371 | |
4038 | |
4372 | if (rvalue_of_x > 0) |
4039 | if (rvalue_of_x > 0) |
4373 | { |
|
|
4374 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
4040 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
4375 | } |
|
|
4376 | else |
4041 | else |
4377 | { |
|
|
4378 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
4042 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
4379 | } |
|
|
4380 | } |
4043 | } |
4381 | |
4044 | |
4382 | case OP_ROUND: |
4045 | case OP_ROUND: |
4383 | x = car (SCHEME_V->args); |
4046 | x = car (SCHEME_V->args); |
4384 | |
4047 | |
… | |
… | |
4390 | |
4053 | |
4391 | case OP_ADD: /* + */ |
4054 | case OP_ADD: /* + */ |
4392 | v = num_zero; |
4055 | v = num_zero; |
4393 | |
4056 | |
4394 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4057 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4395 | v = num_add (v, nvalue (car (x))); |
4058 | v = num_op ('+', v, nvalue (car (x))); |
4396 | |
4059 | |
4397 | s_return (mk_number (SCHEME_A_ v)); |
4060 | s_return (mk_number (SCHEME_A_ v)); |
4398 | |
4061 | |
4399 | case OP_MUL: /* * */ |
4062 | case OP_MUL: /* * */ |
4400 | v = num_one; |
4063 | v = num_one; |
4401 | |
4064 | |
4402 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4065 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4403 | v = num_mul (v, nvalue (car (x))); |
4066 | v = num_op ('+', v, nvalue (car (x))); |
4404 | |
4067 | |
4405 | s_return (mk_number (SCHEME_A_ v)); |
4068 | s_return (mk_number (SCHEME_A_ v)); |
4406 | |
4069 | |
4407 | case OP_SUB: /* - */ |
4070 | case OP_SUB: /* - */ |
4408 | if (cdr (SCHEME_V->args) == NIL) |
4071 | if (cdr (SCHEME_V->args) == NIL) |
… | |
… | |
4415 | x = cdr (SCHEME_V->args); |
4078 | x = cdr (SCHEME_V->args); |
4416 | v = nvalue (car (SCHEME_V->args)); |
4079 | v = nvalue (car (SCHEME_V->args)); |
4417 | } |
4080 | } |
4418 | |
4081 | |
4419 | for (; x != NIL; x = cdr (x)) |
4082 | for (; x != NIL; x = cdr (x)) |
4420 | v = num_sub (v, nvalue (car (x))); |
4083 | v = num_op ('+', v, nvalue (car (x))); |
4421 | |
4084 | |
4422 | s_return (mk_number (SCHEME_A_ v)); |
4085 | s_return (mk_number (SCHEME_A_ v)); |
4423 | |
4086 | |
4424 | case OP_DIV: /* / */ |
4087 | case OP_DIV: /* / */ |
4425 | if (cdr (SCHEME_V->args) == NIL) |
4088 | if (cdr (SCHEME_V->args) == NIL) |
… | |
… | |
4456 | } |
4119 | } |
4457 | |
4120 | |
4458 | for (; x != NIL; x = cdr (x)) |
4121 | for (; x != NIL; x = cdr (x)) |
4459 | { |
4122 | { |
4460 | if (ivalue (car (x)) != 0) |
4123 | if (ivalue (car (x)) != 0) |
4461 | v = num_intdiv (v, nvalue (car (x))); |
4124 | v = num_op ('/', v, nvalue (car (x))); |
4462 | else |
4125 | else |
4463 | Error_0 ("quotient: division by zero"); |
4126 | Error_0 ("quotient: division by zero"); |
4464 | } |
4127 | } |
4465 | |
4128 | |
4466 | s_return (mk_number (SCHEME_A_ v)); |
4129 | s_return (mk_number (SCHEME_A_ v)); |
… | |
… | |
4512 | } |
4175 | } |
4513 | else |
4176 | else |
4514 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4177 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4515 | |
4178 | |
4516 | case OP_CHAR2INT: /* char->integer */ |
4179 | case OP_CHAR2INT: /* char->integer */ |
4517 | { |
4180 | s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4518 | char c; |
|
|
4519 | |
|
|
4520 | c = (char) ivalue (car (SCHEME_V->args)); |
|
|
4521 | s_return (mk_integer (SCHEME_A_ (unsigned char) c)); |
|
|
4522 | } |
|
|
4523 | |
4181 | |
4524 | case OP_INT2CHAR: /* integer->char */ |
4182 | 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)); |
4183 | s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4530 | } |
|
|
4531 | |
4184 | |
4532 | case OP_CHARUPCASE: |
4185 | case OP_CHARUPCASE: |
4533 | { |
4186 | { |
4534 | unsigned char c; |
|
|
4535 | |
|
|
4536 | c = (unsigned char) ivalue (car (SCHEME_V->args)); |
4187 | unsigned char c = ivalue (car (SCHEME_V->args)); |
4537 | c = toupper (c); |
4188 | c = toupper (c); |
4538 | s_return (mk_character (SCHEME_A_ (char) c)); |
4189 | s_return (mk_character (SCHEME_A_ c)); |
4539 | } |
4190 | } |
4540 | |
4191 | |
4541 | case OP_CHARDNCASE: |
4192 | case OP_CHARDNCASE: |
4542 | { |
4193 | { |
4543 | unsigned char c; |
|
|
4544 | |
|
|
4545 | c = (unsigned char) ivalue (car (SCHEME_V->args)); |
4194 | unsigned char c = ivalue (car (SCHEME_V->args)); |
4546 | c = tolower (c); |
4195 | c = tolower (c); |
4547 | s_return (mk_character (SCHEME_A_ (char) c)); |
4196 | s_return (mk_character (SCHEME_A_ c)); |
4548 | } |
4197 | } |
4549 | |
4198 | |
4550 | case OP_STR2SYM: /* string->symbol */ |
4199 | case OP_STR2SYM: /* string->symbol */ |
4551 | s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); |
4200 | s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); |
4552 | |
4201 | |
… | |
… | |
4564 | if (pf == 16 || pf == 10 || pf == 8 || pf == 2) |
4213 | if (pf == 16 || pf == 10 || pf == 8 || pf == 2) |
4565 | { |
4214 | { |
4566 | /* base is OK */ |
4215 | /* base is OK */ |
4567 | } |
4216 | } |
4568 | else |
4217 | else |
4569 | { |
|
|
4570 | pf = -1; |
4218 | pf = -1; |
4571 | } |
|
|
4572 | } |
4219 | } |
4573 | |
4220 | |
4574 | if (pf < 0) |
4221 | if (pf < 0) |
4575 | { |
|
|
4576 | Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); |
4222 | Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); |
4577 | } |
|
|
4578 | else if (*s == '#') /* no use of base! */ |
4223 | else if (*s == '#') /* no use of base! */ |
4579 | { |
|
|
4580 | s_return (mk_sharp_const (SCHEME_A_ s + 1)); |
4224 | s_return (mk_sharp_const (SCHEME_A_ s + 1)); |
4581 | } |
|
|
4582 | else |
4225 | else |
4583 | { |
4226 | { |
4584 | if (pf == 0 || pf == 10) |
4227 | if (pf == 0 || pf == 10) |
4585 | { |
|
|
4586 | s_return (mk_atom (SCHEME_A_ s)); |
4228 | s_return (mk_atom (SCHEME_A_ s)); |
4587 | } |
|
|
4588 | else |
4229 | else |
4589 | { |
4230 | { |
4590 | char *ep; |
4231 | char *ep; |
4591 | long iv = strtol (s, &ep, (int) pf); |
4232 | long iv = strtol (s, &ep, (int) pf); |
4592 | |
4233 | |
4593 | if (*ep == 0) |
4234 | if (*ep == 0) |
4594 | { |
|
|
4595 | s_return (mk_integer (SCHEME_A_ iv)); |
4235 | s_return (mk_integer (SCHEME_A_ iv)); |
4596 | } |
|
|
4597 | else |
4236 | else |
4598 | { |
|
|
4599 | s_return (S_F); |
4237 | s_return (S_F); |
4600 | } |
|
|
4601 | } |
4238 | } |
4602 | } |
4239 | } |
4603 | } |
4240 | } |
4604 | |
4241 | |
4605 | case OP_SYM2STR: /* symbol->string */ |
4242 | case OP_SYM2STR: /* symbol->string */ |
… | |
… | |
4622 | if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) |
4259 | if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) |
4623 | { |
4260 | { |
4624 | /* base is OK */ |
4261 | /* base is OK */ |
4625 | } |
4262 | } |
4626 | else |
4263 | else |
4627 | { |
|
|
4628 | pf = -1; |
4264 | pf = -1; |
4629 | } |
|
|
4630 | } |
4265 | } |
4631 | |
4266 | |
4632 | if (pf < 0) |
4267 | if (pf < 0) |
4633 | { |
|
|
4634 | Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); |
4268 | Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); |
4635 | } |
|
|
4636 | else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) |
4269 | else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) |
4637 | { |
4270 | { |
4638 | char *p; |
4271 | char *p; |
4639 | int len; |
4272 | int len; |
4640 | |
4273 | |
4641 | atom2str (SCHEME_A_ x, (int) pf, &p, &len); |
4274 | atom2str (SCHEME_A_ x, pf, &p, &len); |
4642 | s_return (mk_counted_string (SCHEME_A_ p, len)); |
4275 | s_return (mk_counted_string (SCHEME_A_ p, len)); |
4643 | } |
4276 | } |
4644 | else |
4277 | else |
4645 | { |
|
|
4646 | Error_1 ("atom->string: not an atom:", x); |
4278 | Error_1 ("atom->string: not an atom:", x); |
4647 | } |
|
|
4648 | } |
4279 | } |
4649 | |
4280 | |
4650 | case OP_MKSTRING: /* make-string */ |
4281 | case OP_MKSTRING: /* make-string */ |
4651 | { |
4282 | { |
4652 | int fill = ' '; |
4283 | int fill = ' '; |
4653 | int len; |
4284 | int len; |
4654 | |
4285 | |
4655 | len = ivalue (car (SCHEME_V->args)); |
4286 | len = ivalue (car (SCHEME_V->args)); |
4656 | |
4287 | |
4657 | if (cdr (SCHEME_V->args) != NIL) |
4288 | if (cdr (SCHEME_V->args) != NIL) |
4658 | { |
|
|
4659 | fill = charvalue (cadr (SCHEME_V->args)); |
4289 | fill = charvalue (cadr (SCHEME_V->args)); |
4660 | } |
|
|
4661 | |
4290 | |
4662 | s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); |
4291 | s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); |
4663 | } |
4292 | } |
4664 | |
4293 | |
4665 | case OP_STRLEN: /* string-length */ |
4294 | case OP_STRLEN: /* string-length */ |
… | |
… | |
4673 | str = strvalue (car (SCHEME_V->args)); |
4302 | str = strvalue (car (SCHEME_V->args)); |
4674 | |
4303 | |
4675 | index = ivalue (cadr (SCHEME_V->args)); |
4304 | index = ivalue (cadr (SCHEME_V->args)); |
4676 | |
4305 | |
4677 | if (index >= strlength (car (SCHEME_V->args))) |
4306 | if (index >= strlength (car (SCHEME_V->args))) |
4678 | { |
|
|
4679 | Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); |
4307 | Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); |
4680 | } |
|
|
4681 | |
4308 | |
4682 | s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); |
4309 | s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); |
4683 | } |
4310 | } |
4684 | |
4311 | |
4685 | case OP_STRSET: /* string-set! */ |
4312 | case OP_STRSET: /* string-set! */ |
… | |
… | |
4687 | char *str; |
4314 | char *str; |
4688 | int index; |
4315 | int index; |
4689 | int c; |
4316 | int c; |
4690 | |
4317 | |
4691 | if (is_immutable (car (SCHEME_V->args))) |
4318 | if (is_immutable (car (SCHEME_V->args))) |
4692 | { |
|
|
4693 | Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); |
4319 | Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); |
4694 | } |
|
|
4695 | |
4320 | |
4696 | str = strvalue (car (SCHEME_V->args)); |
4321 | str = strvalue (car (SCHEME_V->args)); |
4697 | |
4322 | |
4698 | index = ivalue (cadr (SCHEME_V->args)); |
4323 | index = ivalue (cadr (SCHEME_V->args)); |
4699 | |
4324 | |
4700 | if (index >= strlength (car (SCHEME_V->args))) |
4325 | if (index >= strlength (car (SCHEME_V->args))) |
4701 | { |
|
|
4702 | Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); |
4326 | Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); |
4703 | } |
|
|
4704 | |
4327 | |
4705 | c = charvalue (caddr (SCHEME_V->args)); |
4328 | c = charvalue (caddr (SCHEME_V->args)); |
4706 | |
4329 | |
4707 | str[index] = (char) c; |
4330 | str[index] = (char) c; |
4708 | s_return (car (SCHEME_V->args)); |
4331 | s_return (car (SCHEME_V->args)); |
… | |
… | |
4715 | pointer newstr; |
4338 | pointer newstr; |
4716 | char *pos; |
4339 | char *pos; |
4717 | |
4340 | |
4718 | /* compute needed length for new string */ |
4341 | /* compute needed length for new string */ |
4719 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4342 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4720 | { |
|
|
4721 | len += strlength (car (x)); |
4343 | len += strlength (car (x)); |
4722 | } |
|
|
4723 | |
4344 | |
4724 | newstr = mk_empty_string (SCHEME_A_ len, ' '); |
4345 | newstr = mk_empty_string (SCHEME_A_ len, ' '); |
4725 | |
4346 | |
4726 | /* store the contents of the argument strings into the new string */ |
4347 | /* 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)) |
4348 | for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) |
4728 | { |
|
|
4729 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4349 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4730 | } |
|
|
4731 | |
4350 | |
4732 | s_return (newstr); |
4351 | s_return (newstr); |
4733 | } |
4352 | } |
4734 | |
4353 | |
4735 | case OP_SUBSTR: /* substring */ |
4354 | case OP_SUBSTR: /* substring */ |
… | |
… | |
4742 | str = strvalue (car (SCHEME_V->args)); |
4361 | str = strvalue (car (SCHEME_V->args)); |
4743 | |
4362 | |
4744 | index0 = ivalue (cadr (SCHEME_V->args)); |
4363 | index0 = ivalue (cadr (SCHEME_V->args)); |
4745 | |
4364 | |
4746 | if (index0 > strlength (car (SCHEME_V->args))) |
4365 | if (index0 > strlength (car (SCHEME_V->args))) |
4747 | { |
|
|
4748 | Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); |
4366 | Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); |
4749 | } |
|
|
4750 | |
4367 | |
4751 | if (cddr (SCHEME_V->args) != NIL) |
4368 | if (cddr (SCHEME_V->args) != NIL) |
4752 | { |
4369 | { |
4753 | index1 = ivalue (caddr (SCHEME_V->args)); |
4370 | index1 = ivalue (caddr (SCHEME_V->args)); |
4754 | |
4371 | |
4755 | if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) |
4372 | if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) |
4756 | { |
|
|
4757 | Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); |
4373 | Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); |
4758 | } |
|
|
4759 | } |
4374 | } |
4760 | else |
4375 | else |
4761 | { |
|
|
4762 | index1 = strlength (car (SCHEME_V->args)); |
4376 | index1 = strlength (car (SCHEME_V->args)); |
4763 | } |
|
|
4764 | |
4377 | |
4765 | len = index1 - index0; |
4378 | len = index1 - index0; |
4766 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4379 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4767 | memcpy (strvalue (x), str + index0, len); |
4380 | memcpy (strvalue (x), str + index0, len); |
4768 | strvalue (x)[len] = 0; |
4381 | strvalue (x)[len] = 0; |
… | |
… | |
4775 | int i; |
4388 | int i; |
4776 | pointer vec; |
4389 | pointer vec; |
4777 | int len = list_length (SCHEME_A_ SCHEME_V->args); |
4390 | int len = list_length (SCHEME_A_ SCHEME_V->args); |
4778 | |
4391 | |
4779 | if (len < 0) |
4392 | if (len < 0) |
4780 | { |
|
|
4781 | Error_1 ("vector: not a proper list:", SCHEME_V->args); |
4393 | Error_1 ("vector: not a proper list:", SCHEME_V->args); |
4782 | } |
|
|
4783 | |
4394 | |
4784 | vec = mk_vector (SCHEME_A_ len); |
4395 | vec = mk_vector (SCHEME_A_ len); |
4785 | |
4396 | |
4786 | #if USE_ERROR_CHECKING |
4397 | #if USE_ERROR_CHECKING |
4787 | if (SCHEME_V->no_memory) |
4398 | if (SCHEME_V->no_memory) |
… | |
… | |
4817 | |
4428 | |
4818 | s_return (vec); |
4429 | s_return (vec); |
4819 | } |
4430 | } |
4820 | |
4431 | |
4821 | case OP_VECLEN: /* vector-length */ |
4432 | case OP_VECLEN: /* vector-length */ |
4822 | s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4433 | s_return (mk_integer (SCHEME_A_ veclength (car (SCHEME_V->args)))); |
4823 | |
4434 | |
4824 | case OP_VECREF: /* vector-ref */ |
4435 | case OP_VECREF: /* vector-ref */ |
4825 | { |
4436 | { |
4826 | int index; |
4437 | int index; |
4827 | |
4438 | |
4828 | index = ivalue (cadr (SCHEME_V->args)); |
4439 | index = ivalue (cadr (SCHEME_V->args)); |
4829 | |
4440 | |
4830 | if (index >= ivalue (car (SCHEME_V->args))) |
4441 | if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) |
4831 | { |
|
|
4832 | Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); |
4442 | Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); |
4833 | } |
|
|
4834 | |
4443 | |
4835 | s_return (vector_elem (car (SCHEME_V->args), index)); |
4444 | s_return (vector_elem (car (SCHEME_V->args), index)); |
4836 | } |
4445 | } |
4837 | |
4446 | |
4838 | case OP_VECSET: /* vector-set! */ |
4447 | case OP_VECSET: /* vector-set! */ |
4839 | { |
4448 | { |
4840 | int index; |
4449 | int index; |
4841 | |
4450 | |
4842 | if (is_immutable (car (SCHEME_V->args))) |
4451 | if (is_immutable (car (SCHEME_V->args))) |
4843 | { |
|
|
4844 | Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); |
4452 | Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); |
4845 | } |
|
|
4846 | |
4453 | |
4847 | index = ivalue (cadr (SCHEME_V->args)); |
4454 | index = ivalue (cadr (SCHEME_V->args)); |
4848 | |
4455 | |
4849 | if (index >= ivalue (car (SCHEME_V->args))) |
4456 | if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) |
4850 | { |
|
|
4851 | Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); |
4457 | Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); |
4852 | } |
|
|
4853 | |
4458 | |
4854 | set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); |
4459 | set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); |
4855 | s_return (car (SCHEME_V->args)); |
4460 | s_return (car (SCHEME_V->args)); |
4856 | } |
4461 | } |
4857 | } |
4462 | } |
… | |
… | |
4912 | } |
4517 | } |
4913 | } |
4518 | } |
4914 | } |
4519 | } |
4915 | |
4520 | |
4916 | static pointer |
4521 | static pointer |
|
|
4522 | opexe_r (SCHEME_P_ enum scheme_opcodes op) |
|
|
4523 | { |
|
|
4524 | pointer x = SCHEME_V->args; |
|
|
4525 | |
|
|
4526 | for (;;) |
|
|
4527 | { |
|
|
4528 | num v = nvalue (car (x)); |
|
|
4529 | x = cdr (x); |
|
|
4530 | |
|
|
4531 | if (x == NIL) |
|
|
4532 | break; |
|
|
4533 | |
|
|
4534 | int r = num_cmp (v, nvalue (car (x))); |
|
|
4535 | |
|
|
4536 | switch (op) |
|
|
4537 | { |
|
|
4538 | case OP_NUMEQ: r = r == 0; break; |
|
|
4539 | case OP_LESS: r = r < 0; break; |
|
|
4540 | case OP_GRE: r = r > 0; break; |
|
|
4541 | case OP_LEQ: r = r <= 0; break; |
|
|
4542 | case OP_GEQ: r = r >= 0; break; |
|
|
4543 | } |
|
|
4544 | |
|
|
4545 | if (!r) |
|
|
4546 | s_return (S_F); |
|
|
4547 | } |
|
|
4548 | |
|
|
4549 | s_return (S_T); |
|
|
4550 | } |
|
|
4551 | |
|
|
4552 | static pointer |
4917 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4553 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4918 | { |
4554 | { |
4919 | pointer x; |
4555 | pointer x = SCHEME_V->args; |
4920 | num v; |
4556 | pointer a = car (x); |
4921 | int (*comp_func) (num, num); |
4557 | pointer d = cdr (x); |
|
|
4558 | int r; |
4922 | |
4559 | |
4923 | switch (op) |
4560 | switch (op) |
4924 | { |
4561 | { |
4925 | case OP_NOT: /* not */ |
4562 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4926 | s_retbool (is_false (car (SCHEME_V->args))); |
4563 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
|
|
4564 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
|
|
4565 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
|
|
4566 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4567 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
|
|
4568 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
|
|
4569 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
|
|
4570 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
|
|
4571 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4927 | |
4572 | |
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 |
4573 | #if USE_CHAR_CLASSIFIERS |
4999 | |
|
|
5000 | case OP_CHARAP: /* char-alphabetic? */ |
4574 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; |
5001 | s_retbool (Cisalpha (ivalue (car (SCHEME_V->args)))); |
4575 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; |
5002 | |
|
|
5003 | case OP_CHARNP: /* char-numeric? */ |
|
|
5004 | s_retbool (Cisdigit (ivalue (car (SCHEME_V->args)))); |
|
|
5005 | |
|
|
5006 | case OP_CHARWP: /* char-whitespace? */ |
4576 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; |
5007 | s_retbool (Cisspace (ivalue (car (SCHEME_V->args)))); |
|
|
5008 | |
|
|
5009 | case OP_CHARUP: /* char-upper-case? */ |
4577 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; |
5010 | s_retbool (Cisupper (ivalue (car (SCHEME_V->args)))); |
|
|
5011 | |
|
|
5012 | case OP_CHARLP: /* char-lower-case? */ |
4578 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; |
5013 | s_retbool (Cislower (ivalue (car (SCHEME_V->args)))); |
|
|
5014 | #endif |
4579 | #endif |
|
|
4580 | |
5015 | #if USE_PORTS |
4581 | #if USE_PORTS |
5016 | |
4582 | 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? */ |
4583 | 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? */ |
4584 | case OP_OUTPORTP: /* output-port? */ r = is_outport (a); break; |
5024 | s_retbool (is_outport (car (SCHEME_V->args))); |
|
|
5025 | #endif |
4585 | #endif |
5026 | |
4586 | |
5027 | case OP_PROCP: /* procedure? */ |
4587 | case OP_PROCP: /* procedure? */ |
5028 | |
4588 | |
5029 | /*-- |
4589 | /*-- |
5030 | * continuation should be procedure by the example |
4590 | * continuation should be procedure by the example |
5031 | * (call-with-current-continuation procedure?) ==> #t |
4591 | * (call-with-current-continuation procedure?) ==> #t |
5032 | * in R^3 report sec. 6.9 |
4592 | * in R^3 report sec. 6.9 |
5033 | */ |
4593 | */ |
5034 | s_retbool (is_proc (car (SCHEME_V->args)) || is_closure (car (SCHEME_V->args)) |
4594 | 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))); |
4595 | break; |
5036 | |
4596 | |
5037 | case OP_PAIRP: /* pair? */ |
4597 | case OP_PAIRP: /* pair? */ r = is_pair (a) ; break; |
5038 | s_retbool (is_pair (car (SCHEME_V->args))); |
4598 | case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break; |
5039 | |
4599 | case OP_ENVP: /* environment? */ r = is_environment (a) ; break; |
5040 | case OP_LISTP: /* list? */ |
4600 | case OP_VECTORP: /* vector? */ r = is_vector (a) ; break; |
5041 | s_retbool (list_length (SCHEME_A_ car (SCHEME_V->args)) >= 0); |
4601 | case OP_EQ: /* eq? */ r = a == cadr (x) ; break; |
5042 | |
4602 | case OP_EQV: /* eqv? */ r = eqv (a, cadr (x)) ; 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 | } |
4603 | } |
5055 | |
4604 | |
5056 | return S_T; |
4605 | s_retbool (r); |
5057 | } |
4606 | } |
5058 | |
4607 | |
5059 | static pointer |
4608 | static pointer |
5060 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4609 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
5061 | { |
4610 | { |
… | |
… | |
5072 | s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); |
4621 | s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); |
5073 | SCHEME_V->args = NIL; |
4622 | SCHEME_V->args = NIL; |
5074 | s_goto (OP_APPLY); |
4623 | s_goto (OP_APPLY); |
5075 | } |
4624 | } |
5076 | else |
4625 | else |
5077 | { |
|
|
5078 | s_return (SCHEME_V->code); |
4626 | s_return (SCHEME_V->code); |
5079 | } |
|
|
5080 | |
4627 | |
5081 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4628 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
5082 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4629 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
5083 | s_return (SCHEME_V->value); |
4630 | s_return (SCHEME_V->value); |
5084 | |
4631 | |
… | |
… | |
5148 | else |
4695 | else |
5149 | { |
4696 | { |
5150 | putstr (SCHEME_A_ "\n"); |
4697 | putstr (SCHEME_A_ "\n"); |
5151 | |
4698 | |
5152 | if (SCHEME_V->interactive_repl) |
4699 | if (SCHEME_V->interactive_repl) |
5153 | { |
|
|
5154 | s_goto (OP_T0LVL); |
4700 | s_goto (OP_T0LVL); |
5155 | } |
|
|
5156 | else |
4701 | else |
5157 | { |
|
|
5158 | return NIL; |
4702 | return NIL; |
5159 | } |
|
|
5160 | } |
4703 | } |
5161 | |
4704 | |
5162 | case OP_REVERSE: /* reverse */ |
4705 | case OP_REVERSE: /* reverse */ |
5163 | s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); |
4706 | s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); |
5164 | |
4707 | |
… | |
… | |
5187 | |
4730 | |
5188 | #if USE_PLIST |
4731 | #if USE_PLIST |
5189 | |
4732 | |
5190 | case OP_PUT: /* put */ |
4733 | case OP_PUT: /* put */ |
5191 | if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) |
4734 | if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) |
5192 | { |
|
|
5193 | Error_0 ("illegal use of put"); |
4735 | Error_0 ("illegal use of put"); |
5194 | } |
|
|
5195 | |
4736 | |
5196 | for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) |
4737 | for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) |
5197 | { |
4738 | { |
5198 | if (caar (x) == y) |
4739 | if (caar (x) == y) |
5199 | { |
|
|
5200 | break; |
4740 | break; |
5201 | } |
|
|
5202 | } |
4741 | } |
5203 | |
4742 | |
5204 | if (x != NIL) |
4743 | if (x != NIL) |
5205 | cdar (x) = caddr (SCHEME_V->args); |
4744 | cdar (x) = caddr (SCHEME_V->args); |
5206 | else |
4745 | else |
… | |
… | |
5283 | } |
4822 | } |
5284 | |
4823 | |
5285 | p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); |
4824 | p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); |
5286 | |
4825 | |
5287 | if (p == NIL) |
4826 | if (p == NIL) |
5288 | { |
|
|
5289 | s_return (S_F); |
4827 | s_return (S_F); |
5290 | } |
|
|
5291 | |
4828 | |
5292 | s_return (p); |
4829 | s_return (p); |
5293 | } |
4830 | } |
5294 | |
4831 | |
5295 | # if USE_STRING_PORTS |
4832 | # if USE_STRING_PORTS |
… | |
… | |
5313 | |
4850 | |
5314 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
4851 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
5315 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); |
4852 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); |
5316 | |
4853 | |
5317 | if (p == NIL) |
4854 | if (p == NIL) |
5318 | { |
|
|
5319 | s_return (S_F); |
4855 | s_return (S_F); |
5320 | } |
|
|
5321 | |
4856 | |
5322 | s_return (p); |
4857 | s_return (p); |
5323 | } |
4858 | } |
5324 | |
4859 | |
5325 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4860 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
… | |
… | |
5329 | if (car (SCHEME_V->args) == NIL) |
4864 | if (car (SCHEME_V->args) == NIL) |
5330 | { |
4865 | { |
5331 | p = port_from_scratch (SCHEME_A); |
4866 | p = port_from_scratch (SCHEME_A); |
5332 | |
4867 | |
5333 | if (p == NIL) |
4868 | if (p == NIL) |
5334 | { |
|
|
5335 | s_return (S_F); |
4869 | s_return (S_F); |
5336 | } |
|
|
5337 | } |
4870 | } |
5338 | else |
4871 | else |
5339 | { |
4872 | { |
5340 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
4873 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
5341 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); |
4874 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); |
5342 | |
4875 | |
5343 | if (p == NIL) |
4876 | if (p == NIL) |
5344 | { |
|
|
5345 | s_return (S_F); |
4877 | s_return (S_F); |
5346 | } |
|
|
5347 | } |
4878 | } |
5348 | |
4879 | |
5349 | s_return (p); |
4880 | s_return (p); |
5350 | } |
4881 | } |
5351 | |
4882 | |
… | |
… | |
5393 | case OP_CURR_ENV: /* current-environment */ |
4924 | case OP_CURR_ENV: /* current-environment */ |
5394 | s_return (SCHEME_V->envir); |
4925 | s_return (SCHEME_V->envir); |
5395 | |
4926 | |
5396 | } |
4927 | } |
5397 | |
4928 | |
5398 | return S_T; |
4929 | abort (); |
5399 | } |
4930 | } |
5400 | |
4931 | |
5401 | static pointer |
4932 | static pointer |
5402 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4933 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5403 | { |
4934 | { |
… | |
… | |
5416 | { |
4947 | { |
5417 | /* ========== reading part ========== */ |
4948 | /* ========== reading part ========== */ |
5418 | #if USE_PORTS |
4949 | #if USE_PORTS |
5419 | case OP_READ: |
4950 | case OP_READ: |
5420 | if (!is_pair (SCHEME_V->args)) |
4951 | if (!is_pair (SCHEME_V->args)) |
5421 | { |
|
|
5422 | s_goto (OP_READ_INTERNAL); |
4952 | s_goto (OP_READ_INTERNAL); |
5423 | } |
|
|
5424 | |
4953 | |
5425 | if (!is_inport (car (SCHEME_V->args))) |
4954 | if (!is_inport (car (SCHEME_V->args))) |
5426 | { |
|
|
5427 | Error_1 ("read: not an input port:", car (SCHEME_V->args)); |
4955 | Error_1 ("read: not an input port:", car (SCHEME_V->args)); |
5428 | } |
|
|
5429 | |
4956 | |
5430 | if (car (SCHEME_V->args) == SCHEME_V->inport) |
4957 | if (car (SCHEME_V->args) == SCHEME_V->inport) |
5431 | { |
|
|
5432 | s_goto (OP_READ_INTERNAL); |
4958 | s_goto (OP_READ_INTERNAL); |
5433 | } |
|
|
5434 | |
4959 | |
5435 | x = SCHEME_V->inport; |
4960 | x = SCHEME_V->inport; |
5436 | SCHEME_V->inport = car (SCHEME_V->args); |
4961 | SCHEME_V->inport = car (SCHEME_V->args); |
5437 | x = cons (x, NIL); |
4962 | x = cons (x, NIL); |
5438 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
4963 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
… | |
… | |
5469 | { |
4994 | { |
5470 | pointer p = SCHEME_V->inport; |
4995 | pointer p = SCHEME_V->inport; |
5471 | int res; |
4996 | int res; |
5472 | |
4997 | |
5473 | if (is_pair (SCHEME_V->args)) |
4998 | if (is_pair (SCHEME_V->args)) |
5474 | { |
|
|
5475 | p = car (SCHEME_V->args); |
4999 | p = car (SCHEME_V->args); |
5476 | } |
|
|
5477 | |
5000 | |
5478 | res = p->object.port->kind & port_string; |
5001 | res = p->object.port->kind & port_string; |
5479 | |
5002 | |
5480 | s_retbool (res); |
5003 | s_retbool (res); |
5481 | } |
5004 | } |
… | |
… | |
5496 | s_return (S_EOF); |
5019 | s_return (S_EOF); |
5497 | /* NOTREACHED */ |
5020 | /* NOTREACHED */ |
5498 | |
5021 | |
5499 | case TOK_VEC: |
5022 | case TOK_VEC: |
5500 | s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); |
5023 | s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); |
5501 | |
|
|
5502 | /* fall through */ |
5024 | /* fall through */ |
|
|
5025 | |
5503 | case TOK_LPAREN: |
5026 | case TOK_LPAREN: |
5504 | SCHEME_V->tok = token (SCHEME_A); |
5027 | SCHEME_V->tok = token (SCHEME_A); |
5505 | |
5028 | |
5506 | if (SCHEME_V->tok == TOK_RPAREN) |
5029 | if (SCHEME_V->tok == TOK_RPAREN) |
5507 | s_return (NIL); |
5030 | s_return (NIL); |
… | |
… | |
5527 | s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); |
5050 | s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); |
5528 | SCHEME_V->tok = TOK_LPAREN; |
5051 | SCHEME_V->tok = TOK_LPAREN; |
5529 | s_goto (OP_RDSEXPR); |
5052 | s_goto (OP_RDSEXPR); |
5530 | } |
5053 | } |
5531 | else |
5054 | else |
5532 | { |
|
|
5533 | s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); |
5055 | s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); |
5534 | } |
|
|
5535 | |
5056 | |
5536 | s_goto (OP_RDSEXPR); |
5057 | s_goto (OP_RDSEXPR); |
5537 | |
5058 | |
5538 | case TOK_COMMA: |
5059 | case TOK_COMMA: |
5539 | s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); |
5060 | s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); |
… | |
… | |
5581 | } |
5102 | } |
5582 | |
5103 | |
5583 | break; |
5104 | break; |
5584 | |
5105 | |
5585 | case OP_RDLIST: |
5106 | case OP_RDLIST: |
5586 | { |
|
|
5587 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
5107 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
5588 | SCHEME_V->tok = token (SCHEME_A); |
5108 | SCHEME_V->tok = token (SCHEME_A); |
5589 | |
5109 | |
5590 | if (SCHEME_V->tok == TOK_EOF) |
5110 | switch (SCHEME_V->tok) |
|
|
5111 | { |
|
|
5112 | case TOK_EOF: |
5591 | s_return (S_EOF); |
5113 | s_return (S_EOF); |
5592 | else if (SCHEME_V->tok == TOK_RPAREN) |
5114 | |
|
|
5115 | case TOK_RPAREN: |
5593 | { |
5116 | { |
5594 | int c = inchar (SCHEME_A); |
5117 | int c = inchar (SCHEME_A); |
5595 | |
5118 | |
5596 | if (c != '\n') |
5119 | if (c != '\n') |
5597 | backchar (SCHEME_A_ c); |
5120 | backchar (SCHEME_A_ c); |
5598 | |
|
|
5599 | #if SHOW_ERROR_LINE |
5121 | #if SHOW_ERROR_LINE |
5600 | else if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
5122 | 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++; |
5123 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line++; |
5602 | |
|
|
5603 | #endif |
5124 | #endif |
|
|
5125 | |
5604 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5126 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5605 | s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args)); |
5127 | s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args)); |
5606 | } |
5128 | } |
5607 | else if (SCHEME_V->tok == TOK_DOT) |
5129 | |
5608 | { |
5130 | case TOK_DOT: |
5609 | s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL); |
5131 | s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL); |
5610 | SCHEME_V->tok = token (SCHEME_A); |
5132 | SCHEME_V->tok = token (SCHEME_A); |
5611 | s_goto (OP_RDSEXPR); |
5133 | s_goto (OP_RDSEXPR); |
5612 | } |
5134 | |
5613 | else |
5135 | default: |
5614 | { |
|
|
5615 | s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL);; |
5136 | s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL); |
5616 | s_goto (OP_RDSEXPR); |
5137 | s_goto (OP_RDSEXPR); |
5617 | } |
5138 | } |
5618 | } |
|
|
5619 | |
5139 | |
5620 | case OP_RDDOT: |
5140 | case OP_RDDOT: |
5621 | if (token (SCHEME_A) != TOK_RPAREN) |
5141 | if (token (SCHEME_A) != TOK_RPAREN) |
5622 | Error_0 ("syntax error: illegal dot expression"); |
5142 | Error_0 ("syntax error: illegal dot expression"); |
5623 | else |
5143 | |
5624 | { |
|
|
5625 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5144 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5626 | s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); |
5145 | s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); |
5627 | } |
|
|
5628 | |
5146 | |
5629 | case OP_RDQUOTE: |
5147 | case OP_RDQUOTE: |
5630 | s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); |
5148 | s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); |
5631 | |
5149 | |
5632 | case OP_RDQQUOTE: |
5150 | case OP_RDQQUOTE: |
… | |
… | |
5732 | |
5250 | |
5733 | case OP_PVECFROM: |
5251 | case OP_PVECFROM: |
5734 | { |
5252 | { |
5735 | int i = ivalue_unchecked (cdr (SCHEME_V->args)); |
5253 | int i = ivalue_unchecked (cdr (SCHEME_V->args)); |
5736 | pointer vec = car (SCHEME_V->args); |
5254 | pointer vec = car (SCHEME_V->args); |
5737 | int len = ivalue_unchecked (vec); |
5255 | int len = veclength (vec); |
5738 | |
5256 | |
5739 | if (i == len) |
5257 | if (i == len) |
5740 | { |
5258 | { |
5741 | putstr (SCHEME_A_ ")"); |
5259 | putstr (SCHEME_A_ ")"); |
5742 | s_return (S_T); |
5260 | s_return (S_T); |
… | |
… | |
5755 | s_goto (OP_P0LIST); |
5273 | s_goto (OP_P0LIST); |
5756 | } |
5274 | } |
5757 | } |
5275 | } |
5758 | } |
5276 | } |
5759 | |
5277 | |
5760 | return S_T; |
5278 | abort (); |
5761 | } |
5279 | } |
5762 | |
5280 | |
5763 | static pointer |
5281 | static pointer |
5764 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5282 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5765 | { |
5283 | { |
… | |
… | |
5816 | |
5334 | |
5817 | case OP_MACROP: /* macro? */ |
5335 | case OP_MACROP: /* macro? */ |
5818 | s_retbool (is_macro (car (SCHEME_V->args))); |
5336 | s_retbool (is_macro (car (SCHEME_V->args))); |
5819 | } |
5337 | } |
5820 | |
5338 | |
5821 | return S_T; /* NOTREACHED */ |
5339 | abort (); |
5822 | } |
5340 | } |
5823 | |
5341 | |
5824 | typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); |
5342 | typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); |
5825 | |
5343 | |
5826 | typedef int (*test_predicate) (pointer); |
5344 | typedef int (*test_predicate) (pointer); |
… | |
… | |
5898 | { |
5416 | { |
5899 | int n = procnum (x); |
5417 | int n = procnum (x); |
5900 | const char *name = dispatch_table[n].name; |
5418 | const char *name = dispatch_table[n].name; |
5901 | |
5419 | |
5902 | if (name == 0) |
5420 | if (name == 0) |
5903 | { |
|
|
5904 | name = "ILLEGAL!"; |
5421 | name = "ILLEGAL!"; |
5905 | } |
|
|
5906 | |
5422 | |
5907 | return name; |
5423 | return name; |
5908 | } |
5424 | } |
5909 | |
5425 | |
5910 | /* kernel of this interpreter */ |
5426 | /* kernel of this interpreter */ |
… | |
… | |
5915 | |
5431 | |
5916 | for (;;) |
5432 | for (;;) |
5917 | { |
5433 | { |
5918 | op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5434 | op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5919 | |
5435 | |
|
|
5436 | #if USE_ERROR_CHECKING |
5920 | if (pcd->name != 0) /* if built-in function, check arguments */ |
5437 | if (pcd->name) /* if built-in function, check arguments */ |
5921 | { |
5438 | { |
|
|
5439 | int ok = 1; |
5922 | char msg[STRBUFFSIZE]; |
5440 | char msg[STRBUFFSIZE]; |
5923 | int ok = 1; |
|
|
5924 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5441 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5925 | |
5442 | |
5926 | #if USE_ERROR_CHECKING |
|
|
5927 | /* Check number of arguments */ |
5443 | /* Check number of arguments */ |
5928 | if (n < pcd->min_arity) |
5444 | if (ecb_expect_false (n < pcd->min_arity)) |
5929 | { |
5445 | { |
5930 | ok = 0; |
5446 | ok = 0; |
5931 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5447 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5932 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
5448 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
5933 | } |
5449 | } |
5934 | |
5450 | else if (ecb_excpect_false (n > pcd->max_arity)) |
5935 | if (ok && n > pcd->max_arity) |
|
|
5936 | { |
5451 | { |
5937 | ok = 0; |
5452 | ok = 0; |
5938 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5453 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5939 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
5454 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
5940 | } |
5455 | } |
5941 | #endif |
|
|
5942 | |
5456 | |
5943 | if (ok) |
5457 | if (ecb_expect_false (ok)) |
5944 | { |
5458 | { |
5945 | if (pcd->arg_tests_encoding != 0) |
5459 | if (pcd->arg_tests_encoding) |
5946 | { |
5460 | { |
5947 | int i = 0; |
5461 | int i = 0; |
5948 | int j; |
5462 | int j; |
5949 | const char *t = pcd->arg_tests_encoding; |
5463 | const char *t = pcd->arg_tests_encoding; |
5950 | pointer arglist = SCHEME_V->args; |
5464 | pointer arglist = SCHEME_V->args; |
… | |
… | |
5965 | if (!tests[j].fct (arg)) |
5479 | if (!tests[j].fct (arg)) |
5966 | break; |
5480 | break; |
5967 | } |
5481 | } |
5968 | |
5482 | |
5969 | if (t[1] != 0) /* last test is replicated as necessary */ |
5483 | if (t[1] != 0) /* last test is replicated as necessary */ |
5970 | { |
|
|
5971 | t++; |
5484 | t++; |
5972 | } |
|
|
5973 | |
5485 | |
5974 | arglist = cdr (arglist); |
5486 | arglist = cdr (arglist); |
5975 | i++; |
5487 | i++; |
5976 | } |
5488 | } |
5977 | while (i < n); |
5489 | while (i < n); |
5978 | |
5490 | |
5979 | #if USE_ERROR_CHECKING |
|
|
5980 | if (i < n) |
5491 | if (i < n) |
5981 | { |
5492 | { |
5982 | ok = 0; |
5493 | ok = 0; |
5983 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); |
5494 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); |
5984 | } |
5495 | } |
5985 | #endif |
|
|
5986 | } |
5496 | } |
5987 | } |
5497 | } |
5988 | |
5498 | |
5989 | if (!ok) |
5499 | if (!ok) |
5990 | { |
5500 | { |
… | |
… | |
5992 | return; |
5502 | return; |
5993 | |
5503 | |
5994 | pcd = dispatch_table + SCHEME_V->op; |
5504 | pcd = dispatch_table + SCHEME_V->op; |
5995 | } |
5505 | } |
5996 | } |
5506 | } |
|
|
5507 | #endif |
5997 | |
5508 | |
5998 | ok_to_freely_gc (SCHEME_A); |
5509 | ok_to_freely_gc (SCHEME_A); |
5999 | |
5510 | |
6000 | if (pcd->func (SCHEME_A_ (enum scheme_opcodes) SCHEME_V->op) == NIL) |
5511 | if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) |
6001 | return; |
5512 | return; |
6002 | |
5513 | |
6003 | #if USE_ERROR_CHECKING |
5514 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
6004 | if (SCHEME_V->no_memory) |
|
|
6005 | { |
5515 | { |
6006 | xwrstr ("No memory!\n"); |
5516 | xwrstr ("No memory!\n"); |
6007 | return; |
5517 | return; |
6008 | } |
5518 | } |
6009 | #endif |
|
|
6010 | } |
5519 | } |
6011 | } |
5520 | } |
6012 | |
5521 | |
6013 | /* ========== Initialization of internal keywords ========== */ |
5522 | /* ========== Initialization of internal keywords ========== */ |
6014 | |
5523 | |
6015 | static void |
5524 | static void |
6016 | assign_syntax (SCHEME_P_ char *name) |
5525 | assign_syntax (SCHEME_P_ const char *name) |
6017 | { |
5526 | { |
6018 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5527 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
6019 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5528 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
6020 | } |
5529 | } |
6021 | |
5530 | |
6022 | static void |
5531 | static void |
6023 | assign_proc (SCHEME_P_ enum scheme_opcodes op, char *name) |
5532 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
6024 | { |
5533 | { |
6025 | pointer x = mk_symbol (SCHEME_A_ name); |
5534 | pointer x = mk_symbol (SCHEME_A_ name); |
6026 | pointer y = mk_proc (SCHEME_A_ op); |
5535 | pointer y = mk_proc (SCHEME_A_ op); |
6027 | new_slot_in_env (SCHEME_A_ x, y); |
5536 | new_slot_in_env (SCHEME_A_ x, y); |
6028 | } |
5537 | } |
… | |
… | |
6030 | static pointer |
5539 | static pointer |
6031 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5540 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
6032 | { |
5541 | { |
6033 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5542 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
6034 | set_typeflag (y, (T_PROC | T_ATOM)); |
5543 | set_typeflag (y, (T_PROC | T_ATOM)); |
6035 | ivalue_unchecked (y) = (long) op; |
5544 | ivalue_unchecked (y) = op; |
6036 | set_num_integer (y); |
5545 | set_num_integer (y); |
6037 | return y; |
5546 | return y; |
6038 | } |
5547 | } |
6039 | |
5548 | |
6040 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5549 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
… | |
… | |
6065 | |
5574 | |
6066 | case 'd': |
5575 | case 'd': |
6067 | return OP_COND0; /* cond */ |
5576 | return OP_COND0; /* cond */ |
6068 | |
5577 | |
6069 | case '*': |
5578 | case '*': |
6070 | return OP_LET0AST; /* let* */ |
5579 | return OP_LET0AST;/* let* */ |
6071 | |
5580 | |
6072 | default: |
5581 | default: |
6073 | return OP_SET0; /* set! */ |
5582 | return OP_SET0; /* set! */ |
6074 | } |
5583 | } |
6075 | |
5584 | |
… | |
… | |
6097 | |
5606 | |
6098 | case 'f': |
5607 | case 'f': |
6099 | return OP_DEF0; /* define */ |
5608 | return OP_DEF0; /* define */ |
6100 | |
5609 | |
6101 | default: |
5610 | default: |
6102 | return OP_LET0REC; /* letrec */ |
5611 | return OP_LET0REC;/* letrec */ |
6103 | } |
5612 | } |
6104 | |
5613 | |
6105 | default: |
5614 | default: |
6106 | return OP_C0STREAM; /* cons-stream */ |
5615 | return OP_C0STREAM; /* cons-stream */ |
6107 | } |
5616 | } |
… | |
… | |
6158 | } |
5667 | } |
6159 | |
5668 | |
6160 | SCHEME_V->gc_verbose = 0; |
5669 | SCHEME_V->gc_verbose = 0; |
6161 | dump_stack_initialize (SCHEME_A); |
5670 | dump_stack_initialize (SCHEME_A); |
6162 | SCHEME_V->code = NIL; |
5671 | SCHEME_V->code = NIL; |
|
|
5672 | SCHEME_V->args = NIL; |
|
|
5673 | SCHEME_V->envir = NIL; |
6163 | SCHEME_V->tracing = 0; |
5674 | SCHEME_V->tracing = 0; |
6164 | |
5675 | |
6165 | /* init NIL */ |
5676 | /* init NIL */ |
6166 | set_typeflag (NIL, T_ATOM | MARK); |
5677 | set_typeflag (NIL, T_ATOM | T_MARK); |
6167 | set_car (NIL, NIL); |
5678 | set_car (NIL, NIL); |
6168 | set_cdr (NIL, NIL); |
5679 | set_cdr (NIL, NIL); |
6169 | /* init T */ |
5680 | /* init T */ |
6170 | set_typeflag (S_T, T_ATOM | MARK); |
5681 | set_typeflag (S_T, T_ATOM | T_MARK); |
6171 | set_car (S_T, S_T); |
5682 | set_car (S_T, S_T); |
6172 | set_cdr (S_T, S_T); |
5683 | set_cdr (S_T, S_T); |
6173 | /* init F */ |
5684 | /* init F */ |
6174 | set_typeflag (S_F, T_ATOM | MARK); |
5685 | set_typeflag (S_F, T_ATOM | T_MARK); |
6175 | set_car (S_F, S_F); |
5686 | set_car (S_F, S_F); |
6176 | set_cdr (S_F, S_F); |
5687 | set_cdr (S_F, S_F); |
|
|
5688 | /* init EOF_OBJ */ |
|
|
5689 | set_typeflag (S_EOF, T_ATOM | T_MARK); |
|
|
5690 | set_car (S_EOF, S_EOF); |
|
|
5691 | set_cdr (S_EOF, S_EOF); |
6177 | /* init sink */ |
5692 | /* init sink */ |
6178 | set_typeflag (S_SINK, T_PAIR | MARK); |
5693 | set_typeflag (S_SINK, T_PAIR | T_MARK); |
6179 | set_car (S_SINK, NIL); |
5694 | set_car (S_SINK, NIL); |
|
|
5695 | |
6180 | /* init c_nest */ |
5696 | /* init c_nest */ |
6181 | SCHEME_V->c_nest = NIL; |
5697 | SCHEME_V->c_nest = NIL; |
6182 | |
5698 | |
6183 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
5699 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
6184 | /* init global_env */ |
5700 | /* init global_env */ |
… | |
… | |
6186 | SCHEME_V->global_env = SCHEME_V->envir; |
5702 | SCHEME_V->global_env = SCHEME_V->envir; |
6187 | /* init else */ |
5703 | /* init else */ |
6188 | x = mk_symbol (SCHEME_A_ "else"); |
5704 | x = mk_symbol (SCHEME_A_ "else"); |
6189 | new_slot_in_env (SCHEME_A_ x, S_T); |
5705 | new_slot_in_env (SCHEME_A_ x, S_T); |
6190 | |
5706 | |
6191 | assign_syntax (SCHEME_A_ "lambda"); |
5707 | { |
6192 | assign_syntax (SCHEME_A_ "quote"); |
5708 | static const char *syntax_names[] = { |
6193 | assign_syntax (SCHEME_A_ "define"); |
5709 | "lambda", "quote", "define", "if", "begin", "set!", |
6194 | assign_syntax (SCHEME_A_ "if"); |
5710 | "let", "let*", "letrec", "cond", "delay", "and", |
6195 | assign_syntax (SCHEME_A_ "begin"); |
5711 | "or", "cons-stream", "macro", "case" |
6196 | assign_syntax (SCHEME_A_ "set!"); |
5712 | }; |
6197 | assign_syntax (SCHEME_A_ "let"); |
5713 | |
6198 | assign_syntax (SCHEME_A_ "let*"); |
5714 | 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"); |
5715 | assign_syntax (SCHEME_A_ syntax_names[i]); |
6203 | assign_syntax (SCHEME_A_ "or"); |
5716 | } |
6204 | assign_syntax (SCHEME_A_ "cons-stream"); |
|
|
6205 | assign_syntax (SCHEME_A_ "macro"); |
|
|
6206 | assign_syntax (SCHEME_A_ "case"); |
|
|
6207 | |
5717 | |
6208 | for (i = 0; i < n; i++) |
5718 | for (i = 0; i < n; i++) |
6209 | { |
|
|
6210 | if (dispatch_table[i].name != 0) |
5719 | if (dispatch_table[i].name != 0) |
6211 | { |
|
|
6212 | assign_proc (SCHEME_A_ (enum scheme_opcodes) i, dispatch_table[i].name); |
5720 | assign_proc (SCHEME_A_ i, dispatch_table[i].name); |
6213 | } |
|
|
6214 | } |
|
|
6215 | |
5721 | |
6216 | /* initialization of global pointers to special symbols */ |
5722 | /* initialization of global pointers to special symbols */ |
6217 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
5723 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
6218 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
5724 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
6219 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
5725 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
6220 | SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); |
5726 | SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); |
6221 | SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); |
5727 | SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); |
6222 | SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); |
5728 | SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); |
6223 | SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); |
5729 | SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); |
6224 | SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); |
5730 | SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); |
6225 | SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); |
5731 | SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); |
6226 | SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); |
5732 | SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); |
6227 | |
5733 | |
6228 | return !SCHEME_V->no_memory; |
5734 | return !SCHEME_V->no_memory; |
6229 | } |
5735 | } |
6230 | |
5736 | |
… | |
… | |
6385 | pointer x; |
5891 | pointer x; |
6386 | |
5892 | |
6387 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
5893 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6388 | |
5894 | |
6389 | if (x != NIL) |
5895 | if (x != NIL) |
6390 | { |
|
|
6391 | set_slot_in_env (SCHEME_A_ x, value); |
5896 | set_slot_in_env (SCHEME_A_ x, value); |
6392 | } |
|
|
6393 | else |
5897 | else |
6394 | { |
|
|
6395 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
5898 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6396 | } |
|
|
6397 | } |
5899 | } |
6398 | |
5900 | |
6399 | #if !STANDALONE |
5901 | #if !STANDALONE |
|
|
5902 | |
6400 | void |
5903 | void |
6401 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
5904 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6402 | { |
5905 | { |
6403 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
5906 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
6404 | } |
5907 | } |
… | |
… | |
6407 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
5910 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
6408 | { |
5911 | { |
6409 | int i; |
5912 | int i; |
6410 | |
5913 | |
6411 | for (i = 0; i < count; i++) |
5914 | for (i = 0; i < count; i++) |
6412 | { |
|
|
6413 | scheme_register_foreign_func (SCHEME_A_ list + i); |
5915 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6414 | } |
|
|
6415 | } |
5916 | } |
6416 | |
5917 | |
6417 | pointer |
5918 | pointer |
6418 | scheme_apply0 (SCHEME_P_ const char *procname) |
5919 | scheme_apply0 (SCHEME_P_ const char *procname) |
6419 | { |
5920 | { |
… | |
… | |
6476 | SCHEME_V->interactive_repl = old_repl; |
5977 | SCHEME_V->interactive_repl = old_repl; |
6477 | restore_from_C_call (SCHEME_A); |
5978 | restore_from_C_call (SCHEME_A); |
6478 | return SCHEME_V->value; |
5979 | return SCHEME_V->value; |
6479 | } |
5980 | } |
6480 | |
5981 | |
6481 | |
|
|
6482 | #endif |
5982 | #endif |
6483 | |
5983 | |
6484 | /* ========== Main ========== */ |
5984 | /* ========== Main ========== */ |
6485 | |
5985 | |
6486 | #if STANDALONE |
5986 | #if STANDALONE |
6487 | |
5987 | |
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 |
5988 | int |
6505 | main (int argc, char **argv) |
5989 | main (int argc, char **argv) |
6506 | { |
5990 | { |
6507 | # endif |
|
|
6508 | # if USE_MULTIPLICITY |
5991 | # if USE_MULTIPLICITY |
6509 | scheme ssc; |
5992 | scheme ssc; |
6510 | scheme *const sc = &ssc; |
5993 | scheme *const SCHEME_V = &ssc; |
6511 | # else |
5994 | # else |
6512 | # endif |
5995 | # endif |
6513 | int fin; |
5996 | int fin; |
6514 | char *file_name = InitFile; |
5997 | char *file_name = InitFile; |
6515 | int retcode; |
5998 | int retcode; |
6516 | int isfile = 1; |
5999 | int isfile = 1; |
6517 | |
|
|
6518 | if (argc == 1) |
|
|
6519 | xwrstr (banner); |
|
|
6520 | |
6000 | |
6521 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6001 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6522 | { |
6002 | { |
6523 | xwrstr ("Usage: tinyscheme -?\n"); |
6003 | xwrstr ("Usage: tinyscheme -?\n"); |
6524 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6004 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
… | |
… | |
6547 | if (access (file_name, 0) != 0) |
6027 | if (access (file_name, 0) != 0) |
6548 | { |
6028 | { |
6549 | char *p = getenv ("TINYSCHEMEINIT"); |
6029 | char *p = getenv ("TINYSCHEMEINIT"); |
6550 | |
6030 | |
6551 | if (p != 0) |
6031 | if (p != 0) |
6552 | { |
|
|
6553 | file_name = p; |
6032 | file_name = p; |
6554 | } |
|
|
6555 | } |
6033 | } |
6556 | #endif |
6034 | #endif |
6557 | |
6035 | |
6558 | do |
6036 | do |
6559 | { |
6037 | { |