SymbolTable.cc 10.6 KB
Newer Older
sebastien's avatar
sebastien committed
1
/*
sebastien's avatar
sebastien committed
2
 * Copyright (C) 2003-2009 Dynare Team
sebastien's avatar
sebastien committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 */

20
#include <algorithm>
21
#include <sstream>
sebastien's avatar
sebastien committed
22
#include <iostream>
sebastien's avatar
sebastien committed
23
#include <cassert>
24

25
#include "SymbolTable.hh"
26

sebastien's avatar
sebastien committed
27
SymbolTable::SymbolTable() : frozen(false), size(0)
28
29
30
{
}

sebastien's avatar
sebastien committed
31
int
sebastien's avatar
sebastien committed
32
SymbolTable::addSymbol(const string &name, SymbolType type, const string &tex_name) throw (AlreadyDeclaredException, FrozenException)
33
{
sebastien's avatar
sebastien committed
34
35
36
  if (frozen)
    throw FrozenException();

sebastien's avatar
sebastien committed
37
  if (exists(name))
38
    {
sebastien's avatar
sebastien committed
39
      if (type_table[getID(name)] == type)
40
41
42
43
        throw AlreadyDeclaredException(name, true);
      else
        throw AlreadyDeclaredException(name, false);
    }
sebastien's avatar
sebastien committed
44

sebastien's avatar
sebastien committed
45
46
47
48
49
50
  int id = size++;

  symbol_table[name] = id;
  type_table.push_back(type);
  name_table.push_back(name);
  tex_name_table.push_back(tex_name);
sebastien's avatar
sebastien committed
51
52

  return id;
sebastien's avatar
sebastien committed
53
54
}

sebastien's avatar
sebastien committed
55
int
56
57
58
59
60
61
62
63
64
65
SymbolTable::addSymbol(const string &name, SymbolType type) throw (AlreadyDeclaredException, FrozenException)
{
  // Construct "tex_name" by prepending an antislash to all underscores in "name"
  string tex_name = name;
  size_t pos = 0;
  while((pos = tex_name.find('_', pos)) != string::npos)
    {
      tex_name.insert(pos, "\\");
      pos += 2;
    }
sebastien's avatar
sebastien committed
66
  return addSymbol(name, type, tex_name);
67
68
}

sebastien's avatar
sebastien committed
69
70
71
72
73
void
SymbolTable::freeze() throw (FrozenException)
{
  if (frozen)
    throw FrozenException();
74

sebastien's avatar
sebastien committed
75
76
77
  frozen = true;

  for(int i = 0; i < size; i++)
78
    {
sebastien's avatar
sebastien committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
      int tsi;
      switch(getType(i))
        {
        case eEndogenous:
          tsi = endo_ids.size();
          endo_ids.push_back(i);
          break;
        case eExogenous:
          tsi = exo_ids.size();
          exo_ids.push_back(i);
          break;
        case eExogenousDet:
          tsi = exo_det_ids.size();
          exo_det_ids.push_back(i);
          break;
        case eParameter:
          tsi = param_ids.size();
          param_ids.push_back(i);
          break;
        default:
          tsi = -1;
          break;
        }
      type_specific_ids.push_back(tsi);
    }
}

void
SymbolTable::changeType(int id, SymbolType newtype) throw (UnknownSymbolIDException, FrozenException)
{
  if (frozen)
    throw FrozenException();

  if (id < 0 || id >= size)
    throw UnknownSymbolIDException(id);

  type_table[id] = newtype;
}

int
SymbolTable::getID(SymbolType type, int tsid) const throw (UnknownTypeSpecificIDException, NotYetFrozenException)
{
  if (!frozen)
    throw NotYetFrozenException();

  switch(type)
    {
    case eEndogenous:
      if (tsid < 0 || tsid >= (int) endo_ids.size())
        throw UnknownTypeSpecificIDException(tsid, type);
      else
        return endo_ids[tsid];
131
    case eExogenous:
sebastien's avatar
sebastien committed
132
133
134
135
      if (tsid < 0 || tsid >= (int) exo_ids.size())
        throw UnknownTypeSpecificIDException(tsid, type);
      else
        return exo_ids[tsid];
136
    case eExogenousDet:
sebastien's avatar
sebastien committed
137
138
139
140
      if (tsid < 0 || tsid >= (int) exo_det_ids.size())
        throw UnknownTypeSpecificIDException(tsid, type);
      else
        return exo_det_ids[tsid];
141
    case eParameter:
sebastien's avatar
sebastien committed
142
143
144
145
146
147
      if (tsid < 0 || tsid >= (int) param_ids.size())
        throw UnknownTypeSpecificIDException(tsid, type);
      else
        return param_ids[tsid];
    default:
      throw UnknownTypeSpecificIDException(tsid, type);
148
149
150
    }
}

sebastien's avatar
sebastien committed
151
void
sebastien's avatar
sebastien committed
152
SymbolTable::writeOutput(ostream &output) const throw (NotYetFrozenException)
153
{
sebastien's avatar
sebastien committed
154
155
156
157
  if (!frozen)
    throw NotYetFrozenException();

  if (exo_nbr() > 0)
158
    {
sebastien's avatar
sebastien committed
159
160
161
      output << "M_.exo_names = '" << getName(exo_ids[0]) << "';" << endl;
      output << "M_.exo_names_tex = '" << getTeXName(exo_ids[0]) << "';" << endl;
      for (int id = 1; id < exo_nbr(); id++)
162
        {
sebastien's avatar
sebastien committed
163
164
          output << "M_.exo_names = strvcat(M_.exo_names, '" << getName(exo_ids[id]) << "');" << endl
                 << "M_.exo_names_tex = strvcat(M_.exo_names_tex, '" << getTeXName(exo_ids[id]) << "');" << endl;
165
166
        }
    }
sebastien's avatar
sebastien committed
167
  if (exo_det_nbr() > 0)
168
    {
sebastien's avatar
sebastien committed
169
170
171
      output << "M_.exo_det_names = '" << getName(exo_det_ids[0]) << "';" << endl;
      output << "M_.exo_det_names_tex = '" << getTeXName(exo_det_ids[0]) << "';" << endl;
      for (int id = 1; id < exo_det_nbr(); id++)
172
        {
sebastien's avatar
sebastien committed
173
174
          output << "M_.exo_det_names = strvcat(M_.exo_det_names, '" << getName(exo_det_ids[id]) << "');" << endl
                 << "M_.exo_det_names_tex = strvcat(M_.exo_det_names_tex, '" << getTeXName(exo_det_ids[id]) << "');" << endl;
175
176
        }
    }
sebastien's avatar
sebastien committed
177
  if (endo_nbr() > 0)
178
    {
sebastien's avatar
sebastien committed
179
180
181
      output << "M_.endo_names = '" << getName(endo_ids[0]) << "';" << endl;
      output << "M_.endo_names_tex = '" << getTeXName(endo_ids[0]) << "';" << endl;
      for (int id = 1; id < endo_nbr(); id++)
182
        {
sebastien's avatar
sebastien committed
183
184
          output << "M_.endo_names = strvcat(M_.endo_names, '" << getName(endo_ids[id]) << "');" << endl
                 << "M_.endo_names_tex = strvcat(M_.endo_names_tex, '" << getTeXName(endo_ids[id]) << "');" << endl;
185
186
        }
    }
sebastien's avatar
sebastien committed
187
  if (param_nbr() > 0)
188
    {
sebastien's avatar
sebastien committed
189
190
191
      output << "M_.param_names = '" << getName(param_ids[0]) << "';" << endl;
      output << "M_.param_names_tex = '" << getTeXName(param_ids[0]) << "';" << endl;
      for (int id = 1; id < param_nbr(); id++)
192
        {
sebastien's avatar
sebastien committed
193
194
          output << "M_.param_names = strvcat(M_.param_names, '" << getName(param_ids[id]) << "');" << endl
                 << "M_.param_names_tex = strvcat(M_.param_names_tex, '" << getTeXName(param_ids[id]) << "');" << endl;
195
196
197
        }
    }

sebastien's avatar
sebastien committed
198
199
200
201
  output << "M_.exo_det_nbr = " << exo_det_nbr() << ";" << endl
         << "M_.exo_nbr = " << exo_nbr() << ";" << endl
         << "M_.endo_nbr = " << endo_nbr() << ";" << endl
         << "M_.param_nbr = " << param_nbr() << ";" << endl;
sebastien's avatar
sebastien committed
202

sebastien's avatar
sebastien committed
203
  output << "M_.Sigma_e = zeros(" << exo_nbr() << ", " << exo_nbr() << ");" << endl;
sebastien's avatar
sebastien committed
204
205

  // Write the auxiliary variable table
sebastien's avatar
sebastien committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  output << "M_.orig_endo_nbr = " << endo_nbr() - aux_vars.size() << ";" << endl;
  if (aux_vars.size() == 0)
    output << "M_.aux_vars = [];" << endl;
  else
    for(int i = 0; i < (int) aux_vars.size(); i++)
      {
        output << "M_.aux_vars(" << i+1 << ").endo_index = " << getTypeSpecificID(aux_vars[i].symb_id)+1 << ";" << endl
               << "M_.aux_vars(" << i+1 << ").type = " << aux_vars[i].type << ";" << endl;
        switch(aux_vars[i].type)
          {
          case avEndoLead:
          case avExoLead:
          case avExpectation:
            break;
          case avEndoLag:
          case avExoLag:
            output << "M_.aux_vars(" << i+1 << ").orig_index = " << getTypeSpecificID(aux_vars[i].orig_symb_id)+1 << ";" << endl
                   << "M_.aux_vars(" << i+1 << ").orig_lead_lag = " << aux_vars[i].orig_lead_lag << ";" << endl;
            break;
          }
      }
sebastien's avatar
sebastien committed
227
228
229
230
231
232

  if (predeterminedNbr() > 0)
    {
      output << "M_.predetermined_variables = [ ";
      for(set<int>::const_iterator it = predetermined_variables.begin();
          it != predetermined_variables.end(); it++)
233
        output << getTypeSpecificID(*it)+1 << " ";
sebastien's avatar
sebastien committed
234
235
      output << "];" << endl;
    }
sebastien's avatar
sebastien committed
236
237
238
}

int
sebastien's avatar
sebastien committed
239
SymbolTable::addLeadAuxiliaryVarInternal(bool endo, int index) throw (FrozenException)
sebastien's avatar
sebastien committed
240
241
{
  ostringstream varname;
sebastien's avatar
sebastien committed
242
243
244
245
246
  if (endo)
    varname << "AUX_ENDO_LEAD_";
  else
    varname << "AUX_EXO_LEAD_";
  varname << index;
sebastien's avatar
sebastien committed
247
248
249
250
251
252
253
254
255
256
257
258
259
  int symb_id;
  try
    {
      symb_id = addSymbol(varname.str(), eEndogenous);
    }
  catch(AlreadyDeclaredException &e)
    {
      cerr << "ERROR: you should rename your variable called " << varname.str() << ", this name is internally used by Dynare" << endl;
      exit(EXIT_FAILURE);
    }

  AuxVarInfo avi;
  avi.symb_id = symb_id;
sebastien's avatar
sebastien committed
260
  avi.type = (endo ? avEndoLead : avExoLead);
sebastien's avatar
sebastien committed
261
262
263
264
265
266
  aux_vars.push_back(avi);

  return symb_id;
}

int
sebastien's avatar
sebastien committed
267
SymbolTable::addLagAuxiliaryVarInternal(bool endo, int orig_symb_id, int orig_lead_lag) throw (FrozenException)
sebastien's avatar
sebastien committed
268
269
{
  ostringstream varname;
sebastien's avatar
sebastien committed
270
271
272
273
  if (endo)
    varname << "AUX_ENDO_LAG_";
  else
    varname << "AUX_EXO_LAG_";
sebastien's avatar
sebastien committed
274
  varname << orig_symb_id << "_" << -orig_lead_lag;
275

sebastien's avatar
sebastien committed
276
277
278
279
280
281
282
283
284
285
286
287
288
  int symb_id;
  try
    {
      symb_id = addSymbol(varname.str(), eEndogenous);
    }
  catch(AlreadyDeclaredException &e)
    {
      cerr << "ERROR: you should rename your variable called " << varname.str() << ", this name is internally used by Dynare" << endl;
      exit(EXIT_FAILURE);
    }

  AuxVarInfo avi;
  avi.symb_id = symb_id;
sebastien's avatar
sebastien committed
289
  avi.type = (endo ? avEndoLag : avExoLag);
sebastien's avatar
sebastien committed
290
  avi.orig_symb_id = orig_symb_id;
sebastien's avatar
sebastien committed
291
  avi.orig_lead_lag = orig_lead_lag;
sebastien's avatar
sebastien committed
292
293
294
  aux_vars.push_back(avi);

  return symb_id;
295
}
296

sebastien's avatar
sebastien committed
297
298
299
300
301
302
int
SymbolTable::addEndoLeadAuxiliaryVar(int index) throw (FrozenException)
{
  return addLeadAuxiliaryVarInternal(true, index);
}

303
int
sebastien's avatar
sebastien committed
304
SymbolTable::addEndoLagAuxiliaryVar(int orig_symb_id, int orig_lead_lag) throw (FrozenException)
305
{
sebastien's avatar
sebastien committed
306
  return addLagAuxiliaryVarInternal(true, orig_symb_id, orig_lead_lag);
307
308
309
}

int
sebastien's avatar
sebastien committed
310
SymbolTable::addExoLeadAuxiliaryVar(int index) throw (FrozenException)
311
{
sebastien's avatar
sebastien committed
312
  return addLeadAuxiliaryVarInternal(false, index);
313
314
315
}

int
sebastien's avatar
sebastien committed
316
SymbolTable::addExoLagAuxiliaryVar(int orig_symb_id, int orig_lead_lag) throw (FrozenException)
317
{
sebastien's avatar
sebastien committed
318
  return addLagAuxiliaryVarInternal(false, orig_symb_id, orig_lead_lag);
319
}
320
321

int
sebastien's avatar
sebastien committed
322
SymbolTable::addExpectationAuxiliaryVar(int information_set, int index) throw (FrozenException)
323
324
{
  ostringstream varname;
sebastien's avatar
sebastien committed
325
326
  varname << "AUX_EXPECT_" << (information_set < 0 ? "LAG" : "LEAD") << "_"
          << abs(information_set) << "_" << index;
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  int symb_id;
  try
    {
      symb_id = addSymbol(varname.str(), eEndogenous);
    }
  catch(AlreadyDeclaredException &e)
    {
      cerr << "ERROR: you should rename your variable called " << varname.str() << ", this name is internally used by Dynare" << endl;
      exit(EXIT_FAILURE);
    }

  AuxVarInfo avi;
  avi.symb_id = symb_id;
  avi.type = avExpectation;
  aux_vars.push_back(avi);

  return symb_id;
}
sebastien's avatar
sebastien committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372

void
SymbolTable::markPredetermined(int symb_id) throw (UnknownSymbolIDException, FrozenException)
{
  if (symb_id < 0 || symb_id >= size)
    throw UnknownSymbolIDException(symb_id);
  if (frozen)
    throw FrozenException();

  assert(getType(symb_id) == eEndogenous);

  predetermined_variables.insert(symb_id);
}

bool
SymbolTable::isPredetermined(int symb_id) const throw (UnknownSymbolIDException)
{
  if (symb_id < 0 || symb_id >= size)
    throw UnknownSymbolIDException(symb_id);

  return(predetermined_variables.find(symb_id) != predetermined_variables.end());
}

int
SymbolTable::predeterminedNbr() const
{
  return(predetermined_variables.size());
}