Changes in doc/user/user.tex [c6dc7f2:48b9b36]
- File:
-
- 1 edited
-
doc/user/user.tex (modified) (171 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
rc6dc7f2 r48b9b36 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 at Jul 13 18:36:18 201914 %% Update Count : 3 87613 %% Last Modified On : Sun May 6 10:33:53 2018 14 %% Update Count : 3319 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 72 72 73 73 % Names used in the document. 74 \newcommand{\Version}{\input{ build/version}}74 \newcommand{\Version}{\input{../../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 olymorphic routines and types, exceptions, concurrency, and modules.148 The primary new features include parametric-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{}} ~\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, requiringsignificant effort and training to incrementally add \CC to a C-based project.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 requires 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 C, \CFA, and \CC I/O mechanisms, where the programs output the same result.163 \begin{c enter}162 For example, the following programs compare the \CFA, C, and \CC I/O mechanisms, where the programs output the same result. 163 \begin{cquote} 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 ;®§\indexc{sout}§180 ®sout | x | y | z | endl;®§\indexc{sout}§ 181 181 } 182 182 \end{cfa} … … 191 191 \end{cfa} 192 192 \end{tabular} 193 \end{c enter}193 \end{cquote} 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 197 196 \subsection{Background} 198 197 199 198 This document is a programmer reference-manual for the \CFA programming language. 200 The manual covers the core features of the language and runtime-system, with simple examples illustrating syntax and semantics of features.199 The manual covers the core features of the language and runtime-system, with simple examples illustrating syntax and semantics of each feature. 201 200 The manual does not teach programming, \ie how to combine the new constructs to build complex programs. 202 The reader musthave 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.201 A reader should already 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. 203 202 Implementers should refer to the \CFA Programming Language Specification for details about the language syntax and semantics. 204 203 Changes to the syntax and additional features are expected to be included in later revisions. … … 207 206 \section{Why fix C?} 208 207 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.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. 210 209 This installation base and the programmers producing it represent a massive software-engineering investment spanning decades and likely to continue for decades more. 211 210 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. 212 211 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. 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: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\% each with a long tail. 213 As well, for 30 years, C has been the number 1 and 2 most popular programming language: 215 214 \begin{center} 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 \\ 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 \\ 222 224 \end{tabular} 223 225 \end{center} 224 226 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. 225 227 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. 226 Nevertheless, C has many problems and omissions that make it an unacceptable programming language for modern needs.228 Unfortunately, C has many problems and omissions that make it an unacceptable programming language for modern needs. 227 229 228 230 As stated, the goal of the \CFA project is to engineer modern language-features into C in an evolutionary rather than revolutionary way. … … 234 236 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. 235 237 As a result, there is a significant learning curve to move to these languages, and C legacy-code must be rewritten. 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; 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; 241 242 as a result, it will fade into disuse. 242 243 Considering the large body of existing C code and programmers, there is significant impetus to ensure C is transformed into a modern programming language. … … 254 255 \begin{lstlisting} 255 256 ®forall( otype T )® T identity( T val ) { return val; } 256 int forty_two = identity( 42 ); §\C{// T is bound to int, forty\_two == 42}§257 int forty_two = identity( 42 ); §\C{// T is bound to int, forty\_two == 42}§ 257 258 \end{lstlisting} 258 259 % extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions. … … 282 283 283 284 double key = 5.0, vals[10] = { /* 10 sorted floating values */ }; 284 double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); §\C{// search sorted array}§285 double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); §\C{// search sorted array}§ 285 286 \end{lstlisting} 286 287 which can be augmented simply with a polymorphic, type-safe, \CFA-overloaded wrappers: … … 291 292 292 293 forall( otype T | { int ?<?( T, T ); } ) unsigned int bsearch( T key, const T * arr, size_t size ) { 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}§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}§ 297 298 int posn = bsearch( 5.0, vals, 10 ); 298 299 \end{lstlisting} … … 306 307 \begin{lstlisting} 307 308 forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); } 308 int * ip = malloc(); §\C{// select type and size from left-hand side}§309 int * ip = malloc(); §\C{// select type and size from left-hand side}§ 309 310 double * dp = malloc(); 310 311 struct S {...} * sp = malloc(); … … 317 318 Whereas, \CFA wraps each of these routines into ones with the overloaded name ©abs©: 318 319 \begin{cfa} 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 );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 ); 329 330 \end{cfa} 330 331 The problem is the name clash between the library routine ©abs© and the \CFA names ©abs©. 331 332 Hence, names appearing in an ©extern "C"© block have \newterm*{C linkage}. 332 333 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. 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 type s.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. 335 336 336 337 This example strongly illustrates a core idea in \CFA: \emph{the \Index{power of a name}}. … … 349 350 \begin{description} 350 351 \item 351 \Indexc{-std=gnu 11}\index{compilation option!-std=gnu11@{©-std=gnu11©}}352 The 2011C 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 C 11mode, which allows inline routines in header files.352 \Indexc{-std=gnu99}\index{compilation option!-std=gnu99@{©-std=gnu99©}} 353 The 1999 C 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 C99 mode, which allows inline routines in header files. 356 357 \end{description} 357 358 The following new \CFA options are available: … … 426 427 \begin{cfa} 427 428 #ifndef __CFORALL__ 428 #include <stdio.h>§\indexc{stdio.h}§ §\C{// C header file}§429 #include <stdio.h>§\indexc{stdio.h}§ §\C{// C header file}§ 429 430 #else 430 #include <fstream>§\indexc{fstream}§ §\C{// \CFA header file}§431 #include <fstream>§\indexc{fstream}§ §\C{// \CFA header file}§ 431 432 #endif 432 433 \end{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} 434 which conditionally includes the correct header file, if the program is compiled using \Indexc{gcc} or \Indexc{cfa}. 540 435 541 436 … … 544 439 Numeric constants are extended to allow \Index{underscore}s\index{constant!underscore}, \eg: 545 440 \begin{cfa} 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}§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}§ 556 451 \end{cfa} 557 452 The rules for placement of underscores are: … … 574 469 575 470 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. 484 485 \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} 502 \end{figure} 503 504 576 505 \section{Exponentiation Operator} 577 506 578 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. 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$.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$. 580 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)©. 581 510 582 511 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} 655 656 \begin{figure} 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} 770 \end{figure} 771 772 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}} 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}} 1044 527 1045 528 While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure. 1046 529 Unfortunately, this restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting. 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.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. 1048 531 For both ©continue© and ©break©, the target label must be directly associated with a ©for©, ©while© or ©do© statement; 1049 532 for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement. … … 1130 613 \end{figure} 1131 614 1132 Both labelled ©continue© and ©break© are a ©goto©\index{goto@ ©goto©!restricted} restricted in the following ways:615 Both labelled ©continue© and ©break© are a ©goto©\index{goto@\lstinline@goto@!restricted} restricted in the following ways: 1133 616 \begin{itemize} 1134 617 \item … … 1143 626 With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader. 1144 627 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. 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}} 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}} 1150 890 \label{s:WithStatement} 1151 891 1152 892 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: 1153 893 \begin{cfa} 1154 struct S { §\C{// aggregate}§1155 char c; §\C{// fields}§894 struct S { §\C{// aggregate}§ 895 char c; §\C{// fields}§ 1156 896 int i; 1157 897 double d; … … 1162 902 \begin{cfa} 1163 903 void f( S s ) { 1164 ®s.®c; ®s.®i; ®s.®d;§\C{// access containing fields}§904 `s.`c; `s.`i; `s.`d; §\C{// access containing fields}§ 1165 905 } 1166 906 \end{cfa} … … 1169 909 \begin{C++} 1170 910 struct S { 1171 char c; §\C{// fields}§911 char c; §\C{// fields}§ 1172 912 int i; 1173 913 double d; 1174 void f() { §\C{// implicit ``this'' aggregate}§1175 ®this->®c; ®this->®i; ®this->®d;§\C{// access containing fields}§914 void f() { §\C{// implicit ``this'' aggregate}§ 915 `this->`c; `this->`i; `this->`d; §\C{// access containing fields}§ 1176 916 } 1177 917 } 1178 918 \end{C++} 1179 Object-oriented nesting of member functions in a \lstinline[language=C++]@class/struct@ allows eliding \lstinline[language=C++] @this->@because of lexical scoping.919 Object-oriented nesting of member functions in a \lstinline[language=C++]@class/struct@ allows eliding \lstinline[language=C++]$this->$ because of lexical scoping. 1180 920 However, for other aggregate parameters, qualification is necessary: 1181 921 \begin{cfa} 1182 922 struct T { double m, n; }; 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.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. 1190 930 Hence, the qualified fields become variables with the side-effect that it is easier to optimizing field references in a block. 1191 931 \begin{cfa} 1192 void f( S & this ) ®with ( this )® {§\C{// with statement}§1193 c; i; d; §\C{\color{red}// this.c, this.i, this.d}§932 void f( S & this ) `with ( this )` { §\C{// with statement}§ 933 c; i; d; §\C{\color{red}// this.c, this.i, this.d}§ 1194 934 } 1195 935 \end{cfa} 1196 936 with the generality of opening multiple aggregate-parameters: 1197 937 \begin{cfa} 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: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: 1205 945 \begin{cfa} 1206 946 §\emph{with-statement}§: … … 1217 957 The difference between parallel and nesting occurs for fields with the same name and type: 1218 958 \begin{cfa} 1219 struct S { int ®i®; int j; double m; } s, w;1220 struct T { int ®i®; int k; int m; } t, w;959 struct S { int `i`; int j; double m; } s, w; 960 struct T { int `i`; int k; int m; } t, w; 1221 961 with ( s, t ) { 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©.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@. 1233 973 \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. 1234 974 Qualification or a cast is used to disambiguate. 1235 975 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©.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@. 1243 983 To solve this problem, parameters are treated like an initialized aggregate: 1244 984 \begin{cfa} … … 1250 990 and implicitly opened \emph{after} a function-body open, to give them higher priority: 1251 991 \begin{cfa} 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: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: 1262 1002 % \begin{cfa} 1263 1003 % struct S { int i, j; } sv; 1264 % with ( sv ) { §\C{// implicit reference}§1004 % with ( sv ) { §\C{// implicit reference}§ 1265 1005 % S & sr = sv; 1266 % with ( sr ) { §\C{// explicit reference}§1006 % with ( sr ) { §\C{// explicit reference}§ 1267 1007 % S * sp = &sv; 1268 % with ( *sp ) { §\C{// computed reference}§1269 % i = 3; j = 4; §\C{\color{red}// sp--{\textgreater}i, sp--{\textgreater}j}§1008 % with ( *sp ) { §\C{// computed reference}§ 1009 % i = 3; j = 4; §\C{\color{red}// sp--{\textgreater}i, sp--{\textgreater}j}§ 1270 1010 % } 1271 % i = 2; j = 3; §\C{\color{red}// sr.i, sr.j}§1011 % i = 2; j = 3; §\C{\color{red}// sr.i, sr.j}§ 1272 1012 % } 1273 % i = 1; j = 2; §\C{\color{red}// sv.i, sv.j}§1013 % i = 1; j = 2; §\C{\color{red}// sv.i, sv.j}§ 1274 1014 % } 1275 1015 % \end{cfa} … … 1279 1019 class C { 1280 1020 int i, 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}§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}§ 1284 1024 } 1285 1025 } … … 1288 1028 \begin{cfa} 1289 1029 struct S { int i, j; }; 1290 int mem( S & ®this® ) { §\C{// explicit "this" parameter}§1291 ®this.®i = 1; §\C{// "this" is not elided}§1030 int mem( S & ®this® ) { §\C{// explicit "this" parameter}§ 1031 ®this.®i = 1; §\C{// "this" is not elided}§ 1292 1032 ®this.®j = 2; 1293 1033 } … … 1298 1038 \begin{cfa} 1299 1039 int mem( S & this ) ®with( this )® { §\C{// with clause}§ 1300 i = 1; §\C{\color{red}// this.i}§1301 j = 2; §\C{\color{red}// this.j}§1040 i = 1; §\C{\color{red}// this.i}§ 1041 j = 2; §\C{\color{red}// this.j}§ 1302 1042 } 1303 1043 \end{cfa} … … 1316 1056 struct S1 { ... } s1; 1317 1057 struct S2 { ... } s2; 1318 ®with( s1 )® { §\C{// with statement}§1058 ®with( s1 )® { §\C{// with statement}§ 1319 1059 // access fields of s1 without qualification 1320 ®with s2® { §\C{// nesting}§1060 ®with s2® { §\C{// nesting}§ 1321 1061 // access fields of s1 and s2 without qualification 1322 1062 } … … 1373 1113 Non-local transfer can cause stack unwinding, \ie non-local routine termination, depending on the kind of raise. 1374 1114 \begin{cfa} 1375 exception_t E {}; §\C{// exception type}§1115 exception_t E {}; §\C{// exception type}§ 1376 1116 void f(...) { 1377 ... throw E{}; ... §\C{// termination}§1378 ... throwResume E{}; ... §\C{// resumption}§1117 ... throw E{}; ... §\C{// termination}§ 1118 ... throwResume E{}; ... §\C{// resumption}§ 1379 1119 } 1380 1120 try { 1381 1121 f(...); 1382 } catch( E e ; §boolean-predicate§ ) { §\C[8cm]{// termination handler}§1122 } catch( E e ; §boolean-predicate§ ) { §\C[8cm]{// termination handler}§ 1383 1123 // recover and continue 1384 } catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}\CRT§1124 } catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}\CRT§ 1385 1125 // repair and return 1386 1126 } finally { … … 1442 1182 For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way: 1443 1183 \begin{cfa} 1444 int ®(*®f®())[®5®]® {...}; §\C{// definition}§1445 ... ®(*®f®())[®3®]® += 1; §\C{// usage}§1184 int ®(*®f®())[®5®]® {...}; §\C{// definition}§ 1185 ... ®(*®f®())[®3®]® += 1; §\C{// usage}§ 1446 1186 \end{cfa} 1447 1187 Essentially, the return type is wrapped around the routine name in successive layers (like an \Index{onion}). 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} 1188 While attempting to make the two contexts consistent is a laudable goal, it has not worked out in practice. 1452 1189 1453 1190 \CFA provides its own type, variable and routine declarations, using a different syntax. … … 1635 1372 *x = 3; // implicit dereference 1636 1373 int * ®const® y = (int *)104; 1637 *y = *x; // implicit dereference1374 *y = *x; // implicit dereference 1638 1375 \end{cfa} 1639 1376 \end{tabular} … … 1649 1386 \hline 1650 1387 \begin{cfa} 1651 lda r1,100 // load address of x1652 ld r2,(r1) // load value of x1653 lda r3,104 // load address of y1654 st r2,(r3) // store x into y1388 lda r1,100 // load address of x 1389 ld r2,(r1) // load value of x 1390 lda r3,104 // load address of y 1391 st r2,(r3) // store x into y 1655 1392 \end{cfa} 1656 1393 & 1657 1394 \begin{cfa} 1658 1395 1659 ld r2,(100) // load value of x1660 1661 st r2,(104) // store x into y1396 ld r2,(100) // load value of x 1397 1398 st r2,(104) // store x into y 1662 1399 \end{cfa} 1663 1400 \end{tabular} … … 1673 1410 \begin{cfa} 1674 1411 int x, y, ®*® p1, ®*® p2, ®**® p3; 1675 p1 = ®&®x; // p1 points to x1676 p2 = p1; // p2 points to x1677 p1 = ®&®y; // p1 points to y1678 p3 = &p2; // p3 points to p21412 p1 = ®&®x; // p1 points to x 1413 p2 = p1; // p2 points to x 1414 p1 = ®&®y; // p1 points to y 1415 p3 = &p2; // p3 points to p2 1679 1416 \end{cfa} 1680 1417 & … … 1687 1424 For example, \Index*{Algol68}~\cite{Algol68} infers pointer dereferencing to select the best meaning for each pointer usage 1688 1425 \begin{cfa} 1689 p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§1426 p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§ 1690 1427 \end{cfa} 1691 1428 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. 1692 Unfortunately, automatic dereferencing does not work in all cases, and so some mechanism is necessary to fix incorrect choices. 1429 Unfortunately, automatic dereferencing does not work in all cases, and so some mechanism is necessary to fix incorrect choices. 1693 1430 1694 1431 Rather than inferring dereference, most programming languages pick one implicit dereferencing semantics, and the programmer explicitly indicates the other to resolve address-duality. 1695 1432 In C, objects of pointer type always manipulate the pointer object's address: 1696 1433 \begin{cfa} 1697 p1 = p2; §\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}§1698 p2 = p1 + x; §\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}§1434 p1 = p2; §\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}§ 1435 p2 = p1 + x; §\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}§ 1699 1436 \end{cfa} 1700 1437 even though the assignment to ©p2© is likely incorrect, and the programmer probably meant: 1701 1438 \begin{cfa} 1702 p1 = p2; §\C{// pointer address assignment}§1703 ®*®p2 = ®*®p1 + x; §\C{// pointed-to value assignment / operation}§1439 p1 = p2; §\C{// pointer address assignment}§ 1440 ®*®p2 = ®*®p1 + x; §\C{// pointed-to value assignment / operation}§ 1704 1441 \end{cfa} 1705 1442 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©). … … 1718 1455 \begin{cfa} 1719 1456 int x, y, ®&® r1, ®&® r2, ®&&® r3; 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}§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}§ 1724 1461 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); §\C{// implicit dereferencing}§ 1725 1462 \end{cfa} … … 1737 1474 For a \CFA reference type, the cancellation on the left-hand side of assignment leaves the reference as an address (\Index{lvalue}): 1738 1475 \begin{cfa} 1739 (&®*®)r1 = &x; §\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}§1476 (&®*®)r1 = &x; §\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}§ 1740 1477 \end{cfa} 1741 1478 Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}): 1742 1479 \begin{cfa} 1743 (&(&®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}§1480 (&(&®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}§ 1744 1481 \end{cfa} 1745 1482 Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth. … … 1749 1486 int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, 1750 1487 &r1 = x, &&r2 = r1, &&&r3 = r2; 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}§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}§ 1758 1495 \end{cfa} 1759 1496 Furthermore, both types are equally performant, as the same amount of dereferencing occurs for both types. … … 1762 1499 As for a pointer type, a reference type may have qualifiers: 1763 1500 \begin{cfa} 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}§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}§ 1773 1510 \end{cfa} 1774 1511 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}: 1775 1512 \begin{cfa} 1776 int & const cr = *0; §\C{// where 0 is the int * zero}§1513 int & const cr = *0; §\C{// where 0 is the int * zero}§ 1777 1514 \end{cfa} 1778 1515 Note, constant reference-types do not prevent \Index{addressing errors} because of explicit storage-management: … … 1781 1518 cr = 5; 1782 1519 free( &cr ); 1783 cr = 7; §\C{// unsound pointer dereference}§1520 cr = 7; §\C{// unsound pointer dereference}§ 1784 1521 \end{cfa} 1785 1522 … … 1806 1543 \begin{cfa} 1807 1544 int w, x, y, z, & ar[3] = { x, y, z }; §\C{// initialize array of references}§ 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}§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}§ 1813 1550 \end{cfa} 1814 1551 … … 1827 1564 Therefore, for pointer/reference initialization, the initializing value must be an address not a value. 1828 1565 \begin{cfa} 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}§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}§ 1832 1569 \end{cfa} 1833 1570 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). … … 1838 1575 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. 1839 1576 \begin{cfa} 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}§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}§ 1842 1579 \end{cfa} 1843 1580 Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©r© can be locally reassigned within ©f©. … … 1866 1603 void f( int & r ); 1867 1604 void g( int * p ); 1868 f( 3 ); g( ®&®3 ); §\C{// compiler implicit generates temporaries}§1869 f( x + y ); g( ®&®(x + y) ); §\C{// compiler implicit generates temporaries}§1605 f( 3 ); g( ®&®3 ); §\C{// compiler implicit generates temporaries}§ 1606 f( x + y ); g( ®&®(x + y) ); §\C{// compiler implicit generates temporaries}§ 1870 1607 \end{cfa} 1871 1608 Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{ … … 1878 1615 \begin{cfa} 1879 1616 void f( int i ); 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}§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}§ 1886 1623 \end{cfa} 1887 1624 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. 1888 1625 Instead, a routine object should be referenced by a ©const© reference: 1889 1626 \begin{cfa} 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}§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}§ 1895 1632 \end{cfa} 1896 1633 because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{ … … 1905 1642 \begin{itemize} 1906 1643 \item 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).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). 1911 1648 \end{itemize} 1912 1649 The following example shows the first rule applied to different \Index{rvalue} contexts: … … 1914 1651 int x, * px, ** ppx, *** pppx, **** ppppx; 1915 1652 int & rx = x, && rrx = rx, &&& rrrx = rrx ; 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)}§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) 1921 1658 \end{cfa} 1922 1659 The following example shows the second rule applied to different \Index{lvalue} contexts: … … 1924 1661 int x, * px, ** ppx, *** pppx; 1925 1662 int & rx = x, && rrx = rx, &&& rrrx = rrx ; 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§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) 1930 1667 \end{cfa} 1931 1668 … … 1940 1677 \begin{cfa} 1941 1678 int x; 1942 x + 1; §\C[2.0in]{// lvalue variable (int) converts to rvalue for expression}§1679 x + 1; // lvalue variable (int) converts to rvalue for expression 1943 1680 \end{cfa} 1944 1681 An rvalue has no type qualifiers (©cv©), so the lvalue qualifiers are dropped. … … 1950 1687 \begin{cfa} 1951 1688 int x, &r = x, f( int p ); 1952 x = ®r® + f( ®r® ); §\C{// lvalue reference converts to rvalue}§1689 x = ®r® + f( ®r® ); // lvalue reference converts to rvalue 1953 1690 \end{cfa} 1954 1691 An rvalue has no type qualifiers (©cv©), so the reference qualifiers are dropped. … … 1957 1694 lvalue to reference conversion: \lstinline[deletekeywords=lvalue]@lvalue-type cv1 T@ converts to ©cv2 T &©, which allows implicitly converting variables to references. 1958 1695 \begin{cfa} 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 \&)}§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 &) 1961 1698 \end{cfa} 1962 1699 Conversion can restrict a type, where ©cv1© $\le$ ©cv2©, \eg passing an ©int© to a ©const volatile int &©, which has low cost. … … 1968 1705 \begin{cfa} 1969 1706 int x, & f( int & p ); 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§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 &) 1972 1709 \end{cfa} 1973 1710 In both case, modifications to the temporary are inaccessible (\Index{warning}). … … 2158 1895 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: 2159 1896 \begin{cfa} 2160 [§\,§] g(); §\C{// no input or output parameters}§2161 [ void ] g( void ); §\C{// no input or output parameters}§1897 [§\,§] g(); §\C{// no input or output parameters}§ 1898 [ void ] g( void ); §\C{// no input or output parameters}§ 2162 1899 \end{cfa} 2163 1900 … … 2177 1914 \begin{cfa} 2178 1915 typedef int foo; 2179 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§1916 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§ 2180 1917 \end{cfa} 2181 1918 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. … … 2185 1922 C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg: 2186 1923 \begin{cfa} 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}§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}§ 2189 1926 \end{cfa} 2190 1927 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: 2191 1928 \begin{cfa} 2192 1929 #define ptoa( n, d ) int (*n)[ d ] 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 ] )}§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 ] )}§ 2195 1932 \end{cfa} 2196 1933 Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms. … … 2214 1951 int z; 2215 1952 ... x = 0; ... y = z; ... 2216 ®return;® §\C{// implicitly return x, y}§1953 ®return;® §\C{// implicitly return x, y}§ 2217 1954 } 2218 1955 \end{cfa} … … 2224 1961 [ int x, int y ] f() { 2225 1962 ... 2226 } §\C{// implicitly return x, y}§1963 } §\C{// implicitly return x, y}§ 2227 1964 \end{cfa} 2228 1965 In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return© had been encountered. … … 2233 1970 [ int x, int y ] f( int, x, int y ) { 2234 1971 ... 2235 } §\C{// implicitly return x, y}§1972 } §\C{// implicitly return x, y}§ 2236 1973 \end{cfa} 2237 1974 This notation allows the compiler to eliminate temporary variables in nested routine calls. 2238 1975 \begin{cfa} 2239 [ int x, int y ] f( int, x, int y ); §\C{// prototype declaration}§1976 [ int x, int y ] f( int, x, int y ); §\C{// prototype declaration}§ 2240 1977 int a, b; 2241 1978 [a, b] = f( f( f( a, b ) ) ); … … 2251 1988 as well, parameter names are optional, \eg: 2252 1989 \begin{cfa} 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}§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}§ 2257 1994 \end{cfa} 2258 1995 This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa). … … 2275 2012 The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg: 2276 2013 \begin{cfa} 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}§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}§ 2281 2018 \end{cfa} 2282 2019 While parameter names are optional, \emph{a routine name cannot be specified}; 2283 2020 for example, the following is incorrect: 2284 2021 \begin{cfa} 2285 * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§2022 * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§ 2286 2023 \end{cfa} 2287 2024 … … 2306 2043 whereas a named (keyword) call may be: 2307 2044 \begin{cfa} 2308 p( z : 3, x : 4, y : 7 ); §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§2045 p( z : 3, x : 4, y : 7 ); §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§ 2309 2046 \end{cfa} 2310 2047 Here the order of the arguments is unimportant, and the names of the parameters are used to associate argument values with the corresponding parameters. … … 2323 2060 For example, the following routine prototypes and definition are all valid. 2324 2061 \begin{cfa} 2325 void p( int, int, int ); §\C{// equivalent prototypes}§2062 void p( int, int, int ); §\C{// equivalent prototypes}§ 2326 2063 void p( int x, int y, int z ); 2327 2064 void p( int y, int x, int z ); 2328 2065 void p( int z, int y, int x ); 2329 void p( int q, int r, int s ) {} §\C{// match with this definition}§2066 void p( int q, int r, int s ) {} §\C{// match with this definition}§ 2330 2067 \end{cfa} 2331 2068 Forcing matching parameter names in routine prototypes with corresponding routine definitions is possible, but goes against a strong tradition in C programming. … … 2339 2076 int f( int x, double y ); 2340 2077 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}§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}§ 2344 2081 \end{cfa} 2345 2082 However, named arguments compound routine resolution in conjunction with conversions: 2346 2083 \begin{cfa} 2347 f( i : 3, 5.7 ); §\C{// ambiguous call ?}§2084 f( i : 3, 5.7 ); §\C{// ambiguous call ?}§ 2348 2085 \end{cfa} 2349 2086 Depending on the cost associated with named arguments, this call could be resolvable or ambiguous. … … 2359 2096 the allowable positional calls are: 2360 2097 \begin{cfa} 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 )}§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 )}§ 2365 2102 // empty arguments 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 )}§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 )}§ 2373 2110 \end{cfa} 2374 2111 Here the missing arguments are inserted from the default values in the parameter list. … … 2394 2131 Default values may only appear in a prototype versus definition context: 2395 2132 \begin{cfa} 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}§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}§ 2399 2136 \end{cfa} 2400 2137 The reason for this restriction is to allow separate compilation. … … 2421 2158 \begin{cfa} 2422 2159 void p( int x, int y = 2, int z = 3... ); 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 */, ... );}§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 */, ... );}§ 2425 2162 \end{cfa} 2426 2163 The first call is an error because arguments 4 and 5 are actually positional not ellipse arguments; … … 2452 2189 Furthermore, overloading cannot handle accessing default arguments in the middle of a positional list, via a missing argument, such as: 2453 2190 \begin{cfa} 2454 p( 1, /* default */, 5 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§2191 p( 1, /* default */, 5 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§ 2455 2192 \end{cfa} 2456 2193 … … 2465 2202 \begin{cfa} 2466 2203 struct { 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}§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}§ 2473 2210 }; 2474 2211 \end{cfa} … … 2478 2215 \begin{cfa} 2479 2216 struct { 2480 int , , ; §\C{// 3 unnamed fields}§2217 int , , ; §\C{// 3 unnamed fields}§ 2481 2218 } 2482 2219 \end{cfa} … … 2525 2262 struct T t; 2526 2263 } s; 2527 2264 2528 2265 2529 2266 … … 2572 2309 const unsigned int size = 5; 2573 2310 int ia[size]; 2574 ... §\C{// assign values to array ia}§2575 qsort( ia, size ); §\C{// sort ascending order using builtin ?<?}§2311 ... §\C{// assign values to array ia}§ 2312 qsort( ia, size ); §\C{// sort ascending order using builtin ?<?}§ 2576 2313 { 2577 2314 ®int ?<?( int x, int y ) { return x > y; }® §\C{// nested routine}§ 2578 qsort( ia, size ); §\C{// sort descending order by local redefinition}§2315 qsort( ia, size ); §\C{// sort descending order by local redefinition}§ 2579 2316 } 2580 2317 \end{cfa} … … 2584 2321 The following program in undefined in \CFA (and Indexc{gcc}) 2585 2322 \begin{cfa} 2586 [* [int]( int )] foo() { §\C{// int (* foo())( int )}§2323 [* [int]( int )] foo() { §\C{// int (* foo())( int )}§ 2587 2324 int ®i® = 7; 2588 2325 int bar( int p ) { 2589 ®i® += 1; §\C{// dependent on local variable}§2590 sout | ®i® ;2326 ®i® += 1; §\C{// dependent on local variable}§ 2327 sout | ®i® | endl; 2591 2328 } 2592 return bar; §\C{// undefined because of local dependence}§2329 return bar; §\C{// undefined because of local dependence}§ 2593 2330 } 2594 2331 int main() { 2595 * [int]( int ) fp = foo(); §\C{// int (* fp)( int )}§2596 sout | fp( 3 ) ;2597 } 2598 \end{cfa} 2599 because 2332 * [int]( int ) fp = foo(); §\C{// int (* fp)( int )}§ 2333 sout | fp( 3 ) | endl; 2334 } 2335 \end{cfa} 2336 because 2600 2337 2601 2338 Currently, there are no \Index{lambda} expressions, \ie unnamed routines because routine names are very important to properly select the correct routine. … … 2606 2343 In C and \CFA, lists of elements appear in several contexts, such as the parameter list of a routine call. 2607 2344 \begin{cfa} 2608 f( ®2, x, 3 + i® ); §\C{// element list}§2345 f( ®2, x, 3 + i® ); §\C{// element list}§ 2609 2346 \end{cfa} 2610 2347 A list of elements is called a \newterm{tuple}, and is different from a \Index{comma expression}. … … 2623 2360 typedef struct { int quot, rem; } div_t; §\C[7cm]{// from include stdlib.h}§ 2624 2361 div_t div( int num, int den ); 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}§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}§ 2627 2364 \end{cfa} 2628 2365 This approach requires a name for the return type and fields, where \Index{naming} is a common programming-language issue. … … 2634 2371 For example, consider C's \Indexc{modf} function, which returns the integral and fractional part of a floating value. 2635 2372 \begin{cfa} 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}§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}§ 2639 2376 \end{cfa} 2640 2377 This approach requires allocating storage for the return values, which complicates the call site with a sequence of variable declarations leading to the call. … … 2663 2400 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. 2664 2401 \begin{cfa} 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}§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}§ 2669 2406 \end{cfa} 2670 2407 In this case, there are two overloaded ©g© routines. … … 2675 2412 The previous examples can be rewritten passing the multiple returned-values directly to the ©printf© function call. 2676 2413 \begin{cfa} 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}§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}§ 2682 2419 \end{cfa} 2683 2420 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. … … 2689 2426 \begin{cfa} 2690 2427 int quot, rem; 2691 [ quot, rem ] = div( 13, 5 ); §\C{// assign multiple variables}§2692 printf( "%d %d\n", quot, rem ); §\C{// print quotient/remainder}\CRT§2428 [ quot, rem ] = div( 13, 5 ); §\C{// assign multiple variables}§ 2429 printf( "%d %d\n", quot, rem ); §\C{// print quotient/remainder}\CRT§ 2693 2430 \end{cfa} 2694 2431 Here, the multiple return-values are matched in much the same way as passing multiple return-values to multiple parameters in a call. … … 2696 2433 2697 2434 \subsection{Expressions} 2698 2699 % Change order of expression evaluation.2700 % http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0145r2.pdf2701 2435 2702 2436 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. … … 2719 2453 In \CFA, it is possible to overcome this restriction by declaring a \newterm{tuple variable}. 2720 2454 \begin{cfa} 2721 [int, int] ®qr® = div( 13, 5 ); §\C{// initialize tuple variable}§2722 printf( "%d %d\n", ®qr® ); §\C{// print quotient/remainder}§2455 [int, int] ®qr® = div( 13, 5 ); §\C{// initialize tuple variable}§ 2456 printf( "%d %d\n", ®qr® ); §\C{// print quotient/remainder}§ 2723 2457 \end{cfa} 2724 2458 It is now possible to match the multiple return-values to a single variable, in much the same way as \Index{aggregation}. … … 2726 2460 One way to access the individual components of a tuple variable is with assignment. 2727 2461 \begin{cfa} 2728 [ quot, rem ] = qr; §\C{// assign multiple variables}§2462 [ quot, rem ] = qr; §\C{// assign multiple variables}§ 2729 2463 \end{cfa} 2730 2464 … … 2749 2483 [int, double] * p; 2750 2484 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}§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}§ 2756 2490 \end{cfa} 2757 2491 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. … … 2820 2554 double y; 2821 2555 [int, double] z; 2822 [y, x] = 3.14; §\C{// mass assignment}§2556 [y, x] = 3.14; §\C{// mass assignment}§ 2823 2557 [x, y] = z; §\C{// multiple assignment}§ 2824 2558 z = 10; §\C{// mass assignment}§ 2825 z = [x, y]; §\C{// multiple assignment}§2559 z = [x, y]; §\C{// multiple assignment}§ 2826 2560 \end{cfa} 2827 2561 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. … … 2867 2601 double c, d; 2868 2602 [ void ] f( [ int, int ] ); 2869 f( [ c, a ] = [ b, d ] = 1.5 ); §\C{// assignments in parameter list}§2603 f( [ c, a ] = [ b, d ] = 1.5 ); // assignments in parameter list 2870 2604 \end{cfa} 2871 2605 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. … … 2880 2614 \begin{cfa} 2881 2615 struct S; 2882 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}§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 constructors 2622 [S, S] y; // uses (1), (1), default constructor 2623 [S, S] z = x.0; // uses (4), (4), copy constructor 2890 2624 \end{cfa} 2891 2625 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)©. … … 2928 2662 A member-access tuple may be used anywhere a tuple can be used, \eg: 2929 2663 \begin{cfa} 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 )}§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 )}§ 2932 2666 \end{cfa} 2933 2667 Note, the fields appearing in a record-field tuple may be specified in any order; … … 2939 2673 void f( double, long ); 2940 2674 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 ]}§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 ]}§ 2943 2677 [ long, int, long ] y = x.[ 2, 0, 2 ]; 2944 2678 \end{cfa} … … 2957 2691 \begin{cfa} 2958 2692 [ int, float, double ] f(); 2959 [ double, float ] x = f().[ 2, 1 ]; §\C{// f() called once}§2693 [ double, float ] x = f().[ 2, 1 ]; §\C{// f() called once}§ 2960 2694 \end{cfa} 2961 2695 … … 2970 2704 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. 2971 2705 \begin{cfa} 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)}§2706 int f(); // (1) 2707 double f(); // (2) 2708 2709 f(); // ambiguous - (1),(2) both equally viable 2710 (int)f(); // choose (2) 2977 2711 \end{cfa} 2978 2712 Since casting is a fundamental operation in \CFA, casts need to be given a meaningful interpretation in the context of tuples. … … 2982 2716 void g(); 2983 2717 2984 (void)f(); §\C{// valid, ignore results}§2985 (int)g(); §\C{// invalid, void cannot be converted to int}§2718 (void)f(); // valid, ignore results 2719 (int)g(); // invalid, void cannot be converted to int 2986 2720 2987 2721 struct A { int x; }; 2988 (struct A)f(); §\C{// invalid, int cannot be converted to A}§2722 (struct A)f(); // invalid, int cannot be converted to A 2989 2723 \end{cfa} 2990 2724 In C, line 4 is a valid cast, which calls ©f© and discards its result. … … 3002 2736 [int, [int, int], int] g(); 3003 2737 3004 ([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}§2738 ([int, double])f(); // (1) valid 2739 ([int, int, int])g(); // (2) valid 2740 ([void, [int, int]])g(); // (3) valid 2741 ([int, int, int, int])g(); // (4) invalid 2742 ([int, [int, int, int]])g(); // (5) invalid 3009 2743 \end{cfa} 3010 2744 … … 3066 2800 void f([int, int], int, int); 3067 2801 3068 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}§2802 f([0, 0], 0, 0); // no cost 2803 f(0, 0, 0, 0); // cost for structuring 2804 f([0, 0,], [0, 0]); // cost for flattening 2805 f([0, 0, 0], 0); // cost for flattening and structuring 3072 2806 \end{cfa} 3073 2807 … … 3132 2866 [ unsigned int, char ] 3133 2867 [ double, double, double ] 3134 [ * int, int * ] §\C{// mix of CFA and ANSI}§2868 [ * int, int * ] §\C{// mix of CFA and ANSI}§ 3135 2869 [ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ] 3136 2870 \end{cfa} … … 3139 2873 Examples of declarations using tuple types are: 3140 2874 \begin{cfa} 3141 [ int, int ] x; §\C{// 2 element tuple, each element of type int}§3142 * [ char, char ] y; §\C{// pointer to a 2 element tuple}§2875 [ int, int ] x; §\C{// 2 element tuple, each element of type int}§ 2876 * [ char, char ] y; §\C{// pointer to a 2 element tuple}§ 3143 2877 [ [ int, int ] ] z ([ int, int ]); 3144 2878 \end{cfa} … … 3157 2891 [ int, int ] w1; 3158 2892 [ int, int, int ] w2; 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}§2893 [ void ] f (int, int, int); /* three input parameters of type int */ 2894 [ void ] g ([ int, int, int ]); /* 3 element tuple as input */ 3161 2895 f( [ 1, 2, 3 ] ); 3162 2896 f( w1, 3 ); … … 3238 2972 [ int, int, int, int ] w = [ 1, 2, 3, 4 ]; 3239 2973 int x = 5; 3240 [ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§2974 [ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§ 3241 2975 \end{cfa} 3242 2976 Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values; … … 3326 3060 both these examples produce indeterminate results: 3327 3061 \begin{cfa} 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}§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}§ 3330 3064 \end{cfa} 3331 3065 … … 3349 3083 3350 3084 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. 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. 3380 3094 \begin{cquote} 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}} \\3095 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 3096 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{\CC}} \\ 3383 3097 \begin{cfa} 3384 3098 int x = 1, y = 2, z = 3; 3385 sout | x ®|® y ®|® z ;3099 sout | x ®|® y ®|® z | endl; 3386 3100 \end{cfa} 3387 3101 & … … 3389 3103 3390 3104 cout << x ®<< " "® << y ®<< " "® << z << endl; 3391 \end{cfa}3392 &3393 \begin{cfa}3394 x = 1; y = 2; z = 33395 print( x, y, z )3396 3105 \end{cfa} 3397 3106 \\ … … 3401 3110 & 3402 3111 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3403 1® ®2® ®3 3404 \end{cfa} 3405 & 3406 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3407 1® ®2® ®3 3112 1 2 3 3408 3113 \end{cfa} 3409 3114 \end{tabular} 3410 3115 \end{cquote} 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 valueseparated by ``\lstinline[showspaces=true]@, @''.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 values separated by ``\lstinline[showspaces=true]@, @''. 3413 3118 \begin{cfa} 3414 3119 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ]; 3415 sout | t1 | t2 ;§\C{// print tuples}§3120 sout | t1 | t2 | endl; §\C{// print tuples}§ 3416 3121 \end{cfa} 3417 3122 \begin{cfa}[showspaces=true,aboveskip=0pt] 3418 3123 1®, ®2®, ®3 4®, ®5®, ®6 3419 3124 \end{cfa} 3420 Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority \emph{overloadable}operator, other than assignment.3125 Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority overloadable operator, other than assignment. 3421 3126 Therefore, fewer output expressions require parenthesis. 3422 3127 \begin{cquote} … … 3425 3130 & 3426 3131 \begin{cfa} 3427 sout | x * 3 | y + 1 | z << 2 | x == y | ®(®x | y®)® | ®(®x || y®)® | ®(®x > z ? 1 : 2®)®;3132 sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl; 3428 3133 \end{cfa} 3429 3134 \\ … … 3431 3136 & 3432 3137 \begin{cfa} 3433 cout << x * 3 << y + 1 << ®(®z << 2®)® << ®(®x == y®)® << ®(®x | y®)® << ®(®x || y®)® << ®(®x > z ? 1 : 2®)®<< endl;3138 cout << x * 3 << y + 1 << ®(®z << 2®)® << ®(®x == y®)® << (x | y) << (x || y) << (x > z ? 1 : 2) << endl; 3434 3139 \end{cfa} 3435 3140 \\ … … 3440 3145 \end{tabular} 3441 3146 \end{cquote} 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 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. 3487 3148 3488 3149 3489 3150 \subsection{Implicit Separator} 3490 3151 3491 The \Index{implicit separator}\index{I/O!separator} character (space/blank) is a separator not a terminator for output.3152 The \Index{implicit separator}\index{I/O!separator} character (space/blank) is a separator not a terminator. 3492 3153 The rules for implicitly adding the separator are: 3493 3154 \begin{enumerate} … … 3495 3156 A separator does not appear at the start or end of a line. 3496 3157 \begin{cfa}[belowskip=0pt] 3497 sout | 1 | 2 | 3 ;3158 sout | 1 | 2 | 3 | endl; 3498 3159 \end{cfa} 3499 3160 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3504 3165 A separator does not appear before or after a character literal or variable. 3505 3166 \begin{cfa} 3506 sout | '1' | '2' | '3' ;3167 sout | '1' | '2' | '3' | endl; 3507 3168 123 3508 3169 \end{cfa} 3509 3170 3510 3171 \item 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 ;3172 A separator does not appear before or after a null (empty) C string. 3173 \begin{cfa} 3174 sout | 1 | "" | 2 | "" | 3 | endl; 3514 3175 123 3515 3176 \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]@([{=$£¥¡¿«@ 3181 %$ 3182 \begin{cfa}[mathescape=off] 3183 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥" 3184 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10 | endl; 3185 \end{cfa} 3186 %$ 3187 \begin{cfa}[mathescape=off,basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt] 3188 x ®(®1 x ®[®2 x ®{®3 x ®=®4 x ®$®5 x ®£®6 x ®¥®7 x ®¡®8 x ®¿®9 x ®«®10 3189 \end{cfa} 3190 %$ 3191 where \lstinline[basicstyle=\tt]@¡¿@ are inverted opening exclamation and question marks, and \lstinline[basicstyle=\tt]@«@ is an opening citation mark. 3516 3192 3517 3193 \item 3518 3194 {\lstset{language=CFA,deletedelim=**[is][]{¢}{¢}} 3519 A sep arator 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.3195 A seperator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[basicstyle=\tt]@,.;!?)]}%¢»@ 3520 3196 \begin{cfa}[belowskip=0pt] 3521 3197 sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x" 3522 | 7 | "¢ x" | 8 | "» x" | 9 | ") x" | 10 | "] x" | 11 | "} x" ;3198 | 7 | "¢ x" | 8 | "» x" | 9 | ") x" | 10 | "] x" | 11 | "} x" | endl; 3523 3199 \end{cfa} 3524 3200 \begin{cfa}[basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt] 3525 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 3526 3202 \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. 3530 %$ 3531 \begin{cfa}[mathescape=off] 3532 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥" 3533 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10; 3534 \end{cfa} 3535 %$ 3536 \begin{cfa}[mathescape=off,basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt] 3537 x ®(®1 x ®[®2 x ®{®3 x ®=®4 x ®$®5 x ®£®6 x ®¥®7 x ®¡®8 x ®¿®9 x ®«®10 3538 \end{cfa} 3539 %$ 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@ 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@ 3543 3207 \begin{cfa}[belowskip=0pt] 3544 sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx" ;3208 sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx" | endl; 3545 3209 \end{cfa} 3546 3210 \begin{cfa}[basicstyle=\tt,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt] … … 3551 3215 If a space is desired before or after one of the special string start/end characters, simply insert a space. 3552 3216 \begin{cfa}[belowskip=0pt] 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 ;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; 3554 3218 \end{cfa} 3555 3219 \begin{cfa}[basicstyle=\tt,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt] … … 3559 3223 3560 3224 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©). 3225 \subsection{Manipulator} 3226 3227 The following \CC-style \Index{manipulator}s and routines control implicit seperation. 3565 3228 \begin{enumerate} 3566 3229 \item 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.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. 3568 3231 The separator string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 3569 3232 \begin{cfa}[mathescape=off,belowskip=0pt] 3570 sepSet( sout, ", $" ); §\C{// set separator from " " to ", \$"}§3571 sout | 1 | 2 | 3 | " \"" | ®sep® | "\"" ;3233 sepSet( sout, ", $" ); §\C{// set separator from " " to ", \$"}§ 3234 sout | 1 | 2 | 3 | " \"" | ®sep® | "\"" | endl; 3572 3235 \end{cfa} 3573 3236 %$ … … 3577 3240 %$ 3578 3241 \begin{cfa}[belowskip=0pt] 3579 sepSet( sout, " " ); §\C{// reset separator to " "}§3580 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )® | "\"" ;3242 sepSet( sout, " " ); §\C{// reset separator to " "}§ 3243 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )® | "\"" | endl; 3581 3244 \end{cfa} 3582 3245 \begin{cfa}[showspaces=true,aboveskip=0pt] … … 3585 3248 ©sepGet© can be used to store a separator and then restore it: 3586 3249 \begin{cfa}[belowskip=0pt] 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 ;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; 3591 3254 \end{cfa} 3592 3255 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3594 3257 \end{cfa} 3595 3258 \begin{cfa}[belowskip=0pt] 3596 sepSet( sout, store ); §\C{// change separator back to original}§3597 sout | 1 | 2 | 3 ;3259 sepSet( sout, store ); §\C{// change separator back to original}§ 3260 sout | 1 | 2 | 3 | endl; 3598 3261 \end{cfa} 3599 3262 \begin{cfa}[showspaces=true,aboveskip=0pt] … … 3602 3265 3603 3266 \item 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.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. 3605 3268 The tuple separator-string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 3606 3269 \begin{cfa}[belowskip=0pt] 3607 sepSetTuple( sout, " " ); §\C{// set tuple separator from ", " to " "}§3608 sout | t1 | t2 | " \"" | ®sepTuple® | "\"" ;3270 sepSetTuple( sout, " " ); §\C{// set tuple separator from ", " to " "}§ 3271 sout | t1 | t2 | " \"" | ®sepTuple® | "\"" | endl; 3609 3272 \end{cfa} 3610 3273 \begin{cfa}[showspaces=true,aboveskip=0pt] … … 3612 3275 \end{cfa} 3613 3276 \begin{cfa}[belowskip=0pt] 3614 sepSetTuple( sout, ", " ); §\C{// reset tuple separator to ", "}§3615 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\"" ;3277 sepSetTuple( sout, ", " ); §\C{// reset tuple separator to ", "}§ 3278 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\"" | endl; 3616 3279 \end{cfa} 3617 3280 \begin{cfa}[showspaces=true,aboveskip=0pt] … … 3621 3284 3622 3285 \item 3623 \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} toggle printing the separator.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. 3624 3287 \begin{cfa}[belowskip=0pt] 3625 sout | sepDisable | 1 | 2 | 3 ; §\C{//turn off implicit separator}§3288 sout | sepDisable | 1 | 2 | 3 | endl; §\C{// globally turn off implicit separator}§ 3626 3289 \end{cfa} 3627 3290 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3629 3292 \end{cfa} 3630 3293 \begin{cfa}[belowskip=0pt] 3631 sout | sepEnable | 1 | 2 | 3 ; §\C{//turn on implicit separator}§3294 sout | sepEnable | 1 | 2 | 3 | endl; §\C{// globally turn on implicit separator}§ 3632 3295 \end{cfa} 3633 3296 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3636 3299 3637 3300 \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.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. 3639 3302 \begin{cfa}[belowskip=0pt] 3640 sout | 1 | sepOff | 2 | 3 ; §\C{// turn off implicit separator for the next item}§3303 sout | 1 | sepOff | 2 | 3 | endl; §\C{// locally turn off implicit separator}§ 3641 3304 \end{cfa} 3642 3305 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3644 3307 \end{cfa} 3645 3308 \begin{cfa}[belowskip=0pt] 3646 sout | sepDisable | 1 | sepOn | 2 | 3 ; §\C{// turn on implicit separator for the next item}§3309 sout | sepDisable | 1 | sepOn | 2 | 3 | endl; §\C{// locally turn on implicit separator}§ 3647 3310 \end{cfa} 3648 3311 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3651 3314 The tuple separator also responses to being turned on and off. 3652 3315 \begin{cfa}[belowskip=0pt] 3653 sout | t1 | sepOff | t2 ; §\C{// turn off implicit separator for the next item}§3316 sout | t1 | sepOff | t2 | endl; §\C{// locally turn on/off implicit separator}§ 3654 3317 \end{cfa} 3655 3318 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3659 3322 use ©sep© to accomplish this functionality. 3660 3323 \begin{cfa}[belowskip=0pt] 3661 sout | sepOn | 1 | 2 | 3 | sepOn ;§\C{// sepOn does nothing at start/end of line}§3324 sout | sepOn | 1 | 2 | 3 | sepOn | endl ; §\C{// sepOn does nothing at start/end of line}§ 3662 3325 \end{cfa} 3663 3326 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3665 3328 \end{cfa} 3666 3329 \begin{cfa}[belowskip=0pt] 3667 sout | sep | 1 | 2 | 3 | sep ;§\C{// use sep to print separator at start/end of line}§3330 sout | sep | 1 | 2 | 3 | sep | endl ; §\C{// use sep to print separator at start/end of line}§ 3668 3331 \end{cfa} 3669 3332 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3670 3333 ® ®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 \item3682 \Indexc{nl}\index{manipulator!nl@©nl©} scans characters until the next newline character, i.e., ignore the remaining characters in the line.3683 \item3684 \Indexc{nlOn}\index{manipulator!nlOn@©nlOn©} reads the newline character, when reading single characters.3685 \item3686 \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 33693 \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 \item3699 \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 23708 33709 43710 53711 3712 63713 \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 newline3715 \item3716 \Indexc{nlOn}\index{manipulator!nlOn@©nlOn©} implicitly prints a newline at the end of each output expression.3717 \item3718 \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 \item3727 \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 0b110113731 sout | bin( -27HH ) | bin( -27H ) | bin( -27 ) | bin( -27L );3732 0b11100101 0b1111111111100101 0b11111111111111111111111111100101 0b®(58 1s)®1001013733 \end{cfa}3734 3735 \item3736 \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 0333740 sout | oct( -27HH ) | oct( -27H ) | oct( -27 ) | oct( -27L );3741 0345 0177745 037777777745 017777777777777777777453742 \end{cfa}3743 Note, octal 0 is \emph{not} preceded by ©0© to prevent confusion.3744 3745 \item3746 \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 0x1b3750 sout | hex( -27HH ) | hex( -27H ) | hex( -27 ) | hex( -27L );3751 0xe5 0xffe5 0xffffffe5 0xffffffffffffffe53752 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+13755 sout | hex( -27.5F ) | hex( -27.5 ) | hex( -27.5L );3756 -0x1.b8p+4 -0x1.b8p+4 -0xd.cp+13757 \end{cfa}3758 3759 \item3760 \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+013765 \end{cfa}3766 3767 \item3768 \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®+43772 \end{cfa}3773 3774 \item3775 \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 1b3780 \end{cfa}3781 3782 \item3783 \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.53788 \end{cfa}3789 3790 \item3791 \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.53795 \end{cfa}3796 3797 \item3798 \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 343808 ® ®4.000000 ® ®4.000000 4.0000003809 ® ®ab ® ®ab ab3810 \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®343830 \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 34563837 \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®343845 \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 abc3859 \end{cfa}3860 3861 \item3862 \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 \item3887 \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 \item3896 \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.5003900 3334 \end{cfa} 3901 3335 \end{enumerate} … … 3907 3341 int main( void ) { 3908 3342 int x = 1, y = 2, z = 3; 3909 sout | x | y | z ;3343 sout | x | y | z | endl; 3910 3344 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ]; 3911 sout | t1 | t2 ; // print tuples3912 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 ;3345 sout | t1 | t2 | endl; // print tuples 3346 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; 3916 3350 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥" 3917 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10 ;3351 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10 | endl; 3918 3352 sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x" 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 ;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; 3922 3356 3923 3357 sepSet( sout, ", $" ); // set separator from " " to ", $" 3924 sout | 1 | 2 | 3 | " \"" | sep | "\"" ;3358 sout | 1 | 2 | 3 | " \"" | sep | "\"" | endl; 3925 3359 sepSet( sout, " " ); // reset separator to " " 3926 sout | 1 | 2 | 3 | " \"" | sepGet( sout ) | "\"" ;3360 sout | 1 | 2 | 3 | " \"" | sepGet( sout ) | "\"" | endl; 3927 3361 3928 3362 char store[sepSize]; 3929 3363 strcpy( store, sepGet( sout ) ); 3930 3364 sepSet( sout, "_" ); 3931 sout | 1 | 2 | 3 ;3365 sout | 1 | 2 | 3 | endl; 3932 3366 sepSet( sout, store ); 3933 sout | 1 | 2 | 3 ;3367 sout | 1 | 2 | 3 | endl; 3934 3368 3935 3369 sepSetTuple( sout, " " ); // set tuple separator from ", " to " " 3936 sout | t1 | t2 | " \"" | sepTuple | "\"" ;3370 sout | t1 | t2 | " \"" | sepTuple | "\"" | endl; 3937 3371 sepSetTuple( sout, ", " ); // reset tuple separator to ", " 3938 sout | t1 | t2 | " \"" | sepGetTuple( sout ) | "\"" ;3939 3940 sout | sepDisable | 1 | 2 | 3 ; // globally turn off implicit separator3941 sout | sepEnable | 1 | 2 | 3 ; // globally turn on implicit separator3942 3943 sout | 1 | sepOff | 2 | 3 ; // locally turn on implicit separator3944 sout | sepDisable | 1 | sepOn | 2 | 3 ; // globally turn off implicit separator3372 sout | t1 | t2 | " \"" | sepGetTuple( sout ) | "\"" | endl; 3373 3374 sout | sepDisable | 1 | 2 | 3 | endl; // globally turn off implicit separator 3375 sout | sepEnable | 1 | 2 | 3 | endl; // globally turn on implicit separator 3376 3377 sout | 1 | sepOff | 2 | 3 | endl; // locally turn on implicit separator 3378 sout | sepDisable | 1 | sepOn | 2 | 3 | endl; // globally turn off implicit separator 3945 3379 sout | sepEnable; 3946 sout | t1 | sepOff | t2 ; // locally turn on/off implicit separator3947 3948 sout | sepOn | 1 | 2 | 3 | sepOn ; // sepOn does nothing at start/end of line3949 sout | sep | 1 | 2 | 3 | sep ; // use sep to print separator at start/end of line3380 sout | t1 | sepOff | t2 | endl; // locally turn on/off implicit separator 3381 3382 sout | sepOn | 1 | 2 | 3 | sepOn | endl ; // sepOn does nothing at start/end of line 3383 sout | sep | 1 | 2 | 3 | sep | endl ; // use sep to print separator at start/end of line 3950 3384 } 3951 3385 … … 3956 3390 \end{comment} 3957 3391 %$ 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 a3965 \begin{itemize}3966 \item3967 ©1©-©9© prefix introduces a decimal value (©0©-©9©),3968 \item3969 ©0© prefix introduces an octal value (©0©-©7©), and3970 \item3971 ©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 \item3984 \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 sequence3993 \end{cfa}3994 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]3995 ®abc ®3996 ®abc ®3997 ®xx®3998 \end{cfa}3999 4000 \item4001 \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+24006 \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 \item4014 \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 unchanged4019 \end{cfa}4020 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]4021 ® -75.35e-4® 254022 \end{cfa}4023 4024 \item4025 \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®xyz4034 \end{cfa}4035 4036 \item4037 \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®bca4046 \end{cfa}4047 \end{enumerate}4048 3392 4049 3393 … … 4415 3759 \begin{itemize} 4416 3760 \item 4417 not determining or writinglong generic types,4418 \item 4419 ensur ingsecondary variables, related to a primary variable, always have the same type.3761 preventing having to determine or write long generic types, 3762 \item 3763 ensure secondary variables, related to a primary variable, always have the same type. 4420 3764 \end{itemize} 4421 3765 … … 4439 3783 There is also the conundrum in type inferencing of when to \emph{\Index{brand}} a type. 4440 3784 That is, when is the type of the variable more important than the type of its initialization expression. 4441 For example, if a change is made in an initialization expression, it can cause cascading type changes and/or errors.3785 For example, if a change is made in an initialization expression, it can cause significant cascading type changes and/or errors. 4442 3786 At some point, a variable type needs to remain constant and the expression to be in error when it changes. 4443 3787 … … 4672 4016 4673 4017 coroutine Fibonacci { 4674 int fn; §\C{// used for communication}§4018 int fn; §\C{// used for communication}§ 4675 4019 }; 4676 4020 void ?{}( Fibonacci * this ) { … … 4678 4022 } 4679 4023 void main( Fibonacci * this ) { 4680 int fn1, fn2; §\C{// retained between resumes}§4681 this->fn = 0; §\C{// case 0}§4024 int fn1, fn2; §\C{// retained between resumes}§ 4025 this->fn = 0; §\C{// case 0}§ 4682 4026 fn1 = this->fn; 4683 suspend(); §\C{// return to last resume}§4684 4685 this->fn = 1; §\C{// case 1}§4027 suspend(); §\C{// return to last resume}§ 4028 4029 this->fn = 1; §\C{// case 1}§ 4686 4030 fn2 = fn1; 4687 4031 fn1 = this->fn; 4688 suspend(); §\C{// return to last resume}§4689 4690 for ( ;; ) { §\C{// general case}§4032 suspend(); §\C{// return to last resume}§ 4033 4034 for ( ;; ) { §\C{// general case}§ 4691 4035 this->fn = fn1 + fn2; 4692 4036 fn2 = fn1; 4693 4037 fn1 = this->fn; 4694 suspend(); §\C{// return to last resume}§4038 suspend(); §\C{// return to last resume}§ 4695 4039 } // for 4696 4040 } 4697 4041 int next( Fibonacci * this ) { 4698 resume( this ); §\C{// transfer to last suspend}§4042 resume( this ); §\C{// transfer to last suspend}§ 4699 4043 return this->fn; 4700 4044 } … … 4702 4046 Fibonacci f1, f2; 4703 4047 for ( int i = 1; i <= 10; i += 1 ) { 4704 sout | next( &f1 ) | ' ' | next( &f2 ) ;4048 sout | next( &f1 ) | ' ' | next( &f2 ) | endl; 4705 4049 } // for 4706 4050 } … … 4768 4112 MyThread f[4]; 4769 4113 } 4770 sout | global.value ;4114 sout | global.value | endl; 4771 4115 } 4772 4116 \end{cfa} … … 4846 4190 void main( First * this ) { 4847 4191 for ( int i = 0; i < 10; i += 1 ) { 4848 sout | "First : Suspend No." | i + 1 ;4192 sout | "First : Suspend No." | i + 1 | endl; 4849 4193 yield(); 4850 4194 } … … 4855 4199 wait( this->lock ); 4856 4200 for ( int i = 0; i < 10; i += 1 ) { 4857 sout | "Second : Suspend No." | i + 1 ;4201 sout | "Second : Suspend No." | i + 1 | endl; 4858 4202 yield(); 4859 4203 } … … 4862 4206 int main( void ) { 4863 4207 signal_once lock; 4864 sout | "User main begin" ;4208 sout | "User main begin" | endl; 4865 4209 { 4866 4210 processor p; … … 4870 4214 } 4871 4215 } 4872 sout | "User main end" ;4216 sout | "User main end" | endl; 4873 4217 } 4874 4218 \end{cfa} … … 5567 4911 void ?{}( Line * l ) { 5568 4912 l->lnth = 0.0; 5569 sout | "default" ;4913 sout | "default" | endl; 5570 4914 } 5571 4915 … … 5574 4918 void ?{}( Line * l, float lnth ) { 5575 4919 l->lnth = lnth; 5576 sout | "lnth" | l->lnth ;4920 sout | "lnth" | l->lnth | endl; 5577 4921 5578 4922 } … … 5580 4924 // destructor 5581 4925 void ^?() { 5582 sout | "destroyed" ;4926 sout | "destroyed" | endl; 5583 4927 l.lnth = 0.0; 5584 4928 } … … 6241 5585 In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg ©int *?*?()©, where the string ©*?*?© can be interpreted as: 6242 5586 \begin{cfa} 6243 *?§\color{red}\textvisiblespace§*? §\C{// dereference operator, dereference operator}§6244 *§\color{red}\textvisiblespace§?*? §\C{// dereference, multiplication operator}§5587 *?§\color{red}\textvisiblespace§*? §\C{// dereference operator, dereference operator}§ 5588 *§\color{red}\textvisiblespace§?*? §\C{// dereference, multiplication operator}§ 6245 5589 \end{cfa} 6246 5590 By default, the first interpretation is selected, which does not yield a meaningful parse. … … 6294 5638 \eg: 6295 5639 \begin{cfa} 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 );}§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 );}§ 6300 5644 \end{cfa} 6301 5645 \CFA continues to support K\&R routine definitions: 6302 5646 \begin{cfa} 6303 f( a, b, c ) §\C{// default int return}§6304 int a, b; char c §\C{// K\&R parameter declarations}§5647 f( a, b, c ) §\C{// default int return}§ 5648 int a, b; char c §\C{// K\&R parameter declarations}§ 6305 5649 { 6306 5650 ... … … 6321 5665 int rtn( int i ); 6322 5666 int rtn( char c ); 6323 rtn( 'x' ); §\C{// programmer expects 2nd rtn to be called}§5667 rtn( 'x' ); §\C{// programmer expects 2nd rtn to be called}§ 6324 5668 \end{cfa} 6325 5669 \item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn© rather than the first. 6326 5670 In particular, output of ©char© variable now print a character rather than the decimal ASCII value of the character. 6327 5671 \begin{cfa} 6328 sout | 'x' | " " | (int)'x' ;5672 sout | 'x' | " " | (int)'x' | endl; 6329 5673 x 120 6330 5674 \end{cfa} … … 6343 5687 \item[Change:] make string literals ©const©: 6344 5688 \begin{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}§5689 char * p = "abc"; §\C{// valid in C, deprecated in \CFA}§ 5690 char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§ 6347 5691 \end{cfa} 6348 5692 The type of a string literal is changed from ©[] char© to ©const [] char©. … … 6351 5695 \begin{cfa} 6352 5696 char * p = "abc"; 6353 p[0] = 'w'; §\C{// segment fault or change constant literal}§5697 p[0] = 'w'; §\C{// segment fault or change constant literal}§ 6354 5698 \end{cfa} 6355 5699 The same problem occurs when passing a string literal to a routine that changes its argument. … … 6363 5707 \item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope: 6364 5708 \begin{cfa} 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}§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}§ 6368 5712 \end{cfa} 6369 5713 is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed. … … 6371 5715 \begin{cfa} 6372 5716 struct X { int i; struct X *next; }; 6373 static struct X a; §\C{// forward definition}§5717 static struct X a; §\C{// forward definition}§ 6374 5718 static struct X b = { 0, ®&a® };§\C{// forward reference, valid in C, invalid in \CFA}§ 6375 static struct X a = { 1, &b }; §\C{// definition}§5719 static struct X a = { 1, &b }; §\C{// definition}§ 6376 5720 \end{cfa} 6377 5721 \item[Rationale:] avoids having different initialization rules for builtin types and user-defined types. … … 6388 5732 struct Person { 6389 5733 enum ®Colour® { R, G, B }; §\C[7cm]{// nested type}§ 6390 struct Face { §\C{// nested type}§6391 ®Colour® Eyes, Hair; §\C{// type defined outside (1 level)}§5734 struct Face { §\C{// nested type}§ 5735 ®Colour® Eyes, Hair; §\C{// type defined outside (1 level)}§ 6392 5736 }; 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}§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}§ 6396 5740 }; 6397 ®Colour® c = R; §\C{// type/enum defined same level}§5741 ®Colour® c = R; §\C{// type/enum defined same level}§ 6398 5742 Person®.Colour® pc = Person®.®R;§\C{// type/enum defined inside}§ 6399 Person®.®Face pretty; §\C{// type defined inside}\CRT§5743 Person®.®Face pretty; §\C{// type defined inside}\CRT§ 6400 5744 \end{cfa} 6401 5745 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. … … 6414 5758 \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: 6415 5759 \begin{cfa} 6416 struct Y; §\C{// struct Y and struct X are at the same scope}§5760 struct Y; §\C{// struct Y and struct X are at the same scope}§ 6417 5761 struct X { 6418 5762 struct Y { /* ... */ } y; … … 6429 5773 \begin{cfa} 6430 5774 void foo() { 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 *}§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 *}§ 6433 5777 } 6434 5778 \end{cfa} … … 6603 5947 void * memalign( size_t align, size_t size );§\indexc{memalign}§ 6604 5948 int posix_memalign( void ** ptr, size_t align, size_t size );§\indexc{posix_memalign}§ 5949 } 5950 5951 // §\CFA§ safe equivalents, i.e., implicit size specification 5952 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, array 5960 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, array 5968 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 ); 6605 5972 6606 5973 // C unsafe initialization/copy 5974 extern "C" { 6607 5975 void * memset( void * dest, int c, size_t size ); 6608 5976 void * memcpy( void * dest, const void * src, size_t size ); 6609 5977 } 6610 5978 6611 forall( dtype T | sized(T) ) {6612 // §\CFA§ safe equivalents, i.e., implicit size specification6613 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, array6621 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, array6629 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 6634 5979 // §\CFA§ safe initialization/copy, i.e., implicit size specification 6635 T * memset( T * dest, char c );§\indexc{memset}§6636 T * memcpy( T * dest, const T * src );§\indexc{memcpy}§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}§ 6637 5982 6638 5983 // §\CFA§ safe initialization/copy array 6639 T * amemset( T dest[], char c, size_t dim ); 6640 T * amemcpy( T dest[], const T src[], size_t dim ); 6641 } 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 ); 6642 5986 6643 5987 // §\CFA§ allocation/deallocation and constructor/destructor … … 6655 5999 6656 6000 6657 \subsection{ String to ValueConversion}6001 \subsection{Conversion} 6658 6002 6659 6003 \leavevmode … … 6691 6035 \leavevmode 6692 6036 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6693 forall( otype T | { int ?<?( T, T ); } ) §\C{// location}§6037 forall( otype T | { int ?<?( T, T ); } ) §\C{// location}§ 6694 6038 T * bsearch( T key, const T * arr, size_t dim );§\indexc{bsearch}§ 6695 6039 6696 forall( otype T | { int ?<?( T, T ); } ) §\C{// position}§6040 forall( otype T | { int ?<?( T, T ); } ) §\C{// position}§ 6697 6041 unsigned int bsearch( T key, const T * arr, size_t dim ); 6698 6042 6699 6043 forall( otype T | { int ?<?( T, T ); } ) 6700 6044 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 }6723 6045 \end{cfa} 6724 6046 … … 6747 6069 \leavevmode 6748 6070 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 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}§ 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(); 6770 6082 \end{cfa} 6771 6083 … … 6810 6122 [ int, long double ] remquo( long double, long double ); 6811 6123 6812 float div( float, float, int * );§\indexc{div}§ §\C{// alternative name for remquo}§6124 float div( float, float, int * );§\indexc{div}§ §\C{// alternative name for remquo}§ 6813 6125 double div( double, double, int * ); 6814 6126 long double div( long double, long double, int * ); … … 6966 6278 long double atan2( long double, long double ); 6967 6279 6968 float atan( float, float ); §\C{// alternative name for atan2}§6280 float atan( float, float ); §\C{// alternative name for atan2}§ 6969 6281 double atan( double, double );§\indexc{atan}§ 6970 6282 long double atan( long double, long double ); … … 7146 6458 7147 6459 7148 \section{Time Keeping}7149 \label{s:Time Keeping}6460 \section{Time} 6461 \label{s:TimeLib} 7150 6462 7151 6463 7152 6464 %\subsection{\texorpdfstring{\protect\lstinline@Duration@}{Duration}} 7153 \subsection{\texorpdfstring{\Lst BasicStyle{Duration}}{Duration}}6465 \subsection{\texorpdfstring{\LstKeywordStyle{\textmd{Duration}}}{Duration}} 7154 6466 \label{s:Duration} 7155 6467 … … 7157 6469 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7158 6470 struct Duration { 7159 int64_t tv; §\C{// nanoseconds}§6471 int64_t tv; §\C{// nanoseconds}§ 7160 6472 }; 7161 6473 … … 7200 6512 7201 6513 Duration abs( Duration rhs ); 6514 6515 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur ); 7202 6516 7203 6517 Duration ?`ns( int64_t nsec ); … … 7223 6537 int64_t ?`d( Duration dur ); 7224 6538 int64_t ?`w( Duration dur ); 7225 7226 Duration max( Duration lhs, Duration rhs );7227 Duration min( Duration lhs, Duration rhs );7228 6539 \end{cfa} 7229 6540 7230 6541 7231 6542 %\subsection{\texorpdfstring{\protect\lstinline@\timeval@}{timeval}} 7232 \subsection{\texorpdfstring{\Lst BasicStyle{timeval}}{timeval}}6543 \subsection{\texorpdfstring{\LstKeywordStyle{\textmd{timeval}}}{timeval}} 7233 6544 \label{s:timeval} 7234 6545 … … 7249 6560 7250 6561 7251 %\subsection{\texorpdfstring{\protect\lstinline@timespec@}{timespec}} 7252 \subsection{\texorpdfstring{\LstBasicStyle{timespec}}{timespec}} 6562 \subsection{\texorpdfstring{\protect\lstinline@timespec@}{timespec}} 7253 6563 \label{s:timespec} 7254 6564 … … 7269 6579 7270 6580 7271 %\subsection{\texorpdfstring{\protect\lstinline@itimerval@}{itimerval}} 7272 \subsection{\texorpdfstring{\LstBasicStyle{itimerval}}{itimerval}} 6581 \subsection{\texorpdfstring{\protect\lstinline@itimerval@}{itimerval}} 7273 6582 \label{s:itimerval} 7274 6583 … … 7280 6589 7281 6590 7282 %\subsection{\texorpdfstring{\protect\lstinline@Time@}{Time}} 7283 \subsection{\texorpdfstring{\LstBasicStyle{Time}}{Time}} 6591 \subsection{\texorpdfstring{\protect\lstinline@Time@}{Time}} 7284 6592 \label{s:Time} 7285 6593 … … 7287 6595 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7288 6596 struct Time { 7289 uint64_t tv; §\C{// nanoseconds since UNIX epoch}§6597 uint64_t tv; §\C{// nanoseconds since UNIX epoch}§ 7290 6598 }; 7291 6599 7292 6600 void ?{}( Time & time ); 7293 6601 void ?{}( Time & time, zero_t ); 7294 6602 void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 ); 7295 6603 Time ?=?( Time & time, zero_t ); 7296 6604 … … 7301 6609 Time ?=?( Time & time, timespec t ); 7302 6610 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 ); 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 ); 7316 6626 7317 6627 char * yy_mm_dd( Time time, char * buf ); … … 7331 6641 7332 6642 size_t strftime( char * buf, size_t size, const char * fmt, Time time ); 7333 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time );7334 6643 \end{cfa} 7335 6644 … … 7352 6661 7353 6662 %\subsection{\texorpdfstring{\protect\lstinline@Clock@}{Clock}} 7354 \subsection{\texorpdfstring{\Lst BasicStyle{Clock}}{Clock}}6663 \subsection{\texorpdfstring{\LstKeywordStyle{\textmd{Clock}}}{Clock}} 7355 6664 \label{s:Clock} 7356 6665 … … 7358 6667 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7359 6668 struct Clock { 7360 Duration offset; §\C{// for virtual clock: contains offset from real-time}§7361 int clocktype; §\C{// implementation only -1 (virtual), CLOCK\_REALTIME}§6669 Duration offset; §\C{// for virtual clock: contains offset from real-time}§ 6670 int clocktype; §\C{// implementation only -1 (virtual), CLOCK\_REALTIME}§ 7362 6671 }; 7363 6672 … … 7366 6675 void ?{}( Clock & clk ); 7367 6676 void ?{}( Clock & clk, Duration adj ); 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}§ 6677 Duration getRes(); 6678 Time getTimeNsec(); §\C{// with nanoseconds}§ 6679 Time getTime(); §\C{// without nanoseconds}§ 7374 6680 Time getTime( Clock & clk ); 7375 6681 Time ?()( Clock & clk ); … … 7387 6693 7388 6694 \begin{cfa} 7389 void ?{}( Int * this ); §\C{// constructor/destructor}§6695 void ?{}( Int * this ); §\C{// constructor/destructor}§ 7390 6696 void ?{}( Int * this, Int init ); 7391 6697 void ?{}( Int * this, zero_t ); … … 7396 6702 void ^?{}( Int * this ); 7397 6703 7398 Int ?=?( Int * lhs, Int rhs ); §\C{// assignment}§6704 Int ?=?( Int * lhs, Int rhs ); §\C{// assignment}§ 7399 6705 Int ?=?( Int * lhs, long int rhs ); 7400 6706 Int ?=?( Int * lhs, unsigned long int rhs ); … … 7413 6719 unsigned long int narrow( Int val ); 7414 6720 7415 int ?==?( Int oper1, Int oper2 ); §\C{// comparison}§6721 int ?==?( Int oper1, Int oper2 ); §\C{// comparison}§ 7416 6722 int ?==?( Int oper1, long int oper2 ); 7417 6723 int ?==?( long int oper2, Int oper1 ); … … 7449 6755 int ?>=?( unsigned long int oper1, Int oper2 ); 7450 6756 7451 Int +?( Int oper ); §\C{// arithmetic}§6757 Int +?( Int oper ); §\C{// arithmetic}§ 7452 6758 Int -?( Int oper ); 7453 6759 Int ~?( Int oper ); … … 7531 6837 Int ?>>=?( Int * lhs, mp_bitcnt_t shift ); 7532 6838 7533 Int abs( Int oper ); §\C{// number functions}§6839 Int abs( Int oper ); §\C{// number functions}§ 7534 6840 Int fact( unsigned long int N ); 7535 6841 Int gcd( Int oper1, Int oper2 ); … … 7556 6862 #include <gmp>§\indexc{gmp}§ 7557 6863 int main( void ) { 7558 sout | "Factorial Numbers" ;6864 sout | "Factorial Numbers" | endl; 7559 6865 Int fact = 1; 7560 6866 7561 sout | 0 | fact ;6867 sout | 0 | fact | endl; 7562 6868 for ( unsigned int i = 1; i <= 40; i += 1 ) { 7563 6869 fact *= i; 7564 sout | i | fact ;6870 sout | i | fact | endl; 7565 6871 } 7566 6872 } … … 7642 6948 // implementation 7643 6949 struct Rational {§\indexc{Rational}§ 7644 long int numerator, denominator; §\C{// invariant: denominator > 0}§6950 long int numerator, denominator; §\C{// invariant: denominator > 0}§ 7645 6951 }; // Rational 7646 6952 7647 Rational rational(); §\C{// constructors}§6953 Rational rational(); §\C{// constructors}§ 7648 6954 Rational rational( long int n ); 7649 6955 Rational rational( long int n, long int d ); … … 7651 6957 void ?{}( Rational * r, one_t ); 7652 6958 7653 long int numerator( Rational r ); §\C{// numerator/denominator getter/setter}§6959 long int numerator( Rational r ); §\C{// numerator/denominator getter/setter}§ 7654 6960 long int numerator( Rational r, long int n ); 7655 6961 long int denominator( Rational r ); 7656 6962 long int denominator( Rational r, long int d ); 7657 6963 7658 int ?==?( Rational l, Rational r ); §\C{// comparison}§6964 int ?==?( Rational l, Rational r ); §\C{// comparison}§ 7659 6965 int ?!=?( Rational l, Rational r ); 7660 6966 int ?<?( Rational l, Rational r ); … … 7663 6969 int ?>=?( Rational l, Rational r ); 7664 6970 7665 Rational -?( Rational r ); §\C{// arithmetic}§6971 Rational -?( Rational r ); §\C{// arithmetic}§ 7666 6972 Rational ?+?( Rational l, Rational r ); 7667 6973 Rational ?-?( Rational l, Rational r ); … … 7669 6975 Rational ?/?( Rational l, Rational r ); 7670 6976 7671 double widen( Rational r ); §\C{// conversion}§6977 double widen( Rational r ); §\C{// conversion}§ 7672 6978 Rational narrow( double f, long int md ); 7673 6979
Note:
See TracChangeset
for help on using the changeset viewer.