annotate dmd/expression/Mod.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 10317f0c89a5
children
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.Mod;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 0
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.RealExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.ComplexExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.Complex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import core.stdc.math;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 Expression Mod(Type type, Expression e1, Expression e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 Loc loc = e1.loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 if (type.isfloating())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 Complex!(real) c;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 if (e2.type.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 real r2 = e2.toReal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 c = Complex!(real)(fmodl(e1.toReal(), r2), fmodl(e1.toImaginary(), r2));;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 else if (e2.type.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 real i2 = e2.toImaginary();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 c = Complex!(real)(fmodl(e1.toReal(), i2), fmodl(e1.toImaginary(), i2));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 if (type.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 e = new RealExp(loc, c.re, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 else if (type.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 e = new RealExp(loc, c.im, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 else if (type.iscomplex())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 e = new ComplexExp(loc, c, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 long n1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 long n2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 long n;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 n1 = e1.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 n2 = e2.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 if (n2 == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 e2.error("divide by 0");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 e2 = new IntegerExp(loc, 1, e2.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 n2 = 1;
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 (e1.type.isunsigned() || e2.type.isunsigned())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 n = (cast(ulong) n1) % (cast(ulong) n2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 n = n1 % n2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 e = new IntegerExp(loc, n, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 }