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-08-14 17:25:29 -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-09-19 08:29:15 -04:00
|
|
|
var _cache, _cache2, k, val;
|
|
|
|
_cache = [parent, expressions, method];
|
|
|
|
this.parent = _cache[0];
|
|
|
|
this.expressions = _cache[1];
|
|
|
|
this.method = _cache[2];
|
2010-02-27 19:46:45 -05:00
|
|
|
this.variables = {};
|
2010-09-19 07:36:37 -04:00
|
|
|
this.tempVars = {
|
|
|
|
general: '_a'
|
|
|
|
};
|
2010-03-30 19:21:14 -04:00
|
|
|
if (this.parent) {
|
2010-09-19 08:29:15 -04:00
|
|
|
_cache2 = this.parent.tempVars;
|
|
|
|
for (k in _cache2) {
|
|
|
|
if (!__hasProp.call(_cache2, k)) continue;
|
|
|
|
val = _cache2[k];
|
2010-09-19 07:36:37 -04:00
|
|
|
(this.tempVars[k] = val);
|
|
|
|
}
|
2010-03-30 19:21:14 -04:00
|
|
|
} else {
|
|
|
|
Scope.root = this;
|
|
|
|
}
|
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-08-24 22:19:53 -04:00
|
|
|
Scope.prototype.find = function(name, options) {
|
|
|
|
if (this.check(name, options)) {
|
2010-02-27 19:46:45 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
this.variables[name] = 'var';
|
|
|
|
return false;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.any = function(fn) {
|
2010-09-19 08:29:15 -04:00
|
|
|
var _cache, k, v;
|
|
|
|
_cache = this.variables;
|
|
|
|
for (v in _cache) {
|
|
|
|
if (!__hasProp.call(_cache, v)) continue;
|
|
|
|
k = _cache[v];
|
2010-03-30 09:02:51 -04:00
|
|
|
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-08-24 22:19:53 -04: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-27 19:46:45 -05:00
|
|
|
}
|
|
|
|
return !!(this.parent && this.parent.check(name));
|
|
|
|
};
|
2010-09-19 07:55:19 -04:00
|
|
|
Scope.prototype.freeVariable = function(type) {
|
|
|
|
var next;
|
|
|
|
if (type) {
|
|
|
|
next = function(prev) {
|
|
|
|
return '_' + type + ((prev && Number(prev.match(/\d+$/) || 1) + 1) || '');
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
type = 'general';
|
|
|
|
next = function(prev) {
|
|
|
|
var ordinal;
|
|
|
|
ordinal = 1 + parseInt(prev.substr(1), 36);
|
|
|
|
return '_' + ordinal.toString(36).replace(/\d/g, 'a');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
while (this.check(this.tempVars[type] || (this.tempVars[type] = next()))) {
|
|
|
|
this.tempVars[type] = next(this.tempVars[type]);
|
2010-02-27 19:46:45 -05:00
|
|
|
}
|
2010-09-19 07:55:19 -04:00
|
|
|
this.variables[this.tempVars[type]] = 'var';
|
|
|
|
return this.tempVars[type];
|
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-09-19 08:29:15 -04:00
|
|
|
var _cache, _result, key, val;
|
2010-02-27 19:46:45 -05:00
|
|
|
return (function() {
|
2010-09-19 08:29:15 -04:00
|
|
|
_result = []; _cache = this.variables;
|
|
|
|
for (key in _cache) {
|
|
|
|
if (!__hasProp.call(_cache, key)) continue;
|
|
|
|
val = _cache[key];
|
2010-08-11 00:40:15 -04:00
|
|
|
if (val === 'var') {
|
2010-09-19 08:29:15 -04:00
|
|
|
_result.push(key);
|
2010-08-14 11:42:19 -04:00
|
|
|
}
|
2010-07-16 22:31:36 -04:00
|
|
|
}
|
2010-09-19 08:29:15 -04:00
|
|
|
return _result;
|
2010-02-27 19:46:45 -05:00
|
|
|
}).call(this).sort();
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.assignedVariables = function() {
|
2010-09-19 08:29:15 -04: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 00:40:15 -04:00
|
|
|
if (val.assigned) {
|
2010-09-19 08:29:15 -04:00
|
|
|
_result.push("" + (key) + " = " + (val.value));
|
2010-08-14 11:42:19 -04:00
|
|
|
}
|
2010-07-16 22:31:36 -04:00
|
|
|
}
|
2010-09-19 08:29:15 -04:00
|
|
|
return _result;
|
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
|
|
|
})();
|