"Fossies" - the Fresh Open Source Software Archive

Member "gentle-97/gentle/symtab.c" (20 Aug 1998, 11004 Bytes) of package /linux/misc/old/gentle-97.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. For more information about "symtab.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2 
    3    GENTLE 97
    4 
    5    Gentle Compiler Construction System 3.0
    6    Copyright (C) 1992, 1997  F.W. Schroeer
    7 
    8 */
    9 
   10 
   11 #include "gen.h"
   12 
   13 typedef struct IdentRec *Ident;
   14 
   15 struct IdentRec
   16 {
   17    char  *firstposptr;
   18    long  length;
   19    Ident next;
   20 
   21    long  symbol_class;
   22      
   23    long  GlobalMeaning;
   24    long  LocalMeaning;
   25    long  LocalMeaning2;
   26    long  FunctorMeaning;
   27    long  ExportFlag;
   28 
   29 };
   30 
   31 char *idstringtab_ptr;
   32 char *idstringtab_endptr;
   33 
   34 struct IdentRec *idtab_ptr;
   35 struct IdentRec *idtab_endptr;
   36 
   37 #define HashTabSize   2048 /* should be a power of two */
   38 
   39 Ident HashTab [HashTabSize];
   40 
   41 #define STRINGTAB_PIECE  10000
   42 #define STRINGTAB_EXTRA    500
   43 #define IDTABSIZE 500
   44 
   45 typedef struct IdentRec IDTAB [IDTABSIZE]; /* preliminary */
   46 
   47 /*----------------------------------------------------------------------------*/
   48 
   49 static allocate_idstringtab ()
   50 {
   51    idstringtab_ptr =
   52       (char *) malloc (STRINGTAB_PIECE + STRINGTAB_EXTRA);
   53    if (idstringtab_ptr == 0) {
   54       Fatal("Running out of storage (Id String Table).");
   55    }
   56    idstringtab_endptr = idstringtab_ptr + STRINGTAB_PIECE - 1;
   57 }
   58 
   59 /*----------------------------------------------------------------------------*/
   60 
   61 static allocate_idtab ()
   62 {
   63    idtab_ptr =
   64       (struct IdentRec *)
   65       malloc (sizeof (IDTAB /*struct IdentRec [IDTABSIZE]*/ ) );
   66    if (idtab_ptr == 0) {
   67       Fatal("Running out of storage (Id Table).");
   68    }
   69    idtab_endptr = & idtab_ptr[IDTABSIZE - 1];
   70 }
   71 
   72 /*----------------------------------------------------------------------------*/
   73 
   74 slice_to_id (idstart, idstop, ref_id)
   75    char *idstart; /* position of first character */
   76    char *idstop;  /* position  a f t e r  last character */
   77    Ident *ref_id;
   78 {
   79    long  hash, length;
   80    Ident chain;
   81    Ident  NewId;
   82 
   83    length = idstop-idstart;
   84    hash = ( length*256 + ((*idstart)&0xf)*16 + (*(idstop-1)&0xf) ) 
   85    & (HashTabSize-1);
   86    chain = HashTab[hash];
   87 
   88    for(;;) {
   89       if (chain == 0) {
   90       
   91      /* not in table */
   92      
   93      NewId = idtab_ptr;
   94         
   95      if (idtab_ptr == idtab_endptr)
   96         allocate_idtab();
   97          else
   98         idtab_ptr++;
   99         
  100 
  101      /* copy id representation  id into idstringtab */
  102      {
  103         register char *i, *freeptr, *stop;
  104 
  105         if (idstringtab_ptr > idstringtab_endptr)
  106            allocate_idstringtab();
  107         freeptr = idstringtab_ptr;
  108 
  109         NewId->firstposptr = idstringtab_ptr;
  110 
  111         i = idstart;
  112         stop = idstop;
  113         while (i < stop) {
  114            *freeptr++ = *i++;
  115         }
  116         *freeptr = '\0';
  117         freeptr++;
  118         idstringtab_ptr= freeptr;
  119      }
  120 
  121      NewId->length = length;
  122      NewId->next = HashTab[hash];
  123         
  124      HashTab[hash] = NewId;
  125 
  126      
  127      init_id_attributes (NewId);
  128    
  129      break;
  130       }
  131 
  132       /* current token == ident at chain ? */
  133       
  134       if (chain->length == length) {
  135          register char *i, *j;
  136      i = idstart; j = chain->firstposptr;
  137      while (i != idstop && *i == *j) {
  138         i++; j++;
  139          }
  140 
  141      if (i == idstop && *j == '\0') {
  142         
  143         /* found */
  144         
  145         NewId = chain;
  146         break;
  147      }
  148       }
  149 
  150       chain = chain->next;
  151    }
  152 
  153    *ref_id = NewId;
  154 }
  155 
  156 /*----------------------------------------------------------------------------*/
  157 
  158 string_to_id (idstart, ref_id)
  159    char *idstart;
  160    Ident *ref_id;
  161 {
  162    char *idstop;
  163 
  164    idstop = idstart;
  165    while (*idstop != '\0') idstop++;
  166    slice_to_id (idstart, idstop, ref_id);
  167 }
  168 
  169 /*----------------------------------------------------------------------------*/
  170 
  171 id_to_string (id, repr)
  172    Ident id;
  173    char **repr;
  174 {
  175    *repr = id->firstposptr;
  176 }
  177 
  178 /*----------------------------------------------------------------------------*/
  179 
  180 void init_idtab ()
  181 {
  182    long i;
  183 
  184    for (i = 0; i<=HashTabSize-1; i++) HashTab[i] = 0;
  185 
  186    allocate_idtab ();
  187    allocate_idstringtab ();
  188 }
  189 
  190 /*----------------------------------------------------------------------------*/
  191 
  192 init_id_attributes (new)
  193    Ident new;
  194 {
  195            
  196    new-> LocalMeaning = 0;
  197    new-> GlobalMeaning = 0;
  198    new-> FunctorMeaning = 0;
  199 
  200    if ('a' <= *(new->firstposptr) && *(new->firstposptr) <= 'z') {
  201       new->symbol_class = SMALLID;
  202    }
  203    else {
  204       new->symbol_class = LARGEID;
  205    }
  206 
  207 }
  208 
  209 /*----------------------------------------------------------------------------*/
  210 
  211 DefGlobalMeaning (id, m, f) Ident id; long m; long f;
  212 {
  213    id->GlobalMeaning = m;
  214    id->ExportFlag = f;
  215 }
  216 
  217 /*----------------------------------------------------------------------------*/
  218 
  219 DefFunctorMeaning (id, m) Ident id; long m;
  220 {
  221    id->FunctorMeaning = m;
  222 }
  223 
  224 /*----------------------------------------------------------------------------*/
  225 
  226 DefLocalMeaning (id, m1, m2) Ident id; long m1; long m2;
  227 {
  228    id->LocalMeaning = m1;
  229    id->LocalMeaning2 = m2;
  230 }
  231 
  232 /*----------------------------------------------------------------------------*/
  233 
  234 ForgetLocalMeaning (id) Ident id;
  235 {
  236    id->LocalMeaning = 0;
  237 }
  238 
  239 /*----------------------------------------------------------------------------*/
  240 
  241 long GetGlobalMeaning (id, m) Ident id; long *m;
  242 {
  243    if (id->GlobalMeaning == 0)
  244       return 0;
  245    *m = id->GlobalMeaning;
  246    return 1;
  247 }
  248 
  249 /*----------------------------------------------------------------------------*/
  250 
  251 long GetExportFlag (id, f) Ident id; long *f;
  252 {
  253    if (id->GlobalMeaning == 0)
  254       return 0;
  255    *f = id->ExportFlag;
  256    return 1;
  257 }
  258 
  259 /*----------------------------------------------------------------------------*/
  260 
  261 long GetFunctorMeaning (id, m) Ident id; long *m;
  262 {
  263    if (id->FunctorMeaning == 0)
  264       return 0;
  265    *m = id->FunctorMeaning;
  266    return 1;
  267 }
  268 
  269 /*----------------------------------------------------------------------------*/
  270 
  271 long GetLocalMeaning (id, m) Ident id; long *m;
  272 {
  273    if (id->LocalMeaning == 0)
  274       return 0;
  275    *m = id->LocalMeaning;
  276    return 1;
  277 }
  278 
  279 /*----------------------------------------------------------------------------*/
  280 
  281 long GetLocalMeaning2 (id, m) Ident id; long *m;
  282 {
  283    if (id->LocalMeaning == 0)
  284       return 0;
  285    *m = id->LocalMeaning2;
  286    return 1;
  287 }
  288 
  289 /*----------------------------------------------------------------------------*/
  290 
  291 long HasGlobalMeaning (id)
  292    Ident id;
  293 {
  294    return id->GlobalMeaning != 0;
  295 }
  296 
  297 /*----------------------------------------------------------------------------*/
  298 
  299 long HasLocalMeaning (id)
  300    Ident id;
  301 {
  302    return id->LocalMeaning != 0;
  303 }
  304 
  305 /*----------------------------------------------------------------------------*/
  306 
  307 long get_symbol_class (Id) Ident Id;
  308 {
  309    return Id->symbol_class;
  310 }
  311 
  312 /*----------------------------------------------------------------------------*/
  313 
  314 enter_keyword (class, idstart)
  315    int class;
  316    char *idstart;
  317 {
  318    Ident NewId;
  319    
  320    string_to_id (idstart, &NewId);
  321    NewId->symbol_class = class;
  322 }
  323 
  324 /*----------------------------------------------------------------------------*/
  325 
  326 static int tokencount = 0;
  327 
  328 UniqueTokenId (ref_id)
  329    Ident *ref_id;
  330 {
  331    char buffer[100];
  332    tokencount++;
  333    sprintf(buffer, "yytoken_%d", tokencount);
  334    string_to_id(buffer, ref_id);
  335 }
  336 
  337 /*----------------------------------------------------------------------------*/
  338 
  339 int is_word(str)
  340    char *str;
  341 {
  342    char *p;
  343    p = str;
  344    while (*p != 0) {
  345       if (('a' <= *p) && (*p <= 'z')) ;
  346       else if (('A' <= *p) && (*p <= 'Z')) ;
  347       else if (('0' <= *p) && (*p <= '9')) ;
  348       else return 0;
  349       p++;
  350    }
  351    return 1;
  352 }
  353 
  354 int namebufsize;
  355 
  356 char *start_namebuf;
  357 char *stop_namebuf;
  358 
  359 char *namebufptr; /* next position for insertion */
  360 
  361 #define NAMEBUFPIECE 100
  362 
  363 resize_namebuf()
  364 {
  365    char *new, *p;
  366 
  367    new = (char *) malloc(namebufsize+NAMEBUFPIECE);
  368    namebufsize += NAMEBUFPIECE;
  369    namebufptr = new;
  370 
  371    for (p = start_namebuf; p <= stop_namebuf; p++) {
  372       *namebufptr = *p;
  373       namebufptr++;
  374    }
  375 
  376    start_namebuf = new;
  377    stop_namebuf = start_namebuf+namebufsize-1;
  378 }
  379 
  380 static initialized = 0;
  381 
  382 clear_buffer()
  383 {
  384    if (! initialized) {
  385       initialized = 1;
  386 
  387       start_namebuf = (char *) malloc(NAMEBUFPIECE);
  388       namebufsize = NAMEBUFPIECE;
  389       stop_namebuf = start_namebuf+namebufsize-1;
  390    }
  391    namebufptr = start_namebuf;
  392 }
  393 
  394 append_to_buf(str)
  395    char *str;
  396 {
  397    char *p;
  398    p = str;
  399    while(*p != 0) {
  400       *namebufptr = *p;
  401       if (namebufptr == stop_namebuf) resize_namebuf();
  402       else namebufptr++;
  403       p++;
  404    }
  405    *namebufptr = 0;
  406 }
  407    
  408 CreateNameForToken (Str, ref_id)
  409    char *Str;
  410    Ident *ref_id;
  411 {
  412 
  413 
  414    clear_buffer();
  415 
  416    if (is_word(Str)) {
  417       append_to_buf("yykw_");
  418       append_to_buf(Str);
  419    }
  420    else {
  421       char *p, *b;
  422 
  423       append_to_buf("yytk");
  424       p = Str;
  425       while (*p != 0) {
  426      switch (*p) {
  427          case '0': case '1': case '2': case '3': case '4': case '5':
  428          case '6': case '7': case '8': case '9': case 'A': case 'B':
  429          case 'C': case 'D': case 'E': case 'F': case 'G': case 'H':
  430          case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
  431          case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T':
  432          case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
  433          case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  434          case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  435          case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  436          case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  437          case 'y': case 'z':
  438         {
  439            char rep[5];
  440            sprintf(rep, "_%c", *p);
  441            append_to_buf(rep);
  442         }
  443      case ' ': append_to_buf("_BLANK"); break;
  444      case '!': append_to_buf("_EXCLAM"); break;
  445      case '"': append_to_buf("_DQUOTE"); break;
  446      case '#': append_to_buf("_SHARP"); break;
  447      case '$': append_to_buf("_DOLLAR"); break;
  448      case '%': append_to_buf("_PERCENT"); break;
  449      case '&': append_to_buf("_AMPERSAND"); break;
  450      case '\'': append_to_buf("_BACKSLAH"); break;
  451      case '(': append_to_buf("_LPAREN"); break;
  452      case ')': append_to_buf("_RPAREN"); break;
  453      case '*': append_to_buf("_ASTERISK"); break;
  454      case '+': append_to_buf("_PLUS"); break;
  455      case ',': append_to_buf("_COMMA"); break;
  456      case '-': append_to_buf("_MINUS"); break;
  457      case '.': append_to_buf("_DOT"); break;
  458      case '/': append_to_buf("_SLASH"); break;
  459      case ':': append_to_buf("_COLON"); break;
  460      case ';': append_to_buf("_SEMICOLON"); break;
  461      case '<': append_to_buf("_LESS"); break;
  462      case '=': append_to_buf("_EQUAL"); break;
  463      case '>': append_to_buf("_GREATER"); break;
  464      case '?': append_to_buf("_QUESTIONM"); break;
  465      case '@': append_to_buf("_ATSIGN"); break;
  466      case '[': append_to_buf("_LBRACKET"); break;
  467      case '\\': append_to_buf("_BACKSLASH"); break;
  468      case ']': append_to_buf("_RBRACKET"); break;
  469      case '^': append_to_buf("_POWER"); break;
  470      case '_': append_to_buf("_UNDERSCORE"); break;
  471      case '`': append_to_buf("_BACKQUOTE"); break;
  472      case '{': append_to_buf("_LBRACE"); break;
  473      case '|': append_to_buf("_BAR"); break;
  474      case '}': append_to_buf("_RBRACE"); break;
  475      case '~': append_to_buf("_TILDE"); break;
  476      default:
  477         {
  478            char rep[5];
  479            sprintf(rep, "_%d", *p);
  480            append_to_buf(rep);
  481         }
  482      }
  483      p++;
  484       }
  485    }
  486    string_to_id(start_namebuf, ref_id);
  487 }
  488 /*----------------------------------------------------------------------------*/