2
* Copyright (c) 2001 by The XFree86 Project, Inc.
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
11
* The above copyright notice and this permission notice shall be included in
12
* all copies or substantial portions of the Software.
14
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
* THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
* Except as contained in this notice, the name of the XFree86 Project shall
23
* not be used in advertising or otherwise to promote the sale, use or other
24
* dealings in this Software without prior written authorization from the
27
* Author: Paulo César Pereira de Andrade
30
/* $XFree86: xc/programs/xedit/lisp/lisp.c,v 1.87tsi Exp $ */
45
#include <unistd.h> /* for sysconf(), and getpagesize() */
49
#define HAS_GETPAGESIZE
50
#define HAS_SC_PAGESIZE /* _SC_PAGESIZE may be an enum for Linux */
53
#if defined(CSRG_BASED)
54
#define HAS_GETPAGESIZE
58
#define HAS_GETPAGESIZE
62
#define HAS_GETPAGESIZE
65
#if defined(__QNXNTO__)
66
#define HAS_SC_PAGESIZE
69
#include "lisp/bytecode.h"
71
#include "lisp/read.h"
72
#include "lisp/format.h"
73
#include "lisp/math.h"
74
#include "lisp/hash.h"
75
#include "lisp/package.h"
76
#include "lisp/pathname.h"
77
#include "lisp/regex.h"
78
#include "lisp/require.h"
79
#include "lisp/stream.h"
80
#include "lisp/struct.h"
81
#include "lisp/time.h"
82
#include "lisp/write.h"
96
static void Lisp__GC(LispObj*, LispObj*);
97
static LispObj *Lisp__New(LispObj*, LispObj*);
99
/* run a user function, to be called only by LispEval */
100
static LispObj *LispRunFunMac(LispObj*, LispObj*, int, int);
102
/* expands and executes a setf method, to be called only by Lisp_Setf */
103
LispObj *LispRunSetf(LispArgList*, LispObj*, LispObj*, LispObj*);
104
LispObj *LispRunSetfMacro(LispAtom*, LispObj*, LispObj*);
106
/* increases storage size for environment */
107
void LispMoreEnvironment(void);
109
/* increases storage size for stack of builtin arguments */
110
void LispMoreStack(void);
112
/* increases storage size for global variables */
113
void LispMoreGlobals(LispPackage*);
116
static INLINE LispObj *LispDoGetVar(LispObj*);
118
static INLINE void LispDoAddVar(LispObj*, LispObj*);
120
/* Helper for importing symbol(s) functions,
121
* Search for the specified object in the current package */
122
static INLINE LispObj *LispGetVarPack(LispObj*);
124
/* create environment for function call */
125
static int LispMakeEnvironment(LispArgList*, LispObj*, LispObj*, int, int);
127
/* if not already in keyword package, move atom to keyword package */
128
static LispObj *LispCheckKeyword(LispObj*);
130
/* builtin backquote parsing */
131
static LispObj *LispEvalBackquoteObject(LispObj*, int, int);
132
/* used also by the bytecode compiler */
133
LispObj *LispEvalBackquote(LispObj*, int);
135
/* create or change object property */
136
void LispSetAtomObjectProperty(LispAtom*, LispObj*);
137
/* remove object property */
138
static void LispRemAtomObjectProperty(LispAtom*);
140
/* allocates a new LispProperty for the given atom */
141
static void LispAllocAtomProperty(LispAtom*);
142
/* Increment reference count of atom property */
143
static void LispIncrementAtomReference(LispAtom*);
144
/* Decrement reference count of atom property */
145
static void LispDecrementAtomReference(LispAtom*);
146
/* Removes all atom properties */
147
static void LispRemAtomAllProperties(LispAtom*);
149
static LispObj *LispAtomPropertyFunction(LispAtom*, LispObj*, int);
151
static INLINE void LispCheckMemLevel(void);
153
void LispAllocSeg(LispObjSeg*, int);
154
static INLINE void LispMark(LispObj*);
156
/* functions, macros, setf methods, and structure definitions */
157
static INLINE void LispProt(LispObj*);
159
static LispObj *LispCheckNeedProtect(LispObj*);
162
#ifdef SIGNALRETURNSINT
167
LispSignalHandler(int);
174
static LispObj lispunbound = {LispNil_t};
175
LispObj *UNBOUND = &lispunbound;
177
static volatile int lisp__disable_int;
178
static volatile int lisp__interrupted;
180
LispObj *Okey, *Orest, *Ooptional, *Oaux, *Olambda;
183
Atom_id Saux, Skey, Soptional, Srest;
184
Atom_id Satom, Ssymbol, Sinteger, Scharacter, Sstring, Slist,
185
Scons, Svector, Sarray, Sstruct, Skeyword, Sfunction, Spathname,
186
Srational, Sfloat, Scomplex, Sopaque, Sdefault;
188
LispObj *Oformat, *Kunspecific;
189
LispObj *Oexpand_setf_method;
191
static LispProperty noproperty;
192
LispProperty *NOPROPERTY = &noproperty;
193
static int segsize, minfree;
196
static LispObjSeg objseg = {NULL, NIL};
197
static LispObjSeg atomseg = {NULL, NIL};
201
LispFile *Stdout, *Stdin, *Stderr;
203
static LispBuiltin lispbuiltins[] = {
204
{LispFunction, Lisp_Mul, "* &rest numbers"},
205
{LispFunction, Lisp_Plus, "+ &rest numbers"},
206
{LispFunction, Lisp_Minus, "- number &rest more-numbers"},
207
{LispFunction, Lisp_Div, "/ number &rest more-numbers"},
208
{LispFunction, Lisp_OnePlus, "1+ number"},
209
{LispFunction, Lisp_OneMinus, "1- number"},
210
{LispFunction, Lisp_Less, "< number &rest more-numbers"},
211
{LispFunction, Lisp_LessEqual, "<= number &rest more-numbers"},
212
{LispFunction, Lisp_Equal_, "= number &rest more-numbers"},
213
{LispFunction, Lisp_Greater, "> number &rest more-numbers"},
214
{LispFunction, Lisp_GreaterEqual, ">= number &rest more-numbers"},
215
{LispFunction, Lisp_NotEqual, "/= number &rest more-numbers"},
216
{LispFunction, Lisp_Max, "max number &rest more-numbers"},
217
{LispFunction, Lisp_Min, "min number &rest more-numbers"},
218
{LispFunction, Lisp_Abs, "abs number"},
219
{LispFunction, Lisp_Acons, "acons key datum alist"},
220
{LispFunction, Lisp_Adjoin, "adjoin item list &key key test test-not"},
221
{LispFunction, Lisp_AlphaCharP, "alpha-char-p char"},
222
{LispMacro, Lisp_And, "and &rest args", 1, 0, Com_And},
223
{LispFunction, Lisp_Append, "append &rest lists"},
224
{LispFunction, Lisp_Apply, "apply function arg &rest more-args", 1},
225
{LispFunction, Lisp_Aref, "aref array &rest subscripts"},
226
{LispFunction, Lisp_Assoc, "assoc item list &key test test-not key"},
227
{LispFunction, Lisp_AssocIf, "assoc-if predicate list &key key"},
228
{LispFunction, Lisp_AssocIfNot, "assoc-if-not predicate list &key key"},
229
{LispFunction, Lisp_Atom, "atom object"},
230
{LispMacro, Lisp_Block, "block name &rest body", 1, 0, Com_Block},
231
{LispFunction, Lisp_BothCaseP, "both-case-p character"},
232
{LispFunction, Lisp_Boundp, "boundp symbol"},
233
{LispFunction, Lisp_Butlast, "butlast list &optional count"},
234
{LispFunction, Lisp_Nbutlast, "nbutlast list &optional count"},
235
{LispFunction, Lisp_Car, "car list", 0, 0, Com_C_r},
236
{LispFunction, Lisp_Car, "first list", 0, 0, Com_C_r},
237
{LispMacro, Lisp_Case, "case keyform &rest body"},
238
{LispMacro, Lisp_Catch, "catch tag &rest body", 1},
239
{LispFunction, Lisp_Cdr, "cdr list", 0, 0, Com_C_r},
240
{LispFunction, Lisp_Cdr, "rest list", 0, 0, Com_C_r},
241
{LispFunction, Lisp_Ceiling, "ceiling number &optional divisor", 1},
242
{LispFunction, Lisp_Fceiling, "fceiling number &optional divisor", 1},
243
{LispFunction, Lisp_Char, "char string index"},
244
{LispFunction, Lisp_Char, "schar simple-string index"},
245
{LispFunction, Lisp_CharLess, "char< character &rest more-characters"},
246
{LispFunction, Lisp_CharLessEqual, "char<= character &rest more-characters"},
247
{LispFunction, Lisp_CharEqual_, "char= character &rest more-characters"},
248
{LispFunction, Lisp_CharGreater, "char> character &rest more-characters"},
249
{LispFunction, Lisp_CharGreaterEqual, "char>= character &rest more-characters"},
250
{LispFunction, Lisp_CharNotEqual_, "char/= character &rest more-characters"},
251
{LispFunction, Lisp_CharLessp, "char-lessp character &rest more-characters"},
252
{LispFunction, Lisp_CharNotGreaterp, "char-not-greaterp character &rest more-characters"},
253
{LispFunction, Lisp_CharEqual, "char-equal character &rest more-characters"},
254
{LispFunction, Lisp_CharGreaterp, "char-greaterp character &rest more-characters"},
255
{LispFunction, Lisp_CharNotLessp, "char-not-lessp character &rest more-characters"},
256
{LispFunction, Lisp_CharNotEqual, "char-not-equal character &rest more-characters"},
257
{LispFunction, Lisp_CharDowncase, "char-downcase character"},
258
{LispFunction, Lisp_CharInt, "char-code character"},
259
{LispFunction, Lisp_CharInt, "char-int character"},
260
{LispFunction, Lisp_CharUpcase, "char-upcase character"},
261
{LispFunction, Lisp_Character, "character object"},
262
{LispFunction, Lisp_Characterp, "characterp object"},
263
{LispFunction, Lisp_Clrhash, "clrhash hash-table"},
264
{LispFunction, Lisp_IntChar, "code-char integer"},
265
{LispFunction, Lisp_Coerce, "coerce object result-type"},
266
{LispFunction, Lisp_Compile, "compile name &optional definition", 1},
267
{LispFunction, Lisp_Complex, "complex realpart &optional imagpart"},
268
{LispMacro, Lisp_Cond, "cond &rest body", 0, 0, Com_Cond},
269
{LispFunction, Lisp_Cons, "cons car cdr", 0, 0, Com_Cons},
270
{LispFunction, Lisp_Consp, "consp object", 0, 0, Com_Consp},
271
{LispFunction, Lisp_Constantp, "constantp form &optional environment"},
272
{LispFunction, Lisp_Conjugate, "conjugate number"},
273
{LispFunction, Lisp_Complexp, "complexp object"},
274
{LispFunction, Lisp_CopyAlist, "copy-alist list"},
275
{LispFunction, Lisp_CopyList, "copy-list list"},
276
{LispFunction, Lisp_CopyTree, "copy-tree list"},
277
{LispFunction, Lisp_Close, "close stream &key abort"},
278
{LispFunction, Lisp_C_r, "caar list", 0, 0, Com_C_r},
279
{LispFunction, Lisp_C_r, "cadr list", 0, 0, Com_C_r},
280
{LispFunction, Lisp_C_r, "cdar list", 0, 0, Com_C_r},
281
{LispFunction, Lisp_C_r, "cddr list", 0, 0, Com_C_r},
282
{LispFunction, Lisp_C_r, "caaar list", 0, 0, Com_C_r},
283
{LispFunction, Lisp_C_r, "caadr list", 0, 0, Com_C_r},
284
{LispFunction, Lisp_C_r, "cadar list", 0, 0, Com_C_r},
285
{LispFunction, Lisp_C_r, "caddr list", 0, 0, Com_C_r},
286
{LispFunction, Lisp_C_r, "cdaar list", 0, 0, Com_C_r},
287
{LispFunction, Lisp_C_r, "cdadr list", 0, 0, Com_C_r},
288
{LispFunction, Lisp_C_r, "cddar list", 0, 0, Com_C_r},
289
{LispFunction, Lisp_C_r, "cdddr list", 0, 0, Com_C_r},
290
{LispFunction, Lisp_C_r, "caaaar list", 0, 0, Com_C_r},
291
{LispFunction, Lisp_C_r, "caaadr list", 0, 0, Com_C_r},
292
{LispFunction, Lisp_C_r, "caadar list", 0, 0, Com_C_r},
293
{LispFunction, Lisp_C_r, "caaddr list", 0, 0, Com_C_r},
294
{LispFunction, Lisp_C_r, "cadaar list", 0, 0, Com_C_r},
295
{LispFunction, Lisp_C_r, "cadadr list", 0, 0, Com_C_r},
296
{LispFunction, Lisp_C_r, "caddar list", 0, 0, Com_C_r},
297
{LispFunction, Lisp_C_r, "cadddr list", 0, 0, Com_C_r},
298
{LispFunction, Lisp_C_r, "cdaaar list", 0, 0, Com_C_r},
299
{LispFunction, Lisp_C_r, "cdaadr list", 0, 0, Com_C_r},
300
{LispFunction, Lisp_C_r, "cdadar list", 0, 0, Com_C_r},
301
{LispFunction, Lisp_C_r, "cdaddr list", 0, 0, Com_C_r},
302
{LispFunction, Lisp_C_r, "cddaar list", 0, 0, Com_C_r},
303
{LispFunction, Lisp_C_r, "cddadr list", 0, 0, Com_C_r},
304
{LispFunction, Lisp_C_r, "cdddar list", 0, 0, Com_C_r},
305
{LispFunction, Lisp_C_r, "cddddr list", 0, 0, Com_C_r},
306
{LispMacro, Lisp_Decf, "decf place &optional delta"},
307
{LispMacro, Lisp_Defconstant, "defconstant name initial-value &optional documentation"},
308
{LispMacro, Lisp_Defmacro, "defmacro name lambda-list &rest body"},
309
{LispMacro, Lisp_Defstruct, "defstruct name &rest description"},
310
{LispMacro, Lisp_Defun, "defun name lambda-list &rest body"},
311
{LispMacro, Lisp_Defsetf, "defsetf function lambda-list &rest body"},
312
{LispMacro, Lisp_Defparameter, "defparameter name initial-value &optional documentation"},
313
{LispMacro, Lisp_Defvar, "defvar name &optional initial-value documentation"},
314
{LispFunction, Lisp_Delete, "delete item sequence &key from-end test test-not start end count key"},
315
{LispFunction, Lisp_DeleteDuplicates, "delete-duplicates sequence &key from-end test test-not start end key"},
316
{LispFunction, Lisp_DeleteIf, "delete-if predicate sequence &key from-end start end count key"},
317
{LispFunction, Lisp_DeleteIfNot, "delete-if-not predicate sequence &key from-end start end count key"},
318
{LispFunction, Lisp_DeleteFile, "delete-file filename"},
319
{LispFunction, Lisp_Denominator, "denominator rational"},
320
{LispFunction, Lisp_DigitChar, "digit-char weight &optional radix"},
321
{LispFunction, Lisp_DigitCharP, "digit-char-p character &optional radix"},
322
{LispFunction, Lisp_Directory, "directory pathname &key all if-cannot-read"},
323
{LispFunction, Lisp_DirectoryNamestring, "directory-namestring pathname"},
324
{LispFunction, Lisp_Disassemble, "disassemble function"},
325
{LispMacro, Lisp_Do, "do init test &rest body"},
326
{LispMacro, Lisp_DoP, "do* init test &rest body"},
327
{LispFunction, Lisp_Documentation, "documentation symbol type"},
328
{LispMacro, Lisp_DoList, "dolist init &rest body", 0, 0, Com_Dolist},
329
{LispMacro, Lisp_DoTimes, "dotimes init &rest body"},
330
{LispMacro, Lisp_DoAllSymbols, "do-all-symbols init &rest body"},
331
{LispMacro, Lisp_DoExternalSymbols, "do-external-symbols init &rest body"},
332
{LispMacro, Lisp_DoSymbols, "do-symbols init &rest body"},
333
{LispFunction, Lisp_Elt, "elt sequence index"},
334
{LispFunction, Lisp_Endp, "endp object"},
335
{LispFunction, Lisp_EnoughNamestring, "enough-namestring pathname &optional defaults"},
336
{LispFunction, Lisp_Eq, "eq left right", 0, 0, Com_Eq},
337
{LispFunction, Lisp_Eql, "eql left right", 0, 0, Com_Eq},
338
{LispFunction, Lisp_Equal, "equal left right", 0, 0, Com_Eq},
339
{LispFunction, Lisp_Equalp, "equalp left right", 0, 0, Com_Eq},
340
{LispFunction, Lisp_Error, "error control-string &rest arguments"},
341
{LispFunction, Lisp_Evenp, "evenp integer"},
342
{LispFunction, Lisp_Export, "export symbols &optional package"},
343
{LispFunction, Lisp_Eval, "eval form"},
344
{LispFunction, Lisp_Every, "every predicate sequence &rest more-sequences"},
345
{LispFunction, Lisp_Some, "some predicate sequence &rest more-sequences"},
346
{LispFunction, Lisp_Notevery, "notevery predicate sequence &rest more-sequences"},
347
{LispFunction, Lisp_Notany, "notany predicate sequence &rest more-sequences"},
348
{LispFunction, Lisp_Fboundp, "fboundp symbol"},
349
{LispFunction, Lisp_Find, "find item sequence &key from-end test test-not start end key"},
350
{LispFunction, Lisp_FindIf, "find-if predicate sequence &key from-end start end key"},
351
{LispFunction, Lisp_FindIfNot, "find-if-not predicate sequence &key from-end start end key"},
352
{LispFunction, Lisp_FileNamestring, "file-namestring pathname"},
353
{LispFunction, Lisp_Fill, "fill sequence item &key start end"},
354
{LispFunction, Lisp_FindAllSymbols, "find-all-symbols string-or-symbol"},
355
{LispFunction, Lisp_FindSymbol, "find-symbol string &optional package", 1},
356
{LispFunction, Lisp_FindPackage, "find-package name"},
357
{LispFunction, Lisp_Float, "float number &optional other"},
358
{LispFunction, Lisp_Floatp, "floatp object"},
359
{LispFunction, Lisp_Floor, "floor number &optional divisor", 1},
360
{LispFunction, Lisp_Ffloor, "ffloor number &optional divisor", 1},
361
{LispFunction, Lisp_Fmakunbound, "fmakunbound symbol"},
362
{LispFunction, Lisp_Format, "format destination control-string &rest arguments"},
363
{LispFunction, Lisp_FreshLine, "fresh-line &optional output-stream"},
364
{LispFunction, Lisp_Funcall, "funcall function &rest arguments", 1},
365
{LispFunction, Lisp_Functionp, "functionp object"},
366
{LispFunction, Lisp_Gc, "gc &optional car cdr"},
367
{LispFunction, Lisp_Gcd, "gcd &rest integers"},
368
{LispFunction, Lisp_Gensym, "gensym &optional arg"},
369
{LispFunction, Lisp_Get, "get symbol indicator &optional default"},
370
{LispFunction, Lisp_Gethash, "gethash key hash-table &optional default", 1},
371
{LispMacro, Lisp_Go, "go tag", 0, 0, Com_Go},
372
{LispFunction, Lisp_GraphicCharP, "graphic-char-p char"},
373
{LispFunction, Lisp_HashTableP, "hash-table-p object"},
374
{LispFunction, Lisp_HashTableCount, "hash-table-count hash-table"},
375
{LispFunction, Lisp_HashTableRehashSize, "hash-table-rehash-size hash-table"},
376
{LispFunction, Lisp_HashTableRehashThreshold, "hash-table-rehash-threshold hash-table"},
377
{LispFunction, Lisp_HashTableSize, "hash-table-size hash-table"},
378
{LispFunction, Lisp_HashTableTest, "hash-table-test hash-table"},
379
{LispFunction, Lisp_HostNamestring, "host-namestring pathname"},
380
{LispMacro, Lisp_If, "if test then &optional else", 0, 0, Com_If},
381
{LispMacro, Lisp_IgnoreErrors, "ignore-errors &rest body", 1},
382
{LispFunction, Lisp_Imagpart, "imagpart number"},
383
{LispMacro, Lisp_InPackage, "in-package name"},
384
{LispMacro, Lisp_Incf, "incf place &optional delta"},
385
{LispFunction, Lisp_Import, "import symbols &optional package"},
386
{LispFunction, Lisp_InputStreamP, "input-stream-p stream"},
387
{LispFunction, Lisp_IntChar, "int-char integer"},
388
{LispFunction, Lisp_Integerp, "integerp object"},
389
{LispFunction, Lisp_Intern, "intern string &optional package", 1},
390
{LispFunction, Lisp_Intersection, "intersection list1 list2 &key test test-not key"},
391
{LispFunction, Lisp_Nintersection, "nintersection list1 list2 &key test test-not key"},
392
{LispFunction, Lisp_Isqrt, "isqrt natural"},
393
{LispFunction, Lisp_Keywordp, "keywordp object"},
394
{LispFunction, Lisp_Last, "last list &optional count", 0, 0, Com_Last},
395
{LispMacro, Lisp_Lambda, "lambda lambda-list &rest body"},
396
{LispFunction, Lisp_Lcm, "lcm &rest integers"},
397
{LispFunction, Lisp_Length, "length sequence", 0, 0, Com_Length},
398
{LispMacro, Lisp_Let, "let init &rest body", 1, 0, Com_Let},
399
{LispMacro, Lisp_LetP, "let* init &rest body", 1, 0, Com_Letx},
400
{LispFunction, Lisp_ListP, "list* object &rest more-objects"},
401
{LispFunction, Lisp_ListAllPackages, "list-all-packages"},
402
{LispFunction, Lisp_List, "list &rest args"},
403
{LispFunction, Lisp_ListLength, "list-length list"},
404
{LispFunction, Lisp_Listp, "listp object", 0, 0, Com_Listp},
405
{LispFunction, Lisp_Listen, "listen &optional input-stream"},
406
{LispFunction, Lisp_Load, "load filename &key verbose print if-does-not-exist"},
407
{LispFunction, Lisp_Logand, "logand &rest integers"},
408
{LispFunction, Lisp_Logeqv, "logeqv &rest integers"},
409
{LispFunction, Lisp_Logior, "logior &rest integers"},
410
{LispFunction, Lisp_Lognot, "lognot integer"},
411
{LispFunction, Lisp_Logxor, "logxor &rest integers"},
412
{LispMacro, Lisp_Loop, "loop &rest body", 0, 0, Com_Loop},
413
{LispFunction, Lisp_LowerCaseP, "lower-case-p character"},
414
{LispFunction, Lisp_MakeArray, "make-array dimensions &key element-type initial-element initial-contents adjustable fill-pointer displaced-to displaced-index-offset"},
415
{LispFunction, Lisp_MakeHashTable, "make-hash-table &key test size rehash-size rehash-threshold initial-contents"},
416
{LispFunction, Lisp_MakeList, "make-list size &key initial-element"},
417
{LispFunction, Lisp_MakePackage, "make-package package-name &key nicknames use"},
418
{LispFunction, Lisp_MakePathname, "make-pathname &key host device directory name type version defaults"},
419
{LispFunction, Lisp_MakeString, "make-string size &key initial-element element-type"},
420
{LispFunction, Lisp_MakeSymbol, "make-symbol name"},
421
{LispFunction, Lisp_MakeStringInputStream, "make-string-input-stream string &optional start end"},
422
{LispFunction, Lisp_MakeStringOutputStream, "make-string-output-stream &key element-type"},
423
{LispFunction, Lisp_GetOutputStreamString, "get-output-stream-string string-output-stream"},
424
{LispFunction, Lisp_Makunbound, "makunbound symbol"},
425
{LispFunction, Lisp_Mapc, "mapc function list &rest more-lists"},
426
{LispFunction, Lisp_Mapcar, "mapcar function list &rest more-lists"},
427
{LispFunction, Lisp_Mapcan, "mapcan function list &rest more-lists"},
428
{LispFunction, Lisp_Maphash, "maphash function hash-table"},
429
{LispFunction, Lisp_Mapl, "mapl function list &rest more-lists"},
430
{LispFunction, Lisp_Maplist, "maplist function list &rest more-lists"},
431
{LispFunction, Lisp_Mapcon, "mapcon function list &rest more-lists"},
432
{LispFunction, Lisp_Member, "member item list &key test test-not key"},
433
{LispFunction, Lisp_MemberIf, "member-if predicate list &key key"},
434
{LispFunction, Lisp_MemberIfNot, "member-if-not predicate list &key key"},
435
{LispFunction, Lisp_Minusp, "minusp number"},
436
{LispFunction, Lisp_Mod, "mod number divisor"},
437
{LispMacro, Lisp_MultipleValueBind, "multiple-value-bind symbols values &rest body"},
438
{LispMacro, Lisp_MultipleValueCall, "multiple-value-call function &rest form", 1},
439
{LispMacro, Lisp_MultipleValueProg1, "multiple-value-prog1 first-form &rest form", 1},
440
{LispMacro, Lisp_MultipleValueList, "multiple-value-list form"},
441
{LispMacro, Lisp_MultipleValueSetq, "multiple-value-setq symbols form"},
442
{LispFunction, Lisp_Nconc, "nconc &rest lists"},
443
{LispFunction, Lisp_Nreverse, "nreverse sequence"},
444
{LispFunction, Lisp_NsetDifference, "nset-difference list1 list2 &key test test-not key"},
445
{LispFunction, Lisp_Nsubstitute, "nsubstitute newitem olditem sequence &key from-end test test-not start end count key"},
446
{LispFunction, Lisp_NsubstituteIf, "nsubstitute-if newitem test sequence &key from-end start end count key"},
447
{LispFunction, Lisp_NsubstituteIfNot, "nsubstitute-if-not newitem test sequence &key from-end start end count key"},
448
{LispFunction, Lisp_Nth, "nth index list"},
449
{LispFunction, Lisp_Nthcdr, "nthcdr index list", 0, 0, Com_Nthcdr},
450
{LispMacro, Lisp_NthValue, "nth-value index form"},
451
{LispFunction, Lisp_Numerator, "numerator rational"},
452
{LispFunction, Lisp_Namestring, "namestring pathname"},
453
{LispFunction, Lisp_Null, "not arg", 0, 0, Com_Null},
454
{LispFunction, Lisp_Null, "null list", 0, 0, Com_Null},
455
{LispFunction, Lisp_Numberp, "numberp object", 0, 0, Com_Numberp},
456
{LispFunction, Lisp_Oddp, "oddp integer"},
457
{LispFunction, Lisp_Open, "open filename &key direction element-type if-exists if-does-not-exist external-format"},
458
{LispFunction, Lisp_OpenStreamP, "open-stream-p stream"},
459
{LispMacro, Lisp_Or, "or &rest args", 1, 0, Com_Or},
460
{LispFunction, Lisp_OutputStreamP, "output-stream-p stream"},
461
{LispFunction, Lisp_Packagep, "packagep object"},
462
{LispFunction, Lisp_PackageName, "package-name package"},
463
{LispFunction, Lisp_PackageNicknames, "package-nicknames package"},
464
{LispFunction, Lisp_PackageUseList, "package-use-list package"},
465
{LispFunction, Lisp_PackageUsedByList, "package-used-by-list package"},
466
{LispFunction, Lisp_Pairlis, "pairlis key data &optional alist"},
467
{LispFunction, Lisp_ParseInteger, "parse-integer string &key start end radix junk-allowed", 1},
468
{LispFunction, Lisp_ParseNamestring, "parse-namestring object &optional host defaults &key start end junk-allowed", 1},
469
{LispFunction, Lisp_PathnameHost, "pathname-host pathname"},
470
{LispFunction, Lisp_PathnameDevice, "pathname-device pathname"},
471
{LispFunction, Lisp_PathnameDirectory, "pathname-directory pathname"},
472
{LispFunction, Lisp_PathnameName, "pathname-name pathname"},
473
{LispFunction, Lisp_PathnameType, "pathname-type pathname"},
474
{LispFunction, Lisp_PathnameVersion, "pathname-version pathname"},
475
{LispFunction, Lisp_Pathnamep, "pathnamep object"},
476
{LispFunction, Lisp_Plusp, "plusp number"},
477
{LispMacro, Lisp_Pop, "pop place"},
478
{LispFunction, Lisp_Position, "position item sequence &key from-end test test-not start end key"},
479
{LispFunction, Lisp_PositionIf, "position-if predicate sequence &key from-end start end key"},
480
{LispFunction, Lisp_PositionIfNot, "position-if-not predicate sequence &key from-end start end key"},
481
{LispFunction, Lisp_Prin1, "prin1 object &optional output-stream"},
482
{LispFunction, Lisp_Princ, "princ object &optional output-stream"},
483
{LispFunction, Lisp_Print, "print object &optional output-stream"},
484
{LispFunction, Lisp_ProbeFile, "probe-file pathname"},
485
{LispFunction, Lisp_Proclaim, "proclaim declaration"},
486
{LispMacro, Lisp_Prog1, "prog1 first &rest body"},
487
{LispMacro, Lisp_Prog2, "prog2 first second &rest body"},
488
{LispMacro, Lisp_Progn, "progn &rest body", 1, 0, Com_Progn},
489
{LispMacro, Lisp_Progv, "progv symbols values &rest body", 1},
490
{LispFunction, Lisp_Provide, "provide module"},
491
{LispMacro, Lisp_Push, "push item place"},
492
{LispMacro, Lisp_Pushnew, "pushnew item place &key key test test-not"},
493
{LispFunction, Lisp_Quit, "quit &optional status"},
494
{LispMacro, Lisp_Quote, "quote object"},
495
{LispFunction, Lisp_Rational, "rational number"},
496
{LispFunction, Lisp_Rationalp, "rationalp object"},
497
{LispFunction, Lisp_Read, "read &optional input-stream eof-error-p eof-value recursive-p"},
498
{LispFunction, Lisp_ReadChar, "read-char &optional input-stream eof-error-p eof-value recursive-p"},
499
{LispFunction, Lisp_ReadCharNoHang, "read-char-no-hang &optional input-stream eof-error-p eof-value recursive-p"},
500
{LispFunction, Lisp_ReadLine, "read-line &optional input-stream eof-error-p eof-value recursive-p", 1},
501
{LispFunction, Lisp_Realpart, "realpart number"},
502
{LispFunction, Lisp_Replace, "replace sequence1 sequence2 &key start1 end1 start2 end2"},
503
{LispFunction, Lisp_ReadFromString, "read-from-string string &optional eof-error-p eof-value &key start end preserve-whitespace", 1},
504
{LispFunction, Lisp_Require, "require module &optional pathname"},
505
{LispFunction, Lisp_Rem, "rem number divisor"},
506
{LispFunction, Lisp_Remhash, "remhash key hash-table"},
507
{LispFunction, Lisp_Remove, "remove item sequence &key from-end test test-not start end count key"},
508
{LispFunction, Lisp_RemoveDuplicates, "remove-duplicates sequence &key from-end test test-not start end key"},
509
{LispFunction, Lisp_RemoveIf, "remove-if predicate sequence &key from-end start end count key"},
510
{LispFunction, Lisp_RemoveIfNot, "remove-if-not predicate sequence &key from-end start end count key"},
511
{LispFunction, Lisp_Remprop, "remprop symbol indicator"},
512
{LispFunction, Lisp_RenameFile, "rename-file filename new-name", 1},
513
{LispMacro, Lisp_Return, "return &optional result", 1, 0, Com_Return},
514
{LispMacro, Lisp_ReturnFrom, "return-from name &optional result", 1, 0, Com_ReturnFrom},
515
{LispFunction, Lisp_Reverse, "reverse sequence"},
516
{LispFunction, Lisp_Round, "round number &optional divisor", 1},
517
{LispFunction, Lisp_Fround, "fround number &optional divisor", 1},
518
{LispFunction, Lisp_Rplaca, "rplaca place value", 0, 0, Com_Rplac_},
519
{LispFunction, Lisp_Rplacd, "rplacd place value", 0, 0, Com_Rplac_},
520
{LispFunction, Lisp_Search, "search sequence1 sequence2 &key from-end test test-not key start1 start2 end1 end2"},
521
{LispFunction, Lisp_Set, "set symbol value"},
522
{LispFunction, Lisp_SetDifference, "set-difference list1 list2 &key test test-not key"},
523
{LispFunction, Lisp_SetExclusiveOr, "set-exclusive-or list1 list2 &key test test-not key"},
524
{LispFunction, Lisp_NsetExclusiveOr, "nset-exclusive-or list1 list2 &key test test-not key"},
525
{LispMacro, Lisp_Setf, "setf &rest form"},
526
{LispMacro, Lisp_Psetf, "psetf &rest form"},
527
{LispMacro, Lisp_SetQ, "setq &rest form", 0, 0, Com_Setq},
528
{LispMacro, Lisp_Psetq, "psetq &rest form"},
529
{LispFunction, Lisp_Sleep, "sleep seconds"},
530
{LispFunction, Lisp_Sort, "sort sequence predicate &key key"},
531
{LispFunction, Lisp_Sqrt, "sqrt number"},
532
{LispFunction, Lisp_Elt, "svref sequence index"},
533
{LispFunction, Lisp_Sort, "stable-sort sequence predicate &key key"},
534
{LispFunction, Lisp_Streamp, "streamp object"},
535
{LispFunction, Lisp_String, "string object"},
536
{LispFunction, Lisp_Stringp, "stringp object"},
537
{LispFunction, Lisp_StringEqual_, "string= string1 string2 &key start1 end1 start2 end2"},
538
{LispFunction, Lisp_StringLess, "string< string1 string2 &key start1 end1 start2 end2"},
539
{LispFunction, Lisp_StringGreater, "string> string1 string2 &key start1 end1 start2 end2"},
540
{LispFunction, Lisp_StringLessEqual, "string<= string1 string2 &key start1 end1 start2 end2"},
541
{LispFunction, Lisp_StringGreaterEqual, "string>= string1 string2 &key start1 end1 start2 end2"},
542
{LispFunction, Lisp_StringNotEqual_, "string/= string1 string2 &key start1 end1 start2 end2"},
543
{LispFunction, Lisp_StringConcat, "string-concat &rest strings"},
544
{LispFunction, Lisp_StringEqual, "string-equal string1 string2 &key start1 end1 start2 end2"},
545
{LispFunction, Lisp_StringGreaterp, "string-greaterp string1 string2 &key start1 end1 start2 end2"},
546
{LispFunction, Lisp_StringNotEqual, "string-not-equal string1 string2 &key start1 end1 start2 end2"},
547
{LispFunction, Lisp_StringNotGreaterp, "string-not-greaterp string1 string2 &key start1 end1 start2 end2"},
548
{LispFunction, Lisp_StringNotLessp, "string-not-lessp string1 string2 &key start1 end1 start2 end2"},
549
{LispFunction, Lisp_StringLessp, "string-lessp string1 string2 &key start1 end1 start2 end2"},
550
{LispFunction, Lisp_StringTrim, "string-trim character-bag string"},
551
{LispFunction, Lisp_StringLeftTrim, "string-left-trim character-bag string"},
552
{LispFunction, Lisp_StringRightTrim, "string-right-trim character-bag string"},
553
{LispFunction, Lisp_StringUpcase, "string-upcase string &key start end"},
554
{LispFunction, Lisp_NstringUpcase, "nstring-upcase string &key start end"},
555
{LispFunction, Lisp_StringDowncase, "string-downcase string &key start end"},
556
{LispFunction, Lisp_NstringDowncase, "nstring-downcase string &key start end"},
557
{LispFunction, Lisp_StringCapitalize, "string-capitalize string &key start end"},
558
{LispFunction, Lisp_NstringCapitalize, "nstring-capitalize string &key start end"},
559
{LispFunction, Lisp_Subseq, "subseq sequence start &optional end"},
560
{LispFunction, Lisp_Subsetp, "subsetp list1 list2 &key test test-not key"},
561
{LispFunction, Lisp_Substitute, "substitute newitem olditem sequence &key from-end test test-not start end count key"},
562
{LispFunction, Lisp_SubstituteIf, "substitute-if newitem test sequence &key from-end start end count key"},
563
{LispFunction, Lisp_SubstituteIfNot, "substitute-if-not newitem test sequence &key from-end start end count key"},
564
{LispFunction, Lisp_SymbolFunction, "symbol-function symbol"},
565
{LispFunction, Lisp_SymbolName, "symbol-name symbol"},
566
{LispFunction, Lisp_Symbolp, "symbolp object"},
567
{LispFunction, Lisp_SymbolPlist, "symbol-plist symbol"},
568
{LispFunction, Lisp_SymbolPackage, "symbol-package symbol"},
569
{LispFunction, Lisp_SymbolValue, "symbol-value symbol"},
570
{LispMacro, Lisp_Tagbody, "tagbody &rest body", 0, 0, Com_Tagbody},
571
{LispFunction, Lisp_Terpri, "terpri &optional output-stream"},
572
{LispFunction, Lisp_Typep, "typep object type"},
573
{LispMacro, Lisp_The, "the value-type form"},
574
{LispMacro, Lisp_Throw, "throw tag result", 1},
575
{LispMacro, Lisp_Time, "time form"},
576
{LispFunction, Lisp_Truename, "truename pathname"},
577
{LispFunction, Lisp_TreeEqual, "tree-equal tree-1 tree-2 &key test test-not"},
578
{LispFunction, Lisp_Truncate, "truncate number &optional divisor", 1},
579
{LispFunction, Lisp_Ftruncate, "ftruncate number &optional divisor", 1},
580
{LispFunction, Lisp_Unexport, "unexport symbols &optional package"},
581
{LispFunction, Lisp_Union, "union list1 list2 &key test test-not key"},
582
{LispFunction, Lisp_Nunion, "nunion list1 list2 &key test test-not key"},
583
{LispMacro, Lisp_Unless, "unless test &rest body", 1, 0, Com_Unless},
584
{LispFunction, Lisp_UserHomedirPathname, "user-homedir-pathname &optional host"},
585
{LispMacro, Lisp_UnwindProtect, "unwind-protect protect &rest cleanup"},
586
{LispFunction, Lisp_UpperCaseP, "upper-case-p character"},
587
{LispFunction, Lisp_Values, "values &rest objects", 1},
588
{LispFunction, Lisp_ValuesList, "values-list list", 1},
589
{LispFunction, Lisp_Vector, "vector &rest objects"},
590
{LispMacro, Lisp_When, "when test &rest body", 1, 0, Com_When},
591
{LispFunction, Lisp_Write, " write object &key case circle escape length level lines pretty readably right-margin stream"},
592
{LispFunction, Lisp_WriteChar, "write-char string &optional output-stream"},
593
{LispFunction, Lisp_WriteLine, "write-line string &optional output-stream &key start end"},
594
{LispFunction, Lisp_WriteString, "write-string string &optional output-stream &key start end"},
595
{LispFunction, Lisp_XeditCharStore, "lisp::char-store string index value", 0, 1},
596
{LispFunction, Lisp_XeditEltStore, "lisp::elt-store sequence index value", 0, 1},
597
{LispFunction, Lisp_XeditMakeStruct, "lisp::make-struct atom &rest init", 0, 1},
598
{LispFunction, Lisp_XeditPut, " lisp::put symbol indicator value", 0, 1},
599
{LispFunction, Lisp_XeditPuthash, "lisp::puthash key hash-table value", 0, 1},
600
{LispFunction, Lisp_XeditSetSymbolPlist, "lisp::set-symbol-plist symbol list", 0, 1},
601
{LispFunction, Lisp_XeditStructAccess, "lisp::struct-access atom struct", 0, 1},
602
{LispFunction, Lisp_XeditStructType, "lisp::struct-type atom struct", 0, 1},
603
{LispFunction, Lisp_XeditStructStore, "lisp::struct-store atom struct value", 0, 1},
604
{LispFunction, Lisp_XeditVectorStore, "lisp::vector-store array &rest values", 0, 1},
605
{LispFunction, Lisp_XeditDocumentationStore, "lisp::documentation-store symbol type string", 0, 1},
606
{LispFunction, Lisp_Zerop, "zerop number"},
609
static LispBuiltin extbuiltins[] = {
610
{LispFunction, Lisp_Getenv, "getenv name"},
611
{LispFunction, Lisp_MakePipe, "make-pipe command-line &key direction element-type external-format"},
612
{LispFunction, Lisp_PipeBroken, "pipe-broken pipe-stream"},
613
{LispFunction, Lisp_PipeErrorStream, "pipe-error-stream pipe-stream"},
614
{LispFunction, Lisp_PipeInputDescriptor, "pipe-input-descriptor pipe-stream"},
615
{LispFunction, Lisp_PipeErrorDescriptor, "pipe-error-descriptor pipe-stream"},
616
{LispFunction, Lisp_Recomp, "re-comp pattern &key nospec icase nosub newline"},
617
{LispFunction, Lisp_Reexec, "re-exec regex string &key count start end notbol noteol"},
618
{LispFunction, Lisp_Rep, "re-p object"},
619
{LispFunction, Lisp_Setenv, "setenv name value &optional overwrite"},
620
{LispFunction, Lisp_Unsetenv, "unsetenv name"},
621
{LispFunction, Lisp_NstringTrim, "nstring-trim character-bag string"},
622
{LispFunction, Lisp_NstringLeftTrim, "nstring-left-trim character-bag string"},
623
{LispFunction, Lisp_NstringRightTrim, "nstring-right-trim character-bag string"},
624
{LispMacro, Lisp_Until, "until test &rest body", 0, 0, Com_Until},
625
{LispMacro, Lisp_While, "while test &rest body", 0, 0, Com_While},
628
/* byte code function argument list for functions that don't change it's
629
* &REST argument list. */
630
extern LispObj x_cons[8];
636
LispGetPageSize(void)
638
static int pagesize = -1;
643
/* Try each supported method in the preferred order */
645
#if defined(_SC_PAGESIZE) || defined(HAS_SC_PAGESIZE)
646
pagesize = sysconf(_SC_PAGESIZE);
651
pagesize = sysconf(_SC_PAGE_SIZE);
654
#ifdef HAS_GETPAGESIZE
656
pagesize = getpagesize();
661
pagesize = PAGE_SIZE;
664
if (pagesize < sizeof(LispObj) * 16)
665
pagesize = sizeof(LispObj) * 16; /* need a reasonable sane size */
671
LispDestroy(char *fmt, ...)
673
static char Error[] = "*** ";
675
if (!lisp__data.destroyed) {
680
vsnprintf(string, sizeof(string), fmt, ap);
683
if (!lisp__data.ignore_errors) {
685
LispFputc(Stderr, '\n');
686
LispFputs(Stderr, Error);
687
LispFputs(Stderr, string);
688
LispFputc(Stderr, '\n');
692
lisp__data.error_condition = STRING(string);
695
if (lisp__data.debugging) {
696
LispDebugger(LispDebugCallWatch, NIL, NIL);
697
LispDebugger(LispDebugCallFatal, NIL, NIL);
701
lisp__data.destroyed = 1;
702
LispBlockUnwind(NULL);
703
if (lisp__data.errexit)
708
if (lisp__data.debugging) {
709
/* when stack variables could be changed, this must be also changed! */
710
lisp__data.debug_level = -1;
711
lisp__data.debug = LispDebugUnspec;
715
while (lisp__data.mem.level) {
716
--lisp__data.mem.level;
717
if (lisp__data.mem.mem[lisp__data.mem.level])
718
free(lisp__data.mem.mem[lisp__data.mem.level]);
720
lisp__data.mem.index = 0;
722
/* If the package was changed and an error happened */
723
PACKAGE = lisp__data.savepackage;
724
lisp__data.pack = lisp__data.savepack;
728
if (!lisp__data.running) {
729
static char Fatal[] = "*** Fatal: nowhere to longjmp.\n";
731
LispFputs(Stderr, Fatal);
736
siglongjmp(lisp__data.jmp, 1);
740
LispContinuable(char *fmt, ...)
744
static char Error[] = "*** Error: ";
747
LispFputc(Stderr, '\n');
748
LispFputs(Stderr, Error);
750
vsnprintf(string, sizeof(string), fmt, ap);
752
LispFputs(Stderr, string);
753
LispFputc(Stderr, '\n');
754
LispFputs(Stderr, "Type 'continue' if you want to proceed: ");
757
/* NOTE: does not check if stdin is a tty */
758
if (LispFgets(Stdin, string, sizeof(string)) &&
759
strcmp(string, "continue\n") == 0)
762
LispDestroy("aborted on continuable error");
766
LispMessage(char *fmt, ...)
772
LispFputc(Stderr, '\n');
774
vsnprintf(string, sizeof(string), fmt, ap);
776
LispFputs(Stderr, string);
777
LispFputc(Stderr, '\n');
782
LispWarning(char *fmt, ...)
786
static char Warning[] = "*** Warning: ";
789
LispFputc(Stderr, '\n');
790
LispFputs(Stderr, Warning);
792
vsnprintf(string, sizeof(string), fmt, ap);
794
LispFputs(Stderr, string);
795
LispFputc(Stderr, '\n');
806
if (lisp__data.debugging) {
808
if (lisp__data.debug == LispDebugFinish)
809
lisp__data.debug = LispDebugUnspec;
810
lisp__data.debug_level = -1;
811
lisp__data.debug_step = 0;
815
lisp__data.block.block_level = 0;
816
if (lisp__data.block.block_size) {
817
while (lisp__data.block.block_size)
818
free(lisp__data.block.block[--lisp__data.block.block_size]);
819
free(lisp__data.block.block);
820
lisp__data.block.block = NULL;
823
lisp__data.destroyed = lisp__data.ignore_errors = 0;
825
if (CONSP(lisp__data.input_list)) {
826
LispUngetInfo **info, *unget = lisp__data.unget[0];
828
while (CONSP(lisp__data.input_list))
829
lisp__data.input_list = CDR(lisp__data.input_list);
830
SINPUT = lisp__data.input_list;
831
while (lisp__data.nunget > 1)
832
free(lisp__data.unget[--lisp__data.nunget]);
833
if ((info = realloc(lisp__data.unget, sizeof(LispUngetInfo*))) != NULL)
834
lisp__data.unget = info;
835
lisp__data.unget[0] = unget;
836
lisp__data.iunget = 0;
840
for (count = 0; lisp__data.mem.level;) {
841
--lisp__data.mem.level;
842
if (lisp__data.mem.mem[lisp__data.mem.level]) {
845
printf("LEAK: %p\n", lisp__data.mem.mem[lisp__data.mem.level]);
849
lisp__data.mem.index = 0;
851
LispWarning("%d raw memory pointer(s) left. Probably a leak.", count);
853
lisp__data.stack.base = lisp__data.stack.length =
854
lisp__data.env.lex = lisp__data.env.length = lisp__data.env.head = 0;
856
lisp__data.protect.length = 0;
858
lisp__data.savepackage = PACKAGE;
859
lisp__data.savepack = lisp__data.pack;
861
lisp__disable_int = lisp__interrupted = 0;
865
LispGC(LispObj *car, LispObj *cdr)
871
Lisp__GC(LispObj *car, LispObj *cdr)
873
register LispObj *entry, *last, *freeobj, **pentry, **eentry;
877
struct timeval start, end;
880
int count = objseg.nfree;
888
DISABLE_INTERRUPTS();
893
++lisp__data.gc.count;
896
gettimeofday(&start, NULL);
898
if (lisp__data.gc.timebits)
899
gettimeofday(&start, NULL);
902
/* Need to measure timings again to check if it is not better/faster
903
* to just mark these fields as any other data, as the interface was
904
* changed to properly handle circular lists in the function body itself.
906
if (lisp__data.gc.immutablebits) {
907
for (j = 0; j < objseg.nsegs; j++) {
908
for (entry = objseg.objects[j], last = entry + segsize;
909
entry < last; entry++)
914
/* Protect all packages */
915
for (entry = PACK; CONSP(entry); entry = CDR(entry)) {
916
LispObj *package = CAR(entry);
917
LispPackage *pack = package->data.package.package;
919
/* Protect cons cell */
922
/* Protect the package cell */
925
/* Protect package name */
926
package->data.package.name->mark = 1;
928
/* Protect package nicknames */
929
LispMark(package->data.package.nicknames);
931
/* Protect global symbols */
932
for (pentry = pack->glb.pairs, eentry = pentry + pack->glb.length;
933
pentry < eentry; pentry++)
934
LispMark((*pentry)->data.atom->property->value);
936
/* Traverse atom list, protecting properties, and function/structure
937
* definitions if lisp__data.gc.immutablebits set */
938
for (i = 0; i < STRTBLSZ; i++) {
939
atom = pack->atoms[i];
941
if (atom->property != NOPROPERTY) {
942
if (atom->a_property)
943
LispMark(atom->property->properties);
944
if (lisp__data.gc.immutablebits) {
945
if (atom->a_function || atom->a_compiled)
946
LispProt(atom->property->fun.function);
948
LispProt(atom->property->setf);
949
if (atom->a_defstruct)
950
LispProt(atom->property->structure.definition);
958
/* protect environment */
959
for (pentry = lisp__data.env.values,
960
eentry = pentry + lisp__data.env.length;
961
pentry < eentry; pentry++)
964
/* protect multiple return values */
965
for (pentry = lisp__data.returns.values,
966
eentry = pentry + lisp__data.returns.count;
967
pentry < eentry; pentry++)
970
/* protect stack of arguments to builtin functions */
971
for (pentry = lisp__data.stack.values,
972
eentry = pentry + lisp__data.stack.length;
973
pentry < eentry; pentry++)
976
/* protect temporary data used by builtin functions */
977
for (pentry = lisp__data.protect.objects,
978
eentry = pentry + lisp__data.protect.length;
979
pentry < eentry; pentry++)
982
for (i = 0; i < sizeof(x_cons) / sizeof(x_cons[0]); i++)
991
LispMark(lisp__data.input_list);
992
LispMark(lisp__data.output_list);
996
for (j = 0; j < objseg.nsegs; j++) {
997
for (entry = objseg.objects[j], last = entry + segsize;
998
entry < last; entry++) {
1001
else if (entry->mark)
1004
switch (XOBJECT_TYPE(entry)) {
1006
free(THESTR(entry));
1007
entry->type = LispCons_t;
1010
switch (entry->data.stream.type) {
1011
case LispStreamString:
1012
free(SSTREAMP(entry)->string);
1013
free(SSTREAMP(entry));
1015
case LispStreamFile:
1016
if (FSTREAMP(entry))
1017
LispFclose(FSTREAMP(entry));
1019
case LispStreamPipe:
1020
/* XXX may need special handling if child hangs */
1021
if (PSTREAMP(entry)) {
1022
if (IPSTREAMP(entry))
1023
LispFclose(IPSTREAMP(entry));
1024
if (OPSTREAMP(entry))
1025
LispFclose(OPSTREAMP(entry));
1026
/* don't bother with error stream, will also
1027
* freed in this GC call, maybe just out
1029
if (PIDPSTREAMP(entry) > 0) {
1030
kill(PIDPSTREAMP(entry), SIGTERM);
1031
waitpid(PIDPSTREAMP(entry), NULL, 0);
1033
free(PSTREAMP(entry));
1039
entry->type = LispCons_t;
1042
mpi_clear(entry->data.mp.integer);
1043
free(entry->data.mp.integer);
1044
entry->type = LispCons_t;
1046
case LispBigratio_t:
1047
mpr_clear(entry->data.mp.ratio);
1048
free(entry->data.mp.ratio);
1049
entry->type = LispCons_t;
1052
if (!SYMBOLP(entry->data.lambda.name))
1053
LispFreeArgList((LispArgList*)
1054
entry->data.lambda.name->data.opaque.data);
1055
entry->type = LispCons_t;
1058
refree(entry->data.regex.regex);
1059
free(entry->data.regex.regex);
1060
entry->type = LispCons_t;
1062
case LispBytecode_t:
1063
free(entry->data.bytecode.bytecode->code);
1064
free(entry->data.bytecode.bytecode);
1065
entry->type = LispCons_t;
1067
case LispHashTable_t:
1068
LispFreeHashTable(entry->data.hash.table);
1069
entry->type = LispCons_t;
1074
entry->type = LispCons_t;
1077
CDR(entry) = freeobj;
1084
objseg.nfree = nfree;
1085
objseg.freeobj = freeobj;
1087
lisp__data.gc.immutablebits = 0;
1090
gettimeofday(&end, NULL);
1091
sec = end.tv_sec - start.tv_sec;
1092
msec = end.tv_usec - start.tv_usec;
1098
"%ld sec, %ld msec, "
1099
"%d recovered, %d free, %d protected, %d total",
1101
objseg.nfree - count, objseg.nfree,
1102
objseg.nobjs - objseg.nfree, objseg.nobjs);
1104
if (lisp__data.gc.timebits) {
1105
gettimeofday(&end, NULL);
1106
if ((msec = end.tv_usec - start.tv_usec) < 0)
1108
lisp__data.gc.gctime += msec;
1112
ENABLE_INTERRUPTS();
1116
LispCheckMemLevel(void)
1120
/* Check for a free slot before the end. */
1121
for (i = lisp__data.mem.index; i < lisp__data.mem.level; i++)
1122
if (lisp__data.mem.mem[i] == NULL) {
1123
lisp__data.mem.index = i;
1127
/* Check for a free slot in the beginning */
1128
for (i = 0; i < lisp__data.mem.index; i++)
1129
if (lisp__data.mem.mem[i] == NULL) {
1130
lisp__data.mem.index = i;
1134
lisp__data.mem.index = lisp__data.mem.level;
1135
++lisp__data.mem.level;
1136
if (lisp__data.mem.index < lisp__data.mem.space)
1137
/* There is free space to store pointer. */
1140
void **ptr = (void**)realloc(lisp__data.mem.mem,
1141
(lisp__data.mem.space + 16) *
1145
LispDestroy("out of memory");
1146
lisp__data.mem.mem = ptr;
1147
lisp__data.mem.space += 16;
1152
LispMused(void *pointer)
1156
DISABLE_INTERRUPTS();
1157
for (i = lisp__data.mem.index; i >= 0; i--)
1158
if (lisp__data.mem.mem[i] == pointer) {
1159
lisp__data.mem.mem[i] = NULL;
1160
lisp__data.mem.index = i;
1164
for (i = lisp__data.mem.level - 1; i > lisp__data.mem.index; i--)
1165
if (lisp__data.mem.mem[i] == pointer) {
1166
lisp__data.mem.mem[i] = NULL;
1167
lisp__data.mem.index = i;
1172
ENABLE_INTERRUPTS();
1176
LispMalloc(size_t size)
1180
DISABLE_INTERRUPTS();
1181
LispCheckMemLevel();
1182
if ((pointer = malloc(size)) == NULL)
1183
LispDestroy("out of memory, couldn't allocate %lu bytes",
1184
(unsigned long)size);
1186
lisp__data.mem.mem[lisp__data.mem.index] = pointer;
1187
ENABLE_INTERRUPTS();
1193
LispCalloc(size_t nmemb, size_t size)
1197
DISABLE_INTERRUPTS();
1198
LispCheckMemLevel();
1199
if ((pointer = calloc(nmemb, size)) == NULL)
1200
LispDestroy("out of memory, couldn't allocate %lu bytes",
1201
(unsigned long)size);
1203
lisp__data.mem.mem[lisp__data.mem.index] = pointer;
1204
ENABLE_INTERRUPTS();
1210
LispRealloc(void *pointer, size_t size)
1215
DISABLE_INTERRUPTS();
1216
if (pointer != NULL) {
1217
for (i = lisp__data.mem.index; i >= 0; i--)
1218
if (lisp__data.mem.mem[i] == pointer)
1221
for (i = lisp__data.mem.index + 1; i < lisp__data.mem.level; i++)
1222
if (lisp__data.mem.mem[i] == pointer)
1226
LispCheckMemLevel();
1227
i = lisp__data.mem.index;
1230
if ((ptr = realloc(pointer, size)) == NULL)
1231
LispDestroy("out of memory, couldn't realloc");
1233
lisp__data.mem.mem[i] = ptr;
1234
ENABLE_INTERRUPTS();
1240
LispStrdup(char *str)
1242
char *ptr = LispMalloc(strlen(str) + 1);
1250
LispFree(void *pointer)
1254
DISABLE_INTERRUPTS();
1255
for (i = lisp__data.mem.index; i >= 0; i--)
1256
if (lisp__data.mem.mem[i] == pointer) {
1257
lisp__data.mem.mem[i] = NULL;
1258
lisp__data.mem.index = i;
1262
for (i = lisp__data.mem.level - 1; i > lisp__data.mem.index; i--)
1263
if (lisp__data.mem.mem[i] == pointer) {
1264
lisp__data.mem.mem[i] = NULL;
1265
lisp__data.mem.index = i;
1271
ENABLE_INTERRUPTS();
1275
LispSetVariable(LispObj *var, LispObj *val, char *fname, int eval)
1278
LispDestroy("%s: %s is not a symbol", fname, STROBJ(var));
1282
return (LispSetVar(var, val));
1286
LispRegisterOpaqueType(char *desc)
1289
int ii = STRHASH(desc);
1291
for (opaque = lisp__data.opqs[ii]; opaque; opaque = opaque->next)
1292
if (strcmp(opaque->desc, desc) == 0)
1293
return (opaque->type);
1294
opaque = (LispOpaque*)LispMalloc(sizeof(LispOpaque));
1295
opaque->desc = LispStrdup(desc);
1296
opaque->next = lisp__data.opqs[ii];
1297
lisp__data.opqs[ii] = opaque;
1298
LispMused(opaque->desc);
1301
return (opaque->type = ++lisp__data.opaque);
1305
LispIntToOpaqueType(int type)
1311
for (i = 0; i < STRTBLSZ; i++) {
1312
opaque = lisp__data.opqs[i];
1314
if (opaque->type == type)
1315
return (opaque->desc);
1316
opaque = opaque->next;
1319
LispDestroy("Opaque type %d not registered", type);
1326
LispDoHashString(char *string)
1331
for (pp = string, ii = count = 0; *pp && count < 32; pp++, count++)
1332
ii = (ii << 1) ^ *pp;
1336
return (ii % STRTBLSZ);
1340
LispGetAtomString(char *string, int perm)
1342
LispStringHash *entry;
1343
int ii = STRHASH(string);
1345
for (entry = lisp__data.strings[ii]; entry != NULL; entry = entry->next)
1346
if (strcmp(entry->string, string) == 0)
1347
return (entry->string);
1349
entry = (LispStringHash*)LispCalloc(1, sizeof(LispStringHash));
1351
entry->string = string;
1353
entry->string = LispStrdup(string);
1356
LispMused(entry->string);
1357
entry->next = lisp__data.strings[ii];
1358
lisp__data.strings[ii] = entry;
1360
return (entry->string);
1364
LispDoGetAtom(char *str, int perm)
1367
int ii = STRHASH(str);
1369
for (atom = lisp__data.pack->atoms[ii]; atom; atom = atom->next)
1370
if (strcmp(atom->string, str) == 0)
1373
atom = (LispAtom*)LispCalloc(1, sizeof(LispAtom));
1374
atom->string = LispGetAtomString(str, perm);
1376
atom->next = lisp__data.pack->atoms[ii];
1377
lisp__data.pack->atoms[ii] = atom;
1378
atom->property = NOPROPERTY;
1384
LispAllocAtomProperty(LispAtom *atom)
1386
LispProperty *property;
1388
if (atom->property != NOPROPERTY)
1389
LispDestroy("internal error at ALLOC-ATOM-PROPERTY");
1391
property = LispCalloc(1, sizeof(LispProperty));
1392
LispMused(property);
1393
atom->property = property;
1394
property->package = lisp__data.pack;
1395
if (atom->package == NULL)
1396
atom->package = PACKAGE;
1398
LispIncrementAtomReference(atom);
1402
LispIncrementAtomReference(LispAtom *atom)
1404
if (atom->property != NOPROPERTY)
1405
/* if atom->property is NOPROPERTY, this is an unbound symbol */
1406
++atom->property->refcount;
1409
/* Assumes atom property is not NOPROPERTY */
1411
LispDecrementAtomReference(LispAtom *atom)
1413
if (atom->property == NOPROPERTY)
1414
/* if atom->property is NOPROPERTY, this is an unbound symbol */
1417
if (atom->property->refcount <= 0)
1418
LispDestroy("internal error at DECREMENT-ATOM-REFERENCE");
1420
--atom->property->refcount;
1422
if (atom->property->refcount == 0) {
1423
LispRemAtomAllProperties(atom);
1424
free(atom->property);
1425
atom->property = NOPROPERTY;
1430
LispRemAtomAllProperties(LispAtom *atom)
1432
if (atom->property != NOPROPERTY) {
1434
LispRemAtomObjectProperty(atom);
1435
if (atom->a_function) {
1436
lisp__data.gc.immutablebits = 1;
1437
LispRemAtomFunctionProperty(atom);
1439
else if (atom->a_compiled) {
1440
lisp__data.gc.immutablebits = 1;
1441
LispRemAtomCompiledProperty(atom);
1443
else if (atom->a_builtin) {
1444
lisp__data.gc.immutablebits = 1;
1445
LispRemAtomBuiltinProperty(atom);
1447
if (atom->a_defsetf) {
1448
lisp__data.gc.immutablebits = 1;
1449
LispRemAtomSetfProperty(atom);
1451
if (atom->a_defstruct) {
1452
lisp__data.gc.immutablebits = 1;
1453
LispRemAtomStructProperty(atom);
1459
LispSetAtomObjectProperty(LispAtom *atom, LispObj *object)
1461
if (atom->property == NOPROPERTY)
1462
LispAllocAtomProperty(atom);
1463
else if (atom->watch) {
1464
if (atom->object == lisp__data.package) {
1465
if (!PACKAGEP(object))
1466
LispDestroy("Symbol %s must be a package, not %s",
1467
ATOMID(lisp__data.package), STROBJ(object));
1468
lisp__data.pack = object->data.package.package;
1473
SETVALUE(atom, object);
1477
LispRemAtomObjectProperty(LispAtom *atom)
1479
if (atom->a_object) {
1481
atom->property->value = NULL;
1486
LispSetAtomCompiledProperty(LispAtom *atom, LispObj *bytecode)
1488
if (atom->property == NOPROPERTY)
1489
LispAllocAtomProperty(atom);
1491
lisp__data.gc.immutablebits = 1;
1492
if (atom->a_builtin) {
1493
atom->a_builtin = 0;
1494
LispFreeArgList(atom->property->alist);
1497
atom->a_function = 0;
1498
atom->a_compiled = 1;
1499
atom->property->fun.function = bytecode;
1503
LispRemAtomCompiledProperty(LispAtom *atom)
1505
if (atom->a_compiled) {
1506
lisp__data.gc.immutablebits = 1;
1507
atom->property->fun.function = NULL;
1508
atom->a_compiled = 0;
1509
LispFreeArgList(atom->property->alist);
1510
atom->property->alist = NULL;
1515
LispSetAtomFunctionProperty(LispAtom *atom, LispObj *function,
1518
if (atom->property == NOPROPERTY)
1519
LispAllocAtomProperty(atom);
1521
lisp__data.gc.immutablebits = 1;
1522
if (atom->a_function == 0 && atom->a_builtin == 0 && atom->a_compiled == 0)
1523
atom->a_function = 1;
1525
if (atom->a_builtin) {
1526
atom->a_builtin = 0;
1527
LispFreeArgList(atom->property->alist);
1530
atom->a_compiled = 0;
1531
atom->a_function = 1;
1534
atom->property->fun.function = function;
1535
atom->property->alist = alist;
1539
LispRemAtomFunctionProperty(LispAtom *atom)
1541
if (atom->a_function) {
1542
lisp__data.gc.immutablebits = 1;
1543
atom->property->fun.function = NULL;
1544
atom->a_function = 0;
1545
LispFreeArgList(atom->property->alist);
1546
atom->property->alist = NULL;
1551
LispSetAtomBuiltinProperty(LispAtom *atom, LispBuiltin *builtin,
1554
if (atom->property == NOPROPERTY)
1555
LispAllocAtomProperty(atom);
1557
lisp__data.gc.immutablebits = 1;
1558
if (atom->a_builtin == 0 && atom->a_function == 0)
1559
atom->a_builtin = 1;
1561
if (atom->a_function) {
1562
atom->a_function = 0;
1563
LispFreeArgList(atom->property->alist);
1567
atom->property->fun.builtin = builtin;
1568
atom->property->alist = alist;
1572
LispRemAtomBuiltinProperty(LispAtom *atom)
1574
if (atom->a_builtin) {
1575
lisp__data.gc.immutablebits = 1;
1576
atom->property->fun.function = NULL;
1577
atom->a_builtin = 0;
1578
LispFreeArgList(atom->property->alist);
1579
atom->property->alist = NULL;
1584
LispSetAtomSetfProperty(LispAtom *atom, LispObj *setf, LispArgList *alist)
1586
if (atom->property == NOPROPERTY)
1587
LispAllocAtomProperty(atom);
1589
lisp__data.gc.immutablebits = 1;
1590
if (atom->a_defsetf)
1591
LispFreeArgList(atom->property->salist);
1593
atom->a_defsetf = 1;
1594
atom->property->setf = setf;
1595
atom->property->salist = alist;
1599
LispRemAtomSetfProperty(LispAtom *atom)
1601
if (atom->a_defsetf) {
1602
lisp__data.gc.immutablebits = 1;
1603
atom->property->setf = NULL;
1604
atom->a_defsetf = 0;
1605
LispFreeArgList(atom->property->salist);
1606
atom->property->salist = NULL;
1611
LispSetAtomStructProperty(LispAtom *atom, LispObj *def, int fun)
1614
/* Not suported by the bytecode compiler... */
1615
LispDestroy("SET-ATOM-STRUCT-PROPERTY: "
1616
"more than 256 fields not supported");
1618
if (atom->property == NOPROPERTY)
1619
LispAllocAtomProperty(atom);
1621
lisp__data.gc.immutablebits = 1;
1622
atom->a_defstruct = 1;
1623
atom->property->structure.definition = def;
1624
atom->property->structure.function = fun;
1628
LispRemAtomStructProperty(LispAtom *atom)
1630
if (atom->a_defstruct) {
1631
lisp__data.gc.immutablebits = 1;
1632
atom->property->structure.definition = NULL;
1633
atom->a_defstruct = 0;
1638
LispGetAtom(char *str)
1640
return (LispDoGetAtom(str, 0));
1644
LispGetPermAtom(char *str)
1646
return (LispDoGetAtom(str, 1));
1649
#define GET_PROPERTY 0
1650
#define ADD_PROPERTY 1
1651
#define REM_PROPERTY 2
1653
LispAtomPropertyFunction(LispAtom *atom, LispObj *key, int function)
1655
LispObj *list = NIL, *result = NIL;
1657
if (function == ADD_PROPERTY) {
1658
if (atom->property == NOPROPERTY)
1659
LispAllocAtomProperty(atom);
1660
if (atom->property->properties == NULL) {
1661
atom->a_property = 1;
1662
atom->property->properties = NIL;
1666
if (atom->a_property) {
1669
for (base = list = atom->property->properties;
1672
if (key == CAR(list)) {
1679
LispDestroy("%s: %s has an odd property list length",
1680
STROBJ(atom->object),
1681
function == REM_PROPERTY ? "REMPROP" : "GET");
1683
if (CONSP(list) && function == REM_PROPERTY) {
1684
if (!CONSP(CDR(list)))
1685
LispDestroy("REMPROP: %s has an odd property list length",
1686
STROBJ(atom->object));
1688
atom->property->properties = CDDR(list);
1690
RPLACD(CDR(base), CDDR(list));
1695
if (function == ADD_PROPERTY) {
1696
atom->property->properties =
1697
CONS(key, CONS(NIL, atom->property->properties));
1698
result = CDR(atom->property->properties);
1701
else if (function == REM_PROPERTY)
1708
LispGetAtomProperty(LispAtom *atom, LispObj *key)
1710
return (LispAtomPropertyFunction(atom, key, GET_PROPERTY));
1714
LispPutAtomProperty(LispAtom *atom, LispObj *key, LispObj *value)
1716
LispObj *result = LispAtomPropertyFunction(atom, key, ADD_PROPERTY);
1718
RPLACA(result, value);
1724
LispRemAtomProperty(LispAtom *atom, LispObj *key)
1726
return (LispAtomPropertyFunction(atom, key, REM_PROPERTY));
1730
LispReplaceAtomPropertyList(LispAtom *atom, LispObj *list)
1732
if (atom->property == NOPROPERTY)
1733
LispAllocAtomProperty(atom);
1734
if (atom->property->properties == NULL)
1735
atom->a_property = 1;
1736
atom->property->properties = list;
1745
/* Used to make sure that when defining a function like:
1746
* (defun my-function (... &key key1 key2 key3 ...)
1747
* key1, key2, and key3 will be in the keyword package
1750
LispCheckKeyword(LispObj *keyword)
1752
if (KEYWORDP(keyword))
1755
return (KEYWORD(ATOMID(keyword)));
1759
LispUseArgList(LispArgList *alist)
1761
if (alist->normals.num_symbols)
1762
LispMused(alist->normals.symbols);
1763
if (alist->optionals.num_symbols) {
1764
LispMused(alist->optionals.symbols);
1765
LispMused(alist->optionals.defaults);
1766
LispMused(alist->optionals.sforms);
1768
if (alist->keys.num_symbols) {
1769
LispMused(alist->keys.symbols);
1770
LispMused(alist->keys.defaults);
1771
LispMused(alist->keys.sforms);
1772
LispMused(alist->keys.keys);
1774
if (alist->auxs.num_symbols) {
1775
LispMused(alist->auxs.symbols);
1776
LispMused(alist->auxs.initials);
1782
LispFreeArgList(LispArgList *alist)
1784
if (alist->normals.num_symbols)
1785
LispFree(alist->normals.symbols);
1786
if (alist->optionals.num_symbols) {
1787
LispFree(alist->optionals.symbols);
1788
LispFree(alist->optionals.defaults);
1789
LispFree(alist->optionals.sforms);
1791
if (alist->keys.num_symbols) {
1792
LispFree(alist->keys.symbols);
1793
LispFree(alist->keys.defaults);
1794
LispFree(alist->keys.sforms);
1795
LispFree(alist->keys.keys);
1797
if (alist->auxs.num_symbols) {
1798
LispFree(alist->auxs.symbols);
1799
LispFree(alist->auxs.initials);
1805
LispCheckNeedProtect(LispObj *object)
1808
switch (OBJECT_TYPE(object)) {
1811
case LispFunction_t:
1823
LispListProtectedArguments(LispArgList *alist)
1827
LispObj *arguments, *cons, *obj, *prev;
1829
arguments = cons = prev = NIL;
1830
for (i = 0; i < alist->optionals.num_symbols; i++) {
1831
if ((obj = LispCheckNeedProtect(alist->optionals.defaults[i])) != NULL) {
1832
if (arguments == NIL) {
1833
arguments = cons = prev = CONS(obj, NIL);
1834
GC_PROTECT(arguments);
1837
RPLACD(cons, CONS(obj, NIL));
1843
for (i = 0; i < alist->keys.num_symbols; i++) {
1844
if ((obj = LispCheckNeedProtect(alist->keys.defaults[i])) != NULL) {
1845
if (arguments == NIL) {
1846
arguments = cons = prev = CONS(obj, NIL);
1847
GC_PROTECT(arguments);
1850
RPLACD(cons, CONS(obj, NIL));
1856
for (i = 0; i < alist->auxs.num_symbols; i++) {
1857
if ((obj = LispCheckNeedProtect(alist->auxs.initials[i])) != NULL) {
1858
if (arguments == NIL) {
1859
arguments = cons = prev = CONS(obj, NIL);
1860
GC_PROTECT(arguments);
1863
RPLACD(cons, CONS(obj, NIL));
1871
/* Don't add a NIL cell at the end, to save some space */
1872
if (arguments != NIL) {
1873
if (arguments == cons)
1874
arguments = CAR(cons);
1876
CDR(prev) = CAR(cons);
1883
LispCheckArguments(LispFunType type, LispObj *list, char *name, int builtin)
1885
static char *types[4] = {"LAMBDA-LIST", "FUNCTION", "MACRO", "SETF-METHOD"};
1886
static char *fnames[4] = {"LAMBDA", "DEFUN", "DEFMACRO", "DEFSETF"};
1891
static char *keys[4] = {"&KEY", "&OPTIONAL", "&REST", "&AUX"};
1892
int rest, optional, key, aux, count;
1894
LispObj *spec, *sform, *defval, *default_value;
1895
char description[8], *desc;
1897
/* If LispRealloc fails, the previous memory will be released
1898
* in LispTopLevel, unless LispMused was called on the pointer */
1899
#define REALLOC_OBJECTS(pointer, count) \
1900
pointer = LispRealloc(pointer, (count) * sizeof(LispObj*))
1902
alist = LispCalloc(1, sizeof(LispArgList));
1905
LispDestroy("%s %s: %s cannot be a %s argument list",
1906
fnames[type], name, STROBJ(list), types[type]);
1907
alist->description = GETATOMID("");
1912
default_value = builtin ? UNSPEC : NIL;
1914
description[0] = '\0';
1916
rest = optional = key = aux = 0;
1917
for (; CONSP(list); list = CDR(list)) {
1922
LispDestroy("builtin function argument cannot have default value");
1924
if (!SYMBOLP(CAR(spec)) ||
1925
(CDR(spec) != NIL && CDDR(spec) != NIL))
1926
LispDestroy("%s %s: bad &AUX argument %s",
1927
fnames[type], name, STROBJ(spec));
1928
defval = CDR(spec) != NIL ? CADR(spec) : NIL;
1929
count = alist->auxs.num_symbols;
1930
REALLOC_OBJECTS(alist->auxs.symbols, count + 1);
1931
REALLOC_OBJECTS(alist->auxs.initials, count + 1);
1932
alist->auxs.symbols[count] = CAR(spec);
1933
alist->auxs.initials[count] = defval;
1934
++alist->auxs.num_symbols;
1937
++alist->num_arguments;
1940
LispDestroy("%s %s: syntax error parsing %s",
1941
fnames[type], name, keys[IREST]);
1943
LispObj *akey = CAR(spec);
1945
defval = default_value;
1948
/* check for special case, as in:
1949
* (defun a (&key ((key name) 'default-value)) name)
1950
* (a 'key 'test) => TEST
1951
* (a) => DEFAULT-VALUE
1953
if (!SYMBOLP(CAR(akey)) || !CONSP(CDR(akey)) ||
1954
!SYMBOLP(CADR(akey)) || CDDR(akey) != NIL ||
1955
(CDR(spec) != NIL && CDDR(spec) != NIL))
1956
LispDestroy("%s %s: bad special &KEY %s",
1957
fnames[type], name, STROBJ(spec));
1958
if (CDR(spec) != NIL)
1959
defval = CADR(spec);
1966
if (!SYMBOLP(CAR(spec)))
1967
LispDestroy("%s %s: %s cannot be a %s argument name",
1969
STROBJ(CAR(spec)), types[type]);
1970
/* check if default value provided, and optionally a `svar' */
1971
else if (CDR(spec) != NIL && (!CONSP(CDR(spec)) ||
1972
(CDDR(spec) != NIL &&
1973
(!SYMBOLP(CAR(CDDR(spec))) ||
1974
CDR(CDDR(spec)) != NIL))))
1975
LispDestroy("%s %s: bad argument specification %s",
1976
fnames[type], name, STROBJ(spec));
1977
if (CONSP(CDR(spec))) {
1978
defval = CADR(spec);
1979
if (CONSP(CDDR(spec)))
1980
sform = CAR(CDDR(spec));
1982
/* Add to keyword package, and set the keyword in the
1983
* argument list, so that a function argument keyword
1984
* will reference the same object, and make comparison
1986
spec = LispCheckKeyword(CAR(spec));
1989
count = alist->keys.num_symbols;
1990
REALLOC_OBJECTS(alist->keys.keys, count + 1);
1991
REALLOC_OBJECTS(alist->keys.defaults, count + 1);
1992
REALLOC_OBJECTS(alist->keys.sforms, count + 1);
1993
REALLOC_OBJECTS(alist->keys.symbols, count + 1);
1994
alist->keys.symbols[count] = spec;
1995
alist->keys.defaults[count] = defval;
1996
alist->keys.sforms[count] = sform;
1997
alist->keys.keys[count] = akey;
1998
++alist->keys.num_symbols;
2001
alist->num_arguments += 1 + (sform != NULL);
2003
else if (optional) {
2004
defval = default_value;
2007
if (!SYMBOLP(CAR(spec)))
2008
LispDestroy("%s %s: %s cannot be a %s argument name",
2010
STROBJ(CAR(spec)), types[type]);
2011
/* check if default value provided, and optionally a `svar' */
2012
else if (CDR(spec) != NIL && (!CONSP(CDR(spec)) ||
2013
(CDDR(spec) != NIL &&
2014
(!SYMBOLP(CAR(CDDR(spec))) ||
2015
CDR(CDDR(spec)) != NIL))))
2016
LispDestroy("%s %s: bad argument specification %s",
2017
fnames[type], name, STROBJ(spec));
2018
if (CONSP(CDR(spec))) {
2019
defval = CADR(spec);
2020
if (CONSP(CDDR(spec)))
2021
sform = CAR(CDDR(spec));
2025
count = alist->optionals.num_symbols;
2026
REALLOC_OBJECTS(alist->optionals.symbols, count + 1);
2027
REALLOC_OBJECTS(alist->optionals.defaults, count + 1);
2028
REALLOC_OBJECTS(alist->optionals.sforms, count + 1);
2029
alist->optionals.symbols[count] = spec;
2030
alist->optionals.defaults[count] = defval;
2031
alist->optionals.sforms[count] = sform;
2032
++alist->optionals.num_symbols;
2035
alist->num_arguments += 1 + (sform != NULL);
2038
/* Normal arguments cannot have default value */
2040
LispDestroy("%s %s: syntax error parsing %s",
2041
fnames[type], name, STROBJ(spec));
2044
/* spec must be an atom, excluding keywords */
2045
else if (!SYMBOLP(spec) || KEYWORDP(spec))
2046
LispDestroy("%s %s: %s cannot be a %s argument",
2047
fnames[type], name, STROBJ(spec), types[type]);
2049
Atom_id atom = ATOMID(spec);
2051
if (atom[0] == '&') {
2052
if (atom == Srest) {
2053
if (rest || aux || CDR(list) == NIL || !SYMBOLP(CADR(list))
2054
/* only &aux allowed after &rest */
2055
|| (CDDR(list) != NIL && !SYMBOLP(CAR(CDDR(list))) &&
2056
ATOMID(CAR(CDDR(list))) != Saux))
2057
LispDestroy("%s %s: syntax error parsing %s",
2058
fnames[type], name, ATOMID(spec));
2060
LispDestroy("%s %s: %s not allowed after %s",
2061
fnames[type], name, keys[IREST], keys[IKEY]);
2066
else if (atom == Skey) {
2068
LispDestroy("%s %s: %s not allowed after %s",
2069
fnames[type], name, ATOMID(spec),
2070
rest ? keys[IREST] : keys[IAUX]);
2075
else if (atom == Soptional) {
2076
if (rest || optional || aux || key)
2077
LispDestroy("%s %s: %s not allowed after %s",
2078
fnames[type], name, ATOMID(spec),
2079
rest ? keys[IREST] :
2082
aux ? keys[IAUX] : keys[IKEY]);
2087
else if (atom == Saux) {
2088
/* &AUX must be the last keyword parameter */
2090
LispDestroy("%s %s: syntax error parsing %s",
2091
fnames[type], name, ATOMID(spec));
2093
LispDestroy("builtin function cannot have &AUX arguments");
2098
/* Untill more lambda-list keywords supported, don't allow
2099
* argument names starting with the '&' character */
2101
LispDestroy("%s %s: %s not allowed/implemented",
2102
fnames[type], name, ATOMID(spec));
2105
/* Add argument to alist */
2107
count = alist->auxs.num_symbols;
2108
REALLOC_OBJECTS(alist->auxs.symbols, count + 1);
2109
REALLOC_OBJECTS(alist->auxs.initials, count + 1);
2110
alist->auxs.symbols[count] = spec;
2111
alist->auxs.initials[count] = default_value;
2112
++alist->auxs.num_symbols;
2115
++alist->num_arguments;
2120
++alist->num_arguments;
2123
/* Add to keyword package, and set the keyword in the
2124
* argument list, so that a function argument keyword
2125
* will reference the same object, and make comparison
2127
spec = LispCheckKeyword(spec);
2128
count = alist->keys.num_symbols;
2129
REALLOC_OBJECTS(alist->keys.keys, count + 1);
2130
REALLOC_OBJECTS(alist->keys.defaults, count + 1);
2131
REALLOC_OBJECTS(alist->keys.sforms, count + 1);
2132
REALLOC_OBJECTS(alist->keys.symbols, count + 1);
2133
alist->keys.symbols[count] = spec;
2134
alist->keys.defaults[count] = default_value;
2135
alist->keys.sforms[count] = NULL;
2136
alist->keys.keys[count] = NULL;
2137
++alist->keys.num_symbols;
2140
++alist->num_arguments;
2142
else if (optional) {
2143
count = alist->optionals.num_symbols;
2144
REALLOC_OBJECTS(alist->optionals.symbols, count + 1);
2145
REALLOC_OBJECTS(alist->optionals.defaults, count + 1);
2146
REALLOC_OBJECTS(alist->optionals.sforms, count + 1);
2147
alist->optionals.symbols[count] = spec;
2148
alist->optionals.defaults[count] = default_value;
2149
alist->optionals.sforms[count] = NULL;
2150
++alist->optionals.num_symbols;
2153
++alist->num_arguments;
2156
count = alist->normals.num_symbols;
2157
REALLOC_OBJECTS(alist->normals.symbols, count + 1);
2158
alist->normals.symbols[count] = spec;
2159
++alist->normals.num_symbols;
2162
++alist->num_arguments;
2167
/* Check for dotted argument list */
2169
LispDestroy("%s %s: %s cannot end %s arguments",
2170
fnames[type], name, STROBJ(list), types[type]);
2173
alist->description = LispGetAtomString(description, 0);
2179
LispAddBuiltinFunction(LispBuiltin *builtin)
2181
static LispObj stream;
2182
static LispString string;
2183
static int first = 1;
2184
LispObj *name, *obj, *list, *cons, *code;
2187
int length = lisp__data.protect.length;
2190
stream.type = LispStream_t;
2191
stream.data.stream.source.string = &string;
2192
stream.data.stream.pathname = NIL;
2193
stream.data.stream.type = LispStreamString;
2194
stream.data.stream.readable = 1;
2195
stream.data.stream.writable = 0;
2199
string.string = builtin->declaration;
2200
string.length = strlen(builtin->declaration);
2204
LispPushInput(&stream);
2206
list = cons = CONS(name, NIL);
2207
if (length + 1 >= lisp__data.protect.space)
2209
lisp__data.protect.objects[lisp__data.protect.length++] = list;
2210
while ((obj = LispRead()) != NULL) {
2211
RPLACD(cons, CONS(obj, NIL));
2214
LispPopInput(&stream);
2216
atom = name->data.atom;
2217
alist = LispCheckArguments(builtin->type, CDR(list), atom->string, 1);
2218
builtin->symbol = CAR(list);
2219
LispSetAtomBuiltinProperty(atom, builtin, alist);
2220
LispUseArgList(alist);
2222
/* Make function a extern symbol, unless told to not do so */
2223
if (!builtin->internal)
2224
LispExportSymbol(name);
2226
lisp__data.protect.length = length;
2227
COD = code; /* LispRead protect data in COD */
2231
LispAllocSeg(LispObjSeg *seg, int cellcount)
2234
LispObj **list, *obj;
2236
DISABLE_INTERRUPTS();
2237
while (seg->nfree < cellcount) {
2238
if ((obj = (LispObj*)calloc(1, sizeof(LispObj) * segsize)) == NULL) {
2239
ENABLE_INTERRUPTS();
2240
LispDestroy("out of memory");
2242
if ((list = (LispObj**)realloc(seg->objects,
2243
sizeof(LispObj*) * (seg->nsegs + 1))) == NULL) {
2245
ENABLE_INTERRUPTS();
2246
LispDestroy("out of memory");
2248
seg->objects = list;
2249
seg->objects[seg->nsegs] = obj;
2251
seg->nfree += segsize;
2252
seg->nobjs += segsize;
2253
for (i = 1; i < segsize; i++, obj++) {
2254
/* Objects of type cons are the most used, save some time
2255
* by not setting it's type in LispNewCons. */
2256
obj->type = LispCons_t;
2259
obj->type = LispCons_t;
2260
CDR(obj) = seg->freeobj;
2261
seg->freeobj = seg->objects[seg->nsegs];
2265
LispMessage("gc: %d cell(s) allocated at %d segment(s)",
2266
seg->nobjs, seg->nsegs);
2268
ENABLE_INTERRUPTS();
2272
LispMark(register LispObj *object)
2275
switch (OBJECT_TYPE(object)) {
2280
case LispFunction_t:
2283
if (OPAQUEP(object->data.lambda.name))
2284
object->data.lambda.name->mark = 1;
2286
LispMark(object->data.lambda.data);
2287
object = object->data.lambda.code;
2290
case LispBackquote_t:
2291
case LispFunctionQuote_t:
2293
object = object->data.quote;
2295
case LispPathname_t:
2297
object = object->data.pathname;
2301
object = object->data.comma.eval;
2304
if (POINTERP(object->data.complex.real))
2305
object->data.complex.real->mark = 1;
2306
if (POINTERP(object->data.complex.imag))
2307
object->data.complex.imag->mark = 1;
2311
for (; CONSP(object) && !object->mark; object = CDR(object)) {
2313
switch (OBJECT_TYPE(CAR(object))) {
2318
case LispPackage_t: /* protected in gc */
2326
case LispBigratio_t:
2327
CAR(object)->mark = 1;
2330
LispMark(CAR(object));
2334
if (POINTERP(object) && !object->mark)
2338
LispMark(object->data.array.list);
2340
object = object->data.array.dim;
2344
object = object->data.struc.fields;
2348
LispMark(object->data.stream.pathname);
2349
if (object->data.stream.type == LispStreamPipe) {
2351
object = object->data.stream.source.program->errorp;
2356
object->data.regex.pattern->mark = 1;
2358
case LispBytecode_t:
2360
object = object->data.bytecode.code;
2362
case LispHashTable_t: {
2364
LispHashEntry *entry = object->data.hash.table->entries,
2365
*last = entry + object->data.hash.table->num_entries;
2370
for (; entry < last; entry++) {
2371
for (i = 0; i < entry->count; i++) {
2372
switch (OBJECT_TYPE(entry->keys[i])) {
2377
case LispFunction_t:
2386
case LispBigratio_t:
2387
entry->keys[i]->mark = 1;
2390
LispMark(entry->keys[i]);
2393
switch (OBJECT_TYPE(entry->values[i])) {
2398
case LispFunction_t:
2407
case LispBigratio_t:
2408
entry->values[i]->mark = 1;
2411
LispMark(entry->values[i]);
2424
LispProt(register LispObj *object)
2427
switch (OBJECT_TYPE(object)) {
2432
case LispFunction_t:
2435
if (OPAQUEP(object->data.lambda.name))
2436
object->data.lambda.name->prot = 1;
2438
LispProt(object->data.lambda.data);
2439
object = object->data.lambda.code;
2442
case LispBackquote_t:
2443
case LispFunctionQuote_t:
2445
object = object->data.quote;
2447
case LispPathname_t:
2449
object = object->data.pathname;
2453
object = object->data.comma.eval;
2456
if (POINTERP(object->data.complex.real))
2457
object->data.complex.real->prot = 1;
2458
if (POINTERP(object->data.complex.imag))
2459
object->data.complex.imag->prot = 1;
2463
for (; CONSP(object) && !object->prot; object = CDR(object)) {
2465
switch (OBJECT_TYPE(CAR(object))) {
2470
case LispFunction_t:
2471
case LispPackage_t: /* protected in gc */
2479
case LispBigratio_t:
2480
CAR(object)->prot = 1;
2483
LispProt(CAR(object));
2487
if (POINTERP(object) && !object->prot)
2491
LispProt(object->data.array.list);
2493
object = object->data.array.dim;
2497
object = object->data.struc.fields;
2501
LispProt(object->data.stream.pathname);
2502
if (object->data.stream.type == LispStreamPipe) {
2504
object = object->data.stream.source.program->errorp;
2509
object->data.regex.pattern->prot = 1;
2511
case LispBytecode_t:
2513
object = object->data.bytecode.code;
2515
case LispHashTable_t: {
2517
LispHashEntry *entry = object->data.hash.table->entries,
2518
*last = entry + object->data.hash.table->num_entries;
2523
for (; entry < last; entry++) {
2524
for (i = 0; i < entry->count; i++) {
2525
switch (OBJECT_TYPE(entry->keys[i])) {
2530
case LispFunction_t:
2539
case LispBigratio_t:
2540
entry->keys[i]->prot = 1;
2543
LispProt(entry->keys[i]);
2546
switch (OBJECT_TYPE(entry->values[i])) {
2551
case LispFunction_t:
2560
case LispBigratio_t:
2561
entry->values[i]->prot = 1;
2564
LispProt(entry->values[i]);
2577
LispProtect(LispObj *key, LispObj *list)
2579
PRO = CONS(CONS(key, list), PRO);
2583
LispUProtect(LispObj *key, LispObj *list)
2585
LispObj *prev, *obj;
2587
for (prev = obj = PRO; obj != NIL; prev = obj, obj = CDR(obj))
2588
if (CAR(CAR(obj)) == key && CDR(CAR(obj)) == list) {
2592
CDR(prev) = CDR(obj);
2596
LispDestroy("no match for %s, at UPROTECT", STROBJ(key));
2600
Lisp__New(LispObj *car, LispObj *cdr)
2607
lisp__data.gc.average = (objseg.nfree + lisp__data.gc.average) >> 1;
2608
if (lisp__data.gc.average < minfree) {
2609
if (lisp__data.gc.expandbits < 6)
2610
++lisp__data.gc.expandbits;
2612
else if (lisp__data.gc.expandbits)
2613
--lisp__data.gc.expandbits;
2614
/* For 32 bit computers, where sizeof(LispObj) == 16,
2615
* minfree is set to 1024, and expandbits limited to 6,
2616
* the maximum extra memory requested here should be 1Mb
2618
cellcount = minfree << lisp__data.gc.expandbits;
2620
/* Try to keep at least 3 times more free cells than the de number
2621
* of used cells in the freelist, to amenize the cost of the gc time,
2622
* in the, currently, very simple gc strategy code. */
2623
cellcount = (objseg.nobjs - objseg.nfree) * 3;
2624
cellcount = cellcount + (minfree - (cellcount % minfree));
2627
if (objseg.freeobj == NIL || objseg.nfree < cellcount)
2628
LispAllocSeg(&objseg, cellcount);
2630
obj = objseg.freeobj;
2631
objseg.freeobj = CDR(obj);
2638
LispNew(LispObj *car, LispObj *cdr)
2640
LispObj *obj = objseg.freeobj;
2643
obj = Lisp__New(car, cdr);
2645
objseg.freeobj = CDR(obj);
2653
LispNewAtom(char *str, int intern)
2656
LispAtom *atom = LispDoGetAtom(str, 0);
2659
if (intern && atom->package == NULL)
2660
atom->package = PACKAGE;
2662
return (atom->object);
2665
if (atomseg.freeobj == NIL)
2666
LispAllocSeg(&atomseg, pagesize);
2667
object = atomseg.freeobj;
2668
atomseg.freeobj = CDR(object);
2671
object->type = LispAtom_t;
2672
object->data.atom = atom;
2673
atom->object = object;
2675
atom->package = PACKAGE;
2681
LispNewStaticAtom(char *str)
2684
LispAtom *atom = LispDoGetAtom(str, 1);
2686
object = LispNewSymbol(atom);
2692
LispNewSymbol(LispAtom *atom)
2695
if (atom->package == NULL)
2696
atom->package = PACKAGE;
2698
return (atom->object);
2703
if (atomseg.freeobj == NIL)
2704
LispAllocSeg(&atomseg, pagesize);
2705
symbol = atomseg.freeobj;
2706
atomseg.freeobj = CDR(symbol);
2709
symbol->type = LispAtom_t;
2710
symbol->data.atom = atom;
2711
atom->object = symbol;
2712
atom->package = PACKAGE;
2718
/* function representation is created on demand and never released,
2719
* even if the function is undefined and never defined again */
2721
LispNewFunction(LispObj *symbol)
2725
if (symbol->data.atom->function)
2726
return (symbol->data.atom->function);
2728
if (symbol->data.atom->package == NULL)
2729
symbol->data.atom->package = PACKAGE;
2731
if (atomseg.freeobj == NIL)
2732
LispAllocSeg(&atomseg, pagesize);
2733
function = atomseg.freeobj;
2734
atomseg.freeobj = CDR(function);
2737
function->type = LispFunction_t;
2738
function->data.atom = symbol->data.atom;
2739
symbol->data.atom->function = function;
2744
/* symbol name representation is created on demand and never released */
2746
LispSymbolName(LispObj *symbol)
2749
LispAtom *atom = symbol->data.atom;
2752
return (atom->name);
2754
if (atomseg.freeobj == NIL)
2755
LispAllocSeg(&atomseg, pagesize);
2756
name = atomseg.freeobj;
2757
atomseg.freeobj = CDR(name);
2760
name->type = LispString_t;
2761
THESTR(name) = atom->string;
2762
STRLEN(name) = strlen(atom->string);
2763
name->data.string.writable = 0;
2770
LispNewFunctionQuote(LispObj *object)
2772
LispObj *quote = LispNew(object, NIL);
2774
quote->type = LispFunctionQuote_t;
2775
quote->data.quote = object;
2781
LispNewDFloat(double value)
2783
LispObj *dfloat = objseg.freeobj;
2786
dfloat = Lisp__New(NIL, NIL);
2788
objseg.freeobj = CDR(dfloat);
2791
dfloat->type = LispDFloat_t;
2792
dfloat->data.dfloat = value;
2798
LispNewString(char *str, long length, int alloced)
2801
LispObj *string = objseg.freeobj;
2804
string = Lisp__New(NIL, NIL);
2806
objseg.freeobj = CDR(string);
2812
cstring = LispMalloc(length + 1);
2813
memcpy(cstring, str, length);
2814
cstring[length] = '\0';
2817
string->type = LispString_t;
2818
THESTR(string) = cstring;
2819
STRLEN(string) = length;
2820
string->data.string.writable = 1;
2826
LispNewComplex(LispObj *realpart, LispObj *imagpart)
2828
LispObj *complexp = objseg.freeobj;
2830
if (complexp == NIL)
2831
complexp = Lisp__New(realpart, imagpart);
2833
objseg.freeobj = CDR(complexp);
2836
complexp->type = LispComplex_t;
2837
complexp->data.complex.real = realpart;
2838
complexp->data.complex.imag = imagpart;
2844
LispNewInteger(long integer)
2846
if (integer > MOST_POSITIVE_FIXNUM || integer < MOST_NEGATIVE_FIXNUM) {
2847
LispObj *object = objseg.freeobj;
2850
object = Lisp__New(NIL, NIL);
2852
objseg.freeobj = CDR(object);
2855
object->type = LispInteger_t;
2856
object->data.integer = integer;
2860
return (FIXNUM(integer));
2864
LispNewRatio(long num, long den)
2866
LispObj *ratio = objseg.freeobj;
2869
ratio = Lisp__New(NIL, NIL);
2871
objseg.freeobj = CDR(ratio);
2874
ratio->type = LispRatio_t;
2875
ratio->data.ratio.numerator = num;
2876
ratio->data.ratio.denominator = den;
2882
LispNewVector(LispObj *objects)
2886
LispObj *array, *dimension;
2888
for (count = 0, array = objects; CONSP(array); count++, array = CDR(array))
2891
GC_PROTECT(objects);
2892
dimension = CONS(FIXNUM(count), NIL);
2893
array = LispNew(objects, dimension);
2894
array->type = LispArray_t;
2895
array->data.array.list = objects;
2896
array->data.array.dim = dimension;
2897
array->data.array.rank = 1;
2898
array->data.array.type = LispNil_t;
2899
array->data.array.zero = count == 0;
2906
LispNewQuote(LispObj *object)
2908
LispObj *quote = LispNew(object, NIL);
2910
quote->type = LispQuote_t;
2911
quote->data.quote = object;
2917
LispNewBackquote(LispObj *object)
2919
LispObj *backquote = LispNew(object, NIL);
2921
backquote->type = LispBackquote_t;
2922
backquote->data.quote = object;
2928
LispNewComma(LispObj *object, int atlist)
2930
LispObj *comma = LispNew(object, NIL);
2932
comma->type = LispComma_t;
2933
comma->data.comma.eval = object;
2934
comma->data.comma.atlist = atlist;
2940
LispNewCons(LispObj *car, LispObj *cdr)
2942
LispObj *cons = objseg.freeobj;
2945
cons = Lisp__New(car, cdr);
2947
objseg.freeobj = CDR(cons);
2957
LispNewLambda(LispObj *name, LispObj *code, LispObj *data, LispFunType type)
2959
LispObj *fun = LispNew(data, code);
2961
fun->type = LispLambda_t;
2962
fun->funtype = type;
2963
fun->data.lambda.name = name;
2964
fun->data.lambda.code = code;
2965
fun->data.lambda.data = data;
2971
LispNewStruct(LispObj *fields, LispObj *def)
2973
LispObj *struc = LispNew(fields, def);
2975
struc->type = LispStruct_t;
2976
struc->data.struc.fields = fields;
2977
struc->data.struc.def = def;
2983
LispNewOpaque(void *data, int type)
2985
LispObj *opaque = LispNew(NIL, NIL);
2987
opaque->type = LispOpaque_t;
2988
opaque->data.opaque.data = data;
2989
opaque->data.opaque.type = type;
2994
/* string argument must be static, or allocated */
2996
LispNewKeyword(char *string)
3000
if (PACKAGE != lisp__data.keyword) {
3001
LispObj *savepackage;
3002
LispPackage *savepack;
3004
/* Save package environment */
3005
savepackage = PACKAGE;
3006
savepack = lisp__data.pack;
3008
/* Change package environment */
3009
PACKAGE = lisp__data.keyword;
3010
lisp__data.pack = lisp__data.key;
3012
/* Create symbol in keyword package */
3013
keyword = LispNewStaticAtom(string);
3015
/* Restore package environment */
3016
PACKAGE = savepackage;
3017
lisp__data.pack = savepack;
3020
/* Just create symbol in keyword package */
3021
keyword = LispNewStaticAtom(string);
3023
/* Export keyword symbol */
3024
LispExportSymbol(keyword);
3026
/* All keywords are constants */
3027
keyword->data.atom->constant = 1;
3029
/* XXX maybe should bound the keyword to itself, but that would
3030
* require allocating a LispProperty structure for every keyword */
3036
LispNewPathname(LispObj *obj)
3038
LispObj *path = LispNew(obj, NIL);
3040
path->type = LispPathname_t;
3041
path->data.pathname = obj;
3047
LispNewStringStream(char *string, int flags, long length, int alloced)
3049
LispObj *stream = LispNew(NIL, NIL);
3051
SSTREAMP(stream) = LispCalloc(1, sizeof(LispString));
3053
SSTREAMP(stream)->string = string;
3055
SSTREAMP(stream)->string = LispMalloc(length + 1);
3056
memcpy(SSTREAMP(stream)->string, string, length);
3057
SSTREAMP(stream)->string[length] = '\0';
3060
stream->type = LispStream_t;
3062
SSTREAMP(stream)->length = length;
3063
LispMused(SSTREAMP(stream));
3064
LispMused(SSTREAMP(stream)->string);
3065
stream->data.stream.type = LispStreamString;
3066
stream->data.stream.readable = (flags & STREAM_READ) != 0;
3067
stream->data.stream.writable = (flags & STREAM_WRITE) != 0;
3068
SSTREAMP(stream)->space = length + 1;
3070
stream->data.stream.pathname = NIL;
3076
LispNewFileStream(LispFile *file, LispObj *path, int flags)
3078
LispObj *stream = LispNew(NIL, NIL);
3080
stream->type = LispStream_t;
3081
FSTREAMP(stream) = file;
3082
stream->data.stream.pathname = path;
3083
stream->data.stream.type = LispStreamFile;
3084
stream->data.stream.readable = (flags & STREAM_READ) != 0;
3085
stream->data.stream.writable = (flags & STREAM_WRITE) != 0;
3091
LispNewPipeStream(LispPipe *program, LispObj *path, int flags)
3093
LispObj *stream = LispNew(NIL, NIL);
3095
stream->type = LispStream_t;
3096
PSTREAMP(stream) = program;
3097
stream->data.stream.pathname = path;
3098
stream->data.stream.type = LispStreamPipe;
3099
stream->data.stream.readable = (flags & STREAM_READ) != 0;
3100
stream->data.stream.writable = (flags & STREAM_WRITE) != 0;
3106
LispNewStandardStream(LispFile *file, LispObj *description, int flags)
3108
LispObj *stream = LispNew(NIL, NIL);
3110
stream->type = LispStream_t;
3111
FSTREAMP(stream) = file;
3112
stream->data.stream.pathname = description;
3113
stream->data.stream.type = LispStreamStandard;
3114
stream->data.stream.readable = (flags & STREAM_READ) != 0;
3115
stream->data.stream.writable = (flags & STREAM_WRITE) != 0;
3121
LispNewBignum(mpi *bignum)
3123
LispObj *integer = LispNew(NIL, NIL);
3125
integer->type = LispBignum_t;
3126
integer->data.mp.integer = bignum;
3127
LispMused(bignum->digs);
3134
LispNewBigratio(mpr *bigratio)
3136
LispObj *ratio = LispNew(NIL, NIL);
3138
ratio->type = LispBigratio_t;
3139
ratio->data.mp.ratio = bigratio;
3140
LispMused(mpr_num(bigratio)->digs);
3141
LispMused(mpr_den(bigratio)->digs);
3142
LispMused(bigratio);
3147
/* name must be of type LispString_t */
3149
LispNewPackage(LispObj *name, LispObj *nicknames)
3151
LispObj *package = LispNew(name, nicknames);
3152
LispPackage *pack = LispCalloc(1, sizeof(LispPackage));
3154
package->type = LispPackage_t;
3155
package->data.package.name = name;
3156
package->data.package.nicknames = nicknames;
3157
package->data.package.package = pack;
3165
LispSymbolFunction(LispObj *symbol)
3167
LispAtom *atom = symbol->data.atom;
3169
if ((atom->a_builtin &&
3170
atom->property->fun.builtin->type == LispFunction) ||
3171
(atom->a_function &&
3172
atom->property->fun.function->funtype == LispFunction) ||
3173
(atom->a_defstruct &&
3174
atom->property->structure.function != STRUCT_NAME) ||
3175
/* XXX currently bytecode is only generated for functions */
3177
symbol = FUNCTION(symbol);
3179
LispDestroy("SYMBOL-FUNCTION: %s is not a function", STROBJ(symbol));
3185
static INLINE LispObj *
3186
LispGetVarPack(LispObj *symbol)
3192
string = ATOMID(symbol);
3193
ii = STRHASH(string);
3195
atom = lisp__data.pack->atoms[ii];
3197
if (strcmp(atom->string, string) == 0)
3198
return (atom->object);
3203
/* Symbol not found, just import it */
3207
/* package must be of type LispPackage_t */
3209
LispUsePackage(LispObj *package)
3214
LispObj **pentry, **eentry;
3216
/* Already using its own symbols... */
3217
if (package == PACKAGE)
3220
/* Check if package not already in use-package list */
3221
for (pentry = lisp__data.pack->use.pairs,
3222
eentry = pentry + lisp__data.pack->use.length;
3223
pentry < eentry; pentry++)
3224
if (*pentry == package)
3227
/* Remember this package is in the use-package list */
3228
if (lisp__data.pack->use.length + 1 >= lisp__data.pack->use.space) {
3229
LispObj **pairs = realloc(lisp__data.pack->use.pairs,
3230
(lisp__data.pack->use.space + 1) *
3234
LispDestroy("out of memory");
3236
lisp__data.pack->use.pairs = pairs;
3237
++lisp__data.pack->use.space;
3239
lisp__data.pack->use.pairs[lisp__data.pack->use.length++] = package;
3241
/* Import all extern symbols from package */
3242
pack = package->data.package.package;
3244
/* Traverse atom list, searching for extern symbols */
3245
for (i = 0; i < STRTBLSZ; i++) {
3246
atom = pack->atoms[i];
3249
LispImportSymbol(atom->object);
3255
/* symbol must be of type LispAtom_t */
3257
LispImportSymbol(LispObj *symbol)
3263
current = LispGetVarPack(symbol);
3264
if (current == NULL || current->data.atom->property == NOPROPERTY) {
3267
if (symbol->data.atom->a_object) {
3268
/* If it is a bounded variable */
3269
if (lisp__data.pack->glb.length + 1 >= lisp__data.pack->glb.space)
3270
LispMoreGlobals(lisp__data.pack);
3271
lisp__data.pack->glb.pairs[lisp__data.pack->glb.length++] = symbol;
3274
/* Create copy of atom in current package */
3275
atom = LispDoGetAtom(ATOMID(symbol), 0);
3276
/* Need to create a copy because if anything new is atached to the
3277
* property, the current package is the owner, not the previous one. */
3279
/* And reference the same properties */
3280
atom->property = symbol->data.atom->property;
3284
else if (current->data.atom->property != symbol->data.atom->property) {
3285
/* Symbol already exists in the current package,
3286
* but does not reference the same variable */
3287
LispContinuable("Symbol %s already defined in package %s. Redefine?",
3288
ATOMID(symbol), THESTR(PACKAGE->data.package.name));
3290
atom = current->data.atom;
3292
/* Continued from error, redefine variable */
3293
LispDecrementAtomReference(atom);
3294
atom->property = symbol->data.atom->property;
3296
atom->a_object = atom->a_function = atom->a_builtin =
3297
atom->a_property = atom->a_defsetf = atom->a_defstruct = 0;
3302
/* Symbol is already available in the current package, just update */
3303
atom = current->data.atom;
3308
/* If importing an important system variable */
3309
atom->watch = symbol->data.atom->watch;
3311
/* Update constant flag */
3312
atom->constant = symbol->data.atom->constant;
3314
/* Set home-package and unique-atom associated with symbol */
3315
atom->package = symbol->data.atom->package;
3316
atom->object = symbol->data.atom->object;
3318
if (symbol->data.atom->a_object)
3320
if (symbol->data.atom->a_function)
3321
atom->a_function = 1;
3322
else if (symbol->data.atom->a_builtin)
3323
atom->a_builtin = 1;
3324
else if (symbol->data.atom->a_compiled)
3325
atom->a_compiled = 1;
3326
if (symbol->data.atom->a_property)
3327
atom->a_property = 1;
3328
if (symbol->data.atom->a_defsetf)
3329
atom->a_defsetf = 1;
3330
if (symbol->data.atom->a_defstruct)
3331
atom->a_defstruct = 1;
3334
/* Increase reference count, more than one package using the symbol */
3335
LispIncrementAtomReference(symbol->data.atom);
3338
/* symbol must be of type LispAtom_t */
3340
LispExportSymbol(LispObj *symbol)
3342
/* This does not automatically export symbols to another package using
3343
* the symbols of the current package */
3344
symbol->data.atom->ext = 1;
3349
LispGetVar(LispObj *atom)
3351
return (LispDoGetVar(atom));
3354
static INLINE LispObj *
3355
LispDoGetVar(LispObj *atom)
3357
#define LispDoGetVar LispGetVar
3359
LispGetVar(LispObj *atom)
3363
int i, base, offset;
3366
name = atom->data.atom;
3367
if (name->constant && name->package == lisp__data.keyword)
3370
/* XXX offset should be stored elsewhere, it is unique, like the string
3371
* pointer. Unless a multi-thread interface is implemented (where
3372
* multiple stacks would be required, the offset value should be
3373
* stored with the string, so that a few cpu cicles could be saved
3374
* by initializing the value to -1, and only searching for the symbol
3375
* binding if it is not -1, and if no binding is found, because the
3376
* lexical scope was left, reset offset to -1. */
3377
offset = name->offset;
3379
base = lisp__data.env.lex;
3380
i = lisp__data.env.head - 1;
3382
if (offset <= i && (offset >= base || name->dyn) &&
3383
lisp__data.env.names[offset] == id)
3384
return (lisp__data.env.values[offset]);
3386
for (; i >= base; i--)
3387
if (lisp__data.env.names[i] == id) {
3390
return (lisp__data.env.values[i]);
3394
/* Keep searching as maybe a rebound dynamic variable */
3396
if (lisp__data.env.names[i] == id) {
3399
return (lisp__data.env.values[i]);
3402
if (name->a_object) {
3403
/* Check for a symbol defined as special, but not yet bound. */
3404
if (name->property->value == UNBOUND)
3407
return (name->property->value);
3411
return (name->a_object ? name->property->value : NULL);
3415
/* Same code as LispDoGetVar, but returns the address of the pointer to
3416
* the object value. Used only by the debugger */
3418
LispGetVarAddr(LispObj *atom)
3424
name = atom->data.atom;
3425
if (name->constant && name->package == lisp__data.keyword)
3430
i = lisp__data.env.head - 1;
3431
for (base = lisp__data.env.lex; i >= base; i--)
3432
if (lisp__data.env.names[i] == id)
3433
return (&(lisp__data.env.values[i]));
3437
if (lisp__data.env.names[i] == id)
3438
return (&(lisp__data.env.values[i]));
3440
if (name->a_object) {
3441
/* Check for a symbol defined as special, but not yet bound */
3442
if (name->property->value == UNBOUND)
3445
return (&(name->property->value));
3449
return (name->a_object ? &(name->property->value) : NULL);
3453
/* Only removes global variables. To be called by makunbound
3454
* Local variables are unbounded once their block is closed anyway.
3457
LispUnsetVar(LispObj *atom)
3459
LispAtom *name = atom->data.atom;
3461
if (name->package) {
3463
LispPackage *pack = name->package->data.package.package;
3465
for (i = pack->glb.length - 1; i > 0; i--)
3466
if (pack->glb.pairs[i] == atom) {
3467
LispRemAtomObjectProperty(name);
3469
if (i < pack->glb.length)
3470
memmove(pack->glb.pairs + i, pack->glb.pairs + i + 1,
3471
sizeof(LispObj*) * (pack->glb.length - i));
3473
/* unset hint about dynamically binded variable */
3482
LispAddVar(LispObj *atom, LispObj *obj)
3484
if (lisp__data.env.length >= lisp__data.env.space)
3485
LispMoreEnvironment();
3487
LispDoAddVar(atom, obj);
3493
LispDoAddVar(LispObj *symbol, LispObj *value)
3495
LispAtom *atom = symbol->data.atom;
3497
atom->offset = lisp__data.env.length;
3498
lisp__data.env.values[lisp__data.env.length] = value;
3499
lisp__data.env.names[lisp__data.env.length++] = atom->string;
3503
LispSetVar(LispObj *atom, LispObj *obj)
3507
int i, base, offset;
3510
name = atom->data.atom;
3511
offset = name->offset;
3513
base = lisp__data.env.lex;
3514
i = lisp__data.env.head - 1;
3516
if (offset <= i && (offset >= base || name->dyn) &&
3517
lisp__data.env.names[offset] == id)
3518
return (lisp__data.env.values[offset] = obj);
3520
for (; i >= base; i--)
3521
if (lisp__data.env.names[i] == id) {
3524
return (lisp__data.env.values[i] = obj);
3529
if (lisp__data.env.names[i] == id)
3530
return (lisp__data.env.values[i] = obj);
3533
LispSetAtomObjectProperty(name, obj);
3538
return (SETVALUE(name, obj));
3541
if (name->a_object) {
3543
LispSetAtomObjectProperty(name, obj);
3548
return (SETVALUE(name, obj));
3551
LispSetAtomObjectProperty(name, obj);
3553
pack = name->package->data.package.package;
3554
if (pack->glb.length >= pack->glb.space)
3555
LispMoreGlobals(pack);
3557
pack->glb.pairs[pack->glb.length++] = atom;
3563
LispProclaimSpecial(LispObj *atom, LispObj *value, LispObj *doc)
3565
int i = 0, dyn, glb;
3570
name = atom->data.atom;
3571
pack = name->package->data.package.package;
3575
/* Note: don't check if a local variable already is using the symbol */
3576
for (i = pack->glb.length - 1; i >= 0; i--)
3577
if (pack->glb.pairs[i] == atom) {
3584
if (name->property->value == UNBOUND && value)
3585
/* if variable was just made special, but not bounded */
3586
LispSetAtomObjectProperty(name, value);
3589
/* Already a global variable, but not marked as special.
3590
* Set hint about dynamically binded variable. */
3593
/* create new special variable */
3594
LispSetAtomObjectProperty(name, value ? value : UNBOUND);
3596
if (pack->glb.length >= pack->glb.space)
3597
LispMoreGlobals(pack);
3599
pack->glb.pairs[pack->glb.length] = atom;
3601
/* set hint about possibly dynamically binded variable */
3606
LispAddDocumentation(atom, doc, LispDocVariable);
3610
LispDefconstant(LispObj *atom, LispObj *value, LispObj *doc)
3613
LispAtom *name = atom->data.atom;
3614
LispPackage *pack = name->package->data.package.package;
3616
/* Unset hint about dynamically binded variable, if set. */
3619
/* Check if variable is bounded as a global variable */
3620
for (i = pack->glb.length - 1; i >= 0; i--)
3621
if (pack->glb.pairs[i] == atom)
3625
/* Not a global variable */
3626
if (pack->glb.length >= pack->glb.space)
3627
LispMoreGlobals(pack);
3629
pack->glb.pairs[pack->glb.length] = atom;
3633
/* If already a constant variable */
3634
if (name->constant && name->a_object && name->property->value != value)
3635
LispWarning("constant %s is being redefined", STROBJ(atom));
3639
/* Set constant value */
3640
LispSetAtomObjectProperty(name, value);
3643
LispAddDocumentation(atom, doc, LispDocVariable);
3647
LispAddDocumentation(LispObj *symbol, LispObj *documentation, LispDocType_t type)
3654
if (!SYMBOLP(symbol) || !STRINGP(documentation))
3655
LispDestroy("DOCUMENTATION: invalid argument");
3657
atom = symbol->data.atom;
3658
if (atom->documentation[type])
3659
LispRemDocumentation(symbol, type);
3661
/* allocate documentation in atomseg */
3662
if (atomseg.freeobj == NIL)
3663
LispAllocSeg(&atomseg, pagesize);
3664
length = STRLEN(documentation);
3665
string = LispMalloc(length);
3666
memcpy(string, THESTR(documentation), length);
3667
string[length] = '\0';
3668
object = atomseg.freeobj;
3669
atomseg.freeobj = CDR(object);
3672
object->type = LispString_t;
3673
THESTR(object) = string;
3674
STRLEN(object) = length;
3675
object->data.string.writable = 0;
3676
atom->documentation[type] = object;
3681
LispRemDocumentation(LispObj *symbol, LispDocType_t type)
3685
if (!SYMBOLP(symbol))
3686
LispDestroy("DOCUMENTATION: invalid argument");
3688
atom = symbol->data.atom;
3689
if (atom->documentation[type]) {
3690
/* reclaim object to atomseg */
3691
free(THESTR(atom->documentation[type]));
3692
CDR(atom->documentation[type]) = atomseg.freeobj;
3693
atomseg.freeobj = atom->documentation[type];
3694
atom->documentation[type] = NULL;
3700
LispGetDocumentation(LispObj *symbol, LispDocType_t type)
3704
if (!SYMBOLP(symbol))
3705
LispDestroy("DOCUMENTATION: invalid argument");
3707
atom = symbol->data.atom;
3709
return (atom->documentation[type] ? atom->documentation[type] : NIL);
3713
LispReverse(LispObj *list)
3715
LispObj *tmp, *res = NIL;
3717
while (list != NIL) {
3728
LispBeginBlock(LispObj *tag, LispBlockType type)
3731
unsigned blevel = lisp__data.block.block_level + 1;
3733
if (blevel > lisp__data.block.block_size) {
3736
if (blevel > MAX_STACK_DEPTH)
3737
LispDestroy("stack overflow");
3739
DISABLE_INTERRUPTS();
3740
blk = realloc(lisp__data.block.block, sizeof(LispBlock*) * (blevel + 1));
3743
if (blk == NULL || (block = malloc(sizeof(LispBlock))) == NULL) {
3744
ENABLE_INTERRUPTS();
3745
LispDestroy("out of memory");
3747
lisp__data.block.block = blk;
3748
lisp__data.block.block[lisp__data.block.block_size] = block;
3749
lisp__data.block.block_size = blevel;
3750
ENABLE_INTERRUPTS();
3752
block = lisp__data.block.block[lisp__data.block.block_level];
3753
if (type == LispBlockCatch && !CONSTANTP(tag)) {
3755
lisp__data.protect.objects[lisp__data.protect.length++] = tag;
3759
block->stack = lisp__data.stack.length;
3760
block->protect = lisp__data.protect.length;
3761
block->block_level = lisp__data.block.block_level;
3763
lisp__data.block.block_level = blevel;
3766
if (lisp__data.debugging) {
3767
block->debug_level = lisp__data.debug_level;
3768
block->debug_step = lisp__data.debug_step;
3776
LispEndBlock(LispBlock *block)
3778
lisp__data.protect.length = block->protect;
3779
lisp__data.block.block_level = block->block_level;
3782
if (lisp__data.debugging) {
3783
if (lisp__data.debug_level >= block->debug_level) {
3784
while (lisp__data.debug_level > block->debug_level) {
3786
--lisp__data.debug_level;
3789
lisp__data.debug_step = block->debug_step;
3795
LispBlockUnwind(LispBlock *block)
3798
int blevel = lisp__data.block.block_level;
3800
while (blevel > 0) {
3801
unwind = lisp__data.block.block[--blevel];
3802
if (unwind->type == LispBlockProtect) {
3805
if (unwind == block)
3806
/* jump above unwind block */
3812
LispEvalBackquoteObject(LispObj *argument, int list, int quote)
3814
LispObj *result = argument, *object;
3816
if (!POINTERP(argument))
3819
else if (XCOMMAP(argument)) {
3820
/* argument may need to be evaluated */
3824
if (!list && argument->data.comma.atlist)
3825
/* cannot append, not in a list */
3826
LispDestroy("EVAL: ,@ only allowed on lists");
3830
LispDestroy("EVAL: comma outside of backquote");
3832
result = object = argument->data.comma.eval;
3833
atlist = COMMAP(object) && object->data.comma.atlist;
3835
if (POINTERP(result) && (XCOMMAP(result) || XBACKQUOTEP(result)))
3836
/* nested commas, reduce 1 level, or backquote,
3837
* don't call LispEval or quote argument will be reset */
3838
result = LispEvalBackquoteObject(object, 0, quote);
3840
else if (quote == 0)
3841
/* just evaluate it */
3842
result = EVAL(result);
3845
result = result == object ? argument : COMMA(result, atlist);
3848
else if (XBACKQUOTEP(argument)) {
3849
object = argument->data.quote;
3851
result = LispEvalBackquote(object, quote + 1);
3853
result = result == object ? argument : BACKQUOTE(result);
3856
else if (XQUOTEP(argument) && POINTERP(argument->data.quote) &&
3857
(XCOMMAP(argument->data.quote) ||
3858
XBACKQUOTEP(argument->data.quote) ||
3859
XCONSP(argument->data.quote))) {
3860
/* ensures `',sym to be the same as `(quote ,sym) */
3861
object = argument->data.quote;
3863
result = LispEvalBackquote(argument->data.quote, quote);
3864
result = result == object ? argument : QUOTE(result);
3871
LispEvalBackquote(LispObj *argument, int quote)
3874
LispObj *result, *object, *cons, *cdr;
3876
if (!CONSP(argument))
3877
return (LispEvalBackquoteObject(argument, 0, quote));
3880
protect = lisp__data.protect.length;
3882
/* always generate a new list for the result, even if nothing
3883
* is evaluated. It is not expected to use backqoutes when
3886
/* reserve a GC protected slot for the result */
3887
if (protect + 1 >= lisp__data.protect.space)
3889
lisp__data.protect.objects[lisp__data.protect.length++] = NIL;
3891
for (cons = argument; ; cons = CDR(cons)) {
3892
/* if false, last argument, and if cons is not NIL, a dotted list */
3893
int list = CONSP(cons), insert;
3901
/* need to insert list elements in result, not just cons it? */
3902
insert = object->data.comma.atlist;
3906
/* evaluate object, if required */
3908
object = LispEvalBackquote(object, quote);
3910
object = LispEvalBackquoteObject(object, insert, quote);
3912
if (result == NIL) {
3913
/* if starting result list */
3916
result = cdr = CONS(object, NIL);
3918
result = cdr = object;
3919
/* gc protect result */
3920
lisp__data.protect.objects[protect] = result;
3923
if (!CONSP(object)) {
3924
result = cdr = object;
3925
/* gc protect result */
3926
lisp__data.protect.objects[protect] = result;
3929
result = cdr = CONS(CAR(object), NIL);
3930
/* gc protect result */
3931
lisp__data.protect.objects[protect] = result;
3933
/* add remaining elements to result */
3934
for (object = CDR(object);
3936
object = CDR(object)) {
3937
RPLACD(cdr, CONS(CAR(object), NIL));
3940
if (object != NIL) {
3941
/* object was a dotted list */
3942
RPLACD(cdr, object);
3950
LispDestroy("EVAL: cannot append to %s", STROBJ(cdr));
3954
RPLACD(cdr, CONS(object, NIL));
3958
RPLACD(cdr, object);
3963
if (!CONSP(object)) {
3964
RPLACD(cdr, object);
3965
/* if object is NIL, it is a empty list appended, not
3966
* creating a dotted list. */
3971
for (; CONSP(object); object = CDR(object)) {
3972
RPLACD(cdr, CONS(CAR(object), NIL));
3975
if (object != NIL) {
3976
/* object was a dotted list */
3977
RPLACD(cdr, object);
3984
/* if last argument list element processed */
3989
lisp__data.protect.length = protect;
3995
LispMoreEnvironment(void)
4000
DISABLE_INTERRUPTS();
4001
names = realloc(lisp__data.env.names,
4002
(lisp__data.env.space + 256) * sizeof(Atom_id));
4003
if (names != NULL) {
4004
values = realloc(lisp__data.env.values,
4005
(lisp__data.env.space + 256) * sizeof(LispObj*));
4006
if (values != NULL) {
4007
lisp__data.env.names = names;
4008
lisp__data.env.values = values;
4009
lisp__data.env.space += 256;
4010
ENABLE_INTERRUPTS();
4016
ENABLE_INTERRUPTS();
4017
LispDestroy("out of memory");
4025
DISABLE_INTERRUPTS();
4026
values = realloc(lisp__data.stack.values,
4027
(lisp__data.stack.space + 256) * sizeof(LispObj*));
4028
if (values == NULL) {
4029
ENABLE_INTERRUPTS();
4030
LispDestroy("out of memory");
4032
lisp__data.stack.values = values;
4033
lisp__data.stack.space += 256;
4034
ENABLE_INTERRUPTS();
4038
LispMoreGlobals(LispPackage *pack)
4042
DISABLE_INTERRUPTS();
4043
pairs = realloc(pack->glb.pairs,
4044
(pack->glb.space + 256) * sizeof(LispObj*));
4045
if (pairs == NULL) {
4046
ENABLE_INTERRUPTS();
4047
LispDestroy("out of memory");
4049
pack->glb.pairs = pairs;
4050
pack->glb.space += 256;
4051
ENABLE_INTERRUPTS();
4055
LispMoreProtects(void)
4059
DISABLE_INTERRUPTS();
4060
objects = realloc(lisp__data.protect.objects,
4061
(lisp__data.protect.space + 256) * sizeof(LispObj*));
4062
if (objects == NULL) {
4063
ENABLE_INTERRUPTS();
4064
LispDestroy("out of memory");
4066
lisp__data.protect.objects = objects;
4067
lisp__data.protect.space += 256;
4068
ENABLE_INTERRUPTS();
4072
LispMakeEnvironment(LispArgList *alist, LispObj *values,
4073
LispObj *name, int eval, int builtin)
4077
LispObj **symbols, **defaults, **sforms;
4079
#define BUILTIN_ARGUMENT(value) \
4080
lisp__data.stack.values[lisp__data.stack.length++] = value
4082
/* If the index value is from register variables, this
4083
* can save some cpu time. Useful for normal arguments
4084
* that are the most common, and thus the ones that
4085
* consume more time in LispMakeEnvironment. */
4086
#define BUILTIN_NO_EVAL_ARGUMENT(index, value) \
4087
lisp__data.stack.values[index] = value
4089
#define NORMAL_ARGUMENT(symbol, value) \
4090
LispDoAddVar(symbol, value)
4093
base = lisp__data.stack.length;
4094
if (base + alist->num_arguments > lisp__data.stack.space) {
4097
while (base + alist->num_arguments > lisp__data.stack.space);
4101
base = lisp__data.env.length;
4102
if (base + alist->num_arguments > lisp__data.env.space) {
4104
LispMoreEnvironment();
4105
while (base + alist->num_arguments > lisp__data.env.space);
4109
desc = alist->description;
4114
goto optional_label;
4126
/* Code below is done in several almost identical loops, to avoid
4127
* checking the value of the arguments eval and builtin too much times */
4130
/* Normal arguments */
4133
count = alist->normals.num_symbols;
4136
for (; i < count && CONSP(values); i++, values = CDR(values)) {
4137
BUILTIN_ARGUMENT(EVAL(CAR(values)));
4141
for (; i < count && CONSP(values); i++, values = CDR(values)) {
4142
BUILTIN_NO_EVAL_ARGUMENT(base + i, CAR(values));
4144
/* macro BUILTIN_NO_EVAL_ARGUMENT does not update
4145
* lisp__data.stack.length, as there is no risk of GC while
4146
* adding the arguments. */
4147
lisp__data.stack.length += i;
4151
symbols = alist->normals.symbols;
4153
for (; i < count && CONSP(values); i++, values = CDR(values)) {
4154
NORMAL_ARGUMENT(symbols[i], EVAL(CAR(values)));
4158
for (; i < count && CONSP(values); i++, values = CDR(values)) {
4159
NORMAL_ARGUMENT(symbols[i], CAR(values));
4164
LispDestroy("%s: too few arguments", STROBJ(name));
4168
goto optional_label;
4182
count = alist->optionals.num_symbols;
4183
defaults = alist->optionals.defaults;
4184
sforms = alist->optionals.sforms;
4187
for (; i < count && CONSP(values); i++, values = CDR(values))
4188
BUILTIN_ARGUMENT(EVAL(CAR(values)));
4189
for (; i < count; i++)
4190
BUILTIN_ARGUMENT(UNSPEC);
4193
for (; i < count && CONSP(values); i++, values = CDR(values))
4194
BUILTIN_ARGUMENT(CAR(values));
4195
for (; i < count; i++)
4196
BUILTIN_ARGUMENT(UNSPEC);
4200
symbols = alist->optionals.symbols;
4202
for (; i < count && CONSP(values); i++, values = CDR(values)) {
4203
NORMAL_ARGUMENT(symbols[i], EVAL(CAR(values)));
4205
NORMAL_ARGUMENT(sforms[i], T);
4210
for (; i < count && CONSP(values); i++, values = CDR(values)) {
4211
NORMAL_ARGUMENT(symbols[i], CAR(values));
4213
NORMAL_ARGUMENT(sforms[i], T);
4218
/* default arguments are evaluated for macros */
4219
for (; i < count; i++) {
4220
if (!CONSTANTP(defaults[i])) {
4221
int head = lisp__data.env.head;
4222
int lex = lisp__data.env.lex;
4224
lisp__data.env.lex = base;
4225
lisp__data.env.head = lisp__data.env.length;
4226
NORMAL_ARGUMENT(symbols[i], EVAL(defaults[i]));
4227
lisp__data.env.head = head;
4228
lisp__data.env.lex = lex;
4231
NORMAL_ARGUMENT(symbols[i], defaults[i]);
4234
NORMAL_ARGUMENT(sforms[i], NIL);
4253
LispObj *val, *karg, **keys;
4255
/* Count number of remaining arguments */
4256
for (karg = values, argc = 0; CONSP(karg); karg = CDR(karg), argc++) {
4259
LispDestroy("%s: &KEY needs arguments as pairs",
4265
* Builtin functions require that the keyword be in the keyword package.
4266
* User functions don't need the arguments being pushed in the stack
4267
* in the declared order (bytecode expects it...).
4268
* XXX Error checking should be done elsewhere, code may be looping
4269
* and doing error check here may consume too much cpu time.
4270
* XXX Would also be good to already have the arguments specified in
4271
* the correct order.
4277
count = alist->keys.num_symbols;
4278
symbols = alist->keys.symbols;
4279
defaults = alist->keys.defaults;
4280
sforms = alist->keys.sforms;
4283
/* Arguments must be created in the declared order */
4286
for (; i < count; i++) {
4287
for (karg = values; CONSP(karg); karg = CDDR(karg)) {
4288
/* This is only true if both point to the
4289
* same symbol in the keyword package. */
4290
if (symbols[i] == CAR(karg)) {
4292
values = CDDR(values);
4294
BUILTIN_ARGUMENT(EVAL(CADR(karg)));
4295
goto keyword_builtin_eval_used_label;
4298
BUILTIN_ARGUMENT(UNSPEC);
4299
keyword_builtin_eval_used_label:;
4303
for (; i < count; i++) {
4304
for (karg = values; CONSP(karg); karg = CDDR(karg)) {
4305
if (symbols[i] == CAR(karg)) {
4307
values = CDDR(values);
4309
BUILTIN_ARGUMENT(CADR(karg));
4310
goto keyword_builtin_used_label;
4313
BUILTIN_ARGUMENT(UNSPEC);
4314
keyword_builtin_used_label:;
4318
if (argc != nused) {
4319
/* Argument(s) may be incorrectly specified, or specified
4320
* twice (what is not an error). */
4321
for (karg = values; CONSP(karg); karg = CDDR(karg)) {
4323
if (KEYWORDP(val)) {
4324
for (i = 0; i < count; i++)
4325
if (symbols[i] == val)
4329
/* Just make the error test true */
4333
goto invalid_keyword_label;
4340
/* The base offset of the atom in the stack, to check for
4341
* keywords specified twice. */
4343
int offset = lisp__data.env.length;
4345
keys = alist->keys.keys;
4346
for (karg = values; CONSP(karg); karg = CDDR(karg)) {
4348
if (SYMBOLP(symbol)) {
4349
/* Must be a keyword, but even if it is a keyword, may
4350
* be a typo, so assume it is correct. If it is not
4351
* in the argument list, it is an error. */
4352
for (i = 0; i < count; i++) {
4353
if (!keys[i] && symbols[i] == symbol) {
4354
LispAtom *atom = symbol->data.atom;
4356
/* Symbol found in the argument list. */
4357
if (atom->offset >= offset &&
4358
atom->offset < offset + nused &&
4359
lisp__data.env.names[atom->offset] ==
4361
/* Specified more than once... */
4362
goto keyword_duplicated_label;
4370
if (!QUOTEP(symbol) || !SYMBOLP(val = symbol->data.quote)) {
4373
goto invalid_keyword_label;
4377
for (i = 0; i < count; i++) {
4378
if (keys[i] && ATOMID(keys[i]) == id) {
4379
LispAtom *atom = val->data.atom;
4381
/* Symbol found in the argument list. */
4382
if (atom->offset >= offset &&
4383
atom->offset < offset + nused &&
4384
lisp__data.env.names[atom->offset] ==
4386
/* Specified more than once... */
4387
goto keyword_duplicated_label;
4393
/* Argument specification not found. */
4395
goto invalid_keyword_label;
4399
NORMAL_ARGUMENT(symbols[i], EVAL(CADR(karg)));
4402
NORMAL_ARGUMENT(symbols[i], CADR(karg));
4405
NORMAL_ARGUMENT(sforms[i], T);
4407
keyword_duplicated_label:;
4410
/* Add variables that were not specified in the function call. */
4411
if (nused < count) {
4414
for (i = 0; i < count; i++) {
4415
Atom_id id = ATOMID(symbols[i]);
4417
for (j = offset + nused - 1; j >= offset; j--) {
4418
if (lisp__data.env.names[j] == id)
4423
/* Argument not specified. Use default value */
4425
/* default arguments are evaluated for macros */
4426
if (!CONSTANTP(defaults[i])) {
4427
int head = lisp__data.env.head;
4428
int lex = lisp__data.env.lex;
4430
lisp__data.env.lex = base;
4431
lisp__data.env.head = lisp__data.env.length;
4432
NORMAL_ARGUMENT(symbols[i], EVAL(defaults[i]));
4433
lisp__data.env.head = head;
4434
lisp__data.env.lex = lex;
4437
NORMAL_ARGUMENT(symbols[i], defaults[i]);
4440
NORMAL_ARGUMENT(sforms[i], NIL);
4450
sforms = alist->keys.sforms;
4451
keys = alist->keys.keys;
4454
for (i = 0; i < alist->keys.num_symbols; i++) {
4458
Atom_id atom = ATOMID(keys[i]);
4460
/* Special keyword specification, need to compare ATOMID
4461
* and keyword specification must be a quoted object */
4462
for (karg = values; CONSP(karg); karg = CDR(karg)) {
4464
if (QUOTEP(val) && atom == ATOMID(val->data.quote)) {
4475
/* Normal keyword specification, can compare object pointers,
4476
* as they point to the same object in the keyword package */
4477
for (karg = values; CONSP(karg); karg = CDR(karg)) {
4478
/* Don't check if argument is a valid keyword or
4479
* special quoted keyword */
4480
if (symbols[i] == CAR(karg)) {
4490
/* Add the variable to environment */
4492
NORMAL_ARGUMENT(symbols[i], eval ? EVAL(val) : val);
4494
NORMAL_ARGUMENT(sforms[i], T);
4498
/* default arguments are evaluated for macros */
4499
if (!CONSTANTP(val)) {
4500
int head = lisp__data.env.head;
4501
int lex = lisp__data.env.lex;
4503
lisp__data.env.lex = base;
4504
lisp__data.env.head = lisp__data.env.length;
4505
NORMAL_ARGUMENT(symbols[i], EVAL(val));
4506
lisp__data.env.head = head;
4507
lisp__data.env.lex = lex;
4510
NORMAL_ARGUMENT(symbols[i], val);
4513
NORMAL_ARGUMENT(sforms[i], NIL);
4518
if (argc != nused) {
4519
/* Argument(s) may be incorrectly specified, or specified
4520
* twice (what is not an error). */
4521
for (karg = values; CONSP(karg); karg = CDDR(karg)) {
4523
if (KEYWORDP(val)) {
4524
for (i = 0; i < count; i++)
4525
if (symbols[i] == val)
4528
else if (QUOTEP(val) && SYMBOLP(val->data.quote)) {
4529
Atom_id atom = ATOMID(val->data.quote);
4531
for (i = 0; i < count; i++)
4532
if (ATOMID(keys[i]) == atom)
4536
/* Just make the error test true */
4540
goto invalid_keyword_label;
4545
goto check_aux_label;
4547
invalid_keyword_label:
4549
/* If not in argument specification list... */
4550
char function_name[36];
4552
strcpy(function_name, STROBJ(name));
4553
LispDestroy("%s: %s is an invalid keyword",
4554
function_name, STROBJ(val));
4560
/* &KEY uses all remaining arguments */
4564
goto finished_label;
4568
if (!CONSP(values)) {
4570
BUILTIN_ARGUMENT(values);
4573
NORMAL_ARGUMENT(alist->rest, values);
4577
/* always allocate a new list, don't know if it will be retained */
4581
cons = CONS(EVAL(CAR(values)), NIL);
4583
BUILTIN_ARGUMENT(cons);
4586
NORMAL_ARGUMENT(alist->rest, cons);
4588
values = CDR(values);
4589
for (; CONSP(values); values = CDR(values)) {
4590
RPLACD(cons, CONS(EVAL(CAR(values)), NIL));
4597
cons = CONS(CAR(values), NIL);
4599
BUILTIN_ARGUMENT(cons);
4602
NORMAL_ARGUMENT(alist->rest, cons);
4604
values = CDR(values);
4605
for (; CONSP(values); values = CDR(values)) {
4606
RPLACD(cons, CONS(CAR(values), NIL));
4611
goto finished_label;
4616
count = alist->auxs.num_symbols;
4617
defaults = alist->auxs.initials;
4618
symbols = alist->auxs.symbols;
4620
int lex = lisp__data.env.lex;
4622
lisp__data.env.lex = base;
4623
lisp__data.env.head = lisp__data.env.length;
4624
for (; i < count; i++) {
4625
NORMAL_ARGUMENT(symbols[i], EVAL(defaults[i]));
4626
++lisp__data.env.head;
4628
lisp__data.env.lex = lex;
4633
LispDestroy("%s: too many arguments", STROBJ(name));
4637
lisp__data.stack.base = base;
4639
lisp__data.env.head = lisp__data.env.length;
4641
#undef BULTIN_ARGUMENT
4642
#undef NORMAL_ARGUMENT
4643
#undef BUILTIN_NO_EVAL_ARGUMENT
4649
LispFuncall(LispObj *function, LispObj *arguments, int eval)
4653
LispBuiltin *builtin;
4654
LispObj *lambda, *result;
4658
if (lisp__data.debugging)
4659
LispDebugger(LispDebugCallBegin, function, arguments);
4662
switch (OBJECT_TYPE(function)) {
4663
case LispFunction_t:
4664
function = function->data.atom->object;
4666
atom = function->data.atom;
4667
if (atom->a_builtin) {
4668
builtin = atom->property->fun.builtin;
4671
eval = builtin->type != LispMacro;
4672
base = LispMakeEnvironment(atom->property->alist,
4673
arguments, function, eval, 1);
4674
if (builtin->multiple_values) {
4676
result = builtin->function(builtin);
4679
result = builtin->function(builtin);
4682
lisp__data.stack.base = lisp__data.stack.length = base;
4684
else if (atom->a_compiled) {
4685
int lex = lisp__data.env.lex;
4686
lambda = atom->property->fun.function;
4687
alist = atom->property->alist;
4689
base = LispMakeEnvironment(alist, arguments, function, eval, 0);
4690
lisp__data.env.lex = base;
4691
result = LispExecuteBytecode(lambda);
4692
lisp__data.env.lex = lex;
4693
lisp__data.env.head = lisp__data.env.length = base;
4695
else if (atom->a_function) {
4696
lambda = atom->property->fun.function;
4697
macro = lambda->funtype == LispMacro;
4698
alist = atom->property->alist;
4700
lambda = lambda->data.lambda.code;
4703
base = LispMakeEnvironment(alist, arguments, function, eval, 0);
4704
result = LispRunFunMac(function, lambda, macro, base);
4706
else if (atom->a_defstruct &&
4707
atom->property->structure.function != STRUCT_NAME) {
4710
if (atom->property->structure.function == STRUCT_CONSTRUCTOR)
4711
atom = Omake_struct->data.atom;
4712
else if (atom->property->structure.function == STRUCT_CHECK)
4713
atom = Ostruct_type->data.atom;
4715
atom = Ostruct_access->data.atom;
4716
builtin = atom->property->fun.builtin;
4718
cons.type = LispCons_t;
4719
cons.data.cons.cdr = arguments;
4723
quote.type = LispQuote_t;
4724
quote.data.quote = function;
4725
cons.data.cons.car = "e;
4726
base = LispMakeEnvironment(atom->property->alist,
4727
&cons, function, 1, 1);
4730
cons.data.cons.car = function;
4731
base = LispMakeEnvironment(atom->property->alist,
4732
&cons, function, 0, 1);
4734
result = builtin->function(builtin);
4736
lisp__data.stack.length = base;
4739
LispDestroy("EVAL: the function %s is not defined",
4746
lambda = function->data.lambda.code;
4747
alist = (LispArgList*)function->data.lambda.name->data.opaque.data;
4748
base = LispMakeEnvironment(alist, arguments, function, eval, 0);
4749
result = LispRunFunMac(function, lambda, 0, base);
4752
if (CAR(function) == Olambda) {
4753
function = EVAL(function);
4754
if (LAMBDAP(function)) {
4757
GC_PROTECT(function);
4758
lambda = function->data.lambda.code;
4759
alist = (LispArgList*)function->data.lambda.name->data.opaque.data;
4760
base = LispMakeEnvironment(alist, arguments, NIL, eval, 0);
4761
result = LispRunFunMac(NIL, lambda, 0, base);
4767
LispDestroy("EVAL: %s is invalid as a function",
4775
if (lisp__data.debugging)
4776
LispDebugger(LispDebugCallEnd, function, result);
4783
LispEval(LispObj *object)
4787
switch (OBJECT_TYPE(object)) {
4789
if ((result = LispDoGetVar(object)) == NULL)
4790
LispDestroy("EVAL: the variable %s is unbound", STROBJ(object));
4793
result = LispFuncall(CAR(object), CDR(object), 1);
4796
result = object->data.quote;
4798
case LispFunctionQuote_t:
4799
result = object->data.quote;
4800
if (SYMBOLP(result))
4801
result = LispSymbolFunction(result);
4802
else if (CONSP(result) && CAR(result) == Olambda)
4803
result = EVAL(result);
4805
LispDestroy("FUNCTION: %s is not a function", STROBJ(result));
4807
case LispBackquote_t:
4808
result = LispEvalBackquote(object->data.quote, 1);
4811
LispDestroy("EVAL: comma outside of backquote");
4821
LispApply1(LispObj *function, LispObj *argument)
4825
arguments.type = LispCons_t;
4826
arguments.data.cons.car = argument;
4827
arguments.data.cons.cdr = NIL;
4829
return (LispFuncall(function, &arguments, 0));
4833
LispApply2(LispObj *function, LispObj *argument1, LispObj *argument2)
4835
LispObj arguments, cdr;
4837
arguments.type = cdr.type = LispCons_t;
4838
arguments.data.cons.car = argument1;
4839
arguments.data.cons.cdr = &cdr;
4840
cdr.data.cons.car = argument2;
4841
cdr.data.cons.cdr = NIL;
4843
return (LispFuncall(function, &arguments, 0));
4847
LispApply3(LispObj *function, LispObj *arg1, LispObj *arg2, LispObj *arg3)
4849
LispObj arguments, car, cdr;
4851
arguments.type = car.type = cdr.type = LispCons_t;
4852
arguments.data.cons.car = arg1;
4853
arguments.data.cons.cdr = &car;
4854
car.data.cons.car = arg2;
4855
car.data.cons.cdr = &cdr;
4856
cdr.data.cons.car = arg3;
4857
cdr.data.cons.cdr = NIL;
4859
return (LispFuncall(function, &arguments, 0));
4863
LispRunFunMac(LispObj *name, LispObj *code, int macro, int base)
4865
LispObj *result = NIL;
4868
int lex = lisp__data.env.lex;
4872
block = LispBeginBlock(name, LispBlockClosure);
4873
lisp__data.env.lex = base;
4874
if (setjmp(block->jmp) == 0) {
4875
for (; CONSP(code); code = CDR(code))
4876
result = EVAL(CAR(code));
4879
LispEndBlock(block);
4881
result = lisp__data.block.block_ret;
4882
lisp__data.env.lex = lex;
4883
lisp__data.env.head = lisp__data.env.length = base;
4888
for (; CONSP(code); code = CDR(code))
4889
result = EVAL(CAR(code));
4890
/* FIXME this does not work if macro has &aux variables,
4891
* but there are several other missing features, like
4892
* destructuring and more lambda list keywords still missing.
4895
lisp__data.env.head = lisp__data.env.length = base;
4898
result = EVAL(result);
4906
LispRunSetf(LispArgList *alist, LispObj *setf, LispObj *place, LispObj *value)
4909
LispObj *store, *code, *expression, *result, quote;
4912
code = setf->data.lambda.code;
4913
store = setf->data.lambda.data;
4915
quote.type = LispQuote_t;
4916
quote.data.quote = value;
4917
LispDoAddVar(CAR(store), "e);
4918
++lisp__data.env.head;
4919
base = LispMakeEnvironment(alist, place, Oexpand_setf_method, 0, 0);
4921
/* build expansion macro */
4923
for (; CONSP(code); code = CDR(code))
4924
expression = EVAL(CAR(code));
4926
/* Minus 1 to pop the added variable */
4927
lisp__data.env.head = lisp__data.env.length = base - 1;
4929
/* protect expansion, and executes it */
4930
GC_PROTECT(expression);
4931
result = EVAL(expression);
4938
LispRunSetfMacro(LispAtom *atom, LispObj *arguments, LispObj *value)
4942
LispObj *place, *body, *result, quote;
4945
base = LispMakeEnvironment(atom->property->alist,
4946
arguments, atom->object, 0, 0);
4947
body = atom->property->fun.function->data.lambda.code;
4949
/* expand macro body */
4950
for (; CONSP(body); body = CDR(body))
4951
place = EVAL(CAR(body));
4953
/* protect expansion */
4956
/* restore environment */
4957
lisp__data.env.head = lisp__data.env.length = base;
4959
/* value is already evaluated */
4960
quote.type = LispQuote_t;
4961
quote.data.quote = value;
4963
/* call setf again */
4964
result = APPLY2(Osetf, place, "e);
4972
LispStrObj(LispObj *object)
4974
static int first = 1;
4975
static char buffer[34];
4976
static LispObj stream;
4977
static LispString string;
4980
stream.type = LispStream_t;
4981
stream.data.stream.source.string = &string;
4982
stream.data.stream.pathname = NIL;
4983
stream.data.stream.type = LispStreamString;
4984
stream.data.stream.readable = 0;
4985
stream.data.stream.writable = 1;
4987
string.string = buffer;
4989
string.space = sizeof(buffer) - 1;
4993
string.length = string.output = 0;
4995
LispWriteObject(&stream, object);
4997
/* make sure string is nul terminated */
4998
string.string[string.length] = '\0';
4999
if (string.length >= 32) {
5000
if (buffer[0] == '(')
5001
strcpy(buffer + 27, "...)");
5003
strcpy(buffer + 28, "...");
5010
LispPrint(LispObj *object, LispObj *stream, int newline)
5012
if (stream != NIL && !STREAMP(stream)) {
5013
LispDestroy("PRINT: %s is not a stream", STROBJ(stream));
5015
if (newline && LispGetColumn(stream))
5016
LispWriteChar(stream, '\n');
5017
LispWriteObject(stream, object);
5018
if (stream == NIL || (stream->data.stream.type == LispStreamStandard &&
5019
stream->data.stream.source.file == Stdout))
5024
LispUpdateResults(LispObj *cod, LispObj *res)
5026
LispSetVar(RUN[2], LispGetVar(RUN[1]));
5027
LispSetVar(RUN[1], LispGetVar(RUN[0]));
5028
LispSetVar(RUN[0], cod);
5030
LispSetVar(RES[2], LispGetVar(RES[1]));
5031
LispSetVar(RES[1], LispGetVar(RES[0]));
5032
LispSetVar(RES[0], res);
5035
#ifdef SIGNALRETURNSINT
5040
LispSignalHandler(int signum)
5043
#ifdef SIGNALRETURNSINT
5049
LispSignal(int signum)
5054
if (lisp__disable_int) {
5055
lisp__interrupted = signum;
5060
errstr = "interrupted";
5063
errstr = "floating point exception";
5066
sprintf(buffer, "signal %d received", signum);
5070
LispDestroy(errstr);
5074
LispDisableInterrupts(void)
5076
++lisp__disable_int;
5080
LispEnableInterrupts(void)
5082
--lisp__disable_int;
5083
if (lisp__disable_int <= 0 && lisp__interrupted)
5084
LispSignal(lisp__interrupted);
5092
lisp__data.sigint = signal(SIGINT, LispSignalHandler);
5093
lisp__data.sigfpe = signal(SIGFPE, LispSignalHandler);
5097
if (sigsetjmp(lisp__data.jmp, 1) == 0) {
5098
lisp__data.running = 1;
5099
if (lisp__data.interactive && lisp__data.prompt) {
5100
LispFputs(Stdout, lisp__data.prompt);
5103
if ((cod = LispRead()) != NULL) {
5105
if (lisp__data.interactive) {
5106
if (RETURN_COUNT >= 0)
5107
LispPrint(obj, NIL, 1);
5108
if (RETURN_COUNT > 0) {
5111
for (i = 0; i < RETURN_COUNT; i++)
5112
LispPrint(RETURN(i), NIL, 1);
5114
LispUpdateResults(cod, obj);
5115
if (LispGetColumn(NIL))
5116
LispWriteChar(NIL, '\n');
5125
signal(SIGINT, lisp__data.sigint);
5126
signal(SIGFPE, lisp__data.sigfpe);
5128
lisp__data.running = 0;
5132
LispExecute(char *str)
5134
static LispObj stream;
5135
static LispString string;
5136
static int first = 1;
5138
int running = lisp__data.running;
5139
LispObj *result, *cod, *obj, **presult = &result;
5141
if (str == NULL || *str == '\0')
5147
stream.type = LispStream_t;
5148
stream.data.stream.source.string = &string;
5149
stream.data.stream.pathname = NIL;
5150
stream.data.stream.type = LispStreamString;
5151
stream.data.stream.readable = 1;
5152
stream.data.stream.writable = 0;
5156
string.string = str;
5157
string.length = strlen(str);
5160
LispPushInput(&stream);
5162
lisp__data.running = 1;
5163
if (sigsetjmp(lisp__data.jmp, 1) != 0)
5170
if ((obj = LispRead()) != NULL) {
5177
LispPopInput(&stream);
5179
lisp__data.running = running;
5190
LispObj *object, *path, *ext;
5192
pagesize = LispGetPageSize();
5193
segsize = pagesize / sizeof(LispObj);
5195
/* Initialize memory management */
5196
lisp__data.mem.mem = (void**)calloc(lisp__data.mem.space = 16,
5198
lisp__data.mem.index = lisp__data.mem.level = 0;
5200
/* Allow LispGetVar to check ATOMID() of unbound symbols */
5201
UNBOUND->data.atom = (LispAtom*)LispCalloc(1, sizeof(LispAtom));
5202
LispMused(UNBOUND->data.atom);
5203
noproperty.value = UNBOUND;
5206
Stdin = LispFdopen(0, FILE_READ);
5208
Stdout = LispFdopen(1, FILE_WRITE | FILE_BUFFERED);
5210
Stderr = LispFdopen(2, FILE_WRITE);
5212
/* minimum number of free cells after GC
5213
* if sizeof(LispObj) == 16, than a minfree of 1024 would try to keep
5214
* at least 16Kb of free cells.
5218
MOD = COD = PRO = NIL;
5223
/* allocate initial object cells */
5224
LispAllocSeg(&objseg, minfree);
5225
LispAllocSeg(&atomseg, pagesize);
5226
lisp__data.gc.average = segsize;
5228
/* Don't allow gc in initialization */
5231
/* Initialize package system, the current package is LISP. Order of
5232
* initialization is very important here */
5233
lisp__data.lisp = LispNewPackage(STRING("LISP"),
5234
CONS(STRING("COMMON-LISP"), NIL));
5236
/* Make LISP package the current one */
5237
lisp__data.pack = lisp__data.savepack =
5238
lisp__data.lisp->data.package.package;
5240
/* Allocate space in LISP package */
5241
LispMoreGlobals(lisp__data.pack);
5243
/* Allocate space for multiple value return values */
5244
lisp__data.returns.values = malloc(MULTIPLE_VALUES_LIMIT *
5245
(sizeof(LispObj*)));
5247
/* Create the first atom, do it "by hand" because macro "PACKAGE"
5248
* cannot yet be used. */
5249
atom = LispGetPermAtom("*PACKAGE*");
5250
lisp__data.package = atomseg.freeobj;
5251
atomseg.freeobj = CDR(atomseg.freeobj);
5253
lisp__data.package->type = LispAtom_t;
5254
lisp__data.package->data.atom = atom;
5255
atom->object = lisp__data.package;
5256
atom->package = lisp__data.lisp;
5258
/* Set package list, to be used by (gc) and (list-all-packages) */
5259
PACK = CONS(lisp__data.lisp, NIL);
5261
/* Make *PACKAGE* a special variable */
5262
LispProclaimSpecial(lisp__data.package, lisp__data.lisp, NIL);
5264
/* Value of macro "PACKAGE" is now properly available */
5266
/* Changing *PACKAGE* is like calling (in-package) */
5267
lisp__data.package->data.atom->watch = 1;
5269
/* And available to other packages */
5270
LispExportSymbol(lisp__data.package);
5272
/* Initialize stacks */
5273
LispMoreEnvironment();
5276
/* Create the KEYWORD package */
5277
Skeyword = GETATOMID("KEYWORD");
5278
object = LispNewPackage(STRING(Skeyword),
5279
CONS(STRING(""), NIL));
5281
/* Update list of packages */
5282
PACK = CONS(object, PACK);
5284
/* Allow easy access to the keyword package */
5285
lisp__data.keyword = object;
5286
lisp__data.key = object->data.package.package;
5288
/* Initialize some static important symbols */
5289
Olambda = STATIC_ATOM("LAMBDA");
5290
LispExportSymbol(Olambda);
5291
Okey = STATIC_ATOM("&KEY");
5292
LispExportSymbol(Okey);
5293
Orest = STATIC_ATOM("&REST");
5294
LispExportSymbol(Orest);
5295
Ooptional = STATIC_ATOM("&OPTIONAL");
5296
LispExportSymbol(Ooptional);
5297
Oaux = STATIC_ATOM("&AUX");
5298
LispExportSymbol(Oaux);
5299
Kunspecific = KEYWORD("UNSPECIFIC");
5300
Oformat = STATIC_ATOM("FORMAT");
5301
Oexpand_setf_method = STATIC_ATOM("EXPAND-SETF-METHOD");
5303
Omake_struct = STATIC_ATOM("MAKE-STRUCT");
5304
Ostruct_access = STATIC_ATOM("STRUCT-ACCESS");
5305
Ostruct_store = STATIC_ATOM("STRUCT-STORE");
5306
Ostruct_type = STATIC_ATOM("STRUCT-TYPE");
5307
Smake_struct = ATOMID(Omake_struct);
5308
Sstruct_access = ATOMID(Ostruct_access);
5309
Sstruct_store = ATOMID(Ostruct_store);
5310
Sstruct_type = ATOMID(Ostruct_type);
5312
/* Initialize some static atom ids */
5313
Snil = GETATOMID("NIL");
5314
St = GETATOMID("T");
5315
Saux = ATOMID(Oaux);
5316
Skey = ATOMID(Okey);
5317
Soptional = ATOMID(Ooptional);
5318
Srest = ATOMID(Orest);
5319
Sand = GETATOMID("AND");
5320
Sor = GETATOMID("OR");
5321
Snot = GETATOMID("NOT");
5322
Satom = GETATOMID("ATOM");
5323
Ssymbol = GETATOMID("SYMBOL");
5324
Sinteger = GETATOMID("INTEGER");
5325
Scharacter = GETATOMID("CHARACTER");
5326
Sstring = GETATOMID("STRING");
5327
Slist = GETATOMID("LIST");
5328
Scons = GETATOMID("CONS");
5329
Svector = GETATOMID("VECTOR");
5330
Sarray = GETATOMID("ARRAY");
5331
Sstruct = GETATOMID("STRUCT");
5332
Sfunction = GETATOMID("FUNCTION");
5333
Spathname = GETATOMID("PATHNAME");
5334
Srational = GETATOMID("RATIONAL");
5335
Sfloat = GETATOMID("FLOAT");
5336
Scomplex = GETATOMID("COMPLEX");
5337
Sopaque = GETATOMID("OPAQUE");
5338
Sdefault = GETATOMID("DEFAULT");
5340
LispArgList_t = LispRegisterOpaqueType("LispArgList*");
5342
lisp__data.unget = malloc(sizeof(LispUngetInfo*));
5343
lisp__data.unget[0] = calloc(1, sizeof(LispUngetInfo));
5344
lisp__data.nunget = 1;
5346
lisp__data.standard_input = ATOM2("*STANDARD-INPUT*");
5347
SINPUT = STANDARDSTREAM(Stdin, lisp__data.standard_input, STREAM_READ);
5348
lisp__data.interactive = 1;
5349
LispProclaimSpecial(lisp__data.standard_input,
5350
lisp__data.input_list = SINPUT, NIL);
5351
LispExportSymbol(lisp__data.standard_input);
5353
lisp__data.standard_output = ATOM2("*STANDARD-OUTPUT*");
5354
SOUTPUT = STANDARDSTREAM(Stdout, lisp__data.standard_output, STREAM_WRITE);
5355
LispProclaimSpecial(lisp__data.standard_output,
5356
lisp__data.output_list = SOUTPUT, NIL);
5357
LispExportSymbol(lisp__data.standard_output);
5359
object = ATOM2("*STANDARD-ERROR*");
5360
lisp__data.error_stream = STANDARDSTREAM(Stderr, object, STREAM_WRITE);
5361
LispProclaimSpecial(object, lisp__data.error_stream, NIL);
5362
LispExportSymbol(object);
5364
lisp__data.modules = ATOM2("*MODULES*");
5365
LispProclaimSpecial(lisp__data.modules, MOD, NIL);
5366
LispExportSymbol(lisp__data.modules);
5368
object = CONS(KEYWORD("UNIX"), CONS(KEYWORD("XEDIT"), NIL));
5369
lisp__data.features = ATOM2("*FEATURES*");
5370
LispProclaimSpecial(lisp__data.features, object, NIL);
5371
LispExportSymbol(lisp__data.features);
5373
object = ATOM2("MULTIPLE-VALUES-LIMIT");
5374
LispDefconstant(object, FIXNUM(MULTIPLE_VALUES_LIMIT + 1), NIL);
5375
LispExportSymbol(object);
5389
lisp__data.prompt = isatty(0) ? "> " : NULL;
5391
lisp__data.errexit = !lisp__data.interactive;
5393
if (lisp__data.interactive) {
5394
/* add +, ++, +++, *, **, and *** */
5395
for (i = 0; i < 3; i++) {
5397
results[i + 1] = '\0';
5398
RUN[i] = ATOM(results);
5399
LispSetVar(RUN[i], NIL);
5400
LispExportSymbol(RUN[i]);
5402
for (i = 0; i < 3; i++) {
5404
results[i + 1] = '\0';
5405
RES[i] = ATOM(results);
5406
LispSetVar(RES[i], NIL);
5407
LispExportSymbol(RES[i]);
5411
RUN[0] = RUN[1] = RUN[2] = RES[0] = RES[1] = RES[2] = NIL;
5413
/* Add LISP builtin functions */
5414
for (i = 0; i < sizeof(lispbuiltins) / sizeof(lispbuiltins[0]); i++)
5415
LispAddBuiltinFunction(&lispbuiltins[i]);
5417
EXECUTE("(require \"lisp\")");
5419
object = ATOM2("*DEFAULT-PATHNAME-DEFAULTS*");
5423
char *pathname = LISPDIR;
5425
length = strlen(pathname);
5426
if (length && pathname[length - 1] != '/') {
5427
pathname = LispMalloc(length + 2);
5429
strcpy(pathname, LISPDIR);
5430
strcpy(pathname + length, "/");
5431
path = LSTRING2(pathname, length + 1);
5434
path = LSTRING(pathname, length);
5440
LispProclaimSpecial(object, APPLY1(Oparse_namestring, path), NIL);
5441
LispExportSymbol(object);
5444
/* Create and make EXT the current package */
5445
PACKAGE = ext = LispNewPackage(STRING("EXT"), NIL);
5446
lisp__data.pack = lisp__data.savepack = PACKAGE->data.package.package;
5448
/* Update list of packages */
5449
PACK = CONS(ext, PACK);
5451
/* Import LISP external symbols in EXT package */
5452
LispUsePackage(lisp__data.lisp);
5454
/* Add EXT non standard builtin functions */
5455
for (i = 0; i < sizeof(extbuiltins) / sizeof(extbuiltins[0]); i++)
5456
LispAddBuiltinFunction(&extbuiltins[i]);
5458
/* Create and make USER the current package */
5460
PACKAGE = LispNewPackage(STRING("USER"),
5461
CONS(STRING("COMMON-LISP-USER"), NIL));
5463
lisp__data.pack = lisp__data.savepack = PACKAGE->data.package.package;
5465
/* Update list of packages */
5466
PACK = CONS(PACKAGE, PACK);
5468
/* USER package inherits all LISP external symbols */
5469
LispUsePackage(lisp__data.lisp);
5470
/* And all EXT external symbols */
5471
LispUsePackage(ext);
5479
/* XXX needs to free all used memory, not just close file descriptors */
5483
LispSetPrompt(char *prompt)
5485
lisp__data.prompt = prompt;
5489
LispSetInteractive(int interactive)
5491
lisp__data.interactive = !!interactive;
5495
LispSetExitOnError(int errexit)
5497
lisp__data.errexit = !!errexit;
5501
LispDebug(int enable)
5503
lisp__data.debugging = !!enable;
5506
/* assumes we are at the toplevel */
5508
lisp__data.debug_level = -1;
5509
lisp__data.debug_step = 0;