Merge from mvo-vcell-cleanup-1-branch.
[bpt/guile.git] / libguile / symbols.c
CommitLineData
a4c91488 1/* Copyright (C) 1995,1996,1997,1998, 2000, 2001 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
a0599745
MD
47#include "libguile/_scm.h"
48#include "libguile/chars.h"
49#include "libguile/eval.h"
ba393257 50#include "libguile/hash.h"
fb43bf74 51#include "libguile/smob.h"
a0599745
MD
52#include "libguile/variable.h"
53#include "libguile/alist.h"
7e73eaee 54#include "libguile/fluids.h"
a0599745
MD
55#include "libguile/strings.h"
56#include "libguile/vectors.h"
00ffa0e7 57#include "libguile/hashtab.h"
a0599745 58#include "libguile/weaks.h"
eb8db440 59#include "libguile/modules.h"
a0599745
MD
60
61#include "libguile/validate.h"
62#include "libguile/symbols.h"
0f2d19dd 63
95b88819
GH
64#ifdef HAVE_STRING_H
65#include <string.h>
66#endif
67
0f2d19dd
JB
68\f
69
0f979f3f
DH
70static SCM symbols;
71
a4c91488
MD
72#ifdef GUILE_DEBUG
73SCM_DEFINE (scm_sys_symbols, "%symbols", 0, 0, 0,
74 (),
75 "Return the system symbol obarray.")
76#define FUNC_NAME s_scm_sys_symbols
77{
78 return symbols;
79}
80#undef FUNC_NAME
81#endif
82
0f979f3f
DH
83\f
84
28b06554
DH
85static char *
86duplicate_string (const char * src, unsigned long length)
87{
88 char * dst = scm_must_malloc (length + 1, "duplicate_string");
1e1384f0
MD
89 memcpy (dst, src, length);
90 dst[length] = 0;
28b06554
DH
91 return dst;
92}
93
94\f
0f2d19dd
JB
95
96/* {Symbols}
97 */
98
1cc91f1b 99
b52e071b
DH
100SCM
101scm_mem2symbol (const char *name, scm_sizet len)
102{
103 scm_sizet raw_hash = scm_string_hash ((const unsigned char *) name, len);
0f979f3f 104 scm_sizet hash = raw_hash % SCM_VECTOR_LENGTH (symbols);
b52e071b
DH
105
106 {
0f979f3f 107 /* Try to find the symbol in the symbols table */
b52e071b
DH
108
109 SCM l;
110
0f979f3f 111 for (l = SCM_VELTS (symbols) [hash]; !SCM_NULLP (l); l = SCM_CDR (l))
b52e071b
DH
112 {
113 SCM sym = SCM_CAAR (l);
114 if (SCM_SYMBOL_HASH (sym) == raw_hash && SCM_SYMBOL_LENGTH (sym) == len)
115 {
116 char *chrs = SCM_SYMBOL_CHARS (sym);
117 scm_sizet i = len;
118
119 while (i != 0)
120 {
121 --i;
122 if (name[i] != chrs[i])
123 goto next_symbol;
124 }
125
126 return sym;
127 }
128 next_symbol:
8d5a2737 129 ;
b52e071b
DH
130 }
131 }
132
133 {
134 /* The symbol was not found - create it. */
135
136 SCM symbol;
137 SCM cell;
138 SCM slot;
139
140 SCM_NEWCELL2 (symbol);
141 SCM_SET_SYMBOL_CHARS (symbol, duplicate_string (name, len));
142 SCM_SET_SYMBOL_HASH (symbol, raw_hash);
143 SCM_SET_PROP_SLOTS (symbol, scm_cons (SCM_BOOL_F, SCM_EOL));
144 SCM_SET_SYMBOL_LENGTH (symbol, (long) len);
145
146 cell = scm_cons (symbol, SCM_UNDEFINED);
0f979f3f
DH
147 slot = SCM_VELTS (symbols) [hash];
148 SCM_VELTS (symbols) [hash] = scm_cons (cell, slot);
b52e071b
DH
149
150 return symbol;
151 }
152}
153
154
155SCM
156scm_str2symbol (const char *str)
157{
158 return scm_mem2symbol (str, strlen (str));
159}
160
3b3b36dd 161SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
8e93e199 162 (SCM obj),
1e6808ea
MG
163 "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
164 "@code{#f}.")
1bbd0b84 165#define FUNC_NAME s_scm_symbol_p
0f2d19dd 166{
8e93e199 167 return SCM_BOOL (SCM_SYMBOLP (obj));
0f2d19dd 168}
1bbd0b84 169#undef FUNC_NAME
0f2d19dd 170
3b3b36dd 171SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
1bbd0b84 172 (SCM s),
1e6808ea
MG
173 "Return the name of @var{symbol} as a string. If the symbol was\n"
174 "part of an object returned as the value of a literal expression\n"
7a095584 175 "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
1e6808ea
MG
176 "Report on Scheme}) or by a call to the @code{read} procedure,\n"
177 "and its name contains alphabetic characters, then the string\n"
178 "returned will contain characters in the implementation's\n"
179 "preferred standard case---some implementations will prefer\n"
180 "upper case, others lower case. If the symbol was returned by\n"
181 "@code{string->symbol}, the case of characters in the string\n"
182 "returned will be the same as the case in the string that was\n"
183 "passed to @code{string->symbol}. It is an error to apply\n"
184 "mutation procedures like @code{string-set!} to strings returned\n"
185 "by this procedure.\n"
186 "\n"
942e5b91 187 "The following examples assume that the implementation's\n"
1e6808ea
MG
188 "standard case is lower case:\n"
189 "\n"
942e5b91 190 "@lisp\n"
1e6808ea
MG
191 "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
192 "(symbol->string 'Martin) @result{} \"martin\"\n"
5ffe9968 193 "(symbol->string\n"
942e5b91
MG
194 " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
195 "@end lisp")
1bbd0b84 196#define FUNC_NAME s_scm_symbol_to_string
0f2d19dd 197{
28b06554 198 SCM_VALIDATE_SYMBOL (1, s);
9fd38a3d 199 return scm_makfromstr (SCM_SYMBOL_CHARS (s), SCM_SYMBOL_LENGTH (s), 0);
0f2d19dd 200}
1bbd0b84 201#undef FUNC_NAME
0f2d19dd
JB
202
203
3b3b36dd 204SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
1e6808ea
MG
205 (SCM string),
206 "Return the symbol whose name is @var{string}. This procedure\n"
942e5b91
MG
207 "can create symbols with names containing special characters or\n"
208 "letters in the non-standard case, but it is usually a bad idea\n"
1e6808ea
MG
209 "to create such symbols because in some implementations of\n"
210 "Scheme they cannot be read as themselves. See\n"
211 "@code{symbol->string}.\n"
212 "\n"
942e5b91 213 "The following examples assume that the implementation's\n"
1e6808ea
MG
214 "standard case is lower case:\n"
215 "\n"
942e5b91
MG
216 "@lisp\n"
217 "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
218 "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
219 "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
220 "(eq? 'JollyWog\n"
221 " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
222 "(string=? \"K. Harper, M.D.\"\n"
223 " (symbol->string\n"
224 " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
225 "@end lisp")
1bbd0b84 226#define FUNC_NAME s_scm_string_to_symbol
0f2d19dd 227{
1e6808ea
MG
228 SCM_VALIDATE_STRING (1, string);
229 return scm_mem2symbol (SCM_STRING_CHARS (string),
230 SCM_STRING_LENGTH (string));
0f2d19dd 231}
1bbd0b84 232#undef FUNC_NAME
0f2d19dd 233
86d31dfe 234#define MAX_PREFIX_LENGTH 30
0f2d19dd 235
86d31dfe 236static int gensym_counter;
0f2d19dd 237
86d31dfe
MV
238SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
239 (SCM prefix),
240 "Create a new symbol with a name constructed from a prefix and\n"
241 "a counter value. The string @var{prefix} can be specified as\n"
242 "an optional argument. Default prefix is @code{g}. The counter\n"
243 "is increased by 1 at each call. There is no provision for\n"
244 "resetting the counter.")
245#define FUNC_NAME s_scm_gensym
0f2d19dd 246{
86d31dfe
MV
247 char buf[MAX_PREFIX_LENGTH + SCM_INTBUFLEN];
248 char *name = buf;
249 int len;
250 if (SCM_UNBNDP (prefix))
251 {
252 name[0] = 'g';
253 len = 1;
254 }
255 else
256 {
257 SCM_VALIDATE_STRING (1, prefix);
258 len = SCM_STRING_LENGTH (prefix);
259 if (len > MAX_PREFIX_LENGTH)
260 name = SCM_MUST_MALLOC (MAX_PREFIX_LENGTH + SCM_INTBUFLEN);
261 strncpy (name, SCM_STRING_CHARS (prefix), len);
262 }
49bc24fe 263 {
86d31dfe
MV
264 int n_digits = scm_iint2str (gensym_counter++, 10, &name[len]);
265 SCM res = scm_mem2symbol (name, len + n_digits);
266 if (name != buf)
267 scm_must_free (name);
268 return res;
49bc24fe 269 }
0f2d19dd 270}
1bbd0b84 271#undef FUNC_NAME
0f2d19dd 272
86d31dfe
MV
273SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
274 (SCM symbol),
275 "Return a hash value for @var{symbol}.")
276#define FUNC_NAME s_scm_symbol_hash
0f2d19dd 277{
86d31dfe
MV
278 SCM_VALIDATE_SYMBOL (1, symbol);
279 return SCM_MAKINUM (SCM_SYMBOL_HASH (symbol));
0f2d19dd 280}
1bbd0b84 281#undef FUNC_NAME
0f2d19dd 282
3b3b36dd 283SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
1bbd0b84 284 (SCM s),
b380b885 285 "Return the contents of @var{symbol}'s @dfn{function slot}.")
1bbd0b84 286#define FUNC_NAME s_scm_symbol_fref
0f2d19dd 287{
3b3b36dd 288 SCM_VALIDATE_SYMBOL (1,s);
0f2d19dd
JB
289 return SCM_SYMBOL_FUNC (s);
290}
1bbd0b84 291#undef FUNC_NAME
0f2d19dd
JB
292
293
3b3b36dd 294SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
1bbd0b84 295 (SCM s),
b380b885 296 "Return the @dfn{property list} currently associated with @var{symbol}.")
1bbd0b84 297#define FUNC_NAME s_scm_symbol_pref
0f2d19dd 298{
3b3b36dd 299 SCM_VALIDATE_SYMBOL (1,s);
0f2d19dd
JB
300 return SCM_SYMBOL_PROPS (s);
301}
1bbd0b84 302#undef FUNC_NAME
0f2d19dd
JB
303
304
3b3b36dd 305SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
1bbd0b84 306 (SCM s, SCM val),
b380b885 307 "Change the binding of @var{symbol}'s function slot.")
1bbd0b84 308#define FUNC_NAME s_scm_symbol_fset_x
0f2d19dd 309{
3b3b36dd 310 SCM_VALIDATE_SYMBOL (1,s);
cf551a2b 311 SCM_SET_SYMBOL_FUNC (s, val);
0f2d19dd
JB
312 return SCM_UNSPECIFIED;
313}
1bbd0b84 314#undef FUNC_NAME
0f2d19dd
JB
315
316
3b3b36dd 317SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
1bbd0b84 318 (SCM s, SCM val),
b380b885 319 "Change the binding of @var{symbol}'s property slot.")
1bbd0b84 320#define FUNC_NAME s_scm_symbol_pset_x
0f2d19dd 321{
3b3b36dd 322 SCM_VALIDATE_SYMBOL (1,s);
0f2d19dd 323 SCM_DEFER_INTS;
cf551a2b 324 SCM_SET_SYMBOL_PROPS (s, val);
0f2d19dd
JB
325 SCM_ALLOW_INTS;
326 return SCM_UNSPECIFIED;
327}
1bbd0b84 328#undef FUNC_NAME
0f2d19dd 329
0f979f3f
DH
330void
331scm_symbols_prehistory ()
332{
a4c91488 333 symbols = scm_make_weak_key_hash_table (SCM_MAKINUM (1009));
0f979f3f
DH
334 scm_permanent_object (symbols);
335}
336
337
0f2d19dd
JB
338void
339scm_init_symbols ()
0f2d19dd 340{
1ff4df7a 341 gensym_counter = 0;
8dc9439f 342#ifndef SCM_MAGIC_SNARFER
a0599745 343#include "libguile/symbols.x"
8dc9439f 344#endif
86d31dfe
MV
345#if SCM_ENABLE_VCELLS
346 scm_init_symbols_deprecated ();
347#endif
0f2d19dd 348}
89e00824
ML
349
350/*
351 Local Variables:
352 c-file-style: "gnu"
353 End:
354*/