/************************************************************************ * * * The SB-Prolog System * * Copyright SUNY at Stony Brook, 1986 * * * ************************************************************************/ /*----------------------------------------------------------------- SB-Prolog is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless he says so in writing. Refer to the SB-Prolog General Public License for full details. Everyone is granted permission to copy, modify and redistribute SB-Prolog, but only under the conditions described in the SB-Prolog General Public License. A copy of this license is supposed to have been given to you along with SB-Prolog so you can know your rights and responsibilities. It should be in a file named COPYING. Among other things, the copyright notice and this notice must be preserved on all copies. ------------------------------------------------------------------ */ /* structure.c */ #include "builtin.h" static char perm = PERM; b_ARG0() { /* (I, T, X) */ /* I is bound to a number; T is bound to a structure; X may be free or bound; no checking for above is done; I out of range will cause failure */ register word op1, op2; register word *top; op1 = gregc(1); deref(op1); if (!isinteger(op1)) { printf("arg0: Index must be an integer.\n"); Fail0; return; } op1 = intval(op1); op2 = gregc(2); deref(op2); if (isconstr(op2) && op1 <= get_str_arity(op2) && op1 > 0) if (unify(*(((pw)(untag(op2))) + op1), gregc(3))) return; if (islist(op2) && op1 <= 2) if (unify(*(((pw)(untag(op2))) + op1 - 1), gregc(3))) return; Fail0; } b_ARITY() { /* (T, N) */ /* T is bound to a structure or constant; N will be unified with the arity of T */ register word op1, op2; register word *top; op1 = gregc(1); deref(op1); if (isconstr(op1)) op2 = makeint(get_arity(get_str_psc(op1))); else if (islist(op1)) op2 = makeint(2); else if (isnum(op1)) op2 = makeint(0); else {printf("arity: Term must be bound.\n"); Fail0; return;} if (!unify(gregc(2), op2)) {Fail0;} } b_LENGTH() { /* (L, N) */ /* L is a list, N will be unified with the length of the list. If L is [], N will be 0; If L is not a list, fail. */ quit("Length builtin not implemented.\n"); } b_FUNCTOR0() { /* (T, F) */ /* T is bound to a structure (no checking), F will be unified with the functor of T */ register word op1, op2; register pw top; struct psc_rec *psc_p; word insert(); op1 = gregc(1); deref(op1); if (islist(op1)) op2 = insert(".", 1, 0, &perm); else { psc_p = get_str_psc(op1); if (get_arity(psc_p) != 0) op2 = insert(get_name(psc_p), get_length(psc_p), 0, &perm); else op2 = op1; } op2 |= CS_TAG; if (!unify(op2, gregc(2))) {Fail0;} } b_BLDSTR() { /* (F, N, T) */ /* F is bound to an atom, N is bound to an integer, (>0) T is free, and will be set to the most general structure with functor F; incomplete error checking */ int i; char a; struct psc_rec *psc_p; register word op1, op2; register pw top; op1 = gregc(1); deref(op1); if (!isconstr(op1)) {printf("bldstr: first arg must be constant\n"); Fail0; return;} op2 = gregc(2); deref(op2); if (!isinteger(op2)) {printf("bldstr: second arg must be integer\n"); Fail0; return;} a = intval(op2); op2 = gregc(3); deref(op2); psc_p = get_str_psc(op1); if (get_arity(psc_p)>0) {printf("bldstr: first arg must be constant\n"); Fail0; return;} if ((a==2) && (get_name(psc_p)[0]=='.') && (get_length(psc_p)==1)) follow(op2) = (word)hreg | LIST_TAG; else if (a==0) follow(op2) = op1; else { op1 = insert(get_name(psc_p), get_length(psc_p), a, &perm); /* printf("bldstr: %c %d %d %d\n", *get_name(psc_p), get_length(psc_p), a, op1); */ op1 = follow(op1); /* returns addr of addr of psc */ follow(op2) = (word)hreg | CS_TAG; *hreg++ = op1; } pushtrail(op2); for (i = 0; i < a; hreg++, i++) make_free(*hreg); }