Changeset 8d66610 for doc/user/user.tex
- Timestamp:
- May 21, 2021, 4:48:10 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:
- 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. - File:
-
- 1 edited
-
doc/user/user.tex (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r5407cdc r8d66610 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : S un Apr 25 19:03:03 202114 %% Update Count : 495113 %% Last Modified On : Sat May 8 08:51:33 2021 14 %% Update Count : 5062 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 65 65 % keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^ 66 66 % 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 68 69 \setlength{\gcolumnposn}{3in} 69 70 \CFAStyle % use default CFA format-style … … 585 586 For example, the octal ©0© or hexadecimal ©0x© prefix may end with an underscore ©0_377© or ©0x_ff©; 586 587 the 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©.588 the type suffixes ©U©, ©L©, \etc may start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©. 588 589 \end{enumerate} 589 590 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). … … 1570 1571 \end{cquote} 1571 1572 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: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: 1573 1574 \begin{cquote} 1574 1575 \begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}} … … 1590 1591 \end{tabular} 1591 1592 \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}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} 1593 1594 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: 1594 1595 \begin{cquote} … … 3147 3148 also, it is unnecessary to specify all the fields of a struct in a multiple record-field tuple. 3148 3149 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.).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). 3150 3151 \begin{cfa} 3151 3152 [ int, int, long, double ] x; … … 3312 3313 3313 3314 \section{Tuples} 3315 \label{tuples} 3314 3316 3315 3317 In C and \CFA, lists of elements appear in several contexts, such as the parameter list for a routine call. … … 3420 3422 3421 3423 \subsection{Tuple Coercions} 3424 \label{tuple coercions}\label{coercions!tuple} 3422 3425 3423 3426 There are four coercions that can be performed on tuples and tuple variables: closing, opening, flattening and structuring. … … 3464 3467 3465 3468 \subsection{Mass Assignment} 3469 \label{mass assignment}\label{assignment!mass} 3466 3470 3467 3471 \CFA permits assignment to several variables at once using mass assignment~\cite{CLU}. … … 3504 3508 3505 3509 \subsection{Multiple Assignment} 3510 \label{multiple assignment}\label{assignment!multiple} 3506 3511 3507 3512 \CFA also supports the assignment of several values at once, known as multiple assignment~\cite{CLU,Galletly96}. … … 3545 3550 3546 3551 \subsection{Cascade Assignment} 3552 \index{cascade assignment}\index{assignment!cascade} 3547 3553 3548 3554 As in C, \CFA mass and multiple assignments can be cascaded, producing cascade assignment. … … 3564 3570 \section{Stream I/O Library} 3565 3571 \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} 3568 3574 3569 3575 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. 3570 3576 Stream 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.3577 Implicit formatting means \CFA selects the output or input format for values that matches the variable's type. 3572 3578 Explicit formatting means additional information is specified to augment how an output or input of value is interpreted. 3573 \CFA formatting i s 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. 3574 3580 Specifically: 3575 3581 \begin{itemize} … … 3584 3590 Hence, it is common programming practice to toggle manipulators on and then back to the default to prevent downstream side-effects. 3585 3591 Without 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 valuesthan Python, plus implicit newline at the end of a print.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. 3589 3595 \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. 3590 3605 The \CFA header file for the I/O library is \Indexc{fstream.hfa}. 3606 3607 3608 \subsection{Basic I/O} 3591 3609 3592 3610 For implicit formatted output, the common case is printing a series of variables separated by whitespace. … … 3601 3619 \begin{cfa} 3602 3620 3603 cout << x ®<< " "® << y ®<< " "® << z<< endl;3621 cout << x ®<< " "® << y ®<< " "® << z << endl; 3604 3622 \end{cfa} 3605 3623 & … … 3653 3671 \end{tabular} 3654 3672 \end{cquote} 3655 Input and output use a uniform operator, ©|©, rather than separate operators, as in ©>>© and ©<<© for \CC.3673 Input and output use a uniform operator, ©|©, rather than \CC's ©>>© and ©<<© input/output operators. 3656 3674 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. 3657 3675 … … 3698 3716 \end{cquote} 3699 3717 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} 3700 3827 3701 3828 … … 3846 3973 3847 3974 \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 sep erator 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. 3849 3976 \begin{cfa}[belowskip=0pt] 3850 3977 sout | 1 | sepOff | 2 | 3; $\C{// turn off implicit separator for the next item}$ … … 4030 4157 sout | wd( 4, "ab" ) | wd( 3, "ab" ) | wd( 2, "ab" ); 4031 4158 \end{cfa} 4032 \begin{cfa}[showspaces=true,aboveskip=0pt ,belowskip=0pt]4159 \begin{cfa}[showspaces=true,aboveskip=0pt] 4033 4160 ® ®34 ® ®34 34 4034 4161 ® ®4.000000 ® ®4.000000 4.000000 … … 4378 4505 \end{cfa} 4379 4506 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 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} 4387 4565 4388 4566 … … 5482 5660 \item 5483 5661 Package: 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.5662 version, dependences, \etc. 5663 \item 5664 Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, \etc. 5487 5665 \end{itemize} 5488 5666 … … 5621 5799 5622 5800 A 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.5801 This project description file contains the package's meta data, including package name, author, version, dependences, \etc. 5624 5802 It should be in the root of the package directory. 5625 5803 … … 5678 5856 Module: a container to organize a set of related types and methods; It has a module name, and several interfaces visible from outside 5679 5857 \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.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. 5683 5861 \end{itemize} 5684 5862 … … 8111 8289 \begin{cquote} 8112 8290 \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}} \\ 8114 8292 \hline 8115 8293 \begin{cfa} 8116 #include <gmp >$\indexc{gmp}$8294 #include <gmp.hfa>$\indexc{gmp}$ 8117 8295 int main( void ) { 8118 8296 sout | "Factorial Numbers";
Note:
See TracChangeset
for help on using the changeset viewer.