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 | |
|
|
62 | /* |
|
|
63 | * Basic memory allocation units |
|
|
64 | */ |
|
|
65 | |
|
|
66 | #define banner "TinyScheme 1.41-s" |
|
|
67 | |
|
|
68 | #include <string.h> |
|
|
69 | #include <stdlib.h> |
|
|
70 | |
69 | |
71 | #if !USE_MULTIPLICITY |
70 | #if !USE_MULTIPLICITY |
72 | static scheme sc; |
71 | static scheme sc; |
73 | #endif |
72 | #endif |
74 | |
73 | |
… | |
… | |
134 | c += 'a' - 'A'; |
133 | c += 'a' - 'A'; |
135 | |
134 | |
136 | return c; |
135 | return c; |
137 | } |
136 | } |
138 | |
137 | |
|
|
138 | static int |
|
|
139 | xisdigit (char c) |
|
|
140 | { |
|
|
141 | return c >= '0' && c <= '9'; |
|
|
142 | } |
|
|
143 | |
|
|
144 | #define toupper(c) xtoupper (c) |
|
|
145 | #define tolower(c) xtolower (c) |
|
|
146 | #define isdigit(c) xisdigit (c) |
|
|
147 | |
139 | #if USE_STRLWR |
148 | #if USE_STRLWR |
140 | static const char * |
149 | static const char * |
141 | strlwr (char *s) |
150 | strlwr (char *s) |
142 | { |
151 | { |
143 | const char *p = s; |
152 | const char *p = s; |
… | |
… | |
152 | } |
161 | } |
153 | #endif |
162 | #endif |
154 | |
163 | |
155 | #define stricmp(a,b) strcmp (a, b) |
164 | #define stricmp(a,b) strcmp (a, b) |
156 | #define strlwr(s) (s) |
165 | #define strlwr(s) (s) |
157 | #define toupper(c) xtoupper(c) |
|
|
158 | #define tolower(c) xtolower(c) |
|
|
159 | |
166 | |
160 | #ifndef prompt |
167 | #ifndef prompt |
161 | # define prompt "ts> " |
168 | # define prompt "ts> " |
162 | #endif |
169 | #endif |
163 | |
170 | |
… | |
… | |
169 | # define FIRST_CELLSEGS 3 |
176 | # define FIRST_CELLSEGS 3 |
170 | #endif |
177 | #endif |
171 | |
178 | |
172 | enum scheme_types |
179 | enum scheme_types |
173 | { |
180 | { |
174 | T_NULL, |
181 | T_FREE, |
175 | T_STRING, |
182 | T_STRING, |
176 | T_NUMBER, |
183 | T_NUMBER, |
177 | T_SYMBOL, |
184 | T_SYMBOL, |
178 | T_PROC, |
185 | T_PROC, |
179 | T_PAIR, |
186 | T_PAIR, |
… | |
… | |
184 | T_PORT, |
191 | T_PORT, |
185 | T_VECTOR, |
192 | T_VECTOR, |
186 | T_MACRO, |
193 | T_MACRO, |
187 | T_PROMISE, |
194 | T_PROMISE, |
188 | T_ENVIRONMENT, |
195 | T_ENVIRONMENT, |
|
|
196 | /* one more... */ |
189 | T_NUM_SYSTEM_TYPES |
197 | T_NUM_SYSTEM_TYPES |
190 | }; |
198 | }; |
191 | |
199 | |
192 | #define T_MASKTYPE 31 /* 0000000000011111 */ |
200 | #define T_MASKTYPE 0x000f |
193 | #define T_SYNTAX 4096 /* 0001000000000000 */ |
201 | #define T_SYNTAX 0x0010 |
194 | #define T_IMMUTABLE 8192 /* 0010000000000000 */ |
202 | #define T_IMMUTABLE 0x0020 |
195 | #define T_ATOM 16384 /* 0100000000000000 */ /* only for gc */ |
203 | #define T_ATOM 0x0040 /* only for gc */ |
196 | #define CLRATOM 49151 /* 1011111111111111 */ /* only for gc */ |
204 | #define T_MARK 0x0080 /* only for gc */ |
197 | #define MARK 32768 /* 1000000000000000 */ |
|
|
198 | #define UNMARK 32767 /* 0111111111111111 */ |
|
|
199 | |
|
|
200 | |
205 | |
201 | static num num_add (num a, num b); |
206 | static num num_add (num a, num b); |
202 | static num num_mul (num a, num b); |
207 | static num num_mul (num a, num b); |
203 | static num num_div (num a, num b); |
208 | static num num_div (num a, num b); |
204 | static num num_intdiv (num a, num b); |
209 | static num num_intdiv (num a, num b); |
… | |
… | |
244 | is_vector (pointer p) |
249 | is_vector (pointer p) |
245 | { |
250 | { |
246 | return type (p) == T_VECTOR; |
251 | return type (p) == T_VECTOR; |
247 | } |
252 | } |
248 | |
253 | |
|
|
254 | #define vecvalue(p) ((p)->object.vector.vvalue) |
|
|
255 | #define veclength(p) ((p)->object.vector.length) |
249 | INTERFACE void fill_vector (pointer vec, pointer obj); |
256 | INTERFACE void fill_vector (pointer vec, pointer obj); |
|
|
257 | INTERFACE uint32_t vector_length (pointer vec); |
250 | INTERFACE pointer vector_elem (pointer vec, int ielem); |
258 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
251 | INTERFACE void set_vector_elem (pointer vec, int ielem, pointer a); |
259 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
|
|
260 | |
|
|
261 | INTERFACE uint32_t |
|
|
262 | vector_length (pointer vec) |
|
|
263 | { |
|
|
264 | return vec->object.vector.length; |
|
|
265 | } |
252 | |
266 | |
253 | INTERFACE INLINE int |
267 | INTERFACE INLINE int |
254 | is_number (pointer p) |
268 | is_number (pointer p) |
255 | { |
269 | { |
256 | return type (p) == T_NUMBER; |
270 | return type (p) == T_NUMBER; |
… | |
… | |
290 | nvalue (pointer p) |
304 | nvalue (pointer p) |
291 | { |
305 | { |
292 | return (p)->object.number; |
306 | return (p)->object.number; |
293 | } |
307 | } |
294 | |
308 | |
295 | INTERFACE long |
309 | static IVALUE |
|
|
310 | num_get_ivalue (const num n) |
|
|
311 | { |
|
|
312 | return num_is_fixnum (n) ? num_ivalue (n) : (IVALUE)num_rvalue (n); |
|
|
313 | } |
|
|
314 | |
|
|
315 | static RVALUE |
|
|
316 | num_get_rvalue (const num n) |
|
|
317 | { |
|
|
318 | return num_is_fixnum (n) ? (RVALUE)num_ivalue (n) : num_rvalue (n); |
|
|
319 | } |
|
|
320 | |
|
|
321 | INTERFACE IVALUE |
296 | ivalue (pointer p) |
322 | ivalue (pointer p) |
297 | { |
323 | { |
298 | return num_is_integer (p) ? num_ivalue ((p)->object.number) : (long) num_rvalue ((p)->object.number); |
324 | return num_get_ivalue (p->object.number); |
299 | } |
325 | } |
300 | |
326 | |
301 | INTERFACE RVALUE |
327 | INTERFACE RVALUE |
302 | rvalue (pointer p) |
328 | rvalue (pointer p) |
303 | { |
329 | { |
304 | return num_is_integer (p) ? (RVALUE) num_ivalue ((p)->object.number) : num_rvalue ((p)->object.number); |
330 | return num_get_rvalue (p->object.number); |
305 | } |
331 | } |
306 | |
332 | |
307 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
333 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
308 | #if USE_FLOAT |
334 | #if USE_REAL |
309 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
335 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
310 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
336 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
311 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
337 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
312 | #else |
338 | #else |
313 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
339 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
… | |
… | |
424 | syntaxname (pointer p) |
450 | syntaxname (pointer p) |
425 | { |
451 | { |
426 | return strvalue (car (p)); |
452 | return strvalue (car (p)); |
427 | } |
453 | } |
428 | |
454 | |
429 | #define procnum(p) ivalue(p) |
455 | #define procnum(p) ivalue (p) |
430 | static const char *procname (pointer x); |
456 | static const char *procname (pointer x); |
431 | |
457 | |
432 | INTERFACE INLINE int |
458 | INTERFACE INLINE int |
433 | is_closure (pointer p) |
459 | is_closure (pointer p) |
434 | { |
460 | { |
… | |
… | |
477 | |
503 | |
478 | #define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) |
504 | #define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) |
479 | |
505 | |
480 | #define is_atom(p) (typeflag (p) & T_ATOM) |
506 | #define is_atom(p) (typeflag (p) & T_ATOM) |
481 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
507 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
482 | #define clratom(p) set_typeflag ((p), typeflag (p) & CLRATOM) |
508 | #define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) |
483 | |
509 | |
484 | #define is_mark(p) (typeflag (p) & MARK) |
510 | #define is_mark(p) (typeflag (p) & T_MARK) |
485 | #define setmark(p) set_typeflag ((p), typeflag (p) | MARK) |
511 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
486 | #define clrmark(p) set_typeflag ((p), typeflag (p) & UNMARK) |
512 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
487 | |
513 | |
488 | INTERFACE INLINE int |
514 | INTERFACE INLINE int |
489 | is_immutable (pointer p) |
515 | is_immutable (pointer p) |
490 | { |
516 | { |
491 | return typeflag (p) & T_IMMUTABLE; |
517 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
492 | } |
518 | } |
493 | |
519 | |
494 | INTERFACE INLINE void |
520 | INTERFACE INLINE void |
495 | setimmutable (pointer p) |
521 | setimmutable (pointer p) |
496 | { |
522 | { |
|
|
523 | #if USE_ERROR_CHECKING |
497 | set_typeflag (p, T_IMMUTABLE); |
524 | set_typeflag (p, typeflag (p) | T_IMMUTABLE); |
|
|
525 | #endif |
498 | } |
526 | } |
499 | |
527 | |
500 | #if USE_CHAR_CLASSIFIERS |
528 | #if USE_CHAR_CLASSIFIERS |
501 | static INLINE int |
529 | static INLINE int |
502 | Cisalpha (int c) |
530 | Cisalpha (int c) |
… | |
… | |
595 | static int file_interactive (SCHEME_P); |
623 | static int file_interactive (SCHEME_P); |
596 | static INLINE int is_one_of (char *s, int c); |
624 | static INLINE int is_one_of (char *s, int c); |
597 | static int alloc_cellseg (SCHEME_P_ int n); |
625 | static int alloc_cellseg (SCHEME_P_ int n); |
598 | static long binary_decode (const char *s); |
626 | 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); |
… | |
… | |
646 | static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
670 | static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
647 | static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
671 | static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
648 | static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
672 | static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
649 | static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
673 | static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
650 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
674 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
651 | static void assign_syntax (SCHEME_P_ char *name); |
675 | static void assign_syntax (SCHEME_P_ const char *name); |
652 | static int syntaxnum (pointer p); |
676 | static int syntaxnum (pointer p); |
653 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, char *name); |
677 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
654 | |
678 | |
655 | static num |
679 | static num |
656 | num_add (num a, num b) |
680 | num_add (num a, num b) |
657 | { |
681 | { |
658 | num ret; |
682 | num ret; |
659 | |
683 | |
660 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
684 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
661 | |
685 | |
662 | if (num_is_fixnum (ret)) |
686 | if (num_is_fixnum (ret)) |
663 | num_set_ivalue (ret, a.value.ivalue + b.value.ivalue); |
687 | num_set_ivalue (ret, num_get_ivalue (a) + num_get_ivalue (b)); |
664 | else |
688 | else |
665 | num_set_rvalue (ret, num_rvalue (a) + num_rvalue (b)); |
689 | num_set_rvalue (ret, num_get_rvalue (a) + num_get_rvalue (b)); |
666 | |
690 | |
667 | return ret; |
691 | return ret; |
668 | } |
692 | } |
669 | |
693 | |
670 | static num |
694 | static num |
… | |
… | |
673 | num ret; |
697 | num ret; |
674 | |
698 | |
675 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
699 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
676 | |
700 | |
677 | if (num_is_fixnum (ret)) |
701 | if (num_is_fixnum (ret)) |
678 | num_set_ivalue (ret, a.value.ivalue * b.value.ivalue); |
702 | num_set_ivalue (ret, num_get_ivalue (a) * num_get_ivalue (b)); |
679 | else |
703 | else |
680 | num_set_rvalue (ret, num_rvalue (a) * num_rvalue (b)); |
704 | num_set_rvalue (ret, num_get_rvalue (a) * num_get_rvalue (b)); |
681 | |
705 | |
682 | return ret; |
706 | return ret; |
683 | } |
707 | } |
684 | |
708 | |
685 | static num |
709 | static num |
686 | num_div (num a, num b) |
710 | num_div (num a, num b) |
687 | { |
711 | { |
688 | num ret; |
712 | num ret; |
689 | |
713 | |
690 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && a.value.ivalue % b.value.ivalue == 0); |
714 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b) && num_get_ivalue (a) % num_get_ivalue (b) == 0); |
691 | |
715 | |
692 | if (num_is_fixnum (ret)) |
716 | if (num_is_fixnum (ret)) |
693 | num_set_ivalue (ret, a.value.ivalue / b.value.ivalue); |
717 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
694 | else |
718 | else |
695 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
719 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
696 | |
720 | |
697 | return ret; |
721 | return ret; |
698 | } |
722 | } |
699 | |
723 | |
700 | static num |
724 | static num |
… | |
… | |
703 | num ret; |
727 | num ret; |
704 | |
728 | |
705 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
729 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
706 | |
730 | |
707 | if (num_is_fixnum (ret)) |
731 | if (num_is_fixnum (ret)) |
708 | num_set_ivalue (ret, a.value.ivalue / b.value.ivalue); |
732 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
709 | else |
733 | else |
710 | num_set_rvalue (ret, num_rvalue (a) / num_rvalue (b)); |
734 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
711 | |
735 | |
712 | return ret; |
736 | return ret; |
713 | } |
737 | } |
714 | |
738 | |
715 | static num |
739 | static num |
… | |
… | |
718 | num ret; |
742 | num ret; |
719 | |
743 | |
720 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
744 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
721 | |
745 | |
722 | if (num_is_fixnum (ret)) |
746 | if (num_is_fixnum (ret)) |
723 | num_set_ivalue (ret, a.value.ivalue - b.value.ivalue); |
747 | num_set_ivalue (ret, num_get_ivalue (a) - num_get_ivalue (b)); |
724 | else |
748 | else |
725 | num_set_rvalue (ret, num_rvalue (a) - num_rvalue (b)); |
749 | num_set_rvalue (ret, num_get_rvalue (a) - num_get_rvalue (b)); |
726 | |
750 | |
727 | return ret; |
751 | return ret; |
728 | } |
752 | } |
729 | |
753 | |
730 | static num |
754 | static num |
… | |
… | |
732 | { |
756 | { |
733 | num ret; |
757 | num ret; |
734 | long e1, e2, res; |
758 | long e1, e2, res; |
735 | |
759 | |
736 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
760 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
737 | e1 = num_ivalue (a); |
761 | e1 = num_get_ivalue (a); |
738 | e2 = num_ivalue (b); |
762 | e2 = num_get_ivalue (b); |
739 | res = e1 % e2; |
763 | res = e1 % e2; |
740 | |
764 | |
741 | /* remainder should have same sign as second operand */ |
765 | /* remainder should have same sign as second operand */ |
742 | if (res > 0) |
766 | if (res > 0) |
743 | { |
767 | { |
… | |
… | |
759 | { |
783 | { |
760 | num ret; |
784 | num ret; |
761 | long e1, e2, res; |
785 | long e1, e2, res; |
762 | |
786 | |
763 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
787 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
764 | e1 = num_ivalue (a); |
788 | e1 = num_get_ivalue (a); |
765 | e2 = num_ivalue (b); |
789 | e2 = num_get_ivalue (b); |
766 | res = e1 % e2; |
790 | res = e1 % e2; |
767 | |
791 | |
768 | /* modulo should have same sign as second operand */ |
792 | /* modulo should have same sign as second operand */ |
769 | if (res * e2 < 0) |
793 | if (res * e2 < 0) |
770 | res += e2; |
794 | res += e2; |
… | |
… | |
778 | { |
802 | { |
779 | int ret; |
803 | int ret; |
780 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
804 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
781 | |
805 | |
782 | if (is_fixnum) |
806 | if (is_fixnum) |
783 | ret = a.value.ivalue == b.value.ivalue; |
807 | ret = num_get_ivalue (a) == num_get_ivalue (b); |
784 | else |
808 | else |
785 | ret = num_rvalue (a) == num_rvalue (b); |
809 | ret = num_get_rvalue (a) == num_get_rvalue (b); |
786 | |
810 | |
787 | return ret; |
811 | return ret; |
788 | } |
812 | } |
789 | |
813 | |
790 | |
814 | |
… | |
… | |
793 | { |
817 | { |
794 | int ret; |
818 | int ret; |
795 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
819 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
796 | |
820 | |
797 | if (is_fixnum) |
821 | if (is_fixnum) |
798 | ret = a.value.ivalue > b.value.ivalue; |
822 | ret = num_get_ivalue (a) > num_get_ivalue (b); |
799 | else |
823 | else |
800 | ret = num_rvalue (a) > num_rvalue (b); |
824 | ret = num_get_rvalue (a) > num_get_rvalue (b); |
801 | |
825 | |
802 | return ret; |
826 | return ret; |
803 | } |
827 | } |
804 | |
828 | |
805 | static int |
829 | static int |
… | |
… | |
813 | { |
837 | { |
814 | int ret; |
838 | int ret; |
815 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
839 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
816 | |
840 | |
817 | if (is_fixnum) |
841 | if (is_fixnum) |
818 | ret = a.value.ivalue < b.value.ivalue; |
842 | ret = num_get_ivalue (a) < num_get_ivalue (b); |
819 | else |
843 | else |
820 | ret = num_rvalue (a) < num_rvalue (b); |
844 | ret = num_get_rvalue (a) < num_get_rvalue (b); |
821 | |
845 | |
822 | return ret; |
846 | return ret; |
823 | } |
847 | } |
824 | |
848 | |
825 | static int |
849 | static int |
… | |
… | |
854 | #endif |
878 | #endif |
855 | |
879 | |
856 | static int |
880 | static int |
857 | is_zero_rvalue (RVALUE x) |
881 | is_zero_rvalue (RVALUE x) |
858 | { |
882 | { |
859 | #if USE_FLOAT |
883 | #if USE_REAL |
860 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
884 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
861 | #else |
885 | #else |
862 | return x == 0; |
886 | return x == 0; |
863 | #endif |
887 | #endif |
864 | } |
888 | } |
… | |
… | |
927 | SCHEME_V->fcells += segsize; |
951 | SCHEME_V->fcells += segsize; |
928 | last = newp + segsize - 1; |
952 | last = newp + segsize - 1; |
929 | |
953 | |
930 | for (p = newp; p <= last; p++) |
954 | for (p = newp; p <= last; p++) |
931 | { |
955 | { |
932 | set_typeflag (p, 0); |
956 | set_typeflag (p, T_FREE); |
|
|
957 | set_car (p, NIL); |
933 | set_cdr (p, p + 1); |
958 | set_cdr (p, p + 1); |
934 | set_car (p, NIL); |
|
|
935 | } |
959 | } |
936 | |
960 | |
937 | /* insert new cells in address order on free list */ |
961 | /* insert new cells in address order on free list */ |
938 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
962 | if (SCHEME_V->free_cell == NIL || p < SCHEME_V->free_cell) |
939 | { |
963 | { |
… | |
… | |
953 | } |
977 | } |
954 | |
978 | |
955 | return n; |
979 | return n; |
956 | } |
980 | } |
957 | |
981 | |
958 | /* get new cell. parameter a, b is marked by gc. */ |
982 | /* get new cell. parameter a, b is marked by gc. */ |
959 | static INLINE pointer |
983 | static INLINE pointer |
960 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
984 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
961 | { |
985 | { |
962 | if (SCHEME_V->free_cell == NIL) |
986 | if (SCHEME_V->free_cell == NIL) |
963 | { |
987 | { |
… | |
… | |
991 | --SCHEME_V->fcells; |
1015 | --SCHEME_V->fcells; |
992 | return x; |
1016 | return x; |
993 | } |
1017 | } |
994 | } |
1018 | } |
995 | |
1019 | |
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 - |
1020 | /* To retain recent allocs before interpreter knows about them - |
1110 | Tehom */ |
1021 | Tehom */ |
1111 | |
1022 | |
1112 | static void |
1023 | static void |
1113 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1024 | push_recent_alloc (SCHEME_P_ pointer recent, pointer extra) |
1114 | { |
1025 | { |
1115 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1026 | pointer holder = get_cell_x (SCHEME_A_ recent, extra); |
1116 | |
1027 | |
1117 | set_typeflag (holder, T_PAIR | T_IMMUTABLE); |
1028 | set_typeflag (holder, T_PAIR); |
|
|
1029 | setimmutable (holder); |
1118 | set_car (holder, recent); |
1030 | set_car (holder, recent); |
1119 | set_cdr (holder, car (S_SINK)); |
1031 | set_cdr (holder, car (S_SINK)); |
1120 | set_car (S_SINK, holder); |
1032 | set_car (S_SINK, holder); |
1121 | } |
1033 | } |
1122 | |
1034 | |
… | |
… | |
1135 | |
1047 | |
1136 | return cell; |
1048 | return cell; |
1137 | } |
1049 | } |
1138 | |
1050 | |
1139 | static pointer |
1051 | static pointer |
1140 | get_vector_object (SCHEME_P_ int len, pointer init) |
1052 | get_vector_object (SCHEME_P_ uint32_t len, pointer init) |
1141 | { |
1053 | { |
1142 | pointer cells = get_consecutive_cells (SCHEME_A_ len / 2 + len % 2 + 1); |
1054 | pointer v = get_cell_x (SCHEME_A_ 0, 0); |
|
|
1055 | pointer *e = malloc (len * sizeof (pointer)); |
1143 | |
1056 | |
1144 | #if USE_ERROR_CHECKING |
1057 | if (!e && USE_ERROR_CHECKING) |
1145 | if (SCHEME_V->no_memory) |
|
|
1146 | return S_SINK; |
1058 | return S_SINK; |
1147 | #endif |
|
|
1148 | |
1059 | |
1149 | /* Record it as a vector so that gc understands it. */ |
1060 | /* Record it as a vector so that gc understands it. */ |
1150 | set_typeflag (cells, T_VECTOR | T_ATOM); |
1061 | set_typeflag (v, T_VECTOR | T_ATOM); |
1151 | ivalue_unchecked (cells) = len; |
1062 | |
1152 | set_num_integer (cells); |
1063 | v->object.vector.vvalue = e; |
|
|
1064 | v->object.vector.length = len; |
1153 | fill_vector (cells, init); |
1065 | fill_vector (v, init); |
1154 | push_recent_alloc (SCHEME_A_ cells, NIL); |
1066 | push_recent_alloc (SCHEME_A_ v, NIL); |
1155 | |
1067 | |
1156 | return cells; |
1068 | return v; |
1157 | } |
1069 | } |
1158 | |
1070 | |
1159 | static INLINE void |
1071 | static INLINE void |
1160 | ok_to_freely_gc (SCHEME_P) |
1072 | ok_to_freely_gc (SCHEME_P) |
1161 | { |
1073 | { |
… | |
… | |
1164 | |
1076 | |
1165 | #if defined TSGRIND |
1077 | #if defined TSGRIND |
1166 | static void |
1078 | static void |
1167 | check_cell_alloced (pointer p, int expect_alloced) |
1079 | check_cell_alloced (pointer p, int expect_alloced) |
1168 | { |
1080 | { |
1169 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to |
1081 | /* Can't use putstr(SCHEME_A_ str) because callers have no access to sc. */ |
1170 | sc. */ |
|
|
1171 | if (typeflag (p) & !expect_alloced) |
1082 | if (typeflag (p) & !expect_alloced) |
1172 | xwrstr ("Cell is already allocated!\n"); |
1083 | xwrstr ("Cell is already allocated!\n"); |
1173 | |
1084 | |
1174 | if (!(typeflag (p)) & expect_alloced) |
1085 | if (!(typeflag (p)) & expect_alloced) |
1175 | xwrstr ("Cell is not allocated!\n"); |
1086 | xwrstr ("Cell is not allocated!\n"); |
… | |
… | |
1198 | if (immutable) |
1109 | if (immutable) |
1199 | setimmutable (x); |
1110 | setimmutable (x); |
1200 | |
1111 | |
1201 | set_car (x, a); |
1112 | set_car (x, a); |
1202 | set_cdr (x, b); |
1113 | set_cdr (x, b); |
|
|
1114 | |
1203 | return x; |
1115 | return x; |
1204 | } |
1116 | } |
1205 | |
1117 | |
1206 | /* ========== oblist implementation ========== */ |
1118 | /* ========== oblist implementation ========== */ |
1207 | |
1119 | |
… | |
… | |
1217 | |
1129 | |
1218 | /* returns the new symbol */ |
1130 | /* returns the new symbol */ |
1219 | static pointer |
1131 | static pointer |
1220 | oblist_add_by_name (SCHEME_P_ const char *name) |
1132 | oblist_add_by_name (SCHEME_P_ const char *name) |
1221 | { |
1133 | { |
1222 | pointer x; |
|
|
1223 | int location; |
1134 | int location; |
1224 | |
1135 | |
1225 | x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
1136 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
1226 | set_typeflag (x, T_SYMBOL); |
1137 | set_typeflag (x, T_SYMBOL); |
1227 | setimmutable (car (x)); |
1138 | setimmutable (car (x)); |
1228 | |
1139 | |
1229 | location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); |
1140 | 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))); |
1141 | set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); |
1231 | return x; |
1142 | return x; |
1232 | } |
1143 | } |
1233 | |
1144 | |
1234 | static INLINE pointer |
1145 | static INLINE pointer |
… | |
… | |
1236 | { |
1147 | { |
1237 | int location; |
1148 | int location; |
1238 | pointer x; |
1149 | pointer x; |
1239 | char *s; |
1150 | char *s; |
1240 | |
1151 | |
1241 | location = hash_fn (name, ivalue_unchecked (SCHEME_V->oblist)); |
1152 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1242 | |
1153 | |
1243 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1154 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1244 | { |
1155 | { |
1245 | s = symname (car (x)); |
1156 | s = symname (car (x)); |
1246 | |
1157 | |
… | |
… | |
1257 | { |
1168 | { |
1258 | int i; |
1169 | int i; |
1259 | pointer x; |
1170 | pointer x; |
1260 | pointer ob_list = NIL; |
1171 | pointer ob_list = NIL; |
1261 | |
1172 | |
1262 | for (i = 0; i < ivalue_unchecked (SCHEME_V->oblist); i++) |
1173 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1263 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1174 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1264 | ob_list = cons (x, ob_list); |
1175 | ob_list = cons (x, ob_list); |
1265 | |
1176 | |
1266 | return ob_list; |
1177 | return ob_list; |
1267 | } |
1178 | } |
… | |
… | |
1341 | mk_character (SCHEME_P_ int c) |
1252 | mk_character (SCHEME_P_ int c) |
1342 | { |
1253 | { |
1343 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1254 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1344 | |
1255 | |
1345 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1256 | set_typeflag (x, (T_CHARACTER | T_ATOM)); |
1346 | ivalue_unchecked (x) = c; |
1257 | ivalue_unchecked (x) = c & 0xff; |
1347 | set_num_integer (x); |
1258 | set_num_integer (x); |
1348 | return x; |
1259 | return x; |
1349 | } |
1260 | } |
1350 | |
1261 | |
1351 | /* get number atom (integer) */ |
1262 | /* get number atom (integer) */ |
… | |
… | |
1370 | set_num_real (x); |
1281 | set_num_real (x); |
1371 | return x; |
1282 | return x; |
1372 | } |
1283 | } |
1373 | |
1284 | |
1374 | static pointer |
1285 | static pointer |
1375 | mk_number (SCHEME_P_ num n) |
1286 | mk_number (SCHEME_P_ const num n) |
1376 | { |
1287 | { |
1377 | if (num_is_fixnum (n)) |
1288 | if (num_is_fixnum (n)) |
1378 | { |
|
|
1379 | return mk_integer (SCHEME_A_ num_ivalue (n)); |
1289 | return mk_integer (SCHEME_A_ num_get_ivalue (n)); |
1380 | } |
|
|
1381 | else |
1290 | else |
1382 | { |
|
|
1383 | return mk_real (SCHEME_A_ num_rvalue (n)); |
1291 | return mk_real (SCHEME_A_ num_get_rvalue (n)); |
1384 | } |
|
|
1385 | } |
1292 | } |
1386 | |
1293 | |
1387 | /* allocate name to string area */ |
1294 | /* allocate name to string area */ |
1388 | static char * |
1295 | static char * |
1389 | store_string (SCHEME_P_ int len_str, const char *str, char fill) |
1296 | store_string (SCHEME_P_ uint32_t len_str, const char *str, char fill) |
1390 | { |
1297 | { |
1391 | char *q; |
|
|
1392 | |
|
|
1393 | q = (char *)malloc (len_str + 1); |
1298 | char *q = malloc (len_str + 1); |
1394 | |
1299 | |
1395 | if (q == 0) |
1300 | if (q == 0 && USE_ERROR_CHECKING) |
1396 | { |
1301 | { |
1397 | #if USE_ERROR_CHECKING |
|
|
1398 | SCHEME_V->no_memory = 1; |
1302 | SCHEME_V->no_memory = 1; |
1399 | return SCHEME_V->strbuff; |
1303 | return SCHEME_V->strbuff; |
1400 | #endif |
|
|
1401 | } |
1304 | } |
1402 | |
1305 | |
1403 | if (str) |
1306 | if (str) |
1404 | { |
1307 | { |
1405 | int l = strlen (str); |
1308 | int l = strlen (str); |
1406 | |
1309 | |
1407 | if (l > len_str) |
1310 | if (l > len_str) |
1408 | l = len_str; |
1311 | l = len_str; |
1409 | |
1312 | |
1410 | memcpy (q, str, l + 1); |
1313 | memcpy (q, str, l); |
1411 | q[l + 1] = 0; |
1314 | q[l] = 0; |
1412 | } |
1315 | } |
1413 | else |
1316 | else |
1414 | { |
1317 | { |
1415 | memset (q, fill, len_str); |
1318 | memset (q, fill, len_str); |
1416 | q[len_str] = 0; |
1319 | q[len_str] = 0; |
… | |
… | |
1425 | { |
1328 | { |
1426 | return mk_counted_string (SCHEME_A_ str, strlen (str)); |
1329 | return mk_counted_string (SCHEME_A_ str, strlen (str)); |
1427 | } |
1330 | } |
1428 | |
1331 | |
1429 | INTERFACE pointer |
1332 | INTERFACE pointer |
1430 | mk_counted_string (SCHEME_P_ const char *str, int len) |
1333 | mk_counted_string (SCHEME_P_ const char *str, uint32_t len) |
1431 | { |
1334 | { |
1432 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1335 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1433 | |
1336 | |
1434 | set_typeflag (x, T_STRING | T_ATOM); |
1337 | set_typeflag (x, T_STRING | T_ATOM); |
1435 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1338 | strvalue (x) = store_string (SCHEME_A_ len, str, 0); |
1436 | strlength (x) = len; |
1339 | strlength (x) = len; |
1437 | return x; |
1340 | return x; |
1438 | } |
1341 | } |
1439 | |
1342 | |
1440 | INTERFACE pointer |
1343 | INTERFACE pointer |
1441 | mk_empty_string (SCHEME_P_ int len, char fill) |
1344 | mk_empty_string (SCHEME_P_ uint32_t len, char fill) |
1442 | { |
1345 | { |
1443 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1346 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1444 | |
1347 | |
1445 | set_typeflag (x, T_STRING | T_ATOM); |
1348 | set_typeflag (x, T_STRING | T_ATOM); |
1446 | strvalue (x) = store_string (SCHEME_A_ len, 0, fill); |
1349 | strvalue (x) = store_string (SCHEME_A_ len, 0, fill); |
1447 | strlength (x) = len; |
1350 | strlength (x) = len; |
1448 | return x; |
1351 | return x; |
1449 | } |
1352 | } |
1450 | |
1353 | |
1451 | INTERFACE pointer |
1354 | INTERFACE pointer |
1452 | mk_vector (SCHEME_P_ int len) |
1355 | mk_vector (SCHEME_P_ uint32_t len) |
1453 | { |
1356 | { |
1454 | return get_vector_object (SCHEME_A_ len, NIL); |
1357 | return get_vector_object (SCHEME_A_ len, NIL); |
1455 | } |
1358 | } |
1456 | |
1359 | |
1457 | INTERFACE void |
1360 | INTERFACE void |
1458 | fill_vector (pointer vec, pointer obj) |
1361 | fill_vector (pointer vec, pointer obj) |
1459 | { |
1362 | { |
1460 | int i; |
1363 | int i; |
1461 | int num = ivalue (vec) / 2 + ivalue (vec) % 2; |
|
|
1462 | |
1364 | |
1463 | for (i = 0; i < num; i++) |
1365 | for (i = 0; i < vec->object.vector.length; i++) |
1464 | { |
1366 | 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 | } |
1367 | } |
1471 | |
1368 | |
1472 | INTERFACE pointer |
1369 | INTERFACE pointer |
1473 | vector_elem (pointer vec, int ielem) |
1370 | vector_elem (pointer vec, uint32_t ielem) |
1474 | { |
1371 | { |
1475 | int n = ielem / 2; |
1372 | return vecvalue(vec)[ielem]; |
1476 | |
|
|
1477 | if (ielem % 2 == 0) |
|
|
1478 | return car (vec + 1 + n); |
|
|
1479 | else |
|
|
1480 | return cdr (vec + 1 + n); |
|
|
1481 | } |
1373 | } |
1482 | |
1374 | |
1483 | INTERFACE void |
1375 | INTERFACE void |
1484 | set_vector_elem (pointer vec, int ielem, pointer a) |
1376 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1485 | { |
1377 | { |
1486 | int n = ielem / 2; |
1378 | vecvalue(vec)[ielem] = a; |
1487 | |
|
|
1488 | if (ielem % 2 == 0) |
|
|
1489 | set_car (vec + 1 + n, a); |
|
|
1490 | else |
|
|
1491 | set_cdr (vec + 1 + n, a); |
|
|
1492 | } |
1379 | } |
1493 | |
1380 | |
1494 | /* get new symbol */ |
1381 | /* get new symbol */ |
1495 | INTERFACE pointer |
1382 | INTERFACE pointer |
1496 | mk_symbol (SCHEME_P_ const char *name) |
1383 | mk_symbol (SCHEME_P_ const char *name) |
1497 | { |
1384 | { |
1498 | pointer x; |
|
|
1499 | |
|
|
1500 | /* first check oblist */ |
1385 | /* first check oblist */ |
1501 | x = oblist_find_by_name (SCHEME_A_ name); |
1386 | pointer x = oblist_find_by_name (SCHEME_A_ name); |
1502 | |
1387 | |
1503 | if (x != NIL) |
1388 | if (x == NIL) |
1504 | return x; |
|
|
1505 | else |
|
|
1506 | { |
|
|
1507 | x = oblist_add_by_name (SCHEME_A_ name); |
1389 | x = oblist_add_by_name (SCHEME_A_ name); |
|
|
1390 | |
1508 | return x; |
1391 | return x; |
1509 | } |
|
|
1510 | } |
1392 | } |
1511 | |
1393 | |
1512 | INTERFACE pointer |
1394 | INTERFACE pointer |
1513 | gensym (SCHEME_P) |
1395 | gensym (SCHEME_P) |
1514 | { |
1396 | { |
… | |
… | |
1567 | has_dec_point = 1; |
1449 | has_dec_point = 1; |
1568 | c = *p++; |
1450 | c = *p++; |
1569 | } |
1451 | } |
1570 | |
1452 | |
1571 | if (!isdigit (c)) |
1453 | if (!isdigit (c)) |
1572 | { |
|
|
1573 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1454 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1574 | } |
|
|
1575 | } |
1455 | } |
1576 | else if (c == '.') |
1456 | else if (c == '.') |
1577 | { |
1457 | { |
1578 | has_dec_point = 1; |
1458 | has_dec_point = 1; |
1579 | c = *p++; |
1459 | c = *p++; |
1580 | |
1460 | |
1581 | if (!isdigit (c)) |
1461 | if (!isdigit (c)) |
1582 | { |
|
|
1583 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1462 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1584 | } |
|
|
1585 | } |
1463 | } |
1586 | else if (!isdigit (c)) |
1464 | else if (!isdigit (c)) |
1587 | { |
|
|
1588 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1465 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1589 | } |
|
|
1590 | |
1466 | |
1591 | for (; (c = *p) != 0; ++p) |
1467 | for (; (c = *p) != 0; ++p) |
1592 | { |
1468 | { |
1593 | if (!isdigit (c)) |
1469 | if (!isdigit (c)) |
1594 | { |
1470 | { |
… | |
… | |
1607 | has_dec_point = 1; /* decimal point illegal |
1483 | has_dec_point = 1; /* decimal point illegal |
1608 | from now on */ |
1484 | from now on */ |
1609 | p++; |
1485 | p++; |
1610 | |
1486 | |
1611 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1487 | if ((*p == '-') || (*p == '+') || isdigit (*p)) |
1612 | { |
|
|
1613 | continue; |
1488 | continue; |
1614 | } |
|
|
1615 | } |
1489 | } |
1616 | } |
1490 | } |
1617 | |
1491 | |
1618 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1492 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1619 | } |
1493 | } |
1620 | } |
1494 | } |
1621 | |
1495 | |
1622 | #if USE_FLOAT |
1496 | #if USE_REAL |
1623 | if (has_dec_point) |
1497 | if (has_dec_point) |
1624 | return mk_real (SCHEME_A_ atof (q)); |
1498 | return mk_real (SCHEME_A_ atof (q)); |
1625 | #endif |
1499 | #endif |
1626 | |
1500 | |
1627 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1501 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
… | |
… | |
1707 | static void |
1581 | static void |
1708 | mark (pointer a) |
1582 | mark (pointer a) |
1709 | { |
1583 | { |
1710 | pointer t, q, p; |
1584 | pointer t, q, p; |
1711 | |
1585 | |
1712 | t = (pointer) 0; |
1586 | t = 0; |
1713 | p = a; |
1587 | p = a; |
1714 | E2: |
1588 | E2: |
1715 | setmark (p); |
1589 | setmark (p); |
1716 | printf ("mark %p %x\n", p, p->flag);//D |
|
|
1717 | |
1590 | |
1718 | if (is_vector (p)) |
1591 | if (is_vector (p)) |
1719 | { |
1592 | { |
1720 | int i; |
1593 | int i; |
1721 | int num = ivalue_unchecked (p) / 2 + ivalue_unchecked (p) % 2; |
|
|
1722 | |
1594 | |
1723 | for (i = 0; i < num; i++) |
1595 | for (i = 0; i < p->object.vector.length; i++) |
1724 | { |
1596 | mark (vecvalue (p)[i]); |
1725 | /* Vector cells will be treated like ordinary cells */ |
|
|
1726 | mark (p + 1 + i); |
|
|
1727 | } |
|
|
1728 | } |
1597 | } |
1729 | |
1598 | |
1730 | if (is_atom (p)) |
1599 | if (is_atom (p)) |
1731 | goto E6; |
1600 | goto E6; |
1732 | |
1601 | |
… | |
… | |
1742 | goto E2; |
1611 | goto E2; |
1743 | } |
1612 | } |
1744 | |
1613 | |
1745 | E5: |
1614 | E5: |
1746 | q = cdr (p); /* down cdr */ |
1615 | q = cdr (p); /* down cdr */ |
1747 | printf ("mark+ %p\n", q, q->flag);//D |
|
|
1748 | |
1616 | |
1749 | if (q && !is_mark (q)) |
1617 | if (q && !is_mark (q)) |
1750 | { |
1618 | { |
1751 | set_cdr (p, t); |
1619 | set_cdr (p, t); |
1752 | t = p; |
1620 | t = p; |
1753 | p = q; |
1621 | p = q; |
1754 | goto E2; |
1622 | goto E2; |
1755 | } |
1623 | } |
1756 | |
1624 | |
1757 | E6: /* up. Undo the link switching from steps E4 and E5. */ |
1625 | E6: /* up. Undo the link switching from steps E4 and E5. */ |
1758 | |
|
|
1759 | if (!t) |
1626 | if (!t) |
1760 | return; |
1627 | return; |
1761 | |
1628 | |
1762 | q = t; |
1629 | q = t; |
1763 | |
1630 | |
… | |
… | |
1831 | if (is_mark (p)) |
1698 | if (is_mark (p)) |
1832 | clrmark (p); |
1699 | clrmark (p); |
1833 | else |
1700 | else |
1834 | { |
1701 | { |
1835 | /* reclaim cell */ |
1702 | /* reclaim cell */ |
1836 | if (typeflag (p) != 0) |
1703 | if (typeflag (p) != T_FREE) |
1837 | { |
1704 | { |
1838 | finalize_cell (SCHEME_A_ p); |
1705 | finalize_cell (SCHEME_A_ p); |
1839 | set_typeflag (p, 0); |
1706 | set_typeflag (p, T_FREE); |
1840 | set_car (p, NIL); |
1707 | set_car (p, NIL); |
1841 | } |
1708 | } |
1842 | |
1709 | |
1843 | ++SCHEME_V->fcells; |
1710 | ++SCHEME_V->fcells; |
1844 | set_cdr (p, SCHEME_V->free_cell); |
1711 | set_cdr (p, SCHEME_V->free_cell); |
… | |
… | |
1854 | static void |
1721 | static void |
1855 | finalize_cell (SCHEME_P_ pointer a) |
1722 | finalize_cell (SCHEME_P_ pointer a) |
1856 | { |
1723 | { |
1857 | if (is_string (a)) |
1724 | if (is_string (a)) |
1858 | free (strvalue (a)); |
1725 | free (strvalue (a)); |
|
|
1726 | else if (is_vector (a)) |
|
|
1727 | free (vecvalue (a)); |
1859 | #if USE_PORTS |
1728 | #if USE_PORTS |
1860 | else if (is_port (a)) |
1729 | else if (is_port (a)) |
1861 | { |
1730 | { |
1862 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1731 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1863 | port_close (SCHEME_A_ a, port_input | port_output); |
1732 | port_close (SCHEME_A_ a, port_input | port_output); |
… | |
… | |
2289 | char *p = SCHEME_V->strbuff; |
2158 | char *p = SCHEME_V->strbuff; |
2290 | |
2159 | |
2291 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2160 | while ((p - SCHEME_V->strbuff < sizeof (SCHEME_V->strbuff)) && !is_one_of (delim, (*p++ = inchar (SCHEME_A)))); |
2292 | |
2161 | |
2293 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2162 | if (p == SCHEME_V->strbuff + 2 && p[-2] == '\\') |
2294 | { |
|
|
2295 | *p = 0; |
2163 | *p = 0; |
2296 | } |
|
|
2297 | else |
2164 | else |
2298 | { |
2165 | { |
2299 | backchar (SCHEME_A_ p[-1]); |
2166 | backchar (SCHEME_A_ p[-1]); |
2300 | *--p = '\0'; |
2167 | *--p = '\0'; |
2301 | } |
2168 | } |
… | |
… | |
2316 | for (;;) |
2183 | for (;;) |
2317 | { |
2184 | { |
2318 | c = inchar (SCHEME_A); |
2185 | c = inchar (SCHEME_A); |
2319 | |
2186 | |
2320 | if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1) |
2187 | if (c == EOF || p - SCHEME_V->strbuff > sizeof (SCHEME_V->strbuff) - 1) |
2321 | { |
|
|
2322 | return S_F; |
2188 | return S_F; |
2323 | } |
|
|
2324 | |
2189 | |
2325 | switch (state) |
2190 | switch (state) |
2326 | { |
2191 | { |
2327 | case st_ok: |
2192 | case st_ok: |
2328 | switch (c) |
2193 | switch (c) |
… | |
… | |
2396 | c = toupper (c); |
2261 | c = toupper (c); |
2397 | |
2262 | |
2398 | if (c >= '0' && c <= 'F') |
2263 | if (c >= '0' && c <= 'F') |
2399 | { |
2264 | { |
2400 | if (c <= '9') |
2265 | if (c <= '9') |
2401 | { |
|
|
2402 | c1 = (c1 << 4) + c - '0'; |
2266 | c1 = (c1 << 4) + c - '0'; |
2403 | } |
|
|
2404 | else |
2267 | else |
2405 | { |
|
|
2406 | c1 = (c1 << 4) + c - 'A' + 10; |
2268 | c1 = (c1 << 4) + c - 'A' + 10; |
2407 | } |
|
|
2408 | |
2269 | |
2409 | if (state == st_x1) |
2270 | if (state == st_x1) |
2410 | { |
|
|
2411 | state = st_x2; |
2271 | state = st_x2; |
2412 | } |
|
|
2413 | else |
2272 | else |
2414 | { |
2273 | { |
2415 | *p++ = c1; |
2274 | *p++ = c1; |
2416 | state = st_ok; |
2275 | state = st_ok; |
2417 | } |
2276 | } |
2418 | } |
2277 | } |
2419 | else |
2278 | else |
2420 | { |
|
|
2421 | return S_F; |
2279 | return S_F; |
2422 | } |
|
|
2423 | |
2280 | |
2424 | break; |
2281 | break; |
2425 | |
2282 | |
2426 | case st_oct1: |
2283 | case st_oct1: |
2427 | case st_oct2: |
2284 | case st_oct2: |
… | |
… | |
2458 | is_one_of (char *s, int c) |
2315 | is_one_of (char *s, int c) |
2459 | { |
2316 | { |
2460 | if (c == EOF) |
2317 | if (c == EOF) |
2461 | return 1; |
2318 | return 1; |
2462 | |
2319 | |
2463 | while (*s) |
2320 | return !!strchr (s, c); |
2464 | if (*s++ == c) |
|
|
2465 | return 1; |
|
|
2466 | |
|
|
2467 | return 0; |
|
|
2468 | } |
2321 | } |
2469 | |
2322 | |
2470 | /* skip white characters */ |
2323 | /* skip white characters */ |
2471 | static INLINE int |
2324 | static INLINE int |
2472 | skipspace (SCHEME_P) |
2325 | skipspace (SCHEME_P) |
… | |
… | |
2477 | { |
2330 | { |
2478 | c = inchar (SCHEME_A); |
2331 | c = inchar (SCHEME_A); |
2479 | #if SHOW_ERROR_LINE |
2332 | #if SHOW_ERROR_LINE |
2480 | if (c == '\n') |
2333 | if (c == '\n') |
2481 | curr_line++; |
2334 | curr_line++; |
2482 | |
|
|
2483 | #endif |
2335 | #endif |
2484 | } |
2336 | } |
2485 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2337 | while (c == ' ' || c == '\n' || c == '\r' || c == '\t'); |
2486 | |
2338 | |
2487 | /* record it */ |
2339 | /* record it */ |
… | |
… | |
2524 | |
2376 | |
2525 | if (is_one_of (" \n\t", c)) |
2377 | if (is_one_of (" \n\t", c)) |
2526 | return TOK_DOT; |
2378 | return TOK_DOT; |
2527 | else |
2379 | else |
2528 | { |
2380 | { |
|
|
2381 | //TODO: ungetc twice in a row is not supported in C |
2529 | backchar (SCHEME_A_ c); |
2382 | backchar (SCHEME_A_ c); |
2530 | backchar (SCHEME_A_ '.'); |
2383 | backchar (SCHEME_A_ '.'); |
2531 | return TOK_ATOM; |
2384 | return TOK_ATOM; |
2532 | } |
2385 | } |
2533 | |
2386 | |
… | |
… | |
2647 | int d = *s / 16; |
2500 | int d = *s / 16; |
2648 | |
2501 | |
2649 | putcharacter (SCHEME_A_ 'x'); |
2502 | putcharacter (SCHEME_A_ 'x'); |
2650 | |
2503 | |
2651 | if (d < 10) |
2504 | if (d < 10) |
2652 | { |
|
|
2653 | putcharacter (SCHEME_A_ d + '0'); |
2505 | putcharacter (SCHEME_A_ d + '0'); |
2654 | } |
|
|
2655 | else |
2506 | else |
2656 | { |
|
|
2657 | putcharacter (SCHEME_A_ d - 10 + 'A'); |
2507 | putcharacter (SCHEME_A_ d - 10 + 'A'); |
2658 | } |
|
|
2659 | |
2508 | |
2660 | d = *s % 16; |
2509 | d = *s % 16; |
2661 | |
2510 | |
2662 | if (d < 10) |
2511 | if (d < 10) |
2663 | { |
|
|
2664 | putcharacter (SCHEME_A_ d + '0'); |
2512 | putcharacter (SCHEME_A_ d + '0'); |
2665 | } |
|
|
2666 | else |
2513 | else |
2667 | { |
|
|
2668 | putcharacter (SCHEME_A_ d - 10 + 'A'); |
2514 | putcharacter (SCHEME_A_ d - 10 + 'A'); |
2669 | } |
|
|
2670 | } |
2515 | } |
2671 | } |
2516 | } |
2672 | } |
2517 | } |
2673 | else |
2518 | else |
2674 | { |
|
|
2675 | putcharacter (SCHEME_A_ * s); |
2519 | putcharacter (SCHEME_A_ * s); |
2676 | } |
|
|
2677 | |
2520 | |
2678 | s++; |
2521 | s++; |
2679 | } |
2522 | } |
2680 | |
2523 | |
2681 | putcharacter (SCHEME_A_ '"'); |
2524 | putcharacter (SCHEME_A_ '"'); |
… | |
… | |
2716 | |
2559 | |
2717 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2560 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2718 | { |
2561 | { |
2719 | if (num_is_integer (l)) |
2562 | if (num_is_integer (l)) |
2720 | xnum (p, ivalue_unchecked (l)); |
2563 | xnum (p, ivalue_unchecked (l)); |
2721 | #if USE_FLOAT |
2564 | #if USE_REAL |
2722 | else |
2565 | else |
2723 | { |
2566 | { |
2724 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2567 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2725 | /* r5rs says there must be a '.' (unless 'e'?) */ |
2568 | /* r5rs says there must be a '.' (unless 'e'?) */ |
2726 | f = strcspn (p, ".e"); |
2569 | f = strcspn (p, ".e"); |
… | |
… | |
2898 | list_star (SCHEME_P_ pointer d) |
2741 | list_star (SCHEME_P_ pointer d) |
2899 | { |
2742 | { |
2900 | pointer p, q; |
2743 | pointer p, q; |
2901 | |
2744 | |
2902 | if (cdr (d) == NIL) |
2745 | if (cdr (d) == NIL) |
2903 | { |
|
|
2904 | return car (d); |
2746 | return car (d); |
2905 | } |
|
|
2906 | |
2747 | |
2907 | p = cons (car (d), cdr (d)); |
2748 | p = cons (car (d), cdr (d)); |
2908 | q = p; |
2749 | q = p; |
2909 | |
2750 | |
2910 | while (cdr (cdr (p)) != NIL) |
2751 | while (cdr (cdr (p)) != NIL) |
2911 | { |
2752 | { |
2912 | d = cons (car (p), cdr (p)); |
2753 | d = cons (car (p), cdr (p)); |
2913 | |
2754 | |
2914 | if (cdr (cdr (p)) != NIL) |
2755 | if (cdr (cdr (p)) != NIL) |
2915 | { |
|
|
2916 | p = cdr (d); |
2756 | p = cdr (d); |
2917 | } |
|
|
2918 | } |
2757 | } |
2919 | |
2758 | |
2920 | set_cdr (p, car (cdr (p))); |
2759 | set_cdr (p, car (cdr (p))); |
2921 | return q; |
2760 | return q; |
2922 | } |
2761 | } |
… | |
… | |
2936 | |
2775 | |
2937 | /* reverse list --- in-place */ |
2776 | /* reverse list --- in-place */ |
2938 | static pointer |
2777 | static pointer |
2939 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2778 | reverse_in_place (SCHEME_P_ pointer term, pointer list) |
2940 | { |
2779 | { |
2941 | pointer p = list, result = term, q; |
2780 | pointer result = term; |
|
|
2781 | pointer p = list; |
2942 | |
2782 | |
2943 | while (p != NIL) |
2783 | while (p != NIL) |
2944 | { |
2784 | { |
2945 | q = cdr (p); |
2785 | pointer q = cdr (p); |
2946 | set_cdr (p, result); |
2786 | set_cdr (p, result); |
2947 | result = p; |
2787 | result = p; |
2948 | p = q; |
2788 | p = q; |
2949 | } |
2789 | } |
2950 | |
2790 | |
… | |
… | |
3025 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
2865 | #if !defined(USE_ALIST_ENV) || !defined(USE_OBJECT_LIST) |
3026 | |
2866 | |
3027 | static int |
2867 | static int |
3028 | hash_fn (const char *key, int table_size) |
2868 | hash_fn (const char *key, int table_size) |
3029 | { |
2869 | { |
3030 | unsigned int hashed = 0; |
2870 | const unsigned char *p = key; |
3031 | const char *c; |
2871 | uint32_t hash = 2166136261; |
3032 | int bits_per_int = sizeof (unsigned int) * 8; |
|
|
3033 | |
2872 | |
3034 | for (c = key; *c; c++) |
2873 | while (*p) |
3035 | { |
2874 | 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 | |
2875 | |
3041 | return hashed % table_size; |
2876 | return hash % table_size; |
3042 | } |
2877 | } |
3043 | #endif |
2878 | #endif |
3044 | |
2879 | |
3045 | #ifndef USE_ALIST_ENV |
2880 | #ifndef USE_ALIST_ENV |
3046 | |
2881 | |
… | |
… | |
3072 | { |
2907 | { |
3073 | pointer slot = immutable_cons (variable, value); |
2908 | pointer slot = immutable_cons (variable, value); |
3074 | |
2909 | |
3075 | if (is_vector (car (env))) |
2910 | if (is_vector (car (env))) |
3076 | { |
2911 | { |
3077 | int location = hash_fn (symname (variable), ivalue_unchecked (car (env))); |
2912 | int location = hash_fn (symname (variable), veclength (car (env))); |
3078 | |
2913 | |
3079 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2914 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
3080 | } |
2915 | } |
3081 | else |
2916 | else |
3082 | set_car (env, immutable_cons (slot, car (env))); |
2917 | set_car (env, immutable_cons (slot, car (env))); |
… | |
… | |
3090 | |
2925 | |
3091 | for (x = env; x != NIL; x = cdr (x)) |
2926 | for (x = env; x != NIL; x = cdr (x)) |
3092 | { |
2927 | { |
3093 | if (is_vector (car (x))) |
2928 | if (is_vector (car (x))) |
3094 | { |
2929 | { |
3095 | location = hash_fn (symname (hdl), ivalue_unchecked (car (x))); |
2930 | location = hash_fn (symname (hdl), veclength (car (x))); |
3096 | y = vector_elem (car (x), location); |
2931 | y = vector_elem (car (x), location); |
3097 | } |
2932 | } |
3098 | else |
2933 | else |
3099 | y = car (x); |
2934 | y = car (x); |
3100 | |
2935 | |
… | |
… | |
3210 | #if USE_ERROR_HOOK |
3045 | #if USE_ERROR_HOOK |
3211 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); |
3046 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); |
3212 | |
3047 | |
3213 | if (x != NIL) |
3048 | if (x != NIL) |
3214 | { |
3049 | { |
3215 | if (a) |
3050 | pointer code = a |
3216 | SCHEME_V->code = cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL); |
3051 | ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) |
3217 | else |
3052 | : NIL; |
3218 | SCHEME_V->code = NIL; |
|
|
3219 | |
3053 | |
3220 | SCHEME_V->code = cons (mk_string (SCHEME_A_ s), SCHEME_V->code); |
3054 | code = cons (mk_string (SCHEME_A_ s), code); |
3221 | setimmutable (car (SCHEME_V->code)); |
3055 | setimmutable (car (code)); |
3222 | SCHEME_V->code = cons (slot_value_in_env (x), SCHEME_V->code); |
3056 | SCHEME_V->code = cons (slot_value_in_env (x), code); |
3223 | SCHEME_V->op = OP_EVAL; |
3057 | SCHEME_V->op = OP_EVAL; |
3224 | |
3058 | |
3225 | return S_T; |
3059 | return S_T; |
3226 | } |
3060 | } |
3227 | #endif |
3061 | #endif |
… | |
… | |
3231 | else |
3065 | else |
3232 | SCHEME_V->args = NIL; |
3066 | SCHEME_V->args = NIL; |
3233 | |
3067 | |
3234 | SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); |
3068 | SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); |
3235 | setimmutable (car (SCHEME_V->args)); |
3069 | setimmutable (car (SCHEME_V->args)); |
3236 | SCHEME_V->op = (int)OP_ERR0; |
3070 | SCHEME_V->op = OP_ERR0; |
3237 | return S_T; |
3071 | return S_T; |
3238 | } |
3072 | } |
3239 | |
3073 | |
3240 | #define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) |
3074 | #define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) |
3241 | #define Error_0(s) Error_1 (s, 0) |
3075 | #define Error_0(s) Error_1 (s, 0) |
3242 | |
3076 | |
3243 | /* Too small to turn into function */ |
3077 | /* Too small to turn into function */ |
3244 | #define BEGIN do { |
3078 | #define BEGIN do { |
3245 | #define END } while (0) |
3079 | #define END } while (0) |
3246 | #define s_goto(a) BEGIN \ |
3080 | #define s_goto(a) BEGIN \ |
3247 | SCHEME_V->op = (int)(a); \ |
3081 | SCHEME_V->op = a; \ |
3248 | return S_T; END |
3082 | return S_T; END |
3249 | |
3083 | |
3250 | #define s_return(a) return xs_return(SCHEME_A_ a) |
3084 | #define s_return(a) return xs_return (SCHEME_A_ a) |
3251 | |
3085 | |
3252 | #ifndef USE_SCHEME_STACK |
3086 | #ifndef USE_SCHEME_STACK |
3253 | |
3087 | |
3254 | /* this structure holds all the interpreter's registers */ |
3088 | /* this structure holds all the interpreter's registers */ |
3255 | struct dump_stack_frame |
3089 | struct dump_stack_frame |
… | |
… | |
3274 | SCHEME_V->dump_size += STACK_GROWTH; |
3108 | 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); |
3109 | SCHEME_V->dump_base = realloc (SCHEME_V->dump_base, sizeof (struct dump_stack_frame) * SCHEME_V->dump_size); |
3276 | } |
3110 | } |
3277 | |
3111 | |
3278 | next_frame = SCHEME_V->dump_base + nframes; |
3112 | next_frame = SCHEME_V->dump_base + nframes; |
|
|
3113 | |
3279 | next_frame->op = op; |
3114 | next_frame->op = op; |
3280 | next_frame->args = args; |
3115 | next_frame->args = args; |
3281 | next_frame->envir = SCHEME_V->envir; |
3116 | next_frame->envir = SCHEME_V->envir; |
3282 | next_frame->code = code; |
3117 | next_frame->code = code; |
|
|
3118 | |
3283 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3119 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3284 | } |
3120 | } |
3285 | |
3121 | |
3286 | static pointer |
3122 | static pointer |
3287 | xs_return (SCHEME_P_ pointer a) |
3123 | xs_return (SCHEME_P_ pointer a) |
… | |
… | |
3292 | SCHEME_V->value = a; |
3128 | SCHEME_V->value = a; |
3293 | |
3129 | |
3294 | if (nframes <= 0) |
3130 | if (nframes <= 0) |
3295 | return NIL; |
3131 | return NIL; |
3296 | |
3132 | |
3297 | nframes--; |
|
|
3298 | frame = SCHEME_V->dump_base + nframes; |
3133 | frame = &SCHEME_V->dump_base[--nframes]; |
3299 | SCHEME_V->op = frame->op; |
3134 | SCHEME_V->op = frame->op; |
3300 | SCHEME_V->args = frame->args; |
3135 | SCHEME_V->args = frame->args; |
3301 | SCHEME_V->envir = frame->envir; |
3136 | SCHEME_V->envir = frame->envir; |
3302 | SCHEME_V->code = frame->code; |
3137 | SCHEME_V->code = frame->code; |
3303 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3138 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3304 | |
3139 | |
3305 | return S_T; |
3140 | return S_T; |
3306 | } |
3141 | } |
3307 | |
3142 | |
3308 | static INLINE void |
3143 | static INLINE void |
3309 | dump_stack_reset (SCHEME_P) |
3144 | dump_stack_reset (SCHEME_P) |
3310 | { |
3145 | { |
3311 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3146 | /* in this implementation, SCHEME_V->dump is the number of frames on the stack */ |
3312 | SCHEME_V->dump = (pointer)0; |
3147 | SCHEME_V->dump = (pointer)+0; |
3313 | } |
3148 | } |
3314 | |
3149 | |
3315 | static INLINE void |
3150 | static INLINE void |
3316 | dump_stack_initialize (SCHEME_P) |
3151 | dump_stack_initialize (SCHEME_P) |
3317 | { |
3152 | { |
3318 | SCHEME_V->dump_size = 0; |
3153 | SCHEME_V->dump_size = 0; |
3319 | SCHEME_V->dump_base = NULL; |
3154 | SCHEME_V->dump_base = 0; |
3320 | dump_stack_reset (SCHEME_A); |
3155 | dump_stack_reset (SCHEME_A); |
3321 | } |
3156 | } |
3322 | |
3157 | |
3323 | static void |
3158 | static void |
3324 | dump_stack_free (SCHEME_P) |
3159 | dump_stack_free (SCHEME_P) |
3325 | { |
3160 | { |
3326 | free (SCHEME_V->dump_base); |
3161 | free (SCHEME_V->dump_base); |
3327 | SCHEME_V->dump_base = NULL; |
3162 | SCHEME_V->dump_base = 0; |
3328 | SCHEME_V->dump = (pointer)0; |
3163 | SCHEME_V->dump = (pointer)0; |
3329 | SCHEME_V->dump_size = 0; |
3164 | SCHEME_V->dump_size = 0; |
3330 | } |
3165 | } |
3331 | |
3166 | |
3332 | static void |
3167 | static void |
… | |
… | |
3374 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3209 | struct dump_stack_frame *frame = SCHEME_V->dump_base; |
3375 | |
3210 | |
3376 | while (cont != NIL) |
3211 | while (cont != NIL) |
3377 | { |
3212 | { |
3378 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3213 | frame->op = ivalue (car (cont)); cont = cdr (cont); |
3379 | frame->args = car (cont) ; cont = cdr (cont); |
3214 | frame->args = car (cont) ; cont = cdr (cont); |
3380 | frame->envir = car (cont) ; cont = cdr (cont); |
3215 | frame->envir = car (cont) ; cont = cdr (cont); |
3381 | frame->code = car (cont) ; cont = cdr (cont); |
3216 | frame->code = car (cont) ; cont = cdr (cont); |
3382 | |
3217 | |
3383 | ++frame; |
3218 | ++frame; |
3384 | ++i; |
3219 | ++i; |
3385 | } |
3220 | } |
3386 | |
3221 | |
… | |
… | |
3415 | SCHEME_V->value = a; |
3250 | SCHEME_V->value = a; |
3416 | |
3251 | |
3417 | if (dump == NIL) |
3252 | if (dump == NIL) |
3418 | return NIL; |
3253 | return NIL; |
3419 | |
3254 | |
3420 | SCHEME_V->op = ivalue (car (dump)); |
3255 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3421 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3256 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3422 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3257 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3423 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3258 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3424 | |
3259 | |
3425 | SCHEME_V->dump = dump; |
3260 | SCHEME_V->dump = dump; |
3426 | |
3261 | |
3427 | return S_T; |
3262 | return S_T; |
3428 | } |
3263 | } |
… | |
… | |
3517 | s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); |
3352 | s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); |
3518 | s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); |
3353 | s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); |
3519 | s_goto (OP_READ_INTERNAL); |
3354 | s_goto (OP_READ_INTERNAL); |
3520 | |
3355 | |
3521 | case OP_T1LVL: /* top level */ |
3356 | case OP_T1LVL: /* top level */ |
3522 | SCHEME_V->code = SCHEME_V->value; |
3357 | SCHEME_V->code = SCHEME_V->value; |
3523 | SCHEME_V->inport = SCHEME_V->save_inport; |
3358 | SCHEME_V->inport = SCHEME_V->save_inport; |
3524 | s_goto (OP_EVAL); |
3359 | s_goto (OP_EVAL); |
3525 | |
3360 | |
3526 | case OP_READ_INTERNAL: /* internal read */ |
3361 | case OP_READ_INTERNAL: /* internal read */ |
3527 | SCHEME_V->tok = token (SCHEME_A); |
3362 | SCHEME_V->tok = token (SCHEME_A); |
3528 | |
3363 | |
3529 | if (SCHEME_V->tok == TOK_EOF) |
3364 | if (SCHEME_V->tok == TOK_EOF) |
3530 | { |
|
|
3531 | s_return (S_EOF); |
3365 | s_return (S_EOF); |
3532 | } |
|
|
3533 | |
3366 | |
3534 | s_goto (OP_RDSEXPR); |
3367 | s_goto (OP_RDSEXPR); |
3535 | |
3368 | |
3536 | case OP_GENSYM: |
3369 | case OP_GENSYM: |
3537 | s_return (gensym (SCHEME_A)); |
3370 | s_return (gensym (SCHEME_A)); |
… | |
… | |
3541 | /* OP_VALUEPRINT is always pushed, because when changing from |
3374 | /* OP_VALUEPRINT is always pushed, because when changing from |
3542 | non-interactive to interactive mode, it needs to be |
3375 | non-interactive to interactive mode, it needs to be |
3543 | already on the stack */ |
3376 | already on the stack */ |
3544 | #if USE_TRACING |
3377 | #if USE_TRACING |
3545 | if (SCHEME_V->tracing) |
3378 | if (SCHEME_V->tracing) |
3546 | { |
|
|
3547 | putstr (SCHEME_A_ "\nGives: "); |
3379 | putstr (SCHEME_A_ "\nGives: "); |
3548 | } |
|
|
3549 | #endif |
3380 | #endif |
3550 | |
3381 | |
3551 | if (file_interactive (SCHEME_A)) |
3382 | if (file_interactive (SCHEME_A)) |
3552 | { |
3383 | { |
3553 | SCHEME_V->print_flag = 1; |
3384 | SCHEME_V->print_flag = 1; |
3554 | SCHEME_V->args = SCHEME_V->value; |
3385 | SCHEME_V->args = SCHEME_V->value; |
3555 | s_goto (OP_P0LIST); |
3386 | s_goto (OP_P0LIST); |
3556 | } |
3387 | } |
3557 | else |
3388 | else |
3558 | { |
|
|
3559 | s_return (SCHEME_V->value); |
3389 | s_return (SCHEME_V->value); |
3560 | } |
|
|
3561 | |
3390 | |
3562 | case OP_EVAL: /* main part of evaluation */ |
3391 | case OP_EVAL: /* main part of evaluation */ |
3563 | #if USE_TRACING |
3392 | #if USE_TRACING |
3564 | if (SCHEME_V->tracing) |
3393 | if (SCHEME_V->tracing) |
3565 | { |
3394 | { |
… | |
… | |
3569 | putstr (SCHEME_A_ "\nEval: "); |
3398 | putstr (SCHEME_A_ "\nEval: "); |
3570 | s_goto (OP_P0LIST); |
3399 | s_goto (OP_P0LIST); |
3571 | } |
3400 | } |
3572 | |
3401 | |
3573 | /* fall through */ |
3402 | /* fall through */ |
|
|
3403 | |
3574 | case OP_REAL_EVAL: |
3404 | case OP_REAL_EVAL: |
3575 | #endif |
3405 | #endif |
3576 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3406 | if (is_symbol (SCHEME_V->code)) /* symbol */ |
3577 | { |
3407 | { |
3578 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
3408 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 1); |
… | |
… | |
3582 | else |
3412 | else |
3583 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3413 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3584 | } |
3414 | } |
3585 | else if (is_pair (SCHEME_V->code)) |
3415 | else if (is_pair (SCHEME_V->code)) |
3586 | { |
3416 | { |
|
|
3417 | x = car (SCHEME_V->code); |
|
|
3418 | |
3587 | if (is_syntax (x = car (SCHEME_V->code))) /* SYNTAX */ |
3419 | if (is_syntax (x)) /* SYNTAX */ |
3588 | { |
3420 | { |
3589 | SCHEME_V->code = cdr (SCHEME_V->code); |
3421 | SCHEME_V->code = cdr (SCHEME_V->code); |
3590 | s_goto (syntaxnum (x)); |
3422 | s_goto (syntaxnum (x)); |
3591 | } |
3423 | } |
3592 | else /* first, eval top element and eval arguments */ |
3424 | else /* first, eval top element and eval arguments */ |
3593 | { |
3425 | { |
3594 | s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code); |
3426 | 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)); */ |
3427 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3596 | SCHEME_V->code = car (SCHEME_V->code); |
3428 | SCHEME_V->code = x; |
3597 | s_goto (OP_EVAL); |
3429 | s_goto (OP_EVAL); |
3598 | } |
3430 | } |
3599 | } |
3431 | } |
3600 | else |
3432 | else |
3601 | s_return (SCHEME_V->code); |
3433 | s_return (SCHEME_V->code); |
… | |
… | |
3654 | putstr (SCHEME_A_ "\nApply to: "); |
3486 | putstr (SCHEME_A_ "\nApply to: "); |
3655 | s_goto (OP_P0LIST); |
3487 | s_goto (OP_P0LIST); |
3656 | } |
3488 | } |
3657 | |
3489 | |
3658 | /* fall through */ |
3490 | /* fall through */ |
|
|
3491 | |
3659 | case OP_REAL_APPLY: |
3492 | case OP_REAL_APPLY: |
3660 | #endif |
3493 | #endif |
3661 | if (is_proc (SCHEME_V->code)) |
3494 | if (is_proc (SCHEME_V->code)) |
3662 | { |
3495 | { |
3663 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3496 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
… | |
… | |
3677 | new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); |
3510 | new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); |
3678 | |
3511 | |
3679 | for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) |
3512 | for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) |
3680 | { |
3513 | { |
3681 | if (y == NIL) |
3514 | if (y == NIL) |
3682 | { |
|
|
3683 | Error_0 ("not enough arguments"); |
3515 | Error_0 ("not enough arguments"); |
3684 | } |
|
|
3685 | else |
3516 | else |
3686 | { |
|
|
3687 | new_slot_in_env (SCHEME_A_ car (x), car (y)); |
3517 | new_slot_in_env (SCHEME_A_ car (x), car (y)); |
3688 | } |
|
|
3689 | } |
3518 | } |
3690 | |
3519 | |
3691 | if (x == NIL) |
3520 | if (x == NIL) |
3692 | { |
3521 | { |
3693 | |
|
|
3694 | /*-- |
3522 | /*-- |
3695 | * if (y != NIL) { |
3523 | * if (y != NIL) { |
3696 | * Error_0("too many arguments"); |
3524 | * Error_0("too many arguments"); |
3697 | * } |
3525 | * } |
3698 | */ |
3526 | */ |
3699 | } |
3527 | } |
3700 | else if (is_symbol (x)) |
3528 | else if (is_symbol (x)) |
3701 | new_slot_in_env (SCHEME_A_ x, y); |
3529 | new_slot_in_env (SCHEME_A_ x, y); |
3702 | else |
3530 | else |
3703 | { |
|
|
3704 | Error_1 ("syntax error in closure: not a symbol:", x); |
3531 | Error_1 ("syntax error in closure: not a symbol:", x); |
3705 | } |
|
|
3706 | |
3532 | |
3707 | SCHEME_V->code = cdr (closure_code (SCHEME_V->code)); |
3533 | SCHEME_V->code = cdr (closure_code (SCHEME_V->code)); |
3708 | SCHEME_V->args = NIL; |
3534 | SCHEME_V->args = NIL; |
3709 | s_goto (OP_BEGIN); |
3535 | s_goto (OP_BEGIN); |
3710 | } |
3536 | } |
… | |
… | |
3780 | x = car (SCHEME_V->code); |
3606 | x = car (SCHEME_V->code); |
3781 | SCHEME_V->code = cadr (SCHEME_V->code); |
3607 | SCHEME_V->code = cadr (SCHEME_V->code); |
3782 | } |
3608 | } |
3783 | |
3609 | |
3784 | if (!is_symbol (x)) |
3610 | if (!is_symbol (x)) |
3785 | { |
|
|
3786 | Error_0 ("variable is not a symbol"); |
3611 | Error_0 ("variable is not a symbol"); |
3787 | } |
|
|
3788 | |
3612 | |
3789 | s_save (SCHEME_A_ OP_DEF1, NIL, x); |
3613 | s_save (SCHEME_A_ OP_DEF1, NIL, x); |
3790 | s_goto (OP_EVAL); |
3614 | s_goto (OP_EVAL); |
3791 | |
3615 | |
3792 | case OP_DEF1: /* define */ |
3616 | case OP_DEF1: /* define */ |
3793 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); |
3617 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); |
3794 | |
3618 | |
3795 | if (x != NIL) |
3619 | if (x != NIL) |
3796 | { |
|
|
3797 | set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); |
3620 | set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); |
3798 | } |
|
|
3799 | else |
3621 | else |
3800 | { |
|
|
3801 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3622 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3802 | } |
|
|
3803 | |
3623 | |
3804 | s_return (SCHEME_V->code); |
3624 | s_return (SCHEME_V->code); |
3805 | |
3625 | |
3806 | |
3626 | |
3807 | case OP_DEFP: /* defined? */ |
3627 | case OP_DEFP: /* defined? */ |
3808 | x = SCHEME_V->envir; |
3628 | x = SCHEME_V->envir; |
3809 | |
3629 | |
3810 | if (cdr (SCHEME_V->args) != NIL) |
3630 | if (cdr (SCHEME_V->args) != NIL) |
3811 | { |
|
|
3812 | x = cadr (SCHEME_V->args); |
3631 | x = cadr (SCHEME_V->args); |
3813 | } |
|
|
3814 | |
3632 | |
3815 | s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); |
3633 | s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); |
3816 | |
3634 | |
3817 | case OP_SET0: /* set! */ |
3635 | case OP_SET0: /* set! */ |
3818 | if (is_immutable (car (SCHEME_V->code))) |
3636 | if (is_immutable (car (SCHEME_V->code))) |
… | |
… | |
3829 | { |
3647 | { |
3830 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3648 | set_slot_in_env (SCHEME_A_ y, SCHEME_V->value); |
3831 | s_return (SCHEME_V->value); |
3649 | s_return (SCHEME_V->value); |
3832 | } |
3650 | } |
3833 | else |
3651 | else |
3834 | { |
|
|
3835 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3652 | Error_1 ("set!: unbound variable:", SCHEME_V->code); |
3836 | } |
|
|
3837 | |
3653 | |
3838 | |
3654 | |
3839 | case OP_BEGIN: /* begin */ |
3655 | case OP_BEGIN: /* begin */ |
3840 | if (!is_pair (SCHEME_V->code)) |
3656 | if (!is_pair (SCHEME_V->code)) |
3841 | { |
|
|
3842 | s_return (SCHEME_V->code); |
3657 | s_return (SCHEME_V->code); |
3843 | } |
|
|
3844 | |
3658 | |
3845 | if (cdr (SCHEME_V->code) != NIL) |
3659 | if (cdr (SCHEME_V->code) != NIL) |
3846 | { |
|
|
3847 | s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code)); |
3660 | s_save (SCHEME_A_ OP_BEGIN, NIL, cdr (SCHEME_V->code)); |
3848 | } |
|
|
3849 | |
3661 | |
3850 | SCHEME_V->code = car (SCHEME_V->code); |
3662 | SCHEME_V->code = car (SCHEME_V->code); |
3851 | s_goto (OP_EVAL); |
3663 | s_goto (OP_EVAL); |
3852 | |
3664 | |
3853 | case OP_IF0: /* if */ |
3665 | case OP_IF0: /* if */ |
… | |
… | |
3874 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3686 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3875 | |
3687 | |
3876 | if (is_pair (SCHEME_V->code)) /* continue */ |
3688 | if (is_pair (SCHEME_V->code)) /* continue */ |
3877 | { |
3689 | { |
3878 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3690 | 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)); |
3691 | Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); |
3881 | } |
|
|
3882 | |
3692 | |
3883 | s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); |
3693 | s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); |
3884 | SCHEME_V->code = cadar (SCHEME_V->code); |
3694 | SCHEME_V->code = cadar (SCHEME_V->code); |
3885 | SCHEME_V->args = NIL; |
3695 | SCHEME_V->args = NIL; |
3886 | s_goto (OP_EVAL); |
3696 | s_goto (OP_EVAL); |
… | |
… | |
3896 | case OP_LET2: /* let */ |
3706 | case OP_LET2: /* let */ |
3897 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3707 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3898 | |
3708 | |
3899 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; |
3709 | 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)) |
3710 | y != NIL; x = cdr (x), y = cdr (y)) |
3901 | { |
|
|
3902 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3711 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3903 | } |
|
|
3904 | |
3712 | |
3905 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3713 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3906 | { |
3714 | { |
3907 | for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) |
3715 | for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) |
3908 | { |
3716 | { |
… | |
… | |
3978 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3786 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3979 | |
3787 | |
3980 | if (is_pair (SCHEME_V->code)) /* continue */ |
3788 | if (is_pair (SCHEME_V->code)) /* continue */ |
3981 | { |
3789 | { |
3982 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3790 | 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)); |
3791 | Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); |
3985 | } |
|
|
3986 | |
3792 | |
3987 | s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); |
3793 | s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); |
3988 | SCHEME_V->code = cadar (SCHEME_V->code); |
3794 | SCHEME_V->code = cadar (SCHEME_V->code); |
3989 | SCHEME_V->args = NIL; |
3795 | SCHEME_V->args = NIL; |
3990 | s_goto (OP_EVAL); |
3796 | s_goto (OP_EVAL); |
… | |
… | |
3997 | s_goto (OP_LET2REC); |
3803 | s_goto (OP_LET2REC); |
3998 | } |
3804 | } |
3999 | |
3805 | |
4000 | case OP_LET2REC: /* letrec */ |
3806 | case OP_LET2REC: /* letrec */ |
4001 | for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) |
3807 | 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)); |
3808 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
4004 | } |
|
|
4005 | |
3809 | |
4006 | SCHEME_V->code = cdr (SCHEME_V->code); |
3810 | SCHEME_V->code = cdr (SCHEME_V->code); |
4007 | SCHEME_V->args = NIL; |
3811 | SCHEME_V->args = NIL; |
4008 | s_goto (OP_BEGIN); |
3812 | s_goto (OP_BEGIN); |
4009 | |
3813 | |
4010 | case OP_COND0: /* cond */ |
3814 | case OP_COND0: /* cond */ |
4011 | if (!is_pair (SCHEME_V->code)) |
3815 | if (!is_pair (SCHEME_V->code)) |
4012 | { |
|
|
4013 | Error_0 ("syntax error in cond"); |
3816 | Error_0 ("syntax error in cond"); |
4014 | } |
|
|
4015 | |
3817 | |
4016 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3818 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
4017 | SCHEME_V->code = caar (SCHEME_V->code); |
3819 | SCHEME_V->code = caar (SCHEME_V->code); |
4018 | s_goto (OP_EVAL); |
3820 | s_goto (OP_EVAL); |
4019 | |
3821 | |
4020 | case OP_COND1: /* cond */ |
3822 | case OP_COND1: /* cond */ |
4021 | if (is_true (SCHEME_V->value)) |
3823 | if (is_true (SCHEME_V->value)) |
4022 | { |
3824 | { |
4023 | if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL) |
3825 | if ((SCHEME_V->code = cdar (SCHEME_V->code)) == NIL) |
4024 | { |
|
|
4025 | s_return (SCHEME_V->value); |
3826 | s_return (SCHEME_V->value); |
4026 | } |
|
|
4027 | |
3827 | |
4028 | if (car (SCHEME_V->code) == SCHEME_V->FEED_TO) |
3828 | if (car (SCHEME_V->code) == SCHEME_V->FEED_TO) |
4029 | { |
3829 | { |
4030 | if (!is_pair (cdr (SCHEME_V->code))) |
3830 | if (!is_pair (cdr (SCHEME_V->code))) |
4031 | { |
|
|
4032 | Error_0 ("syntax error in cond"); |
3831 | Error_0 ("syntax error in cond"); |
4033 | } |
|
|
4034 | |
3832 | |
4035 | x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)); |
3833 | x = cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)); |
4036 | SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL)); |
3834 | SCHEME_V->code = cons (cadr (SCHEME_V->code), cons (x, NIL)); |
4037 | s_goto (OP_EVAL); |
3835 | s_goto (OP_EVAL); |
4038 | } |
3836 | } |
… | |
… | |
4040 | s_goto (OP_BEGIN); |
3838 | s_goto (OP_BEGIN); |
4041 | } |
3839 | } |
4042 | else |
3840 | else |
4043 | { |
3841 | { |
4044 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
3842 | if ((SCHEME_V->code = cdr (SCHEME_V->code)) == NIL) |
4045 | { |
|
|
4046 | s_return (NIL); |
3843 | s_return (NIL); |
4047 | } |
|
|
4048 | else |
3844 | else |
4049 | { |
3845 | { |
4050 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
3846 | s_save (SCHEME_A_ OP_COND1, NIL, SCHEME_V->code); |
4051 | SCHEME_V->code = caar (SCHEME_V->code); |
3847 | SCHEME_V->code = caar (SCHEME_V->code); |
4052 | s_goto (OP_EVAL); |
3848 | s_goto (OP_EVAL); |
… | |
… | |
4058 | set_typeflag (x, T_PROMISE); |
3854 | set_typeflag (x, T_PROMISE); |
4059 | s_return (x); |
3855 | s_return (x); |
4060 | |
3856 | |
4061 | case OP_AND0: /* and */ |
3857 | case OP_AND0: /* and */ |
4062 | if (SCHEME_V->code == NIL) |
3858 | if (SCHEME_V->code == NIL) |
4063 | { |
|
|
4064 | s_return (S_T); |
3859 | s_return (S_T); |
4065 | } |
|
|
4066 | |
3860 | |
4067 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3861 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
4068 | SCHEME_V->code = car (SCHEME_V->code); |
3862 | SCHEME_V->code = car (SCHEME_V->code); |
4069 | s_goto (OP_EVAL); |
3863 | s_goto (OP_EVAL); |
4070 | |
3864 | |
4071 | case OP_AND1: /* and */ |
3865 | case OP_AND1: /* and */ |
4072 | if (is_false (SCHEME_V->value)) |
3866 | if (is_false (SCHEME_V->value)) |
4073 | { |
|
|
4074 | s_return (SCHEME_V->value); |
3867 | s_return (SCHEME_V->value); |
4075 | } |
|
|
4076 | else if (SCHEME_V->code == NIL) |
3868 | else if (SCHEME_V->code == NIL) |
4077 | { |
|
|
4078 | s_return (SCHEME_V->value); |
3869 | s_return (SCHEME_V->value); |
4079 | } |
|
|
4080 | else |
3870 | else |
4081 | { |
3871 | { |
4082 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
3872 | s_save (SCHEME_A_ OP_AND1, NIL, cdr (SCHEME_V->code)); |
4083 | SCHEME_V->code = car (SCHEME_V->code); |
3873 | SCHEME_V->code = car (SCHEME_V->code); |
4084 | s_goto (OP_EVAL); |
3874 | s_goto (OP_EVAL); |
4085 | } |
3875 | } |
4086 | |
3876 | |
4087 | case OP_OR0: /* or */ |
3877 | case OP_OR0: /* or */ |
4088 | if (SCHEME_V->code == NIL) |
3878 | if (SCHEME_V->code == NIL) |
4089 | { |
|
|
4090 | s_return (S_F); |
3879 | s_return (S_F); |
4091 | } |
|
|
4092 | |
3880 | |
4093 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3881 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
4094 | SCHEME_V->code = car (SCHEME_V->code); |
3882 | SCHEME_V->code = car (SCHEME_V->code); |
4095 | s_goto (OP_EVAL); |
3883 | s_goto (OP_EVAL); |
4096 | |
3884 | |
4097 | case OP_OR1: /* or */ |
3885 | case OP_OR1: /* or */ |
4098 | if (is_true (SCHEME_V->value)) |
3886 | if (is_true (SCHEME_V->value)) |
4099 | { |
|
|
4100 | s_return (SCHEME_V->value); |
3887 | s_return (SCHEME_V->value); |
4101 | } |
|
|
4102 | else if (SCHEME_V->code == NIL) |
3888 | else if (SCHEME_V->code == NIL) |
4103 | { |
|
|
4104 | s_return (SCHEME_V->value); |
3889 | s_return (SCHEME_V->value); |
4105 | } |
|
|
4106 | else |
3890 | else |
4107 | { |
3891 | { |
4108 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
3892 | s_save (SCHEME_A_ OP_OR1, NIL, cdr (SCHEME_V->code)); |
4109 | SCHEME_V->code = car (SCHEME_V->code); |
3893 | SCHEME_V->code = car (SCHEME_V->code); |
4110 | s_goto (OP_EVAL); |
3894 | s_goto (OP_EVAL); |
… | |
… | |
4132 | x = car (SCHEME_V->code); |
3916 | x = car (SCHEME_V->code); |
4133 | SCHEME_V->code = cadr (SCHEME_V->code); |
3917 | SCHEME_V->code = cadr (SCHEME_V->code); |
4134 | } |
3918 | } |
4135 | |
3919 | |
4136 | if (!is_symbol (x)) |
3920 | if (!is_symbol (x)) |
4137 | { |
|
|
4138 | Error_0 ("variable is not a symbol"); |
3921 | Error_0 ("variable is not a symbol"); |
4139 | } |
|
|
4140 | |
3922 | |
4141 | s_save (SCHEME_A_ OP_MACRO1, NIL, x); |
3923 | s_save (SCHEME_A_ OP_MACRO1, NIL, x); |
4142 | s_goto (OP_EVAL); |
3924 | s_goto (OP_EVAL); |
4143 | |
3925 | |
4144 | case OP_MACRO1: /* macro */ |
3926 | case OP_MACRO1: /* macro */ |
4145 | set_typeflag (SCHEME_V->value, T_MACRO); |
3927 | set_typeflag (SCHEME_V->value, T_MACRO); |
4146 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); |
3928 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->code, 0); |
4147 | |
3929 | |
4148 | if (x != NIL) |
3930 | if (x != NIL) |
4149 | { |
|
|
4150 | set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); |
3931 | set_slot_in_env (SCHEME_A_ x, SCHEME_V->value); |
4151 | } |
|
|
4152 | else |
3932 | else |
4153 | { |
|
|
4154 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
3933 | new_slot_in_env (SCHEME_A_ SCHEME_V->code, SCHEME_V->value); |
4155 | } |
|
|
4156 | |
3934 | |
4157 | s_return (SCHEME_V->code); |
3935 | s_return (SCHEME_V->code); |
4158 | |
3936 | |
4159 | case OP_CASE0: /* case */ |
3937 | case OP_CASE0: /* case */ |
4160 | s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code)); |
3938 | s_save (SCHEME_A_ OP_CASE1, NIL, cdr (SCHEME_V->code)); |
… | |
… | |
4163 | |
3941 | |
4164 | case OP_CASE1: /* case */ |
3942 | case OP_CASE1: /* case */ |
4165 | for (x = SCHEME_V->code; x != NIL; x = cdr (x)) |
3943 | for (x = SCHEME_V->code; x != NIL; x = cdr (x)) |
4166 | { |
3944 | { |
4167 | if (!is_pair (y = caar (x))) |
3945 | if (!is_pair (y = caar (x))) |
4168 | { |
|
|
4169 | break; |
3946 | break; |
4170 | } |
|
|
4171 | |
3947 | |
4172 | for (; y != NIL; y = cdr (y)) |
3948 | for (; y != NIL; y = cdr (y)) |
4173 | { |
3949 | { |
4174 | if (eqv (car (y), SCHEME_V->value)) |
3950 | if (eqv (car (y), SCHEME_V->value)) |
4175 | { |
|
|
4176 | break; |
3951 | break; |
4177 | } |
|
|
4178 | } |
3952 | } |
4179 | |
3953 | |
4180 | if (y != NIL) |
3954 | if (y != NIL) |
4181 | { |
|
|
4182 | break; |
3955 | break; |
4183 | } |
|
|
4184 | } |
3956 | } |
4185 | |
3957 | |
4186 | if (x != NIL) |
3958 | if (x != NIL) |
4187 | { |
3959 | { |
4188 | if (is_pair (caar (x))) |
3960 | if (is_pair (caar (x))) |
… | |
… | |
4196 | SCHEME_V->code = caar (x); |
3968 | SCHEME_V->code = caar (x); |
4197 | s_goto (OP_EVAL); |
3969 | s_goto (OP_EVAL); |
4198 | } |
3970 | } |
4199 | } |
3971 | } |
4200 | else |
3972 | else |
4201 | { |
|
|
4202 | s_return (NIL); |
3973 | s_return (NIL); |
4203 | } |
|
|
4204 | |
3974 | |
4205 | case OP_CASE2: /* case */ |
3975 | case OP_CASE2: /* case */ |
4206 | if (is_true (SCHEME_V->value)) |
3976 | if (is_true (SCHEME_V->value)) |
4207 | { |
|
|
4208 | s_goto (OP_BEGIN); |
3977 | s_goto (OP_BEGIN); |
4209 | } |
|
|
4210 | else |
3978 | else |
4211 | { |
|
|
4212 | s_return (NIL); |
3979 | s_return (NIL); |
4213 | } |
|
|
4214 | |
3980 | |
4215 | case OP_PAPPLY: /* apply */ |
3981 | case OP_PAPPLY: /* apply */ |
4216 | SCHEME_V->code = car (SCHEME_V->args); |
3982 | SCHEME_V->code = car (SCHEME_V->args); |
4217 | SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); |
3983 | SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); |
4218 | /*SCHEME_V->args = cadr(SCHEME_V->args); */ |
3984 | /*SCHEME_V->args = cadr(SCHEME_V->args); */ |
4219 | s_goto (OP_APPLY); |
3985 | s_goto (OP_APPLY); |
4220 | |
3986 | |
4221 | case OP_PEVAL: /* eval */ |
3987 | case OP_PEVAL: /* eval */ |
4222 | if (cdr (SCHEME_V->args) != NIL) |
3988 | if (cdr (SCHEME_V->args) != NIL) |
4223 | { |
|
|
4224 | SCHEME_V->envir = cadr (SCHEME_V->args); |
3989 | SCHEME_V->envir = cadr (SCHEME_V->args); |
4225 | } |
|
|
4226 | |
3990 | |
4227 | SCHEME_V->code = car (SCHEME_V->args); |
3991 | SCHEME_V->code = car (SCHEME_V->args); |
4228 | s_goto (OP_EVAL); |
3992 | s_goto (OP_EVAL); |
4229 | |
3993 | |
4230 | case OP_CONTINUATION: /* call-with-current-continuation */ |
3994 | case OP_CONTINUATION: /* call-with-current-continuation */ |
4231 | SCHEME_V->code = car (SCHEME_V->args); |
3995 | SCHEME_V->code = car (SCHEME_V->args); |
4232 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_V)), NIL); |
3996 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
4233 | s_goto (OP_APPLY); |
3997 | s_goto (OP_APPLY); |
4234 | } |
3998 | } |
4235 | |
3999 | |
4236 | return S_T; |
4000 | return S_T; |
4237 | } |
4001 | } |
… | |
… | |
4252 | |
4016 | |
4253 | case OP_INEX2EX: /* inexact->exact */ |
4017 | case OP_INEX2EX: /* inexact->exact */ |
4254 | x = car (SCHEME_V->args); |
4018 | x = car (SCHEME_V->args); |
4255 | |
4019 | |
4256 | if (num_is_integer (x)) |
4020 | if (num_is_integer (x)) |
4257 | { |
|
|
4258 | s_return (x); |
4021 | s_return (x); |
4259 | } |
|
|
4260 | else if (modf (rvalue_unchecked (x), &dd) == 0.0) |
4022 | else if (modf (rvalue_unchecked (x), &dd) == 0.0) |
4261 | { |
|
|
4262 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4023 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4263 | } |
|
|
4264 | else |
4024 | else |
4265 | { |
|
|
4266 | Error_1 ("inexact->exact: not integral:", x); |
4025 | Error_1 ("inexact->exact: not integral:", x); |
4267 | } |
|
|
4268 | |
4026 | |
4269 | case OP_EXP: |
4027 | case OP_EXP: |
4270 | x = car (SCHEME_V->args); |
4028 | x = car (SCHEME_V->args); |
4271 | s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4029 | s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4272 | |
4030 | |
… | |
… | |
4296 | |
4054 | |
4297 | case OP_ATAN: |
4055 | case OP_ATAN: |
4298 | x = car (SCHEME_V->args); |
4056 | x = car (SCHEME_V->args); |
4299 | |
4057 | |
4300 | if (cdr (SCHEME_V->args) == NIL) |
4058 | if (cdr (SCHEME_V->args) == NIL) |
4301 | { |
|
|
4302 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4059 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4303 | } |
|
|
4304 | else |
4060 | else |
4305 | { |
4061 | { |
4306 | pointer y = cadr (SCHEME_V->args); |
4062 | pointer y = cadr (SCHEME_V->args); |
4307 | |
4063 | |
4308 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
4064 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
… | |
… | |
4324 | real_result = 0; |
4080 | real_result = 0; |
4325 | |
4081 | |
4326 | /* This 'if' is an R5RS compatibility fix. */ |
4082 | /* This 'if' is an R5RS compatibility fix. */ |
4327 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4083 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4328 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4084 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4329 | { |
|
|
4330 | result = 0.0; |
4085 | result = 0.0; |
4331 | } |
|
|
4332 | else |
4086 | else |
4333 | { |
|
|
4334 | result = pow (rvalue (x), rvalue (y)); |
4087 | result = pow (rvalue (x), rvalue (y)); |
4335 | } |
|
|
4336 | |
4088 | |
4337 | /* Before returning integer result make sure we can. */ |
4089 | /* Before returning integer result make sure we can. */ |
4338 | /* If the test fails, result is too big for integer. */ |
4090 | /* If the test fails, result is too big for integer. */ |
4339 | if (!real_result) |
4091 | if (!real_result) |
4340 | { |
4092 | { |
… | |
… | |
4343 | if (result != (RVALUE) result_as_long) |
4095 | if (result != (RVALUE) result_as_long) |
4344 | real_result = 1; |
4096 | real_result = 1; |
4345 | } |
4097 | } |
4346 | |
4098 | |
4347 | if (real_result) |
4099 | if (real_result) |
4348 | { |
|
|
4349 | s_return (mk_real (SCHEME_A_ result)); |
4100 | s_return (mk_real (SCHEME_A_ result)); |
4350 | } |
|
|
4351 | else |
4101 | else |
4352 | { |
|
|
4353 | s_return (mk_integer (SCHEME_A_ result)); |
4102 | s_return (mk_integer (SCHEME_A_ result)); |
4354 | } |
|
|
4355 | } |
4103 | } |
4356 | |
4104 | |
4357 | case OP_FLOOR: |
4105 | case OP_FLOOR: |
4358 | x = car (SCHEME_V->args); |
4106 | x = car (SCHEME_V->args); |
4359 | s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
4107 | s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
… | |
… | |
4368 | |
4116 | |
4369 | x = car (SCHEME_V->args); |
4117 | x = car (SCHEME_V->args); |
4370 | rvalue_of_x = rvalue (x); |
4118 | rvalue_of_x = rvalue (x); |
4371 | |
4119 | |
4372 | if (rvalue_of_x > 0) |
4120 | if (rvalue_of_x > 0) |
4373 | { |
|
|
4374 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
4121 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
4375 | } |
|
|
4376 | else |
4122 | else |
4377 | { |
|
|
4378 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
4123 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
4379 | } |
|
|
4380 | } |
4124 | } |
4381 | |
4125 | |
4382 | case OP_ROUND: |
4126 | case OP_ROUND: |
4383 | x = car (SCHEME_V->args); |
4127 | x = car (SCHEME_V->args); |
4384 | |
4128 | |
… | |
… | |
4512 | } |
4256 | } |
4513 | else |
4257 | else |
4514 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4258 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4515 | |
4259 | |
4516 | case OP_CHAR2INT: /* char->integer */ |
4260 | case OP_CHAR2INT: /* char->integer */ |
4517 | { |
4261 | 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 | |
4262 | |
4524 | case OP_INT2CHAR: /* integer->char */ |
4263 | 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)); |
4264 | s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4530 | } |
|
|
4531 | |
4265 | |
4532 | case OP_CHARUPCASE: |
4266 | case OP_CHARUPCASE: |
4533 | { |
4267 | { |
4534 | unsigned char c; |
|
|
4535 | |
|
|
4536 | c = (unsigned char) ivalue (car (SCHEME_V->args)); |
4268 | unsigned char c = ivalue (car (SCHEME_V->args)); |
4537 | c = toupper (c); |
4269 | c = toupper (c); |
4538 | s_return (mk_character (SCHEME_A_ (char) c)); |
4270 | s_return (mk_character (SCHEME_A_ c)); |
4539 | } |
4271 | } |
4540 | |
4272 | |
4541 | case OP_CHARDNCASE: |
4273 | case OP_CHARDNCASE: |
4542 | { |
4274 | { |
4543 | unsigned char c; |
|
|
4544 | |
|
|
4545 | c = (unsigned char) ivalue (car (SCHEME_V->args)); |
4275 | unsigned char c = ivalue (car (SCHEME_V->args)); |
4546 | c = tolower (c); |
4276 | c = tolower (c); |
4547 | s_return (mk_character (SCHEME_A_ (char) c)); |
4277 | s_return (mk_character (SCHEME_A_ c)); |
4548 | } |
4278 | } |
4549 | |
4279 | |
4550 | case OP_STR2SYM: /* string->symbol */ |
4280 | case OP_STR2SYM: /* string->symbol */ |
4551 | s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); |
4281 | s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); |
4552 | |
4282 | |
… | |
… | |
4564 | if (pf == 16 || pf == 10 || pf == 8 || pf == 2) |
4294 | if (pf == 16 || pf == 10 || pf == 8 || pf == 2) |
4565 | { |
4295 | { |
4566 | /* base is OK */ |
4296 | /* base is OK */ |
4567 | } |
4297 | } |
4568 | else |
4298 | else |
4569 | { |
|
|
4570 | pf = -1; |
4299 | pf = -1; |
4571 | } |
|
|
4572 | } |
4300 | } |
4573 | |
4301 | |
4574 | if (pf < 0) |
4302 | if (pf < 0) |
4575 | { |
|
|
4576 | Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); |
4303 | Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); |
4577 | } |
|
|
4578 | else if (*s == '#') /* no use of base! */ |
4304 | else if (*s == '#') /* no use of base! */ |
4579 | { |
|
|
4580 | s_return (mk_sharp_const (SCHEME_A_ s + 1)); |
4305 | s_return (mk_sharp_const (SCHEME_A_ s + 1)); |
4581 | } |
|
|
4582 | else |
4306 | else |
4583 | { |
4307 | { |
4584 | if (pf == 0 || pf == 10) |
4308 | if (pf == 0 || pf == 10) |
4585 | { |
|
|
4586 | s_return (mk_atom (SCHEME_A_ s)); |
4309 | s_return (mk_atom (SCHEME_A_ s)); |
4587 | } |
|
|
4588 | else |
4310 | else |
4589 | { |
4311 | { |
4590 | char *ep; |
4312 | char *ep; |
4591 | long iv = strtol (s, &ep, (int) pf); |
4313 | long iv = strtol (s, &ep, (int) pf); |
4592 | |
4314 | |
4593 | if (*ep == 0) |
4315 | if (*ep == 0) |
4594 | { |
|
|
4595 | s_return (mk_integer (SCHEME_A_ iv)); |
4316 | s_return (mk_integer (SCHEME_A_ iv)); |
4596 | } |
|
|
4597 | else |
4317 | else |
4598 | { |
|
|
4599 | s_return (S_F); |
4318 | s_return (S_F); |
4600 | } |
|
|
4601 | } |
4319 | } |
4602 | } |
4320 | } |
4603 | } |
4321 | } |
4604 | |
4322 | |
4605 | case OP_SYM2STR: /* symbol->string */ |
4323 | case OP_SYM2STR: /* symbol->string */ |
… | |
… | |
4622 | if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) |
4340 | if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) |
4623 | { |
4341 | { |
4624 | /* base is OK */ |
4342 | /* base is OK */ |
4625 | } |
4343 | } |
4626 | else |
4344 | else |
4627 | { |
|
|
4628 | pf = -1; |
4345 | pf = -1; |
4629 | } |
|
|
4630 | } |
4346 | } |
4631 | |
4347 | |
4632 | if (pf < 0) |
4348 | if (pf < 0) |
4633 | { |
|
|
4634 | Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); |
4349 | 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)) |
4350 | else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) |
4637 | { |
4351 | { |
4638 | char *p; |
4352 | char *p; |
4639 | int len; |
4353 | int len; |
4640 | |
4354 | |
4641 | atom2str (SCHEME_A_ x, (int) pf, &p, &len); |
4355 | atom2str (SCHEME_A_ x, pf, &p, &len); |
4642 | s_return (mk_counted_string (SCHEME_A_ p, len)); |
4356 | s_return (mk_counted_string (SCHEME_A_ p, len)); |
4643 | } |
4357 | } |
4644 | else |
4358 | else |
4645 | { |
|
|
4646 | Error_1 ("atom->string: not an atom:", x); |
4359 | Error_1 ("atom->string: not an atom:", x); |
4647 | } |
|
|
4648 | } |
4360 | } |
4649 | |
4361 | |
4650 | case OP_MKSTRING: /* make-string */ |
4362 | case OP_MKSTRING: /* make-string */ |
4651 | { |
4363 | { |
4652 | int fill = ' '; |
4364 | int fill = ' '; |
4653 | int len; |
4365 | int len; |
4654 | |
4366 | |
4655 | len = ivalue (car (SCHEME_V->args)); |
4367 | len = ivalue (car (SCHEME_V->args)); |
4656 | |
4368 | |
4657 | if (cdr (SCHEME_V->args) != NIL) |
4369 | if (cdr (SCHEME_V->args) != NIL) |
4658 | { |
|
|
4659 | fill = charvalue (cadr (SCHEME_V->args)); |
4370 | fill = charvalue (cadr (SCHEME_V->args)); |
4660 | } |
|
|
4661 | |
4371 | |
4662 | s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); |
4372 | s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); |
4663 | } |
4373 | } |
4664 | |
4374 | |
4665 | case OP_STRLEN: /* string-length */ |
4375 | case OP_STRLEN: /* string-length */ |
… | |
… | |
4673 | str = strvalue (car (SCHEME_V->args)); |
4383 | str = strvalue (car (SCHEME_V->args)); |
4674 | |
4384 | |
4675 | index = ivalue (cadr (SCHEME_V->args)); |
4385 | index = ivalue (cadr (SCHEME_V->args)); |
4676 | |
4386 | |
4677 | if (index >= strlength (car (SCHEME_V->args))) |
4387 | if (index >= strlength (car (SCHEME_V->args))) |
4678 | { |
|
|
4679 | Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); |
4388 | Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); |
4680 | } |
|
|
4681 | |
4389 | |
4682 | s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); |
4390 | s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); |
4683 | } |
4391 | } |
4684 | |
4392 | |
4685 | case OP_STRSET: /* string-set! */ |
4393 | case OP_STRSET: /* string-set! */ |
… | |
… | |
4687 | char *str; |
4395 | char *str; |
4688 | int index; |
4396 | int index; |
4689 | int c; |
4397 | int c; |
4690 | |
4398 | |
4691 | if (is_immutable (car (SCHEME_V->args))) |
4399 | if (is_immutable (car (SCHEME_V->args))) |
4692 | { |
|
|
4693 | Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); |
4400 | Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); |
4694 | } |
|
|
4695 | |
4401 | |
4696 | str = strvalue (car (SCHEME_V->args)); |
4402 | str = strvalue (car (SCHEME_V->args)); |
4697 | |
4403 | |
4698 | index = ivalue (cadr (SCHEME_V->args)); |
4404 | index = ivalue (cadr (SCHEME_V->args)); |
4699 | |
4405 | |
4700 | if (index >= strlength (car (SCHEME_V->args))) |
4406 | if (index >= strlength (car (SCHEME_V->args))) |
4701 | { |
|
|
4702 | Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); |
4407 | Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); |
4703 | } |
|
|
4704 | |
4408 | |
4705 | c = charvalue (caddr (SCHEME_V->args)); |
4409 | c = charvalue (caddr (SCHEME_V->args)); |
4706 | |
4410 | |
4707 | str[index] = (char) c; |
4411 | str[index] = (char) c; |
4708 | s_return (car (SCHEME_V->args)); |
4412 | s_return (car (SCHEME_V->args)); |
… | |
… | |
4715 | pointer newstr; |
4419 | pointer newstr; |
4716 | char *pos; |
4420 | char *pos; |
4717 | |
4421 | |
4718 | /* compute needed length for new string */ |
4422 | /* compute needed length for new string */ |
4719 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4423 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4720 | { |
|
|
4721 | len += strlength (car (x)); |
4424 | len += strlength (car (x)); |
4722 | } |
|
|
4723 | |
4425 | |
4724 | newstr = mk_empty_string (SCHEME_A_ len, ' '); |
4426 | newstr = mk_empty_string (SCHEME_A_ len, ' '); |
4725 | |
4427 | |
4726 | /* store the contents of the argument strings into the new string */ |
4428 | /* 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)) |
4429 | 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))); |
4430 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4730 | } |
|
|
4731 | |
4431 | |
4732 | s_return (newstr); |
4432 | s_return (newstr); |
4733 | } |
4433 | } |
4734 | |
4434 | |
4735 | case OP_SUBSTR: /* substring */ |
4435 | case OP_SUBSTR: /* substring */ |
… | |
… | |
4742 | str = strvalue (car (SCHEME_V->args)); |
4442 | str = strvalue (car (SCHEME_V->args)); |
4743 | |
4443 | |
4744 | index0 = ivalue (cadr (SCHEME_V->args)); |
4444 | index0 = ivalue (cadr (SCHEME_V->args)); |
4745 | |
4445 | |
4746 | if (index0 > strlength (car (SCHEME_V->args))) |
4446 | if (index0 > strlength (car (SCHEME_V->args))) |
4747 | { |
|
|
4748 | Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); |
4447 | Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); |
4749 | } |
|
|
4750 | |
4448 | |
4751 | if (cddr (SCHEME_V->args) != NIL) |
4449 | if (cddr (SCHEME_V->args) != NIL) |
4752 | { |
4450 | { |
4753 | index1 = ivalue (caddr (SCHEME_V->args)); |
4451 | index1 = ivalue (caddr (SCHEME_V->args)); |
4754 | |
4452 | |
4755 | if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) |
4453 | if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) |
4756 | { |
|
|
4757 | Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); |
4454 | Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); |
4758 | } |
|
|
4759 | } |
4455 | } |
4760 | else |
4456 | else |
4761 | { |
|
|
4762 | index1 = strlength (car (SCHEME_V->args)); |
4457 | index1 = strlength (car (SCHEME_V->args)); |
4763 | } |
|
|
4764 | |
4458 | |
4765 | len = index1 - index0; |
4459 | len = index1 - index0; |
4766 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4460 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4767 | memcpy (strvalue (x), str + index0, len); |
4461 | memcpy (strvalue (x), str + index0, len); |
4768 | strvalue (x)[len] = 0; |
4462 | strvalue (x)[len] = 0; |
… | |
… | |
4775 | int i; |
4469 | int i; |
4776 | pointer vec; |
4470 | pointer vec; |
4777 | int len = list_length (SCHEME_A_ SCHEME_V->args); |
4471 | int len = list_length (SCHEME_A_ SCHEME_V->args); |
4778 | |
4472 | |
4779 | if (len < 0) |
4473 | if (len < 0) |
4780 | { |
|
|
4781 | Error_1 ("vector: not a proper list:", SCHEME_V->args); |
4474 | Error_1 ("vector: not a proper list:", SCHEME_V->args); |
4782 | } |
|
|
4783 | |
4475 | |
4784 | vec = mk_vector (SCHEME_A_ len); |
4476 | vec = mk_vector (SCHEME_A_ len); |
4785 | |
4477 | |
4786 | #if USE_ERROR_CHECKING |
4478 | #if USE_ERROR_CHECKING |
4787 | if (SCHEME_V->no_memory) |
4479 | if (SCHEME_V->no_memory) |
… | |
… | |
4817 | |
4509 | |
4818 | s_return (vec); |
4510 | s_return (vec); |
4819 | } |
4511 | } |
4820 | |
4512 | |
4821 | case OP_VECLEN: /* vector-length */ |
4513 | case OP_VECLEN: /* vector-length */ |
4822 | s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4514 | s_return (mk_integer (SCHEME_A_ veclength (car (SCHEME_V->args)))); |
4823 | |
4515 | |
4824 | case OP_VECREF: /* vector-ref */ |
4516 | case OP_VECREF: /* vector-ref */ |
4825 | { |
4517 | { |
4826 | int index; |
4518 | int index; |
4827 | |
4519 | |
4828 | index = ivalue (cadr (SCHEME_V->args)); |
4520 | index = ivalue (cadr (SCHEME_V->args)); |
4829 | |
4521 | |
4830 | if (index >= ivalue (car (SCHEME_V->args))) |
4522 | if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) |
4831 | { |
|
|
4832 | Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); |
4523 | Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); |
4833 | } |
|
|
4834 | |
4524 | |
4835 | s_return (vector_elem (car (SCHEME_V->args), index)); |
4525 | s_return (vector_elem (car (SCHEME_V->args), index)); |
4836 | } |
4526 | } |
4837 | |
4527 | |
4838 | case OP_VECSET: /* vector-set! */ |
4528 | case OP_VECSET: /* vector-set! */ |
4839 | { |
4529 | { |
4840 | int index; |
4530 | int index; |
4841 | |
4531 | |
4842 | if (is_immutable (car (SCHEME_V->args))) |
4532 | if (is_immutable (car (SCHEME_V->args))) |
4843 | { |
|
|
4844 | Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); |
4533 | Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); |
4845 | } |
|
|
4846 | |
4534 | |
4847 | index = ivalue (cadr (SCHEME_V->args)); |
4535 | index = ivalue (cadr (SCHEME_V->args)); |
4848 | |
4536 | |
4849 | if (index >= ivalue (car (SCHEME_V->args))) |
4537 | if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) |
4850 | { |
|
|
4851 | Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); |
4538 | Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); |
4852 | } |
|
|
4853 | |
4539 | |
4854 | set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); |
4540 | set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); |
4855 | s_return (car (SCHEME_V->args)); |
4541 | s_return (car (SCHEME_V->args)); |
4856 | } |
4542 | } |
4857 | } |
4543 | } |
… | |
… | |
4967 | x = cdr (x); |
4653 | x = cdr (x); |
4968 | |
4654 | |
4969 | for (; x != NIL; x = cdr (x)) |
4655 | for (; x != NIL; x = cdr (x)) |
4970 | { |
4656 | { |
4971 | if (!comp_func (v, nvalue (car (x)))) |
4657 | if (!comp_func (v, nvalue (car (x)))) |
4972 | { |
|
|
4973 | s_retbool (0); |
4658 | s_retbool (0); |
4974 | } |
|
|
4975 | |
4659 | |
4976 | v = nvalue (car (x)); |
4660 | v = nvalue (car (x)); |
4977 | } |
4661 | } |
4978 | |
4662 | |
4979 | s_retbool (1); |
4663 | s_retbool (1); |
… | |
… | |
5072 | s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); |
4756 | s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); |
5073 | SCHEME_V->args = NIL; |
4757 | SCHEME_V->args = NIL; |
5074 | s_goto (OP_APPLY); |
4758 | s_goto (OP_APPLY); |
5075 | } |
4759 | } |
5076 | else |
4760 | else |
5077 | { |
|
|
5078 | s_return (SCHEME_V->code); |
4761 | s_return (SCHEME_V->code); |
5079 | } |
|
|
5080 | |
4762 | |
5081 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
4763 | case OP_SAVE_FORCED: /* Save forced value replacing promise */ |
5082 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
4764 | memcpy (SCHEME_V->code, SCHEME_V->value, sizeof (struct cell)); |
5083 | s_return (SCHEME_V->value); |
4765 | s_return (SCHEME_V->value); |
5084 | |
4766 | |
… | |
… | |
5148 | else |
4830 | else |
5149 | { |
4831 | { |
5150 | putstr (SCHEME_A_ "\n"); |
4832 | putstr (SCHEME_A_ "\n"); |
5151 | |
4833 | |
5152 | if (SCHEME_V->interactive_repl) |
4834 | if (SCHEME_V->interactive_repl) |
5153 | { |
|
|
5154 | s_goto (OP_T0LVL); |
4835 | s_goto (OP_T0LVL); |
5155 | } |
|
|
5156 | else |
4836 | else |
5157 | { |
|
|
5158 | return NIL; |
4837 | return NIL; |
5159 | } |
|
|
5160 | } |
4838 | } |
5161 | |
4839 | |
5162 | case OP_REVERSE: /* reverse */ |
4840 | case OP_REVERSE: /* reverse */ |
5163 | s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); |
4841 | s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); |
5164 | |
4842 | |
… | |
… | |
5187 | |
4865 | |
5188 | #if USE_PLIST |
4866 | #if USE_PLIST |
5189 | |
4867 | |
5190 | case OP_PUT: /* put */ |
4868 | case OP_PUT: /* put */ |
5191 | if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) |
4869 | if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) |
5192 | { |
|
|
5193 | Error_0 ("illegal use of put"); |
4870 | Error_0 ("illegal use of put"); |
5194 | } |
|
|
5195 | |
4871 | |
5196 | for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) |
4872 | for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) |
5197 | { |
4873 | { |
5198 | if (caar (x) == y) |
4874 | if (caar (x) == y) |
5199 | { |
|
|
5200 | break; |
4875 | break; |
5201 | } |
|
|
5202 | } |
4876 | } |
5203 | |
4877 | |
5204 | if (x != NIL) |
4878 | if (x != NIL) |
5205 | cdar (x) = caddr (SCHEME_V->args); |
4879 | cdar (x) = caddr (SCHEME_V->args); |
5206 | else |
4880 | else |
… | |
… | |
5283 | } |
4957 | } |
5284 | |
4958 | |
5285 | p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); |
4959 | p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); |
5286 | |
4960 | |
5287 | if (p == NIL) |
4961 | if (p == NIL) |
5288 | { |
|
|
5289 | s_return (S_F); |
4962 | s_return (S_F); |
5290 | } |
|
|
5291 | |
4963 | |
5292 | s_return (p); |
4964 | s_return (p); |
5293 | } |
4965 | } |
5294 | |
4966 | |
5295 | # if USE_STRING_PORTS |
4967 | # if USE_STRING_PORTS |
… | |
… | |
5313 | |
4985 | |
5314 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
4986 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
5315 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); |
4987 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); |
5316 | |
4988 | |
5317 | if (p == NIL) |
4989 | if (p == NIL) |
5318 | { |
|
|
5319 | s_return (S_F); |
4990 | s_return (S_F); |
5320 | } |
|
|
5321 | |
4991 | |
5322 | s_return (p); |
4992 | s_return (p); |
5323 | } |
4993 | } |
5324 | |
4994 | |
5325 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4995 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
… | |
… | |
5329 | if (car (SCHEME_V->args) == NIL) |
4999 | if (car (SCHEME_V->args) == NIL) |
5330 | { |
5000 | { |
5331 | p = port_from_scratch (SCHEME_A); |
5001 | p = port_from_scratch (SCHEME_A); |
5332 | |
5002 | |
5333 | if (p == NIL) |
5003 | if (p == NIL) |
5334 | { |
|
|
5335 | s_return (S_F); |
5004 | s_return (S_F); |
5336 | } |
|
|
5337 | } |
5005 | } |
5338 | else |
5006 | else |
5339 | { |
5007 | { |
5340 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
5008 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
5341 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); |
5009 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); |
5342 | |
5010 | |
5343 | if (p == NIL) |
5011 | if (p == NIL) |
5344 | { |
|
|
5345 | s_return (S_F); |
5012 | s_return (S_F); |
5346 | } |
|
|
5347 | } |
5013 | } |
5348 | |
5014 | |
5349 | s_return (p); |
5015 | s_return (p); |
5350 | } |
5016 | } |
5351 | |
5017 | |
… | |
… | |
5416 | { |
5082 | { |
5417 | /* ========== reading part ========== */ |
5083 | /* ========== reading part ========== */ |
5418 | #if USE_PORTS |
5084 | #if USE_PORTS |
5419 | case OP_READ: |
5085 | case OP_READ: |
5420 | if (!is_pair (SCHEME_V->args)) |
5086 | if (!is_pair (SCHEME_V->args)) |
5421 | { |
|
|
5422 | s_goto (OP_READ_INTERNAL); |
5087 | s_goto (OP_READ_INTERNAL); |
5423 | } |
|
|
5424 | |
5088 | |
5425 | if (!is_inport (car (SCHEME_V->args))) |
5089 | if (!is_inport (car (SCHEME_V->args))) |
5426 | { |
|
|
5427 | Error_1 ("read: not an input port:", car (SCHEME_V->args)); |
5090 | Error_1 ("read: not an input port:", car (SCHEME_V->args)); |
5428 | } |
|
|
5429 | |
5091 | |
5430 | if (car (SCHEME_V->args) == SCHEME_V->inport) |
5092 | if (car (SCHEME_V->args) == SCHEME_V->inport) |
5431 | { |
|
|
5432 | s_goto (OP_READ_INTERNAL); |
5093 | s_goto (OP_READ_INTERNAL); |
5433 | } |
|
|
5434 | |
5094 | |
5435 | x = SCHEME_V->inport; |
5095 | x = SCHEME_V->inport; |
5436 | SCHEME_V->inport = car (SCHEME_V->args); |
5096 | SCHEME_V->inport = car (SCHEME_V->args); |
5437 | x = cons (x, NIL); |
5097 | x = cons (x, NIL); |
5438 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
5098 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
… | |
… | |
5469 | { |
5129 | { |
5470 | pointer p = SCHEME_V->inport; |
5130 | pointer p = SCHEME_V->inport; |
5471 | int res; |
5131 | int res; |
5472 | |
5132 | |
5473 | if (is_pair (SCHEME_V->args)) |
5133 | if (is_pair (SCHEME_V->args)) |
5474 | { |
|
|
5475 | p = car (SCHEME_V->args); |
5134 | p = car (SCHEME_V->args); |
5476 | } |
|
|
5477 | |
5135 | |
5478 | res = p->object.port->kind & port_string; |
5136 | res = p->object.port->kind & port_string; |
5479 | |
5137 | |
5480 | s_retbool (res); |
5138 | s_retbool (res); |
5481 | } |
5139 | } |
… | |
… | |
5496 | s_return (S_EOF); |
5154 | s_return (S_EOF); |
5497 | /* NOTREACHED */ |
5155 | /* NOTREACHED */ |
5498 | |
5156 | |
5499 | case TOK_VEC: |
5157 | case TOK_VEC: |
5500 | s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); |
5158 | s_save (SCHEME_A_ OP_RDVEC, NIL, NIL); |
5501 | |
|
|
5502 | /* fall through */ |
5159 | /* fall through */ |
|
|
5160 | |
5503 | case TOK_LPAREN: |
5161 | case TOK_LPAREN: |
5504 | SCHEME_V->tok = token (SCHEME_A); |
5162 | SCHEME_V->tok = token (SCHEME_A); |
5505 | |
5163 | |
5506 | if (SCHEME_V->tok == TOK_RPAREN) |
5164 | if (SCHEME_V->tok == TOK_RPAREN) |
5507 | s_return (NIL); |
5165 | s_return (NIL); |
… | |
… | |
5527 | s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); |
5185 | s_save (SCHEME_A_ OP_RDQQUOTEVEC, NIL, NIL); |
5528 | SCHEME_V->tok = TOK_LPAREN; |
5186 | SCHEME_V->tok = TOK_LPAREN; |
5529 | s_goto (OP_RDSEXPR); |
5187 | s_goto (OP_RDSEXPR); |
5530 | } |
5188 | } |
5531 | else |
5189 | else |
5532 | { |
|
|
5533 | s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); |
5190 | s_save (SCHEME_A_ OP_RDQQUOTE, NIL, NIL); |
5534 | } |
|
|
5535 | |
5191 | |
5536 | s_goto (OP_RDSEXPR); |
5192 | s_goto (OP_RDSEXPR); |
5537 | |
5193 | |
5538 | case TOK_COMMA: |
5194 | case TOK_COMMA: |
5539 | s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); |
5195 | s_save (SCHEME_A_ OP_RDUNQUOTE, NIL, NIL); |
… | |
… | |
5581 | } |
5237 | } |
5582 | |
5238 | |
5583 | break; |
5239 | break; |
5584 | |
5240 | |
5585 | case OP_RDLIST: |
5241 | case OP_RDLIST: |
5586 | { |
|
|
5587 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
5242 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
5588 | SCHEME_V->tok = token (SCHEME_A); |
5243 | SCHEME_V->tok = token (SCHEME_A); |
5589 | |
5244 | |
5590 | if (SCHEME_V->tok == TOK_EOF) |
5245 | switch (SCHEME_V->tok) |
|
|
5246 | { |
|
|
5247 | case TOK_EOF: |
5591 | s_return (S_EOF); |
5248 | s_return (S_EOF); |
5592 | else if (SCHEME_V->tok == TOK_RPAREN) |
5249 | |
|
|
5250 | case TOK_RPAREN: |
5593 | { |
5251 | { |
5594 | int c = inchar (SCHEME_A); |
5252 | int c = inchar (SCHEME_A); |
5595 | |
5253 | |
5596 | if (c != '\n') |
5254 | if (c != '\n') |
5597 | backchar (SCHEME_A_ c); |
5255 | backchar (SCHEME_A_ c); |
5598 | |
|
|
5599 | #if SHOW_ERROR_LINE |
5256 | #if SHOW_ERROR_LINE |
5600 | else if (SCHEME_V->load_stack[SCHEME_V->file_i].kind & port_file) |
5257 | 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++; |
5258 | SCHEME_V->load_stack[SCHEME_V->file_i].rep.stdio.curr_line++; |
5602 | |
|
|
5603 | #endif |
5259 | #endif |
|
|
5260 | |
5604 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5261 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5605 | s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args)); |
5262 | s_return (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args)); |
5606 | } |
5263 | } |
5607 | else if (SCHEME_V->tok == TOK_DOT) |
5264 | |
5608 | { |
5265 | case TOK_DOT: |
5609 | s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL); |
5266 | s_save (SCHEME_A_ OP_RDDOT, SCHEME_V->args, NIL); |
5610 | SCHEME_V->tok = token (SCHEME_A); |
5267 | SCHEME_V->tok = token (SCHEME_A); |
5611 | s_goto (OP_RDSEXPR); |
5268 | s_goto (OP_RDSEXPR); |
5612 | } |
5269 | |
5613 | else |
5270 | default: |
5614 | { |
|
|
5615 | s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL);; |
5271 | s_save (SCHEME_A_ OP_RDLIST, SCHEME_V->args, NIL); |
5616 | s_goto (OP_RDSEXPR); |
5272 | s_goto (OP_RDSEXPR); |
5617 | } |
5273 | } |
5618 | } |
|
|
5619 | |
5274 | |
5620 | case OP_RDDOT: |
5275 | case OP_RDDOT: |
5621 | if (token (SCHEME_A) != TOK_RPAREN) |
5276 | if (token (SCHEME_A) != TOK_RPAREN) |
5622 | Error_0 ("syntax error: illegal dot expression"); |
5277 | Error_0 ("syntax error: illegal dot expression"); |
5623 | else |
5278 | |
5624 | { |
|
|
5625 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5279 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5626 | s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); |
5280 | s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); |
5627 | } |
|
|
5628 | |
5281 | |
5629 | case OP_RDQUOTE: |
5282 | case OP_RDQUOTE: |
5630 | s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); |
5283 | s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); |
5631 | |
5284 | |
5632 | case OP_RDQQUOTE: |
5285 | case OP_RDQQUOTE: |
… | |
… | |
5732 | |
5385 | |
5733 | case OP_PVECFROM: |
5386 | case OP_PVECFROM: |
5734 | { |
5387 | { |
5735 | int i = ivalue_unchecked (cdr (SCHEME_V->args)); |
5388 | int i = ivalue_unchecked (cdr (SCHEME_V->args)); |
5736 | pointer vec = car (SCHEME_V->args); |
5389 | pointer vec = car (SCHEME_V->args); |
5737 | int len = ivalue_unchecked (vec); |
5390 | int len = veclength (vec); |
5738 | |
5391 | |
5739 | if (i == len) |
5392 | if (i == len) |
5740 | { |
5393 | { |
5741 | putstr (SCHEME_A_ ")"); |
5394 | putstr (SCHEME_A_ ")"); |
5742 | s_return (S_T); |
5395 | s_return (S_T); |
… | |
… | |
5898 | { |
5551 | { |
5899 | int n = procnum (x); |
5552 | int n = procnum (x); |
5900 | const char *name = dispatch_table[n].name; |
5553 | const char *name = dispatch_table[n].name; |
5901 | |
5554 | |
5902 | if (name == 0) |
5555 | if (name == 0) |
5903 | { |
|
|
5904 | name = "ILLEGAL!"; |
5556 | name = "ILLEGAL!"; |
5905 | } |
|
|
5906 | |
5557 | |
5907 | return name; |
5558 | return name; |
5908 | } |
5559 | } |
5909 | |
5560 | |
5910 | /* kernel of this interpreter */ |
5561 | /* kernel of this interpreter */ |
… | |
… | |
5915 | |
5566 | |
5916 | for (;;) |
5567 | for (;;) |
5917 | { |
5568 | { |
5918 | op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5569 | op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5919 | |
5570 | |
|
|
5571 | #if USE_ERROR_CHECKING |
5920 | if (pcd->name != 0) /* if built-in function, check arguments */ |
5572 | if (pcd->name) /* if built-in function, check arguments */ |
5921 | { |
5573 | { |
|
|
5574 | int ok = 1; |
5922 | char msg[STRBUFFSIZE]; |
5575 | char msg[STRBUFFSIZE]; |
5923 | int ok = 1; |
|
|
5924 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5576 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5925 | |
5577 | |
5926 | #if USE_ERROR_CHECKING |
|
|
5927 | /* Check number of arguments */ |
5578 | /* Check number of arguments */ |
5928 | if (n < pcd->min_arity) |
5579 | if (n < pcd->min_arity) |
5929 | { |
5580 | { |
5930 | ok = 0; |
5581 | ok = 0; |
5931 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5582 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
… | |
… | |
5936 | { |
5587 | { |
5937 | ok = 0; |
5588 | ok = 0; |
5938 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5589 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5939 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
5590 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
5940 | } |
5591 | } |
5941 | #endif |
|
|
5942 | |
5592 | |
5943 | if (ok) |
5593 | if (ok) |
5944 | { |
5594 | { |
5945 | if (pcd->arg_tests_encoding != 0) |
5595 | if (pcd->arg_tests_encoding) |
5946 | { |
5596 | { |
5947 | int i = 0; |
5597 | int i = 0; |
5948 | int j; |
5598 | int j; |
5949 | const char *t = pcd->arg_tests_encoding; |
5599 | const char *t = pcd->arg_tests_encoding; |
5950 | pointer arglist = SCHEME_V->args; |
5600 | pointer arglist = SCHEME_V->args; |
… | |
… | |
5965 | if (!tests[j].fct (arg)) |
5615 | if (!tests[j].fct (arg)) |
5966 | break; |
5616 | break; |
5967 | } |
5617 | } |
5968 | |
5618 | |
5969 | if (t[1] != 0) /* last test is replicated as necessary */ |
5619 | if (t[1] != 0) /* last test is replicated as necessary */ |
5970 | { |
|
|
5971 | t++; |
5620 | t++; |
5972 | } |
|
|
5973 | |
5621 | |
5974 | arglist = cdr (arglist); |
5622 | arglist = cdr (arglist); |
5975 | i++; |
5623 | i++; |
5976 | } |
5624 | } |
5977 | while (i < n); |
5625 | while (i < n); |
5978 | |
5626 | |
5979 | #if USE_ERROR_CHECKING |
|
|
5980 | if (i < n) |
5627 | if (i < n) |
5981 | { |
5628 | { |
5982 | ok = 0; |
5629 | ok = 0; |
5983 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); |
5630 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); |
5984 | } |
5631 | } |
5985 | #endif |
|
|
5986 | } |
5632 | } |
5987 | } |
5633 | } |
5988 | |
5634 | |
5989 | if (!ok) |
5635 | if (!ok) |
5990 | { |
5636 | { |
… | |
… | |
5992 | return; |
5638 | return; |
5993 | |
5639 | |
5994 | pcd = dispatch_table + SCHEME_V->op; |
5640 | pcd = dispatch_table + SCHEME_V->op; |
5995 | } |
5641 | } |
5996 | } |
5642 | } |
|
|
5643 | #endif |
5997 | |
5644 | |
5998 | ok_to_freely_gc (SCHEME_A); |
5645 | ok_to_freely_gc (SCHEME_A); |
5999 | |
5646 | |
6000 | if (pcd->func (SCHEME_A_ (enum scheme_opcodes) SCHEME_V->op) == NIL) |
5647 | if (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL) |
6001 | return; |
5648 | return; |
6002 | |
5649 | |
6003 | #if USE_ERROR_CHECKING |
5650 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
6004 | if (SCHEME_V->no_memory) |
|
|
6005 | { |
5651 | { |
6006 | xwrstr ("No memory!\n"); |
5652 | xwrstr ("No memory!\n"); |
6007 | return; |
5653 | return; |
6008 | } |
5654 | } |
6009 | #endif |
|
|
6010 | } |
5655 | } |
6011 | } |
5656 | } |
6012 | |
5657 | |
6013 | /* ========== Initialization of internal keywords ========== */ |
5658 | /* ========== Initialization of internal keywords ========== */ |
6014 | |
5659 | |
6015 | static void |
5660 | static void |
6016 | assign_syntax (SCHEME_P_ char *name) |
5661 | assign_syntax (SCHEME_P_ const char *name) |
6017 | { |
5662 | { |
6018 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
5663 | pointer x = oblist_add_by_name (SCHEME_A_ name); |
6019 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
5664 | set_typeflag (x, typeflag (x) | T_SYNTAX); |
6020 | } |
5665 | } |
6021 | |
5666 | |
6022 | static void |
5667 | static void |
6023 | assign_proc (SCHEME_P_ enum scheme_opcodes op, char *name) |
5668 | assign_proc (SCHEME_P_ enum scheme_opcodes op, const char *name) |
6024 | { |
5669 | { |
6025 | pointer x = mk_symbol (SCHEME_A_ name); |
5670 | pointer x = mk_symbol (SCHEME_A_ name); |
6026 | pointer y = mk_proc (SCHEME_A_ op); |
5671 | pointer y = mk_proc (SCHEME_A_ op); |
6027 | new_slot_in_env (SCHEME_A_ x, y); |
5672 | new_slot_in_env (SCHEME_A_ x, y); |
6028 | } |
5673 | } |
… | |
… | |
6030 | static pointer |
5675 | static pointer |
6031 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
5676 | mk_proc (SCHEME_P_ enum scheme_opcodes op) |
6032 | { |
5677 | { |
6033 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
5678 | pointer y = get_cell (SCHEME_A_ NIL, NIL); |
6034 | set_typeflag (y, (T_PROC | T_ATOM)); |
5679 | set_typeflag (y, (T_PROC | T_ATOM)); |
6035 | ivalue_unchecked (y) = (long) op; |
5680 | ivalue_unchecked (y) = op; |
6036 | set_num_integer (y); |
5681 | set_num_integer (y); |
6037 | return y; |
5682 | return y; |
6038 | } |
5683 | } |
6039 | |
5684 | |
6040 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
5685 | /* Hard-coded for the given keywords. Remember to rewrite if more are added! */ |
… | |
… | |
6158 | } |
5803 | } |
6159 | |
5804 | |
6160 | SCHEME_V->gc_verbose = 0; |
5805 | SCHEME_V->gc_verbose = 0; |
6161 | dump_stack_initialize (SCHEME_A); |
5806 | dump_stack_initialize (SCHEME_A); |
6162 | SCHEME_V->code = NIL; |
5807 | SCHEME_V->code = NIL; |
|
|
5808 | SCHEME_V->args = NIL; |
|
|
5809 | SCHEME_V->envir = NIL; |
6163 | SCHEME_V->tracing = 0; |
5810 | SCHEME_V->tracing = 0; |
6164 | |
5811 | |
6165 | /* init NIL */ |
5812 | /* init NIL */ |
6166 | set_typeflag (NIL, T_ATOM | MARK); |
5813 | set_typeflag (NIL, T_ATOM | T_MARK); |
6167 | set_car (NIL, NIL); |
5814 | set_car (NIL, NIL); |
6168 | set_cdr (NIL, NIL); |
5815 | set_cdr (NIL, NIL); |
6169 | /* init T */ |
5816 | /* init T */ |
6170 | set_typeflag (S_T, T_ATOM | MARK); |
5817 | set_typeflag (S_T, T_ATOM | T_MARK); |
6171 | set_car (S_T, S_T); |
5818 | set_car (S_T, S_T); |
6172 | set_cdr (S_T, S_T); |
5819 | set_cdr (S_T, S_T); |
6173 | /* init F */ |
5820 | /* init F */ |
6174 | set_typeflag (S_F, T_ATOM | MARK); |
5821 | set_typeflag (S_F, T_ATOM | T_MARK); |
6175 | set_car (S_F, S_F); |
5822 | set_car (S_F, S_F); |
6176 | set_cdr (S_F, S_F); |
5823 | set_cdr (S_F, S_F); |
|
|
5824 | /* init EOF_OBJ */ |
|
|
5825 | set_typeflag (S_EOF, T_ATOM | T_MARK); |
|
|
5826 | set_car (S_EOF, S_EOF); |
|
|
5827 | set_cdr (S_EOF, S_EOF); |
6177 | /* init sink */ |
5828 | /* init sink */ |
6178 | set_typeflag (S_SINK, T_PAIR | MARK); |
5829 | set_typeflag (S_SINK, T_PAIR | T_MARK); |
6179 | set_car (S_SINK, NIL); |
5830 | set_car (S_SINK, NIL); |
|
|
5831 | |
6180 | /* init c_nest */ |
5832 | /* init c_nest */ |
6181 | SCHEME_V->c_nest = NIL; |
5833 | SCHEME_V->c_nest = NIL; |
6182 | |
5834 | |
6183 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
5835 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
6184 | /* init global_env */ |
5836 | /* init global_env */ |
… | |
… | |
6186 | SCHEME_V->global_env = SCHEME_V->envir; |
5838 | SCHEME_V->global_env = SCHEME_V->envir; |
6187 | /* init else */ |
5839 | /* init else */ |
6188 | x = mk_symbol (SCHEME_A_ "else"); |
5840 | x = mk_symbol (SCHEME_A_ "else"); |
6189 | new_slot_in_env (SCHEME_A_ x, S_T); |
5841 | new_slot_in_env (SCHEME_A_ x, S_T); |
6190 | |
5842 | |
6191 | assign_syntax (SCHEME_A_ "lambda"); |
5843 | { |
6192 | assign_syntax (SCHEME_A_ "quote"); |
5844 | static const char *syntax_names[] = { |
6193 | assign_syntax (SCHEME_A_ "define"); |
5845 | "lambda", "quote", "define", "if", "begin", "set!", |
6194 | assign_syntax (SCHEME_A_ "if"); |
5846 | "let", "let*", "letrec", "cond", "delay", "and", |
6195 | assign_syntax (SCHEME_A_ "begin"); |
5847 | "or", "cons-stream", "macro", "case" |
6196 | assign_syntax (SCHEME_A_ "set!"); |
5848 | }; |
6197 | assign_syntax (SCHEME_A_ "let"); |
5849 | |
6198 | assign_syntax (SCHEME_A_ "let*"); |
5850 | 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"); |
5851 | assign_syntax (SCHEME_A_ syntax_names[i]); |
6203 | assign_syntax (SCHEME_A_ "or"); |
5852 | } |
6204 | assign_syntax (SCHEME_A_ "cons-stream"); |
|
|
6205 | assign_syntax (SCHEME_A_ "macro"); |
|
|
6206 | assign_syntax (SCHEME_A_ "case"); |
|
|
6207 | |
5853 | |
6208 | for (i = 0; i < n; i++) |
5854 | for (i = 0; i < n; i++) |
6209 | { |
|
|
6210 | if (dispatch_table[i].name != 0) |
5855 | if (dispatch_table[i].name != 0) |
6211 | { |
|
|
6212 | assign_proc (SCHEME_A_ (enum scheme_opcodes) i, dispatch_table[i].name); |
5856 | assign_proc (SCHEME_A_ i, dispatch_table[i].name); |
6213 | } |
|
|
6214 | } |
|
|
6215 | |
5857 | |
6216 | /* initialization of global pointers to special symbols */ |
5858 | /* initialization of global pointers to special symbols */ |
6217 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
5859 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
6218 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
5860 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
6219 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
5861 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
6220 | SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); |
5862 | SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); |
6221 | SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); |
5863 | SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); |
6222 | SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); |
5864 | SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); |
6223 | SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); |
5865 | SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); |
6224 | SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); |
5866 | SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); |
6225 | SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); |
5867 | SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); |
6226 | SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); |
5868 | SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); |
6227 | |
5869 | |
6228 | return !SCHEME_V->no_memory; |
5870 | return !SCHEME_V->no_memory; |
6229 | } |
5871 | } |
6230 | |
5872 | |
… | |
… | |
6385 | pointer x; |
6027 | pointer x; |
6386 | |
6028 | |
6387 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6029 | x = find_slot_in_env (SCHEME_A_ envir, symbol, 0); |
6388 | |
6030 | |
6389 | if (x != NIL) |
6031 | if (x != NIL) |
6390 | { |
|
|
6391 | set_slot_in_env (SCHEME_A_ x, value); |
6032 | set_slot_in_env (SCHEME_A_ x, value); |
6392 | } |
|
|
6393 | else |
6033 | else |
6394 | { |
|
|
6395 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6034 | new_slot_spec_in_env (SCHEME_A_ envir, symbol, value); |
6396 | } |
|
|
6397 | } |
6035 | } |
6398 | |
6036 | |
6399 | #if !STANDALONE |
6037 | #if !STANDALONE |
|
|
6038 | |
6400 | void |
6039 | void |
6401 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6040 | scheme_register_foreign_func (scheme * sc, scheme_registerable * sr) |
6402 | { |
6041 | { |
6403 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
6042 | scheme_define (SCHEME_A_ SCHEME_V->global_env, mk_symbol (SCHEME_A_ sr->name), mk_foreign_func (SCHEME_A_ sr->f)); |
6404 | } |
6043 | } |
… | |
… | |
6407 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
6046 | scheme_register_foreign_func_list (scheme * sc, scheme_registerable * list, int count) |
6408 | { |
6047 | { |
6409 | int i; |
6048 | int i; |
6410 | |
6049 | |
6411 | for (i = 0; i < count; i++) |
6050 | for (i = 0; i < count; i++) |
6412 | { |
|
|
6413 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6051 | scheme_register_foreign_func (SCHEME_A_ list + i); |
6414 | } |
|
|
6415 | } |
6052 | } |
6416 | |
6053 | |
6417 | pointer |
6054 | pointer |
6418 | scheme_apply0 (SCHEME_P_ const char *procname) |
6055 | scheme_apply0 (SCHEME_P_ const char *procname) |
6419 | { |
6056 | { |
… | |
… | |
6476 | SCHEME_V->interactive_repl = old_repl; |
6113 | SCHEME_V->interactive_repl = old_repl; |
6477 | restore_from_C_call (SCHEME_A); |
6114 | restore_from_C_call (SCHEME_A); |
6478 | return SCHEME_V->value; |
6115 | return SCHEME_V->value; |
6479 | } |
6116 | } |
6480 | |
6117 | |
6481 | |
|
|
6482 | #endif |
6118 | #endif |
6483 | |
6119 | |
6484 | /* ========== Main ========== */ |
6120 | /* ========== Main ========== */ |
6485 | |
6121 | |
6486 | #if STANDALONE |
6122 | #if STANDALONE |
6487 | |
6123 | |
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 |
6124 | int |
6505 | main (int argc, char **argv) |
6125 | main (int argc, char **argv) |
6506 | { |
6126 | { |
6507 | # endif |
|
|
6508 | # if USE_MULTIPLICITY |
6127 | # if USE_MULTIPLICITY |
6509 | scheme ssc; |
6128 | scheme ssc; |
6510 | scheme *const sc = &ssc; |
6129 | scheme *const SCHEME_V = &ssc; |
6511 | # else |
6130 | # else |
6512 | # endif |
6131 | # endif |
6513 | int fin; |
6132 | int fin; |
6514 | char *file_name = InitFile; |
6133 | char *file_name = InitFile; |
6515 | int retcode; |
6134 | int retcode; |
6516 | int isfile = 1; |
6135 | int isfile = 1; |
6517 | |
|
|
6518 | if (argc == 1) |
|
|
6519 | xwrstr (banner); |
|
|
6520 | |
6136 | |
6521 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6137 | if (argc == 2 && strcmp (argv[1], "-?") == 0) |
6522 | { |
6138 | { |
6523 | xwrstr ("Usage: tinyscheme -?\n"); |
6139 | xwrstr ("Usage: tinyscheme -?\n"); |
6524 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
6140 | xwrstr ("or: tinyscheme [<file1> <file2> ...]\n"); |
… | |
… | |
6547 | if (access (file_name, 0) != 0) |
6163 | if (access (file_name, 0) != 0) |
6548 | { |
6164 | { |
6549 | char *p = getenv ("TINYSCHEMEINIT"); |
6165 | char *p = getenv ("TINYSCHEMEINIT"); |
6550 | |
6166 | |
6551 | if (p != 0) |
6167 | if (p != 0) |
6552 | { |
|
|
6553 | file_name = p; |
6168 | file_name = p; |
6554 | } |
|
|
6555 | } |
6169 | } |
6556 | #endif |
6170 | #endif |
6557 | |
6171 | |
6558 | do |
6172 | do |
6559 | { |
6173 | { |