home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Vectronix 2
/
VECTRONIX2.iso
/
FILES_01
/
X_PROLOG.LZH
/
X_PROLOG
/
SOURCES
/
BICOMPAR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-13
|
3KB
|
146 lines
/*
* X PROLOG Vers. 2.0
*
*
* Written by : Andreas Toenne
* CS Dept. , IRB
* University of Dortmund, W-Germany
* <at@unido.uucp>
* <....!seismo!unido!at>
* <at@unido.bitnet>
*
* Copyright : This software is copyrighted by Andreas Toenne.
* Permission is granted hereby to copy the entire
* package including this copyright notice without fee.
*
*/
#include "prolog.h"
#include "extern.h"
#include "error.h"
#define COMPARE(x,y) (x < y ? -1 : x==y ? 0 : 1)
extern term *argument(); /* terms */
/*
* Compare a and b and return
* -1 for a < b
* 0 for a = b
* 1 for a > b (analogous to strcmp in C)
*
* Ordering is defined as :
*
* Var < Int < Struct
*
* Comparing vars is not well defined as frame < ? > copy is unknown.
* Ints are ordered as usual.
* Structs are ordered by
* 1. arity
* 2. name
* 3. arguments from left to right
*/
short _compare(a,b)
register term *a,*b;
{
short aritya, arityb, i, j;
if (ISVAR(a))
{
if (!ISVAR(b))
return(-1);
return(COMPARE(a,b));
}
if (ISINT(a))
{
if (ISVAR(b))
return(1);
if (ISINT(b))
return(COMPARE(VALUE(a), VALUE(b)));
return(-1);
}
if (ISSTRUCT(a))
{
if (!ISSTRUCT(b))
return(1);
aritya = ARITY(a);
arityb = ARITY(b);
i = COMPARE(aritya, arityb);
if (i) /* arities differ */
return(i);
i = strcmp(NAME(a), NAME(b));
if (i)
return(i); /* names differ */
for (j=1; j<=aritya; j++)
if ((i = _compare(argument(a,Topenv,j),
argument(b,Topenv,j))))
break;
return(i);
}
}
/* A == B */
short bieq(args)
term *args[];
{
return(!_compare(args[0], args[1]));
}
/* A \== B */
short bineq(args)
term *args[];
{
return(_compare(args[0], args[1]));
}
/* A @< B */
short biless(args)
term *args[];
{
return(_compare(args[0], args[1]) < 0);
}
/* A @> B */
short bigreat(args)
term *args[];
{
return(_compare(args[0], args[1]) > 0);
}
/* A @=< B */
short bieqless(args)
term *args[];
{
return(_compare(args[0], args[1]) <= 0);
}
/* A @>= B */
short bigreateq(args)
term *args[];
{
return(_compare(args[0], args[1]) >= 0);
}
/* compare(op, a, b) */
short bicompare(args)
term *args[];
{
if (!ISATOM(args[0]))
BIERROR(EBAD);
switch(NAME(args[0])[0])
{
case '=': return(!_compare(args[1], args[2]));
case '<': return(_compare(args[1], args[2]) < 0);
case '>': return(_compare(args[1], args[2]) > 0);
default: BIERROR(EBAD);
}
}