1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
|
/* Copyright (C) 1995,1996,1997,1998, 2000, 2001 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
* As a special exception, the Free Software Foundation gives permission
* for additional uses of the text contained in its release of GUILE.
*
* The exception is that, if you link the GUILE library with other files
* to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the GUILE library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the
* Free Software Foundation under the name GUILE. If you copy
* code from other Free Software Foundation releases into a copy of
* GUILE, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for GUILE, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice. */
#include "libguile/_scm.h"
#include "libguile/chars.h"
#include "libguile/eval.h"
#include "libguile/hash.h"
#include "libguile/smob.h"
#include "libguile/variable.h"
#include "libguile/alist.h"
#include "libguile/fluids.h"
#include "libguile/strings.h"
#include "libguile/vectors.h"
#include "libguile/hashtab.h"
#include "libguile/weaks.h"
#include "libguile/modules.h"
#include "libguile/deprecation.h"
#include "libguile/validate.h"
#include "libguile/symbols.h"
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#if SCM_ENABLE_VCELLS
/* scm_sym2ovcell
* looks up the symbol in an arbitrary obarray.
*/
SCM
scm_sym2ovcell_soft (SCM sym, SCM obarray)
{
SCM lsym, z;
size_t hash = SCM_SYMBOL_HASH (sym) % SCM_VECTOR_LENGTH (obarray);
scm_c_issue_deprecation_warning ("`scm_sym2ovcell_soft' is deprecated. "
"Use hashtables instead.");
SCM_REDEFER_INTS;
for (lsym = SCM_VELTS (obarray)[hash];
SCM_NIMP (lsym);
lsym = SCM_CDR (lsym))
{
z = SCM_CAR (lsym);
if (SCM_EQ_P (SCM_CAR (z), sym))
{
SCM_REALLOW_INTS;
return z;
}
}
SCM_REALLOW_INTS;
return SCM_BOOL_F;
}
SCM
scm_sym2ovcell (SCM sym, SCM obarray)
#define FUNC_NAME "scm_sym2ovcell"
{
SCM answer;
scm_c_issue_deprecation_warning ("`scm_sym2ovcell' is deprecated. "
"Use hashtables instead.");
answer = scm_sym2ovcell_soft (sym, obarray);
if (!SCM_FALSEP (answer))
return answer;
SCM_MISC_ERROR ("uninterned symbol: ~S", scm_list_1 (sym));
return SCM_UNSPECIFIED; /* not reached */
}
#undef FUNC_NAME
/* Intern a symbol whose name is the LEN characters at NAME in OBARRAY.
OBARRAY should be a vector of lists, indexed by the name's hash
value, modulo OBARRAY's length. Each list has the form
((SYMBOL . VALUE) ...), where SYMBOL is a symbol, and VALUE is the
value associated with that symbol (in the current module? in the
system module?)
To "intern" a symbol means: if OBARRAY already contains a symbol by
that name, return its (SYMBOL . VALUE) pair; otherwise, create a
new symbol, add the pair (SYMBOL . SCM_UNDEFINED) to the
appropriate list of the OBARRAY, and return the pair.
If softness is non-zero, don't create a symbol if it isn't already
in OBARRAY; instead, just return #f.
If OBARRAY is SCM_BOOL_F, create a symbol listed in no obarray and
return (SYMBOL . SCM_UNDEFINED). */
SCM
scm_intern_obarray_soft (const char *name,size_t len,SCM obarray,unsigned int softness)
{
SCM symbol = scm_mem2symbol (name, len);
size_t raw_hash = SCM_SYMBOL_HASH (symbol);
size_t hash;
SCM lsym;
scm_c_issue_deprecation_warning ("`scm_intern_obarray_soft' is deprecated. "
"Use hashtables instead.");
if (SCM_FALSEP (obarray))
{
if (softness)
return SCM_BOOL_F;
else
return scm_cons (symbol, SCM_UNDEFINED);
}
hash = raw_hash % SCM_VECTOR_LENGTH (obarray);
for (lsym = SCM_VELTS (obarray)[hash]; SCM_NIMP (lsym); lsym = SCM_CDR (lsym))
{
SCM a = SCM_CAR (lsym);
SCM z = SCM_CAR (a);
if (SCM_EQ_P (z, symbol))
return a;
}
if (softness)
{
return SCM_BOOL_F;
}
else
{
SCM cell = scm_cons (symbol, SCM_UNDEFINED);
SCM slot = SCM_VELTS (obarray) [hash];
SCM_VELTS (obarray) [hash] = scm_cons (cell, slot);
return cell;
}
}
SCM
scm_intern_obarray (const char *name,size_t len,SCM obarray)
{
scm_c_issue_deprecation_warning ("`scm_intern_obarray' is deprecated. "
"Use hashtables instead.");
return scm_intern_obarray_soft (name, len, obarray, 0);
}
SCM
scm_intern (const char *name,size_t len)
{
scm_c_issue_deprecation_warning ("`scm_intern' is deprecated. "
"Use scm_c_define or scm_c_lookup instead.");
{
SCM symbol = scm_mem2symbol (name, len);
SCM var = scm_sym2var (symbol, SCM_BOOL_F, SCM_BOOL_T);
SCM vcell = SCM_VARVCELL (var);
SCM_SETCAR (vcell, symbol);
return vcell;
}
}
SCM
scm_intern0 (const char * name)
{
scm_c_issue_deprecation_warning ("`scm_intern0' is deprecated. "
"Use scm_define or scm_lookup instead.");
return scm_intern (name, strlen (name));
}
/* Intern the symbol named NAME in scm_symhash, and give it the value
VAL. NAME is null-terminated. Use the current top_level lookup
closure to give NAME its value.
*/
SCM
scm_sysintern (const char *name, SCM val)
{
SCM var;
scm_c_issue_deprecation_warning ("`scm_sysintern' is deprecated. "
"Use `scm_define' instead.");
var = scm_c_define (name, val);
return SCM_VARVCELL (var);
}
SCM
scm_sysintern0 (const char *name)
{
SCM var;
SCM symbol;
scm_c_issue_deprecation_warning ("`scm_sysintern0' is deprecated. "
"Use `scm_define' instead.");
symbol = scm_str2symbol (name);
var = scm_sym2var (symbol, scm_current_module_lookup_closure (), SCM_BOOL_T);
if (var == SCM_BOOL_F)
scm_misc_error ("sysintern0", "can't define variable", symbol);
return SCM_VARVCELL (var);
}
/* Lookup the value of the symbol named by the nul-terminated string
NAME in the current module. */
SCM
scm_symbol_value0 (const char *name)
{
scm_c_issue_deprecation_warning ("`scm_symbol_value0' is deprecated. "
"Use `scm_lookup' instead.");
return scm_variable_ref (scm_c_lookup (name));
}
SCM
scm_sym2vcell (SCM sym, SCM thunk, SCM definep)
{
SCM var;
scm_c_issue_deprecation_warning("`scm_sym2vcell' is deprecated. "
"Use `scm_define' or `scm_lookup' instead.");
var = scm_sym2var (sym, thunk, definep);
if (var == SCM_BOOL_F)
return SCM_BOOL_F;
return SCM_VARVCELL (var);
}
SCM_DEFINE (scm_string_to_obarray_symbol, "string->obarray-symbol", 2, 1, 0,
(SCM o, SCM s, SCM softp),
"Intern a new symbol in @var{obarray}, a symbol table, with name\n"
"@var{string}.\n\n"
"If @var{obarray} is @code{#f}, use the default system symbol table. If\n"
"@var{obarray} is @code{#t}, the symbol should not be interned in any\n"
"symbol table; merely return the pair (@var{symbol}\n"
". @var{#<undefined>}).\n\n"
"The @var{soft?} argument determines whether new symbol table entries\n"
"should be created when the specified symbol is not already present in\n"
"@var{obarray}. If @var{soft?} is specified and is a true value, then\n"
"new entries should not be added for symbols not already present in the\n"
"table; instead, simply return @code{#f}.")
#define FUNC_NAME s_scm_string_to_obarray_symbol
{
SCM vcell;
SCM answer;
int softness;
SCM_VALIDATE_STRING (2, s);
SCM_ASSERT (SCM_BOOLP (o) || SCM_VECTORP (o), o, SCM_ARG1, FUNC_NAME);
scm_c_issue_deprecation_warning ("`string->obarray-symbol' is deprecated. "
"Use hashtables instead.");
softness = (!SCM_UNBNDP (softp) && !SCM_FALSEP(softp));
/* iron out some screwy calling conventions */
if (SCM_FALSEP (o))
{
/* nothing interesting to do here. */
return scm_string_to_symbol (s);
}
else if (SCM_EQ_P (o, SCM_BOOL_T))
o = SCM_BOOL_F;
vcell = scm_intern_obarray_soft (SCM_STRING_CHARS(s),
SCM_STRING_LENGTH (s),
o,
softness);
if (SCM_FALSEP (vcell))
return vcell;
answer = SCM_CAR (vcell);
return answer;
}
#undef FUNC_NAME
SCM_DEFINE (scm_intern_symbol, "intern-symbol", 2, 0, 0,
(SCM o, SCM s),
"Add a new symbol to @var{obarray} with name @var{string}, bound to an\n"
"unspecified initial value. The symbol table is not modified if a symbol\n"
"with this name is already present.")
#define FUNC_NAME s_scm_intern_symbol
{
size_t hval;
SCM_VALIDATE_SYMBOL (2,s);
if (SCM_FALSEP (o))
return SCM_UNSPECIFIED;
scm_c_issue_deprecation_warning ("`intern-symbol' is deprecated. "
"Use hashtables instead.");
SCM_VALIDATE_VECTOR (1,o);
hval = SCM_SYMBOL_HASH (s) % SCM_VECTOR_LENGTH (o);
/* If the symbol is already interned, simply return. */
SCM_REDEFER_INTS;
{
SCM lsym;
SCM sym;
for (lsym = SCM_VELTS (o)[hval];
SCM_NIMP (lsym);
lsym = SCM_CDR (lsym))
{
sym = SCM_CAR (lsym);
if (SCM_EQ_P (SCM_CAR (sym), s))
{
SCM_REALLOW_INTS;
return SCM_UNSPECIFIED;
}
}
SCM_VELTS (o)[hval] =
scm_acons (s, SCM_UNDEFINED, SCM_VELTS (o)[hval]);
}
SCM_REALLOW_INTS;
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCM_DEFINE (scm_unintern_symbol, "unintern-symbol", 2, 0, 0,
(SCM o, SCM s),
"Remove the symbol with name @var{string} from @var{obarray}. This\n"
"function returns @code{#t} if the symbol was present and @code{#f}\n"
"otherwise.")
#define FUNC_NAME s_scm_unintern_symbol
{
size_t hval;
scm_c_issue_deprecation_warning ("`unintern-symbol' is deprecated. "
"Use hashtables instead.");
SCM_VALIDATE_SYMBOL (2,s);
if (SCM_FALSEP (o))
return SCM_BOOL_F;
SCM_VALIDATE_VECTOR (1,o);
hval = SCM_SYMBOL_HASH (s) % SCM_VECTOR_LENGTH (o);
SCM_DEFER_INTS;
{
SCM lsym_follow;
SCM lsym;
SCM sym;
for (lsym = SCM_VELTS (o)[hval], lsym_follow = SCM_BOOL_F;
SCM_NIMP (lsym);
lsym_follow = lsym, lsym = SCM_CDR (lsym))
{
sym = SCM_CAR (lsym);
if (SCM_EQ_P (SCM_CAR (sym), s))
{
/* Found the symbol to unintern. */
if (SCM_FALSEP (lsym_follow))
SCM_VELTS(o)[hval] = lsym;
else
SCM_SETCDR (lsym_follow, SCM_CDR(lsym));
SCM_ALLOW_INTS;
return SCM_BOOL_T;
}
}
}
SCM_ALLOW_INTS;
return SCM_BOOL_F;
}
#undef FUNC_NAME
SCM_DEFINE (scm_symbol_binding, "symbol-binding", 2, 0, 0,
(SCM o, SCM s),
"Look up in @var{obarray} the symbol whose name is @var{string}, and\n"
"return the value to which it is bound. If @var{obarray} is @code{#f},\n"
"use the global symbol table. If @var{string} is not interned in\n"
"@var{obarray}, an error is signalled.")
#define FUNC_NAME s_scm_symbol_binding
{
SCM vcell;
scm_c_issue_deprecation_warning ("`symbol-binding' is deprecated. "
"Use hashtables instead.");
SCM_VALIDATE_SYMBOL (2,s);
if (SCM_FALSEP (o))
return scm_variable_ref (scm_lookup (s));
SCM_VALIDATE_VECTOR (1,o);
vcell = scm_sym2ovcell (s, o);
return SCM_CDR(vcell);
}
#undef FUNC_NAME
SCM_DEFINE (scm_symbol_interned_p, "symbol-interned?", 2, 0, 0,
(SCM o, SCM s),
"Return @code{#t} if @var{obarray} contains a symbol with name\n"
"@var{string}, and @code{#f} otherwise.")
#define FUNC_NAME s_scm_symbol_interned_p
{
SCM vcell;
scm_c_issue_deprecation_warning ("`symbol-interned?' is deprecated. "
"Use hashtables instead.");
SCM_VALIDATE_SYMBOL (2,s);
if (SCM_FALSEP (o))
{
SCM var = scm_sym2var (s, SCM_BOOL_F, SCM_BOOL_F);
if (var != SCM_BOOL_F)
return SCM_BOOL_T;
return SCM_BOOL_F;
}
SCM_VALIDATE_VECTOR (1,o);
vcell = scm_sym2ovcell_soft (s, o);
return (SCM_NIMP(vcell)
? SCM_BOOL_T
: SCM_BOOL_F);
}
#undef FUNC_NAME
SCM_DEFINE (scm_symbol_bound_p, "symbol-bound?", 2, 0, 0,
(SCM o, SCM s),
"Return @code{#t} if @var{obarray} contains a symbol with name\n"
"@var{string} bound to a defined value. This differs from\n"
"@var{symbol-interned?} in that the mere mention of a symbol\n"
"usually causes it to be interned; @code{symbol-bound?}\n"
"determines whether a symbol has been given any meaningful\n"
"value.")
#define FUNC_NAME s_scm_symbol_bound_p
{
SCM vcell;
scm_c_issue_deprecation_warning ("`symbol-bound?' is deprecated. "
"Use hashtables instead.");
SCM_VALIDATE_SYMBOL (2,s);
if (SCM_FALSEP (o))
{
SCM var = scm_sym2var (s, SCM_BOOL_F, SCM_BOOL_F);
if (SCM_DEFVARIABLEP (var))
return SCM_BOOL_T;
return SCM_BOOL_F;
}
SCM_VALIDATE_VECTOR (1,o);
vcell = scm_sym2ovcell_soft (s, o);
return SCM_BOOL (SCM_NIMP (vcell) && !SCM_UNBNDP (SCM_CDR (vcell)));
}
#undef FUNC_NAME
SCM_DEFINE (scm_symbol_set_x, "symbol-set!", 3, 0, 0,
(SCM o, SCM s, SCM v),
"Find the symbol in @var{obarray} whose name is @var{string}, and rebind\n"
"it to @var{value}. An error is signalled if @var{string} is not present\n"
"in @var{obarray}.")
#define FUNC_NAME s_scm_symbol_set_x
{
SCM vcell;
scm_c_issue_deprecation_warning ("`symbol-set!' is deprecated. "
"Use the module system instead.");
SCM_VALIDATE_SYMBOL (2,s);
if (SCM_FALSEP (o))
{
scm_define (s, v);
return SCM_UNSPECIFIED;
}
SCM_VALIDATE_VECTOR (1,o);
vcell = scm_sym2ovcell (s, o);
SCM_SETCDR (vcell, v);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
#if 0
static void
copy_and_prune_obarray (SCM from, SCM to)
{
int i;
int length = SCM_VECTOR_LENGTH (from);
for (i = 0; i < length; ++i)
{
SCM head = SCM_VELTS (from)[i]; /* GC protection */
SCM ls = head;
SCM res = SCM_EOL;
SCM *lloc = &res;
while (SCM_NIMP (ls))
{
if (!SCM_UNBNDP (SCM_CDAR (ls)))
{
*lloc = scm_cons (SCM_CAR (ls), SCM_EOL);
lloc = SCM_CDRLOC (*lloc);
}
ls = SCM_CDR (ls);
}
SCM_VELTS (to)[i] = res;
}
}
SCM_DEFINE (scm_builtin_bindings, "builtin-bindings", 0, 0, 0,
(),
"Create and return a copy of the global symbol table, removing all\n"
"unbound symbols.")
#define FUNC_NAME s_scm_builtin_bindings
{
int length = SCM_VECTOR_LENGTH (scm_symhash);
SCM obarray = scm_c_make_hash_table (length);
scm_issue_deprecation_warning ("`builtin-bindings' is deprecated. "
"Use the module system instead.");
copy_and_prune_obarray (scm_symhash, obarray);
return obarray;
}
#undef FUNC_NAME
#endif
#define MAX_PREFIX_LENGTH 30
static int gentemp_counter;
SCM_DEFINE (scm_gentemp, "gentemp", 0, 2, 0,
(SCM prefix, SCM obarray),
"Create a new symbol with a name unique in an obarray.\n"
"The name is constructed from an optional string @var{prefix}\n"
"and a counter value. The default prefix is @code{t}. The\n"
"@var{obarray} is specified as a second optional argument.\n"
"Default is the system obarray where all normal symbols are\n"
"interned. The counter is increased by 1 at each\n"
"call. There is no provision for resetting the counter.")
#define FUNC_NAME s_scm_gentemp
{
char buf[MAX_PREFIX_LENGTH + SCM_INTBUFLEN];
char *name = buf;
int len, n_digits;
scm_c_issue_deprecation_warning ("`gentemp' is deprecated. "
"Use `gensym' instead.");
if (SCM_UNBNDP (prefix))
{
name[0] = 't';
len = 1;
}
else
{
SCM_VALIDATE_STRING (1, prefix);
len = SCM_STRING_LENGTH (prefix);
if (len > MAX_PREFIX_LENGTH)
name = SCM_MUST_MALLOC (MAX_PREFIX_LENGTH + SCM_INTBUFLEN);
strncpy (name, SCM_STRING_CHARS (prefix), len);
}
if (SCM_UNBNDP (obarray))
return scm_gensym (prefix);
else
SCM_ASSERT ((SCM_VECTORP (obarray) || SCM_WVECTP (obarray)),
obarray,
SCM_ARG2,
FUNC_NAME);
do
n_digits = scm_iint2str (gentemp_counter++, 10, &name[len]);
while (!SCM_FALSEP (scm_intern_obarray_soft (name,
len + n_digits,
obarray,
1)));
{
SCM vcell = scm_intern_obarray_soft (name,
len + n_digits,
obarray,
0);
if (name != buf)
scm_must_free (name);
return SCM_CAR (vcell);
}
}
#undef FUNC_NAME
void
scm_init_symbols_deprecated ()
{
gentemp_counter = 0;
#include "libguile/symbols-deprecated.x"
}
#endif /* SCM_ENABLE_VCELLS */
/*
Local Variables:
c-file-style: "gnu"
End:
*/
|