01: /*
02: * logior.java
03: *
04: * Copyright (C) 2003-2004 Peter Graves
05: * $Id: logior.java,v 1.6 2004/02/28 16:55:32 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: // ### logior
27: // logior &rest integers => result-integer
28: public final class logior extends Primitive {
29: private logior() {
30: super ("logior", "&rest integers");
31: }
32:
33: public LispObject execute() {
34: return Fixnum.ZERO;
35: }
36:
37: public LispObject execute(LispObject arg) throws ConditionThrowable {
38: if (arg instanceof Fixnum || arg instanceof Bignum)
39: return arg;
40: return signal(new TypeError(arg, Symbol.INTEGER));
41: }
42:
43: public LispObject execute(LispObject first, LispObject second)
44: throws ConditionThrowable {
45: if (first instanceof Fixnum && second instanceof Fixnum)
46: return new Fixnum(((Fixnum) first).value
47: | ((Fixnum) second).value);
48: BigInteger n1, n2;
49: if (first instanceof Fixnum)
50: n1 = ((Fixnum) first).getBigInteger();
51: else if (first instanceof Bignum)
52: n1 = ((Bignum) first).value;
53: else
54: return signal(new TypeError(first, Symbol.INTEGER));
55: if (second instanceof Fixnum)
56: n2 = ((Fixnum) second).getBigInteger();
57: else if (second instanceof Bignum)
58: n2 = ((Bignum) second).value;
59: else
60: return signal(new TypeError(second, Symbol.INTEGER));
61: return number(n1.or(n2));
62: }
63:
64: public LispObject execute(LispObject[] args)
65: throws ConditionThrowable {
66: final int limit = args.length;
67: int i = 0;
68: // Maybe all the arguments are fixnums.
69: int r = 0;
70: do {
71: if (args[i] instanceof Fixnum) {
72: r |= ((Fixnum) args[i]).value;
73: ++i;
74: } else
75: break;
76: } while (i < limit);
77: if (i == limit)
78: return number(r);
79: // Not all fixnums.
80: BigInteger result = BigInteger.valueOf(r);
81: while (i < limit) {
82: BigInteger n;
83: if (args[i] instanceof Fixnum)
84: n = ((Fixnum) args[i]).getBigInteger();
85: else if (args[i] instanceof Bignum)
86: n = ((Bignum) args[i]).value;
87: else
88: return signal(new TypeError(args[i], Symbol.INTEGER));
89: result = result.or(n);
90: ++i;
91: }
92: return number(result);
93: }
94:
95: private static final Primitive LOGIOR = new logior();
96: }
|