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

Last change on this file was 13143, checked in by ehuelsmann, 15 years ago

Add IntegrityError? and ProcessingTerminated? error classes
and adjust Interpreter.run() accordingly.
No longer call (directly or indirectly) System.exit(),
throw the relevant errors instead.

  • 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 13143 2011-01-13 22:49:28Z 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., 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      throw new ProcessingTerminated();
204    }
205    @Override
206    public LispObject execute(LispObject first, LispObject second)
207     
208    {
209      int status = 0;
210      if (first == Keyword.STATUS)
211        {
212          if (second instanceof Fixnum)
213            status = ((Fixnum)second).value;
214        }
215      throw new ProcessingTerminated(status);
216    }
217  }
218
219  // ### quit
220  private static final Primitive QUIT = new quit();
221  private static class quit extends Primitive {
222    quit() {
223      super("quit", PACKAGE_EXT, true, "&key status");
224    }
225    @Override
226    public LispObject execute()
227    {
228      ((Stream)Symbol.STANDARD_OUTPUT.getSymbolValue())._finishOutput();
229      ((Stream)Symbol.ERROR_OUTPUT.getSymbolValue())._finishOutput();
230      throw new ProcessingTerminated();
231    }
232    @Override
233    public LispObject execute(LispObject first, LispObject second)
234    {
235      int status = 0;
236      if (first == Keyword.STATUS)
237        {
238          if (second instanceof Fixnum)
239            status = ((Fixnum)second).value;
240        }
241      throw new ProcessingTerminated(status);
242    }
243  }
244
245  // ### dump-java-stack
246  private static final Primitive DUMP_JAVA_STACK = new dump_java_stack();
247  private static class dump_java_stack extends Primitive {
248    dump_java_stack() {
249      super("dump-java-stack", PACKAGE_EXT, true);
250    }
251    @Override
252    public LispObject execute()
253    {
254      Thread.dumpStack();
255      return LispThread.currentThread().nothing();
256    }
257  }
258
259  // ### make-temp-file => pathname
260  private static final Primitive MAKE_TEMP_FILE = new make_temp_file();
261  private static class make_temp_file extends Primitive { 
262    make_temp_file() {
263      super("make-temp-file", PACKAGE_EXT, true, "");
264    }
265    @Override
266    public LispObject execute()
267    {
268      try
269        {
270          File file = File.createTempFile("abcl", null, null);
271          if (file != null)
272            return new Pathname(file.getPath());
273        }
274      catch (IOException e)
275        {
276          Debug.trace(e);
277        }
278      return NIL;
279    }
280  }
281
282  // ### interrupt-lisp
283  private static final Primitive INTERRUPT_LISP = new interrupt_lisp();
284  private static class interrupt_lisp extends Primitive {
285    interrupt_lisp() {
286      super("interrupt-lisp", PACKAGE_EXT, true, "");
287    }
288    @Override
289    public LispObject execute()
290    {
291      setInterrupted(true);
292      return T;
293    }
294  }
295
296  // ### getenv variable => string
297  private static final Primitive GETENV = new getenv();
298  private static class getenv extends Primitive
299  {
300    getenv() 
301    {
302      super("getenv", PACKAGE_EXT, true, "variable",
303             "Return the value of the environment VARIABLE if it exists, otherwise return NIL.");
304    }
305    @Override
306    public LispObject execute(LispObject arg)
307    {
308      AbstractString string;
309      if (arg instanceof AbstractString) {
310        string = (AbstractString) arg;
311      } else
312        return type_error(arg, Symbol.STRING);
313      String result = System.getenv(string.getStringValue());
314      if (result != null)
315        return new SimpleString(result);
316      else
317        return NIL;
318    }
319  }
320}
Note: See TracBrowser for help on using the repository browser.