1 /* Copyright (C) 1999,2000,2001, 2003 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
20 #include "libguile/_scm.h"
21 #include "libguile/alist.h"
22 #include "libguile/eval.h"
23 #include "libguile/gh.h"
24 #include "libguile/hash.h"
25 #include "libguile/list.h"
26 #include "libguile/ports.h"
27 #include "libguile/smob.h"
28 #include "libguile/symbols.h"
29 #include "libguile/vectors.h"
30 #include "libguile/weaks.h"
32 #include "libguile/environments.h"
36 scm_t_bits scm_tc16_environment
;
37 scm_t_bits scm_tc16_observer
;
38 #define DEFAULT_OBARRAY_SIZE 31
40 SCM scm_system_environment
;
44 /* error conditions */
47 * Throw an error if symbol is not bound in environment func
50 scm_error_environment_unbound (const char *func
, SCM env
, SCM symbol
)
52 /* Dirk:FIXME:: Should throw an environment:unbound type error */
53 char error
[] = "Symbol `~A' not bound in environment `~A'.";
54 SCM arguments
= scm_cons2 (symbol
, env
, SCM_EOL
);
55 scm_misc_error (func
, error
, arguments
);
60 * Throw an error if func tried to create (define) or remove
61 * (undefine) a new binding for symbol in env
64 scm_error_environment_immutable_binding (const char *func
, SCM env
, SCM symbol
)
66 /* Dirk:FIXME:: Should throw an environment:immutable-binding type error */
67 char error
[] = "Immutable binding in environment ~A (symbol: `~A').";
68 SCM arguments
= scm_cons2 (env
, symbol
, SCM_EOL
);
69 scm_misc_error (func
, error
, arguments
);
74 * Throw an error if func tried to change an immutable location.
77 scm_error_environment_immutable_location (const char *func
, SCM env
, SCM symbol
)
79 /* Dirk:FIXME:: Should throw an environment:immutable-location type error */
80 char error
[] = "Immutable location in environment `~A' (symbol: `~A').";
81 SCM arguments
= scm_cons2 (env
, symbol
, SCM_EOL
);
82 scm_misc_error (func
, error
, arguments
);
87 /* generic environments */
90 /* Create an environment for the given type. Dereferencing type twice must
91 * deliver the initialized set of environment functions. Thus, type will
92 * also determine the signature of the underlying environment implementation.
93 * Dereferencing type once will typically deliver the data fields used by the
94 * underlying environment implementation.
97 scm_make_environment (void *type
)
99 return scm_cell (scm_tc16_environment
, (scm_t_bits
) type
);
103 SCM_DEFINE (scm_environment_p
, "environment?", 1, 0, 0,
105 "Return @code{#t} if @var{obj} is an environment, or @code{#f}\n"
107 #define FUNC_NAME s_scm_environment_p
109 return scm_from_bool (SCM_ENVIRONMENT_P (obj
));
114 SCM_DEFINE (scm_environment_bound_p
, "environment-bound?", 2, 0, 0,
116 "Return @code{#t} if @var{sym} is bound in @var{env}, or\n"
117 "@code{#f} otherwise.")
118 #define FUNC_NAME s_scm_environment_bound_p
120 SCM_ASSERT (SCM_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
121 SCM_ASSERT (scm_is_symbol (sym
), sym
, SCM_ARG2
, FUNC_NAME
);
123 return scm_from_bool (SCM_ENVIRONMENT_BOUND_P (env
, sym
));
128 SCM_DEFINE (scm_environment_ref
, "environment-ref", 2, 0, 0,
130 "Return the value of the location bound to @var{sym} in\n"
131 "@var{env}. If @var{sym} is unbound in @var{env}, signal an\n"
132 "@code{environment:unbound} error.")
133 #define FUNC_NAME s_scm_environment_ref
137 SCM_ASSERT (SCM_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
138 SCM_ASSERT (scm_is_symbol (sym
), sym
, SCM_ARG2
, FUNC_NAME
);
140 val
= SCM_ENVIRONMENT_REF (env
, sym
);
142 if (!SCM_UNBNDP (val
))
145 scm_error_environment_unbound (FUNC_NAME
, env
, sym
);
150 /* This C function is identical to environment-ref, except that if symbol is
151 * unbound in env, it returns the value SCM_UNDEFINED, instead of signalling
155 scm_c_environment_ref (SCM env
, SCM sym
)
157 SCM_ASSERT (SCM_ENVIRONMENT_P (env
), env
, SCM_ARG1
, "scm_c_environment_ref");
158 SCM_ASSERT (scm_is_symbol (sym
), sym
, SCM_ARG2
, "scm_c_environment_ref");
159 return SCM_ENVIRONMENT_REF (env
, sym
);
164 environment_default_folder (SCM proc
, SCM symbol
, SCM value
, SCM tail
)
166 return gh_call3 (proc
, symbol
, value
, tail
);
170 SCM_DEFINE (scm_environment_fold
, "environment-fold", 3, 0, 0,
171 (SCM env
, SCM proc
, SCM init
),
172 "Iterate over all the bindings in @var{env}, accumulating some\n"
174 "For each binding in @var{env}, apply @var{proc} to the symbol\n"
175 "bound, its value, and the result from the previous application\n"
177 "Use @var{init} as @var{proc}'s third argument the first time\n"
178 "@var{proc} is applied.\n"
179 "If @var{env} contains no bindings, this function simply returns\n"
181 "If @var{env} binds the symbol sym1 to the value val1, sym2 to\n"
182 "val2, and so on, then this procedure computes:\n"
190 "Each binding in @var{env} will be processed exactly once.\n"
191 "@code{environment-fold} makes no guarantees about the order in\n"
192 "which the bindings are processed.\n"
193 "Here is a function which, given an environment, constructs an\n"
194 "association list representing that environment's bindings,\n"
195 "using environment-fold:\n"
197 " (define (environment->alist env)\n"
198 " (environment-fold env\n"
199 " (lambda (sym val tail)\n"
200 " (cons (cons sym val) tail))\n"
203 #define FUNC_NAME s_scm_environment_fold
205 SCM_ASSERT (SCM_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
206 SCM_ASSERT (scm_is_true (scm_procedure_p (proc
)),
207 proc
, SCM_ARG2
, FUNC_NAME
);
209 return SCM_ENVIRONMENT_FOLD (env
, environment_default_folder
, proc
, init
);
214 /* This is the C-level analog of environment-fold. For each binding in ENV,
216 * (*proc) (data, symbol, value, previous)
217 * where previous is the value returned from the last call to *PROC, or INIT
218 * for the first call. If ENV contains no bindings, return INIT.
221 scm_c_environment_fold (SCM env
, scm_environment_folder proc
, SCM data
, SCM init
)
223 SCM_ASSERT (SCM_ENVIRONMENT_P (env
), env
, SCM_ARG1
, "scm_c_environment_fold");
225 return SCM_ENVIRONMENT_FOLD (env
, proc
, data
, init
);
229 SCM_DEFINE (scm_environment_define
, "environment-define", 3, 0, 0,
230 (SCM env
, SCM sym
, SCM val
),
231 "Bind @var{sym} to a new location containing @var{val} in\n"
232 "@var{env}. If @var{sym} is already bound to another location\n"
233 "in @var{env} and the binding is mutable, that binding is\n"
234 "replaced. The new binding and location are both mutable. The\n"
235 "return value is unspecified.\n"
236 "If @var{sym} is already bound in @var{env}, and the binding is\n"
237 "immutable, signal an @code{environment:immutable-binding} error.")
238 #define FUNC_NAME s_scm_environment_define
242 SCM_ASSERT (SCM_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
243 SCM_ASSERT (scm_is_symbol (sym
), sym
, SCM_ARG2
, FUNC_NAME
);
245 status
= SCM_ENVIRONMENT_DEFINE (env
, sym
, val
);
247 if (scm_is_eq (status
, SCM_ENVIRONMENT_SUCCESS
))
248 return SCM_UNSPECIFIED
;
249 else if (scm_is_eq (status
, SCM_ENVIRONMENT_BINDING_IMMUTABLE
))
250 scm_error_environment_immutable_binding (FUNC_NAME
, env
, sym
);
257 SCM_DEFINE (scm_environment_undefine
, "environment-undefine", 2, 0, 0,
259 "Remove any binding for @var{sym} from @var{env}. If @var{sym}\n"
260 "is unbound in @var{env}, do nothing. The return value is\n"
262 "If @var{sym} is already bound in @var{env}, and the binding is\n"
263 "immutable, signal an @code{environment:immutable-binding} error.")
264 #define FUNC_NAME s_scm_environment_undefine
268 SCM_ASSERT(SCM_ENVIRONMENT_P(env
), env
, SCM_ARG1
, FUNC_NAME
);
269 SCM_ASSERT(scm_is_symbol(sym
), sym
, SCM_ARG2
, FUNC_NAME
);
271 status
= SCM_ENVIRONMENT_UNDEFINE (env
, sym
);
273 if (scm_is_eq (status
, SCM_ENVIRONMENT_SUCCESS
))
274 return SCM_UNSPECIFIED
;
275 else if (scm_is_eq (status
, SCM_ENVIRONMENT_BINDING_IMMUTABLE
))
276 scm_error_environment_immutable_binding (FUNC_NAME
, env
, sym
);
283 SCM_DEFINE (scm_environment_set_x
, "environment-set!", 3, 0, 0,
284 (SCM env
, SCM sym
, SCM val
),
285 "If @var{env} binds @var{sym} to some location, change that\n"
286 "location's value to @var{val}. The return value is\n"
288 "If @var{sym} is not bound in @var{env}, signal an\n"
289 "@code{environment:unbound} error. If @var{env} binds @var{sym}\n"
290 "to an immutable location, signal an\n"
291 "@code{environment:immutable-location} error.")
292 #define FUNC_NAME s_scm_environment_set_x
296 SCM_ASSERT (SCM_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
297 SCM_ASSERT (scm_is_symbol (sym
), sym
, SCM_ARG2
, FUNC_NAME
);
299 status
= SCM_ENVIRONMENT_SET (env
, sym
, val
);
301 if (scm_is_eq (status
, SCM_ENVIRONMENT_SUCCESS
))
302 return SCM_UNSPECIFIED
;
303 else if (SCM_UNBNDP (status
))
304 scm_error_environment_unbound (FUNC_NAME
, env
, sym
);
305 else if (scm_is_eq (status
, SCM_ENVIRONMENT_LOCATION_IMMUTABLE
))
306 scm_error_environment_immutable_binding (FUNC_NAME
, env
, sym
);
313 SCM_DEFINE (scm_environment_cell
, "environment-cell", 3, 0, 0,
314 (SCM env
, SCM sym
, SCM for_write
),
315 "Return the value cell which @var{env} binds to @var{sym}, or\n"
316 "@code{#f} if the binding does not live in a value cell.\n"
317 "The argument @var{for-write} indicates whether the caller\n"
318 "intends to modify the variable's value by mutating the value\n"
319 "cell. If the variable is immutable, then\n"
320 "@code{environment-cell} signals an\n"
321 "@code{environment:immutable-location} error.\n"
322 "If @var{sym} is unbound in @var{env}, signal an\n"
323 "@code{environment:unbound} error.\n"
324 "If you use this function, you should consider using\n"
325 "@code{environment-observe}, to be notified when @var{sym} gets\n"
326 "re-bound to a new value cell, or becomes undefined.")
327 #define FUNC_NAME s_scm_environment_cell
331 SCM_ASSERT (SCM_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
332 SCM_ASSERT (scm_is_symbol (sym
), sym
, SCM_ARG2
, FUNC_NAME
);
333 SCM_ASSERT (scm_is_bool (for_write
), for_write
, SCM_ARG3
, FUNC_NAME
);
335 location
= SCM_ENVIRONMENT_CELL (env
, sym
, scm_is_true (for_write
));
336 if (!SCM_IMP (location
))
338 else if (SCM_UNBNDP (location
))
339 scm_error_environment_unbound (FUNC_NAME
, env
, sym
);
340 else if (scm_is_eq (location
, SCM_ENVIRONMENT_LOCATION_IMMUTABLE
))
341 scm_error_environment_immutable_location (FUNC_NAME
, env
, sym
);
348 /* This C function is identical to environment-cell, with the following
349 * exceptions: If symbol is unbound in env, it returns the value
350 * SCM_UNDEFINED, instead of signalling an error. If symbol is bound to an
351 * immutable location but the cell is requested for write, the value
352 * SCM_ENVIRONMENT_LOCATION_IMMUTABLE is returned.
355 scm_c_environment_cell(SCM env
, SCM sym
, int for_write
)
357 SCM_ASSERT (SCM_ENVIRONMENT_P (env
), env
, SCM_ARG1
, "scm_c_environment_cell");
358 SCM_ASSERT (scm_is_symbol (sym
), sym
, SCM_ARG2
, "scm_c_environment_cell");
360 return SCM_ENVIRONMENT_CELL (env
, sym
, for_write
);
365 environment_default_observer (SCM env
, SCM proc
)
367 gh_call1 (proc
, env
);
371 SCM_DEFINE (scm_environment_observe
, "environment-observe", 2, 0, 0,
373 "Whenever @var{env}'s bindings change, apply @var{proc} to\n"
375 "This function returns an object, token, which you can pass to\n"
376 "@code{environment-unobserve} to remove @var{proc} from the set\n"
377 "of procedures observing @var{env}. The type and value of\n"
378 "token is unspecified.")
379 #define FUNC_NAME s_scm_environment_observe
381 SCM_ASSERT (SCM_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
383 return SCM_ENVIRONMENT_OBSERVE (env
, environment_default_observer
, proc
, 0);
388 SCM_DEFINE (scm_environment_observe_weak
, "environment-observe-weak", 2, 0, 0,
390 "This function is the same as environment-observe, except that\n"
391 "the reference @var{env} retains to @var{proc} is a weak\n"
392 "reference. This means that, if there are no other live,\n"
393 "non-weak references to @var{proc}, it will be\n"
394 "garbage-collected, and dropped from @var{env}'s\n"
395 "list of observing procedures.")
396 #define FUNC_NAME s_scm_environment_observe_weak
398 SCM_ASSERT (SCM_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
400 return SCM_ENVIRONMENT_OBSERVE (env
, environment_default_observer
, proc
, 1);
405 /* This is the C-level analog of the Scheme functions environment-observe and
406 * environment-observe-weak. Whenever env's bindings change, call the
407 * function proc, passing it env and data. If weak_p is non-zero, env will
408 * retain only a weak reference to data, and if data is garbage collected, the
409 * entire observation will be dropped. This function returns a token, with
410 * the same meaning as those returned by environment-observe and
411 * environment-observe-weak.
414 scm_c_environment_observe (SCM env
, scm_environment_observer proc
, SCM data
, int weak_p
)
415 #define FUNC_NAME "scm_c_environment_observe"
417 SCM_ASSERT (SCM_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
419 return SCM_ENVIRONMENT_OBSERVE (env
, proc
, data
, weak_p
);
424 SCM_DEFINE (scm_environment_unobserve
, "environment-unobserve", 1, 0, 0,
426 "Cancel the observation request which returned the value\n"
427 "@var{token}. The return value is unspecified.\n"
428 "If a call @code{(environment-observe env proc)} returns\n"
429 "@var{token}, then the call @code{(environment-unobserve token)}\n"
430 "will cause @var{proc} to no longer be called when @var{env}'s\n"
432 #define FUNC_NAME s_scm_environment_unobserve
436 SCM_ASSERT (SCM_OBSERVER_P (token
), token
, SCM_ARG1
, FUNC_NAME
);
438 env
= SCM_OBSERVER_ENVIRONMENT (token
);
439 SCM_ENVIRONMENT_UNOBSERVE (env
, token
);
441 return SCM_UNSPECIFIED
;
447 environment_mark (SCM env
)
449 return (*(SCM_ENVIRONMENT_FUNCS (env
)->mark
)) (env
);
454 environment_free (SCM env
)
456 (*(SCM_ENVIRONMENT_FUNCS (env
)->free
)) (env
);
462 environment_print (SCM env
, SCM port
, scm_print_state
*pstate
)
464 return (*(SCM_ENVIRONMENT_FUNCS (env
)->print
)) (env
, port
, pstate
);
472 observer_mark (SCM observer
)
474 scm_gc_mark (SCM_OBSERVER_ENVIRONMENT (observer
));
475 scm_gc_mark (SCM_OBSERVER_DATA (observer
));
481 observer_print (SCM type
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
483 SCM address
= scm_from_size_t (SCM_UNPACK (type
));
484 SCM base16
= scm_number_to_string (address
, scm_from_int (16));
486 scm_puts ("#<observer ", port
);
487 scm_display (base16
, port
);
488 scm_puts (">", port
);
497 * Obarrays form the basic lookup tables used to implement most of guile's
498 * built-in environment types. An obarray is implemented as a hash table with
499 * symbols as keys. The content of the data depends on the environment type.
504 * Enter symbol into obarray. The symbol must not already exist in obarray.
505 * The freshly generated (symbol . data) cell is returned.
508 obarray_enter (SCM obarray
, SCM symbol
, SCM data
)
510 size_t hash
= scm_i_symbol_hash (symbol
) % SCM_HASHTABLE_N_BUCKETS (obarray
);
511 SCM entry
= scm_cons (symbol
, data
);
512 SCM slot
= scm_cons (entry
, SCM_HASHTABLE_BUCKETS (obarray
)[hash
]);
513 SCM_SET_HASHTABLE_BUCKET (obarray
, hash
, slot
);
514 if (SCM_HASHTABLE_N_ITEMS (obarray
) > SCM_HASHTABLE_UPPER (obarray
))
515 scm_i_rehash (obarray
, scm_i_hash_symbol
, 0, "obarray_enter");
522 * Enter symbol into obarray. An existing entry for symbol is replaced. If
523 * an entry existed, the old (symbol . data) cell is returned, #f otherwise.
526 obarray_replace (SCM obarray
, SCM symbol
, SCM data
)
528 size_t hash
= scm_i_symbol_hash (symbol
) % SCM_HASHTABLE_N_BUCKETS (obarray
);
529 SCM new_entry
= scm_cons (symbol
, data
);
533 for (lsym
= SCM_HASHTABLE_BUCKETS (obarray
)[hash
];
535 lsym
= SCM_CDR (lsym
))
537 SCM old_entry
= SCM_CAR (lsym
);
538 if (scm_is_eq (SCM_CAR (old_entry
), symbol
))
540 SCM_SETCAR (lsym
, new_entry
);
545 slot
= scm_cons (new_entry
, SCM_HASHTABLE_BUCKETS (obarray
)[hash
]);
546 SCM_SET_HASHTABLE_BUCKET (obarray
, hash
, slot
);
547 if (SCM_HASHTABLE_N_ITEMS (obarray
) > SCM_HASHTABLE_UPPER (obarray
))
548 scm_i_rehash (obarray
, scm_i_hash_symbol
, 0, "obarray_replace");
555 * Look up symbol in obarray
558 obarray_retrieve (SCM obarray
, SCM sym
)
560 size_t hash
= scm_i_symbol_hash (sym
) % SCM_HASHTABLE_N_BUCKETS (obarray
);
563 for (lsym
= SCM_HASHTABLE_BUCKETS (obarray
)[hash
];
565 lsym
= SCM_CDR (lsym
))
567 SCM entry
= SCM_CAR (lsym
);
568 if (scm_is_eq (SCM_CAR (entry
), sym
))
572 return SCM_UNDEFINED
;
577 * Remove entry from obarray. If the symbol was found and removed, the old
578 * (symbol . data) cell is returned, #f otherwise.
581 obarray_remove (SCM obarray
, SCM sym
)
583 size_t hash
= scm_i_symbol_hash (sym
) % SCM_HASHTABLE_N_BUCKETS (obarray
);
584 SCM table_entry
= SCM_HASHTABLE_BUCKETS (obarray
)[hash
];
585 SCM handle
= scm_sloppy_assq (sym
, table_entry
);
587 if (SCM_CONSP (handle
))
589 SCM new_table_entry
= scm_delq1_x (handle
, table_entry
);
590 SCM_SET_HASHTABLE_BUCKET (obarray
, hash
, new_table_entry
);
591 SCM_HASHTABLE_DECREMENT (obarray
);
599 obarray_remove_all (SCM obarray
)
601 size_t size
= SCM_HASHTABLE_N_BUCKETS (obarray
);
604 for (i
= 0; i
< size
; i
++)
606 SCM_SET_HASHTABLE_BUCKET (obarray
, i
, SCM_EOL
);
608 SCM_SET_HASHTABLE_N_ITEMS (obarray
, 0);
613 /* core environments base
615 * This struct and the corresponding functions form a base class for guile's
616 * built-in environment types.
620 struct core_environments_base
{
621 struct scm_environment_funcs
*funcs
;
628 #define CORE_ENVIRONMENTS_BASE(env) \
629 ((struct core_environments_base *) SCM_CELL_WORD_1 (env))
630 #define CORE_ENVIRONMENT_OBSERVERS(env) \
631 (CORE_ENVIRONMENTS_BASE (env)->observers)
632 #define SCM_SET_CORE_ENVIRONMENT_OBSERVERS(env, v) \
633 (CORE_ENVIRONMENT_OBSERVERS (env) = (v))
634 #define CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR(env) \
635 (CORE_ENVIRONMENTS_BASE (env)->weak_observers)
636 #define CORE_ENVIRONMENT_WEAK_OBSERVERS(env) \
637 (SCM_VELTS (CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env)) [0])
638 #define SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS(env, v) \
639 (SCM_VECTOR_SET (CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env), 0, (v)))
644 core_environments_observe (SCM env
, scm_environment_observer proc
, SCM data
, int weak_p
)
646 SCM observer
= scm_double_cell (scm_tc16_observer
,
653 SCM observers
= CORE_ENVIRONMENT_OBSERVERS (env
);
654 SCM new_observers
= scm_cons (observer
, observers
);
655 SCM_SET_CORE_ENVIRONMENT_OBSERVERS (env
, new_observers
);
659 SCM observers
= CORE_ENVIRONMENT_WEAK_OBSERVERS (env
);
660 SCM new_observers
= scm_acons (SCM_BOOL_F
, observer
, observers
);
661 SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS (env
, new_observers
);
669 core_environments_unobserve (SCM env
, SCM observer
)
671 unsigned int handling_weaks
;
672 for (handling_weaks
= 0; handling_weaks
<= 1; ++handling_weaks
)
674 SCM l
= handling_weaks
675 ? CORE_ENVIRONMENT_WEAK_OBSERVERS (env
)
676 : CORE_ENVIRONMENT_OBSERVERS (env
);
680 SCM rest
= SCM_CDR (l
);
681 SCM first
= handling_weaks
685 if (scm_is_eq (first
, observer
))
687 /* Remove the first observer */
689 ? SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS (env
, rest
)
690 : SCM_SET_CORE_ENVIRONMENT_OBSERVERS (env
, rest
);
695 SCM rest
= SCM_CDR (l
);
697 if (!SCM_NULLP (rest
))
699 SCM next
= handling_weaks
703 if (scm_is_eq (next
, observer
))
705 SCM_SETCDR (l
, SCM_CDR (rest
));
711 } while (!SCM_NULLP (l
));
715 /* Dirk:FIXME:: What to do now, since the observer is not found? */
720 core_environments_mark (SCM env
)
722 scm_gc_mark (CORE_ENVIRONMENT_OBSERVERS (env
));
723 return CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env
);
728 core_environments_finalize (SCM env SCM_UNUSED
)
734 core_environments_preinit (struct core_environments_base
*body
)
737 body
->observers
= SCM_BOOL_F
;
738 body
->weak_observers
= SCM_BOOL_F
;
743 core_environments_init (struct core_environments_base
*body
,
744 struct scm_environment_funcs
*funcs
)
747 body
->observers
= SCM_EOL
;
748 body
->weak_observers
= scm_make_weak_value_alist_vector (scm_from_int (1));
752 /* Tell all observers to clear their caches.
754 * Environments have to be informed about changes in the following cases:
755 * - The observed env has a new binding. This must be always reported.
756 * - The observed env has dropped a binding. This must be always reported.
757 * - A binding in the observed environment has changed. This must only be
758 * reported, if there is a chance that the binding is being cached outside.
759 * However, this potential optimization is not performed currently.
761 * Errors that occur while the observers are called are accumulated and
762 * signalled as one single error message to the caller.
773 update_catch_body (void *ptr
)
775 struct update_data
*data
= (struct update_data
*) ptr
;
776 SCM observer
= data
->observer
;
778 (*SCM_OBSERVER_PROC (observer
))
779 (data
->environment
, SCM_OBSERVER_DATA (observer
));
781 return SCM_UNDEFINED
;
786 update_catch_handler (void *ptr
, SCM tag
, SCM args
)
788 struct update_data
*data
= (struct update_data
*) ptr
;
789 SCM observer
= data
->observer
;
791 scm_from_locale_string ("Observer `~A' signals `~A' error: ~S");
793 return scm_cons (message
, scm_list_3 (observer
, tag
, args
));
798 core_environments_broadcast (SCM env
)
799 #define FUNC_NAME "core_environments_broadcast"
801 unsigned int handling_weaks
;
802 SCM errors
= SCM_EOL
;
804 for (handling_weaks
= 0; handling_weaks
<= 1; ++handling_weaks
)
806 SCM observers
= handling_weaks
807 ? CORE_ENVIRONMENT_WEAK_OBSERVERS (env
)
808 : CORE_ENVIRONMENT_OBSERVERS (env
);
810 for (; !SCM_NULLP (observers
); observers
= SCM_CDR (observers
))
812 struct update_data data
;
813 SCM observer
= handling_weaks
814 ? SCM_CDAR (observers
)
815 : SCM_CAR (observers
);
818 data
.observer
= observer
;
819 data
.environment
= env
;
821 error
= scm_internal_catch (SCM_BOOL_T
,
822 update_catch_body
, &data
,
823 update_catch_handler
, &data
);
825 if (!SCM_UNBNDP (error
))
826 errors
= scm_cons (error
, errors
);
830 if (!SCM_NULLP (errors
))
832 /* Dirk:FIXME:: As soon as scm_misc_error is fixed to handle the name
833 * parameter correctly it should not be necessary any more to also pass
834 * namestr in order to get the desired information from the error
837 SCM ordered_errors
= scm_reverse (errors
);
840 "Observers of `~A' have signalled the following errors: ~S",
841 scm_cons2 (env
, ordered_errors
, SCM_EOL
));
850 * A leaf environment is simply a mutable set of definitions. A leaf
851 * environment supports no operations beyond the common set.
853 * Implementation: The obarray of the leaf environment holds (symbol . value)
854 * pairs. No further information is necessary, since all bindings and
855 * locations in a leaf environment are mutable.
859 struct leaf_environment
{
860 struct core_environments_base base
;
866 #define LEAF_ENVIRONMENT(env) \
867 ((struct leaf_environment *) SCM_CELL_WORD_1 (env))
872 leaf_environment_ref (SCM env
, SCM sym
)
874 SCM obarray
= LEAF_ENVIRONMENT (env
)->obarray
;
875 SCM binding
= obarray_retrieve (obarray
, sym
);
876 return SCM_UNBNDP (binding
) ? binding
: SCM_CDR (binding
);
881 leaf_environment_fold (SCM env
, scm_environment_folder proc
, SCM data
, SCM init
)
885 SCM obarray
= LEAF_ENVIRONMENT (env
)->obarray
;
887 for (i
= 0; i
< SCM_HASHTABLE_N_BUCKETS (obarray
); i
++)
890 for (l
= SCM_HASHTABLE_BUCKETS (obarray
)[i
];
894 SCM binding
= SCM_CAR (l
);
895 SCM symbol
= SCM_CAR (binding
);
896 SCM value
= SCM_CDR (binding
);
897 result
= (*proc
) (data
, symbol
, value
, result
);
905 leaf_environment_define (SCM env
, SCM sym
, SCM val
)
906 #define FUNC_NAME "leaf_environment_define"
908 SCM obarray
= LEAF_ENVIRONMENT (env
)->obarray
;
910 obarray_replace (obarray
, sym
, val
);
911 core_environments_broadcast (env
);
913 return SCM_ENVIRONMENT_SUCCESS
;
919 leaf_environment_undefine (SCM env
, SCM sym
)
920 #define FUNC_NAME "leaf_environment_undefine"
922 SCM obarray
= LEAF_ENVIRONMENT (env
)->obarray
;
923 SCM removed
= obarray_remove (obarray
, sym
);
925 if (scm_is_true (removed
))
926 core_environments_broadcast (env
);
928 return SCM_ENVIRONMENT_SUCCESS
;
934 leaf_environment_set_x (SCM env
, SCM sym
, SCM val
)
935 #define FUNC_NAME "leaf_environment_set_x"
937 SCM obarray
= LEAF_ENVIRONMENT (env
)->obarray
;
938 SCM binding
= obarray_retrieve (obarray
, sym
);
940 if (!SCM_UNBNDP (binding
))
942 SCM_SETCDR (binding
, val
);
943 return SCM_ENVIRONMENT_SUCCESS
;
947 return SCM_UNDEFINED
;
954 leaf_environment_cell (SCM env
, SCM sym
, int for_write SCM_UNUSED
)
956 SCM obarray
= LEAF_ENVIRONMENT (env
)->obarray
;
957 SCM binding
= obarray_retrieve (obarray
, sym
);
963 leaf_environment_mark (SCM env
)
965 scm_gc_mark (LEAF_ENVIRONMENT (env
)->obarray
);
966 return core_environments_mark (env
);
971 leaf_environment_free (SCM env
)
973 core_environments_finalize (env
);
974 scm_gc_free (LEAF_ENVIRONMENT (env
), sizeof (struct leaf_environment
),
980 leaf_environment_print (SCM type
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
982 SCM address
= scm_from_size_t (SCM_UNPACK (type
));
983 SCM base16
= scm_number_to_string (address
, scm_from_int (16));
985 scm_puts ("#<leaf environment ", port
);
986 scm_display (base16
, port
);
987 scm_puts (">", port
);
993 static struct scm_environment_funcs leaf_environment_funcs
= {
994 leaf_environment_ref
,
995 leaf_environment_fold
,
996 leaf_environment_define
,
997 leaf_environment_undefine
,
998 leaf_environment_set_x
,
999 leaf_environment_cell
,
1000 core_environments_observe
,
1001 core_environments_unobserve
,
1002 leaf_environment_mark
,
1003 leaf_environment_free
,
1004 leaf_environment_print
1008 void *scm_type_leaf_environment
= &leaf_environment_funcs
;
1011 SCM_DEFINE (scm_make_leaf_environment
, "make-leaf-environment", 0, 0, 0,
1013 "Create a new leaf environment, containing no bindings.\n"
1014 "All bindings and locations created in the new environment\n"
1016 #define FUNC_NAME s_scm_make_leaf_environment
1018 size_t size
= sizeof (struct leaf_environment
);
1019 struct leaf_environment
*body
= scm_gc_malloc (size
, "leaf environment");
1022 core_environments_preinit (&body
->base
);
1023 body
->obarray
= SCM_BOOL_F
;
1025 env
= scm_make_environment (body
);
1027 core_environments_init (&body
->base
, &leaf_environment_funcs
);
1028 body
->obarray
= scm_c_make_hash_table (DEFAULT_OBARRAY_SIZE
);
1035 SCM_DEFINE (scm_leaf_environment_p
, "leaf-environment?", 1, 0, 0,
1037 "Return @code{#t} if object is a leaf environment, or @code{#f}\n"
1039 #define FUNC_NAME s_scm_leaf_environment_p
1041 return scm_from_bool (SCM_LEAF_ENVIRONMENT_P (object
));
1047 /* eval environments
1049 * A module's source code refers to definitions imported from other modules,
1050 * and definitions made within itself. An eval environment combines two
1051 * environments -- a local environment and an imported environment -- to
1052 * produce a new environment in which both sorts of references can be
1055 * Implementation: The obarray of the eval environment is used to cache
1056 * entries from the local and imported environments such that in most of the
1057 * cases only a single lookup is necessary. Since for neither the local nor
1058 * the imported environment it is known, what kind of environment they form,
1059 * the most general case is assumed. Therefore, entries in the obarray take
1060 * one of the following forms:
1062 * 1) (<symbol> location mutability . source-env), where mutability indicates
1063 * one of the following states: IMMUTABLE if the location is known to be
1064 * immutable, MUTABLE if the location is known to be mutable, UNKNOWN if
1065 * the location has only been requested for non modifying accesses.
1067 * 2) (symbol . source-env) if the symbol has a binding in the source-env, but
1068 * if the source-env can't provide a cell for the binding. Thus, for every
1069 * access, the source-env has to be contacted directly.
1073 struct eval_environment
{
1074 struct core_environments_base base
;
1079 SCM imported_observer
;
1085 #define EVAL_ENVIRONMENT(env) \
1086 ((struct eval_environment *) SCM_CELL_WORD_1 (env))
1088 #define IMMUTABLE SCM_I_MAKINUM (0)
1089 #define MUTABLE SCM_I_MAKINUM (1)
1090 #define UNKNOWN SCM_I_MAKINUM (2)
1092 #define CACHED_LOCATION(x) SCM_CAR (x)
1093 #define CACHED_MUTABILITY(x) SCM_CADR (x)
1094 #define SET_CACHED_MUTABILITY(x, v) SCM_SETCAR (SCM_CDR (x), (v))
1095 #define CACHED_SOURCE_ENVIRONMENT(x) SCM_CDDR (x)
1099 /* eval_environment_lookup will report one of the following distinct results:
1100 * a) (<object> . value) if a cell could be obtained.
1101 * b) <environment> if the environment has to be contacted directly.
1102 * c) IMMUTABLE if an immutable cell was requested for write.
1103 * d) SCM_UNDEFINED if there is no binding for the symbol.
1106 eval_environment_lookup (SCM env
, SCM sym
, int for_write
)
1108 SCM obarray
= EVAL_ENVIRONMENT (env
)->obarray
;
1109 SCM binding
= obarray_retrieve (obarray
, sym
);
1111 if (!SCM_UNBNDP (binding
))
1113 /* The obarray holds an entry for the symbol. */
1115 SCM entry
= SCM_CDR (binding
);
1117 if (SCM_CONSP (entry
))
1119 /* The entry in the obarray is a cached location. */
1121 SCM location
= CACHED_LOCATION (entry
);
1127 mutability
= CACHED_MUTABILITY (entry
);
1128 if (scm_is_eq (mutability
, MUTABLE
))
1131 if (scm_is_eq (mutability
, UNKNOWN
))
1133 SCM source_env
= CACHED_SOURCE_ENVIRONMENT (entry
);
1134 SCM location
= SCM_ENVIRONMENT_CELL (source_env
, sym
, 1);
1136 if (SCM_CONSP (location
))
1138 SET_CACHED_MUTABILITY (entry
, MUTABLE
);
1141 else /* IMMUTABLE */
1143 SET_CACHED_MUTABILITY (entry
, IMMUTABLE
);
1152 /* The obarray entry is an environment */
1159 /* There is no entry for the symbol in the obarray. This can either
1160 * mean that there has not been a request for the symbol yet, or that
1161 * the symbol is really undefined. We are looking for the symbol in
1162 * both the local and the imported environment. If we find a binding, a
1163 * cached entry is created.
1166 struct eval_environment
*body
= EVAL_ENVIRONMENT (env
);
1167 unsigned int handling_import
;
1169 for (handling_import
= 0; handling_import
<= 1; ++handling_import
)
1171 SCM source_env
= handling_import
? body
->imported
: body
->local
;
1172 SCM location
= SCM_ENVIRONMENT_CELL (source_env
, sym
, for_write
);
1174 if (!SCM_UNBNDP (location
))
1176 if (SCM_CONSP (location
))
1178 SCM mutability
= for_write
? MUTABLE
: UNKNOWN
;
1179 SCM entry
= scm_cons2 (location
, mutability
, source_env
);
1180 obarray_enter (obarray
, sym
, entry
);
1183 else if (scm_is_eq (location
, SCM_ENVIRONMENT_LOCATION_NO_CELL
))
1185 obarray_enter (obarray
, sym
, source_env
);
1195 return SCM_UNDEFINED
;
1201 eval_environment_ref (SCM env
, SCM sym
)
1202 #define FUNC_NAME "eval_environment_ref"
1204 SCM location
= eval_environment_lookup (env
, sym
, 0);
1206 if (SCM_CONSP (location
))
1207 return SCM_CDR (location
);
1208 else if (!SCM_UNBNDP (location
))
1209 return SCM_ENVIRONMENT_REF (location
, sym
);
1211 return SCM_UNDEFINED
;
1217 eval_environment_folder (SCM extended_data
, SCM symbol
, SCM value
, SCM tail
)
1219 SCM local
= SCM_CAR (extended_data
);
1221 if (!SCM_ENVIRONMENT_BOUND_P (local
, symbol
))
1223 SCM proc_as_nr
= SCM_CADR (extended_data
);
1224 unsigned long int proc_as_ul
= scm_to_ulong (proc_as_nr
);
1225 scm_environment_folder proc
= (scm_environment_folder
) proc_as_ul
;
1226 SCM data
= SCM_CDDR (extended_data
);
1228 return (*proc
) (data
, symbol
, value
, tail
);
1238 eval_environment_fold (SCM env
, scm_environment_folder proc
, SCM data
, SCM init
)
1240 SCM local
= EVAL_ENVIRONMENT (env
)->local
;
1241 SCM imported
= EVAL_ENVIRONMENT (env
)->imported
;
1242 SCM proc_as_nr
= scm_from_ulong ((unsigned long) proc
);
1243 SCM extended_data
= scm_cons2 (local
, proc_as_nr
, data
);
1244 SCM tmp_result
= scm_c_environment_fold (imported
, eval_environment_folder
, extended_data
, init
);
1246 return scm_c_environment_fold (local
, proc
, data
, tmp_result
);
1251 eval_environment_define (SCM env
, SCM sym
, SCM val
)
1252 #define FUNC_NAME "eval_environment_define"
1254 SCM local
= EVAL_ENVIRONMENT (env
)->local
;
1255 return SCM_ENVIRONMENT_DEFINE (local
, sym
, val
);
1261 eval_environment_undefine (SCM env
, SCM sym
)
1262 #define FUNC_NAME "eval_environment_undefine"
1264 SCM local
= EVAL_ENVIRONMENT (env
)->local
;
1265 return SCM_ENVIRONMENT_UNDEFINE (local
, sym
);
1271 eval_environment_set_x (SCM env
, SCM sym
, SCM val
)
1272 #define FUNC_NAME "eval_environment_set_x"
1274 SCM location
= eval_environment_lookup (env
, sym
, 1);
1276 if (SCM_CONSP (location
))
1278 SCM_SETCDR (location
, val
);
1279 return SCM_ENVIRONMENT_SUCCESS
;
1281 else if (SCM_ENVIRONMENT_P (location
))
1283 return SCM_ENVIRONMENT_SET (location
, sym
, val
);
1285 else if (scm_is_eq (location
, IMMUTABLE
))
1287 return SCM_ENVIRONMENT_LOCATION_IMMUTABLE
;
1291 return SCM_UNDEFINED
;
1298 eval_environment_cell (SCM env
, SCM sym
, int for_write
)
1299 #define FUNC_NAME "eval_environment_cell"
1301 SCM location
= eval_environment_lookup (env
, sym
, for_write
);
1303 if (SCM_CONSP (location
))
1305 else if (SCM_ENVIRONMENT_P (location
))
1306 return SCM_ENVIRONMENT_LOCATION_NO_CELL
;
1307 else if (scm_is_eq (location
, IMMUTABLE
))
1308 return SCM_ENVIRONMENT_LOCATION_IMMUTABLE
;
1310 return SCM_UNDEFINED
;
1316 eval_environment_mark (SCM env
)
1318 struct eval_environment
*body
= EVAL_ENVIRONMENT (env
);
1320 scm_gc_mark (body
->obarray
);
1321 scm_gc_mark (body
->imported
);
1322 scm_gc_mark (body
->imported_observer
);
1323 scm_gc_mark (body
->local
);
1324 scm_gc_mark (body
->local_observer
);
1326 return core_environments_mark (env
);
1331 eval_environment_free (SCM env
)
1333 core_environments_finalize (env
);
1334 scm_gc_free (EVAL_ENVIRONMENT (env
), sizeof (struct eval_environment
),
1335 "eval environment");
1340 eval_environment_print (SCM type
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
1342 SCM address
= scm_from_size_t (SCM_UNPACK (type
));
1343 SCM base16
= scm_number_to_string (address
, scm_from_int (16));
1345 scm_puts ("#<eval environment ", port
);
1346 scm_display (base16
, port
);
1347 scm_puts (">", port
);
1353 static struct scm_environment_funcs eval_environment_funcs
= {
1354 eval_environment_ref
,
1355 eval_environment_fold
,
1356 eval_environment_define
,
1357 eval_environment_undefine
,
1358 eval_environment_set_x
,
1359 eval_environment_cell
,
1360 core_environments_observe
,
1361 core_environments_unobserve
,
1362 eval_environment_mark
,
1363 eval_environment_free
,
1364 eval_environment_print
1368 void *scm_type_eval_environment
= &eval_environment_funcs
;
1372 eval_environment_observer (SCM caller SCM_UNUSED
, SCM eval_env
)
1374 SCM obarray
= EVAL_ENVIRONMENT (eval_env
)->obarray
;
1376 obarray_remove_all (obarray
);
1377 core_environments_broadcast (eval_env
);
1381 SCM_DEFINE (scm_make_eval_environment
, "make-eval-environment", 2, 0, 0,
1382 (SCM local
, SCM imported
),
1383 "Return a new environment object eval whose bindings are the\n"
1384 "union of the bindings in the environments @var{local} and\n"
1385 "@var{imported}, with bindings from @var{local} taking\n"
1386 "precedence. Definitions made in eval are placed in @var{local}.\n"
1387 "Applying @code{environment-define} or\n"
1388 "@code{environment-undefine} to eval has the same effect as\n"
1389 "applying the procedure to @var{local}.\n"
1390 "Note that eval incorporates @var{local} and @var{imported} by\n"
1392 "If, after creating eval, the program changes the bindings of\n"
1393 "@var{local} or @var{imported}, those changes will be visible\n"
1395 "Since most Scheme evaluation takes place in eval environments,\n"
1396 "they transparently cache the bindings received from @var{local}\n"
1397 "and @var{imported}. Thus, the first time the program looks up\n"
1398 "a symbol in eval, eval may make calls to @var{local} or\n"
1399 "@var{imported} to find their bindings, but subsequent\n"
1400 "references to that symbol will be as fast as references to\n"
1401 "bindings in finite environments.\n"
1402 "In typical use, @var{local} will be a finite environment, and\n"
1403 "@var{imported} will be an import environment")
1404 #define FUNC_NAME s_scm_make_eval_environment
1407 struct eval_environment
*body
;
1409 SCM_ASSERT (SCM_ENVIRONMENT_P (local
), local
, SCM_ARG1
, FUNC_NAME
);
1410 SCM_ASSERT (SCM_ENVIRONMENT_P (imported
), imported
, SCM_ARG2
, FUNC_NAME
);
1412 body
= scm_gc_malloc (sizeof (struct eval_environment
), "eval environment");
1414 core_environments_preinit (&body
->base
);
1415 body
->obarray
= SCM_BOOL_F
;
1416 body
->imported
= SCM_BOOL_F
;
1417 body
->imported_observer
= SCM_BOOL_F
;
1418 body
->local
= SCM_BOOL_F
;
1419 body
->local_observer
= SCM_BOOL_F
;
1421 env
= scm_make_environment (body
);
1423 core_environments_init (&body
->base
, &eval_environment_funcs
);
1424 body
->obarray
= scm_c_make_hash_table (DEFAULT_OBARRAY_SIZE
);
1425 body
->imported
= imported
;
1426 body
->imported_observer
1427 = SCM_ENVIRONMENT_OBSERVE (imported
, eval_environment_observer
, env
, 1);
1428 body
->local
= local
;
1429 body
->local_observer
1430 = SCM_ENVIRONMENT_OBSERVE (local
, eval_environment_observer
, env
, 1);
1437 SCM_DEFINE (scm_eval_environment_p
, "eval-environment?", 1, 0, 0,
1439 "Return @code{#t} if object is an eval environment, or @code{#f}\n"
1441 #define FUNC_NAME s_scm_eval_environment_p
1443 return scm_from_bool (SCM_EVAL_ENVIRONMENT_P (object
));
1448 SCM_DEFINE (scm_eval_environment_local
, "eval-environment-local", 1, 0, 0,
1450 "Return the local environment of eval environment @var{env}.")
1451 #define FUNC_NAME s_scm_eval_environment_local
1453 SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
1455 return EVAL_ENVIRONMENT (env
)->local
;
1460 SCM_DEFINE (scm_eval_environment_set_local_x
, "eval-environment-set-local!", 2, 0, 0,
1461 (SCM env
, SCM local
),
1462 "Change @var{env}'s local environment to @var{local}.")
1463 #define FUNC_NAME s_scm_eval_environment_set_local_x
1465 struct eval_environment
*body
;
1467 SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
1468 SCM_ASSERT (SCM_ENVIRONMENT_P (local
), local
, SCM_ARG2
, FUNC_NAME
);
1470 body
= EVAL_ENVIRONMENT (env
);
1472 obarray_remove_all (body
->obarray
);
1473 SCM_ENVIRONMENT_UNOBSERVE (body
->local
, body
->local_observer
);
1475 body
->local
= local
;
1476 body
->local_observer
1477 = SCM_ENVIRONMENT_OBSERVE (local
, eval_environment_observer
, env
, 1);
1479 core_environments_broadcast (env
);
1481 return SCM_UNSPECIFIED
;
1486 SCM_DEFINE (scm_eval_environment_imported
, "eval-environment-imported", 1, 0, 0,
1488 "Return the imported environment of eval environment @var{env}.")
1489 #define FUNC_NAME s_scm_eval_environment_imported
1491 SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
1493 return EVAL_ENVIRONMENT (env
)->imported
;
1498 SCM_DEFINE (scm_eval_environment_set_imported_x
, "eval-environment-set-imported!", 2, 0, 0,
1499 (SCM env
, SCM imported
),
1500 "Change @var{env}'s imported environment to @var{imported}.")
1501 #define FUNC_NAME s_scm_eval_environment_set_imported_x
1503 struct eval_environment
*body
;
1505 SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
1506 SCM_ASSERT (SCM_ENVIRONMENT_P (imported
), imported
, SCM_ARG2
, FUNC_NAME
);
1508 body
= EVAL_ENVIRONMENT (env
);
1510 obarray_remove_all (body
->obarray
);
1511 SCM_ENVIRONMENT_UNOBSERVE (body
->imported
, body
->imported_observer
);
1513 body
->imported
= imported
;
1514 body
->imported_observer
1515 = SCM_ENVIRONMENT_OBSERVE (imported
, eval_environment_observer
, env
, 1);
1517 core_environments_broadcast (env
);
1519 return SCM_UNSPECIFIED
;
1525 /* import environments
1527 * An import environment combines the bindings of a set of argument
1528 * environments, and checks for naming clashes.
1530 * Implementation: The import environment does no caching at all. For every
1531 * access, the list of imported environments is scanned.
1535 struct import_environment
{
1536 struct core_environments_base base
;
1539 SCM import_observers
;
1545 #define IMPORT_ENVIRONMENT(env) \
1546 ((struct import_environment *) SCM_CELL_WORD_1 (env))
1550 /* Lookup will report one of the following distinct results:
1551 * a) <environment> if only environment binds the symbol.
1552 * b) (env-1 env-2 ...) for conflicting bindings in env-1, ...
1553 * c) SCM_UNDEFINED if there is no binding for the symbol.
1556 import_environment_lookup (SCM env
, SCM sym
)
1558 SCM imports
= IMPORT_ENVIRONMENT (env
)->imports
;
1559 SCM result
= SCM_UNDEFINED
;
1562 for (l
= imports
; !SCM_NULLP (l
); l
= SCM_CDR (l
))
1564 SCM imported
= SCM_CAR (l
);
1566 if (SCM_ENVIRONMENT_BOUND_P (imported
, sym
))
1568 if (SCM_UNBNDP (result
))
1570 else if (SCM_CONSP (result
))
1571 result
= scm_cons (imported
, result
);
1573 result
= scm_cons2 (imported
, result
, SCM_EOL
);
1577 if (SCM_CONSP (result
))
1578 return scm_reverse (result
);
1585 import_environment_conflict (SCM env
, SCM sym
, SCM imports
)
1587 SCM conflict_proc
= IMPORT_ENVIRONMENT (env
)->conflict_proc
;
1588 SCM args
= scm_cons2 (env
, sym
, scm_cons (imports
, SCM_EOL
));
1590 return scm_apply_0 (conflict_proc
, args
);
1595 import_environment_ref (SCM env
, SCM sym
)
1596 #define FUNC_NAME "import_environment_ref"
1598 SCM owner
= import_environment_lookup (env
, sym
);
1600 if (SCM_UNBNDP (owner
))
1602 return SCM_UNDEFINED
;
1604 else if (SCM_CONSP (owner
))
1606 SCM resolve
= import_environment_conflict (env
, sym
, owner
);
1608 if (SCM_ENVIRONMENT_P (resolve
))
1609 return SCM_ENVIRONMENT_REF (resolve
, sym
);
1611 return SCM_UNSPECIFIED
;
1615 return SCM_ENVIRONMENT_REF (owner
, sym
);
1622 import_environment_folder (SCM extended_data
, SCM symbol
, SCM value
, SCM tail
)
1623 #define FUNC_NAME "import_environment_fold"
1625 SCM import_env
= SCM_CAR (extended_data
);
1626 SCM imported_env
= SCM_CADR (extended_data
);
1627 SCM owner
= import_environment_lookup (import_env
, symbol
);
1628 SCM proc_as_nr
= SCM_CADDR (extended_data
);
1629 unsigned long int proc_as_ul
= scm_to_ulong (proc_as_nr
);
1630 scm_environment_folder proc
= (scm_environment_folder
) proc_as_ul
;
1631 SCM data
= SCM_CDDDR (extended_data
);
1633 if (SCM_CONSP (owner
) && scm_is_eq (SCM_CAR (owner
), imported_env
))
1634 owner
= import_environment_conflict (import_env
, symbol
, owner
);
1636 if (SCM_ENVIRONMENT_P (owner
))
1637 return (*proc
) (data
, symbol
, value
, tail
);
1638 else /* unresolved conflict */
1639 return (*proc
) (data
, symbol
, SCM_UNSPECIFIED
, tail
);
1645 import_environment_fold (SCM env
, scm_environment_folder proc
, SCM data
, SCM init
)
1647 SCM proc_as_nr
= scm_from_ulong ((unsigned long) proc
);
1651 for (l
= IMPORT_ENVIRONMENT (env
)->imports
; !SCM_NULLP (l
); l
= SCM_CDR (l
))
1653 SCM imported_env
= SCM_CAR (l
);
1654 SCM extended_data
= scm_cons (env
, scm_cons2 (imported_env
, proc_as_nr
, data
));
1656 result
= scm_c_environment_fold (imported_env
, import_environment_folder
, extended_data
, result
);
1664 import_environment_define (SCM env SCM_UNUSED
,
1667 #define FUNC_NAME "import_environment_define"
1669 return SCM_ENVIRONMENT_BINDING_IMMUTABLE
;
1675 import_environment_undefine (SCM env SCM_UNUSED
,
1677 #define FUNC_NAME "import_environment_undefine"
1679 return SCM_ENVIRONMENT_BINDING_IMMUTABLE
;
1685 import_environment_set_x (SCM env
, SCM sym
, SCM val
)
1686 #define FUNC_NAME "import_environment_set_x"
1688 SCM owner
= import_environment_lookup (env
, sym
);
1690 if (SCM_UNBNDP (owner
))
1692 return SCM_UNDEFINED
;
1694 else if (SCM_CONSP (owner
))
1696 SCM resolve
= import_environment_conflict (env
, sym
, owner
);
1698 if (SCM_ENVIRONMENT_P (resolve
))
1699 return SCM_ENVIRONMENT_SET (resolve
, sym
, val
);
1701 return SCM_ENVIRONMENT_LOCATION_IMMUTABLE
;
1705 return SCM_ENVIRONMENT_SET (owner
, sym
, val
);
1712 import_environment_cell (SCM env
, SCM sym
, int for_write
)
1713 #define FUNC_NAME "import_environment_cell"
1715 SCM owner
= import_environment_lookup (env
, sym
);
1717 if (SCM_UNBNDP (owner
))
1719 return SCM_UNDEFINED
;
1721 else if (SCM_CONSP (owner
))
1723 SCM resolve
= import_environment_conflict (env
, sym
, owner
);
1725 if (SCM_ENVIRONMENT_P (resolve
))
1726 return SCM_ENVIRONMENT_CELL (resolve
, sym
, for_write
);
1728 return SCM_ENVIRONMENT_LOCATION_NO_CELL
;
1732 return SCM_ENVIRONMENT_CELL (owner
, sym
, for_write
);
1739 import_environment_mark (SCM env
)
1741 scm_gc_mark (IMPORT_ENVIRONMENT (env
)->imports
);
1742 scm_gc_mark (IMPORT_ENVIRONMENT (env
)->import_observers
);
1743 scm_gc_mark (IMPORT_ENVIRONMENT (env
)->conflict_proc
);
1744 return core_environments_mark (env
);
1749 import_environment_free (SCM env
)
1751 core_environments_finalize (env
);
1752 scm_gc_free (IMPORT_ENVIRONMENT (env
), sizeof (struct import_environment
),
1753 "import environment");
1758 import_environment_print (SCM type
, SCM port
,
1759 scm_print_state
*pstate SCM_UNUSED
)
1761 SCM address
= scm_from_size_t (SCM_UNPACK (type
));
1762 SCM base16
= scm_number_to_string (address
, scm_from_int (16));
1764 scm_puts ("#<import environment ", port
);
1765 scm_display (base16
, port
);
1766 scm_puts (">", port
);
1772 static struct scm_environment_funcs import_environment_funcs
= {
1773 import_environment_ref
,
1774 import_environment_fold
,
1775 import_environment_define
,
1776 import_environment_undefine
,
1777 import_environment_set_x
,
1778 import_environment_cell
,
1779 core_environments_observe
,
1780 core_environments_unobserve
,
1781 import_environment_mark
,
1782 import_environment_free
,
1783 import_environment_print
1787 void *scm_type_import_environment
= &import_environment_funcs
;
1791 import_environment_observer (SCM caller SCM_UNUSED
, SCM import_env
)
1793 core_environments_broadcast (import_env
);
1797 SCM_DEFINE (scm_make_import_environment
, "make-import-environment", 2, 0, 0,
1798 (SCM imports
, SCM conflict_proc
),
1799 "Return a new environment @var{imp} whose bindings are the union\n"
1800 "of the bindings from the environments in @var{imports};\n"
1801 "@var{imports} must be a list of environments. That is,\n"
1802 "@var{imp} binds a symbol to a location when some element of\n"
1803 "@var{imports} does.\n"
1804 "If two different elements of @var{imports} have a binding for\n"
1805 "the same symbol, the @var{conflict-proc} is called with the\n"
1806 "following parameters: the import environment, the symbol and\n"
1807 "the list of the imported environments that bind the symbol.\n"
1808 "If the @var{conflict-proc} returns an environment @var{env},\n"
1809 "the conflict is considered as resolved and the binding from\n"
1810 "@var{env} is used. If the @var{conflict-proc} returns some\n"
1811 "non-environment object, the conflict is considered unresolved\n"
1812 "and the symbol is treated as unspecified in the import\n"
1814 "The checking for conflicts may be performed lazily, i. e. at\n"
1815 "the moment when a value or binding for a certain symbol is\n"
1816 "requested instead of the moment when the environment is\n"
1817 "created or the bindings of the imports change.\n"
1818 "All bindings in @var{imp} are immutable. If you apply\n"
1819 "@code{environment-define} or @code{environment-undefine} to\n"
1820 "@var{imp}, Guile will signal an\n"
1821 " @code{environment:immutable-binding} error. However,\n"
1822 "notice that the set of bindings in @var{imp} may still change,\n"
1823 "if one of its imported environments changes.")
1824 #define FUNC_NAME s_scm_make_import_environment
1826 size_t size
= sizeof (struct import_environment
);
1827 struct import_environment
*body
= scm_gc_malloc (size
, "import environment");
1830 core_environments_preinit (&body
->base
);
1831 body
->imports
= SCM_BOOL_F
;
1832 body
->import_observers
= SCM_BOOL_F
;
1833 body
->conflict_proc
= SCM_BOOL_F
;
1835 env
= scm_make_environment (body
);
1837 core_environments_init (&body
->base
, &import_environment_funcs
);
1838 body
->imports
= SCM_EOL
;
1839 body
->import_observers
= SCM_EOL
;
1840 body
->conflict_proc
= conflict_proc
;
1842 scm_import_environment_set_imports_x (env
, imports
);
1849 SCM_DEFINE (scm_import_environment_p
, "import-environment?", 1, 0, 0,
1851 "Return @code{#t} if object is an import environment, or\n"
1852 "@code{#f} otherwise.")
1853 #define FUNC_NAME s_scm_import_environment_p
1855 return scm_from_bool (SCM_IMPORT_ENVIRONMENT_P (object
));
1860 SCM_DEFINE (scm_import_environment_imports
, "import-environment-imports", 1, 0, 0,
1862 "Return the list of environments imported by the import\n"
1863 "environment @var{env}.")
1864 #define FUNC_NAME s_scm_import_environment_imports
1866 SCM_ASSERT (SCM_IMPORT_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
1868 return IMPORT_ENVIRONMENT (env
)->imports
;
1873 SCM_DEFINE (scm_import_environment_set_imports_x
, "import-environment-set-imports!", 2, 0, 0,
1874 (SCM env
, SCM imports
),
1875 "Change @var{env}'s list of imported environments to\n"
1876 "@var{imports}, and check for conflicts.")
1877 #define FUNC_NAME s_scm_import_environment_set_imports_x
1879 struct import_environment
*body
= IMPORT_ENVIRONMENT (env
);
1880 SCM import_observers
= SCM_EOL
;
1883 SCM_ASSERT (SCM_IMPORT_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
1884 for (l
= imports
; SCM_CONSP (l
); l
= SCM_CDR (l
))
1886 SCM obj
= SCM_CAR (l
);
1887 SCM_ASSERT (SCM_ENVIRONMENT_P (obj
), imports
, SCM_ARG2
, FUNC_NAME
);
1889 SCM_ASSERT (SCM_NULLP (l
), imports
, SCM_ARG2
, FUNC_NAME
);
1891 for (l
= body
->import_observers
; !SCM_NULLP (l
); l
= SCM_CDR (l
))
1893 SCM obs
= SCM_CAR (l
);
1894 SCM_ENVIRONMENT_UNOBSERVE (env
, obs
);
1897 for (l
= imports
; !SCM_NULLP (l
); l
= SCM_CDR (l
))
1899 SCM imp
= SCM_CAR (l
);
1900 SCM obs
= SCM_ENVIRONMENT_OBSERVE (imp
, import_environment_observer
, env
, 1);
1901 import_observers
= scm_cons (obs
, import_observers
);
1904 body
->imports
= imports
;
1905 body
->import_observers
= import_observers
;
1907 return SCM_UNSPECIFIED
;
1913 /* export environments
1915 * An export environment restricts an environment to a specified set of
1918 * Implementation: The export environment does no caching at all. For every
1919 * access, the signature is scanned. The signature that is stored internally
1920 * is an alist of pairs (symbol . (mutability)).
1924 struct export_environment
{
1925 struct core_environments_base base
;
1928 SCM private_observer
;
1934 #define EXPORT_ENVIRONMENT(env) \
1935 ((struct export_environment *) SCM_CELL_WORD_1 (env))
1938 SCM_SYMBOL (symbol_immutable_location
, "immutable-location");
1939 SCM_SYMBOL (symbol_mutable_location
, "mutable-location");
1944 export_environment_ref (SCM env
, SCM sym
)
1945 #define FUNC_NAME "export_environment_ref"
1947 struct export_environment
*body
= EXPORT_ENVIRONMENT (env
);
1948 SCM entry
= scm_assq (sym
, body
->signature
);
1950 if (scm_is_false (entry
))
1951 return SCM_UNDEFINED
;
1953 return SCM_ENVIRONMENT_REF (body
->private, sym
);
1959 export_environment_fold (SCM env
, scm_environment_folder proc
, SCM data
, SCM init
)
1961 struct export_environment
*body
= EXPORT_ENVIRONMENT (env
);
1965 for (l
= body
->signature
; !SCM_NULLP (l
); l
= SCM_CDR (l
))
1967 SCM symbol
= SCM_CAR (l
);
1968 SCM value
= SCM_ENVIRONMENT_REF (body
->private, symbol
);
1969 if (!SCM_UNBNDP (value
))
1970 result
= (*proc
) (data
, symbol
, value
, result
);
1977 export_environment_define (SCM env SCM_UNUSED
,
1980 #define FUNC_NAME "export_environment_define"
1982 return SCM_ENVIRONMENT_BINDING_IMMUTABLE
;
1988 export_environment_undefine (SCM env SCM_UNUSED
, SCM sym SCM_UNUSED
)
1989 #define FUNC_NAME "export_environment_undefine"
1991 return SCM_ENVIRONMENT_BINDING_IMMUTABLE
;
1997 export_environment_set_x (SCM env
, SCM sym
, SCM val
)
1998 #define FUNC_NAME "export_environment_set_x"
2000 struct export_environment
*body
= EXPORT_ENVIRONMENT (env
);
2001 SCM entry
= scm_assq (sym
, body
->signature
);
2003 if (scm_is_false (entry
))
2005 return SCM_UNDEFINED
;
2009 if (scm_is_eq (SCM_CADR (entry
), symbol_mutable_location
))
2010 return SCM_ENVIRONMENT_SET (body
->private, sym
, val
);
2012 return SCM_ENVIRONMENT_LOCATION_IMMUTABLE
;
2019 export_environment_cell (SCM env
, SCM sym
, int for_write
)
2020 #define FUNC_NAME "export_environment_cell"
2022 struct export_environment
*body
= EXPORT_ENVIRONMENT (env
);
2023 SCM entry
= scm_assq (sym
, body
->signature
);
2025 if (scm_is_false (entry
))
2027 return SCM_UNDEFINED
;
2031 if (!for_write
|| scm_is_eq (SCM_CADR (entry
), symbol_mutable_location
))
2032 return SCM_ENVIRONMENT_CELL (body
->private, sym
, for_write
);
2034 return SCM_ENVIRONMENT_LOCATION_IMMUTABLE
;
2041 export_environment_mark (SCM env
)
2043 struct export_environment
*body
= EXPORT_ENVIRONMENT (env
);
2045 scm_gc_mark (body
->private);
2046 scm_gc_mark (body
->private_observer
);
2047 scm_gc_mark (body
->signature
);
2049 return core_environments_mark (env
);
2054 export_environment_free (SCM env
)
2056 core_environments_finalize (env
);
2057 scm_gc_free (EXPORT_ENVIRONMENT (env
), sizeof (struct export_environment
),
2058 "export environment");
2063 export_environment_print (SCM type
, SCM port
,
2064 scm_print_state
*pstate SCM_UNUSED
)
2066 SCM address
= scm_from_size_t (SCM_UNPACK (type
));
2067 SCM base16
= scm_number_to_string (address
, scm_from_int (16));
2069 scm_puts ("#<export environment ", port
);
2070 scm_display (base16
, port
);
2071 scm_puts (">", port
);
2077 static struct scm_environment_funcs export_environment_funcs
= {
2078 export_environment_ref
,
2079 export_environment_fold
,
2080 export_environment_define
,
2081 export_environment_undefine
,
2082 export_environment_set_x
,
2083 export_environment_cell
,
2084 core_environments_observe
,
2085 core_environments_unobserve
,
2086 export_environment_mark
,
2087 export_environment_free
,
2088 export_environment_print
2092 void *scm_type_export_environment
= &export_environment_funcs
;
2096 export_environment_observer (SCM caller SCM_UNUSED
, SCM export_env
)
2098 core_environments_broadcast (export_env
);
2102 SCM_DEFINE (scm_make_export_environment
, "make-export-environment", 2, 0, 0,
2103 (SCM
private, SCM signature
),
2104 "Return a new environment @var{exp} containing only those\n"
2105 "bindings in private whose symbols are present in\n"
2106 "@var{signature}. The @var{private} argument must be an\n"
2108 "The environment @var{exp} binds symbol to location when\n"
2109 "@var{env} does, and symbol is exported by @var{signature}.\n\n"
2110 "@var{signature} is a list specifying which of the bindings in\n"
2111 "@var{private} should be visible in @var{exp}. Each element of\n"
2112 "@var{signature} should be a list of the form:\n"
2113 " (symbol attribute ...)\n"
2114 "where each attribute is one of the following:\n"
2116 "@item the symbol @code{mutable-location}\n"
2117 " @var{exp} should treat the\n"
2118 " location bound to symbol as mutable. That is, @var{exp}\n"
2119 " will pass calls to @code{environment-set!} or\n"
2120 " @code{environment-cell} directly through to private.\n"
2121 "@item the symbol @code{immutable-location}\n"
2122 " @var{exp} should treat\n"
2123 " the location bound to symbol as immutable. If the program\n"
2124 " applies @code{environment-set!} to @var{exp} and symbol, or\n"
2125 " calls @code{environment-cell} to obtain a writable value\n"
2126 " cell, @code{environment-set!} will signal an\n"
2127 " @code{environment:immutable-location} error. Note that, even\n"
2128 " if an export environment treats a location as immutable, the\n"
2129 " underlying environment may treat it as mutable, so its\n"
2130 " value may change.\n"
2132 "It is an error for an element of signature to specify both\n"
2133 "@code{mutable-location} and @code{immutable-location}. If\n"
2134 "neither is specified, @code{immutable-location} is assumed.\n\n"
2135 "As a special case, if an element of signature is a lone\n"
2136 "symbol @var{sym}, it is equivalent to an element of the form\n"
2138 "All bindings in @var{exp} are immutable. If you apply\n"
2139 "@code{environment-define} or @code{environment-undefine} to\n"
2140 "@var{exp}, Guile will signal an\n"
2141 "@code{environment:immutable-binding} error. However,\n"
2142 "notice that the set of bindings in @var{exp} may still change,\n"
2143 "if the bindings in private change.")
2144 #define FUNC_NAME s_scm_make_export_environment
2147 struct export_environment
*body
;
2150 SCM_ASSERT (SCM_ENVIRONMENT_P (private), private, SCM_ARG1
, FUNC_NAME
);
2152 size
= sizeof (struct export_environment
);
2153 body
= scm_gc_malloc (size
, "export environment");
2155 core_environments_preinit (&body
->base
);
2156 body
->private = SCM_BOOL_F
;
2157 body
->private_observer
= SCM_BOOL_F
;
2158 body
->signature
= SCM_BOOL_F
;
2160 env
= scm_make_environment (body
);
2162 core_environments_init (&body
->base
, &export_environment_funcs
);
2163 body
->private = private;
2164 body
->private_observer
2165 = SCM_ENVIRONMENT_OBSERVE (private, export_environment_observer
, env
, 1);
2166 body
->signature
= SCM_EOL
;
2168 scm_export_environment_set_signature_x (env
, signature
);
2175 SCM_DEFINE (scm_export_environment_p
, "export-environment?", 1, 0, 0,
2177 "Return @code{#t} if object is an export environment, or\n"
2178 "@code{#f} otherwise.")
2179 #define FUNC_NAME s_scm_export_environment_p
2181 return scm_from_bool (SCM_EXPORT_ENVIRONMENT_P (object
));
2186 SCM_DEFINE (scm_export_environment_private
, "export-environment-private", 1, 0, 0,
2188 "Return the private environment of export environment @var{env}.")
2189 #define FUNC_NAME s_scm_export_environment_private
2191 SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
2193 return EXPORT_ENVIRONMENT (env
)->private;
2198 SCM_DEFINE (scm_export_environment_set_private_x
, "export-environment-set-private!", 2, 0, 0,
2199 (SCM env
, SCM
private),
2200 "Change the private environment of export environment @var{env}.")
2201 #define FUNC_NAME s_scm_export_environment_set_private_x
2203 struct export_environment
*body
;
2205 SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
2206 SCM_ASSERT (SCM_ENVIRONMENT_P (private), private, SCM_ARG2
, FUNC_NAME
);
2208 body
= EXPORT_ENVIRONMENT (env
);
2209 SCM_ENVIRONMENT_UNOBSERVE (private, body
->private_observer
);
2211 body
->private = private;
2212 body
->private_observer
2213 = SCM_ENVIRONMENT_OBSERVE (private, export_environment_observer
, env
, 1);
2215 return SCM_UNSPECIFIED
;
2220 SCM_DEFINE (scm_export_environment_signature
, "export-environment-signature", 1, 0, 0,
2222 "Return the signature of export environment @var{env}.")
2223 #define FUNC_NAME s_scm_export_environment_signature
2225 SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
2227 return EXPORT_ENVIRONMENT (env
)->signature
;
2233 export_environment_parse_signature (SCM signature
, const char* caller
)
2235 SCM result
= SCM_EOL
;
2238 for (l
= signature
; SCM_CONSP (l
); l
= SCM_CDR (l
))
2240 SCM entry
= SCM_CAR (l
);
2242 if (scm_is_symbol (entry
))
2244 SCM new_entry
= scm_cons2 (entry
, symbol_immutable_location
, SCM_EOL
);
2245 result
= scm_cons (new_entry
, result
);
2256 SCM_ASSERT (SCM_CONSP (entry
), entry
, SCM_ARGn
, caller
);
2257 SCM_ASSERT (scm_is_symbol (SCM_CAR (entry
)), entry
, SCM_ARGn
, caller
);
2259 sym
= SCM_CAR (entry
);
2261 for (l2
= SCM_CDR (entry
); SCM_CONSP (l2
); l2
= SCM_CDR (l2
))
2263 SCM attribute
= SCM_CAR (l2
);
2264 if (scm_is_eq (attribute
, symbol_immutable_location
))
2266 else if (scm_is_eq (attribute
, symbol_mutable_location
))
2269 SCM_ASSERT (0, entry
, SCM_ARGn
, caller
);
2271 SCM_ASSERT (SCM_NULLP (l2
), entry
, SCM_ARGn
, caller
);
2272 SCM_ASSERT (!mutable || !immutable
, entry
, SCM_ARGn
, caller
);
2274 if (!mutable && !immutable
)
2277 mutability
= mutable ? symbol_mutable_location
: symbol_immutable_location
;
2278 new_entry
= scm_cons2 (sym
, mutability
, SCM_EOL
);
2279 result
= scm_cons (new_entry
, result
);
2282 SCM_ASSERT (SCM_NULLP (l
), signature
, SCM_ARGn
, caller
);
2284 /* Dirk:FIXME:: Now we know that signature is syntactically correct. There
2285 * are, however, no checks for symbols entered twice with contradicting
2286 * mutabilities. It would be nice, to implement this test, to be able to
2287 * call the sort functions conveniently from C.
2290 return scm_reverse (result
);
2294 SCM_DEFINE (scm_export_environment_set_signature_x
, "export-environment-set-signature!", 2, 0, 0,
2295 (SCM env
, SCM signature
),
2296 "Change the signature of export environment @var{env}.")
2297 #define FUNC_NAME s_scm_export_environment_set_signature_x
2301 SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env
), env
, SCM_ARG1
, FUNC_NAME
);
2302 parsed_sig
= export_environment_parse_signature (signature
, FUNC_NAME
);
2304 EXPORT_ENVIRONMENT (env
)->signature
= parsed_sig
;
2306 return SCM_UNSPECIFIED
;
2313 scm_environments_prehistory ()
2315 /* create environment smob */
2316 scm_tc16_environment
= scm_make_smob_type ("environment", 0);
2317 scm_set_smob_mark (scm_tc16_environment
, environment_mark
);
2318 scm_set_smob_free (scm_tc16_environment
, environment_free
);
2319 scm_set_smob_print (scm_tc16_environment
, environment_print
);
2321 /* create observer smob */
2322 scm_tc16_observer
= scm_make_smob_type ("observer", 0);
2323 scm_set_smob_mark (scm_tc16_observer
, observer_mark
);
2324 scm_set_smob_print (scm_tc16_observer
, observer_print
);
2326 /* create system environment */
2327 scm_system_environment
= scm_make_leaf_environment ();
2328 scm_permanent_object (scm_system_environment
);
2333 scm_init_environments ()
2335 #include "libguile/environments.x"