KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Function.java
3  *
4  * Copyright (C) 2002-2004 Peter Graves
5  * $Id: Function.java,v 1.38 2004/07/09 17:38:29 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 abstract class Function extends Functional
25 {
26     private final Symbol symbol;
27
28     private int callCount;
29
30     protected Function()
31     {
32         symbol = null;
33     }
34
35     public Function(String JavaDoc name)
36     {
37         if (name != null) {
38             symbol = Symbol.addFunction(name.toUpperCase(), this);
39             if (cold)
40                 symbol.setBuiltInFunction(true);
41             setLambdaName(symbol);
42         } else
43             symbol = null;
44     }
45
46     public Function(String JavaDoc name, String JavaDoc arglist)
47     {
48         this(name);
49         setArglist(new SimpleString(arglist));
50     }
51
52     public Function(String JavaDoc name, Package JavaDoc pkg)
53     {
54         this(name, pkg, false);
55     }
56
57     public Function(String JavaDoc name, Package JavaDoc pkg, boolean exported)
58     {
59         this(name, pkg, exported, null, null);
60     }
61
62     public Function(String JavaDoc name, Package JavaDoc pkg, boolean exported,
63                     String JavaDoc arglist)
64     {
65         this(name, pkg, exported, arglist, null);
66     }
67
68     public Function(String JavaDoc name, Package JavaDoc pkg, boolean exported,
69                     String JavaDoc arglist, String JavaDoc docstring)
70     {
71         if (arglist instanceof String JavaDoc)
72             setArglist(new SimpleString(arglist));
73         if (name != null) {
74             symbol = pkg.intern(name.toUpperCase());
75             symbol.setSymbolFunction(this);
76             if (cold)
77                 symbol.setBuiltInFunction(true);
78             setLambdaName(symbol);
79             if (exported) {
80                 try {
81                     pkg.export(symbol);
82                 }
83                 catch (ConditionThrowable t) {
84                     Debug.assertTrue(false);
85                 }
86             }
87             if (docstring != null) {
88                 try {
89                     symbol.setFunctionDocumentation(docstring);
90                 }
91                 catch (ConditionThrowable t) {
92                     Debug.assertTrue(false);
93                 }
94             }
95         } else
96             symbol = null;
97     }
98
99     public Function(Symbol symbol)
100     {
101         this.symbol = symbol;
102     }
103
104     public LispObject typeOf()
105     {
106         return Symbol.FUNCTION;
107     }
108
109     public LispClass classOf()
110     {
111         return BuiltInClass.FUNCTION;
112     }
113
114     public LispObject typep(LispObject typeSpecifier) throws ConditionThrowable
115     {
116         if (typeSpecifier == Symbol.FUNCTION)
117             return T;
118         if (typeSpecifier == Symbol.COMPILED_FUNCTION)
119             return T;
120         if (typeSpecifier == BuiltInClass.FUNCTION)
121             return T;
122         return super.typep(typeSpecifier);
123     }
124
125     public Symbol getSymbol()
126     {
127         return symbol;
128     }
129
130     public final String JavaDoc getName()
131     {
132         return symbol != null ? symbol.getName() : null;
133     }
134
135     // Primitive0
136
public LispObject execute() throws ConditionThrowable
137     {
138         return signal(new WrongNumberOfArgumentsException(this));
139     }
140
141     // Primitive1
142
public LispObject execute(LispObject arg) throws ConditionThrowable
143     {
144         return signal(new WrongNumberOfArgumentsException(this));
145     }
146
147     // Primitive2
148
public LispObject execute(LispObject first, LispObject second)
149         throws ConditionThrowable
150     {
151         return signal(new WrongNumberOfArgumentsException(this));
152     }
153
154     // Primitive3
155
public LispObject execute(LispObject first, LispObject second,
156                               LispObject third)
157         throws ConditionThrowable
158     {
159         return signal(new WrongNumberOfArgumentsException(this));
160     }
161
162     // Primitive4
163
public LispObject execute(LispObject first, LispObject second,
164                               LispObject third, LispObject fourth)
165         throws ConditionThrowable
166     {
167         return signal(new WrongNumberOfArgumentsException(this));
168     }
169
170     // Primitive
171
public LispObject execute(LispObject[] args) throws ConditionThrowable
172     {
173         return signal(new WrongNumberOfArgumentsException(this));
174     }
175
176     public String JavaDoc writeToString() throws ConditionThrowable
177     {
178         LispObject name = getLambdaName();
179         if (name == null)
180             name = getSymbol();
181         if (name != null) {
182             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("#<FUNCTION");
183             sb.append(' ');
184             sb.append(name.writeToString());
185             sb.append('>');
186             return sb.toString();
187         } else
188             return unreadableString("FUNCTION");
189     }
190
191     // Profiling.
192
public final int getCallCount()
193     {
194         return callCount;
195     }
196
197     public void setCallCount(int n)
198     {
199         callCount = n;
200     }
201
202     public final void incrementCallCount()
203     {
204         ++callCount;
205     }
206 }
207
Popular Tags