Remove #include <stdio.h>. Add #include <string.h>.
[bpt/guile.git] / libguile / symbols.c
1 /* Copyright (C) 1995,1996,1997,1998, 2000, 2001 Free Software Foundation, Inc.
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, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
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.
40 * If you do not wish that, delete this exception notice. */
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
45 \f
46
47 #include "libguile/_scm.h"
48 #include "libguile/chars.h"
49 #include "libguile/eval.h"
50 #include "libguile/hash.h"
51 #include "libguile/smob.h"
52 #include "libguile/variable.h"
53 #include "libguile/alist.h"
54 #include "libguile/fluids.h"
55 #include "libguile/strings.h"
56 #include "libguile/vectors.h"
57 #include "libguile/hashtab.h"
58 #include "libguile/weaks.h"
59 #include "libguile/modules.h"
60
61 #include "libguile/validate.h"
62 #include "libguile/symbols.h"
63
64 #ifdef HAVE_STRING_H
65 #include <string.h>
66 #endif
67
68 \f
69
70 static SCM symbols;
71
72 #ifdef GUILE_DEBUG
73 SCM_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
83 \f
84
85 static char *
86 duplicate_string (const char * src, unsigned long length)
87 {
88 char * dst = scm_must_malloc (length + 1, "duplicate_string");
89 memcpy (dst, src, length);
90 dst[length] = 0;
91 return dst;
92 }
93
94 \f
95
96 /* {Symbols}
97 */
98
99
100 SCM
101 scm_mem2symbol (const char *name, scm_sizet len)
102 {
103 scm_sizet raw_hash = scm_string_hash ((const unsigned char *) name, len);
104 scm_sizet hash = raw_hash % SCM_VECTOR_LENGTH (symbols);
105
106 {
107 /* Try to find the symbol in the symbols table */
108
109 SCM l;
110
111 for (l = SCM_VELTS (symbols) [hash]; !SCM_NULLP (l); l = SCM_CDR (l))
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:
129 ;
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);
147 slot = SCM_VELTS (symbols) [hash];
148 SCM_VELTS (symbols) [hash] = scm_cons (cell, slot);
149
150 return symbol;
151 }
152 }
153
154
155 SCM
156 scm_str2symbol (const char *str)
157 {
158 return scm_mem2symbol (str, strlen (str));
159 }
160
161
162 /* scm_sym2vcell
163 * looks up the symbol in the symhash table.
164 */
165
166 SCM
167 scm_sym2vcell (SCM sym, SCM thunk, SCM definep)
168 #define FUNC_NAME "scm_sym2vcell"
169 {
170 if (SCM_NIMP (thunk))
171 {
172 SCM var;
173
174 if (SCM_EVAL_CLOSURE_P (thunk))
175 /* Bypass evaluator in the standard case. */
176 var = scm_eval_closure_lookup (thunk, sym, definep);
177 else
178 var = scm_apply (thunk, sym, scm_cons (definep, scm_listofnull));
179
180 if (SCM_FALSEP (var))
181 return SCM_BOOL_F;
182 else if (SCM_VARIABLEP (var))
183 return SCM_VARVCELL (var);
184 else
185 SCM_MISC_ERROR ("strangely interned symbol: ~S", SCM_LIST1 (sym));
186 }
187 else
188 {
189 SCM lsym;
190 scm_sizet hash;
191
192 SCM_DEFER_INTS;
193 hash = SCM_SYMBOL_HASH (sym) % SCM_VECTOR_LENGTH (scm_symhash);
194 for (lsym = SCM_VELTS (scm_symhash)[hash]; SCM_NIMP (lsym); lsym = SCM_CDR (lsym))
195 {
196 SCM z = SCM_CAR (lsym);
197 if (SCM_EQ_P (SCM_CAR (z), sym))
198 {
199 SCM_ALLOW_INTS;
200 return z;
201 }
202 }
203
204 if (!SCM_FALSEP (definep))
205 {
206 SCM cell = scm_cons (sym, SCM_UNDEFINED);
207 SCM slot = SCM_VELTS (scm_symhash) [hash];
208
209 SCM_VELTS (scm_symhash) [hash] = scm_cons (cell, slot);
210
211 SCM_ALLOW_INTS;
212 return cell;
213 }
214 else
215 {
216 SCM_ALLOW_INTS;
217 return SCM_BOOL_F;
218 }
219 }
220 }
221 #undef FUNC_NAME
222
223
224 /* scm_sym2ovcell
225 * looks up the symbol in an arbitrary obarray.
226 */
227
228 SCM
229 scm_sym2ovcell_soft (SCM sym, SCM obarray)
230 {
231 SCM lsym, z;
232 scm_sizet hash = SCM_SYMBOL_HASH (sym) % SCM_VECTOR_LENGTH (obarray);
233 SCM_REDEFER_INTS;
234 for (lsym = SCM_VELTS (obarray)[hash];
235 SCM_NIMP (lsym);
236 lsym = SCM_CDR (lsym))
237 {
238 z = SCM_CAR (lsym);
239 if (SCM_EQ_P (SCM_CAR (z), sym))
240 {
241 SCM_REALLOW_INTS;
242 return z;
243 }
244 }
245 SCM_REALLOW_INTS;
246 return SCM_BOOL_F;
247 }
248
249
250 SCM
251 scm_sym2ovcell (SCM sym, SCM obarray)
252 #define FUNC_NAME "scm_sym2ovcell"
253 {
254 SCM answer;
255 answer = scm_sym2ovcell_soft (sym, obarray);
256 if (!SCM_FALSEP (answer))
257 return answer;
258 SCM_MISC_ERROR ("uninterned symbol: ~S", SCM_LIST1 (sym));
259 return SCM_UNSPECIFIED; /* not reached */
260 }
261 #undef FUNC_NAME
262
263
264 /* Intern a symbol whose name is the LEN characters at NAME in OBARRAY.
265
266 OBARRAY should be a vector of lists, indexed by the name's hash
267 value, modulo OBARRAY's length. Each list has the form
268 ((SYMBOL . VALUE) ...), where SYMBOL is a symbol, and VALUE is the
269 value associated with that symbol (in the current module? in the
270 system module?)
271
272 To "intern" a symbol means: if OBARRAY already contains a symbol by
273 that name, return its (SYMBOL . VALUE) pair; otherwise, create a
274 new symbol, add the pair (SYMBOL . SCM_UNDEFINED) to the
275 appropriate list of the OBARRAY, and return the pair.
276
277 If softness is non-zero, don't create a symbol if it isn't already
278 in OBARRAY; instead, just return #f.
279
280 If OBARRAY is SCM_BOOL_F, create a symbol listed in no obarray and
281 return (SYMBOL . SCM_UNDEFINED). */
282
283
284 SCM
285 scm_intern_obarray_soft (const char *name,scm_sizet len,SCM obarray,unsigned int softness)
286 {
287 SCM symbol = scm_mem2symbol (name, len);
288 scm_sizet raw_hash = SCM_SYMBOL_HASH (symbol);
289 scm_sizet hash;
290 SCM lsym;
291
292 if (SCM_FALSEP (obarray))
293 {
294 if (softness)
295 return SCM_BOOL_F;
296 else
297 return scm_cons (symbol, SCM_UNDEFINED);
298 }
299
300 hash = raw_hash % SCM_VECTOR_LENGTH (obarray);
301
302 for (lsym = SCM_VELTS (obarray)[hash]; SCM_NIMP (lsym); lsym = SCM_CDR (lsym))
303 {
304 SCM a = SCM_CAR (lsym);
305 SCM z = SCM_CAR (a);
306 if (SCM_EQ_P (z, symbol))
307 return a;
308 }
309
310 if (softness)
311 {
312 return SCM_BOOL_F;
313 }
314 else
315 {
316 SCM cell = scm_cons (symbol, SCM_UNDEFINED);
317 SCM slot = SCM_VELTS (obarray) [hash];
318
319 SCM_VELTS (obarray) [hash] = scm_cons (cell, slot);
320
321 return cell;
322 }
323 }
324
325
326 SCM
327 scm_intern_obarray (const char *name,scm_sizet len,SCM obarray)
328 {
329 return scm_intern_obarray_soft (name, len, obarray, 0);
330 }
331
332
333 SCM
334 scm_intern (const char *name,scm_sizet len)
335 {
336 return scm_intern_obarray (name, len, scm_symhash);
337 }
338
339
340 SCM
341 scm_intern0 (const char * name)
342 {
343 return scm_intern (name, strlen (name));
344 }
345
346
347 /* Intern the symbol named NAME in scm_symhash, NAME is null-terminated. */
348 SCM
349 scm_sysintern0_no_module_lookup (const char *name)
350 {
351 scm_sizet len = strlen (name);
352 SCM easy_answer;
353 SCM_DEFER_INTS;
354 easy_answer = scm_intern_obarray_soft (name, len, scm_symhash, 1);
355 if (SCM_NIMP (easy_answer))
356 {
357 SCM_ALLOW_INTS;
358 return easy_answer;
359 }
360 else
361 {
362 SCM symbol = scm_mem2symbol (name, len);
363 scm_sizet raw_hash = SCM_SYMBOL_HASH (symbol);
364 scm_sizet hash = raw_hash % SCM_VECTOR_LENGTH (scm_symhash);
365 SCM cell = scm_cons (symbol, SCM_UNDEFINED);
366 SCM slot = SCM_VELTS (scm_symhash) [hash];
367
368 SCM_VELTS (scm_symhash) [hash] = scm_cons (cell, slot);
369 SCM_ALLOW_INTS;
370 return cell;
371 }
372 }
373
374 /* Intern the symbol named NAME in scm_symhash, and give it the value
375 VAL. NAME is null-terminated. Use the current top_level lookup
376 closure to give NAME its value.
377 */
378 SCM
379 scm_sysintern (const char *name, SCM val)
380 {
381 SCM vcell = scm_sysintern0 (name);
382 SCM_SETCDR (vcell, val);
383 return vcell;
384 }
385
386 SCM
387 scm_sysintern0 (const char *name)
388 {
389 SCM lookup_proc;
390 if (scm_module_system_booted_p
391 && SCM_NIMP (lookup_proc = SCM_TOP_LEVEL_LOOKUP_CLOSURE))
392 {
393 SCM sym = scm_str2symbol (name);
394 SCM vcell = scm_sym2vcell (sym, lookup_proc, SCM_BOOL_T);
395 if (SCM_FALSEP (vcell))
396 scm_misc_error ("sysintern0", "can't define variable", sym);
397 return vcell;
398 }
399 else
400 return scm_sysintern0_no_module_lookup (name);
401 }
402
403 /* Lookup the value of the symbol named by the nul-terminated string
404 NAME in the current module. */
405 SCM
406 scm_symbol_value0 (const char *name)
407 {
408 /* This looks silly - we look up the symbol twice. But it is in
409 fact necessary given the current module system because the module
410 lookup closures are written in scheme which needs real symbols. */
411 SCM symbol = scm_str2symbol (name);
412 SCM vcell = scm_sym2vcell (symbol, SCM_TOP_LEVEL_LOOKUP_CLOSURE, SCM_BOOL_F);
413 if (SCM_FALSEP (vcell))
414 return SCM_UNDEFINED;
415 return SCM_CDR (vcell);
416 }
417
418
419 SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
420 (SCM obj),
421 "Returns @code{#t} if @var{obj} is a symbol, otherwise returns\n"
422 "@code{#f}. (r5rs)")
423 #define FUNC_NAME s_scm_symbol_p
424 {
425 return SCM_BOOL (SCM_SYMBOLP (obj));
426 }
427 #undef FUNC_NAME
428
429 SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
430 (SCM s),
431 "Returns the name of @var{symbol} as a string. If the symbol\n"
432 "was part of an object returned as the value of a literal\n"
433 "expression (section @pxref{Literal expressions,,,r4rs, The\n"
434 "Revised^4 Report on Scheme}) or by a call to the @code{read}\n"
435 "procedure, and its name contains alphabetic characters, then\n"
436 "the string returned will contain characters in the\n"
437 "implementation's preferred standard case---some implementations\n"
438 "will prefer upper case, others lower case. If the symbol was\n"
439 "returned by @code{string->symbol}, the case of characters in\n"
440 "the string returned will be the same as the case in the string\n"
441 "that was passed to @code{string->symbol}. It is an error to\n"
442 "apply mutation procedures like @code{string-set!} to strings\n"
443 "returned by this procedure. (r5rs)\n\n"
444 "The following examples assume that the implementation's\n"
445 "standard case is lower case:\n\n"
446 "@lisp\n"
447 "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
448 "(symbol->string 'Martin) @result{} \"martin\"\n"
449 "(symbol->string\n"
450 " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
451 "@end lisp")
452 #define FUNC_NAME s_scm_symbol_to_string
453 {
454 SCM_VALIDATE_SYMBOL (1, s);
455 return scm_makfromstr (SCM_SYMBOL_CHARS (s), SCM_SYMBOL_LENGTH (s), 0);
456 }
457 #undef FUNC_NAME
458
459
460 SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
461 (SCM s),
462 "Returns the symbol whose name is @var{string}. This procedure\n"
463 "can create symbols with names containing special characters or\n"
464 "letters in the non-standard case, but it is usually a bad idea\n"
465 "to create such because in some implementations of Scheme they\n"
466 "cannot be read as themselves. See @code{symbol->string}.\n\n"
467 "The following examples assume that the implementation's\n"
468 "standard case is lower case:\n\n"
469 "@lisp\n"
470 "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
471 "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
472 "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
473 "(eq? 'JollyWog\n"
474 " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
475 "(string=? \"K. Harper, M.D.\"\n"
476 " (symbol->string\n"
477 " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
478 "@end lisp")
479 #define FUNC_NAME s_scm_string_to_symbol
480 {
481 SCM_VALIDATE_STRING (1, s);
482 return scm_mem2symbol (SCM_STRING_CHARS (s), SCM_STRING_LENGTH (s));
483 }
484 #undef FUNC_NAME
485
486
487 SCM_DEFINE (scm_string_to_obarray_symbol, "string->obarray-symbol", 2, 1, 0,
488 (SCM o, SCM s, SCM softp),
489 "Intern a new symbol in @var{obarray}, a symbol table, with name\n"
490 "@var{string}.\n\n"
491 "If @var{obarray} is @code{#f}, use the default system symbol table. If\n"
492 "@var{obarray} is @code{#t}, the symbol should not be interned in any\n"
493 "symbol table; merely return the pair (@var{symbol}\n"
494 ". @var{#<undefined>}).\n\n"
495 "The @var{soft?} argument determines whether new symbol table entries\n"
496 "should be created when the specified symbol is not already present in\n"
497 "@var{obarray}. If @var{soft?} is specified and is a true value, then\n"
498 "new entries should not be added for symbols not already present in the\n"
499 "table; instead, simply return @code{#f}.")
500 #define FUNC_NAME s_scm_string_to_obarray_symbol
501 {
502 SCM vcell;
503 SCM answer;
504 int softness;
505
506 SCM_VALIDATE_STRING (2, s);
507 SCM_ASSERT (SCM_BOOLP (o) || SCM_VECTORP (o), o, SCM_ARG1, FUNC_NAME);
508
509 softness = (!SCM_UNBNDP (softp) && !SCM_FALSEP(softp));
510 /* iron out some screwy calling conventions */
511 if (SCM_FALSEP (o))
512 o = scm_symhash;
513 else if (SCM_EQ_P (o, SCM_BOOL_T))
514 o = SCM_BOOL_F;
515
516 vcell = scm_intern_obarray_soft (SCM_STRING_CHARS(s),
517 SCM_STRING_LENGTH (s),
518 o,
519 softness);
520 if (SCM_FALSEP (vcell))
521 return vcell;
522 answer = SCM_CAR (vcell);
523 return answer;
524 }
525 #undef FUNC_NAME
526
527 SCM_DEFINE (scm_intern_symbol, "intern-symbol", 2, 0, 0,
528 (SCM o, SCM s),
529 "Add a new symbol to @var{obarray} with name @var{string}, bound to an\n"
530 "unspecified initial value. The symbol table is not modified if a symbol\n"
531 "with this name is already present.")
532 #define FUNC_NAME s_scm_intern_symbol
533 {
534 scm_sizet hval;
535 SCM_VALIDATE_SYMBOL (2,s);
536 if (SCM_FALSEP (o))
537 o = scm_symhash;
538 SCM_VALIDATE_VECTOR (1,o);
539 hval = SCM_SYMBOL_HASH (s) % SCM_VECTOR_LENGTH (o);
540 /* If the symbol is already interned, simply return. */
541 SCM_REDEFER_INTS;
542 {
543 SCM lsym;
544 SCM sym;
545 for (lsym = SCM_VELTS (o)[hval];
546 SCM_NIMP (lsym);
547 lsym = SCM_CDR (lsym))
548 {
549 sym = SCM_CAR (lsym);
550 if (SCM_EQ_P (SCM_CAR (sym), s))
551 {
552 SCM_REALLOW_INTS;
553 return SCM_UNSPECIFIED;
554 }
555 }
556 SCM_VELTS (o)[hval] =
557 scm_acons (s, SCM_UNDEFINED, SCM_VELTS (o)[hval]);
558 }
559 SCM_REALLOW_INTS;
560 return SCM_UNSPECIFIED;
561 }
562 #undef FUNC_NAME
563
564 SCM_DEFINE (scm_unintern_symbol, "unintern-symbol", 2, 0, 0,
565 (SCM o, SCM s),
566 "Remove the symbol with name @var{string} from @var{obarray}. This\n"
567 "function returns @code{#t} if the symbol was present and @code{#f}\n"
568 "otherwise.")
569 #define FUNC_NAME s_scm_unintern_symbol
570 {
571 scm_sizet hval;
572 SCM_VALIDATE_SYMBOL (2,s);
573 if (SCM_FALSEP (o))
574 o = scm_symhash;
575 SCM_VALIDATE_VECTOR (1,o);
576 hval = SCM_SYMBOL_HASH (s) % SCM_VECTOR_LENGTH (o);
577 SCM_DEFER_INTS;
578 {
579 SCM lsym_follow;
580 SCM lsym;
581 SCM sym;
582 for (lsym = SCM_VELTS (o)[hval], lsym_follow = SCM_BOOL_F;
583 SCM_NIMP (lsym);
584 lsym_follow = lsym, lsym = SCM_CDR (lsym))
585 {
586 sym = SCM_CAR (lsym);
587 if (SCM_EQ_P (SCM_CAR (sym), s))
588 {
589 /* Found the symbol to unintern. */
590 if (SCM_FALSEP (lsym_follow))
591 SCM_VELTS(o)[hval] = lsym;
592 else
593 SCM_SETCDR (lsym_follow, SCM_CDR(lsym));
594 SCM_ALLOW_INTS;
595 return SCM_BOOL_T;
596 }
597 }
598 }
599 SCM_ALLOW_INTS;
600 return SCM_BOOL_F;
601 }
602 #undef FUNC_NAME
603
604 SCM_DEFINE (scm_symbol_binding, "symbol-binding", 2, 0, 0,
605 (SCM o, SCM s),
606 "Look up in @var{obarray} the symbol whose name is @var{string}, and\n"
607 "return the value to which it is bound. If @var{obarray} is @code{#f},\n"
608 "use the global symbol table. If @var{string} is not interned in\n"
609 "@var{obarray}, an error is signalled.")
610 #define FUNC_NAME s_scm_symbol_binding
611 {
612 SCM vcell;
613 SCM_VALIDATE_SYMBOL (2,s);
614 if (SCM_FALSEP (o))
615 o = scm_symhash;
616 SCM_VALIDATE_VECTOR (1,o);
617 vcell = scm_sym2ovcell (s, o);
618 return SCM_CDR(vcell);
619 }
620 #undef FUNC_NAME
621
622
623 SCM_DEFINE (scm_symbol_interned_p, "symbol-interned?", 2, 0, 0,
624 (SCM o, SCM s),
625 "Return @var{#t} if @var{obarray} contains a symbol with name\n"
626 "@var{string}, and @var{#f} otherwise.")
627 #define FUNC_NAME s_scm_symbol_interned_p
628 {
629 SCM vcell;
630 SCM_VALIDATE_SYMBOL (2,s);
631 if (SCM_FALSEP (o))
632 o = scm_symhash;
633 SCM_VALIDATE_VECTOR (1,o);
634 vcell = scm_sym2ovcell_soft (s, o);
635 return (SCM_NIMP(vcell)
636 ? SCM_BOOL_T
637 : SCM_BOOL_F);
638 }
639 #undef FUNC_NAME
640
641
642 SCM_DEFINE (scm_symbol_bound_p, "symbol-bound?", 2, 0, 0,
643 (SCM o, SCM s),
644 "Return @var{#t} if @var{obarray} contains a symbol with name\n"
645 "@var{string} bound to a defined value. This differs from\n"
646 "@var{symbol-interned?} in that the mere mention of a symbol usually causes\n"
647 "it to be interned; @code{symbol-bound?} determines whether a symbol has\n"
648 "been given any meaningful value.")
649 #define FUNC_NAME s_scm_symbol_bound_p
650 {
651 SCM vcell;
652 SCM_VALIDATE_SYMBOL (2,s);
653 if (SCM_FALSEP (o))
654 o = scm_symhash;
655 SCM_VALIDATE_VECTOR (1,o);
656 vcell = scm_sym2ovcell_soft (s, o);
657 return SCM_BOOL (SCM_NIMP (vcell) && !SCM_UNBNDP (SCM_CDR (vcell)));
658 }
659 #undef FUNC_NAME
660
661
662 SCM_DEFINE (scm_symbol_set_x, "symbol-set!", 3, 0, 0,
663 (SCM o, SCM s, SCM v),
664 "Find the symbol in @var{obarray} whose name is @var{string}, and rebind\n"
665 "it to @var{value}. An error is signalled if @var{string} is not present\n"
666 "in @var{obarray}.")
667 #define FUNC_NAME s_scm_symbol_set_x
668 {
669 SCM vcell;
670 SCM_VALIDATE_SYMBOL (2,s);
671 if (SCM_FALSEP (o))
672 o = scm_symhash;
673 SCM_VALIDATE_VECTOR (1,o);
674 vcell = scm_sym2ovcell (s, o);
675 SCM_SETCDR (vcell, v);
676 return SCM_UNSPECIFIED;
677 }
678 #undef FUNC_NAME
679
680
681 SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
682 (SCM s),
683 "Return the contents of @var{symbol}'s @dfn{function slot}.")
684 #define FUNC_NAME s_scm_symbol_fref
685 {
686 SCM_VALIDATE_SYMBOL (1,s);
687 return SCM_SYMBOL_FUNC (s);
688 }
689 #undef FUNC_NAME
690
691
692 SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
693 (SCM s),
694 "Return the @dfn{property list} currently associated with @var{symbol}.")
695 #define FUNC_NAME s_scm_symbol_pref
696 {
697 SCM_VALIDATE_SYMBOL (1,s);
698 return SCM_SYMBOL_PROPS (s);
699 }
700 #undef FUNC_NAME
701
702
703 SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
704 (SCM s, SCM val),
705 "Change the binding of @var{symbol}'s function slot.")
706 #define FUNC_NAME s_scm_symbol_fset_x
707 {
708 SCM_VALIDATE_SYMBOL (1,s);
709 SCM_SET_SYMBOL_FUNC (s, val);
710 return SCM_UNSPECIFIED;
711 }
712 #undef FUNC_NAME
713
714
715 SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
716 (SCM s, SCM val),
717 "Change the binding of @var{symbol}'s property slot.")
718 #define FUNC_NAME s_scm_symbol_pset_x
719 {
720 SCM_VALIDATE_SYMBOL (1,s);
721 SCM_DEFER_INTS;
722 SCM_SET_SYMBOL_PROPS (s, val);
723 SCM_ALLOW_INTS;
724 return SCM_UNSPECIFIED;
725 }
726 #undef FUNC_NAME
727
728
729 SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
730 (SCM symbol),
731 "Return a hash value for @var{symbol}.")
732 #define FUNC_NAME s_scm_symbol_hash
733 {
734 SCM_VALIDATE_SYMBOL (1, symbol);
735 return SCM_MAKINUM (SCM_SYMBOL_HASH (symbol));
736 }
737 #undef FUNC_NAME
738
739
740 static void
741 copy_and_prune_obarray (SCM from, SCM to)
742 {
743 int i;
744 int length = SCM_VECTOR_LENGTH (from);
745 for (i = 0; i < length; ++i)
746 {
747 SCM head = SCM_VELTS (from)[i]; /* GC protection */
748 SCM ls = head;
749 SCM res = SCM_EOL;
750 SCM *lloc = &res;
751 while (SCM_NIMP (ls))
752 {
753 if (!SCM_UNBNDP (SCM_CDAR (ls)))
754 {
755 *lloc = scm_cons (SCM_CAR (ls), SCM_EOL);
756 lloc = SCM_CDRLOC (*lloc);
757 }
758 ls = SCM_CDR (ls);
759 }
760 SCM_VELTS (to)[i] = res;
761 }
762 }
763
764
765 SCM_DEFINE (scm_builtin_bindings, "builtin-bindings", 0, 0, 0,
766 (),
767 "Create and return a copy of the global symbol table, removing all\n"
768 "unbound symbols.")
769 #define FUNC_NAME s_scm_builtin_bindings
770 {
771 int length = SCM_VECTOR_LENGTH (scm_symhash);
772 SCM obarray = scm_c_make_hash_table (length);
773 copy_and_prune_obarray (scm_symhash, obarray);
774 return obarray;
775 }
776 #undef FUNC_NAME
777
778
779 #define MAX_PREFIX_LENGTH 30
780
781 static int gensym_counter;
782
783 SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
784 (SCM prefix),
785 "Create a new symbol with a name constructed from a prefix and\n"
786 "a counter value. The string @var{prefix} can be specified as\n"
787 "an optional argument. Default prefix is @code{g}. The counter\n"
788 "is increased by 1 at each call. There is no provision for\n"
789 "resetting the counter.")
790 #define FUNC_NAME s_scm_gensym
791 {
792 char buf[MAX_PREFIX_LENGTH + SCM_INTBUFLEN];
793 char *name = buf;
794 int len;
795 if (SCM_UNBNDP (prefix))
796 {
797 name[0] = 'g';
798 len = 1;
799 }
800 else
801 {
802 SCM_VALIDATE_STRING (1, prefix);
803 len = SCM_STRING_LENGTH (prefix);
804 if (len > MAX_PREFIX_LENGTH)
805 name = SCM_MUST_MALLOC (MAX_PREFIX_LENGTH + SCM_INTBUFLEN);
806 strncpy (name, SCM_STRING_CHARS (prefix), len);
807 }
808 {
809 int n_digits = scm_iint2str (gensym_counter++, 10, &name[len]);
810 SCM res = scm_mem2symbol (name, len + n_digits);
811 if (name != buf)
812 scm_must_free (name);
813 return res;
814 }
815 }
816 #undef FUNC_NAME
817
818 static int gentemp_counter;
819
820 SCM_DEFINE (scm_gentemp, "gentemp", 0, 2, 0,
821 (SCM prefix, SCM obarray),
822 "Create a new symbol with a name unique in an obarray.\n"
823 "The name is constructed from an optional string @var{prefix}\n"
824 "and a counter value. The default prefix is @code{t}. The\n"
825 "@var{obarray} is specified as a second optional argument.\n"
826 "Default is the system obarray where all normal symbols are\n"
827 "interned. The counter is increased by 1 at each\n"
828 "call. There is no provision for resetting the counter.")
829 #define FUNC_NAME s_scm_gentemp
830 {
831 char buf[MAX_PREFIX_LENGTH + SCM_INTBUFLEN];
832 char *name = buf;
833 int len, n_digits;
834 if (SCM_UNBNDP (prefix))
835 {
836 name[0] = 't';
837 len = 1;
838 }
839 else
840 {
841 SCM_VALIDATE_STRING (1, prefix);
842 len = SCM_STRING_LENGTH (prefix);
843 if (len > MAX_PREFIX_LENGTH)
844 name = SCM_MUST_MALLOC (MAX_PREFIX_LENGTH + SCM_INTBUFLEN);
845 strncpy (name, SCM_STRING_CHARS (prefix), len);
846 }
847
848 if (SCM_UNBNDP (obarray))
849 obarray = scm_symhash;
850 else
851 SCM_ASSERT ((SCM_VECTORP (obarray) || SCM_WVECTP (obarray)),
852 obarray,
853 SCM_ARG2,
854 FUNC_NAME);
855 do
856 n_digits = scm_iint2str (gentemp_counter++, 10, &name[len]);
857 while (!SCM_FALSEP (scm_intern_obarray_soft (name,
858 len + n_digits,
859 obarray,
860 1)));
861 {
862 SCM vcell = scm_intern_obarray_soft (name,
863 len + n_digits,
864 obarray,
865 0);
866 if (name != buf)
867 scm_must_free (name);
868 return SCM_CAR (vcell);
869 }
870 }
871 #undef FUNC_NAME
872
873
874 void
875 scm_symbols_prehistory ()
876 {
877 symbols = scm_make_weak_key_hash_table (SCM_MAKINUM (1009));
878 scm_permanent_object (symbols);
879 }
880
881
882 void
883 scm_init_symbols ()
884 {
885 gensym_counter = 0;
886 gentemp_counter = 0;
887 #ifndef SCM_MAGIC_SNARFER
888 #include "libguile/symbols.x"
889 #endif
890 }
891
892 /*
893 Local Variables:
894 c-file-style: "gnu"
895 End:
896 */