KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * logand.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: logand.java,v 1.10 2004/03/04 01:26:58 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 // ### logand
27
// logand &rest integers => result-integer
28
public final class logand extends Primitive
29 {
30     private logand()
31     {
32         super("logand", "&rest integers");
33     }
34
35     public LispObject execute()
36     {
37         return Fixnum.MINUS_ONE;
38     }
39
40     public LispObject execute(LispObject first, LispObject second)
41         throws ConditionThrowable
42     {
43         if (first instanceof Fixnum && second instanceof Fixnum) {
44             return new Fixnum(((Fixnum)first).value &
45                               ((Fixnum)second).value);
46         } else {
47             BigInteger JavaDoc n1, n2;
48             if (first instanceof Fixnum)
49                 n1 = ((Fixnum)first).getBigInteger();
50             else if (first instanceof Bignum)
51                 n1 = ((Bignum)first).value;
52             else
53                 return signal(new TypeError(first, Symbol.INTEGER));
54             if (second instanceof Fixnum)
55                 n2 = ((Fixnum)second).getBigInteger();
56             else if (second instanceof Bignum)
57                 n2 = ((Bignum)second).value;
58             else
59                 return signal(new TypeError(second, Symbol.INTEGER));
60             return number(n1.and(n2));
61         }
62     }
63
64     public LispObject execute(LispObject[] args) throws ConditionThrowable
65     {
66         BigInteger JavaDoc result = BigInteger.valueOf(-1);
67         for (int i = 0; i < args.length; i++) {
68             BigInteger JavaDoc n;
69             if (args[i] instanceof Fixnum)
70                 n = ((Fixnum)args[i]).getBigInteger();
71             else if (args[i] instanceof Bignum)
72                 n = ((Bignum)args[i]).value;
73             else
74                 return signal(new TypeError(args[i], Symbol.INTEGER));
75             result = result.and(n);
76         }
77         return number(result);
78     }
79
80     private static final Primitive LOGAND = new logand();
81 }
82
Popular Tags