File: list.d

package info (click to toggle)
gcl27 2.7.1-12
  • links: PTS
  • area: main
  • in suites: forky
  • size: 30,844 kB
  • sloc: lisp: 211,942; ansic: 52,944; sh: 9,347; makefile: 647; tcl: 53; awk: 52
file content (372 lines) | stat: -rw-r--r-- 7,023 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* -*-C-*- */
/*
 Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa
 Copyright (C) 2024 Camm Maguire

This file is part of GNU Common Lisp, herein referred to as GCL

GCL is free software; you can redistribute it and/or modify it under
the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GCL is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public 
License for more details.

You should have received a copy of the GNU Library General Public License 
along with GCL; see the file COPYING.  If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
	list.d

	list manipulating routines
*/

#include "include.h"
#include "num_include.h"
#include "page.h"


object
car(object x) {

	if (x == Cnil)
		return(x);
	if (consp(x))
		return(x->c.c_car);
	FEwrong_type_argument(sLlist, x);
	return(Cnil);
}

object
cdr(object x) {

	if (x == Cnil)
		return(x);
	if (consp(x))
		return(x->c.c_cdr);
	FEwrong_type_argument(sLlist, x);
	return(Cnil);
}


void
stack_cons(void)
{
	object d=vs_pop,a=vs_pop;
	*vs_top++ = make_cons(a,d);
}

object on_stack_list_vector_new(fixnum n,object first,va_list ap)
{object res=(object) alloca_val;
 struct cons *p;
 object x;
 int jj=0;
 p=(struct cons *) res;
 if (n<=0) return Cnil;
 TOP:
#ifdef WIDE_CONS
 set_type_of(p,t_cons);
#endif
 p->c_car= jj||first==OBJNULL ? va_arg(ap,object) : first;
 jj=1;
 if (--n == 0)
   {p->c_cdr = Cnil;
    return res;}
 else
   { x= (object) p;
     x->c.c_cdr= (object) ( ++p);}
 goto TOP;
}

object on_stack_list(fixnum n,...) {
  object x,first;
  va_list ap;
  va_start(ap,n);
  first=va_arg(ap,object);
  x=on_stack_list_vector_new(n,first,ap);
  va_end(ap);
  return x;
}


object
list_vector_new(int n,object first,va_list ap) {

  object ans,*p;

  for (p=&ans;n-->0;first=OBJNULL)
    collect(p,make_cons(first==OBJNULL ? va_arg(ap,object) : first,Cnil));
  *p=Cnil;
 return ans;

}
   
#ifdef WIDE_CONS
#define maybe_set_type_of(a,b) set_type_of(a,b)
#else
#define maybe_set_type_of(a,b)
#endif

#define multi_cons(n_,next_,last_)					\
  ({_tm->tm_nfree -= n_;						\
    for(_x=_tm->tm_free,_p=&_x;n_-->0;_p=&(*_p)->c.c_cdr) {		\
      object _z=*_p;							\
      pageinfo(_z)->in_use++;						\
      maybe_set_type_of(_z,t_cons);					\
      _z->c.c_cdr=OBJ_LINK(_z);						\
      _z->c.c_car=next_;						\
    }									\
    _tm->tm_free=*_p;							\
    *_p=SAFE_CDR(last_);						\
    _x;})

#define n_cons(n_,next_,last_)						\
  ({fixnum _n=n_;object _x=Cnil,*_p;					\
    static struct typemanager *_tm=tm_table+t_cons;			\
    if (_n>=0) {/*FIXME vs_top<vs_base*/				\
      BEGIN_NO_INTERRUPT;						\
      if (_n<=_tm->tm_nfree && !stack_alloc_start)			\
	_x=multi_cons(_n,next_,last_);					\
      else {								\
	for (_p=&_x;_n--;)						\
	  collect(_p,make_cons(next_,Cnil));				\
	*_p=SAFE_CDR(last_);						\
      }									\
      END_NO_INTERRUPT;							\
    }									\
    _x;})

object
n_cons_from_x(fixnum n,object x) {

  return n_cons(n,({object _z=x->c.c_car;x=x->c.c_cdr;_z;}),Cnil);

}


object
listqA(int a,int n,va_list ap) {

  return n_cons(n,va_arg(ap,object),a ? va_arg(ap,object) : Cnil);

}

object list(fixnum n,...) { 

  va_list ap;
  object lis;

  va_start(ap,n);
  lis=listqA(0,n,ap);
  va_end(ap);
  return lis;

}

object listA(fixnum n,...) { 

  va_list ap;
  object lis;

  va_start(ap,n);
  lis=listqA(1,n-1,ap);
  va_end(ap);
  return lis;

}



object
append(object x, object y) {

  return n_cons(length(x),({object _t=x->c.c_car;x=x->c.c_cdr;_t;}),y);

}

object
copy_list(object x) {

	object y;

	if (!consp(x))
		return(x);
	y = make_cons(x->c.c_car, Cnil);
	vs_push(y);
	for (x = x->c.c_cdr; consp(x); x = x->c.c_cdr) {
		y->c.c_cdr = make_cons(x->c.c_car, Cnil);
		y = y->c.c_cdr;
	}
	y->c.c_cdr = x;
	return(vs_pop);
}


DEFUN("CONS",object,fLcons,LISP,2,2,NONE,OO,OO,OO,OO,(object a,object d),"") {

  object x=alloc_object(t_cons);
  x->c.c_car=a;
  x->c.c_cdr=d;
  RETURN1(x);

}

object
make_list(fixnum n) {
  object x =Cnil ;
  while (n-- > 0)
    x = make_cons(Cnil, x);
  return x;
}

static fixnum
list_count(fixnum nargs,object first,object l,va_list ap) {
  fixnum n;
  for (n=0;NEXT_ARG(nargs,ap,l,first,OBJNULL)!=OBJNULL;n++);
  return n;
}

DEFUN("LIST",object,fLlist,LISP,0,MAX_ARGS,NONE,OO,OO,OO,OO,(object first,...),"") {

  object x,l=Cnil;
  va_list ap;
  fixnum nargs=INIT_NARGS(0),n;

  va_start(ap,first);
  n=list_count(nargs,first,l,ap);
  va_end(ap);
  va_start(ap,first);
  x=n_cons(n,NEXT_ARG(nargs,ap,l,first,Cnil),NEXT_ARG(nargs,ap,l,first,Cnil));
  va_end(ap);
  RETURN1(x);

}

DEFUN("LIST*",object,fLlistA,LISP,1,MAX_ARGS,NONE,OO,OO,OO,OO,(object first,...),"") {

  object x,l=Cnil;
  va_list ap;
  fixnum nargs=INIT_NARGS(0),n;

  va_start(ap,first);
  n=list_count(nargs,first,l,ap);
  va_end(ap);
  va_start(ap,first);
  x=n_cons(n-1,NEXT_ARG(nargs,ap,l,first,Cnil),NEXT_ARG(nargs,ap,l,first,Cnil));
  va_end(ap);
  RETURN1(x);

}

void
stack_list(void) {

  object *a;

  a=vs_base;
  vs_base[0]=n_cons(vs_top-vs_base,*a++,Cnil);
  vs_top=vs_base+1;

}
 
object on_stack_make_list(int n) {

  object res=(object) alloca_val;
  struct cons *p = (struct cons *)res;
 if (n<=0) return Cnil;
  TOP:
#ifdef WIDE_CONS
 set_type_of(p,t_cons);
#endif
 p->c_car=Cnil;
 if (--n == 0)
   {p->c_cdr = Cnil;
    return res;}
 else
   {object  x= (object) p;
     x->c.c_cdr= (object) ( ++p);}
 goto TOP;
}


DEFUN("RPLACA",object,fLrplaca,LISP,2,2,NONE,OO,OO,OO,OO,(object o,object c),"") {

  check_type_cons(&o);
  o->c.c_car = c;
  RETURN1(o);

}

DEFUN("RPLACD",object,fLrplacd,LISP,2,2,NONE,OO,OO,OO,OO,(object o,object d),"") {

  check_type_cons(&o);
  o->c.c_cdr = d;
  RETURN1(o);

}

 
void
check_proper_list(object alist) {

    object v;
    /*
    if (alist == Cnil)
	 FEwrong_type_argument(sLlist, alist);
    */
    for (v=alist ; consp(v) ; v=v->c.c_cdr);
    if (v != Cnil)
      TYPE_ERROR(alist,siLproper_list);
}


DEFUN("PROPER-LISTP",object,fSproper_listp,SI,1,1,NONE,OO,OO,OO,OO,(object x),"") { 
  check_proper_list(x);
  RETURN1(Ct);
}


bool
member_eq(object x,object l) {

	for (;  consp(l);  l = l->c.c_cdr)
		if (x == l->c.c_car)
			return(TRUE);
	return(FALSE);
}

void
delete_eq(object x,object *lp) {

	for (;  consp(*lp);  lp = &(*lp)->c.c_cdr)
		if ((*lp)->c.c_car == x) {
			*lp = (*lp)->c.c_cdr;
			return;
		}
}

DEFUN("STATIC-INVERSE-CONS",object,fSstatic_inverse_cons,SI,1,1,NONE,OI,OO,OO,OO,(fixnum x),"") {

   object y=(object)x;

   return is_imm_fixnum(y) ? Cnil : (is_imm_fixnum(y->c.c_cdr) ? y : (y->d.f||y->d.e ? Cnil : y));

}

void
gcl_init_list_function()
{

	sKtest = make_keyword("TEST");
	sKtest_not = make_keyword("TEST-NOT");
	sKkey = make_keyword("KEY");

	sKinitial_element = make_keyword("INITIAL-ELEMENT");


}