1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Fiddling with For#compileNode.

This commit is contained in:
Jeremy Ashkenas 2010-12-21 21:03:52 -05:00
parent ad9b7d700a
commit 4375a03f38
3 changed files with 17 additions and 7 deletions

View file

@ -1826,10 +1826,6 @@
forPart = "" + ivar + " = 0, " + lvar + " = " + svar + ".length; " + ivar + " < " + lvar + "; " + stepPart;
}
}
if (this.scoped) {
body = Closure.wrap(body, true, !this.returns);
}
defPart += this.pluckDirectCall(o, body, name, index);
if (this.returns && !hasPureLast) {
resultPart = "" + this.tab + rvar + " = [];\n";
returnResult = '\n' + (new Return(new Literal(rvar)).compile(o, LEVEL_PAREN));
@ -1841,6 +1837,10 @@
if (this.pattern) {
body.expressions.unshift(new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")));
}
if (this.scoped) {
body = Closure.wrap(body, true, !this.returns);
}
defPart += this.pluckDirectCall(o, body, name, index);
if (namePart) {
varPart = "\n" + idt1 + namePart + ";";
}

View file

@ -1471,9 +1471,6 @@ exports.For = class For extends Base
lvar = scope.freeVariable 'len'
stepPart = if @step then "#{ivar} += #{ @step.compile(o, LEVEL_OP) }" else "#{ivar}++"
forPart = "#{ivar} = 0, #{lvar} = #{svar}.length; #{ivar} < #{lvar}; #{stepPart}"
if @scoped
body = Closure.wrap body, true, not @returns
defPart += @pluckDirectCall o, body, name, index
if @returns and not hasPureLast
resultPart = "#{@tab}#{rvar} = [];\n"
returnResult = '\n' + (new Return(new Literal(rvar)).compile o, LEVEL_PAREN)
@ -1482,6 +1479,9 @@ exports.For = class For extends Base
body = Expressions.wrap [new If @guard, body]
if @pattern
body.expressions.unshift new Assign @name, new Literal "#{svar}[#{ivar}]"
if @scoped
body = Closure.wrap body, true, not @returns
defPart += @pluckDirectCall o, body, name, index
varPart = "\n#{idt1}#{namePart};" if namePart
if @object
forPart = "#{ivar} in #{svar}"

View file

@ -146,6 +146,16 @@ foo = ->
eq foo()[3][4](), 7
# Scoped loop pattern matching.
a = [[0], [1]]
funcs = []
for [v] in a -> funcs.push -> v
eq funcs[0](), 0
eq funcs[1](), 1
# Nested comprehensions.
multiLiner =
for x in [3..5]