KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > functions > MultiplyOp


1 // Copyright (c) 2005 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.kawa.functions;
5 import gnu.math.*;
6 import java.math.*;
7 import gnu.expr.*;
8 import gnu.mapping.*;
9
10 /**
11  * Implement the Scheme standard function "*".
12  * @author Per Bothner
13  */

14
15 public class MultiplyOp extends ProcedureN implements CanInline
16 {
17   public static final MultiplyOp $St = new MultiplyOp("*");
18
19   public MultiplyOp(String JavaDoc name)
20   {
21     super(name);
22   }
23
24   public static Object JavaDoc apply (Object JavaDoc arg1, Object JavaDoc arg2)
25   {
26     return ((Numeric) arg1).mul(arg2);
27   }
28
29   public Object JavaDoc applyN (Object JavaDoc[] args)
30   {
31     int len = args.length;
32     if (len == 0)
33       return IntNum.one ();
34     Number JavaDoc result = (Number JavaDoc) args[0];
35     int code = Arithmetic.classifyValue(result);
36     for (int i = 1; i < len; i++)
37       {
38     Object JavaDoc arg2 = args[i];
39     int code2 = Arithmetic.classifyValue(arg2);
40     code = code < code2 ? code2 : code;
41
42     switch (code)
43       {
44       case Arithmetic.INT_CODE:
45         int i1 = Arithmetic.asInt(result);
46         int i2 = Arithmetic.asInt(arg2);
47         result = new Integer JavaDoc(i1 * i2);
48         break;
49       case Arithmetic.LONG_CODE:
50         long l1 = Arithmetic.asLong(result);
51         long l2 = Arithmetic.asLong(arg2);
52         result = new Long JavaDoc(l1 * l2);
53         break;
54       case Arithmetic.BIGINTEGER_CODE:
55         BigInteger bi1 = Arithmetic.asBigInteger(result);
56         BigInteger bi2 = Arithmetic.asBigInteger(arg2);
57         result = bi1.multiply(bi2);
58         break;
59       case Arithmetic.INTNUM_CODE:
60         result = IntNum.times(Arithmetic.asIntNum(result),
61                   Arithmetic.asIntNum(arg2));
62         break;
63       case Arithmetic.BIGDECIMAL_CODE:
64         BigDecimal bd1 = Arithmetic.asBigDecimal(result);
65         BigDecimal bd2 = Arithmetic.asBigDecimal(arg2);
66         result = bd1.multiply(bd2);
67         break;
68       case Arithmetic.RATNUM_CODE:
69         result = RatNum.times(Arithmetic.asRatNum(result),
70                   Arithmetic.asRatNum(arg2));
71         break;
72       case Arithmetic.FLOAT_CODE:
73         float f1 = Arithmetic.asFloat(result);
74         float f2 = Arithmetic.asFloat(arg2);
75         result = new Float JavaDoc(f1 * f2);
76         break;
77       case Arithmetic.DOUBLE_CODE:
78         double d1 = Arithmetic.asDouble(result);
79         double d2 = Arithmetic.asDouble(arg2);
80         result = new Double JavaDoc(d1 * d2);
81         break;
82       case Arithmetic.FLONUM_CODE:
83         d1 = Arithmetic.asDouble(result);
84         d2 = Arithmetic.asDouble(arg2);
85         result = new DFloNum(d1 * d2);
86         break;
87       default:
88         result = Arithmetic.asNumeric(result)
89           .mul(Arithmetic.asNumeric(arg2));
90       }
91       }
92     return result;
93    }
94
95   public Expression inline (ApplyExp exp, ExpWalker walker)
96   {
97     if (! walker.getCompilation().mustCompile)
98       return exp;
99     Expression folded = exp.inlineIfConstant(this, walker);
100     if (folded != exp)
101       return folded;
102     Expression[] args = exp.getArgs();
103     if (args.length > 2)
104       return AddOp.pairwise(this, exp.getFunction(), args, walker);
105     if (args.length == 2)
106       return AddOp.primInline(104, exp);
107         
108     return exp;
109   }
110 }
111
Popular Tags