1 /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "libguile/_scm.h"
22 #include "libguile/eval.h"
23 #include "libguile/alist.h"
24 #include "libguile/fluids.h"
25 #include "libguile/ports.h"
26 #include "libguile/smob.h"
28 #include "libguile/dynwind.h"
33 Things that can be on the wind list:
35 (enter-proc . leave-proc) dynamic-wind
37 (tag . lazy-catch) lazy-catch
38 tag is either a symbol or a boolean
40 ((fluid ...) . (value ...)) with-fluids
46 SCM_DEFINE (scm_dynamic_wind
, "dynamic-wind", 3, 0, 0,
47 (SCM in_guard
, SCM thunk
, SCM out_guard
),
48 "All three arguments must be 0-argument procedures.\n"
49 "@var{in_guard} is called, then @var{thunk}, then\n"
52 "If, any time during the execution of @var{thunk}, the\n"
53 "continuation of the @code{dynamic_wind} expression is escaped\n"
54 "non-locally, @var{out_guard} is called. If the continuation of\n"
55 "the dynamic-wind is re-entered, @var{in_guard} is called. Thus\n"
56 "@var{in_guard} and @var{out_guard} may be called any number of\n"
59 "(define x 'normal-binding)\n"
61 "(define a-cont (call-with-current-continuation\n"
67 " (lambda () (set! x 'special-binding))\n"
71 " (lambda () (display x) (newline)\n"
72 " (call-with-current-continuation escape)\n"
73 " (display x) (newline)\n"
78 " (lambda () (set! x old-x)))))))\n"
85 "@result{} normal-binding\n"
90 "@result{} a-cont ;; the value of the (define a-cont...)\n"
92 "@result{} normal-binding\n"
94 "@result{} special-binding\n"
96 #define FUNC_NAME s_scm_dynamic_wind
99 SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (out_guard
)),
101 SCM_ARG3
, FUNC_NAME
);
102 scm_call_0 (in_guard
);
103 scm_dynwinds
= scm_acons (in_guard
, out_guard
, scm_dynwinds
);
104 ans
= scm_call_0 (thunk
);
105 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
106 scm_call_0 (out_guard
);
111 /* The implementation of a C-callable dynamic-wind,
112 * scm_internal_dynamic_wind, requires packaging of C pointers in a
113 * smob. Objects of this type are pushed onto the dynwind chain.
116 #define SCM_GUARDSP(obj) SCM_TYP16_PREDICATE (tc16_guards, obj)
117 #define SCM_BEFORE_GUARD(obj) ((scm_t_guard) SCM_CELL_WORD (obj, 1))
118 #define SCM_AFTER_GUARD(obj) ((scm_t_guard) SCM_CELL_WORD (obj, 2))
119 #define SCM_GUARD_DATA(obj) ((void *) SCM_CELL_WORD (obj, 3))
121 static scm_t_bits tc16_guards
;
124 guards_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
126 scm_puts ("#<guards ", port
);
127 scm_intprint (SCM_UNPACK (SCM_CDR (exp
)), 16, port
);
128 scm_putc ('>', port
);
133 scm_internal_dynamic_wind (scm_t_guard before
,
141 SCM_NEWSMOB3 (guards
, tc16_guards
, (scm_t_bits
) before
,
142 (scm_t_bits
) after
, (scm_t_bits
) guard_data
);
143 scm_dynwinds
= scm_acons (guards
, SCM_BOOL_F
, scm_dynwinds
);
144 ans
= inner (inner_data
);
145 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
151 SCM_DEFINE (scm_wind_chain
, "wind-chain", 0, 0, 0,
153 "Return the current wind chain. The wind chain contains all\n"
154 "information required by @code{dynamic-wind} to call its\n"
155 "argument thunks when entering/exiting its scope.")
156 #define FUNC_NAME s_scm_wind_chain
164 scm_swap_bindings (SCM vars
, SCM vals
)
167 while (SCM_NIMP (vals
))
169 tmp
= SCM_VARIABLE_REF (SCM_CAR (vars
));
170 SCM_VARIABLE_SET (SCM_CAR (vars
), SCM_CAR (vals
));
171 SCM_SETCAR (vals
, tmp
);
172 vars
= SCM_CDR (vars
);
173 vals
= SCM_CDR (vals
);
178 scm_dowinds (SCM to
, long delta
)
181 if (SCM_EQ_P (to
, scm_dynwinds
));
187 scm_dowinds (SCM_CDR (to
), 1 + delta
);
188 wind_elt
= SCM_CAR (to
);
190 if (SCM_INUMP (wind_elt
))
192 scm_cross_dynwind_binding_scope (wind_elt
, 0);
197 wind_key
= SCM_CAR (wind_elt
);
198 /* key = #t | symbol | thunk | list of variables | list of fluids */
199 if (SCM_NIMP (wind_key
))
201 if (SCM_CONSP (wind_key
))
203 if (SCM_VARIABLEP (SCM_CAR (wind_key
)))
204 scm_swap_bindings (wind_key
, SCM_CDR (wind_elt
));
205 else if (SCM_FLUIDP (SCM_CAR (wind_key
)))
206 scm_swap_fluids (wind_key
, SCM_CDR (wind_elt
));
208 else if (SCM_GUARDSP (wind_key
))
209 SCM_BEFORE_GUARD (wind_key
) (SCM_GUARD_DATA (wind_key
));
210 else if (SCM_TYP3 (wind_key
) == scm_tc3_closure
)
211 scm_call_0 (wind_key
);
222 from
= SCM_CDR (SCM_CAR (scm_dynwinds
));
223 wind_elt
= SCM_CAR (scm_dynwinds
);
224 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
226 if (SCM_INUMP (wind_elt
))
228 scm_cross_dynwind_binding_scope (wind_elt
, 0);
233 wind_key
= SCM_CAR (wind_elt
);
234 if (SCM_NIMP (wind_key
))
236 if (SCM_CONSP (wind_key
))
238 if (SCM_VARIABLEP (SCM_CAR (wind_key
)))
239 scm_swap_bindings (wind_key
, SCM_CDR (wind_elt
));
240 else if (SCM_FLUIDP (SCM_CAR (wind_key
)))
241 scm_swap_fluids_reverse (wind_key
, SCM_CDR (wind_elt
));
243 else if (SCM_GUARDSP (wind_key
))
244 SCM_AFTER_GUARD (wind_key
) (SCM_GUARD_DATA (wind_key
));
245 else if (SCM_TYP3 (wind_key
) == scm_tc3_closure
)
250 goto tail
; /* scm_dowinds(to, delta-1); */
259 tc16_guards
= scm_make_smob_type ("guards", 0);
260 scm_set_smob_print (tc16_guards
, guards_print
);
261 #include "libguile/dynwind.x"