Changeset 0a061c0 for doc/theses/andrew_beach_MMath/existing.tex
- Timestamp:
- Aug 4, 2021, 4:54:14 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- d2cdd4f
- Parents:
- d83b266 (diff), 199894e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/andrew_beach_MMath/existing.tex
rd83b266 r0a061c0 10 10 11 11 Only those \CFA features pertaining to this thesis are discussed. 12 Also, only new features of \CFA will be discussed, a familiarity with 12 % Also, only new features of \CFA will be discussed, 13 A familiarity with 13 14 C or C-like languages is assumed. 14 15 … … 16 17 \CFA has extensive overloading, allowing multiple definitions of the same name 17 18 to be defined~\cite{Moss18}. 18 \begin{ cfa}19 char i; int i; double i;20 int f(); double f();21 void g( int ); void g( double );22 \end{ cfa}19 \begin{lstlisting}[language=CFA,{moredelim=**[is][\color{red}]{@}{@}}] 20 char @i@; int @i@; double @i@; 21 int @f@(); double @f@(); 22 void @g@( int ); void @g@( double ); 23 \end{lstlisting} 23 24 This feature requires name mangling so the assembly symbols are unique for 24 25 different overloads. For compatibility with names in C, there is also a syntax … … 62 63 int && rri = ri; 63 64 rri = 3; 64 &ri = &j; 65 &ri = &j; // rebindable 65 66 ri = 5; 66 67 \end{cfa} … … 78 79 \end{minipage} 79 80 80 References are intended to be used when you would use pointers but would81 be dereferencing them (almost) every usage.81 References are intended for pointer situations where dereferencing is the common usage, 82 \ie the value is more important than the pointer. 82 83 Mutable references may be assigned to by converting them to a pointer 83 84 with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above … … 85 86 \section{Operators} 86 87 87 \CFA implements operator overloading by providing special names .88 Operator uses are translated into function calls using these names.89 These names arecreated by taking the operator symbols and joining them with88 \CFA implements operator overloading by providing special names, where 89 operator usages are translated into function calls using these names. 90 An operator name is created by taking the operator symbols and joining them with 90 91 @?@s to show where the arguments go. 91 92 For example, 92 infixed multiplication is @?*?@ while prefix dereference is @*?@.93 infixed multiplication is @?*?@, while prefix dereference is @*?@. 93 94 This syntax make it easy to tell the difference between prefix operations 94 95 (such as @++?@) and post-fix operations (@?++@). 95 96 For example, plus and equality operators are defined for a point type. 96 97 \begin{cfa} 97 98 point ?+?(point a, point b) { return point{a.x + b.x, a.y + b.y}; } 98 bool?==?(point a, point b) { return a.x == b.x && a.y == b.y; }99 int ?==?(point a, point b) { return a.x == b.x && a.y == b.y; } 99 100 { 100 101 assert(point{1, 2} + point{3, 4} == point{4, 6}); 101 102 } 102 103 \end{cfa} 103 Note that these special names are not limited to just being used for these 104 operator functions, and may be used name other declarations. 105 Some ``near misses", that will not match an operator form but looks like 106 it may have been supposed to, will generate wantings but otherwise they are 107 left alone. 104 Note these special names are not limited to builtin 105 operators, and hence, may be used with arbitrary types. 106 \begin{cfa} 107 double ?+?( int x, point y ); // arbitrary types 108 \end{cfa} 109 % Some ``near misses", that are that do not match an operator form but looks like 110 % it may have been supposed to, will generate warning but otherwise they are 111 % left alone. 112 Because operators are never part of the type definition they may be added 113 at any time, including on built-in types. 108 114 109 115 %\subsection{Constructors and Destructors} 110 116 111 Both constructors and destructors are operators, which means they are 112 functions with special operator names rather than type names in \Cpp. The 113 special operator names may be used to call the functions explicitly. 114 % Placement new means that this is actually equivant to C++. 117 \CFA also provides constructors and destructors as operators, which means they 118 are functions with special operator names rather than type names in \Cpp. 119 While constructors and destructions are normally called implicitly by the compiler, 120 the special operator names, allow explicit calls. 121 122 % Placement new means that this is actually equivalent to C++. 115 123 116 124 The special name for a constructor is @?{}@, which comes from the 117 125 initialization syntax in C, \eg @Example e = { ... }@. 118 \CFA will generatea constructor call each time a variable is declared,119 passing the initialization arguments to the constructor t.126 \CFA generates a constructor call each time a variable is declared, 127 passing the initialization arguments to the constructor. 120 128 \begin{cfa} 121 129 struct Example { ... }; 122 130 void ?{}(Example & this) { ... } 123 {124 Example a;125 Example b = {};126 }127 131 void ?{}(Example & this, char first, int num) { ... } 128 { 129 Example c = {'a', 2}; 130 } 131 \end{cfa} 132 Both @a@ and @b@ will be initalized with the first constructor, 133 while @c@ will be initalized with the second. 134 Currently, there is no general way to skip initialation. 135 132 Example a; // implicit constructor calls 133 Example b = {}; 134 Example c = {'a', 2}; 135 \end{cfa} 136 Both @a@ and @b@ are initialized with the first constructor, 137 while @c@ is initialized with the second. 138 Constructor calls can be replaced with C initialization using special operator \lstinline{@=}. 139 \begin{cfa} 140 Example d @= {42}; 141 \end{cfa} 136 142 % I don't like the \^{} symbol but $^\wedge$ isn't better. 137 Similarly destructors use the special name @^?{}@ (the @^@ has no special143 Similarly, destructors use the special name @^?{}@ (the @^@ has no special 138 144 meaning). 139 These are a normally called implicitly called on a variable when it goes out140 of scope. They can be called explicitly as well.145 % These are a normally called implicitly called on a variable when it goes out 146 % of scope. They can be called explicitly as well. 141 147 \begin{cfa} 142 148 void ^?{}(Example & this) { ... } 143 149 { 144 Example d; 145 } // <- implicit destructor call 146 \end{cfa} 147 148 Whenever a type is defined, \CFA will create a default zero-argument 150 Example e; // implicit constructor call 151 ^?{}(e); // explicit destructor call 152 ?{}(e); // explicit constructor call 153 } // implicit destructor call 154 \end{cfa} 155 156 Whenever a type is defined, \CFA creates a default zero-argument 149 157 constructor, a copy constructor, a series of argument-per-field constructors 150 158 and a destructor. All user constructors are defined after this. 151 Because operators are never part of the type definition they may be added152 at any time, including on built-in types.153 159 154 160 \section{Polymorphism} … … 202 208 Note, a function named @do_once@ is not required in the scope of @do_twice@ to 203 209 compile it, unlike \Cpp template expansion. Furthermore, call-site inferencing 204 allows local replacement of the mostspecific parametric functions needs for a210 allows local replacement of the specific parametric functions needs for a 205 211 call. 206 212 \begin{cfa} … … 218 224 to @do_twice@ and called within it. 219 225 The global definition of @do_once@ is ignored, however if quadruple took a 220 @double@ argument then the global definition would be used instead as it221 would bea better match.226 @double@ argument, then the global definition would be used instead as it 227 is a better match. 222 228 % Aaron's thesis might be a good reference here. 223 229 224 230 To avoid typing long lists of assertions, constraints can be collect into 225 convenient package scalled a @trait@, which can then be used in an assertion231 convenient package called a @trait@, which can then be used in an assertion 226 232 instead of the individual constraints. 227 233 \begin{cfa} … … 239 245 functionality, like @sumable@, @listable@, \etc. 240 246 241 Polymorphic structures and unions are defined by qualifying theaggregate type247 Polymorphic structures and unions are defined by qualifying an aggregate type 242 248 with @forall@. The type variables work the same except they are used in field 243 249 declarations instead of parameters, returns, and local variable declarations. … … 285 291 coroutine CountUp { 286 292 unsigned int next; 287 } 293 }; 288 294 CountUp countup; 295 for (10) sout | resume(countup).next; // print 10 values 289 296 \end{cfa} 290 297 Each coroutine has a @main@ function, which takes a reference to a coroutine 291 298 object and returns @void@. 292 299 %[numbers=left] Why numbers on this one? 293 \begin{cfa} 300 \begin{cfa}[numbers=left,numberstyle=\scriptsize\sf] 294 301 void main(CountUp & this) { 295 for (unsigned int next = 0 ; true ; ++next) {296 next = up;302 for (unsigned int up = 0;; ++up) { 303 this.next = up; 297 304 suspend;$\label{suspend}$ 298 305 } … … 300 307 \end{cfa} 301 308 In this function, or functions called by this function (helper functions), the 302 @suspend@ statement is used to return execution to the coroutine's caller303 without terminating the coroutine's function .309 @suspend@ statement is used to return execution to the coroutine's resumer 310 without terminating the coroutine's function(s). 304 311 305 312 A coroutine is resumed by calling the @resume@ function, \eg @resume(countup)@. … … 323 330 exclusion on a monitor object by qualifying an object reference parameter with 324 331 @mutex@. 325 \begin{ cfa}326 void example(MonitorA & mutex argA, MonitorB & mutexargB);327 \end{ cfa}332 \begin{lstlisting}[language=CFA,{moredelim=**[is][\color{red}]{@}{@}}] 333 void example(MonitorA & @mutex@ argA, MonitorB & @mutex@ argB); 334 \end{lstlisting} 328 335 When the function is called, it implicitly acquires the monitor lock for all of 329 336 the mutex parameters without deadlock. This semantics means all functions with … … 355 362 { 356 363 StringWorker stringworker; // fork thread running in "main" 357 } // <-implicitly join with thread / wait for completion364 } // implicitly join with thread / wait for completion 358 365 \end{cfa} 359 366 The thread main is where a new thread starts execution after a fork operation
Note: See TracChangeset
for help on using the changeset viewer.