Commit | Line | Data |
---|---|---|
9b8d3288 | 1 | /* Copyright (C) 1995,1996,1997 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 | |
15 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
16 | * | |
17 | * As a special exception, the Free Software Foundation gives permission | |
18 | * for additional uses of the text contained in its release of GUILE. | |
19 | * | |
20 | * The exception is that, if you link the GUILE library with other files | |
21 | * to produce an executable, this does not by itself cause the | |
22 | * resulting executable to be covered by the GNU General Public License. | |
23 | * Your use of that executable is in no way restricted on account of | |
24 | * linking the GUILE library code into it. | |
25 | * | |
26 | * This exception does not however invalidate any other reasons why | |
27 | * the executable file might be covered by the GNU General Public License. | |
28 | * | |
29 | * This exception applies only to the code released by the | |
30 | * Free Software Foundation under the name GUILE. If you copy | |
31 | * code from other Free Software Foundation releases into a copy of | |
32 | * GUILE, as the General Public License permits, the exception does | |
33 | * not apply to the code that you add in this way. To avoid misleading | |
34 | * anyone as to the status of such modified files, you must delete | |
35 | * this exception notice from them. | |
36 | * | |
37 | * If you write modifications of your own for GUILE, it is your choice | |
38 | * whether to permit this exception to apply to your modifications. | |
39 | * If you do not wish that, delete this exception notice. | |
40 | */ | |
41 | \f | |
42 | ||
43 | #include <stdio.h> | |
44 | #include "_scm.h" | |
20e6290e JB |
45 | #include "chars.h" |
46 | #include "eval.h" | |
47 | #include "variable.h" | |
48 | #include "alist.h" | |
49 | #include "mbstrings.h" | |
50 | ||
51 | #include "symbols.h" | |
0f2d19dd | 52 | |
95b88819 GH |
53 | #ifdef HAVE_STRING_H |
54 | #include <string.h> | |
55 | #endif | |
56 | ||
0f2d19dd JB |
57 | \f |
58 | ||
59 | ||
60 | /* NUM_HASH_BUCKETS is the number of symbol scm_hash table buckets. | |
61 | */ | |
62 | #define NUM_HASH_BUCKETS 137 | |
63 | ||
64 | \f | |
65 | ||
66 | ||
67 | /* {Symbols} | |
68 | */ | |
69 | ||
1cc91f1b | 70 | |
0f2d19dd JB |
71 | unsigned long |
72 | scm_strhash (str, len, n) | |
73 | unsigned char *str; | |
74 | scm_sizet len; | |
75 | unsigned long n; | |
0f2d19dd JB |
76 | { |
77 | if (len > 5) | |
78 | { | |
79 | scm_sizet i = 5; | |
80 | unsigned long h = 264 % n; | |
81 | while (i--) | |
566011b9 | 82 | h = ((h << 8) + ((unsigned) (scm_downcase (str[h % len])))) % n; |
0f2d19dd JB |
83 | return h; |
84 | } | |
85 | else | |
86 | { | |
87 | scm_sizet i = len; | |
88 | unsigned long h = 0; | |
89 | while (i) | |
566011b9 | 90 | h = ((h << 8) + ((unsigned) (scm_downcase (str[--i])))) % n; |
0f2d19dd JB |
91 | return h; |
92 | } | |
93 | } | |
94 | ||
95 | int scm_symhash_dim = NUM_HASH_BUCKETS; | |
96 | ||
97 | ||
98 | /* scm_sym2vcell | |
99 | * looks up the symbol in the symhash table. | |
100 | */ | |
1cc91f1b | 101 | |
0f2d19dd JB |
102 | SCM |
103 | scm_sym2vcell (sym, thunk, definep) | |
104 | SCM sym; | |
105 | SCM thunk; | |
106 | SCM definep; | |
0f2d19dd JB |
107 | { |
108 | if (SCM_NIMP(thunk)) | |
109 | { | |
110 | SCM var = scm_apply (thunk, sym, scm_cons(definep, scm_listofnull)); | |
111 | ||
112 | if (var == SCM_BOOL_F) | |
113 | return SCM_BOOL_F; | |
114 | else | |
115 | { | |
116 | if (SCM_IMP(var) || !SCM_VARIABLEP (var)) | |
117 | scm_wta (sym, "strangely interned symbol? ", ""); | |
118 | return SCM_VARVCELL (var); | |
119 | } | |
120 | } | |
121 | else | |
122 | { | |
123 | SCM lsym; | |
124 | SCM * lsymp; | |
125 | SCM z; | |
126 | scm_sizet scm_hash = scm_strhash (SCM_UCHARS (sym), (scm_sizet) SCM_LENGTH (sym), | |
127 | (unsigned long) scm_symhash_dim); | |
128 | ||
129 | SCM_DEFER_INTS; | |
130 | for (lsym = SCM_VELTS (scm_symhash)[scm_hash]; SCM_NIMP (lsym); lsym = SCM_CDR (lsym)) | |
131 | { | |
132 | z = SCM_CAR (lsym); | |
133 | if (SCM_CAR (z) == sym) | |
134 | { | |
135 | SCM_ALLOW_INTS; | |
136 | return z; | |
137 | } | |
138 | } | |
139 | ||
140 | for (lsym = *(lsymp = &SCM_VELTS (scm_weak_symhash)[scm_hash]); | |
141 | SCM_NIMP (lsym); | |
25d8012c | 142 | lsym = *(lsymp = SCM_CDRLOC (lsym))) |
0f2d19dd JB |
143 | { |
144 | z = SCM_CAR (lsym); | |
145 | if (SCM_CAR (z) == sym) | |
146 | { | |
5aab5d96 | 147 | if (SCM_NFALSEP (definep)) |
0f2d19dd | 148 | { |
49bc24fe | 149 | /* Move handle from scm_weak_symhash to scm_symhash. */ |
0f2d19dd JB |
150 | *lsymp = SCM_CDR (lsym); |
151 | SCM_SETCDR (lsym, SCM_VELTS(scm_symhash)[scm_hash]); | |
152 | SCM_VELTS(scm_symhash)[scm_hash] = lsym; | |
153 | } | |
154 | SCM_ALLOW_INTS; | |
155 | return z; | |
156 | } | |
157 | } | |
158 | SCM_ALLOW_INTS; | |
159 | return scm_wta (sym, "uninterned symbol? ", ""); | |
160 | } | |
161 | } | |
162 | ||
163 | /* scm_sym2ovcell | |
49bc24fe | 164 | * looks up the symbol in an arbitrary obarray. |
0f2d19dd | 165 | */ |
1cc91f1b | 166 | |
0f2d19dd JB |
167 | SCM |
168 | scm_sym2ovcell_soft (sym, obarray) | |
169 | SCM sym; | |
170 | SCM obarray; | |
0f2d19dd JB |
171 | { |
172 | SCM lsym, z; | |
173 | scm_sizet scm_hash; | |
174 | ||
175 | scm_hash = scm_strhash (SCM_UCHARS (sym), | |
176 | (scm_sizet) SCM_LENGTH (sym), | |
177 | SCM_LENGTH (obarray)); | |
178 | SCM_REDEFER_INTS; | |
179 | for (lsym = SCM_VELTS (obarray)[scm_hash]; | |
180 | SCM_NIMP (lsym); | |
181 | lsym = SCM_CDR (lsym)) | |
182 | { | |
183 | z = SCM_CAR (lsym); | |
184 | if (SCM_CAR (z) == sym) | |
185 | { | |
186 | SCM_REALLOW_INTS; | |
187 | return z; | |
188 | } | |
189 | } | |
190 | SCM_REALLOW_INTS; | |
191 | return SCM_BOOL_F; | |
192 | } | |
193 | ||
1cc91f1b | 194 | |
0f2d19dd JB |
195 | SCM |
196 | scm_sym2ovcell (sym, obarray) | |
197 | SCM sym; | |
198 | SCM obarray; | |
0f2d19dd JB |
199 | { |
200 | SCM answer; | |
201 | answer = scm_sym2ovcell_soft (sym, obarray); | |
202 | if (answer != SCM_BOOL_F) | |
203 | return answer; | |
204 | scm_wta (sym, "uninterned symbol? ", ""); | |
205 | return SCM_UNSPECIFIED; /* not reached */ | |
206 | } | |
207 | ||
8ce94504 JB |
208 | /* Intern a symbol whose name is the LEN characters at NAME in OBARRAY. |
209 | ||
210 | OBARRAY should be a vector of lists, indexed by the name's hash | |
211 | value, modulo OBARRAY's length. Each list has the form | |
212 | ((SYMBOL . VALUE) ...), where SYMBOL is a symbol, and VALUE is the | |
213 | value associated with that symbol (in the current module? in the | |
214 | system module?) | |
215 | ||
216 | To "intern" a symbol means: if OBARRAY already contains a symbol by | |
217 | that name, return its (SYMBOL . VALUE) pair; otherwise, create a | |
218 | new symbol, add the pair (SYMBOL . SCM_UNDEFINED) to the | |
219 | appropriate list of the OBARRAY, and return the pair. | |
220 | ||
221 | If softness is non-zero, don't create a symbol if it isn't already | |
222 | in OBARRAY; instead, just return #f. | |
223 | ||
224 | If OBARRAY is SCM_BOOL_F, create a symbol listed in no obarray and | |
225 | return (SYMBOL . SCM_UNDEFINED). | |
226 | ||
227 | If OBARRAY is scm_symhash, and that doesn't contain the symbol, | |
228 | check scm_weak_symhash instead. */ | |
229 | ||
1cc91f1b | 230 | |
0f2d19dd JB |
231 | SCM |
232 | scm_intern_obarray_soft (name, len, obarray, softness) | |
233 | char *name; | |
234 | scm_sizet len; | |
235 | SCM obarray; | |
236 | int softness; | |
0f2d19dd JB |
237 | { |
238 | SCM lsym; | |
239 | SCM z; | |
240 | register scm_sizet i; | |
241 | register unsigned char *tmp; | |
242 | scm_sizet scm_hash; | |
243 | ||
244 | SCM_REDEFER_INTS; | |
245 | ||
246 | i = len; | |
247 | tmp = (unsigned char *) name; | |
248 | ||
249 | if (obarray == SCM_BOOL_F) | |
250 | { | |
251 | scm_hash = scm_strhash (tmp, i, 1019); | |
252 | goto uninterned_symbol; | |
253 | } | |
254 | ||
255 | scm_hash = scm_strhash (tmp, i, SCM_LENGTH(obarray)); | |
256 | ||
8ce94504 JB |
257 | /* softness == -1 used to mean that it was known that the symbol |
258 | wasn't already in the obarray. I don't think there are any | |
259 | callers that use that case any more, but just in case... | |
260 | -- JimB, Oct 1996 */ | |
0f2d19dd | 261 | if (softness == -1) |
8ce94504 | 262 | abort (); |
0f2d19dd JB |
263 | |
264 | retry_new_obarray: | |
265 | for (lsym = SCM_VELTS (obarray)[scm_hash]; SCM_NIMP (lsym); lsym = SCM_CDR (lsym)) | |
266 | { | |
267 | z = SCM_CAR (lsym); | |
268 | z = SCM_CAR (z); | |
269 | tmp = SCM_UCHARS (z); | |
270 | if (SCM_LENGTH (z) != len) | |
271 | goto trynext; | |
272 | for (i = len; i--;) | |
273 | if (((unsigned char *) name)[i] != tmp[i]) | |
274 | goto trynext; | |
275 | { | |
276 | SCM a; | |
277 | a = SCM_CAR (lsym); | |
278 | SCM_REALLOW_INTS; | |
279 | return a; | |
280 | } | |
281 | trynext:; | |
282 | } | |
283 | ||
284 | if (obarray == scm_symhash) | |
285 | { | |
286 | obarray = scm_weak_symhash; | |
287 | goto retry_new_obarray; | |
288 | } | |
289 | ||
290 | uninterned_symbol: | |
291 | if (softness) | |
292 | { | |
293 | SCM_REALLOW_INTS; | |
294 | return SCM_BOOL_F; | |
295 | } | |
296 | ||
0f2d19dd JB |
297 | lsym = scm_makfromstr (name, len, SCM_SYMBOL_SLOTS); |
298 | ||
299 | SCM_SETLENGTH (lsym, (long) len, scm_tc7_msymbol); | |
08b5b88c | 300 | SCM_SYMBOL_MULTI_BYTE_STRINGP (lsym) = SCM_BOOL_F; |
0f2d19dd | 301 | SCM_SYMBOL_HASH (lsym) = scm_hash; |
3ff63ce6 | 302 | SCM_SYMBOL_PROPS (lsym) = SCM_EOL; |
0f2d19dd JB |
303 | if (obarray == SCM_BOOL_F) |
304 | { | |
305 | SCM answer; | |
306 | SCM_REALLOW_INTS; | |
307 | SCM_NEWCELL (answer); | |
308 | SCM_DEFER_INTS; | |
25d8012c MD |
309 | SCM_SETCAR (answer, lsym); |
310 | SCM_SETCDR (answer, SCM_UNDEFINED); | |
0f2d19dd JB |
311 | SCM_REALLOW_INTS; |
312 | return answer; | |
313 | } | |
314 | else | |
315 | { | |
316 | SCM a; | |
317 | SCM b; | |
318 | ||
319 | SCM_NEWCELL (a); | |
320 | SCM_NEWCELL (b); | |
321 | SCM_SETCAR (a, lsym); | |
322 | SCM_SETCDR (a, SCM_UNDEFINED); | |
323 | SCM_SETCAR (b, a); | |
324 | SCM_SETCDR (b, SCM_VELTS(obarray)[scm_hash]); | |
325 | SCM_VELTS(obarray)[scm_hash] = b; | |
326 | SCM_REALLOW_INTS; | |
327 | return SCM_CAR (b); | |
328 | } | |
329 | } | |
330 | ||
1cc91f1b | 331 | |
0f2d19dd JB |
332 | SCM |
333 | scm_intern_obarray (name, len, obarray) | |
334 | char *name; | |
335 | scm_sizet len; | |
336 | SCM obarray; | |
0f2d19dd JB |
337 | { |
338 | return scm_intern_obarray_soft (name, len, obarray, 0); | |
339 | } | |
340 | ||
341 | ||
0f2d19dd JB |
342 | SCM |
343 | scm_intern (name, len) | |
344 | char *name; | |
345 | scm_sizet len; | |
0f2d19dd JB |
346 | { |
347 | return scm_intern_obarray (name, len, scm_symhash); | |
348 | } | |
349 | ||
1cc91f1b | 350 | |
0f2d19dd JB |
351 | SCM |
352 | scm_intern0 (name) | |
353 | char * name; | |
0f2d19dd JB |
354 | { |
355 | return scm_intern (name, strlen (name)); | |
356 | } | |
357 | ||
358 | ||
5aab5d96 | 359 | /* Intern the symbol named NAME in scm_symhash, NAME is null-terminated. */ |
0f2d19dd | 360 | SCM |
5aab5d96 | 361 | scm_sysintern0_no_module_lookup (name) |
0f2d19dd | 362 | char *name; |
0f2d19dd JB |
363 | { |
364 | SCM easy_answer; | |
365 | SCM_DEFER_INTS; | |
366 | easy_answer = scm_intern_obarray_soft (name, strlen (name), scm_symhash, 1); | |
367 | if (SCM_NIMP (easy_answer)) | |
368 | { | |
0f2d19dd JB |
369 | SCM_ALLOW_INTS; |
370 | return easy_answer; | |
371 | } | |
372 | else | |
373 | { | |
374 | SCM lsym; | |
375 | scm_sizet len = strlen (name); | |
376 | register unsigned char *tmp = (unsigned char *) name; | |
377 | scm_sizet scm_hash = scm_strhash (tmp, len, (unsigned long) scm_symhash_dim); | |
378 | SCM_NEWCELL (lsym); | |
379 | SCM_SETLENGTH (lsym, (long) len, scm_tc7_ssymbol); | |
380 | SCM_SETCHARS (lsym, name); | |
5aab5d96 | 381 | lsym = scm_cons (lsym, SCM_UNDEFINED); |
0f2d19dd JB |
382 | SCM_VELTS (scm_symhash)[scm_hash] = scm_cons (lsym, SCM_VELTS (scm_symhash)[scm_hash]); |
383 | SCM_ALLOW_INTS; | |
384 | return lsym; | |
385 | } | |
386 | } | |
387 | ||
388 | ||
9b8d3288 MV |
389 | /* Is it safe to access SCM_TOP_LEVEL_LOOKUP_CLOSURE_VAR? |
390 | */ | |
391 | int scm_can_use_top_level_lookup_closure_var; | |
392 | ||
393 | /* Intern the symbol named NAME in scm_symhash, and give it the value | |
394 | VAL. NAME is null-terminated. Use the current top_level lookup | |
395 | closure to give NAME its value. | |
396 | */ | |
397 | SCM | |
398 | scm_sysintern (name, val) | |
399 | char *name; | |
400 | SCM val; | |
5aab5d96 MD |
401 | { |
402 | SCM vcell = scm_sysintern0 (name); | |
403 | SCM_SETCDR (vcell, val); | |
404 | return vcell; | |
405 | } | |
406 | ||
407 | SCM | |
408 | scm_sysintern0 (name) | |
409 | char *name; | |
9b8d3288 MV |
410 | { |
411 | SCM lookup_proc; | |
412 | if (scm_can_use_top_level_lookup_closure_var && | |
413 | SCM_NIMP (lookup_proc = SCM_CDR (scm_top_level_lookup_closure_var))) | |
414 | { | |
415 | SCM sym = SCM_CAR (scm_intern0 (name)); | |
416 | SCM vcell = scm_sym2vcell (sym, lookup_proc, SCM_BOOL_T); | |
417 | if (vcell == SCM_BOOL_F) | |
418 | scm_misc_error ("sysintern", "can't define variable", sym); | |
9b8d3288 MV |
419 | return vcell; |
420 | } | |
421 | else | |
5aab5d96 | 422 | return scm_sysintern0_no_module_lookup (name); |
9b8d3288 MV |
423 | } |
424 | ||
0f2d19dd | 425 | SCM_PROC(s_symbol_p, "symbol?", 1, 0, 0, scm_symbol_p); |
1cc91f1b | 426 | |
0f2d19dd JB |
427 | SCM |
428 | scm_symbol_p(x) | |
429 | SCM x; | |
0f2d19dd | 430 | { |
49bc24fe MD |
431 | if SCM_IMP(x) return SCM_BOOL_F; |
432 | return SCM_SYMBOLP(x) ? SCM_BOOL_T : SCM_BOOL_F; | |
0f2d19dd JB |
433 | } |
434 | ||
435 | SCM_PROC(s_symbol_to_string, "symbol->string", 1, 0, 0, scm_symbol_to_string); | |
1cc91f1b | 436 | |
0f2d19dd JB |
437 | SCM |
438 | scm_symbol_to_string(s) | |
439 | SCM s; | |
0f2d19dd | 440 | { |
49bc24fe MD |
441 | SCM_ASSERT(SCM_NIMP(s) && SCM_SYMBOLP(s), s, SCM_ARG1, s_symbol_to_string); |
442 | return scm_makfromstr(SCM_CHARS(s), (scm_sizet)SCM_LENGTH(s), 0); | |
0f2d19dd JB |
443 | } |
444 | ||
445 | ||
446 | SCM_PROC(s_string_to_symbol, "string->symbol", 1, 0, 0, scm_string_to_symbol); | |
1cc91f1b | 447 | |
0f2d19dd JB |
448 | SCM |
449 | scm_string_to_symbol(s) | |
450 | SCM s; | |
0f2d19dd JB |
451 | { |
452 | SCM vcell; | |
453 | SCM answer; | |
454 | ||
455 | SCM_ASSERT(SCM_NIMP(s) && SCM_ROSTRINGP(s), s, SCM_ARG1, s_string_to_symbol); | |
456 | vcell = scm_intern(SCM_ROCHARS(s), (scm_sizet)SCM_LENGTH(s)); | |
457 | answer = SCM_CAR (vcell); | |
458 | if (SCM_TYP7 (answer) == scm_tc7_msymbol) | |
459 | { | |
460 | if (SCM_REGULAR_STRINGP (s)) | |
08b5b88c | 461 | SCM_SYMBOL_MULTI_BYTE_STRINGP (answer) = SCM_BOOL_F; |
0f2d19dd | 462 | else |
08b5b88c | 463 | SCM_SYMBOL_MULTI_BYTE_STRINGP (answer) = SCM_BOOL_T; |
0f2d19dd JB |
464 | } |
465 | return answer; | |
466 | } | |
467 | ||
468 | ||
469 | SCM_PROC(s_string_to_obarray_symbol, "string->obarray-symbol", 2, 1, 0, scm_string_to_obarray_symbol); | |
1cc91f1b | 470 | |
0f2d19dd JB |
471 | SCM |
472 | scm_string_to_obarray_symbol(o, s, softp) | |
473 | SCM o; | |
474 | SCM s; | |
475 | SCM softp; | |
0f2d19dd JB |
476 | { |
477 | SCM vcell; | |
478 | SCM answer; | |
479 | int softness; | |
480 | ||
49bc24fe MD |
481 | SCM_ASSERT(SCM_NIMP(s) && SCM_ROSTRINGP(s), s, SCM_ARG2, |
482 | s_string_to_obarray_symbol); | |
483 | SCM_ASSERT((o == SCM_BOOL_F) | |
484 | || (o == SCM_BOOL_T) | |
485 | || (SCM_NIMP(o) && SCM_VECTORP(o)), | |
486 | o, | |
487 | SCM_ARG1, | |
488 | s_string_to_obarray_symbol); | |
0f2d19dd JB |
489 | |
490 | softness = ((softp != SCM_UNDEFINED) && (softp != SCM_BOOL_F)); | |
491 | /* iron out some screwy calling conventions */ | |
492 | if (o == SCM_BOOL_F) | |
493 | o = scm_symhash; | |
494 | else if (o == SCM_BOOL_T) | |
495 | o = SCM_BOOL_F; | |
496 | ||
49bc24fe MD |
497 | vcell = scm_intern_obarray_soft (SCM_ROCHARS(s), |
498 | (scm_sizet)SCM_ROLENGTH(s), | |
499 | o, | |
500 | softness); | |
0f2d19dd JB |
501 | if (vcell == SCM_BOOL_F) |
502 | return vcell; | |
503 | answer = SCM_CAR (vcell); | |
504 | if (SCM_TYP7 (s) == scm_tc7_msymbol) | |
505 | { | |
506 | if (SCM_REGULAR_STRINGP (s)) | |
08b5b88c | 507 | SCM_SYMBOL_MULTI_BYTE_STRINGP (answer) = SCM_BOOL_F; |
0f2d19dd | 508 | else |
08b5b88c | 509 | SCM_SYMBOL_MULTI_BYTE_STRINGP (answer) = SCM_BOOL_T; |
0f2d19dd JB |
510 | } |
511 | return answer; | |
512 | } | |
513 | ||
514 | SCM_PROC(s_intern_symbol, "intern-symbol", 2, 0, 0, scm_intern_symbol); | |
1cc91f1b | 515 | |
0f2d19dd JB |
516 | SCM |
517 | scm_intern_symbol(o, s) | |
518 | SCM o; | |
519 | SCM s; | |
0f2d19dd | 520 | { |
49bc24fe MD |
521 | scm_sizet hval; |
522 | SCM_ASSERT(SCM_NIMP(s) && SCM_SYMBOLP(s), s, SCM_ARG2, s_intern_symbol); | |
523 | if (o == SCM_BOOL_F) | |
524 | o = scm_symhash; | |
525 | SCM_ASSERT(SCM_NIMP(o) && SCM_VECTORP(o), o, SCM_ARG1, s_intern_symbol); | |
526 | hval = scm_strhash (SCM_UCHARS (s), SCM_LENGTH (s), SCM_LENGTH(o)); | |
527 | /* If the symbol is already interned, simply return. */ | |
528 | SCM_REDEFER_INTS; | |
529 | { | |
530 | SCM lsym; | |
531 | SCM sym; | |
532 | for (lsym = SCM_VELTS (o)[hval]; | |
533 | SCM_NIMP (lsym); | |
534 | lsym = SCM_CDR (lsym)) | |
535 | { | |
536 | sym = SCM_CAR (lsym); | |
537 | if (SCM_CAR (sym) == s) | |
538 | { | |
539 | SCM_REALLOW_INTS; | |
540 | return SCM_UNSPECIFIED; | |
541 | } | |
542 | } | |
543 | SCM_VELTS (o)[hval] = | |
544 | scm_acons (s, SCM_UNDEFINED, SCM_VELTS (o)[hval]); | |
545 | } | |
546 | SCM_REALLOW_INTS; | |
547 | return SCM_UNSPECIFIED; | |
0f2d19dd JB |
548 | } |
549 | ||
550 | SCM_PROC(s_unintern_symbol, "unintern-symbol", 2, 0, 0, scm_unintern_symbol); | |
1cc91f1b | 551 | |
0f2d19dd JB |
552 | SCM |
553 | scm_unintern_symbol(o, s) | |
554 | SCM o; | |
555 | SCM s; | |
0f2d19dd | 556 | { |
49bc24fe MD |
557 | scm_sizet hval; |
558 | SCM_ASSERT(SCM_NIMP(s) && SCM_SYMBOLP(s), s, SCM_ARG2, s_unintern_symbol); | |
559 | if (o == SCM_BOOL_F) | |
560 | o = scm_symhash; | |
561 | SCM_ASSERT(SCM_NIMP(o) && SCM_VECTORP(o), o, SCM_ARG1, s_unintern_symbol); | |
562 | hval = scm_strhash (SCM_UCHARS (s), SCM_LENGTH (s), SCM_LENGTH(o)); | |
563 | SCM_DEFER_INTS; | |
564 | { | |
565 | SCM lsym_follow; | |
566 | SCM lsym; | |
567 | SCM sym; | |
568 | for (lsym = SCM_VELTS (o)[hval], lsym_follow = SCM_BOOL_F; | |
569 | SCM_NIMP (lsym); | |
570 | lsym_follow = lsym, lsym = SCM_CDR (lsym)) | |
571 | { | |
572 | sym = SCM_CAR (lsym); | |
573 | if (SCM_CAR (sym) == s) | |
574 | { | |
575 | /* Found the symbol to unintern. */ | |
576 | if (lsym_follow == SCM_BOOL_F) | |
577 | SCM_VELTS(o)[hval] = lsym; | |
578 | else | |
25d8012c | 579 | SCM_SETCDR (lsym_follow, SCM_CDR(lsym)); |
49bc24fe MD |
580 | SCM_ALLOW_INTS; |
581 | return SCM_BOOL_T; | |
582 | } | |
583 | } | |
584 | } | |
585 | SCM_ALLOW_INTS; | |
586 | return SCM_BOOL_F; | |
0f2d19dd JB |
587 | } |
588 | ||
589 | SCM_PROC(s_symbol_binding, "symbol-binding", 2, 0, 0, scm_symbol_binding); | |
1cc91f1b | 590 | |
0f2d19dd JB |
591 | SCM |
592 | scm_symbol_binding (o, s) | |
593 | SCM o; | |
594 | SCM s; | |
0f2d19dd JB |
595 | { |
596 | SCM vcell; | |
597 | SCM_ASSERT(SCM_NIMP(s) && SCM_SYMBOLP(s), s, SCM_ARG2, s_symbol_binding); | |
598 | if (o == SCM_BOOL_F) | |
599 | o = scm_symhash; | |
600 | SCM_ASSERT(SCM_NIMP(o) && SCM_VECTORP(o), o, SCM_ARG1, s_symbol_binding); | |
601 | vcell = scm_sym2ovcell (s, o); | |
602 | return SCM_CDR(vcell); | |
603 | } | |
604 | ||
605 | ||
606 | SCM_PROC(s_symbol_interned_p, "symbol-interned?", 2, 0, 0, scm_symbol_interned_p); | |
1cc91f1b | 607 | |
0f2d19dd JB |
608 | SCM |
609 | scm_symbol_interned_p (o, s) | |
610 | SCM o; | |
611 | SCM s; | |
0f2d19dd JB |
612 | { |
613 | SCM vcell; | |
614 | SCM_ASSERT(SCM_NIMP(s) && SCM_SYMBOLP(s), s, SCM_ARG2, s_symbol_interned_p); | |
615 | if (o == SCM_BOOL_F) | |
616 | o = scm_symhash; | |
617 | SCM_ASSERT(SCM_NIMP(o) && SCM_VECTORP(o), o, SCM_ARG1, s_symbol_interned_p); | |
618 | vcell = scm_sym2ovcell_soft (s, o); | |
619 | if (SCM_IMP(vcell) && (o == scm_symhash)) | |
620 | vcell = scm_sym2ovcell_soft (s, scm_weak_symhash); | |
621 | return (SCM_NIMP(vcell) | |
622 | ? SCM_BOOL_T | |
623 | : SCM_BOOL_F); | |
624 | } | |
625 | ||
626 | ||
627 | SCM_PROC(s_symbol_bound_p, "symbol-bound?", 2, 0, 0, scm_symbol_bound_p); | |
1cc91f1b | 628 | |
0f2d19dd JB |
629 | SCM |
630 | scm_symbol_bound_p (o, s) | |
631 | SCM o; | |
632 | SCM s; | |
0f2d19dd JB |
633 | { |
634 | SCM vcell; | |
635 | SCM_ASSERT(SCM_NIMP(s) && SCM_SYMBOLP(s), s, SCM_ARG2, s_symbol_bound_p); | |
636 | if (o == SCM_BOOL_F) | |
637 | o = scm_symhash; | |
638 | SCM_ASSERT(SCM_NIMP(o) && SCM_VECTORP(o), o, SCM_ARG1, s_symbol_bound_p); | |
639 | vcell = scm_sym2ovcell_soft (s, o); | |
640 | return (( SCM_NIMP(vcell) | |
641 | && (SCM_CDR(vcell) != SCM_UNDEFINED)) | |
642 | ? SCM_BOOL_T | |
643 | : SCM_BOOL_F); | |
644 | } | |
645 | ||
646 | ||
647 | SCM_PROC(s_symbol_set_x, "symbol-set!", 3, 0, 0, scm_symbol_set_x); | |
1cc91f1b | 648 | |
0f2d19dd JB |
649 | SCM |
650 | scm_symbol_set_x (o, s, v) | |
651 | SCM o; | |
652 | SCM s; | |
653 | SCM v; | |
0f2d19dd JB |
654 | { |
655 | SCM vcell; | |
656 | SCM_ASSERT(SCM_NIMP(s) && SCM_SYMBOLP(s), s, SCM_ARG2, s_symbol_set_x); | |
657 | if (o == SCM_BOOL_F) | |
658 | o = scm_symhash; | |
659 | SCM_ASSERT(SCM_NIMP(o) && SCM_VECTORP(o), o, SCM_ARG1, s_symbol_set_x); | |
660 | vcell = scm_sym2ovcell (s, o); | |
25d8012c | 661 | SCM_SETCDR (vcell, v); |
0f2d19dd JB |
662 | return SCM_UNSPECIFIED; |
663 | } | |
664 | ||
665 | static void | |
666 | msymbolize (s) | |
667 | SCM s; | |
668 | { | |
669 | SCM string; | |
670 | string = scm_makfromstr (SCM_CHARS (s), SCM_LENGTH (s), SCM_SYMBOL_SLOTS); | |
671 | SCM_SETCHARS (s, SCM_CHARS (string)); | |
672 | SCM_SETLENGTH (s, SCM_LENGTH (s), scm_tc7_msymbol); | |
08b5b88c | 673 | SCM_SYMBOL_MULTI_BYTE_STRINGP (s) = SCM_BOOL_F; |
25d8012c MD |
674 | SCM_SETCDR (string, SCM_EOL); |
675 | SCM_SETCAR (string, SCM_EOL); | |
0f2d19dd JB |
676 | } |
677 | ||
678 | ||
679 | SCM_PROC(s_symbol_fref, "symbol-fref", 1, 0, 0, scm_symbol_fref); | |
1cc91f1b | 680 | |
0f2d19dd JB |
681 | SCM |
682 | scm_symbol_fref (s) | |
683 | SCM s; | |
0f2d19dd JB |
684 | { |
685 | SCM_ASSERT(SCM_NIMP(s) && SCM_SYMBOLP(s), s, SCM_ARG1, s_symbol_fref); | |
686 | SCM_DEFER_INTS; | |
687 | if (SCM_TYP7(s) == scm_tc7_ssymbol) | |
688 | msymbolize (s); | |
689 | SCM_ALLOW_INTS; | |
690 | return SCM_SYMBOL_FUNC (s); | |
691 | } | |
692 | ||
693 | ||
694 | SCM_PROC(s_symbol_pref, "symbol-pref", 1, 0, 0, scm_symbol_pref); | |
1cc91f1b | 695 | |
0f2d19dd JB |
696 | SCM |
697 | scm_symbol_pref (s) | |
698 | SCM s; | |
0f2d19dd JB |
699 | { |
700 | SCM_ASSERT(SCM_NIMP(s) && SCM_SYMBOLP(s), s, SCM_ARG1, s_symbol_pref); | |
701 | SCM_DEFER_INTS; | |
702 | if (SCM_TYP7(s) == scm_tc7_ssymbol) | |
703 | msymbolize (s); | |
704 | SCM_ALLOW_INTS; | |
705 | return SCM_SYMBOL_PROPS (s); | |
706 | } | |
707 | ||
708 | ||
709 | SCM_PROC(s_symbol_fset_x, "symbol-fset!", 2, 0, 0, scm_symbol_fset_x); | |
1cc91f1b | 710 | |
0f2d19dd JB |
711 | SCM |
712 | scm_symbol_fset_x (s, val) | |
713 | SCM s; | |
714 | SCM val; | |
0f2d19dd JB |
715 | { |
716 | SCM_ASSERT(SCM_NIMP(s) && SCM_SYMBOLP(s), s, SCM_ARG1, s_symbol_fset_x); | |
717 | SCM_DEFER_INTS; | |
718 | if (SCM_TYP7(s) == scm_tc7_ssymbol) | |
719 | msymbolize (s); | |
720 | SCM_ALLOW_INTS; | |
721 | SCM_SYMBOL_FUNC (s) = val; | |
722 | return SCM_UNSPECIFIED; | |
723 | } | |
724 | ||
725 | ||
726 | SCM_PROC(s_symbol_pset_x, "symbol-pset!", 2, 0, 0, scm_symbol_pset_x); | |
1cc91f1b | 727 | |
0f2d19dd JB |
728 | SCM |
729 | scm_symbol_pset_x (s, val) | |
730 | SCM s; | |
731 | SCM val; | |
0f2d19dd JB |
732 | { |
733 | SCM_ASSERT(SCM_NIMP(s) && SCM_SYMBOLP(s), s, SCM_ARG1, s_symbol_pset_x); | |
734 | SCM_DEFER_INTS; | |
735 | if (SCM_TYP7(s) == scm_tc7_ssymbol) | |
736 | msymbolize (s); | |
737 | SCM_SYMBOL_PROPS (s) = val; | |
738 | SCM_ALLOW_INTS; | |
739 | return SCM_UNSPECIFIED; | |
740 | } | |
741 | ||
742 | ||
743 | SCM_PROC(s_symbol_hash, "symbol-hash", 1, 0, 0, scm_symbol_hash); | |
1cc91f1b | 744 | |
0f2d19dd JB |
745 | SCM |
746 | scm_symbol_hash (s) | |
747 | SCM s; | |
0f2d19dd JB |
748 | { |
749 | SCM_ASSERT(SCM_NIMP(s) && SCM_SYMBOLP(s), s, SCM_ARG1, s_symbol_hash); | |
750 | return SCM_MAKINUM ((unsigned long)s ^ SCM_SYMBOL_HASH (s)); | |
751 | } | |
752 | ||
753 | ||
1cc91f1b | 754 | |
0f2d19dd JB |
755 | void |
756 | scm_init_symbols () | |
0f2d19dd JB |
757 | { |
758 | #include "symbols.x" | |
759 | } | |
760 |