home *** CD-ROM | disk | FTP | other *** search
/ Vectronix 2 / VECTRONIX2.iso / FILES_01 / X_PROLOG.LZH / X_PROLOG / SOURCES / PROLOG.H < prev    next >
C/C++ Source or Header  |  1990-08-13  |  9KB  |  297 lines

  1. /*
  2.  *        X PROLOG  Vers. 2.0
  3.  *
  4.  *
  5.  *    Written by :     Andreas Toenne
  6.  *            CS Dept. , IRB
  7.  *            University of Dortmund, W-Germany
  8.  *            <at@unido.uucp>
  9.  *            <....!seismo!unido!at>
  10.  *            <at@unido.bitnet>
  11.  *
  12.  *    Copyright :    This software is copyrighted by Andreas Toenne.
  13.  *            Permission is granted hereby to copy the entire
  14.  *            package including this copyright notice without fee.
  15.  *
  16.  */
  17.  
  18.  
  19. #ifndef FILE
  20. #include <stdio.h>
  21. #endif
  22.  
  23. #ifndef TRUE
  24. #define TRUE (1)
  25. #define FALSE (0)
  26. #endif
  27.  
  28. #ifndef MAX
  29. #define MAX(a,b)    ((a) > (b) ? a : b)
  30. #endif
  31.  
  32. #define ATARI
  33.  
  34. #define MAXARGS    20        /* restrict the max numbers of bi_arguments */
  35. #define MAXVARS 50        /* restrict the max # of vars in a term */
  36. #define FREEVAR (term *)0L    /* value of an unbound variable */
  37. #define MAXSTREAMS 15        /* up to 15 streams may be opened */
  38. #define SHELL    "c:\\bin\\msh.prg"    /* the default shell */
  39. #define MAXLINE    8000        /* the maximum length of an input line */
  40. #define MAXNAME    256        /* the maximum length of a name */
  41.  
  42. #define IMAGEVERSION 1        /* number of prolog image */
  43. #define IMAGETYPE    1        /* 1: 68000, 2:VAX, 3.... */
  44. #define MAGIC         "X Prolog Vers. 2.0"
  45.  
  46. /*    info for functors    */
  47.  
  48. #define LOWER_LEFT    0x1    /* decrease left prio. x-operator */
  49. #define LOWER_RIGHT    0x2    /* decrease right prio. x-operator */
  50. #define EXPRESSION    0x4    /* info[0] contains # of expression */
  51. #define SPYPOINT    0x8    /* this is a spypoint */
  52. #define HIDDEN        0x10    /* functor is hidden */
  53. #define PROTECTED    0x20    /* clauses are protected */
  54.  
  55. #define ISLOWER_LEFT(f)    ((f)->flags & LOWER_LEFT)
  56. #define ISLOWER_RIGHT(f) ((f)->flags & LOWER_RIGHT)
  57. #define ISEXPRESSION(f)    ((f)->flags & EXPRESSION)
  58. #define ISSPYPOINT(f)    ((f)->flags & SPYPOINT)
  59. #define ISHIDDEN(f)    ((f)->flags & HIDDEN)
  60. #define ISPROTECTED(f)    ((f)->flags & PROTECTED)
  61.  
  62. #define OP_PRE(x)    ((x)->info[1])
  63. #define OP_INF(x)    ((x)->info[2])
  64. #define OP_POST(x)    ((x)->info[3])
  65.  
  66. /*    various flags for terms */
  67.  
  68. #define ANOPROTO    0x400
  69. #define VAR        0x800
  70. #define INT        0x1000
  71. #define    STRUCT        0x2000
  72. #define ANONYMOUS    0x4000
  73. #define FREE        0x8000
  74.  
  75.  
  76. #define ISFRAME(t)    ((long)(t) >= (long)stack)
  77. #define ISPROTO(t)    ((long)(t) < (long)protofull)
  78. #define ISCOPY(t)    ((long)(t) >= (long)copystack)
  79. #define ISVAR(t)    ((t)->flags & VAR)
  80. #define ISINT(t)    ((t)->flags & INT)
  81. #define ISSTRUCT(t)    ((t)->flags & STRUCT)
  82. #define ISANONYMOUS(t)    ((t)->flags & ANONYMOUS)
  83. #define ISANOPROTO(t)    ((t)->flags & ANOPROTO)
  84. #define ISFREE(t)    ((t)->flags & FREE)
  85.  
  86. #define FUNC(x)        ((functor *)((x)->data.args[0]))
  87. #define ARG(x,n)    (x)->data.args[(n)]
  88. #define VALUE(x)    (x)->data.value
  89. #define REF(x)        (x)->data.ref
  90. #define NAME(x)        FUNC(x)->name
  91. #define ARITY(x)    FUNC(x)->arity
  92. #define ISATOM(x)    (ISSTRUCT(x) && ARITY(x) == 0)
  93.  
  94. /*  various flags for clauses */
  95.  
  96. #define BUILTIN 0x1
  97.  
  98. #define ISBUILTIN(c)    ((c)->flags & BUILTIN)
  99.  
  100. /*  macros for memory handling */
  101.  
  102. #define EMPTY_BACK    (backtop == backstack)
  103. #define PUSH_TRAIL(x) \
  104. {\
  105.     if ((x)<=Backpoint->copylev||(ISFRAME(x)&&(long)(x)<(long)Backpoint))\
  106.     {\
  107.         if (trailtop==trailfull)\
  108.             panic(TRAILFULL);\
  109.         trailstack[trailtop++] = x;\
  110.     }\
  111. }
  112. #define BIND_VAR(x,y) {REF(x)=y;PUSH_TRAIL(x);}
  113.  
  114. /*  various flags for streams */
  115.  
  116. #define INPUT    0x1
  117. #define OUTPUT    0x2
  118. #define CLOSED    0x4
  119. #define SEOF    0x8
  120.  
  121. #define ISFORINPUT(x)    ((x)->status & INPUT)
  122. #define ISFOROUTPUT(x)    ((x)->status & OUTPUT)
  123. #define ISCLOSED(x)    ((x)->status & CLOSED)
  124. #define ISATEOF(x)    ((x)->status & SEOF)
  125.  
  126. /* standard functors */
  127.  
  128. #define STDFUNCTORS     19
  129.  
  130. #define DOTFUNCTOR    (stdfunctor[0])
  131. #define CALLFUNCTOR    (stdfunctor[1])
  132. #define XFXFUNCTOR    (stdfunctor[2])
  133. #define XFYFUNCTOR    (stdfunctor[3])
  134. #define YFXFUNCTOR    (stdfunctor[4])
  135. #define YFYFUNCTOR    (stdfunctor[5])
  136. #define FXFUNCTOR    (stdfunctor[6])
  137. #define FYFUNCTOR    (stdfunctor[7])
  138. #define XFFUNCTOR    (stdfunctor[8])
  139. #define YFFUNCTOR    (stdfunctor[9])
  140. #define COMMAFUNCTOR    (stdfunctor[10])
  141. #define MINUSFUNCTOR    (stdfunctor[11])
  142. #define EOFFUNCTOR    (stdfunctor[12])
  143. #define SEMICOLONFUNCTOR (stdfunctor[13])
  144. #define IFFUNCTOR    (stdfunctor[14])
  145. #define NILFUNCTOR    (stdfunctor[15])
  146. #define USERFUNCTOR    (stdfunctor[16])
  147. #define TRUEFUNCTOR    (stdfunctor[17])
  148. #define ASSERTFUNCTOR    (stdfunctor[18])
  149.  
  150. /* standard atoms */
  151.  
  152. #define NILATOM        term_copy(NILFUNCTOR)
  153. #define ANONYMOUSATOM    anonymous_copy()
  154. #define USERATOM    term_copy(USERFUNCTOR)
  155. #define EOFATOM        term_copy(EOFFUNCTOR)
  156.  
  157. /* misc macros    */
  158.  
  159. #define FILEERROR(a)    {c_errno = (a); return(FALSE);}
  160. #define BIERROR(a)    {c_errno = (a); return(FALSE);}
  161.  
  162. /* reader data */
  163.  
  164. #define UPPER        0    /* upper case */
  165. #define LOWER        1    /* lower case letter */
  166. #define DIGIT        2    /* a digit */
  167. #define QUOTE        3    /* '' */
  168. #define STRING        4    /* "" */
  169. #define SYMBOL        5    /* a symbol char, i.e. $%&/... */
  170. #define SINGLE        6    /* single char word */
  171. #define BRACKET        7    /* */
  172. #define BLANK        8    /* to be ignored */
  173.  
  174. #define TSYMBOL        0    /* a symbol token (name) */
  175. #define TINTEGER    1    /* a integer token */
  176. #define TVARIABLE    2    /* a variable token */
  177. #define TSTRING        3    /* a string (not really a token, but...) */
  178. #define TSPECIAL    4    /* break chars etc. */
  179. #define TDOT        5    /* the dot (end of term) */
  180.  
  181. /*    Data for expresions */
  182.  
  183. #define EXP_NO(f)    ((f)->info[0])
  184.  
  185. #define EXPPLUS        1
  186. #define EXPMINUS    2
  187. #define EXPTIMES    3
  188. #define EXPDIV        4
  189. #define EXPMOD        5
  190. #define EXPNEGATE    6
  191. #define EXPAND        7
  192. #define EXPOR        8
  193. #define EXPLEFT        9
  194. #define EXPRIGHT    10
  195. #define EXPNOT        11
  196. #define EXPCPU        12
  197. #define EXPHEAP        13
  198.  
  199. /*   type definitions */
  200.  
  201. typedef struct _functor        /* a functor descriptor */
  202. {
  203.     short arity;        /* the arity of the functor */
  204.     char *cp;        /* the list of clauses with this functor */
  205.     short flags;        /* functor flags */
  206.     unsigned char info[4];    /* functor info */
  207.     /* the following data is for maintaining the avl-tree */
  208.     struct _functor *left,    /* left subtree */
  209.                *right;    /* right subtree */
  210.     short balance;        /* the balance of this tree */
  211.     /* undefined name */
  212.     char name[1];        /* slot for name */
  213. } functor;
  214.  
  215. typedef struct _term        /* a term skeleton */
  216. {
  217.     short flags;        /* several flags */
  218.     union
  219.     {
  220.         struct _term *args[1];    /* arguments of compound term */
  221.         long    value;        /* integer value */
  222.         struct _term *ref;    /* var binding */
  223.     } data;
  224. } term;
  225.  
  226. typedef struct _env        /* a machine enviroment */
  227. {
  228.     struct _env *pre;    /* logical predecessor */
  229.     term *current;        /* the current active term */
  230.     term *frame;        /* the current frame */
  231.     short nvars;        /* # of variables in that frame */
  232. } env;
  233.  
  234. typedef struct _backlog        /* a backtrack log (choice point) */
  235. {
  236.     env *frozen_env;    /* the 'to be protected' enviroment */
  237.     char *resume;        /* alternative clauses */
  238.     term *copylev;        /* height of frozen copystack */
  239.     long traillev;        /* height of frozen trailstack */
  240.     struct _backlog *pre;    /* previous backlog */
  241. } backlog;
  242.  
  243. typedef struct _clause        /* a clause node */
  244. {
  245.     struct _clause *next;    /* next clause with same functor */
  246.     short nvars;        /* count of variables of that clause */
  247.     short flags;        /* several flags */
  248.     term *head;        /* head of clause 'head :- body.' */
  249.     term *body;
  250. } clause;
  251.  
  252.  
  253. typedef struct            /* an entry for a builtin */
  254. {
  255.     char *name;        /* the function name */
  256.     short (*func)();    /* the function pointer to the bi */
  257.     short nargs;        /* the number of arguments */
  258. } bi;
  259.  
  260. typedef struct            /* a stream descriptor */
  261. {
  262.     FILE *fp;        /* the pointer to the STDIO handler */
  263.     term *atom;        /* the describeing atom */
  264.     short status;        /* the status of the stream */
  265. } stream;
  266.  
  267. typedef struct            /* reader's var lookuptable entry */
  268. {
  269.     char name[MAXNAME];    /* the variable's name */
  270.     term *t;        /* associated term */
  271. } lookup;
  272.  
  273. typedef struct            /* header for prolog images */
  274. {
  275.     char magic[20];        /* magic cookie */
  276.     short version;        /* version of prolog image */
  277.     short i_type;        /* type of machine */
  278.     
  279.     char *f_top;        /* data of functor area */
  280.     long f_size;
  281.     functor *f_tree;    /* root of functor avl tree */
  282.     clause *cl_top;        /* data of clause area */
  283.     long cl_size;
  284.     clause *cl_free;
  285.     char *p_top;        /* data of programs area */
  286.     long p_size;
  287.     term *p_free[MAXARGS];
  288.     long t_size;        /* size of trails */
  289.     long c_size;        /* size of copystack */
  290.     long s_size;        /* size of stack */
  291.     functor *stdf[STDFUNCTORS]; /* all std functors */
  292. } i_header;
  293.  
  294. extern term *anonymous_copy();            /* terms */
  295. extern term *term_copy();            /* terms */
  296.  
  297.