1 Two for-loop variants are supported: a C-style counting for loop 2 consisting of an initialization expression, a test condition 3 and a step expression and a for-in-loop variant which allows 4 enumerating properties of objects or items of arrays. 5 6 Additionally, ucode supports an alternative syntax suitable for 7 template block tags. 8 9 10 -- Expect stdout -- 11 A simple counting for-loop: 12 Iteration 0 13 Iteration 1 14 Iteration 2 15 Iteration 3 16 Iteration 4 17 Iteration 5 18 Iteration 6 19 Iteration 7 20 Iteration 8 21 Iteration 9 22 23 If the loop body consists of only one statement, the curly braces 24 may be omitted: 25 Iteration 0 26 Iteration 1 27 Iteration 2 28 Iteration 3 29 Iteration 4 30 Iteration 5 31 Iteration 6 32 Iteration 7 33 Iteration 8 34 Iteration 9 35 36 Any of the init-, test- and increment expressions may be omitted. 37 38 Loop without initialization statement: 39 Iteration null 40 Iteration 1 41 Iteration 2 42 43 Loop without test statement: 44 Iteration 0 45 Iteration 1 46 Iteration 2 47 48 Loop without init-, test- or increment statement: 49 Iteration 1 50 Iteration 2 51 Iteration 3 52 53 For-in loop enumerating object properties: 54 Key: foo 55 Key: bar 56 57 For-in loop enumerating array items: 58 Item: one 59 Item: two 60 Item: three 61 62 A counting for-loop using the alternative syntax: 63 Iteration 0 64 Iteration 1 65 Iteration 2 66 Iteration 3 67 Iteration 4 68 Iteration 5 69 Iteration 6 70 Iteration 7 71 Iteration 8 72 Iteration 9 73 74 A for-in loop using the alternative syntax: 75 Item 123 76 Item 456 77 Item 789 78 79 For-in and counting for loops may declare variables: 80 Iteration 0 81 Iteration 1 82 Iteration 2 83 84 Item 123 85 Item 456 86 Item 789 87 -- End -- 88 89 -- Testcase -- 90 A simple counting for-loop: 91 {% 92 for (i = 0; i < 10; i++) { 93 print("Iteration "); 94 print(i); 95 print("\n"); 96 } 97 %} 98 99 If the loop body consists of only one statement, the curly braces 100 may be omitted: 101 {% 102 for (i = 0; i < 10; i++) 103 print("Iteration ", i, "\n"); 104 %} 105 106 Any of the init-, test- and increment expressions may be omitted. 107 108 Loop without initialization statement: 109 {% 110 for (; j < 3; j++) 111 print("Iteration " + j + "\n"); 112 %} 113 114 Loop without test statement: 115 {% 116 for (j = 0;; j++) { 117 if (j == 3) 118 break; 119 120 print("Iteration ", j, "\n"); 121 } 122 %} 123 124 Loop without init-, test- or increment statement: 125 {% 126 for (;;) { 127 if (k++ == 3) 128 break; 129 130 print("Iteration ", k, "\n"); 131 } 132 %} 133 134 For-in loop enumerating object properties: 135 {% 136 obj = { foo: true, bar: false }; 137 for (key in obj) 138 print("Key: ", key, "\n"); 139 %} 140 141 For-in loop enumerating array items: 142 {% 143 arr = [ "one", "two", "three" ]; 144 for (item in arr) 145 print("Item: ", item, "\n"); 146 %} 147 148 A counting for-loop using the alternative syntax: 149 {% for (x = 0; x < 10; x++): -%} 150 Iteration {{ x }} 151 {% endfor %} 152 153 A for-in loop using the alternative syntax: 154 {% for (n in [123, 456, 789]): -%} 155 Item {{ n }} 156 {% endfor %} 157 158 For-in and counting for loops may declare variables: 159 {% for (let i = 0; i < 3; i++): %} 160 Iteration {{ i }} 161 {% endfor %} 162 163 {% for (let n in [123, 456, 789]): %} 164 Item {{ n }} 165 {% endfor %} 166 -- End -- 167 168 169 By specifying two loop variables in for-in loop expressions, keys 170 and values can be iterated simultaneously. 171 172 -- Expect stdout -- 173 true 174 false 175 123 176 456 177 [ 0, true ] 178 [ 1, false ] 179 [ 2, 123 ] 180 [ 3, 456 ] 181 foo 182 bar 183 baz 184 qrx 185 [ "foo", true ] 186 [ "bar", false ] 187 [ "baz", 123 ] 188 [ "qrx", 456 ] 189 -- End -- 190 191 -- Testcase -- 192 {% 193 let arr = [ true, false, 123, 456 ]; 194 let obj = { foo: true, bar: false, baz: 123, qrx: 456 }; 195 196 // iterating arrays with one loop variable yields the array values 197 for (let x in arr) 198 print(x, "\n"); 199 200 // iterating arrays with two loop variables yields the array indexes 201 // and their corresponding values 202 for (let x, y in arr) 203 print([x, y], "\n"); 204 205 // iterating objects with one loop variable yields the object keys 206 for (let x in obj) 207 print(x, "\n"); 208 209 // iterating objects with two loop variables yields the object keys 210 // and their corresponding values 211 for (let x, y in obj) 212 print([x, y], "\n"); 213 %} 214 -- End -- 215 216 217 Ensure that for-in loop expressions with more than two variables are 218 rejected. 219 220 -- Expect stderr -- 221 Syntax error: Unexpected token 222 Expecting ';' 223 In line 2, byte 19: 224 225 ` for (let x, y, z in {})` 226 Near here -----------^ 227 228 229 -- End -- 230 231 -- Testcase -- 232 {% 233 for (let x, y, z in {}) 234 ; 235 %} 236 -- End -- 237 238 239 Ensure that assignments in for-in loop expressions are rejected. 240 241 -- Expect stderr -- 242 Syntax error: Unexpected token 243 Expecting ';' 244 In line 2, byte 20: 245 246 ` for (let x = 1, y in {})` 247 Near here ------------^ 248 249 250 -- End -- 251 252 -- Testcase -- 253 {% 254 for (let x = 1, y in {}) 255 ; 256 %} 257 -- End -- 258 259 260 Ensure that too short for-in loop expressions are rejected (1/2). 261 262 -- Expect stderr -- 263 Syntax error: Unexpected token 264 Expecting ';' 265 In line 2, byte 12: 266 267 ` for (let x)` 268 Near here ----^ 269 270 271 -- End -- 272 273 -- Testcase -- 274 {% 275 for (let x) 276 ; 277 %} 278 -- End -- 279 280 281 Ensure that too short for-in loop expressions are rejected (2/2). 282 283 -- Expect stderr -- 284 Syntax error: Unexpected token 285 Expecting ';' 286 In line 2, byte 15: 287 288 ` for (let x, y)` 289 Near here -------^ 290 291 292 -- End -- 293 294 -- Testcase -- 295 {% 296 for (let x, y) 297 ; 298 %} 299 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt