KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > logior


1 /*
2  * logior.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: logior.java,v 1.6 2004/02/28 16:55:32 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 JavaDoc;
25
26 // ### logior
27
// logior &rest integers => result-integer
28
public final class logior extends Primitive
29 {
30     private logior()
31     {
32         super("logior", "&rest integers");
33     }
34
35     public LispObject execute()
36     {
37         return Fixnum.ZERO;
38     }
39
40     public LispObject execute(LispObject arg) throws ConditionThrowable
41     {
42         if (arg instanceof Fixnum || arg instanceof Bignum)
43             return arg;
44         return signal(new TypeError(arg, Symbol.INTEGER));
45     }
46
47     public LispObject execute(LispObject first, LispObject second)
48         throws ConditionThrowable
49     {
50         if (first instanceof Fixnum && second instanceof Fixnum)
51             return new Fixnum(((Fixnum)first).value | ((Fixnum)second).value);
52         BigInteger JavaDoc n1, n2;
53         if (first instanceof Fixnum)
54             n1 = ((Fixnum)first).getBigInteger();
55         else if (first instanceof Bignum)
56             n1 = ((Bignum)first).value;
57         else
58             return signal(new TypeError(first, Symbol.INTEGER));
59         if (second instanceof Fixnum)
60             n2 = ((Fixnum)second).getBigInteger();
61         else if (second instanceof Bignum)
62             n2 = ((Bignum)second).value;
63         else
64             return signal(new TypeError(second, Symbol.INTEGER));
65         return number(n1.or(n2));
66     }
67
68     public LispObject execute(LispObject[] args) throws ConditionThrowable
69     {
70         final int limit = args.length;
71         int i = 0;
72         // Maybe all the arguments are fixnums.
73
int r = 0;
74         do {
75             if (args[i] instanceof Fixnum) {
76                 r |= ((Fixnum)args[i]).value;
77                 ++i;
78             } else
79                 break;
80         } while (i < limit);
81         if (i == limit)
82             return number(r);
83         // Not all fixnums.
84
BigInteger JavaDoc result = BigInteger.valueOf(r);
85         while (i < limit) {
86             BigInteger JavaDoc n;
87             if (args[i] instanceof Fixnum)
88                 n = ((Fixnum)args[i]).getBigInteger();
89             else if (args[i] instanceof Bignum)
90                 n = ((Bignum)args[i]).value;
91             else
92                 return signal(new TypeError(args[i], Symbol.INTEGER));
93             result = result.or(n);
94             ++i;
95         }
96         return number(result);
97     }
98
99     private static final Primitive LOGIOR = new logior();
100 }
101
Popular Tags