1 /* keyword.c                                       -*- mode:c; coding:utf-8; -*-
2  *
3  *   Copyright (c) 2010-2021  Takashi Kato <ktakashi@ymail.com>
4  *
5  *   Redistribution and use in source and binary forms, with or without
6  *   modification, are permitted provided that the following conditions
7  *   are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *
16  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *  $Id: $
29  */
30 #define LIBSAGITTARIUS_BODY
31 #include "sagittarius/private/keyword.h"
32 #include "sagittarius/private/error.h"
33 #include "sagittarius/private/hashtable.h"
34 #include "sagittarius/private/pair.h"
35 #include "sagittarius/private/port.h"
36 #include "sagittarius/private/string.h"
37 #include "sagittarius/private/symbol.h"
38 #include "sagittarius/private/thread.h"
39 #include "sagittarius/private/writer.h"
40 #include "sagittarius/private/builtin-keywords.h"
41 
42 #include "gc-incl.inc"
43 
keyword_print(SgObject obj,SgPort * port,SgWriteContext * ctx)44 static void keyword_print(SgObject obj, SgPort *port, SgWriteContext *ctx)
45 {
46   SgKeyword *k = SG_KEYWORD(obj);
47   if (SG_WRITE_MODE(ctx) == SG_WRITE_DISPLAY) {
48     Sg_Puts(port, k->name);
49   } else {
50     Sg_Putc(port, ':');
51     Sg_WriteSymbolName(k->name, port, ctx,
52 		       (SG_SYMBOL_WRITER_NOESCAPE_INITIAL
53 			|SG_SYMBOL_WRITER_NOESCAPE_EMPTY));
54   }
55 }
56 
57 SG_DEFINE_BUILTIN_CLASS_SIMPLE(Sg_KeywordClass, keyword_print);
58 
59 #ifdef USE_WEAK_KEYWORD
60 # include "sagittarius/private/weak.h"
61 # define Sg_HashTableRef Sg_WeakHashTableRef
62 # define Sg_HashTableSet Sg_WeakHashTableSet
63 #endif
64 
65 static struct
66 {
67 #ifdef USE_WEAK_KEYWORD
68   SgWeakHashTable *table;
69 #else
70   SgHashTable *table;
71 #endif
72   SgInternalMutex mutex;
73 } keywords = { NULL };
74 
75 
Sg_MakeKeyword(SgString * name)76 SgObject Sg_MakeKeyword(SgString *name)
77 {
78   SgObject r;
79   SgKeyword *k;
80 
81   Sg_LockMutex(&keywords.mutex);
82   r = Sg_HashTableRef(keywords.table, name, SG_FALSE);
83   Sg_UnlockMutex(&keywords.mutex);
84 
85   if (SG_KEYWORDP(r)) return r;
86 
87   k = SG_NEW(SgKeyword);
88   SG_SET_CLASS(k, SG_CLASS_KEYWORD);
89   if (SG_IMMUTABLE_STRINGP(name)) {
90     k->name = name;
91   } else {
92     k->name = SG_STRING(Sg_CopyString(name));
93   }
94 
95   Sg_LockMutex(&keywords.mutex);
96   r = Sg_HashTableSet(keywords.table, name, SG_OBJ(k), SG_HASH_NO_OVERWRITE);
97   Sg_UnlockMutex(&keywords.mutex);
98   return r;
99 }
100 
Sg_GetKeyword(SgObject key,SgObject list,SgObject fallback)101 SgObject Sg_GetKeyword(SgObject key, SgObject list, SgObject fallback)
102 {
103   SgObject cp;
104   SG_FOR_EACH(cp, list) {
105     if (!SG_PAIRP(SG_CDR(cp))) {
106       Sg_Error(UC("incomplete key list: %S"), list);
107     }
108     if (key == SG_CAR(cp)) return SG_CADR(cp);
109     cp = SG_CDR(cp);
110   }
111   if (SG_UNBOUNDP(fallback)) {
112     Sg_Error(UC("value for key %S is not provided: %S"), key, list);
113   }
114   return fallback;
115 }
116 
117 #include "builtin-keywords.c"
118 
119 DEFINE_DEBUG_DUMPER(keyword, keywords.table)
120 
Sg__InitKeyword()121 void Sg__InitKeyword()
122 {
123   Sg_InitMutex(&keywords.mutex, FALSE);
124 #ifdef USE_WEAK_KEYWORD
125   keywords.table =
126     SG_WEAK_HASHTABLE(Sg_MakeWeakHashTableSimple(SG_HASH_STRING,
127 						 SG_WEAK_REMOVE_VALUE,
128 						 256, SG_FALSE));
129 #else
130   keywords.table = SG_HASHTABLE(Sg_MakeHashTableSimple(SG_HASH_STRING, 256));
131 #endif
132   init_builtin_keywords();
133 
134   ADD_DEBUG_DUMPER(keyword);
135 }
136 /*
137   end of file
138   Local Variables:
139   coding: utf-8-unix
140   End:
141 */
142