Changeset 0e398ad


Ignore:
Timestamp:
Apr 3, 2023, 5:51:56 PM (13 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, master
Children:
525a46a
Parents:
9432499
Message:

convert to CFAStyle in CFA_intro chapter

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/colby_parsons_MMAth/text/CFA_intro.tex

    r9432499 r0e398ad  
    99\CFA is a layer over C, is transpiled to C and is largely considered to be an extension of C.
    1010Beyond C, it adds productivity features, libraries, a type system, and many other language constructions.
    11 However, \CFA stays true to C as a language, with most code revolving around \code{struct}'s and routines, and respects the same rules as C.
    12 \CFA is not object oriented as it has no notion of \code{this} and no classes or methods, but supports some object oriented adjacent ideas including costructors, destructors, and limited inheritance.
     11However, \CFA stays true to C as a language, with most code revolving around @struct@'s and routines, and respects the same rules as C.
     12\CFA is not object oriented as it has no notion of @this@ and no classes or methods, but supports some object oriented adjacent ideas including costructors, destructors, and limited inheritance.
    1313\CFA is rich with interesting features, but a subset that is pertinent to this work will be discussed.
    1414
     
    1717References in \CFA are a layer of syntactic sugar over pointers to reduce the number of ref/deref operations needed with pointer usage.
    1818Some examples of references in \CFA are shown in Listing~\ref{l:cfa_ref}.
    19 Another related item to note is that the \CFA equivalent of \CC's \code{nullptr} is \code{0p}.
    20 
    21 \begin{cfacode}[caption={Example of \CFA references},label={l:cfa_ref}]
     19Another related item to note is that the \CFA equivalent of \CC's @nullptr@ is @0p@.
     20
     21\begin{cfa}[caption={Example of \CFA references},label={l:cfa_ref}]
    2222int i = 2;
    23 int & ref_i = i;            // declare ref to i
    24 int * ptr_i = &i;           // ptr to i
     23int & ref_i = i;        $\C[1.5in]{// declare ref to i}$
     24int * ptr_i = &i;       $\C{// ptr to i}$
    2525
    2626// address of ref_i is the same as address of i
    2727assert( &ref_i == ptr_i );
    2828
    29 int && ref_ref_i = ref_i;   // can have a ref to a ref
    30 ref_i = 3;                  // set i to 3
     29int && ref_ref_i = ref_i;   $\C{// can have a ref to a ref}$
     30ref_i = 3;                  $\C{// set i to 3}$
    3131int new_i = 4;
    3232
    3333// syntax to rebind ref_i (must cancel implicit deref)
    34 &ref_i = &new_i; // (&*)ref_i = &new_i; (sets underlying ptr)
    35 \end{cfacode}
     34&ref_i = &new_i;        $\C{// (\&*)ref\_i = \&new\_i; (sets underlying ptr)}\CRT$
     35\end{cfa}
    3636
    3737
     
    4343
    4444
    45 \begin{cfacode}[caption={Example of \CFA function overloading},label={l:cfa_overload}]
     45\begin{cfa}[caption={Example of \CFA function overloading},label={l:cfa_overload}]
    4646int foo() { printf("A\n");  return 0;}
    4747int foo( int bar ) { printf("B\n"); return 1; }
     
    5757    foo( a );               // prints 3
    5858}
    59 \end{cfacode}
     59\end{cfa}
    6060
    6161
     
    6868
    6969
    70 \begin{cfacode}[tabsize=3,caption={Usage of \CFA with statement},label={l:cfa_with}]
     70\begin{cfa}[tabsize=3,caption={Usage of \CFA with statement},label={l:cfa_with}]
    7171struct obj {
    7272    int a, b, c;
     
    100100    p.y = 2.71;
    101101}
    102 \end{cfacode}
     102\end{cfa}
    103103
    104104
     
    109109
    110110
    111 \begin{cfacode}[tabsize=3,caption={Example of \CFA operators},label={l:cfa_operate}]
     111\begin{cfa}[tabsize=3,caption={Example of \CFA operators},label={l:cfa_operate}]
    112112struct coord {
    113113    double x;
     
    125125        (op2.x*op2.x + op2.y*op2.y + op2.z*op2.z);
    126126}
    127 \end{cfacode}
     127\end{cfa}
    128128
    129129
     
    134134
    135135
    136 \begin{cfacode}[tabsize=3,caption={Example of \CFA constructors and destructors},label={l:cfa_ctor}]
     136\begin{cfa}[tabsize=3,caption={Example of \CFA constructors and destructors},label={l:cfa_ctor}]
    137137struct discrete_point {
    138138    int x;
     
    157157    discrete_point dp{ 2, -4 }; // specialized ctor
    158158} // ^d{}, ^p{}, ^dp{} all called as they go out of scope
    159 \end{cfacode}
     159\end{cfa}
    160160
    161161
     
    165165
    166166\subsection{Parametric Polymorphism}
    167 \CFA provides parametric polymorphism in the form of \code{forall}, and \code{trait}s.
    168 A \code{forall} takes in a set of types and a list of constraints.
    169 The declarations that follow the \code{forall} are parameterized over the types listed that satisfy the constraints.
    170 Sometimes the list of constraints can be long, which is where a \code{trait} can be used.
    171 A \code{trait} is a collection of constraints that is given a name and can be reused in foralls.
     167\CFA provides parametric polymorphism in the form of @forall@, and @trait@s.
     168A @forall@ takes in a set of types and a list of constraints.
     169The declarations that follow the @forall@ are parameterized over the types listed that satisfy the constraints.
     170Sometimes the list of constraints can be long, which is where a @trait@ can be used.
     171A @trait@ is a collection of constraints that is given a name and can be reused in foralls.
    172172An example of the usage of parametric polymorphism in \CFA is shown in Listing~\ref{l:cfa_poly}.
    173173
    174 \begin{cfacode}[tabsize=3,caption={Example of \CFA polymorphism},label={l:cfa_poly}]
     174\begin{cfa}[tabsize=3,caption={Example of \CFA polymorphism},label={l:cfa_poly}]
    175175// sized() is a trait that means the type has a size
    176176forall( V & | sized(V) )        // type params for trait
     
    215215}
    216216
    217 \end{cfacode}
     217\end{cfa}
    218218
    219219\subsection{Inheritance}
    220220Inheritance in \CFA copies its style from Plan-9 C nominal inheritance.
    221 In \CFA structs can \code{inline} another struct type to gain its fields and to be able to be passed to routines that require a parameter of the inlined type.
     221In \CFA structs can @inline@ another struct type to gain its fields and to be able to be passed to routines that require a parameter of the inlined type.
    222222An example of \CFA inheritance is shown in Listing~\ref{l:cfa_inherit}.
    223223
    224 \begin{cfacode}[tabsize=3,caption={Example of \CFA inheritance},label={l:cfa_inherit}]
     224\begin{cfa}[tabsize=3,caption={Example of \CFA inheritance},label={l:cfa_inherit}]
    225225struct one_d { double x; };
    226226struct two_d {
     
    260260    print_food( p ); // prints 5
    261261}
    262 \end{cfacode}
    263 
    264 
     262\end{cfa}
     263
     264
Note: See TracChangeset for help on using the changeset viewer.