"Fossies" - the Fresh Open Source Software Archive

Member "ncc-2.8/cexpand.C" (11 Oct 2008, 4362 Bytes) of package /linux/privat/old/ncc-2.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /******************************************************************************
    2 $   Expands tokens from the normalized C source to readable strings,
    3 $   and other similar routines representing internal compiler data.
    4 $   The inverse of lexical analyser
    5 ******************************************************************************/
    6 
    7 #include <stdio.h>
    8 #include <stdlib.h>
    9 #include <string.h>
   10 
   11 #include "global.h"
   12 
   13 int cfile_of (NormPtr p)
   14 {
   15     int s = 0, e = C_Nfiles, m;
   16 
   17     while (e - s > 1)
   18         if (C_Files [m = (e + s) / 2].indx < p)
   19             s = m;
   20         else e = m;
   21     return s;
   22 }
   23 
   24 int cline_of (NormPtr p)
   25 {
   26     int s = 0, e = C_Nlines, m;
   27 
   28     while (e - s > 1)
   29         if (C_Lines [m = (e + s) / 2].ftok < p)
   30             s = m;
   31         else e = m;
   32     return C_Lines [s].line;
   33 }
   34 
   35 char *in_file (NormPtr p)
   36 {
   37     return C_Files [cfile_of (p)].file;
   38 }
   39 
   40 #define rcase(x) case RESERVED_ ## x: strcpy (tmp, #x); break
   41 #define ocase(x, y) case x: strcpy (tmp, y); break
   42 
   43 char *expand (int token)
   44 {
   45 static  char tmp [128];
   46     if (token < 0) return "internal BUG";
   47     if (ISSYMBOL (token)) {
   48         if (SYMBOLID (token) > C_Nsyms)
   49             return strcpy (tmp, "***Fatal Flaw**");
   50         return strcpy (tmp, C_Syms [SYMBOLID (token)]);
   51     }
   52     if (ISSTRING (token))
   53         snprintf (tmp, sizeof tmp, "\"%s\"", C_Strings [token - STRINGBASE]);
   54     else if (ISNUMBER (token)) {
   55         if (token >= INUMBER)
   56             sprintf (tmp, "%i", token - INUMBER);
   57         else if (token >= FLOATBASE)
   58             sprintf (tmp, "%e", C_Floats [token - FLOATBASE]);
   59         else if (token >= UINT32BASE)
   60             sprintf (tmp, "%lu", C_Unsigned [token - UINT32BASE]);
   61         else if (token >= INT32BASE)
   62             sprintf (tmp, "%li", C_Ints [token - INT32BASE]);
   63         else if (token >= INT16BASE)
   64             sprintf (tmp, "%i", C_Shortints [token - INT16BASE]);
   65         else sprintf (tmp, "%i", C_Chars [token - INT8BASE]);
   66     } else if (token < 127)
   67         sprintf (tmp, "%c", token);
   68     else if (ISOPERATOR(token)) switch (token) {
   69         ocase (ELLIPSIS, "...");
   70         ocase (POINTSAT, "->");
   71         ocase (MINUSMINUS, "--");
   72         ocase (ASSIGNA, "+=");
   73         ocase (ASSIGNS, "-=");
   74         ocase (ASSIGNM, "*=");
   75         ocase (ASSIGND, "/=");
   76         ocase (ASSIGNR, "%=");
   77         ocase (ASSIGNBA, "&=");
   78         ocase (ASSIGNBO, "|=");
   79         ocase (ASSIGNBX, "^=");
   80         ocase (ASSIGNRS, ">>=");
   81         ocase (ASSIGNLS, "<<=");
   82         ocase (PLUSPLUS, "++");
   83         ocase (GEQCMP, ">=");
   84         ocase (LSH, "<<");
   85         ocase (OROR, "||");
   86         ocase (ANDAND, "&&");
   87         ocase (EQCMP, "==");
   88         ocase (NEQCMP, "!=");
   89         ocase (RSH, ">>");
   90         ocase (LEQCMP, "<=");
   91     default: strcpy (tmp, "n/A\n");
   92     } else switch (token) {
   93         rcase (case);
   94         ocase (FORCEERROR, "//-*-* END OF THE FILE *-*-");
   95         rcase (auto);
   96         rcase (__asm__);
   97         rcase (const);
   98         rcase (extern);
   99         rcase (inline);
  100         rcase (long);
  101         rcase (register);
  102         rcase (short);
  103         rcase (signed);
  104         rcase (static);
  105         rcase (typedef);
  106         rcase (unsigned);
  107         rcase (volatile);
  108         rcase (void);
  109         rcase (char);
  110         rcase (int);
  111         rcase (float);
  112         rcase (double);
  113         rcase (struct);
  114         rcase (union);
  115         rcase (continue);
  116         rcase (do);
  117         rcase (enum);
  118         rcase (for);
  119         rcase (goto);
  120         rcase (if);
  121         rcase (else);
  122         rcase (return);
  123 #ifdef GNU_VIOLATIONS
  124         rcase (__label__);
  125         rcase (__typeof__);
  126 #endif
  127         rcase (sizeof);
  128         rcase (switch);
  129         rcase (while);
  130         rcase (break);
  131         rcase (default);
  132     default: strcpy (tmp, "n/A\n");
  133     }
  134     return tmp;
  135 }
  136 
  137 void debug (const char *s, NormPtr i, int j)
  138 {
  139     FILE *of = stderr;
  140     if (i < 0) i = 0;
  141     if (i + j >= C_Ntok) j = C_Ntok - i - 1;
  142     fprintf (of, "%s (%i): %s\"", in_file (i+j/2), cline_of (i+j/2), s);
  143     while (j--)
  144         fprintf (of, "%s ", expand (CODE [i++]));
  145     fprintf (of, "\"\n");
  146 }
  147 
  148 
  149 void prcode (NormPtr i, int j)
  150 {
  151     PRINTF ("#expression: ");
  152     if (i < 0) i = 0;
  153     if (i + j > C_Ntok) j = C_Ntok - i;
  154     while (j--) {
  155         bool b = ISSYMBOL (CODE [i]) || ISRESERVED (CODE [i]);
  156         PRINTF ("%s", expand (CODE [i++]));
  157         if (b && (ISSYMBOL (CODE [i]) || ISRESERVED (CODE [i])))
  158             PRINTF (" ");
  159     }
  160     PRINTF ("\n");
  161 }
  162 
  163 void prcode (NormPtr i, int j, Symbol p[])
  164 {
  165     PRINTF ("#initialization expression: ");
  166     for (int c = 0; p [c] != ';'; c++)
  167         PRINTF ("%s", expand (p [c]));
  168     PRINTF (" = ");
  169     if (i < 0) i = 0;
  170     if (i + j > C_Ntok) j = C_Ntok - i;
  171     while (j--) {
  172         bool b = ISSYMBOL (CODE [i]) || ISRESERVED (CODE [i]);
  173         PRINTF ("%s", expand (CODE [i++]));
  174         if (b && (ISSYMBOL (CODE [i]) || ISRESERVED (CODE [i])))
  175             PRINTF (" ");
  176     }
  177     PRINTF ("\n");
  178 }
  179 
  180 void prcode (NormPtr i, int j, Symbol p)
  181 {
  182     Symbol pp [2] = { p, ';' };
  183     prcode (i, j, pp);
  184 }