annotate dmd/expression/Equal.d @ 84:be2ab491772e

Expressions -> Vector!Expression
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Mon, 30 Aug 2010 16:12:19 +0100
parents ef02e2e203c2
children e28b18c23469
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.expression.Equal;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.TOK;
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 63
diff changeset
6 import dmd.TY;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.StringExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.GlobalExpressions;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.ArrayLiteralExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.StructLiteralExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.Global;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import core.stdc.string;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 /* Also returns EXP_CANT_INTERPRET if cannot be computed.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 Expression Equal(TOK op, Type type, Expression e1, Expression e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 Loc loc = e1.loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 bool cmp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 real r1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 real r2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 //printf("Equal(e1 = %s, e2 = %s)\n", e1.toChars(), e2.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 assert(op == TOK.TOKequal || op == TOK.TOKnotequal);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 if (e1.op == TOK.TOKnull)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 if (e2.op == TOK.TOKnull)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 cmp = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 else if (e2.op == TOK.TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 { StringExp es2 = cast(StringExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 cmp = (0 == es2.len);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 else if (e2.op == TOK.TOKarrayliteral)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 ArrayLiteralExp es2 = cast(ArrayLiteralExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 cmp = !es2.elements || (0 == es2.elements.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 else if (e2.op == TOK.TOKnull)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 if (e1.op == TOK.TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 StringExp es1 = cast(StringExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 cmp = (0 == es1.len);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 else if (e1.op == TOK.TOKarrayliteral)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 ArrayLiteralExp es1 = cast(ArrayLiteralExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 cmp = !es1.elements || (0 == es1.elements.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 else if (e1.op == TOK.TOKstring && e2.op == TOK.TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 StringExp es1 = cast(StringExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 StringExp es2 = cast(StringExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 if (es1.sz != es2.sz)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 assert(global.errors);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 if (es1.len == es2.len && memcmp(es1.string_, es2.string_, es1.sz * es1.len) == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 cmp = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 else if (e1.op == TOK.TOKarrayliteral && e2.op == TOK.TOKarrayliteral)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 ArrayLiteralExp es1 = cast(ArrayLiteralExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 ArrayLiteralExp es2 = cast(ArrayLiteralExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 if ((!es1.elements || !es1.elements.dim) && (!es2.elements || !es2.elements.dim))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 cmp = true; // both arrays are empty
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 else if (!es1.elements || !es2.elements)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 else if (es1.elements.dim != es2.elements.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 for (size_t i = 0; i < es1.elements.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 73
diff changeset
92 auto ee1 = es1.elements[i];
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 73
diff changeset
93 auto ee2 = es2.elements[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 73
diff changeset
95 auto v = Equal(TOK.TOKequal, Type.tint32, ee1, ee2);
63
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
96 if (v is EXP_CANT_INTERPRET)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 long tmp = v.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 cmp = (tmp != 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 if (!cmp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 else if (e1.op == TOK.TOKarrayliteral && e2.op == TOK.TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 // Swap operands and use common code
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 Expression ee = e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 e1 = e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 e2 = ee;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 goto Lsa;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 else if (e1.op == TOK.TOKstring && e2.op == TOK.TOKarrayliteral)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 Lsa:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 StringExp es1 = cast(StringExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 ArrayLiteralExp es2 = cast(ArrayLiteralExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 size_t dim1 = es1.len;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 size_t dim2 = es2.elements ? es2.elements.dim : 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 if (dim1 != dim2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 for (size_t i = 0; i < dim1; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 ulong c = es1.charAt(i);
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 73
diff changeset
127 auto ee2 = es2.elements[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 if (ee2.isConst() != 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 cmp = (c == ee2.toInteger());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 if (!cmp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 else if (e1.op == TOK.TOKstructliteral && e2.op == TOK.TOKstructliteral)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 StructLiteralExp es1 = cast(StructLiteralExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 StructLiteralExp es2 = cast(StructLiteralExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 if (es1.sd != es2.sd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 else if ((!es1.elements || !es1.elements.dim) && (!es2.elements || !es2.elements.dim))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 cmp = true; // both arrays are empty
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 else if (!es1.elements || !es2.elements)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 else if (es1.elements.dim != es2.elements.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 cmp = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 for (size_t i = 0; i < es1.elements.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 73
diff changeset
154 auto ee1 = es1.elements[i];
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 73
diff changeset
155 auto ee2 = es2.elements[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 if (ee1 == ee2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 if (!ee1 || !ee2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 Expression v = Equal(TOK.TOKequal, Type.tint32, ee1, ee2);
63
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
165 if (v is EXP_CANT_INTERPRET)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 long tmp = v.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 cmp = (tmp != 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 if (!cmp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 ///static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 /// else if (e1.op == TOKarrayliteral && e2.op == TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 /// {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 /// }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 ///}
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 else if (e1.isConst() != 1 || e2.isConst() != 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 else if (e1.type.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 r1 = e1.toReal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 r2 = e2.toReal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 else if (e1.type.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 r1 = e1.toImaginary();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 r2 = e2.toImaginary();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 cmp = (r1 == r2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 else if (e1.type.iscomplex())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 cmp = (e1.toComplex() == e2.toComplex());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 }
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 63
diff changeset
198 else if (e1.type.isintegral() || e1.type.ty == Tpointer)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 cmp = (e1.toInteger() == e2.toInteger());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 if (op == TOK.TOKnotequal)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 cmp = !cmp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 e = new IntegerExp(loc, cmp, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 }