- Timestamp:
- Sep 17, 2025, 9:18:00 AM (3 days ago)
- Branches:
- master
- Children:
- 295ed2d1
- Parents:
- d2e6f84
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
rd2e6f84 r96aca388 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Mon Sep 15 21:14:57202514 %% Update Count : 72 2013 %% Last Modified On : Wed Sep 17 09:15:48 2025 14 %% Update Count : 7251 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 607 607 608 608 C, \CC, Java and other programming languages have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, using a routine like \Indexc{pow( x, y )} instead. 609 Ada, Haskell, Python and other programming languages often use operators ©^© or ©**© for exponentiation.609 Ada, Haskell, Python and other programming languages have an exponentiation operator often using operators ©^© or ©**©. 610 610 However, neither of these operators work in C as ©^© means exclusive-or and ©**© means double dereference. 611 611 Furthermore, using a routine for exponentiation does not match with mathematical expectation, \ie ©-x**-y© becomes ©pow( -x, -y )©. … … 882 882 \end{enumerate} 883 883 884 Before discussing potentiallanguage changes to deal with these problems, it is worth observing that in a typical C program:884 Before discussing language changes to deal with these problems, it is worth observing that in a typical C program: 885 885 \begin{itemize} 886 886 \item … … 902 902 \end{cfa} 903 903 still works. 904 Nevertheless, reversing the default action would have a non-trivial effect on case actions that compound, such as the above example of processing shellarguments.905 T herefore, to preserve backwards compatibility, it is necessary to introduce a new kind of ©switch© statement, called \Indexc{choose}, with no implicit fall-through semantics and an explicit fall-through if the last statement of a case-clause ends with the new keyword \Indexc{fallthrough}, \eg:904 Nevertheless, reversing the default action would have a non-trivial effect on case actions that compound, such as the above example of processing command-line arguments. 905 To preserve backwards compatibility, a new kind of ©switch© statement, called \Indexc{choose} is introduced, with no implicit fall-through semantics and an explicit fall-through if the last statement of a case-clause ends with the new keyword \Indexc{fallthrough}, \eg: 906 906 \begin{cfa} 907 907 ®choose® ( i ) { … … 931 931 \item 932 932 Dealing with unreachable code in a ©switch©/©choose© body is solved by restricting declarations and initialization to the start of statement body, which is executed \emph{before} the transfer to the appropriate ©case© clause\footnote{ 933 Essentially, these declarations are hoisted before the ©switch©/©choose© statement and both declarations and statement are surrounded by a compound statement.} and precluding statements before the first ©case© clause.933 These declarations are hoisted before the ©switch©/©choose© statement and both declarations and statement are surrounded by a compound statement.} and precluding statements before the first ©case© clause. 934 934 Further declarations at the same nesting level as the statement body are disallowed to ensure every transfer into the body is sound. 935 935 \begin{cfa} … … 1033 1033 The range character, ©'~'©, is decorated on the left and right to control how the set values are presented in the loop body. 1034 1034 The range character can be prefixed with ©'+'© or ©'-'© indicating the \emph{direction} the range is scanned, \ie from left to right (ascending) or right to left (descending). 1035 Ascending steppinguses operator \Indexc{+=};1036 descending steppinguses operator \Indexc{-=}.1035 Ascending uses operator \Indexc{+=}; 1036 descending uses operator \Indexc{-=}. 1037 1037 If there is no prefix character, it defaults to ©'+'©. 1038 1038 \begin{cfa} … … 1043 1043 For descending iteration, the ©L© and ©H© values are \emph{implicitly} switched, and the increment/decrement for ©S© is toggled. 1044 1044 Hence, the order of values in a set may not be the order the values are presented during looping. 1045 When changing the iteration direction, this form is faster and safer, \ie the direction prefix can be added/removed without changing existing (correct) program text.1045 Changing the iteration direction is faster and safer because the direction prefix can be added/removed without changing existing (correct) range information. 1046 1046 \R{Warning}: reversing the range endpoints for descending order results in an empty set. 1047 1047 \begin{cfa} … … 1286 1286 1287 1287 1288 1288 1289 \end{cfa} 1289 1290 & 1290 1291 \begin{cfa} 1291 1292 int main() { 1293 sout | nlOff; 1292 1294 for ( S i = 0; i < (S){10,10}; i += 1 ) { sout | i; } sout | "A" | nl; // C 1293 1295 for ( S i; 0 ~ (S){10,10} ) { sout | i; } sout | "B" | nl; // CFA … … 1424 1426 The following example is a linear search for the key 3 in an array, where finding the key is handled with a ©break© and not finding with the ©else© clause on the loop construct. 1425 1427 \begin{cquote} 1426 \begin{cfa} 1427 int a[10]; 1428 \end{cfa} 1429 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{3em}}l@{}} 1428 \begin{tabular}{@{}lll@{}} 1429 \multicolumn{2}{@{}l@{}}{\lstinline{int a[10]}} \\ 1430 1430 \begin{cfa} 1431 1431 … … 2704 2704 \begin{tabular}{@{}l|ll|l@{}} 2705 2705 \multicolumn{2}{@{}c}{\textbf{length}} & \multicolumn{2}{c@{}}{\textbf{pattern}} \\ 2706 \multicolumn{4}{@{}l}{\lstinline{string name = "PETER"} } \\2706 \multicolumn{4}{@{}l}{\lstinline{string name = "PETER"};} \\ 2707 2707 \begin{cfa} 2708 2708 s = name( 0, 4 ); … … 2762 2762 \begin{cquote} 2763 2763 \begin{tabular}{@{}l|l@{}} 2764 \multicolumn{2}{@{}l}{\lstinline{string digit = "0123456789"} } \\2764 \multicolumn{2}{@{}l}{\lstinline{string digit = "0123456789"};} \\ 2765 2765 \begin{cfa}[escapechar={}] 2766 2766 digit( 3, 3 ) = ""; … … 2819 2819 \begin{cquote} 2820 2820 \begin{tabular}{@{}l|l@{}} 2821 \multicolumn{2}{@{}l}{\lstinline{string digit = "0123456789" }} \\2821 \multicolumn{2}{@{}l}{\lstinline{string digit = "0123456789";}} \\ 2822 2822 \begin{cfa} 2823 2823 i = find( digit, '3' ); … … 2837 2837 \begin{cquote} 2838 2838 \begin{tabular}{@{}l|l@{}} 2839 \ begin{cfa}2840 charclass vowels{ "aeiouy" }; 2839 \multicolumn{2}{@{}l}{\lstinline{charclass vowels\{ "aeiouy" \};}} \\ 2840 \begin{cfa} 2841 2841 i = include( "aaeiuyoo", vowels ); 2842 2842 i = include( "aabiuyoo", vowels ); … … 2844 2844 & 2845 2845 \begin{cfa} 2846 2847 2846 8 // compliant 2848 2847 2 // b non-compliant … … 2995 2994 \subsection{C Compatibility} 2996 2995 2997 To ease conversion from C to \CFA, \CFA provides companion C @string@functions.2998 Hence, it is possible to convert a block of C string operations to \CFA strings just by changing the type @char *@ to @string@.2996 To ease conversion from C to \CFA, \CFA provides companion C ©string© functions. 2997 Hence, it is possible to convert a block of C string operations to \CFA strings just by changing the type ©char *© to ©string©. 2999 2998 \begin{cquote} 3000 2999 \begin{tabular}{@{}ll@{}} … … 3016 3015 \end{tabular} 3017 3016 \end{cquote} 3018 However, the conversion fails with I/O because @printf@ cannot print a @string@ using format code @%s@ because\CFA strings are not null terminated.3017 However, the conversion fails with I/O because ©printf© cannot print a ©string© using format code ©%s© as \CFA strings are not null terminated. 3019 3018 Nevertheless, this capability does provide a useful starting point for conversion to safer \CFA strings. 3020 3019 … … 3031 3030 \begin{cquote} 3032 3031 \begin{tabular}{@{}l|l@{}} 3032 \multicolumn{2}{@{}l}{\lstinline{string s = "abc";}} \\ 3033 3033 \begin{C++} 3034 string s = "abc";3035 3034 cout << setw(10) << left << setfill( 'x' ) << s << endl; 3036 3035 \end{C++} 3037 3036 & 3038 3037 \begin{C++} 3039 3040 3038 "abcxxxxxxx" 3041 3039 \end{C++} … … 3048 3046 \begin{cquote} 3049 3047 \begin{tabular}{@{}l|l@{}} 3050 \ begin{cfa}3051 string s = "abc"; 3048 \multicolumn{2}{@{}l}{\lstinline{string s = "abc";}} \\ 3049 \begin{cfa} 3052 3050 sout | bin( s ) | nl 3053 3051 | oct( s ) | nl … … 3059 3057 & 3060 3058 \begin{cfa} 3061 3062 3059 "0b1100001 0b1100010 0b1100011" 3063 3060 "0141 0142 0143" … … 3076 3073 \begin{cquote} 3077 3074 \begin{tabular}{@{}l|l@{}} 3075 \multicolumn{2}{@{}l}{\lstinline{char ch, c[10];}} \\ 3076 \multicolumn{2}{@{}l}{\lstinline{string s;}} \\ 3078 3077 \begin{C++} 3079 char ch, c[10];3080 string s;3081 3078 cin >> ch >> setw( 5 ) >> c >> s; 3082 3079 ®abcde fg® … … 3084 3081 & 3085 3082 \begin{C++} 3086 3087 3088 3083 'a' "bcde" "fg" 3089 3084 … … 3102 3097 \setlength{\tabcolsep}{10pt} 3103 3098 \begin{tabular}{@{}l|l@{}} 3099 \multicolumn{2}{@{}l}{\lstinline{char ch, c[10];}} \\ 3100 \multicolumn{2}{@{}l}{\lstinline{string s;}} \\ 3104 3101 \begin{C++} 3105 char ch, c[10];3106 string s;3107 3102 sin | ch | wdi( 5, c ) | s; 3108 3103 ®abcde fg®
Note:
See TracChangeset
for help on using the changeset viewer.