Changeset 66d92e3
- Timestamp:
- Nov 20, 2023, 10:34:26 AM (13 months ago)
- Branches:
- master
- Children:
- 634cb80
- Parents:
- 8c13ca8
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/enum.tex
r8c13ca8 r66d92e3 14 14 \renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}{-2.0ex \@plus -1ex \@minus -.2ex}{-1em}{\normalfont\normalsize\bfseries}} 15 15 \renewcommand\subparagraph{\@startsection{subparagraph}{4}{\z@}{-1.5ex \@plus -1ex \@minus -.2ex}{-1em}{\normalfont\normalsize\bfseries\itshape}} 16 17 % Denote newterms in particular font and index them without particular font and in lowercase, e.g., \newterm{abc}. 18 % The option parameter provides an index term different from the new term, e.g., \newterm[\texttt{abc}]{abc} 19 % The star version does not lowercase the index information, e.g., \newterm*{IBM}. 20 \newcommand{\newtermFontInline}{\emph} 21 \newcommand{\newterm}{\protect\@ifstar\@snewterm\@newterm} 22 \newcommand{\@newterm}[2][\@empty]{\lowercase{\def\temp{#2}}{\newtermFontInline{#2}}\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi} 23 \newcommand{\@snewterm}[2][\@empty]{{\newtermFontInline{#2}}\ifx#1\@empty\index{#2}\else\index{#1@{\protect#2}}\fi} 16 24 \makeatother 17 25 … … 38 46 \newcommand{\CFAIcon}{\textsf{C\raisebox{\depth}{\rotatebox{180}A}}} % Cforall icon 39 47 \newcommand{\CFA}{\protect\CFAIcon\xspace} % CFA symbolic name 48 \newcommand{\CCIcon}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}} % C++ icon 49 \newcommand{\CC}[1][]{\protect\CCIcon{#1}\xspace} % C++ symbolic name 40 50 \newcommand{\PAB}[1]{{\color{red}PAB: #1}} 41 51 … … 58 68 captionpos=b, 59 69 keepspaces=true, 70 escapechar=\$, % LaTeX escape in CFA code 60 71 % numbers=left, 61 72 % numbersep=5pt, … … 83 94 \begin{abstract} 84 95 An enumeration is a type that defines a list of named constant values in C (and other languages). 85 C usesan integral type as the underlying representation of an enumeration.96 C and \CC use an integral type as the underlying representation of an enumeration. 86 97 \CFA extends C enumerations to allow all basic and custom types for the inner representation. 87 98 \end{abstract} … … 92 103 \begin{lstlisting}[label=lst:weekday] 93 104 enum Weekday { Monday, Tuesday, Wednesday, Thursday=10, Friday, Saturday, Sunday }; 94 \end{lstlisting} 95 The example defines an @enum@ type @Weekday@ with ordered enumerators @Monday@, @Tuesday@, @Wednesday@, @Thursday@, @Friday@, @Saturday@ and @Sunday@. 105 $\(\uparrow\)$ $\(\uparrow\)$ 106 ${\rm \newterm{enumeration name}}$ ${\rm \newterm{enumerator names}} 107 \end{lstlisting} 108 The example defines an enumeration type @Weekday@ with ordered enumerators @Monday@, @Tuesday@, @Wednesday@, @Thursday@, @Friday@, @Saturday@ and @Sunday@. 96 109 The successor of @Tuesday@ is @Monday@ and the predecessor of @Tuesday@ is @Wednesday@. 97 A C enumeration has an integral type, with consecutive enumerator values assigned by the compiler starting at zero or explicitly initializedby the programmer.98 For example, @Monday@ to @Wednesday@ have values 0--2 , @Thursday@ is set to @10@, and after it, @Friday@ to @Sunday@ have values 11--13.99 100 There are 3 attributes for an enumeration: position, label, and value:110 A C enumeration is an integral type, with consecutive enumerator values assigned by the compiler starting at zero or the next explicitly initialized value by the programmer. 111 For example, @Monday@ to @Wednesday@ have values 0--2 implicitly set by the compiler, @Thursday@ is explicitly set to @10@ by the programmer, and @Friday@ to @Sunday@ have values 11--13 implicitly set by the compiler. 112 113 There are 3 attributes for an enumeration: \newterm{position}, \newterm{label}, and \newterm{value}: 101 114 \begin{cquote} 102 \small\sf 115 \small\sf\setlength{\tabcolsep}{3pt} 103 116 \begin{tabular}{rccccccccccc} 104 enumWeekday \{ & Monday, & Tuesday, & Wednesday, & Thursday=10, & Friday, & Saturday, & Sunday \}; \\105 position & 0 & 1 & 2 & 3 & 4 & 5 & 6 \\106 label & Monday & Tuesday & Wednesday & Thursday & Friday & Saturday & Sunday \\107 value & 0 & 1 & 2 & 10 & 11 & 12 & 13117 @enum@ Weekday \{ & Monday, & Tuesday, & Wednesday, & Thursday=10, & Friday, & Saturday, & Sunday \}; \\ 118 \it position & 0 & 1 & 2 & 3 & 4 & 5 & 6 \\ 119 \it label & Monday & Tuesday & Wednesday & Thursday & Friday & Saturday & Sunday \\ 120 \it value & 0 & 1 & 2 & 10 & 11 & 12 & 13 108 121 \end{tabular} 109 122 \end{cquote} 110 123 111 The enumerators of an enum are unscoped, i.e., enumerators declared inside of an enum are visible in the enclosing scope of the enum class.124 The enumerators of an enumeration are unscoped, i.e., enumerators declared inside of an @enum@ are visible in the enclosing scope of the @enum@ type. 112 125 \begin{lstlisting}[label=lst:enum_scope] 113 126 { 114 enum RGB { R, G, B }; 115 int i = R // i == 0 116 } 117 int j = G; // ERROR! G is not declared in this scope 127 enum Weekday { ... }; // enumerators implicitly projected into local scope 128 Weekday weekday = Monday; 129 weekday = Friday; 130 int i = Sunday // i == 13 131 } 132 int j = Wednesday; // ERROR! Wednesday is not declared in this scope 118 133 \end{lstlisting} 119 134 … … 121 136 122 137 A \CFA enumeration is parameterized by a type, which specifies the type for each enumerator. 123 \CFA allows any object type for the enumerators, and values assigned to enumerators must be inthe declared type.138 \CFA allows any object type for the enumerators, and values assigned to enumerators must be from the declared type. 124 139 \begin{lstlisting}[label=lst:color] 125 140 enum Colour( @char *@ ) { Red = "R", Green = "G", Blue = "B" }; 126 141 \end{lstlisting} 127 142 The type of @Colour@ is @char *@ and each enumerator is initialized with a C string. 128 Only types have define an ordering can be automatically initialized (see Section~\ref{s:AutoInitializable}). 129 143 Only types with a defined ordering can be automatically initialized (see Section~\ref{s:AutoInitializable}). 130 144 131 145 % An instance of \CFA-enum (denoted as @<enum_instance>@) is a label for the defined enum name. … … 133 147 % Similarly, the @value()@ function returns the value used to initialize the \CFA-enum. 134 148 135 A \CFA-enum is scoped: enumeration constants are not automatically exposed to the global scope. 136 Enumeration constant can be referenced using qualified expressions like an aggregate that supports qualified references to its fields. The syntax of $qualified\_expression$ for \CFA-enum is the following: 137 $$<qualified\_expression> := <enum\_type>.<enumerator>$$ 138 139 140 \subsection{enumeration instance} 141 \begin{lstlisting}[label=lst:sample_cforall_enum_usage] 142 Colour green = Colour.Green; 143 \end{lstlisting} 144 The ~\ref{lst:sample_cforall_enum_usage} example declares a $enumeration\ instance$ named @red@ and initializes it with $enumeration\ constant$ @Color.Red@. 145 An enumeration instance is a data structure that captures attributes of an enumeration constant, which can be retrieved by functions $position( enumeration\ instance )$, $value( enumeration\ instance )$, and $label( enumeration\ instance )$. 146 \begin{lstlisting} 147 int green_pos = position( green ); // 1 148 char * green_value = value( green ); // "G" 149 char * green_label = label( green ); // "Green" 150 \end{lstlisting} 151 An enumeration instance can be assigned to a variable or used as its position with type integer, its value with declared type T, or its label with type char *, and the compiler will resolve the usage as a type fits the context. 149 \subsection{Enumerator Scoping} 150 151 A \CFA-enum can be scoped, meaning the enumerator constants are not projected into the enclosing scope. 152 \begin{lstlisting} 153 enum Colour( char * ) @!@ { ... }; 154 \end{lstlisting} 155 where the @'!'@ implies the enumerators are \emph{not} projected. 156 The enumerators of a scoped enumeration are accessed using qualification, like the fields of an aggregate. 157 % The syntax of $qualified\_expression$ for \CFA-enum is the following: 158 % $$<qualified\_expression> := <enum\_type>.<enumerator>$$ 159 \begin{lstlisting} 160 Colour colour = @Colour.@Red; // qualification 161 colour = @Colour.@Blue; 162 \end{lstlisting} 163 164 \subsection{Enumerator Attributes} 165 166 The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@, i.e., like @sizeof@. 167 \begin{lstlisting} 168 int green_pos = @position@( Colour.Green ); // 1 169 char * green_value = @value@( Colour.Green ); // "G" 170 char * green_label = @label@( Colour.Green ); // "Green" 171 \end{lstlisting} 172 There are implicit conversions from an enumerator to its attributes. 152 173 \begin{lstlisting}[label=lst:enum_inst_assign_int] 153 int green_pos = green; // equivalent to position( green ); 154 \end{lstlisting} 155 A resolution of an enumeration constant is $unambigious$ if only one of the attributes has the resolvable type. 156 In example~\ref{lst:enum_inst_assign_int}, the right-hand side of the assignment expression expects integer type. 157 The position of an enumeration is @int@, while the other two cannot be resolved as integers. 158 The expression unambiguously returns the position of @green@. 159 \begin{lstlisting}[label=lst:enum_inst_assign_string] 160 char * green_value = green; // equivalent to value( green ); 161 \end{lstlisting} 162 On the other hand, the resolution of an enumeration constant is $ambigious$ if multiple attributes have the expected type. 163 In example~\ref{lst:enum_inst_assign_string}, both value and label have the expected type @char *@. 174 int green_pos = Colour.Green; // 1 175 char * green_value = Colour.Green; // ambiguous 176 char * green_label = Colour.Green; // ambiguous 177 \end{lstlisting} 178 where a conversion is ambiguous, if the enumerator's type is same as an attribute's type. 179 For example, @value( Colour.Green )@ and @label( Colour.Green )@ both have type @char *@. 180 Further examples are: 181 \begin{cquote} 182 \begin{tabular}{ll} 183 \begin{lstlisting} 184 int monday_pos = Monday; // ambiguous 185 int monday_value = Monday; // ambiguous 186 char * monday_label = Monday; // "Monday" 187 188 \end{lstlisting} 189 & 190 \begin{lstlisting} 191 enum(double) Math { PI = 3.14159, E = 2.718 }; 192 int pi_pos = PI; // 0 193 double pi_value = PI; // 3.14159 194 char * pi_label = PI; // "PI" 195 \end{lstlisting} 196 \end{tabular} 197 \end{cquote} 198 Here, @position( Monday )@ and @value( Monday )@ both have type @int@, while all attribute types are unique for enumerator @PI@. 199 164 200 When a resolution is ambiguous, a \textit{resolution precedence} applies: $$value > position > label$$ 165 201 \CFA uses resolution distance to describe if one type can be used as another. While \CFA calculates the resolution distance between the expected type and types of all three attributes, it would not choose the attribute with the closest distance. Instead, when resolving an enumeration constant, \CFA always chooses value whenever it is a possible resolution (resolution distance is not infinite), followed by position, then label. … … 170 206 In the example~\ref{lst:enum_inst_precedence}, while $position( Bar )$ has the closest resolution among the three attributes, $Foo.Bar$ is resolved as $value( Bar )$ because of the resolution precedence. 171 207 172 Although \CFA enumeration captures three different attributes, an instance of enumeration does not occupy extra memory. 173 The @sizeof@ \CFA enumeration instance is always 4 bytes, the same amount of memory to store a C enumeration instance. 208 \PAB{Not sure this is going to work.} 209 210 \subsection{Enumerator Storage} 211 212 Although \CFA enumeration captures three different attributes, an enumeration instance does not store all this information. 213 The @sizeof@ a \CFA enumeration instance is always 4 bytes, the same size as a C enumeration instance (@sizeof( int )@). 174 214 It comes from the fact that: 175 215 \begin{enumerate} … … 179 219 it is always resolved as one of its attributes in terms of real usage. 180 220 \end{enumerate} 181 When creating the enumeration instance green and assigns it with the enumeration constant @Color.Green@, the compilers essentially allocate an integer variables and storethe position 1.221 When creating an enumeration instance @colour@ and assigning it with the enumerator @Color.Green@, the compiler allocates an integer variable and stores the position 1. 182 222 The invocations of $positions()$, $value()$, and $label()$ turn into calls to special functions defined in the prelude: 183 223 \begin{lstlisting}[label=lst:companion_call] … … 195 235 196 236 \begin{lstlisting}[caption={Enum Type Functions}, label=lst:cforall_enum_functions] 197 forall( T ) {198 199 200 const char** const labels;201 202 203 }204 \ end{lstlisting}205 \CFA creates an object of Companion for every \CFA-enumeration.A companion object has the same name as the enumeration is defined for.237 forall( T ) 238 struct Companion { 239 const T * const values; 240 const char ** const labels; 241 int length; 242 }; 243 \end{lstlisting} 244 \CFA creates a @Companion@ object for every \CFA enumeration. 245 A companion object has the same name as the enumeration is defined for. 206 246 A companion object stores values and labels of enumeration constants, in the order of the constants defined in the enumeration. 207 247 208 \CFA generates the definition of companion functions. Because \CFA implicitly stores enumeration instance as its position, the companion function $position$ does nothing but returns the position it passes it. 209 Companions function $value$ and $label$ return the array item at the given position of $values$ and $labels$, respectively. 248 \CFA generates the definition of companion functions. 249 Because \CFA implicitly stores enumeration instance as its position, the companion function @position@ does nothing but return the position it is passed. 250 Companions function @value@ and @label@ return the array item at the given position of @values@ and @labels@, respectively. 210 251 \begin{lstlisting}[label=lst:companion_definition] 211 252 int position( Companion o, int pos ) { return pos; } … … 213 254 char * label( Companion o, int pos ) { return o.labels[ pos ]; } 214 255 \end{lstlisting} 215 Notably, the Companion structure definition, and all companion objects, are visible to theusers.216 A user can retrieve values and labels defined in an enumeration by accessing the values and labels directly, or indirectly by calling Companion functions $values$ and $labels$256 Notably, the @Companion@ structure definition, and all companion objects, are visible to users. 257 A user can retrieve values and labels defined in an enumeration by accessing the values and labels directly, or indirectly by calling @Companion@ functions @values@ and @labels@ 217 258 \begin{lstlisting}[label=lst:companion_definition_values_labels] 218 259 Colour.values; // read the Companion's values 219 values( Colour ); // Same as Colour.values260 values( Colour ); // same as Colour.values 220 261 \end{lstlisting} 221 262 222 263 \subsection{User Define Enumeration Functions} 223 264 224 The Companion objects make extending features for \CFA enumeration easy. 225 265 Companion objects make extending features for \CFA enumeration easy. 226 266 \begin{lstlisting}[label=lst:companion_user_definition] 227 267 char * charastic_string( Companion o, int position ) { 228 return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) );268 return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) ); 229 269 } 230 270 printf( charactic_string ( Color, 1 ) ); 231 >>> Label: G ; Value: G271 >>> Label: Green; Value: G 232 272 \end{lstlisting} 233 273 Defining a function takes a Companion object effectively defines functions for all \CFA enumeration. … … 236 276 Therefore, a user can use the syntax with a user-defined enumeration function call: 237 277 \begin{lstlisting}[label=lst:companion_user_definition] 238 charactic_string ( Color.Green ); // equivalent to charactic_string( Color, 1 )239 >>> Label: G ; Value: G278 charactic_string( Color.Green ); // equivalent to charactic_string( Color, 1 ) 279 >>> Label: Green; Value: G 240 280 \end{lstlisting} 241 281 Similarly, the user can work with the enumeration type itself: (see section ref...) … … 251 291 \subsection{Runtime Enumeration} 252 292 253 The Companion structure definition is visible to users, and users can create an instance of Companion object themselves, which effectively constructs a \textit{Runtime Enumeration}.293 The companion structure definition is visible to users, and users can create an instance of companion object themselves, which effectively constructs a \textit{Runtime Enumeration}. 254 294 \begin{lstlisting}[ label=lst:runtime_enum ] 255 const char values[ ] = { "Hello", "World" };256 const char labels[ ] = { "First", "Second" };257 Companion 295 const char values[$\,$] = { "Hello", "World" }; 296 const char labels[$\,$] = { "First", "Second" }; 297 Companion(char *) MyEnum = { .values: values, .labels: labels, .length: 2 }; 258 298 \end{lstlisting} 259 299 A runtime enumeration can be used to call enumeration functions. … … 267 307 // and MyEnum.First is not recognizable 268 308 \end{lstlisting} 269 During the compilation, \CFA adds enumeration declarations to an enumeration symbol table and creates specialized function definitions for \CFA enumeration. \CFA does not recognize runtime enumeration during compilation and would not add them to the enumeration symbol table, resulting in a lack of supports for runtime enumeration. 309 During the compilation, \CFA adds enumeration declarations to an enumeration symbol table and creates specialized function definitions for \CFA enumeration. 310 \CFA does not recognize runtime enumeration during compilation and would not add them to the enumeration symbol table, resulting in a lack of supports for runtime enumeration. 311 312 \PAB{Not sure how useful this feature is.} 270 313 271 314 \section{Enumeration Features} 272 315 273 A trait is a collection of constraints in \CFA , whichcan be used to describe types.316 A trait is a collection of constraints in \CFA that can be used to describe types. 274 317 The \CFA standard library defines traits to categorize types with related enumeration features. 275 318 … … 279 322 TODO: make the initialization rule a separate section. 280 323 281 If no explicit initializer is given to an enumeration constant, C initializes the first enumeration constant with value 0, and the other enumeration constant has a value equal to its $predecessor+1$.324 If no explicit initializer is given to an enumeration constant, C initializes the first enumeration constant with value 0, and the next enumeration constant has a value equal to its $predecessor + 1$. 282 325 \CFA enumerations have the same rule in enumeration constant initialization. 283 However, not all types can be automatically initialized by \CFA because the meaning of $zero$, $one$, and addition operator may not be well-defined.284 285 A type is auto-Initializable if it has defined @zero_t@, @one_t@, and an addition operator. 326 However, only \CFA types that have defined traits for @zero_t@, @one_t@, and an addition operator can be automatically initialized by \CFA. 327 328 Specifically, a type is auto-initializable only if it satisfies the trait @AutoInitializable@: 286 329 \begin{lstlisting} 287 330 forall(T) … … 292 335 }; 293 336 \end{lstlisting} 294 An example of user-defined @AutoInitializable@ would look like the following:337 An example of a user-defined @AutoInitializable@ is: 295 338 \begin{lstlisting}[label=lst:sample_auto_Initializable] 296 339 struct Odd { int i; }; 297 340 void ?()( Odd & t, zero_t ) { t.i = 1; }; 298 341 void ?()( Odd & t, one_t ) { t.i = 2; }; 299 Odd ?+?( Odd t1, Odd t2 ) 300 { return Odd( t1.i + t2.i); }; 301 \end{lstlisting} 302 When an enumeration declares an AutoInitializable as its type, no explicit initialization is necessary. 342 Odd ?+?( Odd t1, Odd t2 ) { return Odd( t1.i + t2.i); }; 343 \end{lstlisting} 344 When the type of an enumeration is @AutoInitializable@, implicit initialization is available. 303 345 \begin{lstlisting}[label=lst:sample_auto_Initializable_usage] 304 346 enum AutoInitUsage(Odd) { 305 A, B, C = 6, D 306 }; 307 \end{lstlisting} 308 309 In the example~\ref{lst:sample_auto_Initializable_usage_gen}, because no initializer is specified for the first enumeration constant @A@, \CFA initializes it with the value of @zero_t@, which is 1. 310 @B@ and @D@ have the values of their $predecessor + one_t$, while @one_t@ has the value 2. 311 Therefore, the enumeration is initialized as the following: 347 A, B, C = 7, D 348 }; 349 \end{lstlisting} 350 In the example, there is no initializer specified for the first enumeration constant @A@, so \CFA initializes it with the value of @zero_t@, which is 1. 351 @B@ and @D@ have the values of their $predecessor + one_t$, where @one_t@ has the value 2. 352 Therefore, the enumeration is initialized as follows: 312 353 \begin{lstlisting}[label=lst:sample_auto_Initializable_usage_gen] 313 354 enum AutoInitUsage(Odd) { 314 A=1, B=3, C = 6, D=8 315 }; 316 \end{lstlisting} 317 In \CFA, integral types, float types, and imaginary numbers are example types that are AutoInitialiable. 355 A = 1, B = 3, C = 7, D = 9 356 }; 357 \end{lstlisting} 358 Note, there is no mechanism to prevent an even value for the direct initialization, such as @C = 6@. 359 360 In \CFA, character, integral, float, and imaginary types are all @AutoInitialiable@. 318 361 \begin{lstlisting}[label=lst:letter] 319 enum Alphabet(int) { 320 A='A', B, C, D, E, F, G, H, I, J, K, L, M, 321 N, O, P, Q, R, S, T, U, V, W, X, Y, Z, 322 a='a', b, c, d, e, f, g, h, i, j, k, l, m, 323 n, o, p, q, r, s, t, u, v, w, x, y, z 324 }; 325 print( "%c, %c, %c", Alphabet.F, Alphabet.o, Alphabet.o ); 326 >>> F, o, o 362 enum Alphabet( int ) { 363 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, 364 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 365 }; 366 print( "%c, %c, %c", Alphabet.F, Alphabet.o, Alphabet.z ); 367 >>> F, o, z 327 368 \end{lstlisting} 328 369 329 370 \subsection{Iteration and Range} 330 371 331 It is convenient to iterate over a \CFA enumeration. 332 Here is the basic usage: 372 It is convenient to iterate over a \CFA enumeration, e.g.: 333 373 \begin{lstlisting}[label=lst:range_functions] 334 for ( Alphabet ah; Alphabet; ) { 335 printf( "%d ", ah ); 336 } 337 >>> A B C (...omit the rest) 338 \end{lstlisting} 339 The for-loop uses the enumeration type @Alphabet@ as range. 340 When that happens, \CFA iterates all enumerators in the order they defined in the enumeration. 341 @'ch'@ is the iterating enumerator, and it returns the value of an Alphabet in this context according to the precedence rule. 374 for ( Alphabet alph; Alphabet ) { 375 printf( "%d ", alph ); 376 } 377 >>> A B C ... 378 \end{lstlisting} 379 The for-loop uses the enumeration type @Alphabet@ its range, and iterates through all enumerators in the order defined in the enumeration. 380 @alph@ is the iterating enumeration object, which returns the value of an @Alphabet@ in this context according to the precedence rule. 342 381 343 382 \CFA offers a shorthand for iterating all enumeration constants: 344 383 \begin{lstlisting}[label=lst:range_functions] 345 for ( Alphabet ch ) { 346 printf( "%d ", ch ); 347 } 348 >>> A B C (...omit the rest) 349 \end{lstlisting} 350 351 Enumeration supports the \CFA loop control syntax for for-loop. 384 for ( Alphabet alph ) { 385 printf( "%d ", alph ); 386 } 387 >>> A B C ... 388 \end{lstlisting} 389 The following different loop-control syntax is supported: 352 390 \begin{lstlisting}[label=lst:range_functions] 353 391 for ( Alphabet.D ) 354 for ( ch; Alphabet.g ~ Alphabet.z ) 355 for ( Alphabet ch; Alphabet.R ~ Alphabet.X ~ 2 ) 356 \end{lstlisting} 357 Notably, the meaning of "step" of iteration has changed for enumeration. 392 for ( alph; Alphabet.g ~ Alphabet.z ) 393 for ( Alphabet alph; Alphabet.R ~ Alphabet.X ~ 2 ) 394 \end{lstlisting} 395 \PAB{Explain what each loop does.} 396 Notably, the meaning of ``step'' for an iteration has changed for enumeration. 358 397 Consider the following example: 359 398 \begin{lstlisting}[label=lst:range_functions] … … 364 403 printf( "%d ", s ); 365 404 } 366 367 405 >>> 10 12 14 368 406 … … 378 416 It is also possible to iterate over an enumeration's labels, implicitly or explicitly: 379 417 \begin{lstlisting}[label=lst:range_functions_label_implicit] 380 for ( char * ch; Alphabet )418 for ( char * alph; Alphabet ) 381 419 \end{lstlisting} 382 420 This for-loop implicitly iterates every label of the enumeration, because a label is the only valid resolution to the ch with type @char *@ in this case. … … 399 437 During the $Validation$ pass, the compiler links the type declaration to the type's definition. 400 438 It ensures that the name of an enumerator is unique within the enumeration body, and checks if all values of the enumerator have the declaration type. 401 If the declared type is not @Auto 439 If the declared type is not @AutoInitializable@, \CFA rejects the enumeration definition. 402 440 Otherwise, it attempts to initialize enumerators with the enumeration initialization pattern. (a reference to a future initialization pattern section) 403 441 … … 429 467 But the result is computed at run time, and the compiler ensures the @values@ are not changed. 430 468 431 \subsection{ qualified expression}469 \subsection{Qualified Expression} 432 470 433 471 \CFA uses qualified expression to address the scoping of \CFA-enumeration. … … 441 479 If the @aggregation_name@ is identified as a \CFA enumeration, the compiler checks if @field@ presents in the declared \CFA enumeration. 442 480 443 \subsection{\lstinline{with} statement/statement}481 \subsection{\lstinline{with} Statement} 444 482 445 483 \emph{Work in Progress} 446 484 447 Instead of qualifying an enumeration expression every time, one can use the @with@ to expose enumerators to the current scope so that they aredirectly accessible.485 Instead of qualifying an enumeration expression every time, the @with@ can be used to expose enumerators to the current scope, making them directly accessible. 448 486 449 487 \subsection{Instance Declaration}
Note: See TracChangeset
for help on using the changeset viewer.