source: branches/0.15.x/abcl/src/org/armedbear/lisp/Extensions.java

Last change on this file was 11754, checked in by vvoutilainen, 16 years ago

Convert using ClassCastException? to checking instanceof.
Performance tests show this approach to be faster.
Patch by Douglas R. Miles. I modified the patch to
remove tabs, so indentation may be slightly off in places.
That's something that we need to handle separately, abcl
doesn't have a clear indentation policy.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.2 KB
Line 
1/*
2 * Extensions.java
3 *
4 * Copyright (C) 2002-2007 Peter Graves
5 * $Id: Extensions.java 11754 2009-04-12 10:53:39Z 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 java.io.File;
37import java.io.IOException;
38
39public final class Extensions extends Lisp
40{
41  // ### *ed-functions*
42  public static final Symbol _ED_FUNCTIONS_ =
43    exportSpecial("*ED-FUNCTIONS*", PACKAGE_EXT,
44                  list(intern("DEFAULT-ED-FUNCTION", PACKAGE_SYS)));
45
46  // ### truly-the value-type form => result*
47  private static final SpecialOperator TRULY_THE =
48    new SpecialOperator("truly-the", PACKAGE_EXT, true, "type value")
49    {
50      @Override
51      public LispObject execute(LispObject args, Environment env)
52        throws ConditionThrowable
53      {
54        if (args.length() != 2)
55          return error(new WrongNumberOfArgumentsException(this));
56        return eval(args.cadr(), env, LispThread.currentThread());
57      }
58    };
59
60  // ### neq
61  private static final Primitive NEQ =
62    new Primitive(Symbol.NEQ, "obj1 obj2")
63    {
64      @Override
65      public LispObject execute(LispObject first, LispObject second)
66        throws ConditionThrowable
67      {
68        return first != second ? T : NIL;
69      }
70    };
71
72  // ### memq item list => tail
73  private static final Primitive MEMQ =
74    new Primitive(Symbol.MEMQ, "item list")
75    {
76      @Override
77      public LispObject execute(LispObject item, LispObject list)
78        throws ConditionThrowable
79      {
80        while (list instanceof Cons)
81          {
82            if (item == ((Cons)list).car)
83              return list;
84            list = ((Cons)list).cdr;
85          }
86        if (list != NIL)
87          type_error(list, Symbol.LIST);
88        return NIL;
89      }
90    };
91
92  // ### memql item list => tail
93  private static final Primitive MEMQL =
94    new Primitive(Symbol.MEMQL, "item list")
95    {
96      @Override
97      public LispObject execute(LispObject item, LispObject list)
98        throws ConditionThrowable
99      {
100        while (list instanceof Cons)
101          {
102            if (item.eql(((Cons)list).car))
103              return list;
104            list = ((Cons)list).cdr;
105          }
106        if (list != NIL)
107          type_error(list, Symbol.LIST);
108        return NIL;
109      }
110    };
111
112  // ### adjoin-eql item list => new-list
113  private static final Primitive ADJOIN_EQL =
114    new Primitive(Symbol.ADJOIN_EQL, "item list")
115    {
116      @Override
117      public LispObject execute(LispObject item, LispObject list)
118        throws ConditionThrowable
119      {
120        return memql(item, list) ? list : new Cons(item, list);
121      }
122    };
123
124  // ### special-variable-p
125  private static final Primitive SPECIAL_VARIABLE_P =
126    new Primitive("special-variable-p", PACKAGE_EXT, true)
127    {
128      @Override
129      public LispObject execute(LispObject arg) throws ConditionThrowable
130      {
131        return arg.isSpecialVariable() ? T : NIL;
132      }
133    };
134
135  // ### source
136  private static final Primitive SOURCE =
137    new Primitive("source", PACKAGE_EXT, true)
138    {
139      @Override
140      public LispObject execute(LispObject arg) throws ConditionThrowable
141      {
142        return get(arg, Symbol._SOURCE, NIL);
143      }
144    };
145
146  // ### source-file-position
147  private static final Primitive SOURCE_FILE_POSITION =
148    new Primitive("source-file-position", PACKAGE_EXT, true)
149    {
150      @Override
151      public LispObject execute(LispObject arg) throws ConditionThrowable
152      {
153        LispObject obj = get(arg, Symbol._SOURCE, NIL);
154        if (obj instanceof Cons)
155          return obj.cdr();
156        return NIL;
157      }
158    };
159
160  // ### source-pathname
161  public static final Primitive SOURCE_PATHNAME =
162    new Primitive("source-pathname", PACKAGE_EXT, true)
163    {
164      @Override
165      public LispObject execute(LispObject arg) throws ConditionThrowable
166      {
167        LispObject obj = get(arg, Symbol._SOURCE, NIL);
168        if (obj instanceof Cons)
169          return obj.car();
170        return obj;
171      }
172    };
173
174  // ### exit
175  private static final Primitive EXIT =
176    new Primitive("exit", PACKAGE_EXT, true, "&key status")
177    {
178      @Override
179      public LispObject execute() throws ConditionThrowable
180      {
181        exit(0);
182        return LispThread.currentThread().nothing();
183      }
184      @Override
185      public LispObject execute(LispObject first, LispObject second)
186        throws ConditionThrowable
187      {
188        int status = 0;
189        if (first == Keyword.STATUS)
190          {
191            if (second instanceof Fixnum)
192              status = ((Fixnum)second).value;
193          }
194        exit(status);
195        return LispThread.currentThread().nothing();
196      }
197    };
198
199  // ### quit
200  private static final Primitive QUIT =
201    new Primitive("quit", PACKAGE_EXT, true, "&key status")
202    {
203      @Override
204      public LispObject execute() throws ConditionThrowable
205      {
206        exit(0);
207        return LispThread.currentThread().nothing();
208      }
209      @Override
210      public LispObject execute(LispObject first, LispObject second)
211        throws ConditionThrowable
212      {
213        int status = 0;
214        if (first == Keyword.STATUS)
215          {
216            if (second instanceof Fixnum)
217              status = ((Fixnum)second).value;
218          }
219        exit(status);
220        return LispThread.currentThread().nothing();
221      }
222    };
223
224  // ### dump-java-stack
225  private static final Primitive DUMP_JAVA_STACK =
226    new Primitive("dump-java-stack", PACKAGE_EXT, true)
227    {
228      @Override
229      public LispObject execute() throws ConditionThrowable
230      {
231        Thread.dumpStack();
232        return LispThread.currentThread().nothing();
233      }
234    };
235
236  // ### make-temp-file => namestring
237  private static final Primitive MAKE_TEMP_FILE =
238    new Primitive("make-temp-file", PACKAGE_EXT, true, "")
239    {
240      @Override
241      public LispObject execute() throws ConditionThrowable
242      {
243        try
244          {
245            File file = File.createTempFile("abcl", null, null);
246            if (file != null)
247              return new Pathname(file.getPath());
248          }
249        catch (IOException e)
250          {
251            Debug.trace(e);
252          }
253        return NIL;
254      }
255    };
256
257  // ### interrupt-lisp
258  private static final Primitive INTERRUPT_LISP =
259    new Primitive("interrupt-lisp", PACKAGE_EXT, true, "")
260    {
261      @Override
262      public LispObject execute() throws ConditionThrowable
263      {
264        setInterrupted(true);
265        return T;
266      }
267    };
268
269  // ### getenv
270  private static final Primitive GETENV =
271      new Primitive("getenv", PACKAGE_EXT, true)
272  {
273    @Override
274    public LispObject execute(LispObject arg) throws ConditionThrowable
275    {
276      AbstractString string;
277      if (arg instanceof AbstractString) {
278        string = (AbstractString) arg;
279      } else
280        return type_error(arg, Symbol.STRING);
281      String result = System.getenv(string.getStringValue());
282      if (result != null)
283        return new SimpleString(result);
284      else
285        return NIL;
286    }
287  };
288}
Note: See TracBrowser for help on using the repository browser.