Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 switcher(val) { | 5 switcher(val) { |
| 6 var x = 0; | 6 var x = 0; |
| 7 switch (val) { | 7 switch (val) { |
| 8 case 1: | 8 case 1: |
| 9 x = 100; | 9 x = 100; |
| 10 break; | 10 break; |
| 11 case 2: | 11 case 2: |
| 12 x = 200; | 12 x = 200; |
| 13 break; | 13 break; |
| 14 case 3: | 14 case 3: |
| 15 x = 300; | 15 x = 300; |
| 16 break; | 16 break; |
| 17 default: | 17 default: |
| 18 return 400; | 18 return 400; |
| 19 break; // Intentional dead code (regression test for crash). | 19 break; // Intentional dead code (regression test for crash). |
| 20 } | 20 } |
| 21 return x; | 21 return x; |
| 22 } | 22 } |
| 23 | 23 |
| 24 | |
| 25 // Check unambiguated grammar allowing multiple lables per case/default. | |
| 26 switcher2(val) { | |
| 27 var x = 0; | |
| 28 switch (val) { | |
| 29 foo: | |
| 30 bar: | |
| 31 case 1: | |
| 32 baz: | |
| 33 case 2: | |
| 34 fubar: { | |
| 35 x = 100; | |
| 36 break fubar; | |
|
ahe
2012/05/14 09:24:25
That is a cuss word. It means "F'ed up beyond all
Lasse Reichstein Nielsen
2012/05/14 10:34:58
Or "beyond all repair", even.
Will change.
| |
| 37 } | |
| 38 break; | |
| 39 hest: | |
| 40 fisk: | |
| 41 case 3: | |
| 42 case 4: | |
| 43 svin: | |
| 44 default: | |
| 45 barber: { | |
| 46 if (val > 2) { | |
| 47 x = 200; | |
| 48 break; | |
| 49 } else { | |
| 50 // Enable when continue to switch-case is implemented. | |
| 51 continue hest; /// 03: compile-time error | |
| 52 } | |
| 53 } | |
| 54 } | |
| 55 return x; | |
| 56 } | |
| 57 | |
| 58 | |
| 59 badswitches(val) { | |
| 60 // Test some badly formed switch bodies. | |
| 61 // 01 - a label/statement without a following case/default. | |
| 62 // 02 - a label without a following case/default or statement. | |
| 63 switch (val) { | |
| 64 foo: break; /// 01: compile-time error | |
| 65 case 2: /// 02: compile-time error | |
| 66 foo: /// 02: continued | |
| 67 } | |
| 68 } | |
| 69 | |
| 24 main() { | 70 main() { |
| 25 Expect.equals(100, switcher(1)); | 71 Expect.equals(100, switcher(1)); |
| 26 Expect.equals(200, switcher(2)); | 72 Expect.equals(200, switcher(2)); |
| 27 Expect.equals(300, switcher(3)); | 73 Expect.equals(300, switcher(3)); |
| 28 Expect.equals(400, switcher(4)); | 74 Expect.equals(400, switcher(4)); |
| 75 | |
| 76 Expect.equals(100, switcher2(1)); | |
| 77 Expect.equals(100, switcher2(2)); | |
| 78 Expect.equals(200, switcher2(3)); | |
| 79 Expect.equals(200, switcher2(4)); | |
| 80 Expect.equals(200, switcher2(5)); | |
| 81 | |
| 82 badswitches(42); | |
| 29 } | 83 } |
| OLD | NEW |