-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathEnvironment.js
More file actions
51 lines (45 loc) · 1.03 KB
/
Environment.js
File metadata and controls
51 lines (45 loc) · 1.03 KB
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
/**
* Environment: names storage.
*
* Course info: http://dmitrysoshnikov.com/courses/essentials-of-interpretation/
*
* (C) 2018-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
class Environment {
/**
* Creates an environment with the given record.
*/
constructor(record = {}, parent = null) {
this.record = record;
this.parent = parent;
}
/**
* Creates a variable with the given name and value.
*/
define(name, value) {
this.record[name] = value;
return value;
}
/**
* Updates an existing variable.
*/
assign(name, value) {
this.resolve(name).record[name] = value;
return value;
}
/**
* Returns the value of a defined variable, or throws
* if the variable is not defined.
*/
lookup(name) {
return this.resolve(name).record[name];
}
/**
* Returns specific environment in which a variable is defined, or
* throws if a variable is not defined.
*/
resolve(name) {
// Implement here: see Lectures 6, 7
}
}
module.exports = Environment;