summaryrefslogtreecommitdiffstats
path: root/rasdl/symbtbl.cc
blob: 02c6dfc299772d8007588b55442f1e915571c361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
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
158
159
160
161
162
163
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* This file is part of rasdaman community.
*
* Rasdaman community 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.
*
* Rasdaman community 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 rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*************************************************************
 *
 *
 * PURPOSE:
 *
 *
 * COMMENTS:
 *   The internal functions return 0 on error, 1 on success. Not really common,
 *   but it was used in some routines and so I stuck with it. Norbert 25-05-2001.
 *
 ************************************************************/

#include "symbtbl.hh"

#include "raslib/rmdebug.hh"

#include "debug/debug.hh"

YSymbol::YSymbol()
{
	TALK( "YSymbol::YSymbol default constructor" );

	name            =NULL;
	next            =NULL;
	owned_by_symbol=true;   // by default symbols owned by symbols

	scope            =NULL;
	defines         =NULL;
};

YSymbol::YSymbol(const char*_name)
{
	TALK( "YSymbol::YSymbol constructor, name=" << name );

	name            =_name;
	next            =NULL;
	owned_by_symbol=true;   // by default symbols owned by symbols

	scope            =NULL;
	defines         =NULL;
};

YSymbolTable::YSymbolTable()
{
	TALK( "YSymbolTable::YSymbolTable default constructor" );

	scope            =NULL;
	global_scope   =NULL;
};

void YSymbolTable::push_scope(YSymbol*owner)
{
	Scope   *new_scope=new Scope;

	new_scope->up            =scope;
	new_scope->son            =NULL;
	new_scope->symbols      =NULL;
	new_scope->last_symbol   =NULL;
	new_scope->owner         =owner;

	if(scope!=NULL)
	{
		new_scope->next    =scope->son;
		scope->son         =new_scope;

		owner->defines      =new_scope;
	}
	else
	{
		new_scope->next   =NULL;
	};

	if(owner==NULL)
		global_scope=new_scope;

	scope=new_scope;
};

const YSymbol *YSymbolTable::pop_scope()
{
	const YSymbol  *old_symbol   =scope->owner;
	scope                        =scope->up;

	return(old_symbol);
};

void   YSymbolTable::insert_symbol(YSymbol*symbol)const
{
	ENTER( "YSymbolTable::insert_symbol" );

	if(scope!=NULL)
	{
		TALK( "adding symbol " << symbol->get_name() );
		if(scope->last_symbol==NULL)
			scope->symbols=symbol;
		else
			scope->last_symbol->next=symbol;

		scope->last_symbol = symbol;

		symbol->next = NULL;
		symbol->scope = scope;
	}

	LEAVE( "YSymbolTable::insert_symbol" );
};

bool  YSymbolTable::search_this_scope(const char*name,const Scope*this_scope,YSymbol*&result)const
{
	ENTER( "YSymbolTable::search_this_scope" );

	bool found = false;

	if((name==NULL)||(this_scope==NULL))
		found = false;		/* no name or no scope */
	else
	{
		for(YSymbol*scan=this_scope->symbols;scan!=NULL && found!=true;scan=scan->next)
		{
			if(!strcmp(name,scan->name))
			{
				result=scan;
				found = true;
			}
		}
	}

	LEAVE( "YSymbolTable::search_this_scope -> " << found );
	return( found );
};

bool  YSymbolTable::search_scope(const char*name,YSymbol*&result)const
{
	return(search_this_scope(name,scope,result));
};

bool  YSymbolTable::search_scopes(const char*name,YSymbol*&result)const
{
	for(Scope*scan=scope;scan!=NULL;scan=scan->up)
	{
		if(search_this_scope(name,scan,result))
		return(true);
	};
	return(false);
};

bool  YSymbolTable::search_scopes_above(const YSymbol*symbol,YSymbol*&result)const
{
	const char*name=symbol->get_name();
	if(symbol->scope==NULL)
		return(false);

	for(Scope*scan=scope->up;scan!=NULL;scan=scan->up)
	{
		if(search_this_scope(name,scan,result))
			return(true);
	};
	return(false);
};

bool  YSymbolTable::search_my_scope(const char*name,const YSymbol*symbol,YSymbol*&result)const
{
	return(search_this_scope(name,symbol->defines,result));
};

bool  YSymbolTable::search_global_scope(const char*name,YSymbol*&result)const
{
	if(scope==NULL)
		return(false);                                 /* no scope at all */

	Scope*global;
	for(global=scope;global->up!=NULL;global=global->up)
		;   /* get up to the global scope */

	return(search_this_scope(name,global,result));
};

//****************************************************************************
//
//   name      : get_symbol
// purpose    : returns symbol of name in current scope
//   remarks    : returns NULL if no such symbol defined
//
//****************************************************************************
const YSymbol  *YSymbolTable::get_symbol(const char*name)const
{
	YSymbol*result;
	if(search_scope(name,result))
		return(result);
	else
		return(NULL);
};

//****************************************************************************
//
//   name      : scoped_symbol
// purpose    : creates symbol name within current scope
//   remarks    : returns true if success full and false if name already exists
//               *result is always been changed to new or defined symbol
//
//****************************************************************************
bool YSymbolTable::scoped_symbol(YSymbol**result,const char*name,const YWhere&where)
{
	if(search_scope(name,*result))
		return(false);   // duplicate identifier

	*result=new YSymbol(name);
	(*result)->where   =where;

	insert_symbol(*result);
	return(true);
};

const YSymbol   *YSymbolTable::get_defining_symbol()const
{
	if(scope==NULL)
		return(NULL);
	else
		return(scope->owner);
};



void YSymbolTable::Scope::output(FILE*out)const
{
	ENTER( "YSymbolTable::Scope::output" );

	YSymbol   *scan=symbols;

	while (scan!=NULL)
	{
		if(!scan->owned_by_symbol)
		{
			fprintf(out,"/*[%li,%i]*/",scan->where.line,scan->where.column);

			switch(scan->type)
			{
				case YSymbol::dParse_Type:
					scan->Type->output(out);
					break;            
				case YSymbol::dParse_Enumerator:
					scan->enumerator->output(out);
					break;            
				default:
					RMDBGONCE(0, RMDebug::module_rasdl, "YSymbolTable::Scope", "output()  bad YSymbol_type " << scan->type);            
					break;
			}
		}
		scan=(YSymbol*)scan->next;
	}

	LEAVE( "YSymbolTable::Scope::output" );
};


void YSymbolTable::Scope::insertData() const
{
	ENTER( "YSymbolTable::Scope::insertData" );

	YSymbol   *scan=symbols;

	while(scan!=NULL)
	{
		if(!scan->owned_by_symbol)
		{
			switch(scan->type)
			{
				case YSymbol::dParse_Type:
					scan->Type->insertData();
					break;            
				case YSymbol::dParse_Enumerator:
					// scan->enumerator->output(out);
					break;            
				default:
					RMDBGONCE(0, RMDebug::module_rasdl, "YSymbolTable::Scope", "output()  bad YSymbol_type " << scan->type);            
					break;
			}
		}
		scan=(YSymbol*)scan->next;
	}

	LEAVE( "YSymbolTable::Scope::insertData" );
};