1 | \chapter{\texorpdfstring{\CFA}{Cforall} Enumeration} |
---|
2 | |
---|
3 | \CFA extends C-Style enumeration by adding a number of new features that bring enumerations in line with other modern programming languages. |
---|
4 | Any enumeration extensions must be intuitive to C programmers in syntax and semantics. |
---|
5 | The following sections detail my new contributions to enumerations in \CFA. |
---|
6 | |
---|
7 | |
---|
8 | \section{Syntax} |
---|
9 | |
---|
10 | \CFA extends the C enumeration declaration \see{\VRef{s:CEnumeration}} by parameterizing with a type (like a generic type) and adding Plan-9 inheritance \see{\VRef{s:CFAInheritance}} using an @inline@ to another enumeration type. |
---|
11 | \begin{cfa}[identifierstyle=\linespread{0.9}\it] |
---|
12 | $\it enum$-specifier: |
---|
13 | enum @(type-specifier$\(_{opt}\)$)@ identifier$\(_{opt}\)$ { cfa-enumerator-list } |
---|
14 | enum @(type-specifier$\(_{opt}\)$)@ identifier$\(_{opt}\)$ { cfa-enumerator-list , } |
---|
15 | enum @(type-specifier$\(_{opt}\)$)@ identifier |
---|
16 | cfa-enumerator-list: |
---|
17 | cfa-enumerator |
---|
18 | cfa-enumerator-list, cfa-enumerator |
---|
19 | cfa-enumerator: |
---|
20 | enumeration-constant |
---|
21 | @inline $\color{red}enum$-type-name@ |
---|
22 | enumeration-constant = constant-expression |
---|
23 | \end{cfa} |
---|
24 | |
---|
25 | |
---|
26 | \section{Operations} |
---|
27 | |
---|
28 | \CFA enumerations have access to the three enumerations properties \see{\VRef{s:Terminology}}: label, order (position), and value via three overloaded functions @label@, @posn@, and @value@ \see{\VRef{c:trait} for details}. |
---|
29 | \CFA auto-generates these functions for every \CFA enumeration. |
---|
30 | \begin{cfa} |
---|
31 | enum(int) E { A = 3 } e = A; |
---|
32 | sout | A | @label@( A ) | @posn@( A ) | @value@( A ); |
---|
33 | sout | e | @label@( e ) | @posn@( e ) | @value@( e ); |
---|
34 | A A 0 3 |
---|
35 | A A 0 3 |
---|
36 | \end{cfa} |
---|
37 | For output, the default is to print the label. |
---|
38 | An alternate way to get an enumerator's position is to cast it to @int@. |
---|
39 | \begin{cfa} |
---|
40 | sout | A | label( A ) | @(int)A@ | value( A ); |
---|
41 | sout | A | label( A ) | @(int)A@ | value( A ); |
---|
42 | A A @0@ 3 |
---|
43 | A A @0@ 3 |
---|
44 | \end{cfa} |
---|
45 | Finally, \CFA introduces an additional enumeration pseudo-function @countof@ (like @sizeof@, @typeof@) that returns the number of enumerators in an enumeration. |
---|
46 | \begin{cfa} |
---|
47 | enum(int) E { A, B, C, D } e; |
---|
48 | countof( E ); // 4, type argument |
---|
49 | countof( e ); // 4, variable argument |
---|
50 | \end{cfa} |
---|
51 | This built-in function replaces the C idiom for automatically computing the number of enumerators \see{\VRef{s:Usage}}. |
---|
52 | \begin{cfa} |
---|
53 | enum E { A, B, C, D, @N@ }; // N == 4 |
---|
54 | \end{cfa} |
---|
55 | |
---|
56 | The underlying representation of \CFA enumeration object is its position, saved as an integral type. |
---|
57 | Therefore, the size of a \CFA enumeration is consistent with a C enumeration. |
---|
58 | Attribute function @posn@ performs type substitution on an expression from \CFA type to an integral type. |
---|
59 | The label and value of an enumerator are stored in a global data structure for each enumeration, where attribute functions @label@/@value@ map an \CFA enumeration object to the corresponding data. |
---|
60 | These operations do not apply to C Enums because backward compatibility means the necessary backing data structures cannot be supplied. |
---|
61 | |
---|
62 | |
---|
63 | \section{Opaque Enumeration} |
---|
64 | \label{s:OpaqueEnum} |
---|
65 | |
---|
66 | When an enumeration type is empty. it is an \newterm{opaque} enumeration. |
---|
67 | \begin{cfa} |
---|
68 | enum@()@ Mode { O_RDONLY, O_WRONLY, O_CREAT, O_TRUNC, O_APPEND }; |
---|
69 | \end{cfa} |
---|
70 | Here, the compiler chooses the internal representation, which is hidden, so the enumerators cannot be initialized. |
---|
71 | Compared to the C enum, opaque enums are more restrictive regarding typing and cannot be implicitly converted to integers. |
---|
72 | \begin{cfa} |
---|
73 | Mode mode = O_RDONLY; |
---|
74 | int www @=@ mode; $\C{// disallowed}$ |
---|
75 | \end{cfa} |
---|
76 | Opaque enumerations have only two attribute properties, @label@ and @posn@. |
---|
77 | \begin{cfa} |
---|
78 | char * s = label( O_TRUNC ); $\C{// "O\_TRUNC"}$ |
---|
79 | int open = posn( O_WRONLY ); $\C{// 1}$ |
---|
80 | \end{cfa} |
---|
81 | Equality and relational operations are available. |
---|
82 | \begin{cfa} |
---|
83 | if ( mode @==@ O_CREAT ) ... |
---|
84 | bool b = mode @<@ O_APPEND; |
---|
85 | \end{cfa} |
---|
86 | |
---|
87 | |
---|
88 | \section{Typed Enumeration} |
---|
89 | \label{s:EnumeratorTyping} |
---|
90 | |
---|
91 | When an enumeration type is specified, all enumerators have that type and can be initialized with constants of that type or compile-time convertible to that type. |
---|
92 | Figure~\ref{f:EumeratorTyping} shows a series of examples illustrating that all \CFA types can be used with an enumeration, and each type's values are used to set the enumerator constants. |
---|
93 | Note the use of the synonyms @Liz@ and @Beth@ in the last declaration. |
---|
94 | Because enumerators are constants, the enumeration type is implicitly @const@, so all the enumerator types in Figure~\ref{f:EumeratorTyping} are logically rewritten with @const@. |
---|
95 | |
---|
96 | \begin{figure} |
---|
97 | \begin{cfa} |
---|
98 | // integral |
---|
99 | enum( @char@ ) Currency { Dollar = '$\textdollar$', Cent = '$\textcent$', Yen = '$\textyen$', Pound = '$\textsterling$', Euro = 'E' }; |
---|
100 | enum( @signed char@ ) srgb { Red = -1, Green = 0, Blue = 1 }; |
---|
101 | enum( @long long int@ ) BigNum { X = 123_456_789_012_345, Y = 345_012_789_456_123 }; |
---|
102 | // non-integral |
---|
103 | enum( @double@ ) Math { PI_2 = 1.570796, PI = 3.141597, E = 2.718282 }; |
---|
104 | enum( @_Complex@ ) Plane { X = 1.5+3.4i, Y = 7+3i, Z = 0+0.5i }; |
---|
105 | // pointer |
---|
106 | enum( @char *@ ) Name { Fred = "FRED", Mary = "MARY", Jane = "JANE" }; |
---|
107 | int i, j, k; |
---|
108 | enum( @int *@ ) ptr { I = &i, J = &j, K = &k }; |
---|
109 | enum( @int &@ ) ref { I = i, J = j, K = k }; |
---|
110 | // tuple |
---|
111 | enum( @[int, int]@ ) { T = [ 1, 2 ] }; $\C{// new \CFA type}$ |
---|
112 | // function |
---|
113 | void f() {...} void g() {...} |
---|
114 | enum( @void (*)()@ ) funs { F = f, G = g }; |
---|
115 | // aggregate |
---|
116 | struct Person { char * name; int age, height; }; |
---|
117 | enum( @Person@ ) friends { @Liz@ = { "ELIZABETH", 22, 170 }, @Beth@ = Liz, |
---|
118 | Jon = { "JONATHAN", 35, 190 } }; |
---|
119 | \end{cfa} |
---|
120 | % synonym feature unimplemented |
---|
121 | \caption{Enumerator Typing} |
---|
122 | \label{f:EumeratorTyping} |
---|
123 | \end{figure} |
---|
124 | |
---|
125 | An advantage of the typed enumerations is eliminating the \emph{harmonizing} problem between an enumeration and companion data \see{\VRef{s:Usage}}: |
---|
126 | \begin{cfa} |
---|
127 | enum( char * ) integral_types { |
---|
128 | chr = "char", schar = "signed char", uschar = "unsigned char", |
---|
129 | sshort = "signed short int", ushort = "unsigned short int", |
---|
130 | sint = "signed int", usint = "unsigned int", |
---|
131 | ... |
---|
132 | }; |
---|
133 | \end{cfa} |
---|
134 | Note that the enumeration type can be a structure (see @Person@ in Figure~\ref{f:EumeratorTyping}), so it is possible to have the equivalent of multiple arrays of companion data using an array of structures. |
---|
135 | |
---|
136 | While the enumeration type can be any C aggregate, the aggregate's \CFA constructors are \emph{not} used to evaluate an enumerator's value. |
---|
137 | \CFA enumeration constants are compile-time values (static); |
---|
138 | calling constructors happens at runtime (dynamic). |
---|
139 | |
---|
140 | |
---|
141 | \section{Value Conversion} |
---|
142 | |
---|
143 | C has an implicit type conversion from an enumerator to its base type @int@. |
---|
144 | Correspondingly, \CFA has an implicit conversion from a typed enumerator to its base type, allowing typed enumeration to be seamlessly used as the value of its base type |
---|
145 | For example, using type @Currency@ in \VRef[Figure]{f:EumeratorTyping}: |
---|
146 | \begin{cfa} |
---|
147 | char currency = Dollar; $\C{// implicit conversion to base type}$ |
---|
148 | void foo( char ); |
---|
149 | foo( Dollar ); $\C{// implicit conversion to base type}$ |
---|
150 | \end{cfa} |
---|
151 | The implicit conversion induces a \newterm{value cost}, which is a new category (8 tuple) in \CFA's conversion cost model \see{\VRef{s:ConversionCost}} to disambiguate function overloading over a \CFA enumeration and its base type. |
---|
152 | \begin{cfa} |
---|
153 | void baz( char ch ); $\C{// (1)}$ |
---|
154 | void baz( Currency cu ); $\C{// (2)}$ |
---|
155 | baz( Dollar ); |
---|
156 | \end{cfa} |
---|
157 | While both @baz@ functions are applicable to the enumerator @Dollar@, @candidate (1)@ comes with a @value@ cost for the conversion to the enumeration's base type, while @candidate (2)@ has @zero@ cost. |
---|
158 | Hence, \CFA chooses the exact match. |
---|
159 | Value cost is defined to be a more significant factor than an @unsafe@ but less than the other conversion costs: @(unsafe,@ {\color{red}@value@}@, poly, safe, sign, vars, specialization,@ @reference)@. |
---|
160 | \begin{cfa} |
---|
161 | void bar( @int@ ); |
---|
162 | Math x = PI; $\C{// (1)}$ |
---|
163 | double x = 5.5; $\C{// (2)}$ |
---|
164 | bar( x ); $\C{// costs (1, 0, 0, 0, 0, 0, 0, 0) or (0, 1, 0, 0, 0, 0, 0, 0)}$ |
---|
165 | \end{cfa} |
---|
166 | Here, the candidate (1) has a @value@ conversion cost to convert to the base type, while the candidate (2) has an @unsafe@ conversion from @double@ to @int@, |
---|
167 | which is a more expensive conversion. |
---|
168 | Hence, @bar( x )@ resolves @x@ as type @Math@. |
---|
169 | |
---|
170 | % \begin{cfa} |
---|
171 | % forall(T | @CfaEnum(T)@) void bar(T); |
---|
172 | % |
---|
173 | % bar(a); $\C{// (3), with cost (0, 0, 1, 0, 0, 0, 0, 0)}$ |
---|
174 | % \end{cfa} |
---|
175 | % % @Value@ is designed to be less significant than @poly@ to allow function being generic over \CFA enumeration (see ~\ref{c:trait}). |
---|
176 | % Being generic over @CfaEnum@ traits (a pre-defined interface for \CFA enums) is a practice in \CFA to implement functions over \CFA enumerations, as will see in chapter~\ref{c:trait}. |
---|
177 | % @Value@ is a being a more significant cost than @poly@ implies if a overloaeded function defined for @CfaEnum@ (and other generic type), \CFA always try to resolve it as a @CfaEnum@, rather to insert a @value@ conversion. |
---|
178 | |
---|
179 | |
---|
180 | \section{Auto Initialization} |
---|
181 | \CFA extends C's auto-initialization scheme to \CFA enumeration. For an enumeration type with base type T, the initialization scheme is the following: |
---|
182 | \begin{enumerate} |
---|
183 | \item the first enumerator is initialized with @T@'s @zero_t@. |
---|
184 | \item Every other enumerator is initialized with its previous enumerator's value "+1", where "+1" is defined in terms of overloaded operator @?+?(T, one_t)@. |
---|
185 | \end{enumerate} |
---|
186 | |
---|
187 | \begin{cfa} |
---|
188 | struct S { int i; }; |
---|
189 | S ?+?( S & s, one_t ) { return s.i++; } |
---|
190 | void ?{}( S & s, zero_t ) { s.i = 0; } |
---|
191 | enum(S) E { A, B, C, D }; |
---|
192 | \end{cfa} |
---|
193 | |
---|
194 | \section{Subset} |
---|
195 | |
---|
196 | An enumeration's type can be another enumeration. |
---|
197 | \begin{cfa} |
---|
198 | enum( char ) Letter { A = 'A', ..., Z = 'Z' }; |
---|
199 | enum( @Letter@ ) Greek { Alph = @A@, Beta = @B@, Gamma = @G@, ..., Zeta = @Z@ }; // alphabet intersection |
---|
200 | \end{cfa} |
---|
201 | Enumeration @Greek@ may have more or less enumerators than @Letter@, but its enumerator values \emph{must} be from @Letter@. |
---|
202 | Therefore, the set of @Greek@ enumerator values in a subset of the @Letter@ enumerator values. |
---|
203 | @Letter@ is type compatible with enumeration @Letter@ because value conversions are inserted whenever @Letter@ is used in place of @Greek@. |
---|
204 | \begin{cfa} |
---|
205 | Letter l = A; $\C{// allowed}$ |
---|
206 | Greek g = Alph; $\C{// allowed}$ |
---|
207 | l = Alph; $\C{// allowed, conversion to base type}$ |
---|
208 | g = A; $\C{// {\color{red}disallowed}}$ |
---|
209 | void foo( Letter ); |
---|
210 | foo( Beta ); $\C{// allowed, conversion to base type}$ |
---|
211 | void bar( Greek ); |
---|
212 | bar( A ); $\C{// {\color{red}disallowed}}$ |
---|
213 | \end{cfa} |
---|
214 | Hence, @Letter@ enumerators are not type-compatible with the @Greek@ enumeration, but the reverse is true. |
---|
215 | |
---|
216 | |
---|
217 | \section{Inheritance} |
---|
218 | \label{s:CFAInheritance} |
---|
219 | |
---|
220 | \CFA Plan-9 inheritance may be used with \CFA enumerations, where Plan-9 inheritance is containment inheritance with implicit unscoping (like a nested unnamed @struct@/@union@ in C). |
---|
221 | Containment is nominative: an enumeration inherits all enumerators from another enumeration by declaring an @inline statement@ in its enumerator lists. |
---|
222 | \begin{cfa} |
---|
223 | enum( char * ) Names { /* $\see{\VRef[Figure]{f:EumeratorTyping}}$ */ }; |
---|
224 | enum( char * ) Names2 { @inline Names@, Jack = "JACK", Jill = "JILL" }; |
---|
225 | enum( char * ) Names3 { @inline Names2@, Sue = "SUE", Tom = "TOM" }; |
---|
226 | \end{cfa} |
---|
227 | In the preceding example, @Names2@ is defined with five enumerators, three of which are from @Name@ through containment, and two are self-declared. |
---|
228 | @Names3@ inherits all five members from @Names2@ and declares two additional enumerators. |
---|
229 | Hence, enumeration inheritance forms a subset relationship. |
---|
230 | Specifically, the inheritance relationship for the example above is: |
---|
231 | \begin{cfa} |
---|
232 | Names $\(\subset\)$ Names2 $\(\subset\)$ Names3 $\C{// enum type of Names}$ |
---|
233 | \end{cfa} |
---|
234 | |
---|
235 | Inheritance can be nested, and a \CFA enumeration can inline enumerators from more than one \CFA enumeration, forming a tree-like hierarchy. |
---|
236 | However, the uniqueness of the enumeration name applies to enumerators, including those from supertypes, meaning an enumeration cannot name an enumerator with the same label as its subtype's members or inherits |
---|
237 | from multiple enumeration that has overlapping enumerator labels. Consequently, a new type cannot inherit from an enumeration and its supertype or two enumerations with a |
---|
238 | common supertype (the diamond problem) since such would unavoidably introduce duplicate enumerator labels. |
---|
239 | |
---|
240 | The base type must be consistent between subtype and supertype. |
---|
241 | When an enumeration inherits enumerators from another enumeration, it copies the enumerators' @value@ and @label@, even if the @value@ is auto-initialized. |
---|
242 | However, the position of the underlying representation is the order of the enumerator in the new enumeration. |
---|
243 | \begin{cfa} |
---|
244 | enum() E1 { B }; $\C{// B}$ |
---|
245 | enum() E2 { C, D }; $\C{// C D}$ |
---|
246 | enum() E3 { inline E1, inline E2, E }; $\C{// {\color{red}[\(_{E1}\)} B {\color{red}]} {\color{red}[\(_{E2}\)} C D {\color{red}]} E}$ |
---|
247 | enum() E4 { A, inline E3, F}; $\C{// A {\color{blue}[\(_{E3}\)} {\color{red}[\(_{E1}\)} B {\color{red}]} {\color{red}[\(_{E2}\)} C D {\color{red}]} E {\color{blue}]} F}$ |
---|
248 | \end{cfa} |
---|
249 | In the example, @B@ is at position 0 in @E1@ and @E3@, but position 1 in @E4@ as @A@ takes position 0 in @E4@. |
---|
250 | @C@ is at position 0 in @E2@, 1 in @E3@, and 2 in @E4@. |
---|
251 | @D@ is at position 1 in @E2@, 2 in @E3@, and 3 in @E4@. |
---|
252 | |
---|
253 | A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost, called \newterm{enumeration conversion}. |
---|
254 | \begin{cfa} |
---|
255 | enum E2 e2 = C; |
---|
256 | posn( e2 ); $\C[1.75in]{// 0}$ |
---|
257 | enum E3 e3 = e2; $\C{// Assignment with enumeration conversion E2 to E3}$ |
---|
258 | posn( e2 ); $\C{// 1 cost}$ |
---|
259 | void foo( E3 e ); |
---|
260 | foo( e2 ); $\C{// Type compatible with enumeration conversion E2 to E3}$ |
---|
261 | posn( (E3)e2 ); $\C{// Explicit cast with enumeration conversion E2 to E3}$ |
---|
262 | E3 e31 = B; $\C{// No conversion: E3.B}$ |
---|
263 | posn( e31 ); $\C{// 0 cost}\CRT$ |
---|
264 | \end{cfa} |
---|
265 | The last expression is unambiguous. |
---|
266 | While both @E2.B@ and @E3.B@ are valid candidates, @E2.B@ has an associated safe cost and @E3.B@ needs no conversion (@zero@ cost). |
---|
267 | \CFA selects the lowest cost candidate @E3.B@. |
---|
268 | |
---|
269 | For the given function prototypes, the following calls are valid. |
---|
270 | \begin{cquote} |
---|
271 | \begin{tabular}{ll} |
---|
272 | \begin{cfa} |
---|
273 | void f( Names ); |
---|
274 | void g( Names2 ); |
---|
275 | void h( Names3 ); |
---|
276 | void j( const char * ); |
---|
277 | \end{cfa} |
---|
278 | & |
---|
279 | \begin{cfa} |
---|
280 | f( Fred ); |
---|
281 | g( Fred ); g( Jill ); |
---|
282 | h( Fred ); h( Jill ); h( Sue ); |
---|
283 | j( Fred ); j( Jill ); j( Sue ); j( "WILL" ); |
---|
284 | \end{cfa} |
---|
285 | \end{tabular} |
---|
286 | \end{cquote} |
---|
287 | Note, the validity of calls is the same for call-by-reference as for call-by-value, and @const@ restrictions are the same as for other types. |
---|
288 | |
---|
289 | |
---|
290 | \subsection{Offset Calculation} |
---|
291 | |
---|
292 | As discussed in \VRef{s:OpaqueEnum}, \CFA chooses position as a representation of a \CFA enumeration variable. |
---|
293 | When a cast or implicit conversion moves an enumeration from subtype to supertype, the position can be unchanged or increase. |
---|
294 | \CFA determines the position offset with an \newterm{offset calculation} function. |
---|
295 | |
---|
296 | \begin{figure} |
---|
297 | \begin{cfa} |
---|
298 | struct Enumerator; |
---|
299 | struct CFAEnum { vector<variant<CFAEnum, Enumerator>> members; string name; }; |
---|
300 | inline static bool operator==(CFAEnum& lhs, CFAEnum& rhs) { return lhs.name == rhs.name; } |
---|
301 | pair<bool, int> calculateEnumOffset(CFAEnum src, CFAEnum dst) { |
---|
302 | int offset = 0; |
---|
303 | if ( src == dst ) return make_pair(true, 0); |
---|
304 | for ( auto v : dst.members ) { |
---|
305 | if ( holds_alternative<Enumerator>(v) ) { |
---|
306 | offset++; |
---|
307 | } else { |
---|
308 | auto m = get<CFAEnum>(v); |
---|
309 | if ( m == src ) @return@ make_pair( true, offset ); |
---|
310 | auto dist = calculateEnumOffset( src, m ); |
---|
311 | if ( dist.first ) { |
---|
312 | @return@ make_pair( true, offset + dist.second ); |
---|
313 | } else { |
---|
314 | offset += dist.second; |
---|
315 | } |
---|
316 | } |
---|
317 | } |
---|
318 | @return@ make_pair( false, offset ); |
---|
319 | } |
---|
320 | \end{cfa} |
---|
321 | \caption{Compute Offset from Subtype Enumeration to a Supertype} |
---|
322 | \label{s:OffsetSubtypeSuperType} |
---|
323 | \end{figure} |
---|
324 | |
---|
325 | Figure~\ref{s:OffsetSubtypeSuperType} shows an outline of the offset calculation in \CC. |
---|
326 | Structure @CFAEnum@ represents the \CFA enumeration with a vector of variants of @CFAEnum@ or @Enumerator@. |
---|
327 | The algorithm takes two @CFAEnums@ parameters, @src@ and @dst@, with @src@ being the type of expression the conversion applies to, and @dst@ being the type the expression is cast to. |
---|
328 | The algorithm iterates over the members in @dst@ to find @src@. |
---|
329 | If a member is an enumerator of @dst@, the positions of all subsequent members are incremented by one. |
---|
330 | If the current member is @dst@, the function returns true indicating \emph{found} and the accumulated offset. |
---|
331 | Otherwise, the algorithm recurses into the current @CFAEnum@ @m@ to check if its @src@ is convertible to @m@. |
---|
332 | If @src@ is convertible to the current member @m@, this means @src@ is a subtype-of-subtype of @dst@. |
---|
333 | The offset between @src@ and @dst@ is the sum of the offset of @m@ in @dst@ and the offset of @src@ in @m@. |
---|
334 | If @src@ is not a subtype of @m@, the loop continues but with the offset shifted by the size of @m@. |
---|
335 | If the loop ends, than @src@ is not convertible to @dst@, and false is returned. |
---|
336 | |
---|
337 | |
---|
338 | \section{Control Structures} |
---|
339 | |
---|
340 | Enumerators can be used in multiple contexts. |
---|
341 | In most programming languages, an enumerator is implicitly converted to its value (like a typed macro substitution). |
---|
342 | However, enumerator synonyms and typed enumerations make this implicit conversion to value incorrect in some contexts. |
---|
343 | In these contexts, a programmer's intuition assumes an implicit conversion to position. |
---|
344 | |
---|
345 | For example, an intuitive use of enumerations is with the \CFA @switch@/@choose@ statement, where @choose@ performs an implicit @break@ rather than a fall-through at the end of a @case@ clause. |
---|
346 | (For this discussion, ignore the fact that @case@ requires a compile-time constant.) |
---|
347 | \begin{cfa}[belowskip=0pt] |
---|
348 | enum Count { First, Second, Third, Fourth }; |
---|
349 | Count e; |
---|
350 | \end{cfa} |
---|
351 | \begin{cquote} |
---|
352 | \setlength{\tabcolsep}{15pt} |
---|
353 | \noindent |
---|
354 | \begin{tabular}{@{}ll@{}} |
---|
355 | \begin{cfa}[aboveskip=0pt] |
---|
356 | |
---|
357 | choose( e ) { |
---|
358 | case @First@: ...; |
---|
359 | case @Second@: ...; |
---|
360 | case @Third@: ...; |
---|
361 | case @Fourth@: ...; |
---|
362 | } |
---|
363 | \end{cfa} |
---|
364 | & |
---|
365 | \begin{cfa}[aboveskip=0pt] |
---|
366 | // rewrite |
---|
367 | choose( @value@( e ) ) { |
---|
368 | case @value@( First ): ...; |
---|
369 | case @value@( Second ): ...; |
---|
370 | case @value@( Third ): ...; |
---|
371 | case @value@( Fourth ): ...; |
---|
372 | } |
---|
373 | \end{cfa} |
---|
374 | \end{tabular} |
---|
375 | \end{cquote} |
---|
376 | Here, the intuitive code on the left is implicitly transformed into the standard implementation on the right, using the value of the enumeration variable and enumerators. |
---|
377 | However, this implementation is fragile, \eg if the enumeration is changed to: |
---|
378 | \begin{cfa} |
---|
379 | enum Count { First, Second, Third @= First@, Fourth }; |
---|
380 | \end{cfa} |
---|
381 | making @Third == First@ and @Fourth == Second@, causing a compilation error because of duplicate @case@ clauses. |
---|
382 | To better match with programmer intuition, \CFA toggles between value and position semantics depending on the language context. |
---|
383 | For conditional clauses and switch statements, \CFA uses the robust position implementation. |
---|
384 | \begin{cfa} |
---|
385 | if ( @posn@( e ) < posn( Third ) ) ... |
---|
386 | choose( @posn@( e ) ) { |
---|
387 | case @posn@( First ): ...; |
---|
388 | case @posn@( Second ): ...; |
---|
389 | case @posn@( Third ): ...; |
---|
390 | case @posn@( Fourth ): ...; |
---|
391 | } |
---|
392 | \end{cfa} |
---|
393 | |
---|
394 | \CFA provides a special form of for-control for enumerating through an enumeration, where the range is a type. |
---|
395 | \begin{cfa} |
---|
396 | for ( cx; @Count@ ) { sout | cx | nonl; } sout | nl; |
---|
397 | for ( cx; ~= Count ) { sout | cx | nonl; } sout | nl; |
---|
398 | for ( cx; -~= Count ) { sout | cx | nonl; } sout | nl; |
---|
399 | First Second Third Fourth |
---|
400 | First Second Third Fourth |
---|
401 | Fourth Third Second First |
---|
402 | \end{cfa} |
---|
403 | The enumeration type is syntax sugar for looping over all enumerators and assigning each enumerator to the loop index, whose type is inferred from the range type. |
---|
404 | The prefix @+~=@ or @-~=@ iterate forward or backwards through the inclusive enumeration range, where no prefix defaults to @+~=@. |
---|
405 | |
---|
406 | C has an idiom for @if@ and loop predicates of comparing the predicate result ``not equal to 0''. |
---|
407 | \begin{cfa} |
---|
408 | if ( x + y /* != 0 */ ) ... |
---|
409 | while ( p /* != 0 */ ) ... |
---|
410 | \end{cfa} |
---|
411 | This idiom extends to enumerations because there is a boolean conversion in terms of the enumeration value, if and only if such a conversion is available. |
---|
412 | For example, such a conversion exists for all numerical types (integral and floating-point). |
---|
413 | It is possible to explicitly extend this idiom to any typed enumeration by overloading the @!=@ operator. |
---|
414 | \begin{cfa} |
---|
415 | bool ?!=?( Name n, zero_t ) { return n != Fred; } |
---|
416 | Name n = Mary; |
---|
417 | if ( n ) ... // result is true |
---|
418 | \end{cfa} |
---|
419 | Specialize meanings are also possible. |
---|
420 | \begin{cfa} |
---|
421 | enum(int) ErrorCode { Normal = 0, Slow = 1, Overheat = 1000, OutOfResource = 1001 }; |
---|
422 | bool ?!=?( ErrorCode ec, zero_t ) { return ec >= Overheat; } |
---|
423 | ErrorCode code = ...; |
---|
424 | if ( code ) { problem(); } |
---|
425 | \end{cfa} |
---|
426 | |
---|
427 | |
---|
428 | \section{Dimension} |
---|
429 | |
---|
430 | \VRef{s:EnumeratorTyping} introduces the harmonizing problem between an enumeration and secondary information. |
---|
431 | When possible, using a typed enumeration for the secondary information is the best approach. |
---|
432 | However, there are times when combining these two types is not possible. |
---|
433 | For example, the secondary information might precede the enumeration and/or its type is needed directly to declare parameters of functions. |
---|
434 | In these cases, having secondary arrays of the enumeration size are necessary. |
---|
435 | |
---|
436 | To support some level of harmonizing in these cases, an array dimension can be defined using an enumerator type, and the enumerators used as subscripts. |
---|
437 | \begin{cfa} |
---|
438 | enum E1 { A, B, C, N }; // possibly predefined |
---|
439 | enum(int) E2 { A, B, C }; |
---|
440 | float H1[N] = { [A] :$\footnotemark$ 3.4, [B] : 7.1, [C] : 0.01 }; // C |
---|
441 | float H2[@E2@] = { [A] : 3.4, [B] : 7.1, [C] : 0.01 }; // CFA |
---|
442 | \end{cfa} |
---|
443 | \footnotetext{C uses symbol \lstinline{'='} for designator initialization, but \CFA changes it to \lstinline{':'} because of problems with tuple syntax.} |
---|
444 | This approach is also necessary for a predefined typed enumeration (unchangeable), when additional secondary-information need to be added. |
---|
445 | The array subscript operator, namely @?[?]@, is overloaded so that when a \CFA enumerator is used as an array index, it implicitly converts to its position over value to sustain data harmonization. |
---|
446 | This behaviour can be reverted by explicit overloading: |
---|
447 | \begin{cfa} |
---|
448 | float ?[?]( float * arr, E2 index ) { return arr[ value( index ) ]; } |
---|
449 | \end{cfa} |
---|
450 | While enumerator labels @A@, @B@ and @C@ are being defined twice in different enumerations, they are unambiguous within the context. |
---|
451 | Designators in @H1@ are unambiguous becasue @E2@ has a @value@ cost to @int@, which is more expensive than @safe@ cost from C-Enum @E1@ to @int@. |
---|
452 | Designators in @H2@ are resolved as @E2@ because when a \CFA enumeration type is being used as an array dimension, \CFA adds the enumeration type to the initializer's resolution context. |
---|
453 | |
---|
454 | |
---|
455 | \section{I/O} |
---|
456 | |
---|
457 | As seen in multiple examples, \CFA enumerations can be printed and the default property printed is the enumerator's label, which is similar in other programming languages. |
---|
458 | However, very few programming languages provide a mechanism to read in enumerator values. |
---|
459 | Even the @boolean@ type in many languages does not have a mechanism for input using the enumerators @true@ or @false@. |
---|
460 | \VRef[Figure]{f:EnumerationI/O} show \CFA enumeration input based on the enumerator labels. |
---|
461 | When the enumerator labels are packed together in the input stream, the input algorithm scans for the longest matching string. |
---|
462 | For basic types in \CFA, the rule is that the same constants used to initialize a variable in a program are available to initialize a variable using input, where string constants can be quoted or unquoted. |
---|
463 | |
---|
464 | \begin{figure} |
---|
465 | \begin{cquote} |
---|
466 | \setlength{\tabcolsep}{15pt} |
---|
467 | \begin{tabular}{@{}ll@{}} |
---|
468 | \begin{cfa} |
---|
469 | int main() { |
---|
470 | enum(int ) E { BBB = 3, AAA, AA, AB, B }; |
---|
471 | E e; |
---|
472 | |
---|
473 | for () { |
---|
474 | try { |
---|
475 | @sin | e@; |
---|
476 | } catch( missing_data * ) { |
---|
477 | sout | "missing data"; |
---|
478 | continue; // try again |
---|
479 | } |
---|
480 | if ( eof( sin ) ) break; |
---|
481 | sout | e | "= " | value( e ); |
---|
482 | } |
---|
483 | } |
---|
484 | \end{cfa} |
---|
485 | & |
---|
486 | \begin{cfa} |
---|
487 | $\rm input$ |
---|
488 | BBBABAAAAB |
---|
489 | BBB AAA AA AB B |
---|
490 | |
---|
491 | $\rm output$ |
---|
492 | BBB = 3 |
---|
493 | AB = 6 |
---|
494 | AAA = 4 |
---|
495 | AB = 6 |
---|
496 | BBB = 3 |
---|
497 | AAA = 4 |
---|
498 | AA = 5 |
---|
499 | AB = 6 |
---|
500 | B = 7 |
---|
501 | |
---|
502 | \end{cfa} |
---|
503 | \end{tabular} |
---|
504 | \end{cquote} |
---|
505 | \caption{Enumeration I/O} |
---|
506 | \label{f:EnumerationI/O} |
---|
507 | \end{figure} |
---|
508 | |
---|
509 | |
---|
510 | \section{Planet Example} |
---|
511 | |
---|
512 | \VRef[Figure]{f:PlanetExample} shows an archetypal enumeration example illustrating most of the \CFA enumeration features. |
---|
513 | @Planet@ is an enumeration of type @MR@. |
---|
514 | Each planet enumerator is initialized to a specific mass/radius, @MR@, value. |
---|
515 | The unnamed enumeration provides the gravitational-constant enumerator @G@. |
---|
516 | Function @surfaceGravity@ uses the @with@ clause to remove @p@ qualification from fields @mass@ and @radius@. |
---|
517 | The program main uses the pseudo function @countof@ to obtain the number of enumerators in @Planet@, and safely converts the random value into a @Planet@ enumerator using @fromInt@. |
---|
518 | The resulting random orbital-body is used in a @choose@ statement. |
---|
519 | The enumerators in the @case@ clause use the enumerator position for testing. |
---|
520 | The prints use @label@ to print an enumerator's name. |
---|
521 | Finally, a loop enumerates through the planets computing the weight on each planet for a given earth mass. |
---|
522 | The print statement does an equality comparison with an enumeration variable and enumerator (@p == MOON@). |
---|
523 | |
---|
524 | \begin{figure} |
---|
525 | \small |
---|
526 | \begin{cfa} |
---|
527 | struct MR { double mass, radius; }; $\C[3.5in]{// planet definition}$ |
---|
528 | enum( @MR@ ) Planet { $\C{// typed enumeration}$ |
---|
529 | // mass (kg) radius (km) |
---|
530 | MERCURY = { 0.330_E24, 2.4397_E6 }, |
---|
531 | VENUS = { 4.869_E24, 6.0518_E6 }, |
---|
532 | EARTH = { 5.976_E24, 6.3781_E6 }, |
---|
533 | MOON = { 7.346_E22, 1.7380_E6 }, $\C{// not a planet}$ |
---|
534 | MARS = { 0.642_E24, 3.3972_E6 }, |
---|
535 | JUPITER = { 1898._E24, 71.492_E6 }, |
---|
536 | SATURN = { 568.8_E24, 60.268_E6 }, |
---|
537 | URANUS = { 86.86_E24, 25.559_E6 }, |
---|
538 | NEPTUNE = { 102.4_E24, 24.746_E6 }, |
---|
539 | PLUTO = { 1.303_E22, 1.1880_E6 }, $\C{// not a planet}$ |
---|
540 | }; |
---|
541 | enum( double ) { G = 6.6743_E-11 }; $\C{// universal gravitational constant (m3 kg-1 s-2)}$ |
---|
542 | static double surfaceGravity( Planet p ) @with( p )@ { |
---|
543 | return G * mass / ( radius @\@ 2 ); $\C{// no qualification, exponentiation}$ |
---|
544 | } |
---|
545 | static double surfaceWeight( Planet p, double otherMass ) { |
---|
546 | return otherMass * surfaceGravity( p ); |
---|
547 | } |
---|
548 | int main( int argc, char * argv[] ) { |
---|
549 | if ( argc != 2 ) @exit@ | "Usage: " | argv[0] | "earth-weight"; // terminate program |
---|
550 | double earthWeight = convert( argv[1] ); |
---|
551 | double earthMass = earthWeight / surfaceGravity( EARTH ); |
---|
552 | Planet rp = @fromInt@( prng( @countof@( Planet ) ) ); $\C{// select random orbiting body}$ |
---|
553 | @choose( rp )@ { $\C{// implicit breaks}$ |
---|
554 | case MERCURY, VENUS, EARTH, MARS: |
---|
555 | sout | @rp@ | "is a rocky planet"; |
---|
556 | case JUPITER, SATURN, URANUS, NEPTUNE: |
---|
557 | sout | rp | "is a gas-giant planet"; |
---|
558 | default: |
---|
559 | sout | rp | "is not a planet"; |
---|
560 | } |
---|
561 | for ( @p; Planet@ ) { $\C{// enumerate}\CRT$ |
---|
562 | sout | "Your weight on" | ( @p == MOON@ ? "the" : " " ) | p |
---|
563 | | "is" | wd( 1,1, surfaceWeight( p, earthMass ) ) | "kg"; |
---|
564 | } |
---|
565 | } |
---|
566 | $\$$ planet 100 |
---|
567 | JUPITER is a gas-giant planet |
---|
568 | Your weight on MERCURY is 37.7 kg |
---|
569 | Your weight on VENUS is 90.5 kg |
---|
570 | Your weight on EARTH is 100.0 kg |
---|
571 | Your weight on the MOON is 16.6 kg |
---|
572 | Your weight on MARS is 37.9 kg |
---|
573 | Your weight on JUPITER is 252.8 kg |
---|
574 | Your weight on SATURN is 106.6 kg |
---|
575 | Your weight on URANUS is 90.5 kg |
---|
576 | Your weight on NEPTUNE is 113.8 kg |
---|
577 | Your weight on PLUTO is 6.3 kg |
---|
578 | \end{cfa} |
---|
579 | \caption{Planet Example} |
---|
580 | \label{f:PlanetExample} |
---|
581 | \end{figure} |
---|