Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80770 views
1
constant_switch_1: {
2
options = { dead_code: true, evaluate: true };
3
input: {
4
switch (1+1) {
5
case 1: foo(); break;
6
case 1+1: bar(); break;
7
case 1+1+1: baz(); break;
8
}
9
}
10
expect: {
11
bar();
12
}
13
}
14
15
constant_switch_2: {
16
options = { dead_code: true, evaluate: true };
17
input: {
18
switch (1) {
19
case 1: foo();
20
case 1+1: bar(); break;
21
case 1+1+1: baz();
22
}
23
}
24
expect: {
25
foo();
26
bar();
27
}
28
}
29
30
constant_switch_3: {
31
options = { dead_code: true, evaluate: true };
32
input: {
33
switch (10) {
34
case 1: foo();
35
case 1+1: bar(); break;
36
case 1+1+1: baz();
37
default:
38
def();
39
}
40
}
41
expect: {
42
def();
43
}
44
}
45
46
constant_switch_4: {
47
options = { dead_code: true, evaluate: true };
48
input: {
49
switch (2) {
50
case 1:
51
x();
52
if (foo) break;
53
y();
54
break;
55
case 1+1:
56
bar();
57
default:
58
def();
59
}
60
}
61
expect: {
62
bar();
63
def();
64
}
65
}
66
67
constant_switch_5: {
68
options = { dead_code: true, evaluate: true };
69
input: {
70
switch (1) {
71
case 1:
72
x();
73
if (foo) break;
74
y();
75
break;
76
case 1+1:
77
bar();
78
default:
79
def();
80
}
81
}
82
expect: {
83
// the break inside the if ruins our job
84
// we can still get rid of irrelevant cases.
85
switch (1) {
86
case 1:
87
x();
88
if (foo) break;
89
y();
90
}
91
// XXX: we could optimize this better by inventing an outer
92
// labeled block, but that's kinda tricky.
93
}
94
}
95
96
constant_switch_6: {
97
options = { dead_code: true, evaluate: true };
98
input: {
99
OUT: {
100
foo();
101
switch (1) {
102
case 1:
103
x();
104
if (foo) break OUT;
105
y();
106
case 1+1:
107
bar();
108
break;
109
default:
110
def();
111
}
112
}
113
}
114
expect: {
115
OUT: {
116
foo();
117
x();
118
if (foo) break OUT;
119
y();
120
bar();
121
}
122
}
123
}
124
125
constant_switch_7: {
126
options = { dead_code: true, evaluate: true };
127
input: {
128
OUT: {
129
foo();
130
switch (1) {
131
case 1:
132
x();
133
if (foo) break OUT;
134
for (var x = 0; x < 10; x++) {
135
if (x > 5) break; // this break refers to the for, not to the switch; thus it
136
// shouldn't ruin our optimization
137
console.log(x);
138
}
139
y();
140
case 1+1:
141
bar();
142
break;
143
default:
144
def();
145
}
146
}
147
}
148
expect: {
149
OUT: {
150
foo();
151
x();
152
if (foo) break OUT;
153
for (var x = 0; x < 10; x++) {
154
if (x > 5) break;
155
console.log(x);
156
}
157
y();
158
bar();
159
}
160
}
161
}
162
163
constant_switch_8: {
164
options = { dead_code: true, evaluate: true };
165
input: {
166
OUT: switch (1) {
167
case 1:
168
x();
169
for (;;) break OUT;
170
y();
171
break;
172
case 1+1:
173
bar();
174
default:
175
def();
176
}
177
}
178
expect: {
179
OUT: {
180
x();
181
for (;;) break OUT;
182
y();
183
}
184
}
185
}
186
187
constant_switch_9: {
188
options = { dead_code: true, evaluate: true };
189
input: {
190
OUT: switch (1) {
191
case 1:
192
x();
193
for (;;) if (foo) break OUT;
194
y();
195
case 1+1:
196
bar();
197
default:
198
def();
199
}
200
}
201
expect: {
202
OUT: {
203
x();
204
for (;;) if (foo) break OUT;
205
y();
206
bar();
207
def();
208
}
209
}
210
}
211
212