annotate dmd/expression/Min.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
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.Min;
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.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.RealExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.ComplexExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.SymOffExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.Complex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 Expression Min(Type type, Expression e1, Expression e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 Loc loc = e1.loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 if (type.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 e = new RealExp(loc, e1.toReal() - e2.toReal(), type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 else if (type.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 e = new RealExp(loc, e1.toImaginary() - e2.toImaginary(), type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 else if (type.iscomplex())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 // This rigamarole is necessary so that -0.0 doesn't get
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 // converted to +0.0 by doing an extraneous add with +0.0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 Complex!(real) c1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 real r1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 real i1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 Complex!(real) c2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 real r2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 real i2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 Complex!(real) v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 int x;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 if (e1.type.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 r1 = e1.toReal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 x = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 else if (e1.type.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 i1 = e1.toImaginary();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 x = 3;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 c1 = e1.toComplex();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 x = 6;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 if (e2.type.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 r2 = e2.toReal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 else if (e2.type.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 i2 = e2.toImaginary();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 x += 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 c2 = e2.toComplex();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 x += 2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 switch (x)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 case 0+0: v = Complex!(real)(r1 - r2, 0); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 case 0+1: v = Complex!(real)(r1, -i2); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 case 0+2: v = Complex!(real)(r1 - c2.re, -c2.im); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 case 3+0: v = Complex!(real)(-r2, i1); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 case 3+1: v = Complex!(real)(0, i1 - i2); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 case 3+2: v = Complex!(real)(c2.re, i1 - c2.im); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 case 6+0: v = Complex!(real)(c1.re - r2, c1.im); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 case 6+1: v = Complex!(real)(c1.re, c1.im - i2); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 case 6+2: v = Complex!(real)(c1.re - c2.re, c1.im - c2.im); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 e = new ComplexExp(loc, v, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 else if (e1.op == TOK.TOKsymoff)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 SymOffExp soe = cast(SymOffExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 e = new SymOffExp(loc, soe.var, soe.offset - cast(uint)e2.toInteger());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 e = new IntegerExp(loc, e1.toInteger() - e2.toInteger(), type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 }