X-Git-Url: http://git.hcoop.net/bpt/guile.git/blobdiff_plain/93d40df216ff9d8b528d67d20b7867e31ad89827..a61f4e0c61695e17984b25b16fbf720b851b739c:/libguile/environments.c diff --git a/libguile/environments.c b/libguile/environments.c index 8f6910648..bd90a43fe 100644 --- a/libguile/environments.c +++ b/libguile/environments.c @@ -1,43 +1,19 @@ -/* Copyright (C) 1999, 2000 Free Software Foundation, Inc. +/* Copyright (C) 1999,2000,2001, 2003 Free Software Foundation, Inc. * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. * - * This program is distributed in the hope that it will be useful, + * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this software; see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307 USA - * - * As a special exception, the Free Software Foundation gives permission - * for additional uses of the text contained in its release of GUILE. - * - * The exception is that, if you link the GUILE library with other files - * to produce an executable, this does not by itself cause the - * resulting executable to be covered by the GNU General Public License. - * Your use of that executable is in no way restricted on account of - * linking the GUILE library code into it. - * - * This exception does not however invalidate any other reasons why - * the executable file might be covered by the GNU General Public License. - * - * This exception applies only to the code released by the - * Free Software Foundation under the name GUILE. If you copy - * code from other Free Software Foundation releases into a copy of - * GUILE, as the General Public License permits, the exception does - * not apply to the code that you add in this way. To avoid misleading - * anyone as to the status of such modified files, you must delete - * this exception notice from them. - * - * If you write modifications of your own for GUILE, it is your choice - * whether to permit this exception to apply to your modifications. - * If you do not wish that, delete this exception notice. */ + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ @@ -46,6 +22,7 @@ #include "libguile/eval.h" #include "libguile/gh.h" #include "libguile/hash.h" +#include "libguile/list.h" #include "libguile/ports.h" #include "libguile/smob.h" #include "libguile/symbols.h" @@ -56,9 +33,11 @@ -long scm_tc16_environment; -long scm_tc16_observer; -#define DEFAULT_OBARRAY_SIZE 137 +scm_t_bits scm_tc16_environment; +scm_t_bits scm_tc16_observer; +#define DEFAULT_OBARRAY_SIZE 31 + +SCM scm_system_environment; @@ -117,50 +96,46 @@ scm_error_environment_immutable_location (const char *func, SCM env, SCM symbol) SCM scm_make_environment (void *type) { - SCM env; - - SCM_NEWCELL (env); - SCM_SET_CELL_WORD_1 (env, type); - SCM_SET_CELL_TYPE (env, scm_tc16_environment); - - return env; + return scm_cell (scm_tc16_environment, (scm_t_bits) type); } SCM_DEFINE (scm_environment_p, "environment?", 1, 0, 0, (SCM obj), - "Return #t if OBJ is an environment, or #f otherwise.") + "Return @code{#t} if @var{obj} is an environment, or @code{#f}\n" + "otherwise.") #define FUNC_NAME s_scm_environment_p { - return SCM_BOOL (SCM_ENVIRONMENT_P (obj)); + return scm_from_bool (SCM_ENVIRONMENT_P (obj)); } #undef FUNC_NAME SCM_DEFINE (scm_environment_bound_p, "environment-bound?", 2, 0, 0, (SCM env, SCM sym), - "Return #t if SYM is bound in ENV, or #f otherwise.") + "Return @code{#t} if @var{sym} is bound in @var{env}, or\n" + "@code{#f} otherwise.") #define FUNC_NAME s_scm_environment_bound_p { SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, FUNC_NAME); + SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME); - return SCM_BOOL (SCM_ENVIRONMENT_BOUND_P (env, sym)); + return scm_from_bool (SCM_ENVIRONMENT_BOUND_P (env, sym)); } #undef FUNC_NAME SCM_DEFINE (scm_environment_ref, "environment-ref", 2, 0, 0, (SCM env, SCM sym), - "Return the value of the location bound to SYM in ENV.\n" - "If SYM is unbound in ENV, signal an environment:unbound\n" - "error.") + "Return the value of the location bound to @var{sym} in\n" + "@var{env}. If @var{sym} is unbound in @var{env}, signal an\n" + "@code{environment:unbound} error.") #define FUNC_NAME s_scm_environment_ref { SCM val; SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, FUNC_NAME); + SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME); val = SCM_ENVIRONMENT_REF (env, sym); @@ -180,7 +155,7 @@ SCM scm_c_environment_ref (SCM env, SCM sym) { SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, "scm_c_environment_ref"); - SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, "scm_c_environment_ref"); + SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, "scm_c_environment_ref"); return SCM_ENVIRONMENT_REF (env, sym); } @@ -194,34 +169,41 @@ environment_default_folder (SCM proc, SCM symbol, SCM value, SCM tail) SCM_DEFINE (scm_environment_fold, "environment-fold", 3, 0, 0, (SCM env, SCM proc, SCM init), - "Iterate over all the bindings in ENV, accumulating some value.\n" - "For each binding in ENV, apply PROC to the symbol bound, its\n" - "value, and the result from the previous application of PROC.\n" - "Use INIT as PROC's third argument the first time PROC is\n" - "applied.\n" - "If ENV contains no bindings, this function simply returns INIT.\n" - "If ENV binds the symbol sym1 to the value val1, sym2 to val2,\n" - "and so on, then this procedure computes:\n" + "Iterate over all the bindings in @var{env}, accumulating some\n" + "value.\n" + "For each binding in @var{env}, apply @var{proc} to the symbol\n" + "bound, its value, and the result from the previous application\n" + "of @var{proc}.\n" + "Use @var{init} as @var{proc}'s third argument the first time\n" + "@var{proc} is applied.\n" + "If @var{env} contains no bindings, this function simply returns\n" + "@var{init}.\n" + "If @var{env} binds the symbol sym1 to the value val1, sym2 to\n" + "val2, and so on, then this procedure computes:\n" + "@lisp\n" " (proc sym1 val1\n" " (proc sym2 val2\n" " ...\n" " (proc symn valn\n" " init)))\n" - "Each binding in ENV will be processed exactly once.\n" - "environment-fold makes no guarantees about the order in which\n" - "the bindings are processed.\n" + "@end lisp\n" + "Each binding in @var{env} will be processed exactly once.\n" + "@code{environment-fold} makes no guarantees about the order in\n" + "which the bindings are processed.\n" "Here is a function which, given an environment, constructs an\n" "association list representing that environment's bindings,\n" "using environment-fold:\n" + "@lisp\n" " (define (environment->alist env)\n" " (environment-fold env\n" " (lambda (sym val tail)\n" " (cons (cons sym val) tail))\n" - " '()))") + " '()))\n" + "@end lisp") #define FUNC_NAME s_scm_environment_fold { SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T), + SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), proc, SCM_ARG2, FUNC_NAME); return SCM_ENVIRONMENT_FOLD (env, environment_default_folder, proc, init); @@ -246,24 +228,25 @@ scm_c_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init SCM_DEFINE (scm_environment_define, "environment-define", 3, 0, 0, (SCM env, SCM sym, SCM val), - "Bind SYM to a new location containing VAL in ENV. If SYM is\n" - "already bound to another location in ENV and the binding is\n" - "mutable, that binding is replaced. The new binding and\n" - "location are both mutable. The return value is unspecified.\n" - "If SYM is already bound in ENV, and the binding is immutable,\n" - "signal an environment:immutable-binding error.") + "Bind @var{sym} to a new location containing @var{val} in\n" + "@var{env}. If @var{sym} is already bound to another location\n" + "in @var{env} and the binding is mutable, that binding is\n" + "replaced. The new binding and location are both mutable. The\n" + "return value is unspecified.\n" + "If @var{sym} is already bound in @var{env}, and the binding is\n" + "immutable, signal an @code{environment:immutable-binding} error.") #define FUNC_NAME s_scm_environment_define { SCM status; SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, FUNC_NAME); + SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME); status = SCM_ENVIRONMENT_DEFINE (env, sym, val); - if (SCM_EQ_P (status, SCM_ENVIRONMENT_SUCCESS)) + if (scm_is_eq (status, SCM_ENVIRONMENT_SUCCESS)) return SCM_UNSPECIFIED; - else if (SCM_EQ_P (status, SCM_ENVIRONMENT_BINDING_IMMUTABLE)) + else if (scm_is_eq (status, SCM_ENVIRONMENT_BINDING_IMMUTABLE)) scm_error_environment_immutable_binding (FUNC_NAME, env, sym); else abort(); @@ -273,22 +256,23 @@ SCM_DEFINE (scm_environment_define, "environment-define", 3, 0, 0, SCM_DEFINE (scm_environment_undefine, "environment-undefine", 2, 0, 0, (SCM env, SCM sym), - "Remove any binding for SYM from ENV. If SYM is unbound in ENV,\n" - "do nothing. The return value is unspecified.\n" - "If SYM is already bound in ENV, and the binding is immutable,\n" - "signal an environment:immutable-binding error.") + "Remove any binding for @var{sym} from @var{env}. If @var{sym}\n" + "is unbound in @var{env}, do nothing. The return value is\n" + "unspecified.\n" + "If @var{sym} is already bound in @var{env}, and the binding is\n" + "immutable, signal an @code{environment:immutable-binding} error.") #define FUNC_NAME s_scm_environment_undefine { SCM status; SCM_ASSERT(SCM_ENVIRONMENT_P(env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT(SCM_SYMBOLP(sym), sym, SCM_ARG2, FUNC_NAME); + SCM_ASSERT(scm_is_symbol(sym), sym, SCM_ARG2, FUNC_NAME); status = SCM_ENVIRONMENT_UNDEFINE (env, sym); - if (SCM_EQ_P (status, SCM_ENVIRONMENT_SUCCESS)) + if (scm_is_eq (status, SCM_ENVIRONMENT_SUCCESS)) return SCM_UNSPECIFIED; - else if (SCM_EQ_P (status, SCM_ENVIRONMENT_BINDING_IMMUTABLE)) + else if (scm_is_eq (status, SCM_ENVIRONMENT_BINDING_IMMUTABLE)) scm_error_environment_immutable_binding (FUNC_NAME, env, sym); else abort(); @@ -298,25 +282,27 @@ SCM_DEFINE (scm_environment_undefine, "environment-undefine", 2, 0, 0, SCM_DEFINE (scm_environment_set_x, "environment-set!", 3, 0, 0, (SCM env, SCM sym, SCM val), - "If ENV binds SYM to some location, change that location's\n" - "value to VAL. The return value is unspecified.\n" - "If SYM is not bound in ENV, signal an environment:unbound\n" - "error. If ENV binds SYM to an immutable location, signal an\n" - "environment:immutable-location error.") + "If @var{env} binds @var{sym} to some location, change that\n" + "location's value to @var{val}. The return value is\n" + "unspecified.\n" + "If @var{sym} is not bound in @var{env}, signal an\n" + "@code{environment:unbound} error. If @var{env} binds @var{sym}\n" + "to an immutable location, signal an\n" + "@code{environment:immutable-location} error.") #define FUNC_NAME s_scm_environment_set_x { SCM status; SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, FUNC_NAME); + SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME); status = SCM_ENVIRONMENT_SET (env, sym, val); - if (SCM_EQ_P (status, SCM_ENVIRONMENT_SUCCESS)) + if (scm_is_eq (status, SCM_ENVIRONMENT_SUCCESS)) return SCM_UNSPECIFIED; else if (SCM_UNBNDP (status)) scm_error_environment_unbound (FUNC_NAME, env, sym); - else if (SCM_EQ_P (status, SCM_ENVIRONMENT_LOCATION_IMMUTABLE)) + else if (scm_is_eq (status, SCM_ENVIRONMENT_LOCATION_IMMUTABLE)) scm_error_environment_immutable_binding (FUNC_NAME, env, sym); else abort(); @@ -326,30 +312,32 @@ SCM_DEFINE (scm_environment_set_x, "environment-set!", 3, 0, 0, SCM_DEFINE (scm_environment_cell, "environment-cell", 3, 0, 0, (SCM env, SCM sym, SCM for_write), - "Return the value cell which ENV binds to SYM, or #f if the\n" - "binding does not live in a value cell.\n" - "The argument FOR-WRITE indicates whether the caller intends\n" - "to modify the variable's value by mutating the value cell. If\n" - "the variable is immutable, then environment-cell signals an\n" - "environment:immutable-location error.\n" - "If SYM is unbound in ENV, signal an environment:unbound error.\n" + "Return the value cell which @var{env} binds to @var{sym}, or\n" + "@code{#f} if the binding does not live in a value cell.\n" + "The argument @var{for-write} indicates whether the caller\n" + "intends to modify the variable's value by mutating the value\n" + "cell. If the variable is immutable, then\n" + "@code{environment-cell} signals an\n" + "@code{environment:immutable-location} error.\n" + "If @var{sym} is unbound in @var{env}, signal an\n" + "@code{environment:unbound} error.\n" "If you use this function, you should consider using\n" - "environment-observe, to be notified when SYM gets re-bound to\n" - "a new value cell, or becomes undefined.") + "@code{environment-observe}, to be notified when @var{sym} gets\n" + "re-bound to a new value cell, or becomes undefined.") #define FUNC_NAME s_scm_environment_cell { SCM location; SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, FUNC_NAME); - SCM_ASSERT (SCM_BOOLP (for_write), for_write, SCM_ARG3, FUNC_NAME); + SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME); + SCM_ASSERT (scm_is_bool (for_write), for_write, SCM_ARG3, FUNC_NAME); - location = SCM_ENVIRONMENT_CELL (env, sym, !SCM_FALSEP (for_write)); + location = SCM_ENVIRONMENT_CELL (env, sym, scm_is_true (for_write)); if (!SCM_IMP (location)) return location; else if (SCM_UNBNDP (location)) scm_error_environment_unbound (FUNC_NAME, env, sym); - else if (SCM_EQ_P (location, SCM_ENVIRONMENT_LOCATION_IMMUTABLE)) + else if (scm_is_eq (location, SCM_ENVIRONMENT_LOCATION_IMMUTABLE)) scm_error_environment_immutable_location (FUNC_NAME, env, sym); else /* no cell */ return location; @@ -367,7 +355,7 @@ SCM scm_c_environment_cell(SCM env, SCM sym, int for_write) { SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, "scm_c_environment_cell"); - SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, "scm_c_environment_cell"); + SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, "scm_c_environment_cell"); return SCM_ENVIRONMENT_CELL (env, sym, for_write); } @@ -382,11 +370,12 @@ environment_default_observer (SCM env, SCM proc) SCM_DEFINE (scm_environment_observe, "environment-observe", 2, 0, 0, (SCM env, SCM proc), - "Whenever ENV's bindings change, apply PROC to ENV.\n" + "Whenever @var{env}'s bindings change, apply @var{proc} to\n" + "@var{env}.\n" "This function returns an object, token, which you can pass to\n" - "environment-unobserve to remove PROC from the set of\n" - "procedures observing ENV. The type and value of token is\n" - "unspecified.") + "@code{environment-unobserve} to remove @var{proc} from the set\n" + "of procedures observing @var{env}. The type and value of\n" + "token is unspecified.") #define FUNC_NAME s_scm_environment_observe { SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); @@ -399,9 +388,10 @@ SCM_DEFINE (scm_environment_observe, "environment-observe", 2, 0, 0, SCM_DEFINE (scm_environment_observe_weak, "environment-observe-weak", 2, 0, 0, (SCM env, SCM proc), "This function is the same as environment-observe, except that\n" - "the reference ENV retains to PROC is a weak reference. This\n" - "means that, if there are no other live, non-weak references\n" - "to PROC, it will be garbage-collected, and dropped from ENV's\n" + "the reference @var{env} retains to @var{proc} is a weak\n" + "reference. This means that, if there are no other live,\n" + "non-weak references to @var{proc}, it will be\n" + "garbage-collected, and dropped from @var{env}'s\n" "list of observing procedures.") #define FUNC_NAME s_scm_environment_observe_weak { @@ -434,10 +424,11 @@ scm_c_environment_observe (SCM env, scm_environment_observer proc, SCM data, int SCM_DEFINE (scm_environment_unobserve, "environment-unobserve", 1, 0, 0, (SCM token), "Cancel the observation request which returned the value\n" - "TOKEN. The return value is unspecified.\n" - "If a call (environment-observe env proc) returns token, then\n" - "the call (environment-unobserve token) will cause proc to no\n" - "longer be called when env's bindings change.") + "@var{token}. The return value is unspecified.\n" + "If a call @code{(environment-observe env proc)} returns\n" + "@var{token}, then the call @code{(environment-unobserve token)}\n" + "will cause @var{proc} to no longer be called when @var{env}'s\n" + "bindings change.") #define FUNC_NAME s_scm_environment_unobserve { SCM env; @@ -453,21 +444,22 @@ SCM_DEFINE (scm_environment_unobserve, "environment-unobserve", 1, 0, 0, static SCM -mark_environment (SCM env) +environment_mark (SCM env) { return (*(SCM_ENVIRONMENT_FUNCS (env)->mark)) (env); } -static scm_sizet -free_environment (SCM env) +static size_t +environment_free (SCM env) { - return (*(SCM_ENVIRONMENT_FUNCS (env)->free)) (env); + (*(SCM_ENVIRONMENT_FUNCS (env)->free)) (env); + return 0; } static int -print_environment (SCM env, SCM port, scm_print_state *pstate) +environment_print (SCM env, SCM port, scm_print_state *pstate) { return (*(SCM_ENVIRONMENT_FUNCS (env)->print)) (env, port, pstate); } @@ -477,7 +469,7 @@ print_environment (SCM env, SCM port, scm_print_state *pstate) /* observers */ static SCM -mark_observer (SCM observer) +observer_mark (SCM observer) { scm_gc_mark (SCM_OBSERVER_ENVIRONMENT (observer)); scm_gc_mark (SCM_OBSERVER_DATA (observer)); @@ -485,21 +477,14 @@ mark_observer (SCM observer) } -static scm_sizet -free_observer (SCM observer_smob) -{ - return 0; -} - - static int -print_observer (SCM type, SCM port, scm_print_state *pstate) +observer_print (SCM type, SCM port, scm_print_state *pstate SCM_UNUSED) { - SCM address = scm_ulong2num (SCM_UNPACK (type)); - SCM base16 = scm_number_to_string (address, SCM_MAKINUM (16)); + SCM address = scm_from_size_t (SCM_UNPACK (type)); + SCM base16 = scm_number_to_string (address, scm_from_int (16)); scm_puts ("#", port); return 1; @@ -522,10 +507,12 @@ print_observer (SCM type, SCM port, scm_print_state *pstate) static SCM obarray_enter (SCM obarray, SCM symbol, SCM data) { - scm_sizet hash = SCM_SYMBOL_HASH (symbol) % SCM_VECTOR_LENGTH (obarray); + size_t hash = scm_i_symbol_hash (symbol) % SCM_HASHTABLE_N_BUCKETS (obarray); SCM entry = scm_cons (symbol, data); - SCM slot = scm_cons (entry, SCM_VELTS (obarray)[hash]); - SCM_VELTS (obarray)[hash] = slot; + SCM slot = scm_cons (entry, SCM_HASHTABLE_BUCKETS (obarray)[hash]); + SCM_SET_HASHTABLE_BUCKET (obarray, hash, slot); + if (SCM_HASHTABLE_N_ITEMS (obarray) > SCM_HASHTABLE_UPPER (obarray)) + scm_i_rehash (obarray, scm_i_hash_symbol, 0, "obarray_enter"); return entry; } @@ -538,23 +525,27 @@ obarray_enter (SCM obarray, SCM symbol, SCM data) static SCM obarray_replace (SCM obarray, SCM symbol, SCM data) { - scm_sizet hash = SCM_SYMBOL_HASH (symbol) % SCM_VECTOR_LENGTH (obarray); + size_t hash = scm_i_symbol_hash (symbol) % SCM_HASHTABLE_N_BUCKETS (obarray); SCM new_entry = scm_cons (symbol, data); SCM lsym; SCM slot; - for (lsym = SCM_VELTS (obarray)[hash]; !SCM_NULLP (lsym); lsym = SCM_CDR (lsym)) + for (lsym = SCM_HASHTABLE_BUCKETS (obarray)[hash]; + !SCM_NULLP (lsym); + lsym = SCM_CDR (lsym)) { SCM old_entry = SCM_CAR (lsym); - if (SCM_EQ_P (SCM_CAR (old_entry), symbol)) + if (scm_is_eq (SCM_CAR (old_entry), symbol)) { SCM_SETCAR (lsym, new_entry); return old_entry; } } - slot = scm_cons (new_entry, SCM_VELTS (obarray)[hash]); - SCM_VELTS (obarray)[hash] = slot; + slot = scm_cons (new_entry, SCM_HASHTABLE_BUCKETS (obarray)[hash]); + SCM_SET_HASHTABLE_BUCKET (obarray, hash, slot); + if (SCM_HASHTABLE_N_ITEMS (obarray) > SCM_HASHTABLE_UPPER (obarray)) + scm_i_rehash (obarray, scm_i_hash_symbol, 0, "obarray_replace"); return SCM_BOOL_F; } @@ -566,13 +557,15 @@ obarray_replace (SCM obarray, SCM symbol, SCM data) static SCM obarray_retrieve (SCM obarray, SCM sym) { - scm_sizet hash = SCM_SYMBOL_HASH (sym) % SCM_VECTOR_LENGTH (obarray); + size_t hash = scm_i_symbol_hash (sym) % SCM_HASHTABLE_N_BUCKETS (obarray); SCM lsym; - for (lsym = SCM_VELTS (obarray)[hash]; !SCM_NULLP (lsym); lsym = SCM_CDR (lsym)) + for (lsym = SCM_HASHTABLE_BUCKETS (obarray)[hash]; + !SCM_NULLP (lsym); + lsym = SCM_CDR (lsym)) { SCM entry = SCM_CAR (lsym); - if (SCM_EQ_P (SCM_CAR (entry), sym)) + if (scm_is_eq (SCM_CAR (entry), sym)) return entry; } @@ -587,36 +580,32 @@ obarray_retrieve (SCM obarray, SCM sym) static SCM obarray_remove (SCM obarray, SCM sym) { - scm_sizet hash = SCM_SYMBOL_HASH (sym) % SCM_VECTOR_LENGTH (obarray); - SCM lsym; - SCM *lsymp; + size_t hash = scm_i_symbol_hash (sym) % SCM_HASHTABLE_N_BUCKETS (obarray); + SCM table_entry = SCM_HASHTABLE_BUCKETS (obarray)[hash]; + SCM handle = scm_sloppy_assq (sym, table_entry); - /* Dirk:FIXME:: gc problem due to use of &SCM_VELTS[hash] */ - for (lsym = *(lsymp = &SCM_VELTS (obarray)[hash]); - !SCM_NULLP (lsym); - lsym = *(lsymp = SCM_CDRLOC (lsym))) + if (SCM_CONSP (handle)) { - SCM entry = SCM_CAR (lsym); - if (SCM_EQ_P (SCM_CAR (entry), sym)) - { - *lsymp = SCM_CDR (lsym); - return entry; - } + SCM new_table_entry = scm_delq1_x (handle, table_entry); + SCM_SET_HASHTABLE_BUCKET (obarray, hash, new_table_entry); + SCM_HASHTABLE_DECREMENT (obarray); } - return SCM_BOOL_F; + + return handle; } static void obarray_remove_all (SCM obarray) { - scm_sizet size = SCM_VECTOR_LENGTH (obarray); - scm_sizet i; + size_t size = SCM_HASHTABLE_N_BUCKETS (obarray); + size_t i; for (i = 0; i < size; i++) { - SCM_VELTS (obarray)[i] = SCM_EOL; + SCM_SET_HASHTABLE_BUCKET (obarray, i, SCM_EOL); } + SCM_SET_HASHTABLE_N_ITEMS (obarray, 0); } @@ -647,20 +636,17 @@ struct core_environments_base { #define CORE_ENVIRONMENT_WEAK_OBSERVERS(env) \ (SCM_VELTS (CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env)) [0]) #define SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS(env, v) \ - (SCM_VELTS (CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env)) [0] = (v)) + (SCM_VECTOR_SET (CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env), 0, (v))) static SCM core_environments_observe (SCM env, scm_environment_observer proc, SCM data, int weak_p) { - SCM observer; - - SCM_NEWCELL2 (observer); - SCM_SET_CELL_OBJECT_1 (observer, env); - SCM_SET_CELL_OBJECT_2 (observer, data); - SCM_SET_CELL_WORD_3 (observer, proc); - SCM_SET_CELL_TYPE (observer, scm_tc16_observer); + SCM observer = scm_double_cell (scm_tc16_observer, + SCM_UNPACK (env), + SCM_UNPACK (data), + (scm_t_bits) proc); if (!weak_p) { @@ -696,7 +682,7 @@ core_environments_unobserve (SCM env, SCM observer) ? SCM_CDAR (l) : SCM_CAR (l); - if (SCM_EQ_P (first, observer)) + if (scm_is_eq (first, observer)) { /* Remove the first observer */ handling_weaks @@ -714,7 +700,7 @@ core_environments_unobserve (SCM env, SCM observer) ? SCM_CDAR (l) : SCM_CAR (l); - if (SCM_EQ_P (next, observer)) + if (scm_is_eq (next, observer)) { SCM_SETCDR (l, SCM_CDR (rest)); return; @@ -739,7 +725,7 @@ core_environments_mark (SCM env) static void -core_environments_finalize (SCM env) +core_environments_finalize (SCM env SCM_UNUSED) { } @@ -759,7 +745,7 @@ core_environments_init (struct core_environments_base *body, { body->funcs = funcs; body->observers = SCM_EOL; - body->weak_observers = scm_make_weak_value_hash_table (SCM_MAKINUM (1)); + body->weak_observers = scm_make_weak_value_alist_vector (scm_from_int (1)); } @@ -801,9 +787,10 @@ update_catch_handler (void *ptr, SCM tag, SCM args) { struct update_data *data = (struct update_data *) ptr; SCM observer = data->observer; - SCM message = scm_makfrom0str ("Observer `~A' signals `~A' error: ~S"); + SCM message = + scm_from_locale_string ("Observer `~A' signals `~A' error: ~S"); - return scm_cons (message, scm_listify (observer, tag, args, SCM_UNDEFINED)); + return scm_cons (message, scm_list_3 (observer, tag, args)); } @@ -893,14 +880,16 @@ leaf_environment_ref (SCM env, SCM sym) static SCM leaf_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init) { - scm_sizet i; + size_t i; SCM result = init; SCM obarray = LEAF_ENVIRONMENT (env)->obarray; - for (i = 0; i < SCM_VECTOR_LENGTH (obarray); i++) + for (i = 0; i < SCM_HASHTABLE_N_BUCKETS (obarray); i++) { SCM l; - for (l = SCM_VELTS (obarray)[i]; !SCM_NULLP (l); l = SCM_CDR (l)) + for (l = SCM_HASHTABLE_BUCKETS (obarray)[i]; + !SCM_NULLP (l); + l = SCM_CDR (l)) { SCM binding = SCM_CAR (l); SCM symbol = SCM_CAR (binding); @@ -933,7 +922,7 @@ leaf_environment_undefine (SCM env, SCM sym) SCM obarray = LEAF_ENVIRONMENT (env)->obarray; SCM removed = obarray_remove (obarray, sym); - if (!SCM_FALSEP (removed)) + if (scm_is_true (removed)) core_environments_broadcast (env); return SCM_ENVIRONMENT_SUCCESS; @@ -962,7 +951,7 @@ leaf_environment_set_x (SCM env, SCM sym, SCM val) static SCM -leaf_environment_cell(SCM env, SCM sym, int for_write) +leaf_environment_cell (SCM env, SCM sym, int for_write SCM_UNUSED) { SCM obarray = LEAF_ENVIRONMENT (env)->obarray; SCM binding = obarray_retrieve (obarray, sym); @@ -971,31 +960,30 @@ leaf_environment_cell(SCM env, SCM sym, int for_write) static SCM -mark_leaf_environment (SCM env) +leaf_environment_mark (SCM env) { scm_gc_mark (LEAF_ENVIRONMENT (env)->obarray); return core_environments_mark (env); } -static scm_sizet -free_leaf_environment (SCM env) +static void +leaf_environment_free (SCM env) { core_environments_finalize (env); - - free (LEAF_ENVIRONMENT (env)); - return sizeof (struct leaf_environment); + scm_gc_free (LEAF_ENVIRONMENT (env), sizeof (struct leaf_environment), + "leaf environment"); } static int -print_leaf_environment (SCM type, SCM port, scm_print_state *pstate) +leaf_environment_print (SCM type, SCM port, scm_print_state *pstate SCM_UNUSED) { - SCM address = scm_ulong2num (SCM_UNPACK (type)); - SCM base16 = scm_number_to_string (address, SCM_MAKINUM (16)); + SCM address = scm_from_size_t (SCM_UNPACK (type)); + SCM base16 = scm_number_to_string (address, scm_from_int (16)); scm_puts ("#", port); return 1; @@ -1011,9 +999,9 @@ static struct scm_environment_funcs leaf_environment_funcs = { leaf_environment_cell, core_environments_observe, core_environments_unobserve, - mark_leaf_environment, - free_leaf_environment, - print_leaf_environment + leaf_environment_mark, + leaf_environment_free, + leaf_environment_print }; @@ -1027,8 +1015,8 @@ SCM_DEFINE (scm_make_leaf_environment, "make-leaf-environment", 0, 0, 0, "will be mutable.") #define FUNC_NAME s_scm_make_leaf_environment { - scm_sizet size = sizeof (struct leaf_environment); - struct leaf_environment *body = scm_must_malloc (size, FUNC_NAME); + size_t size = sizeof (struct leaf_environment); + struct leaf_environment *body = scm_gc_malloc (size, "leaf environment"); SCM env; core_environments_preinit (&body->base); @@ -1037,7 +1025,7 @@ SCM_DEFINE (scm_make_leaf_environment, "make-leaf-environment", 0, 0, 0, env = scm_make_environment (body); core_environments_init (&body->base, &leaf_environment_funcs); - body->obarray = scm_make_vector (SCM_MAKINUM (DEFAULT_OBARRAY_SIZE), SCM_EOL); + body->obarray = scm_c_make_hash_table (DEFAULT_OBARRAY_SIZE); return env; } @@ -1046,10 +1034,11 @@ SCM_DEFINE (scm_make_leaf_environment, "make-leaf-environment", 0, 0, 0, SCM_DEFINE (scm_leaf_environment_p, "leaf-environment?", 1, 0, 0, (SCM object), - "Return #t if object is a leaf environment, or #f otherwise.") + "Return @code{#t} if object is a leaf environment, or @code{#f}\n" + "otherwise.") #define FUNC_NAME s_scm_leaf_environment_p { - return SCM_BOOL (SCM_LEAF_ENVIRONMENT_P (object)); + return scm_from_bool (SCM_LEAF_ENVIRONMENT_P (object)); } #undef FUNC_NAME @@ -1096,9 +1085,9 @@ struct eval_environment { #define EVAL_ENVIRONMENT(env) \ ((struct eval_environment *) SCM_CELL_WORD_1 (env)) -#define IMMUTABLE SCM_MAKINUM (0) -#define MUTABLE SCM_MAKINUM (1) -#define UNKNOWN SCM_MAKINUM (2) +#define IMMUTABLE SCM_I_MAKINUM (0) +#define MUTABLE SCM_I_MAKINUM (1) +#define UNKNOWN SCM_I_MAKINUM (2) #define CACHED_LOCATION(x) SCM_CAR (x) #define CACHED_MUTABILITY(x) SCM_CADR (x) @@ -1136,10 +1125,10 @@ eval_environment_lookup (SCM env, SCM sym, int for_write) return location; mutability = CACHED_MUTABILITY (entry); - if (SCM_EQ_P (mutability, MUTABLE)) + if (scm_is_eq (mutability, MUTABLE)) return location; - if (SCM_EQ_P (mutability, UNKNOWN)) + if (scm_is_eq (mutability, UNKNOWN)) { SCM source_env = CACHED_SOURCE_ENVIRONMENT (entry); SCM location = SCM_ENVIRONMENT_CELL (source_env, sym, 1); @@ -1191,7 +1180,7 @@ eval_environment_lookup (SCM env, SCM sym, int for_write) obarray_enter (obarray, sym, entry); return location; } - else if (SCM_EQ_P (location, SCM_ENVIRONMENT_LOCATION_NO_CELL)) + else if (scm_is_eq (location, SCM_ENVIRONMENT_LOCATION_NO_CELL)) { obarray_enter (obarray, sym, source_env); return source_env; @@ -1232,7 +1221,7 @@ eval_environment_folder (SCM extended_data, SCM symbol, SCM value, SCM tail) if (!SCM_ENVIRONMENT_BOUND_P (local, symbol)) { SCM proc_as_nr = SCM_CADR (extended_data); - unsigned long int proc_as_ul = scm_num2ulong (proc_as_nr, NULL, NULL); + unsigned long int proc_as_ul = scm_to_ulong (proc_as_nr); scm_environment_folder proc = (scm_environment_folder) proc_as_ul; SCM data = SCM_CDDR (extended_data); @@ -1250,7 +1239,7 @@ eval_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init) { SCM local = EVAL_ENVIRONMENT (env)->local; SCM imported = EVAL_ENVIRONMENT (env)->imported; - SCM proc_as_nr = scm_ulong2num ((unsigned long int) proc); + SCM proc_as_nr = scm_from_ulong ((unsigned long) proc); SCM extended_data = scm_cons2 (local, proc_as_nr, data); SCM tmp_result = scm_c_environment_fold (imported, eval_environment_folder, extended_data, init); @@ -1293,7 +1282,7 @@ eval_environment_set_x (SCM env, SCM sym, SCM val) { return SCM_ENVIRONMENT_SET (location, sym, val); } - else if (SCM_EQ_P (location, IMMUTABLE)) + else if (scm_is_eq (location, IMMUTABLE)) { return SCM_ENVIRONMENT_LOCATION_IMMUTABLE; } @@ -1315,7 +1304,7 @@ eval_environment_cell (SCM env, SCM sym, int for_write) return location; else if (SCM_ENVIRONMENT_P (location)) return SCM_ENVIRONMENT_LOCATION_NO_CELL; - else if (SCM_EQ_P (location, IMMUTABLE)) + else if (scm_is_eq (location, IMMUTABLE)) return SCM_ENVIRONMENT_LOCATION_IMMUTABLE; else return SCM_UNDEFINED; @@ -1324,7 +1313,7 @@ eval_environment_cell (SCM env, SCM sym, int for_write) static SCM -mark_eval_environment (SCM env) +eval_environment_mark (SCM env) { struct eval_environment *body = EVAL_ENVIRONMENT (env); @@ -1338,24 +1327,23 @@ mark_eval_environment (SCM env) } -static scm_sizet -free_eval_environment (SCM env) +static void +eval_environment_free (SCM env) { core_environments_finalize (env); - - free (EVAL_ENVIRONMENT (env)); - return sizeof (struct eval_environment); + scm_gc_free (EVAL_ENVIRONMENT (env), sizeof (struct eval_environment), + "eval environment"); } static int -print_eval_environment (SCM type, SCM port, scm_print_state *pstate) +eval_environment_print (SCM type, SCM port, scm_print_state *pstate SCM_UNUSED) { - SCM address = scm_ulong2num (SCM_UNPACK (type)); - SCM base16 = scm_number_to_string (address, SCM_MAKINUM (16)); + SCM address = scm_from_size_t (SCM_UNPACK (type)); + SCM base16 = scm_number_to_string (address, scm_from_int (16)); scm_puts ("#", port); return 1; @@ -1371,9 +1359,9 @@ static struct scm_environment_funcs eval_environment_funcs = { eval_environment_cell, core_environments_observe, core_environments_unobserve, - mark_eval_environment, - free_eval_environment, - print_eval_environment + eval_environment_mark, + eval_environment_free, + eval_environment_print }; @@ -1381,7 +1369,7 @@ void *scm_type_eval_environment = &eval_environment_funcs; static void -eval_environment_observer (SCM caller, SCM eval_env) +eval_environment_observer (SCM caller SCM_UNUSED, SCM eval_env) { SCM obarray = EVAL_ENVIRONMENT (eval_env)->obarray; @@ -1393,22 +1381,26 @@ eval_environment_observer (SCM caller, SCM eval_env) SCM_DEFINE (scm_make_eval_environment, "make-eval-environment", 2, 0, 0, (SCM local, SCM imported), "Return a new environment object eval whose bindings are the\n" - "union of the bindings in the environments local and imported,\n" - "with bindings from local taking precedence. Definitions made\n" - "in eval are placed in local.\n" - "Applying environment-define or environment-undefine to eval\n" - "has the same effect as applying the procedure to local.\n" - "Note that eval incorporates local and imported by reference:\n" + "union of the bindings in the environments @var{local} and\n" + "@var{imported}, with bindings from @var{local} taking\n" + "precedence. Definitions made in eval are placed in @var{local}.\n" + "Applying @code{environment-define} or\n" + "@code{environment-undefine} to eval has the same effect as\n" + "applying the procedure to @var{local}.\n" + "Note that eval incorporates @var{local} and @var{imported} by\n" + "reference:\n" "If, after creating eval, the program changes the bindings of\n" - "local or imported, those changes will be visible in eval.\n" + "@var{local} or @var{imported}, those changes will be visible\n" + "in eval.\n" "Since most Scheme evaluation takes place in eval environments,\n" - "they transparenty cache the bindings received from local and\n" - "imported. Thus, the first time the program looks up a symbol\n" - "in eval, eval may make calls to local or imported to find\n" - "their bindings, but subsequent references to that symbol will\n" - "be as fast as references to bindings in finite environments.\n" - "In typical use, local will be a finite environment, and\n" - "imported will be an import environment") + "they transparently cache the bindings received from @var{local}\n" + "and @var{imported}. Thus, the first time the program looks up\n" + "a symbol in eval, eval may make calls to @var{local} or\n" + "@var{imported} to find their bindings, but subsequent\n" + "references to that symbol will be as fast as references to\n" + "bindings in finite environments.\n" + "In typical use, @var{local} will be a finite environment, and\n" + "@var{imported} will be an import environment") #define FUNC_NAME s_scm_make_eval_environment { SCM env; @@ -1417,7 +1409,7 @@ SCM_DEFINE (scm_make_eval_environment, "make-eval-environment", 2, 0, 0, SCM_ASSERT (SCM_ENVIRONMENT_P (local), local, SCM_ARG1, FUNC_NAME); SCM_ASSERT (SCM_ENVIRONMENT_P (imported), imported, SCM_ARG2, FUNC_NAME); - body = scm_must_malloc (sizeof (struct eval_environment), FUNC_NAME); + body = scm_gc_malloc (sizeof (struct eval_environment), "eval environment"); core_environments_preinit (&body->base); body->obarray = SCM_BOOL_F; @@ -1429,7 +1421,7 @@ SCM_DEFINE (scm_make_eval_environment, "make-eval-environment", 2, 0, 0, env = scm_make_environment (body); core_environments_init (&body->base, &eval_environment_funcs); - body->obarray = scm_make_vector (SCM_MAKINUM (DEFAULT_OBARRAY_SIZE), SCM_EOL); + body->obarray = scm_c_make_hash_table (DEFAULT_OBARRAY_SIZE); body->imported = imported; body->imported_observer = SCM_ENVIRONMENT_OBSERVE (imported, eval_environment_observer, env, 1); @@ -1444,17 +1436,18 @@ SCM_DEFINE (scm_make_eval_environment, "make-eval-environment", 2, 0, 0, SCM_DEFINE (scm_eval_environment_p, "eval-environment?", 1, 0, 0, (SCM object), - "Return #t if object is an eval environment, or #f otherwise.") + "Return @code{#t} if object is an eval environment, or @code{#f}\n" + "otherwise.") #define FUNC_NAME s_scm_eval_environment_p { - return SCM_BOOL (SCM_EVAL_ENVIRONMENT_P (object)); + return scm_from_bool (SCM_EVAL_ENVIRONMENT_P (object)); } #undef FUNC_NAME SCM_DEFINE (scm_eval_environment_local, "eval-environment-local", 1, 0, 0, (SCM env), - "Return the local environment of eval environment env.") + "Return the local environment of eval environment @var{env}.") #define FUNC_NAME s_scm_eval_environment_local { SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); @@ -1466,7 +1459,7 @@ SCM_DEFINE (scm_eval_environment_local, "eval-environment-local", 1, 0, 0, SCM_DEFINE (scm_eval_environment_set_local_x, "eval-environment-set-local!", 2, 0, 0, (SCM env, SCM local), - "Change env's local environment to LOCAL.") + "Change @var{env}'s local environment to @var{local}.") #define FUNC_NAME s_scm_eval_environment_set_local_x { struct eval_environment *body; @@ -1492,7 +1485,7 @@ SCM_DEFINE (scm_eval_environment_set_local_x, "eval-environment-set-local!", 2, SCM_DEFINE (scm_eval_environment_imported, "eval-environment-imported", 1, 0, 0, (SCM env), - "Return the imported environment of eval environment env.") + "Return the imported environment of eval environment @var{env}.") #define FUNC_NAME s_scm_eval_environment_imported { SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); @@ -1504,7 +1497,7 @@ SCM_DEFINE (scm_eval_environment_imported, "eval-environment-imported", 1, 0, 0, SCM_DEFINE (scm_eval_environment_set_imported_x, "eval-environment-set-imported!", 2, 0, 0, (SCM env, SCM imported), - "Change env's imported environment to IMPORTED.") + "Change @var{env}'s imported environment to @var{imported}.") #define FUNC_NAME s_scm_eval_environment_set_imported_x { struct eval_environment *body; @@ -1594,7 +1587,7 @@ import_environment_conflict (SCM env, SCM sym, SCM imports) SCM conflict_proc = IMPORT_ENVIRONMENT (env)->conflict_proc; SCM args = scm_cons2 (env, sym, scm_cons (imports, SCM_EOL)); - return scm_apply (conflict_proc, args, SCM_EOL); + return scm_apply_0 (conflict_proc, args); } @@ -1633,11 +1626,11 @@ import_environment_folder (SCM extended_data, SCM symbol, SCM value, SCM tail) SCM imported_env = SCM_CADR (extended_data); SCM owner = import_environment_lookup (import_env, symbol); SCM proc_as_nr = SCM_CADDR (extended_data); - unsigned long int proc_as_ul = scm_num2ulong (proc_as_nr, NULL, NULL); + unsigned long int proc_as_ul = scm_to_ulong (proc_as_nr); scm_environment_folder proc = (scm_environment_folder) proc_as_ul; SCM data = SCM_CDDDR (extended_data); - if (SCM_CONSP (owner) && SCM_EQ_P (SCM_CAR (owner), imported_env)) + if (SCM_CONSP (owner) && scm_is_eq (SCM_CAR (owner), imported_env)) owner = import_environment_conflict (import_env, symbol, owner); if (SCM_ENVIRONMENT_P (owner)) @@ -1651,7 +1644,7 @@ import_environment_folder (SCM extended_data, SCM symbol, SCM value, SCM tail) static SCM import_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init) { - SCM proc_as_nr = scm_ulong2num ((unsigned long int) proc); + SCM proc_as_nr = scm_from_ulong ((unsigned long) proc); SCM result = init; SCM l; @@ -1668,7 +1661,9 @@ import_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM ini static SCM -import_environment_define (SCM env, SCM sym, SCM val) +import_environment_define (SCM env SCM_UNUSED, + SCM sym SCM_UNUSED, + SCM val SCM_UNUSED) #define FUNC_NAME "import_environment_define" { return SCM_ENVIRONMENT_BINDING_IMMUTABLE; @@ -1677,7 +1672,8 @@ import_environment_define (SCM env, SCM sym, SCM val) static SCM -import_environment_undefine (SCM env, SCM sym) +import_environment_undefine (SCM env SCM_UNUSED, + SCM sym SCM_UNUSED) #define FUNC_NAME "import_environment_undefine" { return SCM_ENVIRONMENT_BINDING_IMMUTABLE; @@ -1740,7 +1736,7 @@ import_environment_cell (SCM env, SCM sym, int for_write) static SCM -mark_import_environment (SCM env) +import_environment_mark (SCM env) { scm_gc_mark (IMPORT_ENVIRONMENT (env)->imports); scm_gc_mark (IMPORT_ENVIRONMENT (env)->import_observers); @@ -1749,24 +1745,24 @@ mark_import_environment (SCM env) } -static scm_sizet -free_import_environment (SCM env) +static void +import_environment_free (SCM env) { core_environments_finalize (env); - - free (IMPORT_ENVIRONMENT (env)); - return sizeof (struct import_environment); + scm_gc_free (IMPORT_ENVIRONMENT (env), sizeof (struct import_environment), + "import environment"); } static int -print_import_environment (SCM type, SCM port, scm_print_state *pstate) +import_environment_print (SCM type, SCM port, + scm_print_state *pstate SCM_UNUSED) { - SCM address = scm_ulong2num (SCM_UNPACK (type)); - SCM base16 = scm_number_to_string (address, SCM_MAKINUM (16)); + SCM address = scm_from_size_t (SCM_UNPACK (type)); + SCM base16 = scm_number_to_string (address, scm_from_int (16)); scm_puts ("#", port); return 1; @@ -1782,9 +1778,9 @@ static struct scm_environment_funcs import_environment_funcs = { import_environment_cell, core_environments_observe, core_environments_unobserve, - mark_import_environment, - free_import_environment, - print_import_environment + import_environment_mark, + import_environment_free, + import_environment_print }; @@ -1792,7 +1788,7 @@ void *scm_type_import_environment = &import_environment_funcs; static void -import_environment_observer (SCM caller, SCM import_env) +import_environment_observer (SCM caller SCM_UNUSED, SCM import_env) { core_environments_broadcast (import_env); } @@ -1800,32 +1796,35 @@ import_environment_observer (SCM caller, SCM import_env) SCM_DEFINE (scm_make_import_environment, "make-import-environment", 2, 0, 0, (SCM imports, SCM conflict_proc), - "Return a new environment imp whose bindings are the union of\n" - "the bindings from the environments in imports; imports must\n" - "be a list of environments. That is, imp binds symbol to\n" - "location when some element of imports does.\n" - "If two different elements of imports have a binding for the\n" - "same symbol, the conflict-proc is called with the following\n" - "parameters: the import environment, the symbol and the list\n" - "of the imported environments that bind the symbol. If the\n" - "conflict-proc returns an environment env, the conflict is\n" - "considered as resolved and the binding from env is used. If\n" - "the conflict-proc returns some non-environment object, the\n" - "conflict is considered unresolved and the symbol is treated\n" - "as unspecified in the import environment.\n" - "The checking for conflicts may be performed lazily, i. e. at\m" + "Return a new environment @var{imp} whose bindings are the union\n" + "of the bindings from the environments in @var{imports};\n" + "@var{imports} must be a list of environments. That is,\n" + "@var{imp} binds a symbol to a location when some element of\n" + "@var{imports} does.\n" + "If two different elements of @var{imports} have a binding for\n" + "the same symbol, the @var{conflict-proc} is called with the\n" + "following parameters: the import environment, the symbol and\n" + "the list of the imported environments that bind the symbol.\n" + "If the @var{conflict-proc} returns an environment @var{env},\n" + "the conflict is considered as resolved and the binding from\n" + "@var{env} is used. If the @var{conflict-proc} returns some\n" + "non-environment object, the conflict is considered unresolved\n" + "and the symbol is treated as unspecified in the import\n" + "environment.\n" + "The checking for conflicts may be performed lazily, i. e. at\n" "the moment when a value or binding for a certain symbol is\n" "requested instead of the moment when the environment is\n" "created or the bindings of the imports change.\n" - "All bindings in imp are immutable. If you apply\n" - "environment-define or environment-undefine to imp, Guile\n" - "will signal an environment:immutable-binding error. However,\n" - "notice that the set of bindings in imp may still change, if\n" - "one of its imported environments changes.") + "All bindings in @var{imp} are immutable. If you apply\n" + "@code{environment-define} or @code{environment-undefine} to\n" + "@var{imp}, Guile will signal an\n" + " @code{environment:immutable-binding} error. However,\n" + "notice that the set of bindings in @var{imp} may still change,\n" + "if one of its imported environments changes.") #define FUNC_NAME s_scm_make_import_environment { - scm_sizet size = sizeof (struct import_environment); - struct import_environment *body = scm_must_malloc (size, FUNC_NAME); + size_t size = sizeof (struct import_environment); + struct import_environment *body = scm_gc_malloc (size, "import environment"); SCM env; core_environments_preinit (&body->base); @@ -1849,17 +1848,19 @@ SCM_DEFINE (scm_make_import_environment, "make-import-environment", 2, 0, 0, SCM_DEFINE (scm_import_environment_p, "import-environment?", 1, 0, 0, (SCM object), - "Return #t if object is an import environment, or #f otherwise.") + "Return @code{#t} if object is an import environment, or\n" + "@code{#f} otherwise.") #define FUNC_NAME s_scm_import_environment_p { - return SCM_BOOL (SCM_IMPORT_ENVIRONMENT_P (object)); + return scm_from_bool (SCM_IMPORT_ENVIRONMENT_P (object)); } #undef FUNC_NAME SCM_DEFINE (scm_import_environment_imports, "import-environment-imports", 1, 0, 0, (SCM env), - "Return the list of environments imported by the import environment env.") + "Return the list of environments imported by the import\n" + "environment @var{env}.") #define FUNC_NAME s_scm_import_environment_imports { SCM_ASSERT (SCM_IMPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); @@ -1871,7 +1872,8 @@ SCM_DEFINE (scm_import_environment_imports, "import-environment-imports", 1, 0, SCM_DEFINE (scm_import_environment_set_imports_x, "import-environment-set-imports!", 2, 0, 0, (SCM env, SCM imports), - "Change env's list of imported environments to imports, and check for conflicts.") + "Change @var{env}'s list of imported environments to\n" + "@var{imports}, and check for conflicts.") #define FUNC_NAME s_scm_import_environment_set_imports_x { struct import_environment *body = IMPORT_ENVIRONMENT (env); @@ -1882,9 +1884,9 @@ SCM_DEFINE (scm_import_environment_set_imports_x, "import-environment-set-import for (l = imports; SCM_CONSP (l); l = SCM_CDR (l)) { SCM obj = SCM_CAR (l); - SCM_ASSERT (SCM_ENVIRONMENT_P (obj), imports, SCM_ARG1, FUNC_NAME); + SCM_ASSERT (SCM_ENVIRONMENT_P (obj), imports, SCM_ARG2, FUNC_NAME); } - SCM_ASSERT (SCM_NULLP (l), imports, SCM_ARG1, FUNC_NAME); + SCM_ASSERT (SCM_NULLP (l), imports, SCM_ARG2, FUNC_NAME); for (l = body->import_observers; !SCM_NULLP (l); l = SCM_CDR (l)) { @@ -1945,7 +1947,7 @@ export_environment_ref (SCM env, SCM sym) struct export_environment *body = EXPORT_ENVIRONMENT (env); SCM entry = scm_assq (sym, body->signature); - if (SCM_FALSEP (entry)) + if (scm_is_false (entry)) return SCM_UNDEFINED; else return SCM_ENVIRONMENT_REF (body->private, sym); @@ -1972,7 +1974,9 @@ export_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM ini static SCM -export_environment_define (SCM env, SCM sym, SCM val) +export_environment_define (SCM env SCM_UNUSED, + SCM sym SCM_UNUSED, + SCM val SCM_UNUSED) #define FUNC_NAME "export_environment_define" { return SCM_ENVIRONMENT_BINDING_IMMUTABLE; @@ -1981,7 +1985,7 @@ export_environment_define (SCM env, SCM sym, SCM val) static SCM -export_environment_undefine (SCM env, SCM sym) +export_environment_undefine (SCM env SCM_UNUSED, SCM sym SCM_UNUSED) #define FUNC_NAME "export_environment_undefine" { return SCM_ENVIRONMENT_BINDING_IMMUTABLE; @@ -1996,13 +2000,13 @@ export_environment_set_x (SCM env, SCM sym, SCM val) struct export_environment *body = EXPORT_ENVIRONMENT (env); SCM entry = scm_assq (sym, body->signature); - if (SCM_FALSEP (entry)) + if (scm_is_false (entry)) { return SCM_UNDEFINED; } else { - if (SCM_EQ_P (SCM_CADR (entry), symbol_mutable_location)) + if (scm_is_eq (SCM_CADR (entry), symbol_mutable_location)) return SCM_ENVIRONMENT_SET (body->private, sym, val); else return SCM_ENVIRONMENT_LOCATION_IMMUTABLE; @@ -2018,13 +2022,13 @@ export_environment_cell (SCM env, SCM sym, int for_write) struct export_environment *body = EXPORT_ENVIRONMENT (env); SCM entry = scm_assq (sym, body->signature); - if (SCM_FALSEP (entry)) + if (scm_is_false (entry)) { return SCM_UNDEFINED; } else { - if (!for_write || SCM_EQ_P (SCM_CADR (entry), symbol_mutable_location)) + if (!for_write || scm_is_eq (SCM_CADR (entry), symbol_mutable_location)) return SCM_ENVIRONMENT_CELL (body->private, sym, for_write); else return SCM_ENVIRONMENT_LOCATION_IMMUTABLE; @@ -2034,7 +2038,7 @@ export_environment_cell (SCM env, SCM sym, int for_write) static SCM -mark_export_environment (SCM env) +export_environment_mark (SCM env) { struct export_environment *body = EXPORT_ENVIRONMENT (env); @@ -2046,24 +2050,24 @@ mark_export_environment (SCM env) } -static scm_sizet -free_export_environment (SCM env) +static void +export_environment_free (SCM env) { core_environments_finalize (env); - - free (EXPORT_ENVIRONMENT (env)); - return sizeof (struct export_environment); + scm_gc_free (EXPORT_ENVIRONMENT (env), sizeof (struct export_environment), + "export environment"); } static int -print_export_environment (SCM type, SCM port, scm_print_state *pstate) +export_environment_print (SCM type, SCM port, + scm_print_state *pstate SCM_UNUSED) { - SCM address = scm_ulong2num (SCM_UNPACK (type)); - SCM base16 = scm_number_to_string (address, SCM_MAKINUM (16)); + SCM address = scm_from_size_t (SCM_UNPACK (type)); + SCM base16 = scm_number_to_string (address, scm_from_int (16)); scm_puts ("#", port); return 1; @@ -2079,9 +2083,9 @@ static struct scm_environment_funcs export_environment_funcs = { export_environment_cell, core_environments_observe, core_environments_unobserve, - mark_export_environment, - free_export_environment, - print_export_environment + export_environment_mark, + export_environment_free, + export_environment_print }; @@ -2089,7 +2093,7 @@ void *scm_type_export_environment = &export_environment_funcs; static void -export_environment_observer (SCM caller, SCM export_env) +export_environment_observer (SCM caller SCM_UNUSED, SCM export_env) { core_environments_broadcast (export_env); } @@ -2097,50 +2101,56 @@ export_environment_observer (SCM caller, SCM export_env) SCM_DEFINE (scm_make_export_environment, "make-export-environment", 2, 0, 0, (SCM private, SCM signature), - "Return a new environment exp containing only those bindings\n" - "in private whose symbols are present in signature. The\n" - "private argument must be an environment.\n\n" - "The environment exp binds symbol to location when env does,\n" - "and symbol is exported by signature.\n\n" - "Signature is a list specifying which of the bindings in\n" - "private should be visible in exp. Each element of signature\n" - "should be a list of the form:\n" + "Return a new environment @var{exp} containing only those\n" + "bindings in private whose symbols are present in\n" + "@var{signature}. The @var{private} argument must be an\n" + "environment.\n\n" + "The environment @var{exp} binds symbol to location when\n" + "@var{env} does, and symbol is exported by @var{signature}.\n\n" + "@var{signature} is a list specifying which of the bindings in\n" + "@var{private} should be visible in @var{exp}. Each element of\n" + "@var{signature} should be a list of the form:\n" " (symbol attribute ...)\n" "where each attribute is one of the following:\n" - "* the symbol mutable-location exp should treat the location\n" - " bound to symbol as mutable. That is, exp will pass calls\n" - " to env-set! or environment-cell directly through to\n" - " private.\n" - "* the symbol immutable-location exp should treat the\n" - " location bound to symbol as immutable. If the program\n" - " applies environment-set! to exp and symbol, or calls\n" - " environment-cell to obtain a writable value cell,\n" - " environment-set! will signal an\n" - " environment:immutable-location error. Note that, even if\n" - " an export environment treats a location as immutable, the\n" + "@table @asis\n" + "@item the symbol @code{mutable-location}\n" + " @var{exp} should treat the\n" + " location bound to symbol as mutable. That is, @var{exp}\n" + " will pass calls to @code{environment-set!} or\n" + " @code{environment-cell} directly through to private.\n" + "@item the symbol @code{immutable-location}\n" + " @var{exp} should treat\n" + " the location bound to symbol as immutable. If the program\n" + " applies @code{environment-set!} to @var{exp} and symbol, or\n" + " calls @code{environment-cell} to obtain a writable value\n" + " cell, @code{environment-set!} will signal an\n" + " @code{environment:immutable-location} error. Note that, even\n" + " if an export environment treats a location as immutable, the\n" " underlying environment may treat it as mutable, so its\n" " value may change.\n" + "@end table\n" "It is an error for an element of signature to specify both\n" - "mutable-location and immutable-location. If neither is\n" - "specified, immutable-location is assumed.\n\n" + "@code{mutable-location} and @code{immutable-location}. If\n" + "neither is specified, @code{immutable-location} is assumed.\n\n" "As a special case, if an element of signature is a lone\n" - "symbol sym, it is equivalent to an element of the form\n" - "(sym).\n\n" - "All bindings in exp are immutable. If you apply\n" - "environment-define or environment-undefine to exp, Guile\n" - "will signal an environment:immutable-binding error. However,\n" - "notice that the set of bindings in exp may still change, if\n" - "the bindings in private change.") + "symbol @var{sym}, it is equivalent to an element of the form\n" + "@code{(sym)}.\n\n" + "All bindings in @var{exp} are immutable. If you apply\n" + "@code{environment-define} or @code{environment-undefine} to\n" + "@var{exp}, Guile will signal an\n" + "@code{environment:immutable-binding} error. However,\n" + "notice that the set of bindings in @var{exp} may still change,\n" + "if the bindings in private change.") #define FUNC_NAME s_scm_make_export_environment { - scm_sizet size; + size_t size; struct export_environment *body; SCM env; SCM_ASSERT (SCM_ENVIRONMENT_P (private), private, SCM_ARG1, FUNC_NAME); size = sizeof (struct export_environment); - body = scm_must_malloc (size, FUNC_NAME); + body = scm_gc_malloc (size, "export environment"); core_environments_preinit (&body->base); body->private = SCM_BOOL_F; @@ -2164,17 +2174,18 @@ SCM_DEFINE (scm_make_export_environment, "make-export-environment", 2, 0, 0, SCM_DEFINE (scm_export_environment_p, "export-environment?", 1, 0, 0, (SCM object), - "Return #t if object is an export environment, or #f otherwise.") + "Return @code{#t} if object is an export environment, or\n" + "@code{#f} otherwise.") #define FUNC_NAME s_scm_export_environment_p { - return SCM_BOOL (SCM_EXPORT_ENVIRONMENT_P (object)); + return scm_from_bool (SCM_EXPORT_ENVIRONMENT_P (object)); } #undef FUNC_NAME SCM_DEFINE (scm_export_environment_private, "export-environment-private", 1, 0, 0, (SCM env), - "Return the private environment of export environment env.") + "Return the private environment of export environment @var{env}.") #define FUNC_NAME s_scm_export_environment_private { SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); @@ -2186,7 +2197,7 @@ SCM_DEFINE (scm_export_environment_private, "export-environment-private", 1, 0, SCM_DEFINE (scm_export_environment_set_private_x, "export-environment-set-private!", 2, 0, 0, (SCM env, SCM private), - "Change the private environment of export environment env.") + "Change the private environment of export environment @var{env}.") #define FUNC_NAME s_scm_export_environment_set_private_x { struct export_environment *body; @@ -2208,7 +2219,7 @@ SCM_DEFINE (scm_export_environment_set_private_x, "export-environment-set-privat SCM_DEFINE (scm_export_environment_signature, "export-environment-signature", 1, 0, 0, (SCM env), - "Return the signature of export environment env.") + "Return the signature of export environment @var{env}.") #define FUNC_NAME s_scm_export_environment_signature { SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); @@ -2228,7 +2239,7 @@ export_environment_parse_signature (SCM signature, const char* caller) { SCM entry = SCM_CAR (l); - if (SCM_SYMBOLP (entry)) + if (scm_is_symbol (entry)) { SCM new_entry = scm_cons2 (entry, symbol_immutable_location, SCM_EOL); result = scm_cons (new_entry, result); @@ -2243,16 +2254,16 @@ export_environment_parse_signature (SCM signature, const char* caller) SCM l2; SCM_ASSERT (SCM_CONSP (entry), entry, SCM_ARGn, caller); - SCM_ASSERT (SCM_SYMBOLP (SCM_CAR (entry)), entry, SCM_ARGn, caller); + SCM_ASSERT (scm_is_symbol (SCM_CAR (entry)), entry, SCM_ARGn, caller); sym = SCM_CAR (entry); for (l2 = SCM_CDR (entry); SCM_CONSP (l2); l2 = SCM_CDR (l2)) { SCM attribute = SCM_CAR (l2); - if (SCM_EQ_P (attribute, symbol_immutable_location)) + if (scm_is_eq (attribute, symbol_immutable_location)) immutable = 1; - else if (SCM_EQ_P (attribute, symbol_mutable_location)) + else if (scm_is_eq (attribute, symbol_mutable_location)) mutable = 1; else SCM_ASSERT (0, entry, SCM_ARGn, caller); @@ -2282,7 +2293,7 @@ export_environment_parse_signature (SCM signature, const char* caller) SCM_DEFINE (scm_export_environment_set_signature_x, "export-environment-set-signature!", 2, 0, 0, (SCM env, SCM signature), - "Change the signature of export environment env.") + "Change the signature of export environment @var{env}.") #define FUNC_NAME s_scm_export_environment_set_signature_x { SCM parsed_sig; @@ -2303,24 +2314,25 @@ scm_environments_prehistory () { /* create environment smob */ scm_tc16_environment = scm_make_smob_type ("environment", 0); - scm_set_smob_mark (scm_tc16_environment, mark_environment); - scm_set_smob_free (scm_tc16_environment, free_environment); - scm_set_smob_print (scm_tc16_environment, print_environment); + scm_set_smob_mark (scm_tc16_environment, environment_mark); + scm_set_smob_free (scm_tc16_environment, environment_free); + scm_set_smob_print (scm_tc16_environment, environment_print); /* create observer smob */ scm_tc16_observer = scm_make_smob_type ("observer", 0); - scm_set_smob_mark (scm_tc16_observer, mark_observer); - scm_set_smob_free (scm_tc16_observer, free_observer); - scm_set_smob_print (scm_tc16_observer, print_observer); + scm_set_smob_mark (scm_tc16_observer, observer_mark); + scm_set_smob_print (scm_tc16_observer, observer_print); + + /* create system environment */ + scm_system_environment = scm_make_leaf_environment (); + scm_permanent_object (scm_system_environment); } void scm_init_environments () { -#ifndef SCM_MAGIC_SNARFER #include "libguile/environments.x" -#endif }