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. |
… | |
… | |
60 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
65 | #define S_T (&SCHEME_V->xT) //TODO: magic ptr value? |
61 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
66 | #define S_F (&SCHEME_V->xF) //TODO: magic ptr value? |
62 | #define S_SINK (&SCHEME_V->xsink) |
67 | #define S_SINK (&SCHEME_V->xsink) |
63 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
68 | #define S_EOF (&SCHEME_V->xEOF_OBJ) |
64 | |
69 | |
|
|
70 | /* should use libecb */ |
|
|
71 | #if __GNUC__ >= 4 |
|
|
72 | # define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
73 | # define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
|
|
74 | # define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
|
|
75 | #else |
|
|
76 | # define ecb_expect_false(expr) !!(expr) |
|
|
77 | # define ecb_expect_true(expr) !!(expr) |
|
|
78 | #endif |
|
|
79 | |
65 | #if !USE_MULTIPLICITY |
80 | #if !USE_MULTIPLICITY |
66 | static scheme sc; |
81 | static scheme sc; |
67 | #endif |
82 | #endif |
68 | |
83 | |
69 | static void |
84 | static void |
… | |
… | |
128 | c += 'a' - 'A'; |
143 | c += 'a' - 'A'; |
129 | |
144 | |
130 | return c; |
145 | return c; |
131 | } |
146 | } |
132 | |
147 | |
|
|
148 | static int |
|
|
149 | xisdigit (char c) |
|
|
150 | { |
|
|
151 | return c >= '0' && c <= '9'; |
|
|
152 | } |
|
|
153 | |
|
|
154 | #define toupper(c) xtoupper (c) |
|
|
155 | #define tolower(c) xtolower (c) |
|
|
156 | #define isdigit(c) xisdigit (c) |
|
|
157 | |
133 | #if USE_STRLWR |
158 | #if USE_STRLWR |
134 | static const char * |
159 | static const char * |
135 | strlwr (char *s) |
160 | strlwr (char *s) |
136 | { |
161 | { |
137 | const char *p = s; |
162 | const char *p = s; |
… | |
… | |
146 | } |
171 | } |
147 | #endif |
172 | #endif |
148 | |
173 | |
149 | #define stricmp(a,b) strcmp (a, b) |
174 | #define stricmp(a,b) strcmp (a, b) |
150 | #define strlwr(s) (s) |
175 | #define strlwr(s) (s) |
151 | #define toupper(c) xtoupper(c) |
|
|
152 | #define tolower(c) xtolower(c) |
|
|
153 | |
176 | |
154 | #ifndef prompt |
177 | #ifndef prompt |
155 | # define prompt "ts> " |
178 | # define prompt "ts> " |
156 | #endif |
179 | #endif |
157 | |
180 | |
… | |
… | |
188 | #define T_SYNTAX 0x0010 |
211 | #define T_SYNTAX 0x0010 |
189 | #define T_IMMUTABLE 0x0020 |
212 | #define T_IMMUTABLE 0x0020 |
190 | #define T_ATOM 0x0040 /* only for gc */ |
213 | #define T_ATOM 0x0040 /* only for gc */ |
191 | #define T_MARK 0x0080 /* only for gc */ |
214 | #define T_MARK 0x0080 /* only for gc */ |
192 | |
215 | |
193 | static num num_add (num a, num b); |
216 | enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV }; |
194 | static num num_mul (num a, num b); |
217 | |
195 | static num num_div (num a, num b); |
218 | static num num_op (enum num_op op, num a, num b); |
196 | static num num_intdiv (num a, num b); |
219 | static num num_intdiv (num a, num b); |
197 | static num num_sub (num a, num b); |
|
|
198 | static num num_rem (num a, num b); |
220 | static num num_rem (num a, num b); |
199 | static num num_mod (num a, num b); |
221 | static num num_mod (num a, num b); |
200 | static int num_eq (num a, num b); |
|
|
201 | static int num_gt (num a, num b); |
|
|
202 | static int num_ge (num a, num b); |
|
|
203 | static int num_lt (num a, num b); |
|
|
204 | static int num_le (num a, num b); |
|
|
205 | |
222 | |
206 | #if USE_MATH |
223 | #if USE_MATH |
207 | static double round_per_R5RS (double x); |
224 | static double round_per_R5RS (double x); |
208 | #endif |
225 | #endif |
209 | static int is_zero_rvalue (RVALUE x); |
226 | static int is_zero_rvalue (RVALUE x); |
… | |
… | |
230 | |
247 | |
231 | #define strvalue(p) ((p)->object.string.svalue) |
248 | #define strvalue(p) ((p)->object.string.svalue) |
232 | #define strlength(p) ((p)->object.string.length) |
249 | #define strlength(p) ((p)->object.string.length) |
233 | |
250 | |
234 | INTERFACE int is_list (SCHEME_P_ pointer p); |
251 | INTERFACE int is_list (SCHEME_P_ pointer p); |
|
|
252 | |
235 | INTERFACE INLINE int |
253 | INTERFACE INLINE int |
236 | is_vector (pointer p) |
254 | is_vector (pointer p) |
237 | { |
255 | { |
238 | return type (p) == T_VECTOR; |
256 | return type (p) == T_VECTOR; |
239 | } |
257 | } |
240 | |
258 | |
|
|
259 | #define vecvalue(p) ((p)->object.vector.vvalue) |
|
|
260 | #define veclength(p) ((p)->object.vector.length) |
241 | INTERFACE void fill_vector (pointer vec, pointer obj); |
261 | INTERFACE void fill_vector (pointer vec, pointer obj); |
242 | INTERFACE uint32_t vector_length (pointer vec); |
262 | INTERFACE uint32_t vector_length (pointer vec); |
243 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
263 | INTERFACE pointer vector_elem (pointer vec, uint32_t ielem); |
244 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
264 | INTERFACE void set_vector_elem (pointer vec, uint32_t ielem, pointer a); |
245 | |
265 | |
… | |
… | |
314 | { |
334 | { |
315 | return num_get_rvalue (p->object.number); |
335 | return num_get_rvalue (p->object.number); |
316 | } |
336 | } |
317 | |
337 | |
318 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
338 | #define ivalue_unchecked(p) ((p)->object.number.value.ivalue) |
319 | #if USE_FLOAT |
339 | #if USE_REAL |
320 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
340 | # define rvalue_unchecked(p) ((p)->object.number.value.rvalue) |
321 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
341 | # define set_num_integer(p) (p)->object.number.is_fixnum=1; |
322 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
342 | # define set_num_real(p) (p)->object.number.is_fixnum=0; |
323 | #else |
343 | #else |
324 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
344 | # define rvalue_unchecked(p) ((p)->object.number.value.ivalue) |
… | |
… | |
354 | { |
374 | { |
355 | return type (p) == T_PAIR; |
375 | return type (p) == T_PAIR; |
356 | } |
376 | } |
357 | |
377 | |
358 | #define car(p) ((p)->object.cons.car + 0) |
378 | #define car(p) ((p)->object.cons.car + 0) |
359 | #define cdr(p) ((p)->object.cons.cdr) /* find_consecutive_cells uses &cdr */ |
379 | #define cdr(p) ((p)->object.cons.cdr + 0) |
360 | |
380 | |
361 | #define caar(p) car (car (p)) |
381 | static pointer caar (pointer p) { return car (car (p)); } |
362 | #define cadr(p) car (cdr (p)) |
382 | static pointer cadr (pointer p) { return car (cdr (p)); } |
363 | #define cdar(p) cdr (car (p)) |
383 | static pointer cdar (pointer p) { return cdr (car (p)); } |
364 | #define cddr(p) cdr (cdr (p)) |
384 | static pointer cddr (pointer p) { return cdr (cdr (p)); } |
365 | |
385 | |
366 | #define cadar(p) car (cdr (car (p))) |
386 | static pointer cadar (pointer p) { return car (cdr (car (p))); } |
367 | #define caddr(p) car (cdr (cdr (p))) |
387 | static pointer caddr (pointer p) { return car (cdr (cdr (p))); } |
368 | #define cdaar(p) cdr (car (car (p))) |
388 | static pointer cdaar (pointer p) { return cdr (car (car (p))); } |
369 | |
389 | |
370 | INTERFACE void |
390 | INTERFACE void |
371 | set_car (pointer p, pointer q) |
391 | set_car (pointer p, pointer q) |
372 | { |
392 | { |
373 | p->object.cons.car = q; |
393 | p->object.cons.car = q; |
… | |
… | |
486 | return type (p) == T_ENVIRONMENT; |
506 | return type (p) == T_ENVIRONMENT; |
487 | } |
507 | } |
488 | |
508 | |
489 | #define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) |
509 | #define setenvironment(p) set_typeflag ((p), T_ENVIRONMENT) |
490 | |
510 | |
491 | #define is_atom1(p) (TYPESET_ATOM & (1U << type (p))) |
|
|
492 | #define is_atom(p) (typeflag (p) & T_ATOM) |
511 | #define is_atom(p) (typeflag (p) & T_ATOM) |
493 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
512 | #define setatom(p) set_typeflag ((p), typeflag (p) | T_ATOM) |
494 | #define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) |
513 | #define clratom(p) set_typeflag ((p), typeflag (p) & ~T_ATOM) |
495 | |
514 | |
496 | #define is_mark(p) (typeflag (p) & T_MARK) |
515 | #define is_mark(p) (typeflag (p) & T_MARK) |
497 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
516 | #define setmark(p) set_typeflag ((p), typeflag (p) | T_MARK) |
498 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
517 | #define clrmark(p) set_typeflag ((p), typeflag (p) & ~T_MARK) |
499 | |
|
|
500 | #if 0 |
|
|
501 | static int |
|
|
502 | is_atom(pointer p) |
|
|
503 | { |
|
|
504 | if (!is_atom1(p) != !is_atom2(p)) |
|
|
505 | printf ("atoms disagree %x\n", typeflag(p)); |
|
|
506 | |
|
|
507 | return is_atom2(p); |
|
|
508 | } |
|
|
509 | #endif |
|
|
510 | |
518 | |
511 | INTERFACE INLINE int |
519 | INTERFACE INLINE int |
512 | is_immutable (pointer p) |
520 | is_immutable (pointer p) |
513 | { |
521 | { |
514 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
522 | return typeflag (p) & T_IMMUTABLE && USE_ERROR_CHECKING; |
… | |
… | |
618 | static int file_push (SCHEME_P_ const char *fname); |
626 | static int file_push (SCHEME_P_ const char *fname); |
619 | static void file_pop (SCHEME_P); |
627 | static void file_pop (SCHEME_P); |
620 | static int file_interactive (SCHEME_P); |
628 | static int file_interactive (SCHEME_P); |
621 | static INLINE int is_one_of (char *s, int c); |
629 | static INLINE int is_one_of (char *s, int c); |
622 | static int alloc_cellseg (SCHEME_P_ int n); |
630 | static int alloc_cellseg (SCHEME_P_ int n); |
623 | static long binary_decode (const char *s); |
|
|
624 | static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); |
631 | static INLINE pointer get_cell (SCHEME_P_ pointer a, pointer b); |
625 | static void finalize_cell (SCHEME_P_ pointer a); |
632 | static void finalize_cell (SCHEME_P_ pointer a); |
626 | static int count_consecutive_cells (pointer x, int needed); |
633 | static int count_consecutive_cells (pointer x, int needed); |
627 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
634 | static pointer find_slot_in_env (SCHEME_P_ pointer env, pointer sym, int all); |
628 | static pointer mk_number (SCHEME_P_ const num n); |
635 | static pointer mk_number (SCHEME_P_ const num n); |
… | |
… | |
660 | static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); |
667 | static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); |
661 | static pointer revappend (SCHEME_P_ pointer a, pointer b); |
668 | static pointer revappend (SCHEME_P_ pointer a, pointer b); |
662 | static pointer ss_get_cont (SCHEME_P); |
669 | static pointer ss_get_cont (SCHEME_P); |
663 | static void ss_set_cont (SCHEME_P_ pointer cont); |
670 | static void ss_set_cont (SCHEME_P_ pointer cont); |
664 | static void dump_stack_mark (SCHEME_P); |
671 | static void dump_stack_mark (SCHEME_P); |
665 | static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); |
672 | static int opexe_0 (SCHEME_P_ enum scheme_opcodes op); |
|
|
673 | static int opexe_1 (SCHEME_P_ enum scheme_opcodes op); |
666 | static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); |
674 | static int opexe_2 (SCHEME_P_ enum scheme_opcodes op); |
667 | static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
675 | static int opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
668 | static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
676 | static int opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
669 | static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
677 | static int opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
670 | static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
678 | static int opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
671 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
679 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
672 | static void assign_syntax (SCHEME_P_ const char *name); |
680 | static void assign_syntax (SCHEME_P_ const char *name); |
673 | static int syntaxnum (pointer p); |
681 | static int syntaxnum (pointer p); |
674 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
682 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
675 | |
683 | |
676 | static num |
684 | static num |
677 | num_add (num a, num b) |
685 | num_op (enum num_op op, num a, num b) |
678 | { |
686 | { |
679 | num ret; |
687 | num ret; |
680 | |
688 | |
681 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
689 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
682 | |
690 | |
683 | if (num_is_fixnum (ret)) |
691 | if (num_is_fixnum (ret)) |
684 | num_set_ivalue (ret, num_get_ivalue (a) + num_get_ivalue (b)); |
692 | { |
|
|
693 | IVALUE av = num_get_ivalue (a); |
|
|
694 | IVALUE bv = num_get_ivalue (b); |
|
|
695 | |
|
|
696 | switch (op) |
|
|
697 | { |
|
|
698 | case NUM_ADD: av += bv; break; |
|
|
699 | case NUM_SUB: av -= bv; break; |
|
|
700 | case NUM_MUL: av *= bv; break; |
|
|
701 | case NUM_INTDIV: av /= bv; break; |
|
|
702 | } |
|
|
703 | |
|
|
704 | num_set_ivalue (ret, av); |
|
|
705 | } |
685 | else |
706 | else |
686 | num_set_rvalue (ret, num_get_rvalue (a) + num_get_rvalue (b)); |
707 | { |
|
|
708 | RVALUE av = num_get_rvalue (a); |
|
|
709 | RVALUE bv = num_get_rvalue (b); |
687 | |
710 | |
688 | return ret; |
711 | switch (op) |
689 | } |
712 | { |
|
|
713 | case NUM_ADD: av += bv; break; |
|
|
714 | case NUM_SUB: av -= bv; break; |
|
|
715 | case NUM_MUL: av *= bv; break; |
|
|
716 | case NUM_INTDIV: av /= bv; break; |
|
|
717 | } |
690 | |
718 | |
691 | static num |
719 | num_set_rvalue (ret, av); |
692 | num_mul (num a, num b) |
720 | } |
693 | { |
|
|
694 | num ret; |
|
|
695 | |
|
|
696 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
|
|
697 | |
|
|
698 | if (num_is_fixnum (ret)) |
|
|
699 | num_set_ivalue (ret, num_get_ivalue (a) * num_get_ivalue (b)); |
|
|
700 | else |
|
|
701 | num_set_rvalue (ret, num_get_rvalue (a) * num_get_rvalue (b)); |
|
|
702 | |
721 | |
703 | return ret; |
722 | return ret; |
704 | } |
723 | } |
705 | |
724 | |
706 | static num |
725 | static num |
… | |
… | |
717 | |
736 | |
718 | return ret; |
737 | return ret; |
719 | } |
738 | } |
720 | |
739 | |
721 | static num |
740 | static num |
722 | num_intdiv (num a, num b) |
|
|
723 | { |
|
|
724 | num ret; |
|
|
725 | |
|
|
726 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
|
|
727 | |
|
|
728 | if (num_is_fixnum (ret)) |
|
|
729 | num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b)); |
|
|
730 | else |
|
|
731 | num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b)); |
|
|
732 | |
|
|
733 | return ret; |
|
|
734 | } |
|
|
735 | |
|
|
736 | static num |
|
|
737 | num_sub (num a, num b) |
|
|
738 | { |
|
|
739 | num ret; |
|
|
740 | |
|
|
741 | num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); |
|
|
742 | |
|
|
743 | if (num_is_fixnum (ret)) |
|
|
744 | num_set_ivalue (ret, num_get_ivalue (a) - num_get_ivalue (b)); |
|
|
745 | else |
|
|
746 | num_set_rvalue (ret, num_get_rvalue (a) - num_get_rvalue (b)); |
|
|
747 | |
|
|
748 | return ret; |
|
|
749 | } |
|
|
750 | |
|
|
751 | static num |
|
|
752 | num_rem (num a, num b) |
741 | num_rem (num a, num b) |
753 | { |
742 | { |
754 | num ret; |
743 | num ret; |
755 | long e1, e2, res; |
744 | long e1, e2, res; |
756 | |
745 | |
… | |
… | |
792 | |
781 | |
793 | num_set_ivalue (ret, res); |
782 | num_set_ivalue (ret, res); |
794 | return ret; |
783 | return ret; |
795 | } |
784 | } |
796 | |
785 | |
|
|
786 | /* this completely disrespects NaNs */ |
797 | static int |
787 | static int |
798 | num_eq (num a, num b) |
788 | num_cmp (num a, num b) |
799 | { |
789 | { |
|
|
790 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
800 | int ret; |
791 | int ret; |
801 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
|
|
802 | |
792 | |
803 | if (is_fixnum) |
793 | if (is_fixnum) |
804 | ret = num_get_ivalue (a) == num_get_ivalue (b); |
794 | { |
|
|
795 | IVALUE av = num_get_ivalue (a); |
|
|
796 | IVALUE bv = num_get_ivalue (b); |
|
|
797 | |
|
|
798 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
|
|
799 | } |
805 | else |
800 | else |
806 | ret = num_get_rvalue (a) == num_get_rvalue (b); |
801 | { |
|
|
802 | RVALUE av = num_get_rvalue (a); |
|
|
803 | RVALUE bv = num_get_rvalue (b); |
|
|
804 | |
|
|
805 | ret = av == bv ? 0 : av < bv ? -1 : +1; |
|
|
806 | } |
807 | |
807 | |
808 | return ret; |
808 | return ret; |
809 | } |
|
|
810 | |
|
|
811 | |
|
|
812 | static int |
|
|
813 | num_gt (num a, num b) |
|
|
814 | { |
|
|
815 | int ret; |
|
|
816 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
|
|
817 | |
|
|
818 | if (is_fixnum) |
|
|
819 | ret = num_get_ivalue (a) > num_get_ivalue (b); |
|
|
820 | else |
|
|
821 | ret = num_get_rvalue (a) > num_get_rvalue (b); |
|
|
822 | |
|
|
823 | return ret; |
|
|
824 | } |
|
|
825 | |
|
|
826 | static int |
|
|
827 | num_ge (num a, num b) |
|
|
828 | { |
|
|
829 | return !num_lt (a, b); |
|
|
830 | } |
|
|
831 | |
|
|
832 | static int |
|
|
833 | num_lt (num a, num b) |
|
|
834 | { |
|
|
835 | int ret; |
|
|
836 | int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b); |
|
|
837 | |
|
|
838 | if (is_fixnum) |
|
|
839 | ret = num_get_ivalue (a) < num_get_ivalue (b); |
|
|
840 | else |
|
|
841 | ret = num_get_rvalue (a) < num_get_rvalue (b); |
|
|
842 | |
|
|
843 | return ret; |
|
|
844 | } |
|
|
845 | |
|
|
846 | static int |
|
|
847 | num_le (num a, num b) |
|
|
848 | { |
|
|
849 | return !num_gt (a, b); |
|
|
850 | } |
809 | } |
851 | |
810 | |
852 | #if USE_MATH |
811 | #if USE_MATH |
853 | |
812 | |
854 | /* Round to nearest. Round to even if midway */ |
813 | /* Round to nearest. Round to even if midway */ |
… | |
… | |
864 | return ce; |
823 | return ce; |
865 | else if (dfl < dce) |
824 | else if (dfl < dce) |
866 | return fl; |
825 | return fl; |
867 | else |
826 | else |
868 | { |
827 | { |
869 | if (fmod (fl, 2.0) == 0.0) /* I imagine this holds */ |
828 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
870 | return fl; |
829 | return fl; |
871 | else |
830 | else |
872 | return ce; |
831 | return ce; |
873 | } |
832 | } |
874 | } |
833 | } |
875 | #endif |
834 | #endif |
876 | |
835 | |
877 | static int |
836 | static int |
878 | is_zero_rvalue (RVALUE x) |
837 | is_zero_rvalue (RVALUE x) |
879 | { |
838 | { |
880 | #if USE_FLOAT |
839 | #if USE_REAL |
881 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
840 | return x < DBL_MIN && x > -DBL_MIN; /* why the hate of denormals? this should be == 0. */ |
882 | #else |
841 | #else |
883 | return x == 0; |
842 | return x == 0; |
884 | #endif |
843 | #endif |
885 | } |
|
|
886 | |
|
|
887 | static long |
|
|
888 | binary_decode (const char *s) |
|
|
889 | { |
|
|
890 | long x = 0; |
|
|
891 | |
|
|
892 | while (*s != 0 && (*s == '1' || *s == '0')) |
|
|
893 | { |
|
|
894 | x <<= 1; |
|
|
895 | x += *s - '0'; |
|
|
896 | s++; |
|
|
897 | } |
|
|
898 | |
|
|
899 | return x; |
|
|
900 | } |
844 | } |
901 | |
845 | |
902 | /* allocate new cell segment */ |
846 | /* allocate new cell segment */ |
903 | static int |
847 | static int |
904 | alloc_cellseg (SCHEME_P_ int n) |
848 | alloc_cellseg (SCHEME_P_ int n) |
… | |
… | |
978 | |
922 | |
979 | /* get new cell. parameter a, b is marked by gc. */ |
923 | /* get new cell. parameter a, b is marked by gc. */ |
980 | static INLINE pointer |
924 | static INLINE pointer |
981 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
925 | get_cell_x (SCHEME_P_ pointer a, pointer b) |
982 | { |
926 | { |
983 | if (SCHEME_V->free_cell == NIL) |
927 | if (ecb_expect_false (SCHEME_V->free_cell == NIL)) |
984 | { |
928 | { |
985 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
929 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
986 | return S_SINK; |
930 | return S_SINK; |
987 | |
931 | |
988 | if (SCHEME_V->free_cell == NIL) |
932 | if (SCHEME_V->free_cell == NIL) |
… | |
… | |
1132 | |
1076 | |
1133 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
1077 | pointer x = immutable_cons (mk_string (SCHEME_A_ name), NIL); |
1134 | set_typeflag (x, T_SYMBOL); |
1078 | set_typeflag (x, T_SYMBOL); |
1135 | setimmutable (car (x)); |
1079 | setimmutable (car (x)); |
1136 | |
1080 | |
1137 | location = hash_fn (name, vector_length (SCHEME_V->oblist)); |
1081 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1138 | set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); |
1082 | set_vector_elem (SCHEME_V->oblist, location, immutable_cons (x, vector_elem (SCHEME_V->oblist, location))); |
1139 | return x; |
1083 | return x; |
1140 | } |
1084 | } |
1141 | |
1085 | |
1142 | static INLINE pointer |
1086 | static INLINE pointer |
… | |
… | |
1144 | { |
1088 | { |
1145 | int location; |
1089 | int location; |
1146 | pointer x; |
1090 | pointer x; |
1147 | char *s; |
1091 | char *s; |
1148 | |
1092 | |
1149 | location = hash_fn (name, vector_length (SCHEME_V->oblist)); |
1093 | location = hash_fn (name, veclength (SCHEME_V->oblist)); |
1150 | |
1094 | |
1151 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1095 | for (x = vector_elem (SCHEME_V->oblist, location); x != NIL; x = cdr (x)) |
1152 | { |
1096 | { |
1153 | s = symname (car (x)); |
1097 | s = symname (car (x)); |
1154 | |
1098 | |
… | |
… | |
1165 | { |
1109 | { |
1166 | int i; |
1110 | int i; |
1167 | pointer x; |
1111 | pointer x; |
1168 | pointer ob_list = NIL; |
1112 | pointer ob_list = NIL; |
1169 | |
1113 | |
1170 | for (i = 0; i < vector_length (SCHEME_V->oblist); i++) |
1114 | for (i = 0; i < veclength (SCHEME_V->oblist); i++) |
1171 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1115 | for (x = vector_elem (SCHEME_V->oblist, i); x != NIL; x = cdr (x)) |
1172 | ob_list = cons (x, ob_list); |
1116 | ob_list = cons (x, ob_list); |
1173 | |
1117 | |
1174 | return ob_list; |
1118 | return ob_list; |
1175 | } |
1119 | } |
… | |
… | |
1317 | } |
1261 | } |
1318 | |
1262 | |
1319 | return q; |
1263 | return q; |
1320 | } |
1264 | } |
1321 | |
1265 | |
1322 | /* get new string */ |
|
|
1323 | INTERFACE pointer |
1266 | INTERFACE pointer |
1324 | mk_string (SCHEME_P_ const char *str) |
1267 | mk_empty_string (SCHEME_P_ uint32_t len, char fill) |
1325 | { |
1268 | { |
1326 | return mk_counted_string (SCHEME_A_ str, strlen (str)); |
1269 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
|
|
1270 | |
|
|
1271 | set_typeflag (x, T_STRING | T_ATOM); |
|
|
1272 | strvalue (x) = store_string (SCHEME_A_ len, 0, fill); |
|
|
1273 | strlength (x) = len; |
|
|
1274 | return x; |
1327 | } |
1275 | } |
1328 | |
1276 | |
1329 | INTERFACE pointer |
1277 | INTERFACE pointer |
1330 | mk_counted_string (SCHEME_P_ const char *str, uint32_t len) |
1278 | mk_counted_string (SCHEME_P_ const char *str, uint32_t len) |
1331 | { |
1279 | { |
… | |
… | |
1336 | strlength (x) = len; |
1284 | strlength (x) = len; |
1337 | return x; |
1285 | return x; |
1338 | } |
1286 | } |
1339 | |
1287 | |
1340 | INTERFACE pointer |
1288 | INTERFACE pointer |
1341 | mk_empty_string (SCHEME_P_ uint32_t len, char fill) |
1289 | mk_string (SCHEME_P_ const char *str) |
1342 | { |
1290 | { |
1343 | pointer x = get_cell (SCHEME_A_ NIL, NIL); |
1291 | return mk_counted_string (SCHEME_A_ str, strlen (str)); |
1344 | |
|
|
1345 | set_typeflag (x, T_STRING | T_ATOM); |
|
|
1346 | strvalue (x) = store_string (SCHEME_A_ len, 0, fill); |
|
|
1347 | strlength (x) = len; |
|
|
1348 | return x; |
|
|
1349 | } |
1292 | } |
1350 | |
1293 | |
1351 | INTERFACE pointer |
1294 | INTERFACE pointer |
1352 | mk_vector (SCHEME_P_ uint32_t len) |
1295 | mk_vector (SCHEME_P_ uint32_t len) |
1353 | { |
1296 | { |
… | |
… | |
1358 | fill_vector (pointer vec, pointer obj) |
1301 | fill_vector (pointer vec, pointer obj) |
1359 | { |
1302 | { |
1360 | int i; |
1303 | int i; |
1361 | |
1304 | |
1362 | for (i = 0; i < vec->object.vector.length; i++) |
1305 | for (i = 0; i < vec->object.vector.length; i++) |
1363 | vec->object.vector.vvalue[i] = obj; |
1306 | vecvalue (vec)[i] = obj; |
1364 | } |
1307 | } |
1365 | |
1308 | |
1366 | INTERFACE pointer |
1309 | INTERFACE pointer |
1367 | vector_elem (pointer vec, uint32_t ielem) |
1310 | vector_elem (pointer vec, uint32_t ielem) |
1368 | { |
1311 | { |
1369 | return vec->object.vector.vvalue[ielem]; |
1312 | return vecvalue(vec)[ielem]; |
1370 | } |
1313 | } |
1371 | |
1314 | |
1372 | INTERFACE void |
1315 | INTERFACE void |
1373 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1316 | set_vector_elem (pointer vec, uint32_t ielem, pointer a) |
1374 | { |
1317 | { |
1375 | vec->object.vector.vvalue[ielem] = a; |
1318 | vecvalue(vec)[ielem] = a; |
1376 | } |
1319 | } |
1377 | |
1320 | |
1378 | /* get new symbol */ |
1321 | /* get new symbol */ |
1379 | INTERFACE pointer |
1322 | INTERFACE pointer |
1380 | mk_symbol (SCHEME_P_ const char *name) |
1323 | mk_symbol (SCHEME_P_ const char *name) |
… | |
… | |
1390 | |
1333 | |
1391 | INTERFACE pointer |
1334 | INTERFACE pointer |
1392 | gensym (SCHEME_P) |
1335 | gensym (SCHEME_P) |
1393 | { |
1336 | { |
1394 | pointer x; |
1337 | pointer x; |
1395 | char name[40]; |
|
|
1396 | |
1338 | |
1397 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
1339 | for (; SCHEME_V->gensym_cnt < LONG_MAX; SCHEME_V->gensym_cnt++) |
1398 | { |
1340 | { |
1399 | strcpy (name, "gensym-"); |
1341 | char name[40] = "gensym-"; |
1400 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1342 | xnum (name + 7, SCHEME_V->gensym_cnt); |
1401 | |
1343 | |
1402 | /* first check oblist */ |
1344 | /* first check oblist */ |
1403 | x = oblist_find_by_name (SCHEME_A_ name); |
1345 | x = oblist_find_by_name (SCHEME_A_ name); |
1404 | |
1346 | |
1405 | if (x != NIL) |
1347 | if (x == NIL) |
1406 | continue; |
|
|
1407 | else |
|
|
1408 | { |
1348 | { |
1409 | x = oblist_add_by_name (SCHEME_A_ name); |
1349 | x = oblist_add_by_name (SCHEME_A_ name); |
1410 | return x; |
1350 | return x; |
1411 | } |
1351 | } |
1412 | } |
1352 | } |
… | |
… | |
1421 | char c, *p; |
1361 | char c, *p; |
1422 | int has_dec_point = 0; |
1362 | int has_dec_point = 0; |
1423 | int has_fp_exp = 0; |
1363 | int has_fp_exp = 0; |
1424 | |
1364 | |
1425 | #if USE_COLON_HOOK |
1365 | #if USE_COLON_HOOK |
1426 | |
|
|
1427 | if ((p = strstr (q, "::")) != 0) |
1366 | if ((p = strstr (q, "::")) != 0) |
1428 | { |
1367 | { |
1429 | *p = 0; |
1368 | *p = 0; |
1430 | return cons (SCHEME_V->COLON_HOOK, |
1369 | return cons (SCHEME_V->COLON_HOOK, |
1431 | cons (cons (SCHEME_V->QUOTE, |
1370 | cons (cons (SCHEME_V->QUOTE, |
1432 | cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL))); |
1371 | cons (mk_atom (SCHEME_A_ p + 2), NIL)), cons (mk_symbol (SCHEME_A_ strlwr (q)), NIL))); |
1433 | } |
1372 | } |
1434 | |
|
|
1435 | #endif |
1373 | #endif |
1436 | |
1374 | |
1437 | p = q; |
1375 | p = q; |
1438 | c = *p++; |
1376 | c = *p++; |
1439 | |
1377 | |
… | |
… | |
1488 | |
1426 | |
1489 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1427 | return mk_symbol (SCHEME_A_ strlwr (q)); |
1490 | } |
1428 | } |
1491 | } |
1429 | } |
1492 | |
1430 | |
1493 | #if USE_FLOAT |
1431 | #if USE_REAL |
1494 | if (has_dec_point) |
1432 | if (has_dec_point) |
1495 | return mk_real (SCHEME_A_ atof (q)); |
1433 | return mk_real (SCHEME_A_ atof (q)); |
1496 | #endif |
1434 | #endif |
1497 | |
1435 | |
1498 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
1436 | return mk_integer (SCHEME_A_ strtol (q, 0, 10)); |
… | |
… | |
1500 | |
1438 | |
1501 | /* make constant */ |
1439 | /* make constant */ |
1502 | static pointer |
1440 | static pointer |
1503 | mk_sharp_const (SCHEME_P_ char *name) |
1441 | mk_sharp_const (SCHEME_P_ char *name) |
1504 | { |
1442 | { |
1505 | long x; |
|
|
1506 | char tmp[STRBUFFSIZE]; |
|
|
1507 | |
|
|
1508 | if (!strcmp (name, "t")) |
1443 | if (!strcmp (name, "t")) |
1509 | return S_T; |
1444 | return S_T; |
1510 | else if (!strcmp (name, "f")) |
1445 | else if (!strcmp (name, "f")) |
1511 | return S_F; |
1446 | return S_F; |
1512 | else if (*name == 'o') /* #o (octal) */ |
|
|
1513 | { |
|
|
1514 | x = strtol (name + 1, 0, 8); |
|
|
1515 | return mk_integer (SCHEME_A_ x); |
|
|
1516 | } |
|
|
1517 | else if (*name == 'd') /* #d (decimal) */ |
|
|
1518 | { |
|
|
1519 | x = strtol (name + 1, 0, 10); |
|
|
1520 | return mk_integer (SCHEME_A_ x); |
|
|
1521 | } |
|
|
1522 | else if (*name == 'x') /* #x (hex) */ |
|
|
1523 | { |
|
|
1524 | x = strtol (name + 1, 0, 16); |
|
|
1525 | return mk_integer (SCHEME_A_ x); |
|
|
1526 | } |
|
|
1527 | else if (*name == 'b') /* #b (binary) */ |
|
|
1528 | { |
|
|
1529 | x = binary_decode (name + 1); |
|
|
1530 | return mk_integer (SCHEME_A_ x); |
|
|
1531 | } |
|
|
1532 | else if (*name == '\\') /* #\w (character) */ |
1447 | else if (*name == '\\') /* #\w (character) */ |
1533 | { |
1448 | { |
1534 | int c = 0; |
1449 | int c; |
1535 | |
1450 | |
1536 | if (stricmp (name + 1, "space") == 0) |
1451 | if (stricmp (name + 1, "space") == 0) |
1537 | c = ' '; |
1452 | c = ' '; |
1538 | else if (stricmp (name + 1, "newline") == 0) |
1453 | else if (stricmp (name + 1, "newline") == 0) |
1539 | c = '\n'; |
1454 | c = '\n'; |
… | |
… | |
1541 | c = '\r'; |
1456 | c = '\r'; |
1542 | else if (stricmp (name + 1, "tab") == 0) |
1457 | else if (stricmp (name + 1, "tab") == 0) |
1543 | c = '\t'; |
1458 | c = '\t'; |
1544 | else if (name[1] == 'x' && name[2] != 0) |
1459 | else if (name[1] == 'x' && name[2] != 0) |
1545 | { |
1460 | { |
1546 | int c1 = strtol (name + 2, 0, 16); |
1461 | long c1 = strtol (name + 2, 0, 16); |
1547 | |
1462 | |
1548 | if (c1 <= UCHAR_MAX) |
1463 | if (0 <= c1 && c1 <= UCHAR_MAX) |
1549 | c = c1; |
1464 | c = c1; |
1550 | else |
1465 | else |
1551 | return NIL; |
1466 | return NIL; |
1552 | |
1467 | } |
1553 | #if USE_ASCII_NAMES |
1468 | #if USE_ASCII_NAMES |
1554 | } |
|
|
1555 | else if (is_ascii_name (name + 1, &c)) |
1469 | else if (is_ascii_name (name + 1, &c)) |
1556 | { |
|
|
1557 | /* nothing */ |
1470 | /* nothing */; |
1558 | #endif |
1471 | #endif |
1559 | } |
|
|
1560 | else if (name[2] == 0) |
1472 | else if (name[2] == 0) |
1561 | c = name[1]; |
1473 | c = name[1]; |
1562 | else |
1474 | else |
1563 | return NIL; |
1475 | return NIL; |
1564 | |
1476 | |
1565 | return mk_character (SCHEME_A_ c); |
1477 | return mk_character (SCHEME_A_ c); |
1566 | } |
1478 | } |
1567 | else |
1479 | else |
|
|
1480 | { |
|
|
1481 | /* identify base by string index */ |
|
|
1482 | const char baseidx[17] = "ffbf" "ffff" "ofdf" "ffff" "x"; |
|
|
1483 | char *base = strchr (baseidx, *name); |
|
|
1484 | |
|
|
1485 | if (base) |
|
|
1486 | return mk_integer (SCHEME_A_ strtol (name + 1, 0, base - baseidx)); |
|
|
1487 | |
1568 | return NIL; |
1488 | return NIL; |
|
|
1489 | } |
1569 | } |
1490 | } |
1570 | |
1491 | |
1571 | /* ========== garbage collector ========== */ |
1492 | /* ========== garbage collector ========== */ |
1572 | |
1493 | |
1573 | /*-- |
1494 | /*-- |
1574 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1495 | * We use algorithm E (Knuth, The Art of Computer Programming Vol.1, |
1575 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1496 | * sec. 2.3.5), the Schorr-Deutsch-Waite link-inversion algorithm, |
1576 | * for marking. |
1497 | * for marking. |
|
|
1498 | * |
|
|
1499 | * The exception is vectors - vectors are currently marked recursively, |
|
|
1500 | * which is inherited form tinyscheme and could be fixed by having another |
|
|
1501 | * word of context in the vector |
1577 | */ |
1502 | */ |
1578 | static void |
1503 | static void |
1579 | mark (pointer a) |
1504 | mark (pointer a) |
1580 | { |
1505 | { |
1581 | pointer t, q, p; |
1506 | pointer t, q, p; |
… | |
… | |
1583 | t = 0; |
1508 | t = 0; |
1584 | p = a; |
1509 | p = a; |
1585 | E2: |
1510 | E2: |
1586 | setmark (p); |
1511 | setmark (p); |
1587 | |
1512 | |
1588 | if (is_vector (p)) |
1513 | if (ecb_expect_false (is_vector (p))) |
1589 | { |
1514 | { |
1590 | int i; |
1515 | int i; |
1591 | |
1516 | |
1592 | for (i = 0; i < p->object.vector.length; i++) |
1517 | for (i = 0; i < p->object.vector.length; i++) |
1593 | mark (p->object.vector.vvalue[i]); |
1518 | mark (vecvalue (p)[i]); |
1594 | } |
1519 | } |
1595 | |
1520 | |
1596 | if (is_atom (p)) |
1521 | if (is_atom (p)) |
1597 | goto E6; |
1522 | goto E6; |
1598 | |
1523 | |
… | |
… | |
1716 | } |
1641 | } |
1717 | |
1642 | |
1718 | static void |
1643 | static void |
1719 | finalize_cell (SCHEME_P_ pointer a) |
1644 | finalize_cell (SCHEME_P_ pointer a) |
1720 | { |
1645 | { |
|
|
1646 | /* TODO, fast bitmap check? */ |
1721 | if (is_string (a)) |
1647 | if (is_string (a)) |
1722 | free (strvalue (a)); |
1648 | free (strvalue (a)); |
1723 | else if (is_vector (a)) |
1649 | else if (is_vector (a)) |
1724 | free (a->object.vector.vvalue); |
1650 | free (vecvalue (a)); |
1725 | #if USE_PORTS |
1651 | #if USE_PORTS |
1726 | else if (is_port (a)) |
1652 | else if (is_port (a)) |
1727 | { |
1653 | { |
1728 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1654 | if (a->object.port->kind & port_file && a->object.port->rep.stdio.closeit) |
1729 | port_close (SCHEME_A_ a, port_input | port_output); |
1655 | port_close (SCHEME_A_ a, port_input | port_output); |
… | |
… | |
2556 | |
2482 | |
2557 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2483 | if (f <= 1 || f == 10) /* f is the base for numbers if > 1 */ |
2558 | { |
2484 | { |
2559 | if (num_is_integer (l)) |
2485 | if (num_is_integer (l)) |
2560 | xnum (p, ivalue_unchecked (l)); |
2486 | xnum (p, ivalue_unchecked (l)); |
2561 | #if USE_FLOAT |
2487 | #if USE_REAL |
2562 | else |
2488 | else |
2563 | { |
2489 | { |
2564 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2490 | snprintf (p, STRBUFFSIZE, "%.10g", rvalue_unchecked (l)); |
2565 | /* r5rs says there must be a '.' (unless 'e'?) */ |
2491 | /* r5rs says there must be a '.' (unless 'e'?) */ |
2566 | f = strcspn (p, ".e"); |
2492 | f = strcspn (p, ".e"); |
… | |
… | |
2820 | } |
2746 | } |
2821 | else if (is_number (a)) |
2747 | else if (is_number (a)) |
2822 | { |
2748 | { |
2823 | if (is_number (b)) |
2749 | if (is_number (b)) |
2824 | if (num_is_integer (a) == num_is_integer (b)) |
2750 | if (num_is_integer (a) == num_is_integer (b)) |
2825 | return num_eq (nvalue (a), nvalue (b)); |
2751 | return num_cmp (nvalue (a), nvalue (b)) == 0; |
2826 | |
2752 | |
2827 | return 0; |
2753 | return 0; |
2828 | } |
2754 | } |
2829 | else if (is_character (a)) |
2755 | else if (is_character (a)) |
2830 | { |
2756 | { |
… | |
… | |
2904 | { |
2830 | { |
2905 | pointer slot = immutable_cons (variable, value); |
2831 | pointer slot = immutable_cons (variable, value); |
2906 | |
2832 | |
2907 | if (is_vector (car (env))) |
2833 | if (is_vector (car (env))) |
2908 | { |
2834 | { |
2909 | int location = hash_fn (symname (variable), vector_length (car (env))); |
2835 | int location = hash_fn (symname (variable), veclength (car (env))); |
2910 | |
2836 | |
2911 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2837 | set_vector_elem (car (env), location, immutable_cons (slot, vector_elem (car (env), location))); |
2912 | } |
2838 | } |
2913 | else |
2839 | else |
2914 | set_car (env, immutable_cons (slot, car (env))); |
2840 | set_car (env, immutable_cons (slot, car (env))); |
… | |
… | |
2922 | |
2848 | |
2923 | for (x = env; x != NIL; x = cdr (x)) |
2849 | for (x = env; x != NIL; x = cdr (x)) |
2924 | { |
2850 | { |
2925 | if (is_vector (car (x))) |
2851 | if (is_vector (car (x))) |
2926 | { |
2852 | { |
2927 | location = hash_fn (symname (hdl), vector_length (car (x))); |
2853 | location = hash_fn (symname (hdl), veclength (car (x))); |
2928 | y = vector_elem (car (x), location); |
2854 | y = vector_elem (car (x), location); |
2929 | } |
2855 | } |
2930 | else |
2856 | else |
2931 | y = car (x); |
2857 | y = car (x); |
2932 | |
2858 | |
… | |
… | |
3006 | return cdr (slot); |
2932 | return cdr (slot); |
3007 | } |
2933 | } |
3008 | |
2934 | |
3009 | /* ========== Evaluation Cycle ========== */ |
2935 | /* ========== Evaluation Cycle ========== */ |
3010 | |
2936 | |
3011 | static pointer |
2937 | static int |
3012 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2938 | xError_1 (SCHEME_P_ const char *s, pointer a) |
3013 | { |
2939 | { |
3014 | #if USE_ERROR_HOOK |
2940 | #if USE_ERROR_HOOK |
3015 | pointer x; |
2941 | pointer x; |
3016 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2942 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
3042 | #if USE_ERROR_HOOK |
2968 | #if USE_ERROR_HOOK |
3043 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); |
2969 | x = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, hdl, 1); |
3044 | |
2970 | |
3045 | if (x != NIL) |
2971 | if (x != NIL) |
3046 | { |
2972 | { |
3047 | if (a) |
2973 | pointer code = a |
3048 | SCHEME_V->code = cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL); |
2974 | ? cons (cons (SCHEME_V->QUOTE, cons (a, NIL)), NIL) |
3049 | else |
2975 | : NIL; |
3050 | SCHEME_V->code = NIL; |
|
|
3051 | |
2976 | |
3052 | SCHEME_V->code = cons (mk_string (SCHEME_A_ s), SCHEME_V->code); |
2977 | code = cons (mk_string (SCHEME_A_ s), code); |
3053 | setimmutable (car (SCHEME_V->code)); |
2978 | setimmutable (car (code)); |
3054 | SCHEME_V->code = cons (slot_value_in_env (x), SCHEME_V->code); |
2979 | SCHEME_V->code = cons (slot_value_in_env (x), code); |
3055 | SCHEME_V->op = OP_EVAL; |
2980 | SCHEME_V->op = OP_EVAL; |
3056 | |
2981 | |
3057 | return S_T; |
2982 | return 0; |
3058 | } |
2983 | } |
3059 | #endif |
2984 | #endif |
3060 | |
2985 | |
3061 | if (a) |
2986 | if (a) |
3062 | SCHEME_V->args = cons (a, NIL); |
2987 | SCHEME_V->args = cons (a, NIL); |
… | |
… | |
3064 | SCHEME_V->args = NIL; |
2989 | SCHEME_V->args = NIL; |
3065 | |
2990 | |
3066 | SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); |
2991 | SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); |
3067 | setimmutable (car (SCHEME_V->args)); |
2992 | setimmutable (car (SCHEME_V->args)); |
3068 | SCHEME_V->op = OP_ERR0; |
2993 | SCHEME_V->op = OP_ERR0; |
|
|
2994 | |
3069 | return S_T; |
2995 | return 0; |
3070 | } |
2996 | } |
3071 | |
2997 | |
3072 | #define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) |
2998 | #define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) |
3073 | #define Error_0(s) Error_1 (s, 0) |
2999 | #define Error_0(s) Error_1 (s, 0) |
3074 | |
3000 | |
3075 | /* Too small to turn into function */ |
3001 | /* Too small to turn into function */ |
3076 | #define BEGIN do { |
3002 | #define BEGIN do { |
3077 | #define END } while (0) |
3003 | #define END } while (0) |
3078 | #define s_goto(a) BEGIN \ |
3004 | #define s_goto(a) BEGIN \ |
3079 | SCHEME_V->op = a; \ |
3005 | SCHEME_V->op = a; \ |
3080 | return S_T; END |
3006 | return 0; END |
3081 | |
3007 | |
3082 | #define s_return(a) return xs_return (SCHEME_A_ a) |
3008 | #define s_return(a) return xs_return (SCHEME_A_ a) |
3083 | |
3009 | |
3084 | #ifndef USE_SCHEME_STACK |
3010 | #ifndef USE_SCHEME_STACK |
3085 | |
3011 | |
… | |
… | |
3110 | next_frame = SCHEME_V->dump_base + nframes; |
3036 | next_frame = SCHEME_V->dump_base + nframes; |
3111 | |
3037 | |
3112 | next_frame->op = op; |
3038 | next_frame->op = op; |
3113 | next_frame->args = args; |
3039 | next_frame->args = args; |
3114 | next_frame->envir = SCHEME_V->envir; |
3040 | next_frame->envir = SCHEME_V->envir; |
3115 | next_frame->code = code; |
3041 | next_frame->code = code; |
3116 | |
3042 | |
3117 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3043 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3118 | } |
3044 | } |
3119 | |
3045 | |
3120 | static pointer |
3046 | static int |
3121 | xs_return (SCHEME_P_ pointer a) |
3047 | xs_return (SCHEME_P_ pointer a) |
3122 | { |
3048 | { |
3123 | int nframes = (uintptr_t)SCHEME_V->dump; |
3049 | int nframes = (uintptr_t)SCHEME_V->dump; |
3124 | struct dump_stack_frame *frame; |
3050 | struct dump_stack_frame *frame; |
3125 | |
3051 | |
3126 | SCHEME_V->value = a; |
3052 | SCHEME_V->value = a; |
3127 | |
3053 | |
3128 | if (nframes <= 0) |
3054 | if (nframes <= 0) |
3129 | return NIL; |
3055 | return -1; |
3130 | |
3056 | |
3131 | frame = &SCHEME_V->dump_base[--nframes]; |
3057 | frame = &SCHEME_V->dump_base[--nframes]; |
3132 | SCHEME_V->op = frame->op; |
3058 | SCHEME_V->op = frame->op; |
3133 | SCHEME_V->args = frame->args; |
3059 | SCHEME_V->args = frame->args; |
3134 | SCHEME_V->envir = frame->envir; |
3060 | SCHEME_V->envir = frame->envir; |
3135 | SCHEME_V->code = frame->code; |
3061 | SCHEME_V->code = frame->code; |
3136 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3062 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3137 | |
3063 | |
3138 | return S_T; |
3064 | return 0; |
3139 | } |
3065 | } |
3140 | |
3066 | |
3141 | static INLINE void |
3067 | static INLINE void |
3142 | dump_stack_reset (SCHEME_P) |
3068 | dump_stack_reset (SCHEME_P) |
3143 | { |
3069 | { |
… | |
… | |
3238 | dump_stack_free (SCHEME_P) |
3164 | dump_stack_free (SCHEME_P) |
3239 | { |
3165 | { |
3240 | SCHEME_V->dump = NIL; |
3166 | SCHEME_V->dump = NIL; |
3241 | } |
3167 | } |
3242 | |
3168 | |
3243 | static pointer |
3169 | static int |
3244 | xs_return (SCHEME_P_ pointer a) |
3170 | xs_return (SCHEME_P_ pointer a) |
3245 | { |
3171 | { |
3246 | pointer dump = SCHEME_V->dump; |
3172 | pointer dump = SCHEME_V->dump; |
3247 | |
3173 | |
3248 | SCHEME_V->value = a; |
3174 | SCHEME_V->value = a; |
3249 | |
3175 | |
3250 | if (dump == NIL) |
3176 | if (dump == NIL) |
3251 | return NIL; |
3177 | return -1; |
3252 | |
3178 | |
3253 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3179 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3254 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3180 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3255 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3181 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3256 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3182 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3257 | |
3183 | |
3258 | SCHEME_V->dump = dump; |
3184 | SCHEME_V->dump = dump; |
3259 | |
3185 | |
3260 | return S_T; |
3186 | return 0; |
3261 | } |
3187 | } |
3262 | |
3188 | |
3263 | static void |
3189 | static void |
3264 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3190 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3265 | { |
3191 | { |
… | |
… | |
3290 | |
3216 | |
3291 | #endif |
3217 | #endif |
3292 | |
3218 | |
3293 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3219 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3294 | |
3220 | |
3295 | static pointer |
3221 | static int |
3296 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3222 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3297 | { |
3223 | { |
|
|
3224 | pointer args = SCHEME_V->args; |
3298 | pointer x, y; |
3225 | pointer x, y; |
3299 | |
3226 | |
3300 | switch (op) |
3227 | switch (op) |
3301 | { |
3228 | { |
3302 | case OP_LOAD: /* load */ |
3229 | case OP_LOAD: /* load */ |
3303 | if (file_interactive (SCHEME_A)) |
3230 | if (file_interactive (SCHEME_A)) |
3304 | { |
3231 | { |
3305 | xwrstr ("Loading "); xwrstr (strvalue (car (SCHEME_V->args))); xwrstr ("\n"); |
3232 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3306 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (SCHEME_V->args))); |
3233 | //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args))); |
3307 | } |
3234 | } |
3308 | |
3235 | |
3309 | if (!file_push (SCHEME_A_ strvalue (car (SCHEME_V->args)))) |
3236 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3310 | Error_1 ("unable to open", car (SCHEME_V->args)); |
3237 | Error_1 ("unable to open", car (args)); |
3311 | else |
3238 | else |
3312 | { |
3239 | { |
3313 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3240 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3314 | s_goto (OP_T0LVL); |
3241 | s_goto (OP_T0LVL); |
3315 | } |
3242 | } |
… | |
… | |
3350 | s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); |
3277 | s_save (SCHEME_A_ OP_VALUEPRINT, NIL, NIL); |
3351 | s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); |
3278 | s_save (SCHEME_A_ OP_T1LVL, NIL, NIL); |
3352 | s_goto (OP_READ_INTERNAL); |
3279 | s_goto (OP_READ_INTERNAL); |
3353 | |
3280 | |
3354 | case OP_T1LVL: /* top level */ |
3281 | case OP_T1LVL: /* top level */ |
3355 | SCHEME_V->code = SCHEME_V->value; |
3282 | SCHEME_V->code = SCHEME_V->value; |
3356 | SCHEME_V->inport = SCHEME_V->save_inport; |
3283 | SCHEME_V->inport = SCHEME_V->save_inport; |
3357 | s_goto (OP_EVAL); |
3284 | s_goto (OP_EVAL); |
3358 | |
3285 | |
3359 | case OP_READ_INTERNAL: /* internal read */ |
3286 | case OP_READ_INTERNAL: /* internal read */ |
3360 | SCHEME_V->tok = token (SCHEME_A); |
3287 | SCHEME_V->tok = token (SCHEME_A); |
… | |
… | |
3389 | case OP_EVAL: /* main part of evaluation */ |
3316 | case OP_EVAL: /* main part of evaluation */ |
3390 | #if USE_TRACING |
3317 | #if USE_TRACING |
3391 | if (SCHEME_V->tracing) |
3318 | if (SCHEME_V->tracing) |
3392 | { |
3319 | { |
3393 | /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ |
3320 | /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ |
3394 | s_save (SCHEME_A_ OP_REAL_EVAL, SCHEME_V->args, SCHEME_V->code); |
3321 | s_save (SCHEME_A_ OP_REAL_EVAL, args, SCHEME_V->code); |
3395 | SCHEME_V->args = SCHEME_V->code; |
3322 | SCHEME_V->args = SCHEME_V->code; |
3396 | putstr (SCHEME_A_ "\nEval: "); |
3323 | putstr (SCHEME_A_ "\nEval: "); |
3397 | s_goto (OP_P0LIST); |
3324 | s_goto (OP_P0LIST); |
3398 | } |
3325 | } |
3399 | |
3326 | |
… | |
… | |
3410 | else |
3337 | else |
3411 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3338 | Error_1 ("eval: unbound variable:", SCHEME_V->code); |
3412 | } |
3339 | } |
3413 | else if (is_pair (SCHEME_V->code)) |
3340 | else if (is_pair (SCHEME_V->code)) |
3414 | { |
3341 | { |
|
|
3342 | x = car (SCHEME_V->code); |
|
|
3343 | |
3415 | if (is_syntax (x = car (SCHEME_V->code))) /* SYNTAX */ |
3344 | if (is_syntax (x)) /* SYNTAX */ |
3416 | { |
3345 | { |
3417 | SCHEME_V->code = cdr (SCHEME_V->code); |
3346 | SCHEME_V->code = cdr (SCHEME_V->code); |
3418 | s_goto (syntaxnum (x)); |
3347 | s_goto (syntaxnum (x)); |
3419 | } |
3348 | } |
3420 | else /* first, eval top element and eval arguments */ |
3349 | else /* first, eval top element and eval arguments */ |
3421 | { |
3350 | { |
3422 | s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code); |
3351 | s_save (SCHEME_A_ OP_E0ARGS, NIL, SCHEME_V->code); |
3423 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3352 | /* If no macros => s_save(SCHEME_A_ OP_E1ARGS, NIL, cdr(SCHEME_V->code)); */ |
3424 | SCHEME_V->code = car (SCHEME_V->code); |
3353 | SCHEME_V->code = x; |
3425 | s_goto (OP_EVAL); |
3354 | s_goto (OP_EVAL); |
3426 | } |
3355 | } |
3427 | } |
3356 | } |
3428 | else |
3357 | else |
3429 | s_return (SCHEME_V->code); |
3358 | s_return (SCHEME_V->code); |
… | |
… | |
3441 | SCHEME_V->code = cdr (SCHEME_V->code); |
3370 | SCHEME_V->code = cdr (SCHEME_V->code); |
3442 | s_goto (OP_E1ARGS); |
3371 | s_goto (OP_E1ARGS); |
3443 | } |
3372 | } |
3444 | |
3373 | |
3445 | case OP_E1ARGS: /* eval arguments */ |
3374 | case OP_E1ARGS: /* eval arguments */ |
3446 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3375 | args = cons (SCHEME_V->value, args); |
3447 | |
3376 | |
3448 | if (is_pair (SCHEME_V->code)) /* continue */ |
3377 | if (is_pair (SCHEME_V->code)) /* continue */ |
3449 | { |
3378 | { |
3450 | s_save (SCHEME_A_ OP_E1ARGS, SCHEME_V->args, cdr (SCHEME_V->code)); |
3379 | s_save (SCHEME_A_ OP_E1ARGS, args, cdr (SCHEME_V->code)); |
3451 | SCHEME_V->code = car (SCHEME_V->code); |
3380 | SCHEME_V->code = car (SCHEME_V->code); |
3452 | SCHEME_V->args = NIL; |
3381 | SCHEME_V->args = NIL; |
3453 | s_goto (OP_EVAL); |
3382 | s_goto (OP_EVAL); |
3454 | } |
3383 | } |
3455 | else /* end */ |
3384 | else /* end */ |
3456 | { |
3385 | { |
3457 | SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); |
3386 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3458 | SCHEME_V->code = car (SCHEME_V->args); |
3387 | SCHEME_V->code = car (args); |
3459 | SCHEME_V->args = cdr (SCHEME_V->args); |
3388 | SCHEME_V->args = cdr (args); |
3460 | s_goto (OP_APPLY); |
3389 | s_goto (OP_APPLY); |
3461 | } |
3390 | } |
3462 | |
3391 | |
3463 | #if USE_TRACING |
3392 | #if USE_TRACING |
3464 | |
3393 | |
3465 | case OP_TRACING: |
3394 | case OP_TRACING: |
3466 | { |
3395 | { |
3467 | int tr = SCHEME_V->tracing; |
3396 | int tr = SCHEME_V->tracing; |
3468 | |
3397 | |
3469 | SCHEME_V->tracing = ivalue (car (SCHEME_V->args)); |
3398 | SCHEME_V->tracing = ivalue (car (args)); |
3470 | s_return (mk_integer (SCHEME_A_ tr)); |
3399 | s_return (mk_integer (SCHEME_A_ tr)); |
3471 | } |
3400 | } |
3472 | |
3401 | |
3473 | #endif |
3402 | #endif |
3474 | |
3403 | |
3475 | case OP_APPLY: /* apply 'code' to 'args' */ |
3404 | case OP_APPLY: /* apply 'code' to 'args' */ |
3476 | #if USE_TRACING |
3405 | #if USE_TRACING |
3477 | if (SCHEME_V->tracing) |
3406 | if (SCHEME_V->tracing) |
3478 | { |
3407 | { |
3479 | s_save (SCHEME_A_ OP_REAL_APPLY, SCHEME_V->args, SCHEME_V->code); |
3408 | s_save (SCHEME_A_ OP_REAL_APPLY, args, SCHEME_V->code); |
3480 | SCHEME_V->print_flag = 1; |
3409 | SCHEME_V->print_flag = 1; |
3481 | /* SCHEME_V->args=cons(SCHEME_V->code,SCHEME_V->args); */ |
3410 | /* args=cons(SCHEME_V->code,args); */ |
3482 | putstr (SCHEME_A_ "\nApply to: "); |
3411 | putstr (SCHEME_A_ "\nApply to: "); |
3483 | s_goto (OP_P0LIST); |
3412 | s_goto (OP_P0LIST); |
3484 | } |
3413 | } |
3485 | |
3414 | |
3486 | /* fall through */ |
3415 | /* fall through */ |
3487 | |
3416 | |
3488 | case OP_REAL_APPLY: |
3417 | case OP_REAL_APPLY: |
3489 | #endif |
3418 | #endif |
3490 | if (is_proc (SCHEME_V->code)) |
3419 | if (is_proc (SCHEME_V->code)) |
3491 | { |
|
|
3492 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3420 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3493 | } |
|
|
3494 | else if (is_foreign (SCHEME_V->code)) |
3421 | else if (is_foreign (SCHEME_V->code)) |
3495 | { |
3422 | { |
3496 | /* Keep nested calls from GC'ing the arglist */ |
3423 | /* Keep nested calls from GC'ing the arglist */ |
3497 | push_recent_alloc (SCHEME_A_ SCHEME_V->args, NIL); |
3424 | push_recent_alloc (SCHEME_A_ args, NIL); |
3498 | x = SCHEME_V->code->object.ff (SCHEME_A_ SCHEME_V->args); |
3425 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3499 | |
3426 | |
3500 | s_return (x); |
3427 | s_return (x); |
3501 | } |
3428 | } |
3502 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3429 | else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ |
3503 | { |
3430 | { |
3504 | /* Should not accept promise */ |
3431 | /* Should not accept promise */ |
3505 | /* make environment */ |
3432 | /* make environment */ |
3506 | new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); |
3433 | new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); |
3507 | |
3434 | |
3508 | for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) |
3435 | for (x = car (closure_code (SCHEME_V->code)), y = args; is_pair (x); x = cdr (x), y = cdr (y)) |
3509 | { |
3436 | { |
3510 | if (y == NIL) |
3437 | if (y == NIL) |
3511 | Error_0 ("not enough arguments"); |
3438 | Error_0 ("not enough arguments"); |
3512 | else |
3439 | else |
3513 | new_slot_in_env (SCHEME_A_ car (x), car (y)); |
3440 | new_slot_in_env (SCHEME_A_ car (x), car (y)); |
… | |
… | |
3531 | s_goto (OP_BEGIN); |
3458 | s_goto (OP_BEGIN); |
3532 | } |
3459 | } |
3533 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3460 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3534 | { |
3461 | { |
3535 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3462 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3536 | s_return (SCHEME_V->args != NIL ? car (SCHEME_V->args) : NIL); |
3463 | s_return (args != NIL ? car (args) : NIL); |
3537 | } |
3464 | } |
3538 | else |
3465 | else |
3539 | Error_0 ("illegal function"); |
3466 | Error_0 ("illegal function"); |
3540 | |
3467 | |
3541 | case OP_DOMACRO: /* do macro */ |
3468 | case OP_DOMACRO: /* do macro */ |
… | |
… | |
3550 | { |
3477 | { |
3551 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3478 | pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); |
3552 | |
3479 | |
3553 | if (f != NIL) |
3480 | if (f != NIL) |
3554 | { |
3481 | { |
3555 | s_save (SCHEME_A_ OP_LAMBDA1, SCHEME_V->args, SCHEME_V->code); |
3482 | s_save (SCHEME_A_ OP_LAMBDA1, args, SCHEME_V->code); |
3556 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3483 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3557 | SCHEME_V->code = slot_value_in_env (f); |
3484 | SCHEME_V->code = slot_value_in_env (f); |
3558 | s_goto (OP_APPLY); |
3485 | s_goto (OP_APPLY); |
3559 | } |
3486 | } |
3560 | |
3487 | |
… | |
… | |
3571 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
3498 | s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); |
3572 | |
3499 | |
3573 | #endif |
3500 | #endif |
3574 | |
3501 | |
3575 | case OP_MKCLOSURE: /* make-closure */ |
3502 | case OP_MKCLOSURE: /* make-closure */ |
3576 | x = car (SCHEME_V->args); |
3503 | x = car (args); |
3577 | |
3504 | |
3578 | if (car (x) == SCHEME_V->LAMBDA) |
3505 | if (car (x) == SCHEME_V->LAMBDA) |
3579 | x = cdr (x); |
3506 | x = cdr (x); |
3580 | |
3507 | |
3581 | if (cdr (SCHEME_V->args) == NIL) |
3508 | if (cdr (args) == NIL) |
3582 | y = SCHEME_V->envir; |
3509 | y = SCHEME_V->envir; |
3583 | else |
3510 | else |
3584 | y = cadr (SCHEME_V->args); |
3511 | y = cadr (args); |
3585 | |
3512 | |
3586 | s_return (mk_closure (SCHEME_A_ x, y)); |
3513 | s_return (mk_closure (SCHEME_A_ x, y)); |
3587 | |
3514 | |
3588 | case OP_QUOTE: /* quote */ |
3515 | case OP_QUOTE: /* quote */ |
3589 | s_return (car (SCHEME_V->code)); |
3516 | s_return (car (SCHEME_V->code)); |
… | |
… | |
3621 | |
3548 | |
3622 | |
3549 | |
3623 | case OP_DEFP: /* defined? */ |
3550 | case OP_DEFP: /* defined? */ |
3624 | x = SCHEME_V->envir; |
3551 | x = SCHEME_V->envir; |
3625 | |
3552 | |
3626 | if (cdr (SCHEME_V->args) != NIL) |
3553 | if (cdr (args) != NIL) |
3627 | x = cadr (SCHEME_V->args); |
3554 | x = cadr (args); |
3628 | |
3555 | |
3629 | s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); |
3556 | s_retbool (find_slot_in_env (SCHEME_A_ x, car (args), 1) != NIL); |
3630 | |
3557 | |
3631 | case OP_SET0: /* set! */ |
3558 | case OP_SET0: /* set! */ |
3632 | if (is_immutable (car (SCHEME_V->code))) |
3559 | if (is_immutable (car (SCHEME_V->code))) |
3633 | Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); |
3560 | Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); |
3634 | |
3561 | |
… | |
… | |
3665 | |
3592 | |
3666 | case OP_IF1: /* if */ |
3593 | case OP_IF1: /* if */ |
3667 | if (is_true (SCHEME_V->value)) |
3594 | if (is_true (SCHEME_V->value)) |
3668 | SCHEME_V->code = car (SCHEME_V->code); |
3595 | SCHEME_V->code = car (SCHEME_V->code); |
3669 | else |
3596 | else |
3670 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because |
3597 | SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */ |
3671 | |
|
|
3672 | * car(NIL) = NIL */ |
|
|
3673 | s_goto (OP_EVAL); |
3598 | s_goto (OP_EVAL); |
3674 | |
3599 | |
3675 | case OP_LET0: /* let */ |
3600 | case OP_LET0: /* let */ |
3676 | SCHEME_V->args = NIL; |
3601 | SCHEME_V->args = NIL; |
3677 | SCHEME_V->value = SCHEME_V->code; |
3602 | SCHEME_V->value = SCHEME_V->code; |
3678 | SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); |
3603 | SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); |
3679 | s_goto (OP_LET1); |
3604 | s_goto (OP_LET1); |
3680 | |
3605 | |
3681 | case OP_LET1: /* let (calculate parameters) */ |
3606 | case OP_LET1: /* let (calculate parameters) */ |
3682 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3607 | args = cons (SCHEME_V->value, args); |
3683 | |
3608 | |
3684 | if (is_pair (SCHEME_V->code)) /* continue */ |
3609 | if (is_pair (SCHEME_V->code)) /* continue */ |
3685 | { |
3610 | { |
3686 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3611 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3687 | Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); |
3612 | Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); |
3688 | |
3613 | |
3689 | s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); |
3614 | s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code)); |
3690 | SCHEME_V->code = cadar (SCHEME_V->code); |
3615 | SCHEME_V->code = cadar (SCHEME_V->code); |
3691 | SCHEME_V->args = NIL; |
3616 | SCHEME_V->args = NIL; |
3692 | s_goto (OP_EVAL); |
3617 | s_goto (OP_EVAL); |
3693 | } |
3618 | } |
3694 | else /* end */ |
3619 | else /* end */ |
3695 | { |
3620 | { |
3696 | SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); |
3621 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3697 | SCHEME_V->code = car (SCHEME_V->args); |
3622 | SCHEME_V->code = car (args); |
3698 | SCHEME_V->args = cdr (SCHEME_V->args); |
3623 | SCHEME_V->args = cdr (args); |
3699 | s_goto (OP_LET2); |
3624 | s_goto (OP_LET2); |
3700 | } |
3625 | } |
3701 | |
3626 | |
3702 | case OP_LET2: /* let */ |
3627 | case OP_LET2: /* let */ |
3703 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3628 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3704 | |
3629 | |
3705 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; |
3630 | for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args; |
3706 | y != NIL; x = cdr (x), y = cdr (y)) |
3631 | y != NIL; x = cdr (x), y = cdr (y)) |
3707 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3632 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3708 | |
3633 | |
3709 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3634 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3710 | { |
3635 | { |
3711 | for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) |
3636 | for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x)) |
3712 | { |
3637 | { |
3713 | if (!is_pair (x)) |
3638 | if (!is_pair (x)) |
3714 | Error_1 ("Bad syntax of binding in let :", x); |
3639 | Error_1 ("Bad syntax of binding in let :", x); |
3715 | |
3640 | |
3716 | if (!is_list (SCHEME_A_ car (x))) |
3641 | if (!is_list (SCHEME_A_ car (x))) |
3717 | Error_1 ("Bad syntax of binding in let :", car (x)); |
3642 | Error_1 ("Bad syntax of binding in let :", car (x)); |
3718 | |
3643 | |
3719 | SCHEME_V->args = cons (caar (x), SCHEME_V->args); |
3644 | args = cons (caar (x), args); |
3720 | } |
3645 | } |
3721 | |
3646 | |
3722 | x = |
|
|
3723 | mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args), cddr (SCHEME_V->code)), |
3647 | x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)), |
3724 | SCHEME_V->envir); |
3648 | SCHEME_V->envir); |
3725 | new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); |
3649 | new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); |
3726 | SCHEME_V->code = cddr (SCHEME_V->code); |
3650 | SCHEME_V->code = cddr (SCHEME_V->code); |
3727 | SCHEME_V->args = NIL; |
|
|
3728 | } |
3651 | } |
3729 | else |
3652 | else |
3730 | { |
3653 | { |
3731 | SCHEME_V->code = cdr (SCHEME_V->code); |
3654 | SCHEME_V->code = cdr (SCHEME_V->code); |
|
|
3655 | } |
|
|
3656 | |
3732 | SCHEME_V->args = NIL; |
3657 | SCHEME_V->args = NIL; |
3733 | } |
|
|
3734 | |
|
|
3735 | s_goto (OP_BEGIN); |
3658 | s_goto (OP_BEGIN); |
3736 | |
3659 | |
3737 | case OP_LET0AST: /* let* */ |
3660 | case OP_LET0AST: /* let* */ |
3738 | if (car (SCHEME_V->code) == NIL) |
3661 | if (car (SCHEME_V->code) == NIL) |
3739 | { |
3662 | { |
… | |
… | |
3757 | new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); |
3680 | new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); |
3758 | SCHEME_V->code = cdr (SCHEME_V->code); |
3681 | SCHEME_V->code = cdr (SCHEME_V->code); |
3759 | |
3682 | |
3760 | if (is_pair (SCHEME_V->code)) /* continue */ |
3683 | if (is_pair (SCHEME_V->code)) /* continue */ |
3761 | { |
3684 | { |
3762 | s_save (SCHEME_A_ OP_LET2AST, SCHEME_V->args, SCHEME_V->code); |
3685 | s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); |
3763 | SCHEME_V->code = cadar (SCHEME_V->code); |
3686 | SCHEME_V->code = cadar (SCHEME_V->code); |
3764 | SCHEME_V->args = NIL; |
3687 | SCHEME_V->args = NIL; |
3765 | s_goto (OP_EVAL); |
3688 | s_goto (OP_EVAL); |
3766 | } |
3689 | } |
3767 | else /* end */ |
3690 | else /* end */ |
3768 | { |
3691 | { |
3769 | SCHEME_V->code = SCHEME_V->args; |
3692 | SCHEME_V->code = args; |
3770 | SCHEME_V->args = NIL; |
3693 | SCHEME_V->args = NIL; |
3771 | s_goto (OP_BEGIN); |
3694 | s_goto (OP_BEGIN); |
3772 | } |
3695 | } |
3773 | |
3696 | |
3774 | case OP_LET0REC: /* letrec */ |
3697 | case OP_LET0REC: /* letrec */ |
… | |
… | |
3777 | SCHEME_V->value = SCHEME_V->code; |
3700 | SCHEME_V->value = SCHEME_V->code; |
3778 | SCHEME_V->code = car (SCHEME_V->code); |
3701 | SCHEME_V->code = car (SCHEME_V->code); |
3779 | s_goto (OP_LET1REC); |
3702 | s_goto (OP_LET1REC); |
3780 | |
3703 | |
3781 | case OP_LET1REC: /* letrec (calculate parameters) */ |
3704 | case OP_LET1REC: /* letrec (calculate parameters) */ |
3782 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3705 | args = cons (SCHEME_V->value, args); |
3783 | |
3706 | |
3784 | if (is_pair (SCHEME_V->code)) /* continue */ |
3707 | if (is_pair (SCHEME_V->code)) /* continue */ |
3785 | { |
3708 | { |
3786 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3709 | if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) |
3787 | Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); |
3710 | Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); |
3788 | |
3711 | |
3789 | s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); |
3712 | s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code)); |
3790 | SCHEME_V->code = cadar (SCHEME_V->code); |
3713 | SCHEME_V->code = cadar (SCHEME_V->code); |
3791 | SCHEME_V->args = NIL; |
3714 | SCHEME_V->args = NIL; |
3792 | s_goto (OP_EVAL); |
3715 | s_goto (OP_EVAL); |
3793 | } |
3716 | } |
3794 | else /* end */ |
3717 | else /* end */ |
3795 | { |
3718 | { |
3796 | SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); |
3719 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3797 | SCHEME_V->code = car (SCHEME_V->args); |
3720 | SCHEME_V->code = car (args); |
3798 | SCHEME_V->args = cdr (SCHEME_V->args); |
3721 | SCHEME_V->args = cdr (args); |
3799 | s_goto (OP_LET2REC); |
3722 | s_goto (OP_LET2REC); |
3800 | } |
3723 | } |
3801 | |
3724 | |
3802 | case OP_LET2REC: /* letrec */ |
3725 | case OP_LET2REC: /* letrec */ |
3803 | for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) |
3726 | for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y)) |
3804 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3727 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3805 | |
3728 | |
3806 | SCHEME_V->code = cdr (SCHEME_V->code); |
3729 | SCHEME_V->code = cdr (SCHEME_V->code); |
3807 | SCHEME_V->args = NIL; |
3730 | SCHEME_V->args = NIL; |
3808 | s_goto (OP_BEGIN); |
3731 | s_goto (OP_BEGIN); |
… | |
… | |
3894 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3817 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3895 | SCHEME_V->code = car (SCHEME_V->code); |
3818 | SCHEME_V->code = car (SCHEME_V->code); |
3896 | s_goto (OP_EVAL); |
3819 | s_goto (OP_EVAL); |
3897 | |
3820 | |
3898 | case OP_C1STREAM: /* cons-stream */ |
3821 | case OP_C1STREAM: /* cons-stream */ |
3899 | SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register SCHEME_V->args for gc */ |
3822 | SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register args for gc */ |
3900 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3823 | x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); |
3901 | set_typeflag (x, T_PROMISE); |
3824 | set_typeflag (x, T_PROMISE); |
3902 | s_return (cons (SCHEME_V->args, x)); |
3825 | s_return (cons (args, x)); |
3903 | |
3826 | |
3904 | case OP_MACRO0: /* macro */ |
3827 | case OP_MACRO0: /* macro */ |
3905 | if (is_pair (car (SCHEME_V->code))) |
3828 | if (is_pair (car (SCHEME_V->code))) |
3906 | { |
3829 | { |
3907 | x = caar (SCHEME_V->code); |
3830 | x = caar (SCHEME_V->code); |
… | |
… | |
3940 | { |
3863 | { |
3941 | if (!is_pair (y = caar (x))) |
3864 | if (!is_pair (y = caar (x))) |
3942 | break; |
3865 | break; |
3943 | |
3866 | |
3944 | for (; y != NIL; y = cdr (y)) |
3867 | for (; y != NIL; y = cdr (y)) |
3945 | { |
|
|
3946 | if (eqv (car (y), SCHEME_V->value)) |
3868 | if (eqv (car (y), SCHEME_V->value)) |
3947 | break; |
3869 | break; |
3948 | } |
|
|
3949 | |
3870 | |
3950 | if (y != NIL) |
3871 | if (y != NIL) |
3951 | break; |
3872 | break; |
3952 | } |
3873 | } |
3953 | |
3874 | |
… | |
… | |
3973 | s_goto (OP_BEGIN); |
3894 | s_goto (OP_BEGIN); |
3974 | else |
3895 | else |
3975 | s_return (NIL); |
3896 | s_return (NIL); |
3976 | |
3897 | |
3977 | case OP_PAPPLY: /* apply */ |
3898 | case OP_PAPPLY: /* apply */ |
3978 | SCHEME_V->code = car (SCHEME_V->args); |
3899 | SCHEME_V->code = car (args); |
3979 | SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); |
3900 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
3980 | /*SCHEME_V->args = cadr(SCHEME_V->args); */ |
3901 | /*SCHEME_V->args = cadr(args); */ |
3981 | s_goto (OP_APPLY); |
3902 | s_goto (OP_APPLY); |
3982 | |
3903 | |
3983 | case OP_PEVAL: /* eval */ |
3904 | case OP_PEVAL: /* eval */ |
3984 | if (cdr (SCHEME_V->args) != NIL) |
3905 | if (cdr (args) != NIL) |
3985 | SCHEME_V->envir = cadr (SCHEME_V->args); |
3906 | SCHEME_V->envir = cadr (args); |
3986 | |
3907 | |
3987 | SCHEME_V->code = car (SCHEME_V->args); |
3908 | SCHEME_V->code = car (args); |
3988 | s_goto (OP_EVAL); |
3909 | s_goto (OP_EVAL); |
3989 | |
3910 | |
3990 | case OP_CONTINUATION: /* call-with-current-continuation */ |
3911 | case OP_CONTINUATION: /* call-with-current-continuation */ |
3991 | SCHEME_V->code = car (SCHEME_V->args); |
3912 | SCHEME_V->code = car (args); |
3992 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_V)), NIL); |
3913 | SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); |
3993 | s_goto (OP_APPLY); |
3914 | s_goto (OP_APPLY); |
3994 | } |
3915 | } |
3995 | |
3916 | |
3996 | return S_T; |
3917 | abort (); |
3997 | } |
3918 | } |
3998 | |
3919 | |
3999 | static pointer |
3920 | static int |
4000 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
3921 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
4001 | { |
3922 | { |
4002 | pointer x; |
3923 | pointer args = SCHEME_V->args; |
|
|
3924 | pointer x = car (args); |
4003 | num v; |
3925 | num v; |
4004 | |
3926 | |
4005 | #if USE_MATH |
3927 | #if USE_MATH |
4006 | RVALUE dd; |
3928 | RVALUE dd; |
4007 | #endif |
3929 | #endif |
4008 | |
3930 | |
4009 | switch (op) |
3931 | switch (op) |
4010 | { |
3932 | { |
4011 | #if USE_MATH |
3933 | #if USE_MATH |
4012 | |
|
|
4013 | case OP_INEX2EX: /* inexact->exact */ |
3934 | case OP_INEX2EX: /* inexact->exact */ |
4014 | x = car (SCHEME_V->args); |
|
|
4015 | |
|
|
4016 | if (num_is_integer (x)) |
3935 | if (num_is_integer (x)) |
4017 | s_return (x); |
3936 | s_return (x); |
4018 | else if (modf (rvalue_unchecked (x), &dd) == 0.0) |
3937 | else if (modf (rvalue_unchecked (x), &dd) == 0) |
4019 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
3938 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4020 | else |
3939 | else |
4021 | Error_1 ("inexact->exact: not integral:", x); |
3940 | Error_1 ("inexact->exact: not integral:", x); |
4022 | |
3941 | |
4023 | case OP_EXP: |
|
|
4024 | x = car (SCHEME_V->args); |
|
|
4025 | s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
3942 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
4026 | |
|
|
4027 | case OP_LOG: |
|
|
4028 | x = car (SCHEME_V->args); |
|
|
4029 | s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
3943 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
4030 | |
|
|
4031 | case OP_SIN: |
|
|
4032 | x = car (SCHEME_V->args); |
|
|
4033 | s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
3944 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
4034 | |
|
|
4035 | case OP_COS: |
|
|
4036 | x = car (SCHEME_V->args); |
|
|
4037 | s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
3945 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
4038 | |
|
|
4039 | case OP_TAN: |
|
|
4040 | x = car (SCHEME_V->args); |
|
|
4041 | s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
3946 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
4042 | |
|
|
4043 | case OP_ASIN: |
|
|
4044 | x = car (SCHEME_V->args); |
|
|
4045 | s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
3947 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
4046 | |
|
|
4047 | case OP_ACOS: |
|
|
4048 | x = car (SCHEME_V->args); |
|
|
4049 | s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3948 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
4050 | |
3949 | |
4051 | case OP_ATAN: |
3950 | case OP_ATAN: |
4052 | x = car (SCHEME_V->args); |
|
|
4053 | |
|
|
4054 | if (cdr (SCHEME_V->args) == NIL) |
3951 | if (cdr (args) == NIL) |
4055 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
3952 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
4056 | else |
3953 | else |
4057 | { |
3954 | { |
4058 | pointer y = cadr (SCHEME_V->args); |
3955 | pointer y = cadr (args); |
4059 | |
|
|
4060 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
3956 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
4061 | } |
3957 | } |
4062 | |
3958 | |
4063 | case OP_SQRT: |
3959 | case OP_SQRT: |
4064 | x = car (SCHEME_V->args); |
|
|
4065 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
3960 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
4066 | |
3961 | |
4067 | case OP_EXPT: |
3962 | case OP_EXPT: |
4068 | { |
3963 | { |
4069 | RVALUE result; |
3964 | RVALUE result; |
4070 | int real_result = 1; |
3965 | int real_result = 1; |
4071 | pointer y = cadr (SCHEME_V->args); |
3966 | pointer y = cadr (args); |
4072 | |
|
|
4073 | x = car (SCHEME_V->args); |
|
|
4074 | |
3967 | |
4075 | if (num_is_integer (x) && num_is_integer (y)) |
3968 | if (num_is_integer (x) && num_is_integer (y)) |
4076 | real_result = 0; |
3969 | real_result = 0; |
4077 | |
3970 | |
4078 | /* This 'if' is an R5RS compatibility fix. */ |
3971 | /* This 'if' is an R5RS compatibility fix. */ |
4079 | /* NOTE: Remove this 'if' fix for R6RS. */ |
3972 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4080 | if (rvalue (x) == 0 && rvalue (y) < 0) |
3973 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4081 | result = 0.0; |
3974 | result = 0; |
4082 | else |
3975 | else |
4083 | result = pow (rvalue (x), rvalue (y)); |
3976 | result = pow (rvalue (x), rvalue (y)); |
4084 | |
3977 | |
4085 | /* Before returning integer result make sure we can. */ |
3978 | /* Before returning integer result make sure we can. */ |
4086 | /* If the test fails, result is too big for integer. */ |
3979 | /* If the test fails, result is too big for integer. */ |
4087 | if (!real_result) |
3980 | if (!real_result) |
4088 | { |
3981 | { |
4089 | long result_as_long = (long) result; |
3982 | long result_as_long = result; |
4090 | |
3983 | |
4091 | if (result != (RVALUE) result_as_long) |
3984 | if (result != (RVALUE) result_as_long) |
4092 | real_result = 1; |
3985 | real_result = 1; |
4093 | } |
3986 | } |
4094 | |
3987 | |
… | |
… | |
4096 | s_return (mk_real (SCHEME_A_ result)); |
3989 | s_return (mk_real (SCHEME_A_ result)); |
4097 | else |
3990 | else |
4098 | s_return (mk_integer (SCHEME_A_ result)); |
3991 | s_return (mk_integer (SCHEME_A_ result)); |
4099 | } |
3992 | } |
4100 | |
3993 | |
4101 | case OP_FLOOR: |
|
|
4102 | x = car (SCHEME_V->args); |
|
|
4103 | s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
3994 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
4104 | |
|
|
4105 | case OP_CEILING: |
|
|
4106 | x = car (SCHEME_V->args); |
|
|
4107 | s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
3995 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
4108 | |
3996 | |
4109 | case OP_TRUNCATE: |
3997 | case OP_TRUNCATE: |
4110 | { |
3998 | { |
4111 | RVALUE rvalue_of_x; |
3999 | RVALUE rvalue_of_x; |
4112 | |
4000 | |
4113 | x = car (SCHEME_V->args); |
|
|
4114 | rvalue_of_x = rvalue (x); |
4001 | rvalue_of_x = rvalue (x); |
4115 | |
4002 | |
4116 | if (rvalue_of_x > 0) |
4003 | if (rvalue_of_x > 0) |
4117 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
4004 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
4118 | else |
4005 | else |
4119 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
4006 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
4120 | } |
4007 | } |
4121 | |
4008 | |
4122 | case OP_ROUND: |
4009 | case OP_ROUND: |
4123 | x = car (SCHEME_V->args); |
|
|
4124 | |
|
|
4125 | if (num_is_integer (x)) |
4010 | if (num_is_integer (x)) |
4126 | s_return (x); |
4011 | s_return (x); |
4127 | |
4012 | |
4128 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
4013 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
4129 | #endif |
4014 | #endif |
4130 | |
4015 | |
4131 | case OP_ADD: /* + */ |
4016 | case OP_ADD: /* + */ |
4132 | v = num_zero; |
4017 | v = num_zero; |
4133 | |
4018 | |
4134 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4019 | for (x = args; x != NIL; x = cdr (x)) |
4135 | v = num_add (v, nvalue (car (x))); |
4020 | v = num_op ('+', v, nvalue (car (x))); |
4136 | |
4021 | |
4137 | s_return (mk_number (SCHEME_A_ v)); |
4022 | s_return (mk_number (SCHEME_A_ v)); |
4138 | |
4023 | |
4139 | case OP_MUL: /* * */ |
4024 | case OP_MUL: /* * */ |
4140 | v = num_one; |
4025 | v = num_one; |
4141 | |
4026 | |
4142 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4027 | for (x = args; x != NIL; x = cdr (x)) |
4143 | v = num_mul (v, nvalue (car (x))); |
4028 | v = num_op ('*', v, nvalue (car (x))); |
4144 | |
4029 | |
4145 | s_return (mk_number (SCHEME_A_ v)); |
4030 | s_return (mk_number (SCHEME_A_ v)); |
4146 | |
4031 | |
4147 | case OP_SUB: /* - */ |
4032 | case OP_SUB: /* - */ |
4148 | if (cdr (SCHEME_V->args) == NIL) |
4033 | if (cdr (args) == NIL) |
4149 | { |
4034 | { |
4150 | x = SCHEME_V->args; |
4035 | x = args; |
4151 | v = num_zero; |
4036 | v = num_zero; |
4152 | } |
4037 | } |
4153 | else |
4038 | else |
4154 | { |
4039 | { |
4155 | x = cdr (SCHEME_V->args); |
4040 | x = cdr (args); |
4156 | v = nvalue (car (SCHEME_V->args)); |
4041 | v = nvalue (car (args)); |
4157 | } |
4042 | } |
4158 | |
4043 | |
4159 | for (; x != NIL; x = cdr (x)) |
4044 | for (; x != NIL; x = cdr (x)) |
4160 | v = num_sub (v, nvalue (car (x))); |
4045 | v = num_op ('-', v, nvalue (car (x))); |
4161 | |
4046 | |
4162 | s_return (mk_number (SCHEME_A_ v)); |
4047 | s_return (mk_number (SCHEME_A_ v)); |
4163 | |
4048 | |
4164 | case OP_DIV: /* / */ |
4049 | case OP_DIV: /* / */ |
4165 | if (cdr (SCHEME_V->args) == NIL) |
4050 | if (cdr (args) == NIL) |
4166 | { |
4051 | { |
4167 | x = SCHEME_V->args; |
4052 | x = args; |
4168 | v = num_one; |
4053 | v = num_one; |
4169 | } |
4054 | } |
4170 | else |
4055 | else |
4171 | { |
4056 | { |
4172 | x = cdr (SCHEME_V->args); |
4057 | x = cdr (args); |
4173 | v = nvalue (car (SCHEME_V->args)); |
4058 | v = nvalue (car (args)); |
4174 | } |
4059 | } |
4175 | |
4060 | |
4176 | for (; x != NIL; x = cdr (x)) |
4061 | for (; x != NIL; x = cdr (x)) |
4177 | { |
4062 | { |
4178 | if (!is_zero_rvalue (rvalue (car (x)))) |
4063 | if (!is_zero_rvalue (rvalue (car (x)))) |
… | |
… | |
4182 | } |
4067 | } |
4183 | |
4068 | |
4184 | s_return (mk_number (SCHEME_A_ v)); |
4069 | s_return (mk_number (SCHEME_A_ v)); |
4185 | |
4070 | |
4186 | case OP_INTDIV: /* quotient */ |
4071 | case OP_INTDIV: /* quotient */ |
4187 | if (cdr (SCHEME_V->args) == NIL) |
4072 | if (cdr (args) == NIL) |
4188 | { |
4073 | { |
4189 | x = SCHEME_V->args; |
4074 | x = args; |
4190 | v = num_one; |
4075 | v = num_one; |
4191 | } |
4076 | } |
4192 | else |
4077 | else |
4193 | { |
4078 | { |
4194 | x = cdr (SCHEME_V->args); |
4079 | x = cdr (args); |
4195 | v = nvalue (car (SCHEME_V->args)); |
4080 | v = nvalue (car (args)); |
4196 | } |
4081 | } |
4197 | |
4082 | |
4198 | for (; x != NIL; x = cdr (x)) |
4083 | for (; x != NIL; x = cdr (x)) |
4199 | { |
4084 | { |
4200 | if (ivalue (car (x)) != 0) |
4085 | if (ivalue (car (x)) != 0) |
4201 | v = num_intdiv (v, nvalue (car (x))); |
4086 | v = num_op ('/', v, nvalue (car (x))); |
4202 | else |
4087 | else |
4203 | Error_0 ("quotient: division by zero"); |
4088 | Error_0 ("quotient: division by zero"); |
4204 | } |
4089 | } |
4205 | |
4090 | |
4206 | s_return (mk_number (SCHEME_A_ v)); |
4091 | s_return (mk_number (SCHEME_A_ v)); |
4207 | |
4092 | |
4208 | case OP_REM: /* remainder */ |
4093 | case OP_REM: /* remainder */ |
4209 | v = nvalue (car (SCHEME_V->args)); |
4094 | v = nvalue (x); |
4210 | |
4095 | |
4211 | if (ivalue (cadr (SCHEME_V->args)) != 0) |
4096 | if (ivalue (cadr (args)) != 0) |
4212 | v = num_rem (v, nvalue (cadr (SCHEME_V->args))); |
4097 | v = num_rem (v, nvalue (cadr (args))); |
4213 | else |
4098 | else |
4214 | Error_0 ("remainder: division by zero"); |
4099 | Error_0 ("remainder: division by zero"); |
4215 | |
4100 | |
4216 | s_return (mk_number (SCHEME_A_ v)); |
4101 | s_return (mk_number (SCHEME_A_ v)); |
4217 | |
4102 | |
4218 | case OP_MOD: /* modulo */ |
4103 | case OP_MOD: /* modulo */ |
4219 | v = nvalue (car (SCHEME_V->args)); |
4104 | v = nvalue (x); |
4220 | |
4105 | |
4221 | if (ivalue (cadr (SCHEME_V->args)) != 0) |
4106 | if (ivalue (cadr (args)) != 0) |
4222 | v = num_mod (v, nvalue (cadr (SCHEME_V->args))); |
4107 | v = num_mod (v, nvalue (cadr (args))); |
4223 | else |
4108 | else |
4224 | Error_0 ("modulo: division by zero"); |
4109 | Error_0 ("modulo: division by zero"); |
4225 | |
4110 | |
4226 | s_return (mk_number (SCHEME_A_ v)); |
4111 | s_return (mk_number (SCHEME_A_ v)); |
4227 | |
4112 | |
4228 | case OP_CAR: /* car */ |
4113 | case OP_CAR: /* car */ |
4229 | s_return (caar (SCHEME_V->args)); |
4114 | s_return (caar (args)); |
4230 | |
4115 | |
4231 | case OP_CDR: /* cdr */ |
4116 | case OP_CDR: /* cdr */ |
4232 | s_return (cdar (SCHEME_V->args)); |
4117 | s_return (cdar (args)); |
4233 | |
4118 | |
4234 | case OP_CONS: /* cons */ |
4119 | case OP_CONS: /* cons */ |
4235 | set_cdr (SCHEME_V->args, cadr (SCHEME_V->args)); |
4120 | set_cdr (args, cadr (args)); |
4236 | s_return (SCHEME_V->args); |
4121 | s_return (args); |
4237 | |
4122 | |
4238 | case OP_SETCAR: /* set-car! */ |
4123 | case OP_SETCAR: /* set-car! */ |
4239 | if (!is_immutable (car (SCHEME_V->args))) |
4124 | if (!is_immutable (x)) |
4240 | { |
4125 | { |
4241 | set_car (car (SCHEME_V->args), cadr (SCHEME_V->args)); |
4126 | set_car (x, cadr (args)); |
4242 | s_return (car (SCHEME_V->args)); |
4127 | s_return (car (args)); |
4243 | } |
4128 | } |
4244 | else |
4129 | else |
4245 | Error_0 ("set-car!: unable to alter immutable pair"); |
4130 | Error_0 ("set-car!: unable to alter immutable pair"); |
4246 | |
4131 | |
4247 | case OP_SETCDR: /* set-cdr! */ |
4132 | case OP_SETCDR: /* set-cdr! */ |
4248 | if (!is_immutable (car (SCHEME_V->args))) |
4133 | if (!is_immutable (x)) |
4249 | { |
4134 | { |
4250 | set_cdr (car (SCHEME_V->args), cadr (SCHEME_V->args)); |
4135 | set_cdr (x, cadr (args)); |
4251 | s_return (car (SCHEME_V->args)); |
4136 | s_return (car (args)); |
4252 | } |
4137 | } |
4253 | else |
4138 | else |
4254 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4139 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4255 | |
4140 | |
4256 | case OP_CHAR2INT: /* char->integer */ |
4141 | case OP_CHAR2INT: /* char->integer */ |
4257 | s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4142 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4258 | |
4143 | |
4259 | case OP_INT2CHAR: /* integer->char */ |
4144 | case OP_INT2CHAR: /* integer->char */ |
4260 | s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4145 | s_return (mk_character (SCHEME_A_ ivalue (x))); |
4261 | |
4146 | |
4262 | case OP_CHARUPCASE: |
4147 | case OP_CHARUPCASE: |
4263 | { |
4148 | { |
4264 | unsigned char c = ivalue (car (SCHEME_V->args)); |
4149 | unsigned char c = ivalue (x); |
4265 | c = toupper (c); |
4150 | c = toupper (c); |
4266 | s_return (mk_character (SCHEME_A_ c)); |
4151 | s_return (mk_character (SCHEME_A_ c)); |
4267 | } |
4152 | } |
4268 | |
4153 | |
4269 | case OP_CHARDNCASE: |
4154 | case OP_CHARDNCASE: |
4270 | { |
4155 | { |
4271 | unsigned char c = ivalue (car (SCHEME_V->args)); |
4156 | unsigned char c = ivalue (x); |
4272 | c = tolower (c); |
4157 | c = tolower (c); |
4273 | s_return (mk_character (SCHEME_A_ c)); |
4158 | s_return (mk_character (SCHEME_A_ c)); |
4274 | } |
4159 | } |
4275 | |
4160 | |
4276 | case OP_STR2SYM: /* string->symbol */ |
4161 | case OP_STR2SYM: /* string->symbol */ |
4277 | s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); |
4162 | s_return (mk_symbol (SCHEME_A_ strvalue (x))); |
4278 | |
4163 | |
4279 | case OP_STR2ATOM: /* string->atom */ |
4164 | case OP_STR2ATOM: /* string->atom */ |
4280 | { |
4165 | { |
4281 | char *s = strvalue (car (SCHEME_V->args)); |
4166 | char *s = strvalue (x); |
4282 | long pf = 0; |
4167 | long pf = 0; |
4283 | |
4168 | |
4284 | if (cdr (SCHEME_V->args) != NIL) |
4169 | if (cdr (args) != NIL) |
4285 | { |
4170 | { |
4286 | /* we know cadr(SCHEME_V->args) is a natural number */ |
4171 | /* we know cadr(args) is a natural number */ |
4287 | /* see if it is 2, 8, 10, or 16, or error */ |
4172 | /* see if it is 2, 8, 10, or 16, or error */ |
4288 | pf = ivalue_unchecked (cadr (SCHEME_V->args)); |
4173 | pf = ivalue_unchecked (cadr (args)); |
4289 | |
4174 | |
4290 | if (pf == 16 || pf == 10 || pf == 8 || pf == 2) |
4175 | if (pf == 16 || pf == 10 || pf == 8 || pf == 2) |
4291 | { |
4176 | { |
4292 | /* base is OK */ |
4177 | /* base is OK */ |
4293 | } |
4178 | } |
4294 | else |
4179 | else |
4295 | pf = -1; |
4180 | pf = -1; |
4296 | } |
4181 | } |
4297 | |
4182 | |
4298 | if (pf < 0) |
4183 | if (pf < 0) |
4299 | Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); |
4184 | Error_1 ("string->atom: bad base:", cadr (args)); |
4300 | else if (*s == '#') /* no use of base! */ |
4185 | else if (*s == '#') /* no use of base! */ |
4301 | s_return (mk_sharp_const (SCHEME_A_ s + 1)); |
4186 | s_return (mk_sharp_const (SCHEME_A_ s + 1)); |
4302 | else |
4187 | else |
4303 | { |
4188 | { |
4304 | if (pf == 0 || pf == 10) |
4189 | if (pf == 0 || pf == 10) |
… | |
… | |
4315 | } |
4200 | } |
4316 | } |
4201 | } |
4317 | } |
4202 | } |
4318 | |
4203 | |
4319 | case OP_SYM2STR: /* symbol->string */ |
4204 | case OP_SYM2STR: /* symbol->string */ |
4320 | x = mk_string (SCHEME_A_ symname (car (SCHEME_V->args))); |
4205 | x = mk_string (SCHEME_A_ symname (x)); |
4321 | setimmutable (x); |
4206 | setimmutable (x); |
4322 | s_return (x); |
4207 | s_return (x); |
4323 | |
4208 | |
4324 | case OP_ATOM2STR: /* atom->string */ |
4209 | case OP_ATOM2STR: /* atom->string */ |
4325 | { |
4210 | { |
4326 | long pf = 0; |
4211 | long pf = 0; |
4327 | |
4212 | |
4328 | x = car (SCHEME_V->args); |
|
|
4329 | |
|
|
4330 | if (cdr (SCHEME_V->args) != NIL) |
4213 | if (cdr (args) != NIL) |
4331 | { |
4214 | { |
4332 | /* we know cadr(SCHEME_V->args) is a natural number */ |
4215 | /* we know cadr(args) is a natural number */ |
4333 | /* see if it is 2, 8, 10, or 16, or error */ |
4216 | /* see if it is 2, 8, 10, or 16, or error */ |
4334 | pf = ivalue_unchecked (cadr (SCHEME_V->args)); |
4217 | pf = ivalue_unchecked (cadr (args)); |
4335 | |
4218 | |
4336 | if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) |
4219 | if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) |
4337 | { |
4220 | { |
4338 | /* base is OK */ |
4221 | /* base is OK */ |
4339 | } |
4222 | } |
4340 | else |
4223 | else |
4341 | pf = -1; |
4224 | pf = -1; |
4342 | } |
4225 | } |
4343 | |
4226 | |
4344 | if (pf < 0) |
4227 | if (pf < 0) |
4345 | Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); |
4228 | Error_1 ("atom->string: bad base:", cadr (args)); |
4346 | else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) |
4229 | else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) |
4347 | { |
4230 | { |
4348 | char *p; |
4231 | char *p; |
4349 | int len; |
4232 | int len; |
4350 | |
4233 | |
… | |
… | |
4358 | case OP_MKSTRING: /* make-string */ |
4241 | case OP_MKSTRING: /* make-string */ |
4359 | { |
4242 | { |
4360 | int fill = ' '; |
4243 | int fill = ' '; |
4361 | int len; |
4244 | int len; |
4362 | |
4245 | |
4363 | len = ivalue (car (SCHEME_V->args)); |
4246 | len = ivalue (x); |
4364 | |
4247 | |
4365 | if (cdr (SCHEME_V->args) != NIL) |
4248 | if (cdr (args) != NIL) |
4366 | fill = charvalue (cadr (SCHEME_V->args)); |
4249 | fill = charvalue (cadr (args)); |
4367 | |
4250 | |
4368 | s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); |
4251 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4369 | } |
4252 | } |
4370 | |
4253 | |
4371 | case OP_STRLEN: /* string-length */ |
4254 | case OP_STRLEN: /* string-length */ |
4372 | s_return (mk_integer (SCHEME_A_ strlength (car (SCHEME_V->args)))); |
4255 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4373 | |
4256 | |
4374 | case OP_STRREF: /* string-ref */ |
4257 | case OP_STRREF: /* string-ref */ |
4375 | { |
4258 | { |
4376 | char *str; |
4259 | char *str; |
4377 | int index; |
4260 | int index; |
4378 | |
4261 | |
4379 | str = strvalue (car (SCHEME_V->args)); |
4262 | str = strvalue (x); |
4380 | |
4263 | |
4381 | index = ivalue (cadr (SCHEME_V->args)); |
4264 | index = ivalue (cadr (args)); |
4382 | |
4265 | |
4383 | if (index >= strlength (car (SCHEME_V->args))) |
4266 | if (index >= strlength (x)) |
4384 | Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); |
4267 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4385 | |
4268 | |
4386 | s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); |
4269 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4387 | } |
4270 | } |
4388 | |
4271 | |
4389 | case OP_STRSET: /* string-set! */ |
4272 | case OP_STRSET: /* string-set! */ |
4390 | { |
4273 | { |
4391 | char *str; |
4274 | char *str; |
4392 | int index; |
4275 | int index; |
4393 | int c; |
4276 | int c; |
4394 | |
4277 | |
4395 | if (is_immutable (car (SCHEME_V->args))) |
4278 | if (is_immutable (x)) |
4396 | Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); |
4279 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4397 | |
4280 | |
4398 | str = strvalue (car (SCHEME_V->args)); |
4281 | str = strvalue (x); |
4399 | |
4282 | |
4400 | index = ivalue (cadr (SCHEME_V->args)); |
4283 | index = ivalue (cadr (args)); |
4401 | |
4284 | |
4402 | if (index >= strlength (car (SCHEME_V->args))) |
4285 | if (index >= strlength (x)) |
4403 | Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); |
4286 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4404 | |
4287 | |
4405 | c = charvalue (caddr (SCHEME_V->args)); |
4288 | c = charvalue (caddr (args)); |
4406 | |
4289 | |
4407 | str[index] = (char) c; |
4290 | str[index] = c; |
4408 | s_return (car (SCHEME_V->args)); |
4291 | s_return (car (args)); |
4409 | } |
4292 | } |
4410 | |
4293 | |
4411 | case OP_STRAPPEND: /* string-append */ |
4294 | case OP_STRAPPEND: /* string-append */ |
4412 | { |
4295 | { |
4413 | /* in 1.29 string-append was in Scheme in init.scm but was too slow */ |
4296 | /* in 1.29 string-append was in Scheme in init.scm but was too slow */ |
4414 | int len = 0; |
4297 | int len = 0; |
4415 | pointer newstr; |
4298 | pointer newstr; |
4416 | char *pos; |
4299 | char *pos; |
4417 | |
4300 | |
4418 | /* compute needed length for new string */ |
4301 | /* compute needed length for new string */ |
4419 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4302 | for (x = args; x != NIL; x = cdr (x)) |
4420 | len += strlength (car (x)); |
4303 | len += strlength (car (x)); |
4421 | |
4304 | |
4422 | newstr = mk_empty_string (SCHEME_A_ len, ' '); |
4305 | newstr = mk_empty_string (SCHEME_A_ len, ' '); |
4423 | |
4306 | |
4424 | /* store the contents of the argument strings into the new string */ |
4307 | /* store the contents of the argument strings into the new string */ |
4425 | for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) |
4308 | for (pos = strvalue (newstr), x = args; x != NIL; pos += strlength (car (x)), x = cdr (x)) |
4426 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4309 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4427 | |
4310 | |
4428 | s_return (newstr); |
4311 | s_return (newstr); |
4429 | } |
4312 | } |
4430 | |
4313 | |
… | |
… | |
4433 | char *str; |
4316 | char *str; |
4434 | int index0; |
4317 | int index0; |
4435 | int index1; |
4318 | int index1; |
4436 | int len; |
4319 | int len; |
4437 | |
4320 | |
4438 | str = strvalue (car (SCHEME_V->args)); |
4321 | str = strvalue (x); |
4439 | |
4322 | |
4440 | index0 = ivalue (cadr (SCHEME_V->args)); |
4323 | index0 = ivalue (cadr (args)); |
4441 | |
4324 | |
4442 | if (index0 > strlength (car (SCHEME_V->args))) |
4325 | if (index0 > strlength (x)) |
4443 | Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); |
4326 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4444 | |
4327 | |
4445 | if (cddr (SCHEME_V->args) != NIL) |
4328 | if (cddr (args) != NIL) |
4446 | { |
4329 | { |
4447 | index1 = ivalue (caddr (SCHEME_V->args)); |
4330 | index1 = ivalue (caddr (args)); |
4448 | |
4331 | |
4449 | if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) |
4332 | if (index1 > strlength (x) || index1 < index0) |
4450 | Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); |
4333 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4451 | } |
4334 | } |
4452 | else |
4335 | else |
4453 | index1 = strlength (car (SCHEME_V->args)); |
4336 | index1 = strlength (x); |
4454 | |
4337 | |
4455 | len = index1 - index0; |
4338 | len = index1 - index0; |
4456 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4339 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4457 | memcpy (strvalue (x), str + index0, len); |
4340 | memcpy (strvalue (x), str + index0, len); |
4458 | strvalue (x)[len] = 0; |
4341 | strvalue (x)[len] = 0; |
… | |
… | |
4462 | |
4345 | |
4463 | case OP_VECTOR: /* vector */ |
4346 | case OP_VECTOR: /* vector */ |
4464 | { |
4347 | { |
4465 | int i; |
4348 | int i; |
4466 | pointer vec; |
4349 | pointer vec; |
4467 | int len = list_length (SCHEME_A_ SCHEME_V->args); |
4350 | int len = list_length (SCHEME_A_ args); |
4468 | |
4351 | |
4469 | if (len < 0) |
4352 | if (len < 0) |
4470 | Error_1 ("vector: not a proper list:", SCHEME_V->args); |
4353 | Error_1 ("vector: not a proper list:", args); |
4471 | |
4354 | |
4472 | vec = mk_vector (SCHEME_A_ len); |
4355 | vec = mk_vector (SCHEME_A_ len); |
4473 | |
4356 | |
4474 | #if USE_ERROR_CHECKING |
4357 | #if USE_ERROR_CHECKING |
4475 | if (SCHEME_V->no_memory) |
4358 | if (SCHEME_V->no_memory) |
4476 | s_return (S_SINK); |
4359 | s_return (S_SINK); |
4477 | #endif |
4360 | #endif |
4478 | |
4361 | |
4479 | for (x = SCHEME_V->args, i = 0; is_pair (x); x = cdr (x), i++) |
4362 | for (x = args, i = 0; is_pair (x); x = cdr (x), i++) |
4480 | set_vector_elem (vec, i, car (x)); |
4363 | set_vector_elem (vec, i, car (x)); |
4481 | |
4364 | |
4482 | s_return (vec); |
4365 | s_return (vec); |
4483 | } |
4366 | } |
4484 | |
4367 | |
… | |
… | |
4486 | { |
4369 | { |
4487 | pointer fill = NIL; |
4370 | pointer fill = NIL; |
4488 | int len; |
4371 | int len; |
4489 | pointer vec; |
4372 | pointer vec; |
4490 | |
4373 | |
4491 | len = ivalue (car (SCHEME_V->args)); |
4374 | len = ivalue (x); |
4492 | |
4375 | |
4493 | if (cdr (SCHEME_V->args) != NIL) |
4376 | if (cdr (args) != NIL) |
4494 | fill = cadr (SCHEME_V->args); |
4377 | fill = cadr (args); |
4495 | |
4378 | |
4496 | vec = mk_vector (SCHEME_A_ len); |
4379 | vec = mk_vector (SCHEME_A_ len); |
4497 | |
4380 | |
4498 | #if USE_ERROR_CHECKING |
4381 | #if USE_ERROR_CHECKING |
4499 | if (SCHEME_V->no_memory) |
4382 | if (SCHEME_V->no_memory) |
… | |
… | |
4505 | |
4388 | |
4506 | s_return (vec); |
4389 | s_return (vec); |
4507 | } |
4390 | } |
4508 | |
4391 | |
4509 | case OP_VECLEN: /* vector-length */ |
4392 | case OP_VECLEN: /* vector-length */ |
4510 | s_return (mk_integer (SCHEME_A_ vector_length (car (SCHEME_V->args)))); |
4393 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4511 | |
4394 | |
4512 | case OP_VECREF: /* vector-ref */ |
4395 | case OP_VECREF: /* vector-ref */ |
4513 | { |
4396 | { |
4514 | int index; |
4397 | int index; |
4515 | |
4398 | |
4516 | index = ivalue (cadr (SCHEME_V->args)); |
4399 | index = ivalue (cadr (args)); |
4517 | |
4400 | |
4518 | if (index >= vector_length (car (SCHEME_V->args)) && USE_ERROR_CHECKING) |
4401 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4519 | Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); |
4402 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4520 | |
4403 | |
4521 | s_return (vector_elem (car (SCHEME_V->args), index)); |
4404 | s_return (vector_elem (x, index)); |
4522 | } |
4405 | } |
4523 | |
4406 | |
4524 | case OP_VECSET: /* vector-set! */ |
4407 | case OP_VECSET: /* vector-set! */ |
4525 | { |
4408 | { |
4526 | int index; |
4409 | int index; |
4527 | |
4410 | |
4528 | if (is_immutable (car (SCHEME_V->args))) |
4411 | if (is_immutable (x)) |
4529 | Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); |
4412 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4530 | |
4413 | |
4531 | index = ivalue (cadr (SCHEME_V->args)); |
4414 | index = ivalue (cadr (args)); |
4532 | |
4415 | |
4533 | if (index >= vector_length (car (SCHEME_V->args)) && USE_ERROR_CHECKING) |
4416 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4534 | Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); |
4417 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4535 | |
4418 | |
4536 | set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); |
4419 | set_vector_elem (x, index, caddr (args)); |
4537 | s_return (car (SCHEME_V->args)); |
4420 | s_return (x); |
4538 | } |
4421 | } |
4539 | } |
4422 | } |
4540 | |
4423 | |
4541 | return S_T; |
4424 | abort (); |
4542 | } |
4425 | } |
4543 | |
4426 | |
4544 | INTERFACE int |
4427 | INTERFACE int |
4545 | is_list (SCHEME_P_ pointer a) |
4428 | is_list (SCHEME_P_ pointer a) |
4546 | { |
4429 | { |
… | |
… | |
4593 | return -1; |
4476 | return -1; |
4594 | } |
4477 | } |
4595 | } |
4478 | } |
4596 | } |
4479 | } |
4597 | |
4480 | |
4598 | static pointer |
4481 | static int |
|
|
4482 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
|
|
4483 | { |
|
|
4484 | pointer x = SCHEME_V->args; |
|
|
4485 | |
|
|
4486 | for (;;) |
|
|
4487 | { |
|
|
4488 | num v = nvalue (car (x)); |
|
|
4489 | x = cdr (x); |
|
|
4490 | |
|
|
4491 | if (x == NIL) |
|
|
4492 | break; |
|
|
4493 | |
|
|
4494 | int r = num_cmp (v, nvalue (car (x))); |
|
|
4495 | |
|
|
4496 | switch (op) |
|
|
4497 | { |
|
|
4498 | case OP_NUMEQ: r = r == 0; break; |
|
|
4499 | case OP_LESS: r = r < 0; break; |
|
|
4500 | case OP_GRE: r = r > 0; break; |
|
|
4501 | case OP_LEQ: r = r <= 0; break; |
|
|
4502 | case OP_GEQ: r = r >= 0; break; |
|
|
4503 | } |
|
|
4504 | |
|
|
4505 | if (!r) |
|
|
4506 | s_return (S_F); |
|
|
4507 | } |
|
|
4508 | |
|
|
4509 | s_return (S_T); |
|
|
4510 | } |
|
|
4511 | |
|
|
4512 | static int |
4599 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4513 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4600 | { |
4514 | { |
4601 | pointer x; |
4515 | pointer args = SCHEME_V->args; |
4602 | num v; |
4516 | pointer a = car (args); |
4603 | int (*comp_func) (num, num); |
4517 | pointer d = cdr (args); |
|
|
4518 | int r; |
4604 | |
4519 | |
4605 | switch (op) |
4520 | switch (op) |
4606 | { |
4521 | { |
4607 | case OP_NOT: /* not */ |
4522 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4608 | s_retbool (is_false (car (SCHEME_V->args))); |
4523 | case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break; |
|
|
4524 | case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break; |
|
|
4525 | case OP_NULLP: /* null? */ r = a == NIL ; break; |
|
|
4526 | case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break; |
|
|
4527 | case OP_NUMBERP: /* number? */ r = is_number (a) ; break; |
|
|
4528 | case OP_STRINGP: /* string? */ r = is_string (a) ; break; |
|
|
4529 | case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break; |
|
|
4530 | case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */ |
|
|
4531 | case OP_CHARP: /* char? */ r = is_character (a) ; break; |
4609 | |
4532 | |
4610 | case OP_BOOLP: /* boolean? */ |
|
|
4611 | s_retbool (car (SCHEME_V->args) == S_F || car (SCHEME_V->args) == S_T); |
|
|
4612 | |
|
|
4613 | case OP_EOFOBJP: /* boolean? */ |
|
|
4614 | s_retbool (car (SCHEME_V->args) == S_EOF); |
|
|
4615 | |
|
|
4616 | case OP_NULLP: /* null? */ |
|
|
4617 | s_retbool (car (SCHEME_V->args) == NIL); |
|
|
4618 | |
|
|
4619 | case OP_NUMEQ: /* = */ |
|
|
4620 | case OP_LESS: /* < */ |
|
|
4621 | case OP_GRE: /* > */ |
|
|
4622 | case OP_LEQ: /* <= */ |
|
|
4623 | case OP_GEQ: /* >= */ |
|
|
4624 | switch (op) |
|
|
4625 | { |
|
|
4626 | case OP_NUMEQ: |
|
|
4627 | comp_func = num_eq; |
|
|
4628 | break; |
|
|
4629 | |
|
|
4630 | case OP_LESS: |
|
|
4631 | comp_func = num_lt; |
|
|
4632 | break; |
|
|
4633 | |
|
|
4634 | case OP_GRE: |
|
|
4635 | comp_func = num_gt; |
|
|
4636 | break; |
|
|
4637 | |
|
|
4638 | case OP_LEQ: |
|
|
4639 | comp_func = num_le; |
|
|
4640 | break; |
|
|
4641 | |
|
|
4642 | case OP_GEQ: |
|
|
4643 | comp_func = num_ge; |
|
|
4644 | break; |
|
|
4645 | } |
|
|
4646 | |
|
|
4647 | x = SCHEME_V->args; |
|
|
4648 | v = nvalue (car (x)); |
|
|
4649 | x = cdr (x); |
|
|
4650 | |
|
|
4651 | for (; x != NIL; x = cdr (x)) |
|
|
4652 | { |
|
|
4653 | if (!comp_func (v, nvalue (car (x)))) |
|
|
4654 | s_retbool (0); |
|
|
4655 | |
|
|
4656 | v = nvalue (car (x)); |
|
|
4657 | } |
|
|
4658 | |
|
|
4659 | s_retbool (1); |
|
|
4660 | |
|
|
4661 | case OP_SYMBOLP: /* symbol? */ |
|
|
4662 | s_retbool (is_symbol (car (SCHEME_V->args))); |
|
|
4663 | |
|
|
4664 | case OP_NUMBERP: /* number? */ |
|
|
4665 | s_retbool (is_number (car (SCHEME_V->args))); |
|
|
4666 | |
|
|
4667 | case OP_STRINGP: /* string? */ |
|
|
4668 | s_retbool (is_string (car (SCHEME_V->args))); |
|
|
4669 | |
|
|
4670 | case OP_INTEGERP: /* integer? */ |
|
|
4671 | s_retbool (is_integer (car (SCHEME_V->args))); |
|
|
4672 | |
|
|
4673 | case OP_REALP: /* real? */ |
|
|
4674 | s_retbool (is_number (car (SCHEME_V->args))); /* All numbers are real */ |
|
|
4675 | |
|
|
4676 | case OP_CHARP: /* char? */ |
|
|
4677 | s_retbool (is_character (car (SCHEME_V->args))); |
|
|
4678 | #if USE_CHAR_CLASSIFIERS |
4533 | #if USE_CHAR_CLASSIFIERS |
4679 | |
|
|
4680 | case OP_CHARAP: /* char-alphabetic? */ |
4534 | case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break; |
4681 | s_retbool (Cisalpha (ivalue (car (SCHEME_V->args)))); |
4535 | case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break; |
4682 | |
|
|
4683 | case OP_CHARNP: /* char-numeric? */ |
|
|
4684 | s_retbool (Cisdigit (ivalue (car (SCHEME_V->args)))); |
|
|
4685 | |
|
|
4686 | case OP_CHARWP: /* char-whitespace? */ |
4536 | case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break; |
4687 | s_retbool (Cisspace (ivalue (car (SCHEME_V->args)))); |
|
|
4688 | |
|
|
4689 | case OP_CHARUP: /* char-upper-case? */ |
4537 | case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break; |
4690 | s_retbool (Cisupper (ivalue (car (SCHEME_V->args)))); |
|
|
4691 | |
|
|
4692 | case OP_CHARLP: /* char-lower-case? */ |
4538 | case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break; |
4693 | s_retbool (Cislower (ivalue (car (SCHEME_V->args)))); |
|
|
4694 | #endif |
4539 | #endif |
|
|
4540 | |
4695 | #if USE_PORTS |
4541 | #if USE_PORTS |
4696 | |
4542 | case OP_PORTP: /* port? */ r = is_port (a) ; break; |
4697 | case OP_PORTP: /* port? */ |
|
|
4698 | s_retbool (is_port (car (SCHEME_V->args))); |
|
|
4699 | |
|
|
4700 | case OP_INPORTP: /* input-port? */ |
4543 | case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break; |
4701 | s_retbool (is_inport (car (SCHEME_V->args))); |
|
|
4702 | |
|
|
4703 | case OP_OUTPORTP: /* output-port? */ |
4544 | case OP_OUTPORTP: /* output-port? */ r = is_outport (a); break; |
4704 | s_retbool (is_outport (car (SCHEME_V->args))); |
|
|
4705 | #endif |
4545 | #endif |
4706 | |
4546 | |
4707 | case OP_PROCP: /* procedure? */ |
4547 | case OP_PROCP: /* procedure? */ |
4708 | |
4548 | |
4709 | /*-- |
4549 | /*-- |
4710 | * continuation should be procedure by the example |
4550 | * continuation should be procedure by the example |
4711 | * (call-with-current-continuation procedure?) ==> #t |
4551 | * (call-with-current-continuation procedure?) ==> #t |
4712 | * in R^3 report sec. 6.9 |
4552 | * in R^3 report sec. 6.9 |
4713 | */ |
4553 | */ |
4714 | s_retbool (is_proc (car (SCHEME_V->args)) || is_closure (car (SCHEME_V->args)) |
4554 | r = is_proc (a) || is_closure (a) || is_continuation (a) || is_foreign (a); |
4715 | || is_continuation (car (SCHEME_V->args)) || is_foreign (car (SCHEME_V->args))); |
4555 | break; |
4716 | |
4556 | |
4717 | case OP_PAIRP: /* pair? */ |
4557 | case OP_PAIRP: /* pair? */ r = is_pair (a) ; break; |
4718 | s_retbool (is_pair (car (SCHEME_V->args))); |
4558 | case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break; |
4719 | |
4559 | case OP_ENVP: /* environment? */ r = is_environment (a) ; break; |
4720 | case OP_LISTP: /* list? */ |
4560 | case OP_VECTORP: /* vector? */ r = is_vector (a) ; break; |
4721 | s_retbool (list_length (SCHEME_A_ car (SCHEME_V->args)) >= 0); |
4561 | case OP_EQ: /* eq? */ r = a == cadr (args) ; break; |
4722 | |
4562 | case OP_EQV: /* eqv? */ r = eqv (a, cadr (args)) ; break; |
4723 | case OP_ENVP: /* environment? */ |
|
|
4724 | s_retbool (is_environment (car (SCHEME_V->args))); |
|
|
4725 | |
|
|
4726 | case OP_VECTORP: /* vector? */ |
|
|
4727 | s_retbool (is_vector (car (SCHEME_V->args))); |
|
|
4728 | |
|
|
4729 | case OP_EQ: /* eq? */ |
|
|
4730 | s_retbool (car (SCHEME_V->args) == cadr (SCHEME_V->args)); |
|
|
4731 | |
|
|
4732 | case OP_EQV: /* eqv? */ |
|
|
4733 | s_retbool (eqv (car (SCHEME_V->args), cadr (SCHEME_V->args))); |
|
|
4734 | } |
4563 | } |
4735 | |
4564 | |
4736 | return S_T; |
4565 | s_retbool (r); |
4737 | } |
4566 | } |
4738 | |
4567 | |
4739 | static pointer |
4568 | static int |
4740 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4569 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4741 | { |
4570 | { |
|
|
4571 | pointer args = SCHEME_V->args; |
|
|
4572 | pointer a = car (args); |
4742 | pointer x, y; |
4573 | pointer x, y; |
4743 | |
4574 | |
4744 | switch (op) |
4575 | switch (op) |
4745 | { |
4576 | { |
4746 | case OP_FORCE: /* force */ |
4577 | case OP_FORCE: /* force */ |
4747 | SCHEME_V->code = car (SCHEME_V->args); |
4578 | SCHEME_V->code = a; |
4748 | |
4579 | |
4749 | if (is_promise (SCHEME_V->code)) |
4580 | if (is_promise (SCHEME_V->code)) |
4750 | { |
4581 | { |
4751 | /* Should change type to closure here */ |
4582 | /* Should change type to closure here */ |
4752 | s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); |
4583 | s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); |
… | |
… | |
4773 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4604 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4774 | SCHEME_V->outport = cadr (SCHEME_V->args); |
4605 | SCHEME_V->outport = cadr (SCHEME_V->args); |
4775 | } |
4606 | } |
4776 | } |
4607 | } |
4777 | |
4608 | |
4778 | SCHEME_V->args = car (SCHEME_V->args); |
4609 | SCHEME_V->args = a; |
4779 | |
4610 | |
4780 | if (op == OP_WRITE) |
4611 | if (op == OP_WRITE) |
4781 | SCHEME_V->print_flag = 1; |
4612 | SCHEME_V->print_flag = 1; |
4782 | else |
4613 | else |
4783 | SCHEME_V->print_flag = 0; |
4614 | SCHEME_V->print_flag = 0; |
4784 | |
4615 | |
4785 | s_goto (OP_P0LIST); |
4616 | s_goto (OP_P0LIST); |
4786 | |
4617 | |
4787 | case OP_NEWLINE: /* newline */ |
4618 | case OP_NEWLINE: /* newline */ |
4788 | if (is_pair (SCHEME_V->args)) |
4619 | if (is_pair (args)) |
4789 | { |
4620 | { |
4790 | if (car (SCHEME_V->args) != SCHEME_V->outport) |
4621 | if (a != SCHEME_V->outport) |
4791 | { |
4622 | { |
4792 | x = cons (SCHEME_V->outport, NIL); |
4623 | x = cons (SCHEME_V->outport, NIL); |
4793 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4624 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4794 | SCHEME_V->outport = car (SCHEME_V->args); |
4625 | SCHEME_V->outport = a; |
4795 | } |
4626 | } |
4796 | } |
4627 | } |
4797 | |
4628 | |
4798 | putstr (SCHEME_A_ "\n"); |
4629 | putstr (SCHEME_A_ "\n"); |
4799 | s_return (S_T); |
4630 | s_return (S_T); |
4800 | #endif |
4631 | #endif |
4801 | |
4632 | |
4802 | case OP_ERR0: /* error */ |
4633 | case OP_ERR0: /* error */ |
4803 | SCHEME_V->retcode = -1; |
4634 | SCHEME_V->retcode = -1; |
4804 | |
4635 | |
4805 | if (!is_string (car (SCHEME_V->args))) |
4636 | if (!is_string (a)) |
4806 | { |
4637 | { |
4807 | SCHEME_V->args = cons (mk_string (SCHEME_A_ " -- "), SCHEME_V->args); |
4638 | args = cons (mk_string (SCHEME_A_ " -- "), args); |
4808 | setimmutable (car (SCHEME_V->args)); |
4639 | setimmutable (car (args)); |
4809 | } |
4640 | } |
4810 | |
4641 | |
4811 | putstr (SCHEME_A_ "Error: "); |
4642 | putstr (SCHEME_A_ "Error: "); |
4812 | putstr (SCHEME_A_ strvalue (car (SCHEME_V->args))); |
4643 | putstr (SCHEME_A_ strvalue (car (args))); |
4813 | SCHEME_V->args = cdr (SCHEME_V->args); |
4644 | SCHEME_V->args = cdr (args); |
4814 | s_goto (OP_ERR1); |
4645 | s_goto (OP_ERR1); |
4815 | |
4646 | |
4816 | case OP_ERR1: /* error */ |
4647 | case OP_ERR1: /* error */ |
4817 | putstr (SCHEME_A_ " "); |
4648 | putstr (SCHEME_A_ " "); |
4818 | |
4649 | |
4819 | if (SCHEME_V->args != NIL) |
4650 | if (args != NIL) |
4820 | { |
4651 | { |
4821 | s_save (SCHEME_A_ OP_ERR1, cdr (SCHEME_V->args), NIL); |
4652 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4822 | SCHEME_V->args = car (SCHEME_V->args); |
4653 | SCHEME_V->args = a; |
4823 | SCHEME_V->print_flag = 1; |
4654 | SCHEME_V->print_flag = 1; |
4824 | s_goto (OP_P0LIST); |
4655 | s_goto (OP_P0LIST); |
4825 | } |
4656 | } |
4826 | else |
4657 | else |
4827 | { |
4658 | { |
4828 | putstr (SCHEME_A_ "\n"); |
4659 | putstr (SCHEME_A_ "\n"); |
4829 | |
4660 | |
4830 | if (SCHEME_V->interactive_repl) |
4661 | if (SCHEME_V->interactive_repl) |
4831 | s_goto (OP_T0LVL); |
4662 | s_goto (OP_T0LVL); |
4832 | else |
4663 | else |
4833 | return NIL; |
4664 | return -1; |
4834 | } |
4665 | } |
4835 | |
4666 | |
4836 | case OP_REVERSE: /* reverse */ |
4667 | case OP_REVERSE: /* reverse */ |
4837 | s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); |
4668 | s_return (reverse (SCHEME_A_ a)); |
4838 | |
4669 | |
4839 | case OP_LIST_STAR: /* list* */ |
4670 | case OP_LIST_STAR: /* list* */ |
4840 | s_return (list_star (SCHEME_A_ SCHEME_V->args)); |
4671 | s_return (list_star (SCHEME_A_ SCHEME_V->args)); |
4841 | |
4672 | |
4842 | case OP_APPEND: /* append */ |
4673 | case OP_APPEND: /* append */ |
4843 | x = NIL; |
4674 | x = NIL; |
4844 | y = SCHEME_V->args; |
4675 | y = args; |
4845 | |
4676 | |
4846 | if (y == x) |
4677 | if (y == x) |
4847 | s_return (x); |
4678 | s_return (x); |
4848 | |
4679 | |
4849 | /* cdr() in the while condition is not a typo. If car() */ |
4680 | /* cdr() in the while condition is not a typo. If car() */ |
… | |
… | |
4860 | s_return (reverse_in_place (SCHEME_A_ car (y), x)); |
4691 | s_return (reverse_in_place (SCHEME_A_ car (y), x)); |
4861 | |
4692 | |
4862 | #if USE_PLIST |
4693 | #if USE_PLIST |
4863 | |
4694 | |
4864 | case OP_PUT: /* put */ |
4695 | case OP_PUT: /* put */ |
4865 | if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) |
4696 | if (!hasprop (a) || !hasprop (cadr (args))) |
4866 | Error_0 ("illegal use of put"); |
4697 | Error_0 ("illegal use of put"); |
4867 | |
4698 | |
4868 | for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) |
4699 | for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x)) |
4869 | { |
4700 | { |
4870 | if (caar (x) == y) |
4701 | if (caar (x) == y) |
4871 | break; |
4702 | break; |
4872 | } |
4703 | } |
4873 | |
4704 | |
4874 | if (x != NIL) |
4705 | if (x != NIL) |
4875 | cdar (x) = caddr (SCHEME_V->args); |
4706 | cdar (x) = caddr (args); |
4876 | else |
4707 | else |
4877 | symprop (car (SCHEME_V->args)) = cons (cons (y, caddr (SCHEME_V->args)), symprop (car (SCHEME_V->args))); |
4708 | symprop (a) = cons (cons (y, caddr (args)), symprop (a)); |
4878 | |
4709 | |
4879 | s_return (S_T); |
4710 | s_return (S_T); |
4880 | |
4711 | |
4881 | case OP_GET: /* get */ |
4712 | case OP_GET: /* get */ |
4882 | if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) |
4713 | if (!hasprop (a) || !hasprop (cadr (args))) |
4883 | Error_0 ("illegal use of get"); |
4714 | Error_0 ("illegal use of get"); |
4884 | |
4715 | |
4885 | for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) |
4716 | for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x)) |
4886 | if (caar (x) == y) |
4717 | if (caar (x) == y) |
4887 | break; |
4718 | break; |
4888 | |
4719 | |
4889 | if (x != NIL) |
4720 | if (x != NIL) |
4890 | s_return (cdar (x)); |
4721 | s_return (cdar (x)); |
… | |
… | |
4892 | s_return (NIL); |
4723 | s_return (NIL); |
4893 | |
4724 | |
4894 | #endif /* USE_PLIST */ |
4725 | #endif /* USE_PLIST */ |
4895 | |
4726 | |
4896 | case OP_QUIT: /* quit */ |
4727 | case OP_QUIT: /* quit */ |
4897 | if (is_pair (SCHEME_V->args)) |
4728 | if (is_pair (args)) |
4898 | SCHEME_V->retcode = ivalue (car (SCHEME_V->args)); |
4729 | SCHEME_V->retcode = ivalue (a); |
4899 | |
4730 | |
4900 | return NIL; |
4731 | return -1; |
4901 | |
4732 | |
4902 | case OP_GC: /* gc */ |
4733 | case OP_GC: /* gc */ |
4903 | gc (SCHEME_A_ NIL, NIL); |
4734 | gc (SCHEME_A_ NIL, NIL); |
4904 | s_return (S_T); |
4735 | s_return (S_T); |
4905 | |
4736 | |
4906 | case OP_GCVERB: /* gc-verbose */ |
4737 | case OP_GCVERB: /* gc-verbose */ |
4907 | { |
4738 | { |
4908 | int was = SCHEME_V->gc_verbose; |
4739 | int was = SCHEME_V->gc_verbose; |
4909 | |
4740 | |
4910 | SCHEME_V->gc_verbose = (car (SCHEME_V->args) != S_F); |
4741 | SCHEME_V->gc_verbose = (a != S_F); |
4911 | s_retbool (was); |
4742 | s_retbool (was); |
4912 | } |
4743 | } |
4913 | |
4744 | |
4914 | case OP_NEWSEGMENT: /* new-segment */ |
4745 | case OP_NEWSEGMENT: /* new-segment */ |
4915 | if (!is_pair (SCHEME_V->args) || !is_number (car (SCHEME_V->args))) |
4746 | if (!is_pair (args) || !is_number (a)) |
4916 | Error_0 ("new-segment: argument must be a number"); |
4747 | Error_0 ("new-segment: argument must be a number"); |
4917 | |
4748 | |
4918 | alloc_cellseg (SCHEME_A_ (int)ivalue (car (SCHEME_V->args))); |
4749 | alloc_cellseg (SCHEME_A_ (int)ivalue (a)); |
4919 | |
4750 | |
4920 | s_return (S_T); |
4751 | s_return (S_T); |
4921 | |
4752 | |
4922 | case OP_OBLIST: /* oblist */ |
4753 | case OP_OBLIST: /* oblist */ |
4923 | s_return (oblist_all_symbols (SCHEME_A)); |
4754 | s_return (oblist_all_symbols (SCHEME_A)); |
… | |
… | |
4950 | case OP_OPEN_INOUTFILE: |
4781 | case OP_OPEN_INOUTFILE: |
4951 | prop = port_input | port_output; |
4782 | prop = port_input | port_output; |
4952 | break; |
4783 | break; |
4953 | } |
4784 | } |
4954 | |
4785 | |
4955 | p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); |
4786 | p = port_from_filename (SCHEME_A_ strvalue (a), prop); |
4956 | |
4787 | |
4957 | if (p == NIL) |
4788 | if (p == NIL) |
4958 | s_return (S_F); |
4789 | s_return (S_F); |
4959 | |
4790 | |
4960 | s_return (p); |
4791 | s_return (p); |
… | |
… | |
4977 | case OP_OPEN_INOUTSTRING: |
4808 | case OP_OPEN_INOUTSTRING: |
4978 | prop = port_input | port_output; |
4809 | prop = port_input | port_output; |
4979 | break; |
4810 | break; |
4980 | } |
4811 | } |
4981 | |
4812 | |
4982 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
4813 | p = port_from_string (SCHEME_A_ strvalue (a), |
4983 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); |
4814 | strvalue (a) + strlength (a), prop); |
4984 | |
4815 | |
4985 | if (p == NIL) |
4816 | if (p == NIL) |
4986 | s_return (S_F); |
4817 | s_return (S_F); |
4987 | |
4818 | |
4988 | s_return (p); |
4819 | s_return (p); |
… | |
… | |
4990 | |
4821 | |
4991 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4822 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4992 | { |
4823 | { |
4993 | pointer p; |
4824 | pointer p; |
4994 | |
4825 | |
4995 | if (car (SCHEME_V->args) == NIL) |
4826 | if (a == NIL) |
4996 | { |
4827 | { |
4997 | p = port_from_scratch (SCHEME_A); |
4828 | p = port_from_scratch (SCHEME_A); |
4998 | |
4829 | |
4999 | if (p == NIL) |
4830 | if (p == NIL) |
5000 | s_return (S_F); |
4831 | s_return (S_F); |
5001 | } |
4832 | } |
5002 | else |
4833 | else |
5003 | { |
4834 | { |
5004 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
4835 | p = port_from_string (SCHEME_A_ strvalue (a), |
5005 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); |
4836 | strvalue (a) + strlength (a), port_output); |
5006 | |
4837 | |
5007 | if (p == NIL) |
4838 | if (p == NIL) |
5008 | s_return (S_F); |
4839 | s_return (S_F); |
5009 | } |
4840 | } |
5010 | |
4841 | |
… | |
… | |
5013 | |
4844 | |
5014 | case OP_GET_OUTSTRING: /* get-output-string */ |
4845 | case OP_GET_OUTSTRING: /* get-output-string */ |
5015 | { |
4846 | { |
5016 | port *p; |
4847 | port *p; |
5017 | |
4848 | |
5018 | if ((p = car (SCHEME_V->args)->object.port)->kind & port_string) |
4849 | if ((p = a->object.port)->kind & port_string) |
5019 | { |
4850 | { |
5020 | off_t size; |
4851 | off_t size; |
5021 | char *str; |
4852 | char *str; |
5022 | |
4853 | |
5023 | size = p->rep.string.curr - p->rep.string.start + 1; |
4854 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
5039 | } |
4870 | } |
5040 | |
4871 | |
5041 | # endif |
4872 | # endif |
5042 | |
4873 | |
5043 | case OP_CLOSE_INPORT: /* close-input-port */ |
4874 | case OP_CLOSE_INPORT: /* close-input-port */ |
5044 | port_close (SCHEME_A_ car (SCHEME_V->args), port_input); |
4875 | port_close (SCHEME_A_ a, port_input); |
5045 | s_return (S_T); |
4876 | s_return (S_T); |
5046 | |
4877 | |
5047 | case OP_CLOSE_OUTPORT: /* close-output-port */ |
4878 | case OP_CLOSE_OUTPORT: /* close-output-port */ |
5048 | port_close (SCHEME_A_ car (SCHEME_V->args), port_output); |
4879 | port_close (SCHEME_A_ a, port_output); |
5049 | s_return (S_T); |
4880 | s_return (S_T); |
5050 | #endif |
4881 | #endif |
5051 | |
4882 | |
5052 | case OP_INT_ENV: /* interaction-environment */ |
4883 | case OP_INT_ENV: /* interaction-environment */ |
5053 | s_return (SCHEME_V->global_env); |
4884 | s_return (SCHEME_V->global_env); |
… | |
… | |
5055 | case OP_CURR_ENV: /* current-environment */ |
4886 | case OP_CURR_ENV: /* current-environment */ |
5056 | s_return (SCHEME_V->envir); |
4887 | s_return (SCHEME_V->envir); |
5057 | |
4888 | |
5058 | } |
4889 | } |
5059 | |
4890 | |
5060 | return S_T; |
4891 | abort (); |
5061 | } |
4892 | } |
5062 | |
4893 | |
5063 | static pointer |
4894 | static int |
5064 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4895 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
5065 | { |
4896 | { |
|
|
4897 | pointer args = SCHEME_V->args; |
5066 | pointer x; |
4898 | pointer x; |
5067 | |
4899 | |
5068 | if (SCHEME_V->nesting != 0) |
4900 | if (SCHEME_V->nesting != 0) |
5069 | { |
4901 | { |
5070 | int n = SCHEME_V->nesting; |
4902 | int n = SCHEME_V->nesting; |
… | |
… | |
5077 | switch (op) |
4909 | switch (op) |
5078 | { |
4910 | { |
5079 | /* ========== reading part ========== */ |
4911 | /* ========== reading part ========== */ |
5080 | #if USE_PORTS |
4912 | #if USE_PORTS |
5081 | case OP_READ: |
4913 | case OP_READ: |
5082 | if (!is_pair (SCHEME_V->args)) |
4914 | if (!is_pair (args)) |
5083 | s_goto (OP_READ_INTERNAL); |
4915 | s_goto (OP_READ_INTERNAL); |
5084 | |
4916 | |
5085 | if (!is_inport (car (SCHEME_V->args))) |
4917 | if (!is_inport (car (args))) |
5086 | Error_1 ("read: not an input port:", car (SCHEME_V->args)); |
4918 | Error_1 ("read: not an input port:", car (args)); |
5087 | |
4919 | |
5088 | if (car (SCHEME_V->args) == SCHEME_V->inport) |
4920 | if (car (args) == SCHEME_V->inport) |
5089 | s_goto (OP_READ_INTERNAL); |
4921 | s_goto (OP_READ_INTERNAL); |
5090 | |
4922 | |
5091 | x = SCHEME_V->inport; |
4923 | x = SCHEME_V->inport; |
5092 | SCHEME_V->inport = car (SCHEME_V->args); |
4924 | SCHEME_V->inport = car (args); |
5093 | x = cons (x, NIL); |
4925 | x = cons (x, NIL); |
5094 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
4926 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
5095 | s_goto (OP_READ_INTERNAL); |
4927 | s_goto (OP_READ_INTERNAL); |
5096 | |
4928 | |
5097 | case OP_READ_CHAR: /* read-char */ |
4929 | case OP_READ_CHAR: /* read-char */ |
5098 | case OP_PEEK_CHAR: /* peek-char */ |
4930 | case OP_PEEK_CHAR: /* peek-char */ |
5099 | { |
4931 | { |
5100 | int c; |
4932 | int c; |
5101 | |
4933 | |
5102 | if (is_pair (SCHEME_V->args)) |
4934 | if (is_pair (args)) |
5103 | { |
4935 | { |
5104 | if (car (SCHEME_V->args) != SCHEME_V->inport) |
4936 | if (car (args) != SCHEME_V->inport) |
5105 | { |
4937 | { |
5106 | x = SCHEME_V->inport; |
4938 | x = SCHEME_V->inport; |
5107 | x = cons (x, NIL); |
4939 | x = cons (x, NIL); |
5108 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
4940 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
5109 | SCHEME_V->inport = car (SCHEME_V->args); |
4941 | SCHEME_V->inport = car (args); |
5110 | } |
4942 | } |
5111 | } |
4943 | } |
5112 | |
4944 | |
5113 | c = inchar (SCHEME_A); |
4945 | c = inchar (SCHEME_A); |
5114 | |
4946 | |
… | |
… | |
5124 | case OP_CHAR_READY: /* char-ready? */ |
4956 | case OP_CHAR_READY: /* char-ready? */ |
5125 | { |
4957 | { |
5126 | pointer p = SCHEME_V->inport; |
4958 | pointer p = SCHEME_V->inport; |
5127 | int res; |
4959 | int res; |
5128 | |
4960 | |
5129 | if (is_pair (SCHEME_V->args)) |
4961 | if (is_pair (args)) |
5130 | p = car (SCHEME_V->args); |
4962 | p = car (args); |
5131 | |
4963 | |
5132 | res = p->object.port->kind & port_string; |
4964 | res = p->object.port->kind & port_string; |
5133 | |
4965 | |
5134 | s_retbool (res); |
4966 | s_retbool (res); |
5135 | } |
4967 | } |
5136 | |
4968 | |
5137 | case OP_SET_INPORT: /* set-input-port */ |
4969 | case OP_SET_INPORT: /* set-input-port */ |
5138 | SCHEME_V->inport = car (SCHEME_V->args); |
4970 | SCHEME_V->inport = car (args); |
5139 | s_return (SCHEME_V->value); |
4971 | s_return (SCHEME_V->value); |
5140 | |
4972 | |
5141 | case OP_SET_OUTPORT: /* set-output-port */ |
4973 | case OP_SET_OUTPORT: /* set-output-port */ |
5142 | SCHEME_V->outport = car (SCHEME_V->args); |
4974 | SCHEME_V->outport = car (args); |
5143 | s_return (SCHEME_V->value); |
4975 | s_return (SCHEME_V->value); |
5144 | #endif |
4976 | #endif |
5145 | |
4977 | |
5146 | case OP_RDSEXPR: |
4978 | case OP_RDSEXPR: |
5147 | switch (SCHEME_V->tok) |
4979 | switch (SCHEME_V->tok) |
… | |
… | |
5233 | } |
5065 | } |
5234 | |
5066 | |
5235 | break; |
5067 | break; |
5236 | |
5068 | |
5237 | case OP_RDLIST: |
5069 | case OP_RDLIST: |
5238 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
5070 | SCHEME_V->args = cons (SCHEME_V->value, args); |
5239 | SCHEME_V->tok = token (SCHEME_A); |
5071 | SCHEME_V->tok = token (SCHEME_A); |
5240 | |
5072 | |
5241 | switch (SCHEME_V->tok) |
5073 | switch (SCHEME_V->tok) |
5242 | { |
5074 | { |
5243 | case TOK_EOF: |
5075 | case TOK_EOF: |
… | |
… | |
5271 | case OP_RDDOT: |
5103 | case OP_RDDOT: |
5272 | if (token (SCHEME_A) != TOK_RPAREN) |
5104 | if (token (SCHEME_A) != TOK_RPAREN) |
5273 | Error_0 ("syntax error: illegal dot expression"); |
5105 | Error_0 ("syntax error: illegal dot expression"); |
5274 | |
5106 | |
5275 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5107 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5276 | s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); |
5108 | s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args)); |
5277 | |
5109 | |
5278 | case OP_RDQUOTE: |
5110 | case OP_RDQUOTE: |
5279 | s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); |
5111 | s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); |
5280 | |
5112 | |
5281 | case OP_RDQQUOTE: |
5113 | case OP_RDQQUOTE: |
… | |
… | |
5303 | SCHEME_V->args = SCHEME_V->value; |
5135 | SCHEME_V->args = SCHEME_V->value; |
5304 | s_goto (OP_VECTOR); |
5136 | s_goto (OP_VECTOR); |
5305 | |
5137 | |
5306 | /* ========== printing part ========== */ |
5138 | /* ========== printing part ========== */ |
5307 | case OP_P0LIST: |
5139 | case OP_P0LIST: |
5308 | if (is_vector (SCHEME_V->args)) |
5140 | if (is_vector (args)) |
5309 | { |
5141 | { |
5310 | putstr (SCHEME_A_ "#("); |
5142 | putstr (SCHEME_A_ "#("); |
5311 | SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); |
5143 | SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0)); |
5312 | s_goto (OP_PVECFROM); |
5144 | s_goto (OP_PVECFROM); |
5313 | } |
5145 | } |
5314 | else if (is_environment (SCHEME_V->args)) |
5146 | else if (is_environment (args)) |
5315 | { |
5147 | { |
5316 | putstr (SCHEME_A_ "#<ENVIRONMENT>"); |
5148 | putstr (SCHEME_A_ "#<ENVIRONMENT>"); |
5317 | s_return (S_T); |
5149 | s_return (S_T); |
5318 | } |
5150 | } |
5319 | else if (!is_pair (SCHEME_V->args)) |
5151 | else if (!is_pair (args)) |
5320 | { |
5152 | { |
5321 | printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); |
5153 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5322 | s_return (S_T); |
5154 | s_return (S_T); |
5323 | } |
5155 | } |
5324 | else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) |
5156 | else |
5325 | { |
5157 | { |
|
|
5158 | pointer a = car (args); |
|
|
5159 | pointer b = cdr (args); |
|
|
5160 | int ok_abbr = ok_abbrev (b); |
|
|
5161 | SCHEME_V->args = car (b); |
|
|
5162 | |
|
|
5163 | if (a == SCHEME_V->QUOTE && ok_abbr) |
5326 | putstr (SCHEME_A_ "'"); |
5164 | putstr (SCHEME_A_ "'"); |
5327 | SCHEME_V->args = cadr (SCHEME_V->args); |
5165 | else if (a == SCHEME_V->QQUOTE && ok_abbr) |
|
|
5166 | putstr (SCHEME_A_ "`"); |
|
|
5167 | else if (a == SCHEME_V->UNQUOTE && ok_abbr) |
|
|
5168 | putstr (SCHEME_A_ ","); |
|
|
5169 | else if (a == SCHEME_V->UNQUOTESP && ok_abbr) |
|
|
5170 | putstr (SCHEME_A_ ",@"); |
|
|
5171 | else |
|
|
5172 | { |
|
|
5173 | putstr (SCHEME_A_ "("); |
|
|
5174 | s_save (SCHEME_A_ OP_P1LIST, b, NIL); |
|
|
5175 | SCHEME_V->args = a; |
|
|
5176 | } |
|
|
5177 | |
5328 | s_goto (OP_P0LIST); |
5178 | s_goto (OP_P0LIST); |
5329 | } |
5179 | } |
5330 | else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) |
5180 | |
|
|
5181 | case OP_P1LIST: |
|
|
5182 | if (is_pair (args)) |
5331 | { |
5183 | { |
|
|
5184 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5332 | putstr (SCHEME_A_ "`"); |
5185 | putstr (SCHEME_A_ " "); |
5333 | SCHEME_V->args = cadr (SCHEME_V->args); |
5186 | SCHEME_V->args = car (args); |
5334 | s_goto (OP_P0LIST); |
5187 | s_goto (OP_P0LIST); |
5335 | } |
5188 | } |
5336 | else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTE && ok_abbrev (cdr (SCHEME_V->args))) |
|
|
5337 | { |
|
|
5338 | putstr (SCHEME_A_ ","); |
|
|
5339 | SCHEME_V->args = cadr (SCHEME_V->args); |
|
|
5340 | s_goto (OP_P0LIST); |
|
|
5341 | } |
|
|
5342 | else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTESP && ok_abbrev (cdr (SCHEME_V->args))) |
|
|
5343 | { |
|
|
5344 | putstr (SCHEME_A_ ",@"); |
|
|
5345 | SCHEME_V->args = cadr (SCHEME_V->args); |
|
|
5346 | s_goto (OP_P0LIST); |
|
|
5347 | } |
|
|
5348 | else |
|
|
5349 | { |
|
|
5350 | putstr (SCHEME_A_ "("); |
|
|
5351 | s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL); |
|
|
5352 | SCHEME_V->args = car (SCHEME_V->args); |
|
|
5353 | s_goto (OP_P0LIST); |
|
|
5354 | } |
|
|
5355 | |
|
|
5356 | case OP_P1LIST: |
|
|
5357 | if (is_pair (SCHEME_V->args)) |
|
|
5358 | { |
|
|
5359 | s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL); |
|
|
5360 | putstr (SCHEME_A_ " "); |
|
|
5361 | SCHEME_V->args = car (SCHEME_V->args); |
|
|
5362 | s_goto (OP_P0LIST); |
|
|
5363 | } |
|
|
5364 | else if (is_vector (SCHEME_V->args)) |
5189 | else if (is_vector (args)) |
5365 | { |
5190 | { |
5366 | s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); |
5191 | s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); |
5367 | putstr (SCHEME_A_ " . "); |
5192 | putstr (SCHEME_A_ " . "); |
5368 | s_goto (OP_P0LIST); |
5193 | s_goto (OP_P0LIST); |
5369 | } |
5194 | } |
5370 | else |
5195 | else |
5371 | { |
5196 | { |
5372 | if (SCHEME_V->args != NIL) |
5197 | if (args != NIL) |
5373 | { |
5198 | { |
5374 | putstr (SCHEME_A_ " . "); |
5199 | putstr (SCHEME_A_ " . "); |
5375 | printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); |
5200 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5376 | } |
5201 | } |
5377 | |
5202 | |
5378 | putstr (SCHEME_A_ ")"); |
5203 | putstr (SCHEME_A_ ")"); |
5379 | s_return (S_T); |
5204 | s_return (S_T); |
5380 | } |
5205 | } |
5381 | |
5206 | |
5382 | case OP_PVECFROM: |
5207 | case OP_PVECFROM: |
5383 | { |
5208 | { |
5384 | int i = ivalue_unchecked (cdr (SCHEME_V->args)); |
5209 | int i = ivalue_unchecked (cdr (args)); |
5385 | pointer vec = car (SCHEME_V->args); |
5210 | pointer vec = car (args); |
5386 | int len = vector_length (vec); |
5211 | int len = veclength (vec); |
5387 | |
5212 | |
5388 | if (i == len) |
5213 | if (i == len) |
5389 | { |
5214 | { |
5390 | putstr (SCHEME_A_ ")"); |
5215 | putstr (SCHEME_A_ ")"); |
5391 | s_return (S_T); |
5216 | s_return (S_T); |
5392 | } |
5217 | } |
5393 | else |
5218 | else |
5394 | { |
5219 | { |
5395 | pointer elem = vector_elem (vec, i); |
5220 | pointer elem = vector_elem (vec, i); |
5396 | |
5221 | |
5397 | ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; |
5222 | ivalue_unchecked (cdr (args)) = i + 1; |
5398 | s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); |
5223 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5399 | SCHEME_V->args = elem; |
5224 | SCHEME_V->args = elem; |
5400 | |
5225 | |
5401 | if (i > 0) |
5226 | if (i > 0) |
5402 | putstr (SCHEME_A_ " "); |
5227 | putstr (SCHEME_A_ " "); |
5403 | |
5228 | |
5404 | s_goto (OP_P0LIST); |
5229 | s_goto (OP_P0LIST); |
5405 | } |
5230 | } |
5406 | } |
5231 | } |
5407 | } |
5232 | } |
5408 | |
5233 | |
5409 | return S_T; |
5234 | abort (); |
5410 | } |
5235 | } |
5411 | |
5236 | |
5412 | static pointer |
5237 | static int |
5413 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5238 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5414 | { |
5239 | { |
|
|
5240 | pointer args = SCHEME_V->args; |
|
|
5241 | pointer a = car (args); |
5415 | pointer x, y; |
5242 | pointer x, y; |
5416 | |
5243 | |
5417 | switch (op) |
5244 | switch (op) |
5418 | { |
5245 | { |
5419 | case OP_LIST_LENGTH: /* length *//* a.k */ |
5246 | case OP_LIST_LENGTH: /* length *//* a.k */ |
5420 | { |
5247 | { |
5421 | long v = list_length (SCHEME_A_ car (SCHEME_V->args)); |
5248 | long v = list_length (SCHEME_A_ a); |
5422 | |
5249 | |
5423 | if (v < 0) |
5250 | if (v < 0) |
5424 | Error_1 ("length: not a list:", car (SCHEME_V->args)); |
5251 | Error_1 ("length: not a list:", a); |
5425 | |
5252 | |
5426 | s_return (mk_integer (SCHEME_A_ v)); |
5253 | s_return (mk_integer (SCHEME_A_ v)); |
5427 | } |
5254 | } |
5428 | |
5255 | |
5429 | case OP_ASSQ: /* assq *//* a.k */ |
5256 | case OP_ASSQ: /* assq *//* a.k */ |
5430 | x = car (SCHEME_V->args); |
5257 | x = a; |
5431 | |
5258 | |
5432 | for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) |
5259 | for (y = cadr (args); is_pair (y); y = cdr (y)) |
5433 | { |
5260 | { |
5434 | if (!is_pair (car (y))) |
5261 | if (!is_pair (car (y))) |
5435 | Error_0 ("unable to handle non pair element"); |
5262 | Error_0 ("unable to handle non pair element"); |
5436 | |
5263 | |
5437 | if (x == caar (y)) |
5264 | if (x == caar (y)) |
… | |
… | |
5443 | else |
5270 | else |
5444 | s_return (S_F); |
5271 | s_return (S_F); |
5445 | |
5272 | |
5446 | |
5273 | |
5447 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5274 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5448 | SCHEME_V->args = car (SCHEME_V->args); |
5275 | SCHEME_V->args = a; |
5449 | |
5276 | |
5450 | if (SCHEME_V->args == NIL) |
5277 | if (SCHEME_V->args == NIL) |
5451 | s_return (S_F); |
5278 | s_return (S_F); |
5452 | else if (is_closure (SCHEME_V->args)) |
5279 | else if (is_closure (SCHEME_V->args)) |
5453 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5280 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
… | |
… | |
5459 | case OP_CLOSUREP: /* closure? */ |
5286 | case OP_CLOSUREP: /* closure? */ |
5460 | /* |
5287 | /* |
5461 | * Note, macro object is also a closure. |
5288 | * Note, macro object is also a closure. |
5462 | * Therefore, (closure? <#MACRO>) ==> #t |
5289 | * Therefore, (closure? <#MACRO>) ==> #t |
5463 | */ |
5290 | */ |
5464 | s_retbool (is_closure (car (SCHEME_V->args))); |
5291 | s_retbool (is_closure (a)); |
5465 | |
5292 | |
5466 | case OP_MACROP: /* macro? */ |
5293 | case OP_MACROP: /* macro? */ |
5467 | s_retbool (is_macro (car (SCHEME_V->args))); |
5294 | s_retbool (is_macro (a)); |
5468 | } |
5295 | } |
5469 | |
5296 | |
5470 | return S_T; /* NOTREACHED */ |
5297 | abort (); |
5471 | } |
5298 | } |
5472 | |
5299 | |
|
|
5300 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5473 | typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); |
5301 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5474 | |
5302 | |
5475 | typedef int (*test_predicate) (pointer); |
5303 | typedef int (*test_predicate)(pointer); |
5476 | static int |
5304 | static int |
5477 | is_any (pointer p) |
5305 | is_any (pointer p) |
5478 | { |
5306 | { |
5479 | return 1; |
5307 | return 1; |
5480 | } |
5308 | } |
5481 | |
5309 | |
5482 | static int |
5310 | static int |
5483 | is_nonneg (pointer p) |
5311 | is_nonneg (pointer p) |
5484 | { |
5312 | { |
5485 | return ivalue (p) >= 0 && is_integer (p); |
5313 | return ivalue (p) >= 0 && is_integer (p); |
|
|
5314 | } |
|
|
5315 | |
|
|
5316 | static int |
|
|
5317 | tst_is_list (pointer p) |
|
|
5318 | { |
|
|
5319 | return p == NIL || is_pair (p); |
5486 | } |
5320 | } |
5487 | |
5321 | |
5488 | /* Correspond carefully with following defines! */ |
5322 | /* Correspond carefully with following defines! */ |
5489 | static struct |
5323 | static struct |
5490 | { |
5324 | { |
5491 | test_predicate fct; |
5325 | test_predicate fct; |
5492 | const char *kind; |
5326 | const char *kind; |
5493 | } tests[] = |
5327 | } tests[] = |
5494 | { |
5328 | { |
5495 | { 0, 0}, /* unused */ |
5329 | { is_any, 0 }, |
5496 | { is_any, 0}, |
5330 | { is_string, "string" }, |
5497 | { is_string, "string" }, |
5331 | { is_symbol, "symbol" }, |
5498 | { is_symbol, "symbol" }, |
5332 | { is_port, "port" }, |
5499 | { is_port, "port" }, |
|
|
5500 | { is_inport, "input port" }, |
5333 | { is_inport, "input port" }, |
5501 | { is_outport, "output port" }, |
5334 | { is_outport, "output port" }, |
5502 | { is_environment, "environment" }, |
5335 | { is_environment, "environment" }, |
5503 | { is_pair, "pair" }, |
5336 | { is_pair, "pair" }, |
5504 | { 0, "pair or '()" }, |
5337 | { tst_is_list, "pair or '()" }, |
5505 | { is_character, "character" }, |
5338 | { is_character, "character" }, |
5506 | { is_vector, "vector" }, |
5339 | { is_vector, "vector" }, |
5507 | { is_number, "number" }, |
5340 | { is_number, "number" }, |
5508 | { is_integer, "integer" }, |
5341 | { is_integer, "integer" }, |
5509 | { is_nonneg, "non-negative integer" } |
5342 | { is_nonneg, "non-negative integer" } |
5510 | }; |
5343 | }; |
5511 | |
5344 | |
5512 | #define TST_NONE 0 |
5345 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5513 | #define TST_ANY "\001" |
5346 | #define TST_ANY "\001" |
5514 | #define TST_STRING "\002" |
5347 | #define TST_STRING "\002" |
5515 | #define TST_SYMBOL "\003" |
5348 | #define TST_SYMBOL "\003" |
5516 | #define TST_PORT "\004" |
5349 | #define TST_PORT "\004" |
5517 | #define TST_INPORT "\005" |
5350 | #define TST_INPORT "\005" |
5518 | #define TST_OUTPORT "\006" |
5351 | #define TST_OUTPORT "\006" |
5519 | #define TST_ENVIRONMENT "\007" |
5352 | #define TST_ENVIRONMENT "\007" |
5520 | #define TST_PAIR "\010" |
5353 | #define TST_PAIR "\010" |
5521 | #define TST_LIST "\011" |
5354 | #define TST_LIST "\011" |
5522 | #define TST_CHAR "\012" |
5355 | #define TST_CHAR "\012" |
5523 | #define TST_VECTOR "\013" |
5356 | #define TST_VECTOR "\013" |
5524 | #define TST_NUMBER "\014" |
5357 | #define TST_NUMBER "\014" |
5525 | #define TST_INTEGER "\015" |
5358 | #define TST_INTEGER "\015" |
5526 | #define TST_NATURAL "\016" |
5359 | #define TST_NATURAL "\016" |
|
|
5360 | |
|
|
5361 | #define INF_ARG 0xff |
|
|
5362 | #define UNNAMED_OP "" |
|
|
5363 | |
|
|
5364 | static const char opnames[] = |
|
|
5365 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
|
|
5366 | #include "opdefines.h" |
|
|
5367 | #undef OP_DEF |
|
|
5368 | ; |
|
|
5369 | |
|
|
5370 | static const char * |
|
|
5371 | opname (int idx) |
|
|
5372 | { |
|
|
5373 | const char *name = opnames; |
|
|
5374 | |
|
|
5375 | /* should do this at compile time, but would require external program, right? */ |
|
|
5376 | while (idx--) |
|
|
5377 | name += strlen (name) + 1; |
|
|
5378 | |
|
|
5379 | return *name ? name : "ILLEGAL"; |
|
|
5380 | } |
|
|
5381 | |
|
|
5382 | static const char * |
|
|
5383 | procname (pointer x) |
|
|
5384 | { |
|
|
5385 | return opname (procnum (x)); |
|
|
5386 | } |
5527 | |
5387 | |
5528 | typedef struct |
5388 | typedef struct |
5529 | { |
5389 | { |
5530 | dispatch_func func; |
5390 | uint8_t func; |
5531 | char *name; |
5391 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
|
|
5392 | uint8_t builtin; |
5532 | int min_arity; |
5393 | uint8_t min_arity; |
5533 | int max_arity; |
5394 | uint8_t max_arity; |
5534 | char *arg_tests_encoding; |
5395 | char arg_tests_encoding[3]; |
5535 | } op_code_info; |
5396 | } op_code_info; |
5536 | |
5397 | |
5537 | #define INF_ARG 0xffff |
|
|
5538 | |
|
|
5539 | static op_code_info dispatch_table[] = { |
5398 | static const op_code_info dispatch_table[] = { |
5540 | #define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, |
5399 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest }, |
5541 | #include "opdefines.h" |
5400 | #include "opdefines.h" |
|
|
5401 | #undef OP_DEF |
5542 | {0} |
5402 | {0} |
5543 | }; |
5403 | }; |
5544 | |
|
|
5545 | static const char * |
|
|
5546 | procname (pointer x) |
|
|
5547 | { |
|
|
5548 | int n = procnum (x); |
|
|
5549 | const char *name = dispatch_table[n].name; |
|
|
5550 | |
|
|
5551 | if (name == 0) |
|
|
5552 | name = "ILLEGAL!"; |
|
|
5553 | |
|
|
5554 | return name; |
|
|
5555 | } |
|
|
5556 | |
5404 | |
5557 | /* kernel of this interpreter */ |
5405 | /* kernel of this interpreter */ |
5558 | static void |
5406 | static void |
5559 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5407 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5560 | { |
5408 | { |
5561 | SCHEME_V->op = op; |
5409 | SCHEME_V->op = op; |
5562 | |
5410 | |
5563 | for (;;) |
5411 | for (;;) |
5564 | { |
5412 | { |
5565 | op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5413 | const op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5566 | |
5414 | |
5567 | #if USE_ERROR_CHECKING |
5415 | #if USE_ERROR_CHECKING |
5568 | if (pcd->name) /* if built-in function, check arguments */ |
5416 | if (pcd->builtin) /* if built-in function, check arguments */ |
5569 | { |
5417 | { |
5570 | int ok = 1; |
|
|
5571 | char msg[STRBUFFSIZE]; |
5418 | char msg[STRBUFFSIZE]; |
5572 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5419 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5573 | |
5420 | |
5574 | /* Check number of arguments */ |
5421 | /* Check number of arguments */ |
5575 | if (n < pcd->min_arity) |
5422 | if (ecb_expect_false (n < pcd->min_arity)) |
5576 | { |
5423 | { |
5577 | ok = 0; |
|
|
5578 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5424 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5579 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
5425 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
|
|
5426 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5427 | continue; |
5580 | } |
5428 | } |
5581 | |
5429 | else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG)) |
5582 | if (ok && n > pcd->max_arity) |
|
|
5583 | { |
5430 | { |
5584 | ok = 0; |
|
|
5585 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5431 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5586 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
5432 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
|
|
5433 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5434 | continue; |
5587 | } |
5435 | } |
5588 | |
5436 | else |
5589 | if (ok) |
|
|
5590 | { |
5437 | { |
5591 | if (pcd->arg_tests_encoding) |
5438 | if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */ |
5592 | { |
5439 | { |
5593 | int i = 0; |
5440 | int i = 0; |
5594 | int j; |
5441 | int j; |
5595 | const char *t = pcd->arg_tests_encoding; |
5442 | const char *t = pcd->arg_tests_encoding; |
5596 | pointer arglist = SCHEME_V->args; |
5443 | pointer arglist = SCHEME_V->args; |
5597 | |
5444 | |
5598 | do |
5445 | do |
5599 | { |
5446 | { |
5600 | pointer arg = car (arglist); |
5447 | pointer arg = car (arglist); |
5601 | |
5448 | |
5602 | j = (int) t[0]; |
5449 | j = t[0]; |
5603 | |
5450 | |
5604 | if (j == TST_LIST[0]) |
5451 | if (!tests[j - 1].fct (arg)) |
5605 | { |
|
|
5606 | if (arg != NIL && !is_pair (arg)) |
|
|
5607 | break; |
5452 | break; |
5608 | } |
|
|
5609 | else |
|
|
5610 | { |
|
|
5611 | if (!tests[j].fct (arg)) |
|
|
5612 | break; |
|
|
5613 | } |
|
|
5614 | |
5453 | |
5615 | if (t[1] != 0) /* last test is replicated as necessary */ |
5454 | if (t[1]) /* last test is replicated as necessary */ |
5616 | t++; |
5455 | t++; |
5617 | |
5456 | |
5618 | arglist = cdr (arglist); |
5457 | arglist = cdr (arglist); |
5619 | i++; |
5458 | i++; |
5620 | } |
5459 | } |
5621 | while (i < n); |
5460 | while (i < n); |
5622 | |
5461 | |
5623 | if (i < n) |
5462 | if (i < n) |
5624 | { |
5463 | { |
5625 | ok = 0; |
|
|
5626 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); |
5464 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind); |
|
|
5465 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5466 | continue; |
5627 | } |
5467 | } |
5628 | } |
5468 | } |
5629 | } |
5469 | } |
5630 | |
|
|
5631 | if (!ok) |
|
|
5632 | { |
|
|
5633 | if (xError_1 (SCHEME_A_ msg, 0) == NIL) |
|
|
5634 | return; |
|
|
5635 | |
|
|
5636 | pcd = dispatch_table + SCHEME_V->op; |
|
|
5637 | } |
|
|
5638 | } |
5470 | } |
5639 | #endif |
5471 | #endif |
5640 | |
5472 | |
5641 | ok_to_freely_gc (SCHEME_A); |
5473 | ok_to_freely_gc (SCHEME_A); |
5642 | |
5474 | |
5643 | if (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL) |
5475 | static const dispatch_func dispatch_funcs[] = { |
|
|
5476 | opexe_0, |
|
|
5477 | opexe_1, |
|
|
5478 | opexe_2, |
|
|
5479 | opexe_3, |
|
|
5480 | opexe_4, |
|
|
5481 | opexe_5, |
|
|
5482 | opexe_6, |
|
|
5483 | }; |
|
|
5484 | |
|
|
5485 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5644 | return; |
5486 | return; |
5645 | |
5487 | |
5646 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5488 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5647 | { |
5489 | { |
5648 | xwrstr ("No memory!\n"); |
5490 | xwrstr ("No memory!\n"); |
… | |
… | |
5706 | |
5548 | |
5707 | case 'd': |
5549 | case 'd': |
5708 | return OP_COND0; /* cond */ |
5550 | return OP_COND0; /* cond */ |
5709 | |
5551 | |
5710 | case '*': |
5552 | case '*': |
5711 | return OP_LET0AST; /* let* */ |
5553 | return OP_LET0AST;/* let* */ |
5712 | |
5554 | |
5713 | default: |
5555 | default: |
5714 | return OP_SET0; /* set! */ |
5556 | return OP_SET0; /* set! */ |
5715 | } |
5557 | } |
5716 | |
5558 | |
… | |
… | |
5738 | |
5580 | |
5739 | case 'f': |
5581 | case 'f': |
5740 | return OP_DEF0; /* define */ |
5582 | return OP_DEF0; /* define */ |
5741 | |
5583 | |
5742 | default: |
5584 | default: |
5743 | return OP_LET0REC; /* letrec */ |
5585 | return OP_LET0REC;/* letrec */ |
5744 | } |
5586 | } |
5745 | |
5587 | |
5746 | default: |
5588 | default: |
5747 | return OP_C0STREAM; /* cons-stream */ |
5589 | return OP_C0STREAM; /* cons-stream */ |
5748 | } |
5590 | } |
… | |
… | |
5815 | set_cdr (S_T, S_T); |
5657 | set_cdr (S_T, S_T); |
5816 | /* init F */ |
5658 | /* init F */ |
5817 | set_typeflag (S_F, T_ATOM | T_MARK); |
5659 | set_typeflag (S_F, T_ATOM | T_MARK); |
5818 | set_car (S_F, S_F); |
5660 | set_car (S_F, S_F); |
5819 | set_cdr (S_F, S_F); |
5661 | set_cdr (S_F, S_F); |
|
|
5662 | /* init EOF_OBJ */ |
|
|
5663 | set_typeflag (S_EOF, T_ATOM | T_MARK); |
|
|
5664 | set_car (S_EOF, S_EOF); |
|
|
5665 | set_cdr (S_EOF, S_EOF); |
5820 | /* init sink */ |
5666 | /* init sink */ |
5821 | set_typeflag (S_SINK, T_PAIR | T_MARK); |
5667 | set_typeflag (S_SINK, T_PAIR | T_MARK); |
5822 | set_car (S_SINK, NIL); |
5668 | set_car (S_SINK, NIL); |
|
|
5669 | |
5823 | /* init c_nest */ |
5670 | /* init c_nest */ |
5824 | SCHEME_V->c_nest = NIL; |
5671 | SCHEME_V->c_nest = NIL; |
5825 | |
5672 | |
5826 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
5673 | SCHEME_V->oblist = oblist_initial_value (SCHEME_A); |
5827 | /* init global_env */ |
5674 | /* init global_env */ |
… | |
… | |
5840 | |
5687 | |
5841 | for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) |
5688 | for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) |
5842 | assign_syntax (SCHEME_A_ syntax_names[i]); |
5689 | assign_syntax (SCHEME_A_ syntax_names[i]); |
5843 | } |
5690 | } |
5844 | |
5691 | |
|
|
5692 | // TODO: should iterate via strlen, to avoid n² complexity |
5845 | for (i = 0; i < n; i++) |
5693 | for (i = 0; i < n; i++) |
5846 | if (dispatch_table[i].name != 0) |
5694 | if (dispatch_table[i].builtin) |
5847 | assign_proc (SCHEME_A_ i, dispatch_table[i].name); |
5695 | assign_proc (SCHEME_A_ i, opname (i)); |
5848 | |
5696 | |
5849 | /* initialization of global pointers to special symbols */ |
5697 | /* initialization of global pointers to special symbols */ |
5850 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
5698 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
5851 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
5699 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
5852 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
5700 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
5853 | SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); |
5701 | SCHEME_V->UNQUOTE = mk_symbol (SCHEME_A_ "unquote"); |
5854 | SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); |
5702 | SCHEME_V->UNQUOTESP = mk_symbol (SCHEME_A_ "unquote-splicing"); |
5855 | SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); |
5703 | SCHEME_V->FEED_TO = mk_symbol (SCHEME_A_ "=>"); |
5856 | SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); |
5704 | SCHEME_V->COLON_HOOK = mk_symbol (SCHEME_A_ "*colon-hook*"); |
5857 | SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); |
5705 | SCHEME_V->ERROR_HOOK = mk_symbol (SCHEME_A_ "*error-hook*"); |
5858 | SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); |
5706 | SCHEME_V->SHARP_HOOK = mk_symbol (SCHEME_A_ "*sharp-hook*"); |
5859 | SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); |
5707 | SCHEME_V->COMPILE_HOOK = mk_symbol (SCHEME_A_ "*compile-hook*"); |
5860 | |
5708 | |
5861 | return !SCHEME_V->no_memory; |
5709 | return !SCHEME_V->no_memory; |
5862 | } |
5710 | } |
5863 | |
5711 | |
… | |
… | |
5993 | { |
5841 | { |
5994 | dump_stack_reset (SCHEME_A); |
5842 | dump_stack_reset (SCHEME_A); |
5995 | SCHEME_V->envir = SCHEME_V->global_env; |
5843 | SCHEME_V->envir = SCHEME_V->global_env; |
5996 | SCHEME_V->file_i = 0; |
5844 | SCHEME_V->file_i = 0; |
5997 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5845 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5998 | SCHEME_V->load_stack[0].rep.string.start = (char *) cmd; /* This func respects const */ |
5846 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5999 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *) cmd + strlen (cmd); |
5847 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
6000 | SCHEME_V->load_stack[0].rep.string.curr = (char *) cmd; |
5848 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
6001 | #if USE_PORTS |
5849 | #if USE_PORTS |
6002 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5850 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
6003 | #endif |
5851 | #endif |
6004 | SCHEME_V->retcode = 0; |
5852 | SCHEME_V->retcode = 0; |
6005 | SCHEME_V->interactive_repl = 0; |
5853 | SCHEME_V->interactive_repl = 0; |
… | |
… | |
6110 | |
5958 | |
6111 | /* ========== Main ========== */ |
5959 | /* ========== Main ========== */ |
6112 | |
5960 | |
6113 | #if STANDALONE |
5961 | #if STANDALONE |
6114 | |
5962 | |
6115 | # if defined(__APPLE__) && !defined (OSX) |
|
|
6116 | int |
|
|
6117 | main () |
|
|
6118 | { |
|
|
6119 | extern MacTS_main (int argc, char **argv); |
|
|
6120 | char **argv; |
|
|
6121 | int argc = ccommand (&argv); |
|
|
6122 | |
|
|
6123 | MacTS_main (argc, argv); |
|
|
6124 | return 0; |
|
|
6125 | } |
|
|
6126 | |
|
|
6127 | int |
|
|
6128 | MacTS_main (int argc, char **argv) |
|
|
6129 | { |
|
|
6130 | # else |
|
|
6131 | int |
5963 | int |
6132 | main (int argc, char **argv) |
5964 | main (int argc, char **argv) |
6133 | { |
5965 | { |
6134 | # endif |
|
|
6135 | # if USE_MULTIPLICITY |
5966 | # if USE_MULTIPLICITY |
6136 | scheme ssc; |
5967 | scheme ssc; |
6137 | scheme *const SCHEME_V = &ssc; |
5968 | scheme *const SCHEME_V = &ssc; |
6138 | # else |
5969 | # else |
6139 | # endif |
5970 | # endif |