Ignore:
Timestamp:
Aug 8, 2024, 10:02:34 PM (2 months ago)
Author:
Peter A. Buhr <pabuhr@…>
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)
Message:

last proofread of thesis

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/CFAenum.tex

    r11cced6 rc1c0efdb  
    177177
    178178
    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.
     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.
    199199
    200200
     
    263263posn( e2 );                     $\C[1.75in]{// 0}$
    264264enum E3 e3 = e2;        $\C{// Assignment with enumeration conversion E2 to E3}$
    265 posn( e2 );                     $\C{// 1 }$
     265posn( e2 );                     $\C{// 1 cost}$
    266266void foo( E3 e );
    267267foo( e2 );                      $\C{// Type compatible with enumeration conversion E2 to E3}$
    268268posn( (E3)e2 );         $\C{// Explicit cast with enumeration conversion E2 to E3}$
    269269E3 e31 = B;                     $\C{// No conversion: E3.B}$
    270 posn( e31 );            $\C{// 0 }\CRT$
     270posn( e31 );            $\C{// 0 cost}\CRT$
    271271\end{cfa}
    272272The 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@.
     273While 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@.
    274275
    275276For the given function prototypes, the following calls are valid.
     
    449450\footnotetext{C uses symbol \lstinline{'='} for designator initialization, but \CFA changes it to \lstinline{':'} because of problems with tuple syntax.}
    450451This approach is also necessary for a predefined typed enumeration (unchangeable), when additional secondary-information need to be added.
    451 
    452452The 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.
    453453This behaviour can be reverted by explicit overloading:
     
    455455float ?[?]( float * arr, E2 index ) { return arr[ value( index ) ]; }
    456456\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.
     457While enumerator labels @A@, @B@ and @C@ are being defined twice in different enumerations, they are unambiguous within the context.
     458Designators 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@.
     459Designators 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
    461461
    462462\section{I/O}
     463
    463464As 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.
    464465However, very few programming languages provide a mechanism to read in enumerator values.
Note: See TracChangeset for help on using the changeset viewer.