comparison dmd/expression/Mod.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children e28b18c23469
comparison
equal deleted inserted replaced
-1:000000000000 0:10317f0c89a5
1 module dmd.expression.Mod;
2
3 import dmd.Loc;
4 import dmd.Type;
5 import dmd.Expression;
6 import dmd.IntegerExp;
7 import dmd.RealExp;
8 import dmd.ComplexExp;
9 import dmd.Complex;
10
11 import core.stdc.math;
12
13 Expression Mod(Type type, Expression e1, Expression e2)
14 {
15 Expression e;
16 Loc loc = e1.loc;
17
18 if (type.isfloating())
19 {
20 Complex!(real) c;
21
22 if (e2.type.isreal())
23 {
24 real r2 = e2.toReal();
25 c = Complex!(real)(fmodl(e1.toReal(), r2), fmodl(e1.toImaginary(), r2));;
26 }
27 else if (e2.type.isimaginary())
28 {
29 real i2 = e2.toImaginary();
30 c = Complex!(real)(fmodl(e1.toReal(), i2), fmodl(e1.toImaginary(), i2));
31 }
32 else
33 assert(0);
34
35 if (type.isreal())
36 e = new RealExp(loc, c.re, type);
37 else if (type.isimaginary())
38 e = new RealExp(loc, c.im, type);
39 else if (type.iscomplex())
40 e = new ComplexExp(loc, c, type);
41 else
42 assert(0);
43 }
44 else
45 {
46 long n1;
47 long n2;
48 long n;
49
50 n1 = e1.toInteger();
51 n2 = e2.toInteger();
52 if (n2 == 0)
53 {
54 e2.error("divide by 0");
55 e2 = new IntegerExp(loc, 1, e2.type);
56 n2 = 1;
57 }
58
59 if (e1.type.isunsigned() || e2.type.isunsigned())
60 n = (cast(ulong) n1) % (cast(ulong) n2);
61 else
62 n = n1 % n2;
63
64 e = new IntegerExp(loc, n, type);
65 }
66 return e;
67 }