Changeset 8d66610 for doc/user/user.tex


Ignore:
Timestamp:
May 21, 2021, 4:48:10 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
f1bce515
Parents:
5407cdc (diff), 7404cdc (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.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/user/user.tex

    r5407cdc r8d66610  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Sun Apr 25 19:03:03 2021
    14 %% Update Count     : 4951
     13%% Last Modified On : Sat May  8 08:51:33 2021
     14%% Update Count     : 5062
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    6565% keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^
    6666% math escape $...$ (dollar symbol)
    67 \input{common}                                          % common CFA document macros
     67\usepackage{common}                                                                             % common CFA document macros
     68%\input{common}                                                                                 % common CFA document macros
    6869\setlength{\gcolumnposn}{3in}
    6970\CFAStyle                                                                                               % use default CFA format-style
     
    585586For example, the octal ©0© or hexadecimal ©0x© prefix may end with an underscore ©0_377© or ©0x_ff©;
    586587the exponent infix ©E© may start or end with an underscore ©1.0_E10©, ©1.0E_10© or ©1.0_E_10©;
    587 the type suffixes ©U©, ©L©, etc. may start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©.
     588the type suffixes ©U©, ©L©, \etc may start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©.
    588589\end{enumerate}
    589590It 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).
     
    15701571\end{cquote}
    15711572
    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:
     1573All type qualifiers, \eg ©const©, ©volatile©, \etc, are used in the normal way with the new declarations and also appear left to right, \eg:
    15731574\begin{cquote}
    15741575\begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}}
     
    15901591\end{tabular}
    15911592\end{cquote}
    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}
     1593All 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}
    15931594The 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:
    15941595\begin{cquote}
     
    31473148also, it is unnecessary to specify all the fields of a struct in a multiple record-field tuple.
    31483149
    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.).
     3150Since 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).
    31503151\begin{cfa}
    31513152[ int, int, long, double ] x;
     
    33123313
    33133314\section{Tuples}
     3315\label{tuples}
    33143316
    33153317In C and \CFA, lists of elements appear in several contexts, such as the parameter list for a routine call.
     
    34203422
    34213423\subsection{Tuple Coercions}
     3424\label{tuple coercions}\label{coercions!tuple}
    34223425
    34233426There are four coercions that can be performed on tuples and tuple variables: closing, opening, flattening and structuring.
     
    34643467
    34653468\subsection{Mass Assignment}
     3469\label{mass assignment}\label{assignment!mass}
    34663470
    34673471\CFA permits assignment to several variables at once using mass assignment~\cite{CLU}.
     
    35043508
    35053509\subsection{Multiple Assignment}
     3510\label{multiple assignment}\label{assignment!multiple}
    35063511
    35073512\CFA also supports the assignment of several values at once, known as multiple assignment~\cite{CLU,Galletly96}.
     
    35453550
    35463551\subsection{Cascade Assignment}
     3552\index{cascade assignment}\index{assignment!cascade}
    35473553
    35483554As in C, \CFA mass and multiple assignments can be cascaded, producing cascade assignment.
     
    35643570\section{Stream I/O Library}
    35653571\label{s:StreamIOLibrary}
    3566 \index{input/output stream library}
    3567 \index{stream library}
     3572\index{input}\index{output}
     3573\index{stream library}\index{library!stream}
    35683574
    35693575The 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.
    35703576Stream I/O can be implicitly or explicitly formatted.
    3571 Implicit formatting means \CFA selects the output or input format for values that match with the type of a variable.
     3577Implicit formatting means \CFA selects the output or input format for values that matches the variable's type.
    35723578Explicit formatting means additional information is specified to augment how an output or input of value is interpreted.
    3573 \CFA formatting is a cross between C ©printf© and \CC ©cout© manipulators, and Python implicit spacing and newline.
     3579\CFA formatting incorporates ideas from C ©printf©, \CC ©stream© manipulators, and Python implicit spacing and newline.
    35743580Specifically:
    35753581\begin{itemize}
     
    35843590Hence, it is common programming practice to toggle manipulators on and then back to the default to prevent downstream side-effects.
    35853591Without this programming style, errors occur when moving prints, as manipulator effects incorrectly flow into the new location.
    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.
     3592Furthermore, 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.
    35893595\end{itemize}
     3596
     3597The standard polymorphic I/Os stream are ©stdin©/©sin© (input), ©stdout©/©sout© and ©stderr©/©serr© (output) (like C++ ©cin©/©cout©/©cerr©).
     3598Polymorphic streams ©exit© and ©abort© provide implicit program termination without and with generating a stack trace and core file.
     3599Stream ©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}
     3604Note, \CFA stream variables ©stdin©, ©stdout©, ©stderr©, ©exit©, and ©abort© overload C variables ©stdin©, ©stdout©, ©stderr©, and functions ©exit© and ©abort©, respectively.
    35903605The \CFA header file for the I/O library is \Indexc{fstream.hfa}.
     3606
     3607
     3608\subsection{Basic I/O}
    35913609
    35923610For implicit formatted output, the common case is printing a series of variables separated by whitespace.
     
    36013619\begin{cfa}
    36023620
    3603 cout << x ®<< " "® << y ®<< " "® << z << endl;
     3621cout  <<  x  ®<< " "®  <<  y  ®<< " "®  <<  z << endl;
    36043622\end{cfa}
    36053623&
     
    36533671\end{tabular}
    36543672\end{cquote}
    3655 Input and output use a uniform operator, ©|©, rather than separate operators, as in ©>>© and ©<<© for \CC.
     3673Input and output use a uniform operator, ©|©, rather than \CC's ©>>© and ©<<© input/output operators.
    36563674There 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.
    36573675
     
    36983716\end{cquote}
    36993717
     3718\VRef[Figure]{f:CFACommand-LineProcessing} shows idiomatic \CFA command-line processing and copying an input file to an output file.
     3719Note, a stream variable may be copied because it is a reference to an underlying stream data-structures.
     3720All 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
     3726int 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.
     3771Returns 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.
     3778The bytes are written lazily to file when internal buffers fill.
     3779Eager 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.
     3784Pushed-back characters returned by subsequent reads in the reverse order of pushing.
     3785\end{itemize}
     3786The constructor functions:
     3787\begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt]
     3788\item
     3789create an unbound stream, which is subsequently bound to a file with ©open©.
     3790\item
     3791create a bound stream to the associated file with given ©mode©.
     3792\end{itemize}
     3793The destructor closes the stream.
     3794
     3795\begin{figure}
     3796\begin{cfa}
     3797// *********************************** ofstream ***********************************
     3798
     3799bool fail( ofstream & );$\indexc{fail}\index{ofstream@©ofstream©!©fail©}$
     3800void clear( ofstream & );$\indexc{clear}\index{ofstream@©ofstream©!©clear©}$
     3801int flush( ofstream & );$\indexc{flush}\index{ofstream@©ofstream©!©flush©}$
     3802void open( ofstream &, const char name[], const char mode[] = "w" );$\indexc{open}\index{ofstream@©ofstream©!©open©}$
     3803void close( ofstream & );$\indexc{close}\index{ofstream@©ofstream©!©close©}$
     3804ofstream & write( ofstream &, const char data[], size_t size );$\indexc{write}\index{ofstream@©ofstream©!©write©}$
     3805
     3806void ?{}( ofstream & );$\index{ofstream@©ofstream©!©?{}©}$
     3807void ?{}( ofstream &, const char name[], const char mode[] = "w" );
     3808void ^?{}( ofstream & );$\index{ofstream@©ofstream©!©^?{}©}$
     3809
     3810// *********************************** ifstream ***********************************
     3811
     3812bool fail( ifstream & is );$\indexc{fail}\index{ifstream@©ifstream©!©fail©}$
     3813void clear( ifstream & );$\indexc{clear}\index{ifstream@©ifstream©!©clear©}$
     3814bool eof( ifstream & is );$\indexc{eof}\index{ifstream@©ifstream©!©eof©}$
     3815void open( ifstream & is, const char name[], const char mode[] = "r" );$\indexc{open}\index{ifstream@©ifstream©!©open©}$
     3816void close( ifstream & is );$\indexc{close}\index{ifstream@©ifstream©!©close©}$
     3817ifstream & read( ifstream & is, char data[], size_t size );$\indexc{read}\index{ifstream@©ifstream©!©read©}$
     3818ifstream & ungetc( ifstream & is, char c );$\indexc{unget}\index{ifstream@©ifstream©!©unget©}$
     3819
     3820void ?{}( ifstream & is );$\index{ifstream@©ifstream©!©?{}©}$
     3821void ?{}( ifstream & is, const char name[], const char mode[] = "r" );
     3822void ^?{}( ifstream & is );$\index{ifstream@©ifstream©!©^?{}©}$
     3823\end{cfa}
     3824\caption{Stream Functions}
     3825\label{f:StreamFunctions}
     3826\end{figure}
    37003827
    37013828
     
    38463973
    38473974\item
    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.
     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.
    38493976\begin{cfa}[belowskip=0pt]
    38503977sout | 1 | sepOff | 2 | 3; $\C{// turn off implicit separator for the next item}$
     
    40304157sout | wd( 4, "ab" ) | wd( 3, "ab" ) | wd( 2, "ab" );
    40314158\end{cfa}
    4032 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     4159\begin{cfa}[showspaces=true,aboveskip=0pt]
    40334160®  ®34 ® ®34 34
    40344161®  ®4.000000 ® ®4.000000 4.000000
     
    43784505\end{cfa}
    43794506
    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.
     4507
     4508\section{String Stream}
     4509
     4510All 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
     4517int 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}
     4544The constructor functions:
     4545\begin{itemize}[topsep=4pt,itemsep=2pt,parsep=0pt]
     4546\item
     4547create 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
     4554ostrstream & write( ostrstream & os, FILE * stream = stdout );
     4555
     4556void ?{}( ostrstream &, char buf[], size_t size );
     4557
     4558// *********************************** istrstream ***********************************
     4559
     4560void ?{}( istrstream & is, char buf[] );
     4561\end{cfa}
     4562\caption{String Stream Functions}
     4563\label{f:StringStreamFunctions}
     4564\end{figure}
    43874565
    43884566
     
    54825660\item
    54835661Package: a container to organize modules for distribution; It has attributes like name, author,
    5484 version, dependences, etc.
    5485 \item
    5486 Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, etc.
     5662version, dependences, \etc.
     5663\item
     5664Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, \etc.
    54875665\end{itemize}
    54885666
     
    56215799
    56225800A package is defined by putting a project description file, Do.prj, with one or more modules into a directory.
    5623 This project description file contains the package's meta data, including package name, author, version, dependences, etc.
     5801This project description file contains the package's meta data, including package name, author, version, dependences, \etc.
    56245802It should be in the root of the package directory.
    56255803
     
    56785856Module: a container to organize a set of related types and methods; It has a module name, and several interfaces visible from outside
    56795857\item
    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.
     5858Package: a container to organize modules for distribution; It has attributes like name, author, version, dependences, \etc.
     5859\item
     5860Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, \etc.
    56835861\end{itemize}
    56845862
     
    81118289\begin{cquote}
    81128290\begin{tabular}{@{}l@{\hspace{\parindentlnth}}|@{\hspace{\parindentlnth}}l@{}}
    8113 \multicolumn{1}{@{}c|@{\hspace{\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{@{\hspace{\parindentlnth}}c}{\textbf{C}@{}}   \\
     8291\multicolumn{1}{@{}c|@{\hspace{\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{@{\hspace{\parindentlnth}}c@{}}{\textbf{C}}   \\
    81148292\hline
    81158293\begin{cfa}
    8116 #include <gmp>$\indexc{gmp}$
     8294#include <gmp.hfa>$\indexc{gmp}$
    81178295int main( void ) {
    81188296        sout | "Factorial Numbers";
Note: See TracChangeset for help on using the changeset viewer.