01: /*
02: * logxor.java
03: *
04: * Copyright (C) 2003-2004 Peter Graves
05: * $Id: logxor.java,v 1.5 2004/02/28 17:44:46 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.lisp;
23:
24: import java.math.BigInteger;
25:
26: // ### logxor
27: // logxor &rest integers => result-integer
28: // exclusive or
29: public final class logxor extends Primitive {
30: private logxor() {
31: super ("logxor", "&rest integers");
32: }
33:
34: public LispObject execute() {
35: return Fixnum.ZERO;
36: }
37:
38: public LispObject execute(LispObject arg) throws ConditionThrowable {
39: if (arg instanceof Fixnum || arg instanceof Bignum)
40: return arg;
41: return signal(new TypeError(arg, Symbol.INTEGER));
42: }
43:
44: public LispObject execute(LispObject first, LispObject second)
45: throws ConditionThrowable {
46: if (first instanceof Fixnum && second instanceof Fixnum)
47: return new Fixnum(((Fixnum) first).value
48: ^ ((Fixnum) second).value);
49: BigInteger n1, n2;
50: if (first instanceof Fixnum)
51: n1 = ((Fixnum) first).getBigInteger();
52: else if (first instanceof Bignum)
53: n1 = ((Bignum) first).value;
54: else
55: return signal(new TypeError(first, Symbol.INTEGER));
56: if (second instanceof Fixnum)
57: n2 = ((Fixnum) second).getBigInteger();
58: else if (second instanceof Bignum)
59: n2 = ((Bignum) second).value;
60: else
61: return signal(new TypeError(second, Symbol.INTEGER));
62: return number(n1.xor(n2));
63: }
64:
65: public LispObject execute(LispObject[] args)
66: throws ConditionThrowable {
67: final int limit = args.length;
68: int i = 0;
69: // Maybe all the arguments are fixnums.
70: int r = 0;
71: do {
72: if (args[i] instanceof Fixnum) {
73: r ^= ((Fixnum) args[i]).value;
74: ++i;
75: } else
76: break;
77: } while (i < limit);
78: if (i == limit)
79: return number(r);
80: // Not all fixnums.
81: BigInteger result = BigInteger.valueOf(r);
82: while (i < limit) {
83: BigInteger n;
84: if (args[i] instanceof Fixnum)
85: n = ((Fixnum) args[i]).getBigInteger();
86: else if (args[i] instanceof Bignum)
87: n = ((Bignum) args[i]).value;
88: else
89: return signal(new TypeError(args[i], Symbol.INTEGER));
90: result = result.xor(n);
91: ++i;
92: }
93: return number(result);
94: }
95:
96: private static final Primitive LOGXOR = new logxor();
97: }
|