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