 Timestamp:
 Apr 6, 2016, 10:08:32 PM (8 years ago)
 Branches:
 ADT, aaronthesis, armeh, astexperimental, cleanupdtors, ctor, deferred_resn, demangler, enum, forallpointerdecay, gc_noraii, jacob/cs343translation, jenkinssandbox, master, memory, newast, newastuniqueexpr, newenv, no_list, persistentindexer, pthreademulation, qualifiedEnum, resolvnew, string, with_gc
 Children:
 3d9b5da, 78885b5
 Parents:
 3cfe27f
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

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 1 17 % requires tex packages: texlivebase texlivelatexbase texcommon texlivehumanities texlivelatexextra texlivefontsrecommended 2 18 … … 5 21 6 22 % Latex packages used in the document. 7 23 \usepackage[T1]{fontenc} 24 \usepackage{textcomp} 25 \usepackage[latin1]{inputenc} 26 \usepackage{upquote} 8 27 \usepackage{fullpage,times} 9 28 \usepackage{xspace} … … 24 43 % Names used in the document. 25 44 26 \newcommand{\CFA}{C$\ forall$\xspace} % set language symbolic name45 \newcommand{\CFA}{C$\mathbf\forall$\xspace} % set language symbolic name 27 46 \newcommand{\CFL}{Cforall\xspace} % set language text name 28 47 \newcommand{\CC}{C\kern.1em\hbox{+\kern.25em+}\xspace} % CC symbolic name … … 160 179 % CFA based on ANSI C 161 180 \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}]{`}{`}, 165 186 }% 166 187 … … 176 197 showstringspaces=false, 177 198 showlines=true, 199 aboveskip=6pt, 200 belowskip=4pt, 201 literate={\\`}{\raisebox{0.3ex}{\ttfamily\upshape \hspace*{2pt}`}}1, % escape \`, otherwise used to highlight in red 202 %extendedchars=true, 178 203 }% 179 204 … … 181 206 % replace/adjust listings characters that look bad in sanserif 182 207 \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 184 211 \lst@ProcessOther{"3C}{\lst@ttfamily{<}{\texttt{<}}} % replace less than 185 212 \lst@ProcessOther{"3E}{\lst@ttfamily{<}{\texttt{>}}} % replace greater than 186 213 \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 188 216 \lst@ProcessOther{"7E}{\raisebox{0.3ex}{$\scriptstyle\sim\,$}} % replace tilde 189 217 %\lst@ProcessOther{"7E}{\raisebox{.4ex}[1ex][0pt]{\textasciitilde}} % lower tilde … … 366 394 367 395 The command \lstinline@cfa@ is used to compile \CFA program(s). 368 This command works like the GNU \lstinline@gcc@ command, e.g.:396 This command works like the GNU \lstinline@gcc@\index{gcc} command, e.g.: 369 397 \begin{lstlisting} 370 398 cfa [ gccoptions ] C/@{\CFA}@files [ assembler/loaderfiles ] 371 399 \end{lstlisting} 372 The following additional option is available: 400 By default, \CFA programs having the following \lstinline@gcc@ flags turned on: 401 \begin{description} 402 \item 403 \hspace*{4pt}\lstinline@std=gnu99@ 404 The 1999 C standard plus GNU extensions. 405 \end{description} 406 The following new \CFA option is available: 373 407 \begin{description} 374 408 \item … … 382 416 Numeric constants are extended to allow \Index{underscore}s within constants\index{constant!underscore}, e.g.: 383 417 \begin{lstlisting} 384 2 _147_483_648; // decimal constant418 2`_`147`_`483`_`648; // decimal constant 385 419 56_ul; // decimal unsigned long constant 386 420 0_377; // octal constant … … 451 485 \multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 452 486 \begin{lstlisting} 453 * int x, y; 487 `* int x, y;` 454 488 \end{lstlisting} 455 489 & … … 571 605 The point of the new syntax is to allow returning multiple values from a routine~\cite{CLU,Galletly96}, e.g.: 572 606 \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 ) { 574 608 @\emph{routine body}@ 575 609 } … … 639 673 Because 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: 640 674 \begin{lstlisting} 641 [ int x ]f() {675 `[ int x ]` f() { 642 676 ... x = 0; ... x = y; ... 643 return;// implicitly return x677 `return;` // implicitly return x 644 678 } 645 679 \end{lstlisting} … … 697 731 for example, the following is incorrect: 698 732 \begin{lstlisting} 699 * [ int x ] f () fp; // routine name ``f''is not allowed733 * [ int x ] f () fp; // routine name "f" is not allowed 700 734 \end{lstlisting} 701 735 … … 864 898 \subsection{Type Nesting} 865 899 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. 867 901 \begin{quote2} 868 902 \begin{tabular}{@{}l@{\hspace{30pt}}ll@{}} … … 919 953 920 954 int 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; 925 959 } 926 960 \end{lstlisting} 927 961 \end{tabular} 928 962 \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 scoperesolution 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. 963 In 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. 964 In the right example in \CFA, the types are not hoisted and accessed using the fieldselection operator ``\lstinline@.@'' for type qualification, as does Java, rather than the \CC typeselection operator ``\lstinline@::@''. 933 965 934 966 … … 944 976 \begin{lstlisting} 945 977 const 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} 978 int ia[size]; 979 ... // assign values to array ia 980 qsort( 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 987 Nested routines are not firstclass, meaning a nested routine cannot be returned if it has references to variables in its enclosing blocks; 988 the only exception is references to the external block of the translation unit, as these variables persist for the duration of the program. 989 The 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 } 999 int main() { 1000 * [int](int) fp = foo(); // int (*fp)(int) 1001 sout  fp( 3 )  endl; 1002 } 1003 \end{lstlisting} 1004 because 1005 1006 Currently, there are no \Index{lambda} expressions, i.e., unnamed routines because routine names are very important to properly select the correct routine. 954 1007 955 1008 … … 1013 1066 1014 1067 \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 welldefined 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. 1068 Change: A struct is a scope in C++, not in C \\ 1069 Rationale: Class scope is crucial to C++, and a struct is a class. \\ 1070 Effect on original feature: Change to semantics of welldefined feature. \\ 1071 Difficulty of converting: Semantic transformation. \\ 1072 How 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. 1073 The latter is probably rare. 1074 1075 \CFA is C \emph{incompatible} on this issue, and provides semantics similar to \CC. 1076 Nested types are not hoisted and can be referenced using the field selection operator ``\lstinline@.@'', unlike the \CC scoperesolution operator ``\lstinline@::@''. 1077 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. 1078 1022 1079 1023 1080 \item … … 1185 1242 First the righthand tuple is flattened and then the values are assigned individually. 1186 1243 Flattening 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 ]@.1244 For example, the type \lstinline@[ int, [ int, int ], int ]@ can be coerced, using flattening, into the type \lstinline@[ int, int, int, int ]@. 1188 1245 1189 1246 A \newterm{structuring coercion} is the opposite of flattening; … … 1352 1409 \multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1353 1410 \begin{lstlisting} 1354 L1:for ( ... ) {1355 L2:for ( ... ) {1356 L3:for ( ... ) {1357 ... break L1; ...1358 ... break L2; ...1359 ... break L3; // or break1411 `L1:` for ( ... ) { 1412 `L2:` for ( ... ) { 1413 `L3:` for ( ... ) { 1414 ... break `L1`; ... 1415 ... break `L2`; ... 1416 ... break `L3`; // or break 1360 1417 } 1361 1418 } … … 1382 1439 \multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1383 1440 \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`; ... 1390 1447 1391 1448 } … … 1623 1680 \begin{lstlisting} 1624 1681 switch ( i ) { 1625 case 1, 3, 5:1682 `case 1, 3, 5`: 1626 1683 ... 1627 case 2, 4, 6:1684 `case 2, 4, 6`: 1628 1685 ... 1629 1686 } … … 1634 1691 case 1: case 3 : case 5: 1635 1692 ... 1636 case 2: case 4 : case 6: /* even values */1693 case 2: case 4 : case 6: 1637 1694 ... 1638 1695 } … … 1655 1712 \begin{lstlisting} 1656 1713 switch ( i ) { 1657 case 1~51714 `case 1~5:` 1658 1715 ... 1659 case 10~151716 `case 10~15:` 1660 1717 ... 1661 1718 } … … 1672 1729 & 1673 1730 \begin{lstlisting} 1731 1674 1732 // 1, 2, 3, 4, 5 1675 1733 … … 2168 2226 2169 2227 2170 \section{Generics } 2228 \section{Auto TypeInferencing} 2229 2230 Auto typeinferencing 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 2236 auto j = 3.0 * 4; 2237 int i; 2238 auto k = i; 2239 \end{lstlisting} 2240 & 2241 \begin{lstlisting} 2242 #define expr 3.0 * i 2243 typeof(expr) j = expr; 2244 int i; 2245 typeof(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} 2256 The two important capabilities are: 2257 \begin{itemize} 2258 \item 2259 preventing having to determine or write out long generic types, 2260 \item 2261 ensure secondary variables, related to a primary variable, always have the same type. 2262 \end{itemize} 2263 2264 In \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 lefthand side of assignment for type inferencing, so in many cases it is crucial to specify the type of the lefthand side to select the correct type of the righthand expression. 2267 Only for overloaded routines with the same return type is variable typeinferencing possible. 2268 Finally, \lstinline@auto@ presents the programming problem of tracking down a type when the type is actually needed. 2269 For example, given 2270 \begin{lstlisting} 2271 auto j = `...` 2272 \end{lstlisting} 2273 and the need to write a routine to compute using \lstinline@j@ 2274 \begin{lstlisting} 2275 void rtn( `...` parm ); 2276 rtn( j ); 2277 \end{lstlisting} 2278 A programmer must work backwards to determine the type of \lstinline@j@'s initialization expression, reconstructing the possibly long generic typename. 2279 In this situation, having the type name or a short alias is very useful. 2280 2281 There is also the conundrum in type inferencing of when to \emph{\Index{brand}} a type. 2282 That is, when is the type of the variable more important than the type of its initialization expression. 2283 For example, if a change is made in an initialization expression, it can cause hundreds or thousands of cascading type changes and/or errors. 2284 At some point, a programmer wants the type of the variable to remain constant and the expression to be in error when it changes. 2285 2286 Given \lstinline@typedef@ and \lstinline@typeof@ in \CFA, and the strong need to use the type of lefthand side in inferencing, auto typeinferencing is not supported at this time. 2287 Should a significant need arise, this feature can be revisited. 2288 2289 2290 \section{Generics} 2171 2291 2172 2292 \CFA supports parametric polymorphism to allow users to define generic functions and types. … … 2457 2577 2458 2578 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 logicalor 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 logicalor operator has a link with the Shell pipeoperator 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.12.3 1.12.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.12.3i, $1.12.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 standardlibrary is to wrap many of the existing C libraryroutines 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 2581 The number 0 and 1 are treated specially in \CFA, and can be redefined as variables. 2582 One syntactic anomaly is when a field in an structure is names 0 or 1: 2583 \begin{lstlisting} 2584 struct S { 2585 int 0, 1; 2586 } s; 2587 \end{lstlisting} 2588 The problem occurs in accesing these fields using the selection operation ``\lstinline@.@'': 2589 \begin{lstlisting} 2590 s.0 = 0; // ambiguity with floating constant .0 2591 s.1 = 1; // ambiguity with floating constant .1 2592 \end{lstlisting} 2593 To 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} 2598 While this sytact is awkward, it is unlikely many programers will name fields of a structure 0 or 1. 2599 Like the \CC lexical problem with closing templatesyntax, e.g, \lstinline@Foo<Bar<int`>>`@, this issue can be solved with a more powerful lexer/parser. 2600 2601 There are several ambiguous cases with operator identifiers, e.g., \lstinline@int *?*?()@, where the string \lstinline@*?*?@ can be lexed as \lstinline@*@/\lstinline@?*?@ or \lstinline@*?@/\lstinline@*?@. 2602 Since 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. 2603 Even with this special hack, there are 5 general cases that cannot be handled. 2604 The first case is for the functioncall identifier \lstinline@?()@: 2605 \begin{lstlisting} 2606 int *@\textvisiblespace@?()(); // declaration: space required after '*' 2607 *@\textvisiblespace@?()(); // expression: space required after '*' 2608 \end{lstlisting} 2609 Without the space, the string \lstinline@*?()@ is ambiguous without N character look ahead; 2610 it requires scanning ahead to determine if there is a \lstinline@'('@, which is the start of an argument/parameter list. 2611 2612 The 4 remaining cases occur in expressions: 2613 \begin{lstlisting} 2614 i++@\textvisiblespace@?i:0; // space required before '?' 2615 i@\textvisiblespace@?i:0; // space required before '?' 2616 i@\textvisiblespace@?++i:0; // space required after '?' 2617 i@\textvisiblespace@?i:0; // space required after '?' 2618 \end{lstlisting} 2619 In the first two cases, the string \lstinline@i++?@ is ambiguous, where this string can be lexed as \lstinline@i@ / \lstinline@++?@ or \lstinline@i++@ / \lstinline@?@; 2620 it requires scanning ahead to determine if there is a \lstinline@'('@, which is the start of an argument list. 2621 In 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@; 2622 it requires scanning ahead to determine if there is a \lstinline@'('@, which is the start of an argument list. 2654 2623 2655 2624 … … 2676 2645 2677 2646 task creates a type with implicit locking, separate stack, and a thread 2647 2678 2648 2679 2649 \subsection{Monitors} … … 3775 3745 \multicolumn{1}{c}{\textbf{\CFA/\CC}} & \multicolumn{1}{c}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}} \\ 3776 3746 \hline 3777 \begin{lstlisting} 3747 \begin{lstlisting}[boxpos=t] 3778 3748 extern "C" { 3779 3749 #include <sys/types.h> … … 3782 3752 } 3783 3753 size_t fileSize( const char *path ) { 3784 st at s;3754 struct stat s; 3785 3755 stat(path, &s); 3786 3756 return s.st_size; … … 3788 3758 \end{lstlisting} 3789 3759 & 3790 \begin{lstlisting} 3760 \begin{lstlisting}[boxpos=t] 3791 3761 /* 3792 3762 #cgo … … 3807 3777 \end{lstlisting} 3808 3778 & 3809 \begin{lstlisting} 3779 \begin{lstlisting}[boxpos=t] 3810 3780 use libc::{c_int, size_t}; 3811 3812 // The following declarations are3813 3781 // translated from sys/stat.h 3814 3782 #[repr(C)] … … 3818 3786 ... 3819 3787 } 3820 3821 3788 #[link(name = "libc")] 3822 3789 extern { … … 3824 3791 buf: *mut stat_t) > c_int; 3825 3792 } 3826 3827 3793 fn fileSize(path: *const u8) > size_t 3828 3794 { 3829 3795 unsafe { 3830 let mut buf: stat_t = uninit();3831 stat(path, &mut buf);3832 buf.st_size3796 let mut buf: stat_t = uninit(); 3797 stat(path, &mut buf); 3798 buf.st_size 3833 3799 } 3834 3800 } … … 3953 3919 3954 3920 3921 \begin{comment} 3955 3922 \subsubsection{Modules/Packages} 3956 3923 … … 4032 3999 } 4033 4000 \end{lstlisting} 4001 \end{comment} 4002 4034 4003 4035 4004 \subsubsection{Parallel Tasks} … … 4187 4156 \end{flushleft} 4188 4157 4158 \lstset{basicstyle=\sf\relsize{1}} 4159 4160 4189 4161 \subsection{Summary of Language Comparison} 4190 4162 … … 4253 4225 D does not have any builtin concurrency constructs in the 4254 4226 language, though it does have a standard library for concurrency which includes the lowlevel primitives for concurrency. 4227 4228 4229 \appendix 4230 4231 4232 \section{I/O Library} 4233 \label{s:IOLibrary} 4234 \index{input/output library} 4235 4236 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. 4237 The 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} 4242 int x = 0, y = 1, z = 2; 4243 `sout` `` x `` y `` z ` endl`; 4244 \end{lstlisting} 4245 & 4246 \begin{lstlisting} 4247 4248 cout << x << " " << y << " " << z << endl; 4249 \end{lstlisting} 4250 \end{tabular} 4251 \end{quote2} 4252 The \CFA form is half as many characters, and is similar to Python I/O with respect to implicit separators. 4253 4254 The logicalor operator is used because it is the lowestpriority overloadable operator, other than assignment. 4255 Therefore, fewer output expressions require parenthesis. 4256 \begin{quote2} 4257 \begin{tabular}{@{}ll@{}} 4258 \textbf{\CFA:} 4259 & 4260 \begin{lstlisting} 4261 sout  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} 4267 cout << 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} 4271 Finally, the logicalor operator has a link with the Shell pipeoperator for moving data, although data flows in the opposite direction. 4272 4273 The implicit seperator\index{I/O separator} character (space/blank) is a separator not a terminator. 4274 The rules for implicitly adding the separator are: 4275 \begin{enumerate} 4276 \item 4277 A seperator does not appear at the start or end of a line. 4278 \begin{lstlisting}[belowskip=0pt] 4279 sout 1  2  3  endl; 4280 \end{lstlisting} 4281 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4282 1 2 3 4283 \end{lstlisting} 4284 \item 4285 A seperator does not appear before or after a character literal or variable. 4286 \begin{lstlisting} 4287 sout  '1'  '2'  '3'  endl; 4288 123 4289 \end{lstlisting} 4290 \item 4291 A seperator does not appear before or after a null (empty) C string 4292 \begin{lstlisting} 4293 sout  1  ""  2  ""  3  endl; 4294 123 4295 \end{lstlisting} 4296 which is a local mechanism to disable insertion of the separator character. 4297 \item 4298 A 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] 4301 sout  "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] 4305 x (1 x [2 x {3 x $4 x £5 x ¥6 x ¿7 x «8 4306 \end{lstlisting} 4307 %$ 4308 \item 4309 A seperator does not appear after a C string ending with the extended ASCII characters: \lstinline@,.:;!?)]}%¢»@ 4310 \begin{lstlisting}[belowskip=0pt] 4311 sout  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] 4315 1, 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 4318 A 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] 4320 sout  "x '"  1  "' x \`"  2  "\` x \""  3  "\" x"  endl; 4321 \end{lstlisting} 4322 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4323 x '1' x \`2\` x "3" x 4324 \end{lstlisting} 4325 \begin{lstlisting}[showtabs=true,aboveskip=0pt] 4326 sout  "x\t"  1  "\tx"  endl; 4327 x 1 x 4328 \end{lstlisting} 4329 \end{enumerate} 4330 The following \CCstyle \Index{manipulator}s allow further control over implicit seperation. 4331 \begin{lstlisting}[mathescape=off,belowskip=0pt] 4332 sout  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] 4338 sout  1  sepOff  2  3  endl; // turn off implicit separator temporarily 4339 \end{lstlisting} 4340 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4341 12 3 4342 \end{lstlisting} 4343 \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt] 4344 sout  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] 4347 123 4348 \end{lstlisting} 4349 \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt] 4350 sout  1  sepOn  2  3  endl; // turn on implicit separator temporarily 4351 \end{lstlisting} 4352 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4353 1 23 4354 \end{lstlisting} 4355 \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt] 4356 sout  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] 4359 1 2 3 4360 \end{lstlisting} 4361 \begin{lstlisting}[mathescape=off,aboveskip=0pt,aboveskip=0pt,belowskip=0pt] 4362 sepSet( sout, ", $" ); // change separator from " " to ", $" 4363 sout  1  2  3  endl; 4364 \end{lstlisting} 4365 %$ 4366 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt] 4367 1, $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 4376 int 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 // floatingpoint 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 4420 A 1 2 3 4 5 6 7 8 1.1 1.2 1.3 1.1+2.3 1.12.3 1.12.3 abc xyz 4421 $ a.out 4422 A 4423 1 2 3 4 5 6 7 8 4424 1.1 1.2 1.3 4425 1.1+2.3i 1.12.3i 1.12.3i 4426 4427 1.11.21.3 4428 1.1+2.3i1.12.3i1.12.3i 4429 abcxyz 4430 abcxyz 4431 4432 1.1, $1.2, $1.3 4433 1.1+2.3i, $1.12.3i, $1.12.3i 4434 abc, $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 4444 The goal of the \CFA standardlibrary is to wrap many of the existing C libraryroutines that are explicitly polymorphic into implicitly polymorphic versions. 4445 4446 4447 \subsection{malloc} 4448 4449 \begin{lstlisting} 4450 forall( otype T ) T * malloc( void ); 4451 forall( otype T ) T * malloc( char fill ); 4452 forall( otype T ) T * malloc( T * ptr, size_t size ); 4453 forall( otype T ) T * malloc( T * ptr, size_t size, unsigned char fill ); 4454 forall( otype T ) T * calloc( size_t size ); 4455 forall( otype T ) T * realloc( T * ptr, size_t size ); 4456 forall( otype T ) T * realloc( T * ptr, size_t size, unsigned char fill ); 4457 4458 forall( otype T ) T * aligned_alloc( size_t alignment ); 4459 forall( otype T ) T * memalign( size_t alignment ); // deprecated 4460 forall( otype T ) int posix_memalign( T ** ptr, size_t alignment ); 4461 4462 forall( otype T ) T * memset( T * ptr, unsigned char fill ); // use default value '\0' for fill 4463 forall( otype T ) T * memset( T * ptr ); // remove when default value available 4464 \end{lstlisting} 4465 4466 4467 \subsection{ato/strto} 4468 4469 \begin{lstlisting} 4470 int ato( const char * ptr ); 4471 unsigned int ato( const char * ptr ); 4472 long int ato( const char * ptr ); 4473 unsigned long int ato( const char * ptr ); 4474 long long int ato( const char * ptr ); 4475 unsigned long long int ato( const char * ptr ); 4476 float ato( const char * ptr ); 4477 double ato( const char * ptr ); 4478 long double ato( const char * ptr ); 4479 float _Complex ato( const char * ptr ); 4480 double _Complex ato( const char * ptr ); 4481 long double _Complex ato( const char * ptr ); 4482 4483 int strto( const char * sptr, char ** eptr, int base ); 4484 unsigned int strto( const char * sptr, char ** eptr, int base ); 4485 long int strto( const char * sptr, char ** eptr, int base ); 4486 unsigned long int strto( const char * sptr, char ** eptr, int base ); 4487 long long int strto( const char * sptr, char ** eptr, int base ); 4488 unsigned long long int strto( const char * sptr, char ** eptr, int base ); 4489 float strto( const char * sptr, char ** eptr ); 4490 double strto( const char * sptr, char ** eptr ); 4491 long double strto( const char * sptr, char ** eptr ); 4492 float _Complex strto( const char * sptr, char ** eptr ); 4493 double _Complex strto( const char * sptr, char ** eptr ); 4494 long double _Complex strto( const char * sptr, char ** eptr ); 4495 \end{lstlisting} 4496 4497 4498 \subsection{bsearch/qsort} 4499 4500 \begin{lstlisting} 4501 forall( otype T  { int ?<?( T, T ); } ) 4502 T * bsearch( const T key, const T * arr, size_t dimension ); 4503 4504 forall( otype T  { int ?<?( T, T ); } ) 4505 void qsort( const T * arr, size_t dimension ); 4506 \end{lstlisting} 4507 4508 4509 \subsection{abs} 4510 4511 \begin{lstlisting} 4512 char abs( char ); 4513 extern "C" { 4514 int abs( int ); // use default C routine for int 4515 } // extern "C" 4516 long int abs( long int ); 4517 long long int abs( long long int ); 4518 float abs( float ); 4519 double abs( double ); 4520 long double abs( long double ); 4521 float _Complex abs( float _Complex ); 4522 double _Complex abs( double _Complex ); 4523 long double _Complex abs( long double _Complex ); 4524 \end{lstlisting} 4525 4526 4527 \subsection{floor/ceil} 4528 4529 \begin{lstlisting} 4530 float floor( float ); 4531 extern "C" { 4532 double floor( double ); // use C routine for double 4533 } // extern "C" 4534 long double floor( long double ); 4535 4536 float ceil( float ); 4537 extern "C" { 4538 double ceil( double ); // use C routine for double 4539 } // extern "C" 4540 long double ceil( long double ); 4541 \end{lstlisting} 4542 4543 4544 \subsection{random} 4545 4546 \begin{lstlisting} 4547 void rand48seed( long int s ); 4548 char rand48(); 4549 int rand48(); 4550 unsigned int rand48(); 4551 long int rand48(); 4552 unsigned long int rand48(); 4553 float rand48(); 4554 double rand48(); 4555 float _Complex rand48(); 4556 double _Complex rand48(); 4557 long double _Complex rand48(); 4558 \end{lstlisting} 4559 4560 4561 \subsection{min/max/swap} 4562 4563 \begin{lstlisting} 4564 forall( otype T  { int ?<?( T, T ); } ) 4565 T min( const T t1, const T t2 ); 4566 4567 forall( otype T  { int ?>?( T, T ); } ) 4568 T max( const T t1, const T t2 ); 4569 4570 forall( otype T ) 4571 void swap( T * t1, T * t2 ); 4572 \end{lstlisting} 4255 4573 4256 4574
Note: See TracChangeset
for help on using the changeset viewer.