… | |
… | |
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 |
78 | static scheme sc; |
81 | static scheme sc; |
79 | #endif |
82 | #endif |
… | |
… | |
244 | |
247 | |
245 | #define strvalue(p) ((p)->object.string.svalue) |
248 | #define strvalue(p) ((p)->object.string.svalue) |
246 | #define strlength(p) ((p)->object.string.length) |
249 | #define strlength(p) ((p)->object.string.length) |
247 | |
250 | |
248 | INTERFACE int is_list (SCHEME_P_ pointer p); |
251 | INTERFACE int is_list (SCHEME_P_ pointer p); |
|
|
252 | |
249 | INTERFACE INLINE int |
253 | INTERFACE INLINE int |
250 | is_vector (pointer p) |
254 | is_vector (pointer p) |
251 | { |
255 | { |
252 | return type (p) == T_VECTOR; |
256 | return type (p) == T_VECTOR; |
253 | } |
257 | } |
… | |
… | |
663 | static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); |
667 | static pointer reverse_in_place (SCHEME_P_ pointer term, pointer list); |
664 | static pointer revappend (SCHEME_P_ pointer a, pointer b); |
668 | static pointer revappend (SCHEME_P_ pointer a, pointer b); |
665 | static pointer ss_get_cont (SCHEME_P); |
669 | static pointer ss_get_cont (SCHEME_P); |
666 | static void ss_set_cont (SCHEME_P_ pointer cont); |
670 | static void ss_set_cont (SCHEME_P_ pointer cont); |
667 | static void dump_stack_mark (SCHEME_P); |
671 | static void dump_stack_mark (SCHEME_P); |
668 | static pointer opexe_0 (SCHEME_P_ enum scheme_opcodes op); |
672 | static int opexe_0 (SCHEME_P_ enum scheme_opcodes op); |
|
|
673 | static int opexe_1 (SCHEME_P_ enum scheme_opcodes op); |
669 | static pointer opexe_2 (SCHEME_P_ enum scheme_opcodes op); |
674 | static int opexe_2 (SCHEME_P_ enum scheme_opcodes op); |
670 | static pointer opexe_r (SCHEME_P_ enum scheme_opcodes op); |
|
|
671 | static pointer opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
675 | static int opexe_3 (SCHEME_P_ enum scheme_opcodes op); |
672 | static pointer opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
676 | static int opexe_4 (SCHEME_P_ enum scheme_opcodes op); |
673 | static pointer opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
677 | static int opexe_5 (SCHEME_P_ enum scheme_opcodes op); |
674 | static pointer opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
678 | static int opexe_6 (SCHEME_P_ enum scheme_opcodes op); |
675 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
679 | static void Eval_Cycle (SCHEME_P_ enum scheme_opcodes op); |
676 | static void assign_syntax (SCHEME_P_ const char *name); |
680 | static void assign_syntax (SCHEME_P_ const char *name); |
677 | static int syntaxnum (pointer p); |
681 | static int syntaxnum (pointer p); |
678 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
682 | static void assign_proc (SCHEME_P_ enum scheme_opcodes, const char *name); |
679 | |
683 | |
… | |
… | |
819 | return ce; |
823 | return ce; |
820 | else if (dfl < dce) |
824 | else if (dfl < dce) |
821 | return fl; |
825 | return fl; |
822 | else |
826 | else |
823 | { |
827 | { |
824 | if (fmod (fl, 2.0) == 0.0) /* I imagine this holds */ |
828 | if (fmod (fl, 2) == 0) /* I imagine this holds */ |
825 | return fl; |
829 | return fl; |
826 | else |
830 | else |
827 | return ce; |
831 | return ce; |
828 | } |
832 | } |
829 | } |
833 | } |
… | |
… | |
2928 | return cdr (slot); |
2932 | return cdr (slot); |
2929 | } |
2933 | } |
2930 | |
2934 | |
2931 | /* ========== Evaluation Cycle ========== */ |
2935 | /* ========== Evaluation Cycle ========== */ |
2932 | |
2936 | |
2933 | static pointer |
2937 | static int |
2934 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2938 | xError_1 (SCHEME_P_ const char *s, pointer a) |
2935 | { |
2939 | { |
2936 | #if USE_ERROR_HOOK |
2940 | #if USE_ERROR_HOOK |
2937 | pointer x; |
2941 | pointer x; |
2938 | pointer hdl = SCHEME_V->ERROR_HOOK; |
2942 | pointer hdl = SCHEME_V->ERROR_HOOK; |
… | |
… | |
2973 | code = cons (mk_string (SCHEME_A_ s), code); |
2977 | code = cons (mk_string (SCHEME_A_ s), code); |
2974 | setimmutable (car (code)); |
2978 | setimmutable (car (code)); |
2975 | SCHEME_V->code = cons (slot_value_in_env (x), code); |
2979 | SCHEME_V->code = cons (slot_value_in_env (x), code); |
2976 | SCHEME_V->op = OP_EVAL; |
2980 | SCHEME_V->op = OP_EVAL; |
2977 | |
2981 | |
2978 | return S_T; |
2982 | return 0; |
2979 | } |
2983 | } |
2980 | #endif |
2984 | #endif |
2981 | |
2985 | |
2982 | if (a) |
2986 | if (a) |
2983 | SCHEME_V->args = cons (a, NIL); |
2987 | SCHEME_V->args = cons (a, NIL); |
… | |
… | |
2985 | SCHEME_V->args = NIL; |
2989 | SCHEME_V->args = NIL; |
2986 | |
2990 | |
2987 | 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); |
2988 | setimmutable (car (SCHEME_V->args)); |
2992 | setimmutable (car (SCHEME_V->args)); |
2989 | SCHEME_V->op = OP_ERR0; |
2993 | SCHEME_V->op = OP_ERR0; |
|
|
2994 | |
2990 | return S_T; |
2995 | return 0; |
2991 | } |
2996 | } |
2992 | |
2997 | |
2993 | #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) |
2994 | #define Error_0(s) Error_1 (s, 0) |
2999 | #define Error_0(s) Error_1 (s, 0) |
2995 | |
3000 | |
2996 | /* Too small to turn into function */ |
3001 | /* Too small to turn into function */ |
2997 | #define BEGIN do { |
3002 | #define BEGIN do { |
2998 | #define END } while (0) |
3003 | #define END } while (0) |
2999 | #define s_goto(a) BEGIN \ |
3004 | #define s_goto(a) BEGIN \ |
3000 | SCHEME_V->op = a; \ |
3005 | SCHEME_V->op = a; \ |
3001 | return S_T; END |
3006 | return 0; END |
3002 | |
3007 | |
3003 | #define s_return(a) return xs_return (SCHEME_A_ a) |
3008 | #define s_return(a) return xs_return (SCHEME_A_ a) |
3004 | |
3009 | |
3005 | #ifndef USE_SCHEME_STACK |
3010 | #ifndef USE_SCHEME_STACK |
3006 | |
3011 | |
… | |
… | |
3031 | next_frame = SCHEME_V->dump_base + nframes; |
3036 | next_frame = SCHEME_V->dump_base + nframes; |
3032 | |
3037 | |
3033 | next_frame->op = op; |
3038 | next_frame->op = op; |
3034 | next_frame->args = args; |
3039 | next_frame->args = args; |
3035 | next_frame->envir = SCHEME_V->envir; |
3040 | next_frame->envir = SCHEME_V->envir; |
3036 | next_frame->code = code; |
3041 | next_frame->code = code; |
3037 | |
3042 | |
3038 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3043 | SCHEME_V->dump = (pointer)(uintptr_t)(nframes + 1); |
3039 | } |
3044 | } |
3040 | |
3045 | |
3041 | static pointer |
3046 | static int |
3042 | xs_return (SCHEME_P_ pointer a) |
3047 | xs_return (SCHEME_P_ pointer a) |
3043 | { |
3048 | { |
3044 | int nframes = (uintptr_t)SCHEME_V->dump; |
3049 | int nframes = (uintptr_t)SCHEME_V->dump; |
3045 | struct dump_stack_frame *frame; |
3050 | struct dump_stack_frame *frame; |
3046 | |
3051 | |
3047 | SCHEME_V->value = a; |
3052 | SCHEME_V->value = a; |
3048 | |
3053 | |
3049 | if (nframes <= 0) |
3054 | if (nframes <= 0) |
3050 | return NIL; |
3055 | return -1; |
3051 | |
3056 | |
3052 | frame = &SCHEME_V->dump_base[--nframes]; |
3057 | frame = &SCHEME_V->dump_base[--nframes]; |
3053 | SCHEME_V->op = frame->op; |
3058 | SCHEME_V->op = frame->op; |
3054 | SCHEME_V->args = frame->args; |
3059 | SCHEME_V->args = frame->args; |
3055 | SCHEME_V->envir = frame->envir; |
3060 | SCHEME_V->envir = frame->envir; |
3056 | SCHEME_V->code = frame->code; |
3061 | SCHEME_V->code = frame->code; |
3057 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3062 | SCHEME_V->dump = (pointer)(uintptr_t)nframes; |
3058 | |
3063 | |
3059 | return S_T; |
3064 | return 0; |
3060 | } |
3065 | } |
3061 | |
3066 | |
3062 | static INLINE void |
3067 | static INLINE void |
3063 | dump_stack_reset (SCHEME_P) |
3068 | dump_stack_reset (SCHEME_P) |
3064 | { |
3069 | { |
… | |
… | |
3159 | dump_stack_free (SCHEME_P) |
3164 | dump_stack_free (SCHEME_P) |
3160 | { |
3165 | { |
3161 | SCHEME_V->dump = NIL; |
3166 | SCHEME_V->dump = NIL; |
3162 | } |
3167 | } |
3163 | |
3168 | |
3164 | static pointer |
3169 | static int |
3165 | xs_return (SCHEME_P_ pointer a) |
3170 | xs_return (SCHEME_P_ pointer a) |
3166 | { |
3171 | { |
3167 | pointer dump = SCHEME_V->dump; |
3172 | pointer dump = SCHEME_V->dump; |
3168 | |
3173 | |
3169 | SCHEME_V->value = a; |
3174 | SCHEME_V->value = a; |
3170 | |
3175 | |
3171 | if (dump == NIL) |
3176 | if (dump == NIL) |
3172 | return NIL; |
3177 | return -1; |
3173 | |
3178 | |
3174 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3179 | SCHEME_V->op = ivalue (car (dump)); dump = cdr (dump); |
3175 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3180 | SCHEME_V->args = car (dump) ; dump = cdr (dump); |
3176 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3181 | SCHEME_V->envir = car (dump) ; dump = cdr (dump); |
3177 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3182 | SCHEME_V->code = car (dump) ; dump = cdr (dump); |
3178 | |
3183 | |
3179 | SCHEME_V->dump = dump; |
3184 | SCHEME_V->dump = dump; |
3180 | |
3185 | |
3181 | return S_T; |
3186 | return 0; |
3182 | } |
3187 | } |
3183 | |
3188 | |
3184 | static void |
3189 | static void |
3185 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3190 | s_save (SCHEME_P_ enum scheme_opcodes op, pointer args, pointer code) |
3186 | { |
3191 | { |
… | |
… | |
3211 | |
3216 | |
3212 | #endif |
3217 | #endif |
3213 | |
3218 | |
3214 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3219 | #define s_retbool(tf) s_return ((tf) ? S_T : S_F) |
3215 | |
3220 | |
3216 | static pointer |
3221 | static int |
3217 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3222 | opexe_0 (SCHEME_P_ enum scheme_opcodes op) |
3218 | { |
3223 | { |
|
|
3224 | pointer args = SCHEME_V->args; |
3219 | pointer x, y; |
3225 | pointer x, y; |
3220 | |
3226 | |
3221 | switch (op) |
3227 | switch (op) |
3222 | { |
3228 | { |
3223 | case OP_LOAD: /* load */ |
3229 | case OP_LOAD: /* load */ |
3224 | if (file_interactive (SCHEME_A)) |
3230 | if (file_interactive (SCHEME_A)) |
3225 | { |
3231 | { |
3226 | xwrstr ("Loading "); xwrstr (strvalue (car (SCHEME_V->args))); xwrstr ("\n"); |
3232 | xwrstr ("Loading "); xwrstr (strvalue (car (args))); xwrstr ("\n"); |
3227 | //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))); |
3228 | } |
3234 | } |
3229 | |
3235 | |
3230 | if (!file_push (SCHEME_A_ strvalue (car (SCHEME_V->args)))) |
3236 | if (!file_push (SCHEME_A_ strvalue (car (args)))) |
3231 | Error_1 ("unable to open", car (SCHEME_V->args)); |
3237 | Error_1 ("unable to open", car (args)); |
3232 | else |
3238 | else |
3233 | { |
3239 | { |
3234 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3240 | SCHEME_V->args = mk_integer (SCHEME_A_ SCHEME_V->file_i); |
3235 | s_goto (OP_T0LVL); |
3241 | s_goto (OP_T0LVL); |
3236 | } |
3242 | } |
… | |
… | |
3310 | case OP_EVAL: /* main part of evaluation */ |
3316 | case OP_EVAL: /* main part of evaluation */ |
3311 | #if USE_TRACING |
3317 | #if USE_TRACING |
3312 | if (SCHEME_V->tracing) |
3318 | if (SCHEME_V->tracing) |
3313 | { |
3319 | { |
3314 | /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ |
3320 | /*s_save(SCHEME_A_ OP_VALUEPRINT,NIL,NIL); */ |
3315 | 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); |
3316 | SCHEME_V->args = SCHEME_V->code; |
3322 | SCHEME_V->args = SCHEME_V->code; |
3317 | putstr (SCHEME_A_ "\nEval: "); |
3323 | putstr (SCHEME_A_ "\nEval: "); |
3318 | s_goto (OP_P0LIST); |
3324 | s_goto (OP_P0LIST); |
3319 | } |
3325 | } |
3320 | |
3326 | |
… | |
… | |
3364 | SCHEME_V->code = cdr (SCHEME_V->code); |
3370 | SCHEME_V->code = cdr (SCHEME_V->code); |
3365 | s_goto (OP_E1ARGS); |
3371 | s_goto (OP_E1ARGS); |
3366 | } |
3372 | } |
3367 | |
3373 | |
3368 | case OP_E1ARGS: /* eval arguments */ |
3374 | case OP_E1ARGS: /* eval arguments */ |
3369 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3375 | args = cons (SCHEME_V->value, args); |
3370 | |
3376 | |
3371 | if (is_pair (SCHEME_V->code)) /* continue */ |
3377 | if (is_pair (SCHEME_V->code)) /* continue */ |
3372 | { |
3378 | { |
3373 | 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)); |
3374 | SCHEME_V->code = car (SCHEME_V->code); |
3380 | SCHEME_V->code = car (SCHEME_V->code); |
3375 | SCHEME_V->args = NIL; |
3381 | SCHEME_V->args = NIL; |
3376 | s_goto (OP_EVAL); |
3382 | s_goto (OP_EVAL); |
3377 | } |
3383 | } |
3378 | else /* end */ |
3384 | else /* end */ |
3379 | { |
3385 | { |
3380 | SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); |
3386 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3381 | SCHEME_V->code = car (SCHEME_V->args); |
3387 | SCHEME_V->code = car (args); |
3382 | SCHEME_V->args = cdr (SCHEME_V->args); |
3388 | SCHEME_V->args = cdr (args); |
3383 | s_goto (OP_APPLY); |
3389 | s_goto (OP_APPLY); |
3384 | } |
3390 | } |
3385 | |
3391 | |
3386 | #if USE_TRACING |
3392 | #if USE_TRACING |
3387 | |
3393 | |
3388 | case OP_TRACING: |
3394 | case OP_TRACING: |
3389 | { |
3395 | { |
3390 | int tr = SCHEME_V->tracing; |
3396 | int tr = SCHEME_V->tracing; |
3391 | |
3397 | |
3392 | SCHEME_V->tracing = ivalue (car (SCHEME_V->args)); |
3398 | SCHEME_V->tracing = ivalue (car (args)); |
3393 | s_return (mk_integer (SCHEME_A_ tr)); |
3399 | s_return (mk_integer (SCHEME_A_ tr)); |
3394 | } |
3400 | } |
3395 | |
3401 | |
3396 | #endif |
3402 | #endif |
3397 | |
3403 | |
3398 | case OP_APPLY: /* apply 'code' to 'args' */ |
3404 | case OP_APPLY: /* apply 'code' to 'args' */ |
3399 | #if USE_TRACING |
3405 | #if USE_TRACING |
3400 | if (SCHEME_V->tracing) |
3406 | if (SCHEME_V->tracing) |
3401 | { |
3407 | { |
3402 | 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); |
3403 | SCHEME_V->print_flag = 1; |
3409 | SCHEME_V->print_flag = 1; |
3404 | /* SCHEME_V->args=cons(SCHEME_V->code,SCHEME_V->args); */ |
3410 | /* args=cons(SCHEME_V->code,args); */ |
3405 | putstr (SCHEME_A_ "\nApply to: "); |
3411 | putstr (SCHEME_A_ "\nApply to: "); |
3406 | s_goto (OP_P0LIST); |
3412 | s_goto (OP_P0LIST); |
3407 | } |
3413 | } |
3408 | |
3414 | |
3409 | /* fall through */ |
3415 | /* fall through */ |
3410 | |
3416 | |
3411 | case OP_REAL_APPLY: |
3417 | case OP_REAL_APPLY: |
3412 | #endif |
3418 | #endif |
3413 | if (is_proc (SCHEME_V->code)) |
3419 | if (is_proc (SCHEME_V->code)) |
3414 | { |
|
|
3415 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3420 | s_goto (procnum (SCHEME_V->code)); /* PROCEDURE */ |
3416 | } |
|
|
3417 | else if (is_foreign (SCHEME_V->code)) |
3421 | else if (is_foreign (SCHEME_V->code)) |
3418 | { |
3422 | { |
3419 | /* Keep nested calls from GC'ing the arglist */ |
3423 | /* Keep nested calls from GC'ing the arglist */ |
3420 | push_recent_alloc (SCHEME_A_ SCHEME_V->args, NIL); |
3424 | push_recent_alloc (SCHEME_A_ args, NIL); |
3421 | x = SCHEME_V->code->object.ff (SCHEME_A_ SCHEME_V->args); |
3425 | x = SCHEME_V->code->object.ff (SCHEME_A_ args); |
3422 | |
3426 | |
3423 | s_return (x); |
3427 | s_return (x); |
3424 | } |
3428 | } |
3425 | 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 */ |
3426 | { |
3430 | { |
3427 | /* Should not accept promise */ |
3431 | /* Should not accept promise */ |
3428 | /* make environment */ |
3432 | /* make environment */ |
3429 | new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); |
3433 | new_frame_in_env (SCHEME_A_ closure_env (SCHEME_V->code)); |
3430 | |
3434 | |
3431 | 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)) |
3432 | { |
3436 | { |
3433 | if (y == NIL) |
3437 | if (y == NIL) |
3434 | Error_0 ("not enough arguments"); |
3438 | Error_0 ("not enough arguments"); |
3435 | else |
3439 | else |
3436 | new_slot_in_env (SCHEME_A_ car (x), car (y)); |
3440 | new_slot_in_env (SCHEME_A_ car (x), car (y)); |
… | |
… | |
3454 | s_goto (OP_BEGIN); |
3458 | s_goto (OP_BEGIN); |
3455 | } |
3459 | } |
3456 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3460 | else if (is_continuation (SCHEME_V->code)) /* CONTINUATION */ |
3457 | { |
3461 | { |
3458 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3462 | ss_set_cont (SCHEME_A_ cont_dump (SCHEME_V->code)); |
3459 | s_return (SCHEME_V->args != NIL ? car (SCHEME_V->args) : NIL); |
3463 | s_return (args != NIL ? car (args) : NIL); |
3460 | } |
3464 | } |
3461 | else |
3465 | else |
3462 | Error_0 ("illegal function"); |
3466 | Error_0 ("illegal function"); |
3463 | |
3467 | |
3464 | case OP_DOMACRO: /* do macro */ |
3468 | case OP_DOMACRO: /* do macro */ |
… | |
… | |
3473 | { |
3477 | { |
3474 | 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); |
3475 | |
3479 | |
3476 | if (f != NIL) |
3480 | if (f != NIL) |
3477 | { |
3481 | { |
3478 | s_save (SCHEME_A_ OP_LAMBDA1, SCHEME_V->args, SCHEME_V->code); |
3482 | s_save (SCHEME_A_ OP_LAMBDA1, args, SCHEME_V->code); |
3479 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3483 | SCHEME_V->args = cons (SCHEME_V->code, NIL); |
3480 | SCHEME_V->code = slot_value_in_env (f); |
3484 | SCHEME_V->code = slot_value_in_env (f); |
3481 | s_goto (OP_APPLY); |
3485 | s_goto (OP_APPLY); |
3482 | } |
3486 | } |
3483 | |
3487 | |
… | |
… | |
3494 | 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)); |
3495 | |
3499 | |
3496 | #endif |
3500 | #endif |
3497 | |
3501 | |
3498 | case OP_MKCLOSURE: /* make-closure */ |
3502 | case OP_MKCLOSURE: /* make-closure */ |
3499 | x = car (SCHEME_V->args); |
3503 | x = car (args); |
3500 | |
3504 | |
3501 | if (car (x) == SCHEME_V->LAMBDA) |
3505 | if (car (x) == SCHEME_V->LAMBDA) |
3502 | x = cdr (x); |
3506 | x = cdr (x); |
3503 | |
3507 | |
3504 | if (cdr (SCHEME_V->args) == NIL) |
3508 | if (cdr (args) == NIL) |
3505 | y = SCHEME_V->envir; |
3509 | y = SCHEME_V->envir; |
3506 | else |
3510 | else |
3507 | y = cadr (SCHEME_V->args); |
3511 | y = cadr (args); |
3508 | |
3512 | |
3509 | s_return (mk_closure (SCHEME_A_ x, y)); |
3513 | s_return (mk_closure (SCHEME_A_ x, y)); |
3510 | |
3514 | |
3511 | case OP_QUOTE: /* quote */ |
3515 | case OP_QUOTE: /* quote */ |
3512 | s_return (car (SCHEME_V->code)); |
3516 | s_return (car (SCHEME_V->code)); |
… | |
… | |
3544 | |
3548 | |
3545 | |
3549 | |
3546 | case OP_DEFP: /* defined? */ |
3550 | case OP_DEFP: /* defined? */ |
3547 | x = SCHEME_V->envir; |
3551 | x = SCHEME_V->envir; |
3548 | |
3552 | |
3549 | if (cdr (SCHEME_V->args) != NIL) |
3553 | if (cdr (args) != NIL) |
3550 | x = cadr (SCHEME_V->args); |
3554 | x = cadr (args); |
3551 | |
3555 | |
3552 | 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); |
3553 | |
3557 | |
3554 | case OP_SET0: /* set! */ |
3558 | case OP_SET0: /* set! */ |
3555 | if (is_immutable (car (SCHEME_V->code))) |
3559 | if (is_immutable (car (SCHEME_V->code))) |
3556 | 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)); |
3557 | |
3561 | |
… | |
… | |
3588 | |
3592 | |
3589 | case OP_IF1: /* if */ |
3593 | case OP_IF1: /* if */ |
3590 | if (is_true (SCHEME_V->value)) |
3594 | if (is_true (SCHEME_V->value)) |
3591 | SCHEME_V->code = car (SCHEME_V->code); |
3595 | SCHEME_V->code = car (SCHEME_V->code); |
3592 | else |
3596 | else |
3593 | 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 */ |
3594 | |
|
|
3595 | * car(NIL) = NIL */ |
|
|
3596 | s_goto (OP_EVAL); |
3598 | s_goto (OP_EVAL); |
3597 | |
3599 | |
3598 | case OP_LET0: /* let */ |
3600 | case OP_LET0: /* let */ |
3599 | SCHEME_V->args = NIL; |
3601 | SCHEME_V->args = NIL; |
3600 | SCHEME_V->value = SCHEME_V->code; |
3602 | SCHEME_V->value = SCHEME_V->code; |
3601 | 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); |
3602 | s_goto (OP_LET1); |
3604 | s_goto (OP_LET1); |
3603 | |
3605 | |
3604 | case OP_LET1: /* let (calculate parameters) */ |
3606 | case OP_LET1: /* let (calculate parameters) */ |
3605 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3607 | args = cons (SCHEME_V->value, args); |
3606 | |
3608 | |
3607 | if (is_pair (SCHEME_V->code)) /* continue */ |
3609 | if (is_pair (SCHEME_V->code)) /* continue */ |
3608 | { |
3610 | { |
3609 | 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))) |
3610 | 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)); |
3611 | |
3613 | |
3612 | 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)); |
3613 | SCHEME_V->code = cadar (SCHEME_V->code); |
3615 | SCHEME_V->code = cadar (SCHEME_V->code); |
3614 | SCHEME_V->args = NIL; |
3616 | SCHEME_V->args = NIL; |
3615 | s_goto (OP_EVAL); |
3617 | s_goto (OP_EVAL); |
3616 | } |
3618 | } |
3617 | else /* end */ |
3619 | else /* end */ |
3618 | { |
3620 | { |
3619 | SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); |
3621 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3620 | SCHEME_V->code = car (SCHEME_V->args); |
3622 | SCHEME_V->code = car (args); |
3621 | SCHEME_V->args = cdr (SCHEME_V->args); |
3623 | SCHEME_V->args = cdr (args); |
3622 | s_goto (OP_LET2); |
3624 | s_goto (OP_LET2); |
3623 | } |
3625 | } |
3624 | |
3626 | |
3625 | case OP_LET2: /* let */ |
3627 | case OP_LET2: /* let */ |
3626 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3628 | new_frame_in_env (SCHEME_A_ SCHEME_V->envir); |
3627 | |
3629 | |
3628 | 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; |
3629 | y != NIL; x = cdr (x), y = cdr (y)) |
3631 | y != NIL; x = cdr (x), y = cdr (y)) |
3630 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3632 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3631 | |
3633 | |
3632 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3634 | if (is_symbol (car (SCHEME_V->code))) /* named let */ |
3633 | { |
3635 | { |
3634 | 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)) |
3635 | { |
3637 | { |
3636 | if (!is_pair (x)) |
3638 | if (!is_pair (x)) |
3637 | Error_1 ("Bad syntax of binding in let :", x); |
3639 | Error_1 ("Bad syntax of binding in let :", x); |
3638 | |
3640 | |
3639 | if (!is_list (SCHEME_A_ car (x))) |
3641 | if (!is_list (SCHEME_A_ car (x))) |
3640 | Error_1 ("Bad syntax of binding in let :", car (x)); |
3642 | Error_1 ("Bad syntax of binding in let :", car (x)); |
3641 | |
3643 | |
3642 | SCHEME_V->args = cons (caar (x), SCHEME_V->args); |
3644 | args = cons (caar (x), args); |
3643 | } |
3645 | } |
3644 | |
3646 | |
3645 | x = |
|
|
3646 | 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)), |
3647 | SCHEME_V->envir); |
3648 | SCHEME_V->envir); |
3648 | new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); |
3649 | new_slot_in_env (SCHEME_A_ car (SCHEME_V->code), x); |
3649 | SCHEME_V->code = cddr (SCHEME_V->code); |
3650 | SCHEME_V->code = cddr (SCHEME_V->code); |
3650 | SCHEME_V->args = NIL; |
|
|
3651 | } |
3651 | } |
3652 | else |
3652 | else |
3653 | { |
3653 | { |
3654 | SCHEME_V->code = cdr (SCHEME_V->code); |
3654 | SCHEME_V->code = cdr (SCHEME_V->code); |
|
|
3655 | } |
|
|
3656 | |
3655 | SCHEME_V->args = NIL; |
3657 | SCHEME_V->args = NIL; |
3656 | } |
|
|
3657 | |
|
|
3658 | s_goto (OP_BEGIN); |
3658 | s_goto (OP_BEGIN); |
3659 | |
3659 | |
3660 | case OP_LET0AST: /* let* */ |
3660 | case OP_LET0AST: /* let* */ |
3661 | if (car (SCHEME_V->code) == NIL) |
3661 | if (car (SCHEME_V->code) == NIL) |
3662 | { |
3662 | { |
… | |
… | |
3680 | 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); |
3681 | SCHEME_V->code = cdr (SCHEME_V->code); |
3681 | SCHEME_V->code = cdr (SCHEME_V->code); |
3682 | |
3682 | |
3683 | if (is_pair (SCHEME_V->code)) /* continue */ |
3683 | if (is_pair (SCHEME_V->code)) /* continue */ |
3684 | { |
3684 | { |
3685 | s_save (SCHEME_A_ OP_LET2AST, SCHEME_V->args, SCHEME_V->code); |
3685 | s_save (SCHEME_A_ OP_LET2AST, args, SCHEME_V->code); |
3686 | SCHEME_V->code = cadar (SCHEME_V->code); |
3686 | SCHEME_V->code = cadar (SCHEME_V->code); |
3687 | SCHEME_V->args = NIL; |
3687 | SCHEME_V->args = NIL; |
3688 | s_goto (OP_EVAL); |
3688 | s_goto (OP_EVAL); |
3689 | } |
3689 | } |
3690 | else /* end */ |
3690 | else /* end */ |
3691 | { |
3691 | { |
3692 | SCHEME_V->code = SCHEME_V->args; |
3692 | SCHEME_V->code = args; |
3693 | SCHEME_V->args = NIL; |
3693 | SCHEME_V->args = NIL; |
3694 | s_goto (OP_BEGIN); |
3694 | s_goto (OP_BEGIN); |
3695 | } |
3695 | } |
3696 | |
3696 | |
3697 | case OP_LET0REC: /* letrec */ |
3697 | case OP_LET0REC: /* letrec */ |
… | |
… | |
3700 | SCHEME_V->value = SCHEME_V->code; |
3700 | SCHEME_V->value = SCHEME_V->code; |
3701 | SCHEME_V->code = car (SCHEME_V->code); |
3701 | SCHEME_V->code = car (SCHEME_V->code); |
3702 | s_goto (OP_LET1REC); |
3702 | s_goto (OP_LET1REC); |
3703 | |
3703 | |
3704 | case OP_LET1REC: /* letrec (calculate parameters) */ |
3704 | case OP_LET1REC: /* letrec (calculate parameters) */ |
3705 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
3705 | args = cons (SCHEME_V->value, args); |
3706 | |
3706 | |
3707 | if (is_pair (SCHEME_V->code)) /* continue */ |
3707 | if (is_pair (SCHEME_V->code)) /* continue */ |
3708 | { |
3708 | { |
3709 | 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))) |
3710 | 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)); |
3711 | |
3711 | |
3712 | 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)); |
3713 | SCHEME_V->code = cadar (SCHEME_V->code); |
3713 | SCHEME_V->code = cadar (SCHEME_V->code); |
3714 | SCHEME_V->args = NIL; |
3714 | SCHEME_V->args = NIL; |
3715 | s_goto (OP_EVAL); |
3715 | s_goto (OP_EVAL); |
3716 | } |
3716 | } |
3717 | else /* end */ |
3717 | else /* end */ |
3718 | { |
3718 | { |
3719 | SCHEME_V->args = reverse_in_place (SCHEME_A_ NIL, SCHEME_V->args); |
3719 | args = reverse_in_place (SCHEME_A_ NIL, args); |
3720 | SCHEME_V->code = car (SCHEME_V->args); |
3720 | SCHEME_V->code = car (args); |
3721 | SCHEME_V->args = cdr (SCHEME_V->args); |
3721 | SCHEME_V->args = cdr (args); |
3722 | s_goto (OP_LET2REC); |
3722 | s_goto (OP_LET2REC); |
3723 | } |
3723 | } |
3724 | |
3724 | |
3725 | case OP_LET2REC: /* letrec */ |
3725 | case OP_LET2REC: /* letrec */ |
3726 | 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)) |
3727 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3727 | new_slot_in_env (SCHEME_A_ caar (x), car (y)); |
3728 | |
3728 | |
3729 | SCHEME_V->code = cdr (SCHEME_V->code); |
3729 | SCHEME_V->code = cdr (SCHEME_V->code); |
3730 | SCHEME_V->args = NIL; |
3730 | SCHEME_V->args = NIL; |
3731 | s_goto (OP_BEGIN); |
3731 | s_goto (OP_BEGIN); |
… | |
… | |
3817 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3817 | s_save (SCHEME_A_ OP_C1STREAM, NIL, cdr (SCHEME_V->code)); |
3818 | SCHEME_V->code = car (SCHEME_V->code); |
3818 | SCHEME_V->code = car (SCHEME_V->code); |
3819 | s_goto (OP_EVAL); |
3819 | s_goto (OP_EVAL); |
3820 | |
3820 | |
3821 | case OP_C1STREAM: /* cons-stream */ |
3821 | case OP_C1STREAM: /* cons-stream */ |
3822 | 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 */ |
3823 | 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); |
3824 | set_typeflag (x, T_PROMISE); |
3824 | set_typeflag (x, T_PROMISE); |
3825 | s_return (cons (SCHEME_V->args, x)); |
3825 | s_return (cons (args, x)); |
3826 | |
3826 | |
3827 | case OP_MACRO0: /* macro */ |
3827 | case OP_MACRO0: /* macro */ |
3828 | if (is_pair (car (SCHEME_V->code))) |
3828 | if (is_pair (car (SCHEME_V->code))) |
3829 | { |
3829 | { |
3830 | x = caar (SCHEME_V->code); |
3830 | x = caar (SCHEME_V->code); |
… | |
… | |
3863 | { |
3863 | { |
3864 | if (!is_pair (y = caar (x))) |
3864 | if (!is_pair (y = caar (x))) |
3865 | break; |
3865 | break; |
3866 | |
3866 | |
3867 | for (; y != NIL; y = cdr (y)) |
3867 | for (; y != NIL; y = cdr (y)) |
3868 | { |
|
|
3869 | if (eqv (car (y), SCHEME_V->value)) |
3868 | if (eqv (car (y), SCHEME_V->value)) |
3870 | break; |
3869 | break; |
3871 | } |
|
|
3872 | |
3870 | |
3873 | if (y != NIL) |
3871 | if (y != NIL) |
3874 | break; |
3872 | break; |
3875 | } |
3873 | } |
3876 | |
3874 | |
… | |
… | |
3896 | s_goto (OP_BEGIN); |
3894 | s_goto (OP_BEGIN); |
3897 | else |
3895 | else |
3898 | s_return (NIL); |
3896 | s_return (NIL); |
3899 | |
3897 | |
3900 | case OP_PAPPLY: /* apply */ |
3898 | case OP_PAPPLY: /* apply */ |
3901 | SCHEME_V->code = car (SCHEME_V->args); |
3899 | SCHEME_V->code = car (args); |
3902 | SCHEME_V->args = list_star (SCHEME_A_ cdr (SCHEME_V->args)); |
3900 | SCHEME_V->args = list_star (SCHEME_A_ cdr (args)); |
3903 | /*SCHEME_V->args = cadr(SCHEME_V->args); */ |
3901 | /*SCHEME_V->args = cadr(args); */ |
3904 | s_goto (OP_APPLY); |
3902 | s_goto (OP_APPLY); |
3905 | |
3903 | |
3906 | case OP_PEVAL: /* eval */ |
3904 | case OP_PEVAL: /* eval */ |
3907 | if (cdr (SCHEME_V->args) != NIL) |
3905 | if (cdr (args) != NIL) |
3908 | SCHEME_V->envir = cadr (SCHEME_V->args); |
3906 | SCHEME_V->envir = cadr (args); |
3909 | |
3907 | |
3910 | SCHEME_V->code = car (SCHEME_V->args); |
3908 | SCHEME_V->code = car (args); |
3911 | s_goto (OP_EVAL); |
3909 | s_goto (OP_EVAL); |
3912 | |
3910 | |
3913 | case OP_CONTINUATION: /* call-with-current-continuation */ |
3911 | case OP_CONTINUATION: /* call-with-current-continuation */ |
3914 | SCHEME_V->code = car (SCHEME_V->args); |
3912 | SCHEME_V->code = car (args); |
3915 | 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); |
3916 | s_goto (OP_APPLY); |
3914 | s_goto (OP_APPLY); |
3917 | } |
3915 | } |
3918 | |
3916 | |
3919 | abort (); |
3917 | abort (); |
3920 | } |
3918 | } |
3921 | |
3919 | |
3922 | static pointer |
3920 | static int |
3923 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
3921 | opexe_1 (SCHEME_P_ enum scheme_opcodes op) |
3924 | { |
3922 | { |
3925 | pointer x; |
3923 | pointer args = SCHEME_V->args; |
|
|
3924 | pointer x = car (args); |
3926 | num v; |
3925 | num v; |
3927 | |
3926 | |
3928 | #if USE_MATH |
3927 | #if USE_MATH |
3929 | RVALUE dd; |
3928 | RVALUE dd; |
3930 | #endif |
3929 | #endif |
3931 | |
3930 | |
3932 | switch (op) |
3931 | switch (op) |
3933 | { |
3932 | { |
3934 | #if USE_MATH |
3933 | #if USE_MATH |
3935 | |
|
|
3936 | case OP_INEX2EX: /* inexact->exact */ |
3934 | case OP_INEX2EX: /* inexact->exact */ |
3937 | x = car (SCHEME_V->args); |
|
|
3938 | |
|
|
3939 | if (num_is_integer (x)) |
3935 | if (num_is_integer (x)) |
3940 | s_return (x); |
3936 | s_return (x); |
3941 | else if (modf (rvalue_unchecked (x), &dd) == 0.0) |
3937 | else if (modf (rvalue_unchecked (x), &dd) == 0) |
3942 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
3938 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
3943 | else |
3939 | else |
3944 | Error_1 ("inexact->exact: not integral:", x); |
3940 | Error_1 ("inexact->exact: not integral:", x); |
3945 | |
3941 | |
3946 | case OP_EXP: |
|
|
3947 | x = car (SCHEME_V->args); |
|
|
3948 | s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
3942 | case OP_EXP: s_return (mk_real (SCHEME_A_ exp (rvalue (x)))); |
3949 | |
|
|
3950 | case OP_LOG: |
|
|
3951 | x = car (SCHEME_V->args); |
|
|
3952 | s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
3943 | case OP_LOG: s_return (mk_real (SCHEME_A_ log (rvalue (x)))); |
3953 | |
|
|
3954 | case OP_SIN: |
|
|
3955 | x = car (SCHEME_V->args); |
|
|
3956 | s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
3944 | case OP_SIN: s_return (mk_real (SCHEME_A_ sin (rvalue (x)))); |
3957 | |
|
|
3958 | case OP_COS: |
|
|
3959 | x = car (SCHEME_V->args); |
|
|
3960 | s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
3945 | case OP_COS: s_return (mk_real (SCHEME_A_ cos (rvalue (x)))); |
3961 | |
|
|
3962 | case OP_TAN: |
|
|
3963 | x = car (SCHEME_V->args); |
|
|
3964 | s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
3946 | case OP_TAN: s_return (mk_real (SCHEME_A_ tan (rvalue (x)))); |
3965 | |
|
|
3966 | case OP_ASIN: |
|
|
3967 | x = car (SCHEME_V->args); |
|
|
3968 | s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
3947 | case OP_ASIN: s_return (mk_real (SCHEME_A_ asin (rvalue (x)))); |
3969 | |
|
|
3970 | case OP_ACOS: |
|
|
3971 | x = car (SCHEME_V->args); |
|
|
3972 | s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3948 | case OP_ACOS: s_return (mk_real (SCHEME_A_ acos (rvalue (x)))); |
3973 | |
3949 | |
3974 | case OP_ATAN: |
3950 | case OP_ATAN: |
3975 | x = car (SCHEME_V->args); |
|
|
3976 | |
|
|
3977 | if (cdr (SCHEME_V->args) == NIL) |
3951 | if (cdr (args) == NIL) |
3978 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
3952 | s_return (mk_real (SCHEME_A_ atan (rvalue (x)))); |
3979 | else |
3953 | else |
3980 | { |
3954 | { |
3981 | pointer y = cadr (SCHEME_V->args); |
3955 | pointer y = cadr (args); |
3982 | |
|
|
3983 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
3956 | s_return (mk_real (SCHEME_A_ atan2 (rvalue (x), rvalue (y)))); |
3984 | } |
3957 | } |
3985 | |
3958 | |
3986 | case OP_SQRT: |
3959 | case OP_SQRT: |
3987 | x = car (SCHEME_V->args); |
|
|
3988 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
3960 | s_return (mk_real (SCHEME_A_ sqrt (rvalue (x)))); |
3989 | |
3961 | |
3990 | case OP_EXPT: |
3962 | case OP_EXPT: |
3991 | { |
3963 | { |
3992 | RVALUE result; |
3964 | RVALUE result; |
3993 | int real_result = 1; |
3965 | int real_result = 1; |
3994 | pointer y = cadr (SCHEME_V->args); |
3966 | pointer y = cadr (args); |
3995 | |
|
|
3996 | x = car (SCHEME_V->args); |
|
|
3997 | |
3967 | |
3998 | if (num_is_integer (x) && num_is_integer (y)) |
3968 | if (num_is_integer (x) && num_is_integer (y)) |
3999 | real_result = 0; |
3969 | real_result = 0; |
4000 | |
3970 | |
4001 | /* This 'if' is an R5RS compatibility fix. */ |
3971 | /* This 'if' is an R5RS compatibility fix. */ |
4002 | /* NOTE: Remove this 'if' fix for R6RS. */ |
3972 | /* NOTE: Remove this 'if' fix for R6RS. */ |
4003 | if (rvalue (x) == 0 && rvalue (y) < 0) |
3973 | if (rvalue (x) == 0 && rvalue (y) < 0) |
4004 | result = 0.0; |
3974 | result = 0; |
4005 | else |
3975 | else |
4006 | result = pow (rvalue (x), rvalue (y)); |
3976 | result = pow (rvalue (x), rvalue (y)); |
4007 | |
3977 | |
4008 | /* Before returning integer result make sure we can. */ |
3978 | /* Before returning integer result make sure we can. */ |
4009 | /* If the test fails, result is too big for integer. */ |
3979 | /* If the test fails, result is too big for integer. */ |
4010 | if (!real_result) |
3980 | if (!real_result) |
4011 | { |
3981 | { |
4012 | long result_as_long = (long) result; |
3982 | long result_as_long = result; |
4013 | |
3983 | |
4014 | if (result != (RVALUE) result_as_long) |
3984 | if (result != (RVALUE) result_as_long) |
4015 | real_result = 1; |
3985 | real_result = 1; |
4016 | } |
3986 | } |
4017 | |
3987 | |
… | |
… | |
4019 | s_return (mk_real (SCHEME_A_ result)); |
3989 | s_return (mk_real (SCHEME_A_ result)); |
4020 | else |
3990 | else |
4021 | s_return (mk_integer (SCHEME_A_ result)); |
3991 | s_return (mk_integer (SCHEME_A_ result)); |
4022 | } |
3992 | } |
4023 | |
3993 | |
4024 | case OP_FLOOR: |
|
|
4025 | x = car (SCHEME_V->args); |
|
|
4026 | s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
3994 | case OP_FLOOR: s_return (mk_real (SCHEME_A_ floor (rvalue (x)))); |
4027 | |
|
|
4028 | case OP_CEILING: |
|
|
4029 | x = car (SCHEME_V->args); |
|
|
4030 | s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
3995 | case OP_CEILING: s_return (mk_real (SCHEME_A_ ceil (rvalue (x)))); |
4031 | |
3996 | |
4032 | case OP_TRUNCATE: |
3997 | case OP_TRUNCATE: |
4033 | { |
3998 | { |
4034 | RVALUE rvalue_of_x; |
3999 | RVALUE rvalue_of_x; |
4035 | |
4000 | |
4036 | x = car (SCHEME_V->args); |
|
|
4037 | rvalue_of_x = rvalue (x); |
4001 | rvalue_of_x = rvalue (x); |
4038 | |
4002 | |
4039 | if (rvalue_of_x > 0) |
4003 | if (rvalue_of_x > 0) |
4040 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
4004 | s_return (mk_real (SCHEME_A_ floor (rvalue_of_x))); |
4041 | else |
4005 | else |
4042 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
4006 | s_return (mk_real (SCHEME_A_ ceil (rvalue_of_x))); |
4043 | } |
4007 | } |
4044 | |
4008 | |
4045 | case OP_ROUND: |
4009 | case OP_ROUND: |
4046 | x = car (SCHEME_V->args); |
|
|
4047 | |
|
|
4048 | if (num_is_integer (x)) |
4010 | if (num_is_integer (x)) |
4049 | s_return (x); |
4011 | s_return (x); |
4050 | |
4012 | |
4051 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
4013 | s_return (mk_real (SCHEME_A_ round_per_R5RS (rvalue (x)))); |
4052 | #endif |
4014 | #endif |
4053 | |
4015 | |
4054 | case OP_ADD: /* + */ |
4016 | case OP_ADD: /* + */ |
4055 | v = num_zero; |
4017 | v = num_zero; |
4056 | |
4018 | |
4057 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4019 | for (x = args; x != NIL; x = cdr (x)) |
4058 | v = num_op ('+', v, nvalue (car (x))); |
4020 | v = num_op ('+', v, nvalue (car (x))); |
4059 | |
4021 | |
4060 | s_return (mk_number (SCHEME_A_ v)); |
4022 | s_return (mk_number (SCHEME_A_ v)); |
4061 | |
4023 | |
4062 | case OP_MUL: /* * */ |
4024 | case OP_MUL: /* * */ |
4063 | v = num_one; |
4025 | v = num_one; |
4064 | |
4026 | |
4065 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4027 | for (x = args; x != NIL; x = cdr (x)) |
4066 | v = num_op ('+', v, nvalue (car (x))); |
4028 | v = num_op ('*', v, nvalue (car (x))); |
4067 | |
4029 | |
4068 | s_return (mk_number (SCHEME_A_ v)); |
4030 | s_return (mk_number (SCHEME_A_ v)); |
4069 | |
4031 | |
4070 | case OP_SUB: /* - */ |
4032 | case OP_SUB: /* - */ |
4071 | if (cdr (SCHEME_V->args) == NIL) |
4033 | if (cdr (args) == NIL) |
4072 | { |
4034 | { |
4073 | x = SCHEME_V->args; |
4035 | x = args; |
4074 | v = num_zero; |
4036 | v = num_zero; |
4075 | } |
4037 | } |
4076 | else |
4038 | else |
4077 | { |
4039 | { |
4078 | x = cdr (SCHEME_V->args); |
4040 | x = cdr (args); |
4079 | v = nvalue (car (SCHEME_V->args)); |
4041 | v = nvalue (car (args)); |
4080 | } |
4042 | } |
4081 | |
4043 | |
4082 | for (; x != NIL; x = cdr (x)) |
4044 | for (; x != NIL; x = cdr (x)) |
4083 | v = num_op ('+', v, nvalue (car (x))); |
4045 | v = num_op ('-', v, nvalue (car (x))); |
4084 | |
4046 | |
4085 | s_return (mk_number (SCHEME_A_ v)); |
4047 | s_return (mk_number (SCHEME_A_ v)); |
4086 | |
4048 | |
4087 | case OP_DIV: /* / */ |
4049 | case OP_DIV: /* / */ |
4088 | if (cdr (SCHEME_V->args) == NIL) |
4050 | if (cdr (args) == NIL) |
4089 | { |
4051 | { |
4090 | x = SCHEME_V->args; |
4052 | x = args; |
4091 | v = num_one; |
4053 | v = num_one; |
4092 | } |
4054 | } |
4093 | else |
4055 | else |
4094 | { |
4056 | { |
4095 | x = cdr (SCHEME_V->args); |
4057 | x = cdr (args); |
4096 | v = nvalue (car (SCHEME_V->args)); |
4058 | v = nvalue (car (args)); |
4097 | } |
4059 | } |
4098 | |
4060 | |
4099 | for (; x != NIL; x = cdr (x)) |
4061 | for (; x != NIL; x = cdr (x)) |
4100 | { |
4062 | { |
4101 | if (!is_zero_rvalue (rvalue (car (x)))) |
4063 | if (!is_zero_rvalue (rvalue (car (x)))) |
… | |
… | |
4105 | } |
4067 | } |
4106 | |
4068 | |
4107 | s_return (mk_number (SCHEME_A_ v)); |
4069 | s_return (mk_number (SCHEME_A_ v)); |
4108 | |
4070 | |
4109 | case OP_INTDIV: /* quotient */ |
4071 | case OP_INTDIV: /* quotient */ |
4110 | if (cdr (SCHEME_V->args) == NIL) |
4072 | if (cdr (args) == NIL) |
4111 | { |
4073 | { |
4112 | x = SCHEME_V->args; |
4074 | x = args; |
4113 | v = num_one; |
4075 | v = num_one; |
4114 | } |
4076 | } |
4115 | else |
4077 | else |
4116 | { |
4078 | { |
4117 | x = cdr (SCHEME_V->args); |
4079 | x = cdr (args); |
4118 | v = nvalue (car (SCHEME_V->args)); |
4080 | v = nvalue (car (args)); |
4119 | } |
4081 | } |
4120 | |
4082 | |
4121 | for (; x != NIL; x = cdr (x)) |
4083 | for (; x != NIL; x = cdr (x)) |
4122 | { |
4084 | { |
4123 | if (ivalue (car (x)) != 0) |
4085 | if (ivalue (car (x)) != 0) |
… | |
… | |
4127 | } |
4089 | } |
4128 | |
4090 | |
4129 | s_return (mk_number (SCHEME_A_ v)); |
4091 | s_return (mk_number (SCHEME_A_ v)); |
4130 | |
4092 | |
4131 | case OP_REM: /* remainder */ |
4093 | case OP_REM: /* remainder */ |
4132 | v = nvalue (car (SCHEME_V->args)); |
4094 | v = nvalue (x); |
4133 | |
4095 | |
4134 | if (ivalue (cadr (SCHEME_V->args)) != 0) |
4096 | if (ivalue (cadr (args)) != 0) |
4135 | v = num_rem (v, nvalue (cadr (SCHEME_V->args))); |
4097 | v = num_rem (v, nvalue (cadr (args))); |
4136 | else |
4098 | else |
4137 | Error_0 ("remainder: division by zero"); |
4099 | Error_0 ("remainder: division by zero"); |
4138 | |
4100 | |
4139 | s_return (mk_number (SCHEME_A_ v)); |
4101 | s_return (mk_number (SCHEME_A_ v)); |
4140 | |
4102 | |
4141 | case OP_MOD: /* modulo */ |
4103 | case OP_MOD: /* modulo */ |
4142 | v = nvalue (car (SCHEME_V->args)); |
4104 | v = nvalue (x); |
4143 | |
4105 | |
4144 | if (ivalue (cadr (SCHEME_V->args)) != 0) |
4106 | if (ivalue (cadr (args)) != 0) |
4145 | v = num_mod (v, nvalue (cadr (SCHEME_V->args))); |
4107 | v = num_mod (v, nvalue (cadr (args))); |
4146 | else |
4108 | else |
4147 | Error_0 ("modulo: division by zero"); |
4109 | Error_0 ("modulo: division by zero"); |
4148 | |
4110 | |
4149 | s_return (mk_number (SCHEME_A_ v)); |
4111 | s_return (mk_number (SCHEME_A_ v)); |
4150 | |
4112 | |
4151 | case OP_CAR: /* car */ |
4113 | case OP_CAR: /* car */ |
4152 | s_return (caar (SCHEME_V->args)); |
4114 | s_return (caar (args)); |
4153 | |
4115 | |
4154 | case OP_CDR: /* cdr */ |
4116 | case OP_CDR: /* cdr */ |
4155 | s_return (cdar (SCHEME_V->args)); |
4117 | s_return (cdar (args)); |
4156 | |
4118 | |
4157 | case OP_CONS: /* cons */ |
4119 | case OP_CONS: /* cons */ |
4158 | set_cdr (SCHEME_V->args, cadr (SCHEME_V->args)); |
4120 | set_cdr (args, cadr (args)); |
4159 | s_return (SCHEME_V->args); |
4121 | s_return (args); |
4160 | |
4122 | |
4161 | case OP_SETCAR: /* set-car! */ |
4123 | case OP_SETCAR: /* set-car! */ |
4162 | if (!is_immutable (car (SCHEME_V->args))) |
4124 | if (!is_immutable (x)) |
4163 | { |
4125 | { |
4164 | set_car (car (SCHEME_V->args), cadr (SCHEME_V->args)); |
4126 | set_car (x, cadr (args)); |
4165 | s_return (car (SCHEME_V->args)); |
4127 | s_return (car (args)); |
4166 | } |
4128 | } |
4167 | else |
4129 | else |
4168 | Error_0 ("set-car!: unable to alter immutable pair"); |
4130 | Error_0 ("set-car!: unable to alter immutable pair"); |
4169 | |
4131 | |
4170 | case OP_SETCDR: /* set-cdr! */ |
4132 | case OP_SETCDR: /* set-cdr! */ |
4171 | if (!is_immutable (car (SCHEME_V->args))) |
4133 | if (!is_immutable (x)) |
4172 | { |
4134 | { |
4173 | set_cdr (car (SCHEME_V->args), cadr (SCHEME_V->args)); |
4135 | set_cdr (x, cadr (args)); |
4174 | s_return (car (SCHEME_V->args)); |
4136 | s_return (car (args)); |
4175 | } |
4137 | } |
4176 | else |
4138 | else |
4177 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4139 | Error_0 ("set-cdr!: unable to alter immutable pair"); |
4178 | |
4140 | |
4179 | case OP_CHAR2INT: /* char->integer */ |
4141 | case OP_CHAR2INT: /* char->integer */ |
4180 | s_return (mk_integer (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4142 | s_return (mk_integer (SCHEME_A_ ivalue (x))); |
4181 | |
4143 | |
4182 | case OP_INT2CHAR: /* integer->char */ |
4144 | case OP_INT2CHAR: /* integer->char */ |
4183 | s_return (mk_character (SCHEME_A_ ivalue (car (SCHEME_V->args)))); |
4145 | s_return (mk_character (SCHEME_A_ ivalue (x))); |
4184 | |
4146 | |
4185 | case OP_CHARUPCASE: |
4147 | case OP_CHARUPCASE: |
4186 | { |
4148 | { |
4187 | unsigned char c = ivalue (car (SCHEME_V->args)); |
4149 | unsigned char c = ivalue (x); |
4188 | c = toupper (c); |
4150 | c = toupper (c); |
4189 | s_return (mk_character (SCHEME_A_ c)); |
4151 | s_return (mk_character (SCHEME_A_ c)); |
4190 | } |
4152 | } |
4191 | |
4153 | |
4192 | case OP_CHARDNCASE: |
4154 | case OP_CHARDNCASE: |
4193 | { |
4155 | { |
4194 | unsigned char c = ivalue (car (SCHEME_V->args)); |
4156 | unsigned char c = ivalue (x); |
4195 | c = tolower (c); |
4157 | c = tolower (c); |
4196 | s_return (mk_character (SCHEME_A_ c)); |
4158 | s_return (mk_character (SCHEME_A_ c)); |
4197 | } |
4159 | } |
4198 | |
4160 | |
4199 | case OP_STR2SYM: /* string->symbol */ |
4161 | case OP_STR2SYM: /* string->symbol */ |
4200 | s_return (mk_symbol (SCHEME_A_ strvalue (car (SCHEME_V->args)))); |
4162 | s_return (mk_symbol (SCHEME_A_ strvalue (x))); |
4201 | |
4163 | |
4202 | case OP_STR2ATOM: /* string->atom */ |
4164 | case OP_STR2ATOM: /* string->atom */ |
4203 | { |
4165 | { |
4204 | char *s = strvalue (car (SCHEME_V->args)); |
4166 | char *s = strvalue (x); |
4205 | long pf = 0; |
4167 | long pf = 0; |
4206 | |
4168 | |
4207 | if (cdr (SCHEME_V->args) != NIL) |
4169 | if (cdr (args) != NIL) |
4208 | { |
4170 | { |
4209 | /* we know cadr(SCHEME_V->args) is a natural number */ |
4171 | /* we know cadr(args) is a natural number */ |
4210 | /* see if it is 2, 8, 10, or 16, or error */ |
4172 | /* see if it is 2, 8, 10, or 16, or error */ |
4211 | pf = ivalue_unchecked (cadr (SCHEME_V->args)); |
4173 | pf = ivalue_unchecked (cadr (args)); |
4212 | |
4174 | |
4213 | if (pf == 16 || pf == 10 || pf == 8 || pf == 2) |
4175 | if (pf == 16 || pf == 10 || pf == 8 || pf == 2) |
4214 | { |
4176 | { |
4215 | /* base is OK */ |
4177 | /* base is OK */ |
4216 | } |
4178 | } |
4217 | else |
4179 | else |
4218 | pf = -1; |
4180 | pf = -1; |
4219 | } |
4181 | } |
4220 | |
4182 | |
4221 | if (pf < 0) |
4183 | if (pf < 0) |
4222 | Error_1 ("string->atom: bad base:", cadr (SCHEME_V->args)); |
4184 | Error_1 ("string->atom: bad base:", cadr (args)); |
4223 | else if (*s == '#') /* no use of base! */ |
4185 | else if (*s == '#') /* no use of base! */ |
4224 | s_return (mk_sharp_const (SCHEME_A_ s + 1)); |
4186 | s_return (mk_sharp_const (SCHEME_A_ s + 1)); |
4225 | else |
4187 | else |
4226 | { |
4188 | { |
4227 | if (pf == 0 || pf == 10) |
4189 | if (pf == 0 || pf == 10) |
… | |
… | |
4238 | } |
4200 | } |
4239 | } |
4201 | } |
4240 | } |
4202 | } |
4241 | |
4203 | |
4242 | case OP_SYM2STR: /* symbol->string */ |
4204 | case OP_SYM2STR: /* symbol->string */ |
4243 | x = mk_string (SCHEME_A_ symname (car (SCHEME_V->args))); |
4205 | x = mk_string (SCHEME_A_ symname (x)); |
4244 | setimmutable (x); |
4206 | setimmutable (x); |
4245 | s_return (x); |
4207 | s_return (x); |
4246 | |
4208 | |
4247 | case OP_ATOM2STR: /* atom->string */ |
4209 | case OP_ATOM2STR: /* atom->string */ |
4248 | { |
4210 | { |
4249 | long pf = 0; |
4211 | long pf = 0; |
4250 | |
4212 | |
4251 | x = car (SCHEME_V->args); |
|
|
4252 | |
|
|
4253 | if (cdr (SCHEME_V->args) != NIL) |
4213 | if (cdr (args) != NIL) |
4254 | { |
4214 | { |
4255 | /* we know cadr(SCHEME_V->args) is a natural number */ |
4215 | /* we know cadr(args) is a natural number */ |
4256 | /* see if it is 2, 8, 10, or 16, or error */ |
4216 | /* see if it is 2, 8, 10, or 16, or error */ |
4257 | pf = ivalue_unchecked (cadr (SCHEME_V->args)); |
4217 | pf = ivalue_unchecked (cadr (args)); |
4258 | |
4218 | |
4259 | 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)) |
4260 | { |
4220 | { |
4261 | /* base is OK */ |
4221 | /* base is OK */ |
4262 | } |
4222 | } |
4263 | else |
4223 | else |
4264 | pf = -1; |
4224 | pf = -1; |
4265 | } |
4225 | } |
4266 | |
4226 | |
4267 | if (pf < 0) |
4227 | if (pf < 0) |
4268 | Error_1 ("atom->string: bad base:", cadr (SCHEME_V->args)); |
4228 | Error_1 ("atom->string: bad base:", cadr (args)); |
4269 | 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)) |
4270 | { |
4230 | { |
4271 | char *p; |
4231 | char *p; |
4272 | int len; |
4232 | int len; |
4273 | |
4233 | |
… | |
… | |
4281 | case OP_MKSTRING: /* make-string */ |
4241 | case OP_MKSTRING: /* make-string */ |
4282 | { |
4242 | { |
4283 | int fill = ' '; |
4243 | int fill = ' '; |
4284 | int len; |
4244 | int len; |
4285 | |
4245 | |
4286 | len = ivalue (car (SCHEME_V->args)); |
4246 | len = ivalue (x); |
4287 | |
4247 | |
4288 | if (cdr (SCHEME_V->args) != NIL) |
4248 | if (cdr (args) != NIL) |
4289 | fill = charvalue (cadr (SCHEME_V->args)); |
4249 | fill = charvalue (cadr (args)); |
4290 | |
4250 | |
4291 | s_return (mk_empty_string (SCHEME_A_ len, (char) fill)); |
4251 | s_return (mk_empty_string (SCHEME_A_ len, fill)); |
4292 | } |
4252 | } |
4293 | |
4253 | |
4294 | case OP_STRLEN: /* string-length */ |
4254 | case OP_STRLEN: /* string-length */ |
4295 | s_return (mk_integer (SCHEME_A_ strlength (car (SCHEME_V->args)))); |
4255 | s_return (mk_integer (SCHEME_A_ strlength (x))); |
4296 | |
4256 | |
4297 | case OP_STRREF: /* string-ref */ |
4257 | case OP_STRREF: /* string-ref */ |
4298 | { |
4258 | { |
4299 | char *str; |
4259 | char *str; |
4300 | int index; |
4260 | int index; |
4301 | |
4261 | |
4302 | str = strvalue (car (SCHEME_V->args)); |
4262 | str = strvalue (x); |
4303 | |
4263 | |
4304 | index = ivalue (cadr (SCHEME_V->args)); |
4264 | index = ivalue (cadr (args)); |
4305 | |
4265 | |
4306 | if (index >= strlength (car (SCHEME_V->args))) |
4266 | if (index >= strlength (x)) |
4307 | Error_1 ("string-ref: out of bounds:", cadr (SCHEME_V->args)); |
4267 | Error_1 ("string-ref: out of bounds:", cadr (args)); |
4308 | |
4268 | |
4309 | s_return (mk_character (SCHEME_A_ ((unsigned char *) str)[index])); |
4269 | s_return (mk_character (SCHEME_A_ ((unsigned char *)str)[index])); |
4310 | } |
4270 | } |
4311 | |
4271 | |
4312 | case OP_STRSET: /* string-set! */ |
4272 | case OP_STRSET: /* string-set! */ |
4313 | { |
4273 | { |
4314 | char *str; |
4274 | char *str; |
4315 | int index; |
4275 | int index; |
4316 | int c; |
4276 | int c; |
4317 | |
4277 | |
4318 | if (is_immutable (car (SCHEME_V->args))) |
4278 | if (is_immutable (x)) |
4319 | Error_1 ("string-set!: unable to alter immutable string:", car (SCHEME_V->args)); |
4279 | Error_1 ("string-set!: unable to alter immutable string:", x); |
4320 | |
4280 | |
4321 | str = strvalue (car (SCHEME_V->args)); |
4281 | str = strvalue (x); |
4322 | |
4282 | |
4323 | index = ivalue (cadr (SCHEME_V->args)); |
4283 | index = ivalue (cadr (args)); |
4324 | |
4284 | |
4325 | if (index >= strlength (car (SCHEME_V->args))) |
4285 | if (index >= strlength (x)) |
4326 | Error_1 ("string-set!: out of bounds:", cadr (SCHEME_V->args)); |
4286 | Error_1 ("string-set!: out of bounds:", cadr (args)); |
4327 | |
4287 | |
4328 | c = charvalue (caddr (SCHEME_V->args)); |
4288 | c = charvalue (caddr (args)); |
4329 | |
4289 | |
4330 | str[index] = (char) c; |
4290 | str[index] = c; |
4331 | s_return (car (SCHEME_V->args)); |
4291 | s_return (car (args)); |
4332 | } |
4292 | } |
4333 | |
4293 | |
4334 | case OP_STRAPPEND: /* string-append */ |
4294 | case OP_STRAPPEND: /* string-append */ |
4335 | { |
4295 | { |
4336 | /* 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 */ |
4337 | int len = 0; |
4297 | int len = 0; |
4338 | pointer newstr; |
4298 | pointer newstr; |
4339 | char *pos; |
4299 | char *pos; |
4340 | |
4300 | |
4341 | /* compute needed length for new string */ |
4301 | /* compute needed length for new string */ |
4342 | for (x = SCHEME_V->args; x != NIL; x = cdr (x)) |
4302 | for (x = args; x != NIL; x = cdr (x)) |
4343 | len += strlength (car (x)); |
4303 | len += strlength (car (x)); |
4344 | |
4304 | |
4345 | newstr = mk_empty_string (SCHEME_A_ len, ' '); |
4305 | newstr = mk_empty_string (SCHEME_A_ len, ' '); |
4346 | |
4306 | |
4347 | /* store the contents of the argument strings into the new string */ |
4307 | /* store the contents of the argument strings into the new string */ |
4348 | 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)) |
4349 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4309 | memcpy (pos, strvalue (car (x)), strlength (car (x))); |
4350 | |
4310 | |
4351 | s_return (newstr); |
4311 | s_return (newstr); |
4352 | } |
4312 | } |
4353 | |
4313 | |
… | |
… | |
4356 | char *str; |
4316 | char *str; |
4357 | int index0; |
4317 | int index0; |
4358 | int index1; |
4318 | int index1; |
4359 | int len; |
4319 | int len; |
4360 | |
4320 | |
4361 | str = strvalue (car (SCHEME_V->args)); |
4321 | str = strvalue (x); |
4362 | |
4322 | |
4363 | index0 = ivalue (cadr (SCHEME_V->args)); |
4323 | index0 = ivalue (cadr (args)); |
4364 | |
4324 | |
4365 | if (index0 > strlength (car (SCHEME_V->args))) |
4325 | if (index0 > strlength (x)) |
4366 | Error_1 ("substring: start out of bounds:", cadr (SCHEME_V->args)); |
4326 | Error_1 ("substring: start out of bounds:", cadr (args)); |
4367 | |
4327 | |
4368 | if (cddr (SCHEME_V->args) != NIL) |
4328 | if (cddr (args) != NIL) |
4369 | { |
4329 | { |
4370 | index1 = ivalue (caddr (SCHEME_V->args)); |
4330 | index1 = ivalue (caddr (args)); |
4371 | |
4331 | |
4372 | if (index1 > strlength (car (SCHEME_V->args)) || index1 < index0) |
4332 | if (index1 > strlength (x) || index1 < index0) |
4373 | Error_1 ("substring: end out of bounds:", caddr (SCHEME_V->args)); |
4333 | Error_1 ("substring: end out of bounds:", caddr (args)); |
4374 | } |
4334 | } |
4375 | else |
4335 | else |
4376 | index1 = strlength (car (SCHEME_V->args)); |
4336 | index1 = strlength (x); |
4377 | |
4337 | |
4378 | len = index1 - index0; |
4338 | len = index1 - index0; |
4379 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4339 | x = mk_empty_string (SCHEME_A_ len, ' '); |
4380 | memcpy (strvalue (x), str + index0, len); |
4340 | memcpy (strvalue (x), str + index0, len); |
4381 | strvalue (x)[len] = 0; |
4341 | strvalue (x)[len] = 0; |
… | |
… | |
4385 | |
4345 | |
4386 | case OP_VECTOR: /* vector */ |
4346 | case OP_VECTOR: /* vector */ |
4387 | { |
4347 | { |
4388 | int i; |
4348 | int i; |
4389 | pointer vec; |
4349 | pointer vec; |
4390 | int len = list_length (SCHEME_A_ SCHEME_V->args); |
4350 | int len = list_length (SCHEME_A_ args); |
4391 | |
4351 | |
4392 | if (len < 0) |
4352 | if (len < 0) |
4393 | Error_1 ("vector: not a proper list:", SCHEME_V->args); |
4353 | Error_1 ("vector: not a proper list:", args); |
4394 | |
4354 | |
4395 | vec = mk_vector (SCHEME_A_ len); |
4355 | vec = mk_vector (SCHEME_A_ len); |
4396 | |
4356 | |
4397 | #if USE_ERROR_CHECKING |
4357 | #if USE_ERROR_CHECKING |
4398 | if (SCHEME_V->no_memory) |
4358 | if (SCHEME_V->no_memory) |
4399 | s_return (S_SINK); |
4359 | s_return (S_SINK); |
4400 | #endif |
4360 | #endif |
4401 | |
4361 | |
4402 | 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++) |
4403 | set_vector_elem (vec, i, car (x)); |
4363 | set_vector_elem (vec, i, car (x)); |
4404 | |
4364 | |
4405 | s_return (vec); |
4365 | s_return (vec); |
4406 | } |
4366 | } |
4407 | |
4367 | |
… | |
… | |
4409 | { |
4369 | { |
4410 | pointer fill = NIL; |
4370 | pointer fill = NIL; |
4411 | int len; |
4371 | int len; |
4412 | pointer vec; |
4372 | pointer vec; |
4413 | |
4373 | |
4414 | len = ivalue (car (SCHEME_V->args)); |
4374 | len = ivalue (x); |
4415 | |
4375 | |
4416 | if (cdr (SCHEME_V->args) != NIL) |
4376 | if (cdr (args) != NIL) |
4417 | fill = cadr (SCHEME_V->args); |
4377 | fill = cadr (args); |
4418 | |
4378 | |
4419 | vec = mk_vector (SCHEME_A_ len); |
4379 | vec = mk_vector (SCHEME_A_ len); |
4420 | |
4380 | |
4421 | #if USE_ERROR_CHECKING |
4381 | #if USE_ERROR_CHECKING |
4422 | if (SCHEME_V->no_memory) |
4382 | if (SCHEME_V->no_memory) |
… | |
… | |
4428 | |
4388 | |
4429 | s_return (vec); |
4389 | s_return (vec); |
4430 | } |
4390 | } |
4431 | |
4391 | |
4432 | case OP_VECLEN: /* vector-length */ |
4392 | case OP_VECLEN: /* vector-length */ |
4433 | s_return (mk_integer (SCHEME_A_ veclength (car (SCHEME_V->args)))); |
4393 | s_return (mk_integer (SCHEME_A_ veclength (x))); |
4434 | |
4394 | |
4435 | case OP_VECREF: /* vector-ref */ |
4395 | case OP_VECREF: /* vector-ref */ |
4436 | { |
4396 | { |
4437 | int index; |
4397 | int index; |
4438 | |
4398 | |
4439 | index = ivalue (cadr (SCHEME_V->args)); |
4399 | index = ivalue (cadr (args)); |
4440 | |
4400 | |
4441 | if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) |
4401 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4442 | Error_1 ("vector-ref: out of bounds:", cadr (SCHEME_V->args)); |
4402 | Error_1 ("vector-ref: out of bounds:", cadr (args)); |
4443 | |
4403 | |
4444 | s_return (vector_elem (car (SCHEME_V->args), index)); |
4404 | s_return (vector_elem (x, index)); |
4445 | } |
4405 | } |
4446 | |
4406 | |
4447 | case OP_VECSET: /* vector-set! */ |
4407 | case OP_VECSET: /* vector-set! */ |
4448 | { |
4408 | { |
4449 | int index; |
4409 | int index; |
4450 | |
4410 | |
4451 | if (is_immutable (car (SCHEME_V->args))) |
4411 | if (is_immutable (x)) |
4452 | Error_1 ("vector-set!: unable to alter immutable vector:", car (SCHEME_V->args)); |
4412 | Error_1 ("vector-set!: unable to alter immutable vector:", x); |
4453 | |
4413 | |
4454 | index = ivalue (cadr (SCHEME_V->args)); |
4414 | index = ivalue (cadr (args)); |
4455 | |
4415 | |
4456 | if (index >= veclength (car (SCHEME_V->args)) && USE_ERROR_CHECKING) |
4416 | if (index >= veclength (car (args)) && USE_ERROR_CHECKING) |
4457 | Error_1 ("vector-set!: out of bounds:", cadr (SCHEME_V->args)); |
4417 | Error_1 ("vector-set!: out of bounds:", cadr (args)); |
4458 | |
4418 | |
4459 | set_vector_elem (car (SCHEME_V->args), index, caddr (SCHEME_V->args)); |
4419 | set_vector_elem (x, index, caddr (args)); |
4460 | s_return (car (SCHEME_V->args)); |
4420 | s_return (x); |
4461 | } |
4421 | } |
4462 | } |
4422 | } |
4463 | |
4423 | |
4464 | return S_T; |
4424 | abort (); |
4465 | } |
4425 | } |
4466 | |
4426 | |
4467 | INTERFACE int |
4427 | INTERFACE int |
4468 | is_list (SCHEME_P_ pointer a) |
4428 | is_list (SCHEME_P_ pointer a) |
4469 | { |
4429 | { |
… | |
… | |
4516 | return -1; |
4476 | return -1; |
4517 | } |
4477 | } |
4518 | } |
4478 | } |
4519 | } |
4479 | } |
4520 | |
4480 | |
4521 | static pointer |
4481 | static int |
4522 | opexe_r (SCHEME_P_ enum scheme_opcodes op) |
4482 | opexe_2 (SCHEME_P_ enum scheme_opcodes op) |
4523 | { |
4483 | { |
4524 | pointer x = SCHEME_V->args; |
4484 | pointer x = SCHEME_V->args; |
4525 | |
4485 | |
4526 | for (;;) |
4486 | for (;;) |
4527 | { |
4487 | { |
… | |
… | |
4547 | } |
4507 | } |
4548 | |
4508 | |
4549 | s_return (S_T); |
4509 | s_return (S_T); |
4550 | } |
4510 | } |
4551 | |
4511 | |
4552 | static pointer |
4512 | static int |
4553 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4513 | opexe_3 (SCHEME_P_ enum scheme_opcodes op) |
4554 | { |
4514 | { |
4555 | pointer x = SCHEME_V->args; |
4515 | pointer args = SCHEME_V->args; |
4556 | pointer a = car (x); |
4516 | pointer a = car (args); |
4557 | pointer d = cdr (x); |
4517 | pointer d = cdr (args); |
4558 | int r; |
4518 | int r; |
4559 | |
4519 | |
4560 | switch (op) |
4520 | switch (op) |
4561 | { |
4521 | { |
4562 | case OP_NOT: /* not */ r = is_false (a) ; break; |
4522 | case OP_NOT: /* not */ r = is_false (a) ; break; |
… | |
… | |
4596 | |
4556 | |
4597 | case OP_PAIRP: /* pair? */ r = is_pair (a) ; break; |
4557 | case OP_PAIRP: /* pair? */ r = is_pair (a) ; break; |
4598 | case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break; |
4558 | case OP_LISTP: /* list? */ r = list_length (SCHEME_A_ a) >= 0; break; |
4599 | case OP_ENVP: /* environment? */ r = is_environment (a) ; break; |
4559 | case OP_ENVP: /* environment? */ r = is_environment (a) ; break; |
4600 | case OP_VECTORP: /* vector? */ r = is_vector (a) ; break; |
4560 | case OP_VECTORP: /* vector? */ r = is_vector (a) ; break; |
4601 | case OP_EQ: /* eq? */ r = a == cadr (x) ; break; |
4561 | case OP_EQ: /* eq? */ r = a == cadr (args) ; break; |
4602 | case OP_EQV: /* eqv? */ r = eqv (a, cadr (x)) ; break; |
4562 | case OP_EQV: /* eqv? */ r = eqv (a, cadr (args)) ; break; |
4603 | } |
4563 | } |
4604 | |
4564 | |
4605 | s_retbool (r); |
4565 | s_retbool (r); |
4606 | } |
4566 | } |
4607 | |
4567 | |
4608 | static pointer |
4568 | static int |
4609 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4569 | opexe_4 (SCHEME_P_ enum scheme_opcodes op) |
4610 | { |
4570 | { |
|
|
4571 | pointer args = SCHEME_V->args; |
|
|
4572 | pointer a = car (args); |
4611 | pointer x, y; |
4573 | pointer x, y; |
4612 | |
4574 | |
4613 | switch (op) |
4575 | switch (op) |
4614 | { |
4576 | { |
4615 | case OP_FORCE: /* force */ |
4577 | case OP_FORCE: /* force */ |
4616 | SCHEME_V->code = car (SCHEME_V->args); |
4578 | SCHEME_V->code = a; |
4617 | |
4579 | |
4618 | if (is_promise (SCHEME_V->code)) |
4580 | if (is_promise (SCHEME_V->code)) |
4619 | { |
4581 | { |
4620 | /* Should change type to closure here */ |
4582 | /* Should change type to closure here */ |
4621 | s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); |
4583 | s_save (SCHEME_A_ OP_SAVE_FORCED, NIL, SCHEME_V->code); |
… | |
… | |
4642 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4604 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4643 | SCHEME_V->outport = cadr (SCHEME_V->args); |
4605 | SCHEME_V->outport = cadr (SCHEME_V->args); |
4644 | } |
4606 | } |
4645 | } |
4607 | } |
4646 | |
4608 | |
4647 | SCHEME_V->args = car (SCHEME_V->args); |
4609 | SCHEME_V->args = a; |
4648 | |
4610 | |
4649 | if (op == OP_WRITE) |
4611 | if (op == OP_WRITE) |
4650 | SCHEME_V->print_flag = 1; |
4612 | SCHEME_V->print_flag = 1; |
4651 | else |
4613 | else |
4652 | SCHEME_V->print_flag = 0; |
4614 | SCHEME_V->print_flag = 0; |
4653 | |
4615 | |
4654 | s_goto (OP_P0LIST); |
4616 | s_goto (OP_P0LIST); |
4655 | |
4617 | |
4656 | case OP_NEWLINE: /* newline */ |
4618 | case OP_NEWLINE: /* newline */ |
4657 | if (is_pair (SCHEME_V->args)) |
4619 | if (is_pair (args)) |
4658 | { |
4620 | { |
4659 | if (car (SCHEME_V->args) != SCHEME_V->outport) |
4621 | if (a != SCHEME_V->outport) |
4660 | { |
4622 | { |
4661 | x = cons (SCHEME_V->outport, NIL); |
4623 | x = cons (SCHEME_V->outport, NIL); |
4662 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4624 | s_save (SCHEME_A_ OP_SET_OUTPORT, x, NIL); |
4663 | SCHEME_V->outport = car (SCHEME_V->args); |
4625 | SCHEME_V->outport = a; |
4664 | } |
4626 | } |
4665 | } |
4627 | } |
4666 | |
4628 | |
4667 | putstr (SCHEME_A_ "\n"); |
4629 | putstr (SCHEME_A_ "\n"); |
4668 | s_return (S_T); |
4630 | s_return (S_T); |
4669 | #endif |
4631 | #endif |
4670 | |
4632 | |
4671 | case OP_ERR0: /* error */ |
4633 | case OP_ERR0: /* error */ |
4672 | SCHEME_V->retcode = -1; |
4634 | SCHEME_V->retcode = -1; |
4673 | |
4635 | |
4674 | if (!is_string (car (SCHEME_V->args))) |
4636 | if (!is_string (a)) |
4675 | { |
4637 | { |
4676 | SCHEME_V->args = cons (mk_string (SCHEME_A_ " -- "), SCHEME_V->args); |
4638 | args = cons (mk_string (SCHEME_A_ " -- "), args); |
4677 | setimmutable (car (SCHEME_V->args)); |
4639 | setimmutable (car (args)); |
4678 | } |
4640 | } |
4679 | |
4641 | |
4680 | putstr (SCHEME_A_ "Error: "); |
4642 | putstr (SCHEME_A_ "Error: "); |
4681 | putstr (SCHEME_A_ strvalue (car (SCHEME_V->args))); |
4643 | putstr (SCHEME_A_ strvalue (car (args))); |
4682 | SCHEME_V->args = cdr (SCHEME_V->args); |
4644 | SCHEME_V->args = cdr (args); |
4683 | s_goto (OP_ERR1); |
4645 | s_goto (OP_ERR1); |
4684 | |
4646 | |
4685 | case OP_ERR1: /* error */ |
4647 | case OP_ERR1: /* error */ |
4686 | putstr (SCHEME_A_ " "); |
4648 | putstr (SCHEME_A_ " "); |
4687 | |
4649 | |
4688 | if (SCHEME_V->args != NIL) |
4650 | if (args != NIL) |
4689 | { |
4651 | { |
4690 | s_save (SCHEME_A_ OP_ERR1, cdr (SCHEME_V->args), NIL); |
4652 | s_save (SCHEME_A_ OP_ERR1, cdr (args), NIL); |
4691 | SCHEME_V->args = car (SCHEME_V->args); |
4653 | SCHEME_V->args = a; |
4692 | SCHEME_V->print_flag = 1; |
4654 | SCHEME_V->print_flag = 1; |
4693 | s_goto (OP_P0LIST); |
4655 | s_goto (OP_P0LIST); |
4694 | } |
4656 | } |
4695 | else |
4657 | else |
4696 | { |
4658 | { |
4697 | putstr (SCHEME_A_ "\n"); |
4659 | putstr (SCHEME_A_ "\n"); |
4698 | |
4660 | |
4699 | if (SCHEME_V->interactive_repl) |
4661 | if (SCHEME_V->interactive_repl) |
4700 | s_goto (OP_T0LVL); |
4662 | s_goto (OP_T0LVL); |
4701 | else |
4663 | else |
4702 | return NIL; |
4664 | return -1; |
4703 | } |
4665 | } |
4704 | |
4666 | |
4705 | case OP_REVERSE: /* reverse */ |
4667 | case OP_REVERSE: /* reverse */ |
4706 | s_return (reverse (SCHEME_A_ car (SCHEME_V->args))); |
4668 | s_return (reverse (SCHEME_A_ a)); |
4707 | |
4669 | |
4708 | case OP_LIST_STAR: /* list* */ |
4670 | case OP_LIST_STAR: /* list* */ |
4709 | s_return (list_star (SCHEME_A_ SCHEME_V->args)); |
4671 | s_return (list_star (SCHEME_A_ SCHEME_V->args)); |
4710 | |
4672 | |
4711 | case OP_APPEND: /* append */ |
4673 | case OP_APPEND: /* append */ |
4712 | x = NIL; |
4674 | x = NIL; |
4713 | y = SCHEME_V->args; |
4675 | y = args; |
4714 | |
4676 | |
4715 | if (y == x) |
4677 | if (y == x) |
4716 | s_return (x); |
4678 | s_return (x); |
4717 | |
4679 | |
4718 | /* cdr() in the while condition is not a typo. If car() */ |
4680 | /* cdr() in the while condition is not a typo. If car() */ |
… | |
… | |
4729 | s_return (reverse_in_place (SCHEME_A_ car (y), x)); |
4691 | s_return (reverse_in_place (SCHEME_A_ car (y), x)); |
4730 | |
4692 | |
4731 | #if USE_PLIST |
4693 | #if USE_PLIST |
4732 | |
4694 | |
4733 | case OP_PUT: /* put */ |
4695 | case OP_PUT: /* put */ |
4734 | if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) |
4696 | if (!hasprop (a) || !hasprop (cadr (args))) |
4735 | Error_0 ("illegal use of put"); |
4697 | Error_0 ("illegal use of put"); |
4736 | |
4698 | |
4737 | 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)) |
4738 | { |
4700 | { |
4739 | if (caar (x) == y) |
4701 | if (caar (x) == y) |
4740 | break; |
4702 | break; |
4741 | } |
4703 | } |
4742 | |
4704 | |
4743 | if (x != NIL) |
4705 | if (x != NIL) |
4744 | cdar (x) = caddr (SCHEME_V->args); |
4706 | cdar (x) = caddr (args); |
4745 | else |
4707 | else |
4746 | 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)); |
4747 | |
4709 | |
4748 | s_return (S_T); |
4710 | s_return (S_T); |
4749 | |
4711 | |
4750 | case OP_GET: /* get */ |
4712 | case OP_GET: /* get */ |
4751 | if (!hasprop (car (SCHEME_V->args)) || !hasprop (cadr (SCHEME_V->args))) |
4713 | if (!hasprop (a) || !hasprop (cadr (args))) |
4752 | Error_0 ("illegal use of get"); |
4714 | Error_0 ("illegal use of get"); |
4753 | |
4715 | |
4754 | 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)) |
4755 | if (caar (x) == y) |
4717 | if (caar (x) == y) |
4756 | break; |
4718 | break; |
4757 | |
4719 | |
4758 | if (x != NIL) |
4720 | if (x != NIL) |
4759 | s_return (cdar (x)); |
4721 | s_return (cdar (x)); |
… | |
… | |
4761 | s_return (NIL); |
4723 | s_return (NIL); |
4762 | |
4724 | |
4763 | #endif /* USE_PLIST */ |
4725 | #endif /* USE_PLIST */ |
4764 | |
4726 | |
4765 | case OP_QUIT: /* quit */ |
4727 | case OP_QUIT: /* quit */ |
4766 | if (is_pair (SCHEME_V->args)) |
4728 | if (is_pair (args)) |
4767 | SCHEME_V->retcode = ivalue (car (SCHEME_V->args)); |
4729 | SCHEME_V->retcode = ivalue (a); |
4768 | |
4730 | |
4769 | return NIL; |
4731 | return -1; |
4770 | |
4732 | |
4771 | case OP_GC: /* gc */ |
4733 | case OP_GC: /* gc */ |
4772 | gc (SCHEME_A_ NIL, NIL); |
4734 | gc (SCHEME_A_ NIL, NIL); |
4773 | s_return (S_T); |
4735 | s_return (S_T); |
4774 | |
4736 | |
4775 | case OP_GCVERB: /* gc-verbose */ |
4737 | case OP_GCVERB: /* gc-verbose */ |
4776 | { |
4738 | { |
4777 | int was = SCHEME_V->gc_verbose; |
4739 | int was = SCHEME_V->gc_verbose; |
4778 | |
4740 | |
4779 | SCHEME_V->gc_verbose = (car (SCHEME_V->args) != S_F); |
4741 | SCHEME_V->gc_verbose = (a != S_F); |
4780 | s_retbool (was); |
4742 | s_retbool (was); |
4781 | } |
4743 | } |
4782 | |
4744 | |
4783 | case OP_NEWSEGMENT: /* new-segment */ |
4745 | case OP_NEWSEGMENT: /* new-segment */ |
4784 | if (!is_pair (SCHEME_V->args) || !is_number (car (SCHEME_V->args))) |
4746 | if (!is_pair (args) || !is_number (a)) |
4785 | Error_0 ("new-segment: argument must be a number"); |
4747 | Error_0 ("new-segment: argument must be a number"); |
4786 | |
4748 | |
4787 | alloc_cellseg (SCHEME_A_ (int)ivalue (car (SCHEME_V->args))); |
4749 | alloc_cellseg (SCHEME_A_ (int)ivalue (a)); |
4788 | |
4750 | |
4789 | s_return (S_T); |
4751 | s_return (S_T); |
4790 | |
4752 | |
4791 | case OP_OBLIST: /* oblist */ |
4753 | case OP_OBLIST: /* oblist */ |
4792 | s_return (oblist_all_symbols (SCHEME_A)); |
4754 | s_return (oblist_all_symbols (SCHEME_A)); |
… | |
… | |
4819 | case OP_OPEN_INOUTFILE: |
4781 | case OP_OPEN_INOUTFILE: |
4820 | prop = port_input | port_output; |
4782 | prop = port_input | port_output; |
4821 | break; |
4783 | break; |
4822 | } |
4784 | } |
4823 | |
4785 | |
4824 | p = port_from_filename (SCHEME_A_ strvalue (car (SCHEME_V->args)), prop); |
4786 | p = port_from_filename (SCHEME_A_ strvalue (a), prop); |
4825 | |
4787 | |
4826 | if (p == NIL) |
4788 | if (p == NIL) |
4827 | s_return (S_F); |
4789 | s_return (S_F); |
4828 | |
4790 | |
4829 | s_return (p); |
4791 | s_return (p); |
… | |
… | |
4846 | case OP_OPEN_INOUTSTRING: |
4808 | case OP_OPEN_INOUTSTRING: |
4847 | prop = port_input | port_output; |
4809 | prop = port_input | port_output; |
4848 | break; |
4810 | break; |
4849 | } |
4811 | } |
4850 | |
4812 | |
4851 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
4813 | p = port_from_string (SCHEME_A_ strvalue (a), |
4852 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), prop); |
4814 | strvalue (a) + strlength (a), prop); |
4853 | |
4815 | |
4854 | if (p == NIL) |
4816 | if (p == NIL) |
4855 | s_return (S_F); |
4817 | s_return (S_F); |
4856 | |
4818 | |
4857 | s_return (p); |
4819 | s_return (p); |
… | |
… | |
4859 | |
4821 | |
4860 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4822 | case OP_OPEN_OUTSTRING: /* open-output-string */ |
4861 | { |
4823 | { |
4862 | pointer p; |
4824 | pointer p; |
4863 | |
4825 | |
4864 | if (car (SCHEME_V->args) == NIL) |
4826 | if (a == NIL) |
4865 | { |
4827 | { |
4866 | p = port_from_scratch (SCHEME_A); |
4828 | p = port_from_scratch (SCHEME_A); |
4867 | |
4829 | |
4868 | if (p == NIL) |
4830 | if (p == NIL) |
4869 | s_return (S_F); |
4831 | s_return (S_F); |
4870 | } |
4832 | } |
4871 | else |
4833 | else |
4872 | { |
4834 | { |
4873 | p = port_from_string (SCHEME_A_ strvalue (car (SCHEME_V->args)), |
4835 | p = port_from_string (SCHEME_A_ strvalue (a), |
4874 | strvalue (car (SCHEME_V->args)) + strlength (car (SCHEME_V->args)), port_output); |
4836 | strvalue (a) + strlength (a), port_output); |
4875 | |
4837 | |
4876 | if (p == NIL) |
4838 | if (p == NIL) |
4877 | s_return (S_F); |
4839 | s_return (S_F); |
4878 | } |
4840 | } |
4879 | |
4841 | |
… | |
… | |
4882 | |
4844 | |
4883 | case OP_GET_OUTSTRING: /* get-output-string */ |
4845 | case OP_GET_OUTSTRING: /* get-output-string */ |
4884 | { |
4846 | { |
4885 | port *p; |
4847 | port *p; |
4886 | |
4848 | |
4887 | if ((p = car (SCHEME_V->args)->object.port)->kind & port_string) |
4849 | if ((p = a->object.port)->kind & port_string) |
4888 | { |
4850 | { |
4889 | off_t size; |
4851 | off_t size; |
4890 | char *str; |
4852 | char *str; |
4891 | |
4853 | |
4892 | size = p->rep.string.curr - p->rep.string.start + 1; |
4854 | size = p->rep.string.curr - p->rep.string.start + 1; |
… | |
… | |
4908 | } |
4870 | } |
4909 | |
4871 | |
4910 | # endif |
4872 | # endif |
4911 | |
4873 | |
4912 | case OP_CLOSE_INPORT: /* close-input-port */ |
4874 | case OP_CLOSE_INPORT: /* close-input-port */ |
4913 | port_close (SCHEME_A_ car (SCHEME_V->args), port_input); |
4875 | port_close (SCHEME_A_ a, port_input); |
4914 | s_return (S_T); |
4876 | s_return (S_T); |
4915 | |
4877 | |
4916 | case OP_CLOSE_OUTPORT: /* close-output-port */ |
4878 | case OP_CLOSE_OUTPORT: /* close-output-port */ |
4917 | port_close (SCHEME_A_ car (SCHEME_V->args), port_output); |
4879 | port_close (SCHEME_A_ a, port_output); |
4918 | s_return (S_T); |
4880 | s_return (S_T); |
4919 | #endif |
4881 | #endif |
4920 | |
4882 | |
4921 | case OP_INT_ENV: /* interaction-environment */ |
4883 | case OP_INT_ENV: /* interaction-environment */ |
4922 | s_return (SCHEME_V->global_env); |
4884 | s_return (SCHEME_V->global_env); |
… | |
… | |
4927 | } |
4889 | } |
4928 | |
4890 | |
4929 | abort (); |
4891 | abort (); |
4930 | } |
4892 | } |
4931 | |
4893 | |
4932 | static pointer |
4894 | static int |
4933 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4895 | opexe_5 (SCHEME_P_ enum scheme_opcodes op) |
4934 | { |
4896 | { |
|
|
4897 | pointer args = SCHEME_V->args; |
4935 | pointer x; |
4898 | pointer x; |
4936 | |
4899 | |
4937 | if (SCHEME_V->nesting != 0) |
4900 | if (SCHEME_V->nesting != 0) |
4938 | { |
4901 | { |
4939 | int n = SCHEME_V->nesting; |
4902 | int n = SCHEME_V->nesting; |
… | |
… | |
4946 | switch (op) |
4909 | switch (op) |
4947 | { |
4910 | { |
4948 | /* ========== reading part ========== */ |
4911 | /* ========== reading part ========== */ |
4949 | #if USE_PORTS |
4912 | #if USE_PORTS |
4950 | case OP_READ: |
4913 | case OP_READ: |
4951 | if (!is_pair (SCHEME_V->args)) |
4914 | if (!is_pair (args)) |
4952 | s_goto (OP_READ_INTERNAL); |
4915 | s_goto (OP_READ_INTERNAL); |
4953 | |
4916 | |
4954 | if (!is_inport (car (SCHEME_V->args))) |
4917 | if (!is_inport (car (args))) |
4955 | Error_1 ("read: not an input port:", car (SCHEME_V->args)); |
4918 | Error_1 ("read: not an input port:", car (args)); |
4956 | |
4919 | |
4957 | if (car (SCHEME_V->args) == SCHEME_V->inport) |
4920 | if (car (args) == SCHEME_V->inport) |
4958 | s_goto (OP_READ_INTERNAL); |
4921 | s_goto (OP_READ_INTERNAL); |
4959 | |
4922 | |
4960 | x = SCHEME_V->inport; |
4923 | x = SCHEME_V->inport; |
4961 | SCHEME_V->inport = car (SCHEME_V->args); |
4924 | SCHEME_V->inport = car (args); |
4962 | x = cons (x, NIL); |
4925 | x = cons (x, NIL); |
4963 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
4926 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
4964 | s_goto (OP_READ_INTERNAL); |
4927 | s_goto (OP_READ_INTERNAL); |
4965 | |
4928 | |
4966 | case OP_READ_CHAR: /* read-char */ |
4929 | case OP_READ_CHAR: /* read-char */ |
4967 | case OP_PEEK_CHAR: /* peek-char */ |
4930 | case OP_PEEK_CHAR: /* peek-char */ |
4968 | { |
4931 | { |
4969 | int c; |
4932 | int c; |
4970 | |
4933 | |
4971 | if (is_pair (SCHEME_V->args)) |
4934 | if (is_pair (args)) |
4972 | { |
4935 | { |
4973 | if (car (SCHEME_V->args) != SCHEME_V->inport) |
4936 | if (car (args) != SCHEME_V->inport) |
4974 | { |
4937 | { |
4975 | x = SCHEME_V->inport; |
4938 | x = SCHEME_V->inport; |
4976 | x = cons (x, NIL); |
4939 | x = cons (x, NIL); |
4977 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
4940 | s_save (SCHEME_A_ OP_SET_INPORT, x, NIL); |
4978 | SCHEME_V->inport = car (SCHEME_V->args); |
4941 | SCHEME_V->inport = car (args); |
4979 | } |
4942 | } |
4980 | } |
4943 | } |
4981 | |
4944 | |
4982 | c = inchar (SCHEME_A); |
4945 | c = inchar (SCHEME_A); |
4983 | |
4946 | |
… | |
… | |
4993 | case OP_CHAR_READY: /* char-ready? */ |
4956 | case OP_CHAR_READY: /* char-ready? */ |
4994 | { |
4957 | { |
4995 | pointer p = SCHEME_V->inport; |
4958 | pointer p = SCHEME_V->inport; |
4996 | int res; |
4959 | int res; |
4997 | |
4960 | |
4998 | if (is_pair (SCHEME_V->args)) |
4961 | if (is_pair (args)) |
4999 | p = car (SCHEME_V->args); |
4962 | p = car (args); |
5000 | |
4963 | |
5001 | res = p->object.port->kind & port_string; |
4964 | res = p->object.port->kind & port_string; |
5002 | |
4965 | |
5003 | s_retbool (res); |
4966 | s_retbool (res); |
5004 | } |
4967 | } |
5005 | |
4968 | |
5006 | case OP_SET_INPORT: /* set-input-port */ |
4969 | case OP_SET_INPORT: /* set-input-port */ |
5007 | SCHEME_V->inport = car (SCHEME_V->args); |
4970 | SCHEME_V->inport = car (args); |
5008 | s_return (SCHEME_V->value); |
4971 | s_return (SCHEME_V->value); |
5009 | |
4972 | |
5010 | case OP_SET_OUTPORT: /* set-output-port */ |
4973 | case OP_SET_OUTPORT: /* set-output-port */ |
5011 | SCHEME_V->outport = car (SCHEME_V->args); |
4974 | SCHEME_V->outport = car (args); |
5012 | s_return (SCHEME_V->value); |
4975 | s_return (SCHEME_V->value); |
5013 | #endif |
4976 | #endif |
5014 | |
4977 | |
5015 | case OP_RDSEXPR: |
4978 | case OP_RDSEXPR: |
5016 | switch (SCHEME_V->tok) |
4979 | switch (SCHEME_V->tok) |
… | |
… | |
5102 | } |
5065 | } |
5103 | |
5066 | |
5104 | break; |
5067 | break; |
5105 | |
5068 | |
5106 | case OP_RDLIST: |
5069 | case OP_RDLIST: |
5107 | SCHEME_V->args = cons (SCHEME_V->value, SCHEME_V->args); |
5070 | SCHEME_V->args = cons (SCHEME_V->value, args); |
5108 | SCHEME_V->tok = token (SCHEME_A); |
5071 | SCHEME_V->tok = token (SCHEME_A); |
5109 | |
5072 | |
5110 | switch (SCHEME_V->tok) |
5073 | switch (SCHEME_V->tok) |
5111 | { |
5074 | { |
5112 | case TOK_EOF: |
5075 | case TOK_EOF: |
… | |
… | |
5140 | case OP_RDDOT: |
5103 | case OP_RDDOT: |
5141 | if (token (SCHEME_A) != TOK_RPAREN) |
5104 | if (token (SCHEME_A) != TOK_RPAREN) |
5142 | Error_0 ("syntax error: illegal dot expression"); |
5105 | Error_0 ("syntax error: illegal dot expression"); |
5143 | |
5106 | |
5144 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5107 | SCHEME_V->nesting_stack[SCHEME_V->file_i]--; |
5145 | 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)); |
5146 | |
5109 | |
5147 | case OP_RDQUOTE: |
5110 | case OP_RDQUOTE: |
5148 | s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); |
5111 | s_return (cons (SCHEME_V->QUOTE, cons (SCHEME_V->value, NIL))); |
5149 | |
5112 | |
5150 | case OP_RDQQUOTE: |
5113 | case OP_RDQQUOTE: |
… | |
… | |
5172 | SCHEME_V->args = SCHEME_V->value; |
5135 | SCHEME_V->args = SCHEME_V->value; |
5173 | s_goto (OP_VECTOR); |
5136 | s_goto (OP_VECTOR); |
5174 | |
5137 | |
5175 | /* ========== printing part ========== */ |
5138 | /* ========== printing part ========== */ |
5176 | case OP_P0LIST: |
5139 | case OP_P0LIST: |
5177 | if (is_vector (SCHEME_V->args)) |
5140 | if (is_vector (args)) |
5178 | { |
5141 | { |
5179 | putstr (SCHEME_A_ "#("); |
5142 | putstr (SCHEME_A_ "#("); |
5180 | SCHEME_V->args = cons (SCHEME_V->args, mk_integer (SCHEME_A_ 0)); |
5143 | SCHEME_V->args = cons (args, mk_integer (SCHEME_A_ 0)); |
5181 | s_goto (OP_PVECFROM); |
5144 | s_goto (OP_PVECFROM); |
5182 | } |
5145 | } |
5183 | else if (is_environment (SCHEME_V->args)) |
5146 | else if (is_environment (args)) |
5184 | { |
5147 | { |
5185 | putstr (SCHEME_A_ "#<ENVIRONMENT>"); |
5148 | putstr (SCHEME_A_ "#<ENVIRONMENT>"); |
5186 | s_return (S_T); |
5149 | s_return (S_T); |
5187 | } |
5150 | } |
5188 | else if (!is_pair (SCHEME_V->args)) |
5151 | else if (!is_pair (args)) |
5189 | { |
5152 | { |
5190 | printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); |
5153 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5191 | s_return (S_T); |
5154 | s_return (S_T); |
5192 | } |
5155 | } |
5193 | else if (car (SCHEME_V->args) == SCHEME_V->QUOTE && ok_abbrev (cdr (SCHEME_V->args))) |
5156 | else |
5194 | { |
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) |
5195 | putstr (SCHEME_A_ "'"); |
5164 | putstr (SCHEME_A_ "'"); |
5196 | 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 | |
5197 | s_goto (OP_P0LIST); |
5178 | s_goto (OP_P0LIST); |
5198 | } |
5179 | } |
5199 | 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)) |
5200 | { |
5183 | { |
|
|
5184 | s_save (SCHEME_A_ OP_P1LIST, cdr (args), NIL); |
5201 | putstr (SCHEME_A_ "`"); |
5185 | putstr (SCHEME_A_ " "); |
5202 | SCHEME_V->args = cadr (SCHEME_V->args); |
5186 | SCHEME_V->args = car (args); |
5203 | s_goto (OP_P0LIST); |
5187 | s_goto (OP_P0LIST); |
5204 | } |
5188 | } |
5205 | else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTE && ok_abbrev (cdr (SCHEME_V->args))) |
|
|
5206 | { |
|
|
5207 | putstr (SCHEME_A_ ","); |
|
|
5208 | SCHEME_V->args = cadr (SCHEME_V->args); |
|
|
5209 | s_goto (OP_P0LIST); |
|
|
5210 | } |
|
|
5211 | else if (car (SCHEME_V->args) == SCHEME_V->UNQUOTESP && ok_abbrev (cdr (SCHEME_V->args))) |
|
|
5212 | { |
|
|
5213 | putstr (SCHEME_A_ ",@"); |
|
|
5214 | SCHEME_V->args = cadr (SCHEME_V->args); |
|
|
5215 | s_goto (OP_P0LIST); |
|
|
5216 | } |
|
|
5217 | else |
|
|
5218 | { |
|
|
5219 | putstr (SCHEME_A_ "("); |
|
|
5220 | s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL); |
|
|
5221 | SCHEME_V->args = car (SCHEME_V->args); |
|
|
5222 | s_goto (OP_P0LIST); |
|
|
5223 | } |
|
|
5224 | |
|
|
5225 | case OP_P1LIST: |
|
|
5226 | if (is_pair (SCHEME_V->args)) |
|
|
5227 | { |
|
|
5228 | s_save (SCHEME_A_ OP_P1LIST, cdr (SCHEME_V->args), NIL); |
|
|
5229 | putstr (SCHEME_A_ " "); |
|
|
5230 | SCHEME_V->args = car (SCHEME_V->args); |
|
|
5231 | s_goto (OP_P0LIST); |
|
|
5232 | } |
|
|
5233 | else if (is_vector (SCHEME_V->args)) |
5189 | else if (is_vector (args)) |
5234 | { |
5190 | { |
5235 | s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); |
5191 | s_save (SCHEME_A_ OP_P1LIST, NIL, NIL); |
5236 | putstr (SCHEME_A_ " . "); |
5192 | putstr (SCHEME_A_ " . "); |
5237 | s_goto (OP_P0LIST); |
5193 | s_goto (OP_P0LIST); |
5238 | } |
5194 | } |
5239 | else |
5195 | else |
5240 | { |
5196 | { |
5241 | if (SCHEME_V->args != NIL) |
5197 | if (args != NIL) |
5242 | { |
5198 | { |
5243 | putstr (SCHEME_A_ " . "); |
5199 | putstr (SCHEME_A_ " . "); |
5244 | printatom (SCHEME_A_ SCHEME_V->args, SCHEME_V->print_flag); |
5200 | printatom (SCHEME_A_ args, SCHEME_V->print_flag); |
5245 | } |
5201 | } |
5246 | |
5202 | |
5247 | putstr (SCHEME_A_ ")"); |
5203 | putstr (SCHEME_A_ ")"); |
5248 | s_return (S_T); |
5204 | s_return (S_T); |
5249 | } |
5205 | } |
5250 | |
5206 | |
5251 | case OP_PVECFROM: |
5207 | case OP_PVECFROM: |
5252 | { |
5208 | { |
5253 | int i = ivalue_unchecked (cdr (SCHEME_V->args)); |
5209 | int i = ivalue_unchecked (cdr (args)); |
5254 | pointer vec = car (SCHEME_V->args); |
5210 | pointer vec = car (args); |
5255 | int len = veclength (vec); |
5211 | int len = veclength (vec); |
5256 | |
5212 | |
5257 | if (i == len) |
5213 | if (i == len) |
5258 | { |
5214 | { |
5259 | putstr (SCHEME_A_ ")"); |
5215 | putstr (SCHEME_A_ ")"); |
… | |
… | |
5261 | } |
5217 | } |
5262 | else |
5218 | else |
5263 | { |
5219 | { |
5264 | pointer elem = vector_elem (vec, i); |
5220 | pointer elem = vector_elem (vec, i); |
5265 | |
5221 | |
5266 | ivalue_unchecked (cdr (SCHEME_V->args)) = i + 1; |
5222 | ivalue_unchecked (cdr (args)) = i + 1; |
5267 | s_save (SCHEME_A_ OP_PVECFROM, SCHEME_V->args, NIL); |
5223 | s_save (SCHEME_A_ OP_PVECFROM, args, NIL); |
5268 | SCHEME_V->args = elem; |
5224 | SCHEME_V->args = elem; |
5269 | |
5225 | |
5270 | if (i > 0) |
5226 | if (i > 0) |
5271 | putstr (SCHEME_A_ " "); |
5227 | putstr (SCHEME_A_ " "); |
5272 | |
5228 | |
… | |
… | |
5276 | } |
5232 | } |
5277 | |
5233 | |
5278 | abort (); |
5234 | abort (); |
5279 | } |
5235 | } |
5280 | |
5236 | |
5281 | static pointer |
5237 | static int |
5282 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5238 | opexe_6 (SCHEME_P_ enum scheme_opcodes op) |
5283 | { |
5239 | { |
|
|
5240 | pointer args = SCHEME_V->args; |
|
|
5241 | pointer a = car (args); |
5284 | pointer x, y; |
5242 | pointer x, y; |
5285 | |
5243 | |
5286 | switch (op) |
5244 | switch (op) |
5287 | { |
5245 | { |
5288 | case OP_LIST_LENGTH: /* length *//* a.k */ |
5246 | case OP_LIST_LENGTH: /* length *//* a.k */ |
5289 | { |
5247 | { |
5290 | long v = list_length (SCHEME_A_ car (SCHEME_V->args)); |
5248 | long v = list_length (SCHEME_A_ a); |
5291 | |
5249 | |
5292 | if (v < 0) |
5250 | if (v < 0) |
5293 | Error_1 ("length: not a list:", car (SCHEME_V->args)); |
5251 | Error_1 ("length: not a list:", a); |
5294 | |
5252 | |
5295 | s_return (mk_integer (SCHEME_A_ v)); |
5253 | s_return (mk_integer (SCHEME_A_ v)); |
5296 | } |
5254 | } |
5297 | |
5255 | |
5298 | case OP_ASSQ: /* assq *//* a.k */ |
5256 | case OP_ASSQ: /* assq *//* a.k */ |
5299 | x = car (SCHEME_V->args); |
5257 | x = a; |
5300 | |
5258 | |
5301 | for (y = cadr (SCHEME_V->args); is_pair (y); y = cdr (y)) |
5259 | for (y = cadr (args); is_pair (y); y = cdr (y)) |
5302 | { |
5260 | { |
5303 | if (!is_pair (car (y))) |
5261 | if (!is_pair (car (y))) |
5304 | Error_0 ("unable to handle non pair element"); |
5262 | Error_0 ("unable to handle non pair element"); |
5305 | |
5263 | |
5306 | if (x == caar (y)) |
5264 | if (x == caar (y)) |
… | |
… | |
5312 | else |
5270 | else |
5313 | s_return (S_F); |
5271 | s_return (S_F); |
5314 | |
5272 | |
5315 | |
5273 | |
5316 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5274 | case OP_GET_CLOSURE: /* get-closure-code *//* a.k */ |
5317 | SCHEME_V->args = car (SCHEME_V->args); |
5275 | SCHEME_V->args = a; |
5318 | |
5276 | |
5319 | if (SCHEME_V->args == NIL) |
5277 | if (SCHEME_V->args == NIL) |
5320 | s_return (S_F); |
5278 | s_return (S_F); |
5321 | else if (is_closure (SCHEME_V->args)) |
5279 | else if (is_closure (SCHEME_V->args)) |
5322 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
5280 | s_return (cons (SCHEME_V->LAMBDA, closure_code (SCHEME_V->value))); |
… | |
… | |
5328 | case OP_CLOSUREP: /* closure? */ |
5286 | case OP_CLOSUREP: /* closure? */ |
5329 | /* |
5287 | /* |
5330 | * Note, macro object is also a closure. |
5288 | * Note, macro object is also a closure. |
5331 | * Therefore, (closure? <#MACRO>) ==> #t |
5289 | * Therefore, (closure? <#MACRO>) ==> #t |
5332 | */ |
5290 | */ |
5333 | s_retbool (is_closure (car (SCHEME_V->args))); |
5291 | s_retbool (is_closure (a)); |
5334 | |
5292 | |
5335 | case OP_MACROP: /* macro? */ |
5293 | case OP_MACROP: /* macro? */ |
5336 | s_retbool (is_macro (car (SCHEME_V->args))); |
5294 | s_retbool (is_macro (a)); |
5337 | } |
5295 | } |
5338 | |
5296 | |
5339 | abort (); |
5297 | abort (); |
5340 | } |
5298 | } |
5341 | |
5299 | |
|
|
5300 | /* dispatch functions (opexe_x) return new opcode, or 0 for same opcode, or -1 to stop */ |
5342 | typedef pointer (*dispatch_func) (SCHEME_P_ enum scheme_opcodes); |
5301 | typedef int (*dispatch_func)(SCHEME_P_ enum scheme_opcodes); |
5343 | |
5302 | |
5344 | typedef int (*test_predicate) (pointer); |
5303 | typedef int (*test_predicate)(pointer); |
5345 | static int |
5304 | static int |
5346 | is_any (pointer p) |
5305 | is_any (pointer p) |
5347 | { |
5306 | { |
5348 | return 1; |
5307 | return 1; |
5349 | } |
5308 | } |
5350 | |
5309 | |
5351 | static int |
5310 | static int |
5352 | is_nonneg (pointer p) |
5311 | is_nonneg (pointer p) |
5353 | { |
5312 | { |
5354 | return ivalue (p) >= 0 && is_integer (p); |
5313 | return ivalue (p) >= 0 && is_integer (p); |
|
|
5314 | } |
|
|
5315 | |
|
|
5316 | static int |
|
|
5317 | tst_is_list (pointer p) |
|
|
5318 | { |
|
|
5319 | return p == NIL || is_pair (p); |
5355 | } |
5320 | } |
5356 | |
5321 | |
5357 | /* Correspond carefully with following defines! */ |
5322 | /* Correspond carefully with following defines! */ |
5358 | static struct |
5323 | static struct |
5359 | { |
5324 | { |
5360 | test_predicate fct; |
5325 | test_predicate fct; |
5361 | const char *kind; |
5326 | const char *kind; |
5362 | } tests[] = |
5327 | } tests[] = |
5363 | { |
5328 | { |
5364 | { 0, 0}, /* unused */ |
5329 | { is_any, 0 }, |
5365 | { is_any, 0}, |
5330 | { is_string, "string" }, |
5366 | { is_string, "string" }, |
5331 | { is_symbol, "symbol" }, |
5367 | { is_symbol, "symbol" }, |
5332 | { is_port, "port" }, |
5368 | { is_port, "port" }, |
|
|
5369 | { is_inport, "input port" }, |
5333 | { is_inport, "input port" }, |
5370 | { is_outport, "output port" }, |
5334 | { is_outport, "output port" }, |
5371 | { is_environment, "environment" }, |
5335 | { is_environment, "environment" }, |
5372 | { is_pair, "pair" }, |
5336 | { is_pair, "pair" }, |
5373 | { 0, "pair or '()" }, |
5337 | { tst_is_list, "pair or '()" }, |
5374 | { is_character, "character" }, |
5338 | { is_character, "character" }, |
5375 | { is_vector, "vector" }, |
5339 | { is_vector, "vector" }, |
5376 | { is_number, "number" }, |
5340 | { is_number, "number" }, |
5377 | { is_integer, "integer" }, |
5341 | { is_integer, "integer" }, |
5378 | { is_nonneg, "non-negative integer" } |
5342 | { is_nonneg, "non-negative integer" } |
5379 | }; |
5343 | }; |
5380 | |
5344 | |
5381 | #define TST_NONE 0 |
5345 | #define TST_NONE 0 /* TST_NONE used for built-ins, 0 for internal ops */ |
5382 | #define TST_ANY "\001" |
5346 | #define TST_ANY "\001" |
5383 | #define TST_STRING "\002" |
5347 | #define TST_STRING "\002" |
5384 | #define TST_SYMBOL "\003" |
5348 | #define TST_SYMBOL "\003" |
5385 | #define TST_PORT "\004" |
5349 | #define TST_PORT "\004" |
5386 | #define TST_INPORT "\005" |
5350 | #define TST_INPORT "\005" |
5387 | #define TST_OUTPORT "\006" |
5351 | #define TST_OUTPORT "\006" |
5388 | #define TST_ENVIRONMENT "\007" |
5352 | #define TST_ENVIRONMENT "\007" |
5389 | #define TST_PAIR "\010" |
5353 | #define TST_PAIR "\010" |
5390 | #define TST_LIST "\011" |
5354 | #define TST_LIST "\011" |
5391 | #define TST_CHAR "\012" |
5355 | #define TST_CHAR "\012" |
5392 | #define TST_VECTOR "\013" |
5356 | #define TST_VECTOR "\013" |
5393 | #define TST_NUMBER "\014" |
5357 | #define TST_NUMBER "\014" |
5394 | #define TST_INTEGER "\015" |
5358 | #define TST_INTEGER "\015" |
5395 | #define TST_NATURAL "\016" |
5359 | #define TST_NATURAL "\016" |
|
|
5360 | |
|
|
5361 | #define INF_ARG 0xff |
|
|
5362 | #define UNNAMED_OP "" |
|
|
5363 | |
|
|
5364 | static const char opnames[] = |
|
|
5365 | #define OP_DEF(func,name,minarity,maxarity,argtest,op) name "\x00" |
|
|
5366 | #include "opdefines.h" |
|
|
5367 | #undef OP_DEF |
|
|
5368 | ; |
|
|
5369 | |
|
|
5370 | static const char * |
|
|
5371 | opname (int idx) |
|
|
5372 | { |
|
|
5373 | const char *name = opnames; |
|
|
5374 | |
|
|
5375 | /* should do this at compile time, but would require external program, right? */ |
|
|
5376 | while (idx--) |
|
|
5377 | name += strlen (name) + 1; |
|
|
5378 | |
|
|
5379 | return *name ? name : "ILLEGAL"; |
|
|
5380 | } |
|
|
5381 | |
|
|
5382 | static const char * |
|
|
5383 | procname (pointer x) |
|
|
5384 | { |
|
|
5385 | return opname (procnum (x)); |
|
|
5386 | } |
5396 | |
5387 | |
5397 | typedef struct |
5388 | typedef struct |
5398 | { |
5389 | { |
5399 | dispatch_func func; |
5390 | uint8_t func; |
5400 | char *name; |
5391 | /*dispatch_func func;*//*TODO: maybe optionally keep the pointer, for speed? */ |
|
|
5392 | uint8_t builtin; |
5401 | int min_arity; |
5393 | uint8_t min_arity; |
5402 | int max_arity; |
5394 | uint8_t max_arity; |
5403 | char *arg_tests_encoding; |
5395 | char arg_tests_encoding[3]; |
5404 | } op_code_info; |
5396 | } op_code_info; |
5405 | |
5397 | |
5406 | #define INF_ARG 0xffff |
|
|
5407 | |
|
|
5408 | static op_code_info dispatch_table[] = { |
5398 | static const op_code_info dispatch_table[] = { |
5409 | #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 }, |
5410 | #include "opdefines.h" |
5400 | #include "opdefines.h" |
|
|
5401 | #undef OP_DEF |
5411 | {0} |
5402 | {0} |
5412 | }; |
5403 | }; |
5413 | |
|
|
5414 | static const char * |
|
|
5415 | procname (pointer x) |
|
|
5416 | { |
|
|
5417 | int n = procnum (x); |
|
|
5418 | const char *name = dispatch_table[n].name; |
|
|
5419 | |
|
|
5420 | if (name == 0) |
|
|
5421 | name = "ILLEGAL!"; |
|
|
5422 | |
|
|
5423 | return name; |
|
|
5424 | } |
|
|
5425 | |
5404 | |
5426 | /* kernel of this interpreter */ |
5405 | /* kernel of this interpreter */ |
5427 | static void |
5406 | static void |
5428 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5407 | Eval_Cycle (SCHEME_P_ enum scheme_opcodes op) |
5429 | { |
5408 | { |
5430 | SCHEME_V->op = op; |
5409 | SCHEME_V->op = op; |
5431 | |
5410 | |
5432 | for (;;) |
5411 | for (;;) |
5433 | { |
5412 | { |
5434 | op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5413 | const op_code_info *pcd = dispatch_table + SCHEME_V->op; |
5435 | |
5414 | |
5436 | #if USE_ERROR_CHECKING |
5415 | #if USE_ERROR_CHECKING |
5437 | if (pcd->name) /* if built-in function, check arguments */ |
5416 | if (pcd->builtin) /* if built-in function, check arguments */ |
5438 | { |
5417 | { |
5439 | int ok = 1; |
|
|
5440 | char msg[STRBUFFSIZE]; |
5418 | char msg[STRBUFFSIZE]; |
5441 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5419 | int n = list_length (SCHEME_A_ SCHEME_V->args); |
5442 | |
5420 | |
5443 | /* Check number of arguments */ |
5421 | /* Check number of arguments */ |
5444 | if (ecb_expect_false (n < pcd->min_arity)) |
5422 | if (ecb_expect_false (n < pcd->min_arity)) |
5445 | { |
5423 | { |
5446 | ok = 0; |
|
|
5447 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5424 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5448 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
5425 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at least", pcd->min_arity); |
|
|
5426 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5427 | continue; |
5449 | } |
5428 | } |
5450 | else if (ecb_excpect_false (n > pcd->max_arity)) |
5429 | else if (ecb_expect_false (n > pcd->max_arity && pcd->max_arity != INF_ARG)) |
5451 | { |
5430 | { |
5452 | ok = 0; |
|
|
5453 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5431 | snprintf (msg, STRBUFFSIZE, "%s: needs%s %d argument(s)", |
5454 | pcd->name, pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
5432 | opname (SCHEME_V->op), pcd->min_arity == pcd->max_arity ? "" : " at most", pcd->max_arity); |
|
|
5433 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5434 | continue; |
5455 | } |
5435 | } |
5456 | |
5436 | else |
5457 | if (ecb_expect_false (ok)) |
|
|
5458 | { |
5437 | { |
5459 | if (pcd->arg_tests_encoding) |
5438 | if (*pcd->arg_tests_encoding) /* literal 0 and TST_NONE treated the same */ |
5460 | { |
5439 | { |
5461 | int i = 0; |
5440 | int i = 0; |
5462 | int j; |
5441 | int j; |
5463 | const char *t = pcd->arg_tests_encoding; |
5442 | const char *t = pcd->arg_tests_encoding; |
5464 | pointer arglist = SCHEME_V->args; |
5443 | pointer arglist = SCHEME_V->args; |
5465 | |
5444 | |
5466 | do |
5445 | do |
5467 | { |
5446 | { |
5468 | pointer arg = car (arglist); |
5447 | pointer arg = car (arglist); |
5469 | |
5448 | |
5470 | j = (int) t[0]; |
5449 | j = t[0]; |
5471 | |
5450 | |
5472 | if (j == TST_LIST[0]) |
5451 | if (!tests[j - 1].fct (arg)) |
5473 | { |
|
|
5474 | if (arg != NIL && !is_pair (arg)) |
|
|
5475 | break; |
5452 | break; |
5476 | } |
|
|
5477 | else |
|
|
5478 | { |
|
|
5479 | if (!tests[j].fct (arg)) |
|
|
5480 | break; |
|
|
5481 | } |
|
|
5482 | |
5453 | |
5483 | if (t[1] != 0) /* last test is replicated as necessary */ |
5454 | if (t[1]) /* last test is replicated as necessary */ |
5484 | t++; |
5455 | t++; |
5485 | |
5456 | |
5486 | arglist = cdr (arglist); |
5457 | arglist = cdr (arglist); |
5487 | i++; |
5458 | i++; |
5488 | } |
5459 | } |
5489 | while (i < n); |
5460 | while (i < n); |
5490 | |
5461 | |
5491 | if (i < n) |
5462 | if (i < n) |
5492 | { |
5463 | { |
5493 | ok = 0; |
|
|
5494 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", pcd->name, i + 1, tests[j].kind); |
5464 | snprintf (msg, STRBUFFSIZE, "%s: argument %d must be: %s", opname (SCHEME_V->op), i + 1, tests[j].kind); |
|
|
5465 | xError_1 (SCHEME_A_ msg, 0); |
|
|
5466 | continue; |
5495 | } |
5467 | } |
5496 | } |
5468 | } |
5497 | } |
5469 | } |
5498 | |
|
|
5499 | if (!ok) |
|
|
5500 | { |
|
|
5501 | if (xError_1 (SCHEME_A_ msg, 0) == NIL) |
|
|
5502 | return; |
|
|
5503 | |
|
|
5504 | pcd = dispatch_table + SCHEME_V->op; |
|
|
5505 | } |
|
|
5506 | } |
5470 | } |
5507 | #endif |
5471 | #endif |
5508 | |
5472 | |
5509 | ok_to_freely_gc (SCHEME_A); |
5473 | ok_to_freely_gc (SCHEME_A); |
5510 | |
5474 | |
|
|
5475 | static const dispatch_func dispatch_funcs[] = { |
|
|
5476 | opexe_0, |
|
|
5477 | opexe_1, |
|
|
5478 | opexe_2, |
|
|
5479 | opexe_3, |
|
|
5480 | opexe_4, |
|
|
5481 | opexe_5, |
|
|
5482 | opexe_6, |
|
|
5483 | }; |
|
|
5484 | |
5511 | if (ecb_expect_false (pcd->func (SCHEME_A_ SCHEME_V->op) == NIL)) |
5485 | if (ecb_expect_false (dispatch_funcs [pcd->func] (SCHEME_A_ SCHEME_V->op) != 0)) |
5512 | return; |
5486 | return; |
5513 | |
5487 | |
5514 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5488 | if (SCHEME_V->no_memory && USE_ERROR_CHECKING) |
5515 | { |
5489 | { |
5516 | xwrstr ("No memory!\n"); |
5490 | xwrstr ("No memory!\n"); |
… | |
… | |
5713 | |
5687 | |
5714 | for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) |
5688 | for (i = 0; i < sizeof (syntax_names) / sizeof (*syntax_names); ++i) |
5715 | assign_syntax (SCHEME_A_ syntax_names[i]); |
5689 | assign_syntax (SCHEME_A_ syntax_names[i]); |
5716 | } |
5690 | } |
5717 | |
5691 | |
|
|
5692 | // TODO: should iterate via strlen, to avoid n² complexity |
5718 | for (i = 0; i < n; i++) |
5693 | for (i = 0; i < n; i++) |
5719 | if (dispatch_table[i].name != 0) |
5694 | if (dispatch_table[i].builtin) |
5720 | assign_proc (SCHEME_A_ i, dispatch_table[i].name); |
5695 | assign_proc (SCHEME_A_ i, opname (i)); |
5721 | |
5696 | |
5722 | /* initialization of global pointers to special symbols */ |
5697 | /* initialization of global pointers to special symbols */ |
5723 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
5698 | SCHEME_V->LAMBDA = mk_symbol (SCHEME_A_ "lambda"); |
5724 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
5699 | SCHEME_V->QUOTE = mk_symbol (SCHEME_A_ "quote"); |
5725 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
5700 | SCHEME_V->QQUOTE = mk_symbol (SCHEME_A_ "quasiquote"); |
… | |
… | |
5866 | { |
5841 | { |
5867 | dump_stack_reset (SCHEME_A); |
5842 | dump_stack_reset (SCHEME_A); |
5868 | SCHEME_V->envir = SCHEME_V->global_env; |
5843 | SCHEME_V->envir = SCHEME_V->global_env; |
5869 | SCHEME_V->file_i = 0; |
5844 | SCHEME_V->file_i = 0; |
5870 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5845 | SCHEME_V->load_stack[0].kind = port_input | port_string; |
5871 | SCHEME_V->load_stack[0].rep.string.start = (char *) cmd; /* This func respects const */ |
5846 | SCHEME_V->load_stack[0].rep.string.start = (char *)cmd; /* This func respects const */ |
5872 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *) cmd + strlen (cmd); |
5847 | SCHEME_V->load_stack[0].rep.string.past_the_end = (char *)cmd + strlen (cmd); |
5873 | SCHEME_V->load_stack[0].rep.string.curr = (char *) cmd; |
5848 | SCHEME_V->load_stack[0].rep.string.curr = (char *)cmd; |
5874 | #if USE_PORTS |
5849 | #if USE_PORTS |
5875 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5850 | SCHEME_V->loadport = mk_port (SCHEME_A_ SCHEME_V->load_stack); |
5876 | #endif |
5851 | #endif |
5877 | SCHEME_V->retcode = 0; |
5852 | SCHEME_V->retcode = 0; |
5878 | SCHEME_V->interactive_repl = 0; |
5853 | SCHEME_V->interactive_repl = 0; |