SymbolTable.cc 4.99 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

sebastien's avatar
sebastien committed
25
26
27
SymbolTable::SymbolTable() : endo_nbr(0), exo_nbr(0), exo_det_nbr(0), recur_nbr(0), 
                             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;
58
    case eRecursiveVariable:
sebastien's avatar
sebastien committed
59
60
      id = recur_nbr++;
      break;
sebastien's avatar
sebastien committed
61
    case eModelLocalVariable:
sebastien's avatar
sebastien committed
62
63
      id = model_local_variable_nbr++;
      break;
sebastien's avatar
sebastien committed
64
    case eModFileLocalVariable:
sebastien's avatar
sebastien committed
65
66
      id = modfile_local_variable_nbr++;
      break;
67
    case eUnknownFunction:
sebastien's avatar
sebastien committed
68
69
      id = unknown_function_nbr++;
      break;
70
71
    }

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

sebastien's avatar
sebastien committed
78
void
sebastien's avatar
sebastien committed
79
SymbolTable::writeOutput(ostream &output) const
80
{
81
  if (exo_nbr > 0)
82
    {
sebastien's avatar
sebastien committed
83
84
      output << "M_.exo_names = '" << getNameByID(eExogenous, 0) << "';" << endl;
      output << "M_.exo_names_tex = '" << getTeXNameByID(eExogenous, 0) << "';" << endl;
85
      for (int id = 1; id < exo_nbr; id++)
86
        {
sebastien's avatar
sebastien committed
87
88
          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;
89
90
        }
    }
91
  if (exo_det_nbr > 0)
92
    {
michel's avatar
michel committed
93
94
      output << "M_.exo_det_names = '" << getNameByID(eExogenousDet, 0) << "';" << endl;
      output << "M_.exo_det_names_tex = '" << getTeXNameByID(eExogenousDet, 0) << "';" << endl;
95
      for (int id = 1; id < exo_det_nbr; id++)
96
        {
michel's avatar
michel committed
97
98
          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;
99
100
        }
    }
101
  if (endo_nbr > 0)
102
    {
sebastien's avatar
sebastien committed
103
104
      output << "M_.endo_names = '" << getNameByID(eEndogenous, 0) << "';" << endl;
      output << "M_.endo_names_tex = '" << getTeXNameByID(eEndogenous, 0) << "';" << endl;
105
      for (int id = 1; id < endo_nbr; id++)
106
        {
sebastien's avatar
sebastien committed
107
108
          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;
109
110
        }
    }
111
  if (recur_nbr > 0)
112
    {
sebastien's avatar
sebastien committed
113
114
      output << "M_.recur_names = '" << getNameByID(eRecursiveVariable, 0) << "';" << endl;
      output << "M_.recur_names_tex = '" << getTeXNameByID(eRecursiveVariable, 0) << "';" << endl;
115
      for (int id = 1; id < recur_nbr; id++)
116
        {
sebastien's avatar
sebastien committed
117
118
          output << "M_.recur_names = strvcat(M_.recur_names, '" << getNameByID(eRecursiveVariable, id) << "');" << endl
                 << "M_.recur_names_tex = strvcat(M_.recur_names_tex, '" << getTeXNameByID(eRecursiveVariable, id) << "');" << endl;
119
120
        }
    }
121
  if (parameter_nbr > 0)
122
    {
sebastien's avatar
sebastien committed
123
124
      output << "M_.param_names = '" << getNameByID(eParameter, 0) << "';" << endl;
      output << "M_.param_names_tex = '" << getTeXNameByID(eParameter, 0) << "';" << endl;
125
      for (int id = 1; id < parameter_nbr; id++)
126
        {
sebastien's avatar
sebastien committed
127
128
          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;
129
130
131
        }
    }

sebastien's avatar
sebastien committed
132
133
134
135
136
137
138
  output << "M_.exo_det_nbr = " << exo_det_nbr << ";" << endl
         << "M_.exo_nbr = " << exo_nbr << ";" << endl
         << "M_.endo_nbr = " << endo_nbr << ";" << endl
         << "M_.recur_nbr = " << recur_nbr << ";" << endl
         << "M_.param_nbr = " << parameter_nbr << ";" << endl;

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