[601bd9e] | 1 | % ====================================================================== |
---|
| 2 | % ====================================================================== |
---|
| 3 | \chapter{Introduction to \CFA}\label{s:cfa} |
---|
| 4 | % ====================================================================== |
---|
| 5 | % ====================================================================== |
---|
| 6 | |
---|
| 7 | \section{Overview} |
---|
[c459f99] | 8 | The following serves as an introduction to \CFA. |
---|
| 9 | \CFA is a layer over C, is transpiled\footnote{Source to source translator.} to C, and is largely considered to be an extension of C. |
---|
| 10 | Beyond C, it adds productivity features, extended libraries, an advanced type-system, and many control-flow/concurrency constructions. |
---|
| 11 | However, \CFA stays true to the C programming style, with most code revolving around @struct@'s and routines, and respects the same rules as C. |
---|
[2d831a1] | 12 | \CFA is not object oriented as it has no notion of @this@ (receiver) and no structures with methods, but supports some object oriented ideas including constructors, destructors, and limited nominal inheritance. |
---|
[59c05958] | 13 | While \CFA is rich with interesting features, only the subset pertinent to this work is discussed. |
---|
[0faacb8] | 14 | |
---|
| 15 | \section{References} |
---|
[c459f99] | 16 | References in \CFA are similar to references in \CC; however \CFA references are \emph{rebindable}, and support multi-level referencing. |
---|
| 17 | References in \CFA are a layer of syntactic sugar over pointers to reduce the number of ref/deref operations needed with pointer usage. |
---|
[59c05958] | 18 | Another difference is the use of @0p@ instead of C's @NULL@ or \CC's @nullptr@. |
---|
[c459f99] | 19 | Examples of references are shown in \VRef[Listing]{l:cfa_ref}. |
---|
[0faacb8] | 20 | |
---|
[0e398ad] | 21 | \begin{cfa}[caption={Example of \CFA references},label={l:cfa_ref}] |
---|
[0faacb8] | 22 | int i = 2; |
---|
[59c05958] | 23 | int & ref_i = i; $\C{// declare ref to i}$ |
---|
| 24 | int * ptr_i = &i; $\C{// ptr to i}$ |
---|
[0faacb8] | 25 | |
---|
| 26 | // address of ref_i is the same as address of i |
---|
| 27 | assert( &ref_i == ptr_i ); |
---|
| 28 | |
---|
[59c05958] | 29 | int && ref_ref_i = ref_i; $\C{// can have a ref to a ref}$ |
---|
| 30 | ref_i = 3; $\C{// set i to 3}$ |
---|
[0faacb8] | 31 | int new_i = 4; |
---|
| 32 | |
---|
| 33 | // syntax to rebind ref_i (must cancel implicit deref) |
---|
[59c05958] | 34 | &ref_i = &new_i; $\C{// (\&*)ref\_i = \&new\_i; (sets underlying ptr)}$ |
---|
[0e398ad] | 35 | \end{cfa} |
---|
[601bd9e] | 36 | |
---|
| 37 | |
---|
[59c05958] | 38 | \section{Overloading}\label{s:Overloading} |
---|
[c459f99] | 39 | \CFA routines can be overloaded on parameter type, number of parameters, and \emph{return type}. |
---|
| 40 | Variables can also be overloaded on type, meaning that two variables can have the same name so long as they have different types. |
---|
[59c05958] | 41 | A routine or variable is disambiguated at each usage site via its type and surrounding expression context. |
---|
[c459f99] | 42 | A cast is used to disambiguate any conflicting usage. |
---|
| 43 | Examples of overloading are shown in \VRef[Listing]{l:cfa_overload}. |
---|
[0faacb8] | 44 | |
---|
[59c05958] | 45 | \begin{cfa}[caption={Example of \CFA overloading},label={l:cfa_overload}] |
---|
| 46 | int foo() { sout | "A"; return 0;} |
---|
| 47 | int foo( int bar ) { sout | "B"; return 1; } |
---|
| 48 | int foo( double bar ) { sout | "C"; return 2; } |
---|
| 49 | double foo( double bar ) { sout | "D"; return 3; } |
---|
| 50 | void foo( double bar ) { sout | bar; } |
---|
[0faacb8] | 51 | |
---|
| 52 | int main() { |
---|
[59c05958] | 53 | foo(); $\C{// prints A}$ |
---|
| 54 | foo( 0 ); $\C{// prints B}$ |
---|
| 55 | int foo = foo( 0.0 ); $\C{// prints C}$ |
---|
| 56 | double foo = foo( 0.0 ); $\C{// prints D}$ |
---|
| 57 | foo( foo ); $\C{// prints 3., where left-hand side of expression is void}$ |
---|
[0faacb8] | 58 | } |
---|
[0e398ad] | 59 | \end{cfa} |
---|
[601bd9e] | 60 | |
---|
| 61 | |
---|
[68db00e] | 62 | \section{\lstinline{with} Statement}\label{s:with} |
---|
[59c05958] | 63 | The \CFA @with@ statement is for exposing fields of an aggregate type within a scope, allowing field names without qualification. |
---|
[c459f99] | 64 | This feature is also implemented in Pascal~\cite{Pascal}. |
---|
| 65 | It can exist as a stand-alone statement or wrap a routine body to expose aggregate fields. |
---|
| 66 | Examples of the @with@ statement are shown in \VRef[Listing]{l:cfa_with}. |
---|
[59c05958] | 67 | |
---|
| 68 | \begin{cfa}[caption={Example of \CFA \lstinline{with} statement},label={l:cfa_with}] |
---|
| 69 | struct pair { double x, y; }; |
---|
| 70 | struct triple { int a, b, c; }; |
---|
[0faacb8] | 71 | pair p; |
---|
| 72 | |
---|
[59c05958] | 73 | @with( p )@ { $\C{// stand-alone with}$ |
---|
| 74 | p.x = 6.28; p.y = 1.73; $\C{// long form}$ |
---|
| 75 | x = 6.28; y = 1.73; $\C{// short form}$ |
---|
[0faacb8] | 76 | } |
---|
[59c05958] | 77 | void foo( triple t, pair p ) @with( t, p )@ { $\C{// routine with}$ |
---|
| 78 | t.a = 1; t.b = 2; t.c = 3; p.x = 3.14; p.y = 2.71; $\C{// long form}$ |
---|
| 79 | a = 1; b = 2; c = 3; x = 3.14; y = 2.71; $\C{// short form}$ |
---|
[0faacb8] | 80 | } |
---|
[0e398ad] | 81 | \end{cfa} |
---|
[0faacb8] | 82 | |
---|
| 83 | |
---|
[68db00e] | 84 | \section{Operators}\label{s:Operators} |
---|
[c459f99] | 85 | Operators can be overloaded in \CFA with operator routines. |
---|
[59c05958] | 86 | Operators in \CFA are named using an operator symbol and '@?@' to represent operands. |
---|
[c459f99] | 87 | Examples of \CFA operators are shown in \VRef[Listing]{l:cfa_operate}. |
---|
[0faacb8] | 88 | |
---|
[59c05958] | 89 | \begin{cfa}[caption={Example of \CFA operators},label={l:cfa_operate}] |
---|
[0faacb8] | 90 | struct coord { |
---|
[59c05958] | 91 | double x, y, z; |
---|
[0faacb8] | 92 | }; |
---|
[59c05958] | 93 | coord ++@?@( coord & c ) with( c ) { $\C{// post increment}$ |
---|
| 94 | x++; y++; z++; |
---|
| 95 | return c; |
---|
[0faacb8] | 96 | } |
---|
[59c05958] | 97 | coord @?@<=@?@( coord op1, coord op2 ) with( op1 ) { $\C{// ambiguous with both parameters}$ |
---|
| 98 | return (x * x + y * y + z * z) <= (op2.x * op2.x + op2.y * op2.y + op2.z * op2.z); |
---|
[0faacb8] | 99 | } |
---|
[0e398ad] | 100 | \end{cfa} |
---|
[0faacb8] | 101 | |
---|
| 102 | |
---|
| 103 | \section{Constructors and Destructors} |
---|
[c459f99] | 104 | Constructors and destructors in \CFA are special operator routines used for creation and destruction of objects. |
---|
| 105 | The default constructor and destructor for a type are called implicitly upon creation and deletion, respectively. |
---|
| 106 | Examples of \CFA constructors and destructors are shown in \VRef[Listing]{l:cfa_ctor}. |
---|
[0faacb8] | 107 | |
---|
[59c05958] | 108 | \begin{cfa}[caption={Example of \CFA constructors and destructors},label={l:cfa_ctor}] |
---|
[0faacb8] | 109 | struct discrete_point { |
---|
[59c05958] | 110 | int x, y; |
---|
[0faacb8] | 111 | }; |
---|
[59c05958] | 112 | void ?{}( discrete_point & this ) with(this) { $\C{// default constructor}$ |
---|
| 113 | [x, y] = 0; |
---|
[0faacb8] | 114 | } |
---|
[59c05958] | 115 | void ?{}( discrete_point & this, int x, int y ) { $\C{// explicit constructor}$ |
---|
| 116 | this.[x, y] = [x, y]; |
---|
[0faacb8] | 117 | } |
---|
[59c05958] | 118 | void ^?{}( discrete_point & this ) with(this) { $\C{// destructor}$ |
---|
| 119 | ?{}( this ); $\C{// reset by calling default constructor}$ |
---|
[0faacb8] | 120 | } |
---|
| 121 | int main() { |
---|
[59c05958] | 122 | discrete_point x, y{}; $\C{// implicit call to default ctor, ?\{\}}$ |
---|
| 123 | discrete_point s = { 2, -4 }, t{ 4, 2 }; $\C{// explicit call to specialized ctor}$ |
---|
| 124 | } // ^t{}, ^s{}, ^y{}, ^x{} implicit calls in reverse allocation order |
---|
[0e398ad] | 125 | \end{cfa} |
---|
[0faacb8] | 126 | |
---|
| 127 | |
---|
| 128 | \section{Polymorphism}\label{s:poly} |
---|
[59c05958] | 129 | C supports limited polymorphism, often requiring users to implement polymorphism using a @void *@ (type erasure) approach. |
---|
[2d831a1] | 130 | \CFA extends C with generalized overloading polymorphism (see \VRef{s:Overloading}), as well as, parametric polymorphism and limited nominal inheritance. |
---|
[0faacb8] | 131 | |
---|
| 132 | \subsection{Parametric Polymorphism} |
---|
[c459f99] | 133 | \CFA provides parametric polymorphism in the form of @forall@, and @trait@s. |
---|
| 134 | A @forall@ takes in a set of types and a list of constraints. |
---|
| 135 | The declarations that follow the @forall@ are parameterized over the types listed that satisfy the constraints. |
---|
| 136 | A list of @forall@ constraints can be refactored into a named @trait@ and reused in @forall@s. |
---|
| 137 | Examples of \CFA parametric polymorphism are shown in \VRef[Listing]{l:cfa_poly}. |
---|
[0faacb8] | 138 | |
---|
[59c05958] | 139 | \begin{cfa}[caption={Example of \CFA parametric polymorphism},label={l:cfa_poly}] |
---|
[0faacb8] | 140 | // sized() is a trait that means the type has a size |
---|
[59c05958] | 141 | forall( V & | sized(V) ) $\C{// type params for trait}$ |
---|
[0faacb8] | 142 | trait vector_space { |
---|
[59c05958] | 143 | // dtor and copy ctor needed in constraints to pass by copy |
---|
| 144 | void ?{}( V &, V & ); $\C{// copy ctor for return}$ |
---|
| 145 | void ^?{}( V & ); $\C{// dtor}$ |
---|
| 146 | V ?+?( V, V ); $\C{// vector addition}$ |
---|
| 147 | V ?*?( int, V ); $\C{// scalar multiplication}$ |
---|
[0faacb8] | 148 | }; |
---|
| 149 | |
---|
[59c05958] | 150 | forall( V & | vector_space( V ) ) { |
---|
| 151 | V get_inverse( V v1 ) { |
---|
| 152 | return -1 * v1; $\C{// can use ?*? routine defined in trait}$ |
---|
| 153 | } |
---|
| 154 | V add_and_invert( V v1, V v2 ) { |
---|
| 155 | return get_inverse( v1 + v2 ); $\C{// can use ?+? routine defined in trait}$ |
---|
| 156 | } |
---|
[0faacb8] | 157 | } |
---|
| 158 | struct Vec1 { int x; }; |
---|
| 159 | void ?{}( Vec1 & this, Vec1 & other ) { this.x = other.x; } |
---|
| 160 | void ?{}( Vec1 & this, int x ) { this.x = x; } |
---|
| 161 | void ^?{}( Vec1 & this ) {} |
---|
[59c05958] | 162 | Vec1 ?+?( Vec1 v1, Vec1 v2 ) { v1.x += v2.x; return v1; } |
---|
| 163 | Vec1 ?*?( int c, Vec1 v1 ) { v1.x = v1.x * c; return v1; } |
---|
[0faacb8] | 164 | |
---|
| 165 | struct Vec2 { int x; int y; }; |
---|
| 166 | void ?{}( Vec2 & this, Vec2 & other ) { this.x = other.x; this.y = other.y; } |
---|
| 167 | void ?{}( Vec2 & this, int x ) { this.x = x; this.y = x; } |
---|
| 168 | void ^?{}( Vec2 & this ) {} |
---|
[59c05958] | 169 | Vec2 ?+?( Vec2 v1, Vec2 v2 ) { v1.x += v2.x; v1.y += v2.y; return v1; } |
---|
| 170 | Vec2 ?*?( int c, Vec2 v1 ) { v1.x = v1.x * c; v1.y = v1.y * c; return v1; } |
---|
[0faacb8] | 171 | |
---|
| 172 | int main() { |
---|
[59c05958] | 173 | Vec1 v1{ 1 }; $\C{// create Vec1 and call ctor}$ |
---|
| 174 | Vec2 v2{ 2 }; $\C{// create Vec2 and call ctor}$ |
---|
| 175 | // can use forall defined routines since types satisfy trait |
---|
| 176 | add_and_invert( get_inverse( v1 ), v1 ); |
---|
| 177 | add_and_invert( get_inverse( v2 ), v2 ); |
---|
[0faacb8] | 178 | } |
---|
[0e398ad] | 179 | \end{cfa} |
---|
[0faacb8] | 180 | |
---|
[4f6c628] | 181 | \subsection{Inheritance}\label{s:Inheritance} |
---|
[2d831a1] | 182 | Inheritance in \CFA is taken from Plan-9 C's nominal inheritance. |
---|
[c459f99] | 183 | In \CFA, @struct@s can @inline@ another struct type to gain its fields and masquerade as that type. |
---|
[2d831a1] | 184 | Examples of \CFA nominal inheritance are shown in \VRef[Listing]{l:cfa_inherit}. |
---|
[0faacb8] | 185 | |
---|
[2d831a1] | 186 | \begin{cfa}[caption={Example of \CFA nominal inheritance},label={l:cfa_inherit}] |
---|
[0faacb8] | 187 | struct one_d { double x; }; |
---|
[c459f99] | 188 | struct two_d { |
---|
[59c05958] | 189 | @inline@ one_d; |
---|
| 190 | double y; |
---|
[0faacb8] | 191 | }; |
---|
[c459f99] | 192 | struct three_d { |
---|
[59c05958] | 193 | @inline@ two_d; |
---|
| 194 | double z; |
---|
[0faacb8] | 195 | }; |
---|
| 196 | double get_x( one_d & d ){ return d.x; } |
---|
| 197 | |
---|
| 198 | struct dog {}; |
---|
| 199 | struct dog_food { |
---|
[59c05958] | 200 | int count; |
---|
[0faacb8] | 201 | }; |
---|
| 202 | struct pet { |
---|
[59c05958] | 203 | @inline@ dog; |
---|
| 204 | @inline@ dog_food; |
---|
[0faacb8] | 205 | }; |
---|
[59c05958] | 206 | void pet_dog( dog & d ) { sout | "woof"; } |
---|
| 207 | void print_food( dog_food & f ) { sout | f.count; } |
---|
[0faacb8] | 208 | |
---|
| 209 | int main() { |
---|
[59c05958] | 210 | one_d x; |
---|
| 211 | two_d y; |
---|
| 212 | three_d z; |
---|
| 213 | x.x = 1; |
---|
| 214 | y.x = 2; |
---|
| 215 | z.x = 3; |
---|
| 216 | get_x( x ); $\C{// returns 1;}$ |
---|
| 217 | get_x( y ); $\C{// returns 2;}$ |
---|
| 218 | get_x( z ); $\C{// returns 3;}$ |
---|
| 219 | pet p; |
---|
| 220 | p.count = 5; |
---|
| 221 | pet_dog( p ); $\C{// prints woof}$ |
---|
| 222 | print_food( p ); $\C{// prints 5}$ |
---|
[0faacb8] | 223 | } |
---|
[0e398ad] | 224 | \end{cfa} |
---|
[0faacb8] | 225 | |
---|
[59c05958] | 226 | % Local Variables: % |
---|
| 227 | % tab-width: 4 % |
---|
| 228 | % End: % |
---|