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