Changeset f42a6b8 for doc/theses/andrew_beach_MMath/existing.tex
- Timestamp:
- Aug 9, 2021, 4:35:49 PM (2 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, pthread-emulation, qualifiedEnum
- Children:
- cb6b8cb
- Parents:
- 5438e41
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/andrew_beach_MMath/existing.tex
r5438e41 rf42a6b8 10 10 11 11 Only those \CFA features pertaining to this thesis are discussed. 12 % Also, only new features of \CFA will be discussed, 13 A familiarity with 12 Also, only new features of \CFA will be discussed, a familiarity with 14 13 C or C-like languages is assumed. 15 14 … … 17 16 \CFA has extensive overloading, allowing multiple definitions of the same name 18 17 to be defined~\cite{Moss18}. 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}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} 24 23 This feature requires name mangling so the assembly symbols are unique for 25 24 different overloads. For compatibility with names in C, there is also a syntax … … 63 62 int && rri = ri; 64 63 rri = 3; 65 &ri = &j; // rebindable64 &ri = &j; 66 65 ri = 5; 67 66 \end{cfa} … … 79 78 \end{minipage} 80 79 81 References are intended for pointer situations where dereferencing is the common usage,82 \ie the value is more important than the pointer.80 References are intended to be used when you would use pointers but would 81 be dereferencing them (almost) every usage. 83 82 Mutable references may be assigned to by converting them to a pointer 84 83 with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above … … 86 85 \section{Operators} 87 86 88 \CFA implements operator overloading by providing special names , where89 operator usages are translated into function calls using these names.90 An operator name iscreated by taking the operator symbols and joining them with87 \CFA implements operator overloading by providing special names. 88 Operator uses are translated into function calls using these names. 89 These names are created by taking the operator symbols and joining them with 91 90 @?@s to show where the arguments go. 92 91 For example, 93 infixed multiplication is @?*?@ ,while prefix dereference is @*?@.92 infixed multiplication is @?*?@ while prefix dereference is @*?@. 94 93 This syntax make it easy to tell the difference between prefix operations 95 94 (such as @++?@) and post-fix operations (@?++@). 96 For example, plus and equality operators are defined for a point type. 95 97 96 \begin{cfa} 98 97 point ?+?(point a, point b) { return point{a.x + b.x, a.y + b.y}; } 99 int?==?(point a, point b) { return a.x == b.x && a.y == b.y; }98 bool ?==?(point a, point b) { return a.x == b.x && a.y == b.y; } 100 99 { 101 100 assert(point{1, 2} + point{3, 4} == point{4, 6}); 102 101 } 103 102 \end{cfa} 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. 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. 108 109 %\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 % Placement new means that this is actually equivant to C++. 115 116 The special name for a constructor is @?{}@, which comes from the 117 initialization syntax in C, \eg @Example e = { ... }@. 118 \CFA will generate a constructor call each time a variable is declared, 119 passing the initialization arguments to the constructort. 120 \begin{cfa} 121 struct Example { ... }; 122 void ?{}(Example & this) { ... } 123 { 124 Example a; 125 Example b = {}; 126 } 127 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 136 % I don't like the \^{} symbol but $^\wedge$ isn't better. 137 Similarly destructors use the special name @^?{}@ (the @^@ has no special 138 meaning). 139 These are a normally called implicitly called on a variable when it goes out 140 of scope. They can be called explicitly as well. 141 \begin{cfa} 142 void ^?{}(Example & this) { ... } 143 { 144 Example d; 145 } // <- implicit destructor call 146 \end{cfa} 147 148 Whenever a type is defined, \CFA will create a default zero-argument 149 constructor, a copy constructor, a series of argument-per-field constructors 150 and a destructor. All user constructors are defined after this. 112 151 Because operators are never part of the type definition they may be added 113 152 at any time, including on built-in types. 114 115 %\subsection{Constructors and Destructors}116 117 \CFA also provides constructors and destructors as operators, which means they118 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++.123 124 The special name for a constructor is @?{}@, which comes from the125 initialization syntax in C, \eg @Example e = { ... }@.126 \CFA generates a constructor call each time a variable is declared,127 passing the initialization arguments to the constructor.128 \begin{cfa}129 struct Example { ... };130 void ?{}(Example & this) { ... }131 void ?{}(Example & this, char first, int num) { ... }132 Example a; // implicit constructor calls133 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}142 % I don't like the \^{} symbol but $^\wedge$ isn't better.143 Similarly, destructors use the special name @^?{}@ (the @^@ has no special144 meaning).145 % These are a normally called implicitly called on a variable when it goes out146 % of scope. They can be called explicitly as well.147 \begin{cfa}148 void ^?{}(Example & this) { ... }149 {150 Example e; // implicit constructor call151 ^?{}(e); // explicit destructor call152 ?{}(e); // explicit constructor call153 } // implicit destructor call154 \end{cfa}155 156 Whenever a type is defined, \CFA creates a default zero-argument157 constructor, a copy constructor, a series of argument-per-field constructors158 and a destructor. All user constructors are defined after this.159 153 160 154 \section{Polymorphism} … … 208 202 Note, a function named @do_once@ is not required in the scope of @do_twice@ to 209 203 compile it, unlike \Cpp template expansion. Furthermore, call-site inferencing 210 allows local replacement of the specific parametric functions needs for a204 allows local replacement of the most specific parametric functions needs for a 211 205 call. 212 206 \begin{cfa} … … 224 218 to @do_twice@ and called within it. 225 219 The global definition of @do_once@ is ignored, however if quadruple took a 226 @double@ argument ,then the global definition would be used instead as it227 isa better match.220 @double@ argument then the global definition would be used instead as it 221 would be a better match. 228 222 % Aaron's thesis might be a good reference here. 229 223 230 224 To avoid typing long lists of assertions, constraints can be collect into 231 convenient package called a @trait@, which can then be used in an assertion225 convenient packages called a @trait@, which can then be used in an assertion 232 226 instead of the individual constraints. 233 227 \begin{cfa} … … 245 239 functionality, like @sumable@, @listable@, \etc. 246 240 247 Polymorphic structures and unions are defined by qualifying anaggregate type241 Polymorphic structures and unions are defined by qualifying the aggregate type 248 242 with @forall@. The type variables work the same except they are used in field 249 243 declarations instead of parameters, returns, and local variable declarations. … … 291 285 coroutine CountUp { 292 286 unsigned int next; 293 } ;287 } 294 288 CountUp countup; 295 for (10) sout | resume(countup).next; // print 10 values296 289 \end{cfa} 297 290 Each coroutine has a @main@ function, which takes a reference to a coroutine 298 291 object and returns @void@. 299 292 %[numbers=left] Why numbers on this one? 300 \begin{cfa} [numbers=left,numberstyle=\scriptsize\sf]293 \begin{cfa} 301 294 void main(CountUp & this) { 302 for (unsigned int up = 0;; ++up) {303 this.next = up;295 for (unsigned int next = 0 ; true ; ++next) { 296 next = up; 304 297 suspend;$\label{suspend}$ 305 298 } … … 307 300 \end{cfa} 308 301 In this function, or functions called by this function (helper functions), the 309 @suspend@ statement is used to return execution to the coroutine's resumer310 without terminating the coroutine's function (s).302 @suspend@ statement is used to return execution to the coroutine's caller 303 without terminating the coroutine's function. 311 304 312 305 A coroutine is resumed by calling the @resume@ function, \eg @resume(countup)@. … … 330 323 exclusion on a monitor object by qualifying an object reference parameter with 331 324 @mutex@. 332 \begin{ lstlisting}[language=CFA,{moredelim=**[is][\color{red}]{@}{@}}]333 void example(MonitorA & @mutex@ argA, MonitorB & @mutex@argB);334 \end{ lstlisting}325 \begin{cfa} 326 void example(MonitorA & mutex argA, MonitorB & mutex argB); 327 \end{cfa} 335 328 When the function is called, it implicitly acquires the monitor lock for all of 336 329 the mutex parameters without deadlock. This semantics means all functions with … … 362 355 { 363 356 StringWorker stringworker; // fork thread running in "main" 364 } // implicitly join with thread / wait for completion357 } // <- implicitly join with thread / wait for completion 365 358 \end{cfa} 366 359 The thread main is where a new thread starts execution after a fork operation
Note: See TracChangeset
for help on using the changeset viewer.