Changes in doc/uC++toCFA/uC++toCFA.tex [8617ee90:04375cf]
- File:
-
- 1 edited
-
doc/uC++toCFA/uC++toCFA.tex (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/uC++toCFA/uC++toCFA.tex
r8617ee90 r04375cf 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Sat Mar 15 13:38:53202514 %% Update Count : 6 30213 %% Last Modified On : Mon Sep 8 18:10:30 2025 14 %% Update Count : 6534 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 17 % requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended18 17 19 18 \documentclass[11pt]{article} … … 83 82 \setlength{\topmargin}{-0.45in} % move running title into header 84 83 \setlength{\headsep}{0.25in} 84 \setlength{\tabcolsep}{15pt} 85 85 86 86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% … … 134 134 135 135 \maketitle 136 \vspace*{-0. 55in}136 \vspace*{-0.65in} 137 137 138 138 \section{Introduction} 139 139 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} 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). 153 163 \begin{cquote} 154 164 \begin{tabular}{@{}l|l@{}} … … 165 175 & 166 176 \begin{cfa} 167 r2i = 3; $\C[1.0in]{// change x}$177 r2i = 3; $\C[0.875in]{// change x}$ 168 178 &r2i = &r1y; $\C{// change p2i / r2i}$ 169 r2i = 3; $\C{// change y}$179 r2i = 3; $\C{// change y}$ 170 180 &r1x = &r1y; $\C{// change p1x / r1x}$ 171 r2i = 4; $\C{// change y}$181 r2i = 4; $\C{// change y}$ 172 182 &r1x = @0p@; $\C{// reset}\CRT$ 173 183 \end{cfa} … … 179 189 int & @const@ & @const@ crcr = cr; // generalize 180 190 \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} 181 557 Aggregate qualification is reduced or eliminated by opening scopes using the @with@ clause. 182 558 \begin{cfa} … … 194 570 \end{cfa} 195 571 \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} 572 In subsequent code examples, the left example is \CC/\uC and the right example is \CFA. 389 573 390 574 391 575 \section{Constructor / Destructor} 392 576 577 A constructor/destructor must have its structure type as the first parameter and be a reference. 393 578 \begin{cquote} 394 579 \begin{tabular}{@{}l|l@{}} … … 419 604 struct S { int i, j; }; 420 605 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$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$ 425 610 426 611 S s0; 427 612 S 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" 614 S * s2 = new( 0@n@, 2 ); // suffix n => (natural int) 430 615 delete( s2 ); 431 s2 = new( 1 n, 2 );616 s2 = new( 1@n@, 2 ); 432 617 delete( s2 ); 433 S & s3 = *new( 1n, 2 );618 S & s3 = *new( 2, 2 ); 434 619 delete( &s3 ); 435 &s3 = &*new( 1n, 2 );620 &s3 = &*new( 3, 2 ); 436 621 delete( &s3 ); 437 622 \end{cfa} … … 440 625 441 626 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 constructor449 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 calls454 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 constructor460 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 calls465 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 calls528 for ( int i = 0; i < N; i += 1 ) @s[i]( i )@; // constructor calls529 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 calls544 for ( i; N ) @s[i]{ i }@; // constructor calls545 for ( i; N ) sout | s[i]@.@i;546 }547 \end{cfa}548 \end{tabular}549 \end{cquote}550 551 552 627 \section{Coroutine} 553 628 … … 555 630 \begin{tabular}{@{}l|ll@{}} 556 631 \begin{uC++} 557 558 632 @_Coroutine@ C { 559 633 // private coroutine fields … … 648 722 val( val ) {} 649 723 }; 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++} 650 743 _Actor Hello { ${\color{red}\LstCommentStyle{// : public uActor}}$ 651 744 Allocation receive( Message & msg ) { … … 671 764 & 672 765 \begin{cfa} 673 #include <fstream.hfa>674 #include <mutex_stmt.hfa>675 #include <actor.hfa>676 677 struct StrMsg {678 @inline message;@ // derived message679 const char * val; // string message680 };681 void ?{}( StrMsg & msg, const char * str ) {682 @set_allocation( msg, Delete );@ // delete after use683 msg.val = str;684 }685 766 struct Hello { @inline actor;@ }; // derived actor 686 767 allocation receive( Hello & receiver, @start_msg_t@ & ) { … … 760 841 #include <locks.hfa> 761 842 owner_lock m; 762 cond ition_variable( owner_lock ) s; // generic type on mutex lock843 cond_lock( owner_lock ) s; // generic type on mutex lock 763 844 lock( m ); 764 845 if ( ! empty( s ) ) wait( s, m ); … … 799 880 enum { N = 3 }; 800 881 Barrier 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 }813 882 \end{uC++} 814 883 & … … 836 905 enum { N = 3 }; 837 906 Barrier 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} 855 913 856 914 \section{Monitor} … … 921 979 \end{cquote} 922 980 923 \enlargethispage{1000pt} 924 981 \newpage 925 982 \noindent 926 983 External Scheduling
Note:
See TracChangeset
for help on using the changeset viewer.