2010-02-07 15:45:05 -05:00
|
|
|
(function(){
|
2010-02-17 23:22:05 -05:00
|
|
|
var Scope;
|
2010-02-07 15:45:05 -05:00
|
|
|
var __hasProp = Object.prototype.hasOwnProperty;
|
2010-03-07 15:45:45 -05:00
|
|
|
// The **Scope** class regulates lexical scoping within CoffeeScript. As you
|
|
|
|
// generate code, you create a tree of scopes in the same shape as the nested
|
|
|
|
// function bodies. Each scope knows about the variables declared within it,
|
|
|
|
// and has a reference to its parent enclosing scope. In this way, we know which
|
|
|
|
// variables are new and need to be declared with `var`, and which are shared
|
|
|
|
// with the outside.
|
|
|
|
// Set up exported variables for both **Node.js** and the browser.
|
2010-02-17 23:22:05 -05:00
|
|
|
if (!((typeof process !== "undefined" && process !== null))) {
|
|
|
|
this.exports = this;
|
2010-02-13 15:25:04 -05:00
|
|
|
}
|
2010-02-27 19:46:45 -05:00
|
|
|
exports.Scope = (function() {
|
|
|
|
Scope = function Scope(parent, expressions, method) {
|
|
|
|
var _a;
|
|
|
|
_a = [parent, expressions, method];
|
|
|
|
this.parent = _a[0];
|
|
|
|
this.expressions = _a[1];
|
|
|
|
this.method = _a[2];
|
|
|
|
this.variables = {};
|
|
|
|
this.temp_var = this.parent ? this.parent.temp_var : '_a';
|
|
|
|
return this;
|
2010-02-08 21:10:48 -05:00
|
|
|
};
|
2010-03-07 15:45:45 -05:00
|
|
|
// Initialize a scope with its parent, for lookups up the chain,
|
|
|
|
// as well as a reference to the **Expressions** node is belongs to, which is
|
|
|
|
// where it should declare its variables, and a reference to the function that
|
|
|
|
// it wraps.
|
|
|
|
// Look up a variable name in lexical scope, and declare it if it does not
|
|
|
|
// already exist.
|
2010-02-27 19:46:45 -05:00
|
|
|
Scope.prototype.find = function find(name) {
|
|
|
|
if (this.check(name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
this.variables[name] = 'var';
|
|
|
|
return false;
|
|
|
|
};
|
2010-03-07 15:45:45 -05:00
|
|
|
// Reserve a variable name as originating from a function parameter for this
|
|
|
|
// scope. No `var` required for internal references.
|
2010-02-27 19:46:45 -05:00
|
|
|
Scope.prototype.parameter = function parameter(name) {
|
|
|
|
return this.variables[name] = 'param';
|
|
|
|
};
|
2010-03-07 15:45:45 -05:00
|
|
|
// Just check to see if a variable has already been declared, without reserving.
|
2010-02-27 19:46:45 -05:00
|
|
|
Scope.prototype.check = function check(name) {
|
|
|
|
if (this.variables[name]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return !!(this.parent && this.parent.check(name));
|
|
|
|
};
|
2010-03-07 15:45:45 -05:00
|
|
|
// If we need to store an intermediate result, find an available name for a
|
|
|
|
// compiler-generated variable. `_a`, `_b`, and so on...
|
2010-02-27 19:46:45 -05:00
|
|
|
Scope.prototype.free_variable = function free_variable() {
|
|
|
|
var ordinal;
|
|
|
|
while (this.check(this.temp_var)) {
|
|
|
|
ordinal = 1 + parseInt(this.temp_var.substr(1), 36);
|
|
|
|
this.temp_var = '_' + ordinal.toString(36).replace(/\d/g, 'a');
|
|
|
|
}
|
|
|
|
this.variables[this.temp_var] = 'var';
|
|
|
|
return this.temp_var;
|
|
|
|
};
|
2010-03-07 15:45:45 -05:00
|
|
|
// Ensure that an assignment is made at the top of this scope
|
|
|
|
// (or at the top-level scope, if requested).
|
2010-02-27 19:46:45 -05:00
|
|
|
Scope.prototype.assign = function assign(name, value, top_level) {
|
|
|
|
if (top_level && this.parent) {
|
|
|
|
return this.parent.assign(name, value, top_level);
|
|
|
|
}
|
|
|
|
return this.variables[name] = {
|
|
|
|
value: value,
|
|
|
|
assigned: true
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// Does this scope reference any variables that need to be declared in the
|
|
|
|
// given function body?
|
|
|
|
Scope.prototype.has_declarations = function has_declarations(body) {
|
|
|
|
return body === this.expressions && this.declared_variables().length;
|
|
|
|
};
|
|
|
|
// Does this scope reference any assignments that need to be declared at the
|
|
|
|
// top of the given function body?
|
|
|
|
Scope.prototype.has_assignments = function has_assignments(body) {
|
|
|
|
return body === this.expressions && this.assigned_variables().length;
|
|
|
|
};
|
2010-03-07 15:45:45 -05:00
|
|
|
// Return the list of variables first declared in this scope.
|
2010-02-27 19:46:45 -05:00
|
|
|
Scope.prototype.declared_variables = function declared_variables() {
|
|
|
|
var _a, _b, key, val;
|
|
|
|
return (function() {
|
|
|
|
_a = []; _b = this.variables;
|
|
|
|
for (key in _b) { if (__hasProp.call(_b, key)) {
|
|
|
|
val = _b[key];
|
|
|
|
if (val === 'var') {
|
|
|
|
_a.push(key);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
return _a;
|
|
|
|
}).call(this).sort();
|
|
|
|
};
|
2010-03-07 15:45:45 -05:00
|
|
|
// Return the list of assignments that are supposed to be made at the top
|
|
|
|
// of this scope.
|
2010-02-27 19:46:45 -05:00
|
|
|
Scope.prototype.assigned_variables = function assigned_variables() {
|
|
|
|
var _a, _b, key, val;
|
2010-02-16 18:00:40 -05:00
|
|
|
_a = []; _b = this.variables;
|
2010-02-18 20:22:53 -05:00
|
|
|
for (key in _b) { if (__hasProp.call(_b, key)) {
|
2010-02-16 18:00:40 -05:00
|
|
|
val = _b[key];
|
2010-02-27 19:46:45 -05:00
|
|
|
if (val.assigned) {
|
2010-03-08 21:34:10 -05:00
|
|
|
_a.push('' + key + " = " + (val.value));
|
2010-02-08 21:10:48 -05:00
|
|
|
}
|
2010-02-18 20:22:53 -05:00
|
|
|
}}
|
2010-02-16 18:00:40 -05:00
|
|
|
return _a;
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-03-07 15:45:45 -05:00
|
|
|
// Compile the JavaScript for all of the variable declarations in this scope.
|
2010-02-27 19:46:45 -05:00
|
|
|
Scope.prototype.compiled_declarations = function compiled_declarations() {
|
|
|
|
return this.declared_variables().join(', ');
|
|
|
|
};
|
2010-03-07 15:45:45 -05:00
|
|
|
// Compile the JavaScript for all of the variable assignments in this scope.
|
2010-02-27 19:46:45 -05:00
|
|
|
Scope.prototype.compiled_assignments = function compiled_assignments() {
|
|
|
|
return this.assigned_variables().join(', ');
|
|
|
|
};
|
|
|
|
return Scope;
|
|
|
|
}).call(this);
|
2010-02-24 18:56:32 -05:00
|
|
|
})();
|