SymbolTable.cc 4.33 KB
Newer Older
sebastien's avatar
sebastien committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (C) 2003-2008 Dynare Team
 *
 * 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
22
#include <sstream>

23
#include "SymbolTable.hh"
24

25
SymbolTable::SymbolTable() : endo_nbr(0), exo_nbr(0), exo_det_nbr(0),
sebastien's avatar
sebastien committed
26
27
                             parameter_nbr(0), model_local_variable_nbr(0),
                             modfile_local_variable_nbr(0), unknown_function_nbr(0)
28
29
30
{
}

sebastien's avatar
sebastien committed
31
void
32
SymbolTable::addSymbol(const string &name, SymbolType type, const string &tex_name) throw (AlreadyDeclaredException)
33
{
sebastien's avatar
sebastien committed
34
  if (exists(name))
35
36
37
38
39
40
    {
      if (symbol_table[name].first == type)
        throw AlreadyDeclaredException(name, true);
      else
        throw AlreadyDeclaredException(name, false);
    }
sebastien's avatar
sebastien committed
41
42

  int id;
43
44
45
46

  switch (type)
    {
    case eExogenous:
sebastien's avatar
sebastien committed
47
48
      id = exo_nbr++;
      break;
49
    case eExogenousDet:
sebastien's avatar
sebastien committed
50
51
      id = exo_det_nbr++;
      break;
52
    case eEndogenous:
sebastien's avatar
sebastien committed
53
54
      id = endo_nbr++;
      break;
55
    case eParameter:
sebastien's avatar
sebastien committed
56
57
      id = parameter_nbr++;
      break;
sebastien's avatar
sebastien committed
58
    case eModelLocalVariable:
sebastien's avatar
sebastien committed
59
60
      id = model_local_variable_nbr++;
      break;
sebastien's avatar
sebastien committed
61
    case eModFileLocalVariable:
sebastien's avatar
sebastien committed
62
63
      id = modfile_local_variable_nbr++;
      break;
64
    case eUnknownFunction:
sebastien's avatar
sebastien committed
65
66
      id = unknown_function_nbr++;
      break;
67
68
    }

69
  named_symbol_type symbol(type, id); 
sebastien's avatar
sebastien committed
70
71
72
  symbol_table[name] = symbol;
  name_table[symbol] = name;
  tex_name_table[symbol] = tex_name;
73
74
}

sebastien's avatar
sebastien committed
75
void
sebastien's avatar
sebastien committed
76
SymbolTable::writeOutput(ostream &output) const
77
{
78
  if (exo_nbr > 0)
79
    {
sebastien's avatar
sebastien committed
80
81
      output << "M_.exo_names = '" << getNameByID(eExogenous, 0) << "';" << endl;
      output << "M_.exo_names_tex = '" << getTeXNameByID(eExogenous, 0) << "';" << endl;
82
      for (int id = 1; id < exo_nbr; id++)
83
        {
sebastien's avatar
sebastien committed
84
85
          output << "M_.exo_names = strvcat(M_.exo_names, '" << getNameByID(eExogenous, id) << "');" << endl
                 << "M_.exo_names_tex = strvcat(M_.exo_names_tex, '" << getTeXNameByID(eExogenous, id) << "');" << endl;
86
87
        }
    }
88
  if (exo_det_nbr > 0)
89
    {
michel's avatar
michel committed
90
91
      output << "M_.exo_det_names = '" << getNameByID(eExogenousDet, 0) << "';" << endl;
      output << "M_.exo_det_names_tex = '" << getTeXNameByID(eExogenousDet, 0) << "';" << endl;
92
      for (int id = 1; id < exo_det_nbr; id++)
93
        {
michel's avatar
michel committed
94
95
          output << "M_.exo_det_names = srtvcat(M_.exo_det_names, '" << getNameByID(eExogenousDet, id) << "');" << endl
                 << "M_.exo_det_names_tex = strvcat(M_.exo_det_names_tex, '" << getTeXNameByID(eExogenousDet, id) << "');" << endl;
96
97
        }
    }
98
  if (endo_nbr > 0)
99
    {
sebastien's avatar
sebastien committed
100
101
      output << "M_.endo_names = '" << getNameByID(eEndogenous, 0) << "';" << endl;
      output << "M_.endo_names_tex = '" << getTeXNameByID(eEndogenous, 0) << "';" << endl;
102
      for (int id = 1; id < endo_nbr; id++)
103
        {
sebastien's avatar
sebastien committed
104
105
          output << "M_.endo_names = strvcat(M_.endo_names, '" << getNameByID(eEndogenous, id) << "');" << endl
                 << "M_.endo_names_tex = strvcat(M_.endo_names_tex, '" << getTeXNameByID(eEndogenous, id) << "');" << endl;
106
107
        }
    }
108
  if (parameter_nbr > 0)
109
    {
sebastien's avatar
sebastien committed
110
111
      output << "M_.param_names = '" << getNameByID(eParameter, 0) << "';" << endl;
      output << "M_.param_names_tex = '" << getTeXNameByID(eParameter, 0) << "';" << endl;
112
      for (int id = 1; id < parameter_nbr; id++)
113
        {
sebastien's avatar
sebastien committed
114
115
          output << "M_.param_names = strvcat(M_.param_names, '" << getNameByID(eParameter, id) << "');" << endl
                 << "M_.param_names_tex = strvcat(M_.param_names_tex, '" << getTeXNameByID(eParameter, id) << "');" << endl;
116
117
118
        }
    }

sebastien's avatar
sebastien committed
119
120
121
122
123
124
  output << "M_.exo_det_nbr = " << exo_det_nbr << ";" << endl
         << "M_.exo_nbr = " << exo_nbr << ";" << endl
         << "M_.endo_nbr = " << endo_nbr << ";" << endl
         << "M_.param_nbr = " << parameter_nbr << ";" << endl;

  output << "M_.Sigma_e = zeros(" << exo_nbr << ", " << exo_nbr << ");" << endl;
125
}