Ignore:
File:
1 edited

Legend:

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

    r8617ee90 r04375cf  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Sat Mar 15 13:38:53 2025
    14 %% Update Count     : 6302
     13%% Last Modified On : Mon Sep  8 18:10:30 2025
     14%% Update Count     : 6534
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
    17 % requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended
    1817
    1918\documentclass[11pt]{article}
     
    8382\setlength{\topmargin}{-0.45in}                                                 % move running title into header
    8483\setlength{\headsep}{0.25in}
     84\setlength{\tabcolsep}{15pt}
    8585
    8686%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    134134
    135135\maketitle
    136 \vspace*{-0.55in}
     136\vspace*{-0.65in}
    137137
    138138\section{Introduction}
    139139
    140 \CFA is NOT an object-oriented programming-language.
    141 \CFA uses parametric polymorphism and allows overloading of variables and routines:
    142 \begin{cfa}
    143 int i;  char i;  double i;      $\C[2in]{// overload name i}$
    144 int i();  double i();  char i();
    145 i += 1;                                         $\C{// int i}$
    146 i += 1.0;                                       $\C{// double i}$
    147 i += 'a';                                       $\C{// char i}$
    148 int j = i();                            $\C{// int i()}$
    149 double j = i();                         $\C{// double i();}$
    150 char j = i();                           $\C{// char i()}\CRT$
    151 \end{cfa}
    152 \CFA has rebindable references.
     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}
     143int x;  char x;  double x;    // overload name x
     144int x();  double x();  char x();
     145\end{cfa}
     146\vspace*{-8pt}
     147\begin{cquote}
     148\begin{tabular}{@{}l@{\hspace{1in}}|l@{}}
     149\begin{cfa}
     150x += 42;        $\C[1in]{// int x}$
     151x += 42.2;      $\C{// double x}$
     152x += 'a';       $\C{// char x}\CRT$
     153\end{cfa}
     154&
     155\begin{cfa}
     156int j = x();    $\C[1in]{// int x()}$
     157double j = x(); $\C{// double x();}$
     158char 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).
    153163\begin{cquote}
    154164\begin{tabular}{@{}l|l@{}}
     
    165175&
    166176\begin{cfa}
    167 r2i = 3; $\C[1.0in]{// change x}$
     177r2i = 3;         $\C[0.875in]{// change x}$
    168178&r2i = &r1y; $\C{// change p2i / r2i}$
    169 r2i = 3; $\C{// change y}$
     179r2i = 3;        $\C{// change y}$
    170180&r1x = &r1y; $\C{// change p1x / r1x}$
    171 r2i = 4; $\C{// change y}$
     181r2i = 4;        $\C{// change y}$
    172182&r1x = @0p@; $\C{// reset}\CRT$
    173183\end{cfa}
     
    179189int & @const@ & @const@ crcr = cr; // generalize
    180190\end{cfa}
     191
     192
     193\section{Control Flow}
     194
     195The @choose@ statement provides an implicit @break@ after the @case@ clause for safety.
     196It 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++}
     200switch ( i ) {
     201  case 1: ... @break@; // explicit break
     202  case 2: ... @break@; // explicit break
     203  default: ... ;
     204}
     205\end{uC++}
     206&
     207\begin{cfa}
     208choose ( i ) {
     209  case 1: ... ; // implicit break
     210  case 2: ... ; // implicit break
     211  default: ... ;
     212}
     213\end{cfa}
     214\end{tabular}
     215\end{cquote}
     216To simplify creating an infinite loop, the loop condition in optional.
     217\begin{cquote}
     218\begin{tabular}{@{}l|l@{}}
     219\begin{uC++}
     220while ( true ) ...
     221for ( ;; ) ...
     222do ... while ( true )
     223\end{uC++}
     224&
     225\begin{uC++}
     226while ($\,$) ...
     227for ($\,$) ...
     228do ... while ($\,$)
     229\end{uC++}
     230\end{tabular}
     231\end{cquote}
     232To simplify loop iteration a range is provided, from low to high, and a traversal direction, ascending (@+@) or descending (@-@).
     233The 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}
     237For @=@, the range includes the endpoint (@max@/@min@) depending on the direction (@+@/@-@).
     238\begin{cquote}
     239\begin{tabular}{@{}l|l@{}}
     240\begin{uC++}
     241for ( int i = 0; i < @10@; i += 1 ) { ... }
     242for ( int i = @5@; i < @15@; i += @2@ ) { ... }
     243for ( int i = -2; i <@=@ 10; i += 3 ) { ... }
     244for ( int i = 10; i > -3; i @-@= 1 ) { ... }
     245for ( int i = 10; i >@=@ 0; i @-@= 1 ) { ... }
     246\end{uC++}
     247&
     248\begin{cfa}
     249for ( @10@ ) { ... }  /  for ( i; @10@ ) { ... }  // 0 to 9 by 1
     250for ( i; @5@ ~ @15@ ~ @2@ ) { ... } // 5 to 14 by 2
     251for ( i; -2 ~@=@ 10 ~ 3 ) { ... } // -2 to 10 by 3
     252for ( i; -3 @-@~ 10 ) { ... } // not 10 -~= -3, 10 to -2 by -1
     253for ( i; 0 @-@~@=@ 10 ) { ... } // not 10 -~= 0, 10 to 0 by -1
     254\end{cfa}
     255\end{tabular}
     256\end{cquote}
     257A 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.
     258The loop index is available in the @else@ clause.
     259\begin{cquote}
     260\begin{tabular}{@{}l|l@{}}
     261\begin{uC++}
     262int i = 0
     263for ( i = 0; i < 10; i += 1 ) { ... }
     264@if ( i == 10 )@ { ... }
     265\end{uC++}
     266&
     267\begin{cfa}
     268
     269for ( i; 10 ) { ... }
     270@else@ { ... } // i == 10 because of post increment
     271\end{cfa}
     272\end{tabular}
     273\end{cquote}
     274Single/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
     309Currently, \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};
     318try {
     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);
     332try {
     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
     351void 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()
     364void 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@>
     384using namespace std;
     385int i;   double d;   char c;
     386cin >> i >> d >> c;
     387cout << i << ' ' << d << ' ' << c << endl;
     388\end{uC++}
     389&
     390\begin{cfa}
     391#include <@fstream.hfa@>
     392
     393int i;   double d;   char c;
     394sin | i | d | c;
     395sout | i | d | c
     396\end{cfa}
     397\end{tabular}
     398\end{cquote}
     399To 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
     404for ( int i = 0; i < 5; i += 1 ) cout << i << ' ';
     405cout << @endl@;
     4060 1 2 3 4
     407\end{uC++}
     408&
     409\begin{cfa}
     410sout | @nlOff@; // disable auto nl
     411for ( i; 5 ) sout | i;
     412sout | @nl@; sout | @nlOn@;  // enable auto nl
     4130 1 2 3 4
     414\end{cfa}
     415\end{tabular}
     416\end{cquote}
     417Floating-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++}
     421cout << 3.0 << ' ' << showpoint << setprecision(0) << 3.0 << endl;
     4223 3.
     423\end{uC++}
     424&
     425\begin{cfa}
     426sout | @nodp( 3.0 )@ | 3.0;
     4273 3.
     428\end{cfa}
     429\end{tabular}
     430\end{cquote}
     431
     432
     433\section{String}
     434
     435The @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++}
     440s1 = "abcdefg";
     441s2 = s1;
     442s1 += s2;
     443s1 == s2; s1 != s2;
     444s1 < s2;  s1 <= s2;  s1 > s2;  s1 >= s2;
     445s1.length();
     446s1[3];
     447s1.substr( 2 );  s1.substr( 2, 3 );
     448s1.replace( 2, 5, s2 );
     449s1.find( s2 );
     450s1.find_first_of( "cd" );
     451s1.find_first_not_of( "cd" );
     452getline( cin, s1 );
     453cout << s1 << endl;
     454\end{uC++}
     455&
     456\begin{cfa}
     457s1 = "abcdefg";
     458s2 = s1;
     459s1 += s2;
     460s1 == s2; s1 != s2;
     461s1 < s2;  s1 <= s2;  s1 > s2;  s1 >= s2;
     462len( s1 ); // like C strlen( s1 )
     463s1[3];
     464s1( 2 );  s1( 2, 3 );
     465s1( 2, 5 ) = s2;
     466find( s1, s2 );
     467exclude( s1, "cd" );  // longest sequence excluding "c" and "d"
     468include( s1, "cd" );  // longest sequence including "c" and "d"
     469sin | getline( s1 );
     470sout | 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>
     483using namespace std;
     484
     485struct S {
     486        int i;
     487        S( int i ) { S::i = i; }
     488};
     489void f( @uArrayRef( S, parm )@ );
     490int 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>
     507struct S {
     508        int i;
     509};
     510void ?{}( S & s, int i ) { s.i = i; }
     511@forall( [N] )@ void f( @array( S, N ) & parm@ ) {}
     512int 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.
     531The 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++}
     535struct 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};
     540S s;
     541@s.@setter( 3 );  // object calls
     542int k = @s.@getter();
     543\end{uC++}
     544&
     545\begin{cfa}
     546struct S {  int i;  };
     547void ?{}( S & s ) { s.i = 0; } // explicit default constructor
     548int setter( @S & s,@ int j ) @with( s )@ { int t = i; i = j; return t; }
     549int getter( @S & s@ ) @with( s )@ { return i; }
     550
     551S s;
     552setter( @s,@ 3 );  // normal calls
     553int k = getter( @s@ );
     554\end{cfa}
     555\end{tabular}
     556\end{cquote}
    181557Aggregate qualification is reduced or eliminated by opening scopes using the @with@ clause.
    182558\begin{cfa}
     
    194570\end{cfa}
    195571\noindent
    196 In 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++}
    204 for ( @;;@ ) { ... }  /  while ( @true@ ) { ... }
    205 for ( int i = 0; i < @10@; i += 1 ) { ... }
    206 for ( int i = @5@; i < @15@; i += @2@ ) { ... }
    207 for ( int i = -1; i <@=@ 10; i += 3 ) { ... }
    208 for ( int i = 10; i > 0; i @-@= 1 ) { ... }
    209 \end{uC++}
    210 &
    211 \begin{cfa}
    212 for () { ... }  /  while () { ... }
    213 for ( @10@ ) { ... }  /  for ( i; @10@ ) { ... }
    214 for ( i; @5@ ~ @15@ ~ @2@ ) { ... }
    215 for ( i; -1 ~@=@ 10 ~ 3 ) { ... }
    216 for ( i; 0 @-@~ 10 ) { ... }
    217 \end{cfa}
    218 \end{tabular}
    219 \end{cquote}
    220 
    221 \begin{cquote}
    222 \begin{tabular}{@{}l|l@{}}
    223 \begin{uC++}
    224 int i = 0
    225 for ( i = 0; i < 10; i += 1 ) { ... }
    226 @if ( i == 10 )@ { ... }
    227 \end{uC++}
    228 &
    229 \begin{cfa}
    230 
    231 for ( 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@>
    267 using namespace std;
    268 int i;   double d;   char c;
    269 cin >> i >> d >> c;
    270 cout << i << ' ' << d << ' ' << c << endl;
    271 \end{uC++}
    272 &
    273 \begin{cfa}
    274 #include <@fstream.hfa@>
    275 
    276 int i;   double d;   char c;
    277 sin | i | d | c;
    278 sout | i | d | c
    279 \end{cfa}
    280 \end{tabular}
    281 \end{cquote}
    282 To 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 
    287 for ( int i = 0; i < 5; i += 1 ) cout << i << ' ';
    288 cout << @endl@;
    289 
    290 0 1 2 3 4
    291 \end{uC++}
    292 &
    293 \begin{cfa}
    294 sout | @nlOff@; // disable auto nl
    295 for ( i; 5 ) sout | i;
    296 sout | @nl@;
    297 sout | @nlOn@;  // reenable auto nl
    298 0 1 2 3 4
    299 \end{cfa}
    300 \end{tabular}
    301 \end{cquote}
    302 Floating-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++}
    306 cout << 3.0 << ' ' << showpoint << setprecision(0) << 3.0 << endl;
    307 3 3.
    308 \end{uC++}
    309 &
    310 \begin{cfa}
    311 sout | @nodp( 3.0 )@ | 3.0;
    312 3 3.
    313 \end{cfa}
    314 \end{tabular}
    315 \end{cquote}
    316 
    317 
    318 \section{Exception}
    319 
    320 Currently, \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 };
    328 try {
    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 );
    344 try {
    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 
    365 void 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()
    378 void 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}
     572In subsequent code examples, the left example is \CC/\uC and the right example is \CFA.
    389573
    390574
    391575\section{Constructor / Destructor}
    392576
     577A constructor/destructor must have its structure type as the first parameter and be a reference.
    393578\begin{cquote}
    394579\begin{tabular}{@{}l|l@{}}
     
    419604struct S { int i, j; };
    420605
    421 void @?{}@( S & s ) { s.i = s.j = 3; } $\C[3in]{// default}$
    422 void @?{}@( S & s, int i, int j ) { s.i = i; s.j = j; } $\C{// initializer}$
    423 void @?{}@( S & s, const S rhs ) { s.[i,j] = rhs.[i,j]; } $\C{// copy}$
    424 void @^?{}@( S & s ) { s.i = 0; s.j = 0; } $\C{// destructor}\CRT$
     606void @?{}@( @S & s@ ) { s.i = s.j = 3; } $\C[3in]{// default}$
     607void @?{}@( @S & s@, int i, int j ) { s.i = i; s.j = j; } $\C{// initializer}$
     608void @?{}@( @S & s@, const S rhs ) { ?{}( s, rhs.i, rhs.j ); } $\C{// copy}$
     609void @^?{}@( @S & s@ ) { s.i = 0; s.j = 0; } $\C{// destructor}\CRT$
    425610
    426611S s0;
    427612S s1 = { 1, 2 };
    428 // cannot use 0/1 (zero_t/one_t) with "new"
    429 S * s2 = new( 1@n@, 2 ); // n => (int)
     613// bug, cannot use 0/1 (zero_t/one_t) with "new"
     614S * s2 = new( 0@n@, 2 ); // suffix n => (natural int)
    430615delete( s2 );
    431 s2 = new( 1n, 2 );
     616s2 = new( 1@n@, 2 );
    432617delete( s2 );
    433 S & s3 = *new( 1n, 2 );
     618S & s3 = *new( 2, 2 );
    434619delete( &s3 );
    435 &s3 = &*new( 1n, 2 );
     620&s3 = &*new( 3, 2 );
    436621delete( &s3 );
    437622\end{cfa}
     
    440625
    441626
    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++}
    447 struct 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 };
    452 S s;
    453 @s.@setter( 3 );  // object calls
    454 int k = @s.@getter();
    455 \end{uC++}
    456 &
    457 \begin{cfa}
    458 struct S {  int i;  };
    459 void ?{}( S & s ) { s.i = 0; } // explicit default constructor
    460 int setter( @S & s,@ int j ) @with( s )@ { int t = i; i = j; return t; }
    461 int getter( @S & s@ ) @with( s )@ { return i; }
    462 
    463 S s;
    464 setter( @s,@ 3 );  // normal calls
    465 int 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++}
    477 s1 = "abcdefg";
    478 s2 = s1;
    479 s1 += s2;
    480 s1 == s2; s1 != s2;
    481 s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
    482 s1.length();
    483 s1[3];
    484 s1.substr( 2 ); s1.substr( 2, 3 );
    485 s1.replace( 2, 5, s2 );
    486 s1.find( s2 ); s1.rfind( s2 );
    487 s1.find_first_of( s2 ); s1.find_last_of( s2 );
    488 s1.find_first_not_of( s2 ); s1.find_last_not_of( s2 );
    489 getline( cin, s1 );
    490 cout << s1 << endl;
    491 \end{uC++}
    492 &
    493 \begin{cfa}
    494 s1 = "abcdefg";
    495 s2 = s1;
    496 s1 += s2;
    497 s1 == s2; s1 != s2;
    498 s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
    499 size( s1 );
    500 s1[3];
    501 s1( 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 );
    506 sin | getline( s1 );
    507 sout | 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>
    519 using namespace std;
    520 struct 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 };
    525 int 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>
    536 struct S {
    537         int i;
    538 };
    539 void ?{}( S & s, int i ) { s.i = i; sout | "ctor" | s.i; }
    540 void ^?{}( S & s ) { sout | "dtor" | s.i; }
    541 int 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 
    552627\section{Coroutine}
    553628
     
    555630\begin{tabular}{@{}l|ll@{}}
    556631\begin{uC++}
    557 
    558632@_Coroutine@ C {
    559633        // private coroutine fields
     
    648722                val( val ) {}
    649723};
     724\end{uC++}
     725&
     726\begin{cfa}
     727#include <fstream.hfa>
     728#include <mutex_stmt.hfa>
     729#include <actor.hfa>
     730
     731struct StrMsg {
     732        @inline message;@ // derived message
     733        const char * val; // string message
     734};
     735void ?{}( 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++}
    650743_Actor Hello { ${\color{red}\LstCommentStyle{// : public uActor}}$
    651744        Allocation receive( Message & msg ) {
     
    671764&
    672765\begin{cfa}
    673 #include <fstream.hfa>
    674 #include <mutex_stmt.hfa>
    675 #include <actor.hfa>
    676 
    677 struct StrMsg {
    678         @inline message;@ // derived message
    679         const char * val; // string message
    680 };
    681 void ?{}( StrMsg & msg, const char * str ) {
    682         @set_allocation( msg, Delete );@ // delete after use
    683         msg.val = str;
    684 }
    685766struct Hello { @inline actor;@ }; // derived actor
    686767allocation receive( Hello & receiver, @start_msg_t@ & ) {
     
    760841#include <locks.hfa>
    761842owner_lock m;
    762 condition_variable( owner_lock ) s;  // generic type on mutex lock
     843cond_lock( owner_lock ) s;  // generic type on mutex lock
    763844lock( m );
    764845if ( ! empty( s ) ) wait( s, m );
     
    799880enum { N = 3 };
    800881Barrier 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 };
    809 int main() {
    810         uProcessor p[N - 1];
    811         T t[N];
    812 }
    813882\end{uC++}
    814883&
     
    836905enum { N = 3 };
    837906Barrier b{ N };
    838 
    839 thread T {};
    840 void main( T & ) {
    841         for ( 10 ) {
    842                 block( b, 1 );
    843         }
    844 }
    845 
    846 int main() {
    847         processor p[N - 1];
    848         T t[N];
    849 }
    850 \end{cfa}
    851 \end{tabular}
    852 \end{cquote}
    853 
    854 \newpage
     907\end{cfa}
     908\end{tabular}
     909\end{cquote}
     910
     911
     912\enlargethispage{1000pt}
    855913
    856914\section{Monitor}
     
    921979\end{cquote}
    922980
    923 \enlargethispage{1000pt}
    924 
     981\newpage
    925982\noindent
    926983External Scheduling
Note: See TracChangeset for help on using the changeset viewer.