2010-07-24 11:31:43 -04:00
|
|
|
(function() {
|
2010-03-30 19:42:09 -04:00
|
|
|
var Scope;
|
2010-03-30 16:48:43 -04:00
|
|
|
var __hasProp = Object.prototype.hasOwnProperty;
|
2010-06-27 23:55:18 -04:00
|
|
|
if (!(typeof process !== "undefined" && process !== null)) {
|
2010-02-17 23:22:05 -05:00
|
|
|
this.exports = this;
|
2010-02-13 15:25:04 -05:00
|
|
|
}
|
2010-02-27 19:46:45 -05:00
|
|
|
exports.Scope = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope = function(parent, expressions, method) {
|
2010-02-27 19:46:45 -05:00
|
|
|
var _a;
|
|
|
|
_a = [parent, expressions, method];
|
|
|
|
this.parent = _a[0];
|
|
|
|
this.expressions = _a[1];
|
|
|
|
this.method = _a[2];
|
|
|
|
this.variables = {};
|
2010-03-30 19:21:14 -04:00
|
|
|
if (this.parent) {
|
2010-06-12 19:05:13 -04:00
|
|
|
this.tempVar = this.parent.tempVar;
|
2010-03-30 19:21:14 -04:00
|
|
|
} else {
|
|
|
|
Scope.root = this;
|
2010-06-12 19:05:13 -04:00
|
|
|
this.tempVar = '_a';
|
2010-03-30 19:21:14 -04:00
|
|
|
}
|
2010-02-27 19:46:45 -05:00
|
|
|
return this;
|
2010-02-08 21:10:48 -05:00
|
|
|
};
|
2010-03-30 19:21:14 -04:00
|
|
|
Scope.root = null;
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.find = function(name) {
|
2010-02-27 19:46:45 -05:00
|
|
|
if (this.check(name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
this.variables[name] = 'var';
|
|
|
|
return false;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.any = function(fn) {
|
2010-03-30 09:02:51 -04:00
|
|
|
var _a, k, v;
|
|
|
|
_a = this.variables;
|
2010-07-16 22:31:36 -04:00
|
|
|
for (v in _a) {
|
|
|
|
if (!__hasProp.call(_a, v)) continue;
|
2010-03-30 09:02:51 -04:00
|
|
|
k = _a[v];
|
|
|
|
if (fn(v, k)) {
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-16 22:31:36 -04:00
|
|
|
}
|
2010-03-30 09:02:51 -04:00
|
|
|
return false;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.parameter = function(name) {
|
2010-07-24 15:27:11 -04:00
|
|
|
return (this.variables[name] = 'param');
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.check = function(name) {
|
2010-06-16 06:21:42 -04:00
|
|
|
if (this.variables.hasOwnProperty(name)) {
|
2010-02-27 19:46:45 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return !!(this.parent && this.parent.check(name));
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.freeVariable = function() {
|
2010-02-27 19:46:45 -05:00
|
|
|
var ordinal;
|
2010-06-12 19:05:13 -04:00
|
|
|
while (this.check(this.tempVar)) {
|
|
|
|
ordinal = 1 + parseInt(this.tempVar.substr(1), 36);
|
|
|
|
this.tempVar = '_' + ordinal.toString(36).replace(/\d/g, 'a');
|
2010-02-27 19:46:45 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
this.variables[this.tempVar] = 'var';
|
|
|
|
return this.tempVar;
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.assign = function(name, value) {
|
2010-07-24 15:27:11 -04:00
|
|
|
return (this.variables[name] = {
|
2010-02-27 19:46:45 -05:00
|
|
|
value: value,
|
|
|
|
assigned: true
|
2010-07-24 15:27:11 -04:00
|
|
|
});
|
2010-03-30 09:02:51 -04:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.hasDeclarations = function(body) {
|
2010-03-30 09:02:51 -04:00
|
|
|
return body === this.expressions && this.any(function(k, val) {
|
|
|
|
return val === 'var';
|
|
|
|
});
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.hasAssignments = function(body) {
|
2010-03-30 19:42:09 -04:00
|
|
|
return body === this.expressions && this.any(function(k, val) {
|
2010-03-30 09:02:51 -04:00
|
|
|
return val.assigned;
|
2010-03-30 19:42:09 -04:00
|
|
|
});
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.declaredVariables = function() {
|
2010-02-27 19:46:45 -05:00
|
|
|
var _a, _b, key, val;
|
|
|
|
return (function() {
|
|
|
|
_a = []; _b = this.variables;
|
2010-07-16 22:31:36 -04:00
|
|
|
for (key in _b) {
|
|
|
|
if (!__hasProp.call(_b, key)) continue;
|
2010-02-27 19:46:45 -05:00
|
|
|
val = _b[key];
|
2010-03-21 07:17:58 -04:00
|
|
|
val === 'var' ? _a.push(key) : null;
|
2010-07-16 22:31:36 -04:00
|
|
|
}
|
2010-02-27 19:46:45 -05:00
|
|
|
return _a;
|
|
|
|
}).call(this).sort();
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.assignedVariables = function() {
|
2010-02-27 19:46:45 -05:00
|
|
|
var _a, _b, key, val;
|
2010-02-16 18:00:40 -05:00
|
|
|
_a = []; _b = this.variables;
|
2010-07-16 22:31:36 -04:00
|
|
|
for (key in _b) {
|
|
|
|
if (!__hasProp.call(_b, key)) continue;
|
2010-02-16 18:00:40 -05:00
|
|
|
val = _b[key];
|
2010-06-27 23:55:18 -04:00
|
|
|
val.assigned ? _a.push("" + key + " = " + val.value) : null;
|
2010-07-16 22:31:36 -04:00
|
|
|
}
|
2010-02-16 18:00:40 -05:00
|
|
|
return _a;
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.compiledDeclarations = function() {
|
|
|
|
return this.declaredVariables().join(', ');
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.compiledAssignments = function() {
|
|
|
|
return this.assignedVariables().join(', ');
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
|
|
|
return Scope;
|
|
|
|
}).call(this);
|
2010-02-24 18:56:32 -05:00
|
|
|
})();
|