home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Vectronix 2
/
VECTRONIX2.iso
/
FILES_01
/
X_PROLOG.LZH
/
X_PROLOG
/
SOURCES
/
PROLOG.H
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-13
|
9KB
|
297 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.
*
*/
#ifndef FILE
#include <stdio.h>
#endif
#ifndef TRUE
#define TRUE (1)
#define FALSE (0)
#endif
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? a : b)
#endif
#define ATARI
#define MAXARGS 20 /* restrict the max numbers of bi_arguments */
#define MAXVARS 50 /* restrict the max # of vars in a term */
#define FREEVAR (term *)0L /* value of an unbound variable */
#define MAXSTREAMS 15 /* up to 15 streams may be opened */
#define SHELL "c:\\bin\\msh.prg" /* the default shell */
#define MAXLINE 8000 /* the maximum length of an input line */
#define MAXNAME 256 /* the maximum length of a name */
#define IMAGEVERSION 1 /* number of prolog image */
#define IMAGETYPE 1 /* 1: 68000, 2:VAX, 3.... */
#define MAGIC "X Prolog Vers. 2.0"
/* info for functors */
#define LOWER_LEFT 0x1 /* decrease left prio. x-operator */
#define LOWER_RIGHT 0x2 /* decrease right prio. x-operator */
#define EXPRESSION 0x4 /* info[0] contains # of expression */
#define SPYPOINT 0x8 /* this is a spypoint */
#define HIDDEN 0x10 /* functor is hidden */
#define PROTECTED 0x20 /* clauses are protected */
#define ISLOWER_LEFT(f) ((f)->flags & LOWER_LEFT)
#define ISLOWER_RIGHT(f) ((f)->flags & LOWER_RIGHT)
#define ISEXPRESSION(f) ((f)->flags & EXPRESSION)
#define ISSPYPOINT(f) ((f)->flags & SPYPOINT)
#define ISHIDDEN(f) ((f)->flags & HIDDEN)
#define ISPROTECTED(f) ((f)->flags & PROTECTED)
#define OP_PRE(x) ((x)->info[1])
#define OP_INF(x) ((x)->info[2])
#define OP_POST(x) ((x)->info[3])
/* various flags for terms */
#define ANOPROTO 0x400
#define VAR 0x800
#define INT 0x1000
#define STRUCT 0x2000
#define ANONYMOUS 0x4000
#define FREE 0x8000
#define ISFRAME(t) ((long)(t) >= (long)stack)
#define ISPROTO(t) ((long)(t) < (long)protofull)
#define ISCOPY(t) ((long)(t) >= (long)copystack)
#define ISVAR(t) ((t)->flags & VAR)
#define ISINT(t) ((t)->flags & INT)
#define ISSTRUCT(t) ((t)->flags & STRUCT)
#define ISANONYMOUS(t) ((t)->flags & ANONYMOUS)
#define ISANOPROTO(t) ((t)->flags & ANOPROTO)
#define ISFREE(t) ((t)->flags & FREE)
#define FUNC(x) ((functor *)((x)->data.args[0]))
#define ARG(x,n) (x)->data.args[(n)]
#define VALUE(x) (x)->data.value
#define REF(x) (x)->data.ref
#define NAME(x) FUNC(x)->name
#define ARITY(x) FUNC(x)->arity
#define ISATOM(x) (ISSTRUCT(x) && ARITY(x) == 0)
/* various flags for clauses */
#define BUILTIN 0x1
#define ISBUILTIN(c) ((c)->flags & BUILTIN)
/* macros for memory handling */
#define EMPTY_BACK (backtop == backstack)
#define PUSH_TRAIL(x) \
{\
if ((x)<=Backpoint->copylev||(ISFRAME(x)&&(long)(x)<(long)Backpoint))\
{\
if (trailtop==trailfull)\
panic(TRAILFULL);\
trailstack[trailtop++] = x;\
}\
}
#define BIND_VAR(x,y) {REF(x)=y;PUSH_TRAIL(x);}
/* various flags for streams */
#define INPUT 0x1
#define OUTPUT 0x2
#define CLOSED 0x4
#define SEOF 0x8
#define ISFORINPUT(x) ((x)->status & INPUT)
#define ISFOROUTPUT(x) ((x)->status & OUTPUT)
#define ISCLOSED(x) ((x)->status & CLOSED)
#define ISATEOF(x) ((x)->status & SEOF)
/* standard functors */
#define STDFUNCTORS 19
#define DOTFUNCTOR (stdfunctor[0])
#define CALLFUNCTOR (stdfunctor[1])
#define XFXFUNCTOR (stdfunctor[2])
#define XFYFUNCTOR (stdfunctor[3])
#define YFXFUNCTOR (stdfunctor[4])
#define YFYFUNCTOR (stdfunctor[5])
#define FXFUNCTOR (stdfunctor[6])
#define FYFUNCTOR (stdfunctor[7])
#define XFFUNCTOR (stdfunctor[8])
#define YFFUNCTOR (stdfunctor[9])
#define COMMAFUNCTOR (stdfunctor[10])
#define MINUSFUNCTOR (stdfunctor[11])
#define EOFFUNCTOR (stdfunctor[12])
#define SEMICOLONFUNCTOR (stdfunctor[13])
#define IFFUNCTOR (stdfunctor[14])
#define NILFUNCTOR (stdfunctor[15])
#define USERFUNCTOR (stdfunctor[16])
#define TRUEFUNCTOR (stdfunctor[17])
#define ASSERTFUNCTOR (stdfunctor[18])
/* standard atoms */
#define NILATOM term_copy(NILFUNCTOR)
#define ANONYMOUSATOM anonymous_copy()
#define USERATOM term_copy(USERFUNCTOR)
#define EOFATOM term_copy(EOFFUNCTOR)
/* misc macros */
#define FILEERROR(a) {c_errno = (a); return(FALSE);}
#define BIERROR(a) {c_errno = (a); return(FALSE);}
/* reader data */
#define UPPER 0 /* upper case */
#define LOWER 1 /* lower case letter */
#define DIGIT 2 /* a digit */
#define QUOTE 3 /* '' */
#define STRING 4 /* "" */
#define SYMBOL 5 /* a symbol char, i.e. $%&/... */
#define SINGLE 6 /* single char word */
#define BRACKET 7 /* */
#define BLANK 8 /* to be ignored */
#define TSYMBOL 0 /* a symbol token (name) */
#define TINTEGER 1 /* a integer token */
#define TVARIABLE 2 /* a variable token */
#define TSTRING 3 /* a string (not really a token, but...) */
#define TSPECIAL 4 /* break chars etc. */
#define TDOT 5 /* the dot (end of term) */
/* Data for expresions */
#define EXP_NO(f) ((f)->info[0])
#define EXPPLUS 1
#define EXPMINUS 2
#define EXPTIMES 3
#define EXPDIV 4
#define EXPMOD 5
#define EXPNEGATE 6
#define EXPAND 7
#define EXPOR 8
#define EXPLEFT 9
#define EXPRIGHT 10
#define EXPNOT 11
#define EXPCPU 12
#define EXPHEAP 13
/* type definitions */
typedef struct _functor /* a functor descriptor */
{
short arity; /* the arity of the functor */
char *cp; /* the list of clauses with this functor */
short flags; /* functor flags */
unsigned char info[4]; /* functor info */
/* the following data is for maintaining the avl-tree */
struct _functor *left, /* left subtree */
*right; /* right subtree */
short balance; /* the balance of this tree */
/* undefined name */
char name[1]; /* slot for name */
} functor;
typedef struct _term /* a term skeleton */
{
short flags; /* several flags */
union
{
struct _term *args[1]; /* arguments of compound term */
long value; /* integer value */
struct _term *ref; /* var binding */
} data;
} term;
typedef struct _env /* a machine enviroment */
{
struct _env *pre; /* logical predecessor */
term *current; /* the current active term */
term *frame; /* the current frame */
short nvars; /* # of variables in that frame */
} env;
typedef struct _backlog /* a backtrack log (choice point) */
{
env *frozen_env; /* the 'to be protected' enviroment */
char *resume; /* alternative clauses */
term *copylev; /* height of frozen copystack */
long traillev; /* height of frozen trailstack */
struct _backlog *pre; /* previous backlog */
} backlog;
typedef struct _clause /* a clause node */
{
struct _clause *next; /* next clause with same functor */
short nvars; /* count of variables of that clause */
short flags; /* several flags */
term *head; /* head of clause 'head :- body.' */
term *body;
} clause;
typedef struct /* an entry for a builtin */
{
char *name; /* the function name */
short (*func)(); /* the function pointer to the bi */
short nargs; /* the number of arguments */
} bi;
typedef struct /* a stream descriptor */
{
FILE *fp; /* the pointer to the STDIO handler */
term *atom; /* the describeing atom */
short status; /* the status of the stream */
} stream;
typedef struct /* reader's var lookuptable entry */
{
char name[MAXNAME]; /* the variable's name */
term *t; /* associated term */
} lookup;
typedef struct /* header for prolog images */
{
char magic[20]; /* magic cookie */
short version; /* version of prolog image */
short i_type; /* type of machine */
char *f_top; /* data of functor area */
long f_size;
functor *f_tree; /* root of functor avl tree */
clause *cl_top; /* data of clause area */
long cl_size;
clause *cl_free;
char *p_top; /* data of programs area */
long p_size;
term *p_free[MAXARGS];
long t_size; /* size of trails */
long c_size; /* size of copystack */
long s_size; /* size of stack */
functor *stdf[STDFUNCTORS]; /* all std functors */
} i_header;
extern term *anonymous_copy(); /* terms */
extern term *term_copy(); /* terms */