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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
/* xlstruct.c - the defstruct facility */
/* Copyright (c) 1989, by David Michael Betz. */
/* You may give out copies of this software; for conditions see the file */
/* COPYING included with this distribution. */
#include "xlisp.h"
/* forward declarations */
LOCAL VOID addslot P5H(LVAL, LVAL, int, LVAL *, LVAL *);
LOCAL VOID updateslot P3H(LVAL, LVAL, LVAL);
/* local variables */
static char prefix[STRMAX+1];
static char makestr[] = "MAKE-%s";
/* xmkstruct - the '%make-struct' function */
LVAL xmkstruct(V)
{
LVAL type,val;
int i;
/* get the structure type */
type = xlgasymbol();
/* make the structure */
val = newstruct(type,xlargc);
/* store each argument */
for (i = 1; moreargs(); ++i)
setelement(val,i,nextarg());
xllastarg();
/* return the structure */
return (val);
}
/* xcpystruct - the '%copy-struct' function */
LVAL xcpystruct(V)
{
LVAL str,val;
int size,i;
str = xlgastruct();
xllastarg();
size = getsize(str);
val = newstruct(getelement(str,0),size-1);
for (i = 1; i < size; ++i)
setelement(val,i,getelement(str,i));
return (val);
}
/* xstrref - the '%struct-ref' function */
LVAL xstrref(V)
{
LVAL str,val;
int i;
str = xlgastruct();
val = xlgafixnum(); i = (int)getfixnum(val);
xllastarg();
if (i >= getsize(str)) /* wrong structure TAA MOD fix*/
xlerror("bad structure reference",str);
return (getelement(str,i));
}
/* xstrset - the '%struct-set' function */
LVAL xstrset(V)
{
LVAL str,val;
int i;
str = xlgastruct();
val = xlgafixnum(); i = (int)getfixnum(val);
val = xlgetarg();
xllastarg();
if (i >= getsize(str)) /* wrong structure TAA MOD fix*/
xlerror("bad structure reference",str);
setelement(str,i,val);
return (val);
}
/* xstrtypep - the '%struct-type-p' function */
LVAL xstrtypep(V)
{
LVAL type,val;
type = xlgasymbol();
val = xlgetarg();
xllastarg();
if (structp(val)) {
for (val = getelement(val,0);
! null(val);
val = xlgetprop(val,s_strinclude))
if (val == type) return(s_true);
return(NIL);
}
else return(NIL);
}
/* xdefstruct - the 'defstruct' special form */
LVAL xdefstruct(V)
{
LVAL structname,slotname,defexpr,sym,tmp,args,body;
LVAL options,oargs,slots,constrsym,predsym;
char *pname = NULL;
int slotn, has_include;
/* protect some pointers */
xlstkcheck(6);
xlsave(structname);
xlsave(slotname);
xlsave(defexpr);
xlsave(args);
xlsave(body);
xlsave(tmp);
/* initialize */
args = body = NIL;
slotn = 0;
has_include = FALSE;
constrsym = NULL;
predsym = NULL;
/* get the structure name */
tmp = xlgetarg();
if (symbolp(tmp)) {
structname = tmp;
pname = getstring(getpname(structname));
sprintf(prefix, "%s-", pname);
}
/* get the structure name and options */
else if (consp(tmp) && symbolp(car(tmp))) {
structname = car(tmp);
pname = getstring(getpname(structname));
sprintf(prefix, "%s-", pname);
/* handle the list of options */
for (options = cdr(tmp); consp(options); options = cdr(options)) {
/* get the next argument */
tmp = car(options);
/* handle options that don't take arguments */
if (symbolp(tmp)) {
xlerror("unknown option",tmp);
}
/* handle options that take arguments */
else if (consp(tmp) && symbolp(car(tmp))) {
oargs = cdr(tmp);
/* check for the :CONC-NAME keyword */
if (car(tmp) == k_concname) {
/* get the name of the structure to include */
if (!consp(oargs) || !symbolp(car(oargs)))
xlerror("expecting a symbol",oargs);
/* save the prefix */
if (null(car(oargs)))
STRCPY(prefix, "");
else
STRCPY(prefix,getstring(getpname(car(oargs))));
}
/* check for the :INCLUDE keyword */
else if (car(tmp) == k_include) {
LVAL parent;
if (has_include)
xlfail("only one :INCLUDE option allowed");
else
has_include = TRUE;
/* get the name of the structure to include */
if (!consp(oargs) || !symbolp(car(oargs)))
xlerror("expecting a structure name",oargs);
parent = tmp = car(oargs);
oargs = cdr(oargs);
/* add each slot from the included structure */
slots = xlgetprop(tmp,s_sslots);
for (; consp(slots); slots = cdr(slots)) {
if (consp(car(slots)) && consp(cdr(car(slots)))) {
/* get the next slot description */
tmp = car(slots);
/* create the slot access functions */
addslot(car(tmp),car(cdr(tmp)),++slotn,&args,&body);
}
}
/* handle slot initialization overrides */
for (; consp(oargs); oargs = cdr(oargs)) {
tmp = car(oargs);
if (symbolp(tmp)) {
slotname = tmp;
defexpr = NIL;
}
else if (consp(tmp) && symbolp(car(tmp))) {
slotname = car(tmp);
defexpr = (consp(cdr(tmp)) ? car(cdr(tmp)) : NIL);
}
else
xlerror("bad slot description",tmp);
updateslot(args,slotname,defexpr);
}
xlputprop(structname,parent,s_strinclude);
}
/* check for :PRINT-FUNCTION option (Ken Whedbee) */
else if (car(tmp) == k_prntfunc) {
if (!consp(oargs) || !symbolp(car(oargs)))
xlerror("expecting a print function name",oargs);
xlputprop(structname,car(oargs),s_prntfunc);
}
else if (car(tmp) == k_construct) {
if (!consp(oargs) || !symbolp(car(oargs)))
xlerror("expecting a constructor function name",oargs);
if (consp(cdr(oargs)))
xlfail("BOA constructors not supported");
constrsym = car(oargs);
if (! symbolp(constrsym)) xlbadtype(constrsym);
xlputprop(structname,constrsym, s_strconstruct);
}
else if (car(tmp) == k_predicate) {
if (!consp(oargs) || !symbolp(car(oargs)))
xlerror("expecting a predicate function name",oargs);
predsym = car(oargs);
if (! symbolp(predsym)) xlbadtype(predsym);
}
else
xlerror("unknown option",tmp);
}
else
xlerror("bad option syntax",tmp);
}
}
/**** need to add documentation string */
/* flush documentation string */
if (moreargs() && stringp(*xlargv)) (void)(nextarg());
/* get each of the structure members */
while (moreargs()) {
/* get the slot name and default value expression */
tmp = xlgetarg();
if (symbolp(tmp)) {
slotname = tmp;
defexpr = NIL;
}
else if (consp(tmp) && symbolp(car(tmp))) {
slotname = car(tmp);
defexpr = (consp(cdr(tmp)) ? car(cdr(tmp)) : NIL);
}
else
xlerror("bad slot description",tmp);
/* create a closure for non-trivial default expressions */
if (defexpr != NIL) {
tmp = newclosure(NIL,s_lambda,xlenv,xlfenv);
setbody(tmp,cons(defexpr,NIL));
tmp = cons(tmp,NIL);
defexpr = tmp;
}
/* create the slot access functions */
addslot(slotname,defexpr,++slotn,&args,&body);
}
/* store the slotnames and default expressions */
xlputprop(structname,args,s_sslots);
/* enter the MAKE-xxx symbol */
if (constrsym == NULL) {
sprintf(buf, makestr, pname);
#ifdef PACKAGES
constrsym = xlintern(buf, getvalue(s_package));
#else
constrsym = xlenter(buf);
#endif /* PACKAGES */
xlputprop(structname,constrsym, s_strconstruct);
}
/* make the MAKE-xxx function */
if (! null(constrsym)) {
args = cons(lk_key,args);
tmp = cons(structname,NIL);
tmp = cons(s_quote,tmp);
body = cons(tmp,body);
body = cons(s_mkstruct,body);
body = cons(body,NIL);
setfunction(constrsym,
xlclose(constrsym,s_lambda,args,body,xlenv,xlfenv));
}
/* enter the xxx-P symbol */
if (predsym == NULL) {
sprintf(buf,"%s-P", pname);
#ifdef PACKAGES
predsym = xlintern(buf, getvalue(s_package));
#else
predsym = xlenter(buf);
#endif /* PACKAGES */
}
/* make the xxx-P function */
if (! null(predsym)) {
args = cons(s_x,NIL);
body = cons(s_x,NIL);
tmp = cons(structname,NIL);
tmp = cons(s_quote,tmp);
body = cons(tmp,body);
body = cons(s_strtypep,body);
body = cons(body,NIL);
setfunction(predsym,
xlclose(predsym,s_lambda,args,body,NIL,NIL));
}
/* enter the COPY-xxx symbol */
sprintf(buf,"COPY-%s", pname);
#ifdef PACKAGES
sym = xlintern(buf, getvalue(s_package));
#else
sym = xlenter(buf);
#endif /* PACKAGES */
/* make the COPY-xxx function */
args = cons(s_x,NIL);
body = cons(s_x,NIL);
body = cons(s_cpystruct,body);
body = cons(body,NIL);
setfunction(sym,
xlclose(sym,s_lambda,args,body,NIL,NIL));
/* restore the stack */
xlpopn(6);
/* return the structure name */
return (structname);
}
/* xlrdstruct - convert a list to a structure (used by the reader) */
/* Modified by TAA to quote arguments and accept leading colons on keys */
LVAL xlrdstruct P1C(LVAL, list)
{
LVAL structname,slotname,expr,last,val;
/* protect the new structure */
xlsave1(expr);
/* get the structure name */
if (!consp(list) || !symbolp(car(list)))
xlerror("bad structure initialization list",list);
structname = car(list);
list = cdr(list);
/* initialize the constructor function call expression */
expr = consa(xlgetprop(structname, s_strconstruct));
last = expr;
/* turn the rest of the initialization list into keyword arguments */
while (consp(list) && consp(cdr(list))) {
/* get the slot keyword name */
slotname = car(list);
if (!symbolp(slotname))
xlerror("expecting a slot name",slotname);
/* add the slot keyword */
#ifdef PACKAGES
rplacd(last,consa(xlintern(getstring(getpname(slotname)), xlkeypack)));
#else
if (*(getstring(getpname(slotname))) != ':') { /* add colon */
sprintf(buf,":%s",getstring(getpname(slotname)));
rplacd(last,cons(xlenter(buf),NIL));
}
else {
rplacd(last,cons(slotname,NIL));
}
#endif /* PACKAGES */
last = cdr(last);
list = cdr(list);
/* add the value expression -- QUOTED (TAA MOD) */
rplacd(last,cons(NIL,NIL));
last = cdr(last);
rplaca(last, (slotname = cons(s_quote,NIL)));
rplacd(slotname, cons(car(list), NIL));
list = cdr(list);
}
/* make sure all of the initializers were used */
if (consp(list))
xlerror("bad structure initialization list",list);
/* invoke the creation function */
val = xleval(expr);
/* restore the stack */
xlpop();
/* return the new structure */
return (val);
}
/* xlprstruct - print a structure (used by printer) */
VOID xlprstruct P4C(LVAL, fptr, LVAL, vptr, FIXTYPE, plevel, int, flag)
{
LVAL next;
int i,n;
FRAMEP newfp;
next = xlgetprop(getelement(vptr,0), s_prntfunc);
if (!null(next)) { /* Ken Whedbee addition */
newfp = xlsp;
pusharg(cvfixnum((FIXTYPE)(newfp - xlfp)));
pusharg(symbolp(next) ? xlgetfunction(next) : next);
pusharg(cvfixnum((FIXTYPE) 3));
pusharg(vptr);
pusharg(fptr);
pusharg(cvfixnum(plevel));
xlfp = newfp;
xlapply(3);
}
else {
xlputstr(fptr,"#S("); /* TAA MOD */
xlprint(fptr,getelement(vptr,0),flag);
next = xlgetprop(getelement(vptr,0), s_sslots);
for (i = 1, n = getsize(vptr) - 1; i <= n && consp(next); ++i) {
if (consp(car(next))) { /* should always succeed */
xlputc(fptr,' '); /* Alternate, could print " :" */
xlprint(fptr,car(car(next)),flag);
xlputc(fptr,' ');
xlprint(fptr,getelement(vptr,i),flag);
}
next = cdr(next);
}
xlputc(fptr,')');
}
}
/* addslot - make the slot access functions */
LOCAL VOID addslot P5C(LVAL, slotname, LVAL, defexpr, int, slotn, LVAL *, pargs, LVAL *, pbody)
{
LVAL sym,args,body,tmp;
/* protect some pointers */
xlstkcheck(4);
xlsave(sym);
xlsave(args);
xlsave(body);
xlsave(tmp);
/* construct the update function name */
sprintf(buf,"%s%s",prefix,getstring(getpname(slotname)));
#ifdef PACKAGES
sym = xlintern(buf, getvalue(s_package));
#else
sym = xlenter(buf);
#endif /* PACKAGES */
/* make the access function */
args = cons(s_s,NIL);
body = cons(cvfixnum((FIXTYPE)slotn),NIL);
body = cons(s_s,body);
body = cons(s_strref,body);
body = cons(body,NIL);
setfunction(sym,
xlclose(sym,s_lambda,args,body,NIL,NIL));
/* make the update function */
args = cons(s_x,NIL);
args = cons(s_s,args);
body = cons(s_x,NIL);
body = cons(cvfixnum((FIXTYPE)slotn),body);
body = cons(s_s,body);
body = cons(s_strset,body);
body = cons(body,NIL);
xlputprop(sym,
xlclose(NIL,s_lambda,args,body,NIL,NIL),
s_setf);
/* add the slotname to the make-xxx keyword list */
tmp = cons(defexpr,NIL);
tmp = cons(slotname,tmp);
tmp = cons(tmp,NIL);
if ((args = *pargs) == NIL)
*pargs = tmp;
else {
while (consp(cdr(args)))
args = cdr(args);
rplacd(args,tmp);
}
/* add the slotname to the %make-xxx argument list */
tmp = cons(slotname,NIL);
if ((body = *pbody) == NIL)
*pbody = tmp;
else {
while (consp(cdr(body)))
body = cdr(body);
rplacd(body,tmp);
}
/* restore the stack */
xlpopn(4);
}
/* updateslot - update a slot definition */
LOCAL VOID updateslot P3C(LVAL, args, LVAL, slotname, LVAL, defexpr)
{
LVAL tmp;
for (; consp(args); args = cdr(args))
if (slotname == car(car(args))) {
if (defexpr != NIL) {
xlsave1(tmp);
tmp = newclosure(NIL,s_lambda,xlenv,xlfenv);
setbody(tmp,cons(defexpr,NIL));
tmp = cons(tmp,NIL);
defexpr = tmp;
xlpop();
}
rplaca(cdr(car(args)),defexpr);
break;
}
if (args == NIL)
xlerror("unknown slot name",slotname);
}
|