SymbolTable.cc 5.12 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
24
#include "SymbolTable.hh"
#include "Interface.hh"
25

sebastien's avatar
sebastien committed
26
27
28
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)
29
30
31
{
}

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

  int id;
42
43
44
45

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

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

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

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