1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/lib/scope.js

112 lines
3.1 KiB
JavaScript
Raw Normal View History

(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 _a;
_a = [parent, expressions, method];
this.parent = _a[0];
this.expressions = _a[1];
this.method = _a[2];
this.variables = {};
if (this.parent) {
this.tempVar = this.parent.tempVar;
} else {
Scope.root = this;
this.tempVar = '_a';
}
return this;
};
Scope.root = null;
Scope.prototype.find = function(name) {
if (this.check(name)) {
return true;
}
this.variables[name] = 'var';
return false;
};
Scope.prototype.any = function(fn) {
var _a, k, v;
_a = this.variables;
for (v in _a) {
if (!__hasProp.call(_a, v)) continue;
k = _a[v];
if (fn(v, k)) {
return true;
}
}
return false;
};
Scope.prototype.parameter = function(name) {
return (this.variables[name] = 'param');
};
Scope.prototype.check = function(name) {
if (this.variables.hasOwnProperty(name)) {
return true;
}
return !!(this.parent && this.parent.check(name));
};
Scope.prototype.freeVariable = function() {
var ordinal;
while (this.check(this.tempVar)) {
ordinal = 1 + parseInt(this.tempVar.substr(1), 36);
this.tempVar = '_' + ordinal.toString(36).replace(/\d/g, 'a');
}
this.variables[this.tempVar] = 'var';
return this.tempVar;
};
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 _a, _b, key, val;
return (function() {
_a = []; _b = this.variables;
for (key in _b) {
if (!__hasProp.call(_b, key)) continue;
val = _b[key];
2010-08-11 00:40:15 -04:00
if (val === 'var') {
_a.push(key);
};
}
return _a;
}).call(this).sort();
};
Scope.prototype.assignedVariables = function() {
var _a, _b, key, val;
_a = []; _b = this.variables;
for (key in _b) {
if (!__hasProp.call(_b, key)) continue;
val = _b[key];
2010-08-11 00:40:15 -04:00
if (val.assigned) {
_a.push(("" + (key) + " = " + (val.value)));
};
}
return _a;
};
Scope.prototype.compiledDeclarations = function() {
return this.declaredVariables().join(', ');
};
Scope.prototype.compiledAssignments = function() {
return this.assignedVariables().join(', ');
};
return Scope;
}).call(this);
})();