-
Notifications
You must be signed in to change notification settings - Fork 0
/
Context.cs
66 lines (55 loc) · 1.55 KB
/
Context.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
using System.Collections.Generic;
public class SymbolTable
{
public SymbolTable parent;
private Dictionary<string, Value> table;
public SymbolTable()
{
parent = null;
table = new Dictionary<string, Value>();
}
public SymbolTable(Dictionary<string, Value> dict)
{
parent = null;
table = dict;
}
public SymbolTable(SymbolTable givenParent)
{
parent = givenParent;
table = new Dictionary<string, Value>();
}
public Value Get(string name)
{
if (this.table.ContainsKey(name)) return this.table[name];
if (this.parent == null) return null;
return this.parent.Get(name);
}
public Value Set(string name, Value value)
{
this.table[name] = value;
return value;
}
}
public class Context
{
public string name;
public SymbolTable symbolTable;
public Context parent;
public Position parentEntryPos;
public Context(SymbolTable table = null, string givenName = "<module>", Context givenParent = null, Position pos = null)
{
name = givenName;
symbolTable = table == null ? new SymbolTable(givenParent == null ? null : givenParent.symbolTable) : table;
parent = givenParent;
parentEntryPos = pos;
}
public Context(Dictionary<string, Value> dict) : this(new SymbolTable(dict)) {}
public Value Get(string name)
{
return this.symbolTable.Get(name);
}
public Value Set(string name, Value value)
{
return this.symbolTable.Set(name, value);
}
}