- Timestamp:
- Jul 11, 2024, 7:32:44 AM (3 months ago)
- Branches:
- master
- Children:
- 358e3481
- Parents:
- 9d5eacb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r9d5eacb r3be81a4 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Tue Apr 23 14:13:10 202414 %% Update Count : 6 62313 %% Last Modified On : Tue Jul 9 10:43:40 2024 14 %% Update Count : 6887 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 182 182 & 183 183 \begin{cfa}[tabsize=3] 184 #include <fstream >§\indexc{fstream}§184 #include <fstream.hfa>§\indexc{fstream.hfa}§ 185 185 186 186 int main( void ) { … … 445 445 #include <stdio.h>§\indexc{stdio.h}§ §\C{// C header file}§ 446 446 #else 447 #include <fstream >§\indexc{fstream}§ §\C{// \CFA header file}§447 #include <fstream.hfa>§\indexc{fstream.hfa}§ §\C{// \CFA header file}§ 448 448 #endif 449 449 \end{cfa} … … 1006 1006 \subsection{Loop Control} 1007 1007 1008 Looping a fixed number of times, possibly with a loop index, occurs frequently.1009 \CFA condenses simply looping to facilitate coding speed and safety~\see{examples in \VRef[Figure]{f:LoopControlExamples}}.1010 1011 The \Indexc{for}, \Indexc{while}, and \Indexc{do} loop-control are extended to allow an empty conditional, which impliesa comparison value of ©1© (true).1008 Looping a predefined number of times, possibly with a loop index, occurs frequently. 1009 \CFA condenses writing loops to facilitate coding speed and safety. 1010 1011 \Indexc{for}, \Indexc{while}, and \Indexc{do} loop-control\index{loop control} are extended with an empty conditional, meaning a comparison value of ©1© (true). 1012 1012 \begin{cfa} 1013 1013 while ( ®/* empty */® ) §\C{// while ( true )}§ … … 1015 1015 do ... while ( ®/* empty */® ) §\C{// do ... while ( true )}§ 1016 1016 \end{cfa} 1017 1018 The ©for© control\index{for control}, \ie ©for ( /* control */ )©, is extended with a range and step. 1019 A range is a set of values defined by an optional low value (default to 0), tilde, and high value, ©L ~ H©, with an optional step ©~ S© (default to 1), which means an ascending set of values from ©L© to ©H© in positive steps of ©S©. 1020 \begin{cfa} 1021 0 ~ 5 §\C{// \{ 0, 1, 2, 3, 4, 5 \}}§ 1022 -8 ~ -2 ~ 2 §\C{// \{ -8. -6, -4, -2 \}}§ 1023 -3 ~ 3 ~ 1 §\C{// \{ -3, -2, -1, 0, 1, 2, 3 \}}§ 1024 \end{cfa} 1025 \R{Warning}: A range in descending order, \eg ©5 ~ -3© is the null (empty) set, \ie no values in the set. 1026 \R{Warning}: A ©0© or negative step is undefined. 1027 Note, the order of values in a set may not be the order the values are presented during looping. 1028 1029 The range character, ©'~'©, is decorated on the left and right to control how the set values are presented in the loop body. 1030 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). 1031 Ascending stepping uses operator \Indexc{+=}; 1032 descending stepping uses operator \Indexc{-=}. 1033 If there is no prefix character, it defaults to ©'+'©. 1034 \begin{cfa} 1035 -8 ®§\Sp§®~ -2 §\C{// ascending, no prefix}§ 1036 0 ®+®~ 5 §\C{// ascending, prefix}§ 1037 -3 ®-®~ 3 §\C{// descending}§ 1038 \end{cfa} 1039 For descending iteration, the ©L© and ©H© values are \emph{implicitly} switched, and the increment/decrement for ©S© is toggled. 1040 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. 1041 \R{Warning}: reversing the range endpoints for descending order results in an empty set. 1042 \begin{cfa} 1043 for ( i; ®10 -~ 1® ) §{\C{// WRONG descending range!}§ 1044 \end{cfa} 1045 1046 Because C uses zero origin, most loops iterate from 0 to $N - 1$. 1047 Hence, when scanning a range during iteration, the last value is dropped, \eg ©0 ~ 5© is ©0, 1, 2, 3, 4©, an exclusive range, [©L©,©H©\R{)}. 1048 To obtain \emph{all} the values in the range, the range character is postfixed with ©'='©, \eg ©0 ~= 5© is ©0, 1, 2, 3, 4, 5©, an inclusive range, [©L©,©H©\R{]}. 1049 \index{\~}\index{ascending exclusive range} 1050 \index{\~=}\index{ascending inclusive range} 1051 \index{-\~}\index{descending exclusive range} 1052 \index{-\~=}\index{descending inclusive range} 1053 1054 ©for© control is formalized by the following regular expression: 1055 \begin{cquote} 1056 [ ©L© ]\ \ [ ©+©\ \ |\ \ ©-© ]\ \ \R{©~©}\ \ [ ©=© ]\ \ ©H©\ \ [ ©~© ©S© ] 1057 \end{cquote} 1058 where ©[©\,©]© denotes optional and ©|© denotes alternative. 1059 That is, the optional low set value, the optional scan direction (ascending/descending), the (possibly) required range character, the optional include last-scan value, the required high set value, and the optional range character and step value. 1060 \R{Warning}: the regular expression allows the form ©~H©, but this syntax has a preexisting meaning in C: complement the bits of ©H©, \eg ©for ( ~5 )© meaning ©for ( -6 )©, as ©-6© is the complement of ©5©. 1061 This anomaly is unlikely to cause problems because programers will write the shorter ©for ( 5 )©. 1062 1063 The previous ©for© loops have an anonymous loop index in which the range iteration is computed. 1064 To access the value of the range iteration in the loop body, a \Index{loop index} is specified before the range. 1065 \begin{cfa} 1066 for ( ®int i;® 0 ~ 10 ~ 2 ) { ... ®i® ... } §\C{// loop index available in loop body}§ 1067 \end{cfa} 1068 Hence, unlike the 3 components in the C ©for©-control, there are only two components in the \CFA ©for©-control: the optional index variable and the range. 1069 The index type is optional (like \CC ©auto©), where the type is normally inferred from the low value ©L© because it initializes the index (the type of ©H© can be different from ©L©). 1070 When ©L© is omitted, the type of the required high value ©H© is used, as both ©L© and ©H© are the same type in this case. 1071 \begin{cfa} 1072 for ( i; ®1.5® ~ 5 ) §\C{// typeof(1.5) i; 1.5 is low value}§ 1073 for ( i; ®5.5® ) §\C{// typeof(5.5) i; 5.5 is high value}§ 1074 \end{cfa} 1075 1076 The following examples illustrate common \CFA ©for©-control combinations, with the C counter-part in the comment. 1077 \begin{itemize}[itemsep=0pt] 1078 \item 1079 ©H© is implicit ascending exclusive range [0,©H©\R{)}. 1080 \begin{cfa} 1081 for ( ®5® ) §\C{// for ( typeof(5) i; i < 5; i += 1 )}§ 1082 \end{cfa} 1083 \item 1084 ©~=© ©H© is implicit ascending inclusive range [0,©H©\R{]}. 1085 \begin{cfa} 1086 for ( ®~=® 5 ) §\C{// for ( typeof(5) i; i <= 5; i += 1 )}§ 1087 \end{cfa} 1088 \item 1089 ©L© ©~©\index{~@©~©} ©H© is explicit ascending exclusive range [©L©,©H©\R{)}. 1090 \begin{cfa} 1091 for ( 1 ®~® 5 ) §\C{// for ( typeof(1) i = 1; i < 5; i += 1 )}§ 1092 \end{cfa} 1093 \item 1094 ©L© ©~=©\index{~=@©~=©} ©H© is explicit ascending inclusive range [©L©,©H©\R{]}. 1095 \begin{cfa} 1096 for ( 1 ®~=® 5 ) §\C{// for ( typeof(1) i = 1; i <= 5; i += 1 )}§ 1097 \end{cfa} 1098 \item 1099 ©L© ©-~©\index{-~@©-~©} ©H© is explicit descending exclusive range \R{(}©H©,©L©], where ©L© and ©H© are implicitly interchanged to make the range descending. 1100 \begin{cfa} 1101 for ( 1 ®-~® 5 ) §\C{// for ( typeof(1) i = 5; i > 0; i -= 1 )}§ 1102 \end{cfa} 1103 \item 1104 ©L© ©-~=©\index{-~=@©-~=©} ©H© is explicit descending inclusive range \R{[}©H©,©L©], where ©L© and ©H© are implicitly interchanged to make the range descending. 1105 \begin{cfa} 1106 for ( 1 ®-~=® 5 ) §\C{// for ( typeof(1) i = 5; i >= 0; i -= 1 )}§ 1107 \end{cfa} 1108 \end{itemize} 1109 1110 There are situations when the ©for©-control actions need to be moved into the loop body, \eg a mid-loop exit does not need an iteration-completion test in the ©for© control. 1111 The character ©'@'© indicates that a specific ©for©-control action is ignored, \ie generates no code. 1112 \begin{cfa} 1113 for ( i; ®@® -~ 10 ) §\C{// for ( typeof(10) i = 10; \R{/*empty*/}; i -= 1 )}§ 1114 for ( i; 1 ~ ®@® ~ 2 ) §\C{// for ( typeof(1) i = 1; \R{/* empty */}; i += 2 )}§ 1115 for ( i; 1 ~ 10 ~ ®@® ) §\C{// for ( typeof(1) i = 1; i < 10; \R{/* empty */} )}§ 1116 for ( i; 1 ~ ®@® ~ ®@® ) §\C{// for ( typeof(1) i = 1; \R{/* empty */}; \R{/* empty */} )}§ 1117 \end{cfa} 1118 \R{Warning}: ©L© \emph{cannot} be elided for the ascending range, \lstinline{@ ~ 5}, nor ©H© for the descending range, \lstinline{1 -~ @}, as the loop index is uninitialized. 1119 \R{Warning}: ©H© \emph{cannot} be elided in an anonymous loop index, ©1 ~ @©, as there is no index to stop the loop. 1120 1121 There are situations when multiple loop indexes are required. 1122 The character ©':'© means add another index, where any number of indices may be chained in a single ©for© control. 1123 \begin{cfa} 1124 for ( i; 5 ®:® j; 2 ~ 12 ~ 3 ) §\C{// for ( typeof(i) i = 1, j = 2; \R{i < 5 \&\& j < 12}; i += 1, j += 3 )}§ 1125 for ( i; 5 ®:® j; 2 ~ @ ~ 3 ) §\C{// for ( typeof(i) i = 1, j = 2; \R{i < 5}; i += 1, j += 3 )}§ 1126 for ( i; 5 ®:® j; 2.5 ~ @ ~ 3.5 ) §\C{// no C equivalent, without hoisting declaration of floating-point j}§ 1127 \end{cfa} 1128 \VRef[Figure]{f:LoopControlExamples} shows more complex loop-control examples across all the different options. 1017 1129 1018 1130 \begin{figure} … … 1025 1137 for () { sout | "empty"; break; } §\C[3in]{sout | nl | nlOff;}§ 1026 1138 1027 for ( 0 ) { sout | "A"; } sout | "zero";§\C{sout | nl;}§1139 for ( 0 ) { sout | "A"; } §\C{sout | nl;}§ 1028 1140 for ( 1 ) { sout | "A"; } §\C{sout | nl;}§ 1029 1141 for ( 10 ) { sout | "A"; } §\C{sout | nl;}§ … … 1073 1185 empty 1074 1186 1075 zero 1187 1076 1188 A 1077 1189 A A A A A A A A A A … … 1120 1232 \end{figure} 1121 1233 1122 % for () => for ( ;; ) 1123 % for ( 10 - t ) => for ( typeof(10 - t) ? = 0 ; ? < 10 - t; ? += 1 ) // using 0 and 1 1124 % for ( i ; 10 - t ) => for ( typeof(10 - t) i = 0 ; i < 10 - t; i += 1 ) // using 0 and 1 1125 % for ( T i ; 10 - t ) => for ( T i = 0 ; i < 10 - t; i += 1 ) // using 0 and 1 1126 % for ( 3~9 ) => for ( int ? = 3 ; ? < 9; ? += 1 ) // using 1 1127 % for ( i ; 3~9 ) => for ( int i = 3 ; i < 9; i += 1 ) // using 1 1128 % for ( T i ; 3~9 ) => for ( T i = 3 ; i < 9; i += 1 ) // using 1 1129 1130 The ©for© control is extended with 4 new loop-control operators, which are not overloadable: 1131 \begin{description}[itemsep=0pt,parsep=0pt] 1132 \item 1133 \Indexc{\~} (tilde) \Index{up-to exclusive range}, 1134 \item 1135 \Indexc{\~=} (tilde equal) \Index{up-to inclusive range}, 1136 \item 1137 \Indexc{-\~} (minus tilde) \Index{down-to exclusive range}, 1138 \item 1139 \Indexc{-\~=} (minus tilde equal) \Index{down-to inclusive range}. 1140 \end{description} 1141 1142 The ©for© index components, low (L), high (H), and increment (I), are optional. 1143 If missing: 1144 \begin{itemize}[itemsep=0pt,parsep=0pt] 1145 \item 1146 \Indexc{0} is the implicit low value. 1147 \item 1148 \Indexc{1} is the implicit increment value. 1149 \item 1150 The up-to range uses operator \Indexc{+=} for increment. 1151 \item 1152 The down-to range uses operator \Indexc{-=} for decrement. 1153 \end{itemize} 1154 1155 If no type is specified for the loop index, it is the type of the high value H (when the low value is implicit) or the low value L. 1156 \begin{cfa} 1157 for ( ®5® ) §\C{// typeof(5) anonymous-index; 5 is high value}§ 1158 for ( i; ®1.5® ~ 5.5 ) §\C{// typeof(1.5) i; 1.5 is low value}§ 1159 for ( ®int i®; 0 ~ 10 ~ 2 ) §\C{// int i; type is explicit}§ 1160 \end{cfa} 1161 1162 The following are examples for constructing different for-control: 1163 \begin{itemize}[itemsep=0pt] 1164 \item 1165 H is implicit up-to exclusive range [0,H\R{)}. 1166 \begin{cfa} 1167 for ( ®5® ) §\C{// for ( typeof(5) i; i < 5; i += 1 )}§ 1168 \end{cfa} 1169 \item 1170 ©~=© H is implicit up-to inclusive range [0,H\R{]}. 1171 \begin{cfa} 1172 for ( ®~=® 5 ) §\C{// for ( typeof(5) i; i <= 5; i += 1 )}§ 1173 \end{cfa} 1174 \item 1175 L ©~©\index{~@©~©} H is explicit up-to exclusive range [L,H\R{)}. 1176 \begin{cfa} 1177 for ( 1 ®~® 5 ) §\C{// for ( typeof(1) i = 1; i < 5; i += 1 )}§ 1178 \end{cfa} 1179 \item 1180 L ©~=©\index{~=@©~=©} H is explicit up-to inclusive range [L,H\R{]}. 1181 \begin{cfa} 1182 for ( 1 ®~=® 5 ) §\C{// for ( typeof(1) i = 1; i <= 5; i += 1 )}§ 1183 \end{cfa} 1184 \item 1185 L ©-~©\index{-~@©-~©} H is explicit down-to exclusive range [H,L\R{)}, where L and H are implicitly interchanged to make the range down-to. 1186 \begin{cfa} 1187 for ( 1 ®-~® 5 ) §\C{// for ( typeof(1) i = 5; i > 0; i -= 1 )}§ 1188 \end{cfa} 1189 \item 1190 L ©-~=©\index{-~=@©-~=©} H is explicit down-to inclusive range [H,L\R{]}, where L and H are implicitly interchanged to make the range down-to. 1191 \begin{cfa} 1192 for ( 1 ®-~=® 5 ) §\C{// for ( typeof(1) i = 5; i >= 0; i -= 1 )}§ 1193 \end{cfa} 1194 \item 1195 ©@© means put nothing in this field. 1196 \begin{cfa} 1197 for ( i; 1 ~ ®@® ~ 2 ) §\C{// for ( typeof(1) i = 1; \R{/* empty */}; i += 2 )}§ 1198 for ( i; 1 ~ 10 ~ ®@® ) §\C{// for ( typeof(1) i = 1; i < 10; \R{/* empty */} )}§ 1199 for ( i; 1 ~ ®@® ~ ®@® ) §\C{// for ( typeof(1) i = 1; /*empty*/; \R{/* empty */} )}§ 1200 \end{cfa} 1201 L cannot be elided for the up-to range, \lstinline{@ ~ 5}, and H for the down-to range, \lstinline{1 -~ @}, because then the loop index is uninitialized. 1202 Similarly, the high value cannot be elided is an anonymous loop index (©1 ~ @©), as there is no index to stop the loop. 1203 \item 1204 ©:© means add another index. 1205 \begin{cfa} 1206 for ( i; 5 ®:® j; 2 ~ 12 ~ 3 ) §\C{// for ( typeof(i) i = 1, j = 2; i < 5 \&\& j < 12; i += 1, j += 3 )}§ 1207 \end{cfa} 1208 \end{itemize} 1209 \R{Warning}: specifying the down-to range maybe unexpected because the loop control \emph{implicitly} switches the L and H values (and toggles the increment/decrement for I): 1210 \begin{cfa} 1211 for ( i; 1 ~ 10 ) §{\C{// up range}§ 1212 for ( i; 1 -~ 10 ) §{\C{// down range}§ 1213 for ( i; ®10 -~ 1® ) §{\C{// \R{WRONG down range!}}}§ 1214 \end{cfa} 1215 The reason for this semantics is that the range direction can be toggled by adding/removing the minus, ©'-'©, versus interchanging the L and H expressions, which has a greater chance of introducing errors. 1216 1217 1218 %\subsection{\texorpdfstring{Labelled \protect\lstinline{continue} / \protect\lstinline{break}}{Labelled continue / break}} 1219 \subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}} 1220 1221 C \Indexc{continue} and \Indexc{break} statements are restricted to one level of nesting for a particular control structure. 1222 This restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting. 1223 To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@©continue©!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@©break©!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85}, as in Java. 1224 For both ©continue© and ©break©, the target label must be directly associated with a \Indexc{for}, \Indexc{while} or \Indexc{do} statement; 1225 for ©break©, the target label can also be associated with a \Indexc{switch}, \Indexc{if} or compound (©{}©) statement. 1226 \VRef[Figure]{f:MultiLevelExit} shows a comparison between labelled ©continue© and ©break© and the corresponding C equivalent using ©goto© and labels. 1227 The innermost loop has 8 exit points, which cause continuation or termination of one or more of the 7 \Index{nested control-structure}s. 1234 Finally, any type that satisfies the ©Iterate© trait can be used with ©for© control. 1235 \begin{cfa} 1236 forall( T ) trait Iterate { 1237 void ?{}( T & t, zero_t ); 1238 int ?<?( T t1, T t2 ); 1239 int ?<=?( T t1, T t2 ); 1240 int ?>?( T t1, T t2 ); 1241 int ?>=?( T t1, T t2 ); 1242 T ?+=?( T & t1, T t2 ); 1243 T ?+=?( T & t, one_t ); 1244 T ?-=?( T & t1, T t2 ); 1245 T ?-=?( T & t, one_t ); 1246 } 1247 \end{cfa} 1248 \VRef[Figure]{f:ForControlStructureType} shows an example of a structure using ©for© control. 1249 Note, the use of ©(S){0}© when implicitly setting the loop-index type, because using 0 incorrect declares the index to ©int© rather than ©S©. 1250 1251 \begin{figure} 1252 \begin{tabular}{@{}l@{\hspace{5pt}}l@{}} 1253 \begin{cfa} 1254 struct S { int i, j; }; 1255 void ?{}( S & s, int i = 0, int j = 0 ) { s.[i, j] = [i, j]; } 1256 void ?{}( S & s, zero_t ) { s.[i, j] = 0; } 1257 int ?<?( S t1, S t2 ) { return t1.i < t2.i && t1.j < t2.j; } 1258 int ?<=?( S t1, S t2 ) { return t1.i <= t2.i && t1.j <= t2.j; } 1259 int ?>?( S t1, S t2 ) { return t1.i > t2.i && t1.j > t2.j; } 1260 int ?>=?( S t1, S t2 ) { return t1.i >= t2.i && t1.j >= t2.j; } 1261 S ?+=?( S & t1, S t2 ) { t1.i += t2.i; t1.j += t2.j; return t1; } 1262 S ?+=?( S & t, one_t ) { t.i += 1; t.j += 1; return t; } 1263 S ?-=?( S & t1, S t2 ) { t1.i -= t2.i; t1.j -= t2.j; return t1; } 1264 S ?-=?( S & t, one_t ) { t.i -= 1; t.j -= 1; return t; } 1265 ofstream & ?|?( ofstream & os, S s ) { 1266 return os | "(" | s.i | s.j | ")"; 1267 } 1268 void & ?|?( ofstream & os, S s ) { 1269 (ofstream &)(os | s); ends( os ); 1270 } 1271 1272 1273 1274 \end{cfa} 1275 & 1276 \begin{cfa} 1277 int main() { 1278 for ( S i = 0; i < (S){10,10}; i += 1 ) { sout | i; } sout | "A" | nl; // C 1279 for ( S i; 0 ~ (S){10,10} ) { sout | i; } sout | "B" | nl; // CFA 1280 for ( i; (S){10,10} ) { sout | i; } sout | "C" | nl; 1281 for ( i; (S){0} ~ (S){10,10} ) { sout | i; } sout | "D" | nl; 1282 for ( i; (S){0} ~= (S){10,10} ) { sout | i; } sout | "E" | nl; 1283 for ( i; (S){0} ~= (S){10,10} ~ (S){2} ) { sout | i; } sout | "F" | nl; 1284 for ( i; (S){0} -~ (S){10,10} ) { sout | i; } sout | "G" | nl; 1285 for ( i; (S){0} -~= (S){10,10} ) { sout | i; } sout | "H" | nl; 1286 for ( i; (S){0} -~= (S){10,10} ~ (S){2,1} ) { sout | i; } sout | "I" | nl; 1287 } 1288 (0 0) (1 1) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9) A 1289 (0 0) (1 1) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9) B 1290 (0 0) (1 1) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9) C 1291 (0 0) (1 1) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9) D 1292 (0 0) (1 1) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9) (10 10) E 1293 (0 0) (2 0) (4 0) (6 0) (8 0) (10 0) F 1294 (10 10) (9 9) (8 8) (7 7) (6 6) (5 5) (4 4) (3 3) (2 2) (1 1) G 1295 (10 10) (9 9) (8 8) (7 7) (6 6) (5 5) (4 4) (3 3) (2 2) (1 1) (0 0) H 1296 (10 10) (8 9) (6 8) (4 7) (2 6) (0 5) I 1297 \end{cfa} 1298 \end{tabular} 1299 \caption{For Control with Structure Type} 1300 \label{f:ForControlStructureType} 1301 \end{figure} 1302 1228 1303 1229 1304 \begin{figure} … … 1297 1372 \end{figure} 1298 1373 1374 1375 %\subsection{\texorpdfstring{Labelled \protect\lstinline{continue} / \protect\lstinline{break}}{Labelled continue / break}} 1376 \subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}} 1377 1378 C \Indexc{continue} and \Indexc{break} statements are restricted to one level of nesting for a particular control structure. 1379 This restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting. 1380 To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@©continue©!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@©break©!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85}, as in Java. 1381 For both ©continue© and ©break©, the target label must be directly associated with a \Indexc{for}, \Indexc{while} or \Indexc{do} statement; 1382 for ©break©, the target label can also be associated with a \Indexc{switch}, \Indexc{if} or compound (©{}©) statement. 1383 \VRef[Figure]{f:MultiLevelExit} shows a comparison between labelled ©continue© and ©break© and the corresponding C equivalent using ©goto© and labels. 1384 The innermost loop has 8 exit points, which cause continuation or termination of one or more of the 7 \Index{nested control-structure}s. 1385 1299 1386 Both labelled \Indexc{continue} and \Indexc{break} are a \Indexc{goto}\index{goto@©goto©!restricted} restricted in the following ways: 1300 1387 \begin{itemize} … … 3103 3190 Similarly, if the argument is an expression, it must be parenthesized, \eg ©(i + 3)`h©, or only the last operand of the expression is the argument, \eg ©i + (3`h)©. 3104 3191 3105 \VRef[Figure]{f:UnitsComparison} shows a common usage for postfix functions: converting basic literals into user literals.3192 \VRef[Figure]{f:UnitsComparison} shows a common example for postfix functions: converting basic literals into user literals. 3106 3193 \see*{\VRef{s:DynamicStorageManagement} for other uses for postfix functions.} 3107 The \CFA example (left) stores a mass in units of stones (1 stone = 14 lb or 6.35 kg) and provides an addition operator (imagine a full set of arithmetic operators). 3194 The \CFA example (left) stores a mass in units of stones (1 stone = 14 lb or 6.35 kg) and provides an addition operator ©?+?© (imagine a full set of arithmetic operators). 3195 The arithmetic operators manipulate stones and the postfix operations convert to/from different units. 3108 3196 The three postfixing function names ©st©, ©lb©, and ©kg©, represent units stones, pounds, and kilograms, respectively. 3109 3197 Each name has two forms that bidirectional convert: a value of a specified unit to stones, \eg ©w = 14`lb© $\Rightarrow$ ©w == 1© stone or a ©Weight© from stones back to specific units, \eg ©w`lb© (1 stone) to ©14©. 3110 All arithmetic operations manipulate stones and the postfix operations convert to the different units.3111 3198 A similar group of postfix functions provide user constants for converting time units into nanoseconds, which is the basic time unit, \eg ©ns©, ©us©, ©ms©, ©s©, ©m©, ©h©, ©d©, and ©w©, for nanosecond, microsecond, millisecond, second, minute, hour, day, and week, respectively. 3112 3199 (Note, month is not a fixed period of time nor is year because of leap years.) … … 3209 3296 3210 3297 The \CC example (right) provides a \emph{restricted} capability via user literals. 3211 The \lstinline[language=C++]{operator ""} only takes a constant argument (\ie no variableargument), and the constant type must be the highest-level constant-type, \eg ©long double© for all floating-point constants.3298 The \lstinline[language=C++]{operator""} only takes a constant argument (\ie no variable as an argument), and the constant type must be the highest-level constant-type, \eg ©long double© for all floating-point constants. 3212 3299 As well, there is no constant conversion, \ie ©int© to ©double© constants, so integral constants are handled by a separate set of routines, with maximal integral type ©unsigned long long int©. 3213 Finally, there is no mechanism to use this syntax for a bidirectional conversion because \lstinline[language=C++]{operator ""} does not accept variable arguments.3300 Finally, there is no mechanism to use this syntax for a bidirectional conversion because \lstinline[language=C++]{operator""} only accepts a constant argument. 3214 3301 3215 3302 … … 5069 5156 5070 5157 \begin{comment} 5071 #include <fstream >5158 #include <fstream.hfa> 5072 5159 #include <string.h> // strcpy 5073 5160 … … 6330 6417 \begin{figure} 6331 6418 \begin{cfa} 6332 #include <fstream >6419 #include <fstream.hfa> 6333 6420 #include <kernel> 6334 6421 #include <stdlib> … … 9826 9913 A contrary analysis: https://hackaday.com/2024/02/29/the-white-house-memory-safety-appeal-is-a-security-red-herring/ 9827 9914 9915 9916 From: Ian Fox <ianfox97@gmail.com> 9917 Date: Fri, 10 May 2024 22:11:41 +0200 9918 Subject: Article on C and memory safety 9919 To: pabuhr@uwaterloo.ca 9920 9921 Hi Dr. Buhr! 9922 9923 I came across this 9924 <https://crashoverride.com/blog/c-isnt-a-hangover-rust-isnt-a-hangover-cure> 9925 article the other day which I thought made some excellent points, and it 9926 reminded me of your C forall project (especially with regards to the part 9927 about being able to opt in incrementally to the safety features while 9928 rewriting a codebase) so I figured you might enjoy it if you hadn't seen it 9929 already. 9930 9931 All the best! 9932 Ian 9933 9828 9934 % Local Variables: % 9829 9935 % tab-width: 4 %
Note: See TracChangeset
for help on using the changeset viewer.