Changes in / [35897fb:de3a579]
- Files:
-
- 7 edited
-
doc/theses/jiada_liang_MMath/CFAenum.tex (modified) (4 diffs)
-
doc/theses/jiada_liang_MMath/background.tex (modified) (1 diff)
-
doc/theses/jiada_liang_MMath/intro.tex (modified) (5 diffs)
-
doc/theses/jiada_liang_MMath/relatedwork.tex (modified) (4 diffs)
-
doc/theses/jiada_liang_MMath/uw-ethesis-frontpgs.tex (modified) (8 diffs)
-
doc/theses/jiada_liang_MMath/uw-ethesis.tex (modified) (4 diffs)
-
src/Validate/HoistStruct.cpp (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
r35897fb rde3a579 24 24 \label{s:EnumeratorUnscoping} 25 25 26 In C, unscoped enumerators presents a \ newterm{naming problem} when multiple enumeration types appear in the same scope with duplicate enumerator names.26 In C, unscoped enumerators presents a \Newterm{naming problem} when multiple enumeration types appear in the same scope with duplicate enumerator names. 27 27 There is no mechanism in C to resolve these naming conflicts other than renaming one of the duplicates, which may be impossible. 28 28 … … 265 265 266 266 \VRef[Figure]{f:PlanetExample} shows an archetypal enumeration example illustrating most of the \CFA enumeration features. 267 @Planet@ is anenumeration of type @MR@.267 Enumeration @Planet@ is a typed enumeration of type @MR@. 268 268 Each of the planet enumerators is initialized to a specific mass/radius, @MR@, value. 269 The unnamed enumeration provides the gravitational-constant enumerator @G@. 270 Function @surfaceGravity@ uses the @with@ clause to remove @p@ qualification from fields @mass@ and @radius@. 271 The program main uses @SizeE@ to obtain the number of enumerators in @Planet@, and safely converts the random value into a @Planet@ enumerator. 272 The resulting random orbital body is used in a @choose@ statement. 273 The enumerators in the @case@ clause use position for testing. 274 The prints use @labelE@ to print the enumerators label. 275 Finally, a loop iterates through the planets computing the weight on each planet for a given earth weight. 276 The print statement does an equality comparison with an enumeration variable and enumerator. 269 The unnamed enumeration projects the gravitational-constant enumerator @G@. 270 The program main iterates through the planets computing the weight on each planet for a given earth weight. 277 271 278 272 \begin{figure} 279 \small280 273 \begin{cfa} 281 274 struct MR { double mass, radius; }; 282 enum( @MR@) Planet {275 enum( MR ) Planet { 283 276 // mass (kg) radius (km) 284 277 MERCURY = { 0.330_E24, 2.4397_E6 }, … … 292 285 NEPTUNE = { 102.4_E24, 24.746_E6 }, 293 286 }; 294 enum( double ) { G = 6.6743_E-11 }; $\C{// universal gravitational constant (m3 kg-1 s-2)}$ 295 static double surfaceGravity( Planet p ) @with( p )@ { 296 return G * mass / ( radius \ 2u ); $\C{// exponentiation}$ 287 enum( double ) { G = 6.6743E-11 }; $\C{// universal gravitational constant (m3 kg-1 s-2)}$ 288 289 static double surfaceGravity( Planet p ) with( p ) { 290 return G * mass / ( radius * radius ); 297 291 } 298 292 static double surfaceWeight( Planet p, double otherMass ) { … … 303 297 double earthWeight = convert( argv[1] ); 304 298 double mass = earthWeight / surfaceGravity( EARTH ); 305 306 Planet p = @fromInt@( prng( @SizeE@(Planet) ) ); $\C{// select a random orbiting body}$ 307 @choose( p )@ { 308 case MERCURY, VENUS, EARTH, MARS: 309 sout | @labelE( p )@ | "is a rocky planet"; 310 @case JUPITER, SATURN, URANUS, NEPTUNE:@ 311 sout | labelE( p ) | "is a gas-giant planet"; 312 default: 313 sout | labelE( p ) | "is not a planet"; 299 for ( p; Planet ) { 300 sout | "Your weight on" | labelE(p) | "is" | wd(1,1, surfaceWeight( p, mass )) | "kg"; 314 301 } 315 for ( @p; Planet@ ) { 316 sout | "Your weight on" | (@p == MOON@ ? "the" : "") | labelE(p) 317 | "is" | wd(1,1, surfaceWeight( p, mass )) | "kg"; 318 } 319 } 302 } 303 320 304 $\$$ planet 100 321 JUPITER is a gas-giant planet322 305 Your weight on MERCURY is 37.7 kg 323 306 Your weight on VENUS is 90.5 kg 324 307 Your weight on EARTH is 100.0 kg 325 Your weight on theMOON is 16.6 kg308 Your weight on MOON is 16.6 kg 326 309 Your weight on MARS is 37.9 kg 327 310 Your weight on JUPITER is 252.8 kg -
doc/theses/jiada_liang_MMath/background.tex
r35897fb rde3a579 78 78 Here, the aliased constants are: 20, 10, 20, 21, and -7. 79 79 Direct initialization is by a compile-time expression generating a constant value. 80 An enumerator without initialization is \ newterm{auto-initialized}: from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@.80 An enumerator without initialization is \Newterm{auto-initialized}: from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@. 81 81 Because multiple independent enumerators can be combined, enumerators with the same values can occur. 82 82 The enumerators are rvalues, so assignment is disallowed. 83 Finally, enumerators are \ newterm{unscoped}, \ie enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type.83 Finally, enumerators are \Newterm{unscoped}, \ie enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type. 84 84 For unnamed enumeration this semantic is required because there is no type name for scoped qualification. 85 85 -
doc/theses/jiada_liang_MMath/intro.tex
r35897fb rde3a579 1 1 \chapter{Introduction} 2 2 3 All types in a programming language must have a set of constants, and these constants have \ newterm{primary names}, \eg integral types have constants @-1@, @17@, @0xff@, floating-point types have constants @5.3@, @2.3E-5@, @0xff.ffp0@, character types have constants @'a'@, @"abc\n"@, \mbox{\lstinline{u8"}\texttt{\guillemotleft{na\"{i}ve}\guillemotright}\lstinline{"}}, \etc.3 All types in a programming language must have a set of constants, and these constants have \Newterm{primary names}, \eg integral types have constants @-1@, @17@, @0xff@, floating-point types have constants @5.3@, @2.3E-5@, @0xff.ffp0@, character types have constants @'a'@, @"abc\n"@, \mbox{\lstinline{u8"}\texttt{\guillemotleft{na\"{i}ve}\guillemotright}\lstinline{"}}, \etc. 4 4 Con\-stants can be overloaded among types, \eg @0@ is a null pointer for all pointer types, and the value zero for integral and floating-point types. 5 5 (In \CFA, the primary constants @0@ and @1@ can be overloaded for any type.) … … 7 7 In theory, there are an infinite set of primary constant names per type. 8 8 9 \ newterm{Secondary naming} is a common practice in mathematics, engineering and computer science, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MB (megabyte, 1E6), and in general situations, \eg specific times (noon, New Years), cities (Big Apple), flowers (Lily), \etc.10 Many programming languages capture this important software-engineering capability through a mechanism called \ newterm{constant} or \newterm{literal} naming, where a secondary name is aliased to a primary name.9 \Newterm{Secondary naming} is a common practice in mathematics, engineering and computer science, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MB (megabyte, 1E6), and in general situations, \eg specific times (noon, New Years), cities (Big Apple), flowers (Lily), \etc. 10 Many programming languages capture this important software-engineering capability through a mechanism called \Newterm{constant} or \Newterm{literal} naming, where a secondary name is aliased to a primary name. 11 11 Its purpose is for readability and to eliminate duplication of the primary constant throughout a program. 12 12 For example, a meaningful secondary name replaces a primary name throughout a program; 13 13 thereafter, changing the binding of the secondary to primary name automatically distributes the rebinding, preventing errors. 14 In some cases, secondary naming is \ newterm{opaque}, where the matching internal representation can be chosen arbitrarily, and only equality operations are available, \eg @O_RDONLY@, @O_WRONLY@, @O_CREAT@, @O_TRUNC@, @O_APPEND@.15 Because a secondary name is a constant, it cannot appear in a mutable context, \eg \mbox{$\pi$ \lstinline{= 42}} is meaningless, and a constant has no address, \ie it is an \ newterm{rvalue}\footnote{14 In some cases, secondary naming is \Newterm{opaque}, where the matching internal representation can be chosen arbitrarily, and only equality operations are available, \eg @O_RDONLY@, @O_WRONLY@, @O_CREAT@, @O_TRUNC@, @O_APPEND@. 15 Because a secondary name is a constant, it cannot appear in a mutable context, \eg \mbox{$\pi$ \lstinline{= 42}} is meaningless, and a constant has no address, \ie it is an \Newterm{rvalue}\footnote{ 16 16 The term rvalue defines an expression that can only appear on the right-hand side of an assignment expression.}. 17 17 18 18 Secondary names can form an (ordered) set, \eg days of a week, months of a year, floors of a building (basement, ground, 1st), colours in a rainbow, \etc. 19 Many programming languages capture these groupings through a mechanism called an \ newterm{enumeration}.19 Many programming languages capture these groupings through a mechanism called an \Newterm{enumeration}. 20 20 \begin{quote} 21 21 enumerate (verb, transitive). … … 63 63 \label{s:Terminology} 64 64 65 The term \ newterm{enumeration} defines a type with a set of secondary names, and the term \newterm{enumerator} represents an arbitrary secondary name \see{\VRef{s:CEnumeration} for the name derivation}.66 As well, an enumerated type can have three fundamental properties, \ newterm{label}, \newterm{order}, and \newterm{value}.65 The term \Newterm{enumeration} defines a type with a set of secondary names, and the term \Newterm{enumerator} represents an arbitrary secondary name \see{\VRef{s:CEnumeration} for the name derivation}. 66 As well, an enumerated type can have three fundamental properties, \Newterm{label}, \Newterm{order}, and \Newterm{value}. 67 67 \begin{cquote} 68 68 \sf\setlength{\tabcolsep}{3pt} … … 116 116 foo( Size ); // take the address of (reference) Size 117 117 \end{cfa} 118 Taking the address of an immutable variable makes it an \ newterm{lvalue}, which implies it has storage.118 Taking the address of an immutable variable makes it an \Newterm{lvalue}, which implies it has storage. 119 119 With separate compilation, it is necessary to choose one translation unit to perform the initialization. 120 120 If aliasing does require storage, its address and initialization are opaque (compiler only), similar to \CC rvalue reference @&&@. … … 185 185 Here, the constructor name gives different meaning to the values in the common \lstinline[language=Haskell]{Int} type, \eg the value @3@ has different interpretations depending on the constructor name in the pattern matching. 186 186 187 Note, the term \ newterm{variant} is often associated with ADTs.187 Note, the term \Newterm{variant} is often associated with ADTs. 188 188 However, there are multiple languages with a @variant@ type that is not an ADT \see{Algol68~\cite{Algol68} or \CC \lstinline{variant}}. 189 189 In these languages, the variant is often a union using RTTI tags, which cannot be used to simulate an enumeration. -
doc/theses/jiada_liang_MMath/relatedwork.tex
r35897fb rde3a579 12 12 The definition of member types and their constructors are from the outer lexical scope. 13 13 14 In general, an \ newterm{algebraic data type} (ADT) is a composite type, \ie, a type formed by combining other types.15 Three common classes of algebraic types are \ newterm{array type}, \ie homogeneous types, \newterm{product type}, \ie heterogeneous tuples and records (structures), and \newterm{sum type}, \ie tagged product-types (unions).14 In general, an \Newterm{algebraic data type} (ADT) is a composite type, \ie, a type formed by combining other types. 15 Three common classes of algebraic types are \Newterm{array type}, \ie homogeneous types, \Newterm{product type}, \ie heterogeneous tuples and records (structures), and \Newterm{sum type}, \ie tagged product-types (unions). 16 16 Enumerated types are a special case of product/sum types with non-mutable fields, \ie initialized (constructed) once at the type's declaration, possible restricted to compile-time initialization. 17 17 Values of algebraic types are access by subscripting, field qualification, or type (pattern) matching. … … 51 51 \section{Ada} 52 52 53 An Ada enumeration type is a set of ordered unscoped identifiers (enumerators) bound to \emph{unique} \ newterm{literals}.\footnote{%53 An Ada enumeration type is a set of ordered unscoped identifiers (enumerators) bound to \emph{unique} \Newterm{literals}.\footnote{% 54 54 Ada is \emph{case-insensitive} so identifiers may appear in multiple forms and still be the same, \eg \lstinline{Mon}, \lstinline{moN}, and \lstinline{MON} (a questionable design decision).} 55 55 \begin{ada} … … 2177 2177 Here, function @take_class@ has a @weekday@ parameter, and returns @"CS442"@, if the weekday value is @Mon@ or @Wed@, @"CS343"@, if the value is @Tue@ or @Thu@, and @"Tutorial"@ for @Fri@. 2178 2178 The ``@_@'' is a wildcard matching any @weekday@ value, so the function returns @"Take a break"@ for values @Sat@ or @Sun@, which are not matched by the previous cases. 2179 Since the variant has no type, it has a \ newterm{0-arity constructor}, \ie no parameters.2180 Because @weekday@ is a union of values @Mon@ to @Sun@, it is a \ newterm{union type} in turns of the functional-programming paradigm.2179 Since the variant has no type, it has a \Newterm{0-arity constructor}, \ie no parameters. 2180 Because @weekday@ is a union of values @Mon@ to @Sun@, it is a \Newterm{union type} in turns of the functional-programming paradigm. 2181 2181 2182 2182 Each variant can have an associated heterogeneous type, with an n-ary constructor for creating a corresponding value. … … 2202 2202 type @stringList@ = Empty | Pair of string * @stringList@ 2203 2203 \end{ocaml} 2204 which is a recursive sum of product of types, called an \ newterm{algebraic data-type}.2204 which is a recursive sum of product of types, called an \Newterm{algebraic data-type}. 2205 2205 A recursive function is often used to pattern match against a recursive variant type. 2206 2206 \begin{ocaml} -
doc/theses/jiada_liang_MMath/uw-ethesis-frontpgs.tex
r35897fb rde3a579 30 30 \normalsize 31 31 A thesis \\ 32 presented to the University of Waterloo \\ 32 presented to the University of Waterloo \\ 33 33 in fulfillment of the \\ 34 34 thesis requirement for the degree of \\ … … 63 63 The following served on the Examining Committee for this thesis. The decision of the Examining Committee is by majority vote. 64 64 \bigskip 65 66 \noindent 67 \begin{tabbing} 68 Internal-External Member: \= \kill % using longest text to define tab length 69 External Examiner: \> Bruce Bruce \\ 65 66 \noindent 67 \begin{tabbing} 68 Internal-External Member: \= \kill % using longest text to define tab length 69 External Examiner: \> Bruce Bruce \\ 70 70 \> Professor, Dept. of Philosophy of Zoology, University of Wallamaloo \\ 71 \end{tabbing} 72 \bigskip 73 71 \end{tabbing} 72 \bigskip 73 74 74 \noindent 75 75 \begin{tabbing} … … 81 81 \end{tabbing} 82 82 \bigskip 83 83 84 84 \noindent 85 85 \begin{tabbing} … … 89 89 \end{tabbing} 90 90 \bigskip 91 91 92 92 \noindent 93 93 \begin{tabbing} … … 97 97 \end{tabbing} 98 98 \bigskip 99 99 100 100 \noindent 101 101 \begin{tabbing} … … 114 114 \addcontentsline{toc}{chapter}{Author's Declaration} 115 115 \begin{center}\textbf{Author's Declaration}\end{center} 116 116 117 117 \noindent 118 118 I hereby declare that I am the sole author of this thesis. This is a true copy of the thesis, including any required final revisions, as accepted by my examiners. 119 119 120 120 \bigskip 121 121 122 122 \noindent 123 123 I understand that my thesis may be made electronically available to the public. … … 182 182 \phantomsection % allows hyperref to link to the correct page 183 183 184 \begin{comment}185 184 % L I S T O F A B B R E V I A T I O N S 186 185 % --------------------------- … … 190 189 \phantomsection % allows hyperref to link to the correct page 191 190 191 \begin{comment} 192 192 % L I S T O F S Y M B O L S 193 193 % --------------------------- -
doc/theses/jiada_liang_MMath/uw-ethesis.tex
r35897fb rde3a579 112 112 \newsavebox{\myboxB} 113 113 114 \newcommand{\newtermFont}{\emph} 115 \newcommand{\Newterm}[1]{\newtermFont{#1}} 116 %\renewcommand{\newterm}[1]{\newtermFont{#1}} 114 117 \newcommand{\uC}{$\mu$\CC} 115 118 \newcommand{\PAB}[1]{{\color{red}PAB: #1}} … … 154 157 \urlstyle{sf} 155 158 156 %\usepackage[automake,toc,abbreviations]{glossaries-extra} % Exception to the rule of hyperref being the last add-on package157 %\renewcommand*{\glstextformat}[1]{\textcolor{black}{#1}}159 \usepackage[automake,toc,abbreviations]{glossaries-extra} % Exception to the rule of hyperref being the last add-on package 160 \renewcommand*{\glstextformat}[1]{\textcolor{black}{#1}} 158 161 % If glossaries-extra is not in your LaTeX distribution, get it from CTAN (http://ctan.org/pkg/glossaries-extra), 159 162 % although it's supposed to be in both the TeX Live and MikTeX distributions. There are also documentation and … … 196 199 197 200 % Define Glossary terms (This is properly done here, in the preamble and could also be \input{} from a separate file...) 198 %\usepackage[automake,toc,abbreviations]{glossaries-extra} % Exception to the rule of hyperref being the last add-on package199 %\renewcommand*{\glstextformat}[1]{\textcolor{black}{#1}}200 %\input{glossary}201 %\makeglossaries201 \usepackage[automake,toc,abbreviations]{glossaries-extra} % Exception to the rule of hyperref being the last add-on package 202 \renewcommand*{\glstextformat}[1]{\textcolor{black}{#1}} 203 \input{glossary} 204 \makeglossaries 202 205 203 206 %====================================================================== … … 273 276 % GLOSSARIES (Lists of definitions, abbreviations, symbols, etc. provided by the glossaries-extra package) 274 277 % ----------------------------- 275 %\printglossary276 %\cleardoublepage277 %\phantomsection % allows hyperref to link to the correct page278 \printglossary 279 \cleardoublepage 280 \phantomsection % allows hyperref to link to the correct page 278 281 279 282 %---------------------------------------------------------------------- -
src/Validate/HoistStruct.cpp
r35897fb rde3a579 27 27 namespace { 28 28 29 /// Is this a declaration can appear in a struct/union and should be hoisted?30 29 bool shouldHoist( ast::Decl const * decl ) { 31 30 return dynamic_cast< ast::StructDecl const * >( decl ) … … 35 34 } 36 35 37 /// Helper that updates an InstType if the base name could be updated. 38 template<typename InstType> 39 InstType const * preInstType( InstType const * type ) { 40 assert( type->base ); 41 if ( nullptr == type->base->parent ) return type; 42 auto mut = ast::mutate( type ); 43 mut->name = mut->base->name; 44 return mut; 45 } 46 47 /// Update StructInstType and UnionInstType names. 48 struct NameUpdater { 49 ast::StructInstType const * previsit( ast::StructInstType const * type ) { 50 return preInstType( type ); 51 } 52 53 ast::UnionInstType const * previsit( ast::UnionInstType const * type ) { 54 return preInstType( type ); 55 } 56 }; 57 58 ast::Decl const * updateNames( ast::Decl const * decl ) { 59 ast::Pass<NameUpdater> visitor; 60 return decl->accept( visitor ); 61 } 62 63 /* This pass hoists from structs/unions. Hoisted declarations should always 36 /* This pass also does some renaming and internal field alteration, but the 37 * complex part is the actual hoisting. Hoisted declarations should always 64 38 * appear before the declaration they are hoisted out of and if two types are 65 39 * nested in the same declaration their order should not change. 66 * It also sets up parent relationships, does name mangling of hoisted types67 * and updates instance types of the hoisted types.68 40 */ 69 41 struct HoistStructCore final : … … 124 96 auto mut = ast::mutate( decl ); 125 97 mut->parent = parent; 98 mut->name = qualifiedName( mut ); 126 99 extendParams( mut->params, parent->params ); 127 100 decl = mut; … … 143 116 } 144 117 } 145 // Is this a nested type? Then update the name, after the parent's name146 // has been updated (hence the post visit).147 if ( mut->parent ) {148 mut->name = qualifiedName( mut );149 // Top level type that has hoisted? Then do a second pass subpass to make150 // sure we update instance type names after the declaration is renamed.151 } else if ( !declsToAddBefore.empty() ) {152 for ( ast::ptr<ast::Decl> & member : mut->members ) {153 member = updateNames( member.get() );154 }155 for ( ast::ptr<ast::Decl> & declToAdd : declsToAddBefore ) {156 declToAdd = updateNames( declToAdd );157 }158 }159 118 return mut; 160 119 } … … 208 167 } 209 168 169 template<typename InstType> 170 InstType const * preInstType( InstType const * type ) { 171 assert( type->base ); 172 auto mut = ast::mutate( type ); 173 mut->name = mut->base->name; 174 return mut; 175 } 176 210 177 ast::StructInstType const * HoistStructCore::previsit( ast::StructInstType const * type ) { 211 178 return preInstType( preCollectionInstType( type ) );
Note:
See TracChangeset
for help on using the changeset viewer.