Changeset 382edbe


Ignore:
Timestamp:
Jun 8, 2021, 11:44:03 AM (3 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
471ff17
Parents:
21f2e92
Message:

Andrew MMath: Folded in changes to existing. (1/3 from this review)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/andrew_beach_MMath/existing.tex

    r21f2e92 r382edbe  
    1 \chapter{\CFA Existing Features}
     1\chapter{\CFA{} Existing Features}
    22\label{c:existing}
    33
    4 \CFA (C-for-all)~\cite{Cforall} is an open-source project extending ISO C with
     4\CFA is an open-source project extending ISO C with
    55modern safety and productivity features, while still ensuring backwards
    66compatibility with C and its programmers.  \CFA is designed to have an
     
    99existing C code-base allowing programmers to learn \CFA on an as-needed basis.
    1010
    11 Only those \CFA features pertinent to this thesis are discussed.  Many of the
    12 \CFA syntactic and semantic features used in the thesis should be fairly
    13 obvious to the reader.
     11Only those \CFA features pertaining to this thesis are discussed.
     12Also, only new features of \CFA will be discussed, a basic familiarity with
     13C or C-like languages is assumed.
    1414
    1515\section{Overloading and \lstinline{extern}}
     
    4545\CFA adds a reference type to C as an auto-dereferencing pointer.
    4646They work very similarly to pointers.
    47 Reference-types are written the same way as a pointer-type is but each
     47Reference-types are written the same way as a pointer-type but each
    4848asterisk (@*@) is replaced with a ampersand (@&@);
    4949this includes cv-qualifiers and multiple levels of reference.
    5050
    51 They are intended for cases where you would want to use pointers but would
    52 be dereferencing them (almost) every usage.
    53 In most cases a reference can just be thought of as a pointer that
    54 automatically puts a dereference infront of each of its uses (per-level of
    55 reference).
    56 The address-of operator (@&@) acts as an escape and removes one of the
    57 automatic dereference operations.
    58 Mutable references may be assigned to by converting them to a pointer
    59 with a @&@ and then assigning a pointer too them.
    60 
    61 \begin{minipage}{0,45\textwidth}
     51Generally, references act like pointers with an implicate dereferencing
     52operation added to each use of the variable.
     53These automatic dereferences may be disabled with the address-of operator
     54(@&@).
     55
     56% Check to see if these are generating errors.
     57\begin{minipage}{0,5\textwidth}
    6258With references:
    6359\begin{cfa}
     
    7066\end{cfa}
    7167\end{minipage}
    72 \begin{minipage}{0,45\textwidth}
     68\begin{minipage}{0,5\textwidth}
    7369With pointers:
    7470\begin{cfa}
     
    8278\end{minipage}
    8379
    84 \section{Constructors and Destructors}
    85 
    86 Both constructors and destructors are operators, which means they are
    87 functions with special operator names rather than type names in \Cpp. The
    88 special operator names may be used to call the functions explicitly (not
    89 allowed in \Cpp for constructors).
    90 
    91 In general, operator names in \CFA are constructed by bracketing an operator
    92 token with @?@, which indicates the position of the arguments. For example,
     80References are intended to be used when you would use pointers but would
     81be dereferencing them (almost) every usage.
     82Mutable references may be assigned to by converting them to a pointer
     83with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above
     84
     85\section{Operators}
     86
     87\CFA implements operator overloading by providing special names.
     88Operator uses are translated into function calls using these names.
     89These names are created by taking the operator symbols and joining them with
     90@?@ where the arguments would go.
     91For example,
    9392infixed multiplication is @?*?@ while prefix dereference is @*?@.
    9493This syntax make it easy to tell the difference between prefix operations
    9594(such as @++?@) and post-fix operations (@?++@).
    9695
     96\begin{cfa}
     97int ?+?(point a, point b) { return point{a.x + b.x, a.y + b.y}; }
     98bool ?==?(point a, point b) { return a.x == b.x && a.y == b.y; }
     99{
     100        assert(point{1, 2} + point{3, 4} == point{4, 6});
     101}
     102\end{cfa}
     103Note that these special names are not limited to just being used for these
     104operator functions, and may be used name other declarations.
     105Some ``near misses", that will not match an operator form but looks like
     106it may have been supposed to, will generate wantings but otherwise they are
     107left alone.
     108
     109%\subsection{Constructors and Destructors}
     110
     111Both constructors and destructors are operators, which means they are
     112functions with special operator names rather than type names in \Cpp. The
     113special operator names may be used to call the functions explicitly.
     114% Placement new means that this is actually equivant to C++.
     115
    97116The special name for a constructor is @?{}@, which comes from the
    98 initialization syntax in C. That initialation syntax is also the operator
    99 form. \CFA will generate a constructor call each time a variable is declared,
     117initialization syntax in C, \eg @Example e = { ... }@.
     118\CFA will generate a constructor call each time a variable is declared,
    100119passing the initialization arguments to the constructort.
    101120\begin{cfa}
     
    111130}
    112131\end{cfa}
    113 Both @a@ and @b@ will be initalized with the first constructor (there is no
    114 general way to skip initialation) while @c@ will be initalized with the
    115 second.
     132Both @a@ and @b@ will be initalized with the first constructor,
     133while @c@ will be initalized with the second.
     134Currently, there is no general way to skip initialation.
    116135
    117136% I don't like the \^{} symbol but $^\wedge$ isn't better.
    118137Similarly destructors use the special name @^?{}@ (the @^@ has no special
    119 meaning). They can be called explicatly as well but normally they are
    120 implicitly called on a variable when it goes out of scope.
     138meaning).
     139These are a normally called implicitly called on a variable when it goes out
     140of scope. They can be called explicitly as well.
    121141\begin{cfa}
    122142void ^?{}(Example & this) { ... }
    123143{
    124     Example d;
     144        Example d;
    125145} // <- implicit destructor call
    126146\end{cfa}
    127 No operator name is restricted in what function signatures they may be bound
    128 to although most of the forms cannot be called in operator form. Some
    129 ``near-misses" will generate warnings.
    130147
    131148Whenever a type is defined, \CFA will create a default zero-argument
     
    153170char capital_a = identity( 'A' );
    154171\end{cfa}
    155 Each use of a polymorphic declaration will resolve its polymorphic parameters
     172Each use of a polymorphic declaration resolves its polymorphic parameters
    156173(in this case, just @T@) to concrete types (@int@ in the first use and @char@
    157174in the second).
     
    159176To allow a polymorphic function to be separately compiled, the type @T@ must be
    160177constrained by the operations used on @T@ in the function body. The @forall@
    161 clauses is augmented with a list of polymorphic variables (local type names)
     178clause is augmented with a list of polymorphic variables (local type names)
    162179and assertions (constraints), which represent the required operations on those
    163180types used in a function, \eg:
    164181\begin{cfa}
    165 forall( T | { void do_once(T); })
     182forall( T | { void do_once(T); } )
    166183void do_twice(T value) {
    167184        do_once(value);
     
    190207void do_once(double y) { ... }
    191208int quadruple(int x) {
    192         void do_once(int y) { y = y * 2; }
     209        void do_once(int & y) { y = y * 2; }
    193210        do_twice(x);
    194211        return x;
     
    200217function. The matched assertion function is then passed as a function pointer
    201218to @do_twice@ and called within it.
    202 The global definition of @do_once@ is ignored.
     219The global definition of @do_once@ is ignored, however if quadruple took a
     220@double@ argument then the global definition would be used instead as it
     221would be a better match.
     222% Aaron's thesis might be a good reference here.
    203223
    204224To avoid typing long lists of assertions, constraints can be collect into
     
    270290Each coroutine has a @main@ function, which takes a reference to a coroutine
    271291object and returns @void@.
     292%[numbers=left] Why numbers on this one?
    272293\begin{cfa}
    273294void main(CountUp & this) {
    274     for (unsigned int next = 0 ; true ; ++next) {
     295        for (unsigned int next = 0 ; true ; ++next) {
    275296                next = up;
    276297                suspend;$\label{suspend}$
Note: See TracChangeset for help on using the changeset viewer.