-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvironment.cs
228 lines (185 loc) · 5.04 KB
/
Environment.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
using Type = Language.Typer.Type;
interface IEnvironment<T>
{
void Define(string name, T value);
T? Get(Token name);
void Assign(Token name, T value);
bool IsDeclared(string name);
}
class CompilerEnv : IEnvironment<object>
{
readonly CompilerEnv Enclosing;
// local scope nested inside an outer one
public CompilerEnv(CompilerEnv enclosing)
{
Enclosing = enclosing;
}
// global scope’s environment
public CompilerEnv()
{
Enclosing = null!;
}
public readonly Dictionary<string, object> addresses = [];
public void Define(string name, object value)
{
try
{
addresses.Add(name, value);
}
catch (ArgumentException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Variable '" + name + "' already declared.");
Console.ResetColor();
}
}
public object? Get(Token name)
{
if (IsDeclared(name.Lexeme)) return addresses[name.Lexeme];
else if (Enclosing != null) return Enclosing.Get(name);
else return $"Undefined Variable '{name}'.";
}
public void Assign(Token name, object value)
{
if (IsDeclared(name.Lexeme))
{
addresses[name.Lexeme] = value;
return;
}
if (Enclosing != null)
{
Enclosing.Assign(name, value);
return;
}
}
public bool IsDeclared(string name)
{
if (addresses.TryGetValue(name, out object? _)) return true;
return false;
}
public void Clear()
{
addresses.Clear();
}
}
class InterpreterEnv : IEnvironment<object>
{
readonly InterpreterEnv Enclosing;
// local scope nested inside an outer one
public InterpreterEnv(InterpreterEnv enclosing)
{
Enclosing = enclosing;
}
// global scope’s environment
public InterpreterEnv()
{
Enclosing = null;
}
public readonly Dictionary<string, object> values = [];
public void Define(string name, object value)
{
try
{
values.Add(name, value);
}
catch (ArgumentException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Variable '" + name + "' already declared.");
Console.ResetColor();
}
}
public object Get(Token name)
{
if (IsDeclared(name.Lexeme)) return values[name.Lexeme];
else if (Enclosing != null) return Enclosing.Get(name);
else throw new Exception($"Undefined Variable '{name.Lexeme}' in line {name.Line}.");
}
public void Assign(Token name, object value)
{
if (IsDeclared(name.Lexeme))
{
values[name.Lexeme] = value;
return;
}
if (Enclosing != null)
{
Enclosing.Assign(name, value);
return;
}
throw new Exception($"Undefined Variable '{name.Lexeme} in line {name.Line}'.");
}
public object GetAt(int distance, string name)
{
return Ancestor(distance).values[name];
}
public void AssignAt(int distance, string name, object value)
{
Ancestor(distance).values[name] = value;
}
public bool IsDeclared(string name)
{
if (values.TryGetValue(name, out object? _)) return true;
return false;
}
private InterpreterEnv Ancestor(int distance)
{
InterpreterEnv environment = this;
for (int i = 0; i < distance; i++)
{
if (environment.Enclosing == null)
{
throw new Exception($"Invalid scope access at distance {distance}.");
}
environment = environment.Enclosing;
}
return environment;
}
internal void Clear()
{
values.Clear();
}
}
class TypeEnvironment : IEnvironment<List<Type>>
{
readonly TypeEnvironment Enclosing;
// local scope nested inside an outer one
public TypeEnvironment(TypeEnvironment enclosing)
{
Enclosing = enclosing;
}
// global scope’s environment
public TypeEnvironment()
{
Enclosing = null!;
}
private readonly Dictionary<string, List<Type>> values = [];
public void Define(string name, List<Type> types)
{
values.Add(name, types);
}
public List<Type> Get(Token name)
{
if (IsDeclared(name.Lexeme)) return values[name.Lexeme];
else if (Enclosing != null) return Enclosing.Get(name);
else throw new Exception($"Undefined Variable '{name.Lexeme}' at line {name.Line}.");
}
public void Assign(Token name, List<Type> value)
{
if (IsDeclared(name.Lexeme))
{
values[name.Lexeme] = value;
return;
}
if (Enclosing != null)
{
Enclosing.Assign(name, value);
return;
}
}
public bool IsDeclared(string name)
{
if (values.TryGetValue(name, out List<Type>? _)) return true;
return false;
}
}