# Changeset be497c6 for doc/theses/andrew_beach_MMath/existing.tex

Ignore:
Timestamp:
Aug 13, 2021, 3:35:55 PM (11 months ago)
Branches:
enum, forall-pointer-decay, jacob/cs343-translation, master
Children:
3b8acfb
Parents:
cb6b8cb
Message:

Andrew MMath: Used Peter's feedback for the existing chapter.

File:
1 edited

### Legend:

Unmodified
 rcb6b8cb Only those \CFA features pertaining to this thesis are discussed. Also, only new features of \CFA will be discussed, a familiarity with A familiarity with C or C-like languages is assumed. \end{minipage} References are intended to be used when you would use pointers but would be dereferencing them (almost) every usage. References are intended to be used when the indirection of a pointer is required, but the address is not as important as the value and dereferencing is the common usage. Mutable references may be assigned to by converting them to a pointer with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above. % ??? \section{Operators} \CFA implements operator overloading by providing special names. Operator uses are translated into function calls using these names. These names are created by taking the operator symbols and joining them with \CFA implements operator overloading by providing special names, where operator expressions are translated into function calls using these names. An operator name is created by taking the operator symbols and joining them with @?@s to show where the arguments go. For example, infixed multiplication is @?*?@ while prefix dereference is @*?@. infixed multiplication is @?*?@, while prefix dereference is @*?@. This syntax make it easy to tell the difference between prefix operations (such as @++?@) and post-fix operations (@?++@). As an example, here are the addition and equality operators for a point type. \begin{cfa} point ?+?(point a, point b) { return point{a.x + b.x, a.y + b.y}; } bool ?==?(point a, point b) { return a.x == b.x && a.y == b.y; } int ?==?(point a, point b) { return a.x == b.x && a.y == b.y; } { assert(point{1, 2} + point{3, 4} == point{4, 6}); } \end{cfa} Note that these special names are not limited to just being used for these operator functions, and may be used name other declarations. Some near misses", that will not match an operator form but looks like it may have been supposed to, will generate wantings but otherwise they are left alone. Note that this syntax works effectively but a textual transformation, the compiler converts all operators into functions and then resolves them normally. This means any combination of types may be used, although nonsensical ones (like @double ?==?(point, int);@) are discouraged. This feature is also used for all builtin operators as well, although those are implicitly provided by the language. %\subsection{Constructors and Destructors} Both constructors and destructors are operators, which means they are functions with special operator names rather than type names in \Cpp. The special operator names may be used to call the functions explicitly. In \CFA, constructors and destructors are operators, which means they are functions with special operator names rather than type names in \Cpp. Both constructors and destructors can be implicity called by the compiler, however the operator names allow explicit calls. % Placement new means that this is actually equivant to C++. The special name for a constructor is @?{}@, which comes from the initialization syntax in C, \eg @Example e = { ... }@. \CFA will generate a constructor call each time a variable is declared, passing the initialization arguments to the constructort. \CFA generates a constructor call each time a variable is declared, passing the initialization arguments to the constructor. \begin{cfa} struct Example { ... }; \end{cfa} Both @a@ and @b@ will be initalized with the first constructor, while @c@ will be initalized with the second. @b@ because of the explicit call and @a@ implicitly. @c@ will be initalized with the second constructor. Currently, there is no general way to skip initialation. % I don't use @= anywhere in the thesis. % I don't like the \^{} symbol but $^\wedge$ isn't better. Similarly destructors use the special name @^?{}@ (the @^@ has no special Similarly, destructors use the special name @^?{}@ (the @^@ has no special meaning). These are a normally called implicitly called on a variable when it goes out of scope. They can be called explicitly as well. \begin{cfa} void ^?{}(Example & this) { ... } { Example d; } // <- implicit destructor call \end{cfa} Whenever a type is defined, \CFA will create a default zero-argument ^?{}(d); Example e; } // Implicit call of ^?{}(e); \end{cfa} Whenever a type is defined, \CFA creates a default zero-argument constructor, a copy constructor, a series of argument-per-field constructors and a destructor. All user constructors are defined after this. Because operators are never part of the type definition they may be added at any time, including on built-in types. \section{Polymorphism} Note, a function named @do_once@ is not required in the scope of @do_twice@ to compile it, unlike \Cpp template expansion. Furthermore, call-site inferencing allows local replacement of the most specific parametric functions needs for a allows local replacement of the specific parametric functions needs for a call. \begin{cfa} to @do_twice@ and called within it. The global definition of @do_once@ is ignored, however if quadruple took a @double@ argument then the global definition would be used instead as it would be a better match. % Aaron's thesis might be a good reference here. To avoid typing long lists of assertions, constraints can be collect into convenient packages called a @trait@, which can then be used in an assertion @double@ argument, then the global definition would be used instead as it would then be a better match. \todo{cite Aaron's thesis (maybe)} To avoid typing long lists of assertions, constraints can be collected into convenient a package called a @trait@, which can then be used in an assertion instead of the individual constraints. \begin{cfa} functionality, like @sumable@, @listable@, \etc. Polymorphic structures and unions are defined by qualifying the aggregate type Polymorphic structures and unions are defined by qualifying an aggregate type with @forall@. The type variables work the same except they are used in field declarations instead of parameters, returns, and local variable declarations. node(T) * next; T * data; } }; node(int) inode; \end{cfa} coroutine CountUp { unsigned int next; } }; CountUp countup; \end{cfa} void main(CountUp & this) { for (unsigned int next = 0 ; true ; ++next) { next = up; this.next = next; suspend;$\label{suspend}$ } The first resume calls the @main@ function at the top. Thereafter, resume calls continue a coroutine in the last suspended function after the @suspend@ statement, in this case @main@ line~\ref{suspend}.  The @resume@ function takes a reference to the coroutine structure and returns the same reference. The return value allows easy access to communication variables defined in the coroutine object. For example, the @next@ value for coroutine object @countup@ is both generated and collected in the single expression: @resume(countup).next@. statement. In this case there is only one and, hence, the difference between subsequent calls is the state of variables inside the function and the coroutine object. The return value of @resume@ is a reference to the coroutine, to make it convent to access fields of the coroutine in the same expression. Here is a simple example in a helper function: \begin{cfa} unsigned int get_next(CountUp & this) { return resume(this).next; } \end{cfa} When the main function returns the coroutine halts and can no longer be resumed. \subsection{Monitor and Mutex Parameter} { StringWorker stringworker; // fork thread running in "main" } // <- implicitly join with thread / wait for completion } // Implicit call to join(stringworker), waits for completion. \end{cfa} The thread main is where a new thread starts execution after a fork operation