home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 355_02 / slk2.exe / SPP / PR.C < prev    next >
C/C++ Source or Header  |  1991-06-09  |  4KB  |  207 lines

  1. /*
  2.     New Sherlock Preprocessor -- printing routines for debugging.
  3.  
  4.     source:  pr.c
  5.     started: May 19, 1986
  6.     version:
  7.         July 20, 1988
  8.         February 16, 1989
  9.             change the output of pr_tok() so that it is more
  10.             suitable for user error messages.
  11.  
  12.  
  13.     PUBLIC DOMAIN SOFTWARE
  14.  
  15.     Sherlock, including the SPP, SDEL and SDIF programs, was placed in
  16.     the public domain on June 15, 1991, by its author,
  17.  
  18.         Edward K. Ream
  19.         166 North Prospect Ave.
  20.         Madison, WI 53705.
  21.         (608) 257-0802
  22.  
  23.     Sherlock may be used for any commercial or non-commercial purpose.
  24.  
  25.  
  26.     DISCLAIMER OF WARRANTIES
  27.  
  28.     Edward K. Ream (Ream) specifically disclaims all warranties,
  29.     expressed or implied, with respect to this computer software,
  30.     including but not limited to implied warranties of merchantability
  31.     and fitness for a particular purpose.  In no event shall Ream be
  32.     liable for any loss of profit or any commercial damage, including
  33.     but not limited to special, incidental consequential or other damages.
  34. */
  35.  
  36. #include "spp.h"
  37.  
  38. /*
  39.     WARNING!!  These two tables must change if enum.h changes.
  40. */
  41. static char * scope_tab [] = {
  42.     "NULL_SCOPE", "OUTER_SCOPE", "OLD_FORMAL_SCOPE",
  43.     "NEW_FORMAL_SCOPE", "STRUCT_SCOPE", "BLOCK_SCOPE"
  44. };
  45.  
  46. static char * FAR tok_tab [] = {
  47.     "<NULL>", "<ERROR>",
  48.  
  49.     "auto", "char", "const", "double", "enum", "extern",
  50.     "float", "int", "long", "noalias", "register", "short",
  51.     "static", "typedef", "signed", "struct", "union",
  52.     "unsigned", "void", "volatile",
  53.  
  54.     "break", "case",
  55.     "continue", "default", "do", "else", "for",
  56.     "goto",    "if", "return", "switch", "while",
  57.  
  58.     "entry", "sizeof",
  59.  
  60.     ",", "<newline>", ";", "[", "{", "(", "]", "}", ")",
  61.  
  62.     ":", "?",
  63.  
  64.     "ARRAY", "->", ".", "&&", "||", ",",
  65.  
  66.     "=",
  67.     "&=", "/=", "<<=", "-=", "%=",
  68.     "|=", "+=", ">>=", "*=", "^=",
  69.  
  70.     "&", "|", "+", "*", "^",
  71.     "/", "<<", "-", "%", ">>",
  72.  
  73.     "==", ">=", ">", "<=", "<", "!=",
  74.  
  75.     "--", "++", "!", "~",
  76.  
  77.     "(cast)", "post--", "post++", "pre--", "pre++",
  78.     "&", "-", "+", "*", "call",
  79.     
  80.     "character", "end-of-file",
  81.     "identifier", "integer", "float", "long integer",
  82.     "string",
  83.     "unsigned long", "long double",
  84.     "...", "label", "bool"
  85. };
  86.  
  87. /*
  88.     Return the print string of a bool.
  89. */
  90. char *
  91. pr_bool(flag)
  92. bool flag;
  93. {
  94.     return flag ? "TRUE" : "FALSE";
  95. }
  96.  
  97. /*
  98.     Return the print string of a character.
  99. */
  100. static char pdefault[] = "@";
  101.  
  102. char *
  103. pr_ch(c)
  104. int c;
  105. {
  106.     switch (c) {
  107.     case ARG_FLAG:        return "ARG:";
  108.     case POUND_FLAG:    return "POUND:";
  109.     case CONCAT_FLAG:    return "<CONCAT_FLAG>";
  110.     case EORT:        return "<EORT>";
  111.     case EXPAND_OFF:    return "<EXPAND_OFF>";
  112.     case '\0':        return "<\\0>";
  113.     case '\t':        return "<TAB>";
  114.     case '\n':        return "<NL>";
  115.     case '\r':        return "<CR>";
  116.     case '\a':        return "<ALERT>";
  117.     case ' ':        return "< >";
  118.     case END_FILE:        return "<END_FILE>";
  119.     default:
  120.         pdefault[0] = c;
  121.         pdefault[1] = '\0';
  122.         return &pdefault[0];
  123.     }
  124. }
  125.  
  126. /*
  127.     Return the print string of tok.
  128.     Do not assume that t_symbol[] refers to tok.
  129. */
  130. char *
  131. pr_op(tok)
  132. en_tokens tok;
  133. {
  134.     if ((int)tok >= 0 && (int)tok <= LAST_ENUM_TOK) {
  135.         return tok_tab [(int)tok];
  136.     }
  137.     else {
  138.         return "BAD TOKEN";
  139.     }
  140. }
  141.  
  142. char *
  143. pr_scope(en_scope scope)
  144. {
  145.     return scope_tab [scope];
  146. }
  147. /*
  148.     Return the print string of a string.
  149. */
  150. static char pr_buf [1000];
  151.  
  152. char *
  153. pr_str(s)
  154. char *s;
  155. {
  156.     strcpy(pr_buf, "");
  157.     while (*s) {
  158.         strcat(pr_buf, pr_ch(*s));
  159.         s++;
  160.     }
  161.     return &pr_buf[0];
  162. }
  163.  
  164. /*
  165.     Return the print string of the current token.
  166.     Assume the t_symbol refers to the global token.
  167. */
  168. static char buffer [200];
  169. static char buffer2 [100];
  170.  
  171. char *
  172. pr_tok()
  173. {
  174.     if (is(ID_TOK)) {
  175.         if (t_length >= 200-5) {
  176.             printf("truncated identifier: %s\n", t_symbol);
  177.             exit(1);
  178.             return "";
  179.         }
  180.         else {
  181.             strcpy(buffer, t_symbol);
  182.             return buffer;
  183.         }
  184.     }
  185.     else if (is(STRING_TOK)) {
  186.         if (t_length >= 200-5) {
  187.             printf("truncated string: %s\n", t_symbol);
  188.             exit(1);
  189.             return "";
  190.         }
  191.         else {
  192.             strcpy(buffer, t_symbol);
  193.             return buffer;
  194.         }
  195.     }
  196.     else if (is(INT_TOK)) {        
  197.             ltoa(t_value, buffer2, 10);
  198.             return buffer2;
  199.     }
  200.     else if (token < LABEL_TOK) {
  201.         return  tok_tab [(int)token];
  202.     }
  203.     else {
  204.         return "BAD TOKEN";
  205.     }
  206. }
  207.