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