SymbolTable.cc 5.22 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
SymbolTable::SymbolTable() : endo_nbr(0), exo_nbr(0), exo_det_nbr(0), parameter_nbr(0),
sebastien's avatar
sebastien committed
16
17
                             model_local_variable_nbr(0), modfile_local_variable_nbr(0),
                             recur_nbr(0)
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;
  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++;
sebastien's avatar
sebastien committed
46
47
48
49
50
51
    case eModelLocalVariable:
      symboltable[name].id = model_local_variable_nbr;
      return model_local_variable_nbr++;
    case eModFileLocalVariable:
      symboltable[name].id = modfile_local_variable_nbr;
      return modfile_local_variable_nbr++;
52
    }
sebastien's avatar
sebastien committed
53
54
  // should never happen
  return -1;
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
}

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;
}

sebastien's avatar
sebastien committed
92
void
sebastien's avatar
sebastien committed
93
SymbolTable::writeOutput(ostream &output) const
94
{
95
  if (exo_nbr > 0)
96
97
98
    {
      output << "M_.exo_names = '" << getNameByID(eExogenous, 0) << "';\n";
      output << "M_.exo_names_tex = '" << getTexNameByID(eExogenous, 0) << "';\n";
99
      for (int id = 1; id < exo_nbr; id++)
100
101
102
103
104
        {
          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";
        }
    }
105
  if (exo_det_nbr > 0)
106
107
108
    {
      output << "lgxdet_ = '" << getNameByID(eExogenousDet, 0) << "';\n";
      output << "lgxdet_tex_ = '" << getTexNameByID(eExogenousDet, 0) << "';\n";
109
      for (int id = 1; id < exo_det_nbr; id++)
110
111
112
113
114
        {
          output << "lgxdet_ = " + interfaces::strvcat("lgxdet_","'"+getNameByID(eExogenousDet, id)+"'") + ";\n";
          output << "lgxdet_tex_ = " + interfaces::strvcat("lgxdet_tex_","'"+getTexNameByID(eExogenousDet, id)+"'") + ";\n";
        }
    }
115
  if (endo_nbr > 0)
116
117
118
    {
      output << "M_.endo_names = '" << getNameByID(eEndogenous, 0) << "';\n";
      output << "M_.endo_names_tex = '" << getTexNameByID(eEndogenous, 0) << "';\n";
119
      for (int id = 1; id < endo_nbr; id++)
120
121
122
123
124
        {
          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";
        }
    }
125
  if (recur_nbr > 0)
126
127
128
    {
      output << "M_.recur_names = '" << getNameByID(eRecursiveVariable, 0) << "';\n";
      output << "M_.recur_names_tex = '" << getTexNameByID(eRecursiveVariable, 0) << "';\n";
129
      for (int id = 1; id < recur_nbr; id++)
130
131
132
133
134
        {
          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";
        }
    }
135
  if (parameter_nbr > 0)
136
137
138
    {
      output << "M_.param_names = '" << getNameByID(eParameter, 0) << "';\n";
      output << "M_.param_names_tex = '" << getTexNameByID(eParameter, 0) << "';\n";
139
      for (int id = 1; id < parameter_nbr; id++)
140
141
142
143
144
145
        {
          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";
        }
    }

146
147
148
149
150
151
152
  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";
153
}