Changeset 956299b
- Timestamp:
- Feb 8, 2024, 10:48:41 AM (9 months ago)
- Branches:
- master
- Children:
- 14755e5, 714e206
- Parents:
- 211def2
- Location:
- doc/theses/jiada_liang_MMath
- Files:
-
- 3 added
- 2 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/background.tex
r211def2 r956299b 1 1 \chapter{Background} 2 3 4 \section{C-Style Enum} 5 6 The C-Style enumeration has the following syntax and semantics. 7 \begin{cfa} 8 enum Weekday { Monday, Tuesday, Wednesday, Thursday@ = 10@, Friday, Saturday, Sunday }; 9 \end{cfa} 10 Enumerators without an explicitly designated constant value are \Newterm{auto-initialized} by the compiler: from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@. 11 For example, @Monday@ to @Wednesday@ are implicitly assigned with constants @0@--@2@, @Thursday@ is explicitly set to constant @10@, and @Friday@ to @Sunday@ are implicitly assigned with constants @11@--@13@. 12 Initialization may occur in any order. 13 \begin{cfa} 14 enum Weekday { Thursday@ = 10@, Friday, Saturday, Sunday, Monday@ = 0@, Tuesday, Wednesday }; 15 \end{cfa} 16 Note, the comma in the enumerator list can be a terminator or a separator, allowing the list to end with a dangling comma. 17 \begin{cfa} 18 enum Weekday { 19 Thursday = 10, Friday, Saturday, Sunday, 20 Monday = 0, Tuesday, Wednesday@,@ // terminating comma 21 }; 22 \end{cfa} 23 This feature allow enumerator lines to be interchanged without moving a comma.\footnote{ 24 A terminating comma appears in other C syntax, e.g., the initializer list.} 25 Finally, C enumerators are \Newterm{unscoped}, i.e., enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type. 26 27 In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerated values. 28 In practice, since integral constants are used, which have type @int@ (unless qualified with a size suffix), C uses @int@ as the underlying type for enumeration variables. 29 Finally, there is an implicit bidirectional conversion between an enumeration and integral types. 30 \begin{cfa} 31 { 32 enum Weekday { /* as above */ }; $\C{// enumerators implicitly projected into local scope}$ 33 Weekday weekday = Monday; $\C{// weekday == 0}$ 34 weekday = Friday; $\C{// weekday == 11}$ 35 int i = Sunday; $\C{// implicit conversion to int, i == 13}$ 36 weekday = 10000; $\C{// UNDEFINED! implicit conversion to Weekday}$ 37 } 38 int j = Wednesday; $\C{// ERROR! Wednesday is not declared in this scope}$ 39 \end{cfa} 40 The implicit conversion from @int@ to an enumeration type is an unnecessary source of error. 41 42 It is common for C programmers to ``believe'' there are 3 equivalent forms of constant enumeration. 43 \begin{cfa} 44 #define Monday 0 45 static const int Monday = 0; 46 enum { Monday }; 47 \end{cfa} 48 For @#define@, the programmer has to play compiler and explicitly manage the enumeration values; 49 furthermore, these are independent constants outside of any language type mechanism. 50 The same explicit management is true for @const@ declarations, and the @const@ variable cannot appear in constant-expression locations, like @case@ labels, array dimensions,\footnote{ 51 C allows variable-length array-declarations (VLA), so this case does work, but it fails in \CC, which does not support VLAs, unless it is \lstinline{g++}.} and immediate operands of assembler instructions. 52 Only the @enum@ form is managed by the compiler, is part of the language type-system, and works in all C constant-expression locations. -
doc/theses/jiada_liang_MMath/intro.tex
r211def2 r956299b 1 1 \chapter{Introduction} 2 2 3 Testing glossy abbreviations \gls{foo} and \gls{bar}, and glossy definitions \gls{git} and \gls{gulp}. 3 Naming values is a common practice in mathematics and engineering, e.g., $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MHz (1E6), etc. 4 Naming is also commonly used to represent many other numerical phenomenon, such as days of the week, months of a year, floors of a building (basement), specific times (noon, New Years). 5 Many programming languages capture this important software-engineering capability through a mechanism called an \Newterm{enumeration}. 6 An enumeration is similar to other programming-language types by providing a set of constrained values, but adds the ability to name \emph{all} the values in its set. 7 Note, all enumeration names must be unique but different names can represent the same value (eight note, quaver), which are synonyms. 4 8 5 And use the glossy abbreviations \gls{foo} and \gls{bar}, and definitions \gls{git} and \gls{gulp} again. 9 Specifically, an enumerated type restricts its values to a fixed set of named constants. 10 While all types are restricted to a fixed set of values because of the underlying von Neumann architecture, and hence, to a corresponding set of constants, e.g., @3@, @3.5@, @3.5+2.1i@, @'c'@, @"abc"@, etc., these values are not named, other than the programming-language supplied constant names. 11 12 Fundamentally, all enumeration systems have an \Newterm{enumeration} type with an associated set of \Newterm{enumerator} names. 13 An enumeration has three universal attributes, \Newterm{position}, \Newterm{label}, and \Newterm{value}, as shown by this representative enumeration, where position and value can be different. 14 \begin{cquote} 15 \small\sf\setlength{\tabcolsep}{3pt} 16 \begin{tabular}{rccccccccccc} 17 \it\color{red}enumeration & \multicolumn{7}{c}{\it\color{red}enumerators} \\ 18 $\downarrow$\hspace*{25pt} & \multicolumn{7}{c}{$\downarrow$} \\ 19 @enum@ Weekday \{ & Monday, & Tuesday, & Wednesday, & Thursday,& Friday, & Saturday, & Sunday \}; \\ 20 \it\color{red}position & 0 & 1 & 2 & 3 & 4 & 5 & 6 \\ 21 \it\color{red}label & Monday & Tuesday & Wednesday & Thursday & Friday & Saturday & Sunday \\ 22 \it\color{red}value & 0 & 1 & 2 & 3 & 4 & 5 & 6 23 \end{tabular} 24 \end{cquote} 25 Here, the \Newterm{enumeration} @Weekday@ defines the ordered \Newterm{enumerator}s @Monday@, @Tuesday@, @Wednesday@, @Thursday@, @Friday@, @Saturday@ and @Sunday@. 26 By convention, the successor of @Tuesday@ is @Monday@ and the predecessor of @Tuesday@ is @Wednesday@, independent of the associated enumerator constant values. 27 Because an enumerator is a constant, it cannot appear in a mutable context, e.g. @Mon = Sun@ is meaningless, and an enumerator has no address, it is an \Newterm{rvalue}\footnote{ 28 The term rvalue defines an expression that can only appear on the right-hand side of an assignment.}. -
doc/theses/jiada_liang_MMath/uw-ethesis-frontpgs.tex
r211def2 r956299b 131 131 \begin{center}\textbf{Abstract}\end{center} 132 132 133 Enumerated type ... 133 An enumeration is a type defining an ordered set of named constant values, where a name abstracts a value, e.g., @PI@ versus @3.145159@. 134 C restrict an enumeration type to the integral type @signed int@, which \CC support , meaning enumeration names bind to integer constants. 135 \CFA extends C enumerations to allow all basic and custom types for the enumeration type, like other modern programming languages. 136 Furthermore, \CFA adds other useful features for enumerations to support better software-engineering practices and simplify program development. 134 137 135 138 \cleardoublepage -
doc/theses/jiada_liang_MMath/uw-ethesis.tex
r211def2 r956299b 211 211 \input{intro} 212 212 \input{background} 213 \input{content1} 214 \input{content2} 213 \input{CFAenum} 214 \input{implementation} 215 \input{relatedwork} 215 216 \input{performance} 216 217 \input{conclusion}
Note: See TracChangeset
for help on using the changeset viewer.