01: package gnu.jemacs.lang;
02:
03: import gnu.math.*;
04: import gnu.mapping.*;
05:
06: public class AddOp extends ProcedureN {
07: int plusOrMinus = 1;
08:
09: public AddOp(String name, int plusOrMinus) {
10: setName(name);
11: this .plusOrMinus = plusOrMinus;
12: }
13:
14: public static final AddOp $Pl = new AddOp("+", 1);
15: public static final AddOp $Mn = new AddOp("-", -1);
16:
17: public static Object apply2(int plusOrMinus, Object arg1,
18: Object arg2) {
19: Numeric num1 = ELisp.asNumber(arg1);
20: Numeric num2 = ELisp.asNumber(arg2);
21: return num1.add(num2, plusOrMinus);
22: }
23:
24: public static Object $Pl(Object arg1, Object arg2) {
25: return apply2(1, arg1, arg2);
26: }
27:
28: public static Object $Mn(Object arg1, Object arg2) {
29: return apply2(-1, arg1, arg2);
30: }
31:
32: public static Object $Mn(Object arg1) {
33: return ELisp.asNumber(arg1).neg();
34: }
35:
36: public static Object $Pl$V(Object arg1, Object arg2, Object arg3,
37: Object[] rest) {
38: return applyN(1, apply2(1, apply2(1, arg1, arg2), arg3), rest);
39: }
40:
41: public static Object $Mn$V(Object arg1, Object arg2, Object arg3,
42: Object[] rest) {
43: return applyN(-1, apply2(-1, apply2(-1, arg1, arg2), arg3),
44: rest);
45: }
46:
47: public static Object applyN(int plusOrMinus, Object[] args) {
48: int len = args.length;
49: if (len == 0)
50: return IntNum.zero();
51: Object result = args[0];
52: if (len == 1 && plusOrMinus < 0)
53: return $Mn(result);
54: for (int i = 1; i < len; i++)
55: result = apply2(plusOrMinus, result, args[i]);
56: return result;
57: }
58:
59: public static Object applyN(int plusOrMinus, Object init,
60: Object[] args) {
61: int len = args.length;
62: Object result = init;
63: for (int i = 0; i < len; i++)
64: result = apply2(plusOrMinus, result, args[i]);
65: return result;
66: }
67:
68: public Object applyN(Object[] args) {
69: return applyN(plusOrMinus, args);
70: }
71: }
|