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