1 | /* |
---|
2 | * Function.java |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2005 Peter Graves |
---|
5 | * $Id: Function.java 11698 2009-03-05 23:20:43Z astalla $ |
---|
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 | @Override |
---|
140 | public LispObject typeOf() |
---|
141 | { |
---|
142 | return Symbol.FUNCTION; |
---|
143 | } |
---|
144 | |
---|
145 | @Override |
---|
146 | public LispObject classOf() |
---|
147 | { |
---|
148 | return BuiltInClass.FUNCTION; |
---|
149 | } |
---|
150 | |
---|
151 | @Override |
---|
152 | public LispObject typep(LispObject typeSpecifier) throws ConditionThrowable |
---|
153 | { |
---|
154 | if (typeSpecifier == Symbol.FUNCTION) |
---|
155 | return T; |
---|
156 | if (typeSpecifier == Symbol.COMPILED_FUNCTION) |
---|
157 | return T; |
---|
158 | if (typeSpecifier == BuiltInClass.FUNCTION) |
---|
159 | return T; |
---|
160 | return super.typep(typeSpecifier); |
---|
161 | } |
---|
162 | |
---|
163 | @Override |
---|
164 | public final LispObject getPropertyList() |
---|
165 | { |
---|
166 | if (propertyList == null) |
---|
167 | propertyList = NIL; |
---|
168 | return propertyList; |
---|
169 | } |
---|
170 | |
---|
171 | @Override |
---|
172 | public final void setPropertyList(LispObject obj) |
---|
173 | { |
---|
174 | if (obj == null) |
---|
175 | throw new NullPointerException(); |
---|
176 | propertyList = obj; |
---|
177 | } |
---|
178 | |
---|
179 | public final void setClassBytes(byte[] bytes) throws ConditionThrowable |
---|
180 | { |
---|
181 | propertyList = putf(propertyList, Symbol.CLASS_BYTES, |
---|
182 | new JavaObject(bytes)); |
---|
183 | } |
---|
184 | |
---|
185 | @Override |
---|
186 | public LispObject execute() throws ConditionThrowable |
---|
187 | { |
---|
188 | return error(new WrongNumberOfArgumentsException(this)); |
---|
189 | } |
---|
190 | |
---|
191 | @Override |
---|
192 | public LispObject execute(LispObject arg) throws ConditionThrowable |
---|
193 | { |
---|
194 | return error(new WrongNumberOfArgumentsException(this)); |
---|
195 | } |
---|
196 | |
---|
197 | @Override |
---|
198 | public LispObject execute(LispObject first, LispObject second) |
---|
199 | throws ConditionThrowable |
---|
200 | { |
---|
201 | return error(new WrongNumberOfArgumentsException(this)); |
---|
202 | } |
---|
203 | |
---|
204 | @Override |
---|
205 | public LispObject execute(LispObject first, LispObject second, |
---|
206 | LispObject third) |
---|
207 | throws ConditionThrowable |
---|
208 | { |
---|
209 | return error(new WrongNumberOfArgumentsException(this)); |
---|
210 | } |
---|
211 | |
---|
212 | @Override |
---|
213 | public LispObject execute(LispObject first, LispObject second, |
---|
214 | LispObject third, LispObject fourth) |
---|
215 | throws ConditionThrowable |
---|
216 | { |
---|
217 | return error(new WrongNumberOfArgumentsException(this)); |
---|
218 | } |
---|
219 | |
---|
220 | @Override |
---|
221 | public LispObject execute(LispObject first, LispObject second, |
---|
222 | LispObject third, LispObject fourth, |
---|
223 | LispObject fifth) |
---|
224 | throws ConditionThrowable |
---|
225 | { |
---|
226 | return error(new WrongNumberOfArgumentsException(this)); |
---|
227 | } |
---|
228 | |
---|
229 | @Override |
---|
230 | public LispObject execute(LispObject first, LispObject second, |
---|
231 | LispObject third, LispObject fourth, |
---|
232 | LispObject fifth, LispObject sixth) |
---|
233 | throws ConditionThrowable |
---|
234 | { |
---|
235 | return error(new WrongNumberOfArgumentsException(this)); |
---|
236 | } |
---|
237 | |
---|
238 | @Override |
---|
239 | public LispObject execute(LispObject first, LispObject second, |
---|
240 | LispObject third, LispObject fourth, |
---|
241 | LispObject fifth, LispObject sixth, |
---|
242 | LispObject seventh) |
---|
243 | throws ConditionThrowable |
---|
244 | { |
---|
245 | return error(new WrongNumberOfArgumentsException(this)); |
---|
246 | } |
---|
247 | |
---|
248 | @Override |
---|
249 | public LispObject execute(LispObject first, LispObject second, |
---|
250 | LispObject third, LispObject fourth, |
---|
251 | LispObject fifth, LispObject sixth, |
---|
252 | LispObject seventh, LispObject eighth) |
---|
253 | throws ConditionThrowable |
---|
254 | { |
---|
255 | return error(new WrongNumberOfArgumentsException(this)); |
---|
256 | } |
---|
257 | |
---|
258 | @Override |
---|
259 | public LispObject execute(LispObject[] args) throws ConditionThrowable |
---|
260 | { |
---|
261 | return error(new WrongNumberOfArgumentsException(this)); |
---|
262 | } |
---|
263 | |
---|
264 | @Override |
---|
265 | public String writeToString() throws ConditionThrowable |
---|
266 | { |
---|
267 | LispObject name = getLambdaName(); |
---|
268 | if (name != null && name != NIL) { |
---|
269 | StringBuffer sb = new StringBuffer("#<FUNCTION "); |
---|
270 | sb.append(name.writeToString()); |
---|
271 | sb.append(" {"); |
---|
272 | sb.append(Integer.toHexString(System.identityHashCode(this)).toUpperCase()); |
---|
273 | sb.append("}>"); |
---|
274 | return sb.toString(); |
---|
275 | } |
---|
276 | // No name. |
---|
277 | LispObject lambdaList = getLambdaList(); |
---|
278 | if (lambdaList != null) { |
---|
279 | StringBuffer sb = new StringBuffer("#<FUNCTION "); |
---|
280 | sb.append("(LAMBDA "); |
---|
281 | if (lambdaList == NIL) { |
---|
282 | sb.append("()"); |
---|
283 | } else { |
---|
284 | final LispThread thread = LispThread.currentThread(); |
---|
285 | SpecialBinding lastSpecialBinding = thread.lastSpecialBinding; |
---|
286 | thread.bindSpecial(Symbol.PRINT_LENGTH, Fixnum.THREE); |
---|
287 | try { |
---|
288 | sb.append(lambdaList.writeToString()); |
---|
289 | } |
---|
290 | finally { |
---|
291 | thread.lastSpecialBinding = lastSpecialBinding; |
---|
292 | } |
---|
293 | } |
---|
294 | sb.append(")"); |
---|
295 | sb.append(" {"); |
---|
296 | sb.append(Integer.toHexString(System.identityHashCode(this)).toUpperCase()); |
---|
297 | sb.append("}>"); |
---|
298 | return sb.toString(); |
---|
299 | } |
---|
300 | return unreadableString("FUNCTION"); |
---|
301 | } |
---|
302 | |
---|
303 | // Used by the JVM compiler. |
---|
304 | public final void argCountError() throws ConditionThrowable |
---|
305 | { |
---|
306 | error(new WrongNumberOfArgumentsException(this)); |
---|
307 | } |
---|
308 | |
---|
309 | @Override |
---|
310 | // Profiling. |
---|
311 | public final int getCallCount() |
---|
312 | { |
---|
313 | return callCount; |
---|
314 | } |
---|
315 | |
---|
316 | @Override |
---|
317 | public void setCallCount(int n) |
---|
318 | { |
---|
319 | callCount = n; |
---|
320 | } |
---|
321 | |
---|
322 | @Override |
---|
323 | public final void incrementCallCount() |
---|
324 | { |
---|
325 | ++callCount; |
---|
326 | } |
---|
327 | } |
---|