mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
Part of previous commit: updating how temporary variables are generated.
We no longer need to store the name of the last generated variable, instead we store the index.
This commit is contained in:
13
lib/scope.js
13
lib/scope.js
@@ -57,15 +57,12 @@
|
||||
return !!(this.parent && this.parent.check(name));
|
||||
};
|
||||
Scope.prototype.freeVariable = function(type) {
|
||||
var next;
|
||||
next = function(prev) {
|
||||
return '_' + type + ((prev && Number(prev.match(/\d+$/) || 1) + 1) || '');
|
||||
};
|
||||
while (this.check(this.tempVars[type] || (this.tempVars[type] = next()))) {
|
||||
this.tempVars[type] = next(this.tempVars[type]);
|
||||
var temp;
|
||||
while (this.check(temp = '_' + type + ((this.tempVars[type] || (this.tempVars[type] = 1)) > 1 ? this.tempVars[type] : ''))) {
|
||||
this.tempVars[type]++;
|
||||
}
|
||||
this.variables[this.tempVars[type]] = 'var';
|
||||
return this.tempVars[type];
|
||||
this.variables[temp] = 'var';
|
||||
return temp;
|
||||
};
|
||||
Scope.prototype.assign = function(name, value) {
|
||||
return (this.variables[name] = {
|
||||
|
||||
@@ -54,12 +54,9 @@ exports.Scope = class Scope
|
||||
# If we need to store an intermediate result, find an available name for a
|
||||
# compiler-generated variable. `_var`, `_var2`, and so on...
|
||||
freeVariable: (type) ->
|
||||
next = (prev) ->
|
||||
'_' + type + ((prev and Number(prev.match(/\d+$/) or 1) + 1) or '')
|
||||
while @check @tempVars[type] or= next()
|
||||
@tempVars[type] = next @tempVars[type]
|
||||
@variables[@tempVars[type]] = 'var'
|
||||
@tempVars[type]
|
||||
@tempVars[type]++ while @check temp = '_' + type + (if (@tempVars[type] or= 1) > 1 then @tempVars[type] else '')
|
||||
@variables[temp] = 'var'
|
||||
temp
|
||||
|
||||
# Ensure that an assignment is made at the top of this scope
|
||||
# (or at the top-level scope, if requested).
|
||||
|
||||
Reference in New Issue
Block a user