home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Source Code / Scopes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-15  |  4.6 KB  |  201 lines  |  [TEXT/ALFA]

  1. /*
  2.     Harvest C
  3.     Copyright 1992 Eric W. Sink.  All rights reserved.
  4.     
  5.     This file is part of Harvest C.
  6.     
  7.     Harvest C is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU Generic Public License as published by
  9.     the Free Software Foundation; either version 2, or (at your option)
  10.     any later version.
  11.     
  12.     Harvest C is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.     
  17.     You should have received a copy of the GNU General Public License
  18.     along with Harvest C; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.     
  21.     Harvest C is not in any way a product of the Free Software Foundation.
  22.     Harvest C is not GNU software.
  23.     Harvest C is not public domain.
  24.  
  25.     This file may have other copyrights which are applicable as well.
  26.  
  27. */
  28.  
  29. /*
  30.  * Harvest C
  31.  * 
  32.  * Copyright 1991 Eric W. Sink   All rights reserved.
  33.  * 
  34.  * This file implements operations regarding Scopes.
  35.  * 
  36.  */
  37.  
  38. #include "conditcomp.h"
  39. #include <stdio.h>
  40. #include <string.h>
  41.  
  42. #include "structs.h"
  43.  
  44. #pragma segment Scopes
  45.  
  46. #include "TypeRecord.h"
  47. #include "ParseTree.h"
  48. #include "SymTable.h"
  49.  
  50. SYMVia_t
  51. LookUpTag(char *ident)
  52. /*
  53.  * Looks in the current block stack to find the tag with the given name.
  54.  */
  55. {
  56.     SYMVia_t                        result;
  57.     BlockStackVia_t                 tempstk;
  58.     result = NULL;
  59.     tempstk = CurrentBlock;
  60.     while (tempstk) {
  61.     if (Via(Via(tempstk)->block)->Tags)
  62.         result = TableSearch(Via(Via(tempstk)->block)->Tags, ident);
  63.     if (result) {
  64.         tempstk = NULL;
  65.     } else {
  66.         tempstk = Via(tempstk)->next;
  67.     }
  68.     }
  69.     return result;
  70. }
  71.  
  72. SymListVia_t
  73. CurrentSymbols(void)
  74. {
  75.     BlockStackVia_t                 tempstk;
  76.     tempstk = CurrentBlock;
  77.     while (tempstk) {
  78.     if (Via(Via(tempstk)->block)->Symbols)
  79.         return Via(Via(tempstk)->block)->Symbols;
  80.     else
  81.         tempstk = Via(tempstk)->next;
  82.     }
  83.     return NULL;
  84. }
  85.  
  86. SymListVia_t
  87. CurrentTags(void)
  88. {
  89.     BlockStackVia_t                 tempstk;
  90.     tempstk = CurrentBlock;
  91.     while (tempstk) {
  92.     if (Via(Via(tempstk)->block)->Tags)
  93.         return Via(Via(tempstk)->block)->Tags;
  94.     else
  95.         tempstk = Via(tempstk)->next;
  96.     }
  97.     return NULL;
  98. }
  99.  
  100. SymListVia_t
  101. CurrentLabels(void)
  102. {
  103.     BlockStackVia_t                 tempstk;
  104.     tempstk = CurrentBlock;
  105.     while (tempstk) {
  106.     if (Via(Via(tempstk)->block)->Labels)
  107.         return Via(Via(tempstk)->block)->Labels;
  108.     else
  109.         tempstk = Via(tempstk)->next;
  110.     }
  111.     return NULL;
  112. }
  113.  
  114. SymListVia_t
  115. CurrentEnums(void)
  116. {
  117.     BlockStackVia_t                 tempstk;
  118.     tempstk = CurrentBlock;
  119.     while (tempstk) {
  120.     if (Via(Via(tempstk)->block)->Enums)
  121.         return Via(Via(tempstk)->block)->Enums;
  122.     else
  123.         tempstk = Via(tempstk)->next;
  124.     }
  125.     return NULL;
  126. }
  127.  
  128. SYMVia_t
  129. LookUpSymbol(char *ident)
  130. /*
  131.  * Looks in the current block stack to find the symbol with the given name.
  132.  */
  133. {
  134.     SYMVia_t                        result;
  135.     BlockStackVia_t                 tempstk;
  136.     result = NULL;
  137.     tempstk = CurrentBlock;
  138.     while (tempstk) {
  139.     if (Via(Via(tempstk)->block)->Symbols)
  140.         result = TableSearch(
  141.                     Via(Via(tempstk)->block)->Symbols,
  142.                     (ident));
  143.     if (result) {
  144.         tempstk = NULL;
  145.     } else {
  146.         tempstk = Via(tempstk)->next;
  147.     }
  148.     }
  149.     if (!result) {
  150.     tempstk = CurrentBlock;
  151.     while (tempstk) {
  152.         if (Via(Via(tempstk)->block)->Enums)
  153.         result = TableSearch(Via(Via(tempstk)->block)->Enums,
  154.                      (ident));
  155.         if (result) {
  156.         tempstk = NULL;
  157.         } else {
  158.         tempstk = Via(tempstk)->next;
  159.         }
  160.     }
  161.     }
  162.     return result;
  163. }
  164.  
  165. void
  166. PushBlock(ScopesVia_t block)
  167. /*
  168.  * This routine pushes a block onto the block stack.  The block stack is used
  169.  * for keeping track of scope.  The block argument is simply a pointer to the
  170.  * parsetree node containing the compound statement currently being parsed.
  171.  * CurrentBlock is the top of this stack,
  172.  */
  173. {
  174.     BlockStackVia_t                 tempstk;
  175.     if (!block) {
  176.     VeryBadParseError("NULL table in PushBlock()");
  177.     }
  178.     tempstk = CurrentBlock;
  179.     CurrentBlock = Ealloc(sizeof(BlockStack_t));
  180.     Via(CurrentBlock)->block = block;
  181.     Via(CurrentBlock)->next = tempstk;
  182. }
  183.  
  184. void
  185. PopBlock(void)
  186. /* Pops the block stack. See PushBlock() above. */
  187. {
  188.     ScopesVia_t                     result;
  189.     result = NULL;
  190.     if (CurrentBlock) {
  191.     BlockStackVia_t                 tempstk;
  192.     tempstk = CurrentBlock;
  193.     result = Via(CurrentBlock)->block;
  194.     Efree(result);
  195.     CurrentBlock = Via(CurrentBlock)->next;
  196.     Efree(tempstk);
  197.     } else {
  198.     VeryBadParseError("Underflow on scope stack");
  199.     }
  200. }
  201.