adding slightly optimized syntax for range comprehensions that could care less about the index variable. Issue #547

This commit is contained in:
Jeremy Ashkenas 2010-07-27 22:38:38 -04:00
parent 8d544ce80b
commit 051a863ff5
6 changed files with 140 additions and 118 deletions

View File

@ -389,15 +389,24 @@
}) })
], ],
For: [ For: [
o("Statement ForStart ForSource", function() { o("Statement ForBody", function() {
$3.raw = $2.raw; return new ForNode($1, $2, $2.vars[0], $2.vars[1]);
return new ForNode($1, $3, $2[0], $2[1]); }), o("Expression ForBody", function() {
}), o("Expression ForStart ForSource", function() { return new ForNode($1, $2, $2.vars[0], $2.vars[1]);
$3.raw = $2.raw; }), o("ForBody Block", function() {
return new ForNode($1, $3, $2[0], $2[1]); return new ForNode($2, $1, $1.vars[0], $1.vars[1]);
}), o("ForStart ForSource Block", function() { })
],
ForBody: [
o("FOR Range", function() {
return {
source: new ValueNode($2),
vars: []
};
}), o("ForStart ForSource", function() {
$2.raw = $1.raw; $2.raw = $1.raw;
return new ForNode($3, $2, $1[0], $1[1]); $2.vars = $1;
return $2;
}) })
], ],
ForStart: [ ForStart: [

View File

@ -1435,7 +1435,7 @@
return n instanceof CodeNode; return n instanceof CodeNode;
}); });
scope = o.scope; scope = o.scope;
name = this.name && this.name.compile(o); name = (this.name && this.name.compile(o)) || scope.freeVariable();
index = this.index && this.index.compile(o); index = this.index && this.index.compile(o);
if (name && !this.pattern && !codeInBody) { if (name && !this.pattern && !codeInBody) {
scope.find(name); scope.find(name);

File diff suppressed because one or more lines are too long

View File

@ -425,9 +425,14 @@ grammar = {
# Comprehensions can either be normal, with a block of expressions to execute, # Comprehensions can either be normal, with a block of expressions to execute,
# or postfix, with a single expression. # or postfix, with a single expression.
For: [ For: [
o "Statement ForStart ForSource", -> $3.raw = $2.raw; new ForNode $1, $3, $2[0], $2[1] o "Statement ForBody", -> new ForNode $1, $2, $2.vars[0], $2.vars[1]
o "Expression ForStart ForSource", -> $3.raw = $2.raw; new ForNode $1, $3, $2[0], $2[1] o "Expression ForBody", -> new ForNode $1, $2, $2.vars[0], $2.vars[1]
o "ForStart ForSource Block", -> $2.raw = $1.raw; new ForNode $3, $2, $1[0], $1[1] o "ForBody Block", -> new ForNode $2, $1, $1.vars[0], $1.vars[1]
]
ForBody: [
o "FOR Range", -> {source: new ValueNode($2), vars: []}
o "ForStart ForSource", -> $2.raw = $1.raw; $2.vars = $1; $2
] ]
ForStart: [ ForStart: [

View File

@ -1279,7 +1279,7 @@ exports.ForNode = class ForNode extends BaseNode
source = if range then @source.base else @source source = if range then @source.base else @source
codeInBody = @body.contains (n) -> n instanceof CodeNode codeInBody = @body.contains (n) -> n instanceof CodeNode
scope = o.scope scope = o.scope
name = @name and @name.compile o name = (@name and @name.compile(o)) or scope.freeVariable()
index = @index and @index.compile o index = @index and @index.compile o
scope.find name if name and not @pattern and not codeInBody scope.find name if name and not @pattern and not codeInBody
scope.find index if index scope.find index if index

View File

@ -126,3 +126,9 @@ all = value for all key, value of whiskers
ok own.join(' ') is 'Whiskers' ok own.join(' ') is 'Whiskers'
ok all.sort().join(' ') is 'Whiskers cream tabby' ok all.sort().join(' ') is 'Whiskers cream tabby'
# Optimized range comprehensions.
exxes = 'x' for [0...10]
ok exxes.join(' ') is 'x x x x x x x x x x'