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

Last change on this file was 12440, checked in by Mark Evenson, 15 years ago

Documentation updates and conversion to stack trace friendly Primitive declarations.

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