01: package gnu.kawa.functions;
02:
03: import gnu.math.*;
04: import gnu.mapping.*;
05: import gnu.text.Char;
06: import gnu.expr.*;
07:
08: /** Implement that standard Scheme function "eqv?". */
09:
10: public class IsEqv extends Procedure2 implements CanInline {
11: Language language;
12: IsEq isEq;
13:
14: public IsEqv(Language language, String name, IsEq isEq) {
15: this .language = language;
16: this .isEq = isEq;
17: setName(name);
18: }
19:
20: public static boolean apply(Object arg1, Object arg2) {
21: if (arg1 == arg2)
22: return true;
23: if (arg1 instanceof Char || arg1 instanceof Numeric
24: // Symbols can now be equals even if not ==, due to namespace support.
25: || arg1 instanceof Symbol)
26: return arg1.equals(arg2);
27: return false;
28: }
29:
30: public Object apply2(Object arg1, Object arg2) {
31: return language.booleanObject(apply(arg1, arg2));
32: }
33:
34: private static boolean nonNumeric(Expression exp) {
35: if (exp instanceof QuoteExp) {
36: Object value = ((QuoteExp) exp).getValue();
37: return !(value instanceof Numeric || value instanceof Char || value instanceof Symbol);
38: }
39: return false;
40: }
41:
42: public Expression inline(ApplyExp exp, ExpWalker walker) {
43: Expression[] args = exp.getArgs();
44: if (nonNumeric(args[0]) || nonNumeric(args[1]))
45: return new ApplyExp(isEq, args);
46: return exp;
47: }
48: }
|