| [ea89e36] | 1 | # Plan: CFA Typed Enum 
 | 
|---|
 | 2 | ## Introduction
 | 
|---|
 | 3 | CFA allows users to define enum with any type, and an enum's value is a subset of its supertype. The basic syntax of defining a typed enum is the follows:
 | 
|---|
 | 4 | 
 | 
|---|
 | 5 | <div align="center">
 | 
|---|
 | 6 | <i>enum(Typename) EnumName<sub>opt</sub> = { enumerator-list }</i>
 | 
|---|
 | 7 | </div> or
 | 
|---|
 | 8 | , where <i>enumerator-list</i> is a list of <i>identifier</i> or <i>identifier = const-expression</i>.  
 | 
|---|
 | 9 | <div>
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | </div>
 | 
|---|
 | 12 | 
 | 
|---|
 | 13 | ## CFA Typed Enum vs C Enum vs C++ Enum
 | 
|---|
 | 14 | In C, an enum variable declaration is isomorphic to an integer declaration. For example,  
 | 
|---|
 | 15 | <div align="center">
 | 
|---|
 | 16 | <i>enum EnumType VariableName = EnumValue;</i>
 | 
|---|
 | 17 | </div>
 | 
|---|
 | 18 | is equivalent to
 | 
|---|
 | 19 | <div align="center">
 | 
|---|
 | 20 | <i>int VariableName = EnumValue;</i>
 | 
|---|
 | 21 | </div>
 | 
|---|
 | 22 | For backward capatability, we perserve this behaviour in CFA. This is different from an C++ interpretation. C++ disallow an assignment to an enum variable with a value from the base type without an explicit cast. However, if a user defines an enum E using the typed syntax:
 | 
|---|
 | 23 | <div align="center">
 | 
|---|
 | 24 | <i>enum E(int) = EnumValue;</i>
 | 
|---|
 | 25 | </div>
 | 
|---|
 | 26 | Now <i>E</i> is a proper subset of <i>int</i> type. The compiler will stop you from assignment an integer value of a variable with type E, as in C++.</p>
 | 
|---|
 | 27 | Since C++11, C++ supports enum definition with an enum base.
 | 
|---|
 | 28 | <div align="center">
 | 
|---|
 | 29 | <i>enum EnumName : enum-base = { enumerator-list };</i>
 | 
|---|
 | 30 | </div>
 | 
|---|
 | 31 | An enum base is a sequence of basic integral type specifier.
 | 
|---|
 | 32 | <div align="center">
 | 
|---|
 | 33 | <i>
 | 
|---|
 | 34 | enum E: char {A='a'};<br/>
 | 
|---|
 | 35 | E e = A; <br/>
 | 
|---|
 | 36 | cout << e << end;
 | 
|---|
 | 37 | </i>
 | 
|---|
 | 38 | </div>
 | 
|---|
 | 39 | Will print a character 'a' to the output instead of 97.</br>
 | 
|---|
 | 40 | CFA extends this idea to allows a typed enum to have an non-intergral or non-basic type. You can create an enum with a base type floating number, pointer, tuple, structure, function pointer, another enum, and more.  
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | ## Implementation / Todo
 | 
|---|
 | 43 | ### 0. Grammar  
 | 
|---|
 | 44 | Enumerator_value_opt:  
 | 
|---|
 | 45 |         // empty  
 | 
|---|
 | 46 |         // | '=' constant_expression  
 | 
|---|
 | 47 |         //      { $$ = $2; }  
 | 
|---|
 | 48 |         | simple_assignment_operator initializer                                        // FIX ME: enum only deals with constant_expression
 | 
|---|
 | 49 |         ;  
 | 
|---|
 | 50 | Discussion: FIX ME item.  
 | 
|---|
 | 51 | ### 1. Integral Type:
 | 
|---|
 | 52 | $\forall$ enum_expression :  <i>enum($T$) { enumerator_list comma<sub>opt</sub> }</i>,  
 | 
|---|
 | 53 | where $T \in IntegralTypes.$  
 | 
|---|
 | 54 | For $enumerator\_list \rightarrow$<sub>derive</sub> $identifier = constant\_expression$:   
 | 
|---|
 | 55 |  $Code(enumerator\_list) = Code(const\ T \ identifier = constant\_expression)$  
 | 
|---|
 | 56 | For $enumerator\_list \rightarrow$<sub>derive</sub> $identifier_k$ :   
 | 
|---|
 | 57 |  $Code(enumerator\_list) = Code(const\ T \ identifier_k = identifier_{k-1}+1)(k>0)$  
 | 
|---|
 | 58 |  $Code(enumerator\_list) = Code(const\ T \ identifier_k = 0)(k=0)$  
 | 
|---|
 | 59 |   
 | 
|---|
 | 60 | During the code compilation, probably during the <i>static type checking</i> we save all the enum element <b>Names</b>. A compiler throws error if an assignment to an enum variable with a expression that is not an enum name.  
 | 
|---|
 | 61 | $enum\ T(int) \{A =\ 'a'\}$  
 | 
|---|
 | 62 | $T\ Name =\ 'a' $// Error: Cannot assign a variable of type 'T' with a value of type 'char'.  
 | 
|---|
 | 63 | It behaves the same as C++ and many other languages.
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 | ### 2. Non-Integral Type  
 | 
|---|
 | 67 | Syntax: 
 | 
|---|
 | 68 | $enumerator\_list \rightarrow$<sub>derive</sub> $identifier = constant\_expression$
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 | 
 | 
|---|