Files
coffeescript/lib/scope.js

114 lines
3.3 KiB
JavaScript

(function() {
var Scope;
var __hasProp = Object.prototype.hasOwnProperty;
if (!(typeof process !== "undefined" && process !== null)) {
this.exports = this;
}
exports.Scope = (function() {
Scope = function(parent, expressions, method) {
var _cache;
_cache = [parent, expressions, method];
this.parent = _cache[0];
this.expressions = _cache[1];
this.method = _cache[2];
this.variables = {};
if (!this.parent) {
Scope.root = this;
}
return this;
};
Scope.root = null;
Scope.prototype.find = function(name, options) {
if (this.check(name, options)) {
return true;
}
this.variables[name] = 'var';
return false;
};
Scope.prototype.any = function(fn) {
var _cache, k, v;
_cache = this.variables;
for (v in _cache) {
if (!__hasProp.call(_cache, v)) continue;
k = _cache[v];
if (fn(v, k)) {
return true;
}
}
return false;
};
Scope.prototype.parameter = function(name) {
return (this.variables[name] = 'param');
};
Scope.prototype.check = function(name, options) {
var immediate;
immediate = Object.prototype.hasOwnProperty.call(this.variables, name);
if (immediate || (options && options.immediate)) {
return immediate;
}
return !!(this.parent && this.parent.check(name));
};
Scope.prototype.temporary = function(type, index) {
return '_' + type + (index ? (index + 1) : '');
};
Scope.prototype.freeVariable = function(type) {
var index, temp;
index = 0;
while (this.check(temp = this.temporary(type, index))) {
index++;
}
this.variables[temp] = 'var';
return temp;
};
Scope.prototype.assign = function(name, value) {
return (this.variables[name] = {
value: value,
assigned: true
});
};
Scope.prototype.hasDeclarations = function(body) {
return body === this.expressions && this.any(function(k, val) {
return val === 'var';
});
};
Scope.prototype.hasAssignments = function(body) {
return body === this.expressions && this.any(function(k, val) {
return val.assigned;
});
};
Scope.prototype.declaredVariables = function() {
var _cache, _result, key, val;
return (function() {
_result = []; _cache = this.variables;
for (key in _cache) {
if (!__hasProp.call(_cache, key)) continue;
val = _cache[key];
if (val === 'var') {
_result.push(key);
}
}
return _result;
}).call(this).sort();
};
Scope.prototype.assignedVariables = function() {
var _cache, _result, key, val;
_result = []; _cache = this.variables;
for (key in _cache) {
if (!__hasProp.call(_cache, key)) continue;
val = _cache[key];
if (val.assigned) {
_result.push("" + (key) + " = " + (val.value));
}
}
return _result;
};
Scope.prototype.compiledDeclarations = function() {
return this.declaredVariables().join(', ');
};
Scope.prototype.compiledAssignments = function() {
return this.assignedVariables().join(', ');
};
return Scope;
}).call(this);
})();