Changeset be497c6 for doc/theses/andrew_beach_MMath
- Timestamp:
- Aug 13, 2021, 3:35:55 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, pthread-emulation, qualifiedEnum
- Children:
- 3b8acfb
- Parents:
- cb6b8cb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/andrew_beach_MMath/existing.tex
rcb6b8cb rbe497c6 10 10 11 11 Only those \CFA features pertaining to this thesis are discussed. 12 A lso, only new features of \CFA will be discussed, afamiliarity with12 A familiarity with 13 13 C or C-like languages is assumed. 14 14 … … 78 78 \end{minipage} 79 79 80 References are intended to be used when you would use pointers but would 81 be dereferencing them (almost) every usage. 80 References are intended to be used when the indirection of a pointer is 81 required, but the address is not as important as the value and dereferencing 82 is the common usage. 82 83 Mutable references may be assigned to by converting them to a pointer 83 with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above 84 with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above. 85 % ??? 84 86 85 87 \section{Operators} 86 88 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 with89 \CFA implements operator overloading by providing special names, where 90 operator expressions are translated into function calls using these names. 91 An operator name is created by taking the operator symbols and joining them with 90 92 @?@s to show where the arguments go. 91 93 For example, 92 infixed multiplication is @?*?@ while prefix dereference is @*?@.94 infixed multiplication is @?*?@, while prefix dereference is @*?@. 93 95 This syntax make it easy to tell the difference between prefix operations 94 96 (such as @++?@) and post-fix operations (@?++@). 95 97 98 As an example, here are the addition and equality operators for a point type. 96 99 \begin{cfa} 97 100 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; }101 int ?==?(point a, point b) { return a.x == b.x && a.y == b.y; } 99 102 { 100 103 assert(point{1, 2} + point{3, 4} == point{4, 6}); 101 104 } 102 105 \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. 106 Note that this syntax works effectively but a textual transformation, 107 the compiler converts all operators into functions and then resolves them 108 normally. This means any combination of types may be used, 109 although nonsensical ones (like @double ?==?(point, int);@) are discouraged. 110 This feature is also used for all builtin operators as well, 111 although those are implicitly provided by the language. 108 112 109 113 %\subsection{Constructors and Destructors} 110 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 In \CFA, constructors and destructors are operators, which means they are 115 functions with special operator names rather than type names in \Cpp. 116 Both constructors and destructors can be implicity called by the compiler, 117 however the operator names allow explicit calls. 114 118 % Placement new means that this is actually equivant to C++. 115 119 116 120 The special name for a constructor is @?{}@, which comes from the 117 121 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.122 \CFA generates a constructor call each time a variable is declared, 123 passing the initialization arguments to the constructor. 120 124 \begin{cfa} 121 125 struct Example { ... }; … … 131 135 \end{cfa} 132 136 Both @a@ and @b@ will be initalized with the first constructor, 133 while @c@ will be initalized with the second. 137 @b@ because of the explicit call and @a@ implicitly. 138 @c@ will be initalized with the second constructor. 134 139 Currently, there is no general way to skip initialation. 140 % I don't use @= anywhere in the thesis. 135 141 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.141 145 \begin{cfa} 142 146 void ^?{}(Example & this) { ... } 143 147 { 144 148 Example d; 145 } // <- implicit destructor call 146 \end{cfa} 147 148 Whenever a type is defined, \CFA will create a default zero-argument 149 ^?{}(d); 150 151 Example e; 152 } // Implicit call of ^?{}(e); 153 \end{cfa} 154 155 Whenever a type is defined, \CFA creates a default zero-argument 149 156 constructor, a copy constructor, a series of argument-per-field constructors 150 157 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 158 154 159 \section{Polymorphism} … … 202 207 Note, a function named @do_once@ is not required in the scope of @do_twice@ to 203 208 compile it, unlike \Cpp template expansion. Furthermore, call-site inferencing 204 allows local replacement of the mostspecific parametric functions needs for a209 allows local replacement of the specific parametric functions needs for a 205 210 call. 206 211 \begin{cfa} … … 218 223 to @do_twice@ and called within it. 219 224 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 be a better match.222 % Aaron's thesis might be a good reference here. 223 224 To avoid typing long lists of assertions, constraints can be collect into225 convenient packagescalled a @trait@, which can then be used in an assertion225 @double@ argument, then the global definition would be used instead as it 226 would then be a better match. 227 \todo{cite Aaron's thesis (maybe)} 228 229 To avoid typing long lists of assertions, constraints can be collected into 230 convenient a package called a @trait@, which can then be used in an assertion 226 231 instead of the individual constraints. 227 232 \begin{cfa} … … 239 244 functionality, like @sumable@, @listable@, \etc. 240 245 241 Polymorphic structures and unions are defined by qualifying theaggregate type246 Polymorphic structures and unions are defined by qualifying an aggregate type 242 247 with @forall@. The type variables work the same except they are used in field 243 248 declarations instead of parameters, returns, and local variable declarations. … … 247 252 node(T) * next; 248 253 T * data; 249 } 254 }; 250 255 node(int) inode; 251 256 \end{cfa} … … 285 290 coroutine CountUp { 286 291 unsigned int next; 287 } 292 }; 288 293 CountUp countup; 289 294 \end{cfa} … … 294 299 void main(CountUp & this) { 295 300 for (unsigned int next = 0 ; true ; ++next) { 296 next = up;301 this.next = next; 297 302 suspend;$\label{suspend}$ 298 303 } … … 306 311 The first resume calls the @main@ function at the top. Thereafter, resume calls 307 312 continue a coroutine in the last suspended function after the @suspend@ 308 statement, in this case @main@ line~\ref{suspend}. The @resume@ function takes 309 a reference to the coroutine structure and returns the same reference. The 310 return value allows easy access to communication variables defined in the 311 coroutine object. For example, the @next@ value for coroutine object @countup@ 312 is both generated and collected in the single expression: 313 @resume(countup).next@. 313 statement. In this case there is only one and, hence, the difference between 314 subsequent calls is the state of variables inside the function and the 315 coroutine object. 316 The return value of @resume@ is a reference to the coroutine, to make it 317 convent to access fields of the coroutine in the same expression. 318 Here is a simple example in a helper function: 319 \begin{cfa} 320 unsigned int get_next(CountUp & this) { 321 return resume(this).next; 322 } 323 \end{cfa} 324 325 When the main function returns the coroutine halts and can no longer be 326 resumed. 314 327 315 328 \subsection{Monitor and Mutex Parameter} … … 355 368 { 356 369 StringWorker stringworker; // fork thread running in "main" 357 } // <- implicitly join with thread / wait for completion370 } // Implicit call to join(stringworker), waits for completion. 358 371 \end{cfa} 359 372 The thread main is where a new thread starts execution after a fork operation
Note: See TracChangeset
for help on using the changeset viewer.