Changes in doc/user/user.tex [4ab767a:9b71679]
- File:
-
- 1 edited
-
doc/user/user.tex (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r4ab767a r9b71679 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : S at May 8 08:51:33 202114 %% Update Count : 506213 %% Last Modified On : Sun Apr 25 19:03:03 2021 14 %% Update Count : 4951 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 65 65 % keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^ 66 66 % math escape $...$ (dollar symbol) 67 \usepackage{common} % common CFA document macros 68 %\input{common} % common CFA document macros 67 \input{common} % common CFA document macros 69 68 \setlength{\gcolumnposn}{3in} 70 69 \CFAStyle % use default CFA format-style … … 586 585 For example, the octal ©0© or hexadecimal ©0x© prefix may end with an underscore ©0_377© or ©0x_ff©; 587 586 the exponent infix ©E© may start or end with an underscore ©1.0_E10©, ©1.0E_10© or ©1.0_E_10©; 588 the type suffixes ©U©, ©L©, \etcmay start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©.587 the type suffixes ©U©, ©L©, etc. may start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©. 589 588 \end{enumerate} 590 589 It is significantly easier to read and enter long constants when they are broken up into smaller groupings (many cultures use comma and/or period among digits for the same purpose). … … 1571 1570 \end{cquote} 1572 1571 1573 All type qualifiers, \eg ©const©, ©volatile©, \etc, are used in the normal way with the new declarations and also appear left to right, \eg:1572 All type qualifiers, \eg ©const©, ©volatile©, etc., are used in the normal way with the new declarations and also appear left to right, \eg: 1574 1573 \begin{cquote} 1575 1574 \begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}} … … 1591 1590 \end{tabular} 1592 1591 \end{cquote} 1593 All declaration qualifiers, \eg ©extern©, ©static©, \etc, are used in the normal way with the new declarations but can only appear at the start of a \CFA routine declaration,\footnote{\label{StorageClassSpecifier}1592 All declaration qualifiers, \eg ©extern©, ©static©, etc., are used in the normal way with the new declarations but can only appear at the start of a \CFA routine declaration,\footnote{\label{StorageClassSpecifier} 1594 1593 The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.~\cite[\S~6.11.5(1)]{C11}} \eg: 1595 1594 \begin{cquote} … … 3148 3147 also, it is unnecessary to specify all the fields of a struct in a multiple record-field tuple. 3149 3148 3150 Since tuple-index expressions are a form of member-access expression, it is possible to use tuple-index expressions in conjunction with member-access expressions to restructure a tuple (\eg, rearrange components, drop components, duplicate components, \etc).3149 Since tuple-index expressions are a form of member-access expression, it is possible to use tuple-index expressions in conjunction with member-access expressions to restructure a tuple (\eg, rearrange components, drop components, duplicate components, etc.). 3151 3150 \begin{cfa} 3152 3151 [ int, int, long, double ] x; … … 3313 3312 3314 3313 \section{Tuples} 3315 \label{tuples}3316 3314 3317 3315 In C and \CFA, lists of elements appear in several contexts, such as the parameter list for a routine call. … … 3422 3420 3423 3421 \subsection{Tuple Coercions} 3424 \label{tuple coercions}\label{coercions!tuple}3425 3422 3426 3423 There are four coercions that can be performed on tuples and tuple variables: closing, opening, flattening and structuring. … … 3467 3464 3468 3465 \subsection{Mass Assignment} 3469 \label{mass assignment}\label{assignment!mass}3470 3466 3471 3467 \CFA permits assignment to several variables at once using mass assignment~\cite{CLU}. … … 3508 3504 3509 3505 \subsection{Multiple Assignment} 3510 \label{multiple assignment}\label{assignment!multiple}3511 3506 3512 3507 \CFA also supports the assignment of several values at once, known as multiple assignment~\cite{CLU,Galletly96}. … … 3550 3545 3551 3546 \subsection{Cascade Assignment} 3552 \index{cascade assignment}\index{assignment!cascade}3553 3547 3554 3548 As in C, \CFA mass and multiple assignments can be cascaded, producing cascade assignment. … … 3570 3564 \section{Stream I/O Library} 3571 3565 \label{s:StreamIOLibrary} 3572 \index{input }\index{output}3573 \index{stream library} \index{library!stream}3566 \index{input/output stream library} 3567 \index{stream library} 3574 3568 3575 3569 The goal of \CFA stream input/output (I/O) is to simplify the common cases\index{I/O!common case}, while fully supporting polymorphism and user defined types in a consistent way. 3576 3570 Stream I/O can be implicitly or explicitly formatted. 3577 Implicit formatting means \CFA selects the output or input format for values that match es the variable's type.3571 Implicit formatting means \CFA selects the output or input format for values that match with the type of a variable. 3578 3572 Explicit formatting means additional information is specified to augment how an output or input of value is interpreted. 3579 \CFA formatting i ncorporates ideas from C ©printf©, \CC ©stream© manipulators, and Python implicit spacing and newline.3573 \CFA formatting is a cross between C ©printf© and \CC ©cout© manipulators, and Python implicit spacing and newline. 3580 3574 Specifically: 3581 3575 \begin{itemize} … … 3590 3584 Hence, it is common programming practice to toggle manipulators on and then back to the default to prevent downstream side-effects. 3591 3585 Without this programming style, errors occur when moving prints, as manipulator effects incorrectly flow into the new location. 3592 Furthermore, to guarantee no side-effects, manipulator values must be saved and restored across function calls. 3593 \item 3594 \CFA has more sophisticated implicit value spacingthan Python, plus implicit newline at the end of a print.3586 (To guarantee no side-effects, manipulator values must be saved and restored across function calls.) 3587 \item 3588 \CFA has more sophisticated implicit spacing between values than Python, plus implicit newline at the end of a print. 3595 3589 \end{itemize} 3596 3597 The standard polymorphic I/Os stream are ©stdin©/©sin© (input), ©stdout©/©sout© and ©stderr©/©serr© (output) (like C++ ©cin©/©cout©/©cerr©).3598 Polymorphic streams ©exit© and ©abort© provide implicit program termination without and with generating a stack trace and core file.3599 Stream ©exit© implicitly returns ©EXIT_FAILURE© to the shell.3600 \begin{cfa}3601 ®exit® | "x (" | x | ") negative value."; // terminate and return EXIT_FAILURE to shell3602 ®abort® | "x (" | x | ") negative value."; // terminate and generate stack trace and core file3603 \end{cfa}3604 Note, \CFA stream variables ©stdin©, ©stdout©, ©stderr©, ©exit©, and ©abort© overload C variables ©stdin©, ©stdout©, ©stderr©, and functions ©exit© and ©abort©, respectively.3605 3590 The \CFA header file for the I/O library is \Indexc{fstream.hfa}. 3606 3607 3608 \subsection{Basic I/O}3609 3591 3610 3592 For implicit formatted output, the common case is printing a series of variables separated by whitespace. … … 3619 3601 \begin{cfa} 3620 3602 3621 cout << x ®<< " "® << y ®<< " "® << z<< endl;3603 cout << x ®<< " "® << y ®<< " "® << z << endl; 3622 3604 \end{cfa} 3623 3605 & … … 3671 3653 \end{tabular} 3672 3654 \end{cquote} 3673 Input and output use a uniform operator, ©|©, rather than \CC's ©>>© and ©<<© input/output operators.3655 Input and output use a uniform operator, ©|©, rather than separate operators, as in ©>>© and ©<<© for \CC. 3674 3656 There is a weak similarity between the \CFA logical-or operator and the \Index{Shell pipe-operator} for moving data, where data flows in the correct direction for input but the opposite direction for output. 3675 3657 … … 3716 3698 \end{cquote} 3717 3699 3718 \VRef[Figure]{f:CFACommand-LineProcessing} shows idiomatic \CFA command-line processing and copying an input file to an output file.3719 Note, a stream variable may be copied because it is a reference to an underlying stream data-structures.3720 All I/O errors are handles as exceptions, but end-of-file is not an exception as C programmers are use to explicitly checking for it.3721 3722 \begin{figure}3723 \begin{cfa}3724 #include ®<fstream.hfa>®3725 3726 int main( int argc, char * argv[] ) {3727 ®ifstream® in = stdin; $\C{// copy default files}$3728 ®ofstream® out = stdout;3729 3730 try {3731 choose ( argc ) {3732 case 2, 3:3733 ®open®( in, argv[1] ); $\C{// open input file first as output creates file}$3734 if ( argc == 3 ) ®open®( out, argv[2] ); $\C{// do not create output unless input opens}$3735 case 1: ; $\C{// use default files}$3736 default:3737 ®exit® | "Usage" | argv[0] | "[ input-file (default stdin) "3738 "[ output-file (default stdout) ] ]";3739 } // choose3740 } catch( ®Open_Failure® * ex; ex->istream == &in ) {3741 ®exit® | "Unable to open input file" | argv[1];3742 } catch( ®Open_Failure® * ex; ex->ostream == &out ) {3743 ®close®( in ); $\C{// optional}$3744 ®exit® | "Unable to open output file" | argv[2];3745 } // try3746 3747 out | nlOff; $\C{// turn off auto newline}$3748 in | nlOn; $\C{// turn on reading newline}$3749 char ch;3750 for () { $\C{// read/write characters}$3751 in | ch;3752 if ( eof( in ) ) break; $\C{// eof ?}$3753 out | ch;3754 } // for3755 } // main3756 \end{cfa}3757 \caption{\CFA Command-Line Processing}3758 \label{f:CFACommand-LineProcessing}3759 \end{figure}3760 3761 \VRef[Figure]{f:StreamFunctions} shows the stream operations.3762 \begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt]3763 \item3764 \Indexc{fail} tests the stream error-indicator, returning nonzero if it is set.3765 \item3766 \Indexc{clear} resets the stream error-indicator.3767 \item3768 \Indexc{flush} (©ofstream© only) causes any unwritten data for a stream to be written to the file.3769 \item3770 \Indexc{eof} (©ifstream© only) tests the end-of-file indicator for the stream pointed to by stream.3771 Returns true if the end-of-file indicator is set, otherwise false.3772 \item3773 \Indexc{open} binds the file with ©name© to a stream accessed with ©mode© (see ©fopen©).3774 \item3775 \Indexc{close} flushes the stream and closes the file.3776 \item3777 \Indexc{write} (©ofstream© only) write ©size© bytes to the stream.3778 The bytes are written lazily to file when internal buffers fill.3779 Eager buffer writes are done with ©flush©3780 \item3781 \Indexc{read} (©ifstream© only) read ©size© bytes to the stream.3782 \item3783 \Indexc{ungetc} (©ifstream© only) pushes the character back to the input stream.3784 Pushed-back characters returned by subsequent reads in the reverse order of pushing.3785 \end{itemize}3786 The constructor functions:3787 \begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt]3788 \item3789 create an unbound stream, which is subsequently bound to a file with ©open©.3790 \item3791 create a bound stream to the associated file with given ©mode©.3792 \end{itemize}3793 The destructor closes the stream.3794 3795 \begin{figure}3796 \begin{cfa}3797 // *********************************** ofstream ***********************************3798 3799 bool fail( ofstream & );$\indexc{fail}\index{ofstream@©ofstream©!©fail©}$3800 void clear( ofstream & );$\indexc{clear}\index{ofstream@©ofstream©!©clear©}$3801 int flush( ofstream & );$\indexc{flush}\index{ofstream@©ofstream©!©flush©}$3802 void open( ofstream &, const char name[], const char mode[] = "w" );$\indexc{open}\index{ofstream@©ofstream©!©open©}$3803 void close( ofstream & );$\indexc{close}\index{ofstream@©ofstream©!©close©}$3804 ofstream & write( ofstream &, const char data[], size_t size );$\indexc{write}\index{ofstream@©ofstream©!©write©}$3805 3806 void ?{}( ofstream & );$\index{ofstream@©ofstream©!©?{}©}$3807 void ?{}( ofstream &, const char name[], const char mode[] = "w" );3808 void ^?{}( ofstream & );$\index{ofstream@©ofstream©!©^?{}©}$3809 3810 // *********************************** ifstream ***********************************3811 3812 bool fail( ifstream & is );$\indexc{fail}\index{ifstream@©ifstream©!©fail©}$3813 void clear( ifstream & );$\indexc{clear}\index{ifstream@©ifstream©!©clear©}$3814 bool eof( ifstream & is );$\indexc{eof}\index{ifstream@©ifstream©!©eof©}$3815 void open( ifstream & is, const char name[], const char mode[] = "r" );$\indexc{open}\index{ifstream@©ifstream©!©open©}$3816 void close( ifstream & is );$\indexc{close}\index{ifstream@©ifstream©!©close©}$3817 ifstream & read( ifstream & is, char data[], size_t size );$\indexc{read}\index{ifstream@©ifstream©!©read©}$3818 ifstream & ungetc( ifstream & is, char c );$\indexc{unget}\index{ifstream@©ifstream©!©unget©}$3819 3820 void ?{}( ifstream & is );$\index{ifstream@©ifstream©!©?{}©}$3821 void ?{}( ifstream & is, const char name[], const char mode[] = "r" );3822 void ^?{}( ifstream & is );$\index{ifstream@©ifstream©!©^?{}©}$3823 \end{cfa}3824 \caption{Stream Functions}3825 \label{f:StreamFunctions}3826 \end{figure}3827 3700 3828 3701 … … 3973 3846 3974 3847 \item 3975 \Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} and \Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} toggle printing the separator with respect to the next printed item, and then return to the global sep arator setting.3848 \Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} and \Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} toggle printing the separator with respect to the next printed item, and then return to the global seperator setting. 3976 3849 \begin{cfa}[belowskip=0pt] 3977 3850 sout | 1 | sepOff | 2 | 3; $\C{// turn off implicit separator for the next item}$ … … 4157 4030 sout | wd( 4, "ab" ) | wd( 3, "ab" ) | wd( 2, "ab" ); 4158 4031 \end{cfa} 4159 \begin{cfa}[showspaces=true,aboveskip=0pt ]4032 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4160 4033 ® ®34 ® ®34 34 4161 4034 ® ®4.000000 ® ®4.000000 4.000000 … … 4505 4378 \end{cfa} 4506 4379 4507 4508 \section{String Stream} 4509 4510 All the stream formatting capabilities are available to format text to/from a C string rather than to a stream file. 4511 \VRef[Figure]{f:StringStreamProcessing} shows writing (output) and reading (input) from a C string. 4512 \begin{figure} 4513 \begin{cfa} 4514 #include <fstream.hfa> 4515 #include <strstream.hfa> 4516 4517 int main() { 4518 enum { size = 256 }; 4519 char buf[size]; $\C{// output buffer}$ 4520 ®ostrstream osstr = { buf, size };® $\C{// bind output buffer/size}$ 4521 int i = 3, j = 5, k = 7; 4522 double x = 12345678.9, y = 98765.4321e-11; 4523 4524 osstr | i | hex(j) | wd(10, k) | sci(x) | unit(eng(y)); $\C{// same lines of output}$ 4525 write( osstr ); 4526 printf( "%s", buf ); 4527 sout | i | hex(j) | wd(10, k) | sci(x) | unit(eng(y)); 4528 4529 char buf2[] = "12 14 15 3.5 7e4"; $\C{// input buffer}$ 4530 ®istrstream isstr = { buf2 };® 4531 isstr | i | j | k | x | y; 4532 sout | i | j | k | x | y; 4533 } 4534 \end{cfa} 4535 \caption{String Stream Processing} 4536 \label{f:StringStreamProcessing} 4537 \end{figure} 4538 4539 \VRef[Figure]{f:StringStreamFunctions} shows the string stream operations. 4540 \begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt] 4541 \item 4542 \Indexc{write} (©ostrstream© only) writes all the buffered characters to the specified stream (©stdout© default). 4543 \end{itemize} 4544 The constructor functions: 4545 \begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt] 4546 \item 4547 create a bound stream to a write buffer (©ostrstream©) of ©size© or a read buffer (©istrstream©) containing a C string terminated with ©'\0'©. 4548 \end{itemize} 4549 4550 \begin{figure} 4551 \begin{cfa} 4552 // *********************************** ostrstream *********************************** 4553 4554 ostrstream & write( ostrstream & os, FILE * stream = stdout ); 4555 4556 void ?{}( ostrstream &, char buf[], size_t size ); 4557 4558 // *********************************** istrstream *********************************** 4559 4560 void ?{}( istrstream & is, char buf[] ); 4561 \end{cfa} 4562 \caption{String Stream Functions} 4563 \label{f:StringStreamFunctions} 4564 \end{figure} 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. 4565 4387 4566 4388 … … 5660 5482 \item 5661 5483 Package: a container to organize modules for distribution; It has attributes like name, author, 5662 version, dependences, \etc.5663 \item 5664 Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, \etc.5484 version, dependences, etc. 5485 \item 5486 Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, etc. 5665 5487 \end{itemize} 5666 5488 … … 5799 5621 5800 5622 A package is defined by putting a project description file, Do.prj, with one or more modules into a directory. 5801 This project description file contains the package's meta data, including package name, author, version, dependences, \etc.5623 This project description file contains the package's meta data, including package name, author, version, dependences, etc. 5802 5624 It should be in the root of the package directory. 5803 5625 … … 5856 5678 Module: a container to organize a set of related types and methods; It has a module name, and several interfaces visible from outside 5857 5679 \item 5858 Package: a container to organize modules for distribution; It has attributes like name, author, version, dependences, \etc.5859 \item 5860 Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, \etc.5680 Package: a container to organize modules for distribution; It has attributes like name, author, version, dependences, etc. 5681 \item 5682 Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, etc. 5861 5683 \end{itemize} 5862 5684 … … 8289 8111 \begin{cquote} 8290 8112 \begin{tabular}{@{}l@{\hspace{\parindentlnth}}|@{\hspace{\parindentlnth}}l@{}} 8291 \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}@{}} \\ 8292 8114 \hline 8293 8115 \begin{cfa} 8294 #include <gmp .hfa>$\indexc{gmp}$8116 #include <gmp>$\indexc{gmp}$ 8295 8117 int main( void ) { 8296 8118 sout | "Factorial Numbers";
Note:
See TracChangeset
for help on using the changeset viewer.