-
Notifications
You must be signed in to change notification settings - Fork 0
/
Environment.cs
49 lines (41 loc) · 1.11 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
using System.Collections.Generic;
public class Environment
{
private Environment enclosing;
public Environment()
{
enclosing = null;
}
public Environment(Environment enclosing)
{
this.enclosing = enclosing;
}
private Dictionary<string, object> values = new Dictionary<string, object>();
public void define(string name, object value)
{
values.Add(name, value);
}
public object get(LoxSharp.Token name)
{
if (values.ContainsKey(name.lexeme))
{
return values[name.lexeme];
}
if (enclosing != null) return enclosing.get(name);
throw new Errors.RuntimeError(name, $"Undefined variable '{name.lexeme}'.");
}
public void assign(LoxSharp.Token name, object value)
{
if (values.ContainsKey(name.lexeme))
{
values[name.lexeme] = value;
return;
}
if (enclosing != null)
{
enclosing.assign(name, value);
return;
}
throw new Errors.RuntimeError(name, $"Undefined variable '{name.lexeme}.'");
}
}