home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MODEM / UWPC201.ZIP / UW-SRC.ZIP / SYMBOLS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-22  |  4.1 KB  |  158 lines

  1. /*-------------------------------------------------------------------------
  2.  
  3.   SYMBOLS.C - Symbol manipulation routines for the Termcap Compiler.
  4.  
  5.     This file is part of the Termcap Compiler source code.
  6.     Copyright (C) 1990-1991  Rhys Weatherley
  7.  
  8.     This program is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation; either version 1, or (at your option)
  11.     any later version.
  12.  
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.     GNU General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.   Revision History:
  23.   ================
  24.  
  25.    Version  DD/MM/YY  By  Description
  26.    -------  --------  --  --------------------------------------
  27.      1.0    03/04/91  RW  Original Version of SYMBOLS.C
  28.  
  29.   You may contact the author by:
  30.   =============================
  31.  
  32.    e-mail: rhys@cs.uq.oz.au
  33.      mail: Rhys Weatherley
  34.        5 Horizon Drive
  35.        Jamboree Heights
  36.        Queensland 4074
  37.        Australia
  38.  
  39. -------------------------------------------------------------------------*/
  40.  
  41. #include "symbols.h"        /* Declarations for this module */
  42. #include <string.h>
  43.  
  44. char    *malloc    ();
  45.  
  46. /*
  47.  * Declare the structure of the symbol table.
  48.  */
  49. #define    MAX_SYMBOLS    500
  50. struct    symbol    {
  51.           char *name;    /* Name of the symbol */
  52.           int    ref;    /* First reference in chain */
  53.           int    posn;    /* Position of label definition */
  54.         };
  55. static    struct    symbol    SymbolTable[MAX_SYMBOLS];
  56. static    int        NumSymbols=0;
  57. static    int        ScanTable=0;
  58.  
  59. /*
  60.  * Add a new identifier to the symbol table and return its index.
  61.  * Returns -1 if the symbol table is full.
  62.  */
  63. int    addident (ident)
  64. char    *ident;
  65. {
  66.   int index=0;
  67.   while (index < NumSymbols && strcmp (ident,SymbolTable[index].name))
  68.     ++index;
  69.   if (index >= NumSymbols)
  70.     {
  71.       if (NumSymbols >= MAX_SYMBOLS)
  72.         return (-1);        /* Symbol table is full */
  73.       SymbolTable[NumSymbols].name = malloc (strlen (ident) + 1);
  74.       if (SymbolTable[NumSymbols].name == 0)
  75.         return (-1);        /* Not enough memory for symbol */
  76.       index = NumSymbols;
  77.       strcpy (SymbolTable[NumSymbols].name,ident);
  78.       SymbolTable[NumSymbols].posn = -1;
  79.       SymbolTable[NumSymbols++].ref = 0;
  80.     } /* if */
  81.   return (index);
  82. } /* addident */
  83.  
  84. /*
  85.  * Get the name of an identifier.
  86.  */
  87. char    *getname (ident)
  88. int    ident;
  89. {
  90.   return (SymbolTable[ident].name);
  91. } /* getname */
  92.  
  93. /*
  94.  * Add the position for an identifier to the symbol table.
  95.  */
  96. void    addposn (ident,address)
  97. int    ident,address;
  98. {
  99.   SymbolTable[ident].posn = address;
  100. } /* addposn */
  101.  
  102. /*
  103.  * Get the position for an identifier (-1 if undefined).
  104.  */
  105. int    getposn (ident)
  106. int    ident;
  107. {
  108.   return (SymbolTable[ident].posn);
  109. } /* getposn */
  110.  
  111. /*
  112.  * Add a new reference to the symbol table for a particular
  113.  * identifier index.
  114.  */
  115. void    addref    (ident,address)
  116. int    ident,address;
  117. {
  118.   SymbolTable[ident].ref = address;
  119. } /* addref */
  120.  
  121. /*
  122.  * Get the current reference in the symbol table for an identifier.
  123.  */
  124. int    getref    (ident)
  125. int    ident;
  126. {
  127.   return (SymbolTable[ident].ref);
  128. } /* getref */
  129.  
  130. /*
  131.  * Get the first reference address for the first symbol
  132.  * table entry.  Returns the address or -1 at the table's end.
  133.  */
  134. int    firstref ()
  135. {
  136.   ScanTable = 0;
  137.   while (ScanTable < NumSymbols && !SymbolTable[ScanTable].ref)
  138.     ++ScanTable;
  139.   if (ScanTable < NumSymbols)
  140.     return (ScanTable);
  141.    else
  142.     return (-1);
  143. } /* firstref */
  144.  
  145. /*
  146.  * Get the next reference address from the symbol table.
  147.  */
  148. int    nextref    ()
  149. {
  150.   ++ScanTable;
  151.   while (ScanTable < NumSymbols && !SymbolTable[ScanTable].ref)
  152.     ++ScanTable;
  153.   if (ScanTable < NumSymbols)
  154.     return (ScanTable);
  155.    else
  156.     return (-1);
  157. } /* nextref */
  158.