Changeset 7d9a805b for doc/theses/jiada_liang_MMath/CFAenum.tex
- Timestamp:
- Mar 21, 2024, 9:34:28 PM (6 months ago)
- Branches:
- master
- Children:
- 6394ac6
- Parents:
- 0139351
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
r0139351 r7d9a805b 1 \chapter{\CFA -Style Enum}2 3 4 \CFA supports C -Styleenumeration using the same syntax and semantics for backwards compatibility.1 \chapter{\CFA Enumeration} 2 3 4 \CFA supports C enumeration using the same syntax and semantics for backwards compatibility. 5 5 \CFA also extends C-Style enumeration by adding a number of new features that bring enumerations inline with other modern programming languages. 6 6 … … 16 16 Finally, qualification is provided to disambiguate any ambiguous situations. 17 17 \begin{cfa} 18 enum C1 { First, Second, Third, Fourth };19 enum C2 { @Fourth@, @Third@, @Second@, @First@ };20 C1 p() { return Third; } $\C{// correctly resolved duplicate names}$21 C2 p() { return Fourth; }18 enum E1 { First, Second, Third, Fourth }; 19 enum E2 { @Fourth@, @Third@, @Second@, @First@ }; 20 E1 p() { return Third; } $\C{// correctly resolved duplicate names}$ 21 E2 p() { return Fourth; } 22 22 void foo() { 23 C1 e1 = First; C2 e2 = First;23 E1 e1 = First; E2 e2 = First; 24 24 e1 = Second; e2 = Second; 25 25 e1 = p(); e2 = p(); $\C{// correctly resolved function call}$ 26 int i = @C1.@First + @C2.@First; $\C{// ambiguous without qualification}$ 27 } 28 \end{cfa} 29 \CFA overloading allows programmers to use the most meaningful names without fear of unresolvable clashes from included files, which are correctable with qualification. 26 int i = @E1.@First + @E2.@First; $\C{// disambiguate with qualification}$ 27 int j = @(E1)@First + @(E2)@First; $\C{// disambiguate with cast}$ 28 } 29 \end{cfa} 30 \CFA overloading allows programmers to use the most meaningful names without fear of name clashes from include files. 31 Either the type system implicitly disambiguates or the programmer explicitly disambiguates using qualification or casting. 30 32 31 33 … … 34 36 An enumeration can be scoped, so the enumerator constants are not projected into the enclosing scope, using @'!'@. 35 37 \begin{cfa} 36 enum Weekday @!@ { /* as above */};37 enum ( char * ) Names @!@ { /* as above */};38 enum Weekday @!@ { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun }; 39 enum RGB @!@ { Red, Green, Blue }; 38 40 \end{cfa} 39 41 Now the enumerators \emph{must} be qualified with the associated enumeration. 40 42 \begin{cfa} 41 Weekday weekday = @Weekday@.Monday; 42 Names names = @Names.@Fred; 43 names = @Names.@Jane; 43 Weekday weekday = @Weekday@.Mon; 44 weekday = @Weekday@.Sat; 45 RGB rgb = RGB.Red; 46 rgb = RGB.Blue; 44 47 \end{cfa} 45 48 It is possible to toggle back to unscoping using the \CFA @with@ clause/statement (see also \CC \lstinline[language=c++]{using enum} in Section~\ref{s:C++RelatedWork}). 46 49 \begin{cfa} 47 Weekday weekday; 48 with ( @Weekday@, @Names@ ) { $\C{// type names}$ 49 Names names = @Fred@; 50 names = @Jane@; 51 weekday = Saturday; 52 } 53 \end{cfa} 54 As in Section~\ref{s:EnumeratorNameResolution}, opening multiple unscoped enumerations can result in duplicate enumeration names, but \CFA type resolution and falling back to explicit qualification handles name resolution. 50 with ( @Weekday@, @RGB@ ) { $\C{// type names}$ 51 weekday = @Sun@; $\C{// no qualification}$ 52 rgb = @Green@; 53 } 54 \end{cfa} 55 As in Section~\ref{s:EnumeratorNameResolution}, opening multiple unscoped enumerations can result in duplicate enumeration names, but \CFA implicit type resolution and explicit qualification/casting handles name resolution. 55 56 56 57 \section{Enumerator Typing} … … 80 81 \begin{cfa} 81 82 // integral 82 enum( @char@ ) Currency { Dollar = '$\textdollar$', Euro = '$\texteuro$', Pound = '$\textsterling$'};83 enum( @char@ ) Currency { Dollar = '$\textdollar$', Cent = '$\textcent$', Yen = '$\textyen$', Pound = '$\textsterling$', Euro = 'E' }; 83 84 enum( @signed char@ ) srgb { Red = -1, Green = 0, Blue = 1 }; 84 85 enum( @long long int@ ) BigNum { X = 123_456_789_012_345, Y = 345_012_789_456_123 }; … … 87 88 enum( @_Complex@ ) Plane { X = 1.5+3.4i, Y = 7+3i, Z = 0+0.5i }; 88 89 // pointer 89 enum( @c har *@ ) Names{ Fred = "FRED", Mary = "MARY", Jane = "JANE" };90 enum( @const char *@ ) Name { Fred = "FRED", Mary = "MARY", Jane = "JANE" }; 90 91 int i, j, k; 91 92 enum( @int *@ ) ptr { I = &i, J = &j, K = &k }; … … 98 99 // aggregate 99 100 struct Person { char * name; int age, height; }; 100 @***@enum( @Person@ ) friends { @Liz@ = { "ELIZABETH", 22, 170 }, @Beth@ = Liz, Jon = { "JONATHAN", 35, 190 } }; 101 @***@enum( @Person@ ) friends { @Liz@ = { "ELIZABETH", 22, 170 }, @Beth@ = Liz, 102 Jon = { "JONATHAN", 35, 190 } }; 101 103 \end{cfa} 102 104 \caption{Enumerator Typing} … … 140 142 \begin{cfa} 141 143 enum() Mode { O_RDONLY, O_WRONLY, O_CREAT, O_TRUNC, O_APPEND }; 142 @***@Mode iomode = O_RDONLY;143 bool b = iomode == O_RDONLY || iomode < O_APPEND; 144 int i = iomode; $\C{\color{red}// disallowed}$144 Mode iomode = O_RDONLY; 145 bool b = iomode == O_RDONLY || iomode < O_APPEND; $\C{// ordering}$ 146 @***@@int i = iomode;@ $\C{// disallow conversion to int}$ 145 147 \end{cfa} 146 148 … … 149 151 If follows from enumerator typing that the enumerator type can be another enumerator. 150 152 \begin{cfa} 151 enum( @char@ ) Currency { Dollar = '$\textdollar$', Euro = '$\texteuro$', Pound = '$\textsterling$'};152 enum( @Currency@ ) Europe { Euro = Currency.Euro, Pound = Currency.Pound }; // intersection153 enum( @char@ ) Currency { Dollar = '$\textdollar$', Cent = '$\textcent$', Yen = '$\textyen$', Pound = '$\textsterling$', Euro = 'E' }; 154 enum( Currency ) Europe { Euro = Currency.Euro, Pound = Currency.Pound }; 153 155 enum( char ) Letter { A = 'A', B = 'B', C = 'C', ..., Z = 'Z' }; 154 156 enum( @Letter@ ) Greek { Alph = A, Beta = B, ..., Zeta = Z }; // intersection … … 158 160 \begin{cfa} 159 161 Letter letter = A; 160 @***@Greak greek = Beta;161 letter = Beta; $\C{// allowed, letter== B}$162 greek = A; $\C{\color{red}// disallowed}$162 Greak greek = Beta; 163 letter = Beta; $\C{// allowed, greek == B}$ 164 @greek = A;@ $\C{// disallowed}$ 163 165 \end{cfa} 164 166 … … 277 279 p(variable_d); // 3 278 280 \end{cfa} 281 282 283 \section{Planet Example} 284 285 \VRef[Figure]{f:PlanetExample} shows an archetypal enumeration example illustrating all of the \CFA enumeration features. 286 Enumeration @Planet@ is a typed enumeration of type @MR@. 287 Each of the planet enumerators is initialized to a specific mass/radius, @MR@, value. 288 The unnamed enumeration projects the gravitational-constant enumerator @G@. 289 The program main iterates through the planets computing the weight on each planet for a given earth weight. 290 291 \begin{figure} 292 \begin{cfa} 293 struct MR { double mass, radius; }; 294 enum( MR ) Planet { 295 // mass radius 296 MERCURY = { 3.303_E23, 2.4397_E6 }, 297 VENUS = { 4.869_E24, 6.0518_E6 }, 298 EARTH = { 5.976_E24, 6.3781_E6 }, 299 MARS = { 6.421_E23, 3.3972_E6 }, 300 JUPITER = { 1.898_E27, 7.1492_E7 }, 301 SATURN = { 5.688_E26, 6.0268_E7 }, 302 URANUS = { 8.686_E25, 2.5559_E7 }, 303 NEPTUNE = { 1.024_E26, 2.4746_E7 }, 304 }; 305 enum( double ) { G = 6.6743E-11 }; // universal gravitational constant (m3 kg-1 s-2) 306 307 static double surfaceGravity( Planet p ) with( p ) { 308 return G * mass / ( radius * radius ); 309 } 310 static double surfaceWeight( Planet p, double otherMass ) { 311 return otherMass * surfaceGravity( p ); 312 } 313 int main( int argc, char * argv[] ) { 314 if ( argc != 2 ) exit | "Usage: " | argv[0] | "earth-weight"; 315 double earthWeight = convert( argv[1] ); 316 double mass = earthWeight / surfaceGravity( EARTH ); 317 for ( p; Planet ) { 318 sout | "Your weight on" | labelE(p) | "is" | surfaceWeight( p, mass ); 319 } 320 } 321 \end{cfa} 322 \caption{Planet Example} 323 \label{f:PlanetExample} 324 \end{figure}
Note: See TracChangeset
for help on using the changeset viewer.