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

Last change on this file was 12288, checked in by vvoutilainen, 15 years ago

Don't extend Lisp in LispObject, static import Lisp wherever
necessary. Patch by Douglas R. Miles.

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