SymbolTable.cc 4.91 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
32
void
SymbolTable::addSymbol(const string &name, Type type, const string &tex_name) throw (AlreadyDeclaredException)
33
{
sebastien's avatar
sebastien committed
34
35
36
37
38
39
40
  if (exists(name))
    if (symbol_table[name].first == type)
      throw AlreadyDeclaredException(name, true);
    else
      throw AlreadyDeclaredException(name, false);

  int id;
41
42
43
44

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

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

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

sebastien's avatar
sebastien committed
130
131
132
133
134
135
136
  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;
137
}