Changeset e6e3445


Ignore:
Timestamp:
Feb 13, 2018, 9:36:18 AM (4 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
c4d4ecf
Parents:
ff2d1139
Message:

revise switch-statement section

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/general/Paper.tex

    rff2d1139 re6e3445  
    103103
    104104\newenvironment{cquote}{%
    105         \list{}{\lstset{resetmargins=true,aboveskip=0pt,belowskip=0pt}\topsep=4pt\parsep=0pt\leftmargin=\parindent\rightmargin\leftmargin}%
     105        \list{}{\lstset{resetmargins=true,aboveskip=0pt,belowskip=0pt}\topsep=4pt\parsep=0pt\leftmargin=\parindentlnth\rightmargin\leftmargin}%
    106106        \item\relax
    107107}{%
     
    193193The TIOBE~\cite{TIOBE} ranks the top 5 most popular programming languages as: Java 16\%, \Textbf{C 7\%}, \Textbf{\CC 5\%}, \Csharp 4\%, Python 4\% = 36\%, where the next 50 languages are less than 3\% each with a long tail.
    194194The top 3 rankings over the past 30 years are:
    195 \lstDeleteShortInline@%
    196195\begin{center}
    197196\setlength{\tabcolsep}{10pt}
     197\lstDeleteShortInline@%
    198198\begin{tabular}{@{}rccccccc@{}}
    199199                & 2017  & 2012  & 2007  & 2002  & 1997  & 1992  & 1987          \\ \hline
     
    202202\CC             & 3             & 3             & 3             & 3             & 2             & 2             & 4                     \\
    203203\end{tabular}
     204\lstMakeShortInline@%
    204205\end{center}
    205 \lstMakeShortInline@%
    206206Love it or hate it, C is extremely popular, highly used, and one of the few systems languages.
    207207In many cases, \CC is often used solely as a better C.
     
    257257Crucial to the design of a new programming language are the libraries to access thousands of external software features.
    258258Like \CC, \CFA inherits a massive compatible library-base, where other programming languages must rewrite or provide fragile inter-language communication with C.
    259 A simple example is leveraging the existing type-unsafe (@void *@) C @bsearch@ to binary search a sorted floating-point array:
     259A simple example is leveraging the existing type-unsafe (@void *@) C @bsearch@ to binary search a sorted float array:
    260260\begin{lstlisting}
    261261void * bsearch( const void * key, const void * base, size_t nmemb, size_t size,
     
    263263int comp( const void * t1, const void * t2 ) { return *(double *)t1 < *(double *)t2 ? -1 :
    264264                                *(double *)t2 < *(double *)t1 ? 1 : 0; }
    265 double key = 5.0, vals[10] = { /* 10 sorted floating-point values */ };
     265double key = 5.0, vals[10] = { /* 10 sorted float values */ };
    266266double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp );      $\C{// search sorted array}$
    267267\end{lstlisting}
     
    505505In many languages, functions can return at most one value;
    506506however, many operations have multiple outcomes, some exceptional.
    507 Consider C's @div@ and @remquo@ functions, which return the quotient and remainder for a division of integer and floating-point values, respectively.
     507Consider C's @div@ and @remquo@ functions, which return the quotient and remainder for a division of integer and float values, respectively.
    508508\begin{lstlisting}
    509509typedef struct { int quo, rem; } div_t;         $\C{// from include stdlib.h}$
     
    936936
    937937\section{Control Structures}
     938
     939\CFA identifies missing and problematic control structures in C, and extends and modifies these control structures to increase functionality and safety.
    938940
    939941
     
    10441046The implicit targets of the current @continue@ and @break@, \ie the closest enclosing loop or @switch@, change as certain constructs are added or removed.
    10451047
     1048
    10461049\subsection{\texorpdfstring{Enhanced \LstKeywordStyle{switch} Statement}{Enhanced switch Statement}}
    10471050
    1048 \CFA also fixes a number of ergonomic defecits in the @switch@ statements of standard C.
    1049 C can specify a number of equivalent cases by using the default ``fall-through'' semantics of @case@ clauses, \eg @case 1: case 2: case 3:@ -- this syntax is cluttered, however, so \CFA includes a more concise list syntax, @case 1, 2, 3:@.
    1050 For contiguous ranges, \CFA provides an even more concise range syntax as well, @case 1~3:@; lists of ranges are also allowed in case selectors.
    1051 
     1051There are a number of deficiencies with the C @switch@ statements: enumerating @case@ lists, placement of @case@ clauses, scope of the switch body, and fall through between case clauses.
     1052
     1053C has no shorthand for specifying a list of case values, whether the list is non-contiguous or contiguous\footnote{C provides this mechanism via fall through.}.
     1054\CFA provides a shorthand for a non-contiguous list:
     1055\begin{cquote}
     1056\lstDeleteShortInline@%
     1057\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
     1058\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
     1059\begin{cfa}
     1060case 2, 10, 34, 42:
     1061\end{cfa}
     1062&
     1063\begin{cfa}
     1064case 2: case 10: case 34: case 42:
     1065\end{cfa}
     1066\end{tabular}
     1067\lstMakeShortInline@%
     1068\end{cquote}
     1069for a contiguous list:\footnote{gcc provides the same mechanism with awkward syntax, \lstinline@2 ... 42@, where spaces are required around the ellipse.}
     1070\begin{cquote}
     1071\lstDeleteShortInline@%
     1072\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
     1073\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
     1074\begin{cfa}
     1075case 2~42:
     1076\end{cfa}
     1077&
     1078\begin{cfa}
     1079case 2: case 3: ... case 41: case 42:
     1080\end{cfa}
     1081\end{tabular}
     1082\lstMakeShortInline@%
     1083\end{cquote}
     1084and a combination:
     1085\begin{cfa}
     1086case -12~-4, -1~5, 14~21, 34~42:
     1087\end{cfa}
     1088
     1089C allows placement of @case@ clauses \emph{within} statements nested in the @switch@ body (see Duff's device~\cite{Duff83});
     1090\begin{cfa}
     1091switch ( i ) {
     1092  case 0:
     1093        for ( int i = 0; i < 10; i += 1 ) {
     1094                ...
     1095  `case 1:`             // no initialization of loop index
     1096                ...
     1097        }
     1098}
     1099\end{cfa}
     1100\CFA precludes this form of transfer into a control structure because it causes undefined behaviour, especially with respect to missed initialization, and provides very limited functionality.
     1101
     1102C allows placement of declaration within the @switch@ body and unreachable code at the start, resulting in undefined behaviour:
     1103\begin{cfa}
     1104switch ( x ) {
     1105        `int y = 1;`                            $\C{// unreachable initialization}$
     1106        `x = 7;`                                        $\C{// unreachable code without label/branch}$
     1107  case 0:
     1108        ...
     1109        `int z = 0;`                            $\C{// unreachable initialization, cannot appear after case}$
     1110        z = 2;
     1111  case 1:
     1112        `x = z;`                                        $\C{// without fall through, z is undefined}$
     1113}
     1114\end{cfa}
     1115\CFA allows the declaration of local variables, \eg @y@, at the start of the @switch@ with scope across the entire @switch@ body, \ie all @case@ clauses, but no statements.
     1116\CFA disallows the declaration of local variable, \eg @z@, within the @switch@ body, because a declaration cannot occur immediately after a @case@ since a label can only be attached to a statement, and the use of @z@ is undefined in @case 1@ as neither storage allocation nor initialization may have occurred.
     1117
     1118C @switch@ provides multiple entry points into the statement body, but once an entry point is selected, control continues across \emph{all} @case@ clauses until the end of the @switch@ body, called \newterm{fall through}.
     1119@case@ clauses are made disjoint by the @break@ statement.
     1120While the ability to fall through \emph{is} a useful form of control flow, it does not match well with programmer intuition, resulting in many errors from missing @break@ statements.
     1121\CFA provides a new control structure, @choose@ that mimics the @switch@, but reverses the meaning of fall through:
     1122\begin{cquote}
     1123\lstDeleteShortInline@%
     1124\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
     1125\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
     1126\begin{cfa}
     1127`choose` ( day ) {
     1128  case Mon, Tue, Wed, Thu:
     1129        // program
     1130
     1131  case Fri:
     1132        // program
     1133        wallet += pay;
     1134        `fallthrough;`
     1135  case Sat:
     1136        // party
     1137        wallet -= party;
     1138
     1139  case Sun:
     1140        // rest
     1141
     1142  default:
     1143        // error
     1144}
     1145\end{cfa}
     1146&
     1147\begin{cfa}
     1148switch ( day ) {
     1149  case Mon: case Tue: case Wed: case Thu:
     1150        // program
     1151        `break;`
     1152  case Fri:
     1153        // program
     1154        wallet += pay;
     1155
     1156  case Sat:
     1157        // party
     1158        wallet -= party;
     1159        `break;`
     1160  case Sun:
     1161        // rest
     1162        `break;`
     1163  default:
     1164        // error
     1165}
     1166\end{cfa}
     1167\end{tabular}
     1168\lstMakeShortInline@%
     1169\end{cquote}
     1170Collectively, these enhancements reduce programmer burden and increase readability and safety.
     1171
     1172\begin{comment}
    10521173Forgotten @break@ statements at the end of @switch@ cases are a persistent sort of programmer error in C, and the @break@ statements themselves introduce visual clutter and an un-C-like keyword-based block delimiter.
    10531174\CFA addresses this error by introducing a @choose@ statement, which works identically to a @switch@ except that its default end-of-case behaviour is to break rather than to fall through for all non-empty cases.
     
    10701191}
    10711192\end{cfa}
     1193\end{comment}
     1194
    10721195
    10731196\subsection{\texorpdfstring{\LstKeywordStyle{with} Clause / Statement}{with Clause / Statement}}
     
    15981721\section{Literals}
    15991722
    1600 C already includes limited polymorphism for literals -- @0@ can be either an integer or a pointer literal, depending on context, while the syntactic forms of literals of the various integer and floating-point types are very similar, differing from each other only in suffix.
     1723C already includes limited polymorphism for literals -- @0@ can be either an integer or a pointer literal, depending on context, while the syntactic forms of literals of the various integer and float types are very similar, differing from each other only in suffix.
    16011724In keeping with the general \CFA approach of adding features while respecting ``the C way'' of doing things, we have extended both C's polymorphic zero and typed literal syntax to interoperate with user-defined types, while maintaining a backwards-compatible semantics.
    16021725
Note: See TracChangeset for help on using the changeset viewer.