2010-07-24 15:31:43 +00:00
|
|
|
(function() {
|
2010-03-30 23:42:09 +00:00
|
|
|
var Scope;
|
2010-03-30 20:48:43 +00:00
|
|
|
var __hasProp = Object.prototype.hasOwnProperty;
|
2010-08-14 21:25:29 +00:00
|
|
|
if (!(typeof process !== "undefined" && process !== null)) {
|
2010-02-18 04:22:05 +00:00
|
|
|
this.exports = this;
|
2010-02-13 20:25:04 +00:00
|
|
|
}
|
2010-02-28 00:46:45 +00:00
|
|
|
exports.Scope = (function() {
|
2010-05-15 03:40:04 +00:00
|
|
|
Scope = function(parent, expressions, method) {
|
2010-09-19 12:29:15 +00:00
|
|
|
var _cache, _cache2, k, val;
|
|
|
|
_cache = [parent, expressions, method];
|
|
|
|
this.parent = _cache[0];
|
|
|
|
this.expressions = _cache[1];
|
|
|
|
this.method = _cache[2];
|
2010-02-28 00:46:45 +00:00
|
|
|
this.variables = {};
|
2010-09-19 12:37:26 +00:00
|
|
|
this.tempVars = {};
|
2010-03-30 23:21:14 +00:00
|
|
|
if (this.parent) {
|
2010-09-19 12:29:15 +00:00
|
|
|
_cache2 = this.parent.tempVars;
|
|
|
|
for (k in _cache2) {
|
|
|
|
if (!__hasProp.call(_cache2, k)) continue;
|
|
|
|
val = _cache2[k];
|
2010-09-19 11:36:37 +00:00
|
|
|
(this.tempVars[k] = val);
|
|
|
|
}
|
2010-03-30 23:21:14 +00:00
|
|
|
} else {
|
|
|
|
Scope.root = this;
|
|
|
|
}
|
2010-02-28 00:46:45 +00:00
|
|
|
return this;
|
2010-02-09 02:10:48 +00:00
|
|
|
};
|
2010-03-30 23:21:14 +00:00
|
|
|
Scope.root = null;
|
2010-08-25 02:19:53 +00:00
|
|
|
Scope.prototype.find = function(name, options) {
|
|
|
|
if (this.check(name, options)) {
|
2010-02-28 00:46:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
this.variables[name] = 'var';
|
|
|
|
return false;
|
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
Scope.prototype.any = function(fn) {
|
2010-09-19 12:29:15 +00:00
|
|
|
var _cache, k, v;
|
|
|
|
_cache = this.variables;
|
|
|
|
for (v in _cache) {
|
|
|
|
if (!__hasProp.call(_cache, v)) continue;
|
|
|
|
k = _cache[v];
|
2010-03-30 13:02:51 +00:00
|
|
|
if (fn(v, k)) {
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-17 02:31:36 +00:00
|
|
|
}
|
2010-03-30 13:02:51 +00:00
|
|
|
return false;
|
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
Scope.prototype.parameter = function(name) {
|
2010-07-24 19:27:11 +00:00
|
|
|
return (this.variables[name] = 'param');
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2010-08-25 02:19:53 +00:00
|
|
|
Scope.prototype.check = function(name, options) {
|
|
|
|
var immediate;
|
|
|
|
immediate = Object.prototype.hasOwnProperty.call(this.variables, name);
|
|
|
|
if (immediate || (options && options.immediate)) {
|
|
|
|
return immediate;
|
2010-02-28 00:46:45 +00:00
|
|
|
}
|
|
|
|
return !!(this.parent && this.parent.check(name));
|
|
|
|
};
|
2010-09-19 11:55:19 +00:00
|
|
|
Scope.prototype.freeVariable = function(type) {
|
|
|
|
var next;
|
2010-09-19 12:37:26 +00:00
|
|
|
next = function(prev) {
|
|
|
|
return '_' + type + ((prev && Number(prev.match(/\d+$/) || 1) + 1) || '');
|
|
|
|
};
|
2010-09-19 11:55:19 +00:00
|
|
|
while (this.check(this.tempVars[type] || (this.tempVars[type] = next()))) {
|
|
|
|
this.tempVars[type] = next(this.tempVars[type]);
|
2010-02-28 00:46:45 +00:00
|
|
|
}
|
2010-09-19 11:55:19 +00:00
|
|
|
this.variables[this.tempVars[type]] = 'var';
|
|
|
|
return this.tempVars[type];
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
Scope.prototype.assign = function(name, value) {
|
2010-07-24 19:27:11 +00:00
|
|
|
return (this.variables[name] = {
|
2010-02-28 00:46:45 +00:00
|
|
|
value: value,
|
|
|
|
assigned: true
|
2010-07-24 19:27:11 +00:00
|
|
|
});
|
2010-03-30 13:02:51 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
Scope.prototype.hasDeclarations = function(body) {
|
2010-03-30 13:02:51 +00:00
|
|
|
return body === this.expressions && this.any(function(k, val) {
|
|
|
|
return val === 'var';
|
|
|
|
});
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
Scope.prototype.hasAssignments = function(body) {
|
2010-03-30 23:42:09 +00:00
|
|
|
return body === this.expressions && this.any(function(k, val) {
|
2010-03-30 13:02:51 +00:00
|
|
|
return val.assigned;
|
2010-03-30 23:42:09 +00:00
|
|
|
});
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
Scope.prototype.declaredVariables = function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
var _cache, _result, key, val;
|
2010-02-28 00:46:45 +00:00
|
|
|
return (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
_result = []; _cache = this.variables;
|
|
|
|
for (key in _cache) {
|
|
|
|
if (!__hasProp.call(_cache, key)) continue;
|
|
|
|
val = _cache[key];
|
2010-08-11 04:40:15 +00:00
|
|
|
if (val === 'var') {
|
2010-09-19 12:29:15 +00:00
|
|
|
_result.push(key);
|
2010-08-14 15:42:19 +00:00
|
|
|
}
|
2010-07-17 02:31:36 +00:00
|
|
|
}
|
2010-09-19 12:29:15 +00:00
|
|
|
return _result;
|
2010-02-28 00:46:45 +00:00
|
|
|
}).call(this).sort();
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
Scope.prototype.assignedVariables = function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
var _cache, _result, key, val;
|
|
|
|
_result = []; _cache = this.variables;
|
|
|
|
for (key in _cache) {
|
|
|
|
if (!__hasProp.call(_cache, key)) continue;
|
|
|
|
val = _cache[key];
|
2010-08-11 04:40:15 +00:00
|
|
|
if (val.assigned) {
|
2010-09-19 12:29:15 +00:00
|
|
|
_result.push("" + (key) + " = " + (val.value));
|
2010-08-14 15:42:19 +00:00
|
|
|
}
|
2010-07-17 02:31:36 +00:00
|
|
|
}
|
2010-09-19 12:29:15 +00:00
|
|
|
return _result;
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
Scope.prototype.compiledDeclarations = function() {
|
|
|
|
return this.declaredVariables().join(', ');
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
Scope.prototype.compiledAssignments = function() {
|
|
|
|
return this.assignedVariables().join(', ');
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
|
|
|
return Scope;
|
|
|
|
}).call(this);
|
2010-02-24 23:56:32 +00:00
|
|
|
})();
|