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. */
45 #include "libguile/_scm.h"
46 #include "libguile/chars.h"
47 #include "libguile/eval.h"
48 #include "libguile/hash.h"
49 #include "libguile/smob.h"
50 #include "libguile/variable.h"
51 #include "libguile/alist.h"
52 #include "libguile/fluids.h"
53 #include "libguile/strings.h"
54 #include "libguile/vectors.h"
55 #include "libguile/hashtab.h"
56 #include "libguile/weaks.h"
57 #include "libguile/modules.h"
59 #include "libguile/validate.h"
60 #include "libguile/symbols.h"
71 SCM_DEFINE (scm_sys_symbols
, "%symbols", 0, 0, 0,
73 "Return the system symbol obarray.")
74 #define FUNC_NAME s_scm_sys_symbols
88 scm_mem2symbol (const char *name
, size_t len
)
90 size_t raw_hash
= scm_string_hash ((const unsigned char *) name
, len
);
91 size_t hash
= raw_hash
% SCM_VECTOR_LENGTH (symbols
);
94 /* Try to find the symbol in the symbols table */
98 for (l
= SCM_VELTS (symbols
) [hash
]; !SCM_NULLP (l
); l
= SCM_CDR (l
))
100 SCM sym
= SCM_CAAR (l
);
101 if (SCM_SYMBOL_HASH (sym
) == raw_hash
102 && SCM_SYMBOL_LENGTH (sym
) == len
)
104 char *chrs
= SCM_SYMBOL_CHARS (sym
);
110 if (name
[i
] != chrs
[i
])
122 /* The symbol was not found - create it. */
128 symbol
= scm_alloc_double_cell (SCM_MAKE_SYMBOL_TAG (len
),
129 (scm_t_bits
) scm_must_strndup (name
, len
),
131 SCM_UNPACK (scm_cons (SCM_BOOL_F
,
134 slot
= SCM_VELTS (symbols
) [hash
];
135 cell
= scm_cons (symbol
, SCM_UNDEFINED
);
136 SCM_VELTS (symbols
) [hash
] = scm_cons (cell
, 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
187 SCM_VALIDATE_SYMBOL (1, s
);
188 str
= scm_mem2string (SCM_SYMBOL_CHARS (s
), SCM_SYMBOL_LENGTH (s
));
189 scm_remember_upto_here_1 (s
);
195 SCM_DEFINE (scm_string_to_symbol
, "string->symbol", 1, 0, 0,
197 "Return the symbol whose name is @var{string}. This procedure\n"
198 "can create symbols with names containing special characters or\n"
199 "letters in the non-standard case, but it is usually a bad idea\n"
200 "to create such symbols because in some implementations of\n"
201 "Scheme they cannot be read as themselves. See\n"
202 "@code{symbol->string}.\n"
204 "The following examples assume that the implementation's\n"
205 "standard case is lower case:\n"
208 "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
209 "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
210 "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
212 " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
213 "(string=? \"K. Harper, M.D.\"\n"
215 " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
217 #define FUNC_NAME s_scm_string_to_symbol
219 SCM_VALIDATE_STRING (1, string
);
220 return scm_mem2symbol (SCM_STRING_CHARS (string
),
221 SCM_STRING_LENGTH (string
));
225 #define MAX_PREFIX_LENGTH 30
227 static int gensym_counter
;
229 SCM_DEFINE (scm_gensym
, "gensym", 0, 1, 0,
231 "Create a new symbol with a name constructed from a prefix and\n"
232 "a counter value. The string @var{prefix} can be specified as\n"
233 "an optional argument. Default prefix is @code{g}. The counter\n"
234 "is increased by 1 at each call. There is no provision for\n"
235 "resetting the counter.")
236 #define FUNC_NAME s_scm_gensym
238 char buf
[MAX_PREFIX_LENGTH
+ SCM_INTBUFLEN
];
241 if (SCM_UNBNDP (prefix
))
248 SCM_VALIDATE_STRING (1, prefix
);
249 len
= SCM_STRING_LENGTH (prefix
);
250 if (len
> MAX_PREFIX_LENGTH
)
251 name
= SCM_MUST_MALLOC (len
+ SCM_INTBUFLEN
);
252 memcpy (name
, SCM_STRING_CHARS (prefix
), len
);
255 int n_digits
= scm_iint2str (gensym_counter
++, 10, &name
[len
]);
256 SCM res
= scm_mem2symbol (name
, len
+ n_digits
);
258 scm_must_free (name
);
264 SCM_DEFINE (scm_symbol_hash
, "symbol-hash", 1, 0, 0,
266 "Return a hash value for @var{symbol}.")
267 #define FUNC_NAME s_scm_symbol_hash
269 SCM_VALIDATE_SYMBOL (1, symbol
);
270 return SCM_MAKINUM (SCM_SYMBOL_HASH (symbol
));
274 SCM_DEFINE (scm_symbol_fref
, "symbol-fref", 1, 0, 0,
276 "Return the contents of @var{symbol}'s @dfn{function slot}.")
277 #define FUNC_NAME s_scm_symbol_fref
279 SCM_VALIDATE_SYMBOL (1,s
);
280 return SCM_SYMBOL_FUNC (s
);
285 SCM_DEFINE (scm_symbol_pref
, "symbol-pref", 1, 0, 0,
287 "Return the @dfn{property list} currently associated with @var{symbol}.")
288 #define FUNC_NAME s_scm_symbol_pref
290 SCM_VALIDATE_SYMBOL (1,s
);
291 return SCM_SYMBOL_PROPS (s
);
296 SCM_DEFINE (scm_symbol_fset_x
, "symbol-fset!", 2, 0, 0,
298 "Change the binding of @var{symbol}'s function slot.")
299 #define FUNC_NAME s_scm_symbol_fset_x
301 SCM_VALIDATE_SYMBOL (1,s
);
302 SCM_SET_SYMBOL_FUNC (s
, val
);
303 return SCM_UNSPECIFIED
;
308 SCM_DEFINE (scm_symbol_pset_x
, "symbol-pset!", 2, 0, 0,
310 "Change the binding of @var{symbol}'s property slot.")
311 #define FUNC_NAME s_scm_symbol_pset_x
313 SCM_VALIDATE_SYMBOL (1,s
);
315 SCM_SET_SYMBOL_PROPS (s
, val
);
317 return SCM_UNSPECIFIED
;
322 /* Converts the given Scheme symbol OBJ into a C string, containing a copy
323 of OBJ's content with a trailing null byte. If LENP is non-NULL, set
324 *LENP to the string's length.
326 When STR is non-NULL it receives the copy and is returned by the function,
327 otherwise new memory is allocated and the caller is responsible for
328 freeing it via free(). If out of memory, NULL is returned.
330 Note that Scheme symbols may contain arbitrary data, including null
331 characters. This means that null termination is not a reliable way to
332 determine the length of the returned value. However, the function always
333 copies the complete contents of OBJ, and sets *LENP to the length of the
334 scheme symbol (if LENP is non-null). */
335 #define FUNC_NAME "scm_c_symbol2str"
337 scm_c_symbol2str (SCM obj
, char *str
, size_t *lenp
)
341 SCM_ASSERT (SCM_SYMBOLP (obj
), obj
, SCM_ARG1
, FUNC_NAME
);
342 len
= SCM_SYMBOL_LENGTH (obj
);
346 /* FIXME: Should we use exported wrappers for malloc (and free), which
347 * allow windows DLLs to call the correct freeing function? */
348 str
= (char *) malloc ((len
+ 1) * sizeof (char));
353 memcpy (str
, SCM_SYMBOL_CHARS (obj
), len
);
354 scm_remember_upto_here_1 (obj
);
366 scm_symbols_prehistory ()
368 symbols
= scm_make_weak_key_hash_table (SCM_MAKINUM (1009));
369 scm_permanent_object (symbols
);
377 #ifndef SCM_MAGIC_SNARFER
378 #include "libguile/symbols.x"