1 | \chapter{\CFA Enumeration} |
---|
2 | |
---|
3 | % \CFA supports C enumeration using the same syntax and semantics for backwards compatibility. |
---|
4 | % \CFA also extends C-Style enumeration by adding a number of new features that bring enumerations inline with other modern programming languages. |
---|
5 | % Any enumeration extensions must be intuitive to C programmers both in syntax and semantics. |
---|
6 | % The following sections detail all of my new contributions to enumerations in \CFA. |
---|
7 | \CFA extends the enumeration declaration by parameterizing with a type (like a generic type). |
---|
8 | |
---|
9 | |
---|
10 | \begin{cfa}[caption={CFA Enum},captionpos=b,label={l:CFAEnum}] |
---|
11 | $\it enum$-specifier: |
---|
12 | enum @(type-specifier$\(_{opt}\)$)@ identifier$\(_{opt}\)$ { cfa-enumerator-list } |
---|
13 | enum @(type-specifier$\(_{opt}\)$)@ identifier$\(_{opt}\)$ { cfa-enumerator-list , } |
---|
14 | enum @(type-specifier$\(_{opt}\)$)@ identifier |
---|
15 | cfa-enumerator-list: |
---|
16 | cfa-enumerator |
---|
17 | cfa-enumerator, cfa-enumerator-list |
---|
18 | cfa-enumerator: |
---|
19 | enumeration-constant |
---|
20 | $\it inline$ identifier |
---|
21 | enumeration-constant = expression |
---|
22 | \end{cfa} |
---|
23 | |
---|
24 | A \newterm{\CFA enumeration}, or \newterm{\CFA enum}, has an optional type declaration in the bracket next to the @enum@ keyword. |
---|
25 | Without optional type declarations, the syntax defines \newterm{opaque enums}. |
---|
26 | Otherwise, \CFA enum with type declaration are \newterm{typed enums}. |
---|
27 | |
---|
28 | \section{Opaque Enum} |
---|
29 | \label{s:OpaqueEnum} |
---|
30 | Opaque enum is a special CFA enumeration type, where the internal representation is chosen by the compiler and hidden from users. |
---|
31 | Compared C enum, opaque enums are more restrictive in terms of typing, and cannot be implicitly converted to integers. |
---|
32 | Enumerators of opaque enum cannot have initializer. Declaring initializer in the body of opaque enum results in a compile time error. |
---|
33 | \begin{cfa} |
---|
34 | enum@()@ Planets { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE }; |
---|
35 | |
---|
36 | Planet p = URANUS; |
---|
37 | int i = VENUS; @// Error, VENUS cannot be converted into an integral type |
---|
38 | \end{cfa} |
---|
39 | % Each opaque enum has two @attributes@: @position@ and @label@. \CFA auto-generates @attribute functions@ @posn()@ and @label()@ for every \CFA enum to returns the respective attributes. |
---|
40 | Opaque enumerations have two defining properties: @label@ (name) and @order@ (position), exposed to users by predefined @attribute functions@ , with the following signatures: |
---|
41 | \begin{cfa} |
---|
42 | forall( E ) { |
---|
43 | unsigned posn(E e); |
---|
44 | const char * s label(E e); |
---|
45 | }; |
---|
46 | \end{cfa} |
---|
47 | With polymorphic type parameter E being substituted by enumeration types such as @Planet@. |
---|
48 | |
---|
49 | \begin{cfa} |
---|
50 | unsigned i = posn(VENUS); // 1 |
---|
51 | char * s = label(MARS); // "MARS" |
---|
52 | \end{cfa} |
---|
53 | |
---|
54 | \subsection{Representation} |
---|
55 | The underlying representation of \CFA enumeration object is its order, saved as an integral type. Therefore, the size of a \CFA enumeration is consistent with C enumeration. |
---|
56 | Attribute function @posn@ performs type substitution on an expression from \CFA type to integral type. |
---|
57 | Names of enumerators are stored in a global data structure, with @label@ maps \CFA enumeration object to corresponding data. |
---|
58 | |
---|
59 | \section{Typed Enum} |
---|
60 | \label{s:EnumeratorTyping} |
---|
61 | |
---|
62 | \CFA extends the enumeration declaration by parameterizing with a type (like a generic type), allowing enumerators to be assigned any values from the declared type. |
---|
63 | Figure~\ref{f:EumeratorTyping} shows a series of examples illustrating that all \CFA types can be use with an enumeration and each type's values used to set the enumerator constants. |
---|
64 | Note, the synonyms @Liz@ and @Beth@ in the last declaration. |
---|
65 | 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@. |
---|
66 | |
---|
67 | \begin{figure} |
---|
68 | \begin{cfa} |
---|
69 | // integral |
---|
70 | enum( @char@ ) Currency { Dollar = '$\textdollar$', Cent = '$\textcent$', Yen = '$\textyen$', Pound = '$\textsterling$', Euro = 'E' }; |
---|
71 | enum( @signed char@ ) srgb { Red = -1, Green = 0, Blue = 1 }; |
---|
72 | enum( @long long int@ ) BigNum { X = 123_456_789_012_345, Y = 345_012_789_456_123 }; |
---|
73 | // non-integral |
---|
74 | enum( @double@ ) Math { PI_2 = 1.570796, PI = 3.141597, E = 2.718282 }; |
---|
75 | enum( @_Complex@ ) Plane { X = 1.5+3.4i, Y = 7+3i, Z = 0+0.5i }; |
---|
76 | // pointer |
---|
77 | enum( @char *@ ) Name { Fred = "FRED", Mary = "MARY", Jane = "JANE" }; |
---|
78 | int i, j, k; |
---|
79 | enum( @int *@ ) ptr { I = &i, J = &j, K = &k }; |
---|
80 | enum( @int &@ ) ref { I = i, J = j, K = k }; |
---|
81 | // tuple |
---|
82 | enum( @[int, int]@ ) { T = [ 1, 2 ] }; $\C{// new \CFA type}$ |
---|
83 | // function |
---|
84 | void f() {...} void g() {...} |
---|
85 | enum( @void (*)()@ ) funs { F = f, G = g }; |
---|
86 | // aggregate |
---|
87 | struct Person { char * name; int age, height; }; |
---|
88 | @***@enum( @Person@ ) friends { @Liz@ = { "ELIZABETH", 22, 170 }, @Beth@ = Liz, |
---|
89 | Jon = { "JONATHAN", 35, 190 } }; |
---|
90 | \end{cfa} |
---|
91 | \caption{Enumerator Typing} |
---|
92 | \label{f:EumeratorTyping} |
---|
93 | \end{figure} |
---|
94 | |
---|
95 | An advantage of the typed enumerations is eliminating the \emph{harmonizing} problem between an enumeration and companion data \see{\VRef{s:Usage}}: |
---|
96 | \begin{cfa} |
---|
97 | enum( char * ) integral_types { |
---|
98 | chr = "char", schar = "signed char", uschar = "unsigned char", |
---|
99 | sshort = "signed short int", ushort = "unsigned short int", |
---|
100 | sint = "signed int", usint = "unsigned int", |
---|
101 | ... |
---|
102 | }; |
---|
103 | \end{cfa} |
---|
104 | Note, 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. |
---|
105 | |
---|
106 | While the enumeration type can be any C aggregate, the aggregate's \CFA constructors are not used to evaluate an enumerator's value. |
---|
107 | \CFA enumeration constants are compile-time values (static); |
---|
108 | calling constructors happens at runtime (dynamic). |
---|
109 | |
---|
110 | @value@ is an @attribute@ that defined for typed enum along with position and label. @values@ of a typed enum are stored in a global array of declared typed, initialized with |
---|
111 | value of enumerator initializers. @value()@ functions maps an enum to an elements of the array. |
---|
112 | |
---|
113 | |
---|
114 | \subsection{Value Conversion} |
---|
115 | C has an implicit type conversion from an enumerator to its base type @int@. |
---|
116 | Correspondingly, \CFA has an implicit conversion from a typed enumerator to its base type, allowing typed enumeration to be seemlyless used as |
---|
117 | a value of its base type. |
---|
118 | \begin{cfa} |
---|
119 | char currency = Dollar; |
---|
120 | void foo( char * ); |
---|
121 | foo( Fred ); |
---|
122 | \end{cfa} |
---|
123 | |
---|
124 | % During the resolution of expression e with \CFA enumeration type, \CFA adds @value(e)@ as an additional candidate with an extra \newterm{value} cost. |
---|
125 | % For expression @char currency = Dollar@, the is no defined conversion from Dollar (\CFA enumeration) type to basic type and the conversion cost is @infinite@, |
---|
126 | % thus the only valid candidate is @value(Dollar)@. |
---|
127 | The implicit conversion induces a \newterm{value cost}, which is a new category in \CFA's conversion cost model to disambiguate function overloading over for both \CFA enumeration and its base type. |
---|
128 | \begin{cfa} |
---|
129 | void baz( char ch ); $\C{// (1)}$ |
---|
130 | void baz( Currency cu ); $\C{// (2)}$ |
---|
131 | |
---|
132 | baz( Cent ); |
---|
133 | \end{cfa} |
---|
134 | While both baz are applicable to \CFA enumeration, using Cent as a char in @candiate (1)@ comes with a @value@ cost, |
---|
135 | while @candidate (2)@ has @zero@ cost. \CFA always choose a overloaded candidate implemented for a \CFA enumeration itself over a candidate applies on a base type. |
---|
136 | |
---|
137 | Value cost is defined to be a more significant factor than an @unsafe@ but weight less than @poly@. |
---|
138 | With @value@ being an additional category, the current \CFA conversion cost is a 8-tuple: |
---|
139 | @@(unsafe, value, poly, safe, sign, vars, specialization, reference)@@. |
---|
140 | |
---|
141 | \begin{cfa} |
---|
142 | void bar(int); |
---|
143 | enum(int) Month !{ |
---|
144 | January=31, February=29, March=31, April=30, May=31, June-30, |
---|
145 | July=31, August=31, September=30, October=31, November=30, December=31 |
---|
146 | }; |
---|
147 | |
---|
148 | Month a = Februrary; $\C{// (1), with cost (0, 1, 0, 0, 0, 0, 0, 0)}$ |
---|
149 | double a = 5.5; $\C{// (2), with cost (1, 0, 0, 0, 0, 0, 0, 0)}$ |
---|
150 | |
---|
151 | bar(a); |
---|
152 | \end{cfa} |
---|
153 | In the previous example, candidate (1) has an value cost to parameter type int, with is lower than (2) as an unsafe conversion from double to int. |
---|
154 | \CFA chooses value cost over unsafe cost and therefore @a@ of @bar(a)@ is resolved as an @Month@. |
---|
155 | |
---|
156 | \begin{cfa} |
---|
157 | forall(T | @CfaEnum(T)@) void bar(T); |
---|
158 | |
---|
159 | bar(a); $\C{// (3), with cost (0, 0, 1, 0, 0, 0, 0, 0)}$ |
---|
160 | \end{cfa} |
---|
161 | % @Value@ is designed to be less significant than @poly@ to allow function being generic over \CFA enumeration (see ~\ref{c:trait}). |
---|
162 | 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}. |
---|
163 | @Value@ is a being a more significant cost than @poly@ implies if a overloaeded function defined for @CfaEnum@ (and other generic type), \CFA always |
---|
164 | try to resolve it as a @CfaEnum@, rather to insert a @value@ conversion. |
---|
165 | |
---|
166 | \subsection{Explicit Conversion} |
---|
167 | Explicit conversion is allowed on \CFA enumeration to an integral type, in which case \CFA converts \CFA enumeration into its underlying representation, |
---|
168 | which is its @position@. |
---|
169 | |
---|
170 | \section{Auto Initialization} |
---|
171 | |
---|
172 | C auto-initialization works for the integral type @int@ with constant expressions. |
---|
173 | \begin{cfa} |
---|
174 | enum Alphabet ! { |
---|
175 | A = 'A', B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, |
---|
176 | a = 'a', b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z |
---|
177 | }; |
---|
178 | \end{cfa} |
---|
179 | The complexity of the constant expression depends on the level of runtime computation the compiler implements, \eg \CC \lstinline[language={[GNU]C++}]{constexpr} provides complex compile-time computation across multiple types, which blurs the compilation/runtime boundary. |
---|
180 | |
---|
181 | % The notion of auto-initialization is generalized in \CFA enumertation E with base type T in the following way: |
---|
182 | When an enumerator @e@ does not have a initializer, if @e@ has enumeration type @E@ with base type @T@, \CFA auto-initialize @e@ with the following scheme: |
---|
183 | \begin{enumerate} |
---|
184 | % \item Enumerator e is the first enumerator of \CFA enumeration E with base type T. If e declares no no initializer, e is auto-initialized by the $zero\_t$ constructor of T. |
---|
185 | \item if e is first enumerator, e is initialized with T's @zero_t@. |
---|
186 | \item otherwise, if d is the enumerator defined just before e, with d has has been initialized with expression @l@ (@l@ can also be an auto-generated), e is initialized with @l++@. |
---|
187 | % \CFA reports a compile time error if T has no $zero\_t$ constructor. |
---|
188 | % Enumerator e is an enumerator of base-type T enumeration E that position i, where $i \neq 0$. And d is the enumerator with position @i-1@, e is auto-initialized with |
---|
189 | % the result of @value(d)++@. If operator @?++@ is not defined for type T, \CFA reports a compile time error. |
---|
190 | |
---|
191 | % Unfortunately, auto-initialization is not implemented because \CFA is only a transpiler, relying on generated C code to perform the detail work. |
---|
192 | % C does not have the equivalent of \CC \lstinline[language={[GNU]C++}]{constexpr}, and it is currently beyond the scope of the \CFA project to implement a complex runtime interpreter in the transpiler. |
---|
193 | % Nevertheless, the necessary language concepts exist to support this feature. |
---|
194 | \end{enumerate} |
---|
195 | while @?++( T )@ can be explicitly overloaded or implicitly overloaded with properly defined @one_t@ and @?+?(T, T)@. |
---|
196 | |
---|
197 | Unfortunately, auto-initialization with only constant expression is not enforced because \CFA is only a transpiler, relying on generated C code to perform the detail work. |
---|
198 | C does not have the equivalent of \CC \lstinline[language={[GNU]C++}]{constexpr}, and it is currently beyond the scope of the \CFA project to implement a complex runtime interpreter in the transpiler. |
---|
199 | Nevertheless, the necessary language concepts exist to support this feature. |
---|
200 | |
---|
201 | \section{Enumeration Inheritance} |
---|
202 | |
---|
203 | \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). |
---|
204 | Inheritance can be nested, and a \CFA enumeration can inline enumerators from more than one \CFA enumeration, forming a tree-like hierarchy. |
---|
205 | However, the uniqueness of enumeration name applies to enumerators, including those from supertypes, meaning an enumeration cannot name enumerator with the same label as its subtype's members, or inherits |
---|
206 | from multiple enumeration that has overlapping enumerator label. As a consequence, a new type cannot inherits from both an enumeration and its supertype, or two enumerations with a |
---|
207 | common supertype (the diamond problem), since such would unavoidably introduce duplicate enumerator labels. |
---|
208 | |
---|
209 | \begin{cfa} |
---|
210 | enum( char * ) Names { /* as above */ }; |
---|
211 | enum( char * ) Names2 { @inline Names@, Jack = "JACK", Jill = "JILL" }; |
---|
212 | enum( char * ) Names3 { @inline Names2@, Sue = "SUE", Tom = "TOM" }; |
---|
213 | \end{cfa} |
---|
214 | |
---|
215 | % Enumeration @Name2@ inherits all the enumerators and their values from enumeration @Names@ by containment, and a @Names@ enumeration is a @subtype@ of enumeration @Name2@. |
---|
216 | % Note, that enumerators must be unique in inheritance but enumerator values may be repeated. |
---|
217 | |
---|
218 | @Names2@ is defined with five enumerators, three of which are from @Name@ through containment, and two are self-declared. |
---|
219 | @Names3@ inherits all five members from @Names2@ and declare two additional enumerators. |
---|
220 | |
---|
221 | % The enumeration type for the inheriting type must be the same as the inherited type; |
---|
222 | % hence the enumeration type may be omitted for the inheriting enumeration and it is inferred from the inherited enumeration, as for @Name3@. |
---|
223 | % When inheriting from integral types, automatic numbering may be used, so the inheritance placement left to right is important. |
---|
224 | Specifically, the inheritance relationship for @Names@ is: |
---|
225 | \begin{cfa} |
---|
226 | Names $\(\subset\)$ Names2 $\(\subset\)$ Names3 $\C{// enum type of Names}$ |
---|
227 | \end{cfa} |
---|
228 | |
---|
229 | % The enumeration base for the subtype must be the same as the super type. |
---|
230 | The base type must be consistent between subtype and supertype. |
---|
231 | When an enumeration inherits enumerators from another enumeration, it copies the enumerators' @value@ and @label@, even if the @value@ was auto initialized. However, the @position@ as the underlying |
---|
232 | representation will be the order of the enumerator in new enumeration. |
---|
233 | % new enumeration @N@ copies all enumerators from @O@, including those @O@ obtains through inheritance. Enumerators inherited from @O@ |
---|
234 | % keeps same @label@ and @value@, but @position@ may shift to the right if other enumerators or inline enumeration declared in prior of @inline A@. |
---|
235 | % hence the enumeration type may be omitted for the inheriting enumeration and it is inferred from the inherited enumeration, as for @Name3@. |
---|
236 | % When inheriting from integral types, automatic numbering may be used, so the inheritance placement left to right is important. |
---|
237 | |
---|
238 | \begin{cfa} |
---|
239 | enum() Phynchocephalia { Tuatara }; |
---|
240 | enum() Squamata { Snake, Lizard }; |
---|
241 | enum() Lepidosauromorpha { inline Phynchocephalia, inline Squamata, Kuehneosauridae }; |
---|
242 | \end{cfa} |
---|
243 | Snake, for example, has the position 0 in Squamata, but 1 in Lepidosauromorpha as Tuatara inherited from Phynchocephalia is position 0 in Lepidosauromorpha. |
---|
244 | |
---|
245 | A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost. |
---|
246 | \begin{cfa} |
---|
247 | enum Squamata squamata_lizard = Lizard; |
---|
248 | posn(quamata_lizard); // 1 |
---|
249 | enum Lepidosauromorpha lepidosauromorpha_lizard = squamata_lizard; |
---|
250 | posn(lepidosauromorpha_lizard); // 2 |
---|
251 | void foo( Lepidosauromorpha l ); |
---|
252 | foo( squamata_lizard ); |
---|
253 | posn( (Lepidosauromorpha) squamata_lizard ); // 2 |
---|
254 | |
---|
255 | Lepidosauromorpha s = Snake; |
---|
256 | \end{cfa} |
---|
257 | The last expression in the preceding example is unambiguous. While both @Squamata.Snake@ and @Lepidosauromorpha.Snake@ are valid candidate, @Squamata.Snake@ has |
---|
258 | an associated safe cost and \CFA select the zero cost candidate @Lepidosauromorpha.Snake@. |
---|
259 | |
---|
260 | As discussed in \VRef{s:OpaqueEnum}, \CFA chooses position as a representation of \CFA enum. Conversion involves both change of typing |
---|
261 | and possibly @position@. |
---|
262 | |
---|
263 | When converting a subtype to a supertype, the position can only be a larger value. The difference between the position in subtype and in supertype is an "offset". |
---|
264 | \CFA runs a the following algorithm to determine the offset for an enumerator to a super type. |
---|
265 | % In a summary, \CFA loops over members (include enumerators and inline enums) of the supertype. |
---|
266 | % If the member is the matching enumerator, the algorithm returns its position. |
---|
267 | % If the member is a inline enumeration, the algorithm trys to find the enumerator in the inline enumeration. If success, it returns the position of enumerator in the inline enumeration, plus |
---|
268 | % the position in the current enumeration. Otherwises, it increase the offset by the size of inline enumeration. |
---|
269 | |
---|
270 | \begin{cfa} |
---|
271 | struct Enumerator; |
---|
272 | struct CFAEnum { |
---|
273 | vector<variant<CFAEnum, Enumerator>> members; |
---|
274 | }; |
---|
275 | pair<bool, int> calculateEnumOffset( CFAEnum dst, Enumerator e ) { |
---|
276 | int offset = 0; |
---|
277 | for( auto v: dst.members ) { |
---|
278 | if ( v.holds_alternative<Enumerator>() ) { |
---|
279 | auto m = v.get<Enumerator>(); |
---|
280 | if ( m == e ) return make_pair( true, 0 ); |
---|
281 | offset++; |
---|
282 | } else { |
---|
283 | auto p = calculateEnumOffset( v, e ); |
---|
284 | if ( p.first ) return make_pair( true, offset + p.second ); |
---|
285 | offset += p.second; |
---|
286 | } |
---|
287 | } |
---|
288 | return make_pair( false, offset ); |
---|
289 | } |
---|
290 | \end{cfa} |
---|
291 | |
---|
292 | % \begin{cfa} |
---|
293 | % Names fred = Name.Fred; |
---|
294 | % (Names2) fred; (Names3) fred; (Name3) Names.Jack; $\C{// cast to super type}$ |
---|
295 | % Names2 fred2 = fred; Names3 fred3 = fred2; $\C{// assign to super type}$ |
---|
296 | % \end{cfa} |
---|
297 | For the given function prototypes, the following calls are valid. |
---|
298 | \begin{cquote} |
---|
299 | \begin{tabular}{ll} |
---|
300 | \begin{cfa} |
---|
301 | void f( Names ); |
---|
302 | void g( Names2 ); |
---|
303 | void h( Names3 ); |
---|
304 | void j( const char * ); |
---|
305 | \end{cfa} |
---|
306 | & |
---|
307 | \begin{cfa} |
---|
308 | f( Fred ); |
---|
309 | g( Fred ); g( Jill ); |
---|
310 | h( Fred ); h( Jill ); h( Sue ); |
---|
311 | j( Fred ); j( Jill ); j( Sue ); j( "WILL" ); |
---|
312 | \end{cfa} |
---|
313 | \end{tabular} |
---|
314 | \end{cquote} |
---|
315 | 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. |
---|
316 | |
---|
317 | \section{Enumerator Control Structures} |
---|
318 | |
---|
319 | Enumerators can be used in multiple contexts. |
---|
320 | In most programming languages, an enumerator is implicitly converted to its value (like a typed macro substitution). |
---|
321 | However, enumerator synonyms and typed enumerations make this implicit conversion to value incorrect in some contexts. |
---|
322 | In these contexts, a programmer's intuition assumes an implicit conversion to position. |
---|
323 | |
---|
324 | 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. |
---|
325 | (For this discussion, ignore the fact that @case@ requires a compile-time constant.) |
---|
326 | \begin{cfa}[belowskip=0pt] |
---|
327 | enum Count { First, Second, Third, Fourth }; |
---|
328 | Count e; |
---|
329 | \end{cfa} |
---|
330 | \begin{cquote} |
---|
331 | \setlength{\tabcolsep}{15pt} |
---|
332 | \noindent |
---|
333 | \begin{tabular}{@{}ll@{}} |
---|
334 | \begin{cfa}[aboveskip=0pt] |
---|
335 | |
---|
336 | choose( e ) { |
---|
337 | case @First@: ...; |
---|
338 | case @Second@: ...; |
---|
339 | case @Third@: ...; |
---|
340 | case @Fourth@: ...; |
---|
341 | } |
---|
342 | \end{cfa} |
---|
343 | & |
---|
344 | \begin{cfa}[aboveskip=0pt] |
---|
345 | // rewrite |
---|
346 | choose( @value@( e ) ) { |
---|
347 | case @value@( First ): ...; |
---|
348 | case @value@( Second ): ...; |
---|
349 | case @value@( Third ): ...; |
---|
350 | case @value@( Fourth ): ...; |
---|
351 | } |
---|
352 | \end{cfa} |
---|
353 | \end{tabular} |
---|
354 | \end{cquote} |
---|
355 | 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. |
---|
356 | However, this implementation is fragile, \eg if the enumeration is changed to: |
---|
357 | \begin{cfa} |
---|
358 | enum Count { First, Second, Third @= First@, Fourth }; |
---|
359 | \end{cfa} |
---|
360 | making @Third == First@ and @Fourth == Second@, causing a compilation error because of duplicate @case@ clauses. |
---|
361 | To better match with programmer intuition, \CFA toggles between value and position semantics depending on the language context. |
---|
362 | For conditional clauses and switch statements, \CFA uses the robust position implementation. |
---|
363 | \begin{cfa} |
---|
364 | if ( @posn@( e ) < posn( Third ) ) ... |
---|
365 | choose( @posn@( e ) ) { |
---|
366 | case @posn@( First ): ...; |
---|
367 | case @posn@( Second ): ...; |
---|
368 | case @posn@( Third ): ...; |
---|
369 | case @posn@( Fourth ): ...; |
---|
370 | } |
---|
371 | \end{cfa} |
---|
372 | |
---|
373 | \CFA provides a special form of for-control for enumerating through an enumeration, where the range is a type. |
---|
374 | \begin{cfa} |
---|
375 | for ( cx; @Count@ ) { sout | cx | nonl; } sout | nl; |
---|
376 | for ( cx; +~= Count ) { sout | cx | nonl; } sout | nl; |
---|
377 | for ( cx; -~= Count ) { sout | cx | nonl; } sout | nl; |
---|
378 | First Second Third Fourth |
---|
379 | First Second Third Fourth |
---|
380 | Fourth Third Second First |
---|
381 | \end{cfa} |
---|
382 | 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. |
---|
383 | The prefix @+~=@ or @-~=@ iterate forward or backwards through the inclusive enumeration range, where no prefix defaults to @+~=@. |
---|
384 | |
---|
385 | C has an idiom for @if@ and loop predicates of comparing the predicate result ``not equal to 0''. |
---|
386 | \begin{cfa} |
---|
387 | if ( x + y /* != 0 */ ) ... |
---|
388 | while ( p /* != 0 */ ) ... |
---|
389 | \end{cfa} |
---|
390 | 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. |
---|
391 | For example, such a conversion exists for all numerical types (integral and floating-point). |
---|
392 | It is possible to explicitly extend this idiom to any typed enumeration by overloading the @!=@ operator. |
---|
393 | \begin{cfa} |
---|
394 | bool ?!=?( Name n, zero_t ) { return n != Fred; } |
---|
395 | Name n = Mary; |
---|
396 | if ( n ) ... // result is true |
---|
397 | \end{cfa} |
---|
398 | Specialize meanings are also possible. |
---|
399 | \begin{cfa} |
---|
400 | enum(int) ErrorCode { Normal = 0, Slow = 1, Overheat = 1000, OutOfResource = 1001 }; |
---|
401 | bool ?!=?( ErrorCode ec, zero_t ) { return ec >= Overheat; } |
---|
402 | ErrorCode code = ...; |
---|
403 | if ( code ) { problem(); } |
---|
404 | \end{cfa} |
---|
405 | |
---|
406 | |
---|
407 | \section{Enumeration Dimension} |
---|
408 | |
---|
409 | \VRef{s:EnumeratorTyping} introduced the harmonizing problem between an enumeration and secondary information. |
---|
410 | When possible, using a typed enumeration for the secondary information is the best approach. |
---|
411 | However, there are times when combining these two types is not possible. |
---|
412 | For example, the secondary information might precede the enumeration and/or its type is needed directly to declare parameters of functions. |
---|
413 | In these cases, having secondary arrays of the enumeration size are necessary. |
---|
414 | |
---|
415 | 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. |
---|
416 | \begin{cfa} |
---|
417 | enum E1 { A, B, C, N }; // possibly predefined |
---|
418 | enum(int) E2 { A, B, C }; |
---|
419 | float H1[N] = { [A] :$\footnote{Note, C uses the symbol, @'='@ for designator initialization, but \CFA had to change to @':'@ because of problems with tuple syntax.}$ 3.4, [B] : 7.1, [C] : 0.01 }; // C |
---|
420 | float H2[@E2@] = { [A] : 3.4, [B] : 7.1, [C] : 0.01 }; // CFA |
---|
421 | \end{cfa} |
---|
422 | This approach is also necessary for a predefined typed enumeration (unchangeable), when additional secondary-information need to be added. |
---|
423 | |
---|
424 | The array subscript operator, namely ?[?], has been overloaded so that when a CFA enumerator is used as an array index, it implicitly converts |
---|
425 | to its position over value to sustain data hormonization. User can revert the behaviour by: |
---|
426 | \begin{cfa} |
---|
427 | float ?[?](float * arr, E2 index) { return arr[value(index)]; } |
---|
428 | \end{cfa} |
---|
429 | When an enumeration type is being used as an array dimension, \CFA add the enumeration type to initializer's context. As a result, |
---|
430 | @H2@'s array destinators @A@, @B@ and @C@ are resolved unambiguously to type E2. (H1's destinators are also resolved unambiguously to |
---|
431 | E1 because E2 has a @value@ cost to @int@). |
---|
432 | |
---|
433 | \section{Enumeration I/O} |
---|
434 | |
---|
435 | As seen in multiple examples, enumerations can be printed and the default property printed is the enumerator's label, which is similar in other programming languages. |
---|
436 | However, very few programming languages provide a mechanism to read in enumerator values. |
---|
437 | Even the @boolean@ type in many languages does not have a mechanism for input using the enumerators @true@ or @false@. |
---|
438 | \VRef[Figure]{f:EnumerationI/O} show \CFA enumeration input based on the enumerator labels. |
---|
439 | When the enumerator labels are packed together in the input stream, the input algorithm scans for the longest matching string. |
---|
440 | For basic types in \CFA, the constants use to initialize a variable in a program are available to initialize a variable using input, where strings constants can be quoted or unquoted. |
---|
441 | |
---|
442 | \begin{figure} |
---|
443 | \begin{cquote} |
---|
444 | \setlength{\tabcolsep}{15pt} |
---|
445 | \begin{tabular}{@{}ll@{}} |
---|
446 | \begin{cfa} |
---|
447 | int main() { |
---|
448 | enum(int ) E { BBB = 3, AAA, AA, AB, B }; |
---|
449 | E e; |
---|
450 | |
---|
451 | for () { |
---|
452 | try { |
---|
453 | @sin | e@; |
---|
454 | } catch( missing_data * ) { |
---|
455 | sout | "missing data"; |
---|
456 | continue; // try again |
---|
457 | } |
---|
458 | if ( eof( sin ) ) break; |
---|
459 | sout | e | "= " | value( e ); |
---|
460 | } |
---|
461 | } |
---|
462 | \end{cfa} |
---|
463 | & |
---|
464 | \begin{cfa} |
---|
465 | $\rm input$ |
---|
466 | BBBABAAAAB |
---|
467 | BBB AAA AA AB B |
---|
468 | |
---|
469 | $\rm output$ |
---|
470 | BBB = 3 |
---|
471 | AB = 6 |
---|
472 | AAA = 4 |
---|
473 | AB = 6 |
---|
474 | BBB = 3 |
---|
475 | AAA = 4 |
---|
476 | AA = 5 |
---|
477 | AB = 6 |
---|
478 | B = 7 |
---|
479 | |
---|
480 | \end{cfa} |
---|
481 | \end{tabular} |
---|
482 | \end{cquote} |
---|
483 | \caption{Enumeration I/O} |
---|
484 | \label{f:EnumerationI/O} |
---|
485 | \end{figure} |
---|
486 | |
---|
487 | |
---|
488 | |
---|
489 | \section{Planet Example} |
---|
490 | |
---|
491 | \VRef[Figure]{f:PlanetExample} shows an archetypal enumeration example illustrating most of the \CFA enumeration features. |
---|
492 | @Planet@ is an enumeration of type @MR@. |
---|
493 | Each planet enumerator is initialized to a specific mass/radius, @MR@, value. |
---|
494 | The unnamed enumeration provides the gravitational-constant enumerator @G@. |
---|
495 | Function @surfaceGravity@ uses the @with@ clause to remove @p@ qualification from fields @mass@ and @radius@. |
---|
496 | 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@. |
---|
497 | The resulting random orbital-body is used in a @choose@ statement. |
---|
498 | The enumerators in the @case@ clause use the enumerator position for testing. |
---|
499 | The prints use @label@ to print an enumerator's name. |
---|
500 | Finally, a loop enumerates through the planets computing the weight on each planet for a given earth mass. |
---|
501 | The print statement does an equality comparison with an enumeration variable and enumerator (@p == MOON@). |
---|
502 | |
---|
503 | \begin{figure} |
---|
504 | \small |
---|
505 | \begin{cfa} |
---|
506 | struct MR { double mass, radius; }; $\C{// planet definition}$ |
---|
507 | enum( @MR@ ) Planet { $\C{// typed enumeration}$ |
---|
508 | // mass (kg) radius (km) |
---|
509 | MERCURY = { 0.330_E24, 2.4397_E6 }, |
---|
510 | VENUS = { 4.869_E24, 6.0518_E6 }, |
---|
511 | EARTH = { 5.976_E24, 6.3781_E6 }, |
---|
512 | MOON = { 7.346_E22, 1.7380_E6 }, $\C{// not a planet}$ |
---|
513 | MARS = { 0.642_E24, 3.3972_E6 }, |
---|
514 | JUPITER = { 1898._E24, 71.492_E6 }, |
---|
515 | SATURN = { 568.8_E24, 60.268_E6 }, |
---|
516 | URANUS = { 86.86_E24, 25.559_E6 }, |
---|
517 | NEPTUNE = { 102.4_E24, 24.746_E6 }, |
---|
518 | PLUTO = { 1.303_E22, 1.1880_E6 }, $\C{// not a planet}$ |
---|
519 | }; |
---|
520 | enum( double ) { G = 6.6743_E-11 }; $\C{// universal gravitational constant (m3 kg-1 s-2)}$ |
---|
521 | static double surfaceGravity( Planet p ) @with( p )@ { |
---|
522 | return G * mass / ( radius @\@ 2 ); $\C{// no qualification, exponentiation}$ |
---|
523 | } |
---|
524 | static double surfaceWeight( Planet p, double otherMass ) { |
---|
525 | return otherMass * surfaceGravity( p ); |
---|
526 | } |
---|
527 | int main( int argc, char * argv[] ) { |
---|
528 | if ( argc != 2 ) @exit@ | "Usage: " | argv[0] | "earth-weight"; // terminate program |
---|
529 | double earthWeight = convert( argv[1] ); |
---|
530 | double earthMass = earthWeight / surfaceGravity( EARTH ); |
---|
531 | Planet rp = @fromInt@( prng( @countof@( Planet ) ) ); $\C{// select random orbiting body}$ |
---|
532 | @choose( rp )@ { $\C{// implicit breaks}$ |
---|
533 | case MERCURY, VENUS, EARTH, MARS: |
---|
534 | sout | @rp@ | "is a rocky planet"; |
---|
535 | case JUPITER, SATURN, URANUS, NEPTUNE: |
---|
536 | sout | rp | "is a gas-giant planet"; |
---|
537 | default: |
---|
538 | sout | rp | "is not a planet"; |
---|
539 | } |
---|
540 | for ( @p; Planet@ ) { $\C{// enumerate}$ |
---|
541 | sout | "Your weight on" | ( @p == MOON@ ? "the" : " " ) | p |
---|
542 | | "is" | wd( 1,1, surfaceWeight( p, earthMass ) ) | "kg"; |
---|
543 | } |
---|
544 | } |
---|
545 | $\$$ planet 100 |
---|
546 | JUPITER is a gas-giant planet |
---|
547 | Your weight on MERCURY is 37.7 kg |
---|
548 | Your weight on VENUS is 90.5 kg |
---|
549 | Your weight on EARTH is 100.0 kg |
---|
550 | Your weight on the MOON is 16.6 kg |
---|
551 | Your weight on MARS is 37.9 kg |
---|
552 | Your weight on JUPITER is 252.8 kg |
---|
553 | Your weight on SATURN is 106.6 kg |
---|
554 | Your weight on URANUS is 90.5 kg |
---|
555 | Your weight on NEPTUNE is 113.8 kg |
---|
556 | Your weight on PLUTO is 6.3 kg |
---|
557 | \end{cfa} |
---|
558 | \caption{Planet Example} |
---|
559 | \label{f:PlanetExample} |
---|
560 | \end{figure} |
---|