removed extra lines from trailing `then` compilations

This commit is contained in:
satyr 2010-10-25 01:54:36 +09:00
parent 26a115adcf
commit d60aa9a80c
3 changed files with 68 additions and 54 deletions

View File

@ -187,8 +187,8 @@
Expressions.prototype.unwrap = function() {
return this.expressions.length === 1 ? this.expressions[0] : this;
};
Expressions.prototype.empty = function() {
return this.expressions.length === 0;
Expressions.prototype.isEmpty = function() {
return !this.expressions.length;
};
Expressions.prototype.makeReturn = function() {
var _ref2, end, idx;
@ -845,7 +845,7 @@
constScope || (constScope = new Scope(o.scope, constructor.body, constructor));
me || (me = constScope.freeVariable('this'));
pname = pvar.compile(o);
if (constructor.body.empty()) {
if (constructor.body.isEmpty()) {
constructor.body.push(new Return(new Literal('this')));
}
constructor.body.unshift(new Literal("this." + pname + " = function(){ return " + className + ".prototype." + pname + ".apply(" + me + ", arguments); }"));
@ -867,7 +867,7 @@
if (extension) {
construct += '\n' + this.tab + extension.compile(o) + ';';
}
if (!props.empty()) {
if (!props.isEmpty()) {
construct += '\n' + props.compile(o);
}
if (this.returns) {
@ -1235,17 +1235,22 @@
o.indent = this.idt(1);
set = '';
body = this.body;
if (o.level > LEVEL_TOP || this.returns) {
rvar = o.scope.freeVariable('result');
set = "" + this.tab + rvar + " = [];\n";
if (body) {
body = Push.wrap(rvar, body);
if (body.isEmpty()) {
body = '';
} else {
if (o.level > LEVEL_TOP || this.returns) {
rvar = o.scope.freeVariable('result');
set = "" + this.tab + rvar + " = [];\n";
if (body) {
body = Push.wrap(rvar, body);
}
}
if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]);
}
body = "\n" + (body.compile(o, LEVEL_TOP)) + "\n" + this.tab;
}
if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]);
}
code = set + this.tab + ("while (" + (this.condition.compile(o, LEVEL_PAREN)) + ") {\n" + (body.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}");
code = set + this.tab + ("while (" + (this.condition.compile(o, LEVEL_PAREN)) + ") {" + body + "}");
if (this.returns) {
o.indent = this.tab;
code += '\n' + new Return(new Literal(rvar)).compile(o);
@ -1545,7 +1550,7 @@
return '';
};
For.prototype.compileNode = function(o) {
var _ref2, _ref3, _ref4, _ref5, _ref6, body, cond, defPart, forPart, guardPart, idt, index, ivar, lvar, name, namePart, pvar, retPart, rvar, scope, sourcePart, step, svar, tail, tvar, varPart, vars;
var _ref2, _ref3, _ref4, _ref5, _ref6, body, code, cond, defPart, forPart, guardPart, idt, index, ivar, lvar, name, namePart, pvar, retPart, rvar, scope, sourcePart, step, svar, tail, tvar, varPart, vars;
scope = o.scope;
name = !this.pattern && ((_ref2 = this.name) != null ? _ref2.compile(o) : undefined);
index = (_ref3 = this.index) != null ? _ref3.compile(o) : undefined;
@ -1612,20 +1617,27 @@
}
})();
}
if (o.level > LEVEL_TOP || this.returns) {
rvar = scope.freeVariable('result');
defPart += this.tab + rvar + ' = [];\n';
retPart = this.compileReturnValue(rvar, o);
body = Push.wrap(rvar, body);
}
if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]);
}
if (namePart) {
varPart = idt + namePart + ';\n';
}
o.indent = idt;
return defPart + ("" + this.tab + "for (" + forPart + ") {\n" + (guardPart || '') + varPart + (body.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}") + retPart;
code = guardPart + varPart;
if (!body.isEmpty()) {
if (o.level > LEVEL_TOP || this.returns) {
rvar = scope.freeVariable('result');
defPart += this.tab + rvar + ' = [];\n';
retPart = this.compileReturnValue(rvar, o);
body = Push.wrap(rvar, body);
}
if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]);
}
o.indent = idt;
code += body.compile(o, LEVEL_TOP);
}
if (code) {
code = '\n' + code + '\n' + this.tab;
}
return defPart + this.tab + ("for (" + forPart + ") {" + code + "}") + retPart;
};
return For;
})();
@ -1782,7 +1794,7 @@
}).call(this);
Push = {
wrap: function(name, expressions) {
if (expressions.empty() || last(expressions.expressions).containsPureStatement()) {
if (expressions.isEmpty() || last(expressions.expressions).containsPureStatement()) {
return expressions;
}
return expressions.push(new Call(new Value(new Literal(name), [new Accessor(new Literal('push'))]), [expressions.pop()]));

View File

@ -185,8 +185,7 @@ exports.Expressions = class Expressions extends Base
if @expressions.length is 1 then @expressions[0] else this
# Is this an empty block of code?
empty: ->
@expressions.length is 0
isEmpty: -> not @expressions.length
# An Expressions node does not return its entire body, rather it
# ensures that the final expression is returned.
@ -720,7 +719,7 @@ exports.Class = class Class extends Base
constScope or= new Scope o.scope, constructor.body, constructor
me or= constScope.freeVariable 'this'
pname = pvar.compile o
constructor.body.push new Return new Literal 'this' if constructor.body.empty()
constructor.body.push new Return new Literal 'this' if constructor.body.isEmpty()
constructor.body.unshift new Literal "this.#{pname} = function(){ return #{className}.prototype.#{pname}.apply(#{me}, arguments); }"
if pvar
access = if prop.context is 'this' then pvar.base.properties[0] else new Accessor(pvar, 'prototype')
@ -733,7 +732,7 @@ exports.Class = class Class extends Base
o.sharedScope = constScope
construct = @tab + new Assign(variable, constructor).compile(o) + ';'
construct += '\n' + @tab + extension.compile(o) + ';' if extension
construct += '\n' + props.compile o if !props.empty()
construct += '\n' + props.compile o if !props.isEmpty()
construct += '\n' + new Return(variable).compile o if @returns
construct
@ -1049,16 +1048,16 @@ exports.While = class While extends Base
o.indent = @idt 1
set = ''
{body} = this
if o.level > LEVEL_TOP or @returns
rvar = o.scope.freeVariable 'result'
set = "#{@tab}#{rvar} = [];\n"
body = Push.wrap rvar, body if body
body = Expressions.wrap [new If @guard, body] if @guard
code = set + @tab + """
while (#{ @condition.compile o, LEVEL_PAREN }) {
#{ body.compile o, LEVEL_TOP }
#{@tab}}
"""
if body.isEmpty()
body = ''
else
if o.level > LEVEL_TOP or @returns
rvar = o.scope.freeVariable 'result'
set = "#{@tab}#{rvar} = [];\n"
body = Push.wrap rvar, body if body
body = Expressions.wrap [new If @guard, body] if @guard
body = "\n#{ body.compile o, LEVEL_TOP }\n#{@tab}"
code = set + @tab + "while (#{ @condition.compile o, LEVEL_PAREN }) {#{body}}"
if @returns
o.indent = @tab
code += '\n' + new Return(new Literal rvar).compile o
@ -1363,19 +1362,19 @@ exports.For = class For extends Base
when 1 then '++'
when -1 then '--'
else (if pvar < 0 then ' -= ' + pvar.slice 1 else ' += ' + pvar)
if o.level > LEVEL_TOP or @returns
rvar = scope.freeVariable 'result'
defPart += @tab + rvar + ' = [];\n'
retPart = @compileReturnValue rvar, o
body = Push.wrap rvar, body
body = Expressions.wrap [new If @guard, body] if @guard
varPart = idt + namePart + ';\n' if namePart
o.indent = idt
defPart + """
#{@tab}for (#{forPart}) {
#{ guardPart or '' }#{varPart}#{ body.compile o, LEVEL_TOP }
#{@tab}}
""" + retPart
varPart = idt + namePart + ';\n' if namePart
code = guardPart + varPart
unless body.isEmpty()
if o.level > LEVEL_TOP or @returns
rvar = scope.freeVariable 'result'
defPart += @tab + rvar + ' = [];\n'
retPart = @compileReturnValue rvar, o
body = Push.wrap rvar, body
body = Expressions.wrap [new If @guard, body] if @guard
o.indent = idt
code += body.compile o, LEVEL_TOP
code = '\n' + code + '\n' + @tab if code
defPart + @tab + "for (#{forPart}) {#{code}}" + retPart
#### Switch
@ -1500,7 +1499,7 @@ exports.If = class If extends Base
# which is helpful for recording the result arrays from comprehensions.
Push =
wrap: (name, expressions) ->
return expressions if expressions.empty() or
return expressions if expressions.isEmpty() or
last(expressions.expressions).containsPureStatement()
expressions.push new Call(new Value new Literal(name), [new Accessor new Literal 'push']
[expressions.pop()])

View File

@ -9,3 +9,6 @@ ok 'passed' is CoffeeScript.eval '"passed"', bare: on, fileName: 'test'
#750
try ok not CoffeeScript.nodes 'f(->'
catch e then eq e.message, 'unclosed CALL_START on line 1'
eq CoffeeScript.compile('for all k of o then', bare: on, globals: on),
'for (k in o) {}'