SymbolTable.hh 4.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
#ifndef _SYMBOLTABLE_HH
#define _SYMBOLTABLE_HH
//------------------------------------------------------------------------------
/*! \file
  \version 1.0
  \date 12/01/2003
  \par This file defines the SymbolTable class .
*/
//------------------------------------------------------------------------------
sebastien's avatar
sebastien committed
10
11
using namespace std;

12
13
14
#include <map>
#include <string>
#include <vector>
sebastien's avatar
sebastien committed
15
#include <ostream>
sebastien's avatar
sebastien committed
16
#include <iostream>
17

18
#include "SymbolTableTypes.hh"
19

20
21
22
23
24
25
/*!
  \class SymbolTable
  \brief This class keeps track of symbols
*/
class SymbolTable
{
26
private:
27
28
29
  /*! Adds symbol into symbol table
    \param name a string.
    \param type a Type struct.
30
    \param tex_name a string for the TeX name.
31
    \par Description
32
33
34
35
    - warning if symbol is already set with same type
    - error if symbol is already set with different type
    - set Name and Type
    - increase corresponding counter in ModelParameters class
36
  */
37
  int AddSymbol(std::string name, Type type, std::string tex_name);
38
  /*! Symbol table map */
39
40
41
  std::map<std::string, Symbol, std::less<std::string> > symboltable;
  //! Typedef for const iterator on symboltable map
  typedef std::map<std::string, Symbol, std::less<std::string> >::const_iterator symboltable_const_iterator;
42
  /*! Symbol name table indexed by type and ID */
43
44
  std::vector< std::vector<std::string> > name_table;
  std::vector< std::vector<std::string> > tex_name_table;
45
  /*! Changes type of a symbol */
46
  void ResetType(std::string name, Type new_type);
47
48
public :
  /*! Constructor */
49
50
51
52
53
54
55
56
57
  SymbolTable();
  //! Number of declared endogenous variables
  int endo_nbr;
  //! Number of declared exogenous variables
  int exo_nbr;
  //! Number of declared deterministic exogenous variables
  int exo_det_nbr;
  //! Number of declared parameters
  int parameter_nbr;
sebastien's avatar
sebastien committed
58
59
60
61
  //! Number of declared model local variables
  int model_local_variable_nbr;
  //! Number of declared modfile local variables
  int modfile_local_variable_nbr;
62
63
  //! Number of declared recursive variables
  int recur_nbr;
64
  /*! Pointer to error function of parser class */
65
  void (* error) (const char* m);
66
67
68
69
70
71
  /*! Adds a symbol apearing in declaration
    - warning if symbol is already set with same type
    - error if symbol is already set with different type
    - set name, type
    - increase corresponding counter in ModelParameters
  */
72
  int AddSymbolDeclar(std::string name, Type type, std::string tex_name);
73
  /*! Adds symbol range */
74
  void AddSymbolRange(std::string name, int nbr, Type type, std::string tex_name);
75
  /*! Sets a symbol as referenced */
76
  void SetReferenced(std::string name);
77
  /*! Return eReferenced if symbol is referenced eNotReferenced otherwise*/
78
  Reference isReferenced(const std::string &name) const;
79
80
81
  /*! Tests if symbol exists in symbol table
    \return true if exists, false outherwise
  */
82
  inline bool Exist(const std::string &name) const;
83
  /*! Gets name by type and ID */
sebastien's avatar
sebastien committed
84
  inline std::string getNameByID(Type type, int id) const;
85
  /*! Gets tex name by type and ID */
sebastien's avatar
sebastien committed
86
  inline std::string getTexNameByID(Type type, int id) const;
87
  /*! Gets type by name */
88
  inline Type getType(const std::string &name) const;
89
  /*! Gets ID by name */
90
  inline int getID(const std::string &name) const;
sebastien's avatar
sebastien committed
91
  //! Write output of this class
sebastien's avatar
sebastien committed
92
  void writeOutput(std::ostream &output) const;
93
94
};

95
96
97
98
inline bool SymbolTable::Exist(const std::string &name) const
{
  symboltable_const_iterator iter = symboltable.find(name);
  return (iter != symboltable.end());
99
100
101
}

//------------------------------------------------------------------------------
sebastien's avatar
sebastien committed
102
inline std::string  SymbolTable::getNameByID(Type type,int id) const
103
104
105
106
107
108
109
{
  if (id >= 0 && (int)name_table[type].size() > id)
    return(name_table[type][id]);
  else return "";
}

//------------------------------------------------------------------------------
sebastien's avatar
sebastien committed
110
inline std::string  SymbolTable::getTexNameByID(Type type,int id) const
111
112
113
114
115
116
117
{
  if (id >= 0 && (int)tex_name_table[type].size() > id)
    return(tex_name_table[type][id]);
  else return "";
}

//------------------------------------------------------------------------------
118
inline Type SymbolTable::getType(const std::string &name) const
119
{
120
121
  symboltable_const_iterator iter = symboltable.find(name);
  if (iter == symboltable.end())
sebastien's avatar
sebastien committed
122
123
124
125
    {
      cerr << "SymbolTable::getType: unknwon symbol: " << name << endl;
      exit(-1);
    }
126
127
  else
    return iter->second.type;
128
129
130
}

//------------------------------------------------------------------------------
131
inline int SymbolTable::getID(const std::string &name) const
132
{
133
134
  symboltable_const_iterator iter = symboltable.find(name);
  if (iter == symboltable.end())
135
    return -1;
136
137
  else
    return(iter->second.id);
138
139
140
141
}

//------------------------------------------------------------------------------
#endif