SymbolTable.cc 10.1 KB
Newer Older
michel's avatar
michel committed
1
2
3
4
5
6
7
8
9
/*! \file 
 \version 1.0
 \date 04/09/2004
 \par This file implements the SymbolTable class methodes.
*/
//------------------------------------------------------------------------------
#include <iostream>
#include <algorithm>
//------------------------------------------------------------------------------
sebastien's avatar
sebastien committed
10
11
#include "SymbolTable.hh"
#include "Interface.hh"
michel's avatar
michel committed
12
13
14
15
16
17
18
19
20
21
using namespace std;
//------------------------------------------------------------------------------
map<string, Symbol, less<string> > SymbolTable::symboltable;// = *(new symbolmap);
vector< vector<string> > SymbolTable::name_table = *(new vector< vector<string> >);	
vector< vector<string> > SymbolTable::tex_name_table = *(new vector< vector<string> >);	
ostringstream	SymbolTable::output;	
void (* SymbolTable::error) (const char* ) = NULL;
//------------------------------------------------------------------------------
SymbolTable::SymbolTable()
{
michel's avatar
michel committed
22
23
	  name_table.resize(20);
	  tex_name_table.resize(20);
michel's avatar
michel committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
}
//------------------------------------------------------------------------------
SymbolTable::~SymbolTable()
{
	  // Empty
}
//------------------------------------------------------------------------------
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);  
  
38
39
40
  switch (type)
    {
    case eExogenous:
michel's avatar
michel committed
41
	symboltable[name].id = ModelParameters::exo_nbr;
42
43
44
	ModelParameters::exo_nbr++;   
	return  ModelParameters::exo_nbr-1;
    case eExogenousDet:
michel's avatar
michel committed
45
46
47
	symboltable[name].id = ModelParameters::exo_det_nbr;
	ModelParameters::exo_det_nbr++;
	return  ModelParameters::exo_det_nbr-1;
48
    case eEndogenous:
michel's avatar
michel committed
49
50
51
	symboltable[name].id = ModelParameters::endo_nbr;
	ModelParameters::endo_nbr++;
	return  ModelParameters::endo_nbr-1;
52
    case eParameter:
michel's avatar
michel committed
53
54
55
	symboltable[name].id = ModelParameters::parameter_nbr;
	ModelParameters::parameter_nbr++;
	return  ModelParameters::parameter_nbr-1;
56
    case eRecursiveVariable:
michel's avatar
michel committed
57
58
59
	symboltable[name].id = ModelParameters::recur_nbr;
	ModelParameters::recur_nbr++;
	return  ModelParameters::recur_nbr-1;
michel's avatar
michel committed
60
61
62
63
    case eLocalParameter:
	symboltable[name].id = ModelParameters::local_parameter_nbr;
	ModelParameters::local_parameter_nbr++;
	return  ModelParameters::local_parameter_nbr-1;
64
65
66
67
    default:
  // should never happen
      return -1;
    }
michel's avatar
michel committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  
}
//------------------------------------------------------------------------------
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";
82
			return getID(name);		}
michel's avatar
michel committed
83
84
85
		else{
			string msg = "symbol " + name + " declared more than once with different types.";
			(* error) (msg.c_str());
86
			return -1;
michel's avatar
michel committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
		}
	}

}
//------------------------------------------------------------------------------
void SymbolTable::AddSymbolRange(string name,int nbr,Type type, string tex_name)
{
}
//------------------------------------------------------------------------------
void SymbolTable::AddLag(string name,int lag)
{
	//Testing if the symbol exist in the map 
	if ( !Exist(name) ){
		return;
	}
	Type type = symboltable[name].type;
	if ((type == eEndogenous) || (type == eExogenous))
	{
		symboltable[name].lags.push_back(lag);
	}
}
//------------------------------------------------------------------------------
void  SymbolTable::ResetType(string name,Type new_type)
{
	symboltable[name].type = new_type;
}
//------------------------------------------------------------------------------
void  SymbolTable::SetReferenced(string name)
{
	symboltable[name].referenced = eReferenced;
}
//------------------------------------------------------------------------------
Reference 	SymbolTable::isReferenced(std::string name)
{
	return symboltable[name].referenced;
}
//------------------------------------------------------------------------------
void SymbolTable::clean()
{
	string unused;
	bool   warning = false;
	vector<Type>	types(3);
	vector<int>		nb_type(3);
	
	types[0] = eEndogenous;
	types[1] = eExogenous;
	types[2] = eExogenousDet;
	
	nb_type[0] = ModelParameters::endo_nbr;
	nb_type[1] = ModelParameters::exo_nbr;
	nb_type[2] = ModelParameters::exo_det_nbr;
	
	// Removing unused variables
	for (int t = 0; t < 3; t++)
	{
		// Checking if all variables are used
		for (int s1 = 0; s1 < nb_type[t]; s1++)
		{
			string name = getNameByID(types[t],s1);
			string tex_name = getTexNameByID(types[t],s1);
			if (isReferenced(name) == eNotReferenced)
			{
				symboltable.erase(name);
				vector<string>::iterator it;
				it = find(name_table[types[t]].begin(), name_table[types[t]].end(), name);
				name_table[types[t]].erase(it);
				it = find(tex_name_table[types[t]].begin(), tex_name_table[types[t]].end(), tex_name);
				tex_name_table[types[t]].erase(it);
				nb_type[t]--;
				unused += "fprintf(1,'%-30s";
				switch(types[t])
158
159
160
161
162
163
				  {
				  case eEndogenous 	: unused += "Endogenous variable\\n','";break;
				  case eExogenous 	: unused += "Exogenous variable\\n','";break;
				  case eExogenousDet 	: unused += "Exogenous deterministic variable\\n','";break;
				  default : ;
				  }
michel's avatar
michel committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
				unused += name;
				unused += "');\n";
				warning = true;
				for (int s2 = s1; s2 < nb_type[t]; s2++)
				{
					name = getNameByID(types[t],s2);
					// Decrementing symbol table ids in ST
					symboltable[name].id--;
				}
				s1--;
			}
		}
	}
	ModelParameters::endo_nbr 		= nb_type[0];
	ModelParameters::exo_nbr 		= nb_type[1];
	ModelParameters::exo_det_nbr	= nb_type[2];
	/*
	// Checking if unused parameters
	for (int s1 = 0; s1 < ModelParameters::parameter_nbr; s1++)
	{
		string name = getNameByID(eParameter,s1);
		if (isReferenced(name) == eNotReferenced)
		{
				unused += "fprintf(1,'%-30sParameter\\n','";
				unused += name;
				unused += "');\n";

			warning = true;
		}
	}
*/
	if (warning)
	{
		output << "fprintf(1,'Warning : symbol(s) :\\n');\n";
		output << unused;
		output << "fprintf(1,'are declared but not used in the model equations. ');\n";
		output << "reply = input('Continue? [y]\\\\n ','s');\n";
		output << "if isempty(reply), reply='y'; end;\n";
		output << "if strcmpi(reply(1),'n'),\n  return;\nend\n";
	}
	//PrintSymbolTable();
}
string SymbolTable::get()
{
	if (ModelParameters::exo_nbr > 0)
	{
		output << "M_.exo_names = '" << getNameByID(eExogenous, 0) << "';\n";
		output << "M_.exo_names_tex = '" << getTexNameByID(eExogenous, 0) << "';\n";
		for (int id = 1; id < ModelParameters::exo_nbr; id++)
		{
214
215
		  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";
michel's avatar
michel committed
216
217
218
219
220
221
222
223
		}
	}
	if (ModelParameters::exo_det_nbr > 0)
	{
		output << "lgxdet_ = '" << getNameByID(eExogenousDet, 0) << "';\n";
		output << "lgxdet_tex_ = '" << getTexNameByID(eExogenousDet, 0) << "';\n";
		for (int id = 1; id < ModelParameters::exo_det_nbr; id++)
		{
224
225
		  output << "lgxdet_ = " + interfaces::strvcat("lgxdet_","'"+getNameByID(eExogenousDet, id)+"'") + ";\n";
		  output << "lgxdet_tex_ = " + interfaces::strvcat("lgxdet_tex_","'"+getTexNameByID(eExogenousDet, id)+"'") + ";\n";
michel's avatar
michel committed
226
227
228
229
230
231
232
233
		}
	}
	if (ModelParameters::endo_nbr > 0)
	{
		output << "M_.endo_names = '" << getNameByID(eEndogenous, 0) << "';\n";
		output << "M_.endo_names_tex = '" << getTexNameByID(eEndogenous, 0) << "';\n";
		for (int id = 1; id < ModelParameters::endo_nbr; id++)
		{
234
235
		  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";
michel's avatar
michel committed
236
237
238
239
240
241
242
243
		}
	}
	if (ModelParameters::recur_nbr > 0)
	{
		output << "M_.recur_names = '" << getNameByID(eRecursiveVariable, 0) << "';\n";
		output << "M_.recur_names_tex = '" << getTexNameByID(eRecursiveVariable, 0) << "';\n";
		for (int id = 1; id < ModelParameters::recur_nbr; id++)
		{
244
245
		  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";
michel's avatar
michel committed
246
247
248
249
250
251
252
253
		}
	}
	if (ModelParameters::parameter_nbr > 0)
	{
		output << "M_.param_names = '" << getNameByID(eParameter, 0) << "';\n";
		output << "M_.param_names_tex = '" << getTexNameByID(eParameter, 0) << "';\n";
		for (int id = 1; id < ModelParameters::parameter_nbr; id++)
		{
254
255
		  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";
michel's avatar
michel committed
256
257
258
259
260
261
262
263
264
265
266
267
		}
	}

	output << "M_.exo_det_nbr = " << ModelParameters::exo_det_nbr << ";\n";	
	output << "M_.exo_nbr = " << ModelParameters::exo_nbr << ";\n";
	output << "M_.Sigma_e = zeros(" << ModelParameters::exo_nbr
	       << ", " << ModelParameters::exo_nbr << ");\n";
	output << "M_.endo_nbr = " << ModelParameters::endo_nbr << ";\n";
	output << "M_.recur_nbr = " << ModelParameters::recur_nbr << ";\n";
	output << "M_.param_nbr = " << ModelParameters::parameter_nbr << ";\n";
	return output.str();
}
michel's avatar
michel committed
268
269
270
271
272
273
274
275
276
277
278
279
//------------------------------------------------------------------------------
void SymbolTable::erase_local_parameters(void)
{
  std::map<std::string, Symbol, std::less<std::string> >::iterator i = symboltable.begin();
  for(; i != symboltable.end(); ++i)
    {
      if ((i->second).type == eLocalParameter)
	{
	  symboltable.erase(i);
	}
    }
}
michel's avatar
michel committed
280
//------------------------------------------------------------------------------