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-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-08 21:10:48 -05:00
|
|
|
// Scope objects form a tree corresponding to the shape of the function
|
2010-02-07 15:45:05 -05:00
|
|
|
// definitions present in the script. They provide lexical scope, to determine
|
|
|
|
// whether a variable has been seen before or if it needs to be declared.
|
2010-02-08 21:10:48 -05:00
|
|
|
//
|
|
|
|
// Initialize a scope with its parent, for lookups up the chain,
|
|
|
|
// as well as the Expressions body where it should declare its variables,
|
|
|
|
// and the function that it wraps.
|
|
|
|
Scope = (exports.Scope = function Scope(parent, expressions, method) {
|
2010-02-17 23:37:39 -05:00
|
|
|
var _a;
|
|
|
|
_a = [parent, expressions, method];
|
|
|
|
this.parent = _a[0];
|
|
|
|
this.expressions = _a[1];
|
|
|
|
this.method = _a[2];
|
2010-02-13 01:13:08 -05:00
|
|
|
this.variables = {};
|
2010-02-16 18:00:40 -05:00
|
|
|
this.temp_var = this.parent ? this.parent.temp_var : '_a';
|
2010-02-09 08:15:11 -05:00
|
|
|
return this;
|
2010-02-08 21:10:48 -05:00
|
|
|
});
|
2010-02-07 15:45:05 -05:00
|
|
|
// Look up a variable in lexical scope, or declare it if not found.
|
2010-02-17 23:26:03 -05:00
|
|
|
Scope.prototype.find = function find(name) {
|
2010-02-17 23:37:39 -05:00
|
|
|
if (this.check(name)) {
|
|
|
|
return true;
|
2010-02-07 15:45:05 -05:00
|
|
|
}
|
|
|
|
this.variables[name] = 'var';
|
2010-02-17 23:37:39 -05:00
|
|
|
return false;
|
2010-02-07 15:45:05 -05:00
|
|
|
};
|
|
|
|
// Define a local variable as originating from a parameter in current scope
|
|
|
|
// -- no var required.
|
2010-02-08 21:10:48 -05:00
|
|
|
Scope.prototype.parameter = function parameter(name) {
|
2010-02-07 15:45:05 -05:00
|
|
|
return this.variables[name] = 'param';
|
|
|
|
};
|
|
|
|
// Just check to see if a variable has already been declared.
|
2010-02-08 21:10:48 -05:00
|
|
|
Scope.prototype.check = function check(name) {
|
|
|
|
if (this.variables[name]) {
|
2010-02-07 15:45:05 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return !!(this.parent && this.parent.check(name));
|
|
|
|
};
|
|
|
|
// You can reset a found variable on the immediate scope.
|
2010-02-08 21:10:48 -05:00
|
|
|
Scope.prototype.reset = function reset(name) {
|
|
|
|
return delete this.variables[name];
|
|
|
|
};
|
|
|
|
// Find an available, short, name for a compiler-generated variable.
|
|
|
|
Scope.prototype.free_variable = function free_variable() {
|
2010-02-16 18:00:40 -05:00
|
|
|
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');
|
2010-02-08 21:10:48 -05:00
|
|
|
}
|
2010-02-16 18:00:40 -05:00
|
|
|
this.variables[this.temp_var] = 'var';
|
|
|
|
return this.temp_var;
|
2010-02-08 21:10:48 -05:00
|
|
|
};
|
|
|
|
// Ensure that an assignment is made at the top of scope (or top-level
|
|
|
|
// scope, if requested).
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
// Return the list of variables first declared in current scope.
|
|
|
|
Scope.prototype.declared_variables = function declared_variables() {
|
2010-02-16 18:00:40 -05:00
|
|
|
var _a, _b, key, val;
|
2010-02-17 19:19:51 -05:00
|
|
|
return (function() {
|
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-14 17:35:14 -05:00
|
|
|
if (val === 'var') {
|
2010-02-16 18:00:40 -05:00
|
|
|
_a.push(key);
|
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-17 19:19:51 -05:00
|
|
|
}).call(this).sort();
|
2010-02-08 21:10:48 -05:00
|
|
|
};
|
|
|
|
// Return the list of variables that are supposed to be assigned at the top
|
|
|
|
// of scope.
|
|
|
|
Scope.prototype.assigned_variables = function assigned_variables() {
|
2010-02-16 18:00:40 -05:00
|
|
|
var _a, _b, key, val;
|
2010-02-17 23:37:39 -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-17 23:37:39 -05:00
|
|
|
val = _b[key];
|
|
|
|
if (val.assigned) {
|
|
|
|
_a.push(key + ' = ' + val.value);
|
2010-02-08 21:10:48 -05:00
|
|
|
}
|
2010-02-18 20:22:53 -05:00
|
|
|
}}
|
2010-02-17 23:37:39 -05:00
|
|
|
return _a;
|
2010-02-08 21:10:48 -05:00
|
|
|
};
|
2010-02-17 23:37:39 -05:00
|
|
|
// Compile the string representing all of the declared variables for this scope.
|
2010-02-08 21:10:48 -05:00
|
|
|
Scope.prototype.compiled_declarations = function compiled_declarations() {
|
|
|
|
return this.declared_variables().join(', ');
|
|
|
|
};
|
2010-02-17 23:37:39 -05:00
|
|
|
// Compile the string performing all of the variable assignments for this scope.
|
2010-02-08 21:10:48 -05:00
|
|
|
Scope.prototype.compiled_assignments = function compiled_assignments() {
|
2010-02-17 23:37:39 -05:00
|
|
|
return this.assigned_variables().join(', ');
|
2010-02-08 21:10:48 -05:00
|
|
|
};
|
2010-02-07 15:45:05 -05:00
|
|
|
})();
|