Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/user/user.tex

    r4ab767a r9b71679  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Sat May  8 08:51:33 2021
    14 %% Update Count     : 5062
     13%% Last Modified On : Sun Apr 25 19:03:03 2021
     14%% Update Count     : 4951
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    6565% keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^
    6666% 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
    6968\setlength{\gcolumnposn}{3in}
    7069\CFAStyle                                                                                               % use default CFA format-style
     
    586585For example, the octal ©0© or hexadecimal ©0x© prefix may end with an underscore ©0_377© or ©0x_ff©;
    587586the 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©, \etc may start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©.
     587the type suffixes ©U©, ©L©, etc. may start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©.
    589588\end{enumerate}
    590589It 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).
     
    15711570\end{cquote}
    15721571
    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:
     1572All type qualifiers, \eg ©const©, ©volatile©, etc., are used in the normal way with the new declarations and also appear left to right, \eg:
    15741573\begin{cquote}
    15751574\begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}}
     
    15911590\end{tabular}
    15921591\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}
     1592All 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}
    15941593The 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:
    15951594\begin{cquote}
     
    31483147also, it is unnecessary to specify all the fields of a struct in a multiple record-field tuple.
    31493148
    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).
     3149Since 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.).
    31513150\begin{cfa}
    31523151[ int, int, long, double ] x;
     
    33133312
    33143313\section{Tuples}
    3315 \label{tuples}
    33163314
    33173315In C and \CFA, lists of elements appear in several contexts, such as the parameter list for a routine call.
     
    34223420
    34233421\subsection{Tuple Coercions}
    3424 \label{tuple coercions}\label{coercions!tuple}
    34253422
    34263423There are four coercions that can be performed on tuples and tuple variables: closing, opening, flattening and structuring.
     
    34673464
    34683465\subsection{Mass Assignment}
    3469 \label{mass assignment}\label{assignment!mass}
    34703466
    34713467\CFA permits assignment to several variables at once using mass assignment~\cite{CLU}.
     
    35083504
    35093505\subsection{Multiple Assignment}
    3510 \label{multiple assignment}\label{assignment!multiple}
    35113506
    35123507\CFA also supports the assignment of several values at once, known as multiple assignment~\cite{CLU,Galletly96}.
     
    35503545
    35513546\subsection{Cascade Assignment}
    3552 \index{cascade assignment}\index{assignment!cascade}
    35533547
    35543548As in C, \CFA mass and multiple assignments can be cascaded, producing cascade assignment.
     
    35703564\section{Stream I/O Library}
    35713565\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}
    35743568
    35753569The 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.
    35763570Stream I/O can be implicitly or explicitly formatted.
    3577 Implicit formatting means \CFA selects the output or input format for values that matches the variable's type.
     3571Implicit formatting means \CFA selects the output or input format for values that match with the type of a variable.
    35783572Explicit formatting means additional information is specified to augment how an output or input of value is interpreted.
    3579 \CFA formatting incorporates 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.
    35803574Specifically:
    35813575\begin{itemize}
     
    35903584Hence, it is common programming practice to toggle manipulators on and then back to the default to prevent downstream side-effects.
    35913585Without 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 spacing than 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.
    35953589\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 shell
    3602 ®abort® | "x (" | x | ") negative value."; // terminate and generate stack trace and core file
    3603 \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.
    36053590The \CFA header file for the I/O library is \Indexc{fstream.hfa}.
    3606 
    3607 
    3608 \subsection{Basic I/O}
    36093591
    36103592For implicit formatted output, the common case is printing a series of variables separated by whitespace.
     
    36193601\begin{cfa}
    36203602
    3621 cout  <<  x  ®<< " "®  <<  y  ®<< " "®  <<  z << endl;
     3603cout << x ®<< " "® << y ®<< " "® << z << endl;
    36223604\end{cfa}
    36233605&
     
    36713653\end{tabular}
    36723654\end{cquote}
    3673 Input and output use a uniform operator, ©|©, rather than \CC's ©>>© and ©<<© input/output operators.
     3655Input and output use a uniform operator, ©|©, rather than separate operators, as in ©>>© and ©<<© for \CC.
    36743656There 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.
    36753657
     
    37163698\end{cquote}
    37173699
    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                 } // choose
    3740         } 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         } // try
    3746 
    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         } // for
    3755 } // main
    3756 \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 \item
    3764 \Indexc{fail} tests the stream error-indicator, returning nonzero if it is set.
    3765 \item
    3766 \Indexc{clear} resets the stream error-indicator.
    3767 \item
    3768 \Indexc{flush} (©ofstream© only) causes any unwritten data for a stream to be written to the file.
    3769 \item
    3770 \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 \item
    3773 \Indexc{open} binds the file with ©name© to a stream accessed with ©mode© (see ©fopen©).
    3774 \item
    3775 \Indexc{close} flushes the stream and closes the file.
    3776 \item
    3777 \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 \item
    3781 \Indexc{read} (©ifstream© only) read ©size© bytes to the stream.
    3782 \item
    3783 \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 \item
    3789 create an unbound stream, which is subsequently bound to a file with ©open©.
    3790 \item
    3791 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}
    38273700
    38283701
     
    39733846
    39743847\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 separator 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.
    39763849\begin{cfa}[belowskip=0pt]
    39773850sout | 1 | sepOff | 2 | 3; $\C{// turn off implicit separator for the next item}$
     
    41574030sout | wd( 4, "ab" ) | wd( 3, "ab" ) | wd( 2, "ab" );
    41584031\end{cfa}
    4159 \begin{cfa}[showspaces=true,aboveskip=0pt]
     4032\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
    41604033®  ®34 ® ®34 34
    41614034®  ®4.000000 ® ®4.000000 4.000000
     
    45054378\end{cfa}
    45064379
    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.
     4381These stream routines use kernel-thread locking (©futex©\index{futex@©futex©}), which block kernel threads, to prevent interleaving of I/O.
     4382However, the following simple example illustrates how a deadlock can occur (other complex scenarios are possible).
     4383Assume a single kernel thread and two user-level threads calling ©printf©.
     4384One user-level thread acquires the I/O lock and is time-sliced while performing ©printf©.
     4385The other user-level thread then starts execution, calls ©printf©, and blocks the only kernel thread because it cannot acquire the I/O lock.
     4386It 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.
    45654387
    45664388
     
    56605482\item
    56615483Package: 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.
     5484version, dependences, etc.
     5485\item
     5486Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, etc.
    56655487\end{itemize}
    56665488
     
    57995621
    58005622A 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.
     5623This project description file contains the package's meta data, including package name, author, version, dependences, etc.
    58025624It should be in the root of the package directory.
    58035625
     
    58565678Module: a container to organize a set of related types and methods; It has a module name, and several interfaces visible from outside
    58575679\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.
     5680Package: a container to organize modules for distribution; It has attributes like name, author, version, dependences, etc.
     5681\item
     5682Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, etc.
    58615683\end{itemize}
    58625684
     
    82898111\begin{cquote}
    82908112\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}@{}}   \\
    82928114\hline
    82938115\begin{cfa}
    8294 #include <gmp.hfa>$\indexc{gmp}$
     8116#include <gmp>$\indexc{gmp}$
    82958117int main( void ) {
    82968118        sout | "Factorial Numbers";
Note: See TracChangeset for help on using the changeset viewer.