Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/uC++toCFA/uC++toCFA.tex

    r04375cf r8617ee90  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Mon Sep  8 18:10:30 2025
    14 %% Update Count     : 6534
     13%% Last Modified On : Sat Mar 15 13:38:53 2025
     14%% Update Count     : 6302
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     17% requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended
    1718
    1819\documentclass[11pt]{article}
     
    8283\setlength{\topmargin}{-0.45in}                                                 % move running title into header
    8384\setlength{\headsep}{0.25in}
    84 \setlength{\tabcolsep}{15pt}
    8585
    8686%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    134134
    135135\maketitle
    136 \vspace*{-0.65in}
     136\vspace*{-0.55in}
    137137
    138138\section{Introduction}
    139139
    140 \CFA is an extension of the C programming with a trait-style type-system rather then templates and objects as in \CC.
    141 \CFA allows overloading of variables and routines using the left-hand assignment type to precisely select among overloaded names.
    142 \begin{cfa}
    143 int x;  char x;  double x;    // overload name x
    144 int x();  double x();  char x();
    145 \end{cfa}
    146 \vspace*{-8pt}
    147 \begin{cquote}
    148 \begin{tabular}{@{}l@{\hspace{1in}}|l@{}}
    149 \begin{cfa}
    150 x += 42;        $\C[1in]{// int x}$
    151 x += 42.2;      $\C{// double x}$
    152 x += 'a';       $\C{// char x}\CRT$
    153 \end{cfa}
    154 &
    155 \begin{cfa}
    156 int j = x();    $\C[1in]{// int x()}$
    157 double j = x(); $\C{// double x();}$
    158 char j = x();   $\C{// char x()}\CRT$
    159 \end{cfa}
    160 \end{tabular}
    161 \end{cquote}
    162 \CFA generalizes reference types, allowing multiple and rebindable references (like pointers).
     140\CFA is NOT an object-oriented programming-language.
     141\CFA uses parametric polymorphism and allows overloading of variables and routines:
     142\begin{cfa}
     143int i;  char i;  double i;      $\C[2in]{// overload name i}$
     144int i();  double i();  char i();
     145i += 1;                                         $\C{// int i}$
     146i += 1.0;                                       $\C{// double i}$
     147i += 'a';                                       $\C{// char i}$
     148int j = i();                            $\C{// int i()}$
     149double j = i();                         $\C{// double i();}$
     150char j = i();                           $\C{// char i()}\CRT$
     151\end{cfa}
     152\CFA has rebindable references.
    163153\begin{cquote}
    164154\begin{tabular}{@{}l|l@{}}
     
    175165&
    176166\begin{cfa}
    177 r2i = 3;         $\C[0.875in]{// change x}$
     167r2i = 3; $\C[1.0in]{// change x}$
    178168&r2i = &r1y; $\C{// change p2i / r2i}$
    179 r2i = 3;        $\C{// change y}$
     169r2i = 3; $\C{// change y}$
    180170&r1x = &r1y; $\C{// change p1x / r1x}$
    181 r2i = 4;        $\C{// change y}$
     171r2i = 4; $\C{// change y}$
    182172&r1x = @0p@; $\C{// reset}\CRT$
    183173\end{cfa}
     
    189179int & @const@ & @const@ crcr = cr; // generalize
    190180\end{cfa}
    191 
    192 
    193 \section{Control Flow}
    194 
    195 The @choose@ statement provides an implicit @break@ after the @case@ clause for safety.
    196 It is possible to @break default@ in a @case@ clause to transfer to common code in the @default@ clause.
    197 \begin{cquote}
    198 \begin{tabular}{@{}l|l@{}}
    199 \begin{uC++}
    200 switch ( i ) {
    201   case 1: ... @break@; // explicit break
    202   case 2: ... @break@; // explicit break
    203   default: ... ;
    204 }
    205 \end{uC++}
    206 &
    207 \begin{cfa}
    208 choose ( i ) {
    209   case 1: ... ; // implicit break
    210   case 2: ... ; // implicit break
    211   default: ... ;
    212 }
    213 \end{cfa}
    214 \end{tabular}
    215 \end{cquote}
    216 To simplify creating an infinite loop, the loop condition in optional.
    217 \begin{cquote}
    218 \begin{tabular}{@{}l|l@{}}
    219 \begin{uC++}
    220 while ( true ) ...
    221 for ( ;; ) ...
    222 do ... while ( true )
    223 \end{uC++}
    224 &
    225 \begin{uC++}
    226 while ($\,$) ...
    227 for ($\,$) ...
    228 do ... while ($\,$)
    229 \end{uC++}
    230 \end{tabular}
    231 \end{cquote}
    232 To simplify loop iteration a range is provided, from low to high, and a traversal direction, ascending (@+@) or descending (@-@).
    233 The following is the syntax for the loop range, where @[@\,@]@ means optional.
    234 \begin{cfa}[deletekeywords=default]
    235 [ @index ;@ ] [ [ @min@ (default 0) ] [ direction @+@/@-@ (default +) ] @~@ [ @=@ (include endpoint) ] ] @max@ [ @~ increment@ ]
    236 \end{cfa}
    237 For @=@, the range includes the endpoint (@max@/@min@) depending on the direction (@+@/@-@).
    238 \begin{cquote}
    239 \begin{tabular}{@{}l|l@{}}
    240 \begin{uC++}
    241 for ( int i = 0; i < @10@; i += 1 ) { ... }
    242 for ( int i = @5@; i < @15@; i += @2@ ) { ... }
    243 for ( int i = -2; i <@=@ 10; i += 3 ) { ... }
    244 for ( int i = 10; i > -3; i @-@= 1 ) { ... }
    245 for ( int i = 10; i >@=@ 0; i @-@= 1 ) { ... }
    246 \end{uC++}
    247 &
    248 \begin{cfa}
    249 for ( @10@ ) { ... }  /  for ( i; @10@ ) { ... }  // 0 to 9 by 1
    250 for ( i; @5@ ~ @15@ ~ @2@ ) { ... } // 5 to 14 by 2
    251 for ( i; -2 ~@=@ 10 ~ 3 ) { ... } // -2 to 10 by 3
    252 for ( i; -3 @-@~ 10 ) { ... } // not 10 -~= -3, 10 to -2 by -1
    253 for ( i; 0 @-@~@=@ 10 ) { ... } // not 10 -~= 0, 10 to 0 by -1
    254 \end{cfa}
    255 \end{tabular}
    256 \end{cquote}
    257 A terminating loop @else@ (like Python) is executed if the loop terminates normally, \ie the loop conditional becomes false, which is safer than retesting after the loop.
    258 The loop index is available in the @else@ clause.
    259 \begin{cquote}
    260 \begin{tabular}{@{}l|l@{}}
    261 \begin{uC++}
    262 int i = 0
    263 for ( i = 0; i < 10; i += 1 ) { ... }
    264 @if ( i == 10 )@ { ... }
    265 \end{uC++}
    266 &
    267 \begin{cfa}
    268 
    269 for ( i; 10 ) { ... }
    270 @else@ { ... } // i == 10 because of post increment
    271 \end{cfa}
    272 \end{tabular}
    273 \end{cquote}
    274 Single/multiple-level loop exit/continue is provided by the labelled @break@/@continue@. (First example is \CC.)
    275 \begin{cquote}
    276 \begin{tabular}{@{}l|l|l@{}}
    277 \begin{C++}
    278 @L1:@ for ( ;; ) {
    279         for ( ;; ) {
    280                 ... if ( ... ) @goto L1@; ...
    281                 ... if ( ... ) @goto L2@; ...
    282         } @L2: ;@
    283 }
    284 \end{C++}
    285 &
    286 \begin{cfa}
    287 @L1:@ for () {
    288         @L2:@ for () {
    289                 ... if ( ... ) @continue L1@; ...
    290                 ... if ( ... ) @break L2@; ...
    291         }
    292 }
    293 \end{cfa}
    294 &
    295 \begin{cfa}
    296 @L1:@ for () {
    297         @L2:@ for () {
    298                 ... if ( ... ) @continue L1@; ...
    299                 ... if ( ... ) @break L2@; ...
    300         }
    301 }
    302 \end{cfa}
    303 \end{tabular}
    304 \end{cquote}
    305 
    306 
    307 \section{Exception}
    308 
    309 Currently, \CFA uses macros @ExceptionDecl@ and @ExceptionInst@ to declare and instantiate an exception.
    310 \begin{cquote}
    311 \setlength{\tabcolsep}{5pt}
    312 \begin{tabular}{@{}l|ll@{}}
    313 \begin{uC++}
    314 
    315 @_Exception@ E {        // local or global scope
    316         ... // exception fields
    317 };
    318 try {
    319         ... if ( ... ) @_Resume@ E( /* initialization */ ); ...
    320         ... if ( ... ) @_Throw@ E( /* initialization */ ); ...
    321 } @_CatchResume@( E & /* reference */ ) { ... }
    322   catch( E & ) { ... }
    323   catch( ... /* catch any */ ) { ... }
    324   _Finally { ... }
    325 \end{uC++}
    326 &
    327 \begin{cfa}
    328 #include <Exception.hfa>
    329 @ExceptionDecl@( E,             // must be global scope
    330         ... // exception fields
    331 );
    332 try {
    333         ... if ( ... ) @throwResume@ @ExceptionInst@( E, /* intialization */ ); ...
    334         ... if ( ... ) @throw@ @ExceptionInst@( E, /* intialization */ ); ...
    335 } @catchResume@( E @*@ /* pointer */ ) { ... }
    336   catch( E * ) { ... }
    337   catch( exception_t @*@ /* catch any */ ) { ... }
    338   finally { ... }
    339 \end{cfa}
    340 \end{tabular}
    341 \end{cquote}
    342 
    343 
    344 \section{Non-local Exception}
    345 
    346 \begin{cquote}
    347 \begin{tabular}{@{}l|ll@{}}
    348 \begin{uC++}
    349 
    350 
    351 void main() {
    352         try {
    353                 _Enable {
    354                         ... suspend(); ...
    355                 }
    356         } @_CatchResume@( E & /* reference */ ) { ... }
    357           catch( E & ) { ... }
    358 }
    359 \end{uC++}
    360 &
    361 \begin{cfa}
    362 #define resumePoll( coroutine ) resume( coroutine ); poll()
    363 #define suspendPoll suspend; poll()
    364 void main() {
    365         try {
    366                 enable_ehm();
    367                 ... suspendPoll ...
    368                 disable_ehm();
    369         } @catchResume@( E * ) { ... }
    370           catch( E & ) { ... }
    371 }
    372 \end{cfa}
    373 \end{tabular}
    374 \end{cquote}
    375 
    376 
    377 \section{Stream I/O}
    378 
    379 \CFA output streams automatically separate values and insert a newline at the end of the print.
    380 \begin{cquote}
    381 \begin{tabular}{@{}l|l@{}}
    382 \begin{uC++}
    383 #include <@iostream@>
    384 using namespace std;
    385 int i;   double d;   char c;
    386 cin >> i >> d >> c;
    387 cout << i << ' ' << d << ' ' << c << endl;
    388 \end{uC++}
    389 &
    390 \begin{cfa}
    391 #include <@fstream.hfa@>
    392 
    393 int i;   double d;   char c;
    394 sin | i | d | c;
    395 sout | i | d | c
    396 \end{cfa}
    397 \end{tabular}
    398 \end{cquote}
    399 To disable/enable automatic newline at the end of printing, use @nlOff@/@nlOn@ and @nl@.
    400 \begin{cquote}
    401 \begin{tabular}{@{}l|l@{}}
    402 \begin{uC++}
    403 
    404 for ( int i = 0; i < 5; i += 1 ) cout << i << ' ';
    405 cout << @endl@;
    406 0 1 2 3 4
    407 \end{uC++}
    408 &
    409 \begin{cfa}
    410 sout | @nlOff@; // disable auto nl
    411 for ( i; 5 ) sout | i;
    412 sout | @nl@; sout | @nlOn@;  // enable auto nl
    413 0 1 2 3 4
    414 \end{cfa}
    415 \end{tabular}
    416 \end{cquote}
    417 Floating-point numbers without a fraction print with a decimal point, which can be disabled with @nodp@.
    418 \begin{cquote}
    419 \begin{tabular}{@{}l|l@{}}
    420 \begin{uC++}
    421 cout << 3.0 << ' ' << showpoint << setprecision(0) << 3.0 << endl;
    422 3 3.
    423 \end{uC++}
    424 &
    425 \begin{cfa}
    426 sout | @nodp( 3.0 )@ | 3.0;
    427 3 3.
    428 \end{cfa}
    429 \end{tabular}
    430 \end{cquote}
    431 
    432 
    433 \section{String}
    434 
    435 The @string@ type in \CFA is very similar to that in \CC.
    436 \begin{cquote}
    437 \begin{tabular}{@{}l|l@{}}
    438 \multicolumn{2}{@{}l@{}}{\lstinline{string s1, s2;}} \\
    439 \begin{uC++}
    440 s1 = "abcdefg";
    441 s2 = s1;
    442 s1 += s2;
    443 s1 == s2; s1 != s2;
    444 s1 < s2;  s1 <= s2;  s1 > s2;  s1 >= s2;
    445 s1.length();
    446 s1[3];
    447 s1.substr( 2 );  s1.substr( 2, 3 );
    448 s1.replace( 2, 5, s2 );
    449 s1.find( s2 );
    450 s1.find_first_of( "cd" );
    451 s1.find_first_not_of( "cd" );
    452 getline( cin, s1 );
    453 cout << s1 << endl;
    454 \end{uC++}
    455 &
    456 \begin{cfa}
    457 s1 = "abcdefg";
    458 s2 = s1;
    459 s1 += s2;
    460 s1 == s2; s1 != s2;
    461 s1 < s2;  s1 <= s2;  s1 > s2;  s1 >= s2;
    462 len( s1 ); // like C strlen( s1 )
    463 s1[3];
    464 s1( 2 );  s1( 2, 3 );
    465 s1( 2, 5 ) = s2;
    466 find( s1, s2 );
    467 exclude( s1, "cd" );  // longest sequence excluding "c" and "d"
    468 include( s1, "cd" );  // longest sequence including "c" and "d"
    469 sin | getline( s1 );
    470 sout | s1;
    471 \end{cfa}
    472 \end{tabular}
    473 \end{cquote}
    474 
    475 
    476 \section{\texorpdfstring{\lstinline{uArray}}{uArray}}
    477 
    478 \begin{cquote}
    479 \setlength{\tabcolsep}{5pt}
    480 \begin{tabular}{@{}l|l@{}}
    481 \begin{uC++}
    482 #include <iostream>
    483 using namespace std;
    484 
    485 struct S {
    486         int i;
    487         S( int i ) { S::i = i; }
    488 };
    489 void f( @uArrayRef( S, parm )@ );
    490 int main() {
    491         enum { N = 5 };
    492         @uArray( S, s, N );@   // stack, no ctor calls
    493         for ( int i = 0; i < N; i += 1 ) @s[i]( i )@; // ctor calls
    494         for ( int i = 0; i < N; i += 1 ) cout << s[i]@->@i << endl;
    495         f( s );
    496         @uArrayPtr( S, sp, N );@   // heap, no ctor calls
    497         for ( int i = 0; i < N; i += 1 ) @sp[i]( i )@; // ctor calls
    498         for ( int i = 0; i < N; i += 1 ) cout << sp[i]@->@i << endl;
    499         f( sp );
    500 } // delete s, sp
    501 \end{uC++}
    502 &
    503 \begin{cfa}
    504 #include <fstream.hfa>
    505 #include <array.hfa>
    506 #include <memory.hfa>
    507 struct S {
    508         int i;
    509 };
    510 void ?{}( S & s, int i ) { s.i = i; }
    511 @forall( [N] )@ void f( @array( S, N ) & parm@ ) {}
    512 int main() {
    513         enum { N = 5 };
    514         @array( S, N ) s = { delay_init };@ // no ctor calls
    515         for ( i; N ) @s[i]{ i }@; // ctor calls
    516         for ( i; N ) sout | s[i]@.@i;
    517         f( s );
    518     @unique_ptr( array( S, N ) )@ sp = { delay_init }; // heap
    519     for ( int i = 0; i < N; i += 1 ) @(*sp)@[i]{ i }; // ctor calls
    520     for ( int i = 0; i < N; i += 1 ) sout | @(*sp)@[i].i;
    521         f( @*sp@ );
    522 } // delete s, sp
    523 \end{cfa}
    524 \end{tabular}
    525 \end{cquote}
    526 
    527 
    528 \section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}}
    529 
    530 \CFA is NOT an object-oriented programming-language, so there is no receiver (\lstinline[language=c++]{this}) or nested structure routines.
    531 The equivalent of a \emph{member} routine has an explicit structure parameter in any parameter position (often the first).
    532 \begin{cquote}
    533 \begin{tabular}{@{}l|l@{}}
    534 \begin{uC++}
    535 struct S {
    536         int i = 0;  // cheat, implicit default constructor
    537         int setter( int j ) { int t = i; i = j; return t; }
    538         int getter() { return i; }
    539 };
    540 S s;
    541 @s.@setter( 3 );  // object calls
    542 int k = @s.@getter();
    543 \end{uC++}
    544 &
    545 \begin{cfa}
    546 struct S {  int i;  };
    547 void ?{}( S & s ) { s.i = 0; } // explicit default constructor
    548 int setter( @S & s,@ int j ) @with( s )@ { int t = i; i = j; return t; }
    549 int getter( @S & s@ ) @with( s )@ { return i; }
    550 
    551 S s;
    552 setter( @s,@ 3 );  // normal calls
    553 int k = getter( @s@ );
    554 \end{cfa}
    555 \end{tabular}
    556 \end{cquote}
    557181Aggregate qualification is reduced or eliminated by opening scopes using the @with@ clause.
    558182\begin{cfa}
     
    570194\end{cfa}
    571195\noindent
    572 In subsequent code examples, the left example is \CC/\uC and the right example is \CFA.
     196In subsequent code examples, the left example is \uC and the right example is \CFA.
     197
     198
     199\section{Looping}
     200
     201\begin{cquote}
     202\begin{tabular}{@{}l|l@{}}
     203\begin{uC++}
     204for ( @;;@ ) { ... }  /  while ( @true@ ) { ... }
     205for ( int i = 0; i < @10@; i += 1 ) { ... }
     206for ( int i = @5@; i < @15@; i += @2@ ) { ... }
     207for ( int i = -1; i <@=@ 10; i += 3 ) { ... }
     208for ( int i = 10; i > 0; i @-@= 1 ) { ... }
     209\end{uC++}
     210&
     211\begin{cfa}
     212for () { ... }  /  while () { ... }
     213for ( @10@ ) { ... }  /  for ( i; @10@ ) { ... }
     214for ( i; @5@ ~ @15@ ~ @2@ ) { ... }
     215for ( i; -1 ~@=@ 10 ~ 3 ) { ... }
     216for ( i; 0 @-@~ 10 ) { ... }
     217\end{cfa}
     218\end{tabular}
     219\end{cquote}
     220
     221\begin{cquote}
     222\begin{tabular}{@{}l|l@{}}
     223\begin{uC++}
     224int i = 0
     225for ( i = 0; i < 10; i += 1 ) { ... }
     226@if ( i == 10 )@ { ... }
     227\end{uC++}
     228&
     229\begin{cfa}
     230
     231for ( i; 10 ) { ... }
     232@else@ { ... } // i == 10
     233\end{cfa}
     234\end{tabular}
     235\end{cquote}
     236
     237\begin{cquote}
     238\begin{tabular}{@{}l|l@{}}
     239\begin{uC++}
     240@L1:@ for ( ;; ) {
     241        @L2:@ for ( ;; ) {
     242                ... if ( ... ) @break L1@; ...
     243                ... if ( ... ) @break L2@; ...
     244        }
     245}
     246\end{uC++}
     247&
     248\begin{cfa}
     249@L1:@ for () {
     250        @L2:@ for () {
     251                ... if ( ... ) @break L1@; ...
     252                ... if ( ... ) @break L2@; ...
     253        }
     254}
     255\end{cfa}
     256\end{tabular}
     257\end{cquote}
     258
     259
     260\section{Stream I/O}
     261
     262\CFA output streams automatically separate values and insert a newline at the end of the print.
     263\begin{cquote}
     264\begin{tabular}{@{}l|l@{}}
     265\begin{uC++}
     266#include <@iostream@>
     267using namespace std;
     268int i;   double d;   char c;
     269cin >> i >> d >> c;
     270cout << i << ' ' << d << ' ' << c << endl;
     271\end{uC++}
     272&
     273\begin{cfa}
     274#include <@fstream.hfa@>
     275
     276int i;   double d;   char c;
     277sin | i | d | c;
     278sout | i | d | c
     279\end{cfa}
     280\end{tabular}
     281\end{cquote}
     282To disable/enable automatic newline at the end of printing, use @nlOff@/@nlOn@ and @nl@.
     283\begin{cquote}
     284\begin{tabular}{@{}l|l@{}}
     285\begin{uC++}
     286
     287for ( int i = 0; i < 5; i += 1 ) cout << i << ' ';
     288cout << @endl@;
     289
     2900 1 2 3 4
     291\end{uC++}
     292&
     293\begin{cfa}
     294sout | @nlOff@; // disable auto nl
     295for ( i; 5 ) sout | i;
     296sout | @nl@;
     297sout | @nlOn@;  // reenable auto nl
     2980 1 2 3 4
     299\end{cfa}
     300\end{tabular}
     301\end{cquote}
     302Floating-point numbers without a fraction print with a decimal point, which can be disabled with @nodp@.
     303\begin{cquote}
     304\begin{tabular}{@{}l|l@{}}
     305\begin{uC++}
     306cout << 3.0 << ' ' << showpoint << setprecision(0) << 3.0 << endl;
     3073 3.
     308\end{uC++}
     309&
     310\begin{cfa}
     311sout | @nodp( 3.0 )@ | 3.0;
     3123 3.
     313\end{cfa}
     314\end{tabular}
     315\end{cquote}
     316
     317
     318\section{Exception}
     319
     320Currently, \CFA uses macros @ExceptionDecl@ and @ExceptionInst@ to declare and instantiate an exception.
     321\begin{cquote}
     322\begin{tabular}{@{}l|ll@{}}
     323\begin{uC++}
     324
     325@_Exception@ E {        // local or global scope
     326        ... // exception fields
     327};
     328try {
     329        ...
     330        if ( ... ) @_Resume@ E( /* initialization */ );
     331        if ( ... ) @_Throw@ E( /* initialization */ );
     332                ...
     333} @_CatchResume@( E & /* reference */ ) { ... }
     334  catch( E & ) { ... }
     335  catch( ... /* catch any */ ) { ... }
     336  _Finally { ... }
     337\end{uC++}
     338&
     339\begin{cfa}
     340#include <Exception.hfa>
     341@ExceptionDecl@( E,             // must be global scope
     342        ... // exception fields
     343);
     344try {
     345        ...
     346        if ( ... ) @throwResume@ @ExceptionInst@( E, /* intialization */ );
     347        if ( ... ) @throw@ @ExceptionInst@( E, /* intialization */ );
     348        ...
     349} @catchResume@( E @*@ /* pointer */ ) { ... }
     350  catch( E * ) { ... }
     351  catch( exception_t @*@ /* catch any */ ) { ... }
     352  finally { ... }
     353\end{cfa}
     354\end{tabular}
     355\end{cquote}
     356
     357
     358\section{Non-local Exception}
     359
     360\begin{cquote}
     361\begin{tabular}{@{}l|ll@{}}
     362\begin{uC++}
     363
     364
     365void main() {
     366        try {
     367                _Enable {
     368                        ... suspend(); ...
     369                }
     370        } @_CatchResume@( E & /* reference */ ) { ... }
     371          catch( E & ) { ... }
     372}
     373\end{uC++}
     374&
     375\begin{cfa}
     376#define resumePoll( coroutine ) resume( coroutine ); poll()
     377#define suspendPoll suspend; poll()
     378void main() {
     379        try {
     380                enable_ehm();
     381                ... suspendPoll ...
     382                disable_ehm();
     383        } @catchResume@( E * ) { ... }
     384          catch( E & ) { ... }
     385}
     386\end{cfa}
     387\end{tabular}
     388\end{cquote}
    573389
    574390
    575391\section{Constructor / Destructor}
    576392
    577 A constructor/destructor must have its structure type as the first parameter and be a reference.
    578393\begin{cquote}
    579394\begin{tabular}{@{}l|l@{}}
     
    604419struct S { int i, j; };
    605420
    606 void @?{}@( @S & s@ ) { s.i = s.j = 3; } $\C[3in]{// default}$
    607 void @?{}@( @S & s@, int i, int j ) { s.i = i; s.j = j; } $\C{// initializer}$
    608 void @?{}@( @S & s@, const S rhs ) { ?{}( s, rhs.i, rhs.j ); } $\C{// copy}$
    609 void @^?{}@( @S & s@ ) { s.i = 0; s.j = 0; } $\C{// destructor}\CRT$
     421void @?{}@( S & s ) { s.i = s.j = 3; } $\C[3in]{// default}$
     422void @?{}@( S & s, int i, int j ) { s.i = i; s.j = j; } $\C{// initializer}$
     423void @?{}@( S & s, const S rhs ) { s.[i,j] = rhs.[i,j]; } $\C{// copy}$
     424void @^?{}@( S & s ) { s.i = 0; s.j = 0; } $\C{// destructor}\CRT$
    610425
    611426S s0;
    612427S s1 = { 1, 2 };
    613 // bug, cannot use 0/1 (zero_t/one_t) with "new"
    614 S * s2 = new( 0@n@, 2 ); // suffix n => (natural int)
     428// cannot use 0/1 (zero_t/one_t) with "new"
     429S * s2 = new( 1@n@, 2 ); // n => (int)
    615430delete( s2 );
    616 s2 = new( 1@n@, 2 );
     431s2 = new( 1n, 2 );
    617432delete( s2 );
    618 S & s3 = *new( 2, 2 );
     433S & s3 = *new( 1n, 2 );
    619434delete( &s3 );
    620 &s3 = &*new( 3, 2 );
     435&s3 = &*new( 1n, 2 );
    621436delete( &s3 );
    622437\end{cfa}
     
    625440
    626441
     442\section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}}
     443
     444\begin{cquote}
     445\begin{tabular}{@{}l|l@{}}
     446\begin{uC++}
     447struct S {
     448        int i = 0;  // cheat, implicit default constructor
     449        int setter( int j ) { int t = i; i = j; return t; }
     450        int getter() { return i; }
     451};
     452S s;
     453@s.@setter( 3 );  // object calls
     454int k = @s.@getter();
     455\end{uC++}
     456&
     457\begin{cfa}
     458struct S {  int i;  };
     459void ?{}( S & s ) { s.i = 0; } // explicit default constructor
     460int setter( @S & s,@ int j ) @with( s )@ { int t = i; i = j; return t; }
     461int getter( @S & s@ ) @with( s )@ { return i; }
     462
     463S s;
     464setter( @s,@ 3 );  // normal calls
     465int k = getter( @s@ );
     466\end{cfa}
     467\end{tabular}
     468\end{cquote}
     469
     470
     471\section{String}
     472
     473\begin{cquote}
     474\begin{tabular}{@{}l|l@{}}
     475\multicolumn{2}{@{}l@{}}{\lstinline{string s1, s2;}} \\
     476\begin{uC++}
     477s1 = "abcdefg";
     478s2 = s1;
     479s1 += s2;
     480s1 == s2; s1 != s2;
     481s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
     482s1.length();
     483s1[3];
     484s1.substr( 2 ); s1.substr( 2, 3 );
     485s1.replace( 2, 5, s2 );
     486s1.find( s2 ); s1.rfind( s2 );
     487s1.find_first_of( s2 ); s1.find_last_of( s2 );
     488s1.find_first_not_of( s2 ); s1.find_last_not_of( s2 );
     489getline( cin, s1 );
     490cout << s1 << endl;
     491\end{uC++}
     492&
     493\begin{cfa}
     494s1 = "abcdefg";
     495s2 = s1;
     496s1 += s2;
     497s1 == s2; s1 != s2;
     498s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
     499size( s1 );
     500s1[3];
     501s1( 2 ); s1( 2, 3 );
     502// replace( s1, 2, 5, s2 );
     503// find( s1, s2 ), rfind( s1, s2 );
     504// find_first_of( s2 ); find_last_of( s2 );
     505// find_first_not_of( s1, s2 ); find_last_not_of( s1, s2 );
     506sin | getline( s1 );
     507sout | s1;
     508\end{cfa}
     509\end{tabular}
     510\end{cquote}
     511
     512
     513\section{\texorpdfstring{\lstinline{uArray}}{uArray}}
     514
     515\begin{cquote}
     516\begin{tabular}{@{}l|l@{}}
     517\begin{uC++}
     518#include <iostream>
     519using namespace std;
     520struct S {
     521        int i;
     522        S( int i ) { S::i = i; cout << "ctor " << S::i << endl; }
     523        ~S() { S::i = i; cout << "dtor " << S::i << endl; }
     524};
     525int main() {
     526        enum { N = 5 };
     527        @uArray( S, s, N );@   // no constructor calls
     528        for ( int i = 0; i < N; i += 1 ) @s[i]( i )@; // constructor calls
     529        for ( int i = 0; i < N; i += 1 ) cout << s[i]@->@i << endl;
     530}
     531\end{uC++}
     532&
     533\begin{cfa}
     534#include <fstream.hfa>
     535#include <array.hfa>
     536struct S {
     537        int i;
     538};
     539void ?{}( S & s, int i ) { s.i = i; sout | "ctor" | s.i; }
     540void ^?{}( S & s ) { sout | "dtor" | s.i; }
     541int main() {
     542        enum { N = 5 };
     543        @array( S, N ) s = { delay_init };@ // no constructor calls
     544        for ( i; N ) @s[i]{ i }@; // constructor calls
     545        for ( i; N ) sout | s[i]@.@i;
     546}
     547\end{cfa}
     548\end{tabular}
     549\end{cquote}
     550
     551
    627552\section{Coroutine}
    628553
     
    630555\begin{tabular}{@{}l|ll@{}}
    631556\begin{uC++}
     557
    632558@_Coroutine@ C {
    633559        // private coroutine fields
     
    722648                val( val ) {}
    723649};
    724 \end{uC++}
    725 &
    726 \begin{cfa}
    727 #include <fstream.hfa>
    728 #include <mutex_stmt.hfa>
    729 #include <actor.hfa>
    730 
    731 struct StrMsg {
    732         @inline message;@ // derived message
    733         const char * val; // string message
    734 };
    735 void ?{}( StrMsg & msg, const char * str ) {
    736         @set_allocation( msg, Delete );@ // delete after use
    737         msg.val = str;
    738 }
    739 \end{cfa}
    740 \end{tabular}
    741 \begin{tabular}{@{}l|ll@{}}
    742 \begin{uC++}
    743650_Actor Hello { ${\color{red}\LstCommentStyle{// : public uActor}}$
    744651        Allocation receive( Message & msg ) {
     
    764671&
    765672\begin{cfa}
     673#include <fstream.hfa>
     674#include <mutex_stmt.hfa>
     675#include <actor.hfa>
     676
     677struct StrMsg {
     678        @inline message;@ // derived message
     679        const char * val; // string message
     680};
     681void ?{}( StrMsg & msg, const char * str ) {
     682        @set_allocation( msg, Delete );@ // delete after use
     683        msg.val = str;
     684}
    766685struct Hello { @inline actor;@ }; // derived actor
    767686allocation receive( Hello & receiver, @start_msg_t@ & ) {
     
    841760#include <locks.hfa>
    842761owner_lock m;
    843 cond_lock( owner_lock ) s;  // generic type on mutex lock
     762condition_variable( owner_lock ) s;  // generic type on mutex lock
    844763lock( m );
    845764if ( ! empty( s ) ) wait( s, m );
     
    880799enum { N = 3 };
    881800Barrier b{ N };
     801
     802_Task T {
     803        void main() {
     804                for ( int i = 0; i < 10; i += 1 ) {
     805                        b.block( 1 );
     806                }
     807        }
     808};
     809int main() {
     810        uProcessor p[N - 1];
     811        T t[N];
     812}
    882813\end{uC++}
    883814&
     
    905836enum { N = 3 };
    906837Barrier b{ N };
    907 \end{cfa}
    908 \end{tabular}
    909 \end{cquote}
    910 
    911 
    912 \enlargethispage{1000pt}
     838
     839thread T {};
     840void main( T & ) {
     841        for ( 10 ) {
     842                block( b, 1 );
     843        }
     844}
     845
     846int main() {
     847        processor p[N - 1];
     848        T t[N];
     849}
     850\end{cfa}
     851\end{tabular}
     852\end{cquote}
     853
     854\newpage
    913855
    914856\section{Monitor}
     
    979921\end{cquote}
    980922
    981 \newpage
     923\enlargethispage{1000pt}
     924
    982925\noindent
    983926External Scheduling
Note: See TracChangeset for help on using the changeset viewer.