Changeset 5407cdc for doc/user/user.tex
- Timestamp:
- Apr 28, 2021, 4:56:50 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 8d66610
- Parents:
- feacef9 (diff), b7fd2db6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
doc/user/user.tex (modified) (140 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
rfeacef9 r5407cdc 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Mon Feb 15 13:48:53 202114 %% Update Count : 4 45213 %% Last Modified On : Sun Apr 25 19:03:03 2021 14 %% Update Count : 4951 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 66 66 % math escape $...$ (dollar symbol) 67 67 \input{common} % common CFA document macros 68 \setlength{\gcolumnposn}{3in} 68 69 \CFAStyle % use default CFA format-style 69 70 \lstset{language=CFA} % CFA default lnaguage 70 71 \lstnewenvironment{C++}[1][] % use C++ style 71 {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{ @}{@},#1}}72 {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®},#1}} 72 73 {} 73 74 … … 81 82 \newcommand{\Textbf}[2][red]{{\color{#1}{\textbf{#2}}}} 82 83 \newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}} 83 \newcommand{\R}[1]{ \Textbf{#1}}84 \newcommand{\R C}[1]{\Textbf{\LstBasicStyle{#1}}}84 \newcommand{\R}[1]{{\color{red}#1}} 85 \newcommand{\RB}[1]{\Textbf{#1}} 85 86 \newcommand{\B}[1]{{\Textbf[blue]{#1}}} 86 87 \newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}} … … 176 177 int main( void ) { 177 178 int x = 0, y = 1, z = 2; 178 @printf( "%d %d %d\n", x, y, z );@179 ®printf( "%d %d %d\n", x, y, z );® 179 180 } 180 181 \end{cfa} … … 185 186 int main( void ) { 186 187 int x = 0, y = 1, z = 2; 187 @sout | x | y | z;@$\indexc{sout}$188 ®sout | x | y | z;®$\indexc{sout}$ 188 189 } 189 190 \end{cfa} … … 194 195 int main() { 195 196 int x = 0, y = 1, z = 2; 196 @cout<<x<<" "<<y<<" "<<z<<endl;@197 ®cout<<x<<" "<<y<<" "<<z<<endl;® 197 198 } 198 199 \end{cfa} … … 224 225 \begin{tabular}{@{}rcccccccc@{}} 225 226 & 2021 & 2016 & 2011 & 2006 & 2001 & 1996 & 1991 & 1986 \\ \hline 226 \R {C} & \R{1} & \R{2} & \R{2} & \R{1} & \R{1} & \R{1} & \R{1} & \R{1}\\227 \RB{C} & \RB{1}& \RB{2}& \RB{2}& \RB{1}& \RB{1}& \RB{1}& \RB{1}& \RB{1}\\ 227 228 Java & 2 & 1 & 1 & 2 & 3 & 28 & - & - \\ 228 229 Python & 3 & 5 & 6 & 7 & 23 & 13 & - & - \\ … … 258 259 The signature feature of \CFA is \emph{\Index{overload}able} \Index{parametric-polymorphic} functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a ©forall© clause (giving the language its name): 259 260 \begin{cfa} 260 @forall( otype T )@T identity( T val ) { return val; }261 ®forall( otype T )® T identity( T val ) { return val; } 261 262 int forty_two = identity( 42 ); $\C{// T is bound to int, forty\_two == 42}$ 262 263 \end{cfa} … … 322 323 Whereas, \CFA wraps each of these routines into one overloaded name ©abs©: 323 324 \begin{cfa} 324 char @abs@( char );325 extern "C" { int @abs@( int ); } $\C{// use default C routine for int}$326 long int @abs@( long int );327 long long int @abs@( long long int );328 float @abs@( float );329 double @abs@( double );330 long double @abs@( long double );331 float _Complex @abs@( float _Complex );332 double _Complex @abs@( double _Complex );333 long double _Complex @abs@( long double _Complex );325 char ®abs®( char ); 326 extern "C" { int ®abs®( int ); } $\C{// use default C routine for int}$ 327 long int ®abs®( long int ); 328 long long int ®abs®( long long int ); 329 float ®abs®( float ); 330 double ®abs®( double ); 331 long double ®abs®( long double ); 332 float _Complex ®abs®( float _Complex ); 333 double _Complex ®abs®( double _Complex ); 334 long double _Complex ®abs®( long double _Complex ); 334 335 \end{cfa} 335 336 The problem is \Index{name clash} between the C name ©abs© and the \CFA names ©abs©, resulting in two name linkages\index{C linkage}: ©extern "C"© and ©extern "Cforall"© (default). … … 358 359 The 2011 C standard plus GNU extensions. 359 360 \item 360 \Indexc[deletekeywords=inline]{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{\lstinline[deletekeywords=inline] $-fgnu89-inline$}}361 \Indexc[deletekeywords=inline]{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{\lstinline[deletekeywords=inline]{-fgnu89-inline}}} 361 362 Use the traditional GNU semantics for inline routines in C11 mode, which allows inline routines in header files. 362 363 \end{description} … … 530 531 Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism: 531 532 \begin{cfa} 532 int @``@otype = 3; $\C{// make keyword an identifier}$533 double @``@forall = 3.5;533 int ®``®otype = 3; $\C{// make keyword an identifier}$ 534 double ®``®forall = 3.5; 534 535 \end{cfa} 535 536 … … 542 543 // include file uses the CFA keyword "with". 543 544 #if ! defined( with ) $\C{// nesting ?}$ 544 #define with @``@with $\C{// make keyword an identifier}$545 #define with ®``®with $\C{// make keyword an identifier}$ 545 546 #define __CFA_BFD_H__ 546 547 #endif … … 560 561 Numeric constants are extended to allow \Index{underscore}s\index{constant!underscore} as a separator, \eg: 561 562 \begin{cfa} 562 2 @_@147@_@483@_@648; $\C{// decimal constant}$563 56 @_@ul; $\C{// decimal unsigned long constant}$564 0 @_@377; $\C{// octal constant}$565 0x @_@ff@_@ff; $\C{// hexadecimal constant}$566 0x @_@ef3d@_@aa5c; $\C{// hexadecimal constant}$567 3.141 @_@592@_@654; $\C{// floating constant}$568 10 @_@e@_@+1@_@00; $\C{// floating constant}$569 0x @_@ff@_@ff@_@p@_@3; $\C{// hexadecimal floating}$570 0x @_@1.ffff@_@ffff@_@p@_@128@_@l; $\C{// hexadecimal floating long constant}$571 L @_@$"\texttt{\textbackslash{x}}$@_@$\texttt{ff}$@_@$\texttt{ee}"$; $\C{// wide character constant}$563 2®_®147®_®483®_®648; $\C{// decimal constant}$ 564 56®_®ul; $\C{// decimal unsigned long constant}$ 565 0®_®377; $\C{// octal constant}$ 566 0x®_®ff®_®ff; $\C{// hexadecimal constant}$ 567 0x®_®ef3d®_®aa5c; $\C{// hexadecimal constant}$ 568 3.141®_®592®_®654; $\C{// floating constant}$ 569 10®_®e®_®+1®_®00; $\C{// floating constant}$ 570 0x®_®ff®_®ff®_®p®_®3; $\C{// hexadecimal floating}$ 571 0x®_®1.ffff®_®ffff®_®p®_®128®_®l; $\C{// hexadecimal floating long constant}$ 572 L®_®$"\texttt{\textbackslash{x}}$®_®$\texttt{ff}$®_®$\texttt{ee}"$; $\C{// wide character constant}$ 572 573 \end{cfa} 573 574 The rules for placement of underscores are: … … 602 603 Floating exponentiation\index{exponentiation!floating} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the exponent cannot be negative. 603 604 \begin{cfa} 604 sout | 1 @\@ 0 | 1 @\@ 1 | 2 @\@ 8 | -4 @\@ 3 | 5 @\@ 3 | 5 @\@ 32 | 5L @\@ 32 | 5L @\@ 64 | -4 @\@ -3 | -4.0 @\@ -3 | 4.0 @\@2.1605 | (1.0f+2.0fi) @\@(3.0f+2.0fi);606 1 1 256 -64 125 @0@ 3273344365508751233 @0@ @0@-0.015625 18.3791736799526 0.264715-1.1922i605 sout | 1 ®\® 0 | 1 ®\® 1 | 2 ®\® 8 | -4 ®\® 3 | 5 ®\® 3 | 5 ®\® 32 | 5L ®\® 32 | 5L ®\® 64 | -4 ®\® -3 | -4.0 ®\® -3 | 4.0 ®\® 2.1 606 | (1.0f+2.0fi) ®\® (3.0f+2.0fi); 607 1 1 256 -64 125 ®0® 3273344365508751233 ®0® ®0® -0.015625 18.3791736799526 0.264715-1.1922i 607 608 \end{cfa} 608 609 Note, ©5 \ 32© and ©5L \ 64© overflow, and ©-4 \ -3© is a fraction but stored in an integer so all three computations generate an integral zero. … … 612 613 \begin{cfa} 613 614 forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); } ) 614 T ? @\@?( T ep, unsigned int y );615 T ?®\®?( T ep, unsigned int y ); 615 616 forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); } ) 616 T ? @\@?( T ep, unsigned long int y );617 T ?®\®?( T ep, unsigned long int y ); 617 618 \end{cfa} 618 619 The user type ©T© must define multiplication, one (©1©), and ©*©. … … 624 625 625 626 626 %\subsection{\texorpdfstring{\protect\lstinline @if@/\protect\lstinline@while@Statement}{if Statement}}627 %\subsection{\texorpdfstring{\protect\lstinline{if}/\protect\lstinline{while} Statement}{if Statement}} 627 628 \subsection{\texorpdfstring{\LstKeywordStyle{if} / \LstKeywordStyle{while} Statement}{if / while Statement}} 628 629 … … 630 631 Declarations in the ©do©-©while© condition are not useful because they appear after the loop body.} 631 632 \begin{cfa} 632 if ( @int x = f()@) ... $\C{// x != 0}$633 if ( @int x = f(), y = g()@) ... $\C{// x != 0 \&\& y != 0}$634 if ( @int x = f(), y = g(); x < y@) ... $\C{// relational expression}$635 if ( @struct S { int i; } x = { f() }; x.i < 4@) $\C{// relational expression}$636 637 while ( @int x = f()@) ... $\C{// x != 0}$638 while ( @int x = f(), y = g()@) ... $\C{// x != 0 \&\& y != 0}$639 while ( @int x = f(), y = g(); x < y@) ... $\C{// relational expression}$640 while ( @struct S { int i; } x = { f() }; x.i < 4@) ... $\C{// relational expression}$633 if ( ®int x = f()® ) ... $\C{// x != 0}$ 634 if ( ®int x = f(), y = g()® ) ... $\C{// x != 0 \&\& y != 0}$ 635 if ( ®int x = f(), y = g(); x < y® ) ... $\C{// relational expression}$ 636 if ( ®struct S { int i; } x = { f() }; x.i < 4® ) $\C{// relational expression}$ 637 638 while ( ®int x = f()® ) ... $\C{// x != 0}$ 639 while ( ®int x = f(), y = g()® ) ... $\C{// x != 0 \&\& y != 0}$ 640 while ( ®int x = f(), y = g(); x < y® ) ... $\C{// relational expression}$ 641 while ( ®struct S { int i; } x = { f() }; x.i < 4® ) ... $\C{// relational expression}$ 641 642 \end{cfa} 642 643 Unless a relational expression is specified, each variable is compared not equal to 0, which is the standard semantics for the ©if©/©while© expression, and the results are combined using the logical ©&&© operator. … … 645 646 646 647 647 %\section{\texorpdfstring{\protect\lstinline @case@Clause}{case Clause}}648 %\section{\texorpdfstring{\protect\lstinline{case} Clause}{case Clause}} 648 649 \subsection{\texorpdfstring{\LstKeywordStyle{case} Clause}{case Clause}} 649 650 \label{s:caseClause} … … 658 659 \begin{cfa} 659 660 switch ( i ) { 660 case @1, 3, 5@:661 case ®1, 3, 5®: 661 662 ... 662 case @2, 4, 6@:663 case ®2, 4, 6®: 663 664 ... 664 665 } … … 685 686 \end{cquote} 686 687 In addition, subranges are allowed to specify case values.\footnote{ 687 gcc has the same mechanism but awkward syntax, \lstinline @2 ...42@, because a space is required after a number, otherwise the period is a decimal point.}688 gcc has the same mechanism but awkward syntax, \lstinline{2 ...42}, because a space is required after a number, otherwise the period is a decimal point.} 688 689 \begin{cfa} 689 690 switch ( i ) { 690 case @1~5:@$\C{// 1, 2, 3, 4, 5}$691 case ®1~5:® $\C{// 1, 2, 3, 4, 5}$ 691 692 ... 692 case @10~15:@$\C{// 10, 11, 12, 13, 14, 15}$693 case ®10~15:® $\C{// 10, 11, 12, 13, 14, 15}$ 693 694 ... 694 695 } … … 696 697 Lists of subranges are also allowed. 697 698 \begin{cfa} 698 case @1~5, 12~21, 35~42@:699 \end{cfa} 700 701 702 %\section{\texorpdfstring{\protect\lstinline @switch@Statement}{switch Statement}}699 case ®1~5, 12~21, 35~42®: 700 \end{cfa} 701 702 703 %\section{\texorpdfstring{\protect\lstinline{switch} Statement}{switch Statement}} 703 704 \subsection{\texorpdfstring{\LstKeywordStyle{switch} Statement}{switch Statement}} 704 705 … … 740 741 if ( argc == 3 ) { 741 742 // open output file 742 @// open input file743 @} else if ( argc == 2 ) {744 @// open input file (duplicate)745 746 @} else {743 ®// open input file 744 ®} else if ( argc == 2 ) { 745 ®// open input file (duplicate) 746 747 ®} else { 747 748 // usage message 748 749 } … … 755 756 \begin{cfa} 756 757 switch ( i ) { 757 @case 1: case 3: case 5:@// odd values758 ®case 1: case 3: case 5:® // odd values 758 759 // odd action 759 760 break; 760 @case 2: case 4: case 6:@// even values761 ®case 2: case 4: case 6:® // even values 761 762 // even action 762 763 break; … … 774 775 if ( j < k ) { 775 776 ... 776 @case 1:@// transfer into "if" statement777 ®case 1:® // transfer into "if" statement 777 778 ... 778 779 } // if … … 780 781 while ( j < 5 ) { 781 782 ... 782 @case 3:@// transfer into "while" statement783 ®case 3:® // transfer into "while" statement 783 784 ... 784 785 } // while … … 821 822 \begin{cfa} 822 823 switch ( x ) { 823 @int y = 1;@$\C{// unreachable initialization}$824 @x = 7;@$\C{// unreachable code without label/branch}$824 ®int y = 1;® $\C{// unreachable initialization}$ 825 ®x = 7;® $\C{// unreachable code without label/branch}$ 825 826 case 0: ... 826 827 ... 827 @int z = 0;@$\C{// unreachable initialization, cannot appear after case}$828 ®int z = 0;® $\C{// unreachable initialization, cannot appear after case}$ 828 829 z = 2; 829 830 case 1: 830 @x = z;@$\C{// without fall through, z is uninitialized}$831 ®x = z;® $\C{// without fall through, z is uninitialized}$ 831 832 } 832 833 \end{cfa} … … 860 861 Therefore, to preserve backwards compatibility, it is necessary to introduce a new kind of ©switch© statement, called ©choose©, with no implicit fall-through semantics and an explicit fall-through if the last statement of a case-clause ends with the new keyword ©fallthrough©/©fallthru©, \eg: 861 862 \begin{cfa} 862 @choose@( i ) {863 ®choose® ( i ) { 863 864 case 1: case 2: case 3: 864 865 ... 865 @// implicit end of switch (break)866 @case 5:866 ®// implicit end of switch (break) 867 ®case 5: 867 868 ... 868 @fallthru@; $\C{// explicit fall through}$869 ®fallthru®; $\C{// explicit fall through}$ 869 870 case 7: 870 871 ... 871 @break@$\C{// explicit end of switch (redundant)}$872 ®break® $\C{// explicit end of switch (redundant)}$ 872 873 default: 873 874 j = 3; … … 890 891 \begin{cfa} 891 892 switch ( x ) { 892 @int i = 0;@$\C{// allowed only at start}$893 ®int i = 0;® $\C{// allowed only at start}$ 893 894 case 0: 894 895 ... 895 @int j = 0;@$\C{// disallowed}$896 ®int j = 0;® $\C{// disallowed}$ 896 897 case 1: 897 898 { 898 @int k = 0;@$\C{// allowed at different nesting levels}$899 ®int k = 0;® $\C{// allowed at different nesting levels}$ 899 900 ... 900 @case 2:@$\C{// disallow case in nested statements}$901 ®case 2:® $\C{// disallow case in nested statements}$ 901 902 } 902 903 ... … … 915 916 case 3: 916 917 if ( ... ) { 917 ... @fallthru;@// goto case 4918 ... ®fallthru;® // goto case 4 918 919 } else { 919 920 ... … … 930 931 choose ( ... ) { 931 932 case 3: 932 ... @fallthrough common;@933 ... ®fallthrough common;® 933 934 case 4: 934 ... @fallthrough common;@935 936 @common:@// below fallthrough935 ... ®fallthrough common;® 936 937 ®common:® // below fallthrough 937 938 // at case-clause level 938 939 ... // common code for cases 3/4 … … 950 951 for ( ... ) { 951 952 // multi-level transfer 952 ... @fallthru common;@953 ... ®fallthru common;® 953 954 } 954 955 ... 955 956 } 956 957 ... 957 @common:@// below fallthrough958 ®common:® // below fallthrough 958 959 // at case-clause level 959 960 \end{cfa} … … 970 971 \hline 971 972 \begin{cfa} 972 while @($\,$)@{ sout | "empty"; break; }973 do { sout | "empty"; break; } while @($\,$)@;974 for @($\,$)@{ sout | "empty"; break; }975 for ( @0@) { sout | "A"; } sout | "zero";976 for ( @1@) { sout | "A"; }977 for ( @10@) { sout | "A"; }978 for ( @= 10@) { sout | "A"; }979 for ( @1 ~= 10 ~ 2@) { sout | "B"; }980 for ( @10 -~= 1 ~ 2@) { sout | "C"; }981 for ( @0.5 ~ 5.5@) { sout | "D"; }982 for ( @5.5 -~ 0.5@) { sout | "E"; }983 for ( @i; 10@) { sout | i; }984 for ( @i; = 10@) { sout | i; }985 for ( @i; 1 ~= 10 ~ 2@) { sout | i; }986 for ( @i; 10 -~= 1 ~ 2@) { sout | i; }987 for ( @i; 0.5 ~ 5.5@) { sout | i; }988 for ( @i; 5.5 -~ 0.5@) { sout | i; }989 for ( @ui; 2u ~= 10u ~ 2u@) { sout | ui; }990 for ( @ui; 10u -~= 2u ~ 2u@) { sout | ui; }973 while ®($\,$)® { sout | "empty"; break; } 974 do { sout | "empty"; break; } while ®($\,$)®; 975 for ®($\,$)® { sout | "empty"; break; } 976 for ( ®0® ) { sout | "A"; } sout | "zero"; 977 for ( ®1® ) { sout | "A"; } 978 for ( ®10® ) { sout | "A"; } 979 for ( ®= 10® ) { sout | "A"; } 980 for ( ®1 ~= 10 ~ 2® ) { sout | "B"; } 981 for ( ®10 -~= 1 ~ 2® ) { sout | "C"; } 982 for ( ®0.5 ~ 5.5® ) { sout | "D"; } 983 for ( ®5.5 -~ 0.5® ) { sout | "E"; } 984 for ( ®i; 10® ) { sout | i; } 985 for ( ®i; = 10® ) { sout | i; } 986 for ( ®i; 1 ~= 10 ~ 2® ) { sout | i; } 987 for ( ®i; 10 -~= 1 ~ 2® ) { sout | i; } 988 for ( ®i; 0.5 ~ 5.5® ) { sout | i; } 989 for ( ®i; 5.5 -~ 0.5® ) { sout | i; } 990 for ( ®ui; 2u ~= 10u ~ 2u® ) { sout | ui; } 991 for ( ®ui; 10u -~= 2u ~ 2u® ) { sout | ui; } 991 992 enum { N = 10 }; 992 for ( @N@) { sout | "N"; }993 for ( @i; N@) { sout | i; }994 for ( @i; N -~ 0@) { sout | i; }993 for ( ®N® ) { sout | "N"; } 994 for ( ®i; N® ) { sout | i; } 995 for ( ®i; N -~ 0® ) { sout | i; } 995 996 const int start = 3, comp = 10, inc = 2; 996 for ( @i; start ~ comp ~ inc + 1@) { sout | i; }997 for ( i; 1 ~ $\R{@}$) { if ( i > 10 ) break; sout | i; }998 for ( i; 10 -~ $\R{@}$) { if ( i < 0 ) break; sout | i; }999 for ( i; 2 ~ $\R{@}$~ 2 ) { if ( i > 10 ) break; sout | i; }1000 for ( i; 2.1 ~ $\R{@}$ ~ $\R{@}$) { if ( i > 10.5 ) break; sout | i; i += 1.7; }1001 for ( i; 10 -~ $\R{@}$~ 2 ) { if ( i < 0 ) break; sout | i; }1002 for ( i; 12.1 ~ $\R{@}$ ~ $\R{@}$) { if ( i < 2.5 ) break; sout | i; i -= 1.7; }1003 for ( i; 5 @:@ j; -5 ~ $@$) { sout | i | j; }1004 for ( i; 5 @:@ j; -5 -~ $@$) { sout | i | j; }1005 for ( i; 5 @:@ j; -5 ~ $@$~ 2 ) { sout | i | j; }1006 for ( i; 5 @:@ j; -5 -~ $@$~ 2 ) { sout | i | j; }1007 for ( i; 5 @:@ j; -5 ~ $@$) { sout | i | j; }1008 for ( i; 5 @:@ j; -5 -~ $@$) { sout | i | j; }1009 for ( i; 5 @:@ j; -5 ~ $@$~ 2 ) { sout | i | j; }1010 for ( i; 5 @:@ j; -5 -~ $@$~ 2 ) { sout | i | j; }1011 for ( i; 5 @:@ j; -5 -~ $@$ ~ 2 @:@ k; 1.5 ~ $@$) { sout | i | j | k; }1012 for ( i; 5 @:@ j; -5 -~ $@$ ~ 2 @:@ k; 1.5 ~ $@$) { sout | i | j | k; }1013 for ( i; 5 @:@ k; 1.5 ~ $@$ @:@ j; -5 -~ $@$~ 2 ) { sout | i | j | k; }997 for ( ®i; start ~ comp ~ inc + 1® ) { sout | i; } 998 for ( i; 1 ~ ®@® ) { if ( i > 10 ) break; sout | i; } 999 for ( i; 10 -~ ®@® ) { if ( i < 0 ) break; sout | i; } 1000 for ( i; 2 ~ ®@® ~ 2 ) { if ( i > 10 ) break; sout | i; } 1001 for ( i; 2.1 ~ ®@® ~ ®@® ) { if ( i > 10.5 ) break; sout | i; i += 1.7; } 1002 for ( i; 10 -~ ®@® ~ 2 ) { if ( i < 0 ) break; sout | i; } 1003 for ( i; 12.1 ~ ®@® ~ ®@® ) { if ( i < 2.5 ) break; sout | i; i -= 1.7; } 1004 for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; } 1005 for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; } 1006 for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; } 1007 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; } 1008 for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; } 1009 for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; } 1010 for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; } 1011 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; } 1012 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; } 1013 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; } 1014 for ( i; 5 ®:® k; 1.5 ~ @ ®:® j; -5 -~ @ ~ 2 ) { sout | i | j | k; } 1014 1015 \end{cfa} 1015 1016 & … … 1089 1090 The loop index is polymorphic in the type of the comparison value N (when the start value is implicit) or the start value M. 1090 1091 \begin{cfa} 1091 for ( i; @5@) $\C[2.5in]{// typeof(5) i; 5 is comparison value}$1092 for ( i; @1.5@~5.5~0.5 ) $\C{// typeof(1.5) i; 1.5 is start value}$1092 for ( i; ®5® ) $\C[2.5in]{// typeof(5) i; 5 is comparison value}$ 1093 for ( i; ®1.5®~5.5~0.5 ) $\C{// typeof(1.5) i; 1.5 is start value}$ 1093 1094 \end{cfa} 1094 1095 \item 1095 1096 An empty conditional implies comparison value of ©1© (true). 1096 1097 \begin{cfa} 1097 while ( $\R{/*empty*/}$ )$\C{// while ( true )}$1098 for ( $\R{/*empty*/}$) $\C{// for ( ; true; )}$1099 do ... while ( $\R{/*empty*/}$ )$\C{// do ... while ( true )}$1098 while ( ®/*empty*/® ) $\C{// while ( true )}$ 1099 for ( ®/*empty*/® ) $\C{// for ( ; true; )}$ 1100 do ... while ( ®/*empty*/® ) $\C{// do ... while ( true )}$ 1100 1101 \end{cfa} 1101 1102 \item 1102 1103 A comparison N is implicit up-to exclusive range [0,N\R{)}. 1103 1104 \begin{cfa} 1104 for ( @5@) $\C{// for ( typeof(5) i; i < 5; i += 1 )}$1105 for ( ®5® ) $\C{// for ( typeof(5) i; i < 5; i += 1 )}$ 1105 1106 \end{cfa} 1106 1107 \item 1107 1108 A comparison ©=© N is implicit up-to inclusive range [0,N\R{]}. 1108 1109 \begin{cfa} 1109 for ( @=@5 ) $\C{// for ( typeof(5) i; i <= 5; i += 1 )}$1110 for ( ®=®5 ) $\C{// for ( typeof(5) i; i <= 5; i += 1 )}$ 1110 1111 \end{cfa} 1111 1112 \item 1112 1113 The up-to range M ©~©\index{~@©~©} N means exclusive range [M,N\R{)}. 1113 1114 \begin{cfa} 1114 for ( 1 @~@5 ) $\C{// for ( typeof(1) i = 1; i < 5; i += 1 )}$1115 for ( 1®~®5 ) $\C{// for ( typeof(1) i = 1; i < 5; i += 1 )}$ 1115 1116 \end{cfa} 1116 1117 \item 1117 1118 The up-to range M ©~=©\index{~=@©~=©} N means inclusive range [M,N\R{]}. 1118 1119 \begin{cfa} 1119 for ( 1 @~=@5 ) $\C{// for ( typeof(1) i = 1; i <= 5; i += 1 )}$1120 for ( 1®~=®5 ) $\C{// for ( typeof(1) i = 1; i <= 5; i += 1 )}$ 1120 1121 \end{cfa} 1121 1122 \item 1122 1123 The down-to range M ©-~©\index{-~@©-~©} N means exclusive range [N,M\R{)}. 1123 1124 \begin{cfa} 1124 for ( 1 @-~@5 ) $\C{// for ( typeof(1) i = 5; i > 0; i -= 1 )}$1125 for ( 1®-~®5 ) $\C{// for ( typeof(1) i = 5; i > 0; i -= 1 )}$ 1125 1126 \end{cfa} 1126 1127 \item 1127 1128 The down-to range M ©-~=©\index{-~=@©-~=©} N means inclusive range [N,M\R{]}. 1128 1129 \begin{cfa} 1129 for ( 1 @-~=@5 ) $\C{// for ( typeof(1) i = 5; i >= 0; i -= 1 )}$1130 for ( 1®-~=®5 ) $\C{// for ( typeof(1) i = 5; i >= 0; i -= 1 )}$ 1130 1131 \end{cfa} 1131 1132 \item 1132 1133 ©@© means put nothing in this field. 1133 1134 \begin{cfa} 1134 for ( 1~ $\R{@}$~2 )$\C{// for ( typeof(1) i = 1; /*empty*/; i += 2 )}$1135 for ( 1~®@®~2 ) $\C{// for ( typeof(1) i = 1; /*empty*/; i += 2 )}$ 1135 1136 \end{cfa} 1136 1137 \item 1137 1138 ©:© means start another index. 1138 1139 \begin{cfa} 1139 for ( i; 5 @:@j; 2~12~3 ) $\C{// for ( typeof(i) i = 1, j = 2; i < 5 \&\& j < 12; i += 1, j += 3 )}\CRT$1140 for ( i; 5 ®:® j; 2~12~3 ) $\C{// for ( typeof(i) i = 1, j = 2; i < 5 \&\& j < 12; i += 1, j += 3 )}\CRT$ 1140 1141 \end{cfa} 1141 1142 \end{itemize} 1142 1143 1143 1144 1144 %\subsection{\texorpdfstring{Labelled \protect\lstinline @continue@ / \protect\lstinline@break@}{Labelled continue / break}}1145 %\subsection{\texorpdfstring{Labelled \protect\lstinline{continue} / \protect\lstinline{break}}{Labelled continue / break}} 1145 1146 \subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}} 1146 1147 … … 1157 1158 \begin{lrbox}{\myboxA} 1158 1159 \begin{cfa}[tabsize=3] 1159 @Compound:@{1160 @Try:@try {1161 @For:@for ( ... ) {1162 @While:@while ( ... ) {1163 @Do:@do {1164 @If:@if ( ... ) {1165 @Switch:@switch ( ... ) {1160 ®Compound:® { 1161 ®Try:® try { 1162 ®For:® for ( ... ) { 1163 ®While:® while ( ... ) { 1164 ®Do:® do { 1165 ®If:® if ( ... ) { 1166 ®Switch:® switch ( ... ) { 1166 1167 case 3: 1167 @break Compound@;1168 @break Try@;1169 @break For@; /* or */ @continue For@;1170 @break While@; /* or */ @continue While@;1171 @break Do@; /* or */ @continue Do@;1172 @break If@;1173 @break Switch@;1168 ®break Compound®; 1169 ®break Try®; 1170 ®break For®; /* or */ ®continue For®; 1171 ®break While®; /* or */ ®continue While®; 1172 ®break Do®; /* or */ ®continue Do®; 1173 ®break If®; 1174 ®break Switch®; 1174 1175 } // switch 1175 1176 } else { 1176 ... @break If@; ... // terminate if1177 ... ®break If®; ... // terminate if 1177 1178 } // if 1178 1179 } while ( ... ); // do 1179 1180 } // while 1180 1181 } // for 1181 } @finally@{ // always executed1182 } ®finally® { // always executed 1182 1183 } // try 1183 1184 } // compound … … 1189 1190 { 1190 1191 1191 @ForC:@for ( ... ) {1192 @WhileC:@while ( ... ) {1193 @DoC:@do {1192 ®ForC:® for ( ... ) { 1193 ®WhileC:® while ( ... ) { 1194 ®DoC:® do { 1194 1195 if ( ... ) { 1195 1196 switch ( ... ) { 1196 1197 case 3: 1197 @goto Compound@;1198 @goto Try@;1199 @goto ForB@; /* or */ @goto ForC@;1200 @goto WhileB@; /* or */ @goto WhileC@;1201 @goto DoB@; /* or */ @goto DoC@;1202 @goto If@;1203 @goto Switch@;1204 } @Switch:@;1198 ®goto Compound®; 1199 ®goto Try®; 1200 ®goto ForB®; /* or */ ®goto ForC®; 1201 ®goto WhileB®; /* or */ ®goto WhileC®; 1202 ®goto DoB®; /* or */ ®goto DoC®; 1203 ®goto If®; 1204 ®goto Switch®; 1205 } ®Switch:® ; 1205 1206 } else { 1206 ... @goto If@; ... // terminate if1207 } @If:@;1208 } while ( ... ); @DoB:@;1209 } @WhileB:@;1210 } @ForB:@;1211 1212 1213 } @Compound:@;1207 ... ®goto If®; ... // terminate if 1208 } ®If:®; 1209 } while ( ... ); ®DoB:® ; 1210 } ®WhileB:® ; 1211 } ®ForB:® ; 1212 1213 1214 } ®Compound:® ; 1214 1215 \end{cfa} 1215 1216 \end{lrbox} … … 1240 1241 1241 1242 1242 %\subsection{\texorpdfstring{\protect\lstinline @with@Statement}{with Statement}}1243 %\subsection{\texorpdfstring{\protect\lstinline{with} Statement}{with Statement}} 1243 1244 \subsection{\texorpdfstring{\LstKeywordStyle{with} Statement}{with Statement}} 1244 1245 \label{s:WithStatement} … … 1255 1256 \begin{cfa} 1256 1257 Person p 1257 @p.@name; @p.@address; @p.@sex; $\C{// access containing fields}$1258 ®p.®name; ®p.®address; ®p.®sex; $\C{// access containing fields}$ 1258 1259 \end{cfa} 1259 1260 which extends to multiple levels of qualification for nested aggregates and multiple aggregates. 1260 1261 \begin{cfa} 1261 1262 struct Ticket { ... } t; 1262 @p.name@.first; @p.address@.street; $\C{// access nested fields}$1263 @t.@departure; @t.@cost; $\C{// access multiple aggregate}$1263 ®p.name®.first; ®p.address®.street; $\C{// access nested fields}$ 1264 ®t.®departure; ®t.®cost; $\C{// access multiple aggregate}$ 1264 1265 \end{cfa} 1265 1266 Repeated aggregate qualification is tedious and makes code difficult to read. … … 1284 1285 \begin{C++} 1285 1286 struct S { 1286 char @c@; int @i@; double @d@;1287 char ®c®; int ®i®; double ®d®; 1287 1288 void f( /* S * this */ ) { $\C{// implicit ``this'' parameter}$ 1288 @c@; @i@; @d@; $\C{// this->c; this->i; this->d;}$1289 ®c®; ®i®; ®d®; $\C{// this->c; this->i; this->d;}$ 1289 1290 } 1290 1291 } … … 1294 1295 \begin{cfa} 1295 1296 struct T { 1296 char @m@; int @i@; double @n@; $\C{// derived class variables}$1297 char ®m®; int ®i®; double ®n®; $\C{// derived class variables}$ 1297 1298 }; 1298 1299 struct S : public T { 1299 char @c@; int @i@; double @d@; $\C{// class variables}$1300 void g( double @d@, T & t ) {1301 d; @t@.m; @t@.i; @t@.n; $\C{// function parameter}$1302 c; i; @this->@d; @S::@d; $\C{// class S variables}$1303 m; @T::@i; n; $\C{// class T variables}$1300 char ®c®; int ®i®; double ®d®; $\C{// class variables}$ 1301 void g( double ®d®, T & t ) { 1302 d; ®t®.m; ®t®.i; ®t®.n; $\C{// function parameter}$ 1303 c; i; ®this->®d; ®S::®d; $\C{// class S variables}$ 1304 m; ®T::®i; n; $\C{// class T variables}$ 1304 1305 } 1305 1306 }; … … 1311 1312 Hence, the qualified fields become variables with the side-effect that it is simpler to write, easier to read, and optimize field references in a block. 1312 1313 \begin{cfa} 1313 void f( S & this ) @with ( this )@{ $\C{// with statement}$1314 @c@; @i@; @d@; $\C{// this.c, this.i, this.d}$1314 void f( S & this ) ®with ( this )® { $\C{// with statement}$ 1315 ®c®; ®i®; ®d®; $\C{// this.c, this.i, this.d}$ 1315 1316 } 1316 1317 \end{cfa} 1317 1318 with the generality of opening multiple aggregate-parameters: 1318 1319 \begin{cfa} 1319 void g( S & s, T & t ) @with ( s, t )@{ $\C{// multiple aggregate parameters}$1320 c; @s.@i; d; $\C{// s.c, s.i, s.d}$1321 m; @t.@i; n; $\C{// t.m, t.i, t.n}$1320 void g( S & s, T & t ) ®with ( s, t )® { $\C{// multiple aggregate parameters}$ 1321 c; ®s.®i; d; $\C{// s.c, s.i, s.d}$ 1322 m; ®t.®i; n; $\C{// t.m, t.i, t.n}$ 1322 1323 } 1323 1324 \end{cfa} … … 1338 1339 The difference between parallel and nesting occurs for fields with the same name and type: 1339 1340 \begin{cfa} 1340 struct Q { int @i@; int k; int @m@; } q, w;1341 struct R { int @i@; int j; double @m@; } r, w;1341 struct Q { int ®i®; int k; int ®m®; } q, w; 1342 struct R { int ®i®; int j; double ®m®; } r, w; 1342 1343 with ( r, q ) { 1343 1344 j + k; $\C{// unambiguous, r.j + q.k}$ … … 1372 1373 \begin{cfa} 1373 1374 void ?{}( S & s, int i ) with ( s ) { $\C{// constructor}$ 1374 @s.i = i;@j = 3; m = 5.5; $\C{// initialize fields}$1375 ®s.i = i;® j = 3; m = 5.5; $\C{// initialize fields}$ 1375 1376 } 1376 1377 \end{cfa} … … 1385 1386 and implicitly opened \emph{after} a function-body open, to give them higher priority: 1386 1387 \begin{cfa} 1387 void ?{}( S & s, int @i@ ) with ( s ) @with( $\emph{\R{params}}$ )@{ // syntax not allowed, illustration only1388 s.i = @i@; j = 3; m = 5.5;1388 void ?{}( S & s, int ®i® ) with ( s ) ®with( $\emph{\R{params}}$ )® { // syntax not allowed, illustration only 1389 s.i = ®i®; j = 3; m = 5.5; 1389 1390 } 1390 1391 \end{cfa} … … 1469 1470 For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way: 1470 1471 \begin{cfa} 1471 int @(*@f@())[@5@]@{...}; $\C{// definition}$1472 ... @(*@f@())[@3@]@+= 1; $\C{// usage}$1472 int ®(*®f®())[®5®]® {...}; $\C{// definition}$ 1473 ... ®(*®f®())[®3®]® += 1; $\C{// usage}$ 1473 1474 \end{cfa} 1474 1475 Essentially, the return type is wrapped around the routine name in successive layers (like an \Index{onion}). … … 1486 1487 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1487 1488 \begin{cfa}[moredelim={**[is][\color{blue}]{\#}{\#}}] 1488 #[5] *# @int@x1;1489 #* [5]# @int@x2;1490 #[* [5] int]# f @( int p )@;1489 #[5] *# ®int® x1; 1490 #* [5]# ®int® x2; 1491 #[* [5] int]# f®( int p )®; 1491 1492 \end{cfa} 1492 1493 & 1493 1494 \begin{cfa}[moredelim={**[is][\color{blue}]{\#}{\#}}] 1494 @int@#*# x1 #[5]#;1495 @int@#(*#x2#)[5]#;1496 #int (*#f @( int p )@#)[5]#;1495 ®int® #*# x1 #[5]#; 1496 ®int® #(*#x2#)[5]#; 1497 #int (*#f®( int p )®#)[5]#; 1497 1498 \end{cfa} 1498 1499 \end{tabular} … … 1506 1507 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1507 1508 \begin{cfa} 1508 @*@int x, y;1509 ®*® int x, y; 1509 1510 \end{cfa} 1510 1511 & 1511 1512 \begin{cfa} 1512 int @*@x, @*@y;1513 int ®*®x, ®*®y; 1513 1514 \end{cfa} 1514 1515 \end{tabular} … … 1519 1520 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1520 1521 \begin{cfa} 1521 @*@int x;1522 ®*® int x; 1522 1523 int y; 1523 1524 \end{cfa} 1524 1525 & 1525 1526 \begin{cfa} 1526 int @*@x, y;1527 int ®*®x, y; 1527 1528 1528 1529 \end{cfa} … … 1660 1661 & 1661 1662 \begin{cfa} 1662 int * @const@x = (int *)1001663 int * ®const® x = (int *)100 1663 1664 *x = 3; // implicit dereference 1664 int * @const@y = (int *)104;1665 int * ®const® y = (int *)104; 1665 1666 *y = *x; // implicit dereference 1666 1667 \end{cfa} … … 1700 1701 \begin{tabular}{@{}l@{\hspace{2em}}l@{}} 1701 1702 \begin{cfa} 1702 int x, y, @*@ p1, @*@ p2, @**@p3;1703 p1 = @&@x; // p1 points to x1703 int x, y, ®*® p1, ®*® p2, ®**® p3; 1704 p1 = ®&®x; // p1 points to x 1704 1705 p2 = p1; // p2 points to x 1705 p1 = @&@y; // p1 points to y1706 p1 = ®&®y; // p1 points to y 1706 1707 p3 = &p2; // p3 points to p2 1707 1708 \end{cfa} … … 1729 1730 \begin{cfa} 1730 1731 p1 = p2; $\C{// pointer address assignment}$ 1731 @*@p2 = @*@p1 + x; $\C{// pointed-to value assignment / operation}$1732 ®*®p2 = ®*®p1 + x; $\C{// pointed-to value assignment / operation}$ 1732 1733 \end{cfa} 1733 1734 The C semantics work well for situations where manipulation of addresses is the primary meaning and data is rarely accessed, such as storage management (©malloc©/©free©). … … 1745 1746 To support this common case, a reference type is introduced in \CFA, denoted by ©&©, which is the opposite dereference semantics to a pointer type, making the value at the pointed-to location the implicit semantics for dereferencing (similar but not the same as \CC \Index{reference type}s). 1746 1747 \begin{cfa} 1747 int x, y, @&@ r1, @&@ r2, @&&@r3;1748 @&@r1 = &x; $\C{// r1 points to x}$1749 @&@r2 = &r1; $\C{// r2 points to x}$1750 @&@r1 = &y; $\C{// r1 points to y}$1751 @&&@r3 = @&@&r2; $\C{// r3 points to r2}$1748 int x, y, ®&® r1, ®&® r2, ®&&® r3; 1749 ®&®r1 = &x; $\C{// r1 points to x}$ 1750 ®&®r2 = &r1; $\C{// r2 points to x}$ 1751 ®&®r1 = &y; $\C{// r1 points to y}$ 1752 ®&&®r3 = ®&®&r2; $\C{// r3 points to r2}$ 1752 1753 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); $\C{// implicit dereferencing}$ 1753 1754 \end{cfa} … … 1756 1757 One way to conceptualize a reference is via a rewrite rule, where the compiler inserts a dereference operator before the reference variable for each reference qualifier in a declaration, so the previous example becomes: 1757 1758 \begin{cfa} 1758 @*@r2 = ((@*@r1 + @*@r2) @*@ (@**@r3 - @*@r1)) / (@**@r3 - 15);1759 ®*®r2 = ((®*®r1 + ®*®r2) ®*® (®**®r3 - ®*®r1)) / (®**®r3 - 15); 1759 1760 \end{cfa} 1760 1761 When a reference operation appears beside a dereference operation, \eg ©&*©, they cancel out. … … 1765 1766 For a \CFA reference type, the cancellation on the left-hand side of assignment leaves the reference as an address (\Index{lvalue}): 1766 1767 \begin{cfa} 1767 (& @*@)r1 = &x; $\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}$1768 (&®*®)r1 = &x; $\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}$ 1768 1769 \end{cfa} 1769 1770 Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}): 1770 1771 \begin{cfa} 1771 (&(& @*@)@*@)r3 = &(&@*@)r2; $\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}$1772 (&(&®*®)®*®)r3 = &(&®*®)r2; $\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}$ 1772 1773 \end{cfa} 1773 1774 Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth. … … 1792 1793 const int cx = 5; $\C{// cannot change cx;}$ 1793 1794 const int & cr = cx; $\C{// cannot change what cr points to}$ 1794 @&@cr = &cx; $\C{// can change cr}$1795 ®&®cr = &cx; $\C{// can change cr}$ 1795 1796 cr = 7; $\C{// error, cannot change cx}$ 1796 1797 int & const rc = x; $\C{// must be initialized}$ 1797 @&@rc = &x; $\C{// error, cannot change rc}$1798 ®&®rc = &x; $\C{// error, cannot change rc}$ 1798 1799 const int & const crc = cx; $\C{// must be initialized}$ 1799 1800 crc = 7; $\C{// error, cannot change cx}$ 1800 @&@crc = &cx; $\C{// error, cannot change crc}$1801 ®&®crc = &cx; $\C{// error, cannot change crc}$ 1801 1802 \end{cfa} 1802 1803 Hence, for type ©& const©, there is no pointer assignment, so ©&rc = &x© is disallowed, and \emph{the address value cannot be the null pointer unless an arbitrary pointer is coerced\index{coercion} into the reference}: … … 1819 1820 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1820 1821 \begin{cfa} 1821 @const@ * @const@* const int ccp;1822 @const@ & @const@& const int ccr;1822 ®const® * ®const® * const int ccp; 1823 ®const® & ®const® & const int ccr; 1823 1824 \end{cfa} 1824 1825 & 1825 1826 \begin{cfa} 1826 const int * @const@ * @const@ccp;1827 const int * ®const® * ®const® ccp; 1827 1828 1828 1829 \end{cfa} … … 1856 1857 \begin{cfa} 1857 1858 int * p = &x; $\C{// assign address of x}$ 1858 @int * p = x;@$\C{// assign value of x}$1859 ®int * p = x;® $\C{// assign value of x}$ 1859 1860 int & r = x; $\C{// must have address of x}$ 1860 1861 \end{cfa} … … 1880 1881 When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions. 1881 1882 \begin{cfa} 1882 void f( @const@int & cr );1883 void g( @const@int * cp );1884 f( 3 ); g( @&@3 );1885 f( x + y ); g( @&@(x + y) );1883 void f( ®const® int & cr ); 1884 void g( ®const® int * cp ); 1885 f( 3 ); g( ®&®3 ); 1886 f( x + y ); g( ®&®(x + y) ); 1886 1887 \end{cfa} 1887 1888 Here, the compiler passes the address to the literal 3 or the temporary for the expression ©x + y©, knowing the argument cannot be changed through the parameter. … … 1894 1895 void f( int & r ); 1895 1896 void g( int * p ); 1896 f( 3 ); g( @&@3 ); $\C{// compiler implicit generates temporaries}$1897 f( x + y ); g( @&@(x + y) ); $\C{// compiler implicit generates temporaries}$1897 f( 3 ); g( ®&®3 ); $\C{// compiler implicit generates temporaries}$ 1898 f( x + y ); g( ®&®(x + y) ); $\C{// compiler implicit generates temporaries}$ 1898 1899 \end{cfa} 1899 1900 Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{ … … 1916 1917 Instead, a routine object should be referenced by a ©const© reference: 1917 1918 \begin{cfa} 1918 @const@ void (@&@fr)( int ) = f; $\C{// routine reference}$1919 ®const® void (®&® fr)( int ) = f; $\C{// routine reference}$ 1919 1920 fr = ... $\C{// error, cannot change code}$ 1920 1921 &fr = ...; $\C{// changing routine reference}$ … … 1978 1979 \begin{cfa} 1979 1980 int x, &r = x, f( int p ); 1980 x = @r@ + f( @r@); $\C{// lvalue reference converts to rvalue}$1981 x = ®r® + f( ®r® ); $\C{// lvalue reference converts to rvalue}$ 1981 1982 \end{cfa} 1982 1983 An rvalue has no type qualifiers (©cv©), so the reference qualifiers are dropped. 1983 1984 1984 1985 \item 1985 lvalue to reference conversion: \lstinline[deletekeywords=lvalue] @lvalue-type cv1 T@converts to ©cv2 T &©, which allows implicitly converting variables to references.1986 \begin{cfa} 1987 int x, &r = @x@, f( int & p ); $\C{// lvalue variable (int) convert to reference (int \&)}$1988 f( @x@); $\C{// lvalue variable (int) convert to reference (int \&)}$1986 lvalue to reference conversion: \lstinline[deletekeywords=lvalue]{lvalue-type cv1 T} converts to ©cv2 T &©, which allows implicitly converting variables to references. 1987 \begin{cfa} 1988 int x, &r = ®x®, f( int & p ); $\C{// lvalue variable (int) convert to reference (int \&)}$ 1989 f( ®x® ); $\C{// lvalue variable (int) convert to reference (int \&)}$ 1989 1990 \end{cfa} 1990 1991 Conversion can restrict a type, where ©cv1© $\le$ ©cv2©, \eg passing an ©int© to a ©const volatile int &©, which has low cost. … … 1996 1997 \begin{cfa} 1997 1998 int x, & f( int & p ); 1998 f( @x + 3@); $\C[1.5in]{// rvalue parameter (int) implicitly converts to lvalue temporary reference (int \&)}$1999 @&f@(...) = &x; $\C{// rvalue result (int \&) implicitly converts to lvalue temporary reference (int \&)}\CRT$1999 f( ®x + 3® ); $\C[1.5in]{// rvalue parameter (int) implicitly converts to lvalue temporary reference (int \&)}$ 2000 ®&f®(...) = &x; $\C{// rvalue result (int \&) implicitly converts to lvalue temporary reference (int \&)}\CRT$ 2000 2001 \end{cfa} 2001 2002 In both case, modifications to the temporary are inaccessible (\Index{warning}). … … 2164 2165 2165 2166 2167 \section{Enumeration} 2168 2169 An \newterm{enumeration} is a compile-time mechanism to alias names to constants, like ©typedef© is a mechanism to alias names to types. 2170 Its purpose is to define a restricted-value type providing code-readability and maintenance -- changing an enum's value automatically updates all name usages during compilation. 2171 2172 An enumeration type is a set of names, each called an \newterm{enumeration constant} (shortened to \newterm{enum}) aliased to a fixed value (constant). 2173 \begin{cfa} 2174 enum Days { Mon, Tue, Wed, Thu, Fri, Sat, Sun }; // enumeration type definition, set of 7 names & values 2175 Days days = Mon; // enumeration type declaration and initialization 2176 \end{cfa} 2177 The set of enums are injected into the variable namespace at the definition scope. 2178 Hence, enums may be overloaded with enum/variable/function names. 2179 \begin{cfa} 2180 enum Foo { Bar }; 2181 enum Goo { Bar }; $\C[1.75in]{// overload Foo.Bar}$ 2182 int Foo; $\C{// type/variable separate namespace}$ 2183 double Bar; $\C{// overload Foo.Bar, Goo.Bar}\CRT$ 2184 \end{cfa} 2185 An anonymous enumeration injects enums with specific values into a scope. 2186 \begin{cfa} 2187 enum { Prime = 103, BufferSize = 1024 }; 2188 \end{cfa} 2189 An enumeration is better than using C \Index{preprocessor} or constant declarations. 2190 \begin{cquote} 2191 \begin{tabular}{@{}l@{\hspace{4em}}l@{}} 2192 \begin{cfa} 2193 #define Mon 0 2194 ... 2195 #define Sun 6 2196 \end{cfa} 2197 & 2198 \begin{cfa} 2199 const int Mon = 0, 2200 ..., 2201 Sun = 6; 2202 \end{cfa} 2203 \end{tabular} 2204 \end{cquote} 2205 because the enumeration is succinct, has automatic numbering, can appear in ©case© labels, does not use storage, and is part of the language type-system. 2206 Finally, the type of an enum is implicitly or explicitly specified and the constant value can be implicitly or explicitly specified. 2207 Note, enum values may be repeated in an enumeration. 2208 2209 2210 \subsection{Enum type} 2211 2212 The type of enums can be any type, and an enum's value comes from this type. 2213 Because an enum is a constant, it cannot appear in a mutable context, \eg ©Mon = Sun© is disallowed, and has no address (it is an rvalue). 2214 Therefore, an enum is automatically converted to its constant's base-type, \eg comparing/printing an enum compares/prints its value rather than the enum name; 2215 there is no mechanism to print the enum name. 2216 2217 The default enum type is ©int©. 2218 Hence, ©Days© is the set type ©Mon©, ©Tue©, ...\,, ©Sun©, while the type of each enum is ©int© and each enum represents a fixed integral value. 2219 If no values are specified for an integral enum type, the enums are automatically numbered by one from left to right starting at zero. 2220 Hence, the value of enum ©Mon© is 0, ©Tue© is 1, ...\,, ©Sun© is 6. 2221 If an enum value is specified, numbering continues by one from that value for subsequent unnumbered enums. 2222 If an enum value is an expression, the compiler performs constant-folding to obtain a constant value. 2223 2224 \CFA allows other integral types with associated values. 2225 \begin{cfa} 2226 enum( ®char® ) Letter { A ®= 'A'®, B, C, I ®= 'I'®, J, K }; 2227 enum( ®long long int® ) BigNum { X = 123_456_789_012_345, Y = 345_012_789_456_123 }; 2228 \end{cfa} 2229 For enumeration ©Letter©, enum ©A©'s value is explicitly set to ©'A'©, with ©B© and ©C© implicitly numbered with increasing values from ©'A'©, and similarly for enums ©I©, ©J©, and ©K©. 2230 2231 Non-integral enum types must be explicitly initialized, \eg ©double© is not automatically numbered by one. 2232 \begin{cfa} 2233 // non-integral numeric 2234 enum( ®double® ) Math { PI_2 = 1.570796, PI = 3.141597, E = 2.718282 } 2235 // pointer 2236 enum( ®char *® ) Name { Fred = "Fred", Mary = "Mary", Jane = "Jane" }; 2237 int i, j, k; 2238 enum( ®int *® ) ptr { I = &i, J = &j, K = &k }; 2239 enum( ®int &® ) ref { I = i, J = j, K = k }; 2240 // tuple 2241 enum( ®[int, int]® ) { T = [ 1, 2 ] }; 2242 // function 2243 void f() {...} void g() {...} 2244 enum( ®void (*)()® ) funs { F = f, F = g }; 2245 // aggregate 2246 struct S { int i, j; }; 2247 enum( ®S® ) s { A = { 3, 4 }, B = { 7, 8 } }; 2248 // enumeration 2249 enum( ®Letter® ) Greek { Alph = A, Beta = B, /* more enums */ }; // alphabet intersection 2250 \end{cfa} 2251 Enumeration ©Greek© may have more or less enums than ©Letter©, but the enum values \emph{must} be from ©Letter©. 2252 Therefore, ©Greek© enums are a subset of type ©Letter© and are type compatible with enumeration ©Letter©, but ©Letter© enums are not type compatible with enumeration ©Greek©. 2253 2254 The following examples illustrate the difference between the enumeration type and the type of its enums. 2255 \begin{cfa} 2256 Math m = PI; $\C[1.5in]{// allowed}$ 2257 double d = PI; $\C{// allowed, conversion to base type}$ 2258 m = E; $\C{// allowed}$ 2259 m = Alph; $\C{// {\color{red}disallowed}}$ 2260 m = 3.141597; $\C{// {\color{red}disallowed}}$ 2261 d = m; $\C{// allowed}$ 2262 d = Alph; $\C{// {\color{red}disallowed}}$ 2263 Letter l = A; $\C{// allowed}$ 2264 Greek g = Alph; $\C{// allowed}$ 2265 l = Alph; $\C{// allowed, conversion to base type}$ 2266 g = A; $\C{// {\color{red}disallowed}}\CRT$ 2267 \end{cfa} 2268 2269 A constructor \emph{cannot} be used to initialize enums because a constructor executes at runtime. 2270 A fallback is explicit C-style initialization using ©@=©. 2271 \begin{cfa} 2272 enum( struct vec3 ) Axis { Up @= { 1, 0, 0 }, Left @= { 0, 1, 0 }, Front @= { 0, 0, 1 } } 2273 \end{cfa} 2274 Finally, enumeration variables are assignable and comparable only if the appropriate operators are defined for its enum type. 2275 2276 2277 \subsection{Inheritance} 2278 2279 \Index{Plan-9}\index{inheritance!enumeration} inheritance may be used with enumerations. 2280 \begin{cfa} 2281 enum( char * ) Name2 { ®inline Name®, Jack = "Jack", Jill = "Jill" }; 2282 enum ®/* inferred */® Name3 { ®inline Name2®, Sue = "Sue", Tom = "Tom" }; 2283 \end{cfa} 2284 Enumeration ©Name2© inherits all the enums and their values from enumeration ©Name© by containment, and a ©Name© enumeration is a subtype of enumeration ©Name2©. 2285 Note, enums must be unique in inheritance but enum values may be repeated. 2286 The enum type for the inheriting type must be the same as the inherited type; 2287 hence the enum type may be omitted for the inheriting enumeration and it is inferred from the inherited enumeration, as for ©Name3©. 2288 When inheriting from integral types, automatic numbering may be used, so the inheritance placement left to right is important, \eg the placement of ©Sue© and ©Tom© before or after ©inline Name2©. 2289 2290 Specifically, the inheritance relationship for ©Name©s is: 2291 \begin{cfa} 2292 Name $\(\subseteq\)$ Name2 $\(\subseteq\)$ Name3 $\(\subseteq\)$ const char * // enum type of Name 2293 \end{cfa} 2294 Hence, given 2295 \begin{cfa} 2296 void f( Name ); 2297 void g( Name2 ); 2298 void h( Name3 ); 2299 void j( const char * ); 2300 \end{cfa} 2301 the following calls are valid 2302 \begin{cfa} 2303 f( Fred ); 2304 g( Fred ); g( Jill ); 2305 h( Fred ); h( Jill ); h( Sue ); 2306 j( Fred ); j( Jill ); j( Sue ); j( 'W' ); 2307 \end{cfa} 2308 Note, the validity of calls is the same for call-by-reference as for call-by-value, and ©const© restrictions are the same as for other types. 2309 2310 Enums cannot be created at runtime, so inheritence problems, such as contra-variance do not apply. 2311 Only instances of the enum base-type may be created at runtime. 2312 2313 \begin{comment} 2314 The invariance of references, as I show at the bottom, is easy to overlook. Not shown, but on the same topic, is that returns work in the opposite direction as parameters. Hopefully our existing type rules already know both those facts, so that we'd only have to provide the rules that I suggest using the by-value parameters. 2315 2316 The Fred, Jack, and Mary declarations are picked verbatim from our earlier whiteboard, just repeated here for reference. 2317 2318 \begin{cfa} 2319 // Fred is a subset of char * 2320 enum( char *) Fred { A = "A", B = "B", C = "C" }; 2321 // Jack is a subset of Fred 2322 enum( enum Fred ) Jack { W = A, Y = C}; 2323 // Mary is a superset of Fred 2324 enum Mary { inline Fred, D = "hello" }; 2325 2326 // Demonstrating invariance of references 2327 2328 [void] frcs( & * char x ) { char * x0 = x; x = "bye"; } 2329 [void] frf ( & Fred x ) { Fred x0 = x; x = B; } 2330 [void] frj ( & Jack x ) { Jack x0 = x; x = W; } 2331 [void] frm ( & Mary x ) { Mary x0 = x; x = D; } 2332 2333 char * vcs; 2334 Fred vf; 2335 Jack vj; 2336 Mary vm; 2337 2338 // all variant calls: bad (here are noteworthy examples) 2339 frcs( vf ); // can't assign "bye" to vf 2340 frm ( vf ); // can't assign D to vf 2341 frf ( vj ); // can't assign B to vj 2342 vf = B ; frj ( vf ); // can't assign B to frj.x0 2343 vcs = "bye"; frf ( vcs ); // can't assign "bye" to frf.x0 2344 \end{cfa} 2345 2346 This example is really great. However, I think it's work explicitly doing one with ©const &©. 2347 \end{comment} 2348 2349 2166 2350 \section{Routine Definition} 2167 2351 2168 \CFA alsosupports a new syntax for routine definition, as well as \Celeven and K\&R routine syntax.2352 \CFA supports a new syntax for routine definition, as well as \Celeven and K\&R routine syntax. 2169 2353 The point of the new syntax is to allow returning multiple values from a routine~\cite{Galletly96,CLU}, \eg: 2170 2354 \begin{cfa} 2171 @[ int o1, int o2, char o3 ]@f( int i1, char i2, char i3 ) {2355 ®[ int o1, int o2, char o3 ]® f( int i1, char i2, char i3 ) { 2172 2356 $\emph{routine body}$ 2173 2357 } 2174 2358 \end{cfa} 2175 2359 where routine ©f© has three output (return values) and three input parameters. 2176 Existing C syntax cannot be extended with multiple return types because it is impossible to embed a single routine name within multiple return type specifications.2360 Existing C syntax cannot be extended with multiple return types because it is impossible to embed a single routine name within multiple return type-specifications. 2177 2361 2178 2362 In detail, the brackets, ©[]©, enclose the result type, where each return value is named and that name is a local variable of the particular return type.\footnote{ … … 2181 2365 Declaration qualifiers can only appear at the start of a routine definition, \eg: 2182 2366 \begin{cfa} 2183 @extern@[ int x ] g( int y ) {$\,$}2367 ®extern® [ int x ] g( int y ) {$\,$} 2184 2368 \end{cfa} 2185 2369 Lastly, if there are no output parameters or input parameters, the brackets and/or parentheses must still be specified; … … 2200 2384 int (*f(x))[ 5 ] int x; {} 2201 2385 \end{cfa} 2202 The string ``©int (*f(x))[ 5 ]©'' declares a K\&R style routine of type returning a pointer to an array of 5 integers, while the string ``©[ 5 ] int x©'' declares a \CFA style parameter xof type array of 5 integers.2386 The string ``©int (*f(x))[ 5 ]©'' declares a K\&R style routine of type returning a pointer to an array of 5 integers, while the string ``©[ 5 ] int x©'' declares a \CFA style parameter ©x© of type array of 5 integers. 2203 2387 Since the strings overlap starting with the open bracket, ©[©, there is an ambiguous interpretation for the string. 2204 2388 As well, \CFA-style declarations cannot be used to declare parameters for C-style routine-definitions because of the following ambiguity: … … 2239 2423 \begin{minipage}{\linewidth} 2240 2424 \begin{cfa} 2241 @[ int x, int y ]@f() {2425 ®[ int x, int y ]® f() { 2242 2426 int z; 2243 2427 ... x = 0; ... y = z; ... 2244 @return;@$\C{// implicitly return x, y}$2428 ®return;® $\C{// implicitly return x, y}$ 2245 2429 } 2246 2430 \end{cfa} … … 2574 2758 2575 2759 int fred() { 2576 s.t.c = @S.@R; // type qualification2577 struct @S.@T t = { @S.@R, 1, 2 };2578 enum @S.@C c;2579 union @S.T.@U u;2760 s.t.c = ®S.®R; // type qualification 2761 struct ®S.®T t = { ®S.®R, 1, 2 }; 2762 enum ®S.®C c; 2763 union ®S.T.®U u; 2580 2764 } 2581 2765 \end{cfa} … … 2603 2787 qsort( ia, size ); $\C{// sort ascending order using builtin ?<?}$ 2604 2788 { 2605 @int ?<?( int x, int y ) { return x > y; }@$\C{// nested routine}$2789 ®int ?<?( int x, int y ) { return x > y; }® $\C{// nested routine}$ 2606 2790 qsort( ia, size ); $\C{// sort descending order by local redefinition}$ 2607 2791 } … … 2613 2797 \begin{cfa} 2614 2798 [* [int]( int )] foo() { $\C{// int (* foo())( int )}$ 2615 int @i@= 7;2799 int ®i® = 7; 2616 2800 int bar( int p ) { 2617 @i@+= 1; $\C{// dependent on local variable}$2618 sout | @i@;2801 ®i® += 1; $\C{// dependent on local variable}$ 2802 sout | ®i®; 2619 2803 } 2620 2804 return bar; $\C{// undefined because of local dependence}$ … … 2634 2818 In C and \CFA, lists of elements appear in several contexts, such as the parameter list of a routine call. 2635 2819 \begin{cfa} 2636 f( @2, x, 3 + i@); $\C{// element list}$2820 f( ®2, x, 3 + i® ); $\C{// element list}$ 2637 2821 \end{cfa} 2638 2822 A list of elements is called a \newterm{tuple}, and is different from a \Index{comma expression}. … … 2747 2931 In \CFA, it is possible to overcome this restriction by declaring a \newterm{tuple variable}. 2748 2932 \begin{cfa} 2749 [int, int] @qr@= div( 13, 5 ); $\C{// initialize tuple variable}$2750 printf( "%d %d\n", @qr@); $\C{// print quotient/remainder}$2933 [int, int] ®qr® = div( 13, 5 ); $\C{// initialize tuple variable}$ 2934 printf( "%d %d\n", ®qr® ); $\C{// print quotient/remainder}$ 2751 2935 \end{cfa} 2752 2936 It is now possible to match the multiple return-values to a single variable, in much the same way as \Index{aggregation}. … … 3412 3596 \begin{cfa} 3413 3597 int x = 1, y = 2, z = 3; 3414 sout | x @|@ y @|@z;3598 sout | x ®|® y ®|® z; 3415 3599 \end{cfa} 3416 3600 & 3417 3601 \begin{cfa} 3418 3602 3419 cout << x @<< " "@ << y @<< " "@<< z << endl;3603 cout << x ®<< " "® << y ®<< " "® << z << endl; 3420 3604 \end{cfa} 3421 3605 & … … 3426 3610 \\ 3427 3611 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3428 1 @ @2@ @33612 1® ®2® ®3 3429 3613 \end{cfa} 3430 3614 & 3431 3615 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3432 1 @ @2@ @33616 1® ®2® ®3 3433 3617 \end{cfa} 3434 3618 & 3435 3619 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3436 1 @ @2@ @33620 1® ®2® ®3 3437 3621 \end{cfa} 3438 3622 \end{tabular} 3439 3623 \end{cquote} 3440 3624 The \CFA form has half the characters of the \CC form, and is similar to \Index*{Python} I/O with respect to implicit separators and newline. 3441 Similar simplification occurs for \Index{tuple} I/O, which flattens the tuple and prints each value separated by ``\lstinline[showspaces=true] @, @''.3625 Similar simplification occurs for \Index{tuple} I/O, which flattens the tuple and prints each value separated by ``\lstinline[showspaces=true]{, }''. 3442 3626 \begin{cfa} 3443 3627 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ]; … … 3445 3629 \end{cfa} 3446 3630 \begin{cfa}[showspaces=true,aboveskip=0pt] 3447 1 @, @2@, @3 4@, @5@, @63631 1®, ®2®, ®3 4®, ®5®, ®6 3448 3632 \end{cfa} 3449 3633 Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority \emph{overloadable} operator, other than assignment. … … 3454 3638 & 3455 3639 \begin{cfa} 3456 sout | x * 3 | y + 1 | z << 2 | x == y | @(@x | y@)@ | @(@x || y@)@ | @(@x > z ? 1 : 2@)@;3640 sout | x * 3 | y + 1 | z << 2 | x == y | ®(®x | y®)® | ®(®x || y®)® | ®(®x > z ? 1 : 2®)®; 3457 3641 \end{cfa} 3458 3642 \\ … … 3460 3644 & 3461 3645 \begin{cfa} 3462 cout << x * 3 << y + 1 << @(@z << 2@)@ << @(@x == y@)@ << @(@x | y@)@ << @(@x || y@)@ << @(@x > z ? 1 : 2@)@<< endl;3646 cout << x * 3 << y + 1 << ®(®z << 2®)® << ®(®x == y®)® << ®(®x | y®)® << ®(®x || y®)® << ®(®x > z ? 1 : 2®)® << endl; 3463 3647 \end{cfa} 3464 3648 \\ … … 3495 3679 \\ 3496 3680 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3497 @1@ @2.5@ @A@ 3681 ®1® ®2.5® ®A® 3498 3682 3499 3683 … … 3501 3685 & 3502 3686 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3503 @1@ @2.5@ @A@ 3687 ®1® ®2.5® ®A® 3504 3688 3505 3689 … … 3507 3691 & 3508 3692 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3509 @1@ 3510 @2.5@ 3511 @A@ 3693 ®1® 3694 ®2.5® 3695 ®A® 3512 3696 \end{cfa} 3513 3697 \end{tabular} … … 3545 3729 3546 3730 \item 3547 A separator does not appear before a C string starting with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \LstStringStyle{,.;!?)]\}\%\textcent\guillemotright}, where \LstStringStyle{\guillemotright} a closing citation mark.3731 A separator does not appear before a C string starting with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \LstStringStyle{,.;!?)]\}\%\textcent\guillemotright}, where \LstStringStyle{\guillemotright} is a closing citation mark. 3548 3732 \begin{cfa} 3549 3733 sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x" … … 3551 3735 \end{cfa} 3552 3736 \begin{cfa}[showspaces=true] 3553 1 @,@ x 2@.@ x 3@;@ x 4@!@ x 5@?@ x 6@%@ x 7$\R{\LstStringStyle{\textcent}}$ x 8$\R{\LstStringStyle{\guillemotright}}$ x 9@)@ x 10@]@ x 11@}@x3737 1®,® x 2®.® x 3®;® x 4®!® x 5®?® x 6®%® x 7$\R{\LstStringStyle{\textcent}}$ x 8$\R{\LstStringStyle{\guillemotright}}$ x 9®)® x 10®]® x 11®}® x 3554 3738 \end{cfa} 3555 3739 … … 3558 3742 %$ 3559 3743 \begin{cfa} 3560 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $ " | 5 | "x $\LstStringStyle{\textsterling}$" | 6 | "x $\LstStringStyle{\textyen}$"3744 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $\LstStringStyle{\textdollar}$" | 5 | "x $\LstStringStyle{\textsterling}$" | 6 | "x $\LstStringStyle{\textyen}$" 3561 3745 | 7 | "x $\LstStringStyle{\textexclamdown}$" | 8 | "x $\LstStringStyle{\textquestiondown}$" | 9 | "x $\LstStringStyle{\guillemotleft}$" | 10; 3562 3746 \end{cfa} 3563 3747 %$ 3564 3748 \begin{cfa}[showspaces=true] 3565 x @(@1 x @[@2 x @{@3 x @=@4 x $\LstStringStyle{\textdollar}$5 x $\R{\LstStringStyle{\textsterling}}$6 x $\R{\LstStringStyle{\textyen}}$7 x $\R{\LstStringStyle{\textexclamdown}}$8 x $\R{\LstStringStyle{\textquestiondown}}$9 x $\R{\LstStringStyle{\guillemotleft}}$103749 x ®(®1 x ®[®2 x ®{®3 x ®=®4 x $\LstStringStyle{\textdollar}$5 x $\R{\LstStringStyle{\textsterling}}$6 x $\R{\LstStringStyle{\textyen}}$7 x $\R{\LstStringStyle{\textexclamdown}}$8 x $\R{\LstStringStyle{\textquestiondown}}$9 x $\R{\LstStringStyle{\guillemotleft}}$10 3566 3750 \end{cfa} 3567 3751 %$ 3568 3752 3569 3753 \item 3570 A sep erator does not appear before/after a C string starting/ending with the \Index*{ASCII} quote or whitespace characters: \lstinline[basicstyle=\tt,showspaces=true]{`'": \t\v\f\r\n}3754 A separator does not appear before/after a C string starting/ending with the \Index*{ASCII} quote or whitespace characters: \lstinline[basicstyle=\tt,showspaces=true]{`'": \t\v\f\r\n} 3571 3755 \begin{cfa} 3572 3756 sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx"; 3573 3757 \end{cfa} 3574 3758 \begin{cfa}[showspaces=true,showtabs=true] 3575 x @`@1@`@x$\R{\texttt{'}}$2$\R{\texttt{'}}$x$\R{\texttt{"}}$3$\R{\texttt{"}}$x@:@4@:@x@ @5@ @x@ @6@ @x3759 x®`®1®`®x$\R{\texttt{'}}$2$\R{\texttt{'}}$x$\R{\texttt{"}}$3$\R{\texttt{"}}$x®:®4®:®x® ®5® ®x® ®6® ®x 3576 3760 \end{cfa} 3577 3761 … … 3582 3766 \end{cfa} 3583 3767 \begin{cfa}[showspaces=true,showtabs=true] 3584 x ( @ @1@ @) x 2@ @, x 3@ @:x:@ @43768 x (® ®1® ®) x 2® ®, x 3® ®:x:® ®4 3585 3769 \end{cfa} 3586 3770 \end{enumerate} … … 3595 3779 \Indexc{sepSet}\index{manipulator!sepSet@©sepSet©} and \Indexc{sep}\index{manipulator!sep@©sep©}/\Indexc{sepGet}\index{manipulator!sepGet@©sepGet©} set and get the separator string. 3596 3780 The separator string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 3597 \begin{cfa}[escapechar=off,belowskip=0pt] 3598 sepSet( sout, ", $" ); $\C{// set separator from " " to ", \$"}$ 3599 sout | 1 | 2 | 3 | " \"" | @sep@ | "\""; 3600 \end{cfa} 3601 %$ 3602 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt] 3603 1@, $@2@, $@3 @", $"@ 3604 \end{cfa} 3605 %$ 3781 \begin{cfa}[belowskip=0pt] 3782 sepSet( sout, ", $\LstStringStyle{\textdollar}$" ); $\C{// set separator from " " to ", \$"}$ 3783 sout | 1 | 2 | 3 | " \"" | ®sep® | "\""; 3784 \end{cfa} 3785 \begin{cfa}[showspaces=true,aboveskip=0pt] 3786 1®, $\LstStringStyle{\textdollar}$®2®, $\LstStringStyle{\textdollar}$®3 ®", $\LstStringStyle{\textdollar}$"® 3787 \end{cfa} 3606 3788 \begin{cfa}[belowskip=0pt] 3607 3789 sepSet( sout, " " ); $\C{// reset separator to " "}$ 3608 sout | 1 | 2 | 3 | " \"" | @sepGet( sout )@| "\"";3790 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )® | "\""; 3609 3791 \end{cfa} 3610 3792 \begin{cfa}[showspaces=true,aboveskip=0pt] 3611 1 @ @2@ @3 @" "@3793 1® ®2® ®3 ®" "® 3612 3794 \end{cfa} 3613 3795 ©sepGet© can be used to store a separator and then restore it: 3614 3796 \begin{cfa}[belowskip=0pt] 3615 char store[ @sepSize@]; $\C{// sepSize is the maximum separator size}$3797 char store[®sepSize®]; $\C{// sepSize is the maximum separator size}$ 3616 3798 strcpy( store, sepGet( sout ) ); $\C{// copy current separator}$ 3617 3799 sepSet( sout, "_" ); $\C{// change separator to underscore}$ … … 3619 3801 \end{cfa} 3620 3802 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3621 1 @_@2@_@33803 1®_®2®_®3 3622 3804 \end{cfa} 3623 3805 \begin{cfa}[belowskip=0pt] … … 3626 3808 \end{cfa} 3627 3809 \begin{cfa}[showspaces=true,aboveskip=0pt] 3628 1 @ @2@ @33810 1® ®2® ®3 3629 3811 \end{cfa} 3630 3812 … … 3634 3816 \begin{cfa}[belowskip=0pt] 3635 3817 sepSetTuple( sout, " " ); $\C{// set tuple separator from ", " to " "}$ 3636 sout | t1 | t2 | " \"" | @sepTuple@| "\"";3818 sout | t1 | t2 | " \"" | ®sepTuple® | "\""; 3637 3819 \end{cfa} 3638 3820 \begin{cfa}[showspaces=true,aboveskip=0pt] 3639 1 2 3 4 5 6 @" "@3821 1 2 3 4 5 6 ®" "® 3640 3822 \end{cfa} 3641 3823 \begin{cfa}[belowskip=0pt] 3642 3824 sepSetTuple( sout, ", " ); $\C{// reset tuple separator to ", "}$ 3643 sout | t1 | t2 | " \"" | @sepGetTuple( sout )@| "\"";3825 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\""; 3644 3826 \end{cfa} 3645 3827 \begin{cfa}[showspaces=true,aboveskip=0pt] 3646 1, 2, 3 4, 5, 6 @", "@3828 1, 2, 3 4, 5, 6 ®", "® 3647 3829 \end{cfa} 3648 3830 As for ©sepGet©, ©sepGetTuple© can be use to store a tuple separator and then restore it. … … 3696 3878 \end{cfa} 3697 3879 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3698 @ @1 2 3@ @ 3880 ® ®1 2 3® ® 3699 3881 \end{cfa} 3700 3882 \end{enumerate} … … 3716 3898 For example, in: 3717 3899 \begin{cfa} 3718 sin | i | @nl@| j;3719 1 @2@3900 sin | i | ®nl® | j; 3901 1 ®2® 3720 3902 3 3721 3903 \end{cfa} … … 3758 3940 0b0 0b11011 0b11011 0b11011 0b11011 3759 3941 sout | bin( -27HH ) | bin( -27H ) | bin( -27 ) | bin( -27L ); 3760 0b11100101 0b1111111111100101 0b11111111111111111111111111100101 0b @(58 1s)@1001013942 0b11100101 0b1111111111100101 0b11111111111111111111111111100101 0b®(58 1s)®100101 3761 3943 \end{cfa} 3762 3944 … … 3794 3976 3795 3977 \item 3978 \Indexc{eng}( floating-point )\index{manipulator!eng@©eng©} print value in engineering notation with exponent, which means the exponent is adjusted to a multiple of 3. 3979 \begin{cfa}[belowskip=0pt] 3980 sout | eng( 0.0 ) | eng( 27000.5 ) | eng( -27.5e7 ); 3981 0®e0® 27.0005®e3® -275®e6® 3982 \end{cfa} 3983 3984 \item 3985 \Indexc{unit}( engineering-notation )\index{manipulator!unit@©unit©} print engineering exponent as a letter between the range $10^{-24}$ and $10^{24}$: 3986 ©y© $\Rightarrow 10^{-24}$, ©z© $\Rightarrow 10^{-21}$, ©a© $\Rightarrow 10^{-18}$, ©f© $\Rightarrow 10^{-15}$, ©p© $\Rightarrow 10^{-12}$, ©n© $\Rightarrow 10^{-9}$, ©u© $\Rightarrow 10^{-6}$, ©m© $\Rightarrow 10^{-3}$, ©K© $\Rightarrow 10^{3}$, ©M© $\Rightarrow 10^{6}$, ©G© $\Rightarrow 10^{9}$, ©T© $\Rightarrow 10^{12}$, ©P© $\Rightarrow 10^{15}$, ©E© $\Rightarrow 10^{18}$, ©Z© $\Rightarrow 10^{21}$, ©Y© $\Rightarrow 10^{24}$. 3987 For exponent $10^{0}$, no decimal point or letter is printed. 3988 \begin{cfa}[belowskip=0pt] 3989 sout | unit(eng( 0.0 )) | unit(eng( 27000.5 )) | unit(eng( -27.5e7 )); 3990 0 27.0005®K® -275®M® 3991 \end{cfa} 3992 3993 \item 3796 3994 \Indexc{upcase}( bin / hex / floating-point )\index{manipulator!upcase@©upcase©} print letters in a value in upper case. Lower case is the default. 3797 3995 \begin{cfa}[belowskip=0pt] 3798 3996 sout | upcase( bin( 27 ) ) | upcase( hex( 27 ) ) | upcase( 27.5e-10 ) | upcase( hex( 27.5 ) ); 3799 0 @B@11011 0@X@1@B@ 2.75@E@-09 0@X@1.@B@8@P@+43997 0®B®11011 0®X®1®B® 2.75®E®-09 0®X®1.®B®8®P®+4 3800 3998 \end{cfa} 3801 3999 … … 3813 4011 \begin{cfa}[belowskip=0pt] 3814 4012 sout | 0. | nodp( 0. ) | 27.0 | nodp( 27.0 ) | nodp( 27.5 ); 3815 0.0 @0@ 27.0 @27@27.54013 0.0 ®0® 27.0 ®27® 27.5 3816 4014 \end{cfa} 3817 4015 … … 3820 4018 \begin{cfa}[belowskip=0pt] 3821 4019 sout | sign( 27 ) | sign( -27 ) | sign( 27. ) | sign( -27. ) | sign( 27.5 ) | sign( -27.5 ); 3822 @+@27 -27 @+@27.0 -27.0 @+@27.5 -27.53823 \end{cfa} 3824 3825 \item 3826 \Indexc{wd} ©( unsigned char minimum, T val )©\index{manipulator!wd@©wd©}, ©wd( unsigned char minimum, unsigned char precision, T val )©3827 For all types, ©minimum© is the minimumnumber of printed characters.4020 ®+®27 -27 ®+®27.0 -27.0 ®+®27.5 -27.5 4021 \end{cfa} 4022 4023 \item 4024 \Indexc{wd}( minimum, value )\index{manipulator!wd@©wd©}, ©wd©( minimum, precision, value ) 4025 For all types, minimum is the number of printed characters. 3828 4026 If the value is shorter than the minimum, it is padded on the right with spaces. 3829 4027 \begin{cfa}[belowskip=0pt] … … 3833 4031 \end{cfa} 3834 4032 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3835 @ @34 @ @34 343836 @ @4.000000 @ @4.000000 4.0000003837 @ @ab @ @ab ab3838 \end{cfa} 3839 If the value is larger, it is printed without truncation, ignoring the ©minimum©.4033 ® ®34 ® ®34 34 4034 ® ®4.000000 ® ®4.000000 4.000000 4035 ® ®ab ® ®ab ab 4036 \end{cfa} 4037 If the value is larger, it is printed without truncation, ignoring the minimum. 3840 4038 \begin{cfa}[belowskip=0pt] 3841 4039 sout | wd( 4, 34567 ) | wd( 3, 34567 ) | wd( 2, 34567 ); … … 3844 4042 \end{cfa} 3845 4043 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3846 3456 @7@ 345@67@ 34@567@3847 3456 @.@ 345@6.@ 34@56.@3848 abcd @e@ abc@de@ ab@cde@3849 \end{cfa} 3850 3851 For integer types, ©precision©is the minimum number of printed digits.4044 3456®7® 345®67® 34®567® 4045 3456®.® 345®6.® 34®56.® 4046 abcd®e® abc®de® ab®cde® 4047 \end{cfa} 4048 4049 For integer types, precision is the minimum number of printed digits. 3852 4050 If the value is shorter, it is padded on the left with leading zeros. 3853 4051 \begin{cfa}[belowskip=0pt] … … 3855 4053 \end{cfa} 3856 4054 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3857 @0@34 @00@34 @00000000@343858 \end{cfa} 3859 If the value is larger, it is printed without truncation, ignoring the ©precision©.4055 ®0®34 ®00®34 ®00000000®34 4056 \end{cfa} 4057 If the value is larger, it is printed without truncation, ignoring the precision. 3860 4058 \begin{cfa}[belowskip=0pt] 3861 4059 sout | wd( 4,1, 3456 ) | wd( 8,2, 3456 ) | wd( 10,3, 3456 ); … … 3864 4062 3456 3456 3456 3865 4063 \end{cfa} 3866 If ©precision©is 0, nothing is printed for zero.3867 If ©precision©is greater than the minimum, it becomes the minimum.4064 If precision is 0, nothing is printed for zero. 4065 If precision is greater than the minimum, it becomes the minimum. 3868 4066 \begin{cfa}[belowskip=0pt] 3869 4067 sout | wd( 4,0, 0 ) | wd( 3,10, 34 ); 3870 4068 \end{cfa} 3871 4069 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3872 @ @ @00000000@343873 \end{cfa} 3874 For floating-point types, ©precision©is the minimum number of digits after the decimal point.4070 ® ® ®00000000®34 4071 \end{cfa} 4072 For floating-point types, precision is the minimum number of digits after the decimal point. 3875 4073 \begin{cfa}[belowskip=0pt] 3876 4074 sout | wd( 6,3, 27.5 ) | wd( 8,1, 27.5 ) | wd( 8,0, 27.5 ) | wd( 3,8, 27.5 ); 3877 4075 \end{cfa} 3878 4076 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3879 27. @500@ 27.@5@ 28. 27.@50000000@3880 \end{cfa} 3881 For the C-string type, ©precision©is the maximum number of printed characters, so the string is truncated if it exceeds the maximum.4077 27.®500® 27.®5® 28. 27.®50000000® 4078 \end{cfa} 4079 For the C-string type, precision is the maximum number of printed characters, so the string is truncated if it exceeds the maximum. 3882 4080 \begin{cfa}[belowskip=0pt] 3883 4081 sout | wd( 6,8, "abcd" ) | wd( 6,8, "abcdefghijk" ) | wd( 6,3, "abcd" ); … … 3888 4086 3889 4087 \item 3890 \Indexc{ws( unsigned char minimum, unsigned char significant, floating-point )}\index{manipulator!ws@©ws©} 3891 For floating-point type, ©minimum© is the same as for manipulator ©wd©, but ©significant© is the maximum number of significant digits to be printed for both the integer and fractions (versus only the fraction for ©wd©). 3892 If a value's significant digits is greater than ©significant©, the last significant digit is rounded up. 4088 \begin{sloppypar} 4089 \Indexc{ws}( minimum, significant, floating-point )\index{manipulator!ws@©ws©} 4090 For floating-point types, minimum is the same as for manipulator ©wd©, but significant is the maximum number of significant digits to be printed for both the integer and fractions (versus only the fraction for ©wd©). 4091 If a value's significant digits is greater than significant, the last significant digit is rounded up. 4092 \end{sloppypar} 3893 4093 \begin{cfa}[belowskip=0pt] 3894 4094 sout | ws(6,6, 234.567) | ws(6,5, 234.567) | ws(6,4, 234.567) | ws(6,3, 234.567); 3895 4095 \end{cfa} 3896 4096 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3897 234.567 234.5 @7@ 234.@6@ 23@5@3898 \end{cfa} 3899 If a value's magnitude is greater than ©significant©, the value is printed in scientific notation with the specified number of significant digits.4097 234.567 234.5®7® 234.®6® 23®5® 4098 \end{cfa} 4099 If a value's magnitude is greater than significant, the value is printed in scientific notation with the specified number of significant digits. 3900 4100 \begin{cfa}[belowskip=0pt] 3901 4101 sout | ws(6,6, 234567.) | ws(6,5, 234567.) | ws(6,4, 234567.) | ws(6,3, 234567.); 3902 4102 \end{cfa} 3903 4103 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3904 234567. 2.3457 @e+05@ 2.346@e+05@ 2.35@e+05@3905 \end{cfa} 3906 If ©significant© is greater than ©minimum©, it defines the number of printed characters.4104 234567. 2.3457®e+05® 2.346®e+05® 2.35®e+05® 4105 \end{cfa} 4106 If significant is greater than minimum, it defines the number of printed characters. 3907 4107 \begin{cfa}[belowskip=0pt] 3908 4108 sout | ws(3,6, 234567.) | ws(4,6, 234567.) | ws(5,6, 234567.) | ws(6,6, 234567.); … … 3918 4118 \end{cfa} 3919 4119 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3920 27 @ @ 27.000000 27.500000 027 27.500@ @4120 27® ® 27.000000 27.500000 027 27.500® ® 3921 4121 \end{cfa} 3922 4122 … … 3925 4125 \begin{cfa}[belowskip=0pt] 3926 4126 sout | pad0( wd( 4, 27 ) ) | pad0( wd( 4,3, 27 ) ) | pad0( wd( 8,3, 27.5 ) ); 3927 @00@27 @0@27 @00@27.5004127 ®00®27 ®0®27 ®00®27.500 3928 4128 \end{cfa} 3929 4129 \end{enumerate} … … 3989 4189 3990 4190 The format of numeric input values in the same as C constants without a trailing type suffix, as the input value-type is denoted by the input variable. 3991 For © _Bool© type, the constants are ©true© and ©false©.4191 For ©bool© type, the constants are ©true© and ©false©. 3992 4192 For integral types, any number of digits, optionally preceded by a sign (©+© or ©-©), where a 3993 4193 \begin{itemize} … … 4010 4210 \begin{enumerate} 4011 4211 \item 4012 \Indexc{skip( const char * pattern )}\index{manipulator!skip@©skip©} / ©skip( unsigned int length )© / ©const char * pattern© 4013 The argument defines a ©pattern© or ©length©. 4014 The ©pattern© is composed of white-space and non-white-space characters, where \emph{any} white-space character matches 0 or more input white-space characters (hence, consecutive white-space characters in the pattern are combined), and each non-white-space character matches exactly with an input character. 4015 The ©length© is composed of the next $N$ characters, including the newline character. 4212 \Indexc{skip}( pattern )\index{manipulator!skip@©skip©}, ©skip©( length ) 4213 The pattern is composed of white-space and non-white-space characters, where \emph{any} white-space character matches 0 or more input white-space characters (hence, consecutive white-space characters in the pattern are combined), and each non-white-space character matches exactly with an input character. 4214 The length is composed of the next $N$ characters, including the newline character. 4016 4215 If the match successes, the input characters are discarded, and input continues with the next character. 4017 4216 If the match fails, the input characters are left unread. … … 4021 4220 \end{cfa} 4022 4221 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4023 @abc @ 4024 @abc @ 4025 @xx@ 4026 \end{cfa} 4027 4028 \item 4029 \Indexc{wdi} ©( unsigned int maximum, T & val )©\index{manipulator!wdi@©wdi©}4030 For all types except ©char©, ©maximum©is the maximum number of characters read for the current operation.4222 ®abc ® 4223 ®abc ® 4224 ®xx® 4225 \end{cfa} 4226 4227 \item 4228 \Indexc{wdi}( maximum, reference-value )\index{manipulator!wdi@©wdi©} 4229 For all types except ©char©, maximum is the maximum number of characters read for the current operation. 4031 4230 \begin{cfa}[belowskip=0pt] 4032 4231 char s[10]; int i; double d; … … 4034 4233 \end{cfa} 4035 4234 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4036 @abcd1233.456E+2@ 4235 ®abcd1233.456E+2® 4037 4236 \end{cfa} 4038 4237 Note, input ©wdi© cannot be overloaded with output ©wd© because both have the same parameters but return different types. … … 4040 4239 4041 4240 \item 4042 \Indexc{ignore ( T & val )}\index{manipulator!ignore@©ignore©}4241 \Indexc{ignore}( reference-value )\index{manipulator!ignore@©ignore©} 4043 4242 For all types, the data is read from the stream depending on the argument type but ignored, \ie it is not stored in the argument. 4044 4243 \begin{cfa}[belowskip=0pt] … … 4047 4246 \end{cfa} 4048 4247 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4049 @ -75.35e-4@254050 \end{cfa} 4051 4052 \item 4053 \Indexc{incl ( const char * scanset, char * s )}\index{manipulator!incl@©incl©}4054 For the C-string type, the argument defines a ©scanset© that matches any number of characters \emph{in} the set.4055 Matching characters are read into the C string and null terminated.4248 ® -75.35e-4® 25 4249 \end{cfa} 4250 4251 \item 4252 \Indexc{incl}( scanset, input-string )\index{manipulator!incl@©incl©} 4253 For C-string types, the scanset matches any number of characters \emph{in} the set. 4254 Matching characters are read into the C input-string and null terminated. 4056 4255 \begin{cfa}[belowskip=0pt] 4057 4256 char s[10]; … … 4059 4258 \end{cfa} 4060 4259 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4061 @bca@xyz4062 \end{cfa} 4063 4064 \item 4065 \Indexc{excl ( const char * scanset, char * s )}\index{manipulator!excl@©excl©}4066 For the C-string type, the argument defines a ©scanset© that matches any number of characters \emph{not in} the set.4067 Non-matching characters are read into the C string and null terminated.4260 ®bca®xyz 4261 \end{cfa} 4262 4263 \item 4264 \Indexc{excl}( scanset, input-string )\index{manipulator!excl@©excl©} 4265 For C-string types, the scanset matches any number of characters \emph{not in} the set. 4266 Non-matching characters are read into the C input-string and null terminated. 4068 4267 \begin{cfa}[belowskip=0pt] 4069 4268 char s[10]; … … 4071 4270 \end{cfa} 4072 4271 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4073 @xyz@bca4272 ®xyz®bca 4074 4273 \end{cfa} 4075 4274 \end{enumerate} 4076 4275 4077 4276 4277 \subsection{Concurrent Stream Access} 4278 4279 When a stream is shared by multiple threads, input or output characters can be intermixed or cause failure. 4280 For example, if two threads execute the following: 4281 \begin{cfa} 4282 $\emph{thread\(_1\)}$ : sout | "abc " | "def "; 4283 $\emph{thread\(_2\)}$ : sout | "uvw " | "xyz "; 4284 \end{cfa} 4285 possible outputs are: 4286 \begin{cquote} 4287 \begin{tabular}{@{}l|l|l|l|l@{}} 4288 \begin{cfa} 4289 abc def 4290 uvw xyz 4291 \end{cfa} 4292 & 4293 \begin{cfa} 4294 abc uvw xyz 4295 def 4296 \end{cfa} 4297 & 4298 \begin{cfa} 4299 uvw abc xyz def 4300 4301 \end{cfa} 4302 & 4303 \begin{cfa} 4304 abuvwc dexf 4305 yz 4306 \end{cfa} 4307 & 4308 \begin{cfa} 4309 uvw abc def 4310 xyz 4311 \end{cfa} 4312 \end{tabular} 4313 \end{cquote} 4314 Concurrent operations can even corrupt the internal state of the stream resulting in failure. 4315 As a result, some form of mutual exclusion is required for concurrent stream access. 4316 4317 A coarse-grained solution is to perform all stream operations via a single thread or within a monitor providing the necessary mutual exclusion for the stream. 4318 A fine-grained solution is to have a lock for each stream, which is acquired and released around stream operations by each thread. 4319 \CFA provides a fine-grained solution where a \Index{recursive lock} is acquired and released indirectly via a manipulator ©acquire© or instantiating an \Index{RAII} type specific for the kind of stream: ©osacquire©\index{ostream@©ostream©!osacquire@©osacquire©} for output streams and ©isacquire©\index{isacquire@©isacquire©}\index{istream@©istream©!isacquire@©isacquire©} for input streams. 4320 4321 The common usage is manipulator ©acquire©\index{ostream@©ostream©!acquire@©acquire©} to lock a stream during a single cascaded I/O expression, with the manipulator appearing as the first item in a cascade list, \eg: 4322 \begin{cfa} 4323 $\emph{thread\(_1\)}$ : sout | ®acquire® | "abc " | "def "; // manipulator 4324 $\emph{thread\(_2\)}$ : sout | ®acquire® | "uvw " | "xyz "; 4325 \end{cfa} 4326 Now, the order of the thread execution is still non-deterministic, but the output is constrained to two possible lines in either order. 4327 \begin{cquote} 4328 \def\VRfont{\fontfamily{pcr}\upshape\selectfont} 4329 \begin{tabular}{@{}l|l@{}} 4330 \begin{cfa} 4331 abc def 4332 uvw xyz 4333 \end{cfa} 4334 & 4335 \begin{cfa} 4336 uvw xyz 4337 abc def 4338 \end{cfa} 4339 \end{tabular} 4340 \end{cquote} 4341 In summary, the stream lock is acquired by the ©acquire© manipulator and implicitly released at the end of the cascaded I/O expression ensuring all operations in the expression occur atomically. 4342 4343 To lock a stream across multiple I/O operations, an object of type ©osacquire© or ©isacquire© is declared to implicitly acquire/release the stream lock providing mutual exclusion for the object's duration, \eg: 4344 \begin{cfa} 4345 { // acquire sout for block duration 4346 ®osacquire® acq = { sout }; $\C{// named stream locker}$ 4347 sout | 1; 4348 sout | ®acquire® | 2 | 3; $\C{// unnecessary, but ok to acquire and release again}$ 4349 sout | 4; 4350 } // implicitly release the lock when "acq" is deallocated 4351 \end{cfa} 4352 Note, the unnecessary ©acquire© manipulator works because the recursive stream-lock can be acquired/released multiple times by the owner thread. 4353 Hence, calls to functions that also acquire a stream lock for their output do not result in \Index{deadlock}. 4354 4355 The previous values written by threads 1 and 2 can be read in concurrently: 4356 \begin{cfa} 4357 { // acquire sin lock for block duration 4358 ®isacquire acq = { sin };® $\C{// named stream locker}$ 4359 int x, y, z, w; 4360 sin | x; 4361 sin | ®acquire® | y | z; $\C{// unnecessary, but ok to acquire and release again}$ 4362 sin | w; 4363 } // implicitly release the lock when "acq" is deallocated 4364 \end{cfa} 4365 Again, the order of the reading threads is non-deterministic. 4366 Note, non-deterministic reading is rare. 4367 4368 \Textbf{WARNING:} The general problem of \Index{nested locking} can occur if routines are called in an I/O sequence that block, \eg: 4369 \begin{cfa} 4370 sout | ®acquire® | "data:" | rtn( mon ); $\C{// mutex call on monitor}$ 4371 \end{cfa} 4372 If the thread executing the I/O expression blocks in the monitor with the ©sout© lock, other threads writing to ©sout© also block until the thread holding the lock is unblocked and releases it. 4373 This scenario can lead to \Index{deadlock}, if the thread that is going to unblock the thread waiting in the monitor first writes to ©sout© (deadly embrace). 4374 To prevent nested locking, a simple precaution is to factor out the blocking call from the expression, \eg: 4375 \begin{cfa} 4376 int ®data® = rtn( mon ); 4377 sout | acquire | "data:" | ®data®; 4378 \end{cfa} 4379 4380 \Textbf{WARNING:} ©printf©\index{printf@©printf©}, ©scanf©\index{scanf@©scanf©} and their derivatives are unsafe when used with user-level threading, as in \CFA. 4381 These stream routines use kernel-thread locking (©futex©\index{futex@©futex©}), which block kernel threads, to prevent interleaving of I/O. 4382 However, the following simple example illustrates how a deadlock can occur (other complex scenarios are possible). 4383 Assume a single kernel thread and two user-level threads calling ©printf©. 4384 One user-level thread acquires the I/O lock and is time-sliced while performing ©printf©. 4385 The other user-level thread then starts execution, calls ©printf©, and blocks the only kernel thread because it cannot acquire the I/O lock. 4386 It does not help if the kernel lock is multiple acquisition, \ie, the lock owner can acquire it multiple times, because it then results in two user threads in the ©printf© critical section, corrupting the stream. 4387 4388 4389 \begin{comment} 4078 4390 \section{Types} 4079 4391 … … 4154 4466 process((int) s); // type is converted, no function is called 4155 4467 \end{cfa} 4468 \end{comment} 4156 4469 4157 4470 … … 4287 4600 In C, the integer constants 0 and 1 suffice because the integer promotion rules can convert them to any arithmetic type, and the rules for pointer expressions treat constant expressions evaluating to 0 as a special case. 4288 4601 However, user-defined arithmetic types often need the equivalent of a 1 or 0 for their functions or operators, polymorphic functions often need 0 and 1 constants of a type matching their polymorphic parameters, and user-defined pointer-like types may need a null value. 4289 Defining special constants for a user-defined type is more efficient than defining a conversion to the type from © _Bool©.4602 Defining special constants for a user-defined type is more efficient than defining a conversion to the type from ©bool©. 4290 4603 4291 4604 Why just 0 and 1? Why not other integers? No other integers have special status in C. … … 4369 4682 \begin{table}[hbt] 4370 4683 \centering 4371 \input{../refrat/operidents} 4684 \begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{}} 4685 \begin{tabular}{@{}ll@{}} 4686 ©?[?]© & subscripting \impl{?[?]} \\ 4687 ©?()© & function call \impl{?()} \\ 4688 ©?++© & postfix increment \impl{?++} \\ 4689 ©?--© & postfix decrement \impl{?--} \\ 4690 ©++?© & prefix increment \impl{++?} \\ 4691 ©--?© & prefix decrement \impl{--?} \\ 4692 ©*?© & dereference \impl{*?} \\ 4693 ©+?© & unary plus \impl{+?} \\ 4694 ©-?© & arithmetic negation \impl{-?} \\ 4695 ©~?© & bitwise negation \impl{~?} \\ 4696 ©!?© & logical complement \impl{"!?} \\ 4697 ©?\?© & exponentiation \impl{?\?} \\ 4698 ©?*?© & multiplication \impl{?*?} \\ 4699 ©?/?© & division \impl{?/?} \\ 4700 ©?%?© & remainder \impl{?%?} \\ 4701 \end{tabular} 4702 & 4703 \begin{tabular}{@{}ll@{}} 4704 ©?+?© & addition \impl{?+?} \\ 4705 ©?-?© & subtraction \impl{?-?} \\ 4706 ©?<<?© & left shift \impl{?<<?} \\ 4707 ©?>>?© & right shift \impl{?>>?} \\ 4708 ©?<?© & less than \impl{?<?} \\ 4709 ©?<=?© & less than or equal \impl{?<=?} \\ 4710 ©?>=?© & greater than or equal \impl{?>=?} \\ 4711 ©?>?© & greater than \impl{?>?} \\ 4712 ©?==?© & equality \impl{?==?} \\ 4713 ©?!=?© & inequality \impl{?"!=?} \\ 4714 ©?&?© & bitwise AND \impl{?&?} \\ 4715 ©?^?© & exclusive OR \impl{?^?} \\ 4716 ©?|?© & inclusive OR \impl{?"|?} \\ 4717 \\ 4718 \\ 4719 \end{tabular} 4720 & 4721 \begin{tabular}{@{}ll@{}} 4722 ©?=?© & simple assignment \impl{?=?} \\ 4723 ©?\=?© & exponentiation assignment \impl{?\=?} \\ 4724 ©?*=?© & multiplication assignment \impl{?*=?} \\ 4725 ©?/=?© & division assignment \impl{?/=?} \\ 4726 ©?%=?© & remainder assignment \impl{?%=?} \\ 4727 ©?+=?© & addition assignment \impl{?+=?} \\ 4728 ©?-=?© & subtraction assignment \impl{?-=?} \\ 4729 ©?<<=?© & left-shift assignment \impl{?<<=?} \\ 4730 ©?>>=?© & right-shift assignment \impl{?>>=?} \\ 4731 ©?&=?© & bitwise AND assignment \impl{?&=?} \\ 4732 ©?^=?© & exclusive OR assignment \impl{?^=?} \\ 4733 ©?|=?© & inclusive OR assignment \impl{?"|=?} \\ 4734 \\ 4735 \\ 4736 \\ 4737 \end{tabular} 4738 \end{tabular} 4372 4739 \caption{Operator Identifiers} 4373 4740 \label{opids} … … 4457 4824 For example, given 4458 4825 \begin{cfa} 4459 auto j = @...@4826 auto j = ®...® 4460 4827 \end{cfa} 4461 4828 and the need to write a routine to compute using ©j© 4462 4829 \begin{cfa} 4463 void rtn( @...@parm );4830 void rtn( ®...® parm ); 4464 4831 rtn( j ); 4465 4832 \end{cfa} … … 4698 5065 \begin{figure} 4699 5066 \begin{cfa} 4700 #include <fstream >4701 #include <coroutine>4702 4703 coroutineFibonacci {5067 #include <fstream.hfa> 5068 #include ®<coroutine.hfa>® 5069 5070 ®coroutine® Fibonacci { 4704 5071 int fn; $\C{// used for communication}$ 4705 5072 }; 4706 void ?{}( Fibonacci * this ) { 4707 this->fn = 0; 4708 } 4709 void main( Fibonacci * this ) { 5073 5074 void main( Fibonacci & fib ) with( fib ) { $\C{// called on first resume}$ 4710 5075 int fn1, fn2; $\C{// retained between resumes}$ 4711 this->fn = 0; $\C{// case 0}$ 4712 fn1 = this->fn; 4713 suspend(); $\C{// return to last resume}$ 4714 4715 this->fn = 1; $\C{// case 1}$ 4716 fn2 = fn1; 4717 fn1 = this->fn; 4718 suspend(); $\C{// return to last resume}$ 4719 4720 for ( ;; ) { $\C{// general case}$ 4721 this->fn = fn1 + fn2; 4722 fn2 = fn1; 4723 fn1 = this->fn; 4724 suspend(); $\C{// return to last resume}$ 4725 } // for 4726 } 4727 int next( Fibonacci * this ) { 4728 resume( this ); $\C{// transfer to last suspend}$ 4729 return this->fn; 5076 fn = 0; fn1 = fn; $\C{// 1st case}$ 5077 ®suspend;® $\C{// restart last resume}$ 5078 fn = 1; fn2 = fn1; fn1 = fn; $\C{// 2nd case}$ 5079 ®suspend;® $\C{// restart last resume}$ 5080 for () { 5081 fn = fn1 + fn2; fn2 = fn1; fn1 = fn; $\C{// general case}$ 5082 ®suspend;® $\C{// restart last resume}$ 5083 } 5084 } 5085 int next( Fibonacci & fib ) with( fib ) { 5086 ®resume( fib );® $\C{// restart last suspend}$ 5087 return fn; 4730 5088 } 4731 5089 int main() { 4732 5090 Fibonacci f1, f2; 4733 for ( int i = 1; i <= 10; i += 1 ) { 4734 sout | next( &f1 ) | ' ' | next( &f2 ); 4735 } // for 4736 } 4737 \end{cfa} 5091 for ( 10 ) { $\C{// print N Fibonacci values}$ 5092 sout | next( f1 ) | next( f2 ); 5093 } 5094 } 5095 \end{cfa} 5096 \vspace*{-5pt} 4738 5097 \caption{Fibonacci Coroutine} 4739 5098 \label{f:FibonacciCoroutine} … … 4761 5120 \begin{figure} 4762 5121 \begin{cfa} 4763 #include <fstream> 4764 #include <kernel> 4765 #include <monitor> 4766 #include <thread> 4767 4768 monitor global_t { 4769 int value; 4770 }; 4771 4772 void ?{}(global_t * this) { 4773 this->value = 0; 4774 } 4775 4776 static global_t global; 4777 4778 void increment3( global_t * mutex this ) { 4779 this->value += 1; 4780 } 4781 void increment2( global_t * mutex this ) { 4782 increment3( this ); 4783 } 4784 void increment( global_t * mutex this ) { 4785 increment2( this ); 4786 } 5122 #include <fstream.hfa> 5123 #include ®<thread.hfa>® 5124 5125 ®monitor® AtomicCnt { int counter; }; 5126 void ?{}( AtomicCnt & c, int init = 0 ) with(c) { counter = init; } 5127 int inc( AtomicCnt & ®mutex® c, int inc = 1 ) with(c) { return counter += inc; } 5128 int dec( AtomicCnt & ®mutex® c, int dec = 1 ) with(c) { return counter -= dec; } 5129 forall( ostype & | ostream( ostype ) ) { $\C{// print any stream}$ 5130 ostype & ?|?( ostype & os, AtomicCnt c ) { return os | c.counter; } 5131 void ?|?( ostype & os, AtomicCnt c ) { (ostype &)(os | c.counter); ends( os ); } 5132 } 5133 5134 AtomicCnt global; $\C{// shared}$ 4787 5135 4788 5136 thread MyThread {}; 4789 4790 void main( MyThread* this) {4791 for(int i = 0; i < 1_000_000; i++) {4792 increment( &global );5137 void main( MyThread & ) { 5138 for ( i; 100_000 ) { 5139 inc( global ); 5140 dec( global ); 4793 5141 } 4794 5142 } 4795 int main(int argc, char* argv[]) { 4796 processor p; 5143 int main() { 5144 enum { Threads = 4 }; 5145 processor p[Threads - 1]; $\C{// + starting processor}$ 4797 5146 { 4798 MyThread f[4];5147 MyThread t[Threads]; 4799 5148 } 4800 sout | global .value;5149 sout | global; $\C{// print 0}$ 4801 5150 } 4802 5151 \end{cfa} 4803 5152 \caption{Atomic-Counter Monitor} 4804 \ caption{f:AtomicCounterMonitor}5153 \label{f:AtomicCounterMonitor} 4805 5154 \end{figure} 4806 5155 … … 6265 6614 6266 6615 C has a number of syntax ambiguities, which are resolved by taking the longest sequence of overlapping characters that constitute a token. 6267 For example, the program fragment ©x+++++y© is parsed as \lstinline[showspaces=true] @x ++ ++ + y@because operator tokens ©++© and ©+© overlap.6268 Unfortunately, the longest sequence violates a constraint on increment operators, even though the parse \lstinline[showspaces=true] @x ++ + ++ y@might yield a correct expression.6616 For example, the program fragment ©x+++++y© is parsed as \lstinline[showspaces=true]{x ++ ++ + y} because operator tokens ©++© and ©+© overlap. 6617 Unfortunately, the longest sequence violates a constraint on increment operators, even though the parse \lstinline[showspaces=true]{x ++ + ++ y} might yield a correct expression. 6269 6618 Hence, C programmers are aware that spaces have to added to disambiguate certain syntactic cases. 6270 6619 … … 6286 6635 requiring arbitrary whitespace look-ahead for the routine-call parameter-list to disambiguate. 6287 6636 However, the dereference operator \emph{must} have a parameter/argument to dereference ©*?(...)©. 6288 Hence, always interpreting the string ©*?()© as \lstinline[showspaces=true] @* ?()@does not preclude any meaningful program.6637 Hence, always interpreting the string ©*?()© as \lstinline[showspaces=true]{* ?()} does not preclude any meaningful program. 6289 6638 6290 6639 The remaining cases are with the increment/decrement operators and conditional expression, \eg: … … 6394 6743 \begin{cfa} 6395 6744 int i; $\C{// forward definition}$ 6396 int *j = @&i@; $\C{// forward reference, valid in C, invalid in \CFA}$6745 int *j = ®&i®; $\C{// forward reference, valid in C, invalid in \CFA}$ 6397 6746 int i = 0; $\C{// definition}$ 6398 6747 \end{cfa} … … 6402 6751 struct X { int i; struct X *next; }; 6403 6752 static struct X a; $\C{// forward definition}$ 6404 static struct X b = { 0, @&a@};$\C{// forward reference, valid in C, invalid in \CFA}$6753 static struct X b = { 0, ®&a® };$\C{// forward reference, valid in C, invalid in \CFA}$ 6405 6754 static struct X a = { 1, &b }; $\C{// definition}$ 6406 6755 \end{cfa} … … 6415 6764 \item[Change:] have ©struct© introduce a scope for nested types: 6416 6765 \begin{cfa} 6417 enum @Colour@{ R, G, B, Y, C, M };6766 enum ®Colour® { R, G, B, Y, C, M }; 6418 6767 struct Person { 6419 enum @Colour@{ R, G, B }; $\C[7cm]{// nested type}$6768 enum ®Colour® { R, G, B }; $\C[7cm]{// nested type}$ 6420 6769 struct Face { $\C{// nested type}$ 6421 @Colour@Eyes, Hair; $\C{// type defined outside (1 level)}$6770 ®Colour® Eyes, Hair; $\C{// type defined outside (1 level)}$ 6422 6771 }; 6423 @.Colour@shirt; $\C{// type defined outside (top level)}$6424 @Colour@pants; $\C{// type defined same level}$6772 ®.Colour® shirt; $\C{// type defined outside (top level)}$ 6773 ®Colour® pants; $\C{// type defined same level}$ 6425 6774 Face looks[10]; $\C{// type defined same level}$ 6426 6775 }; 6427 @Colour@c = R; $\C{// type/enum defined same level}$6428 Person @.Colour@ pc = Person@.@R;$\C{// type/enum defined inside}$6429 Person @.@Face pretty; $\C{// type defined inside}\CRT$6776 ®Colour® c = R; $\C{// type/enum defined same level}$ 6777 Person®.Colour® pc = Person®.®R;$\C{// type/enum defined inside}$ 6778 Person®.®Face pretty; $\C{// type defined inside}\CRT$ 6430 6779 \end{cfa} 6431 6780 In C, the name of the nested types belongs to the same scope as the name of the outermost enclosing structure, \ie the nested types are hoisted to the scope of the outer-most type, which is not useful and confusing. … … 6502 6851 \label{s:CFAKeywords} 6503 6852 6504 \CFA introduces the following new keywords.6853 \CFA introduces the following new \Index{keyword}s, which cannot be used as identifiers. 6505 6854 6506 6855 \begin{cquote} 6507 \input{../refrat/keywords} 6856 \begin{tabular}{@{}lllllll@{}} 6857 \begin{tabular}{@{}l@{}} 6858 \Indexc{basetypeof} \\ 6859 \Indexc{choose} \\ 6860 \Indexc{coroutine} \\ 6861 \Indexc{disable} \\ 6862 \end{tabular} 6863 & 6864 \begin{tabular}{@{}l@{}} 6865 \Indexc{enable} \\ 6866 \Indexc{exception} \\ 6867 \Indexc{fallthrough} \\ 6868 \Indexc{fallthru} \\ 6869 \end{tabular} 6870 & 6871 \begin{tabular}{@{}l@{}} 6872 \Indexc{finally} \\ 6873 \Indexc{fixup} \\ 6874 \Indexc{forall} \\ 6875 \Indexc{generator} \\ 6876 \end{tabular} 6877 & 6878 \begin{tabular}{@{}l@{}} 6879 \Indexc{int128} \\ 6880 \Indexc{monitor} \\ 6881 \Indexc{mutex} \\ 6882 \Indexc{one_t} \\ 6883 \end{tabular} 6884 & 6885 \begin{tabular}{@{}l@{}} 6886 \Indexc{report} \\ 6887 \Indexc{suspend} \\ 6888 \Indexc{throw} \\ 6889 \Indexc{throwResume} \\ 6890 \end{tabular} 6891 & 6892 \begin{tabular}{@{}l@{}} 6893 \Indexc{trait} \\ 6894 \Indexc{try} \\ 6895 \Indexc{virtual} \\ 6896 \Indexc{waitfor} \\ 6897 \end{tabular} 6898 & 6899 \begin{tabular}{@{}l@{}} 6900 \Indexc{when} \\ 6901 \Indexc{with} \\ 6902 \Indexc{zero_t} \\ 6903 \\ 6904 \end{tabular} 6905 \end{tabular} 6508 6906 \end{cquote} 6509 6907 \CFA introduces the following new \Index{quasi-keyword}s, which can be used as identifiers. 6908 \begin{cquote} 6909 \begin{tabular}{@{}ll@{}} 6910 \begin{tabular}{@{}l@{}} 6911 \Indexc{catch} \\ 6912 \Indexc{catchResume} \\ 6913 \Indexc{finally} \\ 6914 \end{tabular} 6915 & 6916 \begin{tabular}{@{}l@{}} 6917 \Indexc{fixup} \\ 6918 \Indexc{or} \\ 6919 \Indexc{timeout} \\ 6920 \end{tabular} 6921 \end{tabular} 6922 \end{cquote} 6510 6923 6511 6924 \section{Standard Headers} … … 6666 7079 // assume ?|? operator for printing an S 6667 7080 6668 S & sp = * @new@( 3 ); $\C{// call constructor after allocation}$7081 S & sp = *®new®( 3 ); $\C{// call constructor after allocation}$ 6669 7082 sout | sp.i; 6670 @delete@( &sp );6671 6672 S * spa = @anew@( 10, 5 ); $\C{// allocate array and initialize each array element}$7083 ®delete®( &sp ); 7084 7085 S * spa = ®anew®( 10, 5 ); $\C{// allocate array and initialize each array element}$ 6673 7086 for ( i; 10 ) sout | spa[i] | nonl; 6674 7087 sout | nl; 6675 @adelete@( 10, spa );7088 ®adelete®( 10, spa ); 6676 7089 \end{cfa} 6677 7090 Allocation routines ©new©/©anew© allocate a variable/array and initialize storage using the allocated type's constructor. … … 6909 7322 [ int, long double ] remquo( long double, long double ); 6910 7323 6911 float div( float, float, int * );$\indexc{div}$ $\C{// alternative name for remquo}$6912 double div( double, double, int * );6913 long double div( long double, long double, int * );6914 7324 [ int, float ] div( float, float ); 6915 7325 [ int, double ] div( double, double ); … … 6972 7382 long double _Complex log( long double _Complex ); 6973 7383 6974 float log2( float );$\indexc{log2}$ 7384 int log2( unsigned int );$\indexc{log2}$ 7385 long int log2( unsigned long int ); 7386 long long int log2( unsigned long long int ) 7387 float log2( float ); 6975 7388 double log2( double ); 6976 7389 long double log2( long double ); … … 7154 7567 \leavevmode 7155 7568 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7569 // n / align * align 7570 signed char floor( signed char n, signed char align ); 7571 unsigned char floor( unsigned char n, unsigned char align ); 7572 short int floor( short int n, short int align ); 7573 unsigned short int floor( unsigned short int n, unsigned short int align ); 7574 int floor( int n, int align ); 7575 unsigned int floor( unsigned int n, unsigned int align ); 7576 long int floor( long int n, long int align ); 7577 unsigned long int floor( unsigned long int n, unsigned long int align ); 7578 long long int floor( long long int n, long long int align ); 7579 unsigned long long int floor( unsigned long long int n, unsigned long long int align ); 7580 7581 // (n + (align - 1)) / align 7582 signed char ceiling_div( signed char n, char align ); 7583 unsigned char ceiling_div( unsigned char n, unsigned char align ); 7584 short int ceiling_div( short int n, short int align ); 7585 unsigned short int ceiling_div( unsigned short int n, unsigned short int align ); 7586 int ceiling_div( int n, int align ); 7587 unsigned int ceiling_div( unsigned int n, unsigned int align ); 7588 long int ceiling_div( long int n, long int align ); 7589 unsigned long int ceiling_div( unsigned long int n, unsigned long int align ); 7590 long long int ceiling_div( long long int n, long long int align ); 7591 unsigned long long int ceiling_div( unsigned long long int n, unsigned long long int align ); 7592 7593 // floor( n + (n % align != 0 ? align - 1 : 0), align ) 7594 signed char ceiling( signed char n, signed char align ); 7595 unsigned char ceiling( unsigned char n, unsigned char align ); 7596 short int ceiling( short int n, short int align ); 7597 unsigned short int ceiling( unsigned short int n, unsigned short int align ); 7598 int ceiling( int n, int align ); 7599 unsigned int ceiling( unsigned int n, unsigned int align ); 7600 long int ceiling( long int n, long int align ); 7601 unsigned long int ceiling( unsigned long int n, unsigned long int align ); 7602 long long int ceiling( long long int n, long long int align ); 7603 unsigned long long int ceiling( unsigned long long int n, unsigned long long int align ); 7604 7156 7605 float floor( float );$\indexc{floor}$ 7157 7606 double floor( double ); … … 7249 7698 7250 7699 7251 %\subsection{\texorpdfstring{\protect\lstinline @Duration@}{Duration}}7700 %\subsection{\texorpdfstring{\protect\lstinline{Duration}}{Duration}} 7252 7701 \subsection{\texorpdfstring{\LstBasicStyle{Duration}}{Duration}} 7253 7702 \label{s:Duration} … … 7256 7705 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7257 7706 struct Duration { 7258 int64_t t v; $\C{// nanoseconds}$7707 int64_t tn; $\C{// nanoseconds}$ 7259 7708 }; 7260 7709 7261 7710 void ?{}( Duration & dur ); 7262 7711 void ?{}( Duration & dur, zero_t ); 7712 void ?{}( Duration & dur, timeval t ) 7713 void ?{}( Duration & dur, timespec t ) 7263 7714 7264 7715 Duration ?=?( Duration & dur, zero_t ); 7716 Duration ?=?( Duration & dur, timeval t ) 7717 Duration ?=?( Duration & dur, timespec t ) 7265 7718 7266 7719 Duration +?( Duration rhs ); … … 7284 7737 Duration ?%=?( Duration & lhs, Duration rhs ); 7285 7738 7286 _Bool ?==?( Duration lhs, Duration rhs);7287 _Bool ?!=?( Duration lhs, Duration rhs);7288 _Bool ?<? ( Duration lhs, Duration rhs);7289 _Bool ?<=?( Duration lhs, Duration rhs);7290 _Bool ?>? ( Duration lhs, Duration rhs);7291 _Bool ?>=?( Duration lhs, Duration rhs);7292 7293 _Bool ?==?( Duration lhs, zero_t);7294 _Bool ?!=?( Duration lhs, zero_t);7295 _Bool ?<? ( Duration lhs, zero_t);7296 _Bool ?<=?( Duration lhs, zero_t);7297 _Bool ?>? ( Duration lhs, zero_t);7298 _Bool ?>=?( Duration lhs, zero_t);7739 bool ?==?( Duration lhs, zero_t ); 7740 bool ?!=?( Duration lhs, zero_t ); 7741 bool ?<? ( Duration lhs, zero_t ); 7742 bool ?<=?( Duration lhs, zero_t ); 7743 bool ?>? ( Duration lhs, zero_t ); 7744 bool ?>=?( Duration lhs, zero_t ); 7745 7746 bool ?==?( Duration lhs, Duration rhs ); 7747 bool ?!=?( Duration lhs, Duration rhs ); 7748 bool ?<? ( Duration lhs, Duration rhs ); 7749 bool ?<=?( Duration lhs, Duration rhs ); 7750 bool ?>? ( Duration lhs, Duration rhs ); 7751 bool ?>=?( Duration lhs, Duration rhs ); 7299 7752 7300 7753 Duration abs( Duration rhs ); … … 7323 7776 int64_t ?`w( Duration dur ); 7324 7777 7778 double ?`dns( Duration dur ); 7779 double ?`dus( Duration dur ); 7780 double ?`dms( Duration dur ); 7781 double ?`ds( Duration dur ); 7782 double ?`dm( Duration dur ); 7783 double ?`dh( Duration dur ); 7784 double ?`dd( Duration dur ); 7785 double ?`dw( Duration dur ); 7786 7325 7787 Duration max( Duration lhs, Duration rhs ); 7326 7788 Duration min( Duration lhs, Duration rhs ); 7327 \end{cfa} 7328 7329 7330 %\subsection{\texorpdfstring{\protect\lstinline@\timeval@}{timeval}} 7789 7790 forall( ostype & | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur ); 7791 \end{cfa} 7792 7793 7794 %\subsection{\texorpdfstring{\protect\lstinline{timeval}}{timeval}} 7331 7795 \subsection{\texorpdfstring{\LstBasicStyle{timeval}}{timeval}} 7332 7796 \label{s:timeval} … … 7335 7799 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7336 7800 void ?{}( timeval & t ); 7801 void ?{}( timeval & t, zero_t ); 7337 7802 void ?{}( timeval & t, time_t sec, suseconds_t usec ); 7338 7803 void ?{}( timeval & t, time_t sec ); 7339 void ?{}( timeval & t, zero_t );7340 7804 void ?{}( timeval & t, Time time ); 7341 7805 … … 7343 7807 timeval ?+?( timeval & lhs, timeval rhs ); 7344 7808 timeval ?-?( timeval & lhs, timeval rhs ); 7345 _Bool ?==?( timeval lhs, timeval rhs );7346 _Bool ?!=?( timeval lhs, timeval rhs );7347 \end{cfa} 7348 7349 7350 %\subsection{\texorpdfstring{\protect\lstinline @timespec@}{timespec}}7809 bool ?==?( timeval lhs, timeval rhs ); 7810 bool ?!=?( timeval lhs, timeval rhs ); 7811 \end{cfa} 7812 7813 7814 %\subsection{\texorpdfstring{\protect\lstinline{timespec}}{timespec}} 7351 7815 \subsection{\texorpdfstring{\LstBasicStyle{timespec}}{timespec}} 7352 7816 \label{s:timespec} … … 7355 7819 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7356 7820 void ?{}( timespec & t ); 7821 void ?{}( timespec & t, zero_t ); 7357 7822 void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ); 7358 7823 void ?{}( timespec & t, time_t sec ); 7359 void ?{}( timespec & t, zero_t );7360 7824 void ?{}( timespec & t, Time time ); 7361 7825 … … 7363 7827 timespec ?+?( timespec & lhs, timespec rhs ); 7364 7828 timespec ?-?( timespec & lhs, timespec rhs ); 7365 _Bool ?==?( timespec lhs, timespec rhs );7366 _Bool ?!=?( timespec lhs, timespec rhs );7367 \end{cfa} 7368 7369 7370 %\subsection{\texorpdfstring{\protect\lstinline @itimerval@}{itimerval}}7829 bool ?==?( timespec lhs, timespec rhs ); 7830 bool ?!=?( timespec lhs, timespec rhs ); 7831 \end{cfa} 7832 7833 7834 %\subsection{\texorpdfstring{\protect\lstinline{itimerval}}{itimerval}} 7371 7835 \subsection{\texorpdfstring{\LstBasicStyle{itimerval}}{itimerval}} 7372 7836 \label{s:itimerval} … … 7379 7843 7380 7844 7381 %\subsection{\texorpdfstring{\protect\lstinline @Time@}{Time}}7845 %\subsection{\texorpdfstring{\protect\lstinline{Time}}{Time}} 7382 7846 \subsection{\texorpdfstring{\LstBasicStyle{Time}}{Time}} 7383 7847 \label{s:Time} … … 7386 7850 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7387 7851 struct Time { 7388 uint64_t t v; $\C{// nanoseconds since UNIX epoch}$7852 uint64_t tn; $\C{// nanoseconds since UNIX epoch}$ 7389 7853 }; 7390 7854 7391 7855 void ?{}( Time & time ); 7392 7856 void ?{}( Time & time, zero_t ); 7857 void ?{}( Time & time, timeval t ); 7858 void ?{}( Time & time, timespec t ); 7393 7859 7394 7860 Time ?=?( Time & time, zero_t ); 7395 7396 void ?{}( Time & time, timeval t );7397 7861 Time ?=?( Time & time, timeval t ); 7398 7399 void ?{}( Time & time, timespec t );7400 7862 Time ?=?( Time & time, timespec t ); 7401 7863 … … 7407 7869 Time ?-?( Time lhs, Duration rhs ); 7408 7870 Time ?-=?( Time & lhs, Duration rhs ); 7409 _Bool ?==?( Time lhs, Time rhs ); 7410 _Bool ?!=?( Time lhs, Time rhs ); 7411 _Bool ?<?( Time lhs, Time rhs ); 7412 _Bool ?<=?( Time lhs, Time rhs ); 7413 _Bool ?>?( Time lhs, Time rhs ); 7414 _Bool ?>=?( Time lhs, Time rhs ); 7871 bool ?==?( Time lhs, Time rhs ); 7872 bool ?!=?( Time lhs, Time rhs ); 7873 bool ?<?( Time lhs, Time rhs ); 7874 bool ?<=?( Time lhs, Time rhs ); 7875 bool ?>?( Time lhs, Time rhs ); 7876 bool ?>=?( Time lhs, Time rhs ); 7877 7878 int64_t ?`ns( Time t ); 7415 7879 7416 7880 char * yy_mm_dd( Time time, char * buf ); 7417 char * ?`ymd( Time time, char * buf ) { // short form 7418 return yy_mm_dd( time, buf ); 7419 } // ymd 7881 char * ?`ymd( Time time, char * buf ); // short form 7420 7882 7421 7883 char * mm_dd_yy( Time time, char * buf ); 7422 char * ?`mdy( Time time, char * buf ) { // short form 7423 return mm_dd_yy( time, buf ); 7424 } // mdy 7884 char * ?`mdy( Time time, char * buf ); // short form 7425 7885 7426 7886 char * dd_mm_yy( Time time, char * buf ); 7427 char * ?`dmy( Time time, char * buf ) { // short form 7428 return dd_mm_yy( time, buf );; 7429 } // dmy 7887 char * ?`dmy( Time time, char * buf ); // short form 7430 7888 7431 7889 size_t strftime( char * buf, size_t size, const char * fmt, Time time ); 7432 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time ); 7890 7891 forall( ostype & | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time ); 7433 7892 \end{cfa} 7434 7893 … … 7450 7909 7451 7910 7452 %\subsection{\texorpdfstring{\protect\lstinline @Clock@}{Clock}}7911 %\subsection{\texorpdfstring{\protect\lstinline{Clock}}{Clock}} 7453 7912 \subsection{\texorpdfstring{\LstBasicStyle{Clock}}{Clock}} 7454 7913 \label{s:Clock} … … 7456 7915 \leavevmode 7457 7916 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7458 struct Clock { 7459 Duration offset; $\C{// for virtual clock: contains offset from real-time}$ 7460 int clocktype; $\C{// implementation only -1 (virtual), CLOCK\_REALTIME}$ 7917 struct Clock { $\C{// virtual clock}$ 7918 Duration offset; $\C{// offset from computer real-time}$ 7461 7919 }; 7462 7920 7463 void resetClock( Clock & clk ); 7464 void resetClock( Clock & clk, Duration adj ); 7465 void ?{}( Clock & clk ); 7466 void ?{}( Clock & clk, Duration adj ); 7467 7468 Duration getResNsec(); $\C{// with nanoseconds}$ 7469 Duration getRes(); $\C{// without nanoseconds}$ 7470 7471 Time getTimeNsec(); $\C{// with nanoseconds}$ 7472 Time getTime(); $\C{// without nanoseconds}$ 7473 Time getTime( Clock & clk ); 7474 Time ?()( Clock & clk ); 7475 timeval getTime( Clock & clk ); 7476 tm getTime( Clock & clk ); 7921 void ?{}( Clock & clk ); $\C{// create no offset}$ 7922 void ?{}( Clock & clk, Duration adj ); $\C{// create with offset}$ 7923 void reset( Clock & clk, Duration adj ); $\C{// change offset}$ 7924 7925 Duration resolutionHi(); $\C{// clock resolution in nanoseconds (fine)}$ 7926 Duration resolution(); $\C{// clock resolution without nanoseconds (coarse)}$ 7927 7928 Time timeHiRes(); $\C{// real time with nanoseconds}$ 7929 Time time(); $\C{// real time without nanoseconds}$ 7930 Time time( Clock & clk ); $\C{// real time for given clock}$ 7931 Time ?()( Clock & clk ); $\C{//\ \ \ \ alternative syntax}$ 7932 timeval time( Clock & clk ); $\C{// convert to C time format}$ 7933 tm time( Clock & clk ); 7934 Duration processor(); $\C{// non-monotonic duration of kernel thread}$ 7935 Duration program(); $\C{// non-monotonic duration of program CPU}$ 7936 Duration boot(); $\C{// monotonic duration since computer boot}$ 7477 7937 \end{cfa} 7478 7938 … … 7645 8105 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype * os, Int mp ); 7646 8106 \end{cfa} 7647 7648 The following factorial programs contrast using GMP with the \CFA and C interfaces, where the output from these programs appears in \VRef[Figure]{f:MultiPrecisionFactorials}. 8107 \VRef[Figure]{f:MultiPrecisionFactorials} shows \CFA and C factorial programs using the GMP interfaces. 7649 8108 (Compile with flag \Indexc{-lgmp} to link with the GMP library.) 8109 8110 \begin{figure} 7650 8111 \begin{cquote} 7651 8112 \begin{tabular}{@{}l@{\hspace{\parindentlnth}}|@{\hspace{\parindentlnth}}l@{}} 7652 \multicolumn{1}{ c|@{\hspace{\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{@{\hspace{\parindentlnth}}c}{\textbf{C}} \\8113 \multicolumn{1}{@{}c|@{\hspace{\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{@{\hspace{\parindentlnth}}c}{\textbf{C}@{}} \\ 7653 8114 \hline 7654 8115 \begin{cfa} … … 7659 8120 7660 8121 sout | 0 | fact; 7661 for ( unsigned int i = 1; i <= 40; i += 1) {8122 for ( i; 40 ) { 7662 8123 fact *= i; 7663 8124 sout | i | fact; … … 7669 8130 #include <gmp.h>$\indexc{gmp.h}$ 7670 8131 int main( void ) { 7671 @gmp_printf@( "Factorial Numbers\n" );7672 @mpz_t@fact;7673 @mpz_init_set_ui@( fact, 1 );7674 @gmp_printf@( "%d %Zd\n", 0, fact );8132 ®gmp_printf®( "Factorial Numbers\n" ); 8133 ®mpz_t® fact; 8134 ®mpz_init_set_ui®( fact, 1 ); 8135 ®gmp_printf®( "%d %Zd\n", 0, fact ); 7675 8136 for ( unsigned int i = 1; i <= 40; i += 1 ) { 7676 @mpz_mul_ui@( fact, fact, i );7677 @gmp_printf@( "%d %Zd\n", i, fact );8137 ®mpz_mul_ui®( fact, fact, i ); 8138 ®gmp_printf®( "%d %Zd\n", i, fact ); 7678 8139 } 7679 8140 } … … 7681 8142 \end{tabular} 7682 8143 \end{cquote} 7683 7684 \begin{figure} 8144 \small 7685 8145 \begin{cfa} 7686 8146 Factorial Numbers
Note:
See TracChangeset
for help on using the changeset viewer.