source: doc/theses/jiada_liang_MMath/CFAenum.tex @ 7568e5c

Last change on this file since 7568e5c was 7568e5c, checked in by JiadaL <j82liang@…>, 13 hours ago

Minor update on the thesis (add auto initialization and update future work

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