01: package gnu.jemacs.lang;
02:
03: import gnu.math.*;
04: import gnu.mapping.*;
05:
06: public class NumberCompare extends ProcedureN {
07: // Return codes from Numeric.compare:
08: static final int RESULT_GRT = 1;
09: static final int RESULT_EQU = 0;
10: static final int RESULT_LSS = -1;
11: static final int RESULT_NAN = -2;
12: static final int RESULT_NEQ = -3;
13:
14: static final int TRUE_IF_GRT = 16;
15: static final int TRUE_IF_EQU = 8;
16: static final int TRUE_IF_LSS = 4;
17: static final int TRUE_IF_NAN = 2;
18: static final int TRUE_IF_NEQ = 1;
19: int flags;
20:
21: public static final NumberCompare $Eq = make("=", TRUE_IF_EQU);
22: public static final NumberCompare $Gr = make(">", TRUE_IF_GRT);
23: public static final NumberCompare $Gr$Eq = make(">=", TRUE_IF_GRT
24: | TRUE_IF_EQU);
25: public static final NumberCompare $Ls = make("<", TRUE_IF_LSS);
26: public static final NumberCompare $Ls$Eq = make("<=", TRUE_IF_LSS
27: | TRUE_IF_EQU);
28:
29: public static NumberCompare make(String name, int flags) {
30: NumberCompare proc = new NumberCompare();
31: proc.setName(name);
32: proc.flags = flags;
33: return proc;
34: }
35:
36: public Object apply2(Object arg1, Object arg2) {
37: if (apply2(flags, arg1, arg2))
38: return ELisp.TRUE;
39: else
40: return ELisp.FALSE;
41: }
42:
43: public static boolean apply2(int flags, Object arg1, Object arg2) {
44: Numeric num1 = ELisp.asNumber(arg1);
45: Numeric num2 = ELisp.asNumber(arg2);
46: return ((1 << (3 + num1.compare(num2))) & flags) != 0;
47: }
48:
49: public Object applyN(Object[] args) {
50: // if (args.length < 2)
51: // throw new WrongArguments(this.name(),2,"(< x1 x2 ...)");
52: for (int i = 0; i < args.length - 1; i++) {
53: Object arg1 = args[i];
54: Object arg2 = args[i + 1];
55: if (!apply2(flags, arg1, arg2))
56: return ELisp.FALSE;
57: }
58: return ELisp.TRUE;
59: }
60: }
|