home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / SOURCE / PRINT.C < prev    next >
C/C++ Source or Header  |  1996-06-04  |  35KB  |  1,637 lines

  1. /* Copyright 1991 Digital Equipment Corporation.
  2. ** All Rights Reserved.
  3. *****************************************************************/
  4. /*     $Id: print.c,v 1.4 1995/01/14 00:27:20 duchier Exp $     */
  5.  
  6. #ifndef lint
  7. static char vcid[] = "$Id: print.c,v 1.4 1995/01/14 00:27:20 duchier Exp $";
  8. #endif /* lint */
  9.  
  10. #define DOTDOT ": "   /*  RM: Dec 14 1992, should be " : "  */
  11.  
  12.  
  13. #include "extern.h"
  14. #include "trees.h"
  15. #include "types.h"
  16. #include "memory.h"
  17. #include "print.h"  
  18. #include "modules.h"  /*  RM: Jan 13 1993  */
  19. #include "login.h"
  20.  
  21.  
  22.  
  23. ptr_node printed_pointers, pointer_names;
  24.  
  25. long print_depth=PRINT_DEPTH;
  26. long indent=FALSE;
  27. long const_quote=TRUE;
  28. long write_resids=FALSE;
  29. long write_canon=FALSE;
  30. long write_stderr=FALSE;
  31. long write_corefs=TRUE;
  32.  
  33. long gen_sym_counter;
  34. long page_width=PAGE_WIDTH;
  35.  
  36. long display_persistent=FALSE;
  37.   
  38. char *no_name="pointer";
  39. char *name="symbol";
  40. char *buffer;
  41. char seg_format[PRINT_POWER+4];
  42.  
  43. item pretty_things[PRETTY_SIZE];
  44. ptr_item indx;
  45.  
  46. /* Used to distinguish listings from other writes */
  47. static long listing_flag;
  48.  
  49. /* Used to list function bodies in a nice way */
  50. /* Only valid if listing_flag==TRUE */
  51. static long func_flag;
  52.  
  53. /* The output stream for a given print command is put in here */
  54. /* This will be set to stdout, to stderr, or to output_stream */
  55. FILE *outfile;
  56.  
  57. void pretty_psi_term();
  58. void pretty_attr();
  59. void pretty_tag_or_psi_term();
  60.  
  61. /* Precedence of the comma and colon operators (or 0 if none exists) */
  62. #define COMMA_PREC ((commasym->op_data)?(commasym->op_data->precedence):0)
  63. #define COLON_PREC ((colonsym->op_data)?(colonsym->op_data->precedence):0)
  64.  
  65.  
  66. /* Initialize size of single segment of split printing.  Wild_Life         */
  67. /* integers are represented as REALS, and therefore can have higher        */
  68. /* precision than the machine integers.  They will be printed in segments. */
  69. void init_print()
  70. {
  71.   sprintf(seg_format,"%%0%ldd",PRINT_POWER);
  72. }
  73.  
  74.  
  75. /* Generate a nice-looking new variable name. */
  76. char *heap_nice_name()
  77. {
  78.   string tmp1,tmp2;
  79.   long g,len,leading_a;
  80.  
  81.   g= ++gen_sym_counter;
  82.   len=2;
  83.   strcpy(tmp2,"");
  84.   do {
  85.     g--;
  86.     /* Prefix one character to tmp2: */
  87.     sprintf(tmp1,"%c",g%26+'A');
  88.     strcat(tmp1,tmp2);
  89.     strcpy(tmp2,tmp1);
  90.     g=g/26;
  91.     len++;
  92.   } while (g>0 && len<STRLEN);
  93.   if (len>=STRLEN)
  94.     perr("Variable name too long -- the universe has ceased to exist.");
  95.  
  96.   strcpy(tmp1,"_");
  97.   strcat(tmp1,tmp2);
  98.   
  99.   return heap_copy_string(tmp1);
  100. }
  101.  
  102.  
  103. /* Make sure that the new variable name does not exist in the var_tree. */
  104. /* (This situation should be rare.) */
  105. /* Time to print a term is proportional to product of var_tree size and */
  106. /* number of tags in the term.  This may become large in pathological   */
  107. /* cases. */
  108. GENERIC unique_name()
  109. {
  110.   char *name;
  111.  
  112.   do name=heap_nice_name(); while (find(strcmp,name,var_tree));
  113.  
  114.   return (GENERIC) name;
  115. }
  116.  
  117.  
  118.  
  119. /******** STR_TO_INT(s) 
  120.   Converts the string S into a positive integer.
  121.   Returns -1 if s is not an integer.
  122. */
  123. long str_to_int(s)
  124. char *s;
  125. {
  126.   long v=0;
  127.   char c;
  128.  
  129.   c=(*s);
  130.   if (c==0)
  131.     v= -1;
  132.   else {
  133.     while (DIGIT(c)) {
  134.       v=v*10+(c-'0');
  135.       s++;
  136.       c=(*s);
  137.     }
  138.     if (c!=0) v= -1;
  139.   }
  140.  
  141.   return v;
  142. }
  143.  
  144.  
  145.  
  146. /******** PRINT_BIN(b)
  147.   Print the integer B under binary format (currently 26 is printed **-*-).
  148.   This is used to print the binary codes used in type encryption.
  149. */
  150. void print_bin(b)
  151. long b;
  152. {
  153.   long p;
  154.  
  155.   for (p=INT_SIZE;p--;p>0) 
  156.   {
  157.     fprintf(outfile,(b&1?"X":" "));
  158.     b = b>>1;
  159.   }
  160. }
  161.  
  162.  
  163.  
  164. /******** PRINT_CODE(s,c)
  165.   Print a binary code C to a stream s (as used in type encoding).
  166. */
  167. void print_code(s,c)
  168. FILE *s;
  169. ptr_int_list c;
  170. {
  171.   outfile=s;
  172.  
  173.   if (c==NOT_CODED)
  174.     fprintf(outfile,"  (not coded) ");
  175.   else {
  176.     fprintf(outfile,"  [");
  177.     while (c) {
  178.       print_bin(c->value);
  179.       c=c->next;
  180.     }
  181.     fprintf(outfile,"]");
  182.   }
  183. }
  184.  
  185.  
  186. void go_through();
  187.  
  188.  
  189.  
  190. /******** PRINT_OPERATOR_KIND(s,kind)
  191.   Print the kind of an operator.
  192. */
  193. void print_operator_kind(s,kind)
  194. FILE *s;
  195. operator kind;
  196. {
  197.   switch (kind) {
  198.   case xf:
  199.     fprintf(s,"xf");
  200.     break;
  201.   case fx:
  202.     fprintf(s,"fx");
  203.     break;
  204.   case yf:
  205.     fprintf(s,"yf");
  206.     break;
  207.   case fy:
  208.     fprintf(s,"fy");
  209.     break;
  210.   case xfx:
  211.     fprintf(s,"xfx");
  212.     break;
  213.   case xfy:
  214.     fprintf(s,"xfy");
  215.     break;
  216.   case yfx:
  217.     fprintf(s,"yfx");
  218.     break;
  219.   default:
  220.     fprintf(s,"illegal");
  221.     break;
  222.   }
  223. }
  224.  
  225.  
  226.  
  227. /******** CHECK_POINTER(p)
  228.   Count the number of times address P has been encountered in the current
  229.   psi-term being printed. If it is more than once then a tag will have to
  230.   be used.
  231.   If P has not already been seen, then explore the psi_term it points to.
  232. */
  233. void check_pointer(p)
  234. ptr_psi_term p;
  235. {
  236.   ptr_node n;
  237.   
  238.   if (p) {
  239.     deref_ptr(p);
  240.     n=find(intcmp,p,pointer_names);
  241.     if (n==NULL) {
  242.       heap_insert(intcmp,p,&pointer_names,NULL);
  243.       go_through(p);
  244.     }
  245.     else
  246.       n->data=(GENERIC)no_name;
  247.   }
  248. }
  249.  
  250.  
  251.  
  252. /******** GO_THROUGH_TREE(t)
  253.   Explore all the pointers in the attribute tree T.
  254.   Pointers that occur more than once will need a tag.
  255. */
  256. void go_through_tree(t)
  257. ptr_node t;
  258. {
  259.   if (t) {
  260.     go_through_tree(t->left);
  261.     check_pointer((ptr_psi_term)t->data);
  262.     go_through_tree(t->right);
  263.   }
  264. }
  265.  
  266.  
  267.  
  268. /******** GO_THROUGH(t)
  269.   This routine goes through all the sub_terms of psi_term T to determine which
  270.   pointers need to have names given to them for printing because they are
  271.   referred to elsewhere. T is a dereferenced psi_term.
  272. */
  273. void go_through(t)
  274. ptr_psi_term t;
  275. {
  276.   ptr_list l;
  277.  
  278.  
  279.   go_through_tree(t->attr_list);
  280.  
  281.   /*
  282.   if(r=t->resid)
  283.     while(r) {
  284.       if(r->goal->pending)
  285.     go_through(r->goal->a);
  286.       r=r->next;
  287.     } */
  288. }
  289.  
  290.  
  291.  
  292. /******** INSERT_VARIABLES(vars,force)
  293.   This routine gives the name of the query variable to the corresponding
  294.   pointer in the POINTER_NAMES.
  295.   If FORCE is TRUE then variables will be printed as TAGS, even if not
  296.   referred to elsewhere.
  297. */
  298. void insert_variables(vars,force)
  299. ptr_node vars;
  300. long force;
  301. {
  302.   ptr_psi_term p;
  303.   ptr_node n;
  304.   
  305.   if(vars) {
  306.     insert_variables(vars->right,force);
  307.     p=(ptr_psi_term )vars->data;
  308.     deref_ptr(p);
  309.     n=find(intcmp,p,pointer_names);
  310.     if (n)
  311.       if (n->data || force)
  312.     n->data=(GENERIC)vars->key;
  313.     insert_variables(vars->left,force);
  314.   }
  315. }
  316.  
  317.  
  318.  
  319. /******** FORBID_VARIABLES
  320.   This inserts the value of the dereferenced variables into the
  321.   PRINTED_POINTERS tree, so that they will never be printed as
  322.   NAME:value inside a psi-term.
  323.   Each variable is printed as NAME = VALUE by the PRINT_VARIABLES routine.
  324. */
  325. void forbid_variables(n)
  326. ptr_node n;
  327. {
  328.   ptr_psi_term v;
  329.   
  330.   if(n) {
  331.     forbid_variables(n->right);
  332.     v=(ptr_psi_term )n->data;
  333.     deref_ptr(v);
  334.     heap_insert(intcmp,v,&printed_pointers,n->key);
  335.     forbid_variables(n->left);
  336.   }
  337. }
  338.  
  339.  
  340.  
  341.  
  342. /******************************************************************************
  343.   PRINTING ROUTINES.
  344.  
  345.   These routines allow the correct printing in minimal form of a set of
  346.   possibly cyclic psi-terms with coreferences from one to another.
  347.  
  348.   First the term to be printed is explored to locate any cyclic terms or
  349.   coreferences. Then is printed into memory where is it formatted to fit
  350.   within PAGE_WIDTH of the output page. Then it is effectively printed to the
  351.   output stream.
  352.  
  353.  *****************************************************************************/
  354.  
  355.  
  356.  
  357. /* Printing into memory involves the use of an array containing a TAB
  358. position on which to align things then a string to print. The routine
  359. WORK_OUT_LENGTH tries (by trial and error) to print the psi_term into
  360. PAGE_WIDTH columns by inserting line feeds whereever possible */
  361.  
  362.  
  363. /* Does the work of prettyf and prettyf_quote */
  364. /* The q argument is a flag telling whether to quote or not. */
  365. void prettyf_inner(s,q,c)
  366. char *s;
  367. long q;
  368. char c; /* the quote character */
  369. {
  370.   char *sb=buffer;
  371.  
  372.   if (indent) {
  373.     while (*sb) sb++;
  374.     if (q) { *sb = c; sb++; }
  375.     while (*s) {
  376.       if (q && *s==c) { *sb = *s; sb++; }
  377.       *sb = *s; sb++; s++;
  378.     }
  379.     if (q) { *sb = c; sb++; }
  380.     *sb=0;
  381.   }
  382.   else {
  383.     if (q) putc(c,outfile);
  384.     while (*s) {
  385.       if (q && *s==c) { putc(*s,outfile); }
  386.       putc(*s,outfile);
  387.       s++;
  388.     }
  389.     if (q) putc(c,outfile);
  390.   }
  391. }
  392.  
  393.  
  394. /* Return TRUE iff s starts with a non-lowercase character. */
  395. long starts_nonlower(s)
  396. char *s;
  397. {
  398.   return (*s && !LOWER(s[0]));
  399. }
  400.  
  401. /* Return TRUE iff s contains a character that is not alphanumeric. */
  402. long has_non_alpha(s)
  403. char *s;
  404. {
  405.   while (*s) {
  406.     if (!ISALPHA(*s)) return TRUE;
  407.     s++;
  408.   }
  409.   return FALSE;
  410. }
  411.  
  412. /* Return TRUE iff s contains only SYMBOL characters. */
  413. long all_symbol(s)
  414. char *s;
  415. {
  416.   while (*s) {
  417.     if (!SYMBOL(*s)) return FALSE;
  418.     s++;
  419.   }
  420.   return TRUE;
  421. }
  422.  
  423. /* Return TRUE if s represents an integer. */
  424. long is_integer(s)
  425. char *s;
  426. {
  427.   if (!*s) return FALSE;
  428.   if (*s=='-') s++;
  429.   while (*s) {
  430.     if (!DIGIT(*s)) return FALSE;
  431.     s++;
  432.   }
  433.   return TRUE;
  434. }
  435.  
  436. /* Return TRUE if s does not have to be quoted, i.e., */
  437. /* s starts with '_' or a lowercase symbol and contains */
  438. /* all digits, letters, and '_'. */
  439. long no_quote(s)
  440. char *s;
  441. {
  442.   if (!s[0]) return FALSE;
  443.  
  444.   if (s[0]=='%') return FALSE;
  445.   if (SINGLE(s[0]) && s[1]==0) return TRUE;
  446.   if (s[0]=='_'    && s[1]==0) return FALSE;
  447.   if (all_symbol(s)) return TRUE;
  448.  
  449.   if (!LOWER(s[0])) return FALSE;
  450.   s++;
  451.   while (*s) {
  452.     if (!ISALPHA(*s)) return FALSE;
  453.     s++;
  454.   }
  455.   return TRUE;
  456. }
  457.   
  458.  
  459.  
  460. /******** PRETTYF(s)
  461.   This prints the string S into the BUFFER.
  462. */
  463. void prettyf(s)
  464. char *s;
  465. {
  466.   prettyf_inner(s,FALSE,'\'');
  467. }
  468.  
  469.  
  470. void prettyf_quoted_string(s)
  471. char *s;
  472. {
  473.   prettyf_inner(s,const_quote,'"');
  474. }
  475.  
  476.  
  477.  
  478. /****** PRETTYF_QUOTE(s)
  479.   This prints the string S into the buffer.
  480.   S is surrounded by quotes if:
  481.     (1) const_quote==TRUE, and
  482.     (2) S does not represent an integer, and
  483.     (2) S contains a non-alphanumeric character
  484.         or starts with a non-lowercase character, and
  485.     (3) if S is longer than one character, it is not true that S has only
  486.         non-SINGLE SYMBOL characters (in that case, S does not need quotes),and
  487.     (4) if S has only one character, it is a single space or underscore.
  488.   When S is surrounded by quotes, a quote inside S is printed as two quotes.
  489. */
  490. void prettyf_quote(s)
  491. char *s;
  492. {
  493.   prettyf_inner(s, const_quote && !no_quote(s), '\'');
  494. }
  495. /*
  496.                   !is_integer(s) &&
  497.                   (starts_nonlower(s) || has_non_alpha(s)) &&
  498.                   ((int)strlen(s)>1
  499.                   ? !all_symbol(s):
  500.                     ((int)strlen(s)==1
  501.                     ? (s[0]==' ' || s[0]=='_' || UPPER(s[0]) || DIGIT(s[0]))
  502.                     : TRUE
  503.                     )
  504.                   ),
  505.                 '\'');
  506. */
  507.  
  508.  
  509. /******** END_TAB()
  510.   Mark the end of an item.
  511.   Copy the item's string into global space and point to the next item.
  512. */
  513. void end_tab()
  514. {
  515.   if (indent) {
  516.     indx->str=(char *)heap_alloc(strlen(buffer)+1);
  517.     strcpy(indx->str,buffer);
  518.     indx++;
  519.     *buffer=0;
  520.   }
  521. }
  522.  
  523.  
  524.  
  525. /******** MARK_TAB(t)
  526.   Mark a tabbing position T.
  527.   Make the current item point to tabbing position T.
  528. */
  529. void mark_tab(t)
  530. ptr_tab_brk t;
  531. {
  532.   end_tab();
  533.   indx->tab=t;
  534. }
  535.  
  536.  
  537.  
  538. /******** NEW_TAB(t)
  539.   Create a new tabulation mark T.
  540. */
  541. void new_tab(t)
  542. ptr_tab_brk *t;
  543. {
  544.   (*t)=HEAP_ALLOC(tab_brk);
  545.   (*t)->broken=FALSE;
  546.   (*t)->printed=FALSE;
  547.   (*t)->column=0;
  548. }
  549.  
  550.  
  551. /* Utility to correctly handle '\n' inside strings being printed: */
  552. /* What is the column after printing str, when the starting position */
  553. /* is pos? */
  554. /* Same as strlen, except that the length count starts with pos and */
  555. /* \n resets it. */
  556. long strpos(pos, str)
  557. long pos;
  558. char *str;
  559. {
  560.   while (*str) {
  561.     if (str[0]=='\n') pos=0; else pos++;
  562.     str++;
  563.   }
  564.   return pos;
  565. }
  566.  
  567.  
  568. /******** WORK_OUT_LENGTH()
  569.   Calculate the number of blanks before each tabulation.
  570.   Insert line feeds until it all fits into PAGE_WIDTH columns.
  571.   This is done by a trial and error mechanism.
  572. */
  573. void work_out_length()
  574. {
  575.   ptr_item i;
  576.   long done=FALSE;
  577.   long pos;
  578.   ptr_tab_brk worst,root;
  579.   long w;
  580.   
  581.   while(!done) {
  582.     
  583.     pos=0;
  584.     done=TRUE;
  585.     
  586.     w= -1;
  587.     worst=NULL;
  588.     root=NULL;
  589.     
  590.     for(i=pretty_things+1;(unsigned long)i<(unsigned long)indx;i++) {
  591.       
  592.       if(i->tab->broken && i->tab->printed) {
  593.     pos=i->tab->column;
  594.     root=NULL;
  595.       }
  596.       
  597.       if(!i->tab->printed) i->tab->column=pos;
  598.       
  599.       if(!(i->tab->broken))
  600.     if(!root || (root && (root->column)>=(i->tab->column)))
  601.       root=i->tab;
  602.       
  603.       /* pos=pos+strlen(i->str); */
  604.       pos=strpos(pos,i->str);
  605.       i->tab->printed=TRUE;
  606.       
  607.       if(pos>page_width)
  608.     done=FALSE;
  609.       
  610.       if(pos>w) {
  611.     w=pos;
  612.     worst=root;
  613.       }
  614.     }
  615.  
  616.     for(i=pretty_things+1;(unsigned long)i<(unsigned long)indx;i++)
  617.       i->tab->printed=FALSE;
  618.     
  619.     if(!done)      
  620.       if(worst)
  621.     worst->broken=TRUE;
  622.       else
  623.     done=TRUE;
  624.   }
  625. }
  626.  
  627.  
  628.  
  629. /*** RM: Dec 11 1992  (START) ***/
  630.  
  631. /******** COUNT_FEATURES(t)
  632.   Return the number of features of a tree.
  633.   */
  634.  
  635. long count_features(t)
  636.  
  637.      ptr_node t;
  638. {
  639.   long c=0;
  640.   if(t) {
  641.     if(t->left)
  642.       c+=count_features(t->left);
  643.     c++;
  644.     if(t->right)
  645.       c+=count_features(t->right);
  646.   }
  647.   return c;
  648. }
  649.  
  650.  
  651.  
  652. /******** CHECK_LEGAL_CONS(t,t_type)
  653.  
  654.   Check that T is of type T_TYPE, that it has exactly the attributes '1' and
  655.   '2' and that the 2nd is either nil or also long check_legal_cons(t,t_type)
  656. */
  657.  
  658. long check_legal_cons(t,t_type)
  659.      ptr_psi_term t;
  660.      ptr_definition t_type;
  661.      
  662. {
  663.   return (t->type==t_type &&
  664.       count_features(t->attr_list)==2 &&
  665.       find(featcmp,one,t->attr_list) &&
  666.       find(featcmp,two,t->attr_list));
  667. }
  668.  
  669. /*** RM: Dec 11 1992  (END) ***/
  670.  
  671.     
  672.  
  673. /******** PRETTY_LIST(t,depth)
  674.   Pretty print a list.
  675.   On entry we know that T is a legal CONS pair, so we can immediately print
  676.   the opening bracket etc...
  677. */
  678. void pretty_list(t,depth)
  679. ptr_psi_term t;
  680. long depth;
  681. {
  682.   ptr_tab_brk new;
  683.   ptr_list l;
  684.   ptr_definition t_type;
  685.   ptr_psi_term car,cdr;
  686.   ptr_node n,n2;
  687.   char *tag=NULL;
  688.   char colon[2],sep[4],end[3];
  689.   long list_depth; /* 20.8 */
  690.   long done=FALSE; /* RM: Dec 11 1992 */
  691.   
  692.   
  693.   strcpy(sep,"ab");
  694.   strcpy(end,"cd");
  695.   t_type=t->type;
  696.   
  697.   if (overlap_type(t_type,alist)) {
  698.     if (!equal_types(t_type,alist)) {
  699.       pretty_symbol(t_type->keyword);  /*  RM: Jan 13 1993  */
  700.       prettyf(DOTDOT);
  701.     }
  702.     prettyf("[");
  703.     strcpy(sep,",");
  704.     strcpy(end,"]");
  705.   }
  706.  
  707.   /*
  708.     else if (equal_types(t_type,conjunction)) {
  709.       prettyf("(");
  710.       strcpy(sep,DOTDOT);
  711.       strcpy(end,")");
  712.       }
  713.       */
  714.   
  715.   else if (equal_types(t_type,disjunction)) {
  716.     prettyf("{");
  717.     strcpy(sep,";");
  718.     strcpy(end,"}");
  719.   }
  720.  
  721.   
  722.   /* RM: Dec 11 1992  New code for printing lists */
  723.   
  724.   new_tab(&new);
  725.   list_depth=0; /* 20.8 */
  726.   while(!done) {
  727.     mark_tab(new);
  728.     if(list_depth==print_depth)
  729.       prettyf("...");
  730.  
  731.     get_two_args(t->attr_list,&car,&cdr);
  732.     deref_ptr(car);
  733.     deref_ptr(cdr);
  734.  
  735.     
  736.     if(list_depth<print_depth)
  737.       pretty_tag_or_psi_term(car,COMMA_PREC,depth);
  738.     
  739.     /* Determine how to print the CDR */
  740.     n=find(intcmp,cdr,pointer_names);
  741.     
  742.     if(n && n->data) {
  743.       prettyf("|");
  744.       pretty_tag_or_psi_term(cdr,MAX_PRECEDENCE+1,depth);
  745.       done=TRUE;
  746.     }
  747.     else
  748.       if(( /*  RM: Feb  1 1993  */
  749.       (cdr->type==nil && overlap_type(t_type,alist)) ||
  750.       (cdr->type==disj_nil && t_type==disjunction)
  751.       )
  752.       && !cdr->attr_list)
  753.     done=TRUE;
  754.       else
  755.     if(!check_legal_cons(cdr,t_type)) {
  756.       prettyf("|");
  757.       pretty_tag_or_psi_term(cdr,MAX_PRECEDENCE+1,depth);
  758.       done=TRUE;
  759.     }
  760.     else {
  761.       if(list_depth<print_depth)
  762.         prettyf(sep);
  763.       t=cdr;
  764.     }
  765.     
  766.     list_depth++;
  767.   }
  768.   
  769.   prettyf(end);
  770. }
  771.  
  772.  
  773.  
  774. /******** PRETTY_TAG_OR_PSI_TERM(p,depth)
  775.   Print a psi-term, but first precede it with the appropriate TAG. Don't
  776.   reprint the same psi-term twice.
  777. */
  778. void pretty_tag_or_psi_term(p, sprec, depth)
  779. ptr_psi_term p;
  780. long sprec;
  781. long depth;
  782. {
  783.   ptr_node n,n2;
  784.  
  785.   if (p==NULL) {
  786.     prettyf("<VOID>");
  787.     return;
  788.   }
  789.   if (FALSE /*depth>=print_depth*/) { /* 20.8 */
  790.     prettyf("...");
  791.     return;
  792.   }
  793.   deref_ptr(p);
  794.   
  795.   n=find(intcmp,p,pointer_names);
  796.   
  797.   if (n && n->data) {
  798.     if (n->data==(GENERIC)no_name) {
  799.       n->data=unique_name();
  800.       /* sprintf(name,"_%ld%c",++gen_sym_counter,0); */
  801.       /* n->data=(GENERIC)heap_copy_string(name); */
  802.     }
  803.     n2=find(intcmp,p,printed_pointers);
  804.     if(n2==NULL) {
  805.       prettyf(n->data);
  806.       heap_insert(intcmp,p,&printed_pointers,n->data);
  807.       if (!is_top(p)) {
  808.         prettyf(DOTDOT);
  809.         pretty_psi_term(p,COLON_PREC,depth);
  810.       }
  811.     }
  812.     else
  813.       prettyf(n2->data);
  814.   }
  815.   else
  816.     pretty_psi_term(p,sprec,depth);
  817. }
  818.  
  819.  
  820.  
  821. /****************************************************************************/
  822. /* Routines to handle printing of operators. */
  823. /* The main routine is pretty_psi_with_ops, which is called in */
  824. /* pretty_psi_term. */
  825.  
  826.  
  827. /* Check arguments of a potential operator. */
  828. /* Returns existence of arguments 1 and 2 in low two bits of result. */
  829. /* If only argument "1" exists, returns 1. */
  830. /* If only arguments "1" and "2"  exist, returns 3. */
  831. /* Existence of any other arguments causes third bit to be set as well. */
  832. long check_opargs(n)
  833. ptr_node n;
  834. {
  835.   if (n) {
  836.     long f=check_opargs(n->left) | check_opargs(n->right);
  837.     if (!featcmp(n->key,"1")) return 1 | f;
  838.     if (!featcmp(n->key,"2")) return 2 | f;
  839.     return 4 | f;
  840.   }
  841.   else
  842.     return 0;
  843. }
  844.  
  845. #define NOTOP 0
  846. #define INFIX 1
  847. #define PREFIX 2
  848. #define POSTFIX 3
  849.  
  850.  
  851. /* Get information about an operator. */
  852. /* If t is an operator with the correct arguments, return one of     */
  853. /* {INFIX, PREFIX, POSTFIX} and also its precedence and type.        */
  854. /* If t is not an operator, or it has wrong arguments, return NOTOP  */
  855. /* and prec=0.                                                       */
  856. long opcheck(t, prec, type)
  857. ptr_psi_term t;
  858. long *prec;
  859. operator *type;
  860. {
  861.   operator op;
  862.   long result=NOTOP;
  863.   long numarg=check_opargs(t->attr_list);
  864.   ptr_operator_data opdat=t->type->op_data;
  865.  
  866.   *prec=0;
  867.   if (numarg!=1 && numarg!=3) return NOTOP;
  868.   while (opdat) {
  869.     op=opdat->type;
  870.     if (numarg==1) {
  871.       if (op==xf || op==yf) { result=POSTFIX; break; }
  872.       if (op==fx || op==fy) { result=PREFIX; break; }
  873.     }
  874.     if (numarg==3)
  875.       if (op==xfx || op==xfy || op==yfx) { result=INFIX; break; }
  876.     opdat=opdat->next;
  877.   }
  878.   if (opdat==NULL) return NOTOP;
  879.   *prec=opdat->precedence;
  880.   *type=op;
  881.   return result;
  882. }
  883.  
  884.  
  885. /* Write an expression with its operators. */
  886. /* Return TRUE iff the arguments of t are written here (i.e. t was indeed */
  887. /* a valid operator, and is therefore taken care of here).                */
  888. long pretty_psi_with_ops(t,sprec,depth)
  889. ptr_psi_term t;
  890. long sprec;
  891. long depth;
  892. {
  893.   ptr_tab_brk new;
  894.   ptr_psi_term arg1, arg2;
  895.   operator ttype, a1type, a2type;
  896.   long tprec, a1prec, a2prec;
  897.   long tkind, a1kind, a2kind;
  898.   long p1, p2, argswritten;
  899.   long sp; /* surrounding parentheses */
  900.  
  901.   if (write_canon) return FALSE; /* PVR 24.2.94 */
  902.  
  903.   argswritten=TRUE;
  904.   tkind=opcheck(t, &tprec, &ttype);
  905.   sp=(tkind==INFIX||tkind==PREFIX||tkind==POSTFIX) && tprec>=sprec;
  906.   if (sp) prettyf("(");
  907.   if (tkind==INFIX) {
  908.     get_two_args(t->attr_list, &arg1, &arg2);
  909.     deref_ptr(arg1); /* 16.9 */
  910.     deref_ptr(arg2); /* 16.9 */
  911.     a1kind = opcheck(arg1, &a1prec, &a1type);
  912.     a2kind = opcheck(arg2, &a2prec, &a2type);
  913.  
  914.     /* The p1 and p2 flags tell whether to put parens around t's args */
  915.     /* Calculate p1 flag: */
  916.     if      (a1prec>tprec) p1=TRUE;
  917.     else if (a1prec<tprec) p1=FALSE;
  918.     else /* equal priority */
  919.       if (ttype==xfy || ttype==xfx) p1=TRUE;
  920.       else /* yfx */
  921.         if (a1type==yfx || a1type==fx || a1type==fy) p1=FALSE;
  922.         else p1=TRUE;
  923.  
  924.     /* Calculate p2 flag: */
  925.     if      (a2prec>tprec) p2=TRUE;
  926.     else if (a2prec<tprec) p2=FALSE;
  927.     else /* equal priority */
  928.       if (ttype==yfx || ttype==xfx) p2=TRUE;
  929.       else /* xfy */
  930.         if (a2type==xfy || a2type==xf || a2type==yf) p2=FALSE;
  931.         else p2=TRUE;
  932.  
  933.     /* Write the expression */
  934.     if (p1) prettyf("(");
  935.     pretty_tag_or_psi_term(arg1,MAX_PRECEDENCE+1,depth);
  936.     if (p1) prettyf(")");
  937.     if (!p1 && strcmp(t->type->keyword->symbol,",")) {
  938.       prettyf(" ");
  939.     }
  940.     pretty_quote_symbol(t->type->keyword); /*  RM: Jan 13 1993  */
  941.     if (listing_flag && !func_flag &&
  942.         (!strcmp(t->type->keyword->symbol,",") ||
  943.          !strcmp(t->type->keyword->symbol,":-"))) {
  944.       prettyf("\n        ");
  945.     }
  946.     else {
  947.       if (!p2 && strcmp(t->type->keyword->symbol,".")) prettyf(" ");
  948.     }
  949.     if (p2) prettyf("(");
  950.     pretty_tag_or_psi_term(arg2,MAX_PRECEDENCE+1,depth);
  951.     if (p2) prettyf(")");
  952.   }
  953.   else if (tkind==PREFIX) {
  954.     get_two_args(t->attr_list, &arg1, &arg2); /* arg2 does not exist */
  955.     a1kind = opcheck(arg1, &a1prec, &a1type);
  956.  
  957.     /* Calculate p1 flag: */
  958.     if (a1type==fx || a1type==fy) p1=FALSE;
  959.     else p1=(tprec<=a1prec);
  960.  
  961.     pretty_quote_symbol(t->type->keyword);  /*  RM: Jan 13 1993  */
  962.     if (!p1) prettyf(" ");
  963.     if (p1) prettyf("(");
  964.     pretty_tag_or_psi_term(arg1,MAX_PRECEDENCE+1,depth);
  965.     if (p1) prettyf(")");
  966.   }
  967.   else if (tkind==POSTFIX) {
  968.     get_two_args(t->attr_list, &arg1, &arg2); /* arg2 does not exist */
  969.     a1kind = opcheck(arg1, &a1prec, &a1type);
  970.  
  971.     /* Calculate p1 flag: */
  972.     if (a1type==xf || a1type==yf) p1=FALSE;
  973.     else p1=(tprec<=a1prec);
  974.  
  975.     if (p1) prettyf("(");
  976.     pretty_tag_or_psi_term(arg1,MAX_PRECEDENCE+1,depth);
  977.     if (p1) prettyf(")");
  978.     if (!p1) prettyf(" ");
  979.     pretty_quote_symbol(t->type->keyword); /*  RM: Jan 13 1993  */
  980.   }
  981.   else {
  982.     argswritten=FALSE;
  983.   }
  984.   if (sp) prettyf(")");
  985.   return argswritten;
  986. }
  987.  
  988. /****************************************************************************/
  989.  
  990.  
  991. /******** PRETTY_PSI_TERM(t,sprec,depth)  
  992.   Pretty print a psi_term T with sugar for lists.
  993. */
  994. void pretty_psi_term(t,sprec,depth)
  995.      ptr_psi_term t;
  996.      long sprec;
  997.      long depth;
  998. {
  999.   char buf[STRLEN]; /* Big enough for a long number */
  1000.   ptr_residuation r;
  1001.   long argswritten;
  1002.   double fmod();
  1003.   
  1004.   if (t) {
  1005.     deref_ptr(t); /* PVR */
  1006.  
  1007.     /* if (trace) printf("<%d>",t->status); For brunobug.lf PVR 14.2.94 */
  1008.  
  1009.     /*  RM: Feb 12 1993  */
  1010.     if(display_persistent &&
  1011.        (GENERIC)t>heap_pointer)
  1012.       prettyf(" $");
  1013.     
  1014.     if((t->type==alist || t->type==disjunction) && check_legal_cons(t,t->type))
  1015.       pretty_list(t,depth+1); /*  RM: Dec 11 1992  */
  1016.     else
  1017.       if(t->type==nil && !t->attr_list)
  1018.     prettyf("[]");
  1019.       else
  1020.     if(t->type==disj_nil && !t->attr_list) /*  RM: Feb  1 1993  */
  1021.       prettyf("{}");
  1022.     else {
  1023.     argswritten=FALSE;
  1024.     if (t->value) {
  1025. #ifdef CLIFE
  1026.       if(t->type->type==block) {  /* RM 20 Jan 1993 */
  1027.             pretty_block(t);          /* AA 21 Jan 1993 */
  1028.       }
  1029.       else
  1030. #endif /* CLIFE */
  1031.       if (sub_type(t->type,integer)) {
  1032.         /* Print integers in chunks up to the full precision of the REAL */
  1033.         long seg,neg,i;
  1034.         REAL val;
  1035.         char segbuf[100][PRINT_POWER+3];
  1036.         
  1037.         val = *(REAL *)t->value;
  1038.         neg = (val<0.0);
  1039.         if (neg) val = -val;
  1040.         if (val>WL_MAXINT) goto PrintReal;
  1041.         seg=0;
  1042.         while (val>=(double)PRINT_SPLIT) {
  1043.           double tmp;
  1044.           tmp=(REAL)fmod((double)val,(double)PRINT_SPLIT);
  1045.           sprintf(segbuf[seg],seg_format,(unsigned long)tmp);
  1046.           val=floor(val/(double)PRINT_SPLIT);
  1047.           seg++;
  1048.         }
  1049.         sprintf(segbuf[seg],"%s%ld",(neg?"-":""),(unsigned long)val);
  1050.         for (i=seg; i>=0; i--) prettyf(segbuf[i]);
  1051.         if (!equal_types(t->type,integer)) {
  1052.           prettyf(DOTDOT);
  1053.           pretty_symbol(t->type->keyword); /*  RM: Jan 13 1993  */
  1054.         }
  1055.       }
  1056.       else if (sub_type(t->type,real)) {
  1057.       PrintReal:
  1058.         sprintf(buf,"%lg",*(REAL *)t->value);
  1059.         prettyf(buf);
  1060.         if (!equal_types(t->type,real) &&
  1061.         !equal_types(t->type,integer)) {
  1062.           prettyf(DOTDOT);
  1063.           pretty_symbol(t->type->keyword); /*  RM: Jan 13 1993  */
  1064.         }
  1065.       }
  1066.       else if (sub_type(t->type,quoted_string)) {
  1067.         prettyf_quoted_string(t->value);
  1068.         if(!equal_types(t->type,quoted_string)) {
  1069.           prettyf(DOTDOT);
  1070.           pretty_quote_symbol(t->type->keyword); /*  RM: Jan 13 1993  */
  1071.         }
  1072.       }
  1073.       /* DENYS: BYTEDATA */
  1074.       else if (sub_type(t->type,sys_bytedata)) {
  1075.         pretty_quote_symbol(t->type->keyword);
  1076.       }
  1077.       else if (equal_types(t->type,stream)) {
  1078.         sprintf(buf,"stream(%ld)",t->value);
  1079.         prettyf(buf);
  1080.       }
  1081.       else if (equal_types(t->type,eof))
  1082.         pretty_quote_symbol(eof->keyword); /*  RM: Jan 13 1993  */
  1083.       else if (equal_types(t->type,cut))
  1084.         pretty_quote_symbol(cut->keyword); /*  RM: Jan 13 1993  */
  1085.       else {
  1086.         prettyf("*** bad object '");
  1087.         pretty_symbol(t->type->keyword); /*  RM: Jan 13 1993  */
  1088.         prettyf("'***");
  1089.       }
  1090.     }
  1091.     else {
  1092.       if (depth<print_depth) /* 20.8 */
  1093.         argswritten=pretty_psi_with_ops(t,sprec,depth+1);
  1094.       /*  RM: Jan 13 1993  */
  1095.       if (!argswritten) pretty_quote_symbol(t->type->keyword);
  1096.     }
  1097.     
  1098.     /* write_canon -- PVR 24.2.94 */
  1099.     if (!argswritten && t->attr_list &&
  1100.          (depth<print_depth || write_canon)) /* 20.8 */
  1101.       pretty_attr(t->attr_list,depth+1);
  1102.     
  1103.     if (depth>=print_depth && !write_canon && t->attr_list) /* 20.8 */
  1104.       prettyf("(...)");
  1105.       }
  1106.     if (r=t->resid)
  1107.       while (r) {
  1108.     if (r->goal->pending) {
  1109.           if (FALSE /* write_resids 11.8 */) {
  1110.         prettyf("\\");
  1111.         pretty_psi_term(r->goal->a,0,depth);
  1112.           }
  1113.           else
  1114.         prettyf("~");
  1115.     }
  1116.     r=r->next;
  1117.       }
  1118.   }
  1119. }
  1120.  
  1121.  
  1122.  
  1123. /******** DO_PRETTY_ATTR(t,tab,cnt,depth)
  1124.   Pretty print the attribute tree T at position TAB.
  1125.  
  1126.   CNT is what the value of the first integer label should be, so that
  1127.   "p(1=>a,2=>b)" is printed "p(a,b)"
  1128.   but
  1129.   "p(2=>a,3=>b)" is printed as "p(2 => a,3 => b)".
  1130. */
  1131. void do_pretty_attr(t,tab,cnt,two,depth)
  1132. ptr_node t;
  1133. ptr_tab_brk tab;
  1134. long *cnt;
  1135. long two;
  1136. long depth;
  1137. {
  1138.   long v;
  1139.   /* char *s="nnn"; 18.5 */
  1140.   char s[4];
  1141.   ptr_module module;
  1142.  
  1143.   
  1144.   if (t) {
  1145.     if (t->left) {
  1146.       do_pretty_attr(t->left,tab,cnt,two,depth);
  1147.       prettyf(",");
  1148.     }
  1149.     
  1150.     /* Don't start each argument on a new line, */
  1151.     /* unless printing a function body: */
  1152.     mark_tab(tab);
  1153.     
  1154.     v=str_to_int(t->key);
  1155.     if (v<0) {
  1156.       if(display_modules) { /*  RM: Jan 21 1993  */
  1157.     module=extract_module_from_name(t->key);
  1158.     if(module) {
  1159.       prettyf(module->module_name);
  1160.       prettyf("#");
  1161.     }
  1162.       }
  1163.       prettyf_quote(strip_module_name(t->key));
  1164.  
  1165.       prettyf(" => ");
  1166.     }
  1167.     else if (v== *cnt)
  1168.       (*cnt)++ ;
  1169.     else {
  1170.       sprintf(s,"%ld",v);
  1171.       prettyf(s); /* 6.10 */
  1172.       prettyf(" => ");
  1173.     }
  1174.     
  1175.     /* pretty_tag_or_psi_term(t->data,(two?COMMA_PREC:MAX_PRECEDENCE+1)); */
  1176.     pretty_tag_or_psi_term(t->data,COMMA_PREC,depth);
  1177.     
  1178.     if (t->right) {
  1179.       prettyf(",");
  1180.       do_pretty_attr(t->right,tab,cnt,two,depth);
  1181.     }
  1182.   }
  1183. }
  1184.  
  1185.  
  1186. /* Return true if number of attributes is greater than 1 */
  1187. long two_or_more(t)
  1188. ptr_node t;
  1189. {
  1190.   if (t) {
  1191.     if (t->left || t->right) return TRUE; else return FALSE;
  1192.   }
  1193.   else
  1194.     return FALSE;
  1195. }
  1196.  
  1197.  
  1198. /******** PRETTY_ATTR(t,depth)
  1199.   Pretty print the attributes. This calls DO_PRETTY_ATTR which does the real
  1200.   work.
  1201. */
  1202. void pretty_attr(t,depth)
  1203. ptr_node t;
  1204. long depth;
  1205. {
  1206.   ptr_tab_brk new;
  1207.   long cnt=1;
  1208.  
  1209.   prettyf("(");
  1210.   new_tab(&new);
  1211.  
  1212.   do_pretty_attr(t,new,&cnt,two_or_more(t),depth);
  1213.  
  1214.   prettyf(")");
  1215. }
  1216.  
  1217.  
  1218.  
  1219. /******** PRETTY_OUTPUT()
  1220.   Final output of all these pretty things which have been built up.
  1221. */
  1222. void pretty_output()
  1223. {
  1224.   ptr_item i;
  1225.   long j;
  1226.   
  1227.   for(i=pretty_things+1;(unsigned long)i<(unsigned long)indx;i++) {
  1228.     if(i->tab->broken && i->tab->printed) {
  1229.       fprintf(outfile,"\n");
  1230.       for(j=0;j<i->tab->column;j++)
  1231.     fprintf(outfile," ");
  1232.     }
  1233.     fprintf(outfile,"%s",i->str);
  1234.     i->tab->printed=TRUE;
  1235.   }
  1236. }
  1237.  
  1238.  
  1239.  
  1240.  
  1241. /******** PRETTY_VARIABLES(n,tab)
  1242.   Pretty print the variables at position TAB.
  1243. */
  1244. void pretty_variables(n,tab)
  1245. ptr_node n;
  1246. ptr_tab_brk tab;
  1247. {
  1248.   ptr_psi_term tok;
  1249.   ptr_node n2;
  1250.   
  1251.   if(n->left) {
  1252.     pretty_variables(n->left,tab);
  1253.     prettyf(", ");
  1254.   }
  1255.  
  1256.   mark_tab(tab);
  1257.   prettyf(n->key);
  1258.   prettyf(" = ");
  1259.  
  1260.   tok=(ptr_psi_term )n->data;
  1261.   deref_ptr(tok);
  1262.   n2=find(intcmp,tok,printed_pointers);
  1263.   if(strcmp((char *)n2->data,n->key)<0)
  1264.     /* Reference to previously printed variable */
  1265.     prettyf(n2->data);
  1266.   else {
  1267.     if (eqsym->op_data) {
  1268.       long tkind, tprec, ttype, eqprec;
  1269.       eqprec=eqsym->op_data->precedence;
  1270.       tkind=opcheck(tok, &tprec, &ttype);
  1271.       if (tprec>=eqprec) prettyf("(");
  1272.       pretty_psi_term(tok,MAX_PRECEDENCE+1,0);
  1273.       if (tprec>=eqprec) prettyf(")");
  1274.     }
  1275.     else
  1276.       pretty_psi_term(tok,MAX_PRECEDENCE+1,0);
  1277.   }
  1278.   
  1279.   if(n->right) {
  1280.     prettyf(", ");
  1281.     pretty_variables(n->right,tab);
  1282.   }
  1283. }
  1284.  
  1285.  
  1286.  
  1287. /******** PRINT_VARIABLES
  1288.   This prints all the query variables.
  1289.   Symbols generated to print one variable are coherent with those used in
  1290.   other variables.
  1291.   Returns TRUE iff the set of query variables is nonempty.
  1292. */
  1293.  
  1294. long print_variables(printflag)
  1295.      
  1296.      long printflag;
  1297. {
  1298.   ptr_tab_brk new;
  1299.   GENERIC old_heap_pointer;
  1300.   
  1301.   if (!printflag) return FALSE; /* 21.1 */
  1302.   
  1303.   outfile=output_stream;
  1304.   listing_flag=FALSE;
  1305.   old_heap_pointer=heap_pointer;
  1306.   
  1307.   pointer_names=NULL;
  1308.   printed_pointers=NULL;
  1309.   gen_sym_counter=0;
  1310.   go_through_tree(var_tree);
  1311.   insert_variables(var_tree,TRUE);
  1312.   forbid_variables(var_tree);
  1313.   
  1314.   indent=TRUE;
  1315.   const_quote=TRUE;
  1316.   write_resids=TRUE;
  1317.   write_canon=FALSE;
  1318.   *buffer=0;
  1319.   indx=pretty_things;
  1320.  
  1321.   if (var_tree) {
  1322.     new_tab(&new);
  1323.     pretty_variables(var_tree,new);
  1324.     prettyf(".");
  1325.     mark_tab(new);
  1326.     prettyf("\n");
  1327.     end_tab();
  1328.  
  1329.     if (indent) {
  1330.       work_out_length();
  1331.       pretty_output();
  1332.     }
  1333.   }
  1334.   heap_pointer=old_heap_pointer;
  1335.   return (var_tree!=NULL);
  1336. }
  1337.  
  1338.  
  1339.  
  1340. /******** WRITE_ATTRIBUTES(n)
  1341.   Used by all versions of the built-in predicate write,
  1342.   and by the built-in predicate listing.
  1343. */
  1344. void write_attributes(n,tab)
  1345. ptr_node n;
  1346. ptr_tab_brk tab;
  1347. {
  1348.   if(n) {
  1349.     write_attributes(n->left,tab);
  1350.     mark_tab(tab);
  1351.     pretty_tag_or_psi_term(n->data,MAX_PRECEDENCE+1,0);
  1352.     write_attributes(n->right,tab);
  1353.   }
  1354. }
  1355.  
  1356.  
  1357. /******** PRED_WRITE(n)
  1358.   N is an attribute tree to be printed in one lump. This is called by WRITE.
  1359. */
  1360.  
  1361. void main_pred_write();
  1362.  
  1363. /* For the listing built-in */
  1364. void listing_pred_write(n,fflag)
  1365. ptr_node n;
  1366. long fflag;
  1367. {
  1368.   long old_print_depth;
  1369.  
  1370.   listing_flag=TRUE;
  1371.   func_flag=fflag;
  1372.   indent=TRUE;
  1373.   const_quote=TRUE;
  1374.   write_corefs=TRUE;
  1375.   write_stderr=FALSE;
  1376.   write_resids=FALSE;
  1377.   write_canon=FALSE;
  1378.   outfile=output_stream;
  1379.   old_print_depth=print_depth;
  1380.   print_depth=PRINT_DEPTH;
  1381.   main_pred_write(n);
  1382.   print_depth=old_print_depth;
  1383.   fflush(outfile);
  1384. }
  1385.  
  1386. /* For all write builtins */
  1387. /* I.e: write, writeq, pretty_write, pretty_writeq, write_err, writeq_err. */
  1388. void pred_write(n)
  1389. ptr_node n;
  1390. {
  1391.   listing_flag=FALSE;
  1392.   /* write_stderr=FALSE; */
  1393.   outfile=(write_stderr?stderr:output_stream);
  1394.   main_pred_write(n);
  1395.   fflush(outfile);
  1396. }
  1397.  
  1398. void main_pred_write(n)
  1399. ptr_node n;
  1400. {
  1401.   if (n) {
  1402.     GENERIC old_heap_pointer;
  1403.     ptr_tab_brk new;
  1404.  
  1405.     if (!write_corefs) main_pred_write(n->left);
  1406.  
  1407.     old_heap_pointer=heap_pointer;
  1408.     pointer_names=NULL;
  1409.     printed_pointers=NULL;
  1410.     gen_sym_counter=0;
  1411.     if (write_corefs)
  1412.       go_through_tree(n);
  1413.     else
  1414.       check_pointer((ptr_psi_term)n->data);
  1415.     insert_variables(var_tree,FALSE);
  1416.  
  1417.     *buffer=0;
  1418.     
  1419.     indx=pretty_things;
  1420.     new_tab(&new);
  1421.  
  1422.     if (write_corefs) {
  1423.       write_attributes(n,new);
  1424.     }
  1425.     else {
  1426.       mark_tab(new);
  1427.       pretty_tag_or_psi_term(n->data,MAX_PRECEDENCE+1,0);
  1428.     }
  1429.  
  1430.     end_tab();
  1431.  
  1432.     if (indent) {
  1433.       work_out_length();
  1434.       pretty_output();
  1435.     }
  1436.     
  1437.     heap_pointer=old_heap_pointer;
  1438.  
  1439.     if (!write_corefs) main_pred_write(n->right);
  1440.   }
  1441. }
  1442.  
  1443.  
  1444. void main_display_psi_term(); /* Forward declaration */
  1445.  
  1446.  
  1447. /******** DISPLAY_PSI_STDOUT(t)
  1448.   Print the psi_term T to stdout as simply as possible (no indenting).
  1449. */
  1450. void display_psi_stdout(t)
  1451. ptr_psi_term t;
  1452. {
  1453.   outfile=stdout;
  1454.   main_display_psi_term(t);
  1455. }
  1456.  
  1457.  
  1458. /******** DISPLAY_PSI_STDERR(t)
  1459.   Print the psi_term T to stderr as simply as possible (no indenting).
  1460. */
  1461. void display_psi_stderr(t)
  1462. ptr_psi_term t;
  1463. {
  1464.   outfile=stderr;
  1465.   main_display_psi_term(t);
  1466. }
  1467.  
  1468.  
  1469. /******** DISPLAY_PSI_STREAM(t)
  1470.   Print the psi_term T to output_stream as simply as possible (no indenting).
  1471. */
  1472. void display_psi_stream(t)
  1473. ptr_psi_term t;
  1474. {
  1475.   outfile=output_stream;
  1476.   main_display_psi_term(t);
  1477. }
  1478.  
  1479.  
  1480. /******** DISPLAY_PSI(stream,t)
  1481.   Print the psi_term T to the given stream.
  1482. */
  1483. void display_psi(s,t)
  1484. FILE *s;
  1485. ptr_psi_term t;
  1486. {
  1487.   outfile=s;
  1488.   main_display_psi_term(t);
  1489. }
  1490.  
  1491.  
  1492. /* Main loop for previous two entry points */
  1493. void main_display_psi_term(t)
  1494. ptr_psi_term t;
  1495. {
  1496.   GENERIC old_heap_pointer;
  1497.   ptr_tab_brk new;
  1498.  
  1499.   listing_flag=FALSE;
  1500.   if(t) {
  1501.  
  1502.     deref_ptr(t);
  1503.     
  1504.     old_heap_pointer=heap_pointer;
  1505.     pointer_names=NULL;
  1506.     printed_pointers=NULL;
  1507.     gen_sym_counter=0;
  1508.     go_through(t);
  1509.     insert_variables(var_tree,FALSE);
  1510.     
  1511.     indent=FALSE;
  1512.     const_quote=TRUE;
  1513.     write_resids=FALSE;
  1514.     write_canon=FALSE;
  1515.     *buffer=0;
  1516.     indx=pretty_things;
  1517.  
  1518.     new_tab(&new);
  1519.     mark_tab(new);
  1520.     pretty_tag_or_psi_term(t,MAX_PRECEDENCE+1,0);
  1521.     end_tab();
  1522.     if (indent) {
  1523.       work_out_length();
  1524.       pretty_output();
  1525.     }
  1526.     
  1527.     heap_pointer=old_heap_pointer;
  1528.   }
  1529.   else
  1530.     printf("*null psi_term*");
  1531. }
  1532.  
  1533.  
  1534.  
  1535. /******** DISPLAY_COUPLE(u,s,v)
  1536.   Print a couple of psi-terms (u,v) with the correct co-referencing. Print
  1537.   string S in between.
  1538. */
  1539. void display_couple(u,s,v)
  1540. ptr_psi_term u;
  1541. char *s;
  1542. ptr_psi_term v;
  1543. {
  1544.   GENERIC old_heap_pointer;
  1545.   ptr_tab_brk new;
  1546.  
  1547.   output_stream=stdout;
  1548.   listing_flag=FALSE;
  1549.   old_heap_pointer=heap_pointer;
  1550.   
  1551.   pointer_names=NULL;
  1552.   printed_pointers=NULL;
  1553.   gen_sym_counter=0;
  1554.   check_pointer(u);
  1555.   check_pointer(v);
  1556.   insert_variables(var_tree,TRUE);
  1557.   
  1558.   indent=FALSE;
  1559.   const_quote=TRUE;
  1560.   write_resids=FALSE;
  1561.   write_canon=FALSE;
  1562.   *buffer=0;
  1563.   indx=pretty_things;
  1564.   new_tab(&new);
  1565.   mark_tab(new);
  1566.   pretty_tag_or_psi_term(u,MAX_PRECEDENCE+1,0);
  1567.   prettyf(s);
  1568.   pretty_tag_or_psi_term(v,MAX_PRECEDENCE+1,0);
  1569.   end_tab();
  1570.  
  1571.   if (indent) {
  1572.     work_out_length();
  1573.     pretty_output();
  1574.   }
  1575.   
  1576.   heap_pointer=old_heap_pointer;
  1577. }
  1578.  
  1579.  
  1580.  
  1581. /******** PRINT_RESID_MESSAGE
  1582.   This is called in trace mode to print the residuated goal along with the
  1583.   RV set.
  1584. */
  1585. void print_resid_message(t,r)
  1586. ptr_psi_term t;
  1587. ptr_resid_list r; /* 21.9 */
  1588. {
  1589.   GENERIC old_heap_pointer;
  1590.   ptr_tab_brk new;
  1591.   ptr_resid_list r2; /* 21.9 */
  1592.   
  1593.   outfile=stdout;
  1594.   listing_flag=FALSE;
  1595.   old_heap_pointer=heap_pointer;
  1596.   
  1597.   pointer_names=NULL;
  1598.   printed_pointers=NULL;
  1599.   gen_sym_counter=0;
  1600.  
  1601.   check_pointer(t);
  1602.  
  1603.   r2=r;
  1604.   while(r2) {
  1605.     check_pointer(r2->var);
  1606.     r2=r2->next;
  1607.   }
  1608.   
  1609.   insert_variables(var_tree,TRUE);
  1610.   
  1611.   indent=FALSE;
  1612.   const_quote=TRUE;
  1613.   write_resids=FALSE;
  1614.   write_canon=FALSE;
  1615.   *buffer=0;
  1616.   indx=pretty_things;
  1617.   new_tab(&new);
  1618.   mark_tab(new);
  1619.  
  1620.   prettyf("residuating ");
  1621.   pretty_tag_or_psi_term(t,MAX_PRECEDENCE+1,0);
  1622.   prettyf(" on variable(s) {");
  1623.  
  1624.   r2=r;
  1625.   while(r2) {
  1626.     pretty_tag_or_psi_term(r2->var,MAX_PRECEDENCE+1,0);
  1627.     r2=r2->next;
  1628.     if(r2)
  1629.       prettyf(",");
  1630.   }
  1631.  
  1632.   prettyf("}\n");
  1633.   end_tab();
  1634.   
  1635.   heap_pointer=old_heap_pointer;
  1636. }
  1637.