Don't store `tempVars` as we can get the next available one from scope.
This commit is contained in:
parent
ed74f42323
commit
3ab0c12bf1
21
lib/scope.js
21
lib/scope.js
|
@ -6,21 +6,13 @@
|
||||||
}
|
}
|
||||||
exports.Scope = (function() {
|
exports.Scope = (function() {
|
||||||
Scope = function(parent, expressions, method) {
|
Scope = function(parent, expressions, method) {
|
||||||
var _cache, _cache2, k, val;
|
var _cache;
|
||||||
_cache = [parent, expressions, method];
|
_cache = [parent, expressions, method];
|
||||||
this.parent = _cache[0];
|
this.parent = _cache[0];
|
||||||
this.expressions = _cache[1];
|
this.expressions = _cache[1];
|
||||||
this.method = _cache[2];
|
this.method = _cache[2];
|
||||||
this.variables = {};
|
this.variables = {};
|
||||||
this.tempVars = {};
|
if (!this.parent) {
|
||||||
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 {
|
|
||||||
Scope.root = this;
|
Scope.root = this;
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
|
@ -57,12 +49,13 @@
|
||||||
return !!(this.parent && this.parent.check(name));
|
return !!(this.parent && this.parent.check(name));
|
||||||
};
|
};
|
||||||
Scope.prototype.temporary = function(type, index) {
|
Scope.prototype.temporary = function(type, index) {
|
||||||
return '_' + type + ((index) > 1 ? index : '');
|
return '_' + type + (index ? (index + 1) : '');
|
||||||
};
|
};
|
||||||
Scope.prototype.freeVariable = function(type) {
|
Scope.prototype.freeVariable = function(type) {
|
||||||
var temp;
|
var index, temp;
|
||||||
while (this.check(temp = this.temporary(type, this.tempVars[type] || (this.tempVars[type] = 1)))) {
|
index = 0;
|
||||||
this.tempVars[type]++;
|
while (this.check(temp = this.temporary(type, index))) {
|
||||||
|
index++;
|
||||||
}
|
}
|
||||||
this.variables[temp] = 'var';
|
this.variables[temp] = 'var';
|
||||||
return temp;
|
return temp;
|
||||||
|
|
|
@ -20,11 +20,7 @@ exports.Scope = class Scope
|
||||||
constructor: (parent, expressions, method) ->
|
constructor: (parent, expressions, method) ->
|
||||||
[@parent, @expressions, @method] = [parent, expressions, method]
|
[@parent, @expressions, @method] = [parent, expressions, method]
|
||||||
@variables = {}
|
@variables = {}
|
||||||
@tempVars = {}
|
Scope.root = this if not @parent
|
||||||
if @parent
|
|
||||||
(@tempVars[k] = val) for k, val of @parent.tempVars
|
|
||||||
else
|
|
||||||
Scope.root = this
|
|
||||||
|
|
||||||
# Look up a variable name in lexical scope, and declare it if it does not
|
# Look up a variable name in lexical scope, and declare it if it does not
|
||||||
# already exist.
|
# already exist.
|
||||||
|
@ -53,12 +49,13 @@ exports.Scope = class Scope
|
||||||
|
|
||||||
# Generate a temporary variable name at the given index.
|
# Generate a temporary variable name at the given index.
|
||||||
temporary: (type, 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
|
# If we need to store an intermediate result, find an available name for a
|
||||||
# compiler-generated variable. `_var`, `_var2`, and so on...
|
# compiler-generated variable. `_var`, `_var2`, and so on...
|
||||||
freeVariable: (type) ->
|
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'
|
@variables[temp] = 'var'
|
||||||
temp
|
temp
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue