Changeset 0e398ad for doc/theses/colby_parsons_MMAth/text
- Timestamp:
- Apr 3, 2023, 5:51:56 PM (2 years ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 525a46a
- Parents:
- 9432499
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified doc/theses/colby_parsons_MMAth/text/CFA_intro.tex ¶
r9432499 r0e398ad 9 9 \CFA is a layer over C, is transpiled to C and is largely considered to be an extension of C. 10 10 Beyond 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.11 However, \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. 13 13 \CFA is rich with interesting features, but a subset that is pertinent to this work will be discussed. 14 14 … … 17 17 References in \CFA are a layer of syntactic sugar over pointers to reduce the number of ref/deref operations needed with pointer usage. 18 18 Some 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{cfa code}[caption={Example of \CFA references},label={l:cfa_ref}]19 Another 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}] 22 22 int i = 2; 23 int & ref_i = i; // declare ref to i24 int * ptr_i = &i; // ptr to i23 int & ref_i = i; $\C[1.5in]{// declare ref to i}$ 24 int * ptr_i = &i; $\C{// ptr to i}$ 25 25 26 26 // address of ref_i is the same as address of i 27 27 assert( &ref_i == ptr_i ); 28 28 29 int && ref_ref_i = ref_i; // can have a ref to a ref30 ref_i = 3; // set i to 329 int && ref_ref_i = ref_i; $\C{// can have a ref to a ref}$ 30 ref_i = 3; $\C{// set i to 3}$ 31 31 int new_i = 4; 32 32 33 33 // syntax to rebind ref_i (must cancel implicit deref) 34 &ref_i = &new_i; // (&*)ref_i = &new_i; (sets underlying ptr)35 \end{cfa code}34 &ref_i = &new_i; $\C{// (\&*)ref\_i = \&new\_i; (sets underlying ptr)}\CRT$ 35 \end{cfa} 36 36 37 37 … … 43 43 44 44 45 \begin{cfa code}[caption={Example of \CFA function overloading},label={l:cfa_overload}]45 \begin{cfa}[caption={Example of \CFA function overloading},label={l:cfa_overload}] 46 46 int foo() { printf("A\n"); return 0;} 47 47 int foo( int bar ) { printf("B\n"); return 1; } … … 57 57 foo( a ); // prints 3 58 58 } 59 \end{cfa code}59 \end{cfa} 60 60 61 61 … … 68 68 69 69 70 \begin{cfa code}[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}] 71 71 struct obj { 72 72 int a, b, c; … … 100 100 p.y = 2.71; 101 101 } 102 \end{cfa code}102 \end{cfa} 103 103 104 104 … … 109 109 110 110 111 \begin{cfa code}[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}] 112 112 struct coord { 113 113 double x; … … 125 125 (op2.x*op2.x + op2.y*op2.y + op2.z*op2.z); 126 126 } 127 \end{cfa code}127 \end{cfa} 128 128 129 129 … … 134 134 135 135 136 \begin{cfa code}[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}] 137 137 struct discrete_point { 138 138 int x; … … 157 157 discrete_point dp{ 2, -4 }; // specialized ctor 158 158 } // ^d{}, ^p{}, ^dp{} all called as they go out of scope 159 \end{cfa code}159 \end{cfa} 160 160 161 161 … … 165 165 166 166 \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. 168 A @forall@ takes in a set of types and a list of constraints. 169 The declarations that follow the @forall@ are parameterized over the types listed that satisfy the constraints. 170 Sometimes the list of constraints can be long, which is where a @trait@ can be used. 171 A @trait@ is a collection of constraints that is given a name and can be reused in foralls. 172 172 An example of the usage of parametric polymorphism in \CFA is shown in Listing~\ref{l:cfa_poly}. 173 173 174 \begin{cfa code}[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}] 175 175 // sized() is a trait that means the type has a size 176 176 forall( V & | sized(V) ) // type params for trait … … 215 215 } 216 216 217 \end{cfa code}217 \end{cfa} 218 218 219 219 \subsection{Inheritance} 220 220 Inheritance 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.221 In \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. 222 222 An example of \CFA inheritance is shown in Listing~\ref{l:cfa_inherit}. 223 223 224 \begin{cfa code}[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}] 225 225 struct one_d { double x; }; 226 226 struct two_d { … … 260 260 print_food( p ); // prints 5 261 261 } 262 \end{cfa code}263 264 262 \end{cfa} 263 264
Note: See TracChangeset
for help on using the changeset viewer.