Changeset c141c09 for doc


Ignore:
Timestamp:
Aug 2, 2024, 12:32:18 AM (5 hours ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Parents:
3a7cd15
Message:

Update

Location:
doc/theses/jiada_liang_MMath
Files:
2 edited

Legend:

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

    r3a7cd15 rc141c09  
    1 \chapter{C Enum in CFA}
     1\chapter{C Enumeration in CFA}
    22
    3 \CFA supports C enumeration using the same syntax for backwards compatibility, and the semantics is mostly consistent with C with small difference in terms of typing.
    4 C style enums in \CFA language are called "C enum".
    5 \CFA also extends C-Style enumeration by adding a number of new features that bring enumerations inline with other modern programming languages.
     3\CFA supports legacy C enumeration using the same syntax for backwards compatibility.
     4C-style Enumeration in \CFA language are called \newterm{C Enumeration} or \newterm{C Enum}.
     5The semantics of C Enumeration is mostly consistent with C with more restrictive typing.
     6\CFA also extends C Enumeration by adding a number of new features that bring enumerations aligns with other modern programming languages.
    67Any enumeration extensions must be intuitive to C programmers both in syntax and semantics.
    78The following sections detail all of my new contributions to enumerations in \CFA.
     
    2425enum E1 { First, Second, Third, Fourth };
    2526enum E2 { @Fourth@, @Third@, @Second@, @First@ }; $\C{// same enumerator names}$
    26 E1 f() { return Third; }                                $\C{// overloaded functions, different return types}$
     27E1 f() { return Third; }                                $\C{// overload functions with different return types}$
    2728E2 f() { return Fourth; }
    2829void g( E1 e );
     
    7172\section{Type Safety}
    7273
    73 As in Section~\ref{s:Usage}, C's implicit conversion between enumeration and integral type raises a safety concern.
    74 \CFA disallows an implicit conversion from integral type to enueration, and conversion between different C enumeration type.
    75 It loses some degree of its backward compatibility to C, in exchange for type safety.
     74As in Section~\ref{s:Usage}, C's implicit bidirectional conversion between enumeration and integral type raises a safety concern.
     75In \CFA, the conversion is unidirectional:
     76% disallows an implicit conversion from integral type to enumeration, and conversion between different C enumeration type.
     77% It loses some degree of its backward compatibility to C, in exchange for type safety.
     78an enumeration can be implicitly converted into an integral type, with an associated @safe@ conversion cost.
     79But an integral type cannot be implicitly converted into a C enumeration. (Conversion Cost is Infinity.)
    7680\begin{cfa}
    7781enum Bird { Pengin, Robin, Eagle };
     
    8185@enum Bird bird = Shark;@ $\C{// Disallow }$
    8286@enum Bird bird = 1;@  $\C{// Disallow }$
    83 
     87\end{cfa}
     88As a workaround, \CFA allows an explicit cast to an enumeration, turning an integral type to an enumeration that can be used in assignment or function argument,
     89in which case \CFA treats C enuemration as its underlying integral type. In such cases, it is up to user to ensure program correctness.
     90\begin{cfa}
    8491@enum Bird bird = (Bird) Shark@
    8592@enum Bird bird = (Bird) 1;@
    8693\end{cfa}
    87 As a workaround, \CFA allows explicit cast to an enumeration, turning an integral type to an enumeration that can be used in assignment or function argument,
    88 in which case \CFA treats C enuemration as its underlying integral type. In such cases, it is up to user to ensure program correctness.
  • doc/theses/jiada_liang_MMath/CFAenum.tex

    r3a7cd15 rc141c09  
    202202
    203203\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).
    204 Inheritance can be nested, and a \CFA enumeration can inline enumerators from more than one \CFA enumeration, forming a tree-like structure.
    205 Howver, the uniqueness of enumeration label applies to enumerators from supertypes, meaning an enumeration cannot name enumerator with the same label as its subtype's members, or inherits
    206 from multiple enumeration that has overlapping enumerator label. As a consequence, a new type cannot inherits both an enumeration and its supertype, or inherit two enumerations with a
     204Inheritance can be nested, and a \CFA enumeration can inline enumerators from more than one \CFA enumeration, forming a tree-like hierarchy.
     205However, 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
     206from 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
    207207common supertype (the diamond problem), since such would unavoidably introduce duplicate enumerator labels.
    208208
     
    227227\end{cfa}
    228228
    229 The enumeration type for the inheriting type must be the same as the inherited type.
     229% The enumeration base for the subtype must be the same as the super type.
     230The base type must be consistent between subtype and supertype.
    230231When an enumeration inherits enumerators from another enumeration, it copies the enumerators' @value@ and @label@, even if the @value@ was auto initialized. However, the @position@ as the underlying
    231232representation will be the order of the enumerator in new enumeration.
     
    254255Lepidosauromorpha s = Snake;
    255256\end{cfa}
    256 The last expression in the preceding example is umabigious. While both @Squamata.Snake@ and @Lepidosauromorpha.Snake@ are valid candidate, @Squamata.Snake@ has
     257The last expression in the preceding example is unambiguous. While both @Squamata.Snake@ and @Lepidosauromorpha.Snake@ are valid candidate, @Squamata.Snake@ has
    257258an associated safe cost and \CFA select the zero cost candidate @Lepidosauromorpha.Snake@.
    258259
     
    414415To support some level of harmonizing in these cases, an array dimension can be defined using an enumerator type, and the enumerators used as subscripts.
    415416\begin{cfa}
    416 enum E { A, B, C, N }; // possibly predefined
    417 float H1[N] = { [A] : 3.4, [B] : 7.1, [C] : 0.01 }; // C
    418 float H2[@E@] = { [A] : 3.4, [B] : 7.1, [C] : 0.01 }; // CFA
    419 \end{cfa}
    420 (Note, C uses the symbol, @'='@ for designator initialization, but \CFA had to change to @':'@ because of problems with tuple syntax.)
     417enum E1 { A, B, C, N }; // possibly predefined
     418enum(int) E2 { A, B, C };
     419float H1[N] = { [A] :$\footnote{Note, C uses the symbol, @'='@ for designator initialization, but \CFA had to change to @':'@ because of problems with tuple syntax.}$ 3.4, [B] : 7.1, [C] : 0.01 }; // C
     420float H2[@E2@] = { [A] : 3.4, [B] : 7.1, [C] : 0.01 }; // CFA
     421\end{cfa}
    421422This approach is also necessary for a predefined typed enumeration (unchangeable), when additional secondary-information need to be added.
    422423
     424The array subscript operator, namely ?[?], has been overloaded so that when a CFA enumerator is used as an array index, it implicitly converts
     425to its position over value to sustain data hormonization. User can revert the behaviour by:
     426\begin{cfa}
     427float ?[?](float * arr, E2 index) { return arr[value(index)]; }
     428\end{cfa}
     429When an enumeration type is being used as an array dimension, \CFA add the enumeration type to initializer's context. As a result,
     430@H2@'s array destinators @A@, @B@ and @C@ are resolved unambiguously to type E2. (H1's destinators are also resolved unambiguously to
     431E1 because E2 has a @value@ cost to @int@).
    423432
    424433\section{Enumeration I/O}
Note: See TracChangeset for help on using the changeset viewer.