Changeset 53ba273


Ignore:
Timestamp:
Apr 6, 2016, 10:08:32 PM (6 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, ctor, deferred_resn, demangler, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, string, with_gc
Children:
3d9b5da, 78885b5
Parents:
3cfe27f
Message:

switch from std=c99 to std=gnu99, update latex macros, refrat and extend user manual, update limits/iostream/fstream, add rational numbers

Files:
6 added
1 deleted
12 edited
1 moved

Legend:

Unmodified
Added
Removed
  • doc/refrat/refrat.tex

    r3cfe27f r53ba273  
     1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%
     2%%
     3%% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
     4%%
     5%% The contents of this file are covered under the licence agreement in the
     6%% file "LICENCE" distributed with Cforall.
     7%%
     8%% refrat.tex --
     9%%
     10%% Author           : Peter A. Buhr
     11%% Created On       : Wed Apr  6 14:52:25 2016
     12%% Last Modified By : Peter A. Buhr
     13%% Last Modified On : Wed Apr  6 21:57:27 2016
     14%% Update Count     : 2
     15%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     16
    117% requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended
    218
     
    521
    622% Latex packages used in the document.
    7 
    823\usepackage{fullpage,times}
    924\usepackage{xspace}
     
    2338% Names used in the document.
    2439
    25 \newcommand{\CFA}{C$\forall$\xspace}    % set language symbolic name
     40\newcommand{\CFA}{C$\mathbf\forall$\xspace}     % set language symbolic name
    2641\newcommand{\CFL}{Cforall\xspace}               % set language text name
    2742\newcommand{\CC}{C\kern-.1em\hbox{+\kern-.25em+}\xspace} % CC symbolic name
     
    151166% CFA based on ANSI C
    152167\lstdefinelanguage{CFA}[ANSI]{C}%
    153 {morekeywords={asm,_Alignas,_Alignof,_At,_Atomic,_Bool,catch,catchResume,choose,_Complex,trait,disable,dtype,enable,
    154         fallthru,finally,forall,ftype,_Generic,_Imaginary,inline,lvalue,_Noreturn,otype,restrict,_Static_assert,
    155         _Thread_local,throw,throwResume,try,},
     168{morekeywords={_Alignas,_Alignof,__alignof,__alignof__,asm,__asm,__asm__,_At,_Atomic,__attribute,__attribute__,auto,
     169    _Bool,catch,catchResume,choose,_Complex,__complex,__complex__,__const,__const__,disable,dtype,enable,__extension__,
     170        fallthru,finally,forall,ftype,_Generic,_Imaginary,inline,__label__,lvalue,_Noreturn,otype,restrict,_Static_assert,
     171        _Thread_local,throw,throwResume,trait,try,typeof,__typeof,__typeof__,},
    156172}%
    157173
     
    163179xleftmargin=\parindent,
    164180escapechar=@,
     181mathescape=true,
    165182keepspaces=true,
    166183showstringspaces=false,
    167184showlines=true,
     185aboveskip=6pt,
     186belowskip=4pt,
    168187}%
    169188
     
    171190% replace/adjust listings characters that look bad in sanserif
    172191\lst@CCPutMacro
    173 \lst@ProcessOther{"2D}{\lst@ttfamily{-{}}{{\ttfamily\upshape -}}} % replace minus
     192\lst@ProcessOther{"22}{\lst@ttfamily{"}{\raisebox{0.3ex}{\ttfamily\upshape "}}} % replace double quote
     193\lst@ProcessOther{"27}{\lst@ttfamily{'}{\raisebox{0.3ex}{\ttfamily\upshape '\hspace*{-2pt}}}} % replace single quote
     194\lst@ProcessOther{"2D}{\lst@ttfamily{-}{\ttfamily\upshape -}} % replace minus
    174195\lst@ProcessOther{"3C}{\lst@ttfamily{<}{\texttt{<}}} % replace less than
    175196\lst@ProcessOther{"3E}{\lst@ttfamily{<}{\texttt{>}}} % replace greater than
    176197\lst@ProcessOther{"5E}{\raisebox{0.4ex}{$\scriptstyle\land\,$}} % replace circumflex
    177 \lst@ProcessLetter{"5F}{\lst@ttfamily{\char95}{{\makebox[1.2ex][c]{\rule{1ex}{0.1ex}}}}} % replace underscore
     198\lst@ProcessOther{"5F}{\lst@ttfamily{\char95}{{\makebox[1.2ex][c]{\rule{1ex}{0.1ex}}}}} % replace underscore
     199\lst@ProcessOther{"60}{\lst@ttfamily{`}{\raisebox{0.3ex}{\ttfamily\upshape \hspace*{-2pt}`}}} % replace backquote
    178200\lst@ProcessOther{"7E}{\raisebox{0.3ex}{$\scriptstyle\sim\,$}} % replace tilde
    179201%\lst@ProcessOther{"7E}{\raisebox{-.4ex}[1ex][0pt]{\textasciitilde}} % lower tilde
     
    38433865``\lstinline$Safe_pointer$ acts like a pointer to \lstinline$int$''.
    38443866\begin{lstlisting}
    3845 trait ptr_to( type P | pointer( P ), otype T ) {@\impl{ptr_to}@@\use{pointer}@
     3867trait ptr_to( otype P | pointer( P ), otype T ) {@\impl{ptr_to}@@\use{pointer}@
    38463868        lvalue T *?( P );
    38473869        lvalue T ?[?]( P, long int );
    38483870};
    3849 trait ptr_to_const( type P | pointer( P ), otype T ) {@\impl{ptr_to_const}@
     3871trait ptr_to_const( otype P | pointer( P ), otype T ) {@\impl{ptr_to_const}@
    38503872        const lvalue T *?( P );
    38513873        const lvalue T ?[?]( P, long int );@\use{pointer}@
    38523874};
    3853 trait ptr_to_volatile( type P | pointer( P ), otype T ) }@\impl{ptr_to_volatile}@
     3875trait ptr_to_volatile( otype P | pointer( P ), otype T ) }@\impl{ptr_to_volatile}@
    38543876        volatile lvalue T *?( P );
    38553877        volatile lvalue T ?[?]( P, long int );@\use{pointer}@
    38563878};
    3857 trait ptr_to_const_volatile( type P | pointer( P ), otype T ) }@\impl{ptr_to_const_volatile}@
     3879trait ptr_to_const_volatile( otype P | pointer( P ), otype T ) }@\impl{ptr_to_const_volatile}@
    38583880        const volatile lvalue T *?( P );@\use{pointer}@
    38593881        const volatile lvalue T ?[?]( P, long int );
     
    38653887``\lstinline$ptr_to$'' specifications.
    38663888\begin{lstlisting}
    3867 trait m_l_ptr_to( type P | m_l_pointer( P ),@\use{m_l_pointer}@@\impl{m_l_ptr_to}@ otype T | ptr_to( P, T )@\use{ptr_to}@ {
     3889trait m_l_ptr_to( otype P | m_l_pointer( P ),@\use{m_l_pointer}@@\impl{m_l_ptr_to}@ otype T | ptr_to( P, T )@\use{ptr_to}@ {
    38683890        P ?=?( P *, T * );
    38693891        T * ?=?( T **, P );
    38703892};
    3871 trait m_l_ptr_to_const( type P | m_l_pointer( P ),@\use{m_l_pointer}@@\impl{m_l_ptr_to_const}@ otype T | ptr_to_const( P, T )@\use{ptr_to_const}@) {
     3893trait m_l_ptr_to_const( otype P | m_l_pointer( P ),@\use{m_l_pointer}@@\impl{m_l_ptr_to_const}@ otype T | ptr_to_const( P, T )@\use{ptr_to_const}@) {
    38723894        P ?=?( P *, const T * );
    38733895        const T * ?=?( const T **, P );
    38743896};
    3875 trait m_l_ptr_to_volatile( type P | m_l_pointer( P ),@\use{m_l_pointer}@@\impl{m_l_ptr_to_volatile}@ otype T | ptr_to_volatile( P, T )) {@\use{ptr_to_volatile}@
     3897trait m_l_ptr_to_volatile( otype P | m_l_pointer( P ),@\use{m_l_pointer}@@\impl{m_l_ptr_to_volatile}@ otype T | ptr_to_volatile( P, T )) {@\use{ptr_to_volatile}@
    38763898        P ?=?( P *, volatile T * );
    38773899        volatile T * ?=?( volatile T **, P );
    38783900};
    3879 trait m_l_ptr_to_const_volatile( type P | ptr_to_const_volatile( P ),@\use{ptr_to_const_volatile}@@\impl{m_l_ptr_to_const_volatile}@
    3880                 type T | m_l_ptr_to_volatile( P, T ) | m_l_ptr_to_const( P )) {@\use{m_l_ptr_to_const}@@\use{m_l_ptr_to_volatile}@
     3901trait m_l_ptr_to_const_volatile( otype P | ptr_to_const_volatile( P ),@\use{ptr_to_const_volatile}@@\impl{m_l_ptr_to_const_volatile}@
     3902                otype T | m_l_ptr_to_volatile( P, T ) | m_l_ptr_to_const( P )) {@\use{m_l_ptr_to_const}@@\use{m_l_ptr_to_volatile}@
    38813903        P ?=?( P *, const volatile T * );
    38823904        const volatile T * ?=?( const volatile T **, P );
  • doc/user/user.tex

    r3cfe27f r53ba273  
     1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%
     2%%
     3%% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
     4%%
     5%% The contents of this file are covered under the licence agreement in the
     6%% file "LICENCE" distributed with Cforall.
     7%%
     8%% user.tex --
     9%%
     10%% Author           : Peter A. Buhr
     11%% Created On       : Wed Apr  6 14:53:29 2016
     12%% Last Modified By : Peter A. Buhr
     13%% Last Modified On : Wed Apr  6 17:37:39 2016
     14%% Update Count     : 34
     15%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     16
    117% requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended
    218
     
    521
    622% Latex packages used in the document.
    7 
     23\usepackage[T1]{fontenc}
     24\usepackage{textcomp}
     25\usepackage[latin1]{inputenc}
     26\usepackage{upquote}
    827\usepackage{fullpage,times}
    928\usepackage{xspace}
     
    2443% Names used in the document.
    2544
    26 \newcommand{\CFA}{C$\forall$\xspace}    % set language symbolic name
     45\newcommand{\CFA}{C$\mathbf\forall$\xspace}     % set language symbolic name
    2746\newcommand{\CFL}{Cforall\xspace}               % set language text name
    2847\newcommand{\CC}{C\kern-.1em\hbox{+\kern-.25em+}\xspace} % CC symbolic name
     
    160179% CFA based on ANSI C
    161180\lstdefinelanguage{CFA}[ANSI]{C}%
    162 {morekeywords={asm,_Alignas,_Alignof,_At,_Atomic,_Bool,catch,catchResume,choose,_Complex,trait,disable,dtype,enable,
    163         fallthru,finally,forall,ftype,_Generic,_Imaginary,inline,lvalue,_Noreturn,otype,restrict,_Static_assert,
    164         _Thread_local,throw,throwResume,try,},
     181{morekeywords={_Alignas,_Alignof,__alignof,__alignof__,asm,__asm,__asm__,_At,_Atomic,__attribute,__attribute__,auto,
     182    _Bool,catch,catchResume,choose,_Complex,__complex,__complex__,__const,__const__,disable,dtype,enable,__extension__,
     183        fallthru,finally,forall,ftype,_Generic,_Imaginary,inline,__label__,lvalue,_Noreturn,otype,restrict,_Static_assert,
     184        _Thread_local,throw,throwResume,trait,try,typeof,__typeof,__typeof__,},
     185  moredelim=**[is][\color{red}]{`}{`},
    165186}%
    166187
     
    176197showstringspaces=false,
    177198showlines=true,
     199aboveskip=6pt,
     200belowskip=4pt,
     201literate={\\`}{\raisebox{0.3ex}{\ttfamily\upshape \hspace*{-2pt}`}}1, % escape \`, otherwise used to highlight in red
     202%extendedchars=true,
    178203}%
    179204
     
    181206% replace/adjust listings characters that look bad in sanserif
    182207\lst@CCPutMacro
    183 \lst@ProcessOther{"2D}{\lst@ttfamily{-{}}{{\ttfamily\upshape -}}} % replace minus
     208\lst@ProcessOther{"22}{\lst@ttfamily{"}{\raisebox{0.3ex}{\ttfamily\upshape "}}} % replace double quote
     209\lst@ProcessOther{"27}{\lst@ttfamily{'}{\raisebox{0.3ex}{\ttfamily\upshape '\hspace*{-2pt}}}} % replace single quote
     210\lst@ProcessOther{"2D}{\lst@ttfamily{-}{\ttfamily\upshape -}} % replace minus
    184211\lst@ProcessOther{"3C}{\lst@ttfamily{<}{\texttt{<}}} % replace less than
    185212\lst@ProcessOther{"3E}{\lst@ttfamily{<}{\texttt{>}}} % replace greater than
    186213\lst@ProcessOther{"5E}{\raisebox{0.4ex}{$\scriptstyle\land\,$}} % replace circumflex
    187 \lst@ProcessLetter{"5F}{\lst@ttfamily{\char95}{{\makebox[1.2ex][c]{\rule{1ex}{0.1ex}}}}} % replace underscore
     214\lst@ProcessOther{"5F}{\lst@ttfamily{\char95}{{\makebox[1.2ex][c]{\rule{1ex}{0.1ex}}}}} % replace underscore
     215\lst@ProcessOther{"60}{\lst@ttfamily{`}{\raisebox{0.3ex}{\ttfamily\upshape \hspace*{-2pt}`}}} % replace backquote
    188216\lst@ProcessOther{"7E}{\raisebox{0.3ex}{$\scriptstyle\sim\,$}} % replace tilde
    189217%\lst@ProcessOther{"7E}{\raisebox{-.4ex}[1ex][0pt]{\textasciitilde}} % lower tilde
     
    366394
    367395The command \lstinline@cfa@ is used to compile \CFA program(s).
    368 This command works like the GNU \lstinline@gcc@ command, e.g.:
     396This command works like the GNU \lstinline@gcc@\index{gcc} command, e.g.:
    369397\begin{lstlisting}
    370398cfa [ gcc-options ] C/@{\CFA}@-files [ assembler/loader-files ]
    371399\end{lstlisting}
    372 The following additional option is available:
     400By default, \CFA programs having the following \lstinline@gcc@ flags turned on:
     401\begin{description}
     402\item
     403\hspace*{-4pt}\lstinline@-std=gnu99@
     404The 1999 C standard plus GNU extensions.
     405\end{description}
     406The following new \CFA option is available:
    373407\begin{description}
    374408\item
     
    382416Numeric constants are extended to allow \Index{underscore}s within constants\index{constant!underscore}, e.g.:
    383417\begin{lstlisting}
    384 2_147_483_648;                          // decimal constant
     4182`_`147`_`483`_`648;                            // decimal constant
    38541956_ul;                                          // decimal unsigned long constant
    3864200_377;                                          // octal constant
     
    451485\multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}}       & \multicolumn{1}{c}{\textbf{C}}        \\
    452486\begin{lstlisting}
    453 * int x, y;
     487`* int x, y;`
    454488\end{lstlisting}
    455489&
     
    571605The point of the new syntax is to allow returning multiple values from a routine~\cite{CLU,Galletly96}, e.g.:
    572606\begin{lstlisting}
    573 [ int o1, int o2, char o3 ] f( int i1, char i2, char i3 ) {
     607`[ int o1, int o2, char o3 ]` f( int i1, char i2, char i3 ) {
    574608        @\emph{routine body}@
    575609}
     
    639673Because the value in the return variable is automatically returned when a \CFA routine terminates, the \lstinline@return@ statement \emph{does not} contain an expression, as in:
    640674\begin{lstlisting}
    641 [ int x ] f() {
     675`[ int x ]` f() {
    642676        ... x = 0; ... x = y; ...
    643         return; // implicitly return x
     677        `return;` // implicitly return x
    644678}
    645679\end{lstlisting}
     
    697731for example, the following is incorrect:
    698732\begin{lstlisting}
    699 * [ int x ] f () fp;            // routine name ``f'' is not allowed
     733* [ int x ] f () fp;            // routine name "f" is not allowed
    700734\end{lstlisting}
    701735
     
    864898\subsection{Type Nesting}
    865899
    866 C allows \Index{type nesting}, but the nested types are hoisted\index{type!hoisting} (refactored) into the enclosing scope.
     900\CFA allows \Index{type nesting}, and type qualification of the nested types, where as C hoists\index{type!hoisting} (refactors) nested types into the enclosing scope and has no type qualification.
    867901\begin{quote2}
    868902\begin{tabular}{@{}l@{\hspace{30pt}}l|l@{}}
     
    919953
    920954int fred() {
    921         s.t.c = S.R;
    922         struct S.T t = { S.R, 1, 2 };
    923         enum S.C c;
    924         union S.T.U u;
     955        s.t.c = `S.`R;  // type qualification
     956        struct `S.`T t = { `S.`R, 1, 2 };
     957        enum `S.`C c;
     958        union `S.T.`U u;
    925959}
    926960\end{lstlisting}
    927961\end{tabular}
    928962\end{quote2}
    929 
    930 \CFA is C \emph{incompatible} on this issue, and provides semantics similar to \CC.
    931 Nested types are not hoisted and can be referenced using the field selection operator ``\lstinline@.@'', unlike the \CC scope-resolution operator ``\lstinline@::@''.
    932 Given that nested types in C are equivalent to not using them, i.e., they are essentially useless, it is unlikely there are any realistic usages that break because of this incompatibility.
     963In the left example in C, types \lstinline@C@, \lstinline@U@ and \lstinline@T@ are implicitly hoisted outside of type \lstinline@S@ into the containing block scope.
     964In the right example in \CFA, the types are not hoisted and accessed using the field-selection operator ``\lstinline@.@'' for type qualification, as does Java, rather than the \CC type-selection operator ``\lstinline@::@''.
    933965
    934966
     
    944976\begin{lstlisting}
    945977const unsigned int size = 10;
    946 int a[size];
    947 
    948 qsort( a, size );               // ascending order using built in ?<?
    949 {                                               // descending order by local redefinition
    950         int ?<?( int a, int b ) { return a > b; } // nested routine
    951         qsort( a, size );
    952 }
    953 \end{lstlisting}
     978int ia[size];
     979...                                             // assign values to array ia
     980qsort( ia, size );              // sort ascending order using builtin ?<?
     981{
     982        `int ?<?( int x, int y ) { return x > y; }` // nested routine
     983        qsort( ia, size );      // sort descending order by local redefinition
     984}
     985\end{lstlisting}
     986
     987Nested routines are not first-class, meaning a nested routine cannot be returned if it has references to variables in its enclosing blocks;
     988the only exception is references to the external block of the translation unit, as these variables persist for the duration of the program.
     989The following program in undefined in \CFA (and \lstinline@gcc@\index{gcc})
     990\begin{lstlisting}
     991[* [int]( int )] foo() {                // int (*foo())( int )
     992        int `i` = 7;
     993        int bar( int p ) {
     994                `i` += 1;                                       // dependent on local variable
     995                sout | `i` | endl;
     996        }
     997        return bar;                                     // undefined because of local dependence
     998}
     999int main() {
     1000        * [int](int) fp = foo();        // int (*fp)(int)
     1001    sout | fp( 3 ) | endl;
     1002}
     1003\end{lstlisting}
     1004because
     1005
     1006Currently, there are no \Index{lambda} expressions, i.e., unnamed routines because routine names are very important to properly select the correct routine.
    9541007
    9551008
     
    10131066
    10141067\item
    1015 Change: A struct is a scope in C++, not in C
    1016 Rationale: Class scope is crucial to C++, and a struct is a class.
    1017 Effect on original feature: Change to semantics of well-defined feature.
    1018 Difficulty of converting: Semantic transformation.
    1019 How widely used: C programs use struct extremely frequently, but the change is only noticeable when
    1020 struct, enumeration, or enumerator names are referred to outside the struct. The latter is probably
    1021 rare.
     1068Change: A struct is a scope in C++, not in C \\
     1069Rationale: Class scope is crucial to C++, and a struct is a class. \\
     1070Effect on original feature: Change to semantics of well-defined feature. \\
     1071Difficulty of converting: Semantic transformation. \\
     1072How widely used: C programs use struct extremely frequently, but the change is only noticeable when struct, enumeration, or enumerator names are referred to outside the struct.
     1073The latter is probably rare.
     1074
     1075\CFA is C \emph{incompatible} on this issue, and provides semantics similar to \CC.
     1076Nested types are not hoisted and can be referenced using the field selection operator ``\lstinline@.@'', unlike the \CC scope-resolution operator ``\lstinline@::@''.
     1077Given that nested types in C are equivalent to not using them, i.e., they are essentially useless, it is unlikely there are any realistic usages that break because of this incompatibility.
     1078
    10221079
    10231080\item
     
    11851242First the right-hand tuple is flattened and then the values are assigned individually.
    11861243Flattening is also performed on tuple types.
    1187 For example, the type \lstinline@[ int, [ int, int ], int ]@ can be coerced, using flattening, into the type lstinline@[ int, int, int, int ]@.
     1244For example, the type \lstinline@[ int, [ int, int ], int ]@ can be coerced, using flattening, into the type \lstinline@[ int, int, int, int ]@.
    11881245
    11891246A \newterm{structuring coercion} is the opposite of flattening;
     
    13521409\multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}}       & \multicolumn{1}{c}{\textbf{C}}        \\
    13531410\begin{lstlisting}
    1354 L1: for ( ... ) {
    1355         L2: for ( ... ) {
    1356                 L3: for ( ... ) {
    1357                         ... break L1; ...
    1358                         ... break L2; ...
    1359                         ... break L3; // or break
     1411`L1:` for ( ... ) {
     1412        `L2:` for ( ... ) {
     1413                `L3:` for ( ... ) {
     1414                        ... break `L1`; ...
     1415                        ... break `L2`; ...
     1416                        ... break `L3`; // or break
    13601417                }
    13611418        }
     
    13821439\multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}}       & \multicolumn{1}{c}{\textbf{C}}        \\
    13831440\begin{lstlisting}
    1384 L1: for ( ... ) {
    1385         L2: for ( ... ) {
    1386                 L3: for ( ... ) {
    1387                         ... continue L1; ...
    1388                         ... continue L2; ...
    1389                         ... continue L3; ...
     1441`L1`: for ( ... ) {
     1442        `L2`: for ( ... ) {
     1443                `L3`: for ( ... ) {
     1444                        ... continue `L1`; ...
     1445                        ... continue `L2`; ...
     1446                        ... continue `L3`; ...
    13901447
    13911448                }
     
    16231680\begin{lstlisting}
    16241681switch ( i ) {
    1625   case 1, 3, 5:
     1682  `case 1, 3, 5`:
    16261683        ...
    1627   case 2, 4, 6:
     1684  `case 2, 4, 6`:
    16281685        ...
    16291686}
     
    16341691  case 1: case 3 : case 5:
    16351692        ...
    1636   case 2: case 4 : case 6: /* even values */
     1693  case 2: case 4 : case 6:
    16371694        ...
    16381695}
     
    16551712\begin{lstlisting}
    16561713switch ( i ) {
    1657   case 1~5
     1714  `case 1~5:`
    16581715        ...
    1659   case 10~15
     1716  `case 10~15:`
    16601717        ...
    16611718}
     
    16721729&
    16731730\begin{lstlisting}
     1731
    16741732// 1, 2, 3, 4, 5
    16751733
     
    21682226
    21692227
    2170 \section{Generics }
     2228\section{Auto Type-Inferencing}
     2229
     2230Auto type-inferencing occurs in a declaration where a variable's type is inferred from its initialization expression type.
     2231\begin{quote2}
     2232\begin{tabular}{@{}l@{\hspace{30pt}}ll@{}}
     2233\multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CC}}        & \multicolumn{1}{c}{\lstinline@gcc@}\index{gcc} \\
     2234\begin{lstlisting}
     2235
     2236auto j = 3.0 * 4;
     2237int i;
     2238auto k = i;
     2239\end{lstlisting}
     2240&
     2241\begin{lstlisting}
     2242#define expr 3.0 * i
     2243typeof(expr) j = expr;
     2244int i;
     2245typeof(i) k = i;
     2246\end{lstlisting}
     2247&
     2248\begin{lstlisting}
     2249
     2250// use type of initialization expression
     2251
     2252// use type of primary variable
     2253\end{lstlisting}
     2254\end{tabular}
     2255\end{quote2}
     2256The two important capabilities are:
     2257\begin{itemize}
     2258\item
     2259preventing having to determine or write out long generic types,
     2260\item
     2261ensure secondary variables, related to a primary variable, always have the same type.
     2262\end{itemize}
     2263
     2264In \CFA, \lstinline@typedef@ provides a mechanism to alias long type names with short ones, both globally and locally, but not eliminate the use of the short name.
     2265\lstinline@gcc@ provides \lstinline@typeof@ to declare a secondary variable from a primary variable.
     2266\CFA also relies heavily on the specification of the left-hand side of assignment for type inferencing, so in many cases it is crucial to specify the type of the left-hand side to select the correct type of the right-hand expression.
     2267Only for overloaded routines with the same return type is variable type-inferencing possible.
     2268Finally, \lstinline@auto@ presents the programming problem of tracking down a type when the type is actually needed.
     2269For example, given
     2270\begin{lstlisting}
     2271auto j = `...`
     2272\end{lstlisting}
     2273and the need to write a routine to compute using \lstinline@j@
     2274\begin{lstlisting}
     2275void rtn( `...` parm );
     2276rtn( j );
     2277\end{lstlisting}
     2278A programmer must work backwards to determine the type of \lstinline@j@'s initialization expression, reconstructing the possibly long generic type-name.
     2279In this situation, having the type name or a short alias is very useful.
     2280
     2281There is also the conundrum in type inferencing of when to \emph{\Index{brand}} a type.
     2282That is, when is the type of the variable more important than the type of its initialization expression.
     2283For example, if a change is made in an initialization expression, it can cause hundreds or thousands of cascading type changes and/or errors.
     2284At some point, a programmer wants the type of the variable to remain constant and the expression to be in error when it changes.
     2285
     2286Given \lstinline@typedef@ and \lstinline@typeof@ in \CFA, and the strong need to use the type of left-hand side in inferencing, auto type-inferencing is not supported at this time.
     2287Should a significant need arise, this feature can be revisited.
     2288
     2289
     2290\section{Generics}
    21712291
    21722292\CFA supports parametric polymorphism to allow users to define generic functions and types.
     
    24572577
    24582578
    2459 \section{I/O Library}
    2460 \label{s:IOLibrary}
    2461 
    2462 The goal for \CFA I/O is to make I/O as simple as possible for the general case, while fully supporting polmorphism and user defined types in a consistent way.
    2463 The general case is printing out a sequence of variables separated by whitespace.
    2464 \begin{lstlisting}
    2465 int x = 0, y = 1, z = 2;
    2466 sout | x | y | z | endl;
    2467 
    2468 cout << x << " " << y << " " << z << endl;
    2469 \end{lstlisting}
    2470 The \CC form takes almost twice as many characters.
    2471 
    2472 The logical-or operator is used because it is the lowest priority overloadable operator, other than assignment.
    2473 Therefore, most output expressions do not require parenthesis.
    2474 \begin{lstlisting}
    2475 int x = 0, y = 1, z = 2;
    2476 sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl;
    2477 
    2478 cout << x * 3 << y + 1 << (z << 2) << (x == y) << (x | y) << (x || y) << (x > z ? 1 : 2) << endl;
    2479 \end{lstlisting}
    2480 
    2481 Finally, the logical-or operator has a link with the Shell pipe-operator for moving data, although data flows in the opposite direction.
    2482 
    2483 \begin{figure}
    2484 \begin{lstlisting}[mathescape=off]
    2485 #include <fstream>
    2486 
    2487 int main() {
    2488         char c;
    2489         short int si;
    2490         unsigned short int usi;
    2491         int i;
    2492         unsigned int ui;
    2493         long int li;
    2494         unsigned long int uli;
    2495         long long int lli;
    2496         unsigned long long int ulli;
    2497         float f;
    2498         double d;
    2499         long double ld;
    2500         float _Complex fc;
    2501         double _Complex dc;
    2502         long double _Complex ldc;
    2503         char s1[10], s2[10];
    2504 
    2505         ifstream in;
    2506         open( &in, "read.data", "r" );
    2507 
    2508         &in | &c
    2509                 | &si | &usi | &i | &ui | &li | &uli | &lli | &ulli
    2510                 | &f | &d | &ld
    2511                 | &fc | &dc | &ldc
    2512                 | str( s1 ) | str( s2, 10 );
    2513 
    2514         sout | c | ' ' | endl
    2515                  | si | usi | i | ui | li | uli | lli | ulli | endl
    2516                  | f | d | ld | endl
    2517                  | f | "" | d | "" | ld | endl;
    2518 
    2519         sepSet( sout, ", $" );
    2520         sout | fc | dc | ldc | endl
    2521                  | sepOn | s1 | sepOff | s2 | endl
    2522                  | s1 | "" | s2 | endl;
    2523 }
    2524 
    2525 $ cat read.data
    2526 A 1 2 3 4 5 6 7 8 1.1 1.2 1.3 1.1+2.3 1.1-2.3 1.1-2.3 abc xyz
    2527 $ a.out
    2528 A
    2529 1 2 3 4 5 6 7 8
    2530 1.1 1.2 1.3
    2531 1.11.21.3
    2532 1.1+2.3i, $1.1-2.3i, $1.1-2.3i
    2533 , $abcxyz
    2534 abcxyz
    2535 \end{lstlisting}
    2536 \end{figure}
    2537 
    2538 
    2539 \section{Standard Library}
    2540 \label{s:StandardLibrary}
    2541 
    2542 The goal of the \CFA standard-library is to wrap many of the existing C library-routines that are explicitly polymorphic into implicitly polymorphic versions.
    2543 
    2544 
    2545 \subsection{malloc}
    2546 
    2547 \begin{lstlisting}
    2548 forall( otype T ) T * malloc( void );
    2549 forall( otype T ) T * malloc( char fill );
    2550 forall( otype T ) T * malloc( T * ptr, size_t size );
    2551 forall( otype T ) T * malloc( T * ptr, size_t size, unsigned char fill );
    2552 forall( otype T ) T * calloc( size_t size );
    2553 forall( otype T ) T * realloc( T * ptr, size_t size );
    2554 forall( otype T ) T * realloc( T * ptr, size_t size, unsigned char fill );
    2555 
    2556 forall( otype T ) T * aligned_alloc( size_t alignment );
    2557 forall( otype T ) T * memalign( size_t alignment );             // deprecated
    2558 forall( otype T ) int posix_memalign( T ** ptr, size_t alignment );
    2559 
    2560 forall( otype T ) T * memset( T * ptr, unsigned char fill ); // use default value '\0' for fill
    2561 forall( otype T ) T * memset( T * ptr );                                // remove when default value available
    2562 \end{lstlisting}
    2563 
    2564 
    2565 \subsection{ato/strto}
    2566 
    2567 \begin{lstlisting}
    2568 int ato( const char * ptr );
    2569 unsigned int ato( const char * ptr );
    2570 long int ato( const char * ptr );
    2571 unsigned long int ato( const char * ptr );
    2572 long long int ato( const char * ptr );
    2573 unsigned long long int ato( const char * ptr );
    2574 float ato( const char * ptr );
    2575 double ato( const char * ptr );
    2576 long double ato( const char * ptr );
    2577 float _Complex ato( const char * ptr );
    2578 double _Complex ato( const char * ptr );
    2579 long double _Complex ato( const char * ptr );
    2580 
    2581 int strto( const char * sptr, char ** eptr, int base );
    2582 unsigned int strto( const char * sptr, char ** eptr, int base );
    2583 long int strto( const char * sptr, char ** eptr, int base );
    2584 unsigned long int strto( const char * sptr, char ** eptr, int base );
    2585 long long int strto( const char * sptr, char ** eptr, int base );
    2586 unsigned long long int strto( const char * sptr, char ** eptr, int base );
    2587 float strto( const char * sptr, char ** eptr );
    2588 double strto( const char * sptr, char ** eptr );
    2589 long double strto( const char * sptr, char ** eptr );
    2590 float _Complex strto( const char * sptr, char ** eptr );
    2591 double _Complex strto( const char * sptr, char ** eptr );
    2592 long double _Complex strto( const char * sptr, char ** eptr );
    2593 \end{lstlisting}
    2594 
    2595 
    2596 \subsection{bsearch/qsort}
    2597 
    2598 \begin{lstlisting}
    2599 forall( otype T | { int ?<?( T, T ); } )
    2600 T * bsearch( const T key, const T * arr, size_t dimension );
    2601 
    2602 forall( otype T | { int ?<?( T, T ); } )
    2603 void qsort( const T * arr, size_t dimension );
    2604 \end{lstlisting}
    2605 
    2606 
    2607 \subsection{abs}
    2608 
    2609 \begin{lstlisting}
    2610 char abs( char );
    2611 extern "C" {
    2612 int abs( int );         // use default C routine for int
    2613 } // extern
    2614 long int abs( long int );
    2615 long long int abs( long long int );
    2616 float abs( float );
    2617 double abs( double );
    2618 long double abs( long double );
    2619 float _Complex abs( float _Complex );
    2620 double _Complex abs( double _Complex );
    2621 long double _Complex abs( long double _Complex );
    2622 \end{lstlisting}
    2623 
    2624 
    2625 \subsection{random}
    2626 
    2627 \begin{lstlisting}
    2628 void randseed( long int s );
    2629 char random();
    2630 int random();
    2631 unsigned int random();
    2632 long int random();
    2633 unsigned long int random();
    2634 float random();
    2635 double random();
    2636 float _Complex random();
    2637 double _Complex random();
    2638 long double _Complex random();
    2639 \end{lstlisting}
    2640 
    2641 
    2642 \subsection{min/max/swap}
    2643 
    2644 \begin{lstlisting}
    2645 forall( otype T | { int ?<?( T, T ); } )
    2646 T min( const T t1, const T t2 );
    2647 
    2648 forall( otype T | { int ?>?( T, T ); } )
    2649 T max( const T t1, const T t2 );
    2650 
    2651 forall( otype T )
    2652 void swap( T * t1, T * t2 );
    2653 \end{lstlisting}
     2579\section{Syntactic Anomalies}
     2580
     2581The number 0 and 1 are treated specially in \CFA, and can be redefined as variables.
     2582One syntactic anomaly is when a field in an structure is names 0 or 1:
     2583\begin{lstlisting}
     2584struct S {
     2585        int 0, 1;
     2586} s;
     2587\end{lstlisting}
     2588The problem occurs in accesing these fields using the selection operation ``\lstinline@.@'':
     2589\begin{lstlisting}
     2590s.0 = 0;        // ambiguity with floating constant .0
     2591s.1 = 1;        // ambiguity with floating constant .1
     2592\end{lstlisting}
     2593To make this work, a space is required after the field selection:
     2594\begin{lstlisting}
     2595`s.@\textvisiblespace@0` = 0;
     2596`s.@\textvisiblespace@1` = 1;
     2597\end{lstlisting}
     2598While this sytact is awkward, it is unlikely many programers will name fields of a structure 0 or 1.
     2599Like the \CC lexical problem with closing template-syntax, e.g, \lstinline@Foo<Bar<int`>>`@, this issue can be solved with a more powerful lexer/parser.
     2600
     2601There are several ambiguous cases with operator identifiers, e.g., \lstinline@int *?*?()@, where the string \lstinline@*?*?@ can be lexed as \lstinline@*@/\lstinline@?*?@ or \lstinline@*?@/\lstinline@*?@.
     2602Since it is common practise to put a unary operator juxtaposed to an identifier, e.g., \lstinline@*i@, users will be annoyed if they cannot do this with respect to operator identifiers.
     2603Even with this special hack, there are 5 general cases that cannot be handled.
     2604The first case is for the function-call identifier \lstinline@?()@:
     2605\begin{lstlisting}
     2606int *@\textvisiblespace@?()();  // declaration: space required after '*'
     2607*@\textvisiblespace@?()();              // expression: space required after '*'
     2608\end{lstlisting}
     2609Without the space, the string \lstinline@*?()@ is ambiguous without N character look ahead;
     2610it requires scanning ahead to determine if there is a \lstinline@'('@, which is the start of an argument/parameter list.
     2611
     2612The 4 remaining cases occur in expressions:
     2613\begin{lstlisting}
     2614i++@\textvisiblespace@?i:0;             // space required before '?'
     2615i--@\textvisiblespace@?i:0;             // space required before '?'
     2616i@\textvisiblespace@?++i:0;             // space required after '?'
     2617i@\textvisiblespace@?--i:0;             // space required after '?'
     2618\end{lstlisting}
     2619In the first two cases, the string \lstinline@i++?@ is ambiguous, where this string can be lexed as \lstinline@i@ / \lstinline@++?@ or \lstinline@i++@ / \lstinline@?@;
     2620it requires scanning ahead to determine if there is a \lstinline@'('@, which is the start of an argument list.
     2621In the second two cases, the string \lstinline@?++x@ is ambiguous, where this string can be lexed as \lstinline@?++@ / \lstinline@x@ or \lstinline@?@ / y\lstinline@++x@;
     2622it requires scanning ahead to determine if there is a \lstinline@'('@, which is the start of an argument list.
    26542623
    26552624
     
    26762645
    26772646task creates a type with implicit locking, separate stack, and a thread
     2647
    26782648
    26792649\subsection{Monitors}
     
    37753745\multicolumn{1}{c|}{\textbf{\CFA/\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}}   \\
    37763746\hline
    3777 \begin{lstlisting}
     3747\begin{lstlisting}[boxpos=t]
    37783748extern "C" {
    37793749#include <sys/types.h>
     
    37823752}
    37833753size_t fileSize( const char *path ) {
    3784         stat s;
     3754        struct stat s;
    37853755        stat(path, &s);
    37863756        return s.st_size;
     
    37883758\end{lstlisting}
    37893759&
    3790 \begin{lstlisting}
     3760\begin{lstlisting}[boxpos=t]
    37913761/*
    37923762#cgo
     
    38073777\end{lstlisting}
    38083778&
    3809 \begin{lstlisting}
     3779\begin{lstlisting}[boxpos=t]
    38103780use libc::{c_int, size_t};
    3811 
    3812 // The following declarations are
    38133781// translated from sys/stat.h
    38143782#[repr(C)]
     
    38183786        ...
    38193787}
    3820 
    38213788#[link(name = "libc")]
    38223789extern {
     
    38243791        buf: *mut stat_t) -> c_int;
    38253792}
    3826 
    38273793fn fileSize(path: *const u8) -> size_t
    38283794{
    38293795        unsafe {
    3830         let mut buf: stat_t = uninit();
    3831         stat(path, &mut buf);
    3832         buf.st_size
     3796                let mut buf: stat_t = uninit();
     3797                stat(path, &mut buf);
     3798                buf.st_size
    38333799        }
    38343800}
     
    39533919
    39543920
     3921\begin{comment}
    39553922\subsubsection{Modules/Packages}
    39563923
     
    40323999}
    40334000\end{lstlisting}
     4001\end{comment}
     4002
    40344003
    40354004\subsubsection{Parallel Tasks}
     
    41874156\end{flushleft}
    41884157
     4158\lstset{basicstyle=\sf\relsize{-1}}
     4159
     4160
    41894161\subsection{Summary of Language Comparison}
    41904162
     
    42534225D does not have any built-in concurrency constructs in the
    42544226language, though it does have a standard library for concurrency which includes the low-level primitives for concurrency.
     4227
     4228
     4229\appendix
     4230
     4231
     4232\section{I/O Library}
     4233\label{s:IOLibrary}
     4234\index{input/output library}
     4235
     4236The goal for \CFA I/O is to make I/O as simple as possible for the general case, while fully supporting polmorphism and user defined types in a consistent way.
     4237The general case is printing out a sequence of variables separated by whitespace.
     4238\begin{quote2}
     4239\begin{tabular}{@{}l@{\hspace{30pt}}l@{}}
     4240\multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}}       & \multicolumn{1}{c}{\textbf{\CC}}      \\
     4241\begin{lstlisting}
     4242int x = 0, y = 1, z = 2;
     4243`sout` `|` x `|` y `|` z `| endl`;
     4244\end{lstlisting}
     4245&
     4246\begin{lstlisting}
     4247
     4248cout << x << " " << y << " " << z << endl;
     4249\end{lstlisting}
     4250\end{tabular}
     4251\end{quote2}
     4252The \CFA form is half as many characters, and is similar to Python I/O with respect to implicit separators.
     4253
     4254The logical-or operator is used because it is the lowest-priority overloadable operator, other than assignment.
     4255Therefore, fewer output expressions require parenthesis.
     4256\begin{quote2}
     4257\begin{tabular}{@{}ll@{}}
     4258\textbf{\CFA:}
     4259&
     4260\begin{lstlisting}
     4261sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl;
     4262\end{lstlisting}
     4263\\
     4264\textbf{\CC:}
     4265&
     4266\begin{lstlisting}
     4267cout << x * 3 << y + 1 << (z << 2) << (x == y) << (x | y) << (x || y) << (x > z ? 1 : 2) << endl;
     4268\end{lstlisting}
     4269\end{tabular}
     4270\end{quote2}
     4271Finally, the logical-or operator has a link with the Shell pipe-operator for moving data, although data flows in the opposite direction.
     4272
     4273The implicit seperator\index{I/O separator} character (space/blank) is a separator not a terminator.
     4274The rules for implicitly adding the separator are:
     4275\begin{enumerate}
     4276\item
     4277A seperator does not appear at the start or end of a line.
     4278\begin{lstlisting}[belowskip=0pt]
     4279sout 1 | 2 | 3 | endl;
     4280\end{lstlisting}
     4281\begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     42821 2 3
     4283\end{lstlisting}
     4284\item
     4285A seperator does not appear before or after a character literal or variable.
     4286\begin{lstlisting}
     4287sout | '1' | '2' | '3' | endl;
     4288123
     4289\end{lstlisting}
     4290\item
     4291A seperator does not appear before or after a null (empty) C string
     4292\begin{lstlisting}
     4293sout | 1 | "" | 2 | "" | 3 | endl;
     4294123
     4295\end{lstlisting}
     4296which is a local mechanism to disable insertion of the separator character.
     4297\item
     4298A seperator does not appear before a C string starting with the \Index{extended ASCII}\index{ASCII} characters: \lstinline[mathescape=off]@([{$£¥¿«@
     4299%$
     4300\begin{lstlisting}[mathescape=off]
     4301sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x $" | 4 | "x £" | 5 | "x ¥" | 6 | "x ¿" | 7 | "x «" | 8 | endl;
     4302\end{lstlisting}
     4303%$
     4304\begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     4305x (1 x [2 x {3 x $4 x £5 x ¥6 x ¿7 x «8
     4306\end{lstlisting}
     4307%$
     4308\item
     4309A seperator does not appear after a C string ending with the extended ASCII characters: \lstinline@,.:;!?)]}%¢»@
     4310\begin{lstlisting}[belowskip=0pt]
     4311sout | 1 | ", x" | 2 | ". x" | 3 | ": x" | 4 | "; x" | 5 | "! x" | 6 | "? x" | 7 | ") x" | 8 | "] x" | 9 | "} x"
     4312         | 10 | "% x" | 11 | L"¢ x" | 12 | L"» x" | endl;
     4313\end{lstlisting}
     4314\begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     43151, x 2. x 3: x 4; x 5! x 6? x 7) x 8] x 9} x 10% x 11¢ 12»
     4316\end{lstlisting}
     4317\item
     4318A seperator does not appear before or after a C string begining/ending with the characters: \lstinline@\f\n\r\t\v\`'"@
     4319\begin{lstlisting}[belowskip=0pt]
     4320sout | "x '" | 1 | "' x \`" | 2 | "\` x \"" | 3 | "\" x" | endl;
     4321\end{lstlisting}
     4322\begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     4323x '1' x \`2\` x "3" x
     4324\end{lstlisting}
     4325\begin{lstlisting}[showtabs=true,aboveskip=0pt]
     4326sout | "x\t" | 1 | "\tx" | endl;
     4327x       1       x
     4328\end{lstlisting}
     4329\end{enumerate}
     4330The following \CC-style \Index{manipulator}s allow further control over implicit seperation.
     4331\begin{lstlisting}[mathescape=off,belowskip=0pt]
     4332sout | sepOn | 1 | 2 | 3 | sepOn | endl;        // separator at start of line
     4333\end{lstlisting}
     4334\begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     4335 1 2 3
     4336\end{lstlisting}
     4337\begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt]
     4338sout | 1 | sepOff | 2 | 3 | endl;                       // turn off implicit separator temporarily
     4339\end{lstlisting}
     4340\begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     434112 3
     4342\end{lstlisting}
     4343\begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt]
     4344sout | sepDisable | 1 | 2 | 3 | endl;           // turn off implicit separation, affects all subsequent prints
     4345\end{lstlisting}
     4346\begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     4347123
     4348\end{lstlisting}
     4349\begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt]
     4350sout | 1 | sepOn | 2 | 3 | endl;                        // turn on implicit separator temporarily
     4351\end{lstlisting}
     4352\begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     43531 23
     4354\end{lstlisting}
     4355\begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt]
     4356sout | sepEnable | 1 | 2 | 3 | endl;            // turn on implicit separation, affects all subsequent prints
     4357\end{lstlisting}
     4358\begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     43591 2 3
     4360\end{lstlisting}
     4361\begin{lstlisting}[mathescape=off,aboveskip=0pt,aboveskip=0pt,belowskip=0pt]
     4362sepSet( sout, ", $" );                                          // change separator from " " to ", $"
     4363sout | 1 | 2 | 3 | endl;
     4364\end{lstlisting}
     4365%$
     4366\begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt]
     43671, $2, $3
     4368\end{lstlisting}
     4369%$
     4370\VRef[Figure]{f:ExampleIO} shows an example of input and output I/O in \CFA.
     4371
     4372\begin{figure}
     4373\begin{lstlisting}[mathescape=off]
     4374#include <fstream>
     4375
     4376int main() {
     4377        char c;                                                                                                         // basic types
     4378        short int si;
     4379        unsigned short int usi;
     4380        int i;
     4381        unsigned int ui;
     4382        long int li;
     4383        unsigned long int uli;
     4384        long long int lli;
     4385        unsigned long long int ulli;
     4386        float f;
     4387        double d;
     4388        long double ld;
     4389        float _Complex fc;
     4390        double _Complex dc;
     4391        long double _Complex ldc;
     4392        char s1[10], s2[10];
     4393
     4394        ifstream in;                                                                                            // create / open file
     4395        open( &in, "input.data", "r" );
     4396
     4397        &in | &c                                                                                                        // character
     4398                | &si | &usi | &i | &ui | &li | &uli | &lli | &ulli             // integral
     4399                | &f | &d | &ld                                                                                 // floating point
     4400                | &fc | &dc | &ldc                                                                              // floating-point complex
     4401                | cstr( s1 ) | cstr( s2, 10 );                                                  // C string, length unchecked and checked
     4402
     4403        sout | c | ' ' | endl                                                                           // character
     4404                 | si | usi | i | ui | li | uli | lli | ulli | endl             // integral
     4405                 | f | d | ld | endl                                                                    // floating point
     4406                 | fc | dc | ldc | endl;                                                                // complex
     4407        sout | endl;
     4408        sout | f | "" | d | "" | ld | endl                                                      // floating point without separator
     4409                 | sepDisable | fc | dc | ldc | sepEnable | endl                // complex without separator
     4410                 | sepOn | s1 | sepOff | s2 | endl                                              // local separator removal
     4411                 | s1 | "" | s2 | endl;                                                                 // C string withou separator
     4412        sout | endl;
     4413        sepSet( sout, ", $" );                                                                          // change separator, maximum of 15 characters
     4414        sout | f | d | ld | endl                                                                        // floating point without separator
     4415                 | fc | dc | ldc | endl                                                                 // complex without separator
     4416                 | s1 | s2 | endl;
     4417}
     4418
     4419$ cat input.data
     4420A 1 2 3 4 5 6 7 8 1.1 1.2 1.3 1.1+2.3 1.1-2.3 1.1-2.3 abc xyz
     4421$ a.out
     4422A
     44231 2 3 4 5 6 7 8
     44241.1 1.2 1.3
     44251.1+2.3i 1.1-2.3i 1.1-2.3i
     4426
     44271.11.21.3
     44281.1+2.3i1.1-2.3i1.1-2.3i
     4429 abcxyz
     4430abcxyz
     4431
     44321.1, $1.2, $1.3
     44331.1+2.3i, $1.1-2.3i, $1.1-2.3i
     4434abc, $xyz
     4435\end{lstlisting}
     4436\caption{Example I/O}
     4437\label{f:ExampleIO}
     4438\end{figure}
     4439
     4440
     4441\section{Standard Library}
     4442\label{s:StandardLibrary}
     4443
     4444The goal of the \CFA standard-library is to wrap many of the existing C library-routines that are explicitly polymorphic into implicitly polymorphic versions.
     4445
     4446
     4447\subsection{malloc}
     4448
     4449\begin{lstlisting}
     4450forall( otype T ) T * malloc( void );
     4451forall( otype T ) T * malloc( char fill );
     4452forall( otype T ) T * malloc( T * ptr, size_t size );
     4453forall( otype T ) T * malloc( T * ptr, size_t size, unsigned char fill );
     4454forall( otype T ) T * calloc( size_t size );
     4455forall( otype T ) T * realloc( T * ptr, size_t size );
     4456forall( otype T ) T * realloc( T * ptr, size_t size, unsigned char fill );
     4457
     4458forall( otype T ) T * aligned_alloc( size_t alignment );
     4459forall( otype T ) T * memalign( size_t alignment );             // deprecated
     4460forall( otype T ) int posix_memalign( T ** ptr, size_t alignment );
     4461
     4462forall( otype T ) T * memset( T * ptr, unsigned char fill ); // use default value '\0' for fill
     4463forall( otype T ) T * memset( T * ptr );                                // remove when default value available
     4464\end{lstlisting}
     4465
     4466
     4467\subsection{ato/strto}
     4468
     4469\begin{lstlisting}
     4470int ato( const char * ptr );
     4471unsigned int ato( const char * ptr );
     4472long int ato( const char * ptr );
     4473unsigned long int ato( const char * ptr );
     4474long long int ato( const char * ptr );
     4475unsigned long long int ato( const char * ptr );
     4476float ato( const char * ptr );
     4477double ato( const char * ptr );
     4478long double ato( const char * ptr );
     4479float _Complex ato( const char * ptr );
     4480double _Complex ato( const char * ptr );
     4481long double _Complex ato( const char * ptr );
     4482
     4483int strto( const char * sptr, char ** eptr, int base );
     4484unsigned int strto( const char * sptr, char ** eptr, int base );
     4485long int strto( const char * sptr, char ** eptr, int base );
     4486unsigned long int strto( const char * sptr, char ** eptr, int base );
     4487long long int strto( const char * sptr, char ** eptr, int base );
     4488unsigned long long int strto( const char * sptr, char ** eptr, int base );
     4489float strto( const char * sptr, char ** eptr );
     4490double strto( const char * sptr, char ** eptr );
     4491long double strto( const char * sptr, char ** eptr );
     4492float _Complex strto( const char * sptr, char ** eptr );
     4493double _Complex strto( const char * sptr, char ** eptr );
     4494long double _Complex strto( const char * sptr, char ** eptr );
     4495\end{lstlisting}
     4496
     4497
     4498\subsection{bsearch/qsort}
     4499
     4500\begin{lstlisting}
     4501forall( otype T | { int ?<?( T, T ); } )
     4502T * bsearch( const T key, const T * arr, size_t dimension );
     4503
     4504forall( otype T | { int ?<?( T, T ); } )
     4505void qsort( const T * arr, size_t dimension );
     4506\end{lstlisting}
     4507
     4508
     4509\subsection{abs}
     4510
     4511\begin{lstlisting}
     4512char abs( char );
     4513extern "C" {
     4514int abs( int );                         // use default C routine for int
     4515} // extern "C"
     4516long int abs( long int );
     4517long long int abs( long long int );
     4518float abs( float );
     4519double abs( double );
     4520long double abs( long double );
     4521float _Complex abs( float _Complex );
     4522double _Complex abs( double _Complex );
     4523long double _Complex abs( long double _Complex );
     4524\end{lstlisting}
     4525
     4526
     4527\subsection{floor/ceil}
     4528
     4529\begin{lstlisting}
     4530float floor( float );
     4531extern "C" {
     4532double floor( double );         // use C routine for double
     4533} // extern "C"
     4534long double floor( long double );
     4535
     4536float ceil( float );
     4537extern "C" {
     4538double ceil( double );          // use C routine for double
     4539} // extern "C"
     4540long double ceil( long double );
     4541\end{lstlisting}
     4542
     4543
     4544\subsection{random}
     4545
     4546\begin{lstlisting}
     4547void rand48seed( long int s );
     4548char rand48();
     4549int rand48();
     4550unsigned int rand48();
     4551long int rand48();
     4552unsigned long int rand48();
     4553float rand48();
     4554double rand48();
     4555float _Complex rand48();
     4556double _Complex rand48();
     4557long double _Complex rand48();
     4558\end{lstlisting}
     4559
     4560
     4561\subsection{min/max/swap}
     4562
     4563\begin{lstlisting}
     4564forall( otype T | { int ?<?( T, T ); } )
     4565T min( const T t1, const T t2 );
     4566
     4567forall( otype T | { int ?>?( T, T ); } )
     4568T max( const T t1, const T t2 );
     4569
     4570forall( otype T )
     4571void swap( T * t1, T * t2 );
     4572\end{lstlisting}
    42554573
    42564574
  • src/driver/cfa.cc

    r3cfe27f r53ba273  
    1010// Created On       : Tue Aug 20 13:44:49 2002
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jan 28 18:24:06 2016
    13 // Update Count     : 127
     12// Last Modified On : Wed Apr  6 14:04:22 2016
     13// Update Count     : 132
    1414//
    1515
     
    165165                                nargs += 1;
    166166                        } else if ( prefix( arg, "-std=" ) ) {
    167                                 std_flag = true;                                                // std=XX provided
     167                                std_flag = true;                                                // -std=XX provided
    168168                                args[nargs] = argv[i];                                  // pass the argument along
    169169                                nargs += 1;
     
    307307                nargs += 1;
    308308                if ( ! std_flag ) {                                                             // default c99, if none specified
    309                         args[nargs] = "-std=c99";
     309                        args[nargs] = "-std=gnu99";
    310310                        nargs += 1;
    311311                } // if
  • src/libcfa/Makefile.am

    r3cfe27f r53ba273  
    1111## Created On       : Sun May 31 08:54:01 2015
    1212## Last Modified By : Peter A. Buhr
    13 ## Last Modified On : Wed Mar  2 22:59:23 2016
    14 ## Update Count     : 119
     13## Last Modified On : Wed Apr  6 21:10:44 2016
     14## Update Count     : 123
    1515###############################################################################
    1616
     
    6060        ${CC} ${CFLAGS} -c -o $@ $<
    6161
    62 libs = stdlib iostream fstream iterator
     62libs = limits stdlib iostream fstream iterator rational
    6363libcfa_a_SOURCES = libcfa-prelude.c ${libs:=.c}
    6464
    65 cheaders = #  expat
    66 cfaheaders = limits
     65cheaders = # expat
     66cfaheaders = # limits
    6767include_HEADERS = ${cheaders:=.h} ${libs} ${cfaheaders}
    6868
  • src/libcfa/Makefile.in

    r3cfe27f r53ba273  
    8383libcfa_a_AR = $(AR) $(ARFLAGS)
    8484libcfa_a_LIBADD =
    85 am__objects_1 = stdlib.$(OBJEXT) iostream.$(OBJEXT) fstream.$(OBJEXT) \
    86         iterator.$(OBJEXT)
     85am__objects_1 = limits.$(OBJEXT) stdlib.$(OBJEXT) iostream.$(OBJEXT) \
     86        fstream.$(OBJEXT) iterator.$(OBJEXT) rational.$(OBJEXT)
    8787am_libcfa_a_OBJECTS = libcfa-prelude.$(OBJEXT) $(am__objects_1)
    8888libcfa_a_OBJECTS = $(am_libcfa_a_OBJECTS)
     
    213213MAINTAINERCLEANFILES = ${addprefix ${libdir}/,${cfalib_DATA}} \
    214214        ${addprefix ${libdir}/,${lib_LIBRARIES}} ${includedir}/*
    215 libs = stdlib iostream fstream iterator
     215libs = limits stdlib iostream fstream iterator rational
    216216libcfa_a_SOURCES = libcfa-prelude.c ${libs:=.c}
    217 cheaders = #  expat
    218 cfaheaders = limits
     217cheaders = # expat
     218cfaheaders = # limits
    219219include_HEADERS = ${cheaders:=.h} ${libs} ${cfaheaders}
    220220all: all-am
     
    297297@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/iterator.Po@am__quote@
    298298@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa-prelude.Po@am__quote@
     299@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/limits.Po@am__quote@
     300@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rational.Po@am__quote@
    299301@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stdlib.Po@am__quote@
    300302
  • src/libcfa/fstream

    r3cfe27f r53ba273  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Mar  2 15:08:14 2016
    13 // Update Count     : 78
     12// Last Modified On : Tue Apr  5 22:37:12 2016
     13// Update Count     : 82
    1414//
    1515
     
    2020
    2121enum { separateSize = 16 };
    22 struct ofstream { void *file; int separate; char separator[separateSize]; };
     22struct ofstream {
     23        void *file;
     24        _Bool sepDefault;
     25        _Bool sepOnOff;
     26        char separator[separateSize];
     27}; // ofstream
    2328
    2429_Bool sepPrt( ofstream * );
    2530void sepOn( ofstream * );
    2631void sepOff( ofstream * );
     32void sepReset( ofstream * );
     33void sepReset( ofstream *, _Bool );
    2734void sepSet( ofstream *, const char * );
    2835const char * sepGet( ofstream * );
    29 void sepDisable( ofstream * );
    30 void sepEnable( ofstream * );
     36_Bool sepDisable( ofstream * );
     37_Bool sepEnable( ofstream * );
    3138int fail( ofstream * );
    3239int flush( ofstream * );
     
    3946
    4047// implement context istream
    41 struct ifstream { void *file; };
     48struct ifstream {
     49        void *file;
     50}; // ifstream
    4251
    4352int fail( ifstream * is );
  • src/libcfa/fstream.c

    r3cfe27f r53ba273  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Feb 29 18:41:10 2016
    13 // Update Count     : 162
     12// Last Modified On : Wed Apr  6 17:55:27 2016
     13// Update Count     : 176
    1414//
    1515
     
    2525}
    2626
    27 #define IO_MSG "I/O error "
     27#define IO_MSG "I/O error: "
    2828
    29 _Bool sepPrt( ofstream * os ) { return os->separate == 1; }
    30 void sepOn( ofstream * os ) { if ( os->separate != 2 ) os->separate = 1; }
    31 void sepOff( ofstream * os ) { if ( os->separate != 2 ) os->separate = 0; }
     29_Bool sepPrt( ofstream * os ) { return os->sepOnOff; }
     30void sepOn( ofstream * os ) { os->sepOnOff = 1; }
     31void sepOff( ofstream * os ) { os->sepOnOff = 0; }
     32void sepReset( ofstream * os ) { os->sepOnOff = os->sepDefault; }
     33void sepReset( ofstream * os, _Bool reset ) { os->sepDefault = reset; os->sepOnOff = os->sepDefault; }
    3234void sepSet( ofstream * os, const char * s ) {
    3335        strncpy( &(os->separator[0]), s, separateSize - 1 );
     
    3537} // sepSet
    3638const char * sepGet( ofstream * os ) { return &(os->separator[0]); }
    37 void sepDisable( ofstream *os ) { os->separate = 2; }
    38 void sepEnable( ofstream *os ) { os->separate = 0; }
     39_Bool sepDisable( ofstream *os ) {
     40        _Bool temp = os->sepDefault;
     41        os->sepDefault = 0;
     42        sepReset( os );
     43        return temp;
     44} // sepDisable
     45_Bool sepEnable( ofstream *os ) {
     46        _Bool temp = os->sepDefault;
     47        os->sepDefault = 1;
     48        sepReset( os );
     49        return temp;
     50} // sepEnable
    3951
    4052int fail( ofstream * os ) {
     
    4961        FILE *file = fopen( name, mode );
    5062        if ( file == 0 ) {                                                                      // do not change unless successful
    51                 perror( IO_MSG "open output" );
     63                fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
     64                perror( 0 );
    5265                exit( EXIT_FAILURE );
    5366        } // if
     
    94107
    95108
    96 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), 0, { ' ', '\0' } };
     109static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), 1, 0, { ' ', '\0' } };
    97110ofstream *sout = &soutFile;
    98 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), 0, { ' ', '\0' } };
     111static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), 1, 0, { ' ', '\0' } };
    99112ofstream *serr = &serrFile;
    100113
     
    114127        FILE *t = fopen( name, mode );
    115128        if ( t == 0 ) {                                                                         // do not change unless successful
    116                 perror( IO_MSG "open input" );
     129                fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
     130                perror( 0 );
    117131                exit( EXIT_FAILURE );
    118132        } // if
     
    175189// Local Variables: //
    176190// tab-width: 4 //
    177 // compile-command: "cfa fstream.c" //
    178191// End: //
  • src/libcfa/iostream

    r3cfe27f r53ba273  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Mar  2 18:05:27 2016
    13 // Update Count     : 85
     12// Last Modified On : Tue Apr  5 22:32:37 2016
     13// Update Count     : 90
    1414//
    1515
     
    2323        void sepOn( ostype * );
    2424        void sepOff( ostype * );
     25        void sepReset( ostype * );
     26        void sepReset( ostype *, _Bool );
    2527        void sepSet( ostype *, const char * );
    2628        const char * sepGet( ostype * );
    27         void sepDisable( ostype * );
    28         void sepEnable( ostype * );
     29        _Bool sepDisable( ostype * );
     30        _Bool sepEnable( ostype * );
    2931        int fail( ostype * );
    3032        int flush( ostype * );
     
    6769forall( dtype ostype | ostream( ostype ) ) ostype * sepOn( ostype * );
    6870forall( dtype ostype | ostream( ostype ) ) ostype * sepOff( ostype * );
     71forall( dtype ostype | ostream( ostype ) ) ostype * sepDisable( ostype * );
     72forall( dtype ostype | ostream( ostype ) ) ostype * sepEnable( ostype * );
    6973
    7074// writes the range [begin, end) to the given stream
     
    110114forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, long double _Complex * );
    111115
    112 struct _Istream_str1 { char * s; };
    113 _Istream_str1 str( char * );
    114 forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, _Istream_str1 );
     116struct _Istream_cstrUC { char * s; };
     117_Istream_cstrUC cstr( char * );
     118forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, _Istream_cstrUC );
    115119
    116 struct _Istream_str2 { char * s; int size; };
    117 _Istream_str2 str( char *, int size );
    118 forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, _Istream_str2 );
     120struct _Istream_cstrC { char * s; int size; };
     121_Istream_cstrC cstr( char *, int size );
     122forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, _Istream_cstrC );
    119123
    120124#endif // __IOSTREAM_H__
  • src/libcfa/iostream.c

    r3cfe27f r53ba273  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Mar  7 13:51:23 2016
    13 // Update Count     : 227
     12// Last Modified On : Wed Apr  6 16:13:29 2016
     13// Update Count     : 278
    1414//
    1515
     
    2727ostype * ?|?( ostype *os, char c ) {
    2828        prtfmt( os, "%c", c );
     29        sepOff( os );
    2930        return os;
    3031} // ?|?
     
    3233forall( dtype ostype | ostream( ostype ) )
    3334ostype * ?|?( ostype *os, short int si ) {
    34         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     35        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     36        sepReset( os );
    3537        prtfmt( os, "%hd", si );
    3638        return os;
     
    3941forall( dtype ostype | ostream( ostype ) )
    4042ostype * ?|?( ostype *os, unsigned short int usi ) {
    41         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     43        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     44        sepReset( os );
    4245        prtfmt( os, "%hu", usi );
    4346        return os;
     
    4649forall( dtype ostype | ostream( ostype ) )
    4750ostype * ?|?( ostype *os, int i ) {
    48         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     51        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     52        sepReset( os );
    4953        prtfmt( os, "%d", i );
    5054        return os;
     
    5357forall( dtype ostype | ostream( ostype ) )
    5458ostype * ?|?( ostype *os, unsigned int ui ) {
    55         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     59        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     60        sepReset( os );
    5661        prtfmt( os, "%u", ui );
    5762        return os;
     
    6065forall( dtype ostype | ostream( ostype ) )
    6166ostype * ?|?( ostype *os, long int li ) {
    62         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     67        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     68        sepReset( os );
    6369        prtfmt( os, "%ld", li );
    6470        return os;
     
    6773forall( dtype ostype | ostream( ostype ) )
    6874ostype * ?|?( ostype *os, unsigned long int uli ) {
    69         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     75        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     76        sepReset( os );
    7077        prtfmt( os, "%lu", uli );
    7178        return os;
     
    7481forall( dtype ostype | ostream( ostype ) )
    7582ostype * ?|?( ostype *os, long long int lli ) {
    76         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     83        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     84        sepReset( os );
    7785        prtfmt( os, "%lld", lli );
    7886        return os;
     
    8189forall( dtype ostype | ostream( ostype ) )
    8290ostype * ?|?( ostype *os, unsigned long long int ulli ) {
    83         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     91        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     92        sepReset( os );
    8493        prtfmt( os, "%llu", ulli );
    8594        return os;
     
    8897forall( dtype ostype | ostream( ostype ) )
    8998ostype * ?|?( ostype *os, float f ) {
    90         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     99        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     100        sepReset( os );
    91101        prtfmt( os, "%g", f );
    92102        return os;
     
    95105forall( dtype ostype | ostream( ostype ) )
    96106ostype * ?|?( ostype *os, double d ) {
    97         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     107        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     108        sepReset( os );
    98109        prtfmt( os, "%.*lg", DBL_DIG, d );
    99110        return os;
     
    102113forall( dtype ostype | ostream( ostype ) )
    103114ostype * ?|?( ostype *os, long double ld ) {
    104         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     115        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     116        sepReset( os );
    105117        prtfmt( os, "%.*Lg", LDBL_DIG, ld );
    106118        return os;
     
    110122ostype * ?|?( ostype *os, float _Complex fc ) {
    111123        os | crealf( fc );
    112         if ( cimagf( fc ) >= 0 ) os | '+';
    113         os | "" | cimagf( fc ) | 'i';
     124        _Bool temp = sepDisable( os );                                          // disable separators within complex value
     125        if ( cimagf( fc ) >= 0 ) os | '+';                                      // negative value prints '-'
     126        os | cimagf( fc ) | 'i';
     127        sepReset( os, temp );                                                           // reset separator
    114128        return os;
    115129} // ?|?
     
    118132ostype * ?|?( ostype *os, double _Complex dc ) {
    119133        os | creal( dc );
    120         if ( cimag( dc ) >= 0 ) os | '+';
    121         os | "" | cimag( dc ) | 'i';
     134        _Bool temp = sepDisable( os );                                          // disable separators within complex value
     135        if ( cimag( dc ) >= 0 ) os | '+';                                       // negative value prints '-'
     136        os | cimag( dc ) | 'i';
     137        sepReset( os, temp );                                                           // reset separator
    122138        return os;
    123139} // ?|?
     
    126142ostype * ?|?( ostype *os, long double _Complex ldc ) {
    127143        os | creall( ldc );
    128         if ( cimagl( ldc ) >= 0 ) os | '+';
    129         os | "" | cimagl( ldc ) | 'i';
     144        _Bool temp = sepDisable( os );                                          // disable separators within complex value
     145        if ( cimagl( ldc ) >= 0 ) os | '+';                                     // negative value prints '-'
     146        os | cimagl( ldc ) | 'i';
     147        sepReset( os, temp );                                                           // reset separator
    130148        return os;
    131149} // ?|?
     
    134152ostype * ?|?( ostype *os, const char *cp ) {
    135153        enum { Open = 1, Close, OpenClose };
    136         static const char mask[256] = {
     154        static const unsigned char mask[256] = {
    137155                // opening delimiters
    138156                ['('] : Open, ['['] : Open, ['{'] : Open,
    139                 ['$'] : Open, [L'£'] : Open, [L'¥'] : Open, [L'¢'] : Open, [L'¿'] : Open, [L'«'] : Open,
     157                ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
    140158                // closing delimiters
    141159                [','] : Close, ['.'] : Close, [':'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
    142160                [')'] : Close, [']'] : Close, ['}'] : Close,
    143                 ['%'] : Close, [L'»'] : Close,
     161                ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
    144162                // opening-closing delimiters
    145163                ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose,
     164                ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
    146165        }; // mask
    147166
     
    149168        // null string => no separator
    150169  if ( len == 0 ) { sepOff( os ); return os; }
    151         // first character NOT spacing or closing punctuation => add left separator
    152         if ( sepPrt( os ) && isspace( cp[0] ) == 0 && mask[ cp[0] ] != Close && mask[ cp[0] ] != OpenClose ) {
     170        // first character IS NOT spacing or closing punctuation => add left separator
     171        unsigned char ch = cp[0];                                                       // must make unsigned
     172        if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
    153173                prtfmt( os, "%s", sepGet( os ) );
    154174        } // if
    155175        // last character IS spacing or opening punctuation => turn off separator for next item
    156176        unsigned int posn = len - 1;
    157         if ( isspace( cp[posn] ) || mask[ cp[posn] ] == Open || mask[ cp[posn] ] == OpenClose ) {
     177        ch = cp[posn];                                                                          // must make unsigned
     178        if ( mask[ ch ] == Open || mask[ ch ] == OpenClose ) {
    158179                sepOff( os );
    159180        } else {
     
    165186forall( dtype ostype | ostream( ostype ) )
    166187ostype * ?|?( ostype *os, const void *p ) {
    167         if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) ); else sepOn( os );
     188        if ( sepPrt( os ) ) prtfmt( os, "%s", sepGet( os ) );
     189        sepReset( os );
    168190        prtfmt( os, "%p", p );
    169191        return os;
     
    196218} // sepOff
    197219
     220forall( dtype ostype | ostream( ostype ) )
     221ostype * sepEnable( ostype * os ) {
     222        sepEnable( os );
     223        return os;
     224} // sepEnable
     225
     226forall( dtype ostype | ostream( ostype ) )
     227ostype * sepDisable( ostype * os ) {
     228        sepDisable( os );
     229        return os;
     230} // sepDisable
     231
    198232//---------------------------------------
    199233
     
    310344} // ?|?
    311345
    312 _Istream_str1 str( char * s ) { _Istream_str1 s = { s }; return s; }
    313 forall( dtype istype | istream( istype ) )
    314 istype * ?|?( istype * is, _Istream_str1 str ) {
    315         scanfmt( is, "%s", str.s );
    316         return is;
    317 } // str
    318 
    319 _Istream_str2 str( char * s, int size ) { _Istream_str2 s = { s, size }; return s; }
    320 forall( dtype istype | istream( istype ) )
    321 istype * ?|?( istype * is, _Istream_str2 str ) {
     346_Istream_cstrUC cstr( char * s ) { _Istream_cstrUC s = { s }; return s; }
     347forall( dtype istype | istream( istype ) )
     348istype * ?|?( istype * is, _Istream_cstrUC cstr ) {
     349        scanfmt( is, "%s", cstr.s );
     350        return is;
     351} // cstr
     352
     353_Istream_cstrC cstr( char * s, int size ) { _Istream_cstrC s = { s, size }; return s; }
     354forall( dtype istype | istream( istype ) )
     355istype * ?|?( istype * is, _Istream_cstrC cstr ) {
    322356        char buf[16];
    323         sprintf( buf, "%%%ds", str.size );
    324         scanfmt( is, buf, str.s );
    325         return is;
    326 } // str
     357        sprintf( buf, "%%%ds", cstr.size );
     358        scanfmt( is, buf, cstr.s );
     359        return is;
     360} // cstr
    327361
    328362// Local Variables: //
  • src/libcfa/limits

    r3cfe27f r53ba273  
     1//
     2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
     3//
     4// The contents of this file are covered under the licence agreement in the
     5// file "LICENCE" distributed with Cforall.
     6//
     7// limits --
     8//
     9// Author           : Peter A. Buhr
     10// Created On       : Wed Apr  6 18:06:52 2016
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Wed Apr  6 21:08:16 2016
     13// Update Count     : 6
     14//
     15
    116// Integral Constants
    217
    3 const short int MIN = -32768;
    4 const int MIN = -2147483648;
    5 const long int MIN = -9223372036854775807L - 1L;
    6 const long long int MIN = -9223372036854775807LL - 1LL;
     18extern const short int MIN;
     19extern const int MIN;
     20extern const long int MIN;
     21extern const long long int MIN;
    722
    8 const short int MAX = 32767;
    9 const unsigned short int MAX = 65535;
    10 const int MAX = 2147483647;
    11 const unsigned int MAX = 4294967295_U;
    12 const long int MAX = 9223372036854775807_L;
    13 const unsigned long int MAX = 4294967295_U;
    14 const long long int MAX = 9223372036854775807_LL;
    15 const unsigned long long int MAX = 18446744073709551615_ULL;
     23extern const short int MAX;
     24extern const unsigned short int MAX;
     25extern const int MAX;
     26extern const unsigned int MAX;
     27extern const long int MAX;
     28extern const unsigned long int MAX;
     29extern const long long int MAX;
     30extern const unsigned long long int MAX;
    1631
    1732// Floating-Point Constants
    1833
    19 const float PI = 3.141592_F;                            // pi
    20 const float PI_2 = 1.570796_F;                          // pi / 2
    21 const float PI_4 = 0.7853981_F;                         // pi / 4
    22 const float _1_PI = 0.3183098_F;                        // 1 / pi
    23 const float _2_PI = 0.6366197_F;                        // 2 / pi
    24 const float _2_SQRT_PI = 1.128379_F;                    // 2 / sqrt(pi)
     34extern const float PI;                                                                  // pi
     35extern const float PI_2;                                                                // pi / 2
     36extern const float PI_4;                                                                // pi / 4
     37extern const float _1_PI;                                                               // 1 / pi
     38extern const float _2_PI;                                                               // 2 / pi
     39extern const float _2_SQRT_PI;                                                  // 2 / sqrt(pi)
    2540
    26 const double PI = 3.14159265358979323846_D;             // pi
    27 const double PI_2 = 1.57079632679489661923_D;           // pi / 2
    28 const double PI_4 = 0.78539816339744830962_D;           // pi / 4
    29 const double _1_PI = 0.31830988618379067154_D;          // 1 / pi
    30 const double _2_PI = 0.63661977236758134308_D;          // 2 / pi
    31 const double _2_SQRT_PI = 1.12837916709551257390_D;     // 2 / sqrt(pi)
     41extern const double PI;                                                                 // pi
     42extern const double PI_2;                                                               // pi / 2
     43extern const double PI_4;                                                               // pi / 4
     44extern const double _1_PI;                                                              // 1 / pi
     45extern const double _2_PI;                                                              // 2 / pi
     46extern const double _2_SQRT_PI;                                                 // 2 / sqrt(pi)
    3247
    33 const long double PI = 3.1415926535897932384626433832795029_DL; // pi
    34 const long double PI_2 = 1.5707963267948966192313216916397514_DL; // pi / 2
    35 const long double PI_4 = 0.7853981633974483096156608458198757_DL; // pi / 4
    36 const long double _1_PI = 0.3183098861837906715377675267450287_DL; // 1 / pi
    37 const long double _2_PI = 0.6366197723675813430755350534900574_DL; // 2 / pi
    38 const long double _2_SQRT_PI = 1.1283791670955125738961589031215452_DL; // 2 / sqrt(pi)
     48extern const long double PI;                                                    // pi
     49extern const long double PI_2;                                                  // pi / 2
     50extern const long double PI_4;                                                  // pi / 4
     51extern const long double _1_PI;                                                 // 1 / pi
     52extern const long double _2_PI;                                                 // 2 / pi
     53extern const long double _2_SQRT_PI;                                    // 2 / sqrt(pi)
    3954
    40 const _Complex PI = 3.14159265358979323846_D+0.0_iD;    // pi
    41 const _Complex PI_2 = 1.57079632679489661923_D+0.0_iD;  // pi / 2
    42 const _Complex PI_4 = 0.78539816339744830962_D+0.0_iD;  // pi / 4
    43 const _Complex _1_PI = 0.31830988618379067154_D+0.0_iD; // 1 / pi
    44 const _Complex _2_PI = 0.63661977236758134308_D+0.0_iD; // 2 / pi
    45 const _Complex _2_SQRT_PI = 1.12837916709551257390_D+0.0_iD; // 2 / sqrt(pi)
     55extern const _Complex PI;                                                               // pi
     56extern const _Complex PI_2;                                                             // pi / 2
     57extern const _Complex PI_4;                                                             // pi / 4
     58extern const _Complex _1_PI;                                                    // 1 / pi
     59extern const _Complex _2_PI;                                                    // 2 / pi
     60extern const _Complex _2_SQRT_PI;                                               // 2 / sqrt(pi)
    4661
    47 const long _Complex PI = 3.1415926535897932384626433832795029_L+0.0iL; // pi
    48 const long _Complex PI_2 = 1.5707963267948966192313216916397514_L+0.0iL; // pi / 2
    49 const long _Complex PI_4 = 0.7853981633974483096156608458198757_L+0.0iL; // pi / 4
    50 const long _Complex _1_PI = 0.3183098861837906715377675267450287_L+0.0iL; // 1 / pi
    51 const long _Complex _2_PI = 0.6366197723675813430755350534900574_L+0.0iL; // 2 / pi
    52 const long _Complex _2_SQRT_PI = 1.1283791670955125738961589031215452_L+0.0iL; // 2 / sqrt(pi)
     62extern const long _Complex PI;                                                  // pi
     63extern const long _Complex PI_2;                                                // pi / 2
     64extern const long _Complex PI_4;                                                // pi / 4
     65extern const long _Complex _1_PI;                                               // 1 / pi
     66extern const long _Complex _2_PI;                                               // 2 / pi
     67extern const long _Complex _2_SQRT_PI;                                  // 2 / sqrt(pi)
    5368
    54 const float E = 2.718281;                               // e
    55 const float LOG2_E = 1.442695;                          // log_2(e)
    56 const float LOG10_E = 0.4342944;                        // log_10(e)
    57 const float LN_2 = 0.6931471;                           // log_e(2)
    58 const float LN_10 = 2.302585;                           // log_e(10)
    59 const float SQRT_2 = 1.414213;                          // sqrt(2)
    60 const float _1_SQRT_2 = 0.7071067;                      // 1 / sqrt(2)
     69extern const float E;                                                                   // e
     70extern const float LOG2_E;                                                              // log_2(e)
     71extern const float LOG10_E;                                                             // log_10(e)
     72extern const float LN_2;                                                                // log_e(2)
     73extern const float LN_10;                                                               // log_e(10)
     74extern const float SQRT_2;                                                              // sqrt(2)
     75extern const float _1_SQRT_2;                                                   // 1 / sqrt(2)
    6176
    62 const double E = 2.7182818284590452354_D;               // e
    63 const double LOG2_E = 1.4426950408889634074_D;          // log_2(e)
    64 const double LOG10_E = 0.43429448190325182765_D;        // log_10(e)
    65 const double LN_2 = 0.69314718055994530942_D;           // log_e(2)
    66 const double LN_10 = 2.30258509299404568402_D;          // log_e(10)
    67 const double SQRT_2 = 1.41421356237309504880_D;         // sqrt(2)
    68 const double _1_SQRT_2 = 0.70710678118654752440_D;      // 1 / sqrt(2)
     77extern const double E;                                                                  // e
     78extern const double LOG2_E;                                                             // log_2(e)
     79extern const double LOG10_E;                                                    // log_10(e)
     80extern const double LN_2;                                                               // log_e(2)
     81extern const double LN_10;                                                              // log_e(10)
     82extern const double SQRT_2;                                                             // sqrt(2)
     83extern const double _1_SQRT_2;                                                  // 1 / sqrt(2)
    6984
    70 const long double E = 2.7182818284590452353602874713526625_DL; // e
    71 const long double LOG2_E = 1.4426950408889634073599246810018921_DL; // log_2(e)
    72 const long double LOG10_E = 0.4342944819032518276511289189166051_DL; // log_10(e)
    73 const long double LN_2 = 0.6931471805599453094172321214581766_DL; // log_e(2)
    74 const long double LN_10 = 2.3025850929940456840179914546843642_DL; // log_e(10)
    75 const long double SQRT_2 = 1.4142135623730950488016887242096981_DL; // sqrt(2)
    76 const long double _1_SQRT_2 = 0.7071067811865475244008443621048490_DL; // 1/sqrt(2)
     85extern const long double E;                                                             // e
     86extern const long double LOG2_E;                                                // log_2(e)
     87extern const long double LOG10_E;                                               // log_10(e)
     88extern const long double LN_2;                                                  // log_e(2)
     89extern const long double LN_10;                                                 // log_e(10)
     90extern const long double SQRT_2;                                                // sqrt(2)
     91extern const long double _1_SQRT_2;                                             // 1/sqrt(2)
    7792
    78 const _Complex E = 2.7182818284590452354_D+0.0_iD;      // e
    79 const _Complex LOG2_E = 1.4426950408889634074_D+0.0_iD; // log_2(e)
    80 const _Complex LOG10_E = 0.43429448190325182765_D+0.0_iD; // log_10(e)
    81 const _Complex LN_2 = 0.69314718055994530942_D+0.0_iD;  // log_e(2)
    82 const _Complex LN_10 = 2.30258509299404568402_D+0.0_iD; // log_e(10)
    83 const _Complex SQRT_2 = 1.41421356237309504880_D+0.0_iD;        // sqrt(2)
    84 const _Complex _1_SQRT_2 = 0.70710678118654752440_D+0.0_iD; // 1 / sqrt(2)
     93extern const _Complex E;                                                                // e
     94extern const _Complex LOG2_E;                                                   // log_2(e)
     95extern const _Complex LOG10_E;                                                  // log_10(e)
     96extern const _Complex LN_2;                                                             // log_e(2)
     97extern const _Complex LN_10;                                                    // log_e(10)
     98extern const _Complex SQRT_2;                                                   // sqrt(2)
     99extern const _Complex _1_SQRT_2;                                                // 1 / sqrt(2)
    85100
    86 const long _Complex E = 2.7182818284590452353602874713526625_L+0.0_iL; // e
    87 const long _Complex LOG2_E = 1.4426950408889634073599246810018921_L+0.0_iL; // log_2(e)
    88 const long _Complex LOG10_E = 0.4342944819032518276511289189166051_L+0.0_iL; // log_10(e)
    89 const long _Complex LN_2 = 0.6931471805599453094172321214581766_L+0.0_iL; // log_e(2)
    90 const long _Complex LN_10 = 2.3025850929940456840179914546843642_L+0.0_iL; // log_e(10)
    91 const long _Complex SQRT_2 = 1.4142135623730950488016887242096981_L+0.0_iL; // sqrt(2)
    92 const long _Complex _1_SQRT_2 = 0.7071067811865475244008443621048490_L+0.0_iL; // 1 / sqrt(2)
     101extern const long _Complex E;                                                   // e
     102extern const long _Complex LOG2_E;                                              // log_2(e)
     103extern const long _Complex LOG10_E;                                             // log_10(e)
     104extern const long _Complex LN_2;                                                // log_e(2)
     105extern const long _Complex LN_10;                                               // log_e(10)
     106extern const long _Complex SQRT_2;                                              // sqrt(2)
     107extern const long _Complex _1_SQRT_2;                                   // 1 / sqrt(2)
     108
     109// Local Variables: //
     110// mode: c //
     111// tab-width: 4 //
     112// End: //
  • src/libcfa/stdlib

    r3cfe27f r53ba273  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Mar 22 22:34:24 2016
    13 // Update Count     : 69
     12// Last Modified On : Fri Apr  1 22:26:14 2016
     13// Update Count     : 73
    1414//
    1515
     
    1818extern "C" {
    1919#include <stddef.h>                                                                             // size_t
     20#include <math.h>                                                                               // floor
    2021} // extern "C"
    2122
     
    8081char abs( char );
    8182extern "C" {
    82 int abs( int );         // use default C routine for int
    83 } // extern
     83int abs( int );                         // use default C routine for int
     84} // extern "C"
    8485long int abs( long int );
    8586long long int abs( long long int );
     
    9091double _Complex abs( double _Complex );
    9192long double _Complex abs( long double _Complex );
     93
     94//---------------------------------------
     95
     96float floor( float );
     97extern "C" {
     98double floor( double );         // use C routine for double
     99} // extern "C"
     100long double floor( long double );
     101
     102float ceil( float );
     103extern "C" {
     104double ceil( double );          // use C routine for double
     105} // extern "C"
     106long double ceil( long double );
    92107
    93108//---------------------------------------
  • src/libcfa/stdlib.c

    r3cfe27f r53ba273  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Mar 23 13:26:42 2016
    13 // Update Count     : 146
     12// Last Modified On : Wed Mar 30 10:48:41 2016
     13// Update Count     : 149
    1414//
    1515
     
    243243//---------------------------------------
    244244
     245float floor( float v ) { return floorf( v ); }
     246long double floor( long double v ) { return floorl( v ); }
     247
     248float ceil( float v ) { return ceilf( v ); }
     249long double ceil( long double v ) { return ceill( v ); }
     250
     251//---------------------------------------
     252
    245253void rand48seed( long int s ) { srand48( s ); }
    246254char rand48() { return mrand48(); }
Note: See TracChangeset for help on using the changeset viewer.