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