0
|
1 module dmd.expression.Div;
|
|
2
|
114
|
3 import dmd.common;
|
0
|
4 import dmd.Type;
|
|
5 import dmd.Expression;
|
|
6 import dmd.Loc;
|
|
7 import dmd.RealExp;
|
|
8 import dmd.ComplexExp;
|
|
9 import dmd.IntegerExp;
|
|
10 import dmd.Complex;
|
|
11
|
|
12 Expression Div(Type type, Expression e1, Expression e2)
|
|
13 {
|
|
14 Expression e;
|
|
15 Loc loc = e1.loc;
|
|
16
|
|
17 if (type.isfloating())
|
|
18 {
|
|
19 Complex!(real) c;
|
|
20 real r;
|
|
21
|
|
22 //e1.type.print();
|
|
23 //e2.type.print();
|
|
24 if (e2.type.isreal())
|
|
25 {
|
|
26 if (e1.type.isreal())
|
|
27 {
|
|
28 e = new RealExp(loc, e1.toReal() / e2.toReal(), type);
|
|
29 return e;
|
|
30 }
|
|
31
|
|
32 //r = e2.toReal();
|
|
33 //c = e1.toComplex();
|
|
34 //printf("(%Lg + %Lgi) / %Lg\n", creall(c), cimagl(c), r);
|
|
35 r = e2.toReal();
|
|
36 c = e1.toComplex();
|
|
37 c = Complex!(real)(c.re / r, c.im / r);
|
|
38 }
|
|
39 else if (e2.type.isimaginary())
|
|
40 {
|
|
41 //r = e2.toImaginary();
|
|
42 //c = e1.toComplex();
|
|
43 //printf("(%Lg + %Lgi) / %Lgi\n", creall(c), cimagl(c), r);
|
|
44 r = e2.toImaginary();
|
|
45 c = e1.toComplex();
|
|
46 c = Complex!(real)(c.im / r, -c.re / r);
|
|
47 }
|
|
48 else
|
|
49 {
|
|
50 Complex!(real) c1 = e1.toComplex();
|
|
51 Complex!(real) c2 = e2.toComplex();
|
|
52
|
|
53 real denumerator = c2.re*c2.re + c2.im*c2.im;
|
|
54 real numerator_re = c1.re*c2.re + c1.im*c2.im;
|
|
55 real numerator_im = c1.im*c2.re - c1.re*c2.im;
|
|
56
|
|
57 c = Complex!(real)(numerator_re / denumerator, numerator_im / denumerator);
|
|
58 }
|
|
59
|
|
60 if (type.isreal())
|
|
61 e = new RealExp(loc, c.re, type);
|
|
62 else if (type.isimaginary())
|
|
63 e = new RealExp(loc, c.im, type);
|
|
64 else if (type.iscomplex())
|
|
65 e = new ComplexExp(loc, c, type);
|
|
66 else
|
|
67 assert(0);
|
|
68 }
|
|
69 else
|
|
70 {
|
|
71 long n1;
|
|
72 long n2;
|
|
73 long n;
|
|
74
|
|
75 n1 = e1.toInteger();
|
|
76 n2 = e2.toInteger();
|
|
77
|
|
78 if (n2 == 0)
|
|
79 {
|
|
80 e2.error("divide by 0");
|
|
81 e2 = new IntegerExp(loc, 1, e2.type);
|
|
82 n2 = 1;
|
|
83 }
|
|
84
|
|
85 if (e1.type.isunsigned() || e2.type.isunsigned())
|
|
86 n = (cast(ulong) n1) / (cast(ulong) n2);
|
|
87 else
|
|
88 n = n1 / n2;
|
|
89
|
|
90 e = new IntegerExp(loc, n, type);
|
|
91 }
|
|
92
|
|
93 return e;
|
|
94 } |