1 | /* |
---|
2 | * Function.java |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2005 Peter Graves |
---|
5 | * $Id: Function.java 11391 2008-11-15 22:38:34Z vvoutilainen $ |
---|
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 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | public abstract class Function extends Operator |
---|
37 | { |
---|
38 | private LispObject propertyList = NIL; |
---|
39 | private int callCount; |
---|
40 | |
---|
41 | protected Function() {} |
---|
42 | |
---|
43 | public Function(String name) |
---|
44 | { |
---|
45 | if (name != null) { |
---|
46 | Symbol symbol = Symbol.addFunction(name.toUpperCase(), this); |
---|
47 | if (cold) |
---|
48 | symbol.setBuiltInFunction(true); |
---|
49 | setLambdaName(symbol); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | public Function(Symbol symbol, String arglist) |
---|
54 | { |
---|
55 | symbol.setSymbolFunction(this); |
---|
56 | if (cold) |
---|
57 | symbol.setBuiltInFunction(true); |
---|
58 | setLambdaName(symbol); |
---|
59 | setLambdaList(new SimpleString(arglist)); |
---|
60 | } |
---|
61 | |
---|
62 | public Function(Symbol symbol, String arglist, String docstring) |
---|
63 | { |
---|
64 | symbol.setSymbolFunction(this); |
---|
65 | if (cold) |
---|
66 | symbol.setBuiltInFunction(true); |
---|
67 | setLambdaName(symbol); |
---|
68 | setLambdaList(new SimpleString(arglist)); |
---|
69 | if (docstring != null) { |
---|
70 | try { |
---|
71 | symbol.setDocumentation(Symbol.FUNCTION, |
---|
72 | new SimpleString(docstring)); |
---|
73 | } |
---|
74 | catch (ConditionThrowable t) { |
---|
75 | Debug.assertTrue(false); |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | public Function(String name, String arglist) |
---|
81 | { |
---|
82 | this(name); |
---|
83 | setLambdaList(new SimpleString(arglist)); |
---|
84 | } |
---|
85 | |
---|
86 | public Function(String name, Package pkg) |
---|
87 | { |
---|
88 | this(name, pkg, false); |
---|
89 | } |
---|
90 | |
---|
91 | public Function(String name, Package pkg, boolean exported) |
---|
92 | { |
---|
93 | this(name, pkg, exported, null, null); |
---|
94 | } |
---|
95 | |
---|
96 | public Function(String name, Package pkg, boolean exported, |
---|
97 | String arglist) |
---|
98 | { |
---|
99 | this(name, pkg, exported, arglist, null); |
---|
100 | } |
---|
101 | |
---|
102 | public Function(String name, Package pkg, boolean exported, |
---|
103 | String arglist, String docstring) |
---|
104 | { |
---|
105 | if (arglist instanceof String) |
---|
106 | setLambdaList(new SimpleString(arglist)); |
---|
107 | if (name != null) { |
---|
108 | try { |
---|
109 | Symbol symbol; |
---|
110 | if (exported) |
---|
111 | symbol = pkg.internAndExport(name.toUpperCase()); |
---|
112 | else |
---|
113 | symbol = pkg.intern(name.toUpperCase()); |
---|
114 | symbol.setSymbolFunction(this); |
---|
115 | if (cold) |
---|
116 | symbol.setBuiltInFunction(true); |
---|
117 | setLambdaName(symbol); |
---|
118 | if (docstring != null) |
---|
119 | symbol.setDocumentation(Symbol.FUNCTION, |
---|
120 | new SimpleString(docstring)); |
---|
121 | } |
---|
122 | catch (ConditionThrowable t) { |
---|
123 | Debug.assertTrue(false); |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | public Function(LispObject name) |
---|
129 | { |
---|
130 | setLambdaName(name); |
---|
131 | } |
---|
132 | |
---|
133 | public Function(LispObject name, LispObject lambdaList) |
---|
134 | { |
---|
135 | setLambdaName(name); |
---|
136 | setLambdaList(lambdaList); |
---|
137 | } |
---|
138 | |
---|
139 | public LispObject typeOf() |
---|
140 | { |
---|
141 | return Symbol.FUNCTION; |
---|
142 | } |
---|
143 | |
---|
144 | public LispObject classOf() |
---|
145 | { |
---|
146 | return BuiltInClass.FUNCTION; |
---|
147 | } |
---|
148 | |
---|
149 | public LispObject typep(LispObject typeSpecifier) throws ConditionThrowable |
---|
150 | { |
---|
151 | if (typeSpecifier == Symbol.FUNCTION) |
---|
152 | return T; |
---|
153 | if (typeSpecifier == Symbol.COMPILED_FUNCTION) |
---|
154 | return T; |
---|
155 | if (typeSpecifier == BuiltInClass.FUNCTION) |
---|
156 | return T; |
---|
157 | return super.typep(typeSpecifier); |
---|
158 | } |
---|
159 | |
---|
160 | public final LispObject getPropertyList() |
---|
161 | { |
---|
162 | if (propertyList == null) |
---|
163 | propertyList = NIL; |
---|
164 | return propertyList; |
---|
165 | } |
---|
166 | |
---|
167 | public final void setPropertyList(LispObject obj) |
---|
168 | { |
---|
169 | if (obj == null) |
---|
170 | throw new NullPointerException(); |
---|
171 | propertyList = obj; |
---|
172 | } |
---|
173 | |
---|
174 | public final void setClassBytes(byte[] bytes) throws ConditionThrowable |
---|
175 | { |
---|
176 | propertyList = putf(propertyList, Symbol.CLASS_BYTES, |
---|
177 | new JavaObject(bytes)); |
---|
178 | } |
---|
179 | |
---|
180 | public LispObject execute() throws ConditionThrowable |
---|
181 | { |
---|
182 | return error(new WrongNumberOfArgumentsException(this)); |
---|
183 | } |
---|
184 | |
---|
185 | public LispObject execute(LispObject arg) throws ConditionThrowable |
---|
186 | { |
---|
187 | return error(new WrongNumberOfArgumentsException(this)); |
---|
188 | } |
---|
189 | |
---|
190 | public LispObject execute(LispObject first, LispObject second) |
---|
191 | throws ConditionThrowable |
---|
192 | { |
---|
193 | return error(new WrongNumberOfArgumentsException(this)); |
---|
194 | } |
---|
195 | |
---|
196 | public LispObject execute(LispObject first, LispObject second, |
---|
197 | LispObject third) |
---|
198 | throws ConditionThrowable |
---|
199 | { |
---|
200 | return error(new WrongNumberOfArgumentsException(this)); |
---|
201 | } |
---|
202 | |
---|
203 | public LispObject execute(LispObject first, LispObject second, |
---|
204 | LispObject third, LispObject fourth) |
---|
205 | throws ConditionThrowable |
---|
206 | { |
---|
207 | return error(new WrongNumberOfArgumentsException(this)); |
---|
208 | } |
---|
209 | |
---|
210 | public LispObject execute(LispObject first, LispObject second, |
---|
211 | LispObject third, LispObject fourth, |
---|
212 | LispObject fifth) |
---|
213 | throws ConditionThrowable |
---|
214 | { |
---|
215 | return error(new WrongNumberOfArgumentsException(this)); |
---|
216 | } |
---|
217 | |
---|
218 | public LispObject execute(LispObject first, LispObject second, |
---|
219 | LispObject third, LispObject fourth, |
---|
220 | LispObject fifth, LispObject sixth) |
---|
221 | throws ConditionThrowable |
---|
222 | { |
---|
223 | return error(new WrongNumberOfArgumentsException(this)); |
---|
224 | } |
---|
225 | |
---|
226 | public LispObject execute(LispObject first, LispObject second, |
---|
227 | LispObject third, LispObject fourth, |
---|
228 | LispObject fifth, LispObject sixth, |
---|
229 | LispObject seventh) |
---|
230 | throws ConditionThrowable |
---|
231 | { |
---|
232 | return error(new WrongNumberOfArgumentsException(this)); |
---|
233 | } |
---|
234 | |
---|
235 | public LispObject execute(LispObject first, LispObject second, |
---|
236 | LispObject third, LispObject fourth, |
---|
237 | LispObject fifth, LispObject sixth, |
---|
238 | LispObject seventh, LispObject eighth) |
---|
239 | throws ConditionThrowable |
---|
240 | { |
---|
241 | return error(new WrongNumberOfArgumentsException(this)); |
---|
242 | } |
---|
243 | |
---|
244 | public LispObject execute(LispObject[] args) throws ConditionThrowable |
---|
245 | { |
---|
246 | return error(new WrongNumberOfArgumentsException(this)); |
---|
247 | } |
---|
248 | |
---|
249 | public String writeToString() throws ConditionThrowable |
---|
250 | { |
---|
251 | LispObject name = getLambdaName(); |
---|
252 | if (name != null && name != NIL) { |
---|
253 | StringBuffer sb = new StringBuffer("#<FUNCTION "); |
---|
254 | sb.append(name.writeToString()); |
---|
255 | sb.append(" {"); |
---|
256 | sb.append(Integer.toHexString(System.identityHashCode(this)).toUpperCase()); |
---|
257 | sb.append("}>"); |
---|
258 | return sb.toString(); |
---|
259 | } |
---|
260 | // No name. |
---|
261 | LispObject lambdaList = getLambdaList(); |
---|
262 | if (lambdaList != null) { |
---|
263 | StringBuffer sb = new StringBuffer("#<FUNCTION "); |
---|
264 | sb.append("(LAMBDA "); |
---|
265 | if (lambdaList == NIL) { |
---|
266 | sb.append("()"); |
---|
267 | } else { |
---|
268 | final LispThread thread = LispThread.currentThread(); |
---|
269 | SpecialBinding lastSpecialBinding = thread.lastSpecialBinding; |
---|
270 | thread.bindSpecial(Symbol.PRINT_LENGTH, Fixnum.THREE); |
---|
271 | try { |
---|
272 | sb.append(lambdaList.writeToString()); |
---|
273 | } |
---|
274 | finally { |
---|
275 | thread.lastSpecialBinding = lastSpecialBinding; |
---|
276 | } |
---|
277 | } |
---|
278 | sb.append(")"); |
---|
279 | sb.append(" {"); |
---|
280 | sb.append(Integer.toHexString(System.identityHashCode(this)).toUpperCase()); |
---|
281 | sb.append("}>"); |
---|
282 | return sb.toString(); |
---|
283 | } |
---|
284 | return unreadableString("FUNCTION"); |
---|
285 | } |
---|
286 | |
---|
287 | // Used by the JVM compiler. |
---|
288 | public final void argCountError() throws ConditionThrowable |
---|
289 | { |
---|
290 | error(new WrongNumberOfArgumentsException(this)); |
---|
291 | } |
---|
292 | |
---|
293 | // Profiling. |
---|
294 | public final int getCallCount() |
---|
295 | { |
---|
296 | return callCount; |
---|
297 | } |
---|
298 | |
---|
299 | public void setCallCount(int n) |
---|
300 | { |
---|
301 | callCount = n; |
---|
302 | } |
---|
303 | |
---|
304 | public final void incrementCallCount() |
---|
305 | { |
---|
306 | ++callCount; |
---|
307 | } |
---|
308 | } |
---|