home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Vectronix 2
/
VECTRONIX2.iso
/
FILES_01
/
X_PROLOG.LZH
/
X_PROLOG
/
SOURCES
/
BIMATH.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-13
|
3KB
|
162 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 "error.h"
#include "extern.h"
#define ARG1 eval(argument(t, Topenv, 1))
#define ARG2 eval(argument(t, Topenv, 2))
extern term *int_copy(); /* terms */
extern term *argument(); /* terms */
extern long cputime(); /* bisys */
long eval(); /* forward */
/* The evaluating function */
short biis(args)
term *args[];
{
long l;
if (! ISINT(args[0]) && ! ISVAR(args[0]))
BIERROR(EBAD);
if (ISVAR(args[1]))
BIERROR(EBAD);
l = eval(args[1]);
if (c_errno)
return(FALSE);
else
if (ISINT(args[0]))
return(VALUE(args[0]) == l);
else
BIND_VAR(args[0], int_copy(l));
return(TRUE);
}
long eval(t)
register term *t;
{
long l;
if (ISVAR(t))
BIERROR(EEVAL);
if (ISINT(t)) /* return it's value */
return(VALUE(t));
if (FUNC(t) == DOTFUNCTOR) /* take it's first arg */
return((long)FUNC(argument(t,Topenv,1))->name[0]);
if (!EXP_NO(FUNC(t))) /* no expression */
BIERROR(EEVAL);
switch(EXP_NO(FUNC(t)))
{
case EXPPLUS: return(ARG1 + ARG2);
case EXPMINUS: return(ARG1 - ARG2);
case EXPTIMES: return(ARG1 * ARG2);
case EXPDIV: if ((l = ARG2) == 0L)
BIERROR(EEVAL);
return(ARG1 / l);
case EXPMOD: if ((l = ARG2) == 0L)
BIERROR(EEVAL);
return(ARG1 % l);
case EXPNEGATE: return(-1 * ARG1);
case EXPAND: return(ARG1 & ARG2);
case EXPOR: return(ARG1 | ARG2);
case EXPLEFT: return(ARG1 << ARG2);
case EXPRIGHT: return(ARG1 >> ARG2);
case EXPNOT: return(~ ARG1);
case EXPCPU: return(cputime());
case EXPHEAP: return((long)protonext-(long)protostack);
default: BIERROR(EEVAL);
}
}
/* SUCC ( X, X+1 ) */
short bisucc(args)
term *args[];
{
if (ISINT(args[0]))
{
if (ISINT(args[1]))
return(VALUE(args[0]) == VALUE(args[1])+1);
if (!ISVAR(args[1]))
BIERROR(EBAD);
BIND_VAR(args[1], int_copy(VALUE(args[0])+1));
return(TRUE);
}
if (!ISVAR(args[0]) || !ISINT(args[1]))
BIERROR(EBAD);
BIND_VAR(args[0], int_copy(VALUE(args[1])-1));
return(TRUE);
}
/* A =:= B */
short bieeq(args)
term *args[];
{
return(eval(args[0]) == eval(args[1]));
}
/* A =\= B */
short bieneq(args)
term *args[];
{
return(eval(args[0]) != eval(args[1]));
}
/* A < B */
short bieless(args)
term *args[];
{
return(eval(args[0]) < eval(args[1]));
}
/* A > B */
short biegreat(args)
term *args[];
{
return(eval(args[0]) > eval(args[1]));
}
/* A =< B */
short bieeqless(args)
term *args[];
{
return(eval(args[0]) <= eval(args[1]));
}
/* A >= B */
short biegreateq(args)
term *args[];
{
return(eval(args[0]) >= eval(args[1]));
}