Don't store `tempVars` as we can get the next available one from scope.

This commit is contained in:
Stan Angeloff 2010-09-19 17:03:45 +03:00
parent ed74f42323
commit 3ab0c12bf1
2 changed files with 11 additions and 21 deletions

View File

@ -6,21 +6,13 @@
}
exports.Scope = (function() {
Scope = function(parent, expressions, method) {
var _cache, _cache2, k, val;
var _cache;
_cache = [parent, expressions, method];
this.parent = _cache[0];
this.expressions = _cache[1];
this.method = _cache[2];
this.variables = {};
this.tempVars = {};
if (this.parent) {
_cache2 = this.parent.tempVars;
for (k in _cache2) {
if (!__hasProp.call(_cache2, k)) continue;
val = _cache2[k];
(this.tempVars[k] = val);
}
} else {
if (!this.parent) {
Scope.root = this;
}
return this;
@ -57,12 +49,13 @@
return !!(this.parent && this.parent.check(name));
};
Scope.prototype.temporary = function(type, index) {
return '_' + type + ((index) > 1 ? index : '');
return '_' + type + (index ? (index + 1) : '');
};
Scope.prototype.freeVariable = function(type) {
var temp;
while (this.check(temp = this.temporary(type, this.tempVars[type] || (this.tempVars[type] = 1)))) {
this.tempVars[type]++;
var index, temp;
index = 0;
while (this.check(temp = this.temporary(type, index))) {
index++;
}
this.variables[temp] = 'var';
return temp;

View File

@ -20,11 +20,7 @@ exports.Scope = class Scope
constructor: (parent, expressions, method) ->
[@parent, @expressions, @method] = [parent, expressions, method]
@variables = {}
@tempVars = {}
if @parent
(@tempVars[k] = val) for k, val of @parent.tempVars
else
Scope.root = this
Scope.root = this if not @parent
# Look up a variable name in lexical scope, and declare it if it does not
# already exist.
@ -53,12 +49,13 @@ exports.Scope = class Scope
# Generate a temporary variable name at the given index.
temporary: (type, index) ->
'_' + type + (if (index) > 1 then index else '')
'_' + type + (if index then (index + 1) else '')
# If we need to store an intermediate result, find an available name for a
# compiler-generated variable. `_var`, `_var2`, and so on...
freeVariable: (type) ->
@tempVars[type]++ while @check temp = @temporary type, @tempVars[type] or= 1
index = 0
index++ while @check temp = @temporary type, index
@variables[temp] = 'var'
temp