2014-04-03 07:23:37 +04:00
|
|
|
// Node vs browser behavior
|
|
|
|
var env = {};
|
|
|
|
if (typeof module === 'undefined') {
|
|
|
|
var exports = env;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Env implementation
|
|
|
|
function Env(outer, binds, exprs) {
|
|
|
|
this.data = {};
|
|
|
|
this.outer = outer || null;
|
|
|
|
|
|
|
|
if (binds && exprs) {
|
|
|
|
// Returns a new Env with symbols in binds bound to
|
|
|
|
// corresponding values in exprs
|
|
|
|
// TODO: check types of binds and exprs and compare lengths
|
|
|
|
for (var i=0; i<binds.length;i++) {
|
|
|
|
if (binds[i].value === "&") {
|
|
|
|
// variable length arguments
|
|
|
|
this.data[binds[i+1].value] = Array.prototype.slice.call(exprs, i);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
this.data[binds[i].value] = exprs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
Env.prototype.find = function (key) {
|
2022-01-10 02:15:40 +03:00
|
|
|
if (key in this.data) { return this; }
|
2014-04-03 07:23:37 +04:00
|
|
|
else if (this.outer) { return this.outer.find(key); }
|
|
|
|
else { return null; }
|
|
|
|
};
|
2014-12-19 05:33:49 +03:00
|
|
|
Env.prototype.set = function(key, value) {
|
2022-01-10 02:15:40 +03:00
|
|
|
this.data[key] = value;
|
2014-12-19 05:33:49 +03:00
|
|
|
return value;
|
|
|
|
};
|
2014-04-03 07:23:37 +04:00
|
|
|
Env.prototype.get = function(key) {
|
|
|
|
var env = this.find(key);
|
2022-01-10 02:15:40 +03:00
|
|
|
if (!env) { throw new Error("'" + key + "' not found"); }
|
|
|
|
return env.data[key];
|
2014-04-03 07:23:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.Env = env.Env = Env;
|