jashkenas--coffeescript/lib/scope.js

114 lines
3.4 KiB
JavaScript
Raw Normal View History

(function() {
2010-10-01 23:21:34 +00:00
var Scope, _ref, extend, last;
var __hasProp = Object.prototype.hasOwnProperty;
2010-10-01 23:21:34 +00:00
_ref = require('./helpers'), extend = _ref.extend, last = _ref.last;
exports.Scope = (function() {
2010-10-05 03:21:16 +00:00
Scope = (function() {
return function Scope(_arg, _arg2, _arg3) {
this.method = _arg3;
this.expressions = _arg2;
this.parent = _arg;
this.variables = {
'arguments': 'arguments'
};
if (!(this.parent)) {
2010-10-05 03:21:16 +00:00
Scope.root = this;
}
return this;
2010-10-01 23:21:34 +00:00
};
2010-10-05 03:21:16 +00:00
})();
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) {
2010-10-01 23:21:34 +00:00
var _ref2, k, v;
_ref2 = this.variables;
for (v in _ref2) {
if (!__hasProp.call(_ref2, v)) continue;
k = _ref2[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) {
2010-10-01 23:21:34 +00:00
var _ref2, immediate;
immediate = Object.prototype.hasOwnProperty.call(this.variables, 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.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() {
2010-10-01 23:21:34 +00:00
var _ref2, _result, key, val;
return (function() {
2010-10-01 23:21:34 +00:00
_result = []; _ref2 = this.variables;
for (key in _ref2) {
if (!__hasProp.call(_ref2, key)) continue;
val = _ref2[key];
if (val === 'var') {
_result.push(key);
}
}
return _result;
}).call(this).sort();
};
Scope.prototype.assignedVariables = function() {
2010-10-01 23:21:34 +00:00
var _ref2, _result, key, val;
_result = []; _ref2 = this.variables;
for (key in _ref2) {
if (!__hasProp.call(_ref2, key)) continue;
val = _ref2[key];
2010-08-11 04:40:15 +00:00
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);
}).call(this);