source: branches/0.17.x/abcl/src/org/armedbear/lisp/Function.java

Last change on this file was 12255, checked in by ehuelsmann, 16 years ago

Rename ConditionThrowable? to ControlTransfer? and remove

try/catch blocks which don't have anything to do with
non-local transfer of control.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.4 KB
Line 
1/*
2 * Function.java
3 *
4 * Copyright (C) 2002-2005 Peter Graves
5 * $Id: Function.java 12255 2009-11-06 22:36:32Z 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
34package org.armedbear.lisp;
35
36public 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            symbol.setDocumentation(Symbol.FUNCTION,
72                                    new SimpleString(docstring));
73        }
74    }
75
76    public Function(String name, String arglist)
77    {
78        this(name);
79        setLambdaList(new SimpleString(arglist));
80    }
81
82    public Function(String name, Package pkg)
83    {
84        this(name, pkg, false);
85    }
86
87    public Function(String name, Package pkg, boolean exported)
88    {
89        this(name, pkg, exported, null, null);
90    }
91
92    public Function(String name, Package pkg, boolean exported,
93                    String arglist)
94    {
95        this(name, pkg, exported, arglist, null);
96    }
97
98    public Function(String name, Package pkg, boolean exported,
99                    String arglist, String docstring)
100    {
101        if (arglist instanceof String)
102            setLambdaList(new SimpleString(arglist));
103        if (name != null) {
104            Symbol symbol;
105            if (exported)
106                symbol = pkg.internAndExport(name.toUpperCase());
107            else
108                symbol = pkg.intern(name.toUpperCase());
109            symbol.setSymbolFunction(this);
110            if (cold)
111                symbol.setBuiltInFunction(true);
112            setLambdaName(symbol);
113            if (docstring != null)
114                symbol.setDocumentation(Symbol.FUNCTION,
115                                        new SimpleString(docstring));
116        }
117    }
118
119    public Function(LispObject name)
120    {
121        setLambdaName(name);
122    }
123
124    public Function(LispObject name, LispObject lambdaList)
125    {
126        setLambdaName(name);
127        setLambdaList(lambdaList);
128    }
129
130    @Override
131    public LispObject typeOf()
132    {
133        return Symbol.FUNCTION;
134    }
135
136    @Override
137    public LispObject classOf()
138    {
139        return BuiltInClass.FUNCTION;
140    }
141
142    @Override
143    public LispObject typep(LispObject typeSpecifier)
144    {
145        if (typeSpecifier == Symbol.FUNCTION)
146            return T;
147        if (typeSpecifier == Symbol.COMPILED_FUNCTION)
148            return T;
149        if (typeSpecifier == BuiltInClass.FUNCTION)
150            return T;
151        return super.typep(typeSpecifier);
152    }
153
154    @Override
155    public final LispObject getPropertyList()
156    {
157        if (propertyList == null)
158            propertyList = NIL;
159        return propertyList;
160    }
161
162    @Override
163    public final void setPropertyList(LispObject obj)
164    {
165        if (obj == null)
166            throw new NullPointerException();
167        propertyList = obj;
168    }
169
170    public final void setClassBytes(byte[] bytes)
171    {
172        propertyList = putf(propertyList, Symbol.CLASS_BYTES,
173                            new JavaObject(bytes));
174    }
175
176    @Override
177    public LispObject execute()
178    {
179        return error(new WrongNumberOfArgumentsException(this));
180    }
181
182    @Override
183    public LispObject execute(LispObject arg)
184    {
185        return error(new WrongNumberOfArgumentsException(this));
186    }
187
188    @Override
189    public LispObject execute(LispObject first, LispObject second)
190
191    {
192        return error(new WrongNumberOfArgumentsException(this));
193    }
194
195    @Override
196    public LispObject execute(LispObject first, LispObject second,
197                              LispObject third)
198
199    {
200        return error(new WrongNumberOfArgumentsException(this));
201    }
202
203    @Override
204    public LispObject execute(LispObject first, LispObject second,
205                              LispObject third, LispObject fourth)
206
207    {
208        return error(new WrongNumberOfArgumentsException(this));
209    }
210
211    @Override
212    public LispObject execute(LispObject first, LispObject second,
213                              LispObject third, LispObject fourth,
214                              LispObject fifth)
215
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, LispObject sixth)
224
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                              LispObject seventh)
234
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, LispObject eighth)
244
245    {
246        return error(new WrongNumberOfArgumentsException(this));
247    }
248
249    @Override
250    public LispObject execute(LispObject[] args)
251    {
252        return error(new WrongNumberOfArgumentsException(this));
253    }
254
255    @Override
256    public String writeToString()
257    {
258        LispObject name = getLambdaName();
259        if (name != null && name != NIL) {
260            StringBuffer sb = new StringBuffer("#<FUNCTION ");
261            sb.append(name.writeToString());
262            sb.append(" {");
263            sb.append(Integer.toHexString(System.identityHashCode(this)).toUpperCase());
264            sb.append("}>");
265            return sb.toString();
266        }
267        // No name.
268        LispObject lambdaList = getLambdaList();
269        if (lambdaList != null) {
270            StringBuffer sb = new StringBuffer("#<FUNCTION ");
271            sb.append("(LAMBDA ");
272            if (lambdaList == NIL) {
273                sb.append("()");
274            } else {
275                final LispThread thread = LispThread.currentThread();
276                SpecialBinding lastSpecialBinding = thread.lastSpecialBinding;
277                thread.bindSpecial(Symbol.PRINT_LENGTH, Fixnum.THREE);
278                try {
279                    sb.append(lambdaList.writeToString());
280                }
281                finally {
282                    thread.lastSpecialBinding = lastSpecialBinding;
283                }
284            }
285            sb.append(")");
286            sb.append(" {");
287            sb.append(Integer.toHexString(System.identityHashCode(this)).toUpperCase());
288            sb.append("}>");
289            return sb.toString();
290        }
291        return unreadableString("FUNCTION");
292    }
293
294    // Used by the JVM compiler.
295    public final void argCountError()
296    {
297        error(new WrongNumberOfArgumentsException(this));
298    }
299
300    // Profiling.
301    @Override
302    public final int getCallCount()
303    {
304        return callCount;
305    }
306
307    @Override
308    public void setCallCount(int n)
309    {
310        callCount = n;
311    }
312
313    @Override
314    public final void incrementCallCount()
315    {
316        ++callCount;
317    }
318
319    @Override
320    public final int getHotCount()
321    {
322        return hotCount;
323    }
324
325    @Override
326    public void setHotCount(int n)
327    {
328        hotCount = n;
329    }
330
331    @Override
332    public final void incrementHotCount()
333    {
334        ++hotCount;
335    }
336}
Note: See TracBrowser for help on using the repository browser.