-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.js
37 lines (36 loc) · 1000 Bytes
/
environment.js
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
(function(global) {
function Environment(parent) {
this.vars = Object.create(parent ? parent.vars : null);
this.parent = parent;
}
Environment.prototype = {
extend: function() {
return new Environment(this);
},
lookup: function(name) {
var scope = this;
while (scope) {
if (Object.prototype.hasOwnProperty.call(scope.vars, name))
return scope;
scope = scope.parent;
}
},
get: function(name) {
if (name in this.vars)
return this.vars[name];
throw new Error("Undefined variable " + name);
},
set: function(name, value) {
var scope = this.lookup(name);
if (/[a-z]/.test(name))
return (scope || this).vars[name] = value;
else
throw new Error("Cannot assign constant " + name);
},
def: function(name, value) {
return this.vars[name] = value;
}
};
global.Mason = global.Mason || {};
global.Mason.Environment = Environment;
})(window);