KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Layout.java
3  *
4  * Copyright (C) 2003 Peter Graves
5  * $Id: Layout.java,v 1.4 2003/12/20 03:05:51 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 public final class Layout extends LispObject
25 {
26     private final LispClass cls;
27     private final LispObject length;
28     private final LispObject instanceSlots; // A list of slot names.
29

30     public Layout(LispClass cls, LispObject length, LispObject instanceSlots)
31     {
32         this.cls = cls;
33         this.length = length;
34         this.instanceSlots = instanceSlots;
35     }
36
37     public LispClass getLispClass()
38     {
39         return cls;
40     }
41
42     // ### make-layout
43
private static final Primitive3 MAKE_LAYOUT =
44         new Primitive3("make-layout", PACKAGE_SYS, false)
45     {
46         public LispObject execute(LispObject first, LispObject second,
47                                   LispObject third)
48             throws ConditionThrowable
49         {
50             try {
51                 return new Layout((LispClass)first, second, third);
52             }
53             catch (ClassCastException JavaDoc e) {
54                 return signal(new TypeError(first, "class"));
55             }
56         }
57
58     };
59
60     // ### layout-class
61
private static final Primitive1 LAYOUT_CLASS =
62         new Primitive1("layout-class", PACKAGE_SYS, false)
63     {
64         public LispObject execute(LispObject arg) throws ConditionThrowable
65         {
66             try {
67                 return ((Layout)arg).cls;
68             }
69             catch (ClassCastException JavaDoc e) {
70                 return signal(new TypeError(arg, "layout"));
71             }
72         }
73     };
74
75     // ### layout-length
76
private static final Primitive1 LAYOUT_LENGTH =
77         new Primitive1("layout-length", PACKAGE_SYS, false)
78     {
79         public LispObject execute(LispObject arg) throws ConditionThrowable
80         {
81             try {
82                 return ((Layout)arg).length;
83             }
84             catch (ClassCastException JavaDoc e) {
85                 return signal(new TypeError(arg, "layout"));
86             }
87         }
88     };
89
90     // ### instance-slot-index
91
// instance-slot-index layout slot-name => index
92
private static final Primitive2 INSTANCE_SLOT_INDEX =
93         new Primitive2("instance-slot-index", PACKAGE_SYS, false)
94     {
95         public LispObject execute(LispObject first, LispObject second)
96             throws ConditionThrowable
97         {
98             try {
99                 LispObject list = ((Layout)first).instanceSlots;
100                 int index = 0;
101                 while (list != NIL) {
102                     if (list.car() == second)
103                         return new Fixnum(index);
104                     list = list.cdr();
105                     ++index;
106                 }
107                 return NIL;
108             }
109             catch (ClassCastException JavaDoc e) {
110                 return signal(new TypeError(first, "layout"));
111             }
112         }
113     };
114 }
115
Popular Tags