-
Notifications
You must be signed in to change notification settings - Fork 1
/
SymbolTables.cs
395 lines (334 loc) · 8.43 KB
/
SymbolTables.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Compiler
{
namespace Symbols
{
class Table
{
public Table parent = null;
public List<Table> childrens = new List<Table>();
private int number = 0;
public Dictionary<string, Symbol> symbols = new Dictionary<string, Symbol>();
public Dictionary<string, Symbol> tags = new Dictionary<string, Symbol>();
public Table() { }
public Table(Table parent, int number)
{
this.number = number;
this.parent = parent;
}
public void AddSymbols(Table table)
{
this.symbols = this.symbols.Concat(table.symbols).ToDictionary(v =>v.Key, v=> v.Value);
this.tags = this.tags.Concat(table.tags).ToDictionary(v => v.Key, v => v.Value);
}
private void CheckName(Symbol symbol, params Dictionary<string, Symbol>[] dicts)
{
foreach (var dict in dicts)
{
if (dict.ContainsKey(symbol.GetName()))
{
throw new Symbols.Exception(string.Format(
"переопределение {0}", symbol.GetName()
), symbol.GetIndex(), symbol.GetLine());
}
}
}
public IEnumerable<Var> GetVariables()
{
return this.symbols.Values.Where(var => var is Var).ToList().Cast<Var>();
}
public void AddSymbol(Symbol symbol)
{
if (symbol is Var && ((Var)symbol).GetType() is Func
&& this.ContainsVariable(symbol.GetName())
&& symbol.Equals(this.GetVariable(symbol.GetName())))
{
Func f = (Func)this.GetVariable(symbol.GetName()).GetType();
if (f.Merge((Func)((Var)symbol).GetType()))
{
return;
}
}
CheckName(symbol, this.symbols);
this.symbols.Add(symbol.GetName(), symbol);
}
public void RemoveSymbol(Symbol symbol)
{
try
{
this.symbols.Remove(symbol.GetName());
}
catch (Exception) { }
}
public void AddTag(Symbol tag)
{
CheckName(tag, this.tags);
this.tags.Add(tag.GetName(), tag);
}
public bool ContainsSymbol(string name, System.Type symbol_type)
{
return this.symbols.ContainsKey(name) && symbol_type.IsAssignableFrom(this.symbols[name].GetType());
}
public Symbol GetSymbol(Token t, System.Type symbol_type)
{
if (!this.ContainsSymbol(t.GetStrVal(), symbol_type))
{
string error = "";
if (symbol_type == typeof(Type))
{
error = string.Format("тип \"{0}\" не определен", t.GetStrVal());
}
else if (symbol_type == typeof(Var))
{
error = string.Format("переменная \"{0}\" не определена", t.GetStrVal());
}
throw new Symbols.Exception(error, t.GetIndex(), t.GetLine());
}
return this.symbols[t.GetStrVal()];
}
public Symbol GetSymbol(string name, System.Type symbol_type)
{
return this.GetSymbol(new Token(Token.Type.IDENTIFICATOR, name), symbol_type);
}
public Symbol GetTag(Token t)
{
if (!this.ContainsTag(t.GetStrVal()))
{
throw new Symbols.Exception(string.Format("тэг \"{0}\" не определен", t.GetStrVal()), t.GetIndex(), t.GetLine());
}
return this.tags[t.GetStrVal()];
}
public Symbol GetTag(string name)
{
return this.GetTag(new Token(Token.Type.IDENTIFICATOR, name));
}
public bool ContainsVariable(string name)
{
return this.ContainsSymbol(name, typeof(Var));
}
public bool ContainsType(string name)
{
return this.ContainsSymbol(name, typeof(Type));
}
public bool ContainsTag(string name)
{
return this.tags.ContainsKey(name);
}
public Var GetVariable(Token t)
{
return (Var)this.GetSymbol(t, typeof(Var));
}
public Var GetVariable(string name)
{
return this.GetVariable(new Token(Token.Type.IDENTIFICATOR, name));
}
public Type GetType(Token t)
{
return (Type)this.GetSymbol(t, typeof(Type));
}
public Type GetType(string name)
{
return this.GetType(new Token(Token.Type.IDENTIFICATOR, name));
}
public long GetSize()
{
return this.symbols.Count + this.tags.Count();
}
public long GetCountSymbol()
{
return this.symbols.Count;
}
public long GetCountTag()
{
return this.tags.Count();
}
public bool Equels(Table t)
{
foreach (Symbol s in t.symbols.Values)
{
if (!this.symbols.ContainsKey(s.GetName()) || !this.symbols[s.GetName()].Equals(s))
{
return false;
}
}
foreach (Symbol s in t.tags.Values)
{
if (!this.tags.ContainsKey(s.GetName()) || !this.tags[s.GetName()].Equals(s))
{
return false;
}
}
return true;
}
public void Print(StreamWriter stream, int indent = 0)
{
string s_indent = new String(' ', indent);
stream.WriteLine(s_indent + "<<<table #" + this.number + ">>>");
stream.WriteLine(s_indent + "<tags>");
foreach (var pair in this.tags)
{
pair.Value.Print(stream, indent);
stream.Write(stream.NewLine);
}
stream.WriteLine(s_indent + "<types>");
foreach (var pair in this.symbols.Where(kvpair => kvpair.Value is Type))
{
pair.Value.Print(stream, indent);
stream.Write(stream.NewLine);
}
stream.WriteLine(s_indent + "<vars>");
foreach (var pair in this.symbols.Where(kvpair => kvpair.Value is Var))
{
pair.Value.Print(stream, indent);
stream.Write(stream.NewLine);
}
this.childrens.ForEach(table => table.Print(stream, indent + 1));
}
}
partial class StackTable
{
private Table root = null, current = null;
private int count_tables = 0;
public Table GetRoot()
{
return this.root;
}
public StackTable()
{
this.NewTable();
}
public void NewTable()
{
int index = current == null ? 0 : current.childrens.Count + 1;
Table table = new Table(this.current, ++this.count_tables);
if (this.root == null)
{
this.current = this.root = table;
}
else
{
this.current.childrens.Add(table);
this.current = table;
}
}
public void StepUp()
{
this.current = this.current.parent;
}
public Table GetCurrentTable()
{
return this.current;
}
public Table PopTable()
{
Table res = this.current;
this.current = res.parent;
try
{
this.current.childrens.RemoveAt(this.current.childrens.FindIndex(table => table == res));
}
catch (System.ArgumentOutOfRangeException){ }
return res;
}
private bool ContainsSymbol(string name, System.Type symbol_type)
{
Table itr = this.current;
while (itr != null)
{
if (itr.ContainsSymbol(name, symbol_type))
{
return true;
}
itr = itr.parent;
}
return false;
}
private bool ContainsTag(string name)
{
Table itr = this.current;
while (itr != null)
{
if (itr.ContainsTag(name))
{
return true;
}
itr = itr.parent;
}
return false;
}
private Symbol GetSymbol(Token t, System.Type symbol_type)
{
Table itr = this.current;
while (itr != null)
{
if (itr.ContainsSymbol(t.GetStrVal(), symbol_type))
{
return itr.GetSymbol(t, symbol_type);
}
itr = itr.parent;
}
return this.root.GetSymbol(t, symbol_type);
}
public Symbol GetTag(Token t)
{
Table itr = this.current;
while (itr != null)
{
if (itr.ContainsTag(t.GetStrVal()))
{
return itr.GetTag(t);
}
itr = itr.parent;
}
return this.root.GetTag(t);
}
public Symbol GetTag(string name)
{
return this.GetTag(new Token(Token.Type.IDENTIFICATOR, name));
}
public void AddSymbol(Symbol symbol)
{
this.current.AddSymbol(symbol);
}
public void AddTag(Symbol tag)
{
this.current.AddTag(tag);
}
public bool ContainsType(string name)
{
return this.ContainsSymbol(name, typeof(Type));
}
public bool ContainsVariable(string name)
{
return this.ContainsSymbol(name, typeof(Var));
}
public Var GetVariable(Token t)
{
return (Var)this.GetSymbol(t, typeof(Var));
}
public Var GetVariable(string name)
{
return this.GetVariable(new Token(Token.Type.IDENTIFICATOR, name));
}
public Type GetType(Token t)
{
return (Type)this.GetSymbol(t, typeof(Type));
}
public Type GetType(string name)
{
return (Type)this.GetType(new Token(Token.Type.IDENTIFICATOR, name));
}
public bool IsGlobal()
{
return this.root == this.current;
}
public void Print(StreamWriter stream)
{
this.root.Print(stream);
}
}
}
}