diff --git a/lib/scope.js b/lib/scope.js index 27b20219..66af2231 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -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; diff --git a/src/scope.coffee b/src/scope.coffee index 9e8da7e6..46d73c1a 100644 --- a/src/scope.coffee +++ b/src/scope.coffee @@ -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