mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 03:21:20 -05:00
finished up the CoffeeScript version of the Scope object
This commit is contained in:
@@ -1,50 +1,27 @@
|
||||
(function(){
|
||||
var dup;
|
||||
var Scope, succ;
|
||||
var __hasProp = Object.prototype.hasOwnProperty;
|
||||
dup = function dup(input) {
|
||||
var __a, __b, __c, key, output, val;
|
||||
output = null;
|
||||
if (input instanceof Array) {
|
||||
output = [];
|
||||
__a = input;
|
||||
for (__b = 0; __b < __a.length; __b++) {
|
||||
val = __a[__b];
|
||||
output.push(val);
|
||||
}
|
||||
} else {
|
||||
output = {
|
||||
};
|
||||
__c = input;
|
||||
for (key in __c) {
|
||||
val = __c[key];
|
||||
if (__hasProp.call(__c, key)) {
|
||||
output.key = val;
|
||||
}
|
||||
}
|
||||
output;
|
||||
}
|
||||
return output;
|
||||
};
|
||||
// scope objects form a tree corresponding to the shape of the function
|
||||
// Scope objects form a tree corresponding to the shape of the function
|
||||
// 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.
|
||||
exports.Scope = function Scope(parent, expressions, func) {
|
||||
//
|
||||
// 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) {
|
||||
var __a;
|
||||
// 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.
|
||||
this.parent = parent;
|
||||
this.expressions = expressions;
|
||||
this.function = func;
|
||||
this.method = method;
|
||||
this.variables = {
|
||||
};
|
||||
__a = this.temp_variable = this.parent ? dup(this.parent.temp_variable) : '__a';
|
||||
this.temp_variable = this.parent ? this.parent.temp_variable : '__a';
|
||||
__a = this;
|
||||
return Scope === this.constructor ? this : __a;
|
||||
};
|
||||
});
|
||||
// Look up a variable in lexical scope, or declare it if not found.
|
||||
exports.Scope.prototype.find = function find(name, rem) {
|
||||
var found, remote;
|
||||
remote = (typeof rem !== "undefined" && rem !== null) ? rem : false;
|
||||
Scope.prototype.find = function find(name, remote) {
|
||||
var found;
|
||||
found = this.check(name);
|
||||
if (found || remote) {
|
||||
return found;
|
||||
@@ -54,20 +31,99 @@
|
||||
};
|
||||
// Define a local variable as originating from a parameter in current scope
|
||||
// -- no var required.
|
||||
exports.Scope.prototype.parameter = function parameter(name) {
|
||||
Scope.prototype.parameter = function parameter(name) {
|
||||
return this.variables[name] = 'param';
|
||||
};
|
||||
// Just check to see if a variable has already been declared.
|
||||
exports.Scope.prototype.check = function check(name) {
|
||||
if ((typeof this.variables[name] !== "undefined" && this.variables[name] !== null)) {
|
||||
Scope.prototype.check = function check(name) {
|
||||
if (this.variables[name]) {
|
||||
return true;
|
||||
}
|
||||
// TODO: what does that ruby !! mean..? need to follow up
|
||||
// .. this next line is prolly wrong ..
|
||||
return !!(this.parent && this.parent.check(name));
|
||||
};
|
||||
// You can reset a found variable on the immediate scope.
|
||||
exports.Scope.prototype.reset = function reset(name) {
|
||||
return this.variables[name] = undefined;
|
||||
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() {
|
||||
while (check(this.temp_variable)) {
|
||||
((this.temp_variable = succ(this.temp_variable)));
|
||||
}
|
||||
this.variables[this.temp_variable] = 'var';
|
||||
return this.temp_variable;
|
||||
};
|
||||
// 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() {
|
||||
var __a, __b, key, val;
|
||||
return ((function() {
|
||||
__a = []; __b = this.variables;
|
||||
for (key in __b) {
|
||||
val = __b[key];
|
||||
if (__hasProp.call(__b, key)) {
|
||||
if (val === 'var') {
|
||||
__a.push(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
return __a;
|
||||
}).call(this)).sort();
|
||||
};
|
||||
// Return the list of variables that are supposed to be assigned at the top
|
||||
// of scope.
|
||||
Scope.prototype.assigned_variables = function assigned_variables() {
|
||||
var __a, __b, key, val;
|
||||
return ((function() {
|
||||
__a = []; __b = this.variables;
|
||||
for (key in __b) {
|
||||
val = __b[key];
|
||||
if (__hasProp.call(__b, key)) {
|
||||
if (val.assigned) {
|
||||
__a.push([key, val.value]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return __a;
|
||||
}).call(this)).sort();
|
||||
};
|
||||
Scope.prototype.compiled_declarations = function compiled_declarations() {
|
||||
return this.declared_variables().join(', ');
|
||||
};
|
||||
Scope.prototype.compiled_assignments = function compiled_assignments() {
|
||||
var __a, __b, __c, t;
|
||||
return ((function() {
|
||||
__a = []; __b = this.assigned_variables();
|
||||
for (__c = 0; __c < __b.length; __c++) {
|
||||
t = __b[__c];
|
||||
__a.push(t[0] + ' = ' + t[1]);
|
||||
}
|
||||
return __a;
|
||||
}).call(this)).join(', ');
|
||||
};
|
||||
// Helper functions:
|
||||
// The next character alphabetically, to produce the following string.
|
||||
succ = function succ(str) {
|
||||
return str.slice(0, str.length - 1) + String.fromCharCode(str.charCodeAt(str.length - 1) + 1);
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user