jashkenas--coffeescript/lib/scope.js

117 lines
3.2 KiB
JavaScript
Raw Normal View History

(function() {
var Scope, extend, last, _ref;
2010-10-01 23:21:34 +00:00
_ref = require('./helpers'), extend = _ref.extend, last = _ref.last;
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 = {};
if (!this.parent) {
Scope.root = this;
}
}
2010-10-21 03:09:06 +00:00
Scope.prototype.add = function(name, type) {
2010-10-25 01:42:37 +00:00
var pos;
if (typeof (pos = this.positions[name]) === 'number') {
return this.variables[pos].type = type;
} else {
return this.positions[name] = this.variables.push({
name: name,
type: type
}) - 1;
}
};
Scope.prototype.find = function(name, options) {
if (this.check(name, options)) {
return true;
}
2010-10-21 03:09:06 +00:00
this.add(name, 'var');
2010-12-07 04:32:32 +00:00
this.hasDeclarations = true;
return false;
};
Scope.prototype.parameter = function(name) {
if (this.shared && this.check(name, true)) {
return;
}
2010-10-21 03:09:06 +00:00
return this.add(name, 'param');
};
Scope.prototype.check = function(name, immediate) {
var found, _ref;
found = !!this.type(name);
if (found || immediate) {
return found;
}
return !!((_ref = this.parent) != null ? _ref.check(name) : void 0);
};
2010-10-20 08:13:43 +00:00
Scope.prototype.temporary = function(name, index) {
if (name.length > 1) {
return '_' + name + (index > 1 ? index : '');
} else {
return '_' + (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a');
}
};
Scope.prototype.type = function(name) {
var v, _i, _len, _ref;
_ref = this.variables;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
v = _ref[_i];
if (v.name === name) {
return v.type;
}
}
return null;
};
Scope.prototype.freeVariable = function(type) {
var index, temp;
index = 0;
while (this.check((temp = this.temporary(type, index)), true)) {
index++;
}
2010-10-21 03:09:06 +00:00
this.add(temp, 'var');
2010-12-07 04:32:32 +00:00
this.hasDeclarations = true;
return temp;
};
Scope.prototype.assign = function(name, value) {
2010-12-07 04:32:32 +00:00
this.add(name, {
value: value,
assigned: true
});
2010-12-07 04:32:32 +00:00
return this.hasAssignments = true;
};
Scope.prototype.declaredVariables = function() {
2010-12-07 04:32:32 +00:00
var realVars, tempVars, v, _i, _len, _ref;
realVars = [];
tempVars = [];
_ref = this.variables;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
v = _ref[_i];
if (v.type === 'var') {
2010-12-07 04:32:32 +00:00
(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() {
2010-11-09 05:17:08 +00:00
var v, _i, _len, _ref, _results;
_ref = this.variables;
2010-11-09 05:17:08 +00:00
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
v = _ref[_i];
if (v.type.assigned) {
2010-11-09 05:17:08 +00:00
_results.push("" + v.name + " = " + v.type.value);
}
}
2010-11-09 05:17:08 +00:00
return _results;
};
return Scope;
}();
}).call(this);