jashkenas--coffeescript/lib/coffee-script/scope.js

147 lines
3.6 KiB
JavaScript
Raw Normal View History

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