1 /* Copyright (C) 1995, 1996, 1998, 1999 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program 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
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
56 Things that can be on the wind list:
58 (enter-proc . leave-proc) dynamic-wind
60 (tag . lazy-catch) lazy-catch
61 tag is either a symbol or a boolean
63 ((fluid ...) . (value ...)) with-fluids
69 SCM_PROC(s_dynamic_wind
, "dynamic-wind", 3, 0, 0, scm_dynamic_wind
);
72 scm_dynamic_wind (thunk1
, thunk2
, thunk3
)
78 SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (thunk3
)),
80 SCM_ARG3
, s_dynamic_wind
);
81 scm_apply (thunk1
, SCM_EOL
, SCM_EOL
);
82 scm_dynwinds
= scm_acons (thunk1
, thunk3
, scm_dynwinds
);
83 ans
= scm_apply (thunk2
, SCM_EOL
, SCM_EOL
);
84 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
85 scm_apply (thunk3
, SCM_EOL
, SCM_EOL
);
89 /* The implementation of a C-callable dynamic-wind,
90 * scm_internal_dynamic_wind, requires packaging of C pointers in a
91 * smob. Objects of this type are pushed onto the dynwind chain.
94 typedef struct guardsmem
{
100 #define SCM_GUARDSMEM(obj) ((guardsmem *) SCM_CDR (obj))
101 #define SCM_BEFORE_GUARD(obj) (SCM_GUARDSMEM (obj)->before)
102 #define SCM_AFTER_GUARD(obj) (SCM_GUARDSMEM (obj)->after)
103 #define SCM_GUARD_DATA(obj) (SCM_GUARDSMEM (obj)->data)
104 #define SCM_GUARDSP(obj) (SCM_CAR (obj) == tc16_guards)
106 static long tc16_guards
;
109 freeguards (SCM guards
)
111 scm_must_free ((char *) SCM_CDR (guards
));
112 return sizeof (guardsmem
);
116 printguards (SCM exp
, SCM port
, scm_print_state
*pstate
)
118 scm_puts ("#<guards ", port
);
119 scm_intprint (SCM_CDR (exp
), 16, port
);
120 scm_putc ('>', port
);
125 scm_internal_dynamic_wind (scm_guard_t before
,
134 g
= (guardsmem
*) scm_must_malloc (sizeof (*g
), "guards");
137 g
->data
= guard_data
;
138 SCM_NEWSMOB (guards
, tc16_guards
, g
);
139 scm_dynwinds
= scm_acons (guards
, SCM_BOOL_F
, scm_dynwinds
);
140 ans
= inner (inner_data
);
141 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
147 SCM_PROC (s_wind_chain
, "wind-chain", 0, 0, 0, scm_wind_chain
);
157 scm_swap_bindings (SCM glocs
, SCM vals
)
160 while (SCM_NIMP (vals
))
162 tmp
= SCM_GLOC_VAL (SCM_CAR (glocs
));
163 SCM_SETCDR (SCM_CAR (glocs
) - 1L, SCM_CAR (vals
));
164 SCM_SETCAR (vals
, tmp
);
165 glocs
= SCM_CDR (glocs
);
166 vals
= SCM_CDR (vals
);
171 scm_dowinds (to
, delta
)
176 if (scm_dynwinds
== to
);
182 scm_dowinds (SCM_CDR (to
), 1 + delta
);
183 wind_elt
= SCM_CAR (to
);
185 if (SCM_INUMP (wind_elt
))
187 scm_cross_dynwind_binding_scope (wind_elt
, 0);
192 wind_key
= SCM_CAR (wind_elt
);
193 /* key = #t | symbol | thunk | list of glocs | list of fluids */
194 if (SCM_NIMP (wind_key
))
196 if (SCM_TYP3 (wind_key
) == scm_tc3_cons_gloc
)
197 scm_swap_bindings (wind_key
, SCM_CDR (wind_elt
));
198 else if (SCM_TYP3 (wind_key
) == scm_tc3_cons
)
199 scm_swap_fluids (wind_key
, SCM_CDR (wind_elt
));
200 else if (SCM_GUARDSP (wind_key
))
201 SCM_BEFORE_GUARD (wind_key
) (SCM_GUARD_DATA (wind_key
));
202 else if (SCM_TYP3 (wind_key
) == scm_tc3_closure
)
203 scm_apply (wind_key
, SCM_EOL
, SCM_EOL
);
214 from
= SCM_CDR (SCM_CAR (scm_dynwinds
));
215 wind_elt
= SCM_CAR (scm_dynwinds
);
216 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
218 if (SCM_INUMP (wind_elt
))
220 scm_cross_dynwind_binding_scope (wind_elt
, 0);
225 wind_key
= SCM_CAR (wind_elt
);
226 if (SCM_NIMP (wind_key
))
228 if (SCM_TYP3 (wind_key
) == scm_tc3_cons_gloc
)
229 scm_swap_bindings (wind_key
, from
);
230 else if (SCM_TYP3 (wind_key
) == scm_tc3_cons
)
231 scm_swap_fluids_reverse (wind_key
, from
);
232 else if (SCM_GUARDSP (wind_key
))
233 SCM_AFTER_GUARD (wind_key
) (SCM_GUARD_DATA (wind_key
));
234 else if (SCM_TYP3 (wind_key
) == scm_tc3_closure
)
235 scm_apply (from
, SCM_EOL
, SCM_EOL
);
239 goto tail
; /* scm_dowinds(to, delta-1); */
248 tc16_guards
= scm_make_smob_type_mfpe ("guards", sizeof (struct guardsmem
),
249 NULL
, freeguards
, printguards
, NULL
);