simple visitor API and code to figure out scope and references

This commit is contained in:
Mihai Bazon
2012-08-19 15:57:50 +03:00
parent 4488758d48
commit 6c35135ace
6 changed files with 474 additions and 102 deletions

View File

@@ -62,3 +62,38 @@ function defaults(args, defs) {
}
return ret;
};
function noop() {};
var MAP = (function(){
function MAP(a, f, o) {
var ret = [], top = [], i;
function doit() {
var val = f.call(o, a[i], i);
if (val instanceof AtTop) {
val = val.v;
if (val instanceof Splice) {
top.push.apply(top, val.v);
} else {
top.push(val);
}
}
else if (val !== skip) {
if (val instanceof Splice) {
ret.push.apply(ret, val.v);
} else {
ret.push(val);
}
}
};
if (a instanceof Array) for (i = 0; i < a.length; ++i) doit();
else for (i in a) if (HOP(a, i)) doit();
return top.concat(ret);
};
MAP.at_top = function(val) { return new AtTop(val) };
MAP.splice = function(val) { return new Splice(val) };
var skip = MAP.skip = {};
function AtTop(val) { this.v = val };
function Splice(val) { this.v = val };
return MAP;
})();