source: doc/proposals/enum.md @ 508671e

Last change on this file since 508671e was ea89e36, checked in by JiadaL <j82liang@…>, 3 years ago

Add proposal

  • Property mode set to 100644
File size: 3.0 KB
Line 
1# Plan: CFA Typed Enum
2## Introduction
3CFA 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
14In 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>
18is equivalent to
19<div align="center">
20<i>int VariableName = EnumValue;</i>
21</div>
22For 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>
26Now <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>
27Since 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>
31An enum base is a sequence of basic integral type specifier.
32<div align="center">
33<i>
34enum E: char {A='a'};<br/>
35E e = A; <br/>
36cout << e << end;
37</i>
38</div>
39Will print a character 'a' to the output instead of 97.</br>
40CFA 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 
44Enumerator_value_opt: 
45        // empty 
46        // | '=' constant_expression 
47        //      { $$ = $2; } 
48        | simple_assignment_operator initializer                                        // FIX ME: enum only deals with constant_expression
49        ; 
50Discussion: FIX ME item. 
51### 1. Integral Type:
52$\forall$ enum_expression :  <i>enum($T$) { enumerator_list comma<sub>opt</sub> }</i>, 
53where $T \in IntegralTypes.$ 
54For $enumerator\_list \rightarrow$<sub>derive</sub> $identifier = constant\_expression$:   
55&emsp;$Code(enumerator\_list) = Code(const\ T \ identifier = constant\_expression)$ 
56For $enumerator\_list \rightarrow$<sub>derive</sub> $identifier_k$ :   
57&emsp;$Code(enumerator\_list) = Code(const\ T \ identifier_k = identifier_{k-1}+1)(k>0)$ 
58&emsp;$Code(enumerator\_list) = Code(const\ T \ identifier_k = 0)(k=0)$ 
59 
60During 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'. 
63It behaves the same as C++ and many other languages.
64
65
66### 2. Non-Integral Type 
67Syntax:
68$enumerator\_list \rightarrow$<sub>derive</sub> $identifier = constant\_expression$
69
70
Note: See TracBrowser for help on using the repository browser.