-
Notifications
You must be signed in to change notification settings - Fork 1
/
Symboltable.cs
460 lines (398 loc) · 11.4 KB
/
Symboltable.cs
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
using System;
using System.Collections;
using System.Collections.Generic;
public class Type
{
public enum T {
INT, REAL, CHAR, STRING, BOOLEAN, PROC, FUNC, CALL
}
T ty;
int size;
Type of;
// default constructor
public Type ()
{
ty = T.INT;
size = 1;
of = null;
}
// Custom contructor intialises all fields
public Type( Type.T t, int s, Type o )
{
ty = t;
size = s;
of = o;
}
// get...() methods
public Type Of {
get { return of; }
}
public T Ty {
get { return ty; }
}
public int Size {
get { return size; }
}
public string toString ()
{
if ( ty == T.INT )
return( "INT" );
else if ( ty == T.REAL )
return( "REAL" );
else if ( ty == T.BOOLEAN )
return( "BOOLEAN" );
else if ( ty == T.STRING )
return( "STRING" );
else if ( ty == T.CHAR )
return( "CHAR" );
else if ( ty == T.FUNC )
return( "FUNC" );
else if ( ty == T.PROC )
return( "PROC" );
else
return ("DESCONOCIDO");
}
public void dump()
{
if ( ty == T.INT )
Console.Write( "INT" );
else if ( ty == T.REAL )
Console.Write( "REAL" );
else if ( ty == T.CHAR )
Console.Write( "CHAR" );
else if ( ty == T.BOOLEAN )
Console.Write( "BOOLEAN" );
else if ( ty == T.STRING )
Console.Write( "STRING" );
else if ( ty == T.FUNC )
Console.Write( "FUNC" );
else if ( ty == T.PROC )
Console.Write( "PROC" );
else
Console.Error.WriteLine( "*** TIPO INTERNO DESCONOCIDO ***" );
}
}
////////////////////////////////////////////////////////////////////////////////
//
// CLASS: Symbol y derivadas
//
// Clases para los simbolos
//
////////////////////////////////////////////////////////////////////////////////
public class Symbol
{
private string name;
private Type type;
private uint scope;
protected Errors err;
private bool isLiteral;
public Symbol( string n, Type t )
{
name = n;
type = t;
scope = 0;
isLiteral = false;
err = Errors.Instance;
}
public Type Type {
get { return type; }
set { type = value; }
}
public string Name {
get { return name; }
}
public uint Scope {
get { return scope; }
set { scope = value; }
}
public bool Literal {
get { return isLiteral; }
set { isLiteral = value; }
}
public virtual void dump()
{
Console.Write( "Global ({0}) ", name );
Console.Write( "Literal {0}", isLiteral);
Console.Write( "tiene tipo :" );
type.dump();
Console.WriteLine();
}
}
public class Constant: Symbol
{
private string cvalue;
public Constant (string n, Type t): base (n, t)
{
cvalue = "";
}
public string Value
{
get { return cvalue; }
set { cvalue = value; }
}
public override void dump()
{
base.dump();
Console.WriteLine(" Cte value: " + cvalue);
}
}
public class Param: Symbol
{
private uint offset;
private bool reference;
public Param (string n, Type t, uint o, bool isref): base (n,t)
{
offset = o;
reference = isref;
}
public uint Offset {
get { return offset; }
}
public bool Reference {
get { return reference; }
}
}
public class Procedure: Symbol
{
private Hashtable parameters;
private Hashtable local;
public Procedure (string n, Type t): base (n, t)
{
parameters = new Hashtable();
local = new Hashtable();
}
public Hashtable Parameters {
get { return parameters; }
set { parameters = value; }
}
public Hashtable Locales {
get { return local; }
set { local = value; }
}
public void addToLocals (Symbol n)
{
if (parameters.Contains(n.Name))
throw new fpc2ilException("Local variable '" + n.Name + "' has been previously defined as an argument");
if (local.Contains(n.Name))
throw new fpc2ilException("Redefinition of local variable '" + n.Name + "'");
try {
local.Add(n.Name, n);
} catch (ArgumentException) {}
}
private Param getParamForOffset (uint offset)
{
foreach (Param p in parameters.Values)
if (p.Offset == offset)
return p;
return null; /* no deberia devolver nunca null */
}
public bool checkParams (ArrayList paramList)
{
int callParamNumber = paramList.Count;
int number = parameters.Count;
//Console.WriteLine("P: " + number + "C: " + callParamNumber);
if (number != callParamNumber)
{
throw new fpc2ilException("Procedure '" + Name + "' expects " + number + " parameters but it is been called with " + callParamNumber);
return false;
}
int i = 0;
object[] keys = new object[number];
parameters.Keys.CopyTo(keys, 0);
for (i=0; i<number; i++)
{
Param param = getParamForOffset((uint)i);
Symbol aux = (Symbol)paramList[i];
if (param.Reference && aux.Literal)
{
throw new fpc2ilException("Parameter '" + param.Name + "' expects a reference but is being called with a literal " + aux.Name);
return false;
}
string proc_type = param.Type.toString();
string call_type = aux.Type.toString();
if (proc_type != call_type)
throw new fpc2ilException ("Procedure param '" + param.Name + "' (" + proc_type + ") and call param '" +
aux.Name + "' (" + call_type + ") type doesn't match");
}
return true;
}
public Symbol find (string n)
{
if ( local.Contains(n) )
return (Symbol)local[n];
else if (parameters.Contains(n))
return (Symbol)parameters[n];
else {
throw new fpc2ilException( "Undeclared symbol '" + n + "'");
return null;
}
}
public void dumpParametros ()
{
foreach (Param s in parameters.Values)
{
Console.Write("\t*Parametro.{1}-Ref:{2}: {0} : ", s.Name, s.Offset, s.Reference);
s.Type.dump();
Console.WriteLine();
}
}
public void dumpLocales ()
{
foreach (Symbol v in local.Values)
{
Console.Write("\t*Local : {0} : ", v.Name);
v.Type.dump();
if (v is Constant) { Constant t = (Constant)v; Console.Write(" Cte value: " + t.Value); }
Console.WriteLine();
}
}
public override void dump()
{
base.dump();
if (parameters.Count != 0)
dumpParametros();
else
Console.WriteLine("\t*Sin parametros");
if (local.Count != 0)
dumpLocales();
else
Console.WriteLine("\t*Sin variables locales");
}
}
public class Function: Procedure
{
private Type typereturn;
public Function(string name, Type type): base(name, type)
{
typereturn = new Type();
}
public Type TypeReturn {
get { return typereturn; }
set { typereturn = value; }
}
public override void dump()
{
base.dump();
Console.Write("\t*Devuelve: ");
typereturn.dump();
Console.WriteLine();
}
}
////////////////////////////////////////////////////////////////////////////////
//
// CLASS: SymbolTable
//
// La tabla de simbolos es un singleton
//
////////////////////////////////////////////////////////////////////////////////
public sealed class Symboltable
{
private Errors err;
private uint scope;
private Hashtable table;
private static readonly Symboltable __instance = new Symboltable();
// Constructor
private Symboltable ()
{
scope = 0;
table = new Hashtable();
err = Errors.Instance;
}
public static Symboltable Instance
{
get
{
return __instance;
}
}
public uint Scope {
get { return scope; }
}
public void add (Symbol s)
{
if ( table.Contains(s.Name) )
throw new fpc2ilException("Redefinition of '" + s.Name + "'");
try {
//s.Scope = scope;
table.Add( s.Name, s );
} catch (ArgumentException e) {}
}
public Symbol find (string n)
{
if ( table.Contains(n) )
return (Symbol)table[n];
else {
throw new fpc2ilException("Undeclared symbol '" + n + "'");
return null;
}
}
public Symbol getSymbol( string n )
{
if ( table.Contains(n) )
return (Symbol)table[n];
else
return null;
}
public Symbol getSymbol (string name, string n, ref int type)
{
Procedure aux = null;
/* TIpo: 0 nada , 1 parametro, 2 local */
if (table.Contains(name)) {
aux = (Procedure)table[name];
if (aux.Parameters.Contains(n))
{
type = 1;
return (Symbol)aux.Parameters[n];
}
else if (aux.Locales.Contains(n))
{
type = 2;
return (Symbol)aux.Locales[n];
}
else {
if (table.Contains(n))
return (Symbol)table[n];
else
return null;
}
} else {
return null;
}
}
public Symbol findInAllScopes (string name, string n)
{
Procedure aux = null;
if (table.Contains(name)) {
aux = (Procedure)table[name];
if (aux.Name == n)
return aux; /* TODO: Esto quizas de problemas, En pascal si es una funcion se usa como variable de retorno el
nombre de la funcion,asi es que devolvemos de momento la propia funcion,asi es que
devolvemos de momento la propia funcion. Quizas deberiamos crear un nuevo simbolo tipo
return y meterlo en la tabla de simbolos para la generacion de codigo */
if (aux.Parameters.Contains(n))
return (Symbol)aux.Parameters[n];
else if (aux.Locales.Contains(n))
return (Symbol)aux.Locales[n];
else {
if (table.Contains(n))
return (Symbol)table[n];
else {
throw new fpc2ilException("Undeclared symbol '" + n + "'");
return null;
}
}
} else {
throw new fpc2ilException("Undeclared procedure '" + name + "'");
return null;
}
}
// dump()
// Dumps out the contents of the symbol table.
public void dump()
{
Console.Write( "\n*** TABLA DE SIMBOLOS ***\n\n" );
foreach( Symbol s in table.Values )
s.dump();
Console.Write( "\n***********************\n" );
}
}