coco fd028a0: closes #13; leading comments/literals are now placed before variable declarations

This commit is contained in:
Jeremy Ashkenas
2010-11-16 00:11:52 -05:00
parent ee6f24b48a
commit 0c11267045
2 changed files with 34 additions and 12 deletions

View File

@@ -257,17 +257,31 @@
}
};
Expressions.prototype.compileWithDeclarations = function(o) {
var code, scope;
var code, exp, i, post, rest, scope, _len, _ref;
code = post = '';
_ref = this.expressions;
for (i = 0, _len = _ref.length; i < _len; i++) {
exp = _ref[i];
exp = exp.unwrap();
if (!(exp instanceof Comment || exp instanceof Literal)) {
break;
}
}
o.level = LEVEL_TOP;
code = this.compileNode(o);
if (i) {
rest = this.expressions.splice(i, this.expressions.length);
code = this.compileNode(o);
this.expressions = rest;
}
post = this.compileNode(o);
scope = o.scope;
if (scope.hasAssignments(this)) {
code = "" + this.tab + "var " + (multident(scope.compiledAssignments(), this.tab)) + ";\n" + code;
}
if (!o.globals && o.scope.hasDeclarations(this)) {
code = "" + this.tab + "var " + (scope.compiledDeclarations()) + ";\n" + code;
code += "" + this.tab + "var " + (scope.compiledDeclarations()) + ";\n";
}
return code;
if (scope.hasAssignments(this)) {
code += "" + this.tab + "var " + (multident(scope.compiledAssignments(), this.tab)) + ";\n";
}
return code + post;
};
Expressions.wrap = function(nodes) {
if (nodes.length === 1 && nodes[0] instanceof Expressions) {

View File

@@ -230,14 +230,22 @@ exports.Expressions = class Expressions extends Base
# Compile the expressions body for the contents of a function, with
# declarations of all inner variables pushed up to the top.
compileWithDeclarations: (o) ->
code = post = ''
for exp, i in @expressions
exp = exp.unwrap()
break unless exp instanceof Comment or exp instanceof Literal
o.level = LEVEL_TOP
code = @compileNode o
if i
rest = @expressions.splice i, @expressions.length
code = @compileNode o
@expressions = rest
post = @compileNode o
{scope} = o
if scope.hasAssignments this
code = "#{@tab}var #{ multident scope.compiledAssignments(), @tab };\n#{code}"
if not o.globals and o.scope.hasDeclarations this
code = "#{@tab}var #{ scope.compiledDeclarations() };\n#{code}"
code
code += "#{@tab}var #{ scope.compiledDeclarations() };\n"
if scope.hasAssignments this
code += "#{@tab}var #{ multident scope.compiledAssignments(), @tab };\n"
code + post
# Wrap up the given nodes as an **Expressions**, unless it already happens
# to be one.