Source code: com/memoire/foo/FooFix.java
1 /**
2 * @modification $Date: 2002/12/16 18:56:25 $
3 * @statut unstable
4 * @file FooFix.java
5 * @version 0.08
6 * @author Guillaume Desnoix
7 * @email guillaume@desnoix.com
8 * @license GNU General Public License 2 (GPL2)
9 * @copyright 1999-2001 Guillaume Desnoix
10 */
11
12 package com.memoire.foo;
13 import com.memoire.foo.*;
14
15 public class FooFix
16 {
17 private static FooCategory pkg_=null;
18
19 public static final FooCategory init()
20 {
21 if(pkg_==null)
22 {
23 pkg_=FooCategory.create("&fix");
24 FooCategory.create(Integer.class).alias("&integer").extend(pkg_);
25 FooCategory.create(Byte .class).alias("&byte" ).extend(pkg_);
26 FooCategory.create(Short .class).alias("&short" ).extend(pkg_);
27 FooCategory.create(Long .class).alias("&long" ).extend(pkg_);
28
29 pkg_.setMessage("abs" ,Math.class,"abs(int)");
30 pkg_.setMessage("max" ,Math.class,"max(int,int)");
31 pkg_.setMessage("min" ,Math.class,"min(int,int)");
32
33 pkg_.setMessage("+" ,FooFix.class,"addMsg");
34 pkg_.setMessage("-" ,FooFix.class,"subMsg");
35 pkg_.setMessage("*" ,FooFix.class,"mulMsg");
36 pkg_.setMessage("/" ,FooFix.class,"divMsg");
37 pkg_.setMessage("&" ,FooFix.class,"andMsg");
38 pkg_.setMessage(":" ,FooFix.class,"orMsg");
39 pkg_.setMessage("^" ,FooFix.class,"xorMsg");
40
41 pkg_.setMessage("<" ,FooFix.class,"ltMsg");
42 pkg_.setMessage("<=",FooFix.class,"leMsg");
43 pkg_.setMessage(">" ,FooFix.class,"gtMsg");
44 pkg_.setMessage(">=",FooFix.class,"geMsg");
45 pkg_.setMessage("==",FooFix.class,"eqMsg");
46 pkg_.setMessage("!=",FooFix.class,"neMsg");
47 pkg_.setMessage("~" ,FooFix.class,"mtMsg");
48 pkg_.setMessage("!~",FooFix.class,"nmMsg");
49 pkg_.setMessage("²" ,FooFix.class,"sqrMsg");
50 pkg_.setMessage("**",FooFix.class,"powMsg");
51
52 pkg_.setMessage("times",FooFix.class,"times",false);
53 pkg_.setMessage(".." ,FooFix.class,"range");
54 }
55
56 return pkg_;
57 }
58
59 private static final Integer[] INTS;
60
61 static
62 {
63 INTS=new Integer[21];
64 for(int i=0;i<21;i++) INTS[i]=new Integer(i);
65 }
66 //private static final Integer INT_ONE =new Integer(1);
67 //private static final Integer INT_TWO =new Integer(2);
68
69 public static Integer create(int _value)
70 {
71 return (_value<21 ? INTS[_value] : new Integer(_value));
72 }
73
74 public static Number addMsg(Object[] _n)
75 {
76 return operation(_n,0);
77 }
78
79 public static Number subMsg(Object[] _n)
80 {
81 return operation(_n,1);
82 }
83
84 public static Number mulMsg(Object[] _n)
85 {
86 return operation(_n,2);
87 }
88
89 public static Number divMsg(Object[] _n)
90 {
91 return operation(_n,3);
92 }
93
94 public static Number andMsg(Object[] _n)
95 {
96 return operation(_n,4);
97 }
98
99 public static Number orMsg(Object[] _n)
100 {
101 return operation(_n,5);
102 }
103
104 public static Number xorMsg(Object[] _n)
105 {
106 return operation(_n,6);
107 }
108
109 private static Number operation(Object[] _n, int _op)
110 {
111 FooLib.checkClassArgument(Number.class,_n[0],0);
112 int r=((Number)_n[0]).intValue();
113 for(int i=1;i<_n.length;i++)
114 {
115 FooLib.checkClassArgument(Number.class,_n[i],i);
116 int a=((Number)_n[i]).intValue();
117 switch(_op)
118 {
119 case 0: r+=a; break;
120 case 1: r-=a; break;
121 case 2: r*=a; break;
122 case 3: r/=a; break;
123 case 4: r&=a; break;
124 case 5: r|=a; break;
125 case 6: r^=a; break;
126 }
127 }
128 return create(r);
129 }
130
131 public static Boolean ltMsg(Object[] _n)
132 {
133 return comparison(_n,0);
134 }
135
136 public static Boolean leMsg(Object[] _n)
137 {
138 return comparison(_n,1);
139 }
140
141 public static Boolean gtMsg(Object[] _n)
142 {
143 return comparison(_n,2);
144 }
145
146 public static Boolean geMsg(Object[] _n)
147 {
148 return comparison(_n,3);
149 }
150
151 public static Boolean eqMsg(Object[] _n)
152 {
153 return comparison(_n,4);
154 }
155
156 public static Boolean neMsg(Object[] _n)
157 {
158 return comparison(_n,5);
159 }
160
161 public static Boolean mtMsg(Object[] _n)
162 {
163 return comparison(_n,6);
164 }
165
166 public static Boolean nmMsg(Object[] _n)
167 {
168 return comparison(_n,7);
169 }
170
171 private static Boolean comparison(Object[] _n, int _op)
172 {
173 FooLib.checkClassArgument(Number.class,_n[0],0);
174 boolean r=true;
175 int v=((Number)_n[0]).intValue();
176 for(int i=1;i<_n.length;i++)
177 {
178 FooLib.checkClassArgument(Number.class,_n[i],i);
179 int a=((Number)_n[i]).intValue();
180 switch(_op)
181 {
182 case 0: r&=(v< a); break;
183 case 1: r&=(v<=a); break;
184 case 2: r&=(v> a); break;
185 case 3: r&=(v>=a); break;
186 case 4: r&=(v==a); break;
187 case 5: r&=(v!=a); break;
188 case 6: r&=(v==a); break;
189 case 7: r&=(v!=a); break;
190 }
191 }
192 return FooBoolean.create(r);
193 }
194
195 public static Number sqrMsg(Number _a)
196 {
197 int a=_a.intValue();
198 return create(a*a);
199 }
200
201 public static Number powMsg(Number _a,Integer _b)
202 {
203 int a=_a.intValue();
204 int b=_b.intValue();
205 return create((int)Math.pow(a,b));
206 }
207
208 //tmp
209 public static Object times(Object[] _body)
210 {
211 int n=((Number)_body[0]).intValue();
212 Object r=null;
213 while(n>0) { r=FooLib.evalBlock(_body,1); n--; }
214 return r;
215 }
216
217 public static FooList range(Number _a, Number _b)
218 {
219 int a=_a.intValue();
220 int b=_b.intValue();
221
222 FooList r=new FooList();
223 while(a<=b) { r.add(new Integer(a)); a++; }
224 return r;
225 }
226 }