1 | /* |
---|
2 | * Function.java |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2005 Peter Graves |
---|
5 | * $Id: Function.java 12079 2009-07-31 19:45:54Z ehuelsmann $ |
---|
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 | private int hotCount; |
---|
41 | |
---|
42 | protected Function() {} |
---|
43 | |
---|
44 | public Function(String name) |
---|
45 | { |
---|
46 | if (name != null) { |
---|
47 | Symbol symbol = Symbol.addFunction(name.toUpperCase(), this); |
---|
48 | if (cold) |
---|
49 | symbol.setBuiltInFunction(true); |
---|
50 | setLambdaName(symbol); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | public Function(Symbol symbol, String arglist) |
---|
55 | { |
---|
56 | symbol.setSymbolFunction(this); |
---|
57 | if (cold) |
---|
58 | symbol.setBuiltInFunction(true); |
---|
59 | setLambdaName(symbol); |
---|
60 | setLambdaList(new SimpleString(arglist)); |
---|
61 | } |
---|
62 | |
---|
63 | public Function(Symbol symbol, String arglist, String docstring) |
---|
64 | { |
---|
65 | symbol.setSymbolFunction(this); |
---|
66 | if (cold) |
---|
67 | symbol.setBuiltInFunction(true); |
---|
68 | setLambdaName(symbol); |
---|
69 | setLambdaList(new SimpleString(arglist)); |
---|
70 | if (docstring != null) { |
---|
71 | try { |
---|
72 | symbol.setDocumentation(Symbol.FUNCTION, |
---|
73 | new SimpleString(docstring)); |
---|
74 | } |
---|
75 | catch (ConditionThrowable t) { |
---|
76 | Debug.assertTrue(false); |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | public Function(String name, String arglist) |
---|
82 | { |
---|
83 | this(name); |
---|
84 | setLambdaList(new SimpleString(arglist)); |
---|
85 | } |
---|
86 | |
---|
87 | public Function(String name, Package pkg) |
---|
88 | { |
---|
89 | this(name, pkg, false); |
---|
90 | } |
---|
91 | |
---|
92 | public Function(String name, Package pkg, boolean exported) |
---|
93 | { |
---|
94 | this(name, pkg, exported, null, null); |
---|
95 | } |
---|
96 | |
---|
97 | public Function(String name, Package pkg, boolean exported, |
---|
98 | String arglist) |
---|
99 | { |
---|
100 | this(name, pkg, exported, arglist, null); |
---|
101 | } |
---|
102 | |
---|
103 | public Function(String name, Package pkg, boolean exported, |
---|
104 | String arglist, String docstring) |
---|
105 | { |
---|
106 | if (arglist instanceof String) |
---|
107 | setLambdaList(new SimpleString(arglist)); |
---|
108 | if (name != null) { |
---|
109 | try { |
---|
110 | Symbol symbol; |
---|
111 | if (exported) |
---|
112 | symbol = pkg.internAndExport(name.toUpperCase()); |
---|
113 | else |
---|
114 | symbol = pkg.intern(name.toUpperCase()); |
---|
115 | symbol.setSymbolFunction(this); |
---|
116 | if (cold) |
---|
117 | symbol.setBuiltInFunction(true); |
---|
118 | setLambdaName(symbol); |
---|
119 | if (docstring != null) |
---|
120 | symbol.setDocumentation(Symbol.FUNCTION, |
---|
121 | new SimpleString(docstring)); |
---|
122 | } |
---|
123 | catch (ConditionThrowable t) { |
---|
124 | Debug.assertTrue(false); |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | public Function(LispObject name) |
---|
130 | { |
---|
131 | setLambdaName(name); |
---|
132 | } |
---|
133 | |
---|
134 | public Function(LispObject name, LispObject lambdaList) |
---|
135 | { |
---|
136 | setLambdaName(name); |
---|
137 | setLambdaList(lambdaList); |
---|
138 | } |
---|
139 | |
---|
140 | @Override |
---|
141 | public LispObject typeOf() |
---|
142 | { |
---|
143 | return Symbol.FUNCTION; |
---|
144 | } |
---|
145 | |
---|
146 | @Override |
---|
147 | public LispObject classOf() |
---|
148 | { |
---|
149 | return BuiltInClass.FUNCTION; |
---|
150 | } |
---|
151 | |
---|
152 | @Override |
---|
153 | public LispObject typep(LispObject typeSpecifier) throws ConditionThrowable |
---|
154 | { |
---|
155 | if (typeSpecifier == Symbol.FUNCTION) |
---|
156 | return T; |
---|
157 | if (typeSpecifier == Symbol.COMPILED_FUNCTION) |
---|
158 | return T; |
---|
159 | if (typeSpecifier == BuiltInClass.FUNCTION) |
---|
160 | return T; |
---|
161 | return super.typep(typeSpecifier); |
---|
162 | } |
---|
163 | |
---|
164 | @Override |
---|
165 | public final LispObject getPropertyList() |
---|
166 | { |
---|
167 | if (propertyList == null) |
---|
168 | propertyList = NIL; |
---|
169 | return propertyList; |
---|
170 | } |
---|
171 | |
---|
172 | @Override |
---|
173 | public final void setPropertyList(LispObject obj) |
---|
174 | { |
---|
175 | if (obj == null) |
---|
176 | throw new NullPointerException(); |
---|
177 | propertyList = obj; |
---|
178 | } |
---|
179 | |
---|
180 | public final void setClassBytes(byte[] bytes) throws ConditionThrowable |
---|
181 | { |
---|
182 | propertyList = putf(propertyList, Symbol.CLASS_BYTES, |
---|
183 | new JavaObject(bytes)); |
---|
184 | } |
---|
185 | |
---|
186 | @Override |
---|
187 | public LispObject execute() throws ConditionThrowable |
---|
188 | { |
---|
189 | return error(new WrongNumberOfArgumentsException(this)); |
---|
190 | } |
---|
191 | |
---|
192 | @Override |
---|
193 | public LispObject execute(LispObject arg) throws ConditionThrowable |
---|
194 | { |
---|
195 | return error(new WrongNumberOfArgumentsException(this)); |
---|
196 | } |
---|
197 | |
---|
198 | @Override |
---|
199 | public LispObject execute(LispObject first, LispObject second) |
---|
200 | throws ConditionThrowable |
---|
201 | { |
---|
202 | return error(new WrongNumberOfArgumentsException(this)); |
---|
203 | } |
---|
204 | |
---|
205 | @Override |
---|
206 | public LispObject execute(LispObject first, LispObject second, |
---|
207 | LispObject third) |
---|
208 | throws ConditionThrowable |
---|
209 | { |
---|
210 | return error(new WrongNumberOfArgumentsException(this)); |
---|
211 | } |
---|
212 | |
---|
213 | @Override |
---|
214 | public LispObject execute(LispObject first, LispObject second, |
---|
215 | LispObject third, LispObject fourth) |
---|
216 | throws ConditionThrowable |
---|
217 | { |
---|
218 | return error(new WrongNumberOfArgumentsException(this)); |
---|
219 | } |
---|
220 | |
---|
221 | @Override |
---|
222 | public LispObject execute(LispObject first, LispObject second, |
---|
223 | LispObject third, LispObject fourth, |
---|
224 | LispObject fifth) |
---|
225 | throws ConditionThrowable |
---|
226 | { |
---|
227 | return error(new WrongNumberOfArgumentsException(this)); |
---|
228 | } |
---|
229 | |
---|
230 | @Override |
---|
231 | public LispObject execute(LispObject first, LispObject second, |
---|
232 | LispObject third, LispObject fourth, |
---|
233 | LispObject fifth, LispObject sixth) |
---|
234 | throws ConditionThrowable |
---|
235 | { |
---|
236 | return error(new WrongNumberOfArgumentsException(this)); |
---|
237 | } |
---|
238 | |
---|
239 | @Override |
---|
240 | public LispObject execute(LispObject first, LispObject second, |
---|
241 | LispObject third, LispObject fourth, |
---|
242 | LispObject fifth, LispObject sixth, |
---|
243 | LispObject seventh) |
---|
244 | throws ConditionThrowable |
---|
245 | { |
---|
246 | return error(new WrongNumberOfArgumentsException(this)); |
---|
247 | } |
---|
248 | |
---|
249 | @Override |
---|
250 | public LispObject execute(LispObject first, LispObject second, |
---|
251 | LispObject third, LispObject fourth, |
---|
252 | LispObject fifth, LispObject sixth, |
---|
253 | LispObject seventh, LispObject eighth) |
---|
254 | throws ConditionThrowable |
---|
255 | { |
---|
256 | return error(new WrongNumberOfArgumentsException(this)); |
---|
257 | } |
---|
258 | |
---|
259 | @Override |
---|
260 | public LispObject execute(LispObject[] args) throws ConditionThrowable |
---|
261 | { |
---|
262 | return error(new WrongNumberOfArgumentsException(this)); |
---|
263 | } |
---|
264 | |
---|
265 | @Override |
---|
266 | public String writeToString() throws ConditionThrowable |
---|
267 | { |
---|
268 | LispObject name = getLambdaName(); |
---|
269 | if (name != null && name != NIL) { |
---|
270 | StringBuffer sb = new StringBuffer("#<FUNCTION "); |
---|
271 | sb.append(name.writeToString()); |
---|
272 | sb.append(" {"); |
---|
273 | sb.append(Integer.toHexString(System.identityHashCode(this)).toUpperCase()); |
---|
274 | sb.append("}>"); |
---|
275 | return sb.toString(); |
---|
276 | } |
---|
277 | // No name. |
---|
278 | LispObject lambdaList = getLambdaList(); |
---|
279 | if (lambdaList != null) { |
---|
280 | StringBuffer sb = new StringBuffer("#<FUNCTION "); |
---|
281 | sb.append("(LAMBDA "); |
---|
282 | if (lambdaList == NIL) { |
---|
283 | sb.append("()"); |
---|
284 | } else { |
---|
285 | final LispThread thread = LispThread.currentThread(); |
---|
286 | SpecialBinding lastSpecialBinding = thread.lastSpecialBinding; |
---|
287 | thread.bindSpecial(Symbol.PRINT_LENGTH, Fixnum.THREE); |
---|
288 | try { |
---|
289 | sb.append(lambdaList.writeToString()); |
---|
290 | } |
---|
291 | finally { |
---|
292 | thread.lastSpecialBinding = lastSpecialBinding; |
---|
293 | } |
---|
294 | } |
---|
295 | sb.append(")"); |
---|
296 | sb.append(" {"); |
---|
297 | sb.append(Integer.toHexString(System.identityHashCode(this)).toUpperCase()); |
---|
298 | sb.append("}>"); |
---|
299 | return sb.toString(); |
---|
300 | } |
---|
301 | return unreadableString("FUNCTION"); |
---|
302 | } |
---|
303 | |
---|
304 | // Used by the JVM compiler. |
---|
305 | public final void argCountError() throws ConditionThrowable |
---|
306 | { |
---|
307 | error(new WrongNumberOfArgumentsException(this)); |
---|
308 | } |
---|
309 | |
---|
310 | // Profiling. |
---|
311 | @Override |
---|
312 | public final int getCallCount() |
---|
313 | { |
---|
314 | return callCount; |
---|
315 | } |
---|
316 | |
---|
317 | @Override |
---|
318 | public void setCallCount(int n) |
---|
319 | { |
---|
320 | callCount = n; |
---|
321 | } |
---|
322 | |
---|
323 | @Override |
---|
324 | public final void incrementCallCount() |
---|
325 | { |
---|
326 | ++callCount; |
---|
327 | } |
---|
328 | |
---|
329 | @Override |
---|
330 | public final int getHotCount() |
---|
331 | { |
---|
332 | return hotCount; |
---|
333 | } |
---|
334 | |
---|
335 | @Override |
---|
336 | public void setHotCount(int n) |
---|
337 | { |
---|
338 | hotCount = n; |
---|
339 | } |
---|
340 | |
---|
341 | @Override |
---|
342 | public final void incrementHotCount() |
---|
343 | { |
---|
344 | ++hotCount; |
---|
345 | } |
---|
346 | } |
---|