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

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