mirror of
https://github.com/kanaka/mal.git
synced 2024-11-10 12:47:45 +03:00
ea81a8087b
- types: low-level mapping to the implementation language. - core: functions on types that are exposed directly to mal. - printer: implementation called by pr-str, str, prn, println. - env: the environment implementation - Also, unindent all TCO while loops so that the diff of step4 and step5 are minimized.
41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
// 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) {
|
|
if (key in this.data) { return this; }
|
|
else if (this.outer) { return this.outer.find(key); }
|
|
else { return null; }
|
|
};
|
|
Env.prototype.set = function(key, value) { this.data[key] = value; return value; },
|
|
Env.prototype.get = function(key) {
|
|
var env = this.find(key);
|
|
if (!env) { throw new Error("'" + key + "' not found"); }
|
|
return env.data[key];
|
|
};
|
|
|
|
exports.Env = env.Env = Env;
|