- Timestamp:
- Aug 30, 2025, 9:13:52 AM (4 weeks ago)
- Branches:
- master
- Children:
- 7a8de40
- Parents:
- 304e436
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/uC++toCFA/uC++toCFA.tex
r304e436 r567a75f 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 : Thu Aug 28 13:41:34 2025 14 %% Update Count : 6493 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} … … 141 141 \CFA uses parametric polymorphism and allows overloading of variables and routines: 142 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} 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} 152 162 \CFA has rebindable references. 153 163 \begin{cquote} … … 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} … … 194 204 \end{cfa} 195 205 \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@ ) { ... } 206 In subsequent code examples, the left example is \CC/\uC and the right example is \CFA. 207 208 209 \section{Control Flow} 210 211 The @choose@ statement provides an implicit @break@ after the @case@ clause for safety. 212 It is possible to @break default@ in a @case@ clause to transfer to common code in the @default@ clause. 213 \begin{cquote} 214 \begin{tabular}{@{}l|l@{}} 215 \begin{uC++} 216 switch ( i ) { 217 case 1: ... @break@; // explicit break 218 case 2: ... @break@; // explicit break 219 default: ... ; 220 } 221 \end{uC++} 222 & 223 \begin{cfa} 224 choose ( i ) { 225 case 1: ... ; // implicit break 226 case 2: ... ; // implicit break 227 default: ... ; 228 } 229 \end{cfa} 230 \end{tabular} 231 \end{cquote} 232 To simplify creating an infinite loop, the loop condition in optional. 233 \begin{cquote} 234 \begin{tabular}{@{}l|l@{}} 235 \begin{uC++} 236 while ( true ) ... 237 for ( ;; ) ... 238 do ... while ( true ) 239 \end{uC++} 240 & 241 \begin{uC++} 242 while ($\,$) ... 243 for ($\,$) ... 244 do ... while ($\,$) 245 \end{uC++} 246 \end{tabular} 247 \end{cquote} 248 To simplify loop iteration a range is provided, specified from low to high, and a traversal direction, ascending (@+@) or descending (@-@). 249 The following is the syntax for the loop range, where @[@\,@]@ means optional. 250 \begin{cfa}[deletekeywords=default] 251 [ @index ;@ ] [ [ @min@ (default 0) ] [ direction @+@/@-@ (default +) ] @~@ [ @=@ (include endpoint) ] ] @max@ [ @~ increment@ ] 252 \end{cfa} 253 For @=@, the range includes the endpoint (@max@/@min@) depending on the direction (@+@/@-@). 254 \begin{cquote} 255 \begin{tabular}{@{}l|l@{}} 256 \begin{uC++} 205 257 for ( int i = 0; i < @10@; i += 1 ) { ... } 206 258 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 259 for ( int i = -2; i <@=@ 10; i += 3 ) { ... } 260 for ( int i = 10; i > -3; i @-@= 1 ) { ... } 261 for ( int i = 10; i >@=@ 0; i @-@= 1 ) { ... } 262 \end{uC++} 263 & 264 \begin{cfa} 265 for ( @10@ ) { ... } / for ( i; @10@ ) { ... } // 0 to 9 by 1 266 for ( i; @5@ ~ @15@ ~ @2@ ) { ... } // 5 to 14 by 2 267 for ( i; -2 ~@=@ 10 ~ 3 ) { ... } // -2 to 10 by 3 268 for ( i; -3 @-@~ 10 ) { ... } // not 10 -~= -3, 10 to -2 by -1 269 for ( i; 0 @-@~@=@ 10 ) { ... } // not 10 -~= 0, 10 to 0 by -1 270 \end{cfa} 271 \end{tabular} 272 \end{cquote} 273 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. 274 The loop index is available in the @else@ clause. 221 275 \begin{cquote} 222 276 \begin{tabular}{@{}l|l@{}} … … 230 284 231 285 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++}286 @else@ { ... } // i == 10 because of post increment 287 \end{cfa} 288 \end{tabular} 289 \end{cquote} 290 Single/multiple-level loop exit/continue is provided by the labelled @break@/@continue@. (First example is \CC.) 291 \begin{cquote} 292 \begin{tabular}{@{}l|l|l@{}} 293 \begin{C++} 240 294 @L1:@ for ( ;; ) { 241 @L2:@for ( ;; ) {242 ... if ( ... ) @ breakL1@; ...243 ... if ( ... ) @ breakL2@; ...244 } 245 } 246 \end{ uC++}295 for ( ;; ) { 296 ... if ( ... ) @goto L1@; ... 297 ... if ( ... ) @goto L2@; ... 298 } @L2: ;@ 299 } 300 \end{C++} 247 301 & 248 302 \begin{cfa} 249 303 @L1:@ for () { 250 304 @L2:@ for () { 251 ... if ( ... ) @ breakL1@; ...305 ... if ( ... ) @continue L1@; ... 252 306 ... if ( ... ) @break L2@; ... 253 307 } 254 308 } 255 309 \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. 310 & 311 \begin{cfa} 312 @L1:@ for () { 313 @L2:@ for () { 314 ... if ( ... ) @continue L1@; ... 315 ... if ( ... ) @break L2@; ... 316 } 317 } 313 318 \end{cfa} 314 319 \end{tabular} … … 320 325 Currently, \CFA uses macros @ExceptionDecl@ and @ExceptionInst@ to declare and instantiate an exception. 321 326 \begin{cquote} 327 \setlength{\tabcolsep}{5pt} 322 328 \begin{tabular}{@{}l|ll@{}} 323 329 \begin{uC++} … … 327 333 }; 328 334 try { 329 ... 330 if ( ... ) @_Resume@ E( /* initialization */ ); 331 if ( ... ) @_Throw@ E( /* initialization */ ); 332 ... 335 ... if ( ... ) @_Resume@ E( /* initialization */ ); ... 336 ... if ( ... ) @_Throw@ E( /* initialization */ ); ... 333 337 } @_CatchResume@( E & /* reference */ ) { ... } 334 338 catch( E & ) { ... } … … 343 347 ); 344 348 try { 345 ... 346 if ( ... ) @throwResume@ @ExceptionInst@( E, /* intialization */ ); 347 if ( ... ) @throw@ @ExceptionInst@( E, /* intialization */ ); 348 ... 349 ... if ( ... ) @throwResume@ @ExceptionInst@( E, /* intialization */ ); ... 350 ... if ( ... ) @throw@ @ExceptionInst@( E, /* intialization */ ); ... 349 351 } @catchResume@( E @*@ /* pointer */ ) { ... } 350 352 catch( E * ) { ... } … … 389 391 390 392 393 \section{Stream I/O} 394 395 \CFA output streams automatically separate values and insert a newline at the end of the print. 396 \begin{cquote} 397 \begin{tabular}{@{}l|l@{}} 398 \begin{uC++} 399 #include <@iostream@> 400 using namespace std; 401 int i; double d; char c; 402 cin >> i >> d >> c; 403 cout << i << ' ' << d << ' ' << c << endl; 404 \end{uC++} 405 & 406 \begin{cfa} 407 #include <@fstream.hfa@> 408 409 int i; double d; char c; 410 sin | i | d | c; 411 sout | i | d | c 412 \end{cfa} 413 \end{tabular} 414 \end{cquote} 415 To disable/enable automatic newline at the end of printing, use @nlOff@/@nlOn@ and @nl@. 416 \begin{cquote} 417 \begin{tabular}{@{}l|l@{}} 418 \begin{uC++} 419 420 for ( int i = 0; i < 5; i += 1 ) cout << i << ' '; 421 cout << @endl@; 422 0 1 2 3 4 423 \end{uC++} 424 & 425 \begin{cfa} 426 sout | @nlOff@; // disable auto nl 427 for ( i; 5 ) sout | i; 428 sout | @nl@; sout | @nlOn@; // enable auto nl 429 0 1 2 3 4 430 \end{cfa} 431 \end{tabular} 432 \end{cquote} 433 Floating-point numbers without a fraction print with a decimal point, which can be disabled with @nodp@. 434 \begin{cquote} 435 \begin{tabular}{@{}l|l@{}} 436 \begin{uC++} 437 cout << 3.0 << ' ' << showpoint << setprecision(0) << 3.0 << endl; 438 3 3. 439 \end{uC++} 440 & 441 \begin{cfa} 442 sout | @nodp( 3.0 )@ | 3.0; 443 3 3. 444 \end{cfa} 445 \end{tabular} 446 \end{cquote} 447 448 449 \section{String} 450 451 \begin{cquote} 452 \begin{tabular}{@{}l|l@{}} 453 \multicolumn{2}{@{}l@{}}{\lstinline{string s1, s2;}} \\ 454 \begin{uC++} 455 s1 = "abcdefg"; 456 s2 = s1; 457 s1 += s2; 458 s1 == s2; s1 != s2; 459 s1 < s2; s1 <= s2; s1 > s2; s1 >= s2; 460 s1.length(); 461 s1[3]; 462 s1.substr( 2 ); s1.substr( 2, 3 ); 463 s1.replace( 2, 5, s2 ); 464 s1.find( s2 ); 465 s1.find_first_of( "cd" ); 466 s1.find_first_not_of( "cd" ); 467 getline( cin, s1 ); 468 cout << s1 << endl; 469 \end{uC++} 470 & 471 \begin{cfa} 472 s1 = "abcdefg"; 473 s2 = s1; 474 s1 += s2; 475 s1 == s2; s1 != s2; 476 s1 < s2; s1 <= s2; s1 > s2; s1 >= s2; 477 len( s1 ); 478 s1[3]; 479 s1( 2 ); s1( 2, 3 ); 480 s1( 2, 5 ) = s2; 481 find( s1, s2 ); 482 exclude( s1, "cd" ); // longest sequence excluding "c" and "d" 483 include( s1, "cd" ); // longest sequence including "c" and "d" 484 sin | getline( s1 ); 485 sout | s1; 486 \end{cfa} 487 \end{tabular} 488 \end{cquote} 489 490 491 \section{\texorpdfstring{\lstinline{uArray}}{uArray}} 492 493 \begin{cquote} 494 \begin{tabular}{@{}l|l@{}} 495 \begin{uC++} 496 #include <iostream> 497 using namespace std; 498 499 struct S { 500 int i; 501 S( int i ) { S::i = i; } 502 }; 503 void f( uArrayRef( S, parm ) ); 504 int main() { 505 enum { N = 5 }; 506 @uArray( S, s, N );@ // stack, no ctor calls 507 for ( int i = 0; i < N; i += 1 ) @s[i]( i )@; // ctor calls 508 for ( int i = 0; i < N; i += 1 ) cout << s[i]@->@i << endl; 509 f( s ); 510 @uArrayPtr( S, sp, N );@ // heap, no ctor calls 511 for ( int i = 0; i < N; i += 1 ) @sp[i]( i )@; // ctor calls 512 for ( int i = 0; i < N; i += 1 ) cout << sp[i]@->@i << endl; 513 f( sp ); 514 } // delete s, sp 515 \end{uC++} 516 & 517 \begin{cfa} 518 #include <fstream.hfa> 519 #include <array.hfa> 520 #include <memory.hfa> 521 struct S { 522 int i; 523 }; 524 void ?{}( S & s, int i ) { s.i = i; } 525 forall( [N] ) void f( array( S, N ) & parm ) {} 526 int main() { 527 enum { N = 5 }; 528 @array( S, N ) s = { delay_init };@ // no ctor calls 529 for ( i; N ) @s[i]{ i }@; // ctor calls 530 for ( i; N ) sout | s[i]@.@i; 531 f( s ); 532 @unique_ptr( array( S, N ) )@ sp = { delay_init }; // heap 533 for ( int i = 0; i < N; i += 1 ) @(*sp)@[i]{ i }; // ctor calls 534 for ( int i = 0; i < N; i += 1 ) sout | @(*sp)@[i].i; 535 f( @*sp@ ); 536 } // delete s, sp 537 \end{cfa} 538 \end{tabular} 539 \end{cquote} 540 541 542 \section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}} 543 544 \begin{cquote} 545 \begin{tabular}{@{}l|l@{}} 546 \begin{uC++} 547 struct S { 548 int i = 0; // cheat, implicit default constructor 549 int setter( int j ) { int t = i; i = j; return t; } 550 int getter() { return i; } 551 }; 552 S s; 553 @s.@setter( 3 ); // object calls 554 int k = @s.@getter(); 555 \end{uC++} 556 & 557 \begin{cfa} 558 struct S { int i; }; 559 void ?{}( S & s ) { s.i = 0; } // explicit default constructor 560 int setter( @S & s,@ int j ) @with( s )@ { int t = i; i = j; return t; } 561 int getter( @S & s@ ) @with( s )@ { return i; } 562 563 S s; 564 setter( @s,@ 3 ); // normal calls 565 int k = getter( @s@ ); 566 \end{cfa} 567 \end{tabular} 568 \end{cquote} 569 570 391 571 \section{Constructor / Destructor} 392 572 … … 421 601 void @?{}@( S & s ) { s.i = s.j = 3; } $\C[3in]{// default}$ 422 602 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}$603 void @?{}@( S & s, const S rhs ) { ?{}( s, rhs.i, rhs.j ); } $\C{// copy}$ 424 604 void @^?{}@( S & s ) { s.i = 0; s.j = 0; } $\C{// destructor}\CRT$ 425 605 … … 440 620 441 621 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 622 \section{Coroutine} 553 623 … … 760 830 #include <locks.hfa> 761 831 owner_lock m; 762 cond ition_variable( owner_lock ) s; // generic type on mutex lock832 cond_lock( owner_lock ) s; // generic type on mutex lock 763 833 lock( m ); 764 834 if ( ! empty( s ) ) wait( s, m ); … … 799 869 enum { N = 3 }; 800 870 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 871 \end{uC++} 814 872 & … … 836 894 enum { N = 3 }; 837 895 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 896 \end{cfa} 851 897 \end{tabular}
Note:
See TracChangeset
for help on using the changeset viewer.