jashkenas--coffeescript/lib/scope.js

166 lines
4.8 KiB
JavaScript
Raw Normal View History

(function() {
2010-10-01 23:21:34 +00:00
var Scope, _ref, extend, last;
_ref = require('./helpers'), extend = _ref.extend, last = _ref.last;
exports.Scope = (function() {
2010-10-05 03:21:16 +00:00
Scope = (function() {
function Scope(_arg, _arg2, _arg3) {
var _arg, _arg2, _arg3;
2010-10-05 03:21:16 +00:00
this.method = _arg3;
this.expressions = _arg2;
this.parent = _arg;
this.variables = [
{
name: 'arguments',
type: 'arguments'
}
];
if (this.parent) {
this.garbage = this.parent.garbage;
} else {
this.garbage = [];
2010-10-05 03:21:16 +00:00
Scope.root = this;
}
return this;
2010-10-01 23:21:34 +00:00
};
return Scope;
2010-10-05 03:21:16 +00:00
})();
Scope.root = null;
Scope.prototype.startLevel = function() {
return this.garbage.push([]);
};
Scope.prototype.endLevel = function() {
var _i, _len, _len2, _ref2, _result, garbage, i, v, vars;
vars = this.variables;
_result = [];
for (_i = 0, _len = (_ref2 = this.garbage.pop()).length; _i < _len; _i++) {
garbage = _ref2[_i];
if (this.type(garbage) === 'var') {
for (i = 0, _len2 = vars.length; i < _len2; i++) {
v = vars[i];
if (v.name === garbage.name) {
vars.splice(i, 1, {
name: garbage.name,
type: 'reuse'
});
break;
}
}
}
}
return _result;
};
Scope.prototype.find = function(name, options) {
if (this.check(name, options)) {
return true;
}
this.variables.push({
name: name,
type: 'var'
});
return false;
};
Scope.prototype.any = function(fn) {
var _i, _len, _ref2, v;
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
v = _ref2[_i];
if (fn(v.name, v.type)) {
return true;
}
}
return false;
};
Scope.prototype.parameter = function(name) {
return this.variables.push({
name: name,
type: 'param'
});
};
Scope.prototype.check = function(name, options) {
2010-10-01 23:21:34 +00:00
var _ref2, immediate;
immediate = !!this.type(name);
if (immediate || ((options != null) ? options.immediate : undefined)) {
return immediate;
}
return !!(((_ref2 = this.parent) != null) ? _ref2.check(name) : undefined);
};
Scope.prototype.temporary = function(type, index) {
return type.length > 1 ? '_' + type + (index > 1 ? index : '') : '_' + (index + parseInt(type, 36)).toString(36).replace(/\d/g, 'a');
};
Scope.prototype.type = function(name) {
var _i, _len, _ref2, v;
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
v = _ref2[_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)) && this.type(temp) !== 'reuse') {
index++;
}
this.variables.push({
name: temp,
type: 'var'
});
if (this.garbage.length) {
last(this.garbage).push(temp);
}
return temp;
};
Scope.prototype.assign = function(name, value) {
return this.variables.push({
name: name,
type: {
value: value,
assigned: true
}
});
};
Scope.prototype.hasDeclarations = function(body) {
return body === this.expressions && this.any(function(k, val) {
return (val === 'var' || val === 'reuse');
});
};
Scope.prototype.hasAssignments = function(body) {
return body === this.expressions && this.any(function(k, val) {
return val.assigned;
});
};
Scope.prototype.declaredVariables = function() {
var _i, _len, _ref2, _ref3, _result, v;
return (function() {
_result = [];
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
v = _ref2[_i];
if (((_ref3 = v.type) === 'var' || _ref3 === 'reuse')) {
_result.push(v.name);
}
}
return _result;
}).call(this).sort();
};
Scope.prototype.assignedVariables = function() {
var _i, _len, _ref2, _result, v;
_result = [];
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
v = _ref2[_i];
if (v.type.assigned) {
_result.push("" + (v.name) + " = " + (v.type.value));
}
}
return _result;
};
Scope.prototype.compiledDeclarations = function() {
return this.declaredVariables().join(', ');
};
Scope.prototype.compiledAssignments = function() {
return this.assignedVariables().join(', ');
};
return Scope;
}).call(this);
}).call(this);