home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd3.lzh / SBPROLOG2.2 / SIM / io.c < prev    next >
Text File  |  1991-08-10  |  4KB  |  134 lines

  1. /************************************************************************
  2. *                                    *
  3. *    The SB-Prolog System                        *
  4. *    Copyright SUNY at Stony Brook, 1986                *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /* io.c */
  26.  
  27. #include "sim.h"
  28. #include "aux.h"
  29.  
  30. writepname(file, name_ptr, length)
  31. FILE *file;
  32. char           *name_ptr;
  33. unsigned short length;
  34. {
  35.    int i, slen;
  36.    char ch;
  37.  
  38.    slen = length>80 ? 80 : length; /* trim to 80 characters */
  39.    for ( i = 1; i <= slen; ++i ) {
  40.     ch = *(name_ptr++);
  41.     putc((ch<' ' ? ' ' : ch), file);    /* nonprintables to blanks */
  42.     }
  43.    /* fflush(file); */
  44. }
  45.  
  46. writeqname(file, name_ptr, length)
  47. FILE *file;
  48. char           *name_ptr;
  49. unsigned short length;
  50. {
  51.    int i, need_to_quote;
  52.  
  53.    need_to_quote = 0;
  54.    for (i=0; i<length; ++i)
  55.        if ( (*(name_ptr+i)<'0') ||
  56.          (*(name_ptr+i)>'9' && *(name_ptr+i)<'A') ||
  57.          (*(name_ptr+i)>'Z' && *(name_ptr+i)<'a') ||
  58.          (*(name_ptr+i)>'z')
  59.        ) need_to_quote = 1;
  60.    if (need_to_quote) fprintf(file, "'");
  61.    for ( i = 1; i <= length; ++i ) {
  62.        if (need_to_quote && (*name_ptr == '\'')) fprintf(file, "'");
  63.         putc(*(name_ptr++), file);
  64.    }
  65.    if (need_to_quote) fprintf(file, "'");
  66.    /* fflush(file); */
  67. }
  68.  
  69. printterm(term, car)
  70.     word term;
  71.     byte car;
  72. {
  73.     register pw top;
  74.     unsigned short i, arity1;
  75.     struct psc_rec *psc_ptr1;
  76.  
  77.     ptd: switch((int)(term&3)) {
  78.     case FREE:
  79.         nderef(term, ptd);
  80.         printf("_%x", untag(term));
  81.         return;
  82.        case CS:
  83.         psc_ptr1 = get_str_psc(term);
  84.         if (get_etype(psc_ptr1) == T_BUFF) {
  85.            printf("Buffer_%x", get_name(psc_ptr1));
  86.            } 
  87.         else writepname(stdout, get_name(psc_ptr1), get_length(psc_ptr1));
  88.         if ( get_arity(psc_ptr1) == 0 ) return;   /* constant */
  89.         /* structure */
  90.         printf("(");
  91.         arity1 = ( get_arity(psc_ptr1) );
  92.         untag(term);
  93.         for ( i = 1; i <= arity1; i++ ) {
  94.         printterm(term += 4, CAR);
  95.                    if (i<arity1 /*-1*/ ) printf(",");
  96.            }
  97.            printf(")");
  98.                /* fflush(stdout); */
  99.                return;
  100.     case NUM:
  101.         if (isinteger(term)) printf("%d", intval(term));
  102.         else printf("%f", floatval(term));
  103.         return;
  104.     case LIST:
  105.         untag(term);
  106.         if ( car ) printf("[");
  107.         printterm(follow(term), CAR);
  108.         term = follow(term+4);
  109.         ldp: switch((int)(term&3)) {
  110.         case FREE:
  111.             nderef(term, ldp);
  112.             goto vertbar;
  113.         case LIST:
  114.             printf(",");
  115.             printterm(term, CDR);
  116.             return;
  117.         case CS:
  118.             if (isnil(term)) {    /* term is the 'nil' constant */
  119.             printf("]");
  120.             /* fflush(stdout); */
  121.             return;
  122.             }     /* else fall through to the vert bar case */
  123.            case NUM:
  124.             vertbar:
  125.             printf("|");
  126.             printterm(term, CAR);
  127.             printf("]");
  128.             /* fflush(stdout); */
  129.             return;
  130.        }
  131.    }
  132. }
  133.  
  134.