#include "libguile/_scm.h"
#include "libguile/chars.h"
#include "libguile/eval.h"
+#include "libguile/hash.h"
#include "libguile/smob.h"
#include "libguile/variable.h"
#include "libguile/alist.h"
#include "libguile/fluids.h"
#include "libguile/strings.h"
#include "libguile/vectors.h"
+#include "libguile/hashtab.h"
#include "libguile/weaks.h"
#include "libguile/modules.h"
\f
-/* NUM_HASH_BUCKETS is the number of symbol scm_hash table buckets.
- */
-#define NUM_HASH_BUCKETS 137
+static SCM symbols;
\f
*/
-unsigned long
-scm_string_hash (const unsigned char *str, scm_sizet len)
+SCM
+scm_mem2symbol (const char *name, scm_sizet len)
{
- if (len > 5)
- {
- scm_sizet i = 5;
- unsigned long h = 264;
- while (i--)
- h = (h << 8) + ((unsigned) (scm_downcase (str[h % len])));
- return h;
- }
- else
- {
- scm_sizet i = len;
- unsigned long h = 0;
- while (i)
- h = (h << 8) + ((unsigned) (scm_downcase (str[--i])));
- return h;
- }
+ scm_sizet raw_hash = scm_string_hash ((const unsigned char *) name, len);
+ scm_sizet hash = raw_hash % SCM_VECTOR_LENGTH (symbols);
+
+ {
+ /* Try to find the symbol in the symbols table */
+
+ SCM l;
+
+ for (l = SCM_VELTS (symbols) [hash]; !SCM_NULLP (l); l = SCM_CDR (l))
+ {
+ SCM sym = SCM_CAAR (l);
+ if (SCM_SYMBOL_HASH (sym) == raw_hash && SCM_SYMBOL_LENGTH (sym) == len)
+ {
+ char *chrs = SCM_SYMBOL_CHARS (sym);
+ scm_sizet i = len;
+
+ while (i != 0)
+ {
+ --i;
+ if (name[i] != chrs[i])
+ goto next_symbol;
+ }
+
+ return sym;
+ }
+ next_symbol:
+ ;
+ }
+ }
+
+ {
+ /* The symbol was not found - create it. */
+
+ SCM symbol;
+ SCM cell;
+ SCM slot;
+
+ SCM_NEWCELL2 (symbol);
+ SCM_SET_SYMBOL_CHARS (symbol, duplicate_string (name, len));
+ SCM_SET_SYMBOL_HASH (symbol, raw_hash);
+ SCM_SET_PROP_SLOTS (symbol, scm_cons (SCM_BOOL_F, SCM_EOL));
+ SCM_SET_SYMBOL_LENGTH (symbol, (long) len);
+
+ cell = scm_cons (symbol, SCM_UNDEFINED);
+ slot = SCM_VELTS (symbols) [hash];
+ SCM_VELTS (symbols) [hash] = scm_cons (cell, slot);
+
+ return symbol;
+ }
}
-int scm_symhash_dim = NUM_HASH_BUCKETS;
+SCM
+scm_str2symbol (const char *str)
+{
+ return scm_mem2symbol (str, strlen (str));
+}
/* scm_sym2vcell
SCM
scm_sym2vcell (SCM sym, SCM thunk, SCM definep)
+#define FUNC_NAME "scm_sym2vcell"
{
if (SCM_NIMP (thunk))
{
if (SCM_FALSEP (var))
return SCM_BOOL_F;
+ else if (SCM_VARIABLEP (var))
+ return SCM_VARVCELL (var);
else
- {
- if (SCM_IMP(var) || !SCM_VARIABLEP (var))
- scm_wta (sym, "strangely interned symbol? ", "");
- return SCM_VARVCELL (var);
- }
+ SCM_MISC_ERROR ("strangely interned symbol: ~S", SCM_LIST1 (sym));
}
else
{
SCM lsym;
- SCM * lsymp;
- SCM z;
- scm_sizet hash = SCM_SYMBOL_HASH (sym) % scm_symhash_dim;
+ scm_sizet hash;
SCM_DEFER_INTS;
+ hash = SCM_SYMBOL_HASH (sym) % SCM_VECTOR_LENGTH (scm_symhash);
for (lsym = SCM_VELTS (scm_symhash)[hash]; SCM_NIMP (lsym); lsym = SCM_CDR (lsym))
{
- z = SCM_CAR (lsym);
+ SCM z = SCM_CAR (lsym);
if (SCM_EQ_P (SCM_CAR (z), sym))
{
SCM_ALLOW_INTS;
}
}
- for (lsym = *(lsymp = &SCM_VELTS (scm_weak_symhash)[hash]);
- SCM_NIMP (lsym);
- lsym = *(lsymp = SCM_CDRLOC (lsym)))
+ if (!SCM_FALSEP (definep))
{
- z = SCM_CAR (lsym);
- if (SCM_EQ_P (SCM_CAR (z), sym))
- {
- if (SCM_NFALSEP (definep))
- {
- /* Move handle from scm_weak_symhash to scm_symhash. */
- *lsymp = SCM_CDR (lsym);
- SCM_SETCDR (lsym, SCM_VELTS(scm_symhash)[hash]);
- SCM_VELTS(scm_symhash)[hash] = lsym;
- }
- SCM_ALLOW_INTS;
- return z;
- }
+ SCM cell = scm_cons (sym, SCM_UNDEFINED);
+ SCM slot = SCM_VELTS (scm_symhash) [hash];
+
+ SCM_VELTS (scm_symhash) [hash] = scm_cons (cell, slot);
+
+ SCM_ALLOW_INTS;
+ return cell;
+ }
+ else
+ {
+ SCM_ALLOW_INTS;
+ return SCM_BOOL_F;
}
- SCM_ALLOW_INTS;
- return scm_wta (sym, "uninterned symbol? ", "");
}
}
+#undef FUNC_NAME
+
/* scm_sym2ovcell
* looks up the symbol in an arbitrary obarray.
SCM
scm_sym2ovcell (SCM sym, SCM obarray)
+#define FUNC_NAME "scm_sym2ovcell"
{
SCM answer;
answer = scm_sym2ovcell_soft (sym, obarray);
if (!SCM_FALSEP (answer))
return answer;
- scm_wta (sym, "uninterned symbol? ", "");
+ SCM_MISC_ERROR ("uninterned symbol: ~S", SCM_LIST1 (sym));
return SCM_UNSPECIFIED; /* not reached */
}
+#undef FUNC_NAME
+
/* Intern a symbol whose name is the LEN characters at NAME in OBARRAY.
in OBARRAY; instead, just return #f.
If OBARRAY is SCM_BOOL_F, create a symbol listed in no obarray and
- return (SYMBOL . SCM_UNDEFINED).
-
- If OBARRAY is scm_symhash, and that doesn't contain the symbol,
- check scm_weak_symhash instead. */
+ return (SYMBOL . SCM_UNDEFINED). */
SCM
scm_intern_obarray_soft (const char *name,scm_sizet len,SCM obarray,unsigned int softness)
{
- scm_sizet raw_hash = scm_string_hash ((unsigned char *) name, len);
+ SCM symbol = scm_mem2symbol (name, len);
+ scm_sizet raw_hash = SCM_SYMBOL_HASH (symbol);
scm_sizet hash;
SCM lsym;
- SCM_REDEFER_INTS;
-
if (SCM_FALSEP (obarray))
{
- hash = raw_hash % 1019;
- goto uninterned_symbol;
+ if (softness)
+ return SCM_BOOL_F;
+ else
+ return scm_cons (symbol, SCM_UNDEFINED);
}
hash = raw_hash % SCM_VECTOR_LENGTH (obarray);
- retry_new_obarray:
for (lsym = SCM_VELTS (obarray)[hash]; SCM_NIMP (lsym); lsym = SCM_CDR (lsym))
{
- scm_sizet i;
SCM a = SCM_CAR (lsym);
SCM z = SCM_CAR (a);
- unsigned char *tmp = SCM_SYMBOL_UCHARS (z);
- if (SCM_SYMBOL_HASH (z) != raw_hash)
- goto trynext;
- if (SCM_SYMBOL_LENGTH (z) != len)
- goto trynext;
- for (i = len; i--;)
- if (((unsigned char *) name)[i] != tmp[i])
- goto trynext;
- {
- SCM_REALLOW_INTS;
+ if (SCM_EQ_P (z, symbol))
return a;
- }
- trynext:;
- }
-
- if (SCM_EQ_P (obarray, scm_symhash))
- {
- obarray = scm_weak_symhash;
- goto retry_new_obarray;
}
- uninterned_symbol:
if (softness)
{
- SCM_REALLOW_INTS;
return SCM_BOOL_F;
}
-
- SCM_NEWCELL2 (lsym);
- SCM_SET_SYMBOL_CHARS (lsym, duplicate_string (name, len));
- SCM_SET_SYMBOL_HASH (lsym, raw_hash);
- SCM_SET_PROP_SLOTS (lsym, scm_cons (SCM_BOOL_F, SCM_EOL));
- SCM_SET_SYMBOL_LENGTH (lsym, (long) len);
-
- if (SCM_FALSEP (obarray))
- {
- SCM answer;
- SCM_REALLOW_INTS;
- SCM_NEWCELL (answer);
- SCM_DEFER_INTS;
- SCM_SETCAR (answer, lsym);
- SCM_SETCDR (answer, SCM_UNDEFINED);
- SCM_REALLOW_INTS;
- return answer;
- }
else
{
- SCM a;
- SCM b;
-
- SCM_NEWCELL (a);
- SCM_NEWCELL (b);
- SCM_SETCAR (a, lsym);
- SCM_SETCDR (a, SCM_UNDEFINED);
- SCM_SETCAR (b, a);
- SCM_SETCDR (b, SCM_VELTS(obarray)[hash]);
- SCM_VELTS(obarray)[hash] = b;
- SCM_REALLOW_INTS;
- return SCM_CAR (b);
+ SCM cell = scm_cons (symbol, SCM_UNDEFINED);
+ SCM slot = SCM_VELTS (obarray) [hash];
+
+ SCM_VELTS (obarray) [hash] = scm_cons (cell, slot);
+
+ return cell;
}
}
SCM
scm_sysintern0_no_module_lookup (const char *name)
{
+ scm_sizet len = strlen (name);
SCM easy_answer;
SCM_DEFER_INTS;
- easy_answer = scm_intern_obarray_soft (name, strlen (name), scm_symhash, 1);
+ easy_answer = scm_intern_obarray_soft (name, len, scm_symhash, 1);
if (SCM_NIMP (easy_answer))
{
SCM_ALLOW_INTS;
}
else
{
- SCM lsym;
- scm_sizet len = strlen (name);
- scm_sizet raw_hash = scm_string_hash ((unsigned char *) name, len);
- scm_sizet hash = raw_hash % scm_symhash_dim;
-
- SCM_NEWCELL2 (lsym);
- SCM_SET_SYMBOL_CHARS (lsym, name);
- SCM_SET_SYMBOL_HASH (lsym, raw_hash);
- SCM_SET_PROP_SLOTS (lsym, scm_cons (SCM_BOOL_F, SCM_EOL));
- SCM_SET_SYMBOL_LENGTH (lsym, (long) len);
-
- lsym = scm_cons (lsym, SCM_UNDEFINED);
- SCM_VELTS (scm_symhash)[hash] = scm_cons (lsym, SCM_VELTS (scm_symhash)[hash]);
+ SCM symbol = scm_mem2symbol (name, len);
+ scm_sizet raw_hash = SCM_SYMBOL_HASH (symbol);
+ scm_sizet hash = raw_hash % SCM_VECTOR_LENGTH (scm_symhash);
+ SCM cell = scm_cons (symbol, SCM_UNDEFINED);
+ SCM slot = SCM_VELTS (scm_symhash) [hash];
+
+ SCM_VELTS (scm_symhash) [hash] = scm_cons (cell, slot);
SCM_ALLOW_INTS;
- return lsym;
+ return cell;
}
}
if (scm_module_system_booted_p
&& SCM_NIMP (lookup_proc = SCM_TOP_LEVEL_LOOKUP_CLOSURE))
{
- SCM sym = SCM_CAR (scm_intern0 (name));
+ SCM sym = scm_str2symbol (name);
SCM vcell = scm_sym2vcell (sym, lookup_proc, SCM_BOOL_T);
if (SCM_FALSEP (vcell))
scm_misc_error ("sysintern0", "can't define variable", sym);
/* This looks silly - we look up the symbol twice. But it is in
fact necessary given the current module system because the module
lookup closures are written in scheme which needs real symbols. */
- SCM symbol = scm_intern_obarray_soft (name, strlen (name), scm_symhash, 0);
- SCM vcell = scm_sym2vcell (SCM_CAR (symbol),
- SCM_TOP_LEVEL_LOOKUP_CLOSURE,
- SCM_BOOL_F);
+ SCM symbol = scm_str2symbol (name);
+ SCM vcell = scm_sym2vcell (symbol, SCM_TOP_LEVEL_LOOKUP_CLOSURE, SCM_BOOL_F);
if (SCM_FALSEP (vcell))
return SCM_UNDEFINED;
return SCM_CDR (vcell);
}
+
SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
(SCM obj),
- "Returns @t{#t} if @var{obj} is a symbol, otherwise returns @t{#f}. (r5rs)")
+ "Returns @code{#t} if @var{obj} is a symbol, otherwise returns\n"
+ "@code{#f}. (r5rs)")
#define FUNC_NAME s_scm_symbol_p
{
return SCM_BOOL (SCM_SYMBOLP (obj));
SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
(SCM s),
- "Returns the name of @var{symbol} as a string. If the symbol was part of\n"
- "an object returned as the value of a literal expression (section\n"
- "@pxref{Literal expressions,,,r4rs, The Revised^4 Report on Scheme}) or\n"
- "by a call to the @samp{read} procedure, and its name contains alphabetic\n"
- "characters, then the string returned will contain characters in the\n"
- "implementation's preferred standard case---some implementations will\n"
- "prefer upper case, others lower case. If the symbol was returned by\n"
- "@samp{string->symbol}, the case of characters in the string returned\n"
- "will be the same as the case in the string that was passed to\n"
- "@samp{string->symbol}. It is an error to apply mutation procedures like\n"
- "@code{string-set!} to strings returned by this procedure. (r5rs)\n\n"
- "The following examples assume that the implementation's standard case is\n"
- "lower case:\n\n"
- "@format\n"
- "@t{(symbol->string 'flying-fish) \n"
- " ==> \"flying-fish\"\n"
- "(symbol->string 'Martin) ==> \"martin\"\n"
+ "Returns the name of @var{symbol} as a string. If the symbol\n"
+ "was part of an object returned as the value of a literal\n"
+ "expression (section @pxref{Literal expressions,,,r4rs, The\n"
+ "Revised^4 Report on Scheme}) or by a call to the @code{read}\n"
+ "procedure, and its name contains alphabetic characters, then\n"
+ "the string returned will contain characters in the\n"
+ "implementation's preferred standard case---some implementations\n"
+ "will prefer upper case, others lower case. If the symbol was\n"
+ "returned by @code{string->symbol}, the case of characters in\n"
+ "the string returned will be the same as the case in the string\n"
+ "that was passed to @code{string->symbol}. It is an error to\n"
+ "apply mutation procedures like @code{string-set!} to strings\n"
+ "returned by this procedure. (r5rs)\n\n"
+ "The following examples assume that the implementation's\n"
+ "standard case is lower case:\n\n"
+ "@lisp\n"
+ "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
+ "(symbol->string 'Martin) @result{} \"martin\"\n"
"(symbol->string\n"
- " (string->symbol \"Malvina\")) \n"
- " ==> \"Malvina\"\n"
- "}\n"
- "@end format")
+ " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
+ "@end lisp")
#define FUNC_NAME s_scm_symbol_to_string
{
SCM_VALIDATE_SYMBOL (1, s);
SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
(SCM s),
- "Returns the symbol whose name is @var{string}. This procedure can\n"
- "create symbols with names containing special characters or letters in\n"
- "the non-standard case, but it is usually a bad idea to create such\n"
- "symbols because in some implementations of Scheme they cannot be read as\n"
- "themselves. See @samp{symbol->string}.\n\n"
- "The following examples assume that the implementation's standard case is\n"
- "lower case:\n\n"
-"@format\n"
-"@t{(eq? 'mISSISSIppi 'mississippi) \n"
-" ==> #t\n"
-"(string->symbol \"mISSISSIppi\") \n"
-" ==>\n"
-" @r{}the symbol with name \"mISSISSIppi\"\n"
-"(eq? 'bitBlt (string->symbol \"bitBlt\")) \n"
-" ==> #f\n"
-"(eq? 'JollyWog\n"
-" (string->symbol\n"
-" (symbol->string 'JollyWog))) \n"
-" ==> #t\n"
-"(string=? \"K. Harper, M.D.\"\n"
-" (symbol->string\n"
-" (string->symbol \"K. Harper, M.D.\"))) \n"
-" ==> #t\n"
-"}\n"
- "@end format")
+ "Returns the symbol whose name is @var{string}. This procedure\n"
+ "can create symbols with names containing special characters or\n"
+ "letters in the non-standard case, but it is usually a bad idea\n"
+ "to create such because in some implementations of Scheme they\n"
+ "cannot be read as themselves. See @code{symbol->string}.\n\n"
+ "The following examples assume that the implementation's\n"
+ "standard case is lower case:\n\n"
+ "@lisp\n"
+ "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
+ "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
+ "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
+ "(eq? 'JollyWog\n"
+ " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
+ "(string=? \"K. Harper, M.D.\"\n"
+ " (symbol->string\n"
+ " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
+ "@end lisp")
#define FUNC_NAME s_scm_string_to_symbol
{
- SCM vcell;
- SCM answer;
-
- SCM_VALIDATE_STRING (1,s);
- vcell = scm_intern (SCM_STRING_CHARS (s), SCM_STRING_LENGTH (s));
- answer = SCM_CAR (vcell);
- return answer;
+ SCM_VALIDATE_STRING (1, s);
+ return scm_mem2symbol (SCM_STRING_CHARS (s), SCM_STRING_LENGTH (s));
}
#undef FUNC_NAME
o = scm_symhash;
SCM_VALIDATE_VECTOR (1,o);
vcell = scm_sym2ovcell_soft (s, o);
- if (SCM_IMP (vcell) && SCM_EQ_P (o, scm_symhash))
- vcell = scm_sym2ovcell_soft (s, scm_weak_symhash);
return (SCM_NIMP(vcell)
? SCM_BOOL_T
: SCM_BOOL_F);
(SCM o, SCM s),
"Return @var{#t} if @var{obarray} contains a symbol with name\n"
"@var{string} bound to a defined value. This differs from\n"
- "@var{symbol-bound?} in that the mere mention of a symbol usually causes\n"
+ "@var{symbol-interned?} in that the mere mention of a symbol usually causes\n"
"it to be interned; @code{symbol-bound?} determines whether a symbol has\n"
"been given any meaningful value.")
#define FUNC_NAME s_scm_symbol_bound_p
#define FUNC_NAME s_scm_builtin_bindings
{
int length = SCM_VECTOR_LENGTH (scm_symhash);
- SCM obarray = scm_make_vector (SCM_MAKINUM (length), SCM_EOL);
+ SCM obarray = scm_c_make_hash_table (length);
copy_and_prune_obarray (scm_symhash, obarray);
return obarray;
}
#undef FUNC_NAME
-SCM_DEFINE (scm_builtin_weak_bindings, "builtin-weak-bindings", 0, 0, 0,
- (),
- "")
-#define FUNC_NAME s_scm_builtin_weak_bindings
-{
- int length = SCM_VECTOR_LENGTH (scm_weak_symhash);
- SCM obarray = scm_make_doubly_weak_hash_table (SCM_MAKINUM (length));
- copy_and_prune_obarray (scm_weak_symhash, obarray);
- return obarray;
-}
-#undef FUNC_NAME
-
#define MAX_PREFIX_LENGTH 30
static int gensym_counter;
SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
(SCM prefix),
- "Create a new symbol with name constructed from a prefix and a counter value.\n"
- "The string PREFIX can be specified as an optional argument.\n"
- "Default prefix is @code{g}. The counter is increased by 1 at each call.\n"
- "There is no provision for resetting the counter.")
+ "Create a new symbol with a name constructed from a prefix and\n"
+ "a counter value. The string @var{prefix} can be specified as\n"
+ "an optional argument. Default prefix is @code{g}. The counter\n"
+ "is increased by 1 at each call. There is no provision for\n"
+ "resetting the counter.")
#define FUNC_NAME s_scm_gensym
{
char buf[MAX_PREFIX_LENGTH + SCM_INTBUFLEN];
}
{
int n_digits = scm_iint2str (gensym_counter++, 10, &name[len]);
- SCM res = SCM_CAR (scm_intern (name, len + n_digits));
+ SCM res = scm_mem2symbol (name, len + n_digits);
if (name != buf)
scm_must_free (name);
return res;
SCM_DEFINE (scm_gentemp, "gentemp", 0, 2, 0,
(SCM prefix, SCM obarray),
"Create a new symbol with a name unique in an obarray.\n"
- "The name is constructed from an optional string PREFIX and a counter\n"
- "value. The default prefix is @var{t}. The OBARRAY is specified as a\n"
- "second optional argument. Default is the system obarray where all\n"
- "normal symbols are interned. The counter is increased by 1 at each\n"
+ "The name is constructed from an optional string @var{prefix}\n"
+ "and a counter value. The default prefix is @code{t}. The\n"
+ "@var{obarray} is specified as a second optional argument.\n"
+ "Default is the system obarray where all normal symbols are\n"
+ "interned. The counter is increased by 1 at each\n"
"call. There is no provision for resetting the counter.")
#define FUNC_NAME s_scm_gentemp
{
}
#undef FUNC_NAME
+
+void
+scm_symbols_prehistory ()
+{
+ symbols = scm_make_weak_key_hash_table (SCM_MAKINUM (277));
+ scm_permanent_object (symbols);
+}
+
+
void
scm_init_symbols ()
{