ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/microscheme/scheme.c
(Generate patch)

Comparing microscheme/scheme.c (file contents):
Revision 1.12 by root, Thu Nov 26 07:30:25 2015 UTC vs.
Revision 1.20 by root, Thu Nov 26 22:53:28 2015 UTC

70/* should use libecb */ 70/* should use libecb */
71#if __GNUC__ >= 4 71#if __GNUC__ >= 4
72# define ecb_expect(expr,value) __builtin_expect ((expr),(value)) 72# define ecb_expect(expr,value) __builtin_expect ((expr),(value))
73# define ecb_expect_false(expr) ecb_expect (!!(expr), 0) 73# define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
74# define ecb_expect_true(expr) ecb_expect (!!(expr), 1) 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)
75#endif 78#endif
76 79
77#if !USE_MULTIPLICITY 80#if !USE_MULTIPLICITY
78static scheme sc; 81static scheme sc;
79#endif 82#endif
208#define T_SYNTAX 0x0010 211#define T_SYNTAX 0x0010
209#define T_IMMUTABLE 0x0020 212#define T_IMMUTABLE 0x0020
210#define T_ATOM 0x0040 /* only for gc */ 213#define T_ATOM 0x0040 /* only for gc */
211#define T_MARK 0x0080 /* only for gc */ 214#define T_MARK 0x0080 /* only for gc */
212 215
213static num num_add (num a, num b); 216enum num_op { NUM_ADD, NUM_SUB, NUM_MUL, NUM_INTDIV };
214static num num_mul (num a, num b); 217
215static num num_div (num a, num b); 218static num num_op (enum num_op op, num a, num b);
216static num num_intdiv (num a, num b); 219static num num_intdiv (num a, num b);
217static num num_sub (num a, num b);
218static num num_rem (num a, num b); 220static num num_rem (num a, num b);
219static num num_mod (num a, num b); 221static num num_mod (num a, num b);
220static int num_eq (num a, num b);
221static int num_gt (num a, num b);
222static int num_ge (num a, num b);
223static int num_lt (num a, num b);
224static int num_le (num a, num b);
225 222
226#if USE_MATH 223#if USE_MATH
227static double round_per_R5RS (double x); 224static double round_per_R5RS (double x);
228#endif 225#endif
229static int is_zero_rvalue (RVALUE x); 226static int is_zero_rvalue (RVALUE x);
250 247
251#define strvalue(p) ((p)->object.string.svalue) 248#define strvalue(p) ((p)->object.string.svalue)
252#define strlength(p) ((p)->object.string.length) 249#define strlength(p) ((p)->object.string.length)
253 250
254INTERFACE int is_list (SCHEME_P_ pointer p); 251INTERFACE int is_list (SCHEME_P_ pointer p);
252
255INTERFACE INLINE int 253INTERFACE INLINE int
256is_vector (pointer p) 254is_vector (pointer p)
257{ 255{
258 return type (p) == T_VECTOR; 256 return type (p) == T_VECTOR;
259} 257}
669static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); 667static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list);
670static pointer revappend (SCHEME_P_ pointer a, pointer b); 668static pointer revappend (SCHEME_P_ pointer a, pointer b);
671static pointer ss_get_cont (SCHEME_P); 669static pointer ss_get_cont (SCHEME_P);
672static void ss_set_cont (SCHEME_P_ pointer cont); 670static void ss_set_cont (SCHEME_P_ pointer cont);
673static void dump_stack_mark (SCHEME_P); 671static void dump_stack_mark (SCHEME_P);
674static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); 672static int opexe_0 (SCHEME_P_ enum scheme_opcodes op);
673static int opexe_1 (SCHEME_P_ enum scheme_opcodes op);
675static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); 674static int opexe_2 (SCHEME_P_ enum scheme_opcodes op);
676static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); 675static int opexe_3 (SCHEME_P_ enum scheme_opcodes op);
677static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); 676static int opexe_4 (SCHEME_P_ enum scheme_opcodes op);
678static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); 677static int opexe_5 (SCHEME_P_ enum scheme_opcodes op);
679static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); 678static int opexe_6 (SCHEME_P_ enum scheme_opcodes op);
680static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); 679static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op);
681static void assign_syntax (SCHEME_P_ const char *name); 680static void assign_syntax (SCHEME_P_ const char *name);
682static int syntaxnum (pointer p); 681static int syntaxnum (pointer p);
683static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); 682static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name);
684 683
685static num 684static num
686num_add (num a, num b) 685num_op (enum num_op op, num a, num b)
687{ 686{
688 num ret; 687 num ret;
689 688
690 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b)); 689 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
691 690
692 if (num_is_fixnum (ret)) 691 if (num_is_fixnum (ret))
693 num_set_ivalue (ret, num_get_ivalue (a) + num_get_ivalue (b)); 692 {
693 IVALUE av = num_get_ivalue (a);
694 IVALUE bv = num_get_ivalue (b);
695
696 switch (op)
697 {
698 case NUM_ADD: av += bv; break;
699 case NUM_SUB: av -= bv; break;
700 case NUM_MUL: av *= bv; break;
701 case NUM_INTDIV: av /= bv; break;
702 }
703
704 num_set_ivalue (ret, av);
705 }
694 else 706 else
695 num_set_rvalue (ret, num_get_rvalue (a) + num_get_rvalue (b)); 707 {
708 RVALUE av = num_get_rvalue (a);
709 RVALUE bv = num_get_rvalue (b);
696 710
697 return ret; 711 switch (op)
698} 712 {
713 case NUM_ADD: av += bv; break;
714 case NUM_SUB: av -= bv; break;
715 case NUM_MUL: av *= bv; break;
716 case NUM_INTDIV: av /= bv; break;
717 }
699 718
700static num 719 num_set_rvalue (ret, av);
701num_mul (num a, num b) 720 }
702{
703 num ret;
704
705 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
706
707 if (num_is_fixnum (ret))
708 num_set_ivalue (ret, num_get_ivalue (a) * num_get_ivalue (b));
709 else
710 num_set_rvalue (ret, num_get_rvalue (a) * num_get_rvalue (b));
711 721
712 return ret; 722 return ret;
713} 723}
714 724
715static num 725static num
726 736
727 return ret; 737 return ret;
728} 738}
729 739
730static num 740static num
731num_intdiv (num a, num b)
732{
733 num ret;
734
735 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
736
737 if (num_is_fixnum (ret))
738 num_set_ivalue (ret, num_get_ivalue (a) / num_get_ivalue (b));
739 else
740 num_set_rvalue (ret, num_get_rvalue (a) / num_get_rvalue (b));
741
742 return ret;
743}
744
745static num
746num_sub (num a, num b)
747{
748 num ret;
749
750 num_set_fixnum (ret, num_is_fixnum (a) && num_is_fixnum (b));
751
752 if (num_is_fixnum (ret))
753 num_set_ivalue (ret, num_get_ivalue (a) - num_get_ivalue (b));
754 else
755 num_set_rvalue (ret, num_get_rvalue (a) - num_get_rvalue (b));
756
757 return ret;
758}
759
760static num
761num_rem (num a, num b) 741num_rem (num a, num b)
762{ 742{
763 num ret; 743 num ret;
764 long e1, e2, res; 744 long e1, e2, res;
765 745
801 781
802 num_set_ivalue (ret, res); 782 num_set_ivalue (ret, res);
803 return ret; 783 return ret;
804} 784}
805 785
786/* this completely disrespects NaNs */
806static int 787static int
807num_eq (num a, num b) 788num_cmp (num a, num b)
808{ 789{
790 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
809 int ret; 791 int ret;
810 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
811 792
812 if (is_fixnum) 793 if (is_fixnum)
813 ret = num_get_ivalue (a) == num_get_ivalue (b); 794 {
795 IVALUE av = num_get_ivalue (a);
796 IVALUE bv = num_get_ivalue (b);
797
798 ret = av == bv ? 0 : av < bv ? -1 : +1;
799 }
814 else 800 else
815 ret = num_get_rvalue (a) == num_get_rvalue (b); 801 {
802 RVALUE av = num_get_rvalue (a);
803 RVALUE bv = num_get_rvalue (b);
804
805 ret = av == bv ? 0 : av < bv ? -1 : +1;
806 }
816 807
817 return ret; 808 return ret;
818}
819
820
821static int
822num_gt (num a, num b)
823{
824 int ret;
825 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
826
827 if (is_fixnum)
828 ret = num_get_ivalue (a) > num_get_ivalue (b);
829 else
830 ret = num_get_rvalue (a) > num_get_rvalue (b);
831
832 return ret;
833}
834
835static int
836num_ge (num a, num b)
837{
838 return !num_lt (a, b);
839}
840
841static int
842num_lt (num a, num b)
843{
844 int ret;
845 int is_fixnum = num_is_fixnum (a) && num_is_fixnum (b);
846
847 if (is_fixnum)
848 ret = num_get_ivalue (a) < num_get_ivalue (b);
849 else
850 ret = num_get_rvalue (a) < num_get_rvalue (b);
851
852 return ret;
853}
854
855static int
856num_le (num a, num b)
857{
858 return !num_gt (a, b);
859} 809}
860 810
861#if USE_MATH 811#if USE_MATH
862 812
863/* Round to nearest. Round to even if midway */ 813/* Round to nearest. Round to even if midway */
873 return ce; 823 return ce;
874 else if (dfl < dce) 824 else if (dfl < dce)
875 return fl; 825 return fl;
876 else 826 else
877 { 827 {
878 if (fmod (fl, 2.0) == 0.0) /* I imagine this holds */ 828 if (fmod (fl, 2) == 0) /* I imagine this holds */
879 return fl; 829 return fl;
880 else 830 else
881 return ce; 831 return ce;
882 } 832 }
883} 833}
2796 } 2746 }
2797 else if (is_number (a)) 2747 else if (is_number (a))
2798 { 2748 {
2799 if (is_number (b)) 2749 if (is_number (b))
2800 if (num_is_integer (a) == num_is_integer (b)) 2750 if (num_is_integer (a) == num_is_integer (b))
2801 return num_eq (nvalue (a), nvalue (b)); 2751 return num_cmp (nvalue (a), nvalue (b)) == 0;
2802 2752
2803 return 0; 2753 return 0;
2804 } 2754 }
2805 else if (is_character (a)) 2755 else if (is_character (a))
2806 { 2756 {
2982 return cdr (slot); 2932 return cdr (slot);
2983} 2933}
2984 2934
2985/* ========== Evaluation Cycle ========== */ 2935/* ========== Evaluation Cycle ========== */
2986 2936
2987static pointer 2937static int
2988xError_1 (SCHEME_P_ const char *s, pointer a) 2938xError_1 (SCHEME_P_ const char *s, pointer a)
2989{ 2939{
2990#if USE_ERROR_HOOK 2940#if USE_ERROR_HOOK
2991 pointer x; 2941 pointer x;
2992 pointer hdl = SCHEME_V->ERROR_HOOK; 2942 pointer hdl = SCHEME_V->ERROR_HOOK;
3027 code = cons (mk_string (SCHEME_A_ s), code); 2977 code = cons (mk_string (SCHEME_A_ s), code);
3028 setimmutable (car (code)); 2978 setimmutable (car (code));
3029 SCHEME_V->code = cons (slot_value_in_env (x), code); 2979 SCHEME_V->code = cons (slot_value_in_env (x), code);
3030 SCHEME_V->op = OP_EVAL; 2980 SCHEME_V->op = OP_EVAL;
3031 2981
3032 return S_T; 2982 return 0;
3033 } 2983 }
3034#endif 2984#endif
3035 2985
3036 if (a) 2986 if (a)
3037 SCHEME_V->args = cons (a, NIL); 2987 SCHEME_V->args = cons (a, NIL);
3039 SCHEME_V->args = NIL; 2989 SCHEME_V->args = NIL;
3040 2990
3041 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args); 2991 SCHEME_V->args = cons (mk_string (SCHEME_A_ s), SCHEME_V->args);
3042 setimmutable (car (SCHEME_V->args)); 2992 setimmutable (car (SCHEME_V->args));
3043 SCHEME_V->op = OP_ERR0; 2993 SCHEME_V->op = OP_ERR0;
2994
3044 return S_T; 2995 return 0;
3045} 2996}
3046 2997
3047#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a) 2998#define Error_1(s, a) return xError_1(SCHEME_A_ USE_ERROR_CHECKING ? s : "", a)
3048#define Error_0(s) Error_1 (s, 0) 2999#define Error_0(s) Error_1 (s, 0)
3049 3000
3050/* Too small to turn into function */ 3001/* Too small to turn into function */
3051#define BEGIN do { 3002#define BEGIN do {
3052#define END } while (0) 3003#define END } while (0)
3053#define s_goto(a) BEGIN \ 3004#define s_goto(a) BEGIN \
3054 SCHEME_V->op = a; \ 3005 SCHEME_V->op = a; \
3055 return S_T; END 3006 return 0; END
3056 3007
3057#define s_return(a) return xs_return (SCHEME_A_ a) 3008#define s_return(a) return xs_return (SCHEME_A_ a)
3058 3009
3059#ifndef USE_SCHEME_STACK 3010#ifndef USE_SCHEME_STACK
3060 3011
3085 next_frame = SCHEME_V->dump_base + nframes; 3036 next_frame = SCHEME_V->dump_base + nframes;
3086 3037
3087 next_frame->op = op; 3038 next_frame->op = op;
3088 next_frame->args = args; 3039 next_frame->args = args;
3089 next_frame->envir = SCHEME_V->envir; 3040 next_frame->envir = SCHEME_V->envir;
3090 next_frame->code = code; 3041 next_frame->code = code;
3091 3042
3092 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); 3043 SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1);
3093} 3044}
3094 3045
3095static pointer 3046static int
3096xs_return (SCHEME_P_ pointer a) 3047xs_return (SCHEME_P_ pointer a)
3097{ 3048{
3098 int nframes = (uintptr_t)SCHEME_V->dump; 3049 int nframes = (uintptr_t)SCHEME_V->dump;
3099 struct dump_stack_frame *frame; 3050 struct dump_stack_frame *frame;
3100 3051
3101 SCHEME_V->value = a; 3052 SCHEME_V->value = a;
3102 3053
3103 if (nframes <= 0) 3054 if (nframes <= 0)
3104 return NIL; 3055 return -1;
3105 3056
3106 frame = &SCHEME_V->dump_base[--nframes]; 3057 frame = &SCHEME_V->dump_base[--nframes];
3107 SCHEME_V->op = frame->op; 3058 SCHEME_V->op = frame->op;
3108 SCHEME_V->args = frame->args; 3059 SCHEME_V->args = frame->args;
3109 SCHEME_V->envir = frame->envir; 3060 SCHEME_V->envir = frame->envir;
3110 SCHEME_V->code = frame->code; 3061 SCHEME_V->code = frame->code;
3111 SCHEME_V->dump = (pointer)(uintptr_t)nframes; 3062 SCHEME_V->dump = (pointer)(uintptr_t)nframes;
3112 3063
3113 return S_T; 3064 return 0;
3114} 3065}
3115 3066
3116static INLINE void 3067static INLINE void
3117dump_stack_reset (SCHEME_P) 3068dump_stack_reset (SCHEME_P)
3118{ 3069{
3213dump_stack_free (SCHEME_P) 3164dump_stack_free (SCHEME_P)
3214{ 3165{
3215 SCHEME_V->dump = NIL; 3166 SCHEME_V->dump = NIL;
3216} 3167}
3217 3168
3218static pointer 3169static int
3219xs_return (SCHEME_P_ pointer a) 3170xs_return (SCHEME_P_ pointer a)
3220{ 3171{
3221 pointer dump = SCHEME_V->dump; 3172 pointer dump = SCHEME_V->dump;
3222 3173
3223 SCHEME_V->value = a; 3174 SCHEME_V->value = a;
3224 3175
3225 if (dump == NIL) 3176 if (dump == NIL)
3226 return NIL; 3177 return -1;
3227 3178
3228 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); 3179 SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump);
3229 SCHEME_V->args = car (dump) ; dump = cdr (dump); 3180 SCHEME_V->args = car (dump) ; dump = cdr (dump);
3230 SCHEME_V->envir = car (dump) ; dump = cdr (dump); 3181 SCHEME_V->envir = car (dump) ; dump = cdr (dump);
3231 SCHEME_V->code = car (dump) ; dump = cdr (dump); 3182 SCHEME_V->code = car (dump) ; dump = cdr (dump);
3232 3183
3233 SCHEME_V->dump = dump; 3184 SCHEME_V->dump = dump;
3234 3185
3235 return S_T; 3186 return 0;
3236} 3187}
3237 3188
3238static void 3189static void
3239s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) 3190s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code)
3240{ 3191{
3265 3216
3266#endif 3217#endif
3267 3218
3268#define s_retbool(tf) s_return ((tf) ? S_T : S_F) 3219#define s_retbool(tf) s_return ((tf) ? S_T : S_F)
3269 3220
3270static pointer 3221static int
3271opexe_0 (SCHEME_P_ enum scheme_opcodes op) 3222opexe_0 (SCHEME_P_ enum scheme_opcodes op)
3272{ 3223{
3224 pointer args = SCHEME_V->args;
3273 pointer x, y; 3225 pointer x, y;
3274 3226
3275 switch (op) 3227 switch (op)
3276 { 3228 {
3277 case OP_LOAD: /* load */ 3229 case OP_LOAD: /* load */
3278 if (file_interactive (SCHEME_A)) 3230 if (file_interactive (SCHEME_A))
3279 { 3231 {
3280 xwrstr ("Loading "); xwrstr (strvalue (car (SCHEME_V->args))); xwrstr ("\n"); 3232 xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n");
3281 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (SCHEME_V->args))); 3233 //D fprintf (SCHEME_V->outport->object.port->rep.stdio.file, "Loading %s\n", strvalue (car (args)));
3282 } 3234 }
3283 3235
3284 if (!file_push (SCHEME_A_ strvalue (car (SCHEME_V->args)))) 3236 if (!file_push (SCHEME_A_ strvalue (car (args))))
3285 Error_1 ("unable to open", car (SCHEME_V->args)); 3237 Error_1 ("unable to open", car (args));
3286 else 3238 else
3287 { 3239 {
3288 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); 3240 SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i);
3289 s_goto (OP_T0LVL); 3241 s_goto (OP_T0LVL);
3290 } 3242 }
3364 case OP_EVAL: /* main part of evaluation */ 3316 case OP_EVAL: /* main part of evaluation */
3365#if USE_TRACING 3317#if USE_TRACING
3366 if (SCHEME_V->tracing) 3318 if (SCHEME_V->tracing)
3367 { 3319 {
3368 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ 3320 /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */
3369 s_save (SCHEME_A_ OP_REAL_EVAL, SCHEME_V->args, SCHEME_V->code); 3321 s_save (SCHEME_A_ OP_REAL_EVAL, args, SCHEME_V->code);
3370 SCHEME_V->args = SCHEME_V->code; 3322 SCHEME_V->args = SCHEME_V->code;
3371 putstr (SCHEME_A_ "\nEval: "); 3323 putstr (SCHEME_A_ "\nEval: ");
3372 s_goto (OP_P0LIST); 3324 s_goto (OP_P0LIST);
3373 } 3325 }
3374 3326
3418 SCHEME_V->code = cdr (SCHEME_V->code); 3370 SCHEME_V->code = cdr (SCHEME_V->code);
3419 s_goto (OP_E1ARGS); 3371 s_goto (OP_E1ARGS);
3420 } 3372 }
3421 3373
3422 case OP_E1ARGS: /* eval arguments */ 3374 case OP_E1ARGS: /* eval arguments */
3423 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3375 args = cons (SCHEME_V->value, args);
3424 3376
3425 if (is_pair (SCHEME_V->code)) /* continue */ 3377 if (is_pair (SCHEME_V->code)) /* continue */
3426 { 3378 {
3427 s_save (SCHEME_A_ OP_E1ARGS, SCHEME_V->args, cdr (SCHEME_V->code)); 3379 s_save (SCHEME_A_ OP_E1ARGS, args, cdr (SCHEME_V->code));
3428 SCHEME_V->code = car (SCHEME_V->code); 3380 SCHEME_V->code = car (SCHEME_V->code);
3429 SCHEME_V->args = NIL; 3381 SCHEME_V->args = NIL;
3430 s_goto (OP_EVAL); 3382 s_goto (OP_EVAL);
3431 } 3383 }
3432 else /* end */ 3384 else /* end */
3433 { 3385 {
3434 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3386 args = reverse_in_place (SCHEME_A_ NIL, args);
3435 SCHEME_V->code = car (SCHEME_V->args); 3387 SCHEME_V->code = car (args);
3436 SCHEME_V->args = cdr (SCHEME_V->args); 3388 SCHEME_V->args = cdr (args);
3437 s_goto (OP_APPLY); 3389 s_goto (OP_APPLY);
3438 } 3390 }
3439 3391
3440#if USE_TRACING 3392#if USE_TRACING
3441 3393
3442 case OP_TRACING: 3394 case OP_TRACING:
3443 { 3395 {
3444 int tr = SCHEME_V->tracing; 3396 int tr = SCHEME_V->tracing;
3445 3397
3446 SCHEME_V->tracing = ivalue (car (SCHEME_V->args)); 3398 SCHEME_V->tracing = ivalue (car (args));
3447 s_return (mk_integer (SCHEME_A_ tr)); 3399 s_return (mk_integer (SCHEME_A_ tr));
3448 } 3400 }
3449 3401
3450#endif 3402#endif
3451 3403
3452 case OP_APPLY: /* apply 'code' to 'args' */ 3404 case OP_APPLY: /* apply 'code' to 'args' */
3453#if USE_TRACING 3405#if USE_TRACING
3454 if (SCHEME_V->tracing) 3406 if (SCHEME_V->tracing)
3455 { 3407 {
3456 s_save (SCHEME_A_ OP_REAL_APPLY, SCHEME_V->args, SCHEME_V->code); 3408 s_save (SCHEME_A_ OP_REAL_APPLY, args, SCHEME_V->code);
3457 SCHEME_V->print_flag = 1; 3409 SCHEME_V->print_flag = 1;
3458 /* SCHEME_V->args=cons(SCHEME_V->code,SCHEME_V->args); */ 3410 /* args=cons(SCHEME_V->code,args); */
3459 putstr (SCHEME_A_ "\nApply to: "); 3411 putstr (SCHEME_A_ "\nApply to: ");
3460 s_goto (OP_P0LIST); 3412 s_goto (OP_P0LIST);
3461 } 3413 }
3462 3414
3463 /* fall through */ 3415 /* fall through */
3464 3416
3465 case OP_REAL_APPLY: 3417 case OP_REAL_APPLY:
3466#endif 3418#endif
3467 if (is_proc (SCHEME_V->code)) 3419 if (is_proc (SCHEME_V->code))
3468 {
3469 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ 3420 s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */
3470 }
3471 else if (is_foreign (SCHEME_V->code)) 3421 else if (is_foreign (SCHEME_V->code))
3472 { 3422 {
3473 /* Keep nested calls from GC'ing the arglist */ 3423 /* Keep nested calls from GC'ing the arglist */
3474 push_recent_alloc (SCHEME_A_ SCHEME_V->args, NIL); 3424 push_recent_alloc (SCHEME_A_ args, NIL);
3475 x = SCHEME_V->code->object.ff (SCHEME_A_ SCHEME_V->args); 3425 x = SCHEME_V->code->object.ff (SCHEME_A_ args);
3476 3426
3477 s_return (x); 3427 s_return (x);
3478 } 3428 }
3479 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */ 3429 else if (is_closure (SCHEME_V->code) || is_macro (SCHEME_V->code) || is_promise (SCHEME_V->code)) /* CLOSURE */
3480 { 3430 {
3481 /* Should not accept promise */ 3431 /* Should not accept promise */
3482 /* make environment */ 3432 /* make environment */
3483 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); 3433 new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code));
3484 3434
3485 for (x = car (closure_code (SCHEME_V->code)), y = SCHEME_V->args; is_pair (x); x = cdr (x), y = cdr (y)) 3435 for (x = car (closure_code (SCHEME_V->code)), y = args; is_pair (x); x = cdr (x), y = cdr (y))
3486 { 3436 {
3487 if (y == NIL) 3437 if (y == NIL)
3488 Error_0 ("not enough arguments"); 3438 Error_0 ("not enough arguments");
3489 else 3439 else
3490 new_slot_in_env (SCHEME_A_ car (x), car (y)); 3440 new_slot_in_env (SCHEME_A_ car (x), car (y));
3508 s_goto (OP_BEGIN); 3458 s_goto (OP_BEGIN);
3509 } 3459 }
3510 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ 3460 else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */
3511 { 3461 {
3512 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); 3462 ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code));
3513 s_return (SCHEME_V->args != NIL ? car (SCHEME_V->args) : NIL); 3463 s_return (args != NIL ? car (args) : NIL);
3514 } 3464 }
3515 else 3465 else
3516 Error_0 ("illegal function"); 3466 Error_0 ("illegal function");
3517 3467
3518 case OP_DOMACRO: /* do macro */ 3468 case OP_DOMACRO: /* do macro */
3527 { 3477 {
3528 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1); 3478 pointer f = find_slot_in_env (SCHEME_A_ SCHEME_V->envir, SCHEME_V->COMPILE_HOOK, 1);
3529 3479
3530 if (f != NIL) 3480 if (f != NIL)
3531 { 3481 {
3532 s_save (SCHEME_A_ OP_LAMBDA1, SCHEME_V->args, SCHEME_V->code); 3482 s_save (SCHEME_A_ OP_LAMBDA1, args, SCHEME_V->code);
3533 SCHEME_V->args = cons (SCHEME_V->code, NIL); 3483 SCHEME_V->args = cons (SCHEME_V->code, NIL);
3534 SCHEME_V->code = slot_value_in_env (f); 3484 SCHEME_V->code = slot_value_in_env (f);
3535 s_goto (OP_APPLY); 3485 s_goto (OP_APPLY);
3536 } 3486 }
3537 3487
3548 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir)); 3498 s_return (mk_closure (SCHEME_A_ SCHEME_V->code, SCHEME_V->envir));
3549 3499
3550#endif 3500#endif
3551 3501
3552 case OP_MKCLOSURE: /* make-closure */ 3502 case OP_MKCLOSURE: /* make-closure */
3553 x = car (SCHEME_V->args); 3503 x = car (args);
3554 3504
3555 if (car (x) == SCHEME_V->LAMBDA) 3505 if (car (x) == SCHEME_V->LAMBDA)
3556 x = cdr (x); 3506 x = cdr (x);
3557 3507
3558 if (cdr (SCHEME_V->args) == NIL) 3508 if (cdr (args) == NIL)
3559 y = SCHEME_V->envir; 3509 y = SCHEME_V->envir;
3560 else 3510 else
3561 y = cadr (SCHEME_V->args); 3511 y = cadr (args);
3562 3512
3563 s_return (mk_closure (SCHEME_A_ x, y)); 3513 s_return (mk_closure (SCHEME_A_ x, y));
3564 3514
3565 case OP_QUOTE: /* quote */ 3515 case OP_QUOTE: /* quote */
3566 s_return (car (SCHEME_V->code)); 3516 s_return (car (SCHEME_V->code));
3598 3548
3599 3549
3600 case OP_DEFP: /* defined? */ 3550 case OP_DEFP: /* defined? */
3601 x = SCHEME_V->envir; 3551 x = SCHEME_V->envir;
3602 3552
3603 if (cdr (SCHEME_V->args) != NIL) 3553 if (cdr (args) != NIL)
3604 x = cadr (SCHEME_V->args); 3554 x = cadr (args);
3605 3555
3606 s_retbool (find_slot_in_env (SCHEME_A_ x, car (SCHEME_V->args), 1) != NIL); 3556 s_retbool (find_slot_in_env (SCHEME_A_ x, car (args), 1) != NIL);
3607 3557
3608 case OP_SET0: /* set! */ 3558 case OP_SET0: /* set! */
3609 if (is_immutable (car (SCHEME_V->code))) 3559 if (is_immutable (car (SCHEME_V->code)))
3610 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code)); 3560 Error_1 ("set!: unable to alter immutable variable", car (SCHEME_V->code));
3611 3561
3642 3592
3643 case OP_IF1: /* if */ 3593 case OP_IF1: /* if */
3644 if (is_true (SCHEME_V->value)) 3594 if (is_true (SCHEME_V->value))
3645 SCHEME_V->code = car (SCHEME_V->code); 3595 SCHEME_V->code = car (SCHEME_V->code);
3646 else 3596 else
3647 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because 3597 SCHEME_V->code = cadr (SCHEME_V->code); /* (if #f 1) ==> () because * car(NIL) = NIL */
3648
3649 * car(NIL) = NIL */
3650 s_goto (OP_EVAL); 3598 s_goto (OP_EVAL);
3651 3599
3652 case OP_LET0: /* let */ 3600 case OP_LET0: /* let */
3653 SCHEME_V->args = NIL; 3601 SCHEME_V->args = NIL;
3654 SCHEME_V->value = SCHEME_V->code; 3602 SCHEME_V->value = SCHEME_V->code;
3655 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code); 3603 SCHEME_V->code = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code);
3656 s_goto (OP_LET1); 3604 s_goto (OP_LET1);
3657 3605
3658 case OP_LET1: /* let (calculate parameters) */ 3606 case OP_LET1: /* let (calculate parameters) */
3659 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3607 args = cons (SCHEME_V->value, args);
3660 3608
3661 if (is_pair (SCHEME_V->code)) /* continue */ 3609 if (is_pair (SCHEME_V->code)) /* continue */
3662 { 3610 {
3663 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3611 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3664 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code)); 3612 Error_1 ("Bad syntax of binding spec in let :", car (SCHEME_V->code));
3665 3613
3666 s_save (SCHEME_A_ OP_LET1, SCHEME_V->args, cdr (SCHEME_V->code)); 3614 s_save (SCHEME_A_ OP_LET1, args, cdr (SCHEME_V->code));
3667 SCHEME_V->code = cadar (SCHEME_V->code); 3615 SCHEME_V->code = cadar (SCHEME_V->code);
3668 SCHEME_V->args = NIL; 3616 SCHEME_V->args = NIL;
3669 s_goto (OP_EVAL); 3617 s_goto (OP_EVAL);
3670 } 3618 }
3671 else /* end */ 3619 else /* end */
3672 { 3620 {
3673 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3621 args = reverse_in_place (SCHEME_A_ NIL, args);
3674 SCHEME_V->code = car (SCHEME_V->args); 3622 SCHEME_V->code = car (args);
3675 SCHEME_V->args = cdr (SCHEME_V->args); 3623 SCHEME_V->args = cdr (args);
3676 s_goto (OP_LET2); 3624 s_goto (OP_LET2);
3677 } 3625 }
3678 3626
3679 case OP_LET2: /* let */ 3627 case OP_LET2: /* let */
3680 new_frame_in_env (SCHEME_A_ SCHEME_V->envir); 3628 new_frame_in_env (SCHEME_A_ SCHEME_V->envir);
3681 3629
3682 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = SCHEME_V->args; 3630 for (x = is_symbol (car (SCHEME_V->code)) ? cadr (SCHEME_V->code) : car (SCHEME_V->code), y = args;
3683 y != NIL; x = cdr (x), y = cdr (y)) 3631 y != NIL; x = cdr (x), y = cdr (y))
3684 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3632 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3685 3633
3686 if (is_symbol (car (SCHEME_V->code))) /* named let */ 3634 if (is_symbol (car (SCHEME_V->code))) /* named let */
3687 { 3635 {
3688 for (x = cadr (SCHEME_V->code), SCHEME_V->args = NIL; x != NIL; x = cdr (x)) 3636 for (x = cadr (SCHEME_V->code), args = NIL; x != NIL; x = cdr (x))
3689 { 3637 {
3690 if (!is_pair (x)) 3638 if (!is_pair (x))
3691 Error_1 ("Bad syntax of binding in let :", x); 3639 Error_1 ("Bad syntax of binding in let :", x);
3692 3640
3693 if (!is_list (SCHEME_A_ car (x))) 3641 if (!is_list (SCHEME_A_ car (x)))
3694 Error_1 ("Bad syntax of binding in let :", car (x)); 3642 Error_1 ("Bad syntax of binding in let :", car (x));
3695 3643
3696 SCHEME_V->args = cons (caar (x), SCHEME_V->args); 3644 args = cons (caar (x), args);
3697 } 3645 }
3698 3646
3699 x =
3700 mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args), cddr (SCHEME_V->code)), 3647 x = mk_closure (SCHEME_A_ cons (reverse_in_place (SCHEME_A_ NIL, args), cddr (SCHEME_V->code)),
3701 SCHEME_V->envir); 3648 SCHEME_V->envir);
3702 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); 3649 new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x);
3703 SCHEME_V->code = cddr (SCHEME_V->code); 3650 SCHEME_V->code = cddr (SCHEME_V->code);
3704 SCHEME_V->args = NIL;
3705 } 3651 }
3706 else 3652 else
3707 { 3653 {
3708 SCHEME_V->code = cdr (SCHEME_V->code); 3654 SCHEME_V->code = cdr (SCHEME_V->code);
3655 }
3656
3709 SCHEME_V->args = NIL; 3657 SCHEME_V->args = NIL;
3710 }
3711
3712 s_goto (OP_BEGIN); 3658 s_goto (OP_BEGIN);
3713 3659
3714 case OP_LET0AST: /* let* */ 3660 case OP_LET0AST: /* let* */
3715 if (car (SCHEME_V->code) == NIL) 3661 if (car (SCHEME_V->code) == NIL)
3716 { 3662 {
3734 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value); 3680 new_slot_in_env (SCHEME_A_ caar (SCHEME_V->code), SCHEME_V->value);
3735 SCHEME_V->code = cdr (SCHEME_V->code); 3681 SCHEME_V->code = cdr (SCHEME_V->code);
3736 3682
3737 if (is_pair (SCHEME_V->code)) /* continue */ 3683 if (is_pair (SCHEME_V->code)) /* continue */
3738 { 3684 {
3739 s_save (SCHEME_A_ OP_LET2AST, SCHEME_V->args, SCHEME_V->code); 3685 s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code);
3740 SCHEME_V->code = cadar (SCHEME_V->code); 3686 SCHEME_V->code = cadar (SCHEME_V->code);
3741 SCHEME_V->args = NIL; 3687 SCHEME_V->args = NIL;
3742 s_goto (OP_EVAL); 3688 s_goto (OP_EVAL);
3743 } 3689 }
3744 else /* end */ 3690 else /* end */
3745 { 3691 {
3746 SCHEME_V->code = SCHEME_V->args; 3692 SCHEME_V->code = args;
3747 SCHEME_V->args = NIL; 3693 SCHEME_V->args = NIL;
3748 s_goto (OP_BEGIN); 3694 s_goto (OP_BEGIN);
3749 } 3695 }
3750 3696
3751 case OP_LET0REC: /* letrec */ 3697 case OP_LET0REC: /* letrec */
3754 SCHEME_V->value = SCHEME_V->code; 3700 SCHEME_V->value = SCHEME_V->code;
3755 SCHEME_V->code = car (SCHEME_V->code); 3701 SCHEME_V->code = car (SCHEME_V->code);
3756 s_goto (OP_LET1REC); 3702 s_goto (OP_LET1REC);
3757 3703
3758 case OP_LET1REC: /* letrec (calculate parameters) */ 3704 case OP_LET1REC: /* letrec (calculate parameters) */
3759 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 3705 args = cons (SCHEME_V->value, args);
3760 3706
3761 if (is_pair (SCHEME_V->code)) /* continue */ 3707 if (is_pair (SCHEME_V->code)) /* continue */
3762 { 3708 {
3763 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code))) 3709 if (!is_pair (car (SCHEME_V->code)) || !is_pair (cdar (SCHEME_V->code)))
3764 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code)); 3710 Error_1 ("Bad syntax of binding spec in letrec :", car (SCHEME_V->code));
3765 3711
3766 s_save (SCHEME_A_ OP_LET1REC, SCHEME_V->args, cdr (SCHEME_V->code)); 3712 s_save (SCHEME_A_ OP_LET1REC, args, cdr (SCHEME_V->code));
3767 SCHEME_V->code = cadar (SCHEME_V->code); 3713 SCHEME_V->code = cadar (SCHEME_V->code);
3768 SCHEME_V->args = NIL; 3714 SCHEME_V->args = NIL;
3769 s_goto (OP_EVAL); 3715 s_goto (OP_EVAL);
3770 } 3716 }
3771 else /* end */ 3717 else /* end */
3772 { 3718 {
3773 SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); 3719 args = reverse_in_place (SCHEME_A_ NIL, args);
3774 SCHEME_V->code = car (SCHEME_V->args); 3720 SCHEME_V->code = car (args);
3775 SCHEME_V->args = cdr (SCHEME_V->args); 3721 SCHEME_V->args = cdr (args);
3776 s_goto (OP_LET2REC); 3722 s_goto (OP_LET2REC);
3777 } 3723 }
3778 3724
3779 case OP_LET2REC: /* letrec */ 3725 case OP_LET2REC: /* letrec */
3780 for (x = car (SCHEME_V->code), y = SCHEME_V->args; y != NIL; x = cdr (x), y = cdr (y)) 3726 for (x = car (SCHEME_V->code), y = args; y != NIL; x = cdr (x), y = cdr (y))
3781 new_slot_in_env (SCHEME_A_ caar (x), car (y)); 3727 new_slot_in_env (SCHEME_A_ caar (x), car (y));
3782 3728
3783 SCHEME_V->code = cdr (SCHEME_V->code); 3729 SCHEME_V->code = cdr (SCHEME_V->code);
3784 SCHEME_V->args = NIL; 3730 SCHEME_V->args = NIL;
3785 s_goto (OP_BEGIN); 3731 s_goto (OP_BEGIN);
3871 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); 3817 s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code));
3872 SCHEME_V->code = car (SCHEME_V->code); 3818 SCHEME_V->code = car (SCHEME_V->code);
3873 s_goto (OP_EVAL); 3819 s_goto (OP_EVAL);
3874 3820
3875 case OP_C1STREAM: /* cons-stream */ 3821 case OP_C1STREAM: /* cons-stream */
3876 SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register SCHEME_V->args for gc */ 3822 SCHEME_V->args = SCHEME_V->value; /* save SCHEME_V->value to register args for gc */
3877 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir); 3823 x = mk_closure (SCHEME_A_ cons (NIL, SCHEME_V->code), SCHEME_V->envir);
3878 set_typeflag (x, T_PROMISE); 3824 set_typeflag (x, T_PROMISE);
3879 s_return (cons (SCHEME_V->args, x)); 3825 s_return (cons (args, x));
3880 3826
3881 case OP_MACRO0: /* macro */ 3827 case OP_MACRO0: /* macro */
3882 if (is_pair (car (SCHEME_V->code))) 3828 if (is_pair (car (SCHEME_V->code)))
3883 { 3829 {
3884 x = caar (SCHEME_V->code); 3830 x = caar (SCHEME_V->code);
3917 { 3863 {
3918 if (!is_pair (y = caar (x))) 3864 if (!is_pair (y = caar (x)))
3919 break; 3865 break;
3920 3866
3921 for (; y != NIL; y = cdr (y)) 3867 for (; y != NIL; y = cdr (y))
3922 {
3923 if (eqv (car (y), SCHEME_V->value)) 3868 if (eqv (car (y), SCHEME_V->value))
3924 break; 3869 break;
3925 }
3926 3870
3927 if (y != NIL) 3871 if (y != NIL)
3928 break; 3872 break;
3929 } 3873 }
3930 3874
3950 s_goto (OP_BEGIN); 3894 s_goto (OP_BEGIN);
3951 else 3895 else
3952 s_return (NIL); 3896 s_return (NIL);
3953 3897
3954 case OP_PAPPLY: /* apply */ 3898 case OP_PAPPLY: /* apply */
3955 SCHEME_V->code = car (SCHEME_V->args); 3899 SCHEME_V->code = car (args);
3956 SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); 3900 SCHEME_V->args = list_star (SCHEME_A_ cdr (args));
3957 /*SCHEME_V->args = cadr(SCHEME_V->args); */ 3901 /*SCHEME_V->args = cadr(args); */
3958 s_goto (OP_APPLY); 3902 s_goto (OP_APPLY);
3959 3903
3960 case OP_PEVAL: /* eval */ 3904 case OP_PEVAL: /* eval */
3961 if (cdr (SCHEME_V->args) != NIL) 3905 if (cdr (args) != NIL)
3962 SCHEME_V->envir = cadr (SCHEME_V->args); 3906 SCHEME_V->envir = cadr (args);
3963 3907
3964 SCHEME_V->code = car (SCHEME_V->args); 3908 SCHEME_V->code = car (args);
3965 s_goto (OP_EVAL); 3909 s_goto (OP_EVAL);
3966 3910
3967 case OP_CONTINUATION: /* call-with-current-continuation */ 3911 case OP_CONTINUATION: /* call-with-current-continuation */
3968 SCHEME_V->code = car (SCHEME_V->args); 3912 SCHEME_V->code = car (args);
3969 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL); 3913 SCHEME_V->args = cons (mk_continuation (SCHEME_A_ ss_get_cont (SCHEME_A)), NIL);
3970 s_goto (OP_APPLY); 3914 s_goto (OP_APPLY);
3971 } 3915 }
3972 3916
3973 return S_T; 3917 abort ();
3974} 3918}
3975 3919
3976static pointer 3920static int
3977opexe_2 (SCHEME_P_ enum scheme_opcodes op) 3921opexe_1 (SCHEME_P_ enum scheme_opcodes op)
3978{ 3922{
3979 pointer x; 3923 pointer args = SCHEME_V->args;
3924 pointer x = car (args);
3980 num v; 3925 num v;
3981 3926
3982#if USE_MATH 3927#if USE_MATH
3983 RVALUE dd; 3928 RVALUE dd;
3984#endif 3929#endif
3985 3930
3986 switch (op) 3931 switch (op)
3987 { 3932 {
3988#if USE_MATH 3933#if USE_MATH
3989
3990 case OP_INEX2EX: /* inexact->exact */ 3934 case OP_INEX2EX: /* inexact->exact */
3991 x = car (SCHEME_V->args);
3992
3993 if (num_is_integer (x)) 3935 if (num_is_integer (x))
3994 s_return (x); 3936 s_return (x);
3995 else if (modf (rvalue_unchecked (x), &dd) == 0.0) 3937 else if (modf (rvalue_unchecked (x), &dd) == 0)
3996 s_return (mk_integer (SCHEME_A_ ivalue (x))); 3938 s_return (mk_integer (SCHEME_A_ ivalue (x)));
3997 else 3939 else
3998 Error_1 ("inexact->exact: not integral:", x); 3940 Error_1 ("inexact->exact: not integral:", x);
3999 3941
4000 case OP_EXP:
4001 x = car (SCHEME_V->args);
4002 s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); 3942 case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x))));
4003
4004 case OP_LOG:
4005 x = car (SCHEME_V->args);
4006 s_return (mk_real (SCHEME_A_ log (rvalue (x)))); 3943 case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x))));
4007
4008 case OP_SIN:
4009 x = car (SCHEME_V->args);
4010 s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); 3944 case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x))));
4011
4012 case OP_COS:
4013 x = car (SCHEME_V->args);
4014 s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); 3945 case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x))));
4015
4016 case OP_TAN:
4017 x = car (SCHEME_V->args);
4018 s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); 3946 case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x))));
4019
4020 case OP_ASIN:
4021 x = car (SCHEME_V->args);
4022 s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); 3947 case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x))));
4023
4024 case OP_ACOS:
4025 x = car (SCHEME_V->args);
4026 s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); 3948 case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x))));
4027 3949
4028 case OP_ATAN: 3950 case OP_ATAN:
4029 x = car (SCHEME_V->args);
4030
4031 if (cdr (SCHEME_V->args) == NIL) 3951 if (cdr (args) == NIL)
4032 s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); 3952 s_return (mk_real (SCHEME_A_ atan (rvalue (x))));
4033 else 3953 else
4034 { 3954 {
4035 pointer y = cadr (SCHEME_V->args); 3955 pointer y = cadr (args);
4036
4037 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); 3956 s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y))));
4038 } 3957 }
4039 3958
4040 case OP_SQRT: 3959 case OP_SQRT:
4041 x = car (SCHEME_V->args);
4042 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); 3960 s_return (mk_real (SCHEME_A_ sqrt (rvalue (x))));
4043 3961
4044 case OP_EXPT: 3962 case OP_EXPT:
4045 { 3963 {
4046 RVALUE result; 3964 RVALUE result;
4047 int real_result = 1; 3965 int real_result = 1;
4048 pointer y = cadr (SCHEME_V->args); 3966 pointer y = cadr (args);
4049
4050 x = car (SCHEME_V->args);
4051 3967
4052 if (num_is_integer (x) && num_is_integer (y)) 3968 if (num_is_integer (x) && num_is_integer (y))
4053 real_result = 0; 3969 real_result = 0;
4054 3970
4055 /* This 'if' is an R5RS compatibility fix. */ 3971 /* This 'if' is an R5RS compatibility fix. */
4056 /* NOTE: Remove this 'if' fix for R6RS. */ 3972 /* NOTE: Remove this 'if' fix for R6RS. */
4057 if (rvalue (x) == 0 && rvalue (y) < 0) 3973 if (rvalue (x) == 0 && rvalue (y) < 0)
4058 result = 0.0; 3974 result = 0;
4059 else 3975 else
4060 result = pow (rvalue (x), rvalue (y)); 3976 result = pow (rvalue (x), rvalue (y));
4061 3977
4062 /* Before returning integer result make sure we can. */ 3978 /* Before returning integer result make sure we can. */
4063 /* If the test fails, result is too big for integer. */ 3979 /* If the test fails, result is too big for integer. */
4064 if (!real_result) 3980 if (!real_result)
4065 { 3981 {
4066 long result_as_long = (long) result; 3982 long result_as_long = result;
4067 3983
4068 if (result != (RVALUE) result_as_long) 3984 if (result != (RVALUE) result_as_long)
4069 real_result = 1; 3985 real_result = 1;
4070 } 3986 }
4071 3987
4073 s_return (mk_real (SCHEME_A_ result)); 3989 s_return (mk_real (SCHEME_A_ result));
4074 else 3990 else
4075 s_return (mk_integer (SCHEME_A_ result)); 3991 s_return (mk_integer (SCHEME_A_ result));
4076 } 3992 }
4077 3993
4078 case OP_FLOOR:
4079 x = car (SCHEME_V->args);
4080 s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); 3994 case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x))));
4081
4082 case OP_CEILING:
4083 x = car (SCHEME_V->args);
4084 s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); 3995 case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x))));
4085 3996
4086 case OP_TRUNCATE: 3997 case OP_TRUNCATE:
4087 { 3998 {
4088 RVALUE rvalue_of_x; 3999 RVALUE rvalue_of_x;
4089 4000
4090 x = car (SCHEME_V->args);
4091 rvalue_of_x = rvalue (x); 4001 rvalue_of_x = rvalue (x);
4092 4002
4093 if (rvalue_of_x > 0) 4003 if (rvalue_of_x > 0)
4094 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); 4004 s_return (mk_real (SCHEME_A_ floor (rvalue_of_x)));
4095 else 4005 else
4096 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); 4006 s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x)));
4097 } 4007 }
4098 4008
4099 case OP_ROUND: 4009 case OP_ROUND:
4100 x = car (SCHEME_V->args);
4101
4102 if (num_is_integer (x)) 4010 if (num_is_integer (x))
4103 s_return (x); 4011 s_return (x);
4104 4012
4105 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); 4013 s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x))));
4106#endif 4014#endif
4107 4015
4108 case OP_ADD: /* + */ 4016 case OP_ADD: /* + */
4109 v = num_zero; 4017 v = num_zero;
4110 4018
4111 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4019 for (x = args; x != NIL; x = cdr (x))
4112 v = num_add (v, nvalue (car (x))); 4020 v = num_op ('+', v, nvalue (car (x)));
4113 4021
4114 s_return (mk_number (SCHEME_A_ v)); 4022 s_return (mk_number (SCHEME_A_ v));
4115 4023
4116 case OP_MUL: /* * */ 4024 case OP_MUL: /* * */
4117 v = num_one; 4025 v = num_one;
4118 4026
4119 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4027 for (x = args; x != NIL; x = cdr (x))
4120 v = num_mul (v, nvalue (car (x))); 4028 v = num_op ('+', v, nvalue (car (x)));
4121 4029
4122 s_return (mk_number (SCHEME_A_ v)); 4030 s_return (mk_number (SCHEME_A_ v));
4123 4031
4124 case OP_SUB: /* - */ 4032 case OP_SUB: /* - */
4125 if (cdr (SCHEME_V->args) == NIL) 4033 if (cdr (args) == NIL)
4126 { 4034 {
4127 x = SCHEME_V->args; 4035 x = args;
4128 v = num_zero; 4036 v = num_zero;
4129 } 4037 }
4130 else 4038 else
4131 { 4039 {
4132 x = cdr (SCHEME_V->args); 4040 x = cdr (args);
4133 v = nvalue (car (SCHEME_V->args)); 4041 v = nvalue (car (args));
4134 } 4042 }
4135 4043
4136 for (; x != NIL; x = cdr (x)) 4044 for (; x != NIL; x = cdr (x))
4137 v = num_sub (v, nvalue (car (x))); 4045 v = num_op ('+', v, nvalue (car (x)));
4138 4046
4139 s_return (mk_number (SCHEME_A_ v)); 4047 s_return (mk_number (SCHEME_A_ v));
4140 4048
4141 case OP_DIV: /* / */ 4049 case OP_DIV: /* / */
4142 if (cdr (SCHEME_V->args) == NIL) 4050 if (cdr (args) == NIL)
4143 { 4051 {
4144 x = SCHEME_V->args; 4052 x = args;
4145 v = num_one; 4053 v = num_one;
4146 } 4054 }
4147 else 4055 else
4148 { 4056 {
4149 x = cdr (SCHEME_V->args); 4057 x = cdr (args);
4150 v = nvalue (car (SCHEME_V->args)); 4058 v = nvalue (car (args));
4151 } 4059 }
4152 4060
4153 for (; x != NIL; x = cdr (x)) 4061 for (; x != NIL; x = cdr (x))
4154 { 4062 {
4155 if (!is_zero_rvalue (rvalue (car (x)))) 4063 if (!is_zero_rvalue (rvalue (car (x))))
4159 } 4067 }
4160 4068
4161 s_return (mk_number (SCHEME_A_ v)); 4069 s_return (mk_number (SCHEME_A_ v));
4162 4070
4163 case OP_INTDIV: /* quotient */ 4071 case OP_INTDIV: /* quotient */
4164 if (cdr (SCHEME_V->args) == NIL) 4072 if (cdr (args) == NIL)
4165 { 4073 {
4166 x = SCHEME_V->args; 4074 x = args;
4167 v = num_one; 4075 v = num_one;
4168 } 4076 }
4169 else 4077 else
4170 { 4078 {
4171 x = cdr (SCHEME_V->args); 4079 x = cdr (args);
4172 v = nvalue (car (SCHEME_V->args)); 4080 v = nvalue (car (args));
4173 } 4081 }
4174 4082
4175 for (; x != NIL; x = cdr (x)) 4083 for (; x != NIL; x = cdr (x))
4176 { 4084 {
4177 if (ivalue (car (x)) != 0) 4085 if (ivalue (car (x)) != 0)
4178 v = num_intdiv (v, nvalue (car (x))); 4086 v = num_op ('/', v, nvalue (car (x)));
4179 else 4087 else
4180 Error_0 ("quotient: division by zero"); 4088 Error_0 ("quotient: division by zero");
4181 } 4089 }
4182 4090
4183 s_return (mk_number (SCHEME_A_ v)); 4091 s_return (mk_number (SCHEME_A_ v));
4184 4092
4185 case OP_REM: /* remainder */ 4093 case OP_REM: /* remainder */
4186 v = nvalue (car (SCHEME_V->args)); 4094 v = nvalue (x);
4187 4095
4188 if (ivalue (cadr (SCHEME_V->args)) != 0) 4096 if (ivalue (cadr (args)) != 0)
4189 v = num_rem (v, nvalue (cadr (SCHEME_V->args))); 4097 v = num_rem (v, nvalue (cadr (args)));
4190 else 4098 else
4191 Error_0 ("remainder: division by zero"); 4099 Error_0 ("remainder: division by zero");
4192 4100
4193 s_return (mk_number (SCHEME_A_ v)); 4101 s_return (mk_number (SCHEME_A_ v));
4194 4102
4195 case OP_MOD: /* modulo */ 4103 case OP_MOD: /* modulo */
4196 v = nvalue (car (SCHEME_V->args)); 4104 v = nvalue (x);
4197 4105
4198 if (ivalue (cadr (SCHEME_V->args)) != 0) 4106 if (ivalue (cadr (args)) != 0)
4199 v = num_mod (v, nvalue (cadr (SCHEME_V->args))); 4107 v = num_mod (v, nvalue (cadr (args)));
4200 else 4108 else
4201 Error_0 ("modulo: division by zero"); 4109 Error_0 ("modulo: division by zero");
4202 4110
4203 s_return (mk_number (SCHEME_A_ v)); 4111 s_return (mk_number (SCHEME_A_ v));
4204 4112
4205 case OP_CAR: /* car */ 4113 case OP_CAR: /* car */
4206 s_return (caar (SCHEME_V->args)); 4114 s_return (caar (args));
4207 4115
4208 case OP_CDR: /* cdr */ 4116 case OP_CDR: /* cdr */
4209 s_return (cdar (SCHEME_V->args)); 4117 s_return (cdar (args));
4210 4118
4211 case OP_CONS: /* cons */ 4119 case OP_CONS: /* cons */
4212 set_cdr (SCHEME_V->args, cadr (SCHEME_V->args)); 4120 set_cdr (args, cadr (args));
4213 s_return (SCHEME_V->args); 4121 s_return (args);
4214 4122
4215 case OP_SETCAR: /* set-car! */ 4123 case OP_SETCAR: /* set-car! */
4216 if (!is_immutable (car (SCHEME_V->args))) 4124 if (!is_immutable (x))
4217 { 4125 {
4218 set_car (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4126 set_car (x, cadr (args));
4219 s_return (car (SCHEME_V->args)); 4127 s_return (car (args));
4220 } 4128 }
4221 else 4129 else
4222 Error_0 ("set-car!: unable to alter immutable pair"); 4130 Error_0 ("set-car!: unable to alter immutable pair");
4223 4131
4224 case OP_SETCDR: /* set-cdr! */ 4132 case OP_SETCDR: /* set-cdr! */
4225 if (!is_immutable (car (SCHEME_V->args))) 4133 if (!is_immutable (x))
4226 { 4134 {
4227 set_cdr (car (SCHEME_V->args), cadr (SCHEME_V->args)); 4135 set_cdr (x, cadr (args));
4228 s_return (car (SCHEME_V->args)); 4136 s_return (car (args));
4229 } 4137 }
4230 else 4138 else
4231 Error_0 ("set-cdr!: unable to alter immutable pair"); 4139 Error_0 ("set-cdr!: unable to alter immutable pair");
4232 4140
4233 case OP_CHAR2INT: /* char->integer */ 4141 case OP_CHAR2INT: /* char->integer */
4234 s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4142 s_return (mk_integer (SCHEME_A_ ivalue (x)));
4235 4143
4236 case OP_INT2CHAR: /* integer->char */ 4144 case OP_INT2CHAR: /* integer->char */
4237 s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args)))); 4145 s_return (mk_character (SCHEME_A_ ivalue (x)));
4238 4146
4239 case OP_CHARUPCASE: 4147 case OP_CHARUPCASE:
4240 { 4148 {
4241 unsigned char c = ivalue (car (SCHEME_V->args)); 4149 unsigned char c = ivalue (x);
4242 c = toupper (c); 4150 c = toupper (c);
4243 s_return (mk_character (SCHEME_A_ c)); 4151 s_return (mk_character (SCHEME_A_ c));
4244 } 4152 }
4245 4153
4246 case OP_CHARDNCASE: 4154 case OP_CHARDNCASE:
4247 { 4155 {
4248 unsigned char c = ivalue (car (SCHEME_V->args)); 4156 unsigned char c = ivalue (x);
4249 c = tolower (c); 4157 c = tolower (c);
4250 s_return (mk_character (SCHEME_A_ c)); 4158 s_return (mk_character (SCHEME_A_ c));
4251 } 4159 }
4252 4160
4253 case OP_STR2SYM: /* string->symbol */ 4161 case OP_STR2SYM: /* string->symbol */
4254 s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); 4162 s_return (mk_symbol (SCHEME_A_ strvalue (x)));
4255 4163
4256 case OP_STR2ATOM: /* string->atom */ 4164 case OP_STR2ATOM: /* string->atom */
4257 { 4165 {
4258 char *s = strvalue (car (SCHEME_V->args)); 4166 char *s = strvalue (x);
4259 long pf = 0; 4167 long pf = 0;
4260 4168
4261 if (cdr (SCHEME_V->args) != NIL) 4169 if (cdr (args) != NIL)
4262 { 4170 {
4263 /* we know cadr(SCHEME_V->args) is a natural number */ 4171 /* we know cadr(args) is a natural number */
4264 /* see if it is 2, 8, 10, or 16, or error */ 4172 /* see if it is 2, 8, 10, or 16, or error */
4265 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4173 pf = ivalue_unchecked (cadr (args));
4266 4174
4267 if (pf == 16 || pf == 10 || pf == 8 || pf == 2) 4175 if (pf == 16 || pf == 10 || pf == 8 || pf == 2)
4268 { 4176 {
4269 /* base is OK */ 4177 /* base is OK */
4270 } 4178 }
4271 else 4179 else
4272 pf = -1; 4180 pf = -1;
4273 } 4181 }
4274 4182
4275 if (pf < 0) 4183 if (pf < 0)
4276 Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); 4184 Error_1 ("string->atom: bad base:", cadr (args));
4277 else if (*s == '#') /* no use of base! */ 4185 else if (*s == '#') /* no use of base! */
4278 s_return (mk_sharp_const (SCHEME_A_ s + 1)); 4186 s_return (mk_sharp_const (SCHEME_A_ s + 1));
4279 else 4187 else
4280 { 4188 {
4281 if (pf == 0 || pf == 10) 4189 if (pf == 0 || pf == 10)
4292 } 4200 }
4293 } 4201 }
4294 } 4202 }
4295 4203
4296 case OP_SYM2STR: /* symbol->string */ 4204 case OP_SYM2STR: /* symbol->string */
4297 x = mk_string (SCHEME_A_ symname (car (SCHEME_V->args))); 4205 x = mk_string (SCHEME_A_ symname (x));
4298 setimmutable (x); 4206 setimmutable (x);
4299 s_return (x); 4207 s_return (x);
4300 4208
4301 case OP_ATOM2STR: /* atom->string */ 4209 case OP_ATOM2STR: /* atom->string */
4302 { 4210 {
4303 long pf = 0; 4211 long pf = 0;
4304 4212
4305 x = car (SCHEME_V->args);
4306
4307 if (cdr (SCHEME_V->args) != NIL) 4213 if (cdr (args) != NIL)
4308 { 4214 {
4309 /* we know cadr(SCHEME_V->args) is a natural number */ 4215 /* we know cadr(args) is a natural number */
4310 /* see if it is 2, 8, 10, or 16, or error */ 4216 /* see if it is 2, 8, 10, or 16, or error */
4311 pf = ivalue_unchecked (cadr (SCHEME_V->args)); 4217 pf = ivalue_unchecked (cadr (args));
4312 4218
4313 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2)) 4219 if (is_number (x) && (pf == 16 || pf == 10 || pf == 8 || pf == 2))
4314 { 4220 {
4315 /* base is OK */ 4221 /* base is OK */
4316 } 4222 }
4317 else 4223 else
4318 pf = -1; 4224 pf = -1;
4319 } 4225 }
4320 4226
4321 if (pf < 0) 4227 if (pf < 0)
4322 Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); 4228 Error_1 ("atom->string: bad base:", cadr (args));
4323 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x)) 4229 else if (is_number (x) || is_character (x) || is_string (x) || is_symbol (x))
4324 { 4230 {
4325 char *p; 4231 char *p;
4326 int len; 4232 int len;
4327 4233
4335 case OP_MKSTRING: /* make-string */ 4241 case OP_MKSTRING: /* make-string */
4336 { 4242 {
4337 int fill = ' '; 4243 int fill = ' ';
4338 int len; 4244 int len;
4339 4245
4340 len = ivalue (car (SCHEME_V->args)); 4246 len = ivalue (x);
4341 4247
4342 if (cdr (SCHEME_V->args) != NIL) 4248 if (cdr (args) != NIL)
4343 fill = charvalue (cadr (SCHEME_V->args)); 4249 fill = charvalue (cadr (args));
4344 4250
4345 s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); 4251 s_return (mk_empty_string (SCHEME_A_ len, fill));
4346 } 4252 }
4347 4253
4348 case OP_STRLEN: /* string-length */ 4254 case OP_STRLEN: /* string-length */
4349 s_return (mk_integer (SCHEME_A_ strlength (car (SCHEME_V->args)))); 4255 s_return (mk_integer (SCHEME_A_ strlength (x)));
4350 4256
4351 case OP_STRREF: /* string-ref */ 4257 case OP_STRREF: /* string-ref */
4352 { 4258 {
4353 char *str; 4259 char *str;
4354 int index; 4260 int index;
4355 4261
4356 str = strvalue (car (SCHEME_V->args)); 4262 str = strvalue (x);
4357 4263
4358 index = ivalue (cadr (SCHEME_V->args)); 4264 index = ivalue (cadr (args));
4359 4265
4360 if (index >= strlength (car (SCHEME_V->args))) 4266 if (index >= strlength (x))
4361 Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); 4267 Error_1 ("string-ref: out of bounds:", cadr (args));
4362 4268
4363 s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); 4269 s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index]));
4364 } 4270 }
4365 4271
4366 case OP_STRSET: /* string-set! */ 4272 case OP_STRSET: /* string-set! */
4367 { 4273 {
4368 char *str; 4274 char *str;
4369 int index; 4275 int index;
4370 int c; 4276 int c;
4371 4277
4372 if (is_immutable (car (SCHEME_V->args))) 4278 if (is_immutable (x))
4373 Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); 4279 Error_1 ("string-set!: unable to alter immutable string:", x);
4374 4280
4375 str = strvalue (car (SCHEME_V->args)); 4281 str = strvalue (x);
4376 4282
4377 index = ivalue (cadr (SCHEME_V->args)); 4283 index = ivalue (cadr (args));
4378 4284
4379 if (index >= strlength (car (SCHEME_V->args))) 4285 if (index >= strlength (x))
4380 Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); 4286 Error_1 ("string-set!: out of bounds:", cadr (args));
4381 4287
4382 c = charvalue (caddr (SCHEME_V->args)); 4288 c = charvalue (caddr (args));
4383 4289
4384 str[index] = (char) c; 4290 str[index] = c;
4385 s_return (car (SCHEME_V->args)); 4291 s_return (car (args));
4386 } 4292 }
4387 4293
4388 case OP_STRAPPEND: /* string-append */ 4294 case OP_STRAPPEND: /* string-append */
4389 { 4295 {
4390 /* in 1.29 string-append was in Scheme in init.scm but was too slow */ 4296 /* in 1.29 string-append was in Scheme in init.scm but was too slow */
4391 int len = 0; 4297 int len = 0;
4392 pointer newstr; 4298 pointer newstr;
4393 char *pos; 4299 char *pos;
4394 4300
4395 /* compute needed length for new string */ 4301 /* compute needed length for new string */
4396 for (x = SCHEME_V->args; x != NIL; x = cdr (x)) 4302 for (x = args; x != NIL; x = cdr (x))
4397 len += strlength (car (x)); 4303 len += strlength (car (x));
4398 4304
4399 newstr = mk_empty_string (SCHEME_A_ len, ' '); 4305 newstr = mk_empty_string (SCHEME_A_ len, ' ');
4400 4306
4401 /* store the contents of the argument strings into the new string */ 4307 /* store the contents of the argument strings into the new string */
4402 for (pos = strvalue (newstr), x = SCHEME_V->args; x != NIL; pos += strlength (car (x)), x = cdr (x)) 4308 for (pos = strvalue (newstr), x = args; x != NIL; pos += strlength (car (x)), x = cdr (x))
4403 memcpy (pos, strvalue (car (x)), strlength (car (x))); 4309 memcpy (pos, strvalue (car (x)), strlength (car (x)));
4404 4310
4405 s_return (newstr); 4311 s_return (newstr);
4406 } 4312 }
4407 4313
4410 char *str; 4316 char *str;
4411 int index0; 4317 int index0;
4412 int index1; 4318 int index1;
4413 int len; 4319 int len;
4414 4320
4415 str = strvalue (car (SCHEME_V->args)); 4321 str = strvalue (x);
4416 4322
4417 index0 = ivalue (cadr (SCHEME_V->args)); 4323 index0 = ivalue (cadr (args));
4418 4324
4419 if (index0 > strlength (car (SCHEME_V->args))) 4325 if (index0 > strlength (x))
4420 Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); 4326 Error_1 ("substring: start out of bounds:", cadr (args));
4421 4327
4422 if (cddr (SCHEME_V->args) != NIL) 4328 if (cddr (args) != NIL)
4423 { 4329 {
4424 index1 = ivalue (caddr (SCHEME_V->args)); 4330 index1 = ivalue (caddr (args));
4425 4331
4426 if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) 4332 if (index1 > strlength (x) || index1 < index0)
4427 Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); 4333 Error_1 ("substring: end out of bounds:", caddr (args));
4428 } 4334 }
4429 else 4335 else
4430 index1 = strlength (car (SCHEME_V->args)); 4336 index1 = strlength (x);
4431 4337
4432 len = index1 - index0; 4338 len = index1 - index0;
4433 x = mk_empty_string (SCHEME_A_ len, ' '); 4339 x = mk_empty_string (SCHEME_A_ len, ' ');
4434 memcpy (strvalue (x), str + index0, len); 4340 memcpy (strvalue (x), str + index0, len);
4435 strvalue (x)[len] = 0; 4341 strvalue (x)[len] = 0;
4439 4345
4440 case OP_VECTOR: /* vector */ 4346 case OP_VECTOR: /* vector */
4441 { 4347 {
4442 int i; 4348 int i;
4443 pointer vec; 4349 pointer vec;
4444 int len = list_length (SCHEME_A_ SCHEME_V->args); 4350 int len = list_length (SCHEME_A_ args);
4445 4351
4446 if (len < 0) 4352 if (len < 0)
4447 Error_1 ("vector: not a proper list:", SCHEME_V->args); 4353 Error_1 ("vector: not a proper list:", args);
4448 4354
4449 vec = mk_vector (SCHEME_A_ len); 4355 vec = mk_vector (SCHEME_A_ len);
4450 4356
4451#if USE_ERROR_CHECKING 4357#if USE_ERROR_CHECKING
4452 if (SCHEME_V->no_memory) 4358 if (SCHEME_V->no_memory)
4453 s_return (S_SINK); 4359 s_return (S_SINK);
4454#endif 4360#endif
4455 4361
4456 for (x = SCHEME_V->args, i = 0; is_pair (x); x = cdr (x), i++) 4362 for (x = args, i = 0; is_pair (x); x = cdr (x), i++)
4457 set_vector_elem (vec, i, car (x)); 4363 set_vector_elem (vec, i, car (x));
4458 4364
4459 s_return (vec); 4365 s_return (vec);
4460 } 4366 }
4461 4367
4463 { 4369 {
4464 pointer fill = NIL; 4370 pointer fill = NIL;
4465 int len; 4371 int len;
4466 pointer vec; 4372 pointer vec;
4467 4373
4468 len = ivalue (car (SCHEME_V->args)); 4374 len = ivalue (x);
4469 4375
4470 if (cdr (SCHEME_V->args) != NIL) 4376 if (cdr (args) != NIL)
4471 fill = cadr (SCHEME_V->args); 4377 fill = cadr (args);
4472 4378
4473 vec = mk_vector (SCHEME_A_ len); 4379 vec = mk_vector (SCHEME_A_ len);
4474 4380
4475#if USE_ERROR_CHECKING 4381#if USE_ERROR_CHECKING
4476 if (SCHEME_V->no_memory) 4382 if (SCHEME_V->no_memory)
4482 4388
4483 s_return (vec); 4389 s_return (vec);
4484 } 4390 }
4485 4391
4486 case OP_VECLEN: /* vector-length */ 4392 case OP_VECLEN: /* vector-length */
4487 s_return (mk_integer (SCHEME_A_ veclength (car (SCHEME_V->args)))); 4393 s_return (mk_integer (SCHEME_A_ veclength (x)));
4488 4394
4489 case OP_VECREF: /* vector-ref */ 4395 case OP_VECREF: /* vector-ref */
4490 { 4396 {
4491 int index; 4397 int index;
4492 4398
4493 index = ivalue (cadr (SCHEME_V->args)); 4399 index = ivalue (cadr (args));
4494 4400
4495 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4401 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4496 Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); 4402 Error_1 ("vector-ref: out of bounds:", cadr (args));
4497 4403
4498 s_return (vector_elem (car (SCHEME_V->args), index)); 4404 s_return (vector_elem (x, index));
4499 } 4405 }
4500 4406
4501 case OP_VECSET: /* vector-set! */ 4407 case OP_VECSET: /* vector-set! */
4502 { 4408 {
4503 int index; 4409 int index;
4504 4410
4505 if (is_immutable (car (SCHEME_V->args))) 4411 if (is_immutable (x))
4506 Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); 4412 Error_1 ("vector-set!: unable to alter immutable vector:", x);
4507 4413
4508 index = ivalue (cadr (SCHEME_V->args)); 4414 index = ivalue (cadr (args));
4509 4415
4510 if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) 4416 if (index >= veclength (car (args)) && USE_ERROR_CHECKING)
4511 Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); 4417 Error_1 ("vector-set!: out of bounds:", cadr (args));
4512 4418
4513 set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); 4419 set_vector_elem (x, index, caddr (args));
4514 s_return (car (SCHEME_V->args)); 4420 s_return (x);
4515 } 4421 }
4516 } 4422 }
4517 4423
4518 return S_T; 4424 abort ();
4519} 4425}
4520 4426
4521INTERFACE int 4427INTERFACE int
4522is_list (SCHEME_P_ pointer a) 4428is_list (SCHEME_P_ pointer a)
4523{ 4429{
4570 return -1; 4476 return -1;
4571 } 4477 }
4572 } 4478 }
4573} 4479}
4574 4480
4575static pointer 4481static int
4482opexe_2 (SCHEME_P_ enum scheme_opcodes op)
4483{
4484 pointer x = SCHEME_V->args;
4485
4486 for (;;)
4487 {
4488 num v = nvalue (car (x));
4489 x = cdr (x);
4490
4491 if (x == NIL)
4492 break;
4493
4494 int r = num_cmp (v, nvalue (car (x)));
4495
4496 switch (op)
4497 {
4498 case OP_NUMEQ: r = r == 0; break;
4499 case OP_LESS: r = r < 0; break;
4500 case OP_GRE: r = r > 0; break;
4501 case OP_LEQ: r = r <= 0; break;
4502 case OP_GEQ: r = r >= 0; break;
4503 }
4504
4505 if (!r)
4506 s_return (S_F);
4507 }
4508
4509 s_return (S_T);
4510}
4511
4512static int
4576opexe_3 (SCHEME_P_ enum scheme_opcodes op) 4513opexe_3 (SCHEME_P_ enum scheme_opcodes op)
4577{ 4514{
4578 pointer x; 4515 pointer args = SCHEME_V->args;
4579 num v; 4516 pointer a = car (args);
4580 int (*comp_func) (num, num); 4517 pointer d = cdr (args);
4518 int r;
4581 4519
4582 switch (op) 4520 switch (op)
4583 { 4521 {
4584 case OP_NOT: /* not */ 4522 case OP_NOT: /* not */ r = is_false (a) ; break;
4585 s_retbool (is_false (car (SCHEME_V->args))); 4523 case OP_BOOLP: /* boolean? */ r = a == S_F || a == S_T; break;
4524 case OP_EOFOBJP: /* eof-object? */ r = a == S_EOF ; break;
4525 case OP_NULLP: /* null? */ r = a == NIL ; break;
4526 case OP_SYMBOLP: /* symbol? */ r = is_symbol (a) ; break;
4527 case OP_NUMBERP: /* number? */ r = is_number (a) ; break;
4528 case OP_STRINGP: /* string? */ r = is_string (a) ; break;
4529 case OP_INTEGERP: /* integer? */ r = is_integer (a) ; break;
4530 case OP_REALP: /* real? */ r = is_number (a) ; break; /* all numbers are real */
4531 case OP_CHARP: /* char? */ r = is_character (a) ; break;
4586 4532
4587 case OP_BOOLP: /* boolean? */
4588 s_retbool (car (SCHEME_V->args) == S_F || car (SCHEME_V->args) == S_T);
4589
4590 case OP_EOFOBJP: /* boolean? */
4591 s_retbool (car (SCHEME_V->args) == S_EOF);
4592
4593 case OP_NULLP: /* null? */
4594 s_retbool (car (SCHEME_V->args) == NIL);
4595
4596 case OP_NUMEQ: /* = */
4597 case OP_LESS: /* < */
4598 case OP_GRE: /* > */
4599 case OP_LEQ: /* <= */
4600 case OP_GEQ: /* >= */
4601 switch (op)
4602 {
4603 case OP_NUMEQ:
4604 comp_func = num_eq;
4605 break;
4606
4607 case OP_LESS:
4608 comp_func = num_lt;
4609 break;
4610
4611 case OP_GRE:
4612 comp_func = num_gt;
4613 break;
4614
4615 case OP_LEQ:
4616 comp_func = num_le;
4617 break;
4618
4619 case OP_GEQ:
4620 comp_func = num_ge;
4621 break;
4622 }
4623
4624 x = SCHEME_V->args;
4625 v = nvalue (car (x));
4626 x = cdr (x);
4627
4628 for (; x != NIL; x = cdr (x))
4629 {
4630 if (!comp_func (v, nvalue (car (x))))
4631 s_retbool (0);
4632
4633 v = nvalue (car (x));
4634 }
4635
4636 s_retbool (1);
4637
4638 case OP_SYMBOLP: /* symbol? */
4639 s_retbool (is_symbol (car (SCHEME_V->args)));
4640
4641 case OP_NUMBERP: /* number? */
4642 s_retbool (is_number (car (SCHEME_V->args)));
4643
4644 case OP_STRINGP: /* string? */
4645 s_retbool (is_string (car (SCHEME_V->args)));
4646
4647 case OP_INTEGERP: /* integer? */
4648 s_retbool (is_integer (car (SCHEME_V->args)));
4649
4650 case OP_REALP: /* real? */
4651 s_retbool (is_number (car (SCHEME_V->args))); /* All numbers are real */
4652
4653 case OP_CHARP: /* char? */
4654 s_retbool (is_character (car (SCHEME_V->args)));
4655#if USE_CHAR_CLASSIFIERS 4533#if USE_CHAR_CLASSIFIERS
4656
4657 case OP_CHARAP: /* char-alphabetic? */ 4534 case OP_CHARAP: /* char-alphabetic? */ r = Cisalpha (ivalue (a)); break;
4658 s_retbool (Cisalpha (ivalue (car (SCHEME_V->args)))); 4535 case OP_CHARNP: /* char-numeric? */ r = Cisdigit (ivalue (a)); break;
4659
4660 case OP_CHARNP: /* char-numeric? */
4661 s_retbool (Cisdigit (ivalue (car (SCHEME_V->args))));
4662
4663 case OP_CHARWP: /* char-whitespace? */ 4536 case OP_CHARWP: /* char-whitespace? */ r = Cisspace (ivalue (a)); break;
4664 s_retbool (Cisspace (ivalue (car (SCHEME_V->args))));
4665
4666 case OP_CHARUP: /* char-upper-case? */ 4537 case OP_CHARUP: /* char-upper-case? */ r = Cisupper (ivalue (a)); break;
4667 s_retbool (Cisupper (ivalue (car (SCHEME_V->args))));
4668
4669 case OP_CHARLP: /* char-lower-case? */ 4538 case OP_CHARLP: /* char-lower-case? */ r = Cislower (ivalue (a)); break;
4670 s_retbool (Cislower (ivalue (car (SCHEME_V->args))));
4671#endif 4539#endif
4540
4672#if USE_PORTS 4541#if USE_PORTS
4673 4542 case OP_PORTP: /* port? */ r = is_port (a) ; break;
4674 case OP_PORTP: /* port? */
4675 s_retbool (is_port (car (SCHEME_V->args)));
4676
4677 case OP_INPORTP: /* input-port? */ 4543 case OP_INPORTP: /* input-port? */ r = is_inport (a) ; break;
4678 s_retbool (is_inport (car (SCHEME_V->args)));
4679
4680 case OP_OUTPORTP: /* output-port? */ 4544 case OP_OUTPORTP: /* output-port? */ r = is_outport (a); break;
4681 s_retbool (is_outport (car (SCHEME_V->args)));
4682#endif 4545#endif
4683 4546
4684 case OP_PROCP: /* procedure? */ 4547 case OP_PROCP: /* procedure? */
4685 4548
4686 /*-- 4549 /*--
4687 * continuation should be procedure by the example 4550 * continuation should be procedure by the example
4688 * (call-with-current-continuation procedure?) ==> #t 4551 * (call-with-current-continuation procedure?) ==> #t
4689 * in R^3 report sec. 6.9 4552 * in R^3 report sec. 6.9
4690 */ 4553 */
4691 s_retbool (is_proc (car (SCHEME_V->args)) || is_closure (car (SCHEME_V->args)) 4554 r = is_proc (a) || is_closure (a) || is_continuation (a) || is_foreign (a);
4692 || is_continuation (car (SCHEME_V->args)) || is_foreign (car (SCHEME_V->args))); 4555 break;
4693 4556
4694 case OP_PAIRP: /* pair? */ 4557 case OP_PAIRP: /* pair? */ r = is_pair (a) ; break;
4695 s_retbool (is_pair (car (SCHEME_V->args))); 4558 case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break;
4696 4559 case OP_ENVP: /* environment? */ r = is_environment (a) ; break;
4697 case OP_LISTP: /* list? */ 4560 case OP_VECTORP: /* vector? */ r = is_vector (a) ; break;
4698 s_retbool (list_length (SCHEME_A_ car (SCHEME_V->args)) >= 0); 4561 case OP_EQ: /* eq? */ r = a == cadr (args) ; break;
4699 4562 case OP_EQV: /* eqv? */ r = eqv (a, cadr (args)) ; break;
4700 case OP_ENVP: /* environment? */
4701 s_retbool (is_environment (car (SCHEME_V->args)));
4702
4703 case OP_VECTORP: /* vector? */
4704 s_retbool (is_vector (car (SCHEME_V->args)));
4705
4706 case OP_EQ: /* eq? */
4707 s_retbool (car (SCHEME_V->args) == cadr (SCHEME_V->args));
4708
4709 case OP_EQV: /* eqv? */
4710 s_retbool (eqv (car (SCHEME_V->args), cadr (SCHEME_V->args)));
4711 } 4563 }
4712 4564
4713 return S_T; 4565 s_retbool (r);
4714} 4566}
4715 4567
4716static pointer 4568static int
4717opexe_4 (SCHEME_P_ enum scheme_opcodes op) 4569opexe_4 (SCHEME_P_ enum scheme_opcodes op)
4718{ 4570{
4571 pointer args = SCHEME_V->args;
4572 pointer a = car (args);
4719 pointer x, y; 4573 pointer x, y;
4720 4574
4721 switch (op) 4575 switch (op)
4722 { 4576 {
4723 case OP_FORCE: /* force */ 4577 case OP_FORCE: /* force */
4724 SCHEME_V->code = car (SCHEME_V->args); 4578 SCHEME_V->code = a;
4725 4579
4726 if (is_promise (SCHEME_V->code)) 4580 if (is_promise (SCHEME_V->code))
4727 { 4581 {
4728 /* Should change type to closure here */ 4582 /* Should change type to closure here */
4729 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); 4583 s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code);
4750 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4604 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4751 SCHEME_V->outport = cadr (SCHEME_V->args); 4605 SCHEME_V->outport = cadr (SCHEME_V->args);
4752 } 4606 }
4753 } 4607 }
4754 4608
4755 SCHEME_V->args = car (SCHEME_V->args); 4609 SCHEME_V->args = a;
4756 4610
4757 if (op == OP_WRITE) 4611 if (op == OP_WRITE)
4758 SCHEME_V->print_flag = 1; 4612 SCHEME_V->print_flag = 1;
4759 else 4613 else
4760 SCHEME_V->print_flag = 0; 4614 SCHEME_V->print_flag = 0;
4761 4615
4762 s_goto (OP_P0LIST); 4616 s_goto (OP_P0LIST);
4763 4617
4764 case OP_NEWLINE: /* newline */ 4618 case OP_NEWLINE: /* newline */
4765 if (is_pair (SCHEME_V->args)) 4619 if (is_pair (args))
4766 { 4620 {
4767 if (car (SCHEME_V->args) != SCHEME_V->outport) 4621 if (a != SCHEME_V->outport)
4768 { 4622 {
4769 x = cons (SCHEME_V->outport, NIL); 4623 x = cons (SCHEME_V->outport, NIL);
4770 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); 4624 s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL);
4771 SCHEME_V->outport = car (SCHEME_V->args); 4625 SCHEME_V->outport = a;
4772 } 4626 }
4773 } 4627 }
4774 4628
4775 putstr (SCHEME_A_ "\n"); 4629 putstr (SCHEME_A_ "\n");
4776 s_return (S_T); 4630 s_return (S_T);
4777#endif 4631#endif
4778 4632
4779 case OP_ERR0: /* error */ 4633 case OP_ERR0: /* error */
4780 SCHEME_V->retcode = -1; 4634 SCHEME_V->retcode = -1;
4781 4635
4782 if (!is_string (car (SCHEME_V->args))) 4636 if (!is_string (a))
4783 { 4637 {
4784 SCHEME_V->args = cons (mk_string (SCHEME_A_ " -- "), SCHEME_V->args); 4638 args = cons (mk_string (SCHEME_A_ " -- "), args);
4785 setimmutable (car (SCHEME_V->args)); 4639 setimmutable (car (args));
4786 } 4640 }
4787 4641
4788 putstr (SCHEME_A_ "Error: "); 4642 putstr (SCHEME_A_ "Error: ");
4789 putstr (SCHEME_A_ strvalue (car (SCHEME_V->args))); 4643 putstr (SCHEME_A_ strvalue (car (args)));
4790 SCHEME_V->args = cdr (SCHEME_V->args); 4644 SCHEME_V->args = cdr (args);
4791 s_goto (OP_ERR1); 4645 s_goto (OP_ERR1);
4792 4646
4793 case OP_ERR1: /* error */ 4647 case OP_ERR1: /* error */
4794 putstr (SCHEME_A_ " "); 4648 putstr (SCHEME_A_ " ");
4795 4649
4796 if (SCHEME_V->args != NIL) 4650 if (args != NIL)
4797 { 4651 {
4798 s_save (SCHEME_A_ OP_ERR1, cdr (SCHEME_V->args), NIL); 4652 s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL);
4799 SCHEME_V->args = car (SCHEME_V->args); 4653 SCHEME_V->args = a;
4800 SCHEME_V->print_flag = 1; 4654 SCHEME_V->print_flag = 1;
4801 s_goto (OP_P0LIST); 4655 s_goto (OP_P0LIST);
4802 } 4656 }
4803 else 4657 else
4804 { 4658 {
4805 putstr (SCHEME_A_ "\n"); 4659 putstr (SCHEME_A_ "\n");
4806 4660
4807 if (SCHEME_V->interactive_repl) 4661 if (SCHEME_V->interactive_repl)
4808 s_goto (OP_T0LVL); 4662 s_goto (OP_T0LVL);
4809 else 4663 else
4810 return NIL; 4664 return -1;
4811 } 4665 }
4812 4666
4813 case OP_REVERSE: /* reverse */ 4667 case OP_REVERSE: /* reverse */
4814 s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); 4668 s_return (reverse (SCHEME_A_ a));
4815 4669
4816 case OP_LIST_STAR: /* list* */ 4670 case OP_LIST_STAR: /* list* */
4817 s_return (list_star (SCHEME_A_ SCHEME_V->args)); 4671 s_return (list_star (SCHEME_A_ SCHEME_V->args));
4818 4672
4819 case OP_APPEND: /* append */ 4673 case OP_APPEND: /* append */
4820 x = NIL; 4674 x = NIL;
4821 y = SCHEME_V->args; 4675 y = args;
4822 4676
4823 if (y == x) 4677 if (y == x)
4824 s_return (x); 4678 s_return (x);
4825 4679
4826 /* cdr() in the while condition is not a typo. If car() */ 4680 /* cdr() in the while condition is not a typo. If car() */
4837 s_return (reverse_in_place (SCHEME_A_ car (y), x)); 4691 s_return (reverse_in_place (SCHEME_A_ car (y), x));
4838 4692
4839#if USE_PLIST 4693#if USE_PLIST
4840 4694
4841 case OP_PUT: /* put */ 4695 case OP_PUT: /* put */
4842 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4696 if (!hasprop (a) || !hasprop (cadr (args)))
4843 Error_0 ("illegal use of put"); 4697 Error_0 ("illegal use of put");
4844 4698
4845 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4699 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4846 { 4700 {
4847 if (caar (x) == y) 4701 if (caar (x) == y)
4848 break; 4702 break;
4849 } 4703 }
4850 4704
4851 if (x != NIL) 4705 if (x != NIL)
4852 cdar (x) = caddr (SCHEME_V->args); 4706 cdar (x) = caddr (args);
4853 else 4707 else
4854 symprop (car (SCHEME_V->args)) = cons (cons (y, caddr (SCHEME_V->args)), symprop (car (SCHEME_V->args))); 4708 symprop (a) = cons (cons (y, caddr (args)), symprop (a));
4855 4709
4856 s_return (S_T); 4710 s_return (S_T);
4857 4711
4858 case OP_GET: /* get */ 4712 case OP_GET: /* get */
4859 if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) 4713 if (!hasprop (a) || !hasprop (cadr (args)))
4860 Error_0 ("illegal use of get"); 4714 Error_0 ("illegal use of get");
4861 4715
4862 for (x = symprop (car (SCHEME_V->args)), y = cadr (SCHEME_V->args); x != NIL; x = cdr (x)) 4716 for (x = symprop (a), y = cadr (args); x != NIL; x = cdr (x))
4863 if (caar (x) == y) 4717 if (caar (x) == y)
4864 break; 4718 break;
4865 4719
4866 if (x != NIL) 4720 if (x != NIL)
4867 s_return (cdar (x)); 4721 s_return (cdar (x));
4869 s_return (NIL); 4723 s_return (NIL);
4870 4724
4871#endif /* USE_PLIST */ 4725#endif /* USE_PLIST */
4872 4726
4873 case OP_QUIT: /* quit */ 4727 case OP_QUIT: /* quit */
4874 if (is_pair (SCHEME_V->args)) 4728 if (is_pair (args))
4875 SCHEME_V->retcode = ivalue (car (SCHEME_V->args)); 4729 SCHEME_V->retcode = ivalue (a);
4876 4730
4877 return NIL; 4731 return -1;
4878 4732
4879 case OP_GC: /* gc */ 4733 case OP_GC: /* gc */
4880 gc (SCHEME_A_ NIL, NIL); 4734 gc (SCHEME_A_ NIL, NIL);
4881 s_return (S_T); 4735 s_return (S_T);
4882 4736
4883 case OP_GCVERB: /* gc-verbose */ 4737 case OP_GCVERB: /* gc-verbose */
4884 { 4738 {
4885 int was = SCHEME_V->gc_verbose; 4739 int was = SCHEME_V->gc_verbose;
4886 4740
4887 SCHEME_V->gc_verbose = (car (SCHEME_V->args) != S_F); 4741 SCHEME_V->gc_verbose = (a != S_F);
4888 s_retbool (was); 4742 s_retbool (was);
4889 } 4743 }
4890 4744
4891 case OP_NEWSEGMENT: /* new-segment */ 4745 case OP_NEWSEGMENT: /* new-segment */
4892 if (!is_pair (SCHEME_V->args) || !is_number (car (SCHEME_V->args))) 4746 if (!is_pair (args) || !is_number (a))
4893 Error_0 ("new-segment: argument must be a number"); 4747 Error_0 ("new-segment: argument must be a number");
4894 4748
4895 alloc_cellseg (SCHEME_A_ (int)ivalue (car (SCHEME_V->args))); 4749 alloc_cellseg (SCHEME_A_ (int)ivalue (a));
4896 4750
4897 s_return (S_T); 4751 s_return (S_T);
4898 4752
4899 case OP_OBLIST: /* oblist */ 4753 case OP_OBLIST: /* oblist */
4900 s_return (oblist_all_symbols (SCHEME_A)); 4754 s_return (oblist_all_symbols (SCHEME_A));
4927 case OP_OPEN_INOUTFILE: 4781 case OP_OPEN_INOUTFILE:
4928 prop = port_input | port_output; 4782 prop = port_input | port_output;
4929 break; 4783 break;
4930 } 4784 }
4931 4785
4932 p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); 4786 p = port_from_filename (SCHEME_A_ strvalue (a), prop);
4933 4787
4934 if (p == NIL) 4788 if (p == NIL)
4935 s_return (S_F); 4789 s_return (S_F);
4936 4790
4937 s_return (p); 4791 s_return (p);
4954 case OP_OPEN_INOUTSTRING: 4808 case OP_OPEN_INOUTSTRING:
4955 prop = port_input | port_output; 4809 prop = port_input | port_output;
4956 break; 4810 break;
4957 } 4811 }
4958 4812
4959 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4813 p = port_from_string (SCHEME_A_ strvalue (a),
4960 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); 4814 strvalue (a) + strlength (a), prop);
4961 4815
4962 if (p == NIL) 4816 if (p == NIL)
4963 s_return (S_F); 4817 s_return (S_F);
4964 4818
4965 s_return (p); 4819 s_return (p);
4967 4821
4968 case OP_OPEN_OUTSTRING: /* open-output-string */ 4822 case OP_OPEN_OUTSTRING: /* open-output-string */
4969 { 4823 {
4970 pointer p; 4824 pointer p;
4971 4825
4972 if (car (SCHEME_V->args) == NIL) 4826 if (a == NIL)
4973 { 4827 {
4974 p = port_from_scratch (SCHEME_A); 4828 p = port_from_scratch (SCHEME_A);
4975 4829
4976 if (p == NIL) 4830 if (p == NIL)
4977 s_return (S_F); 4831 s_return (S_F);
4978 } 4832 }
4979 else 4833 else
4980 { 4834 {
4981 p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), 4835 p = port_from_string (SCHEME_A_ strvalue (a),
4982 strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); 4836 strvalue (a) + strlength (a), port_output);
4983 4837
4984 if (p == NIL) 4838 if (p == NIL)
4985 s_return (S_F); 4839 s_return (S_F);
4986 } 4840 }
4987 4841
4990 4844
4991 case OP_GET_OUTSTRING: /* get-output-string */ 4845 case OP_GET_OUTSTRING: /* get-output-string */
4992 { 4846 {
4993 port *p; 4847 port *p;
4994 4848
4995 if ((p = car (SCHEME_V->args)->object.port)->kind & port_string) 4849 if ((p = a->object.port)->kind & port_string)
4996 { 4850 {
4997 off_t size; 4851 off_t size;
4998 char *str; 4852 char *str;
4999 4853
5000 size = p->rep.string.curr - p->rep.string.start + 1; 4854 size = p->rep.string.curr - p->rep.string.start + 1;
5016 } 4870 }
5017 4871
5018# endif 4872# endif
5019 4873
5020 case OP_CLOSE_INPORT: /* close-input-port */ 4874 case OP_CLOSE_INPORT: /* close-input-port */
5021 port_close (SCHEME_A_ car (SCHEME_V->args), port_input); 4875 port_close (SCHEME_A_ a, port_input);
5022 s_return (S_T); 4876 s_return (S_T);
5023 4877
5024 case OP_CLOSE_OUTPORT: /* close-output-port */ 4878 case OP_CLOSE_OUTPORT: /* close-output-port */
5025 port_close (SCHEME_A_ car (SCHEME_V->args), port_output); 4879 port_close (SCHEME_A_ a, port_output);
5026 s_return (S_T); 4880 s_return (S_T);
5027#endif 4881#endif
5028 4882
5029 case OP_INT_ENV: /* interaction-environment */ 4883 case OP_INT_ENV: /* interaction-environment */
5030 s_return (SCHEME_V->global_env); 4884 s_return (SCHEME_V->global_env);
5032 case OP_CURR_ENV: /* current-environment */ 4886 case OP_CURR_ENV: /* current-environment */
5033 s_return (SCHEME_V->envir); 4887 s_return (SCHEME_V->envir);
5034 4888
5035 } 4889 }
5036 4890
5037 return S_T; 4891 abort ();
5038} 4892}
5039 4893
5040static pointer 4894static int
5041opexe_5 (SCHEME_P_ enum scheme_opcodes op) 4895opexe_5 (SCHEME_P_ enum scheme_opcodes op)
5042{ 4896{
4897 pointer args = SCHEME_V->args;
5043 pointer x; 4898 pointer x;
5044 4899
5045 if (SCHEME_V->nesting != 0) 4900 if (SCHEME_V->nesting != 0)
5046 { 4901 {
5047 int n = SCHEME_V->nesting; 4902 int n = SCHEME_V->nesting;
5054 switch (op) 4909 switch (op)
5055 { 4910 {
5056 /* ========== reading part ========== */ 4911 /* ========== reading part ========== */
5057#if USE_PORTS 4912#if USE_PORTS
5058 case OP_READ: 4913 case OP_READ:
5059 if (!is_pair (SCHEME_V->args)) 4914 if (!is_pair (args))
5060 s_goto (OP_READ_INTERNAL); 4915 s_goto (OP_READ_INTERNAL);
5061 4916
5062 if (!is_inport (car (SCHEME_V->args))) 4917 if (!is_inport (car (args)))
5063 Error_1 ("read: not an input port:", car (SCHEME_V->args)); 4918 Error_1 ("read: not an input port:", car (args));
5064 4919
5065 if (car (SCHEME_V->args) == SCHEME_V->inport) 4920 if (car (args) == SCHEME_V->inport)
5066 s_goto (OP_READ_INTERNAL); 4921 s_goto (OP_READ_INTERNAL);
5067 4922
5068 x = SCHEME_V->inport; 4923 x = SCHEME_V->inport;
5069 SCHEME_V->inport = car (SCHEME_V->args); 4924 SCHEME_V->inport = car (args);
5070 x = cons (x, NIL); 4925 x = cons (x, NIL);
5071 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4926 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5072 s_goto (OP_READ_INTERNAL); 4927 s_goto (OP_READ_INTERNAL);
5073 4928
5074 case OP_READ_CHAR: /* read-char */ 4929 case OP_READ_CHAR: /* read-char */
5075 case OP_PEEK_CHAR: /* peek-char */ 4930 case OP_PEEK_CHAR: /* peek-char */
5076 { 4931 {
5077 int c; 4932 int c;
5078 4933
5079 if (is_pair (SCHEME_V->args)) 4934 if (is_pair (args))
5080 { 4935 {
5081 if (car (SCHEME_V->args) != SCHEME_V->inport) 4936 if (car (args) != SCHEME_V->inport)
5082 { 4937 {
5083 x = SCHEME_V->inport; 4938 x = SCHEME_V->inport;
5084 x = cons (x, NIL); 4939 x = cons (x, NIL);
5085 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); 4940 s_save (SCHEME_A_ OP_SET_INPORT, x, NIL);
5086 SCHEME_V->inport = car (SCHEME_V->args); 4941 SCHEME_V->inport = car (args);
5087 } 4942 }
5088 } 4943 }
5089 4944
5090 c = inchar (SCHEME_A); 4945 c = inchar (SCHEME_A);
5091 4946
5101 case OP_CHAR_READY: /* char-ready? */ 4956 case OP_CHAR_READY: /* char-ready? */
5102 { 4957 {
5103 pointer p = SCHEME_V->inport; 4958 pointer p = SCHEME_V->inport;
5104 int res; 4959 int res;
5105 4960
5106 if (is_pair (SCHEME_V->args)) 4961 if (is_pair (args))
5107 p = car (SCHEME_V->args); 4962 p = car (args);
5108 4963
5109 res = p->object.port->kind & port_string; 4964 res = p->object.port->kind & port_string;
5110 4965
5111 s_retbool (res); 4966 s_retbool (res);
5112 } 4967 }
5113 4968
5114 case OP_SET_INPORT: /* set-input-port */ 4969 case OP_SET_INPORT: /* set-input-port */
5115 SCHEME_V->inport = car (SCHEME_V->args); 4970 SCHEME_V->inport = car (args);
5116 s_return (SCHEME_V->value); 4971 s_return (SCHEME_V->value);
5117 4972
5118 case OP_SET_OUTPORT: /* set-output-port */ 4973 case OP_SET_OUTPORT: /* set-output-port */
5119 SCHEME_V->outport = car (SCHEME_V->args); 4974 SCHEME_V->outport = car (args);
5120 s_return (SCHEME_V->value); 4975 s_return (SCHEME_V->value);
5121#endif 4976#endif
5122 4977
5123 case OP_RDSEXPR: 4978 case OP_RDSEXPR:
5124 switch (SCHEME_V->tok) 4979 switch (SCHEME_V->tok)
5210 } 5065 }
5211 5066
5212 break; 5067 break;
5213 5068
5214 case OP_RDLIST: 5069 case OP_RDLIST:
5215 SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); 5070 SCHEME_V->args = cons (SCHEME_V->value, args);
5216 SCHEME_V->tok = token (SCHEME_A); 5071 SCHEME_V->tok = token (SCHEME_A);
5217 5072
5218 switch (SCHEME_V->tok) 5073 switch (SCHEME_V->tok)
5219 { 5074 {
5220 case TOK_EOF: 5075 case TOK_EOF:
5248 case OP_RDDOT: 5103 case OP_RDDOT:
5249 if (token (SCHEME_A) != TOK_RPAREN) 5104 if (token (SCHEME_A) != TOK_RPAREN)
5250 Error_0 ("syntax error: illegal dot expression"); 5105 Error_0 ("syntax error: illegal dot expression");
5251 5106
5252 SCHEME_V->nesting_stack[SCHEME_V->file_i]--; 5107 SCHEME_V->nesting_stack[SCHEME_V->file_i]--;
5253 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, SCHEME_V->args)); 5108 s_return (reverse_in_place (SCHEME_A_ SCHEME_V->value, args));
5254 5109
5255 case OP_RDQUOTE: 5110 case OP_RDQUOTE:
5256 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); 5111 s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL)));
5257 5112
5258 case OP_RDQQUOTE: 5113 case OP_RDQQUOTE:
5280 SCHEME_V->args = SCHEME_V->value; 5135 SCHEME_V->args = SCHEME_V->value;
5281 s_goto (OP_VECTOR); 5136 s_goto (OP_VECTOR);
5282 5137
5283 /* ========== printing part ========== */ 5138 /* ========== printing part ========== */
5284 case OP_P0LIST: 5139 case OP_P0LIST:
5285 if (is_vector (SCHEME_V->args)) 5140 if (is_vector (args))
5286 { 5141 {
5287 putstr (SCHEME_A_ "#("); 5142 putstr (SCHEME_A_ "#(");
5288 SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); 5143 SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0));
5289 s_goto (OP_PVECFROM); 5144 s_goto (OP_PVECFROM);
5290 } 5145 }
5291 else if (is_environment (SCHEME_V->args)) 5146 else if (is_environment (args))
5292 { 5147 {
5293 putstr (SCHEME_A_ "#<ENVIRONMENT>"); 5148 putstr (SCHEME_A_ "#<ENVIRONMENT>");
5294 s_return (S_T); 5149 s_return (S_T);
5295 } 5150 }
5296 else if (!is_pair (SCHEME_V->args)) 5151 else if (!is_pair (args))
5297 { 5152 {
5298 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5153 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5299 s_return (S_T); 5154 s_return (S_T);
5300 } 5155 }
5301 else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5156 else
5302 { 5157 {
5158 pointer a = car (args);
5159 pointer b = cdr (args);
5160 int ok_abbr = ok_abbrev (b);
5161 SCHEME_V->args = car (b);
5162
5163 if (a == SCHEME_V->QUOTE && ok_abbr)
5303 putstr (SCHEME_A_ "'"); 5164 putstr (SCHEME_A_ "'");
5304 SCHEME_V->args = cadr (SCHEME_V->args); 5165 else if (a == SCHEME_V->QQUOTE && ok_abbr)
5166 putstr (SCHEME_A_ "`");
5167 else if (a == SCHEME_V->UNQUOTE && ok_abbr)
5168 putstr (SCHEME_A_ ",");
5169 else if (a == SCHEME_V->UNQUOTESP && ok_abbr)
5170 putstr (SCHEME_A_ ",@");
5171 else
5172 {
5173 putstr (SCHEME_A_ "(");
5174 s_save (SCHEME_A_ OP_P1LIST, b, NIL);
5175 SCHEME_V->args = a;
5176 }
5177
5305 s_goto (OP_P0LIST); 5178 s_goto (OP_P0LIST);
5306 } 5179 }
5307 else if (car (SCHEME_V->args) == SCHEME_V->QQUOTE && ok_abbrev (cdr (SCHEME_V->args))) 5180
5181 case OP_P1LIST:
5182 if (is_pair (args))
5308 { 5183 {
5184 s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL);
5309 putstr (SCHEME_A_ "`"); 5185 putstr (SCHEME_A_ " ");
5310 SCHEME_V->args = cadr (SCHEME_V->args); 5186 SCHEME_V->args = car (args);
5311 s_goto (OP_P0LIST); 5187 s_goto (OP_P0LIST);
5312 } 5188 }
5313 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTE && ok_abbrev (cdr (SCHEME_V->args)))
5314 {
5315 putstr (SCHEME_A_ ",");
5316 SCHEME_V->args = cadr (SCHEME_V->args);
5317 s_goto (OP_P0LIST);
5318 }
5319 else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTESP && ok_abbrev (cdr (SCHEME_V->args)))
5320 {
5321 putstr (SCHEME_A_ ",@");
5322 SCHEME_V->args = cadr (SCHEME_V->args);
5323 s_goto (OP_P0LIST);
5324 }
5325 else
5326 {
5327 putstr (SCHEME_A_ "(");
5328 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL);
5329 SCHEME_V->args = car (SCHEME_V->args);
5330 s_goto (OP_P0LIST);
5331 }
5332
5333 case OP_P1LIST:
5334 if (is_pair (SCHEME_V->args))
5335 {
5336 s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL);
5337 putstr (SCHEME_A_ " ");
5338 SCHEME_V->args = car (SCHEME_V->args);
5339 s_goto (OP_P0LIST);
5340 }
5341 else if (is_vector (SCHEME_V->args)) 5189 else if (is_vector (args))
5342 { 5190 {
5343 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); 5191 s_save (SCHEME_A_ OP_P1LIST, NIL, NIL);
5344 putstr (SCHEME_A_ " . "); 5192 putstr (SCHEME_A_ " . ");
5345 s_goto (OP_P0LIST); 5193 s_goto (OP_P0LIST);
5346 } 5194 }
5347 else 5195 else
5348 { 5196 {
5349 if (SCHEME_V->args != NIL) 5197 if (args != NIL)
5350 { 5198 {
5351 putstr (SCHEME_A_ " . "); 5199 putstr (SCHEME_A_ " . ");
5352 printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); 5200 printatom (SCHEME_A_ args, SCHEME_V->print_flag);
5353 } 5201 }
5354 5202
5355 putstr (SCHEME_A_ ")"); 5203 putstr (SCHEME_A_ ")");
5356 s_return (S_T); 5204 s_return (S_T);
5357 } 5205 }
5358 5206
5359 case OP_PVECFROM: 5207 case OP_PVECFROM:
5360 { 5208 {
5361 int i = ivalue_unchecked (cdr (SCHEME_V->args)); 5209 int i = ivalue_unchecked (cdr (args));
5362 pointer vec = car (SCHEME_V->args); 5210 pointer vec = car (args);
5363 int len = veclength (vec); 5211 int len = veclength (vec);
5364 5212
5365 if (i == len) 5213 if (i == len)
5366 { 5214 {
5367 putstr (SCHEME_A_ ")"); 5215 putstr (SCHEME_A_ ")");
5369 } 5217 }
5370 else 5218 else
5371 { 5219 {
5372 pointer elem = vector_elem (vec, i); 5220 pointer elem = vector_elem (vec, i);
5373 5221
5374 ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; 5222 ivalue_unchecked (cdr (args)) = i + 1;
5375 s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); 5223 s_save (SCHEME_A_ OP_PVECFROM, args, NIL);
5376 SCHEME_V->args = elem; 5224 SCHEME_V->args = elem;
5377 5225
5378 if (i > 0) 5226 if (i > 0)
5379 putstr (SCHEME_A_ " "); 5227 putstr (SCHEME_A_ " ");
5380 5228
5381 s_goto (OP_P0LIST); 5229 s_goto (OP_P0LIST);
5382 } 5230 }
5383 } 5231 }
5384 } 5232 }
5385 5233
5386 return S_T; 5234 abort ();
5387} 5235}
5388 5236
5389static pointer 5237static int
5390opexe_6 (SCHEME_P_ enum scheme_opcodes op) 5238opexe_6 (SCHEME_P_ enum scheme_opcodes op)
5391{ 5239{
5240 pointer args = SCHEME_V->args;
5241 pointer a = car (args);
5392 pointer x, y; 5242 pointer x, y;
5393 5243
5394 switch (op) 5244 switch (op)
5395 { 5245 {
5396 case OP_LIST_LENGTH: /* length *//* a.k */ 5246 case OP_LIST_LENGTH: /* length *//* a.k */
5397 { 5247 {
5398 long v = list_length (SCHEME_A_ car (SCHEME_V->args)); 5248 long v = list_length (SCHEME_A_ a);
5399 5249
5400 if (v < 0) 5250 if (v < 0)
5401 Error_1 ("length: not a list:", car (SCHEME_V->args)); 5251 Error_1 ("length: not a list:", a);
5402 5252
5403 s_return (mk_integer (SCHEME_A_ v)); 5253 s_return (mk_integer (SCHEME_A_ v));
5404 } 5254 }
5405 5255
5406 case OP_ASSQ: /* assq *//* a.k */ 5256 case OP_ASSQ: /* assq *//* a.k */
5407 x = car (SCHEME_V->args); 5257 x = a;
5408 5258
5409 for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) 5259 for (y = cadr (args); is_pair (y); y = cdr (y))
5410 { 5260 {
5411 if (!is_pair (car (y))) 5261 if (!is_pair (car (y)))
5412 Error_0 ("unable to handle non pair element"); 5262 Error_0 ("unable to handle non pair element");
5413 5263
5414 if (x == caar (y)) 5264 if (x == caar (y))
5420 else 5270 else
5421 s_return (S_F); 5271 s_return (S_F);
5422 5272
5423 5273
5424 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ 5274 case OP_GET_CLOSURE: /* get-closure-code *//* a.k */
5425 SCHEME_V->args = car (SCHEME_V->args); 5275 SCHEME_V->args = a;
5426 5276
5427 if (SCHEME_V->args == NIL) 5277 if (SCHEME_V->args == NIL)
5428 s_return (S_F); 5278 s_return (S_F);
5429 else if (is_closure (SCHEME_V->args)) 5279 else if (is_closure (SCHEME_V->args))
5430 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); 5280 s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value)));
5436 case OP_CLOSUREP: /* closure? */ 5286 case OP_CLOSUREP: /* closure? */
5437 /* 5287 /*
5438 * Note, macro object is also a closure. 5288 * Note, macro object is also a closure.
5439 * Therefore, (closure? <#MACRO>) ==> #t 5289 * Therefore, (closure? <#MACRO>) ==> #t
5440 */ 5290 */
5441 s_retbool (is_closure (car (SCHEME_V->args))); 5291 s_retbool (is_closure (a));
5442 5292
5443 case OP_MACROP: /* macro? */ 5293 case OP_MACROP: /* macro? */
5444 s_retbool (is_macro (car (SCHEME_V->args))); 5294 s_retbool (is_macro (a));
5445 } 5295 }
5446 5296
5447 return S_T; /* NOTREACHED */ 5297 abort ();
5448} 5298}
5449 5299
5300/* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */
5450typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); 5301typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes);
5451 5302
5452typedef int (*test_predicate) (pointer); 5303typedef int (*test_predicate)(pointer);
5453static int 5304static int
5454is_any (pointer p) 5305is_any (pointer p)
5455{ 5306{
5456 return 1; 5307 return 1;
5457} 5308}
5458 5309
5459static int 5310static int
5460is_nonneg (pointer p) 5311is_nonneg (pointer p)
5461{ 5312{
5462 return ivalue (p) >= 0 && is_integer (p); 5313 return ivalue (p) >= 0 && is_integer (p);
5314}
5315
5316static int
5317tst_is_list (pointer p)
5318{
5319 return p == NIL || is_pair (p);
5463} 5320}
5464 5321
5465/* Correspond carefully with following defines! */ 5322/* Correspond carefully with following defines! */
5466static struct 5323static struct
5467{ 5324{
5468 test_predicate fct; 5325 test_predicate fct;
5469 const char *kind; 5326 const char *kind;
5470} tests[] = 5327} tests[] =
5471{ 5328{
5472 { 0, 0}, /* unused */ 5329 { is_any, 0 },
5473 { is_any, 0}, 5330 { is_string, "string" },
5474 { is_string, "string" }, 5331 { is_symbol, "symbol" },
5475 { is_symbol, "symbol" }, 5332 { is_port, "port" },
5476 { is_port, "port" },
5477 { is_inport, "input port" }, 5333 { is_inport, "input port" },
5478 { is_outport, "output port" }, 5334 { is_outport, "output port" },
5479 { is_environment, "environment" }, 5335 { is_environment, "environment" },
5480 { is_pair, "pair" }, 5336 { is_pair, "pair" },
5481 { 0, "pair or '()" }, 5337 { tst_is_list, "pair or '()" },
5482 { is_character, "character" }, 5338 { is_character, "character" },
5483 { is_vector, "vector" }, 5339 { is_vector, "vector" },
5484 { is_number, "number" }, 5340 { is_number, "number" },
5485 { is_integer, "integer" }, 5341 { is_integer, "integer" },
5486 { is_nonneg, "non-negative integer" } 5342 { is_nonneg, "non-negative integer" }
5487}; 5343};
5488 5344
5489#define TST_NONE 0 5345#define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */
5490#define TST_ANY "\001" 5346#define TST_ANY "\001"
5491#define TST_STRING "\002" 5347#define TST_STRING "\002"
5492#define TST_SYMBOL "\003" 5348#define TST_SYMBOL "\003"
5493#define TST_PORT "\004" 5349#define TST_PORT "\004"
5494#define TST_INPORT "\005" 5350#define TST_INPORT "\005"
5495#define TST_OUTPORT "\006" 5351#define TST_OUTPORT "\006"
5496#define TST_ENVIRONMENT "\007" 5352#define TST_ENVIRONMENT "\007"
5497#define TST_PAIR "\010" 5353#define TST_PAIR "\010"
5498#define TST_LIST "\011" 5354#define TST_LIST "\011"
5499#define TST_CHAR "\012" 5355#define TST_CHAR "\012"
5500#define TST_VECTOR "\013" 5356#define TST_VECTOR "\013"
5501#define TST_NUMBER "\014" 5357#define TST_NUMBER "\014"
5502#define TST_INTEGER "\015" 5358#define TST_INTEGER "\015"
5503#define TST_NATURAL "\016" 5359#define TST_NATURAL "\016"
5360
5361#define INF_ARG 0xff
5362#define UNNAMED_OP ""
5363
5364static const char opnames[] =
5365#define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00"
5366#include "opdefines.h"
5367#undef OP_DEF
5368;
5369
5370static const char *
5371opname (int idx)
5372{
5373 const char *name = opnames;
5374
5375 /* should do this at compile time, but would require external program, right? */
5376 while (idx--)
5377 name += strlen (name) + 1;
5378
5379 return *name ? name : "ILLEGAL";
5380}
5381
5382static const char *
5383procname (pointer x)
5384{
5385 return opname (procnum (x));
5386}
5504 5387
5505typedef struct 5388typedef struct
5506{ 5389{
5507 dispatch_func func; 5390 uint8_t func;
5508 char *name; 5391 /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */
5392 uint8_t builtin;
5509 int min_arity; 5393 uint8_t min_arity;
5510 int max_arity; 5394 uint8_t max_arity;
5511 char *arg_tests_encoding; 5395 char arg_tests_encoding[3];
5512} op_code_info; 5396} op_code_info;
5513 5397
5514#define INF_ARG 0xffff
5515
5516static op_code_info dispatch_table[] = { 5398static const op_code_info dispatch_table[] = {
5517#define OP_DEF(A,B,C,D,E,OP) {A,B,C,D,E}, 5399#define OP_DEF(func,name,minarity,maxarity,argtest,op) { func, sizeof (name) > 1, minarity, maxarity, argtest },
5518#include "opdefines.h" 5400#include "opdefines.h"
5401#undef OP_DEF
5519 {0} 5402 {0}
5520}; 5403};
5521
5522static const char *
5523procname (pointer x)
5524{
5525 int n = procnum (x);
5526 const char *name = dispatch_table[n].name;
5527
5528 if (name == 0)
5529 name = "ILLEGAL!";
5530
5531 return name;
5532}
5533 5404
5534/* kernel of this interpreter */ 5405/* kernel of this interpreter */
5535static void 5406static void
5536Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) 5407Eval_Cycle (SCHEME_P_ enum scheme_opcodes op)
5537{ 5408{
5538 SCHEME_V->op = op; 5409 SCHEME_V->op = op;
5539 5410
5540 for (;;) 5411 for (;;)
5541 { 5412 {
5542 op_code_info *pcd = dispatch_table + SCHEME_V->op; 5413 const op_code_info *pcd = dispatch_table + SCHEME_V->op;
5543 5414
5544#if USE_ERROR_CHECKING 5415#if USE_ERROR_CHECKING
5545 if (pcd->name) /* if built-in function, check arguments */ 5416 if (pcd->builtin) /* if built-in function, check arguments */
5546 { 5417 {
5547 int ok = 1; 5418 int ok = 1;
5548 char msg[STRBUFFSIZE]; 5419 char msg[STRBUFFSIZE];
5549 int n = list_length (SCHEME_A_ SCHEME_V->args); 5420 int n = list_length (SCHEME_A_ SCHEME_V->args);
5550 5421
5551 /* Check number of arguments */ 5422 /* Check number of arguments */
5552 if (ecb_expect_false (n < pcd->min_arity)) 5423 if (ecb_expect_false (n < pcd->min_arity))
5553 { 5424 {
5554 ok = 0; 5425 ok = 0;
5555 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5426 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5556 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); 5427 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity);
5557 } 5428 }
5558 else if (ecb_excpect_false (n > pcd->max_arity)) 5429 else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG))
5559 { 5430 {
5560 ok = 0; 5431 ok = 0;
5561 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", 5432 snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)",
5562 pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); 5433 opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity);
5563 } 5434 }
5564 5435 else
5565 if (ecb_expect_false (ok))
5566 { 5436 {
5567 if (pcd->arg_tests_encoding) 5437 if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */
5568 { 5438 {
5569 int i = 0; 5439 int i = 0;
5570 int j; 5440 int j;
5571 const char *t = pcd->arg_tests_encoding; 5441 const char *t = pcd->arg_tests_encoding;
5572 pointer arglist = SCHEME_V->args; 5442 pointer arglist = SCHEME_V->args;
5573 5443
5574 do 5444 do
5575 { 5445 {
5576 pointer arg = car (arglist); 5446 pointer arg = car (arglist);
5577 5447
5578 j = (int) t[0]; 5448 j = t[0];
5579 5449
5580 if (j == TST_LIST[0]) 5450 if (!tests[j - 1].fct (arg))
5581 {
5582 if (arg != NIL && !is_pair (arg))
5583 break; 5451 break;
5584 }
5585 else
5586 {
5587 if (!tests[j].fct (arg))
5588 break;
5589 }
5590 5452
5591 if (t[1] != 0) /* last test is replicated as necessary */ 5453 if (t[1]) /* last test is replicated as necessary */
5592 t++; 5454 t++;
5593 5455
5594 arglist = cdr (arglist); 5456 arglist = cdr (arglist);
5595 i++; 5457 i++;
5596 } 5458 }
5597 while (i < n); 5459 while (i < n);
5598 5460
5599 if (i < n) 5461 if (i < n)
5600 { 5462 {
5601 ok = 0; 5463 ok = 0;
5602 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); 5464 snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind);
5603 } 5465 }
5604 } 5466 }
5605 } 5467 }
5606 5468
5607 if (!ok) 5469 if (!ok)
5608 { 5470 {
5471 /* tinyscheme tested for returncode, but Error_1 always diverts? */
5609 if (xError_1 (SCHEME_A_ msg, 0) == NIL) 5472 xError_1 (SCHEME_A_ msg, 0);
5610 return; 5473 continue;
5611
5612 pcd = dispatch_table + SCHEME_V->op;
5613 } 5474 }
5614 } 5475 }
5615#endif 5476#endif
5616 5477
5617 ok_to_freely_gc (SCHEME_A); 5478 ok_to_freely_gc (SCHEME_A);
5618 5479
5480 static const dispatch_func dispatch_funcs[] = {
5481 opexe_0,
5482 opexe_1,
5483 opexe_2,
5484 opexe_3,
5485 opexe_4,
5486 opexe_5,
5487 opexe_6,
5488 };
5489
5619 if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) 5490 if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0))
5620 return; 5491 return;
5621 5492
5622 if (SCHEME_V->no_memory && USE_ERROR_CHECKING) 5493 if (SCHEME_V->no_memory && USE_ERROR_CHECKING)
5623 { 5494 {
5624 xwrstr ("No memory!\n"); 5495 xwrstr ("No memory!\n");
5821 5692
5822 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) 5693 for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i)
5823 assign_syntax (SCHEME_A_ syntax_names[i]); 5694 assign_syntax (SCHEME_A_ syntax_names[i]);
5824 } 5695 }
5825 5696
5697 // TODO: should iterate via strlen, to avoid n² complexity
5826 for (i = 0; i < n; i++) 5698 for (i = 0; i < n; i++)
5827 if (dispatch_table[i].name != 0) 5699 if (dispatch_table[i].builtin)
5828 assign_proc (SCHEME_A_ i, dispatch_table[i].name); 5700 assign_proc (SCHEME_A_ i, opname (i));
5829 5701
5830 /* initialization of global pointers to special symbols */ 5702 /* initialization of global pointers to special symbols */
5831 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); 5703 SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda");
5832 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); 5704 SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote");
5833 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); 5705 SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote");
5974{ 5846{
5975 dump_stack_reset (SCHEME_A); 5847 dump_stack_reset (SCHEME_A);
5976 SCHEME_V->envir = SCHEME_V->global_env; 5848 SCHEME_V->envir = SCHEME_V->global_env;
5977 SCHEME_V->file_i = 0; 5849 SCHEME_V->file_i = 0;
5978 SCHEME_V->load_stack[0].kind = port_input | port_string; 5850 SCHEME_V->load_stack[0].kind = port_input | port_string;
5979 SCHEME_V->load_stack[0].rep.string.start = (char *) cmd; /* This func respects const */ 5851 SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */
5980 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *) cmd + strlen (cmd); 5852 SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd);
5981 SCHEME_V->load_stack[0].rep.string.curr = (char *) cmd; 5853 SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd;
5982#if USE_PORTS 5854#if USE_PORTS
5983 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); 5855 SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack);
5984#endif 5856#endif
5985 SCHEME_V->retcode = 0; 5857 SCHEME_V->retcode = 0;
5986 SCHEME_V->interactive_repl = 0; 5858 SCHEME_V->interactive_repl = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines