1 /* Copyright (C) 1995,1996,1997,1998, 2000, 2001 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
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"
61 #include "libguile/validate.h"
62 #include "libguile/symbols.h"
73 SCM_DEFINE (scm_sys_symbols
, "%symbols", 0, 0, 0,
75 "Return the system symbol obarray.")
76 #define FUNC_NAME s_scm_sys_symbols
90 scm_mem2symbol (const char *name
, scm_sizet len
)
92 scm_sizet raw_hash
= scm_string_hash ((const unsigned char *) name
, len
);
93 scm_sizet hash
= raw_hash
% SCM_VECTOR_LENGTH (symbols
);
96 /* Try to find the symbol in the symbols table */
100 for (l
= SCM_VELTS (symbols
) [hash
]; !SCM_NULLP (l
); l
= SCM_CDR (l
))
102 SCM sym
= SCM_CAR (l
);
103 if (SCM_SYMBOL_HASH (sym
) == raw_hash
104 && SCM_SYMBOL_LENGTH (sym
) == len
)
106 char *chrs
= SCM_SYMBOL_CHARS (sym
);
112 if (name
[i
] != chrs
[i
])
124 /* The symbol was not found - create it. */
129 SCM_NEWCELL2 (symbol
);
130 SCM_SET_SYMBOL_CHARS (symbol
, scm_must_strndup (name
, len
));
131 SCM_SET_SYMBOL_HASH (symbol
, raw_hash
);
132 SCM_SET_PROP_SLOTS (symbol
, scm_cons (SCM_BOOL_F
, SCM_EOL
));
133 SCM_SET_SYMBOL_LENGTH (symbol
, (long) len
);
135 slot
= SCM_VELTS (symbols
) [hash
];
136 SCM_VELTS (symbols
) [hash
] = scm_cons (symbol
, slot
);
144 scm_str2symbol (const char *str
)
146 return scm_mem2symbol (str
, strlen (str
));
149 SCM_DEFINE (scm_symbol_p
, "symbol?", 1, 0, 0,
151 "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
153 #define FUNC_NAME s_scm_symbol_p
155 return SCM_BOOL (SCM_SYMBOLP (obj
));
159 SCM_DEFINE (scm_symbol_to_string
, "symbol->string", 1, 0, 0,
161 "Return the name of @var{symbol} as a string. If the symbol was\n"
162 "part of an object returned as the value of a literal expression\n"
163 "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
164 "Report on Scheme}) or by a call to the @code{read} procedure,\n"
165 "and its name contains alphabetic characters, then the string\n"
166 "returned will contain characters in the implementation's\n"
167 "preferred standard case---some implementations will prefer\n"
168 "upper case, others lower case. If the symbol was returned by\n"
169 "@code{string->symbol}, the case of characters in the string\n"
170 "returned will be the same as the case in the string that was\n"
171 "passed to @code{string->symbol}. It is an error to apply\n"
172 "mutation procedures like @code{string-set!} to strings returned\n"
173 "by this procedure.\n"
175 "The following examples assume that the implementation's\n"
176 "standard case is lower case:\n"
179 "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
180 "(symbol->string 'Martin) @result{} \"martin\"\n"
182 " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
184 #define FUNC_NAME s_scm_symbol_to_string
186 SCM_VALIDATE_SYMBOL (1, s
);
187 return scm_makfromstr (SCM_SYMBOL_CHARS (s
), SCM_SYMBOL_LENGTH (s
), 0);
192 SCM_DEFINE (scm_string_to_symbol
, "string->symbol", 1, 0, 0,
194 "Return the symbol whose name is @var{string}. This procedure\n"
195 "can create symbols with names containing special characters or\n"
196 "letters in the non-standard case, but it is usually a bad idea\n"
197 "to create such symbols because in some implementations of\n"
198 "Scheme they cannot be read as themselves. See\n"
199 "@code{symbol->string}.\n"
201 "The following examples assume that the implementation's\n"
202 "standard case is lower case:\n"
205 "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
206 "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
207 "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
209 " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
210 "(string=? \"K. Harper, M.D.\"\n"
212 " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
214 #define FUNC_NAME s_scm_string_to_symbol
216 SCM_VALIDATE_STRING (1, string
);
217 return scm_mem2symbol (SCM_STRING_CHARS (string
),
218 SCM_STRING_LENGTH (string
));
222 #define MAX_PREFIX_LENGTH 30
224 static int gensym_counter
;
226 SCM_DEFINE (scm_gensym
, "gensym", 0, 1, 0,
228 "Create a new symbol with a name constructed from a prefix and\n"
229 "a counter value. The string @var{prefix} can be specified as\n"
230 "an optional argument. Default prefix is @code{g}. The counter\n"
231 "is increased by 1 at each call. There is no provision for\n"
232 "resetting the counter.")
233 #define FUNC_NAME s_scm_gensym
235 char buf
[MAX_PREFIX_LENGTH
+ SCM_INTBUFLEN
];
238 if (SCM_UNBNDP (prefix
))
245 SCM_VALIDATE_STRING (1, prefix
);
246 len
= SCM_STRING_LENGTH (prefix
);
247 if (len
> MAX_PREFIX_LENGTH
)
248 name
= SCM_MUST_MALLOC (MAX_PREFIX_LENGTH
+ SCM_INTBUFLEN
);
249 strncpy (name
, SCM_STRING_CHARS (prefix
), len
);
252 int n_digits
= scm_iint2str (gensym_counter
++, 10, &name
[len
]);
253 SCM res
= scm_mem2symbol (name
, len
+ n_digits
);
255 scm_must_free (name
);
261 SCM_DEFINE (scm_symbol_hash
, "symbol-hash", 1, 0, 0,
263 "Return a hash value for @var{symbol}.")
264 #define FUNC_NAME s_scm_symbol_hash
266 SCM_VALIDATE_SYMBOL (1, symbol
);
267 return SCM_MAKINUM (SCM_SYMBOL_HASH (symbol
));
271 SCM_DEFINE (scm_symbol_fref
, "symbol-fref", 1, 0, 0,
273 "Return the contents of @var{symbol}'s @dfn{function slot}.")
274 #define FUNC_NAME s_scm_symbol_fref
276 SCM_VALIDATE_SYMBOL (1,s
);
277 return SCM_SYMBOL_FUNC (s
);
282 SCM_DEFINE (scm_symbol_pref
, "symbol-pref", 1, 0, 0,
284 "Return the @dfn{property list} currently associated with @var{symbol}.")
285 #define FUNC_NAME s_scm_symbol_pref
287 SCM_VALIDATE_SYMBOL (1,s
);
288 return SCM_SYMBOL_PROPS (s
);
293 SCM_DEFINE (scm_symbol_fset_x
, "symbol-fset!", 2, 0, 0,
295 "Change the binding of @var{symbol}'s function slot.")
296 #define FUNC_NAME s_scm_symbol_fset_x
298 SCM_VALIDATE_SYMBOL (1,s
);
299 SCM_SET_SYMBOL_FUNC (s
, val
);
300 return SCM_UNSPECIFIED
;
305 SCM_DEFINE (scm_symbol_pset_x
, "symbol-pset!", 2, 0, 0,
307 "Change the binding of @var{symbol}'s property slot.")
308 #define FUNC_NAME s_scm_symbol_pset_x
310 SCM_VALIDATE_SYMBOL (1,s
);
312 SCM_SET_SYMBOL_PROPS (s
, val
);
314 return SCM_UNSPECIFIED
;
319 scm_symbols_prehistory ()
321 symbols
= scm_make_weak_key_hash_table (SCM_MAKINUM (1009));
322 scm_permanent_object (symbols
);
330 #ifndef SCM_MAGIC_SNARFER
331 #include "libguile/symbols.x"
333 #if SCM_ENABLE_VCELLS
334 scm_init_symbols_deprecated ();