Changeset b067d9b for doc/user/user.tex
- Timestamp:
- Oct 29, 2019, 4:01:24 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 773db65, 9421f3d8
- Parents:
- 7951100 (diff), 8364209 (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) (172 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r7951100 rb067d9b 1 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 %% 2 %% 3 3 %% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo 4 4 %% 5 5 %% The contents of this file are covered under the licence agreement in the 6 6 %% file "LICENCE" distributed with Cforall. 7 %% 8 %% user.tex -- 9 %% 7 %% 8 %% user.tex -- 9 %% 10 10 %% Author : Peter A. Buhr 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 May 6 10:33:53 201814 %% Update Count : 3 31913 %% Last Modified On : Sat Jul 13 18:36:18 2019 14 %% Update Count : 3876 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 72 72 73 73 % Names used in the document. 74 \newcommand{\Version}{\input{ ../../version}}74 \newcommand{\Version}{\input{build/version}} 75 75 \newcommand{\Textbf}[2][red]{{\color{#1}{\textbf{#2}}}} 76 76 \newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}} … … 146 146 \CFA adds many modern programming-language features that directly lead to increased \emph{\Index{safety}} and \emph{\Index{productivity}}, while maintaining interoperability with existing C programs and achieving similar performance. 147 147 Like C, \CFA is a statically typed, procedural (non-\Index{object-oriented}) language with a low-overhead runtime, meaning there is no global \Index{garbage-collection}, but \Index{regional garbage-collection}\index{garbage-collection!regional} is possible. 148 The primary new features include p arametric-polymorphic routines and types, exceptions, concurrency, and modules.148 The primary new features include polymorphic routines and types, exceptions, concurrency, and modules. 149 149 150 150 One of the main design philosophies of \CFA is to ``\Index{describe not prescribe}'', which means \CFA tries to provide a pathway from low-level C programming to high-level \CFA programming, but it does not force programmers to ``do the right thing''. … … 155 155 As well, new programs can be written in \CFA using a combination of C and \CFA features. 156 156 157 \Index*[C++]{\CC{}} had a similar goal 30 years ago, allowing object-oriented programming to be incrementally added to C.158 However, \CC currently has the disadvantages of a strong object-oriented bias, multiple legacy design-choices that cannot be updated, and active divergence of the language model from C, all of which requiressignificant effort and training to incrementally add \CC to a C-based project.157 \Index*[C++]{\CC{}}~\cite{c++:v1} had a similar goal 30 years ago, allowing object-oriented programming to be incrementally added to C. 158 However, \CC currently has the disadvantages of a strong object-oriented bias, multiple legacy design-choices that cannot be updated, and active divergence of the language model from C, requiring significant effort and training to incrementally add \CC to a C-based project. 159 159 In contrast, \CFA has 30 years of hindsight and a clean starting point. 160 160 161 161 Like \Index*[C++]{\CC{}}, there may be both an old and new ways to achieve the same effect. 162 For example, the following programs compare the \CFA, C, and \CC I/O mechanisms, where the programs output the same result.163 \begin{c quote}162 For example, the following programs compare the C, \CFA, and \CC I/O mechanisms, where the programs output the same result. 163 \begin{center} 164 164 \begin{tabular}{@{}l@{\hspace{1.5em}}l@{\hspace{1.5em}}l@{}} 165 165 \multicolumn{1}{c@{\hspace{1.5em}}}{\textbf{C}} & \multicolumn{1}{c}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{\CC}} \\ … … 178 178 int main( void ) { 179 179 int x = 0, y = 1, z = 2; 180 ®sout | x | y | z | endl;®§\indexc{sout}§180 ®sout | x | y | z;®§\indexc{sout}§ 181 181 } 182 182 \end{cfa} … … 191 191 \end{cfa} 192 192 \end{tabular} 193 \end{c quote}193 \end{center} 194 194 While the \CFA I/O looks similar to the \Index*[C++]{\CC{}} output style, there are important differences, such as automatic spacing between variables as in \Index*{Python} (see~\VRef{s:IOLibrary}). 195 195 196 196 197 \subsection{Background} 197 198 198 199 This document is a programmer reference-manual for the \CFA programming language. 199 The manual covers the core features of the language and runtime-system, with simple examples illustrating syntax and semantics of each feature.200 The manual covers the core features of the language and runtime-system, with simple examples illustrating syntax and semantics of features. 200 201 The manual does not teach programming, \ie how to combine the new constructs to build complex programs. 201 A reader should alreadyhave an intermediate knowledge of control flow, data structures, and concurrency issues to understand the ideas presented, as well as some experience programming in C/\CC.202 The reader must have an intermediate knowledge of control flow, data structures, and concurrency issues to understand the ideas presented, as well as some experience programming in C/\CC. 202 203 Implementers should refer to the \CFA Programming Language Specification for details about the language syntax and semantics. 203 204 Changes to the syntax and additional features are expected to be included in later revisions. … … 206 207 \section{Why fix C?} 207 208 208 The C programming language is a foundational technology for modern computing with millions of lines of code implementing everything from commercial operating-systems (especially UNIX systems) to hobby projects.209 The C programming language is a foundational technology for modern computing with millions of lines of code implementing everything from hobby projects to commercial operating-systems. 209 210 This installation base and the programmers producing it represent a massive software-engineering investment spanning decades and likely to continue for decades more. 210 211 Even with all its problems, C continues to be popular because it allows writing software at virtually any level in a computer system without restriction. 211 212 For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is usually the only language of choice. 212 The TIOBE index~\cite{TIOBE} for March 2016 showed the following programming-language popularity: \Index*{Java} 20.5\%, C 14.5\%, \Index*[C++]{\CC{}} 6.7\%, \Csharp 4.3\%, \Index*{Python} 4.3\%, where the next 50 languages are less than 3\% eachwith a long tail.213 As well, for 30 years, C has been the number 1 and 2 most popular programming language:213 The TIOBE index~\cite{TIOBE} for July 2018 ranks the top five most \emph{popular} programming languages as \Index*{Java} 16\%, C 14\%, \Index*[C++]{\CC{}} 7.5\%, Python 6\%, Visual Basic 4\% = 47.5\%, where the next 50 languages are less than 4\% each, with a long tail. 214 The top 3 rankings over the past 30 years are: 214 215 \begin{center} 215 \setlength{\tabcolsep}{1.5ex} 216 \begin{tabular}{@{}r|c|c|c|c|c|c|c@{}} 217 Ranking & 2016 & 2011 & 2006 & 2001 & 1996 & 1991 & 1986 \\ 218 \hline 219 Java & 1 & 1 & 1 & 3 & 29 & - & - \\ 220 \hline 221 \R{C} & \R{2} & \R{2} & \R{2} & \R{1} & \R{1} & \R{1} & \R{1} \\ 222 \hline 223 \CC & 3 & 3 & 3 & 2 & 2 & 2 & 7 \\ 216 \setlength{\tabcolsep}{10pt} 217 \begin{tabular}{@{}rccccccc@{}} 218 & 2018 & 2013 & 2008 & 2003 & 1998 & 1993 & 1988 \\ \hline 219 Java & 1 & 2 & 1 & 1 & 16 & - & - \\ 220 \R{C} & \R{2} & \R{1} & \R{2} & \R{2} & \R{1} & \R{1} & \R{1} \\ 221 \CC & 3 & 4 & 3 & 3 & 2 & 2 & 5 \\ 224 222 \end{tabular} 225 223 \end{center} 226 224 Hence, C is still an extremely important programming language, with double the usage of \Index*[C++]{\CC{}}; in many cases, \CC is often used solely as a better C. 227 225 Love it or hate it, C has been an important and influential part of computer science for 40 years and its appeal is not diminishing. 228 Unfortunately, C has many problems and omissions that make it an unacceptable programming language for modern needs.226 Nevertheless, C has many problems and omissions that make it an unacceptable programming language for modern needs. 229 227 230 228 As stated, the goal of the \CFA project is to engineer modern language-features into C in an evolutionary rather than revolutionary way. … … 236 234 These languages have different syntax and semantics from C, do not interoperate directly with C, and are not systems languages because of restrictive memory-management or garbage collection. 237 235 As a result, there is a significant learning curve to move to these languages, and C legacy-code must be rewritten. 238 These costs can be prohibitive for many companies with a large software-base in C/\CC, and a significant number of programmers require retraining to the new programming language. 239 240 The result of this project is a language that is largely backwards compatible with \Index*[C11]{\Celeven{}}~\cite{C11}, but fixes many of the well known C problems while containing modern language-features. 241 Without significant extension to the C programming language, it is becoming unable to cope with the needs of modern programming problems and programmers; 236 These costs can be prohibitive for many companies with a large software-base in C/\CC, and a significant number of programmers require retraining in the new programming language. 237 238 The result of this project is a language that is largely backwards compatible with \Index*[C11]{\Celeven{}}~\cite{C11}, but fixes many of the well known C problems while adding modern language-features. 239 To achieve these goals required a significant engineering exercise, where we had to ``think inside the existing C box''. 240 Without these significant extension to C, it is unable to cope with the needs of modern programming problems and programmers; 242 241 as a result, it will fade into disuse. 243 242 Considering the large body of existing C code and programmers, there is significant impetus to ensure C is transformed into a modern programming language. … … 255 254 \begin{lstlisting} 256 255 ®forall( otype T )® T identity( T val ) { return val; } 257 int forty_two = identity( 42 ); §\C{// T is bound to int, forty\_two == 42}§256 int forty_two = identity( 42 ); §\C{// T is bound to int, forty\_two == 42}§ 258 257 \end{lstlisting} 259 258 % extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions. … … 283 282 284 283 double key = 5.0, vals[10] = { /* 10 sorted floating values */ }; 285 double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); §\C{// search sorted array}§284 double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); §\C{// search sorted array}§ 286 285 \end{lstlisting} 287 286 which can be augmented simply with a polymorphic, type-safe, \CFA-overloaded wrappers: … … 292 291 293 292 forall( otype T | { int ?<?( T, T ); } ) unsigned int bsearch( T key, const T * arr, size_t size ) { 294 T * result = bsearch( key, arr, size ); §\C{// call first version}§295 return result ? result - arr : size; } §\C{// pointer subtraction includes sizeof(T)}§296 297 double * val = bsearch( 5.0, vals, 10 ); §\C{// selection based on return type}§293 T * result = bsearch( key, arr, size ); §\C{// call first version}§ 294 return result ? result - arr : size; } §\C{// pointer subtraction includes sizeof(T)}§ 295 296 double * val = bsearch( 5.0, vals, 10 ); §\C{// selection based on return type}§ 298 297 int posn = bsearch( 5.0, vals, 10 ); 299 298 \end{lstlisting} … … 307 306 \begin{lstlisting} 308 307 forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); } 309 int * ip = malloc(); §\C{// select type and size from left-hand side}§308 int * ip = malloc(); §\C{// select type and size from left-hand side}§ 310 309 double * dp = malloc(); 311 310 struct S {...} * sp = malloc(); … … 318 317 Whereas, \CFA wraps each of these routines into ones with the overloaded name ©abs©: 319 318 \begin{cfa} 320 char abs( char );321 ®extern "C" {® int abs( int ); ®}®§\C{// use default C routine for int}§322 long int abs( long int );323 long long int abs( long long int );324 float abs( float );325 double abs( double );326 long double abs( long double );327 float _Complex abs( float _Complex );328 double _Complex abs( double _Complex );329 long double _Complex abs( long double _Complex );319 char ®abs®( char ); 320 extern "C" { int ®abs®( int ); } §\C{// use default C routine for int}§ 321 long int ®abs®( long int ); 322 long long int ®abs®( long long int ); 323 float ®abs®( float ); 324 double ®abs®( double ); 325 long double ®abs®( long double ); 326 float _Complex ®abs®( float _Complex ); 327 double _Complex ®abs®( double _Complex ); 328 long double _Complex ®abs®( long double _Complex ); 330 329 \end{cfa} 331 330 The problem is the name clash between the library routine ©abs© and the \CFA names ©abs©. 332 331 Hence, names appearing in an ©extern "C"© block have \newterm*{C linkage}. 333 332 Then overloading polymorphism uses a mechanism called \newterm{name mangling}\index{mangling!name} to create unique names that are different from C names, which are not mangled. 334 Hence, there is the same need as in \CC, to know if a name is a C or \CFA name, so it can be correctly formed.335 There is no way around this problem, other than C's approach of creating unique names for each pairing of operation and type .333 Hence, there is the same need, as in \CC, to know if a name is a C or \CFA name, so it can be correctly formed. 334 There is no way around this problem, other than C's approach of creating unique names for each pairing of operation and types. 336 335 337 336 This example strongly illustrates a core idea in \CFA: \emph{the \Index{power of a name}}. … … 350 349 \begin{description} 351 350 \item 352 \Indexc{-std=gnu 99}\index{compilation option!-std=gnu99@{©-std=gnu99©}}353 The 1999C standard plus GNU extensions.354 \item 355 \Indexc[deletekeywords=inline]{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{\lstinline[deletekeywords=inline] @-fgnu89-inline@}}356 Use the traditional GNU semantics for inline routines in C 99mode, which allows inline routines in header files.351 \Indexc{-std=gnu11}\index{compilation option!-std=gnu11@{©-std=gnu11©}} 352 The 2011 C standard plus GNU extensions. 353 \item 354 \Indexc[deletekeywords=inline]{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{\lstinline[deletekeywords=inline]$-fgnu89-inline$}} 355 Use the traditional GNU semantics for inline routines in C11 mode, which allows inline routines in header files. 357 356 \end{description} 358 357 The following new \CFA options are available: … … 427 426 \begin{cfa} 428 427 #ifndef __CFORALL__ 429 #include <stdio.h>§\indexc{stdio.h}§ §\C{// C header file}§428 #include <stdio.h>§\indexc{stdio.h}§ §\C{// C header file}§ 430 429 #else 431 #include <fstream>§\indexc{fstream}§ §\C{// \CFA header file}§430 #include <fstream>§\indexc{fstream}§ §\C{// \CFA header file}§ 432 431 #endif 433 432 \end{cfa} 434 which conditionally includes the correct header file, if the program is compiled using \Indexc{gcc} or \Indexc{cfa}. 433 which conditionally includes the correct header file, if the program is compiled using \Indexc{gcc} or \Indexc{cfa}. 434 435 The \CFA translator has multiple steps. 436 The following flags control how the tranlator works, the stages run, and printing within a stage. 437 The majority of these flags are used by \CFA developers, but some are occasionally useful to programmers. 438 \begin{description}[topsep=5pt,itemsep=0pt,parsep=0pt] 439 \item 440 \Indexc{-h}\index{translator option!-h@{©-h©}}, \Indexc{--help}\index{translator option!--help@{©--help©}} \, print help message 441 \item 442 \Indexc{-l}\index{translator option!-l@{©-l©}}, \Indexc{--libcfa}\index{translator option!--libcfa@{©--libcfa©}} \, generate libcfa.c 443 \item 444 \Indexc{-L}\index{translator option!-L@{©-L©}}, \Indexc{--linemarks}\index{translator option!--linemarks@{©--linemarks©}} \, generate line marks 445 \item 446 \Indexc{-m}\index{translator option!-m@{©-m©}}, \Indexc{--no-main}\index{translator option!--no-main@{©--no-main©}} \, do not replace main 447 \item 448 \Indexc{-N}\index{translator option!-N@{©-N©}}, \Indexc{--no-linemarks}\index{translator option!--no-linemarks@{©--no-linemarks©}} \, do not generate line marks 449 \item 450 \Indexc{-n}\index{translator option!-n@{©-n©}}, \Indexc{--no-prelude}\index{translator option!--no-prelude@{©--no-prelude©}} \, do not read prelude 451 \item 452 \Indexc{-p}\index{translator option!-p@{©-p©}}, \Indexc{--prototypes}\index{translator option!--prototypes@{©--prototypes©}} \, generate prototypes for prelude functions 453 \item 454 \Indexc{-P}\index{translator option!-P@{©-P©}}, \Indexc{--print}\index{translator option!--print@{©--print©}} \, one of: 455 \begin{description}[topsep=0pt,itemsep=0pt,parsep=0pt] 456 \item 457 \Indexc{altexpr}\index{translator option!-P@{©-P©}!©altexpr©}\index{translator option!--print@{©-print©}!©altexpr©} \, alternatives for expressions 458 \item 459 \Indexc{ascodegen}\index{translator option!-P@{©-P©}!©ascodegen©}\index{translator option!--print@{©-print©}!©ascodegen©} \, as codegen rather than AST 460 \item 461 \Indexc{ast}\index{translator option!-P@{©-P©}!©ast©}\index{translator option!--print@{©-print©}!©ast©} \, AST after parsing 462 \item 463 \Indexc{astdecl}\index{translator option!-P@{©-P©}!©astdecl©}\index{translator option!--print@{©-print©}!©astdecl©} \, AST after declaration validation pass 464 \item 465 \Indexc{asterr}\index{translator option!-P@{©-P©}!©asterr©}\index{translator option!--print@{©-print©}!©asterr©} \, AST on error 466 \item 467 \Indexc{astexpr}\index{translator option!-P@{©-P©}!©astexpr©}\index{translator option!--print@{©-print©}!©altexpr©} \, AST after expression analysis 468 \item 469 \Indexc{astgen}\index{translator option!-P@{©-P©}!©astgen©}\index{translator option!--print@{©-print©}!©astgen©} \, AST after instantiate generics 470 \item 471 \Indexc{box}\index{translator option!-P@{©-P©}!©box©}\index{translator option!--print@{©-print©}!©box©} \, before box step 472 \item 473 \Indexc{ctordtor}\index{translator option!-P@{©-P©}!©ctordtor©}\index{translator option!--print@{©-print©}!©ctordtor©} \, after ctor/dtor are replaced 474 \item 475 \Indexc{codegen}\index{translator option!-P@{©-P©}!©codegen©}\index{translator option!--print@{©-print©}!©codegen©} \, before code generation 476 \item 477 \Indexc{declstats}\index{translator option!-P@{©-P©}!©declstats©}\index{translator option!--print@{©-print©}!©declstats©} \, code property statistics 478 \item 479 \Indexc{parse}\index{translator option!-P@{©-P©}!©parse©}\index{translator option!--print@{©-print©}!©parse©} \, yacc (parsing) debug information 480 \item 481 \Indexc{pretty}\index{translator option!-P@{©-P©}!©pretty©}\index{translator option!--print@{©-print©}!©pretty©} \, prettyprint for ascodegen flag 482 \item 483 \Indexc{resolver}\index{translator option!-P@{©-P©}!©resolver©}\index{translator option!--print@{©-print©}!©resolver©} \, before resolver step 484 \item 485 \Indexc{rproto}\index{translator option!-P@{©-P©}!©rproto©}\index{translator option!--print@{©-print©}!©rproto©} \, resolver-proto instance 486 \item 487 \Indexc{rsteps}\index{translator option!-P@{©-P©}!©rsteps©}\index{translator option!--print@{©-print©}!©rsteps©} \, resolver steps 488 \item 489 \Indexc{symevt}\index{translator option!-P@{©-P©}!©symevt©}\index{translator option!--print@{©-print©}!©symevt©} \, symbol table events 490 \item 491 \Indexc{tree}\index{translator option!-P@{©-P©}!©tree©}\index{translator option!--print@{©-print©}!©tree©} \, parse tree 492 \item 493 \Indexc{tuple}\index{translator option!-P@{©-P©}!©tuple©}\index{translator option!--print@{©-print©}!©tuple©} \, after tuple expansion 494 \end{description} 495 \item 496 \Indexc{--prelude-dir} <directory> \, prelude directory for debug/nodebug 497 \item 498 \Indexc{-S}\index{translator option!-S@{©-S©}!©counters,heap,time,all,none©}, \Indexc{--statistics}\index{translator option!--statistics@{©--statistics©}!©counters,heap,time,all,none©} <option-list> \, enable profiling information: 499 \begin{description}[topsep=0pt,itemsep=0pt,parsep=0pt] 500 \item 501 \Indexc{counters,heap,time,all,none} 502 \end{description} 503 \item 504 \Indexc{-t}\index{translator option!-t@{©-t©}}, \Indexc{--tree}\index{translator option!--tree@{©--tree©}} build in tree 505 \end{description} 506 507 508 \section{Backquote Identifiers} 509 \label{s:BackquoteIdentifiers} 510 511 \CFA introduces several new keywords (see \VRef{s:CFAKeywords}) that can clash with existing C variable-names in legacy code. 512 Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism: 513 \begin{cfa} 514 int ®`®otype®`® = 3; §\C{// make keyword an identifier}§ 515 double ®`®forall®`® = 3.5; 516 \end{cfa} 517 518 Existing C programs with keyword clashes can be converted by enclosing keyword identifiers in backquotes, and eventually the identifier name can be changed to a non-keyword name. 519 \VRef[Figure]{f:HeaderFileInterposition} shows how clashes in existing C header-files (see~\VRef{s:StandardHeaders}) can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©. 520 Several common C header-files with keyword clashes are fixed in the standard \CFA header-library, so there is a seamless programming-experience. 521 522 \begin{figure} 523 \begin{cfa} 524 // include file uses the CFA keyword "with". 525 #if ! defined( with ) §\C{// nesting ?}§ 526 #define with ®`®with®`® §\C{// make keyword an identifier}§ 527 #define __CFA_BFD_H__ 528 #endif 529 530 ®#include_next <bfdlink.h> §\C{// must have internal check for multiple expansion}§ 531 ® 532 #if defined( with ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§ 533 #undef with 534 #undef __CFA_BFD_H__ 535 #endif 536 \end{cfa} 537 \caption{Header-File Interposition} 538 \label{f:HeaderFileInterposition} 539 \end{figure} 435 540 436 541 … … 439 544 Numeric constants are extended to allow \Index{underscore}s\index{constant!underscore}, \eg: 440 545 \begin{cfa} 441 2®_®147®_®483®_®648; §\C{// decimal constant}§442 56®_®ul; §\C{// decimal unsigned long constant}§443 0®_®377; §\C{// octal constant}§444 0x®_®ff®_®ff; §\C{// hexadecimal constant}§445 0x®_®ef3d®_®aa5c; §\C{// hexadecimal constant}§446 3.141®_®592®_®654; §\C{// floating constant}§447 10®_®e®_®+1®_®00; §\C{// floating constant}§448 0x®_®ff®_®ff®_®p®_®3; §\C{// hexadecimal floating}§449 0x®_®1.ffff®_®ffff®_®p®_®128®_®l; §\C{// hexadecimal floating long constant}§450 L®_®§"\texttt{\textbackslash{x}}§®_®§\texttt{ff}§®_®§\texttt{ee}"§; §\C{// wide character constant}§546 2®_®147®_®483®_®648; §\C{// decimal constant}§ 547 56®_®ul; §\C{// decimal unsigned long constant}§ 548 0®_®377; §\C{// octal constant}§ 549 0x®_®ff®_®ff; §\C{// hexadecimal constant}§ 550 0x®_®ef3d®_®aa5c; §\C{// hexadecimal constant}§ 551 3.141®_®592®_®654; §\C{// floating constant}§ 552 10®_®e®_®+1®_®00; §\C{// floating constant}§ 553 0x®_®ff®_®ff®_®p®_®3; §\C{// hexadecimal floating}§ 554 0x®_®1.ffff®_®ffff®_®p®_®128®_®l; §\C{// hexadecimal floating long constant}§ 555 L®_®§"\texttt{\textbackslash{x}}§®_®§\texttt{ff}§®_®§\texttt{ee}"§; §\C{// wide character constant}§ 451 556 \end{cfa} 452 557 The rules for placement of underscores are: … … 469 574 470 575 471 \section{Backquote Identifiers} 472 \label{s:BackquoteIdentifiers} 473 474 \CFA introduces several new keywords (see \VRef{s:CFAKeywords}) that can clash with existing C variable-names in legacy code. 475 Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism: 476 \begin{cfa} 477 int ®`®otype®`® = 3; §\C{// make keyword an identifier}§ 478 double ®`®forall®`® = 3.5; 479 \end{cfa} 480 481 Existing C programs with keyword clashes can be converted by enclosing keyword identifiers in backquotes, and eventually the identifier name can be changed to a non-keyword name. 482 \VRef[Figure]{f:HeaderFileInterposition} shows how clashes in existing C header-files (see~\VRef{s:StandardHeaders}) can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©. 483 Several common C header-files with keyword clashes are fixed in the standard \CFA header-library, so there is a seamless programming-experience. 576 \section{Exponentiation Operator} 577 578 C, \CC, and Java (and many other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow}, to perform the exponentiation operation. 579 \CFA extends the basic operators with the exponentiation operator ©?\?©\index{?\\?@©?\?©} and ©?\=?©\index{?\\=?@©\=?©}, as in, ©x \ y© and ©x \= y©, which means $x^y$ and $x \leftarrow x^y$. 580 The priority of the exponentiation operator is between the cast and multiplicative operators, so that ©w * (int)x \ (int)y * z© is parenthesized as ©((w * (((int)x) \ ((int)y))) * z)©. 581 582 As for \Index{division}, there are exponentiation operators for integral and floating types, including the builtin \Index{complex} types. 583 Integral exponentiation\index{exponentiation!unsigned integral} is performed with repeated multiplication\footnote{The multiplication computation is $O(\log y)$.} (or shifting if the exponent is 2). 584 Overflow from large exponents or negative exponents return zero. 585 Floating exponentiation\index{exponentiation!floating} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the exponent cannot be negative. 586 \begin{cfa} 587 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 588 | (1.0f+2.0fi) ®\® (3.0f+2.0fi); 589 1 1 256 -64 125 ®0® 3273344365508751233 ®0® ®0® -0.015625 18.3791736799526 0.264715-1.1922i 590 \end{cfa} 591 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. 592 Parenthesis are necessary for complex constants or the expression is parsed as ©1.0f+®(®2.0fi \ 3.0f®)®+2.0fi©. 593 The exponentiation operator is available for all the basic types, but for user-defined types, only the integral-computation version is available. 594 \begin{cfa} 595 forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) 596 OT ?®\®?( OT ep, unsigned int y ); 597 forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) 598 OT ?®\®?( OT ep, unsigned long int y ); 599 \end{cfa} 600 The user type ©T© must define multiplication, one, ©1©, and, ©*©. 601 602 603 \section{Control Structures} 604 605 \CFA identifies inconsistent, problematic, and missing control structures in C, and extends, modifies, and adds control structures to increase functionality and safety. 606 607 608 %\subsection{\texorpdfstring{\protect\lstinline@if@/\protect\lstinline@while@ Statement}{if Statement}} 609 \subsection{\texorpdfstring{\LstKeywordStyle{if}/\LstKeywordStyle{while} Statement}{if/while Statement}} 610 611 The ©if©/©while© expression allows declarations, similar to ©for© declaration expression. 612 (Does not make sense for ©do©-©while©.) 613 \begin{cfa} 614 if ( ®int x = f()® ) ... §\C{// x != 0}§ 615 if ( ®int x = f(), y = g()® ) ... §\C{// x != 0 \&\& y != 0}§ 616 if ( ®int x = f(), y = g(); x < y® ) ... §\C{// relational expression}§ 617 if ( ®struct S { int i; } x = { f() }; x.i < 4® ) §\C{// relational expression}§ 618 619 while ( ®int x = f()® ) ... §\C{// x != 0}§ 620 while ( ®int x = f(), y = g()® ) ... §\C{// x != 0 \&\& y != 0}§ 621 while ( ®int x = f(), y = g(); x < y® ) ... §\C{// relational expression}§ 622 while ( ®struct S { int i; } x = { f() }; x.i < 4® ) ... §\C{// relational expression}§ 623 \end{cfa} 624 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.\footnote{\CC only provides a single declaration always compared not equal to 0.} 625 The scope of the declaration(s) is local to the @if@ statement but exist within both the ``then'' and ``else'' clauses. 626 627 628 \subsection{Loop Control} 629 630 The ©for©/©while©/©do-while© loop-control allows empty or simplified ranges (see Figure~\ref{f:LoopControlExamples}). 631 \begin{itemize} 632 \item 633 An empty conditional implies ©1©. 634 \item 635 The up-to range ©~©\index{~@©~©} means exclusive range [M,N). 636 \item 637 The up-to range ©~=©\index{~=@©~=©} means inclusive range [M,N]. 638 \item 639 The down-to range ©-~©\index{-~@©-~©} means exclusive range [N,M). 640 \item 641 The down-to range ©-~=©\index{-~=@©-~=©} means inclusive range [N,M]. 642 \item 643 ©@© means put nothing in this field. 644 \item 645 ©0© is the implicit start value; 646 \item 647 ©1© is the implicit increment value. 648 \item 649 The up-to range uses ©+=© for increment; 650 \item 651 The down-to range uses ©-=© for decrement. 652 \item 653 The loop index is polymorphic in the type of the start value or comparison value when start is implicitly ©0©. 654 \end{itemize} 484 655 485 656 \begin{figure} 486 \begin{cfa} 487 // include file uses the CFA keyword "with". 488 #if ! defined( with ) §\C{// nesting ?}§ 489 #define with ®`®with®`® §\C{// make keyword an identifier}§ 490 #define __CFA_BFD_H__ 491 #endif 492 493 ®#include_next <bfdlink.h> §\C{// must have internal check for multiple expansion}§ 494 ® 495 #if defined( with ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§ 496 #undef with 497 #undef __CFA_BFD_H__ 498 #endif 499 \end{cfa} 500 \caption{Header-File Interposition} 501 \label{f:HeaderFileInterposition} 657 \begin{cquote} 658 \begin{tabular}{@{}l|l@{}} 659 \multicolumn{1}{c|}{loop control} & \multicolumn{1}{c}{output} \\ 660 \hline 661 \begin{cfa} 662 sout | nlOff; 663 while ®()® { sout | "empty"; break; } sout | nl; 664 do { sout | "empty"; break; } while ®()®; sout | nl; 665 for ®()® { sout | "empty"; break; } sout | nl; 666 for ( ®0® ) { sout | "A"; } sout | "zero" | nl; 667 for ( ®1® ) { sout | "A"; } sout | nl; 668 for ( ®10® ) { sout | "A"; } sout | nl; 669 for ( ®1 ~= 10 ~ 2® ) { sout | "B"; } sout | nl; 670 for ( ®10 -~= 1 ~ 2® ) { sout | "C"; } sout | nl; 671 for ( ®0.5 ~ 5.5® ) { sout | "D"; } sout | nl; 672 for ( ®5.5 -~ 0.5® ) { sout | "E"; } sout | nl; 673 for ( ®i; 10® ) { sout | i; } sout | nl; 674 for ( ®i; 1 ~= 10 ~ 2® ) { sout | i; } sout | nl; 675 for ( ®i; 10 -~= 1 ~ 2® ) { sout | i; } sout | nl; 676 for ( ®i; 0.5 ~ 5.5® ) { sout | i; } sout | nl; 677 for ( ®i; 5.5 -~ 0.5® ) { sout | i; } sout | nl; 678 for ( ®ui; 2u ~= 10u ~ 2u® ) { sout | ui; } sout | nl; 679 for ( ®ui; 10u -~= 2u ~ 2u® ) { sout | ui; } sout | nl; 680 enum { N = 10 }; 681 for ( ®N® ) { sout | "N"; } sout | nl; 682 for ( ®i; N® ) { sout | i; } sout | nl; 683 for ( ®i; N -~ 0® ) { sout | i; } sout | nl; 684 const int start = 3, comp = 10, inc = 2; 685 for ( ®i; start ~ comp ~ inc + 1® ) { sout | i; } sout | nl; 686 for ( ®i; 1 ~ @® ) { if ( i > 10 ) break; 687 sout | i; } sout | nl; 688 for ( ®i; 10 -~ @® ) { if ( i < 0 ) break; 689 sout | i; } sout | nl; 690 for ( ®i; 2 ~ @ ~ 2® ) { if ( i > 10 ) break; 691 sout | i; } sout | nl; 692 for ( ®i; 2.1 ~ @ ~ @® ) { if ( i > 10.5 ) break; 693 sout | i; i += 1.7; } sout | nl; 694 for ( ®i; 10 -~ @ ~ 2® ) { if ( i < 0 ) break; 695 sout | i; } sout | nl; 696 for ( ®i; 12.1 ~ @ ~ @® ) { if ( i < 2.5 ) break; 697 sout | i; i -= 1.7; } sout | nl; 698 for ( ®i; 5 : j; -5 ~ @® ) { sout | i | j; } sout | nl; 699 for ( ®i; 5 : j; -5 -~ @® ) { sout | i | j; } sout | nl; 700 for ( ®i; 5 : j; -5 ~ @ ~ 2® ) { sout | i | j; } sout | nl; 701 for ( ®i; 5 : j; -5 -~ @ ~ 2® ) { sout | i | j; } sout | nl; 702 for ( ®j; -5 ~ @ : i; 5® ) { sout | i | j; } sout | nl; 703 for ( ®j; -5 -~ @ : i; 5® ) { sout | i | j; } sout | nl; 704 for ( ®j; -5 ~ @ ~ 2 : i; 5® ) { sout | i | j; } sout | nl; 705 for ( ®j; -5 -~ @ ~ 2 : i; 5® ) { sout | i | j; } sout | nl; 706 for ( ®j; -5 -~ @ ~ 2 : i; 5 : k; 1.5 ~ @® ) { 707 sout | i | j | k; } sout | nl; 708 for ( ®j; -5 -~ @ ~ 2 : k; 1.5 ~ @ : i; 5® ) { 709 sout | i | j | k; } sout | nl; 710 for ( ®k; 1.5 ~ @ : j; -5 -~ @ ~ 2 : i; 5® ) { 711 sout | i | j | k; } sout | nl; 712 \end{cfa} 713 & 714 \begin{cfa} 715 716 empty 717 empty 718 empty 719 zero 720 A 721 A A A A A A A A A A 722 B B B B B 723 C C C C C 724 D D D D D 725 E E E E E 726 0 1 2 3 4 5 6 7 8 9 727 1 3 5 7 9 728 10 8 6 4 2 729 0.5 1.5 2.5 3.5 4.5 730 5.5 4.5 3.5 2.5 1.5 731 2 4 6 8 10 732 10 8 6 4 2 733 734 N N N N N N N N N N 735 0 1 2 3 4 5 6 7 8 9 736 10 9 8 7 6 5 4 3 2 1 737 738 3 6 9 739 740 1 2 3 4 5 6 7 8 9 10 741 742 10 9 8 7 6 5 4 3 2 1 0 743 744 2 4 6 8 10 745 746 2.1 3.8 5.5 7.2 8.9 747 748 10 8 6 4 2 0 749 750 12.1 10.4 8.7 7 5.3 3.6 751 0 -5 1 -4 2 -3 3 -2 4 -1 752 0 -5 1 -6 2 -7 3 -8 4 -9 753 0 -5 1 -3 2 -1 3 1 4 3 754 0 -5 1 -7 2 -9 3 -11 4 -13 755 0 -5 1 -4 2 -3 3 -2 4 -1 756 0 -5 1 -6 2 -7 3 -8 4 -9 757 0 -5 1 -3 2 -1 3 1 4 3 758 0 -5 1 -7 2 -9 3 -11 4 -13 759 760 0 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5 761 762 0 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5 763 764 0 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5 765 \end{cfa} 766 \end{tabular} 767 \end{cquote} 768 \caption{Loop Control Examples} 769 \label{f:LoopControlExamples} 502 770 \end{figure} 503 771 504 772 505 \section{Exponentiation Operator} 506 507 C, \CC, and Java (and many other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow}, to perform the exponentiation operation. 508 \CFA extends the basic operators with the exponentiation operator ©?\?©\index{?\\?@\lstinline@?\?@} and ©?\=?©\index{?\\=?@\lstinline@?\=?@}, as in, ©x \ y© and ©x \= y©, which means $x^y$ and $x \leftarrow x^y$. 509 The priority of the exponentiation operator is between the cast and multiplicative operators, so that ©w * (int)x \ (int)y * z© is parenthesized as ©((w * (((int)x) \ ((int)y))) * z)©. 510 511 As for \Index{division}, there are exponentiation operators for integral and floating types, including the builtin \Index{complex} types. 512 Unsigned integral exponentiation\index{exponentiation!unsigned integral} is performed with repeated multiplication\footnote{The multiplication computation is $O(\log y)$.} (or shifting if the base is 2). 513 Signed integral exponentiation\index{exponentiation!signed integral} is performed with repeated multiplication (or shifting if the base is 2), but yields a floating result because $x^{-y}=1/x^y$. 514 Hence, it is important to designate exponent integral-constants as unsigned or signed: ©3 \ 3u© return an integral result, while ©3 \ 3© returns a floating result. 515 Floating exponentiation\index{exponentiation!floating} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the base cannot be negative. 516 \begin{cfa} 517 sout | 2 ®\® 8u | 4 ®\® 3u | -4 ®\® 3u | 4 ®\® -3 | -4 ®\® -3 | 4.0 ®\® 2.1 | (1.0f+2.0fi) ®\® (3.0f+2.0fi) | endl; 518 256 64 -64 0.015625 -0.015625 18.3791736799526 0.264715-1.1922i 519 \end{cfa} 520 Parenthesis are necessary for the complex constants or the expresion is parsed as ©1.0f+(2.0fi \ 3.0f)+2.0fi©. 521 The exponentiation operator is available for all the basic types, but for user-defined types, only the integral-computation versions are available. 522 For returning an integral value, the user type ©T© must define multiplication, ©*©, and one, ©1©; 523 for returning a floating value, an additional divide of type ©T© into a ©double© returning a ©double© (©double ?/?( double, T )©) is necessary for negative exponents. 524 525 526 \section{\texorpdfstring{Labelled \protect\lstinline@continue@ / \protect\lstinline@break@}{Labelled continue / break}} 773 %\section{\texorpdfstring{\protect\lstinline@switch@ Statement}{switch Statement}} 774 \subsection{\texorpdfstring{\LstKeywordStyle{switch} Statement}{switch Statement}} 775 776 C allows a number of questionable forms for the ©switch© statement: 777 \begin{enumerate} 778 \item 779 By default, the end of a ©case© clause\footnote{ 780 In this section, the term \emph{case clause} refers to either a ©case© or ©default© clause.} 781 \emph{falls through} to the next ©case© clause in the ©switch© statement; 782 to exit a ©switch© statement from a ©case© clause requires explicitly terminating the clause with a transfer statement, most commonly ©break©: 783 \begin{cfa} 784 switch ( i ) { 785 case 1: 786 ... 787 // fall-through 788 case 2: 789 ... 790 break; // exit switch statement 791 } 792 \end{cfa} 793 The ability to fall-through to the next clause \emph{is} a useful form of control flow, specifically when a sequence of case actions compound: 794 \begin{cquote} 795 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 796 \begin{cfa} 797 switch ( argc ) { 798 case 3: 799 // open output file 800 // fall-through 801 case 2: 802 // open input file 803 break; // exit switch statement 804 default: 805 // usage message 806 } 807 \end{cfa} 808 & 809 \begin{cfa} 810 811 if ( argc == 3 ) { 812 // open output file 813 ®// open input file 814 ®} else if ( argc == 2 ) { 815 ®// open input file (duplicate) 816 817 ®} else { 818 // usage message 819 } 820 \end{cfa} 821 \end{tabular} 822 \end{cquote} 823 In this example, case 2 is always done if case 3 is done. 824 This control flow is difficult to simulate with if statements or a ©switch© statement without fall-through as code must be duplicated or placed in a separate routine. 825 C also uses fall-through to handle multiple case-values resulting in the same action: 826 \begin{cfa} 827 switch ( i ) { 828 ®case 1: case 3: case 5:® // odd values 829 // odd action 830 break; 831 ®case 2: case 4: case 6:® // even values 832 // even action 833 break; 834 } 835 \end{cfa} 836 However, this situation is handled in other languages without fall-through by allowing a list of case values. 837 While fall-through itself is not a problem, the problem occurs when fall-through is the default, as this semantics is unintuitive to many programmers and is different from virtually all other programming languages with a ©switch© statement. 838 Hence, default fall-through semantics results in a large number of programming errors as programmers often \emph{forget} the ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through. 839 840 \item 841 It is possible to place ©case© clauses on statements nested \emph{within} the body of the ©switch© statement: 842 \begin{cfa} 843 switch ( i ) { 844 case 0: 845 if ( j < k ) { 846 ... 847 ®case 1:® // transfer into "if" statement 848 ... 849 } // if 850 case 2: 851 while ( j < 5 ) { 852 ... 853 ®case 3:® // transfer into "while" statement 854 ... 855 } // while 856 } // switch 857 \end{cfa} 858 The problem with this usage is branching into control structures, which is known to cause both comprehension and technical difficulties. 859 The comprehension problem occurs from the inability to determine how control reaches a particular point due to the number of branches leading to it. 860 The technical problem results from the inability to ensure declaration and initialization of variables when blocks are not entered at the beginning. 861 There are no positive arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it. 862 Nevertheless, C does have an idiom where this capability is used, known as ``\Index*{Duff's device}''~\cite{Duff83}: 863 \begin{cfa} 864 register int n = (count + 7) / 8; 865 switch ( count % 8 ) { 866 case 0: do{ *to = *from++; 867 case 7: *to = *from++; 868 case 6: *to = *from++; 869 case 5: *to = *from++; 870 case 4: *to = *from++; 871 case 3: *to = *from++; 872 case 2: *to = *from++; 873 case 1: *to = *from++; 874 } while ( --n > 0 ); 875 } 876 \end{cfa} 877 which unrolls a loop N times (N = 8 above) and uses the ©switch© statement to deal with any iterations not a multiple of N. 878 While efficient, this sort of special purpose usage is questionable: 879 \begin{quote} 880 Disgusting, no? But it compiles and runs just fine. I feel a combination of pride and revulsion at this 881 discovery.~\cite{Duff83} 882 \end{quote} 883 \item 884 It is possible to place the ©default© clause anywhere in the list of labelled clauses for a ©switch© statement, rather than only at the end. 885 Virtually all programming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list. 886 The logic for this semantics is that after checking all the ©case© clauses without success, the ©default© clause is selected; 887 hence, physically placing the ©default© clause at the end of the ©case© clause list matches with this semantics. 888 This physical placement can be compared to the physical placement of an ©else© clause at the end of a series of connected ©if©/©else© statements. 889 890 \item 891 It is possible to place unreachable code at the start of a ©switch© statement, as in: 892 \begin{cfa} 893 switch ( x ) { 894 ®int y = 1;® §\C{// unreachable initialization}§ 895 ®x = 7;® §\C{// unreachable code without label/branch}§ 896 case 0: ... 897 ... 898 ®int z = 0;® §\C{// unreachable initialization, cannot appear after case}§ 899 z = 2; 900 case 1: 901 ®x = z;® §\C{// without fall through, z is uninitialized}§ 902 } 903 \end{cfa} 904 While the declaration of the local variable ©y© is useful with a scope across all ©case© clauses, the initialization for such a variable is defined to never be executed because control always transfers over it. 905 Furthermore, any statements before the first ©case© clause can only be executed if labelled and transferred to using a ©goto©, either from outside or inside of the ©switch©, both of which are problematic. 906 As well, the declaration of ©z© cannot occur after the ©case© because a label can only be attached to a statement, and without a fall through to case 3, ©z© is uninitialized. 907 The key observation is that the ©switch© statement branches into control structure, \ie there are multiple entry points into its statement body. 908 \end{enumerate} 909 910 Before discussing potential language changes to deal with these problems, it is worth observing that in a typical C program: 911 \begin{itemize} 912 \item 913 the number of ©switch© statements is small, 914 \item 915 most ©switch© statements are well formed (\ie no \Index*{Duff's device}), 916 \item 917 the ©default© clause is usually written as the last case-clause, 918 \item 919 and there is only a medium amount of fall-through from one ©case© clause to the next, and most of these result from a list of case values executing common code, rather than a sequence of case actions that compound. 920 \end{itemize} 921 These observations put into perspective the \CFA changes to the ©switch©. 922 \begin{enumerate} 923 \item 924 Eliminating default fall-through has the greatest potential for affecting existing code. 925 However, even if fall-through is removed, most ©switch© statements would continue to work because of the explicit transfers already present at the end of each ©case© clause, the common placement of the ©default© clause at the end of the case list, and the most common use of fall-through, \ie a list of ©case© clauses executing common code, \eg: 926 \begin{cfa} 927 case 1: case 2: case 3: ... 928 \end{cfa} 929 still works. 930 Nevertheless, reversing the default action would have a non-trivial effect on case actions that compound, such as the above example of processing shell arguments. 931 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: 932 \begin{cfa} 933 ®choose® ( i ) { 934 case 1: case 2: case 3: 935 ... 936 ®// implicit end of switch (break) 937 ®case 5: 938 ... 939 ®fallthru®; §\C{// explicit fall through}§ 940 case 7: 941 ... 942 ®break® §\C{// explicit end of switch (redundant)}§ 943 default: 944 j = 3; 945 } 946 \end{cfa} 947 Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses; 948 An implicit ©break© is applied only at the end of the \emph{statements} following a ©case© clause. 949 An explicit ©fallthru© is retained because it is a C-idiom most C programmers expect, and its absence might discourage programmers from using the ©choose© statement. 950 As well, allowing an explicit ©break© from the ©choose© is a carry over from the ©switch© statement, and expected by C programmers. 951 \item 952 \Index*{Duff's device} is eliminated from both ©switch© and ©choose© statements, and only invalidates a small amount of very questionable code. 953 Hence, the ©case© clause must appear at the same nesting level as the ©switch©/©choose© body, as is done in most other programming languages with ©switch© statements. 954 \item 955 The issue of ©default© at locations other than at the end of the cause clause can be solved by using good programming style, and there are a few reasonable situations involving fall-through where the ©default© clause needs to appear is locations other than at the end. 956 Therefore, no change is made for this issue. 957 \item 958 Dealing with unreachable code in a ©switch©/©choose© body is solved by restricting declarations and associated initialization to the start of statement body, which is executed \emph{before} the transfer to the appropriate ©case© clause\footnote{ 959 Essentially, these declarations are hoisted before the ©switch©/©choose© statement and both declarations and statement are surrounded by a compound statement.} and precluding statements before the first ©case© clause. 960 Further declarations at the same nesting level as the statement body are disallowed to ensure every transfer into the body is sound. 961 \begin{cfa} 962 switch ( x ) { 963 ®int i = 0;® §\C{// allowed only at start}§ 964 case 0: 965 ... 966 ®int j = 0;® §\C{// disallowed}§ 967 case 1: 968 { 969 ®int k = 0;® §\C{// allowed at different nesting levels}§ 970 ... 971 ®case 2:® §\C{// disallow case in nested statements}§ 972 } 973 ... 974 } 975 \end{cfa} 976 \end{enumerate} 977 978 979 %\section{\texorpdfstring{\protect\lstinline@case@ Clause}{case Clause}} 980 \subsection{\texorpdfstring{\LstKeywordStyle{case} Statement}{case Statement}} 981 982 C restricts the ©case© clause of a ©switch© statement to a single value. 983 For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case© clauses rather than multiple values. 984 Requiring a ©case© clause for each value does not seem to be in the spirit of brevity normally associated with C. 985 Therefore, the ©case© clause is extended with a list of values, as in: 986 \begin{cquote} 987 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} 988 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\ 989 \begin{cfa} 990 switch ( i ) { 991 case ®1, 3, 5®: 992 ... 993 case ®2, 4, 6®: 994 ... 995 } 996 \end{cfa} 997 & 998 \begin{cfa} 999 switch ( i ) { 1000 case 1: case 3 : case 5: 1001 ... 1002 case 2: case 4 : case 6: 1003 ... 1004 } 1005 \end{cfa} 1006 & 1007 \begin{cfa} 1008 1009 // odd values 1010 1011 // even values 1012 1013 1014 \end{cfa} 1015 \end{tabular} 1016 \end{cquote} 1017 In addition, subranges are allowed to specify case values.\footnote{ 1018 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.} 1019 \begin{cfa} 1020 switch ( i ) { 1021 case ®1~5:® §\C{// 1, 2, 3, 4, 5}§ 1022 ... 1023 case ®10~15:® §\C{// 10, 11, 12, 13, 14, 15}§ 1024 ... 1025 } 1026 \end{cfa} 1027 Lists of subranges are also allowed. 1028 \begin{cfa} 1029 case ®1~5, 12~21, 35~42®: 1030 \end{cfa} 1031 1032 1033 % for () => for ( ;; ) 1034 % for ( 10 - t ) => for ( typeof(10 - t) ? = 0 ; ? < 10 - t; ? += 1 ) // using 0 and 1 1035 % for ( i ; 10 - t ) => for ( typeof(10 - t) i = 0 ; i < 10 - t; i += 1 ) // using 0 and 1 1036 % for ( T i ; 10 - t ) => for ( T i = 0 ; i < 10 - t; i += 1 ) // using 0 and 1 1037 % for ( 3~9 ) => for ( int ? = 3 ; ? < 9; ? += 1 ) // using 1 1038 % for ( i ; 3~9 ) => for ( int i = 3 ; i < 9; i += 1 ) // using 1 1039 % for ( T i ; 3~9 ) => for ( T i = 3 ; i < 9; i += 1 ) // using 1 1040 1041 1042 %\subsection{\texorpdfstring{Labelled \protect\lstinline@continue@ / \protect\lstinline@break@}{Labelled continue / break}} 1043 \subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}} 527 1044 528 1045 While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure. 529 1046 Unfortunately, this restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting. 530 To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@ \lstinline@continue@!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@\lstinline@break@!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85}, as in Java.1047 To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@©continue©!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@©break©!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85}, as in Java. 531 1048 For both ©continue© and ©break©, the target label must be directly associated with a ©for©, ©while© or ©do© statement; 532 1049 for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement. … … 613 1130 \end{figure} 614 1131 615 Both labelled ©continue© and ©break© are a ©goto©\index{goto@ \lstinline@goto@!restricted} restricted in the following ways:1132 Both labelled ©continue© and ©break© are a ©goto©\index{goto@©goto©!restricted} restricted in the following ways: 616 1133 \begin{itemize} 617 1134 \item … … 626 1143 With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader. 627 1144 Finally, using an explicit target for the transfer instead of an implicit target allows new constructs to be added or removed without affecting existing constructs. 628 The implicit targets of the current ©continue© and ©break©, \ie the closest enclosing loop or ©switch©, change as certain constructs are added or removed. 629 630 631 \section{\texorpdfstring{\protect\lstinline@switch@ Statement}{switch Statement}} 632 633 C allows a number of questionable forms for the ©switch© statement: 634 \begin{enumerate} 635 \item 636 By default, the end of a ©case© clause\footnote{ 637 In this section, the term \emph{case clause} refers to either a ©case© or ©default© clause.} 638 \emph{falls through} to the next ©case© clause in the ©switch© statement; 639 to exit a ©switch© statement from a ©case© clause requires explicitly terminating the clause with a transfer statement, most commonly ©break©: 640 \begin{cfa} 641 switch ( i ) { 642 case 1: 643 ... 644 // fall-through 645 case 2: 646 ... 647 break; // exit switch statement 648 } 649 \end{cfa} 650 The ability to fall-through to the next clause \emph{is} a useful form of control flow, specifically when a sequence of case actions compound: 651 \begin{cquote} 652 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 653 \begin{cfa} 654 switch ( argc ) { 655 case 3: 656 // open output file 657 // fall-through 658 case 2: 659 // open input file 660 break; // exit switch statement 661 default: 662 // usage message 663 } 664 \end{cfa} 665 & 666 \begin{cfa} 667 668 if ( argc == 3 ) { 669 // open output file 670 ®// open input file 671 ®} else if ( argc == 2 ) { 672 ®// open input file (duplicate) 673 674 ®} else { 675 // usage message 676 } 677 \end{cfa} 678 \end{tabular} 679 \end{cquote} 680 In this example, case 2 is always done if case 3 is done. 681 This control flow is difficult to simulate with if statements or a ©switch© statement without fall-through as code must be duplicated or placed in a separate routine. 682 C also uses fall-through to handle multiple case-values resulting in the same action: 683 \begin{cfa} 684 switch ( i ) { 685 ®case 1: case 3: case 5:® // odd values 686 // odd action 687 break; 688 ®case 2: case 4: case 6:® // even values 689 // even action 690 break; 691 } 692 \end{cfa} 693 However, this situation is handled in other languages without fall-through by allowing a list of case values. 694 While fall-through itself is not a problem, the problem occurs when fall-through is the default, as this semantics is unintuitive to many programmers and is different from virtually all other programming languages with a ©switch© statement. 695 Hence, default fall-through semantics results in a large number of programming errors as programmers often \emph{forget} the ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through. 696 697 \item 698 It is possible to place ©case© clauses on statements nested \emph{within} the body of the ©switch© statement: 699 \begin{cfa} 700 switch ( i ) { 701 case 0: 702 if ( j < k ) { 703 ... 704 ®case 1:® // transfer into "if" statement 705 ... 706 } // if 707 case 2: 708 while ( j < 5 ) { 709 ... 710 ®case 3:® // transfer into "while" statement 711 ... 712 } // while 713 } // switch 714 \end{cfa} 715 The problem with this usage is branching into control structures, which is known to cause both comprehension and technical difficulties. 716 The comprehension problem occurs from the inability to determine how control reaches a particular point due to the number of branches leading to it. 717 The technical problem results from the inability to ensure declaration and initialization of variables when blocks are not entered at the beginning. 718 There are no positive arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it. 719 Nevertheless, C does have an idiom where this capability is used, known as ``\Index*{Duff's device}''~\cite{Duff83}: 720 \begin{cfa} 721 register int n = (count + 7) / 8; 722 switch ( count % 8 ) { 723 case 0: do{ *to = *from++; 724 case 7: *to = *from++; 725 case 6: *to = *from++; 726 case 5: *to = *from++; 727 case 4: *to = *from++; 728 case 3: *to = *from++; 729 case 2: *to = *from++; 730 case 1: *to = *from++; 731 } while ( --n > 0 ); 732 } 733 \end{cfa} 734 which unrolls a loop N times (N = 8 above) and uses the ©switch© statement to deal with any iterations not a multiple of N. 735 While efficient, this sort of special purpose usage is questionable: 736 \begin{quote} 737 Disgusting, no? But it compiles and runs just fine. I feel a combination of pride and revulsion at this 738 discovery.~\cite{Duff83} 739 \end{quote} 740 \item 741 It is possible to place the ©default© clause anywhere in the list of labelled clauses for a ©switch© statement, rather than only at the end. 742 Virtually all programming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list. 743 The logic for this semantics is that after checking all the ©case© clauses without success, the ©default© clause is selected; 744 hence, physically placing the ©default© clause at the end of the ©case© clause list matches with this semantics. 745 This physical placement can be compared to the physical placement of an ©else© clause at the end of a series of connected ©if©/©else© statements. 746 747 \item 748 It is possible to place unreachable code at the start of a ©switch© statement, as in: 749 \begin{cfa} 750 switch ( x ) { 751 ®int y = 1;® §\C{// unreachable initialization}§ 752 ®x = 7;® §\C{// unreachable code without label/branch}§ 753 case 0: ... 754 ... 755 ®int z = 0;® §\C{// unreachable initialization, cannot appear after case}§ 756 z = 2; 757 case 1: 758 ®x = z;® §\C{// without fall through, z is uninitialized}§ 759 } 760 \end{cfa} 761 While the declaration of the local variable ©y© is useful with a scope across all ©case© clauses, the initialization for such a variable is defined to never be executed because control always transfers over it. 762 Furthermore, any statements before the first ©case© clause can only be executed if labelled and transferred to using a ©goto©, either from outside or inside of the ©switch©, both of which are problematic. 763 As well, the declaration of ©z© cannot occur after the ©case© because a label can only be attached to a statement, and without a fall through to case 3, ©z© is uninitialized. 764 The key observation is that the ©switch© statement branches into control structure, \ie there are multiple entry points into its statement body. 765 \end{enumerate} 766 767 Before discussing potential language changes to deal with these problems, it is worth observing that in a typical C program: 768 \begin{itemize} 769 \item 770 the number of ©switch© statements is small, 771 \item 772 most ©switch© statements are well formed (\ie no \Index*{Duff's device}), 773 \item 774 the ©default© clause is usually written as the last case-clause, 775 \item 776 and there is only a medium amount of fall-through from one ©case© clause to the next, and most of these result from a list of case values executing common code, rather than a sequence of case actions that compound. 777 \end{itemize} 778 These observations put into perspective the \CFA changes to the ©switch©. 779 \begin{enumerate} 780 \item 781 Eliminating default fall-through has the greatest potential for affecting existing code. 782 However, even if fall-through is removed, most ©switch© statements would continue to work because of the explicit transfers already present at the end of each ©case© clause, the common placement of the ©default© clause at the end of the case list, and the most common use of fall-through, \ie a list of ©case© clauses executing common code, \eg: 783 \begin{cfa} 784 case 1: case 2: case 3: ... 785 \end{cfa} 786 still works. 787 Nevertheless, reversing the default action would have a non-trivial effect on case actions that compound, such as the above example of processing shell arguments. 788 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: 789 \begin{cfa} 790 ®choose® ( i ) { 791 case 1: case 2: case 3: 792 ... 793 ®// implicit end of switch (break) 794 ®case 5: 795 ... 796 ®fallthru®; §\C{// explicit fall through}§ 797 case 7: 798 ... 799 ®break® §\C{// explicit end of switch (redundant)}§ 800 default: 801 j = 3; 802 } 803 \end{cfa} 804 Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses; 805 An implicit ©break© is applied only at the end of the \emph{statements} following a ©case© clause. 806 An explicit ©fallthru© is retained because it is a C-idiom most C programmers expect, and its absence might discourage programmers from using the ©choose© statement. 807 As well, allowing an explicit ©break© from the ©choose© is a carry over from the ©switch© statement, and expected by C programmers. 808 \item 809 \Index*{Duff's device} is eliminated from both ©switch© and ©choose© statements, and only invalidates a small amount of very questionable code. 810 Hence, the ©case© clause must appear at the same nesting level as the ©switch©/©choose© body, as is done in most other programming languages with ©switch© statements. 811 \item 812 The issue of ©default© at locations other than at the end of the cause clause can be solved by using good programming style, and there are a few reasonable situations involving fall-through where the ©default© clause needs to appear is locations other than at the end. 813 Therefore, no change is made for this issue. 814 \item 815 Dealing with unreachable code in a ©switch©/©choose© body is solved by restricting declarations and associated initialization to the start of statement body, which is executed \emph{before} the transfer to the appropriate ©case© clause\footnote{ 816 Essentially, these declarations are hoisted before the ©switch©/©choose© statement and both declarations and statement are surrounded by a compound statement.} and precluding statements before the first ©case© clause. 817 Further declarations at the same nesting level as the statement body are disallowed to ensure every transfer into the body is sound. 818 \begin{cfa} 819 switch ( x ) { 820 ®int i = 0;® §\C{// allowed only at start}§ 821 case 0: 822 ... 823 ®int j = 0;® §\C{// disallowed}§ 824 case 1: 825 { 826 ®int k = 0;® §\C{// allowed at different nesting levels}§ 827 ... 828 ®case 2:® §\C{// disallow case in nested statements}§ 829 } 830 ... 831 } 832 \end{cfa} 833 \end{enumerate} 834 835 836 \section{\texorpdfstring{\protect\lstinline@case@ Clause}{case Clause}} 837 838 C restricts the ©case© clause of a ©switch© statement to a single value. 839 For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case© clauses rather than multiple values. 840 Requiring a ©case© clause for each value does not seem to be in the spirit of brevity normally associated with C. 841 Therefore, the ©case© clause is extended with a list of values, as in: 842 \begin{cquote} 843 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} 844 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\ 845 \begin{cfa} 846 switch ( i ) { 847 case ®1, 3, 5®: 848 ... 849 case ®2, 4, 6®: 850 ... 851 } 852 \end{cfa} 853 & 854 \begin{cfa} 855 switch ( i ) { 856 case 1: case 3 : case 5: 857 ... 858 case 2: case 4 : case 6: 859 ... 860 } 861 \end{cfa} 862 & 863 \begin{cfa} 864 865 // odd values 866 867 // even values 868 869 870 \end{cfa} 871 \end{tabular} 872 \end{cquote} 873 In addition, subranges are allowed to specify case values.\footnote{ 874 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.} 875 \begin{cfa} 876 switch ( i ) { 877 case ®1~5:® §\C{// 1, 2, 3, 4, 5}§ 878 ... 879 case ®10~15:® §\C{// 10, 11, 12, 13, 14, 15}§ 880 ... 881 } 882 \end{cfa} 883 Lists of subranges are also allowed. 884 \begin{cfa} 885 case ®1~5, 12~21, 35~42®: 886 \end{cfa} 887 888 889 \section{\texorpdfstring{\protect\lstinline@with@ Statement}{with Statement}} 1145 Otherwise, the implicit targets of the current ©continue© and ©break©, \ie the closest enclosing loop or ©switch©, change as certain constructs are added or removed. 1146 1147 1148 %\section{\texorpdfstring{\protect\lstinline@with@ Statement}{with Statement}} 1149 \section{\texorpdfstring{\LstKeywordStyle{with} Statement}{with Statement}} 890 1150 \label{s:WithStatement} 891 1151 892 1152 Grouping heterogeneous data into \newterm{aggregate}s (structure/union) is a common programming practice, and an aggregate can be further organized into more complex structures, such as arrays and containers: 893 1153 \begin{cfa} 894 struct S { §\C{// aggregate}§895 char c; §\C{// fields}§1154 struct S { §\C{// aggregate}§ 1155 char c; §\C{// fields}§ 896 1156 int i; 897 1157 double d; … … 902 1162 \begin{cfa} 903 1163 void f( S s ) { 904 `s.`c; `s.`i; `s.`d;§\C{// access containing fields}§1164 ®s.®c; ®s.®i; ®s.®d; §\C{// access containing fields}§ 905 1165 } 906 1166 \end{cfa} … … 909 1169 \begin{C++} 910 1170 struct S { 911 char c; §\C{// fields}§1171 char c; §\C{// fields}§ 912 1172 int i; 913 1173 double d; 914 void f() { §\C{// implicit ``this'' aggregate}§915 `this->`c; `this->`i; `this->`d;§\C{// access containing fields}§1174 void f() { §\C{// implicit ``this'' aggregate}§ 1175 ®this->®c; ®this->®i; ®this->®d; §\C{// access containing fields}§ 916 1176 } 917 1177 } 918 1178 \end{C++} 919 Object-oriented nesting of member functions in a \lstinline[language=C++]@class/struct@ allows eliding \lstinline[language=C++] $this->$because of lexical scoping.1179 Object-oriented nesting of member functions in a \lstinline[language=C++]@class/struct@ allows eliding \lstinline[language=C++]@this->@ because of lexical scoping. 920 1180 However, for other aggregate parameters, qualification is necessary: 921 1181 \begin{cfa} 922 1182 struct T { double m, n; }; 923 int S::f( T & t ) { §\C{// multiple aggregate parameters}§924 c; i; d; §\C{\color{red}// this--{\textgreater}.c, this--{\textgreater}.i, this--{\textgreater}.d}§925 `t.`m; `t.`n;§\C{// must qualify}§926 } 927 \end{cfa} 928 929 To simplify the programmer experience, \CFA provides a @with@statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate qualification to fields by opening a scope containing the field identifiers.1183 int S::f( T & t ) { §\C{// multiple aggregate parameters}§ 1184 c; i; d; §\C{\color{red}// this--{\textgreater}.c, this--{\textgreater}.i, this--{\textgreater}.d}§ 1185 ®t.®m; ®t.®n; §\C{// must qualify}§ 1186 } 1187 \end{cfa} 1188 1189 To simplify the programmer experience, \CFA provides a ©with© statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate qualification to fields by opening a scope containing the field identifiers. 930 1190 Hence, the qualified fields become variables with the side-effect that it is easier to optimizing field references in a block. 931 1191 \begin{cfa} 932 void f( S & this ) `with ( this )` {§\C{// with statement}§933 c; i; d; §\C{\color{red}// this.c, this.i, this.d}§1192 void f( S & this ) ®with ( this )® { §\C{// with statement}§ 1193 c; i; d; §\C{\color{red}// this.c, this.i, this.d}§ 934 1194 } 935 1195 \end{cfa} 936 1196 with the generality of opening multiple aggregate-parameters: 937 1197 \begin{cfa} 938 void f( S & s, T & t ) `with ( s, t )` {§\C{// multiple aggregate parameters}§939 c; i; d; §\C{\color{red}// s.c, s.i, s.d}§940 m; n; §\C{\color{red}// t.m, t.n}§941 } 942 \end{cfa} 943 944 In detail, the @with@statement has the form:1198 void f( S & s, T & t ) ®with ( s, t )® { §\C{// multiple aggregate parameters}§ 1199 c; i; d; §\C{\color{red}// s.c, s.i, s.d}§ 1200 m; n; §\C{\color{red}// t.m, t.n}§ 1201 } 1202 \end{cfa} 1203 1204 In detail, the ©with© statement has the form: 945 1205 \begin{cfa} 946 1206 §\emph{with-statement}§: … … 957 1217 The difference between parallel and nesting occurs for fields with the same name and type: 958 1218 \begin{cfa} 959 struct S { int `i`; int j; double m; } s, w;960 struct T { int `i`; int k; int m; } t, w;1219 struct S { int ®i®; int j; double m; } s, w; 1220 struct T { int ®i®; int k; int m; } t, w; 961 1221 with ( s, t ) { 962 j + k; §\C{// unambiguous, s.j + t.k}§963 m = 5.0; §\C{// unambiguous, t.m = 5.0}§964 m = 1; §\C{// unambiguous, s.m = 1}§965 int a = m; §\C{// unambiguous, a = s.i }§966 double b = m; §\C{// unambiguous, b = t.m}§967 int c = s.i + t.i; §\C{// unambiguous, qualification}§968 (double)m; §\C{// unambiguous, cast}§969 } 970 \end{cfa} 971 For parallel semantics, both @s.i@ and @t.i@ are visible, so @i@is ambiguous without qualification;972 for nested semantics, @t.i@ hides @s.i@, so @i@ implies @t.i@.1222 j + k; §\C{// unambiguous, s.j + t.k}§ 1223 m = 5.0; §\C{// unambiguous, t.m = 5.0}§ 1224 m = 1; §\C{// unambiguous, s.m = 1}§ 1225 int a = m; §\C{// unambiguous, a = s.i }§ 1226 double b = m; §\C{// unambiguous, b = t.m}§ 1227 int c = s.i + t.i; §\C{// unambiguous, qualification}§ 1228 (double)m; §\C{// unambiguous, cast}§ 1229 } 1230 \end{cfa} 1231 For parallel semantics, both ©s.i© and ©t.i© are visible, so ©i© is ambiguous without qualification; 1232 for nested semantics, ©t.i© hides ©s.i©, so ©i© implies ©t.i©. 973 1233 \CFA's ability to overload variables means fields with the same name but different types are automatically disambiguated, eliminating most qualification when opening multiple aggregates. 974 1234 Qualification or a cast is used to disambiguate. 975 1235 976 There is an interesting problem between parameters and the function-body @with@, \eg:977 \begin{cfa} 978 void ?{}( S & s, int i ) with ( s ) { §\C{// constructor}§979 `s.i = i;` j = 3; m = 5.5;§\C{// initialize fields}§980 } 981 \end{cfa} 982 Here, the assignment @s.i = i@ means @s.i = s.i@, which is meaningless, and there is no mechanism to qualify the parameter @i@, making the assignment impossible using the function-body @with@.1236 There is an interesting problem between parameters and the function-body ©with©, \eg: 1237 \begin{cfa} 1238 void ?{}( S & s, int i ) with ( s ) { §\C{// constructor}§ 1239 ®s.i = i;® j = 3; m = 5.5; §\C{// initialize fields}§ 1240 } 1241 \end{cfa} 1242 Here, the assignment ©s.i = i© means ©s.i = s.i©, which is meaningless, and there is no mechanism to qualify the parameter ©i©, making the assignment impossible using the function-body ©with©. 983 1243 To solve this problem, parameters are treated like an initialized aggregate: 984 1244 \begin{cfa} … … 990 1250 and implicitly opened \emph{after} a function-body open, to give them higher priority: 991 1251 \begin{cfa} 992 void ?{}( S & s, int `i` ) with ( s ) `with( §\emph{\color{red}params}§ )`{993 s.i = `i`; j = 3; m = 5.5;994 } 995 \end{cfa} 996 Finally, a cast may be used to disambiguate among overload variables in a @with@expression:997 \begin{cfa} 998 with ( w ) { ... } §\C{// ambiguous, same name and no context}§999 with ( (S)w ) { ... } §\C{// unambiguous, cast}§1000 \end{cfa} 1001 and @with@expressions may be complex expressions with type reference (see Section~\ref{s:References}) to aggregate:1252 void ?{}( S & s, int ®i® ) with ( s ) ®with( §\emph{\color{red}params}§ )® { 1253 s.i = ®i®; j = 3; m = 5.5; 1254 } 1255 \end{cfa} 1256 Finally, a cast may be used to disambiguate among overload variables in a ©with© expression: 1257 \begin{cfa} 1258 with ( w ) { ... } §\C{// ambiguous, same name and no context}§ 1259 with ( (S)w ) { ... } §\C{// unambiguous, cast}§ 1260 \end{cfa} 1261 and ©with© expressions may be complex expressions with type reference (see Section~\ref{s:References}) to aggregate: 1002 1262 % \begin{cfa} 1003 1263 % struct S { int i, j; } sv; 1004 % with ( sv ) { §\C{// implicit reference}§1264 % with ( sv ) { §\C{// implicit reference}§ 1005 1265 % S & sr = sv; 1006 % with ( sr ) { §\C{// explicit reference}§1266 % with ( sr ) { §\C{// explicit reference}§ 1007 1267 % S * sp = &sv; 1008 % with ( *sp ) { §\C{// computed reference}§1009 % i = 3; j = 4; §\C{\color{red}// sp--{\textgreater}i, sp--{\textgreater}j}§1268 % with ( *sp ) { §\C{// computed reference}§ 1269 % i = 3; j = 4; §\C{\color{red}// sp--{\textgreater}i, sp--{\textgreater}j}§ 1010 1270 % } 1011 % i = 2; j = 3; §\C{\color{red}// sr.i, sr.j}§1271 % i = 2; j = 3; §\C{\color{red}// sr.i, sr.j}§ 1012 1272 % } 1013 % i = 1; j = 2; §\C{\color{red}// sv.i, sv.j}§1273 % i = 1; j = 2; §\C{\color{red}// sv.i, sv.j}§ 1014 1274 % } 1015 1275 % \end{cfa} … … 1019 1279 class C { 1020 1280 int i, j; 1021 int mem() { §\C{\color{red}// implicit "this" parameter}§1022 i = 1; §\C{\color{red}// this->i}§1023 j = 2; §\C{\color{red}// this->j}§1281 int mem() { §\C{\color{red}// implicit "this" parameter}§ 1282 i = 1; §\C{\color{red}// this->i}§ 1283 j = 2; §\C{\color{red}// this->j}§ 1024 1284 } 1025 1285 } … … 1028 1288 \begin{cfa} 1029 1289 struct S { int i, j; }; 1030 int mem( S & ®this® ) { §\C{// explicit "this" parameter}§1031 ®this.®i = 1; §\C{// "this" is not elided}§1290 int mem( S & ®this® ) { §\C{// explicit "this" parameter}§ 1291 ®this.®i = 1; §\C{// "this" is not elided}§ 1032 1292 ®this.®j = 2; 1033 1293 } … … 1038 1298 \begin{cfa} 1039 1299 int mem( S & this ) ®with( this )® { §\C{// with clause}§ 1040 i = 1; §\C{\color{red}// this.i}§1041 j = 2; §\C{\color{red}// this.j}§1300 i = 1; §\C{\color{red}// this.i}§ 1301 j = 2; §\C{\color{red}// this.j}§ 1042 1302 } 1043 1303 \end{cfa} … … 1056 1316 struct S1 { ... } s1; 1057 1317 struct S2 { ... } s2; 1058 ®with( s1 )® { §\C{// with statement}§1318 ®with( s1 )® { §\C{// with statement}§ 1059 1319 // access fields of s1 without qualification 1060 ®with s2® { §\C{// nesting}§1320 ®with s2® { §\C{// nesting}§ 1061 1321 // access fields of s1 and s2 without qualification 1062 1322 } … … 1113 1373 Non-local transfer can cause stack unwinding, \ie non-local routine termination, depending on the kind of raise. 1114 1374 \begin{cfa} 1115 exception_t E {}; §\C{// exception type}§1375 exception_t E {}; §\C{// exception type}§ 1116 1376 void f(...) { 1117 ... throw E{}; ... §\C{// termination}§1118 ... throwResume E{}; ... §\C{// resumption}§1377 ... throw E{}; ... §\C{// termination}§ 1378 ... throwResume E{}; ... §\C{// resumption}§ 1119 1379 } 1120 1380 try { 1121 1381 f(...); 1122 } catch( E e ; §boolean-predicate§ ) { §\C[8cm]{// termination handler}§1382 } catch( E e ; §boolean-predicate§ ) { §\C[8cm]{// termination handler}§ 1123 1383 // recover and continue 1124 } catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}\CRT§1384 } catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}\CRT§ 1125 1385 // repair and return 1126 1386 } finally { … … 1182 1442 For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way: 1183 1443 \begin{cfa} 1184 int ®(*®f®())[®5®]® {...}; §\C{// definition}§1185 ... ®(*®f®())[®3®]® += 1; §\C{// usage}§1444 int ®(*®f®())[®5®]® {...}; §\C{// definition}§ 1445 ... ®(*®f®())[®3®]® += 1; §\C{// usage}§ 1186 1446 \end{cfa} 1187 1447 Essentially, the return type is wrapped around the routine name in successive layers (like an \Index{onion}). 1188 While attempting to make the two contexts consistent is a laudable goal, it has not worked out in practice. 1448 While attempting to make the two contexts consistent is a laudable goal, it has not worked out in practice, even though Dennis Richie believed otherwise: 1449 \begin{quote} 1450 In spite of its difficulties, I believe that the C's approach to declarations remains plausible, and am comfortable with it; it is a useful unifying principle.~\cite[p.~12]{Ritchie93} 1451 \end{quote} 1189 1452 1190 1453 \CFA provides its own type, variable and routine declarations, using a different syntax. … … 1372 1635 *x = 3; // implicit dereference 1373 1636 int * ®const® y = (int *)104; 1374 *y = *x; // implicit dereference1637 *y = *x; // implicit dereference 1375 1638 \end{cfa} 1376 1639 \end{tabular} … … 1386 1649 \hline 1387 1650 \begin{cfa} 1388 lda r1,100 // load address of x1389 ld r2,(r1) // load value of x1390 lda r3,104 // load address of y1391 st r2,(r3) // store x into y1651 lda r1,100 // load address of x 1652 ld r2,(r1) // load value of x 1653 lda r3,104 // load address of y 1654 st r2,(r3) // store x into y 1392 1655 \end{cfa} 1393 1656 & 1394 1657 \begin{cfa} 1395 1658 1396 ld r2,(100) // load value of x1397 1398 st r2,(104) // store x into y1659 ld r2,(100) // load value of x 1660 1661 st r2,(104) // store x into y 1399 1662 \end{cfa} 1400 1663 \end{tabular} … … 1410 1673 \begin{cfa} 1411 1674 int x, y, ®*® p1, ®*® p2, ®**® p3; 1412 p1 = ®&®x; // p1 points to x1413 p2 = p1; // p2 points to x1414 p1 = ®&®y; // p1 points to y1415 p3 = &p2; // p3 points to p21675 p1 = ®&®x; // p1 points to x 1676 p2 = p1; // p2 points to x 1677 p1 = ®&®y; // p1 points to y 1678 p3 = &p2; // p3 points to p2 1416 1679 \end{cfa} 1417 1680 & … … 1424 1687 For example, \Index*{Algol68}~\cite{Algol68} infers pointer dereferencing to select the best meaning for each pointer usage 1425 1688 \begin{cfa} 1426 p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§1689 p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§ 1427 1690 \end{cfa} 1428 1691 Algol68 infers the following dereferencing ©*p2 = *p1 + x©, because adding the arbitrary integer value in ©x© to the address of ©p1© and storing the resulting address into ©p2© is an unlikely operation. 1429 Unfortunately, automatic dereferencing does not work in all cases, and so some mechanism is necessary to fix incorrect choices. 1692 Unfortunately, automatic dereferencing does not work in all cases, and so some mechanism is necessary to fix incorrect choices. 1430 1693 1431 1694 Rather than inferring dereference, most programming languages pick one implicit dereferencing semantics, and the programmer explicitly indicates the other to resolve address-duality. 1432 1695 In C, objects of pointer type always manipulate the pointer object's address: 1433 1696 \begin{cfa} 1434 p1 = p2; §\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}§1435 p2 = p1 + x; §\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}§1697 p1 = p2; §\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}§ 1698 p2 = p1 + x; §\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}§ 1436 1699 \end{cfa} 1437 1700 even though the assignment to ©p2© is likely incorrect, and the programmer probably meant: 1438 1701 \begin{cfa} 1439 p1 = p2; §\C{// pointer address assignment}§1440 ®*®p2 = ®*®p1 + x; §\C{// pointed-to value assignment / operation}§1702 p1 = p2; §\C{// pointer address assignment}§ 1703 ®*®p2 = ®*®p1 + x; §\C{// pointed-to value assignment / operation}§ 1441 1704 \end{cfa} 1442 1705 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©). … … 1455 1718 \begin{cfa} 1456 1719 int x, y, ®&® r1, ®&® r2, ®&&® r3; 1457 ®&®r1 = &x; §\C{// r1 points to x}§1458 ®&®r2 = &r1; §\C{// r2 points to x}§1459 ®&®r1 = &y; §\C{// r1 points to y}§1460 ®&&®r3 = ®&®&r2; §\C{// r3 points to r2}§1720 ®&®r1 = &x; §\C{// r1 points to x}§ 1721 ®&®r2 = &r1; §\C{// r2 points to x}§ 1722 ®&®r1 = &y; §\C{// r1 points to y}§ 1723 ®&&®r3 = ®&®&r2; §\C{// r3 points to r2}§ 1461 1724 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); §\C{// implicit dereferencing}§ 1462 1725 \end{cfa} … … 1474 1737 For a \CFA reference type, the cancellation on the left-hand side of assignment leaves the reference as an address (\Index{lvalue}): 1475 1738 \begin{cfa} 1476 (&®*®)r1 = &x; §\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}§1739 (&®*®)r1 = &x; §\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}§ 1477 1740 \end{cfa} 1478 1741 Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}): 1479 1742 \begin{cfa} 1480 (&(&®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}§1743 (&(&®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}§ 1481 1744 \end{cfa} 1482 1745 Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth. … … 1486 1749 int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, 1487 1750 &r1 = x, &&r2 = r1, &&&r3 = r2; 1488 ***p3 = 3; §\C{// change x}§1489 r3 = 3; §\C{// change x, ***r3}§1490 **p3 = ...; §\C{// change p1}§1491 &r3 = ...; §\C{// change r1, (\&*)**r3, 1 cancellation}§1492 *p3 = ...; §\C{// change p2}§1493 &&r3 = ...; §\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}§1494 &&&r3 = p3; §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§1751 ***p3 = 3; §\C{// change x}§ 1752 r3 = 3; §\C{// change x, ***r3}§ 1753 **p3 = ...; §\C{// change p1}§ 1754 &r3 = ...; §\C{// change r1, (\&*)**r3, 1 cancellation}§ 1755 *p3 = ...; §\C{// change p2}§ 1756 &&r3 = ...; §\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}§ 1757 &&&r3 = p3; §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§ 1495 1758 \end{cfa} 1496 1759 Furthermore, both types are equally performant, as the same amount of dereferencing occurs for both types. … … 1499 1762 As for a pointer type, a reference type may have qualifiers: 1500 1763 \begin{cfa} 1501 const int cx = 5; §\C{// cannot change cx;}§1502 const int & cr = cx; §\C{// cannot change what cr points to}§1503 ®&®cr = &cx; §\C{// can change cr}§1504 cr = 7; §\C{// error, cannot change cx}§1505 int & const rc = x; §\C{// must be initialized}§1506 ®&®rc = &x; §\C{// error, cannot change rc}§1507 const int & const crc = cx; §\C{// must be initialized}§1508 crc = 7; §\C{// error, cannot change cx}§1509 ®&®crc = &cx; §\C{// error, cannot change crc}§1764 const int cx = 5; §\C{// cannot change cx;}§ 1765 const int & cr = cx; §\C{// cannot change what cr points to}§ 1766 ®&®cr = &cx; §\C{// can change cr}§ 1767 cr = 7; §\C{// error, cannot change cx}§ 1768 int & const rc = x; §\C{// must be initialized}§ 1769 ®&®rc = &x; §\C{// error, cannot change rc}§ 1770 const int & const crc = cx; §\C{// must be initialized}§ 1771 crc = 7; §\C{// error, cannot change cx}§ 1772 ®&®crc = &cx; §\C{// error, cannot change crc}§ 1510 1773 \end{cfa} 1511 1774 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}: 1512 1775 \begin{cfa} 1513 int & const cr = *0; §\C{// where 0 is the int * zero}§1776 int & const cr = *0; §\C{// where 0 is the int * zero}§ 1514 1777 \end{cfa} 1515 1778 Note, constant reference-types do not prevent \Index{addressing errors} because of explicit storage-management: … … 1518 1781 cr = 5; 1519 1782 free( &cr ); 1520 cr = 7; §\C{// unsound pointer dereference}§1783 cr = 7; §\C{// unsound pointer dereference}§ 1521 1784 \end{cfa} 1522 1785 … … 1543 1806 \begin{cfa} 1544 1807 int w, x, y, z, & ar[3] = { x, y, z }; §\C{// initialize array of references}§ 1545 &ar[1] = &w; §\C{// change reference array element}§1546 typeof( ar[1] ) p; §\C{// (gcc) is int, \ie the type of referenced object}§1547 typeof( &ar[1] ) q; §\C{// (gcc) is int \&, \ie the type of reference}§1548 sizeof( ar[1] ) == sizeof( int ); §\C{// is true, \ie the size of referenced object}§1549 sizeof( &ar[1] ) == sizeof( int *) §\C{// is true, \ie the size of a reference}§1808 &ar[1] = &w; §\C{// change reference array element}§ 1809 typeof( ar[1] ) p; §\C{// (gcc) is int, \ie the type of referenced object}§ 1810 typeof( &ar[1] ) q; §\C{// (gcc) is int \&, \ie the type of reference}§ 1811 sizeof( ar[1] ) == sizeof( int ); §\C{// is true, \ie the size of referenced object}§ 1812 sizeof( &ar[1] ) == sizeof( int *) §\C{// is true, \ie the size of a reference}§ 1550 1813 \end{cfa} 1551 1814 … … 1564 1827 Therefore, for pointer/reference initialization, the initializing value must be an address not a value. 1565 1828 \begin{cfa} 1566 int * p = &x; §\C{// assign address of x}§1567 ®int * p = x;® §\C{// assign value of x}§1568 int & r = x; §\C{// must have address of x}§1829 int * p = &x; §\C{// assign address of x}§ 1830 ®int * p = x;® §\C{// assign value of x}§ 1831 int & r = x; §\C{// must have address of x}§ 1569 1832 \end{cfa} 1570 1833 Like the previous example with C pointer-arithmetic, it is unlikely assigning the value of ©x© into a pointer is meaningful (again, a warning is usually given). … … 1575 1838 Similarly, when a reference type is used for a parameter/return type, the call-site argument does not require a reference operator for the same reason. 1576 1839 \begin{cfa} 1577 int & f( int & r ); §\C{// reference parameter and return}§1578 z = f( x ) + f( y ); §\C{// reference operator added, temporaries needed for call results}§1840 int & f( int & r ); §\C{// reference parameter and return}§ 1841 z = f( x ) + f( y ); §\C{// reference operator added, temporaries needed for call results}§ 1579 1842 \end{cfa} 1580 1843 Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©r© can be locally reassigned within ©f©. … … 1603 1866 void f( int & r ); 1604 1867 void g( int * p ); 1605 f( 3 ); g( ®&®3 ); §\C{// compiler implicit generates temporaries}§1606 f( x + y ); g( ®&®(x + y) ); §\C{// compiler implicit generates temporaries}§1868 f( 3 ); g( ®&®3 ); §\C{// compiler implicit generates temporaries}§ 1869 f( x + y ); g( ®&®(x + y) ); §\C{// compiler implicit generates temporaries}§ 1607 1870 \end{cfa} 1608 1871 Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{ … … 1615 1878 \begin{cfa} 1616 1879 void f( int i ); 1617 void (* fp)( int ); §\C{// routine pointer}§1618 fp = f; §\C{// reference initialization}§1619 fp = &f; §\C{// pointer initialization}§1620 fp = *f; §\C{// reference initialization}§1621 fp(3); §\C{// reference invocation}§1622 (*fp)(3); §\C{// pointer invocation}§1880 void (* fp)( int ); §\C{// routine pointer}§ 1881 fp = f; §\C{// reference initialization}§ 1882 fp = &f; §\C{// pointer initialization}§ 1883 fp = *f; §\C{// reference initialization}§ 1884 fp(3); §\C{// reference invocation}§ 1885 (*fp)(3); §\C{// pointer invocation}§ 1623 1886 \end{cfa} 1624 1887 While C's treatment of routine objects has similarity to inferring a reference type in initialization contexts, the examples are assignment not initialization, and all possible forms of assignment are possible (©f©, ©&f©, ©*f©) without regard for type. 1625 1888 Instead, a routine object should be referenced by a ©const© reference: 1626 1889 \begin{cfa} 1627 ®const® void (®&® fr)( int ) = f; §\C{// routine reference}§1628 fr = ... §\C{// error, cannot change code}§1629 &fr = ...; §\C{// changing routine reference}§1630 fr( 3 ); §\C{// reference call to f}§1631 (*fr)(3); §\C{// error, incorrect type}§1890 ®const® void (®&® fr)( int ) = f; §\C{// routine reference}§ 1891 fr = ... §\C{// error, cannot change code}§ 1892 &fr = ...; §\C{// changing routine reference}§ 1893 fr( 3 ); §\C{// reference call to f}§ 1894 (*fr)(3); §\C{// error, incorrect type}§ 1632 1895 \end{cfa} 1633 1896 because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{ … … 1642 1905 \begin{itemize} 1643 1906 \item 1644 if ©R© is an \Index{rvalue} of type ©T & $_1$...&$_r$© where $r \ge 1$ references (©&© symbols) then ©&R© has type ©T ®*®&$_{\color{red}2}$...&$_{\color{red}r}$©, \ie ©T© pointer with $r-1$ references (©&© symbols).1645 1646 \item 1647 if ©L© is an \Index{lvalue} of type ©T & $_1$...&$_l$© where $l \ge 0$ references (©&© symbols) then ©&L© has type ©T ®*®&$_{\color{red}1}$...&$_{\color{red}l}$©, \ie ©T© pointer with $l$ references (©&© symbols).1907 if ©R© is an \Index{rvalue} of type ©T &©$_1\cdots$ ©&©$_r$, where $r \ge 1$ references (©&© symbols), than ©&R© has type ©T ®*®&©$_{\color{red}2}\cdots$ ©&©$_{\color{red}r}$, \ie ©T© pointer with $r-1$ references (©&© symbols). 1908 1909 \item 1910 if ©L© is an \Index{lvalue} of type ©T &©$_1\cdots$ ©&©$_l$, where $l \ge 0$ references (©&© symbols), than ©&L© has type ©T ®*®&©$_{\color{red}1}\cdots$ ©&©$_{\color{red}l}$, \ie ©T© pointer with $l$ references (©&© symbols). 1648 1911 \end{itemize} 1649 1912 The following example shows the first rule applied to different \Index{rvalue} contexts: … … 1651 1914 int x, * px, ** ppx, *** pppx, **** ppppx; 1652 1915 int & rx = x, && rrx = rx, &&& rrrx = rrx ; 1653 x = rrrx; // rrrx is an lvalue with type int &&& (equivalent to x)1654 px = &rrrx; // starting from rrrx, &rrrx is an rvalue with type int *&&& (&x)1655 ppx = &&rrrx; // starting from &rrrx, &&rrrx is an rvalue with type int **&& (&rx)1656 pppx = &&&rrrx; // starting from &&rrrx, &&&rrrx is an rvalue with type int ***& (&rrx)1657 ppppx = &&&&rrrx; // starting from &&&rrrx, &&&&rrrx is an rvalue with type int **** (&rrrx)1916 x = rrrx; §\C[2.0in]{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}§ 1917 px = &rrrx; §\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (\&x)}§ 1918 ppx = &&rrrx; §\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (\&rx)}§ 1919 pppx = &&&rrrx; §\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (\&rrx)}§ 1920 ppppx = &&&&rrrx; §\C{// starting from \&\&\&rrrx, \&\&\&\&rrrx is an rvalue with type int **** (\&rrrx)}§ 1658 1921 \end{cfa} 1659 1922 The following example shows the second rule applied to different \Index{lvalue} contexts: … … 1661 1924 int x, * px, ** ppx, *** pppx; 1662 1925 int & rx = x, && rrx = rx, &&& rrrx = rrx ; 1663 rrrx = 2; // rrrx is an lvalue with type int &&& (equivalent to x)1664 &rrrx = px; // starting from rrrx, &rrrx is an rvalue with type int *&&& (rx)1665 &&rrrx = ppx; // starting from &rrrx, &&rrrx is an rvalue with type int **&& (rrx)1666 &&&rrrx = pppx; // starting from &&rrrx, &&&rrrx is an rvalue with type int ***& (rrrx)1926 rrrx = 2; §\C{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}§ 1927 &rrrx = px; §\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (rx)}§ 1928 &&rrrx = ppx; §\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (rrx)}§ 1929 &&&rrrx = pppx; §\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (rrrx)}\CRT§ 1667 1930 \end{cfa} 1668 1931 … … 1677 1940 \begin{cfa} 1678 1941 int x; 1679 x + 1; // lvalue variable (int) converts to rvalue for expression1942 x + 1; §\C[2.0in]{// lvalue variable (int) converts to rvalue for expression}§ 1680 1943 \end{cfa} 1681 1944 An rvalue has no type qualifiers (©cv©), so the lvalue qualifiers are dropped. … … 1687 1950 \begin{cfa} 1688 1951 int x, &r = x, f( int p ); 1689 x = ®r® + f( ®r® ); // lvalue reference converts to rvalue1952 x = ®r® + f( ®r® ); §\C{// lvalue reference converts to rvalue}§ 1690 1953 \end{cfa} 1691 1954 An rvalue has no type qualifiers (©cv©), so the reference qualifiers are dropped. … … 1694 1957 lvalue to reference conversion: \lstinline[deletekeywords=lvalue]@lvalue-type cv1 T@ converts to ©cv2 T &©, which allows implicitly converting variables to references. 1695 1958 \begin{cfa} 1696 int x, &r = ®x®, f( int & p ); // lvalue variable (int) convert to reference (int &)1697 f( ®x® ); // lvalue variable (int) convert to reference (int &)1959 int x, &r = ®x®, f( int & p ); §\C{// lvalue variable (int) convert to reference (int \&)}§ 1960 f( ®x® ); §\C{// lvalue variable (int) convert to reference (int \&)}§ 1698 1961 \end{cfa} 1699 1962 Conversion can restrict a type, where ©cv1© $\le$ ©cv2©, \eg passing an ©int© to a ©const volatile int &©, which has low cost. … … 1705 1968 \begin{cfa} 1706 1969 int x, & f( int & p ); 1707 f( ®x + 3® ); // rvalue parameter (int) implicitly converts to lvalue temporary reference (int &)1708 ®&f®(...) = &x; // rvalue result (int &) implicitly converts to lvalue temporary reference (int &)1970 f( ®x + 3® ); §\C[1.5in]{// rvalue parameter (int) implicitly converts to lvalue temporary reference (int \&)}§ 1971 ®&f®(...) = &x; §\C{// rvalue result (int \&) implicitly converts to lvalue temporary reference (int \&)}\CRT§ 1709 1972 \end{cfa} 1710 1973 In both case, modifications to the temporary are inaccessible (\Index{warning}). … … 1895 2158 in both cases the type is assumed to be void as opposed to old style C defaults of int return type and unknown parameter types, respectively, as in: 1896 2159 \begin{cfa} 1897 [§\,§] g(); §\C{// no input or output parameters}§1898 [ void ] g( void ); §\C{// no input or output parameters}§2160 [§\,§] g(); §\C{// no input or output parameters}§ 2161 [ void ] g( void ); §\C{// no input or output parameters}§ 1899 2162 \end{cfa} 1900 2163 … … 1914 2177 \begin{cfa} 1915 2178 typedef int foo; 1916 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§2179 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§ 1917 2180 \end{cfa} 1918 2181 The string ``©int (* foo)©'' declares a C-style named-parameter of type pointer to an integer (the parenthesis are superfluous), while the same string declares a \CFA style unnamed parameter of type routine returning integer with unnamed parameter of type pointer to foo. … … 1922 2185 C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg: 1923 2186 \begin{cfa} 1924 [ int ] f( * int, int * ); §\C{// returns an integer, accepts 2 pointers to integers}§1925 [ * int, int * ] f( int ); §\C{// returns 2 pointers to integers, accepts an integer}§2187 [ int ] f( * int, int * ); §\C{// returns an integer, accepts 2 pointers to integers}§ 2188 [ * int, int * ] f( int ); §\C{// returns 2 pointers to integers, accepts an integer}§ 1926 2189 \end{cfa} 1927 2190 The reason for allowing both declaration styles in the new context is for backwards compatibility with existing preprocessor macros that generate C-style declaration-syntax, as in: 1928 2191 \begin{cfa} 1929 2192 #define ptoa( n, d ) int (*n)[ d ] 1930 int f( ptoa( p, 5 ) ) ... §\C{// expands to int f( int (*p)[ 5 ] )}§1931 [ int ] f( ptoa( p, 5 ) ) ... §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§2193 int f( ptoa( p, 5 ) ) ... §\C{// expands to int f( int (*p)[ 5 ] )}§ 2194 [ int ] f( ptoa( p, 5 ) ) ... §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§ 1932 2195 \end{cfa} 1933 2196 Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms. … … 1951 2214 int z; 1952 2215 ... x = 0; ... y = z; ... 1953 ®return;® §\C{// implicitly return x, y}§2216 ®return;® §\C{// implicitly return x, y}§ 1954 2217 } 1955 2218 \end{cfa} … … 1961 2224 [ int x, int y ] f() { 1962 2225 ... 1963 } §\C{// implicitly return x, y}§2226 } §\C{// implicitly return x, y}§ 1964 2227 \end{cfa} 1965 2228 In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return© had been encountered. … … 1970 2233 [ int x, int y ] f( int, x, int y ) { 1971 2234 ... 1972 } §\C{// implicitly return x, y}§2235 } §\C{// implicitly return x, y}§ 1973 2236 \end{cfa} 1974 2237 This notation allows the compiler to eliminate temporary variables in nested routine calls. 1975 2238 \begin{cfa} 1976 [ int x, int y ] f( int, x, int y ); §\C{// prototype declaration}§2239 [ int x, int y ] f( int, x, int y ); §\C{// prototype declaration}§ 1977 2240 int a, b; 1978 2241 [a, b] = f( f( f( a, b ) ) ); … … 1988 2251 as well, parameter names are optional, \eg: 1989 2252 \begin{cfa} 1990 [ int x ] f (); §\C{// returning int with no parameters}§1991 [ * int ] g (int y); §\C{// returning pointer to int with int parameter}§1992 [ ] h ( int, char ); §\C{// returning no result with int and char parameters}§1993 [ * int, int ] j ( int ); §\C{// returning pointer to int and int, with int parameter}§2253 [ int x ] f (); §\C{// returning int with no parameters}§ 2254 [ * int ] g (int y); §\C{// returning pointer to int with int parameter}§ 2255 [ ] h ( int, char ); §\C{// returning no result with int and char parameters}§ 2256 [ * int, int ] j ( int ); §\C{// returning pointer to int and int, with int parameter}§ 1994 2257 \end{cfa} 1995 2258 This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa). … … 2012 2275 The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg: 2013 2276 \begin{cfa} 2014 * [ int x ] () fp; §\C{// pointer to routine returning int with no parameters}§2015 * [ * int ] (int y) gp; §\C{// pointer to routine returning pointer to int with int parameter}§2016 * [ ] (int,char) hp; §\C{// pointer to routine returning no result with int and char parameters}§2017 * [ * int,int ] ( int ) jp; §\C{// pointer to routine returning pointer to int and int, with int parameter}§2277 * [ int x ] () fp; §\C{// pointer to routine returning int with no parameters}§ 2278 * [ * int ] (int y) gp; §\C{// pointer to routine returning pointer to int with int parameter}§ 2279 * [ ] (int,char) hp; §\C{// pointer to routine returning no result with int and char parameters}§ 2280 * [ * int,int ] ( int ) jp; §\C{// pointer to routine returning pointer to int and int, with int parameter}§ 2018 2281 \end{cfa} 2019 2282 While parameter names are optional, \emph{a routine name cannot be specified}; 2020 2283 for example, the following is incorrect: 2021 2284 \begin{cfa} 2022 * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§2285 * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§ 2023 2286 \end{cfa} 2024 2287 … … 2043 2306 whereas a named (keyword) call may be: 2044 2307 \begin{cfa} 2045 p( z : 3, x : 4, y : 7 ); §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§2308 p( z : 3, x : 4, y : 7 ); §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§ 2046 2309 \end{cfa} 2047 2310 Here the order of the arguments is unimportant, and the names of the parameters are used to associate argument values with the corresponding parameters. … … 2060 2323 For example, the following routine prototypes and definition are all valid. 2061 2324 \begin{cfa} 2062 void p( int, int, int ); §\C{// equivalent prototypes}§2325 void p( int, int, int ); §\C{// equivalent prototypes}§ 2063 2326 void p( int x, int y, int z ); 2064 2327 void p( int y, int x, int z ); 2065 2328 void p( int z, int y, int x ); 2066 void p( int q, int r, int s ) {} §\C{// match with this definition}§2329 void p( int q, int r, int s ) {} §\C{// match with this definition}§ 2067 2330 \end{cfa} 2068 2331 Forcing matching parameter names in routine prototypes with corresponding routine definitions is possible, but goes against a strong tradition in C programming. … … 2076 2339 int f( int x, double y ); 2077 2340 2078 f( j : 3, i : 4 ); §\C{// 1st f}§2079 f( x : 7, y : 8.1 ); §\C{// 2nd f}§2080 f( 4, 5 ); §\C{// ambiguous call}§2341 f( j : 3, i : 4 ); §\C{// 1st f}§ 2342 f( x : 7, y : 8.1 ); §\C{// 2nd f}§ 2343 f( 4, 5 ); §\C{// ambiguous call}§ 2081 2344 \end{cfa} 2082 2345 However, named arguments compound routine resolution in conjunction with conversions: 2083 2346 \begin{cfa} 2084 f( i : 3, 5.7 ); §\C{// ambiguous call ?}§2347 f( i : 3, 5.7 ); §\C{// ambiguous call ?}§ 2085 2348 \end{cfa} 2086 2349 Depending on the cost associated with named arguments, this call could be resolvable or ambiguous. … … 2096 2359 the allowable positional calls are: 2097 2360 \begin{cfa} 2098 p(); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§2099 p( 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§2100 p( 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§2101 p( 4, 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 4 )}§2361 p(); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§ 2362 p( 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§ 2363 p( 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§ 2364 p( 4, 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 4 )}§ 2102 2365 // empty arguments 2103 p( , 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 4 )}§2104 p( 4, , 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 4 )}§2105 p( 4, 4, ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§2106 p( 4, , ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§2107 p( , 4, ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 3 )}§2108 p( , , 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 4 )}§2109 p( , , ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§2366 p( , 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 4 )}§ 2367 p( 4, , 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 4 )}§ 2368 p( 4, 4, ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§ 2369 p( 4, , ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§ 2370 p( , 4, ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 3 )}§ 2371 p( , , 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 4 )}§ 2372 p( , , ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§ 2110 2373 \end{cfa} 2111 2374 Here the missing arguments are inserted from the default values in the parameter list. … … 2131 2394 Default values may only appear in a prototype versus definition context: 2132 2395 \begin{cfa} 2133 void p( int x, int y = 2, int z = 3 ); §\C{// prototype: allowed}§2134 void p( int, int = 2, int = 3 ); §\C{// prototype: allowed}§2135 void p( int x, int y = 2, int z = 3 ) {} §\C{// definition: not allowed}§2396 void p( int x, int y = 2, int z = 3 ); §\C{// prototype: allowed}§ 2397 void p( int, int = 2, int = 3 ); §\C{// prototype: allowed}§ 2398 void p( int x, int y = 2, int z = 3 ) {} §\C{// definition: not allowed}§ 2136 2399 \end{cfa} 2137 2400 The reason for this restriction is to allow separate compilation. … … 2158 2421 \begin{cfa} 2159 2422 void p( int x, int y = 2, int z = 3... ); 2160 p( 1, 4, 5, 6, z : 3 ); §\C{// assume p( /* positional */, ... , /* named */ );}§2161 p( 1, z : 3, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§2423 p( 1, 4, 5, 6, z : 3 ); §\C{// assume p( /* positional */, ... , /* named */ );}§ 2424 p( 1, z : 3, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§ 2162 2425 \end{cfa} 2163 2426 The first call is an error because arguments 4 and 5 are actually positional not ellipse arguments; … … 2189 2452 Furthermore, overloading cannot handle accessing default arguments in the middle of a positional list, via a missing argument, such as: 2190 2453 \begin{cfa} 2191 p( 1, /* default */, 5 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§2454 p( 1, /* default */, 5 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§ 2192 2455 \end{cfa} 2193 2456 … … 2202 2465 \begin{cfa} 2203 2466 struct { 2204 int f1; §\C{// named field}§2205 int f2 : 4; §\C{// named field with bit field size}§2206 int : 3; §\C{// unnamed field for basic type with bit field size}§2207 int ; §\C{// disallowed, unnamed field}§2208 int *; §\C{// disallowed, unnamed field}§2209 int (*)( int ); §\C{// disallowed, unnamed field}§2467 int f1; §\C{// named field}§ 2468 int f2 : 4; §\C{// named field with bit field size}§ 2469 int : 3; §\C{// unnamed field for basic type with bit field size}§ 2470 int ; §\C{// disallowed, unnamed field}§ 2471 int *; §\C{// disallowed, unnamed field}§ 2472 int (*)( int ); §\C{// disallowed, unnamed field}§ 2210 2473 }; 2211 2474 \end{cfa} … … 2215 2478 \begin{cfa} 2216 2479 struct { 2217 int , , ; §\C{// 3 unnamed fields}§2480 int , , ; §\C{// 3 unnamed fields}§ 2218 2481 } 2219 2482 \end{cfa} … … 2262 2525 struct T t; 2263 2526 } s; 2264 2527 2265 2528 2266 2529 … … 2309 2572 const unsigned int size = 5; 2310 2573 int ia[size]; 2311 ... §\C{// assign values to array ia}§2312 qsort( ia, size ); §\C{// sort ascending order using builtin ?<?}§2574 ... §\C{// assign values to array ia}§ 2575 qsort( ia, size ); §\C{// sort ascending order using builtin ?<?}§ 2313 2576 { 2314 2577 ®int ?<?( int x, int y ) { return x > y; }® §\C{// nested routine}§ 2315 qsort( ia, size ); §\C{// sort descending order by local redefinition}§2578 qsort( ia, size ); §\C{// sort descending order by local redefinition}§ 2316 2579 } 2317 2580 \end{cfa} … … 2321 2584 The following program in undefined in \CFA (and Indexc{gcc}) 2322 2585 \begin{cfa} 2323 [* [int]( int )] foo() { §\C{// int (* foo())( int )}§2586 [* [int]( int )] foo() { §\C{// int (* foo())( int )}§ 2324 2587 int ®i® = 7; 2325 2588 int bar( int p ) { 2326 ®i® += 1; §\C{// dependent on local variable}§2327 sout | ®i® | endl;2589 ®i® += 1; §\C{// dependent on local variable}§ 2590 sout | ®i®; 2328 2591 } 2329 return bar; §\C{// undefined because of local dependence}§2592 return bar; §\C{// undefined because of local dependence}§ 2330 2593 } 2331 2594 int main() { 2332 * [int]( int ) fp = foo(); §\C{// int (* fp)( int )}§2333 sout | fp( 3 ) | endl;2334 } 2335 \end{cfa} 2336 because 2595 * [int]( int ) fp = foo(); §\C{// int (* fp)( int )}§ 2596 sout | fp( 3 ); 2597 } 2598 \end{cfa} 2599 because 2337 2600 2338 2601 Currently, there are no \Index{lambda} expressions, \ie unnamed routines because routine names are very important to properly select the correct routine. … … 2343 2606 In C and \CFA, lists of elements appear in several contexts, such as the parameter list of a routine call. 2344 2607 \begin{cfa} 2345 f( ®2, x, 3 + i® ); §\C{// element list}§2608 f( ®2, x, 3 + i® ); §\C{// element list}§ 2346 2609 \end{cfa} 2347 2610 A list of elements is called a \newterm{tuple}, and is different from a \Index{comma expression}. … … 2360 2623 typedef struct { int quot, rem; } div_t; §\C[7cm]{// from include stdlib.h}§ 2361 2624 div_t div( int num, int den ); 2362 div_t qr = div( 13, 5 ); §\C{// return quotient/remainder aggregate}§2363 printf( "%d %d\n", qr.quot, qr.rem ); §\C{// print quotient/remainder}§2625 div_t qr = div( 13, 5 ); §\C{// return quotient/remainder aggregate}§ 2626 printf( "%d %d\n", qr.quot, qr.rem ); §\C{// print quotient/remainder}§ 2364 2627 \end{cfa} 2365 2628 This approach requires a name for the return type and fields, where \Index{naming} is a common programming-language issue. … … 2371 2634 For example, consider C's \Indexc{modf} function, which returns the integral and fractional part of a floating value. 2372 2635 \begin{cfa} 2373 double modf( double x, double * i ); §\C{// from include math.h}§2374 double intp, frac = modf( 13.5, &intp ); §\C{// return integral and fractional components}§2375 printf( "%g %g\n", intp, frac ); §\C{// print integral/fractional components}§2636 double modf( double x, double * i ); §\C{// from include math.h}§ 2637 double intp, frac = modf( 13.5, &intp ); §\C{// return integral and fractional components}§ 2638 printf( "%g %g\n", intp, frac ); §\C{// print integral/fractional components}§ 2376 2639 \end{cfa} 2377 2640 This approach requires allocating storage for the return values, which complicates the call site with a sequence of variable declarations leading to the call. … … 2400 2663 When a function call is passed as an argument to another call, the best match of actual arguments to formal parameters is evaluated given all possible expression interpretations in the current scope. 2401 2664 \begin{cfa} 2402 void g( int, int ); §\C{// 1}§2403 void g( double, double ); §\C{// 2}§2404 g( div( 13, 5 ) ); §\C{// select 1}§2405 g( modf( 13.5 ) ); §\C{// select 2}§2665 void g( int, int ); §\C{// 1}§ 2666 void g( double, double ); §\C{// 2}§ 2667 g( div( 13, 5 ) ); §\C{// select 1}§ 2668 g( modf( 13.5 ) ); §\C{// select 2}§ 2406 2669 \end{cfa} 2407 2670 In this case, there are two overloaded ©g© routines. … … 2412 2675 The previous examples can be rewritten passing the multiple returned-values directly to the ©printf© function call. 2413 2676 \begin{cfa} 2414 [ int, int ] div( int x, int y ); §\C{// from include stdlib}§2415 printf( "%d %d\n", div( 13, 5 ) ); §\C{// print quotient/remainder}§2416 2417 [ double, double ] modf( double x ); §\C{// from include math}§2418 printf( "%g %g\n", modf( 13.5 ) ); §\C{// print integral/fractional components}§2677 [ int, int ] div( int x, int y ); §\C{// from include stdlib}§ 2678 printf( "%d %d\n", div( 13, 5 ) ); §\C{// print quotient/remainder}§ 2679 2680 [ double, double ] modf( double x ); §\C{// from include math}§ 2681 printf( "%g %g\n", modf( 13.5 ) ); §\C{// print integral/fractional components}§ 2419 2682 \end{cfa} 2420 2683 This approach provides the benefits of compile-time checking for appropriate return statements as in aggregation, but without the required verbosity of declaring a new named type. … … 2426 2689 \begin{cfa} 2427 2690 int quot, rem; 2428 [ quot, rem ] = div( 13, 5 ); §\C{// assign multiple variables}§2429 printf( "%d %d\n", quot, rem ); §\C{// print quotient/remainder}\CRT§2691 [ quot, rem ] = div( 13, 5 ); §\C{// assign multiple variables}§ 2692 printf( "%d %d\n", quot, rem ); §\C{// print quotient/remainder}\CRT§ 2430 2693 \end{cfa} 2431 2694 Here, the multiple return-values are matched in much the same way as passing multiple return-values to multiple parameters in a call. … … 2433 2696 2434 2697 \subsection{Expressions} 2698 2699 % Change order of expression evaluation. 2700 % http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0145r2.pdf 2435 2701 2436 2702 Multiple-return-value functions provide \CFA with a new syntax for expressing a combination of expressions in the return statement and a combination of types in a function signature. … … 2453 2719 In \CFA, it is possible to overcome this restriction by declaring a \newterm{tuple variable}. 2454 2720 \begin{cfa} 2455 [int, int] ®qr® = div( 13, 5 ); §\C{// initialize tuple variable}§2456 printf( "%d %d\n", ®qr® ); §\C{// print quotient/remainder}§2721 [int, int] ®qr® = div( 13, 5 ); §\C{// initialize tuple variable}§ 2722 printf( "%d %d\n", ®qr® ); §\C{// print quotient/remainder}§ 2457 2723 \end{cfa} 2458 2724 It is now possible to match the multiple return-values to a single variable, in much the same way as \Index{aggregation}. … … 2460 2726 One way to access the individual components of a tuple variable is with assignment. 2461 2727 \begin{cfa} 2462 [ quot, rem ] = qr; §\C{// assign multiple variables}§2728 [ quot, rem ] = qr; §\C{// assign multiple variables}§ 2463 2729 \end{cfa} 2464 2730 … … 2483 2749 [int, double] * p; 2484 2750 2485 int y = x.0; §\C{// access int component of x}§2486 y = f().1; §\C{// access int component of f}§2487 p->0 = 5; §\C{// access int component of tuple pointed-to by p}§2488 g( x.1, x.0 ); §\C{// rearrange x to pass to g}§2489 double z = [ x, f() ].0.1; §\C{// access second component of first component of tuple expression}§2751 int y = x.0; §\C{// access int component of x}§ 2752 y = f().1; §\C{// access int component of f}§ 2753 p->0 = 5; §\C{// access int component of tuple pointed-to by p}§ 2754 g( x.1, x.0 ); §\C{// rearrange x to pass to g}§ 2755 double z = [ x, f() ].0.1; §\C{// access second component of first component of tuple expression}§ 2490 2756 \end{cfa} 2491 2757 Tuple-index expressions can occur on any tuple-typed expression, including tuple-returning functions, square-bracketed tuple expressions, and other tuple-index expressions, provided the retrieved component is also a tuple. … … 2554 2820 double y; 2555 2821 [int, double] z; 2556 [y, x] = 3.14; §\C{// mass assignment}§2822 [y, x] = 3.14; §\C{// mass assignment}§ 2557 2823 [x, y] = z; §\C{// multiple assignment}§ 2558 2824 z = 10; §\C{// mass assignment}§ 2559 z = [x, y]; §\C{// multiple assignment}§2825 z = [x, y]; §\C{// multiple assignment}§ 2560 2826 \end{cfa} 2561 2827 Let $L_i$ for $i$ in $[0, n)$ represent each component of the flattened left side, $R_i$ represent each component of the flattened right side of a multiple assignment, and $R$ represent the right side of a mass assignment. … … 2601 2867 double c, d; 2602 2868 [ void ] f( [ int, int ] ); 2603 f( [ c, a ] = [ b, d ] = 1.5 ); // assignments in parameter list2869 f( [ c, a ] = [ b, d ] = 1.5 ); §\C{// assignments in parameter list}§ 2604 2870 \end{cfa} 2605 2871 The tuple expression begins with a mass assignment of ©1.5© into ©[b, d]©, which assigns ©1.5© into ©b©, which is truncated to ©1©, and ©1.5© into ©d©, producing the tuple ©[1, 1.5]© as a result. … … 2614 2880 \begin{cfa} 2615 2881 struct S; 2616 void ?{}(S *); // (1)2617 void ?{}(S *, int); // (2)2618 void ?{}(S * double); // (3)2619 void ?{}(S *, S); // (4)2620 2621 [S, S] x = [3, 6.28]; // uses (2), (3), specialized constructors2622 [S, S] y; // uses (1), (1), default constructor2623 [S, S] z = x.0; // uses (4), (4), copy constructor2882 void ?{}(S *); §\C{// (1)}§ 2883 void ?{}(S *, int); §\C{// (2)}§ 2884 void ?{}(S * double); §\C{// (3)}§ 2885 void ?{}(S *, S); §\C{// (4)}§ 2886 2887 [S, S] x = [3, 6.28]; §\C{// uses (2), (3), specialized constructors}§ 2888 [S, S] y; §\C{// uses (1), (1), default constructor}§ 2889 [S, S] z = x.0; §\C{// uses (4), (4), copy constructor}§ 2624 2890 \end{cfa} 2625 2891 In this example, ©x© is initialized by the multiple constructor calls ©?{}(&x.0, 3)© and ©?{}(&x.1, 6.28)©, while ©y© is initialized by two default constructor calls ©?{}(&y.0)© and ©?{}(&y.1)©. … … 2662 2928 A member-access tuple may be used anywhere a tuple can be used, \eg: 2663 2929 \begin{cfa} 2664 s.[ y, z, x ] = [ 3, 3.2, 'x' ]; §\C{// equivalent to s.x = 'x', s.y = 3, s.z = 3.2}§2665 f( s.[ y, z ] ); §\C{// equivalent to f( s.y, s.z )}§2930 s.[ y, z, x ] = [ 3, 3.2, 'x' ]; §\C{// equivalent to s.x = 'x', s.y = 3, s.z = 3.2}§ 2931 f( s.[ y, z ] ); §\C{// equivalent to f( s.y, s.z )}§ 2666 2932 \end{cfa} 2667 2933 Note, the fields appearing in a record-field tuple may be specified in any order; … … 2673 2939 void f( double, long ); 2674 2940 2675 f( x.[ 0, 3 ] ); §\C{// f( x.0, x.3 )}§2676 x.[ 0, 1 ] = x.[ 1, 0 ]; §\C{// [ x.0, x.1 ] = [ x.1, x.0 ]}§2941 f( x.[ 0, 3 ] ); §\C{// f( x.0, x.3 )}§ 2942 x.[ 0, 1 ] = x.[ 1, 0 ]; §\C{// [ x.0, x.1 ] = [ x.1, x.0 ]}§ 2677 2943 [ long, int, long ] y = x.[ 2, 0, 2 ]; 2678 2944 \end{cfa} … … 2691 2957 \begin{cfa} 2692 2958 [ int, float, double ] f(); 2693 [ double, float ] x = f().[ 2, 1 ]; §\C{// f() called once}§2959 [ double, float ] x = f().[ 2, 1 ]; §\C{// f() called once}§ 2694 2960 \end{cfa} 2695 2961 … … 2704 2970 That is, a cast can be used to select the type of an expression when it is ambiguous, as in the call to an overloaded function. 2705 2971 \begin{cfa} 2706 int f(); // (1)2707 double f(); // (2)2708 2709 f(); // ambiguous - (1),(2) both equally viable2710 (int)f(); // choose (2)2972 int f(); §\C{// (1)}§ 2973 double f(); §\C{// (2)}§ 2974 2975 f(); §\C{// ambiguous - (1),(2) both equally viable}§ 2976 (int)f(); §\C{// choose (2)}§ 2711 2977 \end{cfa} 2712 2978 Since casting is a fundamental operation in \CFA, casts need to be given a meaningful interpretation in the context of tuples. … … 2716 2982 void g(); 2717 2983 2718 (void)f(); // valid, ignore results2719 (int)g(); // invalid, void cannot be converted to int2984 (void)f(); §\C{// valid, ignore results}§ 2985 (int)g(); §\C{// invalid, void cannot be converted to int}§ 2720 2986 2721 2987 struct A { int x; }; 2722 (struct A)f(); // invalid, int cannot be converted to A2988 (struct A)f(); §\C{// invalid, int cannot be converted to A}§ 2723 2989 \end{cfa} 2724 2990 In C, line 4 is a valid cast, which calls ©f© and discards its result. … … 2736 3002 [int, [int, int], int] g(); 2737 3003 2738 ([int, double])f(); // (1) valid2739 ([int, int, int])g(); // (2) valid2740 ([void, [int, int]])g(); // (3) valid2741 ([int, int, int, int])g(); // (4) invalid2742 ([int, [int, int, int]])g(); // (5) invalid3004 ([int, double])f(); §\C{// (1) valid}§ 3005 ([int, int, int])g(); §\C{// (2) valid}§ 3006 ([void, [int, int]])g(); §\C{// (3) valid}§ 3007 ([int, int, int, int])g(); §\C{// (4) invalid}§ 3008 ([int, [int, int, int]])g(); §\C{// (5) invalid}§ 2743 3009 \end{cfa} 2744 3010 … … 2800 3066 void f([int, int], int, int); 2801 3067 2802 f([0, 0], 0, 0); // no cost2803 f(0, 0, 0, 0); // cost for structuring2804 f([0, 0,], [0, 0]); // cost for flattening2805 f([0, 0, 0], 0); // cost for flattening and structuring3068 f([0, 0], 0, 0); §\C{// no cost}§ 3069 f(0, 0, 0, 0); §\C{// cost for structuring}§ 3070 f([0, 0,], [0, 0]); §\C{// cost for flattening}§ 3071 f([0, 0, 0], 0); §\C{// cost for flattening and structuring}§ 2806 3072 \end{cfa} 2807 3073 … … 2866 3132 [ unsigned int, char ] 2867 3133 [ double, double, double ] 2868 [ * int, int * ] §\C{// mix of CFA and ANSI}§3134 [ * int, int * ] §\C{// mix of CFA and ANSI}§ 2869 3135 [ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ] 2870 3136 \end{cfa} … … 2873 3139 Examples of declarations using tuple types are: 2874 3140 \begin{cfa} 2875 [ int, int ] x; §\C{// 2 element tuple, each element of type int}§2876 * [ char, char ] y; §\C{// pointer to a 2 element tuple}§3141 [ int, int ] x; §\C{// 2 element tuple, each element of type int}§ 3142 * [ char, char ] y; §\C{// pointer to a 2 element tuple}§ 2877 3143 [ [ int, int ] ] z ([ int, int ]); 2878 3144 \end{cfa} … … 2891 3157 [ int, int ] w1; 2892 3158 [ int, int, int ] w2; 2893 [ void ] f (int, int, int); /* three input parameters of type int */2894 [ void ] g ([ int, int, int ]); /* 3 element tuple as input */3159 [ void ] f (int, int, int); §\C{// three input parameters of type int}§ 3160 [ void ] g ([ int, int, int ]); §\C{3 element tuple as input}§ 2895 3161 f( [ 1, 2, 3 ] ); 2896 3162 f( w1, 3 ); … … 2972 3238 [ int, int, int, int ] w = [ 1, 2, 3, 4 ]; 2973 3239 int x = 5; 2974 [ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§3240 [ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§ 2975 3241 \end{cfa} 2976 3242 Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values; … … 3060 3326 both these examples produce indeterminate results: 3061 3327 \begin{cfa} 3062 f( x++, x++ ); §\C{// C routine call with side effects in arguments}§3063 [ v1, v2 ] = [ x++, x++ ]; §\C{// side effects in righthand side of multiple assignment}§3328 f( x++, x++ ); §\C{// C routine call with side effects in arguments}§ 3329 [ v1, v2 ] = [ x++, x++ ]; §\C{// side effects in righthand side of multiple assignment}§ 3064 3330 \end{cfa} 3065 3331 … … 3083 3349 3084 3350 3085 \section{I/O Library} 3086 \label{s:IOLibrary} 3087 \index{input/output library} 3088 3089 The goal of \CFA 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. 3090 The approach combines ideas from \CC and Python. 3091 The \CFA header file for the I/O library is \Indexc{fstream}. 3092 3093 The common case is printing out a sequence of variables separated by whitespace. 3351 \section{Stream I/O Library} 3352 \label{s:StreamIOLibrary} 3353 \index{input/output stream library} 3354 \index{stream library} 3355 3356 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. 3357 Stream I/O can be implicitly or explicitly formatted. 3358 Implicit formatting means \CFA selects the output or input format for values that match with the type of a variable. 3359 Explicit formatting means additional information is specified to augment how an output or input of value is interpreted. 3360 \CFA formatting is a cross between C ©printf© and \CC ©cout© manipulators, and Python implicit spacing and newline. 3361 Specifically: 3362 \begin{itemize} 3363 \item 3364 ©printf©/Python format codes are dense, making them difficult to read and remember. 3365 \CFA/\CC format manipulators are named, making them easier to read and remember. 3366 \item 3367 ©printf©/Python separates format codes from associated variables, making it difficult to match codes with variables. 3368 \CFA/\CC co-locate codes with associated variables, where \CFA has the tighter binding. 3369 \item 3370 Format manipulators in \CFA have local effect, whereas \CC have global effect, except ©setw©. 3371 Hence, it is common programming practice to toggle manipulators on and then back to the default to prevent downstream side-effects. 3372 Without this programming style, errors occur when moving prints, as manipulator effects incorrectly flow into the new location. 3373 (To guarantee no side-effects, manipulator values must be saved and restored across function calls.) 3374 \item 3375 \CFA has more sophisticated implicit spacing between values than Python, plus implicit newline at the end of a print. 3376 \end{itemize} 3377 The \CFA header file for the I/O library is \Indexc{fstream.hfa}. 3378 3379 For implicit formatted output, the common case is printing a series of variables separated by whitespace. 3094 3380 \begin{cquote} 3095 \begin{tabular}{@{}l@{\hspace{ 3em}}l@{}}3096 \multicolumn{1}{c@{\hspace{ 3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{\CC}} \\3381 \begin{tabular}{@{}l@{\hspace{2em}}l@{\hspace{2em}}l@{}} 3382 \multicolumn{1}{c@{\hspace{2em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{\CC}} & \multicolumn{1}{c}{\textbf{Python}} \\ 3097 3383 \begin{cfa} 3098 3384 int x = 1, y = 2, z = 3; 3099 sout | x ®|® y ®|® z | endl;3385 sout | x ®|® y ®|® z; 3100 3386 \end{cfa} 3101 3387 & … … 3103 3389 3104 3390 cout << x ®<< " "® << y ®<< " "® << z << endl; 3391 \end{cfa} 3392 & 3393 \begin{cfa} 3394 x = 1; y = 2; z = 3 3395 print( x, y, z ) 3105 3396 \end{cfa} 3106 3397 \\ … … 3110 3401 & 3111 3402 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3112 1 2 3 3403 1® ®2® ®3 3404 \end{cfa} 3405 & 3406 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3407 1® ®2® ®3 3113 3408 \end{cfa} 3114 3409 \end{tabular} 3115 3410 \end{cquote} 3116 The \CFA form has half the characters of the \CC form, and is similar to \Index*{Python} I/O with respect to implicit separators .3117 Similar simplification occurs for \Index{tuple} I/O, which prints all tuple valuesseparated by ``\lstinline[showspaces=true]@, @''.3411 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. 3412 Similar simplification occurs for \Index{tuple} I/O, which flattens the tuple and prints each value separated by ``\lstinline[showspaces=true]@, @''. 3118 3413 \begin{cfa} 3119 3414 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ]; 3120 sout | t1 | t2 | endl;§\C{// print tuples}§3415 sout | t1 | t2; §\C{// print tuples}§ 3121 3416 \end{cfa} 3122 3417 \begin{cfa}[showspaces=true,aboveskip=0pt] 3123 3418 1®, ®2®, ®3 4®, ®5®, ®6 3124 3419 \end{cfa} 3125 Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority overloadableoperator, other than assignment.3420 Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority \emph{overloadable} operator, other than assignment. 3126 3421 Therefore, fewer output expressions require parenthesis. 3127 3422 \begin{cquote} … … 3130 3425 & 3131 3426 \begin{cfa} 3132 sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl;3427 sout | x * 3 | y + 1 | z << 2 | x == y | ®(®x | y®)® | ®(®x || y®)® | ®(®x > z ? 1 : 2®)®; 3133 3428 \end{cfa} 3134 3429 \\ … … 3136 3431 & 3137 3432 \begin{cfa} 3138 cout << x * 3 << y + 1 << ®(®z << 2®)® << ®(®x == y®)® << (x | y) << (x || y) << (x > z ? 1 : 2)<< endl;3433 cout << x * 3 << y + 1 << ®(®z << 2®)® << ®(®x == y®)® << ®(®x | y®)® << ®(®x || y®)® << ®(®x > z ? 1 : 2®)® << endl; 3139 3434 \end{cfa} 3140 3435 \\ … … 3145 3440 \end{tabular} 3146 3441 \end{cquote} 3147 There is a weak similarity between the \CFA logical-or operator and the Shell pipe-operator for moving data, where data flows in the correct direction for input but the opposite direction for output. 3442 Input and output use a uniform operator, ©|©, rather than separate operators, as in ©>>© and ©<<© for \CC. 3443 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. 3444 3445 For implicit formatted input, the common case is reading a sequence of values separated by whitespace, where the type of an input constant must match with the type of the input variable. 3446 \begin{cquote} 3447 \begin{lrbox}{\LstBox} 3448 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 3449 int x; double y char z; 3450 \end{cfa} 3451 \end{lrbox} 3452 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{3em}}l@{}} 3453 \multicolumn{1}{@{}l@{}}{\usebox\LstBox} \\ 3454 \multicolumn{1}{c@{\hspace{2em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{\CC}} & \multicolumn{1}{c}{\textbf{Python}} \\ 3455 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 3456 sin | x | y | z; 3457 \end{cfa} 3458 & 3459 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 3460 cin >> x >> y >> z; 3461 \end{cfa} 3462 & 3463 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 3464 x = int(input()); y = float(input()); z = input(); 3465 \end{cfa} 3466 \\ 3467 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3468 ®1® ®2.5® ®A® 3469 3470 3471 \end{cfa} 3472 & 3473 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3474 ®1® ®2.5® ®A® 3475 3476 3477 \end{cfa} 3478 & 3479 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3480 ®1® 3481 ®2.5® 3482 ®A® 3483 \end{cfa} 3484 \end{tabular} 3485 \end{cquote} 3486 3148 3487 3149 3488 3150 3489 \subsection{Implicit Separator} 3151 3490 3152 The \Index{implicit separator}\index{I/O!separator} character (space/blank) is a separator not a terminator .3491 The \Index{implicit separator}\index{I/O!separator} character (space/blank) is a separator not a terminator for output. 3153 3492 The rules for implicitly adding the separator are: 3154 3493 \begin{enumerate} … … 3156 3495 A separator does not appear at the start or end of a line. 3157 3496 \begin{cfa}[belowskip=0pt] 3158 sout | 1 | 2 | 3 | endl;3497 sout | 1 | 2 | 3; 3159 3498 \end{cfa} 3160 3499 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3165 3504 A separator does not appear before or after a character literal or variable. 3166 3505 \begin{cfa} 3167 sout | '1' | '2' | '3' | endl;3506 sout | '1' | '2' | '3'; 3168 3507 123 3169 3508 \end{cfa} 3170 3509 3171 3510 \item 3172 A separator does not appear before or after a null (empty) C string .3173 \begin{cfa} 3174 sout | 1 | "" | 2 | "" | 3 | endl;3511 A separator does not appear before or after a null (empty) C string, which is a local mechanism to disable insertion of the separator character. 3512 \begin{cfa} 3513 sout | 1 | "" | 2 | "" | 3; 3175 3514 123 3176 3515 \end{cfa} 3177 which is a local mechanism to disable insertion of the separator character. 3178 3179 \item 3180 A separator does not appear before a C string starting with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[mathescape=off,basicstyle=\tt]@([{=$£¥¡¿«@ 3516 3517 \item 3518 {\lstset{language=CFA,deletedelim=**[is][]{¢}{¢}} 3519 A separator does not appear before a C string starting with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[basicstyle=\tt]@,.;!?)]}%¢»@, where \lstinline[basicstyle=\tt]@»@ is a closing citation mark. 3520 \begin{cfa}[belowskip=0pt] 3521 sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x" 3522 | 7 | "¢ x" | 8 | "» x" | 9 | ") x" | 10 | "] x" | 11 | "} x"; 3523 \end{cfa} 3524 \begin{cfa}[basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt] 3525 1®,® x 2®.® x 3®;® x 4®!® x 5®?® x 6®%® x 7§\color{red}\textcent§ x 8®»® x 9®)® x 10®]® x 11®}® x 3526 \end{cfa}}% 3527 3528 \item 3529 A separator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[mathescape=off,basicstyle=\tt]@([{=$£¥¡¿«@, where \lstinline[basicstyle=\tt]@¡¿@ are inverted opening exclamation and question marks, and \lstinline[basicstyle=\tt]@«@ is an opening citation mark. 3181 3530 %$ 3182 3531 \begin{cfa}[mathescape=off] 3183 3532 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥" 3184 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10 | endl;3533 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10; 3185 3534 \end{cfa} 3186 3535 %$ … … 3189 3538 \end{cfa} 3190 3539 %$ 3191 where \lstinline[basicstyle=\tt]@¡¿@ are inverted opening exclamation and question marks, and \lstinline[basicstyle=\tt]@«@ is an opening citation mark. 3192 3193 \item 3194 {\lstset{language=CFA,deletedelim=**[is][]{¢}{¢}} 3195 A seperator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[basicstyle=\tt]@,.;!?)]}%¢»@ 3540 3541 \item 3542 A seperator 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@ 3196 3543 \begin{cfa}[belowskip=0pt] 3197 sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x" 3198 | 7 | "¢ x" | 8 | "» x" | 9 | ") x" | 10 | "] x" | 11 | "} x" | endl; 3199 \end{cfa} 3200 \begin{cfa}[basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt] 3201 1®,® x 2®.® x 3®;® x 4®!® x 5®?® x 6®%® x 7§\color{red}\textcent§ x 8®»® x 9®)® x 10®]® x 11®}® x 3202 \end{cfa}}% 3203 where \lstinline[basicstyle=\tt]@»@ is a closing citation mark. 3204 3205 \item 3206 A seperator does not appear before or after a C string begining/ending with the \Index*{ASCII} quote or whitespace characters: \lstinline[basicstyle=\tt,showspaces=true]@`'": \t\v\f\r\n@ 3207 \begin{cfa}[belowskip=0pt] 3208 sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx" | endl; 3544 sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx"; 3209 3545 \end{cfa} 3210 3546 \begin{cfa}[basicstyle=\tt,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt] … … 3215 3551 If a space is desired before or after one of the special string start/end characters, simply insert a space. 3216 3552 \begin{cfa}[belowskip=0pt] 3217 sout | "x (§\color{red}\texttt{\textvisiblespace}§" | 1 | "§\color{red}\texttt{\textvisiblespace}§) x" | 2 | "§\color{red}\texttt{\textvisiblespace}§, x" | 3 | "§\color{red}\texttt{\textvisiblespace}§:x:§\color{red}\texttt{\textvisiblespace}§" | 4 | endl;3553 sout | "x (§\color{red}\texttt{\textvisiblespace}§" | 1 | "§\color{red}\texttt{\textvisiblespace}§) x" | 2 | "§\color{red}\texttt{\textvisiblespace}§, x" | 3 | "§\color{red}\texttt{\textvisiblespace}§:x:§\color{red}\texttt{\textvisiblespace}§" | 4; 3218 3554 \end{cfa} 3219 3555 \begin{cfa}[basicstyle=\tt,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt] … … 3223 3559 3224 3560 3225 \subsection{Manipulator} 3226 3227 The following \CC-style \Index{manipulator}s and routines control implicit seperation. 3561 \subsection{Separation Manipulators} 3562 3563 The following \Index{manipulator}s control \Index{implicit output separation}. 3564 The effect of these manipulators is global for an output stream (except ©sepOn© and ©sepOff©). 3228 3565 \begin{enumerate} 3229 3566 \item 3230 Routines\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.3567 \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. 3231 3568 The separator string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 3232 3569 \begin{cfa}[mathescape=off,belowskip=0pt] 3233 sepSet( sout, ", $" ); §\C{// set separator from " " to ", \$"}§3234 sout | 1 | 2 | 3 | " \"" | ®sep® | "\"" | endl;3570 sepSet( sout, ", $" ); §\C{// set separator from " " to ", \$"}§ 3571 sout | 1 | 2 | 3 | " \"" | ®sep® | "\""; 3235 3572 \end{cfa} 3236 3573 %$ … … 3240 3577 %$ 3241 3578 \begin{cfa}[belowskip=0pt] 3242 sepSet( sout, " " ); §\C{// reset separator to " "}§3243 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )® | "\"" | endl;3579 sepSet( sout, " " ); §\C{// reset separator to " "}§ 3580 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )® | "\""; 3244 3581 \end{cfa} 3245 3582 \begin{cfa}[showspaces=true,aboveskip=0pt] … … 3248 3585 ©sepGet© can be used to store a separator and then restore it: 3249 3586 \begin{cfa}[belowskip=0pt] 3250 char store[®sepSize®]; §\C{// sepSize is the maximum separator size}§3251 strcpy( store, sepGet( sout ) ); §\C{// copy current separator}§3252 sepSet( sout, "_" ); §\C{// change separator to underscore}§3253 sout | 1 | 2 | 3 | endl;3587 char store[®sepSize®]; §\C{// sepSize is the maximum separator size}§ 3588 strcpy( store, sepGet( sout ) ); §\C{// copy current separator}§ 3589 sepSet( sout, "_" ); §\C{// change separator to underscore}§ 3590 sout | 1 | 2 | 3; 3254 3591 \end{cfa} 3255 3592 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3257 3594 \end{cfa} 3258 3595 \begin{cfa}[belowskip=0pt] 3259 sepSet( sout, store ); §\C{// change separator back to original}§3260 sout | 1 | 2 | 3 | endl;3596 sepSet( sout, store ); §\C{// change separator back to original}§ 3597 sout | 1 | 2 | 3; 3261 3598 \end{cfa} 3262 3599 \begin{cfa}[showspaces=true,aboveskip=0pt] … … 3265 3602 3266 3603 \item 3267 Routine\Indexc{sepSetTuple}\index{manipulator!sepSetTuple@©sepSetTuple©} and \Indexc{sepTuple}\index{manipulator!sepTuple@©sepTuple©}/\Indexc{sepGetTuple}\index{manipulator!sepGetTuple@©sepGetTuple©} get and set the tuple separator-string.3604 \Indexc{sepSetTuple}\index{manipulator!sepSetTuple@©sepSetTuple©} and \Indexc{sepTuple}\index{manipulator!sepTuple@©sepTuple©}/\Indexc{sepGetTuple}\index{manipulator!sepGetTuple@©sepGetTuple©} get and set the tuple separator-string. 3268 3605 The tuple separator-string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 3269 3606 \begin{cfa}[belowskip=0pt] 3270 sepSetTuple( sout, " " ); §\C{// set tuple separator from ", " to " "}§3271 sout | t1 | t2 | " \"" | ®sepTuple® | "\"" | endl;3607 sepSetTuple( sout, " " ); §\C{// set tuple separator from ", " to " "}§ 3608 sout | t1 | t2 | " \"" | ®sepTuple® | "\""; 3272 3609 \end{cfa} 3273 3610 \begin{cfa}[showspaces=true,aboveskip=0pt] … … 3275 3612 \end{cfa} 3276 3613 \begin{cfa}[belowskip=0pt] 3277 sepSetTuple( sout, ", " ); §\C{// reset tuple separator to ", "}§3278 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\"" | endl;3614 sepSetTuple( sout, ", " ); §\C{// reset tuple separator to ", "}§ 3615 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\""; 3279 3616 \end{cfa} 3280 3617 \begin{cfa}[showspaces=true,aboveskip=0pt] … … 3284 3621 3285 3622 \item 3286 Manipulators \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} \emph{globally} toggle printing the separator, \ie the seperator is adjusted with respect to all subsequent printed items.3623 \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} toggle printing the separator. 3287 3624 \begin{cfa}[belowskip=0pt] 3288 sout | sepDisable | 1 | 2 | 3 | endl; §\C{// globallyturn off implicit separator}§3625 sout | sepDisable | 1 | 2 | 3; §\C{// turn off implicit separator}§ 3289 3626 \end{cfa} 3290 3627 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3292 3629 \end{cfa} 3293 3630 \begin{cfa}[belowskip=0pt] 3294 sout | sepEnable | 1 | 2 | 3 | endl; §\C{// globallyturn on implicit separator}§3631 sout | sepEnable | 1 | 2 | 3; §\C{// turn on implicit separator}§ 3295 3632 \end{cfa} 3296 3633 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3299 3636 3300 3637 \item 3301 Manipulators \Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} and \Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} \emph{locally} toggle printing the separator, \ie the seperator is adjusted only with respect to the next printed item.3638 \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. 3302 3639 \begin{cfa}[belowskip=0pt] 3303 sout | 1 | sepOff | 2 | 3 | endl; §\C{// locally turn off implicit separator}§3640 sout | 1 | sepOff | 2 | 3; §\C{// turn off implicit separator for the next item}§ 3304 3641 \end{cfa} 3305 3642 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3307 3644 \end{cfa} 3308 3645 \begin{cfa}[belowskip=0pt] 3309 sout | sepDisable | 1 | sepOn | 2 | 3 | endl; §\C{// locally turn on implicit separator}§3646 sout | sepDisable | 1 | sepOn | 2 | 3; §\C{// turn on implicit separator for the next item}§ 3310 3647 \end{cfa} 3311 3648 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3314 3651 The tuple separator also responses to being turned on and off. 3315 3652 \begin{cfa}[belowskip=0pt] 3316 sout | t1 | sepOff | t2 | endl; §\C{// locally turn on/off implicit separator}§3653 sout | t1 | sepOff | t2; §\C{// turn off implicit separator for the next item}§ 3317 3654 \end{cfa} 3318 3655 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3322 3659 use ©sep© to accomplish this functionality. 3323 3660 \begin{cfa}[belowskip=0pt] 3324 sout | sepOn | 1 | 2 | 3 | sepOn | endl ;§\C{// sepOn does nothing at start/end of line}§3661 sout | sepOn | 1 | 2 | 3 | sepOn; §\C{// sepOn does nothing at start/end of line}§ 3325 3662 \end{cfa} 3326 3663 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3328 3665 \end{cfa} 3329 3666 \begin{cfa}[belowskip=0pt] 3330 sout | sep | 1 | 2 | 3 | sep | endl ;§\C{// use sep to print separator at start/end of line}§3667 sout | sep | 1 | 2 | 3 | sep ; §\C{// use sep to print separator at start/end of line}§ 3331 3668 \end{cfa} 3332 3669 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3333 3670 ® ®1 2 3® ® 3671 \end{cfa} 3672 \end{enumerate} 3673 3674 3675 \subsection{Newline Manipulators} 3676 3677 The following \Index{manipulators} control \Index{newline separation} for input and output. 3678 3679 For input: 3680 \begin{enumerate}[parsep=0pt] 3681 \item 3682 \Indexc{nl}\index{manipulator!nl@©nl©} scans characters until the next newline character, i.e., ignore the remaining characters in the line. 3683 \item 3684 \Indexc{nlOn}\index{manipulator!nlOn@©nlOn©} reads the newline character, when reading single characters. 3685 \item 3686 \Indexc{nlOff}\index{manipulator!nlOff@©nlOff©} does \emph{not} read the newline character, when reading single characters. 3687 \end{enumerate} 3688 For example, in: 3689 \begin{cfa} 3690 sin | i | ®nl® | j; 3691 1 ®2® 3692 3 3693 \end{cfa} 3694 variable ©i© is assigned 1, the 2 is skipped, and variable ©j© is assigned 3. 3695 3696 For output: 3697 \begin{enumerate}[parsep=0pt] 3698 \item 3699 \Indexc{nl}\index{manipulator!nl@©nl©} inserts a newline. 3700 \begin{cfa} 3701 sout | nl; §\C{// only print newline}§ 3702 sout | 2; §\C{// implicit newline}§ 3703 sout | 3 | nl | 4 | nl; §\C{// terminating nl merged with implicit newline}§ 3704 sout | 5 | nl | nl; §\C{// again terminating nl merged with implicit newline}§ 3705 sout | 6; §\C{// implicit newline}§ 3706 3707 2 3708 3 3709 4 3710 5 3711 3712 6 3713 \end{cfa} 3714 Note, a terminating ©nl© is merged (overrides) with the implicit newline at the end of the ©sout© expression, otherwise it is impossible to to print a single newline 3715 \item 3716 \Indexc{nlOn}\index{manipulator!nlOn@©nlOn©} implicitly prints a newline at the end of each output expression. 3717 \item 3718 \Indexc{nlOff}\index{manipulator!nlOff@©nlOff©} does \emph{not} implicitly print a newline at the end of each output expression. 3719 \end{enumerate} 3720 3721 3722 \subsection{Output Value Manipulators} 3723 3724 The following \Index{manipulator}s control formatting of output values (printing), and only affect the format of the argument. 3725 \begin{enumerate} 3726 \item 3727 \Indexc{bin}( integer )\index{manipulator!bin@©bin©} print value in base 2 preceded by ©0b©/©0B©. 3728 \begin{cfa}[belowskip=0pt] 3729 sout | bin( 0 ) | bin( 27HH ) | bin( 27H ) | bin( 27 ) | bin( 27L ); 3730 0b0 0b11011 0b11011 0b11011 0b11011 3731 sout | bin( -27HH ) | bin( -27H ) | bin( -27 ) | bin( -27L ); 3732 0b11100101 0b1111111111100101 0b11111111111111111111111111100101 0b®(58 1s)®100101 3733 \end{cfa} 3734 3735 \item 3736 \Indexc{oct}( integer )\index{manipulator!oct@©oct©} print value in base 8 preceded by ©0©. 3737 \begin{cfa}[belowskip=0pt] 3738 sout | oct( 0 ) | oct( 27HH ) | oct( 27H ) | oct( 27 ) | oct( 27L ); 3739 0 033 033 033 033 3740 sout | oct( -27HH ) | oct( -27H ) | oct( -27 ) | oct( -27L ); 3741 0345 0177745 037777777745 01777777777777777777745 3742 \end{cfa} 3743 Note, octal 0 is \emph{not} preceded by ©0© to prevent confusion. 3744 3745 \item 3746 \Indexc{hex}( integer / floating-point )\index{manipulator!hex@©hex©} print value in base 16 preceded by ©0x©/©0X©. 3747 \begin{cfa}[belowskip=0pt] 3748 sout | hex( 0 ) | hex( 27HH ) | hex( 27H ) | hex( 27 ) | hex( 27L ); 3749 0 0x1b 0x1b 0x1b 0x1b 3750 sout | hex( -27HH ) | hex( -27H ) | hex( -27 ) | hex( -27L ); 3751 0xe5 0xffe5 0xffffffe5 0xffffffffffffffe5 3752 3753 sout | hex( 0.0 ) | hex( 27.5F ) | hex( 27.5 ) | hex( 27.5L ); 3754 0x0.p+0 0x1.b8p+4 0x1.b8p+4 0xd.cp+1 3755 sout | hex( -27.5F ) | hex( -27.5 ) | hex( -27.5L ); 3756 -0x1.b8p+4 -0x1.b8p+4 -0xd.cp+1 3757 \end{cfa} 3758 3759 \item 3760 \Indexc{sci}( floating-point )\index{manipulator!sci@©sci©} print value in scientific notation with exponent. 3761 Default is 6 digits of precision. 3762 \begin{cfa}[belowskip=0pt] 3763 sout | sci( 0.0 ) | sci( 27.5 ) | sci( -27.5 ); 3764 0.000000e+00 2.750000e+01 -2.750000e+01 3765 \end{cfa} 3766 3767 \item 3768 \Indexc{upcase}( bin / hex / floating-point )\index{manipulator!upcase@©upcase©} print letters in a value in upper case. Lower case is the default. 3769 \begin{cfa}[belowskip=0pt] 3770 sout | upcase( bin( 27 ) ) | upcase( hex( 27 ) ) | upcase( 27.5e-10 ) | upcase( hex( 27.5 ) ); 3771 0®B®11011 0®X®1®B® 2.75®E®-09 0®X®1.®B®8®P®+4 3772 \end{cfa} 3773 3774 \item 3775 \Indexc{nobase}( integer )\index{manipulator!nobase@©nobase©} do not precede ©bin©, ©oct©, ©hex© with ©0b©/©0B©, ©0©, or ©0x©/©0X©. 3776 Printing the base is the default. 3777 \begin{cfa}[belowskip=0pt] 3778 sout | nobase( bin( 27 ) ) | nobase( oct( 27 ) ) | nobase( hex( 27 ) ); 3779 11011 33 1b 3780 \end{cfa} 3781 3782 \item 3783 \Indexc{nodp}( floating-point )\index{manipulator!nodp@©nodp©} do not print a decimal point if there are no fractional digits. 3784 Printing a decimal point is the default, if there are no fractional digits. 3785 \begin{cfa}[belowskip=0pt] 3786 sout | 0. | nodp( 0. ) | 27.0 | nodp( 27.0 ) | nodp( 27.5 ); 3787 0.0 ®0® 27.0 ®27® 27.5 3788 \end{cfa} 3789 3790 \item 3791 \Indexc{sign}( integer / floating-point )\index{manipulator!sign@©sign©} prefix with plus or minus sign (©+© or ©-©). Only printing the minus sign is the default. 3792 \begin{cfa}[belowskip=0pt] 3793 sout | sign( 27 ) | sign( -27 ) | sign( 27. ) | sign( -27. ) | sign( 27.5 ) | sign( -27.5 ); 3794 ®+®27 -27 ®+®27.0 -27.0 ®+®27.5 -27.5 3795 \end{cfa} 3796 3797 \item 3798 \Indexc{wd}©( unsigned char minimum, T val )©\index{manipulator!wd@©wd©}, ©wd( unsigned char minimum, unsigned char precision, T val )© 3799 For all types, ©minimum© is the minimum number of printed characters. 3800 If the value is shorter than the minimum, it is padded on the right with spaces. 3801 \begin{cfa}[belowskip=0pt] 3802 sout | wd( 4, 34) | wd( 3, 34 ) | wd( 2, 34 ); 3803 sout | wd( 10, 4.) | wd( 9, 4. ) | wd( 8, 4. ); 3804 sout | wd( 4, "ab" ) | wd( 3, "ab" ) | wd( 2, "ab" ); 3805 \end{cfa} 3806 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3807 ® ®34 ® ®34 34 3808 ® ®4.000000 ® ®4.000000 4.000000 3809 ® ®ab ® ®ab ab 3810 \end{cfa} 3811 If the value is larger, it is printed without truncation, ignoring the ©minimum©. 3812 \begin{cfa}[belowskip=0pt] 3813 sout | wd( 4, 34567 ) | wd( 3, 34567 ) | wd( 2, 34567 ); 3814 sout | wd( 4, 3456. ) | wd( 3, 3456. ) | wd( 2, 3456. ); 3815 sout | wd( 4, "abcde" ) | wd( 3, "abcde" ) | wd( 2,"abcde" ); 3816 \end{cfa} 3817 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3818 3456®7® 345®67® 34®567® 3819 3456®.® 345®6.® 34®56.® 3820 abcd®e® abc®de® ab®cde® 3821 \end{cfa} 3822 3823 For integer types, ©precision© is the minimum number of printed digits. 3824 If the value is shorter, it is padded on the left with leading zeros. 3825 \begin{cfa}[belowskip=0pt] 3826 sout | wd( 4,3, 34 ) | wd( 8,4, 34 ) | wd( 10,10, 34 ); 3827 \end{cfa} 3828 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3829 ®0®34 ®00®34 ®00000000®34 3830 \end{cfa} 3831 If the value is larger, it is printed without truncation, ignoring the ©precision©. 3832 \begin{cfa}[belowskip=0pt] 3833 sout | wd( 4,1, 3456 ) | wd( 8,2, 3456 ) | wd( 10,3, 3456 ); 3834 \end{cfa} 3835 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3836 3456 3456 3456 3837 \end{cfa} 3838 If ©precision© is 0, nothing is printed for zero. 3839 If ©precision© is greater than the minimum, it becomes the minimum. 3840 \begin{cfa}[belowskip=0pt] 3841 sout | wd( 4,0, 0 ) | wd( 3,10, 34 ); 3842 \end{cfa} 3843 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3844 ® ® ®00000000®34 3845 \end{cfa} 3846 For floating-point types, ©precision© is the minimum number of digits after the decimal point. 3847 \begin{cfa}[belowskip=0pt] 3848 sout | wd( 6,3, 27.5 ) | wd( 8,1, 27.5 ) | wd( 8,0, 27.5 ) | wd( 3,8, 27.5 ); 3849 \end{cfa} 3850 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3851 27.®500® 27.®5® 28. 27.®50000000® 3852 \end{cfa} 3853 For the C-string type, ©precision© is the maximum number of printed characters, so the string is truncared if it exceeds the maximum. 3854 \begin{cfa}[belowskip=0pt] 3855 sout | wd( 6,8, "abcd" ) | wd( 6,8, "abcdefghijk" ) | wd( 6,3, "abcd" ); 3856 \end{cfa} 3857 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3858 abcd abcdefgh abc 3859 \end{cfa} 3860 3861 \item 3862 \Indexc{ws( unsigned char minimum, unsigned char significant, floating-point )}\index{manipulator!ws@©ws©} 3863 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©). 3864 If a value's significant digits is greater than ©significant©, the last significant digit is rounded up. 3865 \begin{cfa}[belowskip=0pt] 3866 sout | ws(6,6, 234.567) | ws(6,5, 234.567) | ws(6,4, 234.567) | ws(6,3, 234.567); 3867 \end{cfa} 3868 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3869 234.567 234.5®7® 234.®6® 23®5® 3870 \end{cfa} 3871 If a value's magnitude is greater than ©significant©, the value is printed in scientific notation with the specified number of significant digits. 3872 \begin{cfa}[belowskip=0pt] 3873 sout | ws(6,6, 234567.) | ws(6,5, 234567.) | ws(6,4, 234567.) | ws(6,3, 234567.); 3874 \end{cfa} 3875 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3876 234567. 2.3457®e+05® 2.346®e+05® 2.35®e+05® 3877 \end{cfa} 3878 If ©significant© is greater than ©minimum©, it defines the number of printed characters. 3879 \begin{cfa}[belowskip=0pt] 3880 sout | ws(3,6, 234567.) | ws(4,6, 234567.) | ws(5,6, 234567.) | ws(6,6, 234567.); 3881 \end{cfa} 3882 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3883 234567. 234567. 234567. 234567. 3884 \end{cfa} 3885 3886 \item 3887 \Indexc{left}( field-width )\index{manipulator!left@©left©} left justify within the given field. 3888 \begin{cfa}[belowskip=0pt] 3889 sout | left(wd(4, 27)) | left(wd(10, 27.)) | left(wd(10, 27.5)) | left(wd(4,3, 27)) | left(wd(10,3, 27.5)); 3890 \end{cfa} 3891 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3892 27® ® 27.000000 27.500000 027 27.500® ® 3893 \end{cfa} 3894 3895 \item 3896 \Indexc{pad0}( field-width )\index{manipulator!pad0@©pad0©} left pad with zeroes (0). 3897 \begin{cfa}[belowskip=0pt] 3898 sout | pad0( wd( 4, 27 ) ) | pad0( wd( 4,3, 27 ) ) | pad0( wd( 8,3, 27.5 ) ); 3899 ®00®27 ®0®27 ®00®27.500 3334 3900 \end{cfa} 3335 3901 \end{enumerate} … … 3341 3907 int main( void ) { 3342 3908 int x = 1, y = 2, z = 3; 3343 sout | x | y | z | endl;3909 sout | x | y | z; 3344 3910 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ]; 3345 sout | t1 | t2 | endl; // print tuples3346 sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl;3347 sout | 1 | 2 | 3 | endl;3348 sout | '1' | '2' | '3' | endl;3349 sout | 1 | "" | 2 | "" | 3 | endl;3911 sout | t1 | t2; // print tuples 3912 sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2); 3913 sout | 1 | 2 | 3; 3914 sout | '1' | '2' | '3'; 3915 sout | 1 | "" | 2 | "" | 3; 3350 3916 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥" 3351 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10 | endl;3917 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10; 3352 3918 sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x" 3353 | 7 | "¢ x" | 8 | "» x" | 9 | ") x" | 10 | "] x" | 11 | "} x" | endl;3354 sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx" | endl;3355 sout | "x ( " | 1 | " ) x" | 2 | " , x" | 3 | " :x: " | 4 | endl;3919 | 7 | "¢ x" | 8 | "» x" | 9 | ") x" | 10 | "] x" | 11 | "} x"; 3920 sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx"; 3921 sout | "x ( " | 1 | " ) x" | 2 | " , x" | 3 | " :x: " | 4; 3356 3922 3357 3923 sepSet( sout, ", $" ); // set separator from " " to ", $" 3358 sout | 1 | 2 | 3 | " \"" | sep | "\"" | endl;3924 sout | 1 | 2 | 3 | " \"" | sep | "\""; 3359 3925 sepSet( sout, " " ); // reset separator to " " 3360 sout | 1 | 2 | 3 | " \"" | sepGet( sout ) | "\"" | endl;3926 sout | 1 | 2 | 3 | " \"" | sepGet( sout ) | "\""; 3361 3927 3362 3928 char store[sepSize]; 3363 3929 strcpy( store, sepGet( sout ) ); 3364 3930 sepSet( sout, "_" ); 3365 sout | 1 | 2 | 3 | endl;3931 sout | 1 | 2 | 3; 3366 3932 sepSet( sout, store ); 3367 sout | 1 | 2 | 3 | endl;3933 sout | 1 | 2 | 3; 3368 3934 3369 3935 sepSetTuple( sout, " " ); // set tuple separator from ", " to " " 3370 sout | t1 | t2 | " \"" | sepTuple | "\"" | endl;3936 sout | t1 | t2 | " \"" | sepTuple | "\""; 3371 3937 sepSetTuple( sout, ", " ); // reset tuple separator to ", " 3372 sout | t1 | t2 | " \"" | sepGetTuple( sout ) | "\"" | endl;3373 3374 sout | sepDisable | 1 | 2 | 3 | endl; // globally turn off implicit separator3375 sout | sepEnable | 1 | 2 | 3 | endl; // globally turn on implicit separator3376 3377 sout | 1 | sepOff | 2 | 3 | endl; // locally turn on implicit separator3378 sout | sepDisable | 1 | sepOn | 2 | 3 | endl; // globally turn off implicit separator3938 sout | t1 | t2 | " \"" | sepGetTuple( sout ) | "\""; 3939 3940 sout | sepDisable | 1 | 2 | 3; // globally turn off implicit separator 3941 sout | sepEnable | 1 | 2 | 3; // globally turn on implicit separator 3942 3943 sout | 1 | sepOff | 2 | 3; // locally turn on implicit separator 3944 sout | sepDisable | 1 | sepOn | 2 | 3; // globally turn off implicit separator 3379 3945 sout | sepEnable; 3380 sout | t1 | sepOff | t2 | endl; // locally turn on/off implicit separator3381 3382 sout | sepOn | 1 | 2 | 3 | sepOn | endl; // sepOn does nothing at start/end of line3383 sout | sep | 1 | 2 | 3 | sep | endl; // use sep to print separator at start/end of line3946 sout | t1 | sepOff | t2; // locally turn on/off implicit separator 3947 3948 sout | sepOn | 1 | 2 | 3 | sepOn ; // sepOn does nothing at start/end of line 3949 sout | sep | 1 | 2 | 3 | sep ; // use sep to print separator at start/end of line 3384 3950 } 3385 3951 … … 3390 3956 \end{comment} 3391 3957 %$ 3958 3959 3960 \subsection{Input Value Manipulators} 3961 3962 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. 3963 For ©_Bool© type, the constants are ©true© and ©false©. 3964 For integral types, any number of digits, optionally preceded by a sign (©+© or ©-©), where a 3965 \begin{itemize} 3966 \item 3967 ©1©-©9© prefix introduces a decimal value (©0©-©9©), 3968 \item 3969 ©0© prefix introduces an octal value (©0©-©7©), and 3970 \item 3971 ©0x© or ©0X© prefix introduces a hexadecimal value (©0©-©f©) with lower or upper case letters. 3972 \end{itemize} 3973 For floating-point types, any number of decimal digits, optionally preceded by a sign (©+© or ©-©), optionally containing a decimal point, and optionally followed by an exponent, ©e© or ©E©, with signed (optional) decimal digits. 3974 Floating-point values can also be written in hexadecimal format preceded by ©0x© or ©0X© with hexadecimal digits and exponent denoted by ©p© or ©P©. 3975 3976 For the C-string type, the input values are \emph{not} the same as C-string constants, \ie double quotes bracketing arbitrary text with escape sequences. 3977 Instead, the next sequence of non-whitespace characters are read, and the input sequence is terminated with delimiter ©'\0'©. 3978 The string variable \emph{must} be large enough to contain the input sequence. 3979 3980 The following \Index{manipulator}s control formatting of input values (reading), and only affect the format of the argument. 3981 3982 \begin{enumerate} 3983 \item 3984 \Indexc{skip( const char * pattern )}\index{manipulator!skip@©skip©} / ©skip( unsigned int length )© / ©const char * pattern© 3985 The argument defines a ©pattern© or ©length©. 3986 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. 3987 The ©length© is composed of the next $N$ characters, including the newline character. 3988 If the match successes, the input characters are discarded, and input continues with the next character. 3989 If the match fails, the input characters are left unread. 3990 \begin{cfa}[belowskip=0pt] 3991 char sk[$\,$] = "abc"; 3992 sin | "abc " | skip( sk ) | skip( 5 ); // match input sequence 3993 \end{cfa} 3994 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3995 ®abc ® 3996 ®abc ® 3997 ®xx® 3998 \end{cfa} 3999 4000 \item 4001 \Indexc{wdi}©( unsigned int maximum, T & val )©\index{manipulator!wdi@©wdi©} 4002 For all types except ©char©, ©maximum© is the maximum number of characters read for the current operation. 4003 \begin{cfa}[belowskip=0pt] 4004 char s[10]; int i; double d; 4005 sin | wdi( 4, s ) | wdi( 3, i ) | wdi( 8, d ); // c == "abcd", i == 123, d == 3.456E+2 4006 \end{cfa} 4007 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4008 ®abcd1233.456E+2® 4009 \end{cfa} 4010 Note, input ©wdi© cannot be overloaded with output ©wd© because both have the same parameters but return different types. 4011 Currently, \CFA cannot distinguish between these two manipulators in the middle of an ©sout©/©sin© expression based on return type. 4012 4013 \item 4014 \Indexc{ignore( T & val )}\index{manipulator!ignore@©ignore©} 4015 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. 4016 \begin{cfa}[belowskip=0pt] 4017 double d; 4018 sin | ignore( d ); // d is unchanged 4019 \end{cfa} 4020 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4021 ® -75.35e-4® 25 4022 \end{cfa} 4023 4024 \item 4025 \Indexc{incl( const char * scanset, char * s )}\index{manipulator!incl@©incl©} 4026 For the C-string type, the argument defines a ©scanset© that matches any number of characters \emph{in} the set. 4027 Matching characters are read into the C string and null terminated. 4028 \begin{cfa}[belowskip=0pt] 4029 char s[10]; 4030 sin | incl( "abc", s ); 4031 \end{cfa} 4032 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4033 ®bca®xyz 4034 \end{cfa} 4035 4036 \item 4037 \Indexc{excl( const char * scanset, char * s )}\index{manipulator!excl@©excl©} 4038 For the C-string type, the argument defines a ©scanset© that matches any number of characters \emph{not in} the set. 4039 Non-matching characters are read into the C string and null terminated. 4040 \begin{cfa}[belowskip=0pt] 4041 char s[10]; 4042 sin | excl( "abc", s ); 4043 \end{cfa} 4044 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4045 ®xyz®bca 4046 \end{cfa} 4047 \end{enumerate} 3392 4048 3393 4049 … … 3759 4415 \begin{itemize} 3760 4416 \item 3761 preventing having to determine or writelong generic types,3762 \item 3763 ensur esecondary variables, related to a primary variable, always have the same type.4417 not determining or writing long generic types, 4418 \item 4419 ensuring secondary variables, related to a primary variable, always have the same type. 3764 4420 \end{itemize} 3765 4421 … … 3783 4439 There is also the conundrum in type inferencing of when to \emph{\Index{brand}} a type. 3784 4440 That is, when is the type of the variable more important than the type of its initialization expression. 3785 For example, if a change is made in an initialization expression, it can cause significantcascading type changes and/or errors.4441 For example, if a change is made in an initialization expression, it can cause cascading type changes and/or errors. 3786 4442 At some point, a variable type needs to remain constant and the expression to be in error when it changes. 3787 4443 … … 4016 4672 4017 4673 coroutine Fibonacci { 4018 int fn; §\C{// used for communication}§4674 int fn; §\C{// used for communication}§ 4019 4675 }; 4020 4676 void ?{}( Fibonacci * this ) { … … 4022 4678 } 4023 4679 void main( Fibonacci * this ) { 4024 int fn1, fn2; §\C{// retained between resumes}§4025 this->fn = 0; §\C{// case 0}§4680 int fn1, fn2; §\C{// retained between resumes}§ 4681 this->fn = 0; §\C{// case 0}§ 4026 4682 fn1 = this->fn; 4027 suspend(); §\C{// return to last resume}§4028 4029 this->fn = 1; §\C{// case 1}§4683 suspend(); §\C{// return to last resume}§ 4684 4685 this->fn = 1; §\C{// case 1}§ 4030 4686 fn2 = fn1; 4031 4687 fn1 = this->fn; 4032 suspend(); §\C{// return to last resume}§4033 4034 for ( ;; ) { §\C{// general case}§4688 suspend(); §\C{// return to last resume}§ 4689 4690 for ( ;; ) { §\C{// general case}§ 4035 4691 this->fn = fn1 + fn2; 4036 4692 fn2 = fn1; 4037 4693 fn1 = this->fn; 4038 suspend(); §\C{// return to last resume}§4694 suspend(); §\C{// return to last resume}§ 4039 4695 } // for 4040 4696 } 4041 4697 int next( Fibonacci * this ) { 4042 resume( this ); §\C{// transfer to last suspend}§4698 resume( this ); §\C{// transfer to last suspend}§ 4043 4699 return this->fn; 4044 4700 } … … 4046 4702 Fibonacci f1, f2; 4047 4703 for ( int i = 1; i <= 10; i += 1 ) { 4048 sout | next( &f1 ) | ' ' | next( &f2 ) | endl;4704 sout | next( &f1 ) | ' ' | next( &f2 ); 4049 4705 } // for 4050 4706 } … … 4112 4768 MyThread f[4]; 4113 4769 } 4114 sout | global.value | endl;4770 sout | global.value; 4115 4771 } 4116 4772 \end{cfa} … … 4190 4846 void main( First * this ) { 4191 4847 for ( int i = 0; i < 10; i += 1 ) { 4192 sout | "First : Suspend No." | i + 1 | endl;4848 sout | "First : Suspend No." | i + 1; 4193 4849 yield(); 4194 4850 } … … 4199 4855 wait( this->lock ); 4200 4856 for ( int i = 0; i < 10; i += 1 ) { 4201 sout | "Second : Suspend No." | i + 1 | endl;4857 sout | "Second : Suspend No." | i + 1; 4202 4858 yield(); 4203 4859 } … … 4206 4862 int main( void ) { 4207 4863 signal_once lock; 4208 sout | "User main begin" | endl;4864 sout | "User main begin"; 4209 4865 { 4210 4866 processor p; … … 4214 4870 } 4215 4871 } 4216 sout | "User main end" | endl;4872 sout | "User main end"; 4217 4873 } 4218 4874 \end{cfa} … … 4911 5567 void ?{}( Line * l ) { 4912 5568 l->lnth = 0.0; 4913 sout | "default" | endl;5569 sout | "default"; 4914 5570 } 4915 5571 … … 4918 5574 void ?{}( Line * l, float lnth ) { 4919 5575 l->lnth = lnth; 4920 sout | "lnth" | l->lnth | endl;5576 sout | "lnth" | l->lnth; 4921 5577 4922 5578 } … … 4924 5580 // destructor 4925 5581 void ^?() { 4926 sout | "destroyed" | endl;5582 sout | "destroyed"; 4927 5583 l.lnth = 0.0; 4928 5584 } … … 5585 6241 In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg ©int *?*?()©, where the string ©*?*?© can be interpreted as: 5586 6242 \begin{cfa} 5587 *?§\color{red}\textvisiblespace§*? §\C{// dereference operator, dereference operator}§5588 *§\color{red}\textvisiblespace§?*? §\C{// dereference, multiplication operator}§6243 *?§\color{red}\textvisiblespace§*? §\C{// dereference operator, dereference operator}§ 6244 *§\color{red}\textvisiblespace§?*? §\C{// dereference, multiplication operator}§ 5589 6245 \end{cfa} 5590 6246 By default, the first interpretation is selected, which does not yield a meaningful parse. … … 5638 6294 \eg: 5639 6295 \begin{cfa} 5640 x; §\C{// int x}§5641 *y; §\C{// int *y}§5642 f( p1, p2 ); §\C{// int f( int p1, int p2 );}§5643 g( p1, p2 ) int p1, p2; §\C{// int g( int p1, int p2 );}§6296 x; §\C{// int x}§ 6297 *y; §\C{// int *y}§ 6298 f( p1, p2 ); §\C{// int f( int p1, int p2 );}§ 6299 g( p1, p2 ) int p1, p2; §\C{// int g( int p1, int p2 );}§ 5644 6300 \end{cfa} 5645 6301 \CFA continues to support K\&R routine definitions: 5646 6302 \begin{cfa} 5647 f( a, b, c ) §\C{// default int return}§5648 int a, b; char c §\C{// K\&R parameter declarations}§6303 f( a, b, c ) §\C{// default int return}§ 6304 int a, b; char c §\C{// K\&R parameter declarations}§ 5649 6305 { 5650 6306 ... … … 5665 6321 int rtn( int i ); 5666 6322 int rtn( char c ); 5667 rtn( 'x' ); §\C{// programmer expects 2nd rtn to be called}§6323 rtn( 'x' ); §\C{// programmer expects 2nd rtn to be called}§ 5668 6324 \end{cfa} 5669 6325 \item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn© rather than the first. 5670 6326 In particular, output of ©char© variable now print a character rather than the decimal ASCII value of the character. 5671 6327 \begin{cfa} 5672 sout | 'x' | " " | (int)'x' | endl;6328 sout | 'x' | " " | (int)'x'; 5673 6329 x 120 5674 6330 \end{cfa} … … 5687 6343 \item[Change:] make string literals ©const©: 5688 6344 \begin{cfa} 5689 char * p = "abc"; §\C{// valid in C, deprecated in \CFA}§5690 char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§6345 char * p = "abc"; §\C{// valid in C, deprecated in \CFA}§ 6346 char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§ 5691 6347 \end{cfa} 5692 6348 The type of a string literal is changed from ©[] char© to ©const [] char©. … … 5695 6351 \begin{cfa} 5696 6352 char * p = "abc"; 5697 p[0] = 'w'; §\C{// segment fault or change constant literal}§6353 p[0] = 'w'; §\C{// segment fault or change constant literal}§ 5698 6354 \end{cfa} 5699 6355 The same problem occurs when passing a string literal to a routine that changes its argument. … … 5707 6363 \item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope: 5708 6364 \begin{cfa} 5709 int i; §\C{// forward definition}§5710 int *j = ®&i®; §\C{// forward reference, valid in C, invalid in \CFA}§5711 int i = 0; §\C{// definition}§6365 int i; §\C{// forward definition}§ 6366 int *j = ®&i®; §\C{// forward reference, valid in C, invalid in \CFA}§ 6367 int i = 0; §\C{// definition}§ 5712 6368 \end{cfa} 5713 6369 is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed. … … 5715 6371 \begin{cfa} 5716 6372 struct X { int i; struct X *next; }; 5717 static struct X a; §\C{// forward definition}§6373 static struct X a; §\C{// forward definition}§ 5718 6374 static struct X b = { 0, ®&a® };§\C{// forward reference, valid in C, invalid in \CFA}§ 5719 static struct X a = { 1, &b }; §\C{// definition}§6375 static struct X a = { 1, &b }; §\C{// definition}§ 5720 6376 \end{cfa} 5721 6377 \item[Rationale:] avoids having different initialization rules for builtin types and user-defined types. … … 5732 6388 struct Person { 5733 6389 enum ®Colour® { R, G, B }; §\C[7cm]{// nested type}§ 5734 struct Face { §\C{// nested type}§5735 ®Colour® Eyes, Hair; §\C{// type defined outside (1 level)}§6390 struct Face { §\C{// nested type}§ 6391 ®Colour® Eyes, Hair; §\C{// type defined outside (1 level)}§ 5736 6392 }; 5737 ®.Colour® shirt; §\C{// type defined outside (top level)}§5738 ®Colour® pants; §\C{// type defined same level}§5739 Face looks[10]; §\C{// type defined same level}§6393 ®.Colour® shirt; §\C{// type defined outside (top level)}§ 6394 ®Colour® pants; §\C{// type defined same level}§ 6395 Face looks[10]; §\C{// type defined same level}§ 5740 6396 }; 5741 ®Colour® c = R; §\C{// type/enum defined same level}§6397 ®Colour® c = R; §\C{// type/enum defined same level}§ 5742 6398 Person®.Colour® pc = Person®.®R;§\C{// type/enum defined inside}§ 5743 Person®.®Face pretty; §\C{// type defined inside}\CRT§6399 Person®.®Face pretty; §\C{// type defined inside}\CRT§ 5744 6400 \end{cfa} 5745 6401 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. … … 5758 6414 \item[Difficulty of converting:] Semantic transformation. To make the struct type name visible in the scope of the enclosing struct, the struct tag could be declared in the scope of the enclosing struct, before the enclosing struct is defined. Example: 5759 6415 \begin{cfa} 5760 struct Y; §\C{// struct Y and struct X are at the same scope}§6416 struct Y; §\C{// struct Y and struct X are at the same scope}§ 5761 6417 struct X { 5762 6418 struct Y { /* ... */ } y; … … 5773 6429 \begin{cfa} 5774 6430 void foo() { 5775 int * b = malloc( sizeof(int) ); §\C{// implicitly convert void * to int *}§5776 char * c = b; §\C{// implicitly convert int * to void *, and then void * to char *}§6431 int * b = malloc( sizeof(int) ); §\C{// implicitly convert void * to int *}§ 6432 char * c = b; §\C{// implicitly convert int * to void *, and then void * to char *}§ 5777 6433 } 5778 6434 \end{cfa} … … 5947 6603 void * memalign( size_t align, size_t size );§\indexc{memalign}§ 5948 6604 int posix_memalign( void ** ptr, size_t align, size_t size );§\indexc{posix_memalign}§ 5949 }5950 5951 // §\CFA§ safe equivalents, i.e., implicit size specification5952 forall( dtype T | sized(T) ) T * malloc( void );5953 forall( dtype T | sized(T) ) T * calloc( size_t dim );5954 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size );5955 forall( dtype T | sized(T) ) T * memalign( size_t align );5956 forall( dtype T | sized(T) ) T * aligned_alloc( size_t align );5957 forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align );5958 5959 // §\CFA§ safe general allocation, fill, resize, array5960 forall( dtype T | sized(T) ) T * alloc( void );§\indexc{alloc}§5961 forall( dtype T | sized(T) ) T * alloc( char fill );5962 forall( dtype T | sized(T) ) T * alloc( size_t dim );5963 forall( dtype T | sized(T) ) T * alloc( size_t dim, char fill );5964 forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim );5965 forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );5966 5967 // §\CFA§ safe general allocation, align, fill, array5968 forall( dtype T | sized(T) ) T * align_alloc( size_t align );5969 forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill );5970 forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim );5971 forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill );5972 6605 5973 6606 // C unsafe initialization/copy 5974 extern "C" {5975 6607 void * memset( void * dest, int c, size_t size ); 5976 6608 void * memcpy( void * dest, const void * src, size_t size ); 5977 6609 } 5978 6610 6611 forall( dtype T | sized(T) ) { 6612 // §\CFA§ safe equivalents, i.e., implicit size specification 6613 T * malloc( void ); 6614 T * calloc( size_t dim ); 6615 T * realloc( T * ptr, size_t size ); 6616 T * memalign( size_t align ); 6617 T * aligned_alloc( size_t align ); 6618 int posix_memalign( T ** ptr, size_t align ); 6619 6620 // §\CFA§ safe general allocation, fill, resize, array 6621 T * alloc( void );§\indexc{alloc}§ 6622 T * alloc( char fill ); 6623 T * alloc( size_t dim ); 6624 T * alloc( size_t dim, char fill ); 6625 T * alloc( T ptr[], size_t dim ); 6626 T * alloc( T ptr[], size_t dim, char fill ); 6627 6628 // §\CFA§ safe general allocation, align, fill, array 6629 T * align_alloc( size_t align ); 6630 T * align_alloc( size_t align, char fill ); 6631 T * align_alloc( size_t align, size_t dim ); 6632 T * align_alloc( size_t align, size_t dim, char fill ); 6633 5979 6634 // §\CFA§ safe initialization/copy, i.e., implicit size specification 5980 forall( dtype T | sized(T) )T * memset( T * dest, char c );§\indexc{memset}§5981 forall( dtype T | sized(T) )T * memcpy( T * dest, const T * src );§\indexc{memcpy}§6635 T * memset( T * dest, char c );§\indexc{memset}§ 6636 T * memcpy( T * dest, const T * src );§\indexc{memcpy}§ 5982 6637 5983 6638 // §\CFA§ safe initialization/copy array 5984 forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ); 5985 forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ); 6639 T * amemset( T dest[], char c, size_t dim ); 6640 T * amemcpy( T dest[], const T src[], size_t dim ); 6641 } 5986 6642 5987 6643 // §\CFA§ allocation/deallocation and constructor/destructor … … 5999 6655 6000 6656 6001 \subsection{ Conversion}6657 \subsection{String to Value Conversion} 6002 6658 6003 6659 \leavevmode … … 6035 6691 \leavevmode 6036 6692 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6037 forall( otype T | { int ?<?( T, T ); } ) §\C{// location}§6693 forall( otype T | { int ?<?( T, T ); } ) §\C{// location}§ 6038 6694 T * bsearch( T key, const T * arr, size_t dim );§\indexc{bsearch}§ 6039 6695 6040 forall( otype T | { int ?<?( T, T ); } ) §\C{// position}§6696 forall( otype T | { int ?<?( T, T ); } ) §\C{// position}§ 6041 6697 unsigned int bsearch( T key, const T * arr, size_t dim ); 6042 6698 6043 6699 forall( otype T | { int ?<?( T, T ); } ) 6044 6700 void qsort( const T * arr, size_t dim );§\indexc{qsort}§ 6701 6702 forall( otype E | { int ?<?( E, E ); } ) { 6703 E * bsearch( E key, const E * vals, size_t dim );§\indexc{bsearch}§ §\C{// location}§ 6704 size_t bsearch( E key, const E * vals, size_t dim );§\C{// position}§ 6705 E * bsearchl( E key, const E * vals, size_t dim );§\indexc{bsearchl}§ 6706 size_t bsearchl( E key, const E * vals, size_t dim ); 6707 E * bsearchu( E key, const E * vals, size_t dim );§\indexc{bsearchu}§ 6708 size_t bsearchu( E key, const E * vals, size_t dim ); 6709 } 6710 6711 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) { 6712 E * bsearch( K key, const E * vals, size_t dim ); 6713 size_t bsearch( K key, const E * vals, size_t dim ); 6714 E * bsearchl( K key, const E * vals, size_t dim ); 6715 size_t bsearchl( K key, const E * vals, size_t dim ); 6716 E * bsearchu( K key, const E * vals, size_t dim ); 6717 size_t bsearchu( K key, const E * vals, size_t dim ); 6718 } 6719 6720 forall( otype E | { int ?<?( E, E ); } ) { 6721 void qsort( E * vals, size_t dim );§\indexc{qsort}§ 6722 } 6045 6723 \end{cfa} 6046 6724 … … 6069 6747 \leavevmode 6070 6748 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6071 void rand48seed( long int s );§\indexc{rand48seed}§ 6072 char rand48();§\indexc{rand48}§ 6073 int rand48(); 6074 unsigned int rand48(); 6075 long int rand48(); 6076 unsigned long int rand48(); 6077 float rand48(); 6078 double rand48(); 6079 float _Complex rand48(); 6080 double _Complex rand48(); 6081 long double _Complex rand48(); 6749 void srandom( unsigned int seed );§\indexc{srandom}§ 6750 char random( void );§\indexc{random}§ 6751 char random( char u ); §\C{// [0,u)}§ 6752 char random( char l, char u ); §\C{// [l,u)}§ 6753 int random( void ); 6754 int random( int u ); §\C{// [0,u)}§ 6755 int random( int l, int u ); §\C{// [l,u)}§ 6756 unsigned int random( void ); 6757 unsigned int random( unsigned int u ); §\C{// [0,u)}§ 6758 unsigned int random( unsigned int l, unsigned int u ); §\C{// [l,u)}§ 6759 long int random( void ); 6760 long int random( long int u ); §\C{// [0,u)}§ 6761 long int random( long int l, long int u ); §\C{// [l,u)}§ 6762 unsigned long int random( void ); 6763 unsigned long int random( unsigned long int u ); §\C{// [0,u)}§ 6764 unsigned long int random( unsigned long int l, unsigned long int u ); §\C{// [l,u)}§ 6765 float random( void ); §\C{// [0.0, 1.0)}§ 6766 double random( void ); §\C{// [0.0, 1.0)}§ 6767 float _Complex random( void ); §\C{// [0.0, 1.0)+[0.0, 1.0)i}§ 6768 double _Complex random( void ); §\C{// [0.0, 1.0)+[0.0, 1.0)i}§ 6769 long double _Complex random( void ); §\C{// [0.0, 1.0)+[0.0, 1.0)i}§ 6082 6770 \end{cfa} 6083 6771 … … 6122 6810 [ int, long double ] remquo( long double, long double ); 6123 6811 6124 float div( float, float, int * );§\indexc{div}§ §\C{// alternative name for remquo}§6812 float div( float, float, int * );§\indexc{div}§ §\C{// alternative name for remquo}§ 6125 6813 double div( double, double, int * ); 6126 6814 long double div( long double, long double, int * ); … … 6278 6966 long double atan2( long double, long double ); 6279 6967 6280 float atan( float, float ); §\C{// alternative name for atan2}§6968 float atan( float, float ); §\C{// alternative name for atan2}§ 6281 6969 double atan( double, double );§\indexc{atan}§ 6282 6970 long double atan( long double, long double ); … … 6458 7146 6459 7147 6460 \section{Time }6461 \label{s:Time Lib}7148 \section{Time Keeping} 7149 \label{s:TimeKeeping} 6462 7150 6463 7151 6464 7152 %\subsection{\texorpdfstring{\protect\lstinline@Duration@}{Duration}} 6465 \subsection{\texorpdfstring{\Lst KeywordStyle{\textmd{Duration}}}{Duration}}7153 \subsection{\texorpdfstring{\LstBasicStyle{Duration}}{Duration}} 6466 7154 \label{s:Duration} 6467 7155 … … 6469 7157 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6470 7158 struct Duration { 6471 int64_t tv; §\C{// nanoseconds}§7159 int64_t tv; §\C{// nanoseconds}§ 6472 7160 }; 6473 7161 … … 6512 7200 6513 7201 Duration abs( Duration rhs ); 6514 6515 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );6516 7202 6517 7203 Duration ?`ns( int64_t nsec ); … … 6537 7223 int64_t ?`d( Duration dur ); 6538 7224 int64_t ?`w( Duration dur ); 7225 7226 Duration max( Duration lhs, Duration rhs ); 7227 Duration min( Duration lhs, Duration rhs ); 6539 7228 \end{cfa} 6540 7229 6541 7230 6542 7231 %\subsection{\texorpdfstring{\protect\lstinline@\timeval@}{timeval}} 6543 \subsection{\texorpdfstring{\Lst KeywordStyle{\textmd{timeval}}}{timeval}}7232 \subsection{\texorpdfstring{\LstBasicStyle{timeval}}{timeval}} 6544 7233 \label{s:timeval} 6545 7234 … … 6560 7249 6561 7250 6562 \subsection{\texorpdfstring{\protect\lstinline@timespec@}{timespec}} 7251 %\subsection{\texorpdfstring{\protect\lstinline@timespec@}{timespec}} 7252 \subsection{\texorpdfstring{\LstBasicStyle{timespec}}{timespec}} 6563 7253 \label{s:timespec} 6564 7254 … … 6579 7269 6580 7270 6581 \subsection{\texorpdfstring{\protect\lstinline@itimerval@}{itimerval}} 7271 %\subsection{\texorpdfstring{\protect\lstinline@itimerval@}{itimerval}} 7272 \subsection{\texorpdfstring{\LstBasicStyle{itimerval}}{itimerval}} 6582 7273 \label{s:itimerval} 6583 7274 … … 6589 7280 6590 7281 6591 \subsection{\texorpdfstring{\protect\lstinline@Time@}{Time}} 7282 %\subsection{\texorpdfstring{\protect\lstinline@Time@}{Time}} 7283 \subsection{\texorpdfstring{\LstBasicStyle{Time}}{Time}} 6592 7284 \label{s:Time} 6593 7285 … … 6595 7287 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6596 7288 struct Time { 6597 uint64_t tv; §\C{// nanoseconds since UNIX epoch}§7289 uint64_t tv; §\C{// nanoseconds since UNIX epoch}§ 6598 7290 }; 6599 7291 6600 7292 void ?{}( Time & time ); 6601 7293 void ?{}( Time & time, zero_t ); 6602 void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 ); 7294 6603 7295 Time ?=?( Time & time, zero_t ); 6604 7296 … … 6609 7301 Time ?=?( Time & time, timespec t ); 6610 7302 6611 Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; } 6612 Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; } 6613 Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } 6614 6615 Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; } 6616 Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tv - rhs.tv }; } 6617 Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } 6618 _Bool ?==?( Time lhs, Time rhs ) { return lhs.tv == rhs.tv; } 6619 _Bool ?!=?( Time lhs, Time rhs ) { return lhs.tv != rhs.tv; } 6620 _Bool ?<?( Time lhs, Time rhs ) { return lhs.tv < rhs.tv; } 6621 _Bool ?<=?( Time lhs, Time rhs ) { return lhs.tv <= rhs.tv; } 6622 _Bool ?>?( Time lhs, Time rhs ) { return lhs.tv > rhs.tv; } 6623 _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; } 6624 6625 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time ); 7303 Time ?+?( Time & lhs, Duration rhs ); 7304 Time ?+?( Duration lhs, Time rhs ); 7305 Time ?+=?( Time & lhs, Duration rhs ); 7306 7307 Duration ?-?( Time lhs, Time rhs ); 7308 Time ?-?( Time lhs, Duration rhs ); 7309 Time ?-=?( Time & lhs, Duration rhs ); 7310 _Bool ?==?( Time lhs, Time rhs ); 7311 _Bool ?!=?( Time lhs, Time rhs ); 7312 _Bool ?<?( Time lhs, Time rhs ); 7313 _Bool ?<=?( Time lhs, Time rhs ); 7314 _Bool ?>?( Time lhs, Time rhs ); 7315 _Bool ?>=?( Time lhs, Time rhs ); 6626 7316 6627 7317 char * yy_mm_dd( Time time, char * buf ); … … 6641 7331 6642 7332 size_t strftime( char * buf, size_t size, const char * fmt, Time time ); 7333 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time ); 6643 7334 \end{cfa} 6644 7335 … … 6661 7352 6662 7353 %\subsection{\texorpdfstring{\protect\lstinline@Clock@}{Clock}} 6663 \subsection{\texorpdfstring{\Lst KeywordStyle{\textmd{Clock}}}{Clock}}7354 \subsection{\texorpdfstring{\LstBasicStyle{Clock}}{Clock}} 6664 7355 \label{s:Clock} 6665 7356 … … 6667 7358 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6668 7359 struct Clock { 6669 Duration offset; §\C{// for virtual clock: contains offset from real-time}§6670 int clocktype; §\C{// implementation only -1 (virtual), CLOCK\_REALTIME}§7360 Duration offset; §\C{// for virtual clock: contains offset from real-time}§ 7361 int clocktype; §\C{// implementation only -1 (virtual), CLOCK\_REALTIME}§ 6671 7362 }; 6672 7363 … … 6675 7366 void ?{}( Clock & clk ); 6676 7367 void ?{}( Clock & clk, Duration adj ); 6677 Duration getRes(); 6678 Time getTimeNsec(); §\C{// with nanoseconds}§ 6679 Time getTime(); §\C{// without nanoseconds}§ 7368 7369 Duration getResNsec(); §\C{// with nanoseconds}§ 7370 Duration getRes(); §\C{// without nanoseconds}§ 7371 7372 Time getTimeNsec(); §\C{// with nanoseconds}§ 7373 Time getTime(); §\C{// without nanoseconds}§ 6680 7374 Time getTime( Clock & clk ); 6681 7375 Time ?()( Clock & clk ); … … 6693 7387 6694 7388 \begin{cfa} 6695 void ?{}( Int * this ); §\C{// constructor/destructor}§7389 void ?{}( Int * this ); §\C{// constructor/destructor}§ 6696 7390 void ?{}( Int * this, Int init ); 6697 7391 void ?{}( Int * this, zero_t ); … … 6702 7396 void ^?{}( Int * this ); 6703 7397 6704 Int ?=?( Int * lhs, Int rhs ); §\C{// assignment}§7398 Int ?=?( Int * lhs, Int rhs ); §\C{// assignment}§ 6705 7399 Int ?=?( Int * lhs, long int rhs ); 6706 7400 Int ?=?( Int * lhs, unsigned long int rhs ); … … 6719 7413 unsigned long int narrow( Int val ); 6720 7414 6721 int ?==?( Int oper1, Int oper2 ); §\C{// comparison}§7415 int ?==?( Int oper1, Int oper2 ); §\C{// comparison}§ 6722 7416 int ?==?( Int oper1, long int oper2 ); 6723 7417 int ?==?( long int oper2, Int oper1 ); … … 6755 7449 int ?>=?( unsigned long int oper1, Int oper2 ); 6756 7450 6757 Int +?( Int oper ); §\C{// arithmetic}§7451 Int +?( Int oper ); §\C{// arithmetic}§ 6758 7452 Int -?( Int oper ); 6759 7453 Int ~?( Int oper ); … … 6837 7531 Int ?>>=?( Int * lhs, mp_bitcnt_t shift ); 6838 7532 6839 Int abs( Int oper ); §\C{// number functions}§7533 Int abs( Int oper ); §\C{// number functions}§ 6840 7534 Int fact( unsigned long int N ); 6841 7535 Int gcd( Int oper1, Int oper2 ); … … 6862 7556 #include <gmp>§\indexc{gmp}§ 6863 7557 int main( void ) { 6864 sout | "Factorial Numbers" | endl;7558 sout | "Factorial Numbers"; 6865 7559 Int fact = 1; 6866 7560 6867 sout | 0 | fact | endl;7561 sout | 0 | fact; 6868 7562 for ( unsigned int i = 1; i <= 40; i += 1 ) { 6869 7563 fact *= i; 6870 sout | i | fact | endl;7564 sout | i | fact; 6871 7565 } 6872 7566 } … … 6948 7642 // implementation 6949 7643 struct Rational {§\indexc{Rational}§ 6950 long int numerator, denominator; §\C{// invariant: denominator > 0}§7644 long int numerator, denominator; §\C{// invariant: denominator > 0}§ 6951 7645 }; // Rational 6952 7646 6953 Rational rational(); §\C{// constructors}§7647 Rational rational(); §\C{// constructors}§ 6954 7648 Rational rational( long int n ); 6955 7649 Rational rational( long int n, long int d ); … … 6957 7651 void ?{}( Rational * r, one_t ); 6958 7652 6959 long int numerator( Rational r ); §\C{// numerator/denominator getter/setter}§7653 long int numerator( Rational r ); §\C{// numerator/denominator getter/setter}§ 6960 7654 long int numerator( Rational r, long int n ); 6961 7655 long int denominator( Rational r ); 6962 7656 long int denominator( Rational r, long int d ); 6963 7657 6964 int ?==?( Rational l, Rational r ); §\C{// comparison}§7658 int ?==?( Rational l, Rational r ); §\C{// comparison}§ 6965 7659 int ?!=?( Rational l, Rational r ); 6966 7660 int ?<?( Rational l, Rational r ); … … 6969 7663 int ?>=?( Rational l, Rational r ); 6970 7664 6971 Rational -?( Rational r ); §\C{// arithmetic}§7665 Rational -?( Rational r ); §\C{// arithmetic}§ 6972 7666 Rational ?+?( Rational l, Rational r ); 6973 7667 Rational ?-?( Rational l, Rational r ); … … 6975 7669 Rational ?/?( Rational l, Rational r ); 6976 7670 6977 double widen( Rational r ); §\C{// conversion}§7671 double widen( Rational r ); §\C{// conversion}§ 6978 7672 Rational narrow( double f, long int md ); 6979 7673
Note:
See TracChangeset
for help on using the changeset viewer.