375ce8cf7761e2bc43cf3193f7d70e66519144ab
[bpt/guile.git] / libguile / symbols.c
1 /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 */
17
18
19 \f
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "libguile/_scm.h"
25 #include "libguile/chars.h"
26 #include "libguile/eval.h"
27 #include "libguile/hash.h"
28 #include "libguile/smob.h"
29 #include "libguile/variable.h"
30 #include "libguile/alist.h"
31 #include "libguile/fluids.h"
32 #include "libguile/strings.h"
33 #include "libguile/vectors.h"
34 #include "libguile/hashtab.h"
35 #include "libguile/weaks.h"
36 #include "libguile/modules.h"
37 #include "libguile/read.h"
38 #include "libguile/srfi-13.h"
39
40 #include "libguile/validate.h"
41 #include "libguile/symbols.h"
42
43 #ifdef HAVE_STRING_H
44 #include <string.h>
45 #endif
46
47 \f
48
49 static SCM symbols;
50
51 #ifdef GUILE_DEBUG
52 SCM_DEFINE (scm_sys_symbols, "%symbols", 0, 0, 0,
53 (),
54 "Return the system symbol obarray.")
55 #define FUNC_NAME s_scm_sys_symbols
56 {
57 return symbols;
58 }
59 #undef FUNC_NAME
60 #endif
61
62 \f
63
64 /* {Symbols}
65 */
66
67 /* In order to optimize reading speed, this function breaks part of
68 * the hashtable abstraction. The optimizations are:
69 *
70 * 1. The argument string can be compared directly to symbol objects
71 * without first creating an SCM string object. (This would have
72 * been necessary if we had used the hashtable API in hashtab.h.)
73 *
74 * 2. We can use the raw hash value stored in scm_i_symbol_hash (sym)
75 * to speed up lookup.
76 *
77 * Both optimizations might be possible without breaking the
78 * abstraction if the API in hashtab.c is improved.
79 */
80
81 unsigned long
82 scm_i_hash_symbol (SCM obj, unsigned long n, void *closure)
83 {
84 return scm_i_symbol_hash (obj) % n;
85 }
86
87 static SCM
88 scm_i_mem2symbol (SCM str)
89 {
90 const char *name = scm_i_string_chars (str);
91 size_t len = scm_i_string_length (str);
92
93 size_t raw_hash = scm_string_hash ((const unsigned char *) name, len) / 2;
94 size_t hash = raw_hash % SCM_HASHTABLE_N_BUCKETS (symbols);
95
96 {
97 /* Try to find the symbol in the symbols table */
98
99 SCM l;
100
101 for (l = SCM_HASHTABLE_BUCKETS (symbols) [hash];
102 !SCM_NULLP (l);
103 l = SCM_CDR (l))
104 {
105 SCM sym = SCM_CAAR (l);
106 if (scm_i_symbol_hash (sym) == raw_hash
107 && scm_i_symbol_length (sym) == len)
108 {
109 const char *chrs = scm_i_symbol_chars (sym);
110 size_t i = len;
111
112 while (i != 0)
113 {
114 --i;
115 if (name[i] != chrs[i])
116 goto next_symbol;
117 }
118
119 return sym;
120 }
121 next_symbol:
122 ;
123 }
124 }
125
126 {
127 /* The symbol was not found - create it. */
128 SCM symbol = scm_i_make_symbol (str, raw_hash,
129 scm_cons (SCM_BOOL_F, SCM_EOL));
130
131 SCM slot = SCM_HASHTABLE_BUCKETS (symbols) [hash];
132 SCM cell = scm_cons (symbol, SCM_UNDEFINED);
133 SCM_SET_HASHTABLE_BUCKET (symbols, hash, scm_cons (cell, slot));
134 SCM_HASHTABLE_INCREMENT (symbols);
135 if (SCM_HASHTABLE_N_ITEMS (symbols) > SCM_HASHTABLE_UPPER (symbols))
136 scm_i_rehash (symbols, scm_i_hash_symbol, 0, "scm_mem2symbol");
137
138 return symbol;
139 }
140 }
141
142 static SCM
143 scm_i_mem2uninterned_symbol (SCM str)
144 {
145 const char *name = scm_i_string_chars (str);
146 size_t len = scm_i_string_length (str);
147
148 size_t raw_hash = (scm_string_hash ((const unsigned char *) name, len)/2
149 + SCM_T_BITS_MAX/2 + 1);
150
151 return scm_i_make_symbol (str, raw_hash,
152 scm_cons (SCM_BOOL_F, SCM_EOL));
153 }
154
155 SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
156 (SCM obj),
157 "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
158 "@code{#f}.")
159 #define FUNC_NAME s_scm_symbol_p
160 {
161 return scm_from_bool (scm_is_symbol (obj));
162 }
163 #undef FUNC_NAME
164
165 SCM_DEFINE (scm_symbol_interned_p, "symbol-interned?", 1, 0, 0,
166 (SCM symbol),
167 "Return @code{#t} if @var{symbol} is interned, otherwise return\n"
168 "@code{#f}.")
169 #define FUNC_NAME s_scm_symbol_interned_p
170 {
171 SCM_VALIDATE_SYMBOL (1, symbol);
172 return scm_from_bool (scm_i_symbol_is_interned (symbol));
173 }
174 #undef FUNC_NAME
175
176 SCM_DEFINE (scm_make_symbol, "make-symbol", 1, 0, 0,
177 (SCM name),
178 "Return a new uninterned symbol with the name @var{name}. "
179 "The returned symbol is guaranteed to be unique and future "
180 "calls to @code{string->symbol} will not return it.")
181 #define FUNC_NAME s_scm_make_symbol
182 {
183 SCM_VALIDATE_STRING (1, name);
184 return scm_i_mem2uninterned_symbol (name);
185 }
186 #undef FUNC_NAME
187
188 SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
189 (SCM s),
190 "Return the name of @var{symbol} as a string. If the symbol was\n"
191 "part of an object returned as the value of a literal expression\n"
192 "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
193 "Report on Scheme}) or by a call to the @code{read} procedure,\n"
194 "and its name contains alphabetic characters, then the string\n"
195 "returned will contain characters in the implementation's\n"
196 "preferred standard case---some implementations will prefer\n"
197 "upper case, others lower case. If the symbol was returned by\n"
198 "@code{string->symbol}, the case of characters in the string\n"
199 "returned will be the same as the case in the string that was\n"
200 "passed to @code{string->symbol}. It is an error to apply\n"
201 "mutation procedures like @code{string-set!} to strings returned\n"
202 "by this procedure.\n"
203 "\n"
204 "The following examples assume that the implementation's\n"
205 "standard case is lower case:\n"
206 "\n"
207 "@lisp\n"
208 "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
209 "(symbol->string 'Martin) @result{} \"martin\"\n"
210 "(symbol->string\n"
211 " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
212 "@end lisp")
213 #define FUNC_NAME s_scm_symbol_to_string
214 {
215 SCM_VALIDATE_SYMBOL (1, s);
216 return scm_i_symbol_substring (s, 0, scm_i_symbol_length (s));
217 }
218 #undef FUNC_NAME
219
220
221 SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
222 (SCM string),
223 "Return the symbol whose name is @var{string}. This procedure\n"
224 "can create symbols with names containing special characters or\n"
225 "letters in the non-standard case, but it is usually a bad idea\n"
226 "to create such symbols because in some implementations of\n"
227 "Scheme they cannot be read as themselves. See\n"
228 "@code{symbol->string}.\n"
229 "\n"
230 "The following examples assume that the implementation's\n"
231 "standard case is lower case:\n"
232 "\n"
233 "@lisp\n"
234 "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
235 "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
236 "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
237 "(eq? 'JollyWog\n"
238 " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
239 "(string=? \"K. Harper, M.D.\"\n"
240 " (symbol->string\n"
241 " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
242 "@end lisp")
243 #define FUNC_NAME s_scm_string_to_symbol
244 {
245 SCM_VALIDATE_STRING (1, string);
246 return scm_i_mem2symbol (string);
247 }
248 #undef FUNC_NAME
249
250 SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0,
251 (SCM str),
252 "Return the symbol whose name is @var{str}. @var{str} is\n"
253 "converted to lowercase before the conversion is done, if Guile\n"
254 "is currently reading symbols case-insensitively.")
255 #define FUNC_NAME s_scm_string_ci_to_symbol
256 {
257 return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
258 ? scm_string_downcase(str)
259 : str);
260 }
261 #undef FUNC_NAME
262
263 #define MAX_PREFIX_LENGTH 30
264
265 SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
266 (SCM prefix),
267 "Create a new symbol with a name constructed from a prefix and\n"
268 "a counter value. The string @var{prefix} can be specified as\n"
269 "an optional argument. Default prefix is @code{ g}. The counter\n"
270 "is increased by 1 at each call. There is no provision for\n"
271 "resetting the counter.")
272 #define FUNC_NAME s_scm_gensym
273 {
274 static int gensym_counter = 0;
275
276 SCM suffix, name;
277 int n, n_digits;
278 char buf[SCM_INTBUFLEN];
279
280 if (SCM_UNBNDP (prefix))
281 prefix = scm_from_locale_string (" g");
282
283 /* mutex in case another thread looks and incs at the exact same moment */
284 scm_mutex_lock (&scm_i_misc_mutex);
285 n = gensym_counter++;
286 scm_mutex_unlock (&scm_i_misc_mutex);
287
288 n_digits = scm_iint2str (n, 10, buf);
289 suffix = scm_from_locale_stringn (buf, n_digits);
290 name = scm_string_append (scm_list_2 (prefix, suffix));
291 return scm_string_to_symbol (name);
292 }
293 #undef FUNC_NAME
294
295 SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
296 (SCM symbol),
297 "Return a hash value for @var{symbol}.")
298 #define FUNC_NAME s_scm_symbol_hash
299 {
300 SCM_VALIDATE_SYMBOL (1, symbol);
301 return scm_from_ulong (scm_i_symbol_hash (symbol));
302 }
303 #undef FUNC_NAME
304
305 SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
306 (SCM s),
307 "Return the contents of @var{symbol}'s @dfn{function slot}.")
308 #define FUNC_NAME s_scm_symbol_fref
309 {
310 SCM_VALIDATE_SYMBOL (1, s);
311 return SCM_CAR (SCM_CELL_OBJECT_3 (s));
312 }
313 #undef FUNC_NAME
314
315
316 SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
317 (SCM s),
318 "Return the @dfn{property list} currently associated with @var{symbol}.")
319 #define FUNC_NAME s_scm_symbol_pref
320 {
321 SCM_VALIDATE_SYMBOL (1, s);
322 return SCM_CDR (SCM_CELL_OBJECT_3 (s));
323 }
324 #undef FUNC_NAME
325
326
327 SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
328 (SCM s, SCM val),
329 "Change the binding of @var{symbol}'s function slot.")
330 #define FUNC_NAME s_scm_symbol_fset_x
331 {
332 SCM_VALIDATE_SYMBOL (1, s);
333 SCM_SETCAR (SCM_CELL_OBJECT_3 (s), val);
334 return SCM_UNSPECIFIED;
335 }
336 #undef FUNC_NAME
337
338
339 SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
340 (SCM s, SCM val),
341 "Change the binding of @var{symbol}'s property slot.")
342 #define FUNC_NAME s_scm_symbol_pset_x
343 {
344 SCM_VALIDATE_SYMBOL (1, s);
345 SCM_SETCDR (SCM_CELL_OBJECT_3 (s), val);
346 return SCM_UNSPECIFIED;
347 }
348 #undef FUNC_NAME
349
350 SCM
351 scm_from_locale_symbol (const char *sym)
352 {
353 return scm_string_to_symbol (scm_from_locale_string (sym));
354 }
355
356 SCM
357 scm_from_locale_symboln (const char *sym, size_t len)
358 {
359 return scm_string_to_symbol (scm_from_locale_stringn (sym, len));
360 }
361
362 void
363 scm_symbols_prehistory ()
364 {
365 symbols = scm_make_weak_key_hash_table (scm_from_int (2139));
366 scm_permanent_object (symbols);
367 }
368
369
370 void
371 scm_init_symbols ()
372 {
373 #include "libguile/symbols.x"
374 }
375
376 /*
377 Local Variables:
378 c-file-style: "gnu"
379 End:
380 */