Changeset c141c09 for doc/theses
- Timestamp:
- Aug 2, 2024, 12:32:18 AM (5 months ago)
- Branches:
- master
- Children:
- a9ae5ca, b12e4ad
- Parents:
- 3a7cd15
- 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} 2 2 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. 4 C-style Enumeration in \CFA language are called \newterm{C Enumeration} or \newterm{C Enum}. 5 The 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. 6 7 Any enumeration extensions must be intuitive to C programmers both in syntax and semantics. 7 8 The following sections detail all of my new contributions to enumerations in \CFA. … … 24 25 enum E1 { First, Second, Third, Fourth }; 25 26 enum E2 { @Fourth@, @Third@, @Second@, @First@ }; $\C{// same enumerator names}$ 26 E1 f() { return Third; } $\C{// overload ed functions,different return types}$27 E1 f() { return Third; } $\C{// overload functions with different return types}$ 27 28 E2 f() { return Fourth; } 28 29 void g( E1 e ); … … 71 72 \section{Type Safety} 72 73 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. 74 As in Section~\ref{s:Usage}, C's implicit bidirectional conversion between enumeration and integral type raises a safety concern. 75 In \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. 78 an enumeration can be implicitly converted into an integral type, with an associated @safe@ conversion cost. 79 But an integral type cannot be implicitly converted into a C enumeration. (Conversion Cost is Infinity.) 76 80 \begin{cfa} 77 81 enum Bird { Pengin, Robin, Eagle }; … … 81 85 @enum Bird bird = Shark;@ $\C{// Disallow }$ 82 86 @enum Bird bird = 1;@ $\C{// Disallow }$ 83 87 \end{cfa} 88 As 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, 89 in 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} 84 91 @enum Bird bird = (Bird) Shark@ 85 92 @enum Bird bird = (Bird) 1;@ 86 93 \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 202 202 203 203 \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 How ver, the uniqueness of enumeration label applies to enumeratorsfrom supertypes, meaning an enumeration cannot name enumerator with the same label as its subtype's members, or inherits206 from multiple enumeration that has overlapping enumerator label. As a consequence, a new type cannot inherits both an enumeration and its supertype, or inherittwo enumerations with a204 Inheritance can be nested, and a \CFA enumeration can inline enumerators from more than one \CFA enumeration, forming a tree-like hierarchy. 205 However, 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 206 from 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 207 207 common supertype (the diamond problem), since such would unavoidably introduce duplicate enumerator labels. 208 208 … … 227 227 \end{cfa} 228 228 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. 230 The base type must be consistent between subtype and supertype. 230 231 When 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 231 232 representation will be the order of the enumerator in new enumeration. … … 254 255 Lepidosauromorpha s = Snake; 255 256 \end{cfa} 256 The last expression in the preceding example is u mabigious. While both @Squamata.Snake@ and @Lepidosauromorpha.Snake@ are valid candidate, @Squamata.Snake@ has257 The last expression in the preceding example is unambiguous. While both @Squamata.Snake@ and @Lepidosauromorpha.Snake@ are valid candidate, @Squamata.Snake@ has 257 258 an associated safe cost and \CFA select the zero cost candidate @Lepidosauromorpha.Snake@. 258 259 … … 414 415 To support some level of harmonizing in these cases, an array dimension can be defined using an enumerator type, and the enumerators used as subscripts. 415 416 \begin{cfa} 416 enum E { A, B, C, N }; // possibly predefined417 float H1[N] = { [A] : 3.4, [B] : 7.1, [C] : 0.01 }; // C 418 float H 2[@E@] = { [A] : 3.4, [B] : 7.1, [C] : 0.01 }; // CFA419 \end{cfa} 420 (Note, C uses the symbol, @'='@ for designator initialization, but \CFA had to change to @':'@ because of problems with tuple syntax.) 417 enum E1 { A, B, C, N }; // possibly predefined 418 enum(int) E2 { A, B, C }; 419 float 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 420 float H2[@E2@] = { [A] : 3.4, [B] : 7.1, [C] : 0.01 }; // CFA 421 \end{cfa} 421 422 This approach is also necessary for a predefined typed enumeration (unchangeable), when additional secondary-information need to be added. 422 423 424 The array subscript operator, namely ?[?], has been overloaded so that when a CFA enumerator is used as an array index, it implicitly converts 425 to its position over value to sustain data hormonization. User can revert the behaviour by: 426 \begin{cfa} 427 float ?[?](float * arr, E2 index) { return arr[value(index)]; } 428 \end{cfa} 429 When 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 431 E1 because E2 has a @value@ cost to @int@). 423 432 424 433 \section{Enumeration I/O}
Note: See TracChangeset
for help on using the changeset viewer.