annotate dmd/expression/Add.d @ 114:e28b18c23469

added a module dmd.common for commonly used stuff it currently holds code for consistency checking of predefined versions also added a VisualD project file
author Trass3r
date Wed, 01 Sep 2010 18:21:58 +0200
parents 5c9b78899f5d
children 52188e7e3fb5
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
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 16
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.RealExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.ComplexExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.SymOffExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.Complex;
16
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
13 import dmd.Util : printf;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 Expression Add(Type type, Expression e1, Expression e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 Loc loc = e1.loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 printf("Add(e1 = %s, e2 = %s)\n", e1.toChars(), e2.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 if (type.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 e = new RealExp(loc, e1.toReal() + e2.toReal(), type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 else if (type.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 e = new RealExp(loc, e1.toImaginary() + e2.toImaginary(), type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 else if (type.iscomplex())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 // This rigamarole is necessary so that -0.0 doesn't get
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 // converted to +0.0 by doing an extraneous add with +0.0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 Complex!(real) c1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 real r1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 real i1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 Complex!(real) c2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 real r2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 real i2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 Complex!(real) v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 int x;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 if (e1.type.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 r1 = e1.toReal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 x = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 else if (e1.type.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 i1 = e1.toImaginary();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 x = 3;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 c1 = e1.toComplex();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 x = 6;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 if (e2.type.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 r2 = e2.toReal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 else if (e2.type.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 i2 = e2.toImaginary();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 x += 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 c2 = e2.toComplex();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 x += 2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 switch (x)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 case 0+0: v = Complex!(real)(r1 + r2, 0); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 case 0+1: v = Complex!(real)(r1, i2); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 case 0+2: v = Complex!(real)(r1 + c2.re, c2.im); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 case 3+0: v = Complex!(real)(r2, i1); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 case 3+1: v = Complex!(real)(0, i1 + i2); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 case 3+2: v = Complex!(real)(c2.re, i1 + c2.im); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 case 6+0: v = Complex!(real)(c1.re + r2, c1.im); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 case 6+1: v = Complex!(real)(c1.re, c1.im + i2); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 case 6+2: v = Complex!(real)(c1.re + c2.re, c1.im + c2.im); break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 e = new ComplexExp(loc, v, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 else if (e1.op == TOK.TOKsymoff)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 SymOffExp soe = cast(SymOffExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 e = new SymOffExp(loc, soe.var, soe.offset + cast(uint)e2.toInteger());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 else if (e2.op == TOK.TOKsymoff)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 SymOffExp soe = cast(SymOffExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 e = new SymOffExp(loc, soe.var, soe.offset + cast(uint)e1.toInteger());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 e = new IntegerExp(loc, e1.toInteger() + e2.toInteger(), type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 return e;
16
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
107 }