mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Added generator for custom free variable types, e.g., '_cache',
'_cache2', and so on.
This commit is contained in:
parent
1f2c6c77fd
commit
fe68261bc2
2 changed files with 31 additions and 13 deletions
25
lib/scope.js
25
lib/scope.js
|
@ -58,14 +58,25 @@
|
|||
}
|
||||
return !!(this.parent && this.parent.check(name));
|
||||
};
|
||||
Scope.prototype.freeVariable = function() {
|
||||
var ordinal;
|
||||
while (this.check(this.tempVars.general)) {
|
||||
ordinal = 1 + parseInt(this.tempVars.general.substr(1), 36);
|
||||
this.tempVars.general = '_' + ordinal.toString(36).replace(/\d/g, 'a');
|
||||
Scope.prototype.freeVariable = function(type) {
|
||||
var next;
|
||||
if (type) {
|
||||
next = function(prev) {
|
||||
return '_' + type + ((prev && Number(prev.match(/\d+$/) || 1) + 1) || '');
|
||||
};
|
||||
} else {
|
||||
type = 'general';
|
||||
next = function(prev) {
|
||||
var ordinal;
|
||||
ordinal = 1 + parseInt(prev.substr(1), 36);
|
||||
return '_' + ordinal.toString(36).replace(/\d/g, 'a');
|
||||
};
|
||||
}
|
||||
this.variables[this.tempVars.general] = 'var';
|
||||
return this.tempVars.general;
|
||||
while (this.check(this.tempVars[type] || (this.tempVars[type] = next()))) {
|
||||
this.tempVars[type] = next(this.tempVars[type]);
|
||||
}
|
||||
this.variables[this.tempVars[type]] = 'var';
|
||||
return this.tempVars[type];
|
||||
};
|
||||
Scope.prototype.assign = function(name, value) {
|
||||
return (this.variables[name] = {
|
||||
|
|
|
@ -54,12 +54,19 @@ exports.Scope = class Scope
|
|||
|
||||
# If we need to store an intermediate result, find an available name for a
|
||||
# compiler-generated variable. `_a`, `_b`, and so on...
|
||||
freeVariable: ->
|
||||
while @check @tempVars.general
|
||||
ordinal = 1 + parseInt @tempVars.general.substr(1), 36
|
||||
@tempVars.general = '_' + ordinal.toString(36).replace(/\d/g, 'a')
|
||||
@variables[@tempVars.general] = 'var'
|
||||
@tempVars.general
|
||||
freeVariable: (type) ->
|
||||
if type
|
||||
next = (prev) ->
|
||||
'_' + type + ((prev and Number(prev.match(/\d+$/) or 1) + 1) or '')
|
||||
else
|
||||
type = 'general'
|
||||
next = (prev) ->
|
||||
ordinal = 1 + parseInt prev.substr(1), 36
|
||||
'_' + ordinal.toString(36).replace /\d/g, 'a'
|
||||
while @check @tempVars[type] or= next()
|
||||
@tempVars[type] = next @tempVars[type]
|
||||
@variables[@tempVars[type]] = 'var'
|
||||
@tempVars[type]
|
||||
|
||||
# Ensure that an assignment is made at the top of this scope
|
||||
# (or at the top-level scope, if requested).
|
||||
|
|
Loading…
Reference in a new issue