1 // Node vs browser behavior
3 if (typeof module
=== 'undefined') {
8 function Env(outer
, binds
, exprs
) {
10 this.outer
= outer
|| null;
13 // Returns a new Env with symbols in binds bound to
14 // corresponding values in exprs
15 // TODO: check types of binds and exprs and compare lengths
16 for (var i
=0; i
<binds
.length
;i
++) {
17 if (binds
[i
].value
=== "&") {
18 // variable length arguments
19 this.data
[binds
[i
+1].value
] = Array
.prototype.slice
.call(exprs
, i
);
22 this.data
[binds
[i
].value
] = exprs
[i
];
28 Env
.prototype.find = function (key
) {
29 if (!key
.constructor || key
.constructor.name
!== 'Symbol') {
30 throw new Error("env.find key must be a symbol")
32 if (key
.value
in this.data
) { return this; }
33 else if (this.outer
) { return this.outer
.find(key
); }
36 Env
.prototype.set = function(key
, value
) {
37 if (!key
.constructor || key
.constructor.name
!== 'Symbol') {
38 throw new Error("env.set key must be a symbol")
40 this.data
[key
.value
] = value
;
43 Env
.prototype.get = function(key
) {
44 if (!key
.constructor || key
.constructor.name
!== 'Symbol') {
45 throw new Error("env.get key must be a symbol")
47 var env
= this.find(key
);
48 if (!env
) { throw new Error("'" + key
.value
+ "' not found"); }
49 return env
.data
[key
.value
];
52 exports
.Env
= env
.Env
= Env
;