SymbolTable.cc 4.42 KB
Newer Older
1
#include <algorithm>
2
3
#include <sstream>

4
5
#include "SymbolTable.hh"
#include "Interface.hh"
6

7
SymbolTable::SymbolTable() : endo_nbr(0), exo_nbr(0), exo_det_nbr(0), parameter_nbr(0),
sebastien's avatar
sebastien committed
8
                             model_local_variable_nbr(0), modfile_local_variable_nbr(0),
9
                             recur_nbr(0), unknown_function_nbr(0)
10
11
12
{
}

sebastien's avatar
sebastien committed
13
14
void
SymbolTable::addSymbol(const string &name, Type type, const string &tex_name) throw (AlreadyDeclaredException)
15
{
sebastien's avatar
sebastien committed
16
17
18
19
20
21
22
  if (exists(name))
    if (symbol_table[name].first == type)
      throw AlreadyDeclaredException(name, true);
    else
      throw AlreadyDeclaredException(name, false);

  int id;
23
24
25
26

  switch (type)
    {
    case eExogenous:
sebastien's avatar
sebastien committed
27
28
      id = exo_nbr++;
      break;
29
    case eExogenousDet:
sebastien's avatar
sebastien committed
30
31
      id = exo_det_nbr++;
      break;
32
    case eEndogenous:
sebastien's avatar
sebastien committed
33
34
      id = endo_nbr++;
      break;
35
    case eParameter:
sebastien's avatar
sebastien committed
36
37
      id = parameter_nbr++;
      break;
38
    case eRecursiveVariable:
sebastien's avatar
sebastien committed
39
40
      id = recur_nbr++;
      break;
sebastien's avatar
sebastien committed
41
    case eModelLocalVariable:
sebastien's avatar
sebastien committed
42
43
      id = model_local_variable_nbr++;
      break;
sebastien's avatar
sebastien committed
44
    case eModFileLocalVariable:
sebastien's avatar
sebastien committed
45
46
      id = modfile_local_variable_nbr++;
      break;
47
    case eUnknownFunction:
sebastien's avatar
sebastien committed
48
49
      id = unknown_function_nbr++;
      break;
50
51
    }

sebastien's avatar
sebastien committed
52
53
54
55
  symbol_type symbol(type, id); 
  symbol_table[name] = symbol;
  name_table[symbol] = name;
  tex_name_table[symbol] = tex_name;
56
57
}

sebastien's avatar
sebastien committed
58
void
sebastien's avatar
sebastien committed
59
SymbolTable::writeOutput(ostream &output) const
60
{
61
  if (exo_nbr > 0)
62
    {
sebastien's avatar
sebastien committed
63
64
      output << "M_.exo_names = '" << getNameByID(eExogenous, 0) << "';" << endl;
      output << "M_.exo_names_tex = '" << getTeXNameByID(eExogenous, 0) << "';" << endl;
65
      for (int id = 1; id < exo_nbr; id++)
66
        {
sebastien's avatar
sebastien committed
67
68
          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;
69
70
        }
    }
71
  if (exo_det_nbr > 0)
72
    {
sebastien's avatar
sebastien committed
73
74
      output << "lgxdet_ = '" << getNameByID(eExogenousDet, 0) << "';" << endl;
      output << "lgxdet_tex_ = '" << getTeXNameByID(eExogenousDet, 0) << "';" << endl;
75
      for (int id = 1; id < exo_det_nbr; id++)
76
        {
sebastien's avatar
sebastien committed
77
78
          output << "lgxdet_ = " << interfaces::strvcat("lgxdet_","'"+getNameByID(eExogenousDet, id)+"'") << ";" << endl;
          output << "lgxdet_tex_ = " << interfaces::strvcat("lgxdet_tex_","'"+getTeXNameByID(eExogenousDet, id)+"'") << ";" << endl;
79
80
        }
    }
81
  if (endo_nbr > 0)
82
    {
sebastien's avatar
sebastien committed
83
84
      output << "M_.endo_names = '" << getNameByID(eEndogenous, 0) << "';" << endl;
      output << "M_.endo_names_tex = '" << getTeXNameByID(eEndogenous, 0) << "';" << endl;
85
      for (int id = 1; id < endo_nbr; id++)
86
        {
sebastien's avatar
sebastien committed
87
88
          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;
89
90
        }
    }
91
  if (recur_nbr > 0)
92
    {
sebastien's avatar
sebastien committed
93
94
      output << "M_.recur_names = '" << getNameByID(eRecursiveVariable, 0) << "';" << endl;
      output << "M_.recur_names_tex = '" << getTeXNameByID(eRecursiveVariable, 0) << "';" << endl;
95
      for (int id = 1; id < recur_nbr; id++)
96
        {
sebastien's avatar
sebastien committed
97
98
          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;
99
100
        }
    }
101
  if (parameter_nbr > 0)
102
    {
sebastien's avatar
sebastien committed
103
104
      output << "M_.param_names = '" << getNameByID(eParameter, 0) << "';" << endl;
      output << "M_.param_names_tex = '" << getTeXNameByID(eParameter, 0) << "';" << endl;
105
      for (int id = 1; id < parameter_nbr; id++)
106
        {
sebastien's avatar
sebastien committed
107
108
          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;
109
110
111
        }
    }

sebastien's avatar
sebastien committed
112
113
114
115
116
117
118
  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;
119
}