Mercurial > projects > ddmd
annotate dmd/CaseRangeStatement.d @ 187:b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author | Abscissa |
---|---|
date | Tue, 07 Jun 2011 23:37:34 -0400 |
parents | e3afd1303184 |
children |
rev | line source |
---|---|
0 | 1 module dmd.CaseRangeStatement; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Statement; |
64 | 5 import dmd.ArrayTypes; |
0 | 6 import dmd.Expression; |
64 | 7 import dmd.ExpStatement; |
8 import dmd.IntegerExp; | |
9 import dmd.CaseStatement; | |
10 import dmd.CompoundStatement; | |
0 | 11 import dmd.Statement; |
12 import dmd.Loc; | |
13 import dmd.OutBuffer; | |
64 | 14 import dmd.WANT; |
15 import dmd.SwitchStatement; | |
0 | 16 import dmd.HdrGenState; |
17 import dmd.Scope; | |
18 | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
19 import dmd.DDMDExtensions; |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
20 |
0 | 21 class CaseRangeStatement : Statement |
22 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
23 mixin insertMemberExtension!(typeof(this)); |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
24 |
0 | 25 Expression first; |
26 Expression last; | |
27 Statement statement; | |
28 | |
29 this(Loc loc, Expression first, Expression last, Statement s) | |
30 { | |
178 | 31 register(); |
32 | |
0 | 33 super(loc); |
51 | 34 this.first = first; |
35 this.last = last; | |
36 this.statement = s; | |
0 | 37 } |
38 | |
72 | 39 override Statement syntaxCopy() |
0 | 40 { |
41 assert(false); | |
42 } | |
43 | |
72 | 44 override Statement semantic(Scope sc) |
0 | 45 { |
64 | 46 SwitchStatement sw = sc.sw; |
47 | |
48 //printf("CaseRangeStatement.semantic() %s\n", toChars()); | |
49 if (sw.isFinal) | |
50 error("case ranges not allowed in final switch"); | |
51 | |
52 first = first.semantic(sc); | |
53 first = first.implicitCastTo(sc, sw.condition.type); | |
54 first = first.optimize(WANTvalue | WANTinterpret); | |
55 long fval = first.toInteger(); | |
56 | |
57 last = last.semantic(sc); | |
58 last = last.implicitCastTo(sc, sw.condition.type); | |
59 last = last.optimize(WANTvalue | WANTinterpret); | |
60 long lval = last.toInteger(); | |
61 | |
62 if (lval - fval > 256) | |
63 { | |
64 error("more than 256 cases in case range"); | |
65 lval = fval + 256; | |
66 } | |
67 | |
68 /* This works by replacing the CaseRange with an array of Case's. | |
69 * | |
70 * case a: .. case b: s; | |
71 * => | |
72 * case a: | |
73 * [...] | |
74 * case b: | |
75 * s; | |
76 */ | |
77 | |
78 Statements statements = new Statements(); | |
79 for (long i = fval; i <= lval; i++) | |
80 { | |
81 Statement s = statement; | |
82 if (i != lval) | |
83 s = new ExpStatement(loc, null); | |
84 Expression e = new IntegerExp(loc, i, first.type); | |
85 Statement cs = new CaseStatement(loc, e, s); | |
122
c77e9f4f1793
Statements -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
86 statements.push(cs); |
64 | 87 } |
88 Statement s = new CompoundStatement(loc, statements); | |
89 s = s.semantic(sc); | |
90 return s; | |
0 | 91 } |
92 | |
72 | 93 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 94 { |
64 | 95 buf.writestring("case "); |
96 first.toCBuffer(buf, hgs); | |
97 buf.writestring(": .. case "); | |
98 last.toCBuffer(buf, hgs); | |
99 buf.writenl(); | |
100 statement.toCBuffer(buf, hgs); | |
0 | 101 } |
72 | 102 } |