| 1 | \chapter{\CFA Enumeration}
|
|---|
| 2 |
|
|---|
| 3 | \CFA extends C-Style enumeration by adding a number of new features that bring enumerations inline with other modern programming languages.
|
|---|
| 4 | Any enumeration extensions must be intuitive to C programmers both in syntax and semantics.
|
|---|
| 5 | The following sections detail all of 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, there is 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 buildin 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 integral type.
|
|---|
| 59 | The label and value of an enumerator is 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 backwards compatibility means the necessary backing data structures cannot be supplied.
|
|---|
| 61 |
|
|---|
| 62 | \section{Opaque Enumeration}
|
|---|
| 63 | \label{s:OpaqueEnum}
|
|---|
| 64 |
|
|---|
| 65 | When an enumeration type is empty is it an \newterm{opaque} enumeration.
|
|---|
| 66 | \begin{cfa}
|
|---|
| 67 | enum@()@ Mode { O_RDONLY, O_WRONLY, O_CREAT, O_TRUNC, O_APPEND };
|
|---|
| 68 | \end{cfa}
|
|---|
| 69 | Here, the internal representation is chosen by the compiler and hidden, so the enumerators cannot be initialized.
|
|---|
| 70 | Compared to the C enum, opaque enums are more restrictive in terms of typing and cannot be implicitly converted to integers.
|
|---|
| 71 | \begin{cfa}
|
|---|
| 72 | Mode mode = O_RDONLY;
|
|---|
| 73 | int www @=@ mode; $\C{// disallowed}$
|
|---|
| 74 | \end{cfa}
|
|---|
| 75 | Opaque enumerations have only two attribute properties @label@ and @posn@.
|
|---|
| 76 | \begin{cfa}
|
|---|
| 77 | char * s = label( O_TRUNC ); $\C{// "O\_TRUNC"}$
|
|---|
| 78 | int open = posn( O_WRONLY ); $\C{// 1}$
|
|---|
| 79 | \end{cfa}
|
|---|
| 80 | The equality and relational operations are available.
|
|---|
| 81 | \begin{cfa}
|
|---|
| 82 | if ( mode @==@ O_CREAT ) ...
|
|---|
| 83 | bool b = mode @<@ O_APPEND;
|
|---|
| 84 | \end{cfa}
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | \section{Typed Enumeration}
|
|---|
| 88 | \label{s:EnumeratorTyping}
|
|---|
| 89 |
|
|---|
| 90 | When an enumeration type is specified, all enumerators have that type and can be initialized with constants of that type or compile-time convertable to that type.
|
|---|
| 91 | 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.
|
|---|
| 92 | Note, the use of the synonyms @Liz@ and @Beth@ in the last declaration.
|
|---|
| 93 | 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@.
|
|---|
| 94 |
|
|---|
| 95 | \begin{figure}
|
|---|
| 96 | \begin{cfa}
|
|---|
| 97 | // integral
|
|---|
| 98 | enum( @char@ ) Currency { Dollar = '$\textdollar$', Cent = '$\textcent$', Yen = '$\textyen$', Pound = '$\textsterling$', Euro = 'E' };
|
|---|
| 99 | enum( @signed char@ ) srgb { Red = -1, Green = 0, Blue = 1 };
|
|---|
| 100 | enum( @long long int@ ) BigNum { X = 123_456_789_012_345, Y = 345_012_789_456_123 };
|
|---|
| 101 | // non-integral
|
|---|
| 102 | enum( @double@ ) Math { PI_2 = 1.570796, PI = 3.141597, E = 2.718282 };
|
|---|
| 103 | enum( @_Complex@ ) Plane { X = 1.5+3.4i, Y = 7+3i, Z = 0+0.5i };
|
|---|
| 104 | // pointer
|
|---|
| 105 | enum( @char *@ ) Name { Fred = "FRED", Mary = "MARY", Jane = "JANE" };
|
|---|
| 106 | int i, j, k;
|
|---|
| 107 | enum( @int *@ ) ptr { I = &i, J = &j, K = &k };
|
|---|
| 108 | enum( @int &@ ) ref { I = i, J = j, K = k };
|
|---|
| 109 | // tuple
|
|---|
| 110 | enum( @[int, int]@ ) { T = [ 1, 2 ] }; $\C{// new \CFA type}$
|
|---|
| 111 | // function
|
|---|
| 112 | void f() {...} void g() {...}
|
|---|
| 113 | enum( @void (*)()@ ) funs { F = f, G = g };
|
|---|
| 114 | // aggregate
|
|---|
| 115 | struct Person { char * name; int age, height; };
|
|---|
| 116 | enum( @Person@ ) friends { @Liz@ = { "ELIZABETH", 22, 170 }, @Beth@ = Liz,
|
|---|
| 117 | Jon = { "JONATHAN", 35, 190 } };
|
|---|
| 118 | \end{cfa}
|
|---|
| 119 | % synonym feature unimplemented
|
|---|
| 120 | \caption{Enumerator Typing}
|
|---|
| 121 | \label{f:EumeratorTyping}
|
|---|
| 122 | \end{figure}
|
|---|
| 123 |
|
|---|
| 124 | An advantage of the typed enumerations is eliminating the \emph{harmonizing} problem between an enumeration and companion data \see{\VRef{s:Usage}}:
|
|---|
| 125 | \begin{cfa}
|
|---|
| 126 | enum( char * ) integral_types {
|
|---|
| 127 | chr = "char", schar = "signed char", uschar = "unsigned char",
|
|---|
| 128 | sshort = "signed short int", ushort = "unsigned short int",
|
|---|
| 129 | sint = "signed int", usint = "unsigned int",
|
|---|
| 130 | ...
|
|---|
| 131 | };
|
|---|
| 132 | \end{cfa}
|
|---|
| 133 | 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.
|
|---|
| 134 |
|
|---|
| 135 | While the enumeration type can be any C aggregate, the aggregate's \CFA constructors are \emph{not} used to evaluate an enumerator's value.
|
|---|
| 136 | \CFA enumeration constants are compile-time values (static);
|
|---|
| 137 | calling constructors happens at runtime (dynamic).
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 | \section{Value Conversion}
|
|---|
| 141 |
|
|---|
| 142 | C has an implicit type conversion from an enumerator to its base type @int@.
|
|---|
| 143 | 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
|
|---|
| 144 | For example, using type @Currency@ in \VRef[Figure]{f:EumeratorTyping}:
|
|---|
| 145 | \begin{cfa}
|
|---|
| 146 | char currency = Dollar; $\C{// implicit conversion to base type}$
|
|---|
| 147 | void foo( char );
|
|---|
| 148 | foo( Dollar ); $\C{// implicit conversion to base type}$
|
|---|
| 149 | \end{cfa}
|
|---|
| 150 | 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.
|
|---|
| 151 | \begin{cfa}
|
|---|
| 152 | void baz( char ch ); $\C{// (1)}$
|
|---|
| 153 | void baz( Currency cu ); $\C{// (2)}$
|
|---|
| 154 | baz( Dollar );
|
|---|
| 155 | \end{cfa}
|
|---|
| 156 | 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.
|
|---|
| 157 | Hence, \CFA chooses the exact match.
|
|---|
| 158 | 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)@.
|
|---|
| 159 | \begin{cfa}
|
|---|
| 160 | void bar( @int@ );
|
|---|
| 161 | Math x = PI; $\C{// (1)}$
|
|---|
| 162 | double x = 5.5; $\C{// (2)}$
|
|---|
| 163 | bar( x ); $\C{// costs (1, 0, 0, 0, 0, 0, 0, 0) or (0, 1, 0, 0, 0, 0, 0, 0)}$
|
|---|
| 164 | \end{cfa}
|
|---|
| 165 | Here, candidate (1) has a value conversion cost to convert to the base type, while candidate (2) has an unsafe conversion from @double@ to @int@.
|
|---|
| 166 | Hence, @bar( x )@ resolves @x@ as type @Math@.
|
|---|
| 167 |
|
|---|
| 168 | % \begin{cfa}
|
|---|
| 169 | % forall(T | @CfaEnum(T)@) void bar(T);
|
|---|
| 170 | %
|
|---|
| 171 | % bar(a); $\C{// (3), with cost (0, 0, 1, 0, 0, 0, 0, 0)}$
|
|---|
| 172 | % \end{cfa}
|
|---|
| 173 | % % @Value@ is designed to be less significant than @poly@ to allow function being generic over \CFA enumeration (see ~\ref{c:trait}).
|
|---|
| 174 | % 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}.
|
|---|
| 175 | % @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.
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 | \section{Auto Initialization}
|
|---|
| 179 |
|
|---|
| 180 | A partially implemented feature is auto-initialization, which works for the C integral type with constant expressions.
|
|---|
| 181 | \begin{cfa}
|
|---|
| 182 | enum Week { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }; // 0-2, 10-13
|
|---|
| 183 | \end{cfa}
|
|---|
| 184 | 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.
|
|---|
| 185 |
|
|---|
| 186 | If \CFA had powerful compilation expression evaluation, auto initialization would be implemented as follows.
|
|---|
| 187 | \begin{cfa}
|
|---|
| 188 | enum E(T) { A, B, C };
|
|---|
| 189 | \end{cfa}
|
|---|
| 190 | \begin{enumerate}
|
|---|
| 191 | \item the first enumerator, @A@, is initialized with @T@'s @zero_t@.
|
|---|
| 192 | \item otherwise, the next enumerator is initialized with the previous enumerator's value using operator @?++@, where @?++( T )@ can be overloaded for any type @T@.
|
|---|
| 193 | \end{enumerate}
|
|---|
| 194 |
|
|---|
| 195 | Unfortunately, constant expressions in C are not powerful and \CFA is only a transpiler, relying on generated C code to perform the detail work.
|
|---|
| 196 | 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.
|
|---|
| 197 | Nevertheless, the necessary language concepts exist to support this feature.
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 | \section{Subset}
|
|---|
| 201 |
|
|---|
| 202 | An enumeration's type can be another enumeration.
|
|---|
| 203 | \begin{cfa}
|
|---|
| 204 | enum( char ) Letter { A = 'A', ... };
|
|---|
| 205 | enum( @Letter@ ) Greek { Alph = A, Beta = B, ... }; // alphabet intersection
|
|---|
| 206 | \end{cfa}
|
|---|
| 207 | Enumeration @Greek@ may have more or less enums than @Letter@, but the enum values \emph{must} be from @Letter@.
|
|---|
| 208 | Therefore, @Greek@ enums are a subset of type @Letter@ and are type compatible with enumeration @Letter@, but @Letter@ enums are not type compatible with enumeration @Greek@.
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 | \section{Inheritance}
|
|---|
| 212 | \label{s:CFAInheritance}
|
|---|
| 213 |
|
|---|
| 214 | \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).
|
|---|
| 215 | Containment is nominative: an enumeration inherits all enumerators from another enumeration by declaring an @inline statement@ in its enumerator lists.
|
|---|
| 216 | \begin{cfa}
|
|---|
| 217 | enum( char * ) Names { /* $\see{\VRef[Figure]{f:EumeratorTyping}}$ */ };
|
|---|
| 218 | enum( char * ) Names2 { @inline Names@, Jack = "JACK", Jill = "JILL" };
|
|---|
| 219 | enum( char * ) Names3 { @inline Names2@, Sue = "SUE", Tom = "TOM" };
|
|---|
| 220 | \end{cfa}
|
|---|
| 221 | In the preceding example, @Names2@ is defined with five enumerators, three of which are from @Name@ through containment, and two are self-declared.
|
|---|
| 222 | @Names3@ inherits all five members from @Names2@ and declares two additional enumerators.
|
|---|
| 223 | Hence, enumeration inheritance forms a subset relationship.
|
|---|
| 224 | Specifically, the inheritance relationship for the example above is:
|
|---|
| 225 | \begin{cfa}
|
|---|
| 226 | Names $\(\subset\)$ Names2 $\(\subset\)$ Names3 $\C{// enum type of Names}$
|
|---|
| 227 | \end{cfa}
|
|---|
| 228 |
|
|---|
| 229 | Inheritance can be nested, and a \CFA enumeration can inline enumerators from more than one \CFA enumeration, forming a tree-like hierarchy.
|
|---|
| 230 | 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
|
|---|
| 231 | 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
|
|---|
| 232 | common supertype (the diamond problem), since such would unavoidably introduce duplicate enumerator labels.
|
|---|
| 233 |
|
|---|
| 234 | The base type must be consistent between subtype and supertype.
|
|---|
| 235 | When an enumeration inherits enumerators from another enumeration, it copies the enumerators' @value@ and @label@, even if the @value@ is auto initialized.
|
|---|
| 236 | However, the position of the underlying representation is the order of the enumerator in the new enumeration.
|
|---|
| 237 | \begin{cfa}
|
|---|
| 238 | enum() E1 { A };
|
|---|
| 239 | enum() E2 { B, C };
|
|---|
| 240 | enum() E3 { inline E1, inline E2, D };
|
|---|
| 241 | \end{cfa}
|
|---|
| 242 | Here, @A@ has position 0 in @E1@ and @E3@.
|
|---|
| 243 | @B@ has position 0 in @E2@ and 1 in @E3@.
|
|---|
| 244 | @C@ has position 1 in @E2@ and position 2 in @E3@.
|
|---|
| 245 | @D@ has position 3 in @E3@.
|
|---|
| 246 |
|
|---|
| 247 | A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost.
|
|---|
| 248 | \begin{cfa}
|
|---|
| 249 | enum E2 e2 = C;
|
|---|
| 250 | posn( e2 ); $\C[1.75in]{// 1}$
|
|---|
| 251 | enum E3 e3 = e2;
|
|---|
| 252 | posn( e2 ); $\C{// 2}$
|
|---|
| 253 | void foo( E3 e );
|
|---|
| 254 | foo( e2 );
|
|---|
| 255 | posn( (E3)e2 ); $\C{// 2}$
|
|---|
| 256 | E3 e31 = B;
|
|---|
| 257 | posn( e31 ); $\C{// 1}\CRT$
|
|---|
| 258 | \end{cfa}
|
|---|
| 259 | The last expression is unambiguous.
|
|---|
| 260 | While both @E2.B@ and @E3.B@ are valid candidate, @E2.B@ has an associated safe cost and \CFA selects the zero cost candidate @E3.B@.
|
|---|
| 261 | Hence, as discussed in \VRef{s:OpaqueEnum}, \CFA chooses position as a representation of the \CFA enum.
|
|---|
| 262 | Therefore, conversion involves both a change of type and possibly position.
|
|---|
| 263 |
|
|---|
| 264 | When converting a subtype to a supertype, its position can only be a larger value.
|
|---|
| 265 | The difference between the position in the subtype and in the supertype is its \newterm{offset}.
|
|---|
| 266 | \VRef[Figure]{s:OffsetSubtypeSuperType} show the algorithm to determine the offset for an subtype enumerator to its super type.
|
|---|
| 267 | \PAB{You need to explain the algorithm.}
|
|---|
| 268 |
|
|---|
| 269 | \begin{figure}
|
|---|
| 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 | \caption{Compute Offset from Subtype Enumerator to Super Type}
|
|---|
| 292 | \label{s:OffsetSubtypeSuperType}
|
|---|
| 293 | \end{figure}
|
|---|
| 294 |
|
|---|
| 295 | For the given function prototypes, the following calls are valid.
|
|---|
| 296 | \begin{cquote}
|
|---|
| 297 | \begin{tabular}{ll}
|
|---|
| 298 | \begin{cfa}
|
|---|
| 299 | void f( Names );
|
|---|
| 300 | void g( Names2 );
|
|---|
| 301 | void h( Names3 );
|
|---|
| 302 | void j( const char * );
|
|---|
| 303 | \end{cfa}
|
|---|
| 304 | &
|
|---|
| 305 | \begin{cfa}
|
|---|
| 306 | f( Fred );
|
|---|
| 307 | g( Fred ); g( Jill );
|
|---|
| 308 | h( Fred ); h( Jill ); h( Sue );
|
|---|
| 309 | j( Fred ); j( Jill ); j( Sue ); j( "WILL" );
|
|---|
| 310 | \end{cfa}
|
|---|
| 311 | \end{tabular}
|
|---|
| 312 | \end{cquote}
|
|---|
| 313 | 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.
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 | \section{Control Structures}
|
|---|
| 317 |
|
|---|
| 318 | Enumerators can be used in multiple contexts.
|
|---|
| 319 | In most programming languages, an enumerator is implicitly converted to its value (like a typed macro substitution).
|
|---|
| 320 | However, enumerator synonyms and typed enumerations make this implicit conversion to value incorrect in some contexts.
|
|---|
| 321 | In these contexts, a programmer's intuition assumes an implicit conversion to position.
|
|---|
| 322 |
|
|---|
| 323 | 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.
|
|---|
| 324 | (For this discussion, ignore the fact that @case@ requires a compile-time constant.)
|
|---|
| 325 | \begin{cfa}[belowskip=0pt]
|
|---|
| 326 | enum Count { First, Second, Third, Fourth };
|
|---|
| 327 | Count e;
|
|---|
| 328 | \end{cfa}
|
|---|
| 329 | \begin{cquote}
|
|---|
| 330 | \setlength{\tabcolsep}{15pt}
|
|---|
| 331 | \noindent
|
|---|
| 332 | \begin{tabular}{@{}ll@{}}
|
|---|
| 333 | \begin{cfa}[aboveskip=0pt]
|
|---|
| 334 |
|
|---|
| 335 | choose( e ) {
|
|---|
| 336 | case @First@: ...;
|
|---|
| 337 | case @Second@: ...;
|
|---|
| 338 | case @Third@: ...;
|
|---|
| 339 | case @Fourth@: ...;
|
|---|
| 340 | }
|
|---|
| 341 | \end{cfa}
|
|---|
| 342 | &
|
|---|
| 343 | \begin{cfa}[aboveskip=0pt]
|
|---|
| 344 | // rewrite
|
|---|
| 345 | choose( @value@( e ) ) {
|
|---|
| 346 | case @value@( First ): ...;
|
|---|
| 347 | case @value@( Second ): ...;
|
|---|
| 348 | case @value@( Third ): ...;
|
|---|
| 349 | case @value@( Fourth ): ...;
|
|---|
| 350 | }
|
|---|
| 351 | \end{cfa}
|
|---|
| 352 | \end{tabular}
|
|---|
| 353 | \end{cquote}
|
|---|
| 354 | 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.
|
|---|
| 355 | However, this implementation is fragile, \eg if the enumeration is changed to:
|
|---|
| 356 | \begin{cfa}
|
|---|
| 357 | enum Count { First, Second, Third @= First@, Fourth };
|
|---|
| 358 | \end{cfa}
|
|---|
| 359 | making @Third == First@ and @Fourth == Second@, causing a compilation error because of duplicate @case@ clauses.
|
|---|
| 360 | To better match with programmer intuition, \CFA toggles between value and position semantics depending on the language context.
|
|---|
| 361 | For conditional clauses and switch statements, \CFA uses the robust position implementation.
|
|---|
| 362 | \begin{cfa}
|
|---|
| 363 | if ( @posn@( e ) < posn( Third ) ) ...
|
|---|
| 364 | choose( @posn@( e ) ) {
|
|---|
| 365 | case @posn@( First ): ...;
|
|---|
| 366 | case @posn@( Second ): ...;
|
|---|
| 367 | case @posn@( Third ): ...;
|
|---|
| 368 | case @posn@( Fourth ): ...;
|
|---|
| 369 | }
|
|---|
| 370 | \end{cfa}
|
|---|
| 371 |
|
|---|
| 372 | \CFA provides a special form of for-control for enumerating through an enumeration, where the range is a type.
|
|---|
| 373 | \begin{cfa}
|
|---|
| 374 | for ( cx; @Count@ ) { sout | cx | nonl; } sout | nl;
|
|---|
| 375 | for ( cx; ~= Count ) { sout | cx | nonl; } sout | nl;
|
|---|
| 376 | for ( cx; -~= Count ) { sout | cx | nonl; } sout | nl;
|
|---|
| 377 | First Second Third Fourth
|
|---|
| 378 | First Second Third Fourth
|
|---|
| 379 | Fourth Third Second First
|
|---|
| 380 | \end{cfa}
|
|---|
| 381 | 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.
|
|---|
| 382 | The prefix @+~=@ or @-~=@ iterate forward or backwards through the inclusive enumeration range, where no prefix defaults to @+~=@.
|
|---|
| 383 |
|
|---|
| 384 | C has an idiom for @if@ and loop predicates of comparing the predicate result ``not equal to 0''.
|
|---|
| 385 | \begin{cfa}
|
|---|
| 386 | if ( x + y /* != 0 */ ) ...
|
|---|
| 387 | while ( p /* != 0 */ ) ...
|
|---|
| 388 | \end{cfa}
|
|---|
| 389 | 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.
|
|---|
| 390 | For example, such a conversion exists for all numerical types (integral and floating-point).
|
|---|
| 391 | It is possible to explicitly extend this idiom to any typed enumeration by overloading the @!=@ operator.
|
|---|
| 392 | \begin{cfa}
|
|---|
| 393 | bool ?!=?( Name n, zero_t ) { return n != Fred; }
|
|---|
| 394 | Name n = Mary;
|
|---|
| 395 | if ( n ) ... // result is true
|
|---|
| 396 | \end{cfa}
|
|---|
| 397 | Specialize meanings are also possible.
|
|---|
| 398 | \begin{cfa}
|
|---|
| 399 | enum(int) ErrorCode { Normal = 0, Slow = 1, Overheat = 1000, OutOfResource = 1001 };
|
|---|
| 400 | bool ?!=?( ErrorCode ec, zero_t ) { return ec >= Overheat; }
|
|---|
| 401 | ErrorCode code = ...;
|
|---|
| 402 | if ( code ) { problem(); }
|
|---|
| 403 | \end{cfa}
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 | \section{Dimension}
|
|---|
| 407 |
|
|---|
| 408 | \VRef{s:EnumeratorTyping} introduces the harmonizing problem between an enumeration and secondary information.
|
|---|
| 409 | When possible, using a typed enumeration for the secondary information is the best approach.
|
|---|
| 410 | However, there are times when combining these two types is not possible.
|
|---|
| 411 | For example, the secondary information might precede the enumeration and/or its type is needed directly to declare parameters of functions.
|
|---|
| 412 | In these cases, having secondary arrays of the enumeration size are necessary.
|
|---|
| 413 |
|
|---|
| 414 | 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.
|
|---|
| 415 | \begin{cfa}
|
|---|
| 416 | enum E1 { A, B, C, N }; // possibly predefined
|
|---|
| 417 | enum(int) E2 { A, B, C };
|
|---|
| 418 | float H1[N] = { [A] :$\footnotemark$ 3.4, [B] : 7.1, [C] : 0.01 }; // C
|
|---|
| 419 | float H2[@E2@] = { [A] : 3.4, [B] : 7.1, [C] : 0.01 }; // CFA
|
|---|
| 420 | \end{cfa}
|
|---|
| 421 | \footnotetext{C uses symbol \lstinline{'='} for designator initialization, but \CFA changes it to \lstinline{':'} because of problems with tuple syntax.}
|
|---|
| 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 @?[?]@, 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.
|
|---|
| 425 | This behaviour can be reverted by explicit overloading:
|
|---|
| 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 adds the enumeration type to the initializer's context.
|
|---|
| 430 | As a result, @H2@'s array destinators @A@, @B@ and @C@ are resolved unambiguously to type @E2@.
|
|---|
| 431 | (@H1@'s destinators are also resolved unambiguously to @E1@ because @E2@ has a @value@ cost.)
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 | \section{I/O}
|
|---|
| 435 |
|
|---|
| 436 | 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.
|
|---|
| 437 | However, very few programming languages provide a mechanism to read in enumerator values.
|
|---|
| 438 | Even the @boolean@ type in many languages does not have a mechanism for input using the enumerators @true@ or @false@.
|
|---|
| 439 | \VRef[Figure]{f:EnumerationI/O} show \CFA enumeration input based on the enumerator labels.
|
|---|
| 440 | When the enumerator labels are packed together in the input stream, the input algorithm scans for the longest matching string.
|
|---|
| 441 | 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 strings constants can be quoted or unquoted.
|
|---|
| 442 |
|
|---|
| 443 | \begin{figure}
|
|---|
| 444 | \begin{cquote}
|
|---|
| 445 | \setlength{\tabcolsep}{15pt}
|
|---|
| 446 | \begin{tabular}{@{}ll@{}}
|
|---|
| 447 | \begin{cfa}
|
|---|
| 448 | int main() {
|
|---|
| 449 | enum(int ) E { BBB = 3, AAA, AA, AB, B };
|
|---|
| 450 | E e;
|
|---|
| 451 |
|
|---|
| 452 | for () {
|
|---|
| 453 | try {
|
|---|
| 454 | @sin | e@;
|
|---|
| 455 | } catch( missing_data * ) {
|
|---|
| 456 | sout | "missing data";
|
|---|
| 457 | continue; // try again
|
|---|
| 458 | }
|
|---|
| 459 | if ( eof( sin ) ) break;
|
|---|
| 460 | sout | e | "= " | value( e );
|
|---|
| 461 | }
|
|---|
| 462 | }
|
|---|
| 463 | \end{cfa}
|
|---|
| 464 | &
|
|---|
| 465 | \begin{cfa}
|
|---|
| 466 | $\rm input$
|
|---|
| 467 | BBBABAAAAB
|
|---|
| 468 | BBB AAA AA AB B
|
|---|
| 469 |
|
|---|
| 470 | $\rm output$
|
|---|
| 471 | BBB = 3
|
|---|
| 472 | AB = 6
|
|---|
| 473 | AAA = 4
|
|---|
| 474 | AB = 6
|
|---|
| 475 | BBB = 3
|
|---|
| 476 | AAA = 4
|
|---|
| 477 | AA = 5
|
|---|
| 478 | AB = 6
|
|---|
| 479 | B = 7
|
|---|
| 480 |
|
|---|
| 481 | \end{cfa}
|
|---|
| 482 | \end{tabular}
|
|---|
| 483 | \end{cquote}
|
|---|
| 484 | \caption{Enumeration I/O}
|
|---|
| 485 | \label{f:EnumerationI/O}
|
|---|
| 486 | \end{figure}
|
|---|
| 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[3.5in]{// 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}\CRT$
|
|---|
| 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}
|
|---|