home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pascal.zip / symtable / symtable.c < prev    next >
Text File  |  1995-10-29  |  8KB  |  314 lines

  1. /*
  2.  *        C . A . P .   S Y M B O L   T A B L E
  3.  *
  4.  *        C O R E   P R O C E D U R E S
  5.  *
  6.  *        Stéphane Charette @ C.A.P. Services
  7.  *
  8.  *        Last modified:  Stéphane Charette, 1995 October 29
  9.  *
  10.  *****************************************************************************
  11.  *
  12.  *        Project:    BILL
  13.  *        Group:    symbol table
  14.  *        File:        symtable\symtable.c
  15.  *        Version:    0.1.5
  16.  *
  17.  *        This file contains all of the source code that makes up the symbol
  18.  *        table used with the interpreter BILL.
  19.  */
  20.  
  21.  
  22.  
  23. /*
  24.  *    Versions:
  25.  *
  26.  *    0.0.1    - design of structure and implementation, Stéphane Charette, 94Mar23-27
  27.  *    0.1.0    - first working version, SC, 94Mar27
  28.  *    0.1.1    - added verification for referencing uninitialized variables, SC, 94Mar27
  29.  *    0.1.2    - added verification for undefined variables, SC, 94Mar27-28
  30.  *    0.1.3    - removed verification for undefined variables, SC, 94Apr9
  31.  *    0.1.3    - added constant variable types, SC, 94Apr9
  32.  *    0.1.4    - fixed bug while looking if entry exists, SC, 94Apr11
  33.  *    0.1.5    - ported to OS/2, SC, 94Apr23-27
  34.  *    0.1.5 - changed text formatting, SC, 95Oct29
  35.  */
  36.  #define _SYMTABLE_VERSION "Symbol table v0.1.5, Stéphane Charette, 95Oct29\n"
  37.  
  38.  
  39.  
  40. /*
  41.  *    Includes
  42.  */
  43.     #include "..\symtable\symtable.hi"            // internal symbol table include file
  44.  
  45.  
  46.  
  47. /*
  48.  *    FUNCTION:  InitSymTable
  49.  */
  50. ERR InitSymTable( )
  51. {
  52.     USHORT i;                                            // temporary index counter
  53.     ERR RC = ERR_OK;
  54.  
  55.     #if( _SYMTABLE_DEBUG )
  56.     Symbol_Table_Debug.ScreenOutput = FALSE;    // pre-initialize debug value
  57.     Symbol_Table_Debug.FileOutput = FALSE;        // pre-initialize debug value
  58.     #endif
  59.  
  60.     SymTableInternalError = ERR_OK;                // internal symbol table error
  61.  
  62.     // initialize the actual table
  63.     for( i = 0; i < SYMTABLE_MAX_ENTRY; Table[i++].Used = false );
  64.  
  65.     return RC;
  66. }
  67.  
  68.  
  69.  
  70. /*
  71.  *    FUNCTION:  ResetSymTable
  72.  */
  73. ERR ResetSymTable( )
  74. {
  75.     return ERR_OK;
  76. }
  77.  
  78.  
  79.  
  80. /*
  81.  *    FUNCTION:  SymTableError
  82.  */
  83. void SymTableError( ERR error_code )
  84. {
  85.     SymTableInternalError = error_code;
  86.  
  87.     #if( _SYMTABLE_DEBUG )
  88.     ResetSymTableDebug( );            // shutdown the debug procedures
  89.     #else
  90.     printf( "\nError encountered in symbol table:  %i\n\a", SymTableInternalError );
  91.     #endif
  92.  
  93.     ResetSymTable( );                    // perform shutdown procedures
  94.  
  95.     ExitCode( error_code );            // return to operating system
  96. }
  97.  
  98.  
  99.  
  100. /*
  101.  *    FUNCTION:  NewTableEntry
  102.  ***************************************
  103.  *    Creates a new entry in the variable    *
  104.  *    table and assigns it the name that    *
  105.  *    is specified as "name".  Return the    *
  106.  *    index into the table entry using        *
  107.  *    the variable "index".                    *
  108.  ***************************************/
  109. ERR NewTableEntry( SHORT *index, UCHAR name[] )
  110. {
  111.     ERR RC = ERR_OK;
  112.     SHORT newid = SYM_NO_ID;    // index of first available symbol in table
  113.     USHORT i;                        // tmp counter
  114.  
  115.     // assume the worst - set the index to indicate an error
  116.     *index = SYM_NO_ID;
  117.  
  118.     // make certain the requested ID is not already in the table
  119.     for( i = 0; i < SYMTABLE_MAX_ENTRY; i++ )
  120.     {
  121.         if( ( Table[i].Used == false ) && ( newid == SYM_NO_ID ) ) newid = i;    // save this position for the new variable
  122.         if( ( Table[i].Used == true ) && ( stricmp( Table[i].Name, name ) == 0 ) ) newid = SYM_DUPLICATE_ID;    // duplicate!
  123.     }
  124.  
  125.     if( newid == SYM_NO_ID )    // is the symbol table filled?
  126.     {
  127.         SymTableError( RC = ERR_SYMT_TOO_MANY_IDS );
  128.     }
  129.     else
  130.     {
  131.         if( ( newid >= 0 ) && ( newid < SYMTABLE_MAX_ENTRY ) )    // is the "available" index returned valid?
  132.         {    // initialize the new table entry
  133.             Table[newid].Used = true;
  134.             strcpy( Table[newid].Name, name );
  135.             Table[newid].Constant = false;
  136.             *index = newid;
  137.         }
  138.     }
  139.  
  140.     return RC;
  141. }
  142.  
  143.  
  144.  
  145. /*
  146.  *    FUNCTION:  GetTableEntry
  147.  ***************************************
  148.  *    Look in the symbol table for a        *
  149.  *    variable called "name" and return    *
  150.  *    it's index using the variable            *
  151.  *    "index".                                        *
  152.  ***************************************/
  153. ERR GetTableEntry( SHORT *index, UCHAR name[] )
  154. {
  155.     ERR RC = ERR_OK;
  156.     USHORT i;            // tmp counter
  157.  
  158.     // assume the worst - set the index to indicate we didn't find it
  159.     *index = SYM_NO_ID;
  160.  
  161.     // find the id in the table
  162.     for( i = 0; i < SYMTABLE_MAX_ENTRY; i++ )
  163.     {
  164.         if( ( Table[i].Used == true ) && ( stricmp( Table[i].Name, name ) == 0 ) )
  165.         {                            // found it!
  166.             *index = i;    // keep the index
  167.             break;        // break out of the for loop
  168.         }
  169.     }
  170.  
  171.     if( i == SYMTABLE_MAX_ENTRY )        // is the value of i past the maximum range?
  172.     {                                            // ...this would indicate that we didn't find the var
  173.         RC = ERR_SYMT_UNDEFINED_ID;    // ...so return the error
  174.     }
  175.  
  176.     return RC;
  177. }
  178.  
  179.  
  180.  
  181. /*
  182.  *    FUNCTION:  TableMakeConst
  183.  ***************************************
  184.  *    Look in the symbol table for the        *
  185.  *    variable and change it's attributes    *
  186.  *    so that it's recognized as a const.    *
  187.  ***************************************/
  188. ERR TableMakeConst( UCHAR name[] )
  189. {
  190.     ERR RC = ERR_OK;
  191.     USHORT i;                        // tmp counter
  192.  
  193.     // find the id in the table
  194.     for( i = 0; i < SYMTABLE_MAX_ENTRY; i++ )
  195.     {
  196.         if( ( Table[i].Used == true ) && ( stricmp( Table[i].Name, name ) == 0 ) )
  197.         {                                // found it!
  198.             Table[i].Constant = true;    // make it a constant
  199.             break;                    // break out of the for loop
  200.         }
  201.     }
  202.  
  203.     if( i == SYMTABLE_MAX_ENTRY )        // is the value of i past the maximum ranger?
  204.     {                                            // ...this would indicate that we didn't find the var
  205.         RC = ERR_SYMT_UNDEFINED_ID;    // ...so return the error
  206.     }
  207.  
  208.     return RC;
  209. }
  210.  
  211.  
  212.  
  213. /*
  214.  *    FUNCTION:  TableIsConst
  215.  ***************************************
  216.  *    Look in the symbol table for the        *
  217.  *    variable and see if it's declared    *
  218.  *    as being a constant.                        *
  219.  ***************************************/
  220. ERR TableIsConst( BOOL *isconst, UCHAR name[] )
  221. {
  222.     ERR RC = ERR_OK;
  223.     USHORT i;
  224.  
  225.     // find the id in the table
  226.     for( i = 0; i < SYMTABLE_MAX_ENTRY; i++ )
  227.     {
  228.         if( ( Table[i].Used == true ) && ( stricmp( Table[i].Name, name ) == 0 ) )
  229.         {                                        // found it!
  230.             *isconst = Table[i].Constant;    // get the value
  231.             break;                            // break out of the for loop
  232.         }
  233.     }
  234.  
  235.     if( i == SYMTABLE_MAX_ENTRY )        // is the value of i past the maximum ranger?
  236.     {                                            // ...this would indicate that we didn't find the var
  237.         RC = ERR_SYMT_UNDEFINED_ID;    // ...so return the error
  238.     }
  239.  
  240.     return RC;
  241. }
  242.  
  243.  
  244.  
  245. #if( _SYMTABLE_DEBUG )
  246. /*
  247.  *    FUNCTION:  InitSymTableDebug
  248.  */
  249. ERR InitSymTableDebug( BOOL screen, BOOL source )
  250. {
  251.     ERR RC = ERR_OK;
  252.  
  253.     Symbol_Table_Debug.ScreenOutput = screen;
  254.     Symbol_Table_Debug.FileOutput = source;
  255.  
  256.     if( Symbol_Table_Debug.FileOutput )
  257.     {
  258.         if( ! ( SymTableDebugFilePtr = fopen( SYMTABLE_DEBUG_FILENAME, "wt" ) ) ) SymTableError( ERR_SYMT_DEBUG_FILE_ERROR );
  259.     }
  260.  
  261.     SymTableDebugWrite( "\n%s\n", _SYMTABLE_VERSION );
  262.     if( Symbol_Table_Debug.ScreenOutput ) SymTableDebugWrite( "=> screen output enabled.\n" );
  263.         else SymTableDebugWrite( "=> screen output disabled.\n" );
  264.     if( Symbol_Table_Debug.FileOutput ) SymTableDebugWrite( "=> output to file \"%s\" enabled.\n", SYMTABLE_DEBUG_FILENAME );
  265.         else SymTableDebugWrite( "=> output to file disabled.\n" );
  266.  
  267.     return RC;
  268. }
  269. #endif
  270.  
  271.  
  272.  
  273. #if( _SYMTABLE_DEBUG )
  274. /*
  275.  *    FUNCTION:  ResetSymTableDebug
  276.  */
  277. ERR ResetSymTableDebug( )
  278. {
  279.     ERR RC = ERR_OK;
  280.  
  281.     if( SymTableInternalError ) SymTableDebugWrite( "\nError encountered in symbol table:  %i\n\a", SymTableInternalError );
  282.  
  283.     if( Symbol_Table_Debug.FileOutput ) fclose( SymTableDebugFilePtr );
  284.  
  285.     return RC;
  286. }
  287. #endif
  288.  
  289.  
  290.  
  291. #if( _SYMTABLE_DEBUG )
  292. /*
  293.  *    FUNCTION:    SymTableDebugWrite
  294.  */
  295. ERR SymTableDebugWrite( UCHAR *text, ... )
  296. {
  297.     ERR RC = ERR_OK;
  298.  
  299.     va_list ptr;                /* va is a variable argument type implemented in newer revisions */
  300.     va_start( ptr, text );        /* of ANSI C.  Support library includes vfprintf, vprintf, vsprintf */
  301.  
  302.     // if screen debug output is enabled...
  303.     if( Symbol_Table_Debug.ScreenOutput ) vprintf( text, ptr );                            // ...then output to screen
  304.  
  305.     // if file debug output is enabled...
  306.     if( Symbol_Table_Debug.FileOutput ) vfprintf( SymTableDebugFilePtr, text, ptr );    // ...then output to file
  307.  
  308.     va_end( ptr );
  309.  
  310.     return RC;
  311. }
  312. #endif
  313.  
  314.