keywords.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library 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 GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <string.h>
  21. #include "libguile/_scm.h"
  22. #include "libguile/async.h"
  23. #include "libguile/ports.h"
  24. #include "libguile/root.h"
  25. #include "libguile/smob.h"
  26. #include "libguile/hashtab.h"
  27. #include "libguile/validate.h"
  28. #include "libguile/keywords.h"
  29. #include "libguile/strings.h"
  30. scm_t_bits scm_tc16_keyword;
  31. #define KEYWORDP(X) (SCM_SMOB_PREDICATE (scm_tc16_keyword, (X)))
  32. #define KEYWORDSYM(X) (SCM_SMOB_OBJECT (X))
  33. static int
  34. keyword_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  35. {
  36. scm_puts ("#:", port);
  37. scm_display (KEYWORDSYM (exp), port);
  38. return 1;
  39. }
  40. SCM_DEFINE (scm_keyword_p, "keyword?", 1, 0, 0,
  41. (SCM obj),
  42. "Return @code{#t} if the argument @var{obj} is a keyword, else\n"
  43. "@code{#f}.")
  44. #define FUNC_NAME s_scm_keyword_p
  45. {
  46. return scm_from_bool (KEYWORDP (obj));
  47. }
  48. #undef FUNC_NAME
  49. SCM_DEFINE (scm_symbol_to_keyword, "symbol->keyword", 1, 0, 0,
  50. (SCM symbol),
  51. "Return the keyword with the same name as @var{symbol}.")
  52. #define FUNC_NAME s_scm_symbol_to_keyword
  53. {
  54. SCM keyword;
  55. SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, 0, NULL, "symbol");
  56. SCM_CRITICAL_SECTION_START;
  57. /* njrev: NEWSMOB and hashq_set_x can raise errors */
  58. keyword = scm_hashq_ref (scm_keyword_obarray, symbol, SCM_BOOL_F);
  59. if (scm_is_false (keyword))
  60. {
  61. SCM_NEWSMOB (keyword, scm_tc16_keyword, SCM_UNPACK (symbol));
  62. scm_hashq_set_x (scm_keyword_obarray, symbol, keyword);
  63. }
  64. SCM_CRITICAL_SECTION_END;
  65. return keyword;
  66. }
  67. #undef FUNC_NAME
  68. SCM_DEFINE (scm_keyword_to_symbol, "keyword->symbol", 1, 0, 0,
  69. (SCM keyword),
  70. "Return the symbol with the same name as @var{keyword}.")
  71. #define FUNC_NAME s_scm_keyword_to_symbol
  72. {
  73. scm_assert_smob_type (scm_tc16_keyword, keyword);
  74. return KEYWORDSYM (keyword);
  75. }
  76. #undef FUNC_NAME
  77. int
  78. scm_is_keyword (SCM val)
  79. {
  80. return KEYWORDP (val);
  81. }
  82. SCM
  83. scm_from_locale_keyword (const char *str)
  84. {
  85. return scm_symbol_to_keyword (scm_from_locale_symbol (str));
  86. }
  87. SCM
  88. scm_from_locale_keywordn (const char *str, size_t len)
  89. {
  90. return scm_symbol_to_keyword (scm_from_locale_symboln (str, len));
  91. }
  92. /* njrev: critical sections reviewed so far up to here */
  93. void
  94. scm_init_keywords ()
  95. {
  96. scm_tc16_keyword = scm_make_smob_type ("keyword", 0);
  97. scm_set_smob_mark (scm_tc16_keyword, scm_markcdr);
  98. scm_set_smob_print (scm_tc16_keyword, keyword_print);
  99. scm_keyword_obarray = scm_c_make_hash_table (0);
  100. #include "libguile/keywords.x"
  101. }
  102. /*
  103. Local Variables:
  104. c-file-style: "gnu"
  105. End:
  106. */