SymbolTable.cc 5.34 KB
Newer Older
1
2
3
4
5
/*! \file
  \version 1.0
  \date 04/09/2004
  \par This file implements the SymbolTable class methodes.
*/
6

7
8
#include <iostream>
#include <algorithm>
9
10
#include <sstream>

11
12
13
#include "SymbolTable.hh"
#include "Interface.hh"
using namespace std;
14

15
16
SymbolTable::SymbolTable() : endo_nbr(0), exo_nbr(0), exo_det_nbr(0), parameter_nbr(0),
                             local_parameter_nbr(0), recur_nbr(0)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
  name_table.resize(20);
  tex_name_table.resize(20);
}

int SymbolTable::AddSymbol(string name,Type type, string tex_name)
{
  symboltable[name].type = type;
  symboltable[name].referenced = eNotReferenced;
  name_table[(int) type].push_back(name);
  tex_name_table[(int) type].push_back(tex_name);

  switch (type)
    {
    case eExogenous:
32
33
      symboltable[name].id = exo_nbr;
      return exo_nbr++;
34
    case eExogenousDet:
35
36
      symboltable[name].id = exo_det_nbr;
      return exo_det_nbr++;
37
    case eEndogenous:
38
39
      symboltable[name].id = endo_nbr;
      return endo_nbr++;
40
    case eParameter:
41
42
      symboltable[name].id = parameter_nbr;
      return parameter_nbr++;
43
    case eRecursiveVariable:
44
45
      symboltable[name].id = recur_nbr;
      return recur_nbr++;
46
    case eLocalParameter:
47
48
      symboltable[name].id = local_parameter_nbr;
      return local_parameter_nbr++;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    default:
      // should never happen
      return -1;
    }

}

int SymbolTable::AddSymbolDeclar(string name,Type type, string tex_name)
{
  //Testing if the symbol exist in the map
  if ( !Exist(name) )
    {
      //The symbol dosn't exist, adding it
      return AddSymbol(name,type, tex_name);
    }
  else
    {
      //The symbol exists, testing its type
      if (symboltable[name].type == type)
        {
          cout << "Warning : symbol " << name << " declared more than once.\n";
          return getID(name);
        }
      else
        {
          string msg = "symbol " + name + " declared more than once with different types.";
          (* error) (msg.c_str());
          return -1;
        }
    }

}

void SymbolTable::AddSymbolRange(string name,int nbr,Type type, string tex_name)
{
}

void  SymbolTable::ResetType(string name,Type new_type)
{
  symboltable[name].type = new_type;
}

void  SymbolTable::SetReferenced(string name)
{
  symboltable[name].referenced = eReferenced;
}

96
Reference SymbolTable::isReferenced(const std::string &name) const
97
{
98
99
  symboltable_const_iterator iter = symboltable.find(name);
  return iter->second.referenced;
100
101
}

sebastien's avatar
sebastien committed
102
103
void
SymbolTable::writeOutput(ostream &output)
104
{
105
  if (exo_nbr > 0)
106
107
108
    {
      output << "M_.exo_names = '" << getNameByID(eExogenous, 0) << "';\n";
      output << "M_.exo_names_tex = '" << getTexNameByID(eExogenous, 0) << "';\n";
109
      for (int id = 1; id < exo_nbr; id++)
110
111
112
113
114
        {
          output << "M_.exo_names = " + interfaces::strvcat("M_.exo_names","'"+getNameByID(eExogenous, id)+"'") + ";\n";
          output << "M_.exo_names_tex = " + interfaces::strvcat("M_.exo_names_tex","'"+getTexNameByID(eExogenous, id)+"'") + ";\n";
        }
    }
115
  if (exo_det_nbr > 0)
116
117
118
    {
      output << "lgxdet_ = '" << getNameByID(eExogenousDet, 0) << "';\n";
      output << "lgxdet_tex_ = '" << getTexNameByID(eExogenousDet, 0) << "';\n";
119
      for (int id = 1; id < exo_det_nbr; id++)
120
121
122
123
124
        {
          output << "lgxdet_ = " + interfaces::strvcat("lgxdet_","'"+getNameByID(eExogenousDet, id)+"'") + ";\n";
          output << "lgxdet_tex_ = " + interfaces::strvcat("lgxdet_tex_","'"+getTexNameByID(eExogenousDet, id)+"'") + ";\n";
        }
    }
125
  if (endo_nbr > 0)
126
127
128
    {
      output << "M_.endo_names = '" << getNameByID(eEndogenous, 0) << "';\n";
      output << "M_.endo_names_tex = '" << getTexNameByID(eEndogenous, 0) << "';\n";
129
      for (int id = 1; id < endo_nbr; id++)
130
131
132
133
134
        {
          output << "M_.endo_names = " + interfaces::strvcat("M_.endo_names","'"+getNameByID(eEndogenous, id)+"'") + ";\n";
          output << "M_.endo_names_tex = " + interfaces::strvcat("M_.endo_names_tex","'"+getTexNameByID(eEndogenous, id)+"'") + ";\n";
        }
    }
135
  if (recur_nbr > 0)
136
137
138
    {
      output << "M_.recur_names = '" << getNameByID(eRecursiveVariable, 0) << "';\n";
      output << "M_.recur_names_tex = '" << getTexNameByID(eRecursiveVariable, 0) << "';\n";
139
      for (int id = 1; id < recur_nbr; id++)
140
141
142
143
144
        {
          output << "M_.recur_names = " + interfaces::strvcat("M_.recur_names","'"+getNameByID(eRecursiveVariable, id)+"'") + ";\n";
          output << "M_.recur_names_tex = " + interfaces::strvcat("M_.recur_names_tex","'"+getTexNameByID(eRecursiveVariable, id)+"'") + ";\n";
        }
    }
145
  if (parameter_nbr > 0)
146
147
148
    {
      output << "M_.param_names = '" << getNameByID(eParameter, 0) << "';\n";
      output << "M_.param_names_tex = '" << getTexNameByID(eParameter, 0) << "';\n";
149
      for (int id = 1; id < parameter_nbr; id++)
150
151
152
153
154
155
        {
          output << "M_.param_names = " + interfaces::strvcat("M_.param_names","'"+getNameByID(eParameter, id)+"'") + ";\n";
          output << "M_.param_names_tex = " + interfaces::strvcat("M_.param_names_tex","'"+getTexNameByID(eParameter, id)+"'") + ";\n";
        }
    }

156
157
158
159
160
161
162
  output << "M_.exo_det_nbr = " << exo_det_nbr << ";\n";
  output << "M_.exo_nbr = " << exo_nbr << ";\n";
  output << "M_.Sigma_e = zeros(" << exo_nbr
         << ", " << exo_nbr << ");\n";
  output << "M_.endo_nbr = " << endo_nbr << ";\n";
  output << "M_.recur_nbr = " << recur_nbr << ";\n";
  output << "M_.param_nbr = " << parameter_nbr << ";\n";
163
}