123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include <string.h>
- #include <stdarg.h>
- #include "libguile/_scm.h"
- #include "libguile/async.h"
- #include "libguile/ports.h"
- #include "libguile/root.h"
- #include "libguile/smob.h"
- #include "libguile/hashtab.h"
- #include "libguile/validate.h"
- #include "libguile/keywords.h"
- #include "libguile/strings.h"
- static SCM keyword_obarray;
- #define SCM_KEYWORDP(x) (SCM_HAS_TYP7 (x, scm_tc7_keyword))
- #define SCM_KEYWORD_SYMBOL(x) (SCM_CELL_OBJECT_1 (x))
- SCM_DEFINE (scm_keyword_p, "keyword?", 1, 0, 0,
- (SCM obj),
- "Return @code{#t} if the argument @var{obj} is a keyword, else\n"
- "@code{#f}.")
- #define FUNC_NAME s_scm_keyword_p
- {
- return scm_from_bool (SCM_KEYWORDP (obj));
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_symbol_to_keyword, "symbol->keyword", 1, 0, 0,
- (SCM symbol),
- "Return the keyword with the same name as @var{symbol}.")
- #define FUNC_NAME s_scm_symbol_to_keyword
- {
- SCM keyword;
- SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, 0, NULL, "symbol");
- SCM_CRITICAL_SECTION_START;
-
- keyword = scm_hashq_ref (keyword_obarray, symbol, SCM_BOOL_F);
- if (scm_is_false (keyword))
- {
- keyword = scm_cell (scm_tc7_keyword, SCM_UNPACK (symbol));
- scm_hashq_set_x (keyword_obarray, symbol, keyword);
- }
- SCM_CRITICAL_SECTION_END;
- return keyword;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_keyword_to_symbol, "keyword->symbol", 1, 0, 0,
- (SCM keyword),
- "Return the symbol with the same name as @var{keyword}.")
- #define FUNC_NAME s_scm_keyword_to_symbol
- {
- SCM_VALIDATE_KEYWORD (1, keyword);
- return SCM_KEYWORD_SYMBOL (keyword);
- }
- #undef FUNC_NAME
- int
- scm_is_keyword (SCM val)
- {
- return SCM_KEYWORDP (val);
- }
- SCM
- scm_from_locale_keyword (const char *name)
- {
- return scm_symbol_to_keyword (scm_from_locale_symbol (name));
- }
- SCM
- scm_from_locale_keywordn (const char *name, size_t len)
- {
- return scm_symbol_to_keyword (scm_from_locale_symboln (name, len));
- }
- SCM
- scm_from_latin1_keyword (const char *name)
- {
- return scm_symbol_to_keyword (scm_from_latin1_symbol (name));
- }
- SCM
- scm_from_utf8_keyword (const char *name)
- {
- return scm_symbol_to_keyword (scm_from_utf8_symbol (name));
- }
- SCM_SYMBOL (scm_keyword_argument_error, "keyword-argument-error");
- void
- scm_c_bind_keyword_arguments (const char *subr, SCM rest,
- scm_t_keyword_arguments_flags flags, ...)
- {
- va_list va;
- if (SCM_UNLIKELY (!(flags & SCM_ALLOW_NON_KEYWORD_ARGUMENTS)
- && scm_ilength (rest) % 2 != 0))
- scm_error (scm_keyword_argument_error,
- subr, "Odd length of keyword argument list",
- SCM_EOL, SCM_BOOL_F);
- while (scm_is_pair (rest))
- {
- SCM kw_or_arg = SCM_CAR (rest);
- SCM tail = SCM_CDR (rest);
- if (scm_is_keyword (kw_or_arg) && scm_is_pair (tail))
- {
- SCM kw;
- SCM *arg_p;
- va_start (va, flags);
- for (;;)
- {
- kw = va_arg (va, SCM);
- if (SCM_UNBNDP (kw))
- {
-
- if (!(flags & SCM_ALLOW_OTHER_KEYS))
- scm_error_scm (scm_keyword_argument_error,
- scm_from_locale_string (subr),
- scm_from_latin1_string
- ("Unrecognized keyword"),
- SCM_EOL, scm_list_1 (kw_or_arg));
- break;
- }
- arg_p = va_arg (va, SCM *);
- if (scm_is_eq (kw_or_arg, kw))
- {
-
- *arg_p = SCM_CAR (tail);
- break;
- }
- }
- va_end (va);
-
- rest = SCM_CDR (tail);
- }
- else
- {
-
- if (!(flags & SCM_ALLOW_NON_KEYWORD_ARGUMENTS))
- scm_error_scm (scm_keyword_argument_error,
- scm_from_locale_string (subr),
- scm_from_latin1_string ("Invalid keyword"),
- SCM_EOL, scm_list_1 (kw_or_arg));
-
- rest = tail;
- }
- }
- }
- void
- scm_init_keywords ()
- {
- keyword_obarray = scm_c_make_hash_table (0);
- #include "libguile/keywords.x"
- }
|