2 * Copyright 2001-2006 Adrian Thurston <thurston@cs.queensu.ca>
5 /* This file is part of Ragel.
7 * Ragel is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Ragel is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Ragel; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 typedef unsigned long long Size;
35 friend inline Key operator+(const Key key1, const Key key2);
36 friend inline Key operator-(const Key key1, const Key key2);
37 friend inline Key operator/(const Key key1, const Key key2);
38 friend inline long operator&(const Key key1, const Key key2);
40 friend inline bool operator<( const Key key1, const Key key2 );
41 friend inline bool operator<=( const Key key1, const Key key2 );
42 friend inline bool operator>( const Key key1, const Key key2 );
43 friend inline bool operator>=( const Key key1, const Key key2 );
44 friend inline bool operator==( const Key key1, const Key key2 );
45 friend inline bool operator!=( const Key key1, const Key key2 );
50 Key( const Key &key ) : key(key.key) {}
51 Key( long key ) : key(key) {}
53 /* Returns the value used to represent the key. This value must be
54 * interpreted based on signedness. */
55 long getVal() const { return key; };
57 /* Returns the key casted to a long long. This form of the key does not
58 * require and signedness interpretation. */
59 long long getLongLong() const;
61 bool isUpper() const { return ( 'A' <= key && key <= 'Z' ); }
62 bool isLower() const { return ( 'a' <= key && key <= 'z' ); }
63 bool isPrintable() const { return ( 32 <= key && key < 127 ); }
66 { return Key( 'A' + ( key - 'a' ) ); }
68 { return Key( 'a' + ( key - 'A' ) ); }
70 void operator+=( const Key other )
72 /* FIXME: must be made aware of isSigned. */
76 void operator-=( const Key other )
78 /* FIXME: must be made aware of isSigned. */
82 void operator|=( const Key other )
84 /* FIXME: must be made aware of isSigned. */
88 /* Decrement. Needed only for ranges. */
89 inline void decrement();
90 inline void increment();
107 HostType *defaultAlphType;
108 bool explicitUnsigned;
112 /* Target language. */
120 extern HostLang *hostLang;
121 extern HostLangType hostLangType;
123 extern HostLang hostLangC;
124 extern HostLang hostLangD;
125 extern HostLang hostLangJava;
127 /* An abstraction of the key operators that manages key operations such as
128 * comparison and increment according the signedness of the key. */
131 /* Default to signed alphabet. */
137 /* Default to signed alphabet. */
138 KeyOps( bool isSigned )
139 :isSigned(isSigned) {}
145 void setAlphType( HostType *alphType )
147 this->alphType = alphType;
148 isSigned = alphType->isSigned;
150 minKey = (long) alphType->minVal;
151 maxKey = (long) alphType->maxVal;
154 minKey = (long) (unsigned long) alphType->minVal;
155 maxKey = (long) (unsigned long) alphType->maxVal;
159 /* Compute the distance between two keys. */
160 Size span( Key key1, Key key2 )
163 (unsigned long long)(
164 (long long)key2.key -
165 (long long)key1.key + 1) :
166 (unsigned long long)(
167 (unsigned long)key2.key) -
168 (unsigned long long)((unsigned long)key1.key) + 1;
172 { return span( minKey, maxKey ); }
174 HostType *typeSubsumes( long long maxVal )
176 for ( int i = 0; i < hostLang->numHostTypes; i++ ) {
177 if ( maxVal <= hostLang->hostTypes[i].maxVal )
178 return hostLang->hostTypes + i;
183 HostType *typeSubsumes( bool isSigned, long long maxVal )
185 for ( int i = 0; i < hostLang->numHostTypes; i++ ) {
186 if ( ( isSigned && hostLang->hostTypes[i].isSigned || !isSigned ) &&
187 maxVal <= hostLang->hostTypes[i].maxVal )
188 return hostLang->hostTypes + i;
194 extern KeyOps *keyOps;
196 inline bool operator<( const Key key1, const Key key2 )
198 return keyOps->isSigned ? key1.key < key2.key :
199 (unsigned long)key1.key < (unsigned long)key2.key;
202 inline bool operator<=( const Key key1, const Key key2 )
204 return keyOps->isSigned ? key1.key <= key2.key :
205 (unsigned long)key1.key <= (unsigned long)key2.key;
208 inline bool operator>( const Key key1, const Key key2 )
210 return keyOps->isSigned ? key1.key > key2.key :
211 (unsigned long)key1.key > (unsigned long)key2.key;
214 inline bool operator>=( const Key key1, const Key key2 )
216 return keyOps->isSigned ? key1.key >= key2.key :
217 (unsigned long)key1.key >= (unsigned long)key2.key;
220 inline bool operator==( const Key key1, const Key key2 )
222 return key1.key == key2.key;
225 inline bool operator!=( const Key key1, const Key key2 )
227 return key1.key != key2.key;
230 /* Decrement. Needed only for ranges. */
231 inline void Key::decrement()
233 key = keyOps->isSigned ? key - 1 : ((unsigned long)key)-1;
236 /* Increment. Needed only for ranges. */
237 inline void Key::increment()
239 key = keyOps->isSigned ? key+1 : ((unsigned long)key)+1;
242 inline long long Key::getLongLong() const
244 return keyOps->isSigned ? (long long)key : (long long)(unsigned long)key;
247 inline Key operator+(const Key key1, const Key key2)
249 /* FIXME: must be made aware of isSigned. */
250 return Key( key1.key + key2.key );
253 inline Key operator-(const Key key1, const Key key2)
255 /* FIXME: must be made aware of isSigned. */
256 return Key( key1.key - key2.key );
259 inline long operator&(const Key key1, const Key key2)
261 /* FIXME: must be made aware of isSigned. */
262 return key1.key & key2.key;
265 inline Key operator/(const Key key1, const Key key2)
267 /* FIXME: must be made aware of isSigned. */
268 return key1.key / key2.key;
271 #endif /* _COMMON_H */