light refactors to scope.coffee

This commit is contained in:
Jeremy Ashkenas 2010-02-17 23:37:39 -05:00
parent 138692183b
commit 879b3d76bd
2 changed files with 26 additions and 35 deletions

View File

@ -12,22 +12,22 @@
// 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) {
this.parent = parent;
this.expressions = expressions;
this.method = 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;
});
// Look up a variable in lexical scope, or declare it if not found.
Scope.prototype.find = function find(name) {
var found;
found = this.check(name);
if (found) {
return found;
if (this.check(name)) {
return true;
}
this.variables[name] = 'var';
return found;
return false;
};
// Define a local variable as originating from a parameter in current scope
// -- no var required.
@ -94,29 +94,21 @@
// of scope.
Scope.prototype.assigned_variables = function assigned_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.assigned) {
_a.push([key, val.value]);
}
_a = []; _b = this.variables;
for (key in _b) if (__hasProp.call(_b, key)) {
val = _b[key];
if (val.assigned) {
_a.push(key + ' = ' + val.value);
}
return _a;
}).call(this).sort();
}
return _a;
};
// Compile the string representing all of the declared variables for this scope.
Scope.prototype.compiled_declarations = function compiled_declarations() {
return this.declared_variables().join(', ');
};
// Compile the string performing all of the variable assignments for this scope.
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(', ');
return this.assigned_variables().join(', ');
};
})();

View File

@ -8,19 +8,16 @@ this.exports: this unless process?
# as well as the Expressions body where it should declare its variables,
# and the function that it wraps.
Scope: exports.Scope: (parent, expressions, method) ->
@parent: parent
@expressions: expressions
@method: method
[@parent, @expressions, @method]: [parent, expressions, method]
@variables: {}
@temp_var: if @parent then @parent.temp_var else '_a'
this
# Look up a variable in lexical scope, or declare it if not found.
Scope::find: (name) ->
found: @check name
return found if found
return true if @check name
@variables[name]: 'var'
found
false
# Define a local variable as originating from a parameter in current scope
# -- no var required.
@ -67,10 +64,12 @@ Scope::declared_variables: ->
# Return the list of variables that are supposed to be assigned at the top
# of scope.
Scope::assigned_variables: ->
([key, val.value] for key, val of @variables when val.assigned).sort()
key + ' = ' + val.value for key, val of @variables when val.assigned
# Compile the string representing all of the declared variables for this scope.
Scope::compiled_declarations: ->
@declared_variables().join(', ')
@declared_variables().join ', '
# Compile the string performing all of the variable assignments for this scope.
Scope::compiled_assignments: ->
(t[0] + ' = ' + t[1] for t in @assigned_variables()).join(', ')
@assigned_variables().join ', '