*** empty log message ***
[bpt/guile.git] / libguile / keywords.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003, 2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18
19 \f
20
21 #include <string.h>
22
23 #include "libguile/_scm.h"
24 #include "libguile/ports.h"
25 #include "libguile/root.h"
26 #include "libguile/smob.h"
27 #include "libguile/hashtab.h"
28
29 #include "libguile/validate.h"
30 #include "libguile/keywords.h"
31 #include "libguile/strings.h"
32
33 \f
34
35 scm_t_bits scm_tc16_keyword;
36
37 #define KEYWORDP(X) (SCM_SMOB_PREDICATE (scm_tc16_keyword, (X)))
38 #define KEYWORDSYM(X) (SCM_SMOB_OBJECT (X))
39
40 static int
41 keyword_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
42 {
43 scm_puts ("#:", port);
44 scm_display (KEYWORDSYM (exp), port);
45 return 1;
46 }
47
48 SCM_DEFINE (scm_keyword_p, "keyword?", 1, 0, 0,
49 (SCM obj),
50 "Return @code{#t} if the argument @var{obj} is a keyword, else\n"
51 "@code{#f}.")
52 #define FUNC_NAME s_scm_keyword_p
53 {
54 return scm_from_bool (KEYWORDP (obj));
55 }
56 #undef FUNC_NAME
57
58 SCM_DEFINE (scm_symbol_to_keyword, "symbol->keyword", 1, 0, 0,
59 (SCM symbol),
60 "Return the keyword with the same name as @var{symbol}.")
61 #define FUNC_NAME s_scm_symbol_to_keyword
62 {
63 SCM keyword;
64
65 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, 0, NULL, "symbol");
66
67 SCM_DEFER_INTS;
68 keyword = scm_hashq_ref (scm_keyword_obarray, symbol, SCM_BOOL_F);
69 if (scm_is_false (keyword))
70 {
71 SCM_NEWSMOB (keyword, scm_tc16_keyword, SCM_UNPACK (symbol));
72 scm_hashq_set_x (scm_keyword_obarray, symbol, keyword);
73 }
74 SCM_ALLOW_INTS;
75 return keyword;
76 }
77 #undef FUNC_NAME
78
79 SCM_DEFINE (scm_keyword_to_symbol, "keyword->symbol", 1, 0, 0,
80 (SCM keyword),
81 "Return the symbol with the same name as @var{keyword}.")
82 #define FUNC_NAME s_scm_keyword_to_symbol
83 {
84 scm_assert_smob_type (scm_tc16_keyword, keyword);
85 return KEYWORDSYM (keyword);
86 }
87 #undef FUNC_NAME
88
89 int
90 scm_is_keyword (SCM val)
91 {
92 return KEYWORDP (val);
93 }
94
95 SCM
96 scm_from_locale_keyword (const char *str)
97 {
98 return scm_symbol_to_keyword (scm_from_locale_symbol (str));
99 }
100
101 SCM
102 scm_from_locale_keywordn (const char *str, size_t len)
103 {
104 return scm_symbol_to_keyword (scm_from_locale_symboln (str, len));
105 }
106
107
108 void
109 scm_init_keywords ()
110 {
111 scm_tc16_keyword = scm_make_smob_type ("keyword", 0);
112 scm_set_smob_mark (scm_tc16_keyword, scm_markcdr);
113 scm_set_smob_print (scm_tc16_keyword, keyword_print);
114
115 scm_keyword_obarray = scm_c_make_hash_table (0);
116 #include "libguile/keywords.x"
117 }
118
119
120 /*
121 Local Variables:
122 c-file-style: "gnu"
123 End:
124 */