/************************************************************************ * * * 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. ------------------------------------------------------------------ */ /* compare.c */ #include "builtin.h" extern float floatval(); b_COMPARE() /* R1, R2: two terms to be compared; R3: free var to set the result of the comparison: R1 < R2: int<0, R1 = R2: 0, >0 o/w. R3 must be free and a LOCAL new variable */ /* LIST is the largest term of the all four kinds */ { if (!unify(gregc(3), makeint(compare(gregc(1), gregc(2))))) {Fail0;} } compare(val1, val2) word val1, val2; { int a, b, c; struct psc_rec *ptr1, *ptr2; register pw top; float fpt_val; deref(val2); cd: switch ((int)(val1&3)) { case FREE: nderef(val1, cd); if (!isnonvar(val2)) return(val1 - val2); else return -1; case NUM: if (!isnonvar(val2)) return 1; else if (isinteger(val1) && isinteger(val2)) return intval(val1) - intval(val2); else if (isfloat(val1) && isfloat(val2)) { fpt_val = floatval(val1) - floatval(val2); if (fpt_val > 0) return 1; else if (fpt_val == 0) return 0; else return -1; } else return -1; case CS: if (!isnonvar(val2) || isinteger(val2)) return 1; else if (islist(val2)) return -1; else { ptr1 = get_str_psc(val1); ptr2 = get_str_psc(val2); a = get_arity(ptr1); b = get_arity(ptr2); if (a != b) return a-b; c = comalpha(ptr1, ptr2); if (c || (a == 0)) return c; untag(val1); untag(val2); for (b = 1; b <= a; b++) { c = compare(follow(((pw)val1)+b), follow(((pw)val2)+b)); if (c) break; } return c; } /* break; */ case LIST: if (!islist(val2)) return 1; else { untag(val1); untag(val2); c = compare(follow(val1), follow(val2)); if (c) return c; else return compare(follow(((pw)val1)+1),follow(((pw)val2)+1)); } /* break; */ } } comalpha(name1, name2) struct psc_rec *name1, *name2; { char s1[256], s2[256]; if (name1 == name2) return 0; namestring(name1, s1); namestring(name2, s2); return strcmp(s1, s2); }