summaryrefslogtreecommitdiff
path: root/lisp/struct.c
blob: 45b7e70f0925365ae29a31620e92f7cec5008660 (plain)
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
/*
 * Copyright (c) 2001 by The XFree86 Project, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *  
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Except as contained in this notice, the name of the XFree86 Project shall
 * not be used in advertising or otherwise to promote the sale, use or other
 * dealings in this Software without prior written authorization from the
 * XFree86 Project.
 *
 * Author: Paulo César Pereira de Andrade
 */

/* $XFree86: xc/programs/xedit/lisp/struct.c,v 1.23 2003/04/27 18:17:34 tsi Exp $ */

#include "lisp/struct.h"

/*
 * Prototypes
 */
static LispObj *LispStructAccessOrStore(LispBuiltin*, int);

/*
 * Initialization
 */
LispObj *Omake_struct, *Ostruct_access, *Ostruct_store, *Ostruct_type;

Atom_id Smake_struct, Sstruct_access, Sstruct_store, Sstruct_type;

/*
 * Implementation
 */
LispObj *
Lisp_Defstruct(LispBuiltin *builtin)
/*
 defstruct name &rest description
 */
{
    int intern;
    LispAtom *atom;
    int i, size, length, slength;
    char *name, *strname, *sname;
    LispObj *list, *cons, *object, *definition, *documentation;

    LispObj *oname, *description;

    description = ARGUMENT(1);
    oname = ARGUMENT(0);

    CHECK_SYMBOL(oname);

    strname = ATOMID(oname);
    length = strlen(strname);

	    /* MAKE- */
    size = length + 6;
    name = LispMalloc(size);

    sprintf(name, "MAKE-%s", strname);
    atom = (object = ATOM(name))->data.atom;

    if (atom->a_builtin)
	LispDestroy("%s: %s cannot be a structure name",
		    STRFUN(builtin), STROBJ(oname));

    intern = !atom->ext;

    if (CONSP(description) && STRINGP(CAR(description))) {
	documentation = CAR(description);
	description = CDR(description);
    }
    else
	documentation = NIL;

    /* get structure fields and default values */
    for (list = description; CONSP(list); list = CDR(list)) {
	object = CAR(list);

	cons = list;
	if (CONSP(object)) {
	    if ((CONSP(CDR(object)) && CDR(CDR(object)) != NIL) ||
		(!CONSP(CDR(object)) && CDR(object) != NIL))
	    LispDestroy("%s: bad initialization %s",
			STRFUN(builtin), STROBJ(object));
	    cons = object;
	    object = CAR(object);
	}
	if (!SYMBOLP(object) || strcmp(ATOMID(object), "P") == 0)
	    /* p is invalid as a field name due to `type'-p */
	    LispDestroy("%s: %s cannot be a field for %s",
			STRFUN(builtin), STROBJ(object), ATOMID(oname));

	if (!KEYWORDP(object))
	    CAR(cons) = KEYWORD(ATOMID(object));

	/* check for repeated field names */
	for (object = description; object != list; object = CDR(object)) {
	    LispObj *left = CAR(object), *right = CAR(list);

	    if (CONSP(left))
		left = CAR(left);
	    if (CONSP(right))
		right = CAR(right);

	    if (ATOMID(left) == ATOMID(right))
		LispDestroy("%s: only one slot named %s allowed",
			    STRFUN(builtin), STROBJ(left));
	}
    }

    /* atom should not have been modified */
    definition = CONS(oname, description);
    LispSetAtomStructProperty(atom, definition, STRUCT_CONSTRUCTOR);
    if (!intern)
	LispExportSymbol(object);

    atom = oname->data.atom;
    if (atom->a_defstruct)
	LispWarning("%s: structure %s is being redefined",
		    STRFUN(builtin), strname);
    LispSetAtomStructProperty(atom, definition, STRUCT_NAME);

    sprintf(name, "%s-P", strname);
    atom = (object = ATOM(name))->data.atom;
    LispSetAtomStructProperty(atom, definition, STRUCT_CHECK);
    if (!intern)
	LispExportSymbol(object);

    for (i = 0, list = description; CONSP(list); i++, list = CDR(list)) {
	if (CONSP(CAR(list)))
	    sname = ATOMID(CAR(CAR(list)));
	else
	    sname = ATOMID(CAR(list));
	slength = strlen(sname);
	if (length + slength + 2 > size) {
	    size = length + slength + 2;
	    name = LispRealloc(name, size);
	}
	sprintf(name, "%s-%s", strname, sname);
	atom = (object = ATOM(name))->data.atom;
	LispSetAtomStructProperty(atom, definition, i);
	if (!intern)
	    LispExportSymbol(object);
    }

    LispFree(name);

    if (documentation != NIL)
	LispAddDocumentation(oname, documentation, LispDocStructure);

    return (oname);
}

/* helper functions
 *	DONT explicitly call them. Non standard functions.
 */
LispObj *
Lisp_XeditMakeStruct(LispBuiltin *builtin)
/*
 lisp::make-struct atom &rest init
 */
{
    int nfld, ncvt, length = lisp__data.protect.length;
    LispAtom *atom = NULL;

    LispObj *definition, *object, *field, *fields, *value = NIL, *cons, *list;
    LispObj *struc, *init;

    init = ARGUMENT(1);
    struc = ARGUMENT(0);

    field = cons = NIL;
    if (!POINTERP(struc) ||
	!(XSYMBOLP(struc) || XFUNCTIONP(struc)) ||
	(atom = struc->data.atom)->a_defstruct == 0 ||
	 atom->property->structure.function != STRUCT_CONSTRUCTOR)
	LispDestroy("%s: invalid constructor %s",
		    STRFUN(builtin), STROBJ(struc));
    definition = atom->property->structure.definition;

    ncvt = nfld = 0;
    fields = NIL;

    /* check for errors in argument list */
    for (list = init, nfld = 0; CONSP(list); list = CDR(list)) {
	CHECK_KEYWORD(CAR(list));
	if (!CONSP(CDR(list)))
	    LispDestroy("%s: values must be provided as pairs",
			ATOMID(struc));
	nfld++;
	list = CDR(list);
    }

    /* create structure, CAR(definition) is structure name */
    for (list = CDR(definition); CONSP(list); list = CDR(list)) {
	Atom_id id;
	LispObj *defvalue = NIL;

	++nfld;
	field = CAR(list);
	if (CONSP(field)) {
	    /* if default value provided */
	    if (CONSP(CDR(field)))
		defvalue = CAR(CDR(field));
	    field = CAR(field);
	}
	id = ATOMID(field);

	for (object = init; CONSP(object); object = CDR(object)) {
	    /* field is a keyword, test above checked it */
	    field = CAR(object);
	    if (id == ATOMID(field)) {
		/* value provided */
		value = CAR(CDR(object));
		ncvt++;
		break;
	    }
	    object = CDR(object);
	}

	/* if no initialization given */
	if (!CONSP(object)) {
	    /* if default value in structure definition */
	    if (defvalue != NIL)
		value = EVAL(defvalue);
	    else
		value = NIL;
	}

	if (fields == NIL) {
	    fields = cons = CONS(value, NIL);
	    if (length + 1 >= lisp__data.protect.space)
		LispMoreProtects();
	    lisp__data.protect.objects[lisp__data.protect.length++] = fields;
	}
	else {
	    RPLACD(cons, CONS(value, NIL));
	    cons = CDR(cons);
	}
    }

    /* if not enough arguments were converted, need to check because
     * it is acceptable to set a field more than once, but in that case,
     * only the first value will be used. */
    if (nfld > ncvt) {
	for (list = init; CONSP(list); list = CDR(list)) {
	    Atom_id id = ATOMID(CAR(list));

	    for (object = CDR(definition); CONSP(object);
		 object = CDR(object)) {
		field = CAR(object);
		if (CONSP(field))
		    field = CAR(field);
		if (ATOMID(field) == id)
		    break;
	    }
	    if (!CONSP(object))
		LispDestroy("%s: %s is not a field for %s",
			    ATOMID(struc), STROBJ(CAR(list)),
			    ATOMID(CAR(definition)));
	    list = CDR(list);
	}
    }

    lisp__data.protect.length = length;

    return (STRUCT(fields, definition));
}

static LispObj *
LispStructAccessOrStore(LispBuiltin *builtin, int store)
/*
 lisp::struct-access atom struct
 lisp::struct-store atom struct value
 */
{
    long offset;
    LispAtom *atom;
    LispObj *definition, *list;

    LispObj *name, *struc, *value = NIL;

    if (store)
	value = ARGUMENT(2);
    struc = ARGUMENT(1);
    name = ARGUMENT(0);

    if (!POINTERP(name) ||
	!(XSYMBOLP(name) || XFUNCTIONP(name)) ||
	(atom = name->data.atom)->a_defstruct == 0 ||
	(offset = atom->property->structure.function) < 0) {
	LispDestroy("%s: invalid argument %s",
		    STRFUN(builtin), STROBJ(name));
	/*NOTREACHED*/
	offset = 0;
	atom = NULL;
    }
    definition = atom->property->structure.definition;

    /* check if the object is of the required type */
    if (!STRUCTP(struc) || struc->data.struc.def != definition)
	LispDestroy("%s: %s is not a %s",
		    ATOMID(name), STROBJ(struc), ATOMID(CAR(definition)));

    for (list = struc->data.struc.fields; offset; list = CDR(list), offset--)
	;

    return (store ? RPLACA(list, value) : CAR(list));
}

LispObj *
Lisp_XeditStructAccess(LispBuiltin *builtin)
/*
 lisp::struct-access atom struct
 */
{
    return (LispStructAccessOrStore(builtin, 0));
}

LispObj *
Lisp_XeditStructStore(LispBuiltin *builtin)
/*
 lisp::struct-store atom struct value
 */
{
    return (LispStructAccessOrStore(builtin, 1));
}

LispObj *
Lisp_XeditStructType(LispBuiltin *builtin)
/*
 lisp::struct-type atom struct
 */
{
    LispAtom *atom = NULL;

    LispObj *definition, *struc, *name;

    struc = ARGUMENT(1);
    name = ARGUMENT(0);

    if (!POINTERP(name) ||
	!(XSYMBOLP(name) || XFUNCTIONP(name)) ||
	(atom = name->data.atom)->a_defstruct == 0 ||
	(atom->property->structure.function != STRUCT_CHECK))
	LispDestroy("%s: invalid argument %s",
		    STRFUN(builtin), STROBJ(name));
    definition = atom->property->structure.definition;

    /* check if the object is of the required type */
    if (STRUCTP(struc) && struc->data.struc.def == definition)
	return (T);

    return (NIL);
}