Changeset c1c0efdb for doc/theses/jiada_liang_MMath/CFAenum.tex
- Timestamp:
- Aug 8, 2024, 10:02:34 PM (2 months ago)
- Branches:
- master
- Children:
- 7568e5c
- Parents:
- 11cced6
- git-author:
- Peter A. Buhr <pabuhr@…> (08/08/24 22:01:56)
- git-committer:
- Peter A. Buhr <pabuhr@…> (08/08/24 22:02:34)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
r11cced6 rc1c0efdb 177 177 178 178 179 \section{Auto Initialization}180 181 A partially implemented feature is auto-initialization, which works for the C integral type with constant expressions.182 \begin{cfa}183 enum Week { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }; // 0-2, 10-13184 \end{cfa}185 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.186 187 If \CFA had powerful compilation expression evaluation, auto initialization would be implemented as follows.188 \begin{cfa}189 enum E(T) { A, B, C };190 \end{cfa}191 \begin{enumerate}192 \item the first enumerator, @A@, is initialized with @T@'s @zero_t@.193 \item otherwise, the next enumerator is initialized with the previous enumerator's value using operator @?++@, where @?++( T )@ can be overloaded for any type @T@.194 \end{enumerate}195 196 Unfortunately, constant expressions in C are not powerful and \CFA is only a transpiler, relying on generated C code to perform the detail work.197 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.198 Nevertheless, the necessary language concepts exist to support this feature.179 % \section{Auto Initialization} 180 % 181 % A partially implemented feature is auto-initialization, which works for the C integral type with constant expressions. 182 % \begin{cfa} 183 % enum Week { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }; // 0-2, 10-13 184 % \end{cfa} 185 % 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. 186 % 187 % If \CFA had powerful compilation expression evaluation, auto initialization would be implemented as follows. 188 % \begin{cfa} 189 % enum E(T) { A, B, C }; 190 % \end{cfa} 191 % \begin{enumerate} 192 % \item the first enumerator, @A@, is initialized with @T@'s @zero_t@. 193 % \item otherwise, the next enumerator is initialized with the previous enumerator's value using operator @?++@, where @?++( T )@ can be overloaded for any type @T@. 194 % \end{enumerate} 195 % 196 % Unfortunately, constant expressions in C are not powerful and \CFA is only a transpiler, relying on generated C code to perform the detail work. 197 % 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. 198 % Nevertheless, the necessary language concepts exist to support this feature. 199 199 200 200 … … 263 263 posn( e2 ); $\C[1.75in]{// 0}$ 264 264 enum E3 e3 = e2; $\C{// Assignment with enumeration conversion E2 to E3}$ 265 posn( e2 ); $\C{// 1 }$265 posn( e2 ); $\C{// 1 cost}$ 266 266 void foo( E3 e ); 267 267 foo( e2 ); $\C{// Type compatible with enumeration conversion E2 to E3}$ 268 268 posn( (E3)e2 ); $\C{// Explicit cast with enumeration conversion E2 to E3}$ 269 269 E3 e31 = B; $\C{// No conversion: E3.B}$ 270 posn( e31 ); $\C{// 0 }\CRT$270 posn( e31 ); $\C{// 0 cost}\CRT$ 271 271 \end{cfa} 272 272 The last expression is unambiguous. 273 While both @E2.B@ and @E3.B@ are valid candidates, @E2.B@ has an associated safe cost and @E3.B@ need not a conversion (@zero@ cost). \CFA selects the lowest cost candidate @E3.B@. 273 While both @E2.B@ and @E3.B@ are valid candidates, @E2.B@ has an associated safe cost and @E3.B@ needs no conversion (@zero@ cost). 274 \CFA selects the lowest cost candidate @E3.B@. 274 275 275 276 For the given function prototypes, the following calls are valid. … … 449 450 \footnotetext{C uses symbol \lstinline{'='} for designator initialization, but \CFA changes it to \lstinline{':'} because of problems with tuple syntax.} 450 451 This approach is also necessary for a predefined typed enumeration (unchangeable), when additional secondary-information need to be added. 451 452 452 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. 453 453 This behaviour can be reverted by explicit overloading: … … 455 455 float ?[?]( float * arr, E2 index ) { return arr[ value( index ) ]; } 456 456 \end{cfa} 457 While enumerator labels @A@, @B@ and @C@ are being defined twice in different enumerations, they are 458 unambiguous within the context. Designators in H1 are unambiguous becasue @E2@ has a @value@ cost to @int@, which 459 is more expensive than @safe@ cost from C-Enum @E1@ to @int@. On the hand, designators in @H2@ are resolved as @E2@ because 460 when a \CFA enumeration type is being used as an array dimension, \CFA adds the enumeration type to the initializer's resolution context. 457 While enumerator labels @A@, @B@ and @C@ are being defined twice in different enumerations, they are unambiguous within the context. 458 Designators 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@. 459 Designators 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. 460 461 461 462 462 \section{I/O} 463 463 464 As 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. 464 465 However, very few programming languages provide a mechanism to read in enumerator values.
Note: See TracChangeset
for help on using the changeset viewer.