SymbolTable.cc 8.29 KB
Newer Older
1
2
3
4
5
/*! \file
  \version 1.0
  \date 04/09/2004
  \par This file implements the SymbolTable class methodes.
*/
6

7
8
#include <iostream>
#include <algorithm>
9
10
#include <sstream>

11
12
13
#include "SymbolTable.hh"
#include "Interface.hh"
using namespace std;
14
15

SymbolTable::SymbolTable(ModelParameters &mod_param_arg) : mod_param(mod_param_arg)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
  name_table.resize(20);
  tex_name_table.resize(20);
}

SymbolTable::~SymbolTable()
{
  // Empty
}

int SymbolTable::AddSymbol(string name,Type type, string tex_name)
{
  symboltable[name].type = type;
  symboltable[name].referenced = eNotReferenced;
  name_table[(int) type].push_back(name);
  tex_name_table[(int) type].push_back(tex_name);

  switch (type)
    {
    case eExogenous:
36
37
      symboltable[name].id = mod_param.exo_nbr;
      return mod_param.exo_nbr++;
38
    case eExogenousDet:
39
40
      symboltable[name].id = mod_param.exo_det_nbr;
      return mod_param.exo_det_nbr++;
41
    case eEndogenous:
42
43
      symboltable[name].id = mod_param.endo_nbr;
      return mod_param.endo_nbr++;
44
    case eParameter:
45
46
      symboltable[name].id = mod_param.parameter_nbr;
      return mod_param.parameter_nbr++;
47
    case eRecursiveVariable:
48
49
      symboltable[name].id = mod_param.recur_nbr;
      return mod_param.recur_nbr++;
50
    case eLocalParameter:
51
52
      symboltable[name].id = mod_param.local_parameter_nbr;
      return mod_param.local_parameter_nbr++;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    default:
      // should never happen
      return -1;
    }

}

int SymbolTable::AddSymbolDeclar(string name,Type type, string tex_name)
{
  //Testing if the symbol exist in the map
  if ( !Exist(name) )
    {
      //The symbol dosn't exist, adding it
      return AddSymbol(name,type, tex_name);
    }
  else
    {
      //The symbol exists, testing its type
      if (symboltable[name].type == type)
        {
          cout << "Warning : symbol " << name << " declared more than once.\n";
          return getID(name);
        }
      else
        {
          string msg = "symbol " + name + " declared more than once with different types.";
          (* error) (msg.c_str());
          return -1;
        }
    }

}

void SymbolTable::AddSymbolRange(string name,int nbr,Type type, string tex_name)
{
}

void  SymbolTable::ResetType(string name,Type new_type)
{
  symboltable[name].type = new_type;
}

void  SymbolTable::SetReferenced(string name)
{
  symboltable[name].referenced = eReferenced;
}

100
Reference SymbolTable::isReferenced(const std::string &name) const
101
{
102
103
  symboltable_const_iterator iter = symboltable.find(name);
  return iter->second.referenced;
104
105
}

106
107
#if 0 // Commented out on 27/11/2006, SV

108
109
110
111
112
113
114
115
116
117
118
void SymbolTable::clean()
{
  string unused;
  bool   warning = false;
  vector<Type>  types(3);
  vector<int>   nb_type(3);

  types[0] = eEndogenous;
  types[1] = eExogenous;
  types[2] = eExogenousDet;

119
120
121
  nb_type[0] = mod_param.endo_nbr;
  nb_type[1] = mod_param.exo_nbr;
  nb_type[2] = mod_param.exo_det_nbr;
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

  // Removing unused variables
  for (int t = 0; t < 3; t++)
    {
      // Checking if all variables are used
      for (int s1 = 0; s1 < nb_type[t]; s1++)
        {
          string name = getNameByID(types[t],s1);
          string tex_name = getTexNameByID(types[t],s1);
          if (isReferenced(name) == eNotReferenced)
            {
              symboltable.erase(name);
              vector<string>::iterator it;
              it = find(name_table[types[t]].begin(), name_table[types[t]].end(), name);
              name_table[types[t]].erase(it);
              it = find(tex_name_table[types[t]].begin(), tex_name_table[types[t]].end(), tex_name);
              tex_name_table[types[t]].erase(it);
              nb_type[t]--;
              unused += "fprintf(1,'%-30s";
              switch(types[t])
                {
                case eEndogenous  : unused += "Endogenous variable\\n','";break;
                case eExogenous   : unused += "Exogenous variable\\n','";break;
                case eExogenousDet  : unused += "Exogenous deterministic variable\\n','";break;
                default : ;
                }
              unused += name;
              unused += "');\n";
              warning = true;
              for (int s2 = s1; s2 < nb_type[t]; s2++)
                {
                  name = getNameByID(types[t],s2);
                  // Decrementing symbol table ids in ST
                  symboltable[name].id--;
                }
              s1--;
            }
        }
    }
161
162
163
  mod_param.endo_nbr     = nb_type[0];
  mod_param.exo_nbr    = nb_type[1];
  mod_param.exo_det_nbr  = nb_type[2];
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  /*
  // Checking if unused parameters
  for (int s1 = 0; s1 < ModelParameters::parameter_nbr; s1++)
  {
  string name = getNameByID(eParameter,s1);
  if (isReferenced(name) == eNotReferenced)
  {
  unused += "fprintf(1,'%-30sParameter\\n','";
  unused += name;
  unused += "');\n";

  warning = true;
  }
  }
  */
  if (warning)
    {
      output << "fprintf(1,'Warning : symbol(s) :\\n');\n";
      output << unused;
      output << "fprintf(1,'are declared but not used in the model equations. ');\n";
      output << "reply = input('Continue? [y]\\\\n ','s');\n";
      output << "if isempty(reply), reply='y'; end;\n";
      output << "if strcmpi(reply(1),'n'),\n  return;\nend\n";
    }
  //PrintSymbolTable();
}
190
#endif // Comment
191
192
193

string SymbolTable::get()
{
194
195
196
  ostringstream output;

  if (mod_param.exo_nbr > 0)
197
198
199
    {
      output << "M_.exo_names = '" << getNameByID(eExogenous, 0) << "';\n";
      output << "M_.exo_names_tex = '" << getTexNameByID(eExogenous, 0) << "';\n";
200
      for (int id = 1; id < mod_param.exo_nbr; id++)
201
202
203
204
205
        {
          output << "M_.exo_names = " + interfaces::strvcat("M_.exo_names","'"+getNameByID(eExogenous, id)+"'") + ";\n";
          output << "M_.exo_names_tex = " + interfaces::strvcat("M_.exo_names_tex","'"+getTexNameByID(eExogenous, id)+"'") + ";\n";
        }
    }
206
  if (mod_param.exo_det_nbr > 0)
207
208
209
    {
      output << "lgxdet_ = '" << getNameByID(eExogenousDet, 0) << "';\n";
      output << "lgxdet_tex_ = '" << getTexNameByID(eExogenousDet, 0) << "';\n";
210
      for (int id = 1; id < mod_param.exo_det_nbr; id++)
211
212
213
214
215
        {
          output << "lgxdet_ = " + interfaces::strvcat("lgxdet_","'"+getNameByID(eExogenousDet, id)+"'") + ";\n";
          output << "lgxdet_tex_ = " + interfaces::strvcat("lgxdet_tex_","'"+getTexNameByID(eExogenousDet, id)+"'") + ";\n";
        }
    }
216
  if (mod_param.endo_nbr > 0)
217
218
219
    {
      output << "M_.endo_names = '" << getNameByID(eEndogenous, 0) << "';\n";
      output << "M_.endo_names_tex = '" << getTexNameByID(eEndogenous, 0) << "';\n";
220
      for (int id = 1; id < mod_param.endo_nbr; id++)
221
222
223
224
225
        {
          output << "M_.endo_names = " + interfaces::strvcat("M_.endo_names","'"+getNameByID(eEndogenous, id)+"'") + ";\n";
          output << "M_.endo_names_tex = " + interfaces::strvcat("M_.endo_names_tex","'"+getTexNameByID(eEndogenous, id)+"'") + ";\n";
        }
    }
226
  if (mod_param.recur_nbr > 0)
227
228
229
    {
      output << "M_.recur_names = '" << getNameByID(eRecursiveVariable, 0) << "';\n";
      output << "M_.recur_names_tex = '" << getTexNameByID(eRecursiveVariable, 0) << "';\n";
230
      for (int id = 1; id < mod_param.recur_nbr; id++)
231
232
233
234
235
        {
          output << "M_.recur_names = " + interfaces::strvcat("M_.recur_names","'"+getNameByID(eRecursiveVariable, id)+"'") + ";\n";
          output << "M_.recur_names_tex = " + interfaces::strvcat("M_.recur_names_tex","'"+getTexNameByID(eRecursiveVariable, id)+"'") + ";\n";
        }
    }
236
  if (mod_param.parameter_nbr > 0)
237
238
239
    {
      output << "M_.param_names = '" << getNameByID(eParameter, 0) << "';\n";
      output << "M_.param_names_tex = '" << getTexNameByID(eParameter, 0) << "';\n";
240
      for (int id = 1; id < mod_param.parameter_nbr; id++)
241
242
243
244
245
246
        {
          output << "M_.param_names = " + interfaces::strvcat("M_.param_names","'"+getNameByID(eParameter, id)+"'") + ";\n";
          output << "M_.param_names_tex = " + interfaces::strvcat("M_.param_names_tex","'"+getTexNameByID(eParameter, id)+"'") + ";\n";
        }
    }

247
248
249
250
251
252
253
  output << "M_.exo_det_nbr = " << mod_param.exo_det_nbr << ";\n";
  output << "M_.exo_nbr = " << mod_param.exo_nbr << ";\n";
  output << "M_.Sigma_e = zeros(" << mod_param.exo_nbr
         << ", " << mod_param.exo_nbr << ");\n";
  output << "M_.endo_nbr = " << mod_param.endo_nbr << ";\n";
  output << "M_.recur_nbr = " << mod_param.recur_nbr << ";\n";
  output << "M_.param_nbr = " << mod_param.parameter_nbr << ";\n";
254
255
  return output.str();
}