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 20:59:58 -05:00
parent 460272291f
commit ad9b7d700a
2 changed files with 16 additions and 9 deletions

View file

@ -1796,6 +1796,9 @@
rvar = scope.freeVariable('results'); rvar = scope.freeVariable('results');
} }
ivar = (this.range ? name : index) || scope.freeVariable('i'); ivar = (this.range ? name : index) || scope.freeVariable('i');
if (this.pattern) {
name = ivar;
}
varPart = ''; varPart = '';
guardPart = ''; guardPart = '';
defPart = ''; defPart = '';
@ -1814,7 +1817,9 @@
defPart = "" + this.tab + (ref = scope.freeVariable('ref')) + " = " + svar + ";\n"; defPart = "" + this.tab + (ref = scope.freeVariable('ref')) + " = " + svar + ";\n";
svar = ref; svar = ref;
} }
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(o, LEVEL_TOP) : name ? "" + name + " = " + svar + "[" + ivar + "]" : void 0; if (name && !this.pattern) {
namePart = "" + name + " = " + svar + "[" + ivar + "]";
}
if (!this.object) { if (!this.object) {
lvar = scope.freeVariable('len'); lvar = scope.freeVariable('len');
stepPart = this.step ? "" + ivar + " += " + (this.step.compile(o, LEVEL_OP)) : "" + ivar + "++"; stepPart = this.step ? "" + ivar + " += " + (this.step.compile(o, LEVEL_OP)) : "" + ivar + "++";
@ -1824,9 +1829,7 @@
if (this.scoped) { if (this.scoped) {
body = Closure.wrap(body, true, !this.returns); body = Closure.wrap(body, true, !this.returns);
} }
if (!this.pattern) { defPart += this.pluckDirectCall(o, body, name, index);
defPart += this.pluckDirectCall(o, body, name, index);
}
if (this.returns && !hasPureLast) { if (this.returns && !hasPureLast) {
resultPart = "" + this.tab + rvar + " = [];\n"; resultPart = "" + this.tab + rvar + " = [];\n";
returnResult = '\n' + (new Return(new Literal(rvar)).compile(o, LEVEL_PAREN)); returnResult = '\n' + (new Return(new Literal(rvar)).compile(o, LEVEL_PAREN));
@ -1835,6 +1838,9 @@
if (this.guard) { if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]); body = Expressions.wrap([new If(this.guard, body)]);
} }
if (this.pattern) {
body.expressions.unshift(new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")));
}
if (namePart) { if (namePart) {
varPart = "\n" + idt1 + namePart + ";"; varPart = "\n" + idt1 + namePart + ";";
} }

View file

@ -1451,6 +1451,7 @@ exports.For = class For extends Base
scope.find(index, immediate: yes) if index scope.find(index, immediate: yes) if index
rvar = scope.freeVariable 'results' if @returns and not hasPureLast rvar = scope.freeVariable 'results' if @returns and not hasPureLast
ivar = (if @range then name else index) or scope.freeVariable 'i' ivar = (if @range then name else index) or scope.freeVariable 'i'
name = ivar if @pattern
varPart = '' varPart = ''
guardPart = '' guardPart = ''
defPart = '' defPart = ''
@ -1464,23 +1465,23 @@ exports.For = class For extends Base
if (name or @own) and not IDENTIFIER.test svar if (name or @own) and not IDENTIFIER.test svar
defPart = "#{@tab}#{ref = scope.freeVariable 'ref'} = #{svar};\n" defPart = "#{@tab}#{ref = scope.freeVariable 'ref'} = #{svar};\n"
svar = ref svar = ref
namePart = if @pattern if name and not @pattern
new Assign(@name, new Literal "#{svar}[#{ivar}]").compile o, LEVEL_TOP namePart = "#{name} = #{svar}[#{ivar}]"
else if name
"#{name} = #{svar}[#{ivar}]"
unless @object unless @object
lvar = scope.freeVariable 'len' lvar = scope.freeVariable 'len'
stepPart = if @step then "#{ivar} += #{ @step.compile(o, LEVEL_OP) }" else "#{ivar}++" stepPart = if @step then "#{ivar} += #{ @step.compile(o, LEVEL_OP) }" else "#{ivar}++"
forPart = "#{ivar} = 0, #{lvar} = #{svar}.length; #{ivar} < #{lvar}; #{stepPart}" forPart = "#{ivar} = 0, #{lvar} = #{svar}.length; #{ivar} < #{lvar}; #{stepPart}"
if @scoped if @scoped
body = Closure.wrap body, true, not @returns body = Closure.wrap body, true, not @returns
defPart += @pluckDirectCall o, body, name, index unless @pattern defPart += @pluckDirectCall o, body, name, index
if @returns and not hasPureLast if @returns and not hasPureLast
resultPart = "#{@tab}#{rvar} = [];\n" resultPart = "#{@tab}#{rvar} = [];\n"
returnResult = '\n' + (new Return(new Literal(rvar)).compile o, LEVEL_PAREN) returnResult = '\n' + (new Return(new Literal(rvar)).compile o, LEVEL_PAREN)
body = Push.wrap rvar, body body = Push.wrap rvar, body
if @guard if @guard
body = Expressions.wrap [new If @guard, body] body = Expressions.wrap [new If @guard, body]
if @pattern
body.expressions.unshift new Assign @name, new Literal "#{svar}[#{ivar}]"
varPart = "\n#{idt1}#{namePart};" if namePart varPart = "\n#{idt1}#{namePart};" if namePart
if @object if @object
forPart = "#{ivar} in #{svar}" forPart = "#{ivar} in #{svar}"