2010-07-24 11:31:43 -04:00
|
|
|
(function() {
|
2010-11-01 23:58:03 -04:00
|
|
|
var Scope, extend, last, _ref;
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-10-01 19:21:34 -04:00
|
|
|
_ref = require('./helpers'), extend = _ref.extend, last = _ref.last;
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-12-23 13:50:52 -05:00
|
|
|
exports.Scope = Scope = (function() {
|
2011-12-18 20:00:09 -05:00
|
|
|
Scope.name = 'Scope';
|
2010-12-06 01:01:57 -05:00
|
|
|
Scope.root = null;
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-11-20 20:39:35 -05:00
|
|
|
function Scope(parent, expressions, method) {
|
|
|
|
this.parent = parent;
|
|
|
|
this.expressions = expressions;
|
|
|
|
this.method = method;
|
2010-11-11 21:48:08 -05:00
|
|
|
this.variables = [
|
|
|
|
{
|
|
|
|
name: 'arguments',
|
|
|
|
type: 'arguments'
|
2010-10-04 23:21:16 -04:00
|
|
|
}
|
2010-11-11 21:48:08 -05:00
|
|
|
];
|
|
|
|
this.positions = {};
|
2011-08-07 02:59:37 -04:00
|
|
|
if (!this.parent) Scope.root = this;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-12-13 21:24:32 -05:00
|
|
|
Scope.prototype.add = function(name, type, immediate) {
|
2011-08-07 02:59:37 -04:00
|
|
|
if (this.shared && !immediate) return this.parent.add(name, type, immediate);
|
2011-11-14 11:18:45 -05:00
|
|
|
if (Object.prototype.hasOwnProperty.call(this.positions, name)) {
|
2011-11-10 03:08:38 -05:00
|
|
|
return this.variables[this.positions[name]].type = type;
|
2010-11-08 23:07:51 -05:00
|
|
|
} else {
|
|
|
|
return this.positions[name] = this.variables.push({
|
|
|
|
name: name,
|
|
|
|
type: type
|
|
|
|
}) - 1;
|
|
|
|
}
|
2010-10-19 19:51:34 -04:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-08-24 22:19:53 -04:00
|
|
|
Scope.prototype.find = function(name, options) {
|
2011-08-07 02:59:37 -04:00
|
|
|
if (this.check(name, options)) return true;
|
2010-10-20 23:09:06 -04:00
|
|
|
this.add(name, 'var');
|
2010-03-30 09:02:51 -04:00
|
|
|
return false;
|
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.parameter = function(name) {
|
2011-08-07 02:59:37 -04:00
|
|
|
if (this.shared && this.parent.check(name, true)) return;
|
2010-10-20 23:09:06 -04:00
|
|
|
return this.add(name, 'param');
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-11-02 00:31:42 -04:00
|
|
|
Scope.prototype.check = function(name, immediate) {
|
2011-03-28 17:12:27 -04:00
|
|
|
var found, _ref2;
|
2010-11-02 00:31:42 -04:00
|
|
|
found = !!this.type(name);
|
2011-08-07 02:59:37 -04:00
|
|
|
if (found || immediate) return found;
|
2011-03-28 17:12:27 -04:00
|
|
|
return !!((_ref2 = this.parent) != null ? _ref2.check(name) : void 0);
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-10-20 04:13:43 -04:00
|
|
|
Scope.prototype.temporary = function(name, index) {
|
2010-11-28 12:28:46 -05:00
|
|
|
if (name.length > 1) {
|
|
|
|
return '_' + name + (index > 1 ? index : '');
|
|
|
|
} else {
|
|
|
|
return '_' + (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a');
|
|
|
|
}
|
2010-09-19 09:18:01 -04:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-10-19 19:36:50 -04:00
|
|
|
Scope.prototype.type = function(name) {
|
2011-03-28 17:12:27 -04:00
|
|
|
var v, _i, _len, _ref2;
|
|
|
|
_ref2 = this.variables;
|
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
|
|
|
v = _ref2[_i];
|
2011-08-07 02:59:37 -04:00
|
|
|
if (v.name === name) return v.type;
|
2010-10-19 19:36:50 -04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2011-12-14 12:38:19 -05:00
|
|
|
Scope.prototype.freeVariable = function(name, reserve) {
|
2010-11-27 21:04:40 -05:00
|
|
|
var index, temp;
|
2011-12-14 12:38:19 -05:00
|
|
|
if (reserve == null) reserve = true;
|
2010-09-19 10:03:45 -04:00
|
|
|
index = 0;
|
2011-12-14 12:38:19 -05:00
|
|
|
while (this.check((temp = this.temporary(name, index)))) {
|
2010-09-19 10:03:45 -04:00
|
|
|
index++;
|
2010-02-27 19:46:45 -05:00
|
|
|
}
|
2011-12-14 12:38:19 -05:00
|
|
|
if (reserve) this.add(temp, 'var', true);
|
2010-09-19 08:50:17 -04:00
|
|
|
return temp;
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.assign = function(name, value) {
|
2010-12-06 23:32:32 -05:00
|
|
|
this.add(name, {
|
2010-10-19 19:51:34 -04:00
|
|
|
value: value,
|
|
|
|
assigned: true
|
2011-11-10 03:08:38 -05:00
|
|
|
}, true);
|
2010-12-06 23:32:32 -05:00
|
|
|
return this.hasAssignments = true;
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-12-12 12:16:27 -05:00
|
|
|
Scope.prototype.hasDeclarations = function() {
|
|
|
|
return !!this.declaredVariables().length;
|
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.declaredVariables = function() {
|
2011-03-28 17:12:27 -04:00
|
|
|
var realVars, tempVars, v, _i, _len, _ref2;
|
2010-12-06 23:32:32 -05:00
|
|
|
realVars = [];
|
|
|
|
tempVars = [];
|
2011-03-28 17:12:27 -04:00
|
|
|
_ref2 = this.variables;
|
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
|
|
|
v = _ref2[_i];
|
2011-08-08 12:55:22 -04:00
|
|
|
if (v.type === 'var') {
|
|
|
|
(v.name.charAt(0) === '_' ? tempVars : realVars).push(v.name);
|
|
|
|
}
|
2010-11-01 23:58:03 -04:00
|
|
|
}
|
2010-12-06 23:32:32 -05:00
|
|
|
return realVars.sort().concat(tempVars.sort());
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.assignedVariables = function() {
|
2011-03-28 17:12:27 -04:00
|
|
|
var v, _i, _len, _ref2, _results;
|
|
|
|
_ref2 = this.variables;
|
2010-11-09 00:17:08 -05:00
|
|
|
_results = [];
|
2011-03-28 17:12:27 -04:00
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
|
|
|
v = _ref2[_i];
|
2011-08-07 02:59:37 -04:00
|
|
|
if (v.type.assigned) _results.push("" + v.name + " = " + v.type.value);
|
2010-07-16 22:31:36 -04:00
|
|
|
}
|
2010-11-09 00:17:08 -05:00
|
|
|
return _results;
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-02-27 19:46:45 -05:00
|
|
|
return Scope;
|
2011-12-14 10:39:20 -05:00
|
|
|
|
2010-12-23 13:50:52 -05:00
|
|
|
})();
|
2011-12-14 10:39:20 -05:00
|
|
|
|
2010-09-21 03:53:58 -04:00
|
|
|
}).call(this);
|