From 23fb6185962533c5a75bf4b0a40028d4c32aa556 Mon Sep 17 00:00:00 2001 From: Martin Carlberg Date: Mon, 9 Sep 2013 16:38:38 +0200 Subject: [PATCH] Fixed: Compiler now generates correct empty statement after 'if (a);' Earlier the compiler generated the 'if' without the last ';' when a empty statement is used, with sometimes devastating result. The same if an empty statement was after the 'else'. I have also added a test case for some different empty statement senarios. --- Objective-J/ObjJAcornCompiler.js | 7 +++++-- .../OutputTests/Misc/empty-statements.j | 7 +++++++ .../OutputTests/Misc/empty-statements.js | 13 +++++++++++++ .../Preprocessor/OutputTests/OutputTest.j | 1 + 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 Tests/Objective-J/Preprocessor/OutputTests/Misc/empty-statements.j create mode 100644 Tests/Objective-J/Preprocessor/OutputTests/Misc/empty-statements.js diff --git a/Objective-J/ObjJAcornCompiler.js b/Objective-J/ObjJAcornCompiler.js index 9bdad14a3..d53c354be 100644 --- a/Objective-J/ObjJAcornCompiler.js +++ b/Objective-J/ObjJAcornCompiler.js @@ -580,7 +580,8 @@ IfStatement: function(node, st, c) { buffer.concat("if ("); } c(node.test, st, "Expression"); - if (generate) buffer.concat(")\n"); + // We don't want EmptyStatements to generate an extra parenthesis except when it is in a while, for, ... + if (generate) buffer.concat(node.consequent.type === "EmptyStatement" ? ");\n" : ")\n"); indentation += indentStep; c(node.consequent, st, "Statement"); indentation = indentation.substring(indentationSpaces); @@ -588,8 +589,10 @@ IfStatement: function(node, st, c) { if (alternate) { var alternateNotIf = alternate.type !== "IfStatement"; if (generate) { + var emptyStatement = alternate.type === "EmptyStatement"; buffer.concat(indentation); - buffer.concat(alternateNotIf ? "else\n" : "else "); + // We don't want EmptyStatements to generate an extra parenthesis except when it is in a while, for, ... + buffer.concat(alternateNotIf ? emptyStatement ? "else;\n" : "else\n" : "else "); } if (alternateNotIf) indentation += indentStep; diff --git a/Tests/Objective-J/Preprocessor/OutputTests/Misc/empty-statements.j b/Tests/Objective-J/Preprocessor/OutputTests/Misc/empty-statements.j new file mode 100644 index 000000000..983c029a5 --- /dev/null +++ b/Tests/Objective-J/Preprocessor/OutputTests/Misc/empty-statements.j @@ -0,0 +1,7 @@ +function f(x) { + var a = 2, b = 1; + while (a < b); + for (;;); + if (a < b); else; + do {} while (a < b); +} diff --git a/Tests/Objective-J/Preprocessor/OutputTests/Misc/empty-statements.js b/Tests/Objective-J/Preprocessor/OutputTests/Misc/empty-statements.js new file mode 100644 index 000000000..1be2ab550 --- /dev/null +++ b/Tests/Objective-J/Preprocessor/OutputTests/Misc/empty-statements.js @@ -0,0 +1,13 @@ +f = function(x) +{ + var a = 2, + b = 1; + while (a < b); + for (; ; ); + if (a < b); + else; + do + { + } + while (a < b); +} diff --git a/Tests/Objective-J/Preprocessor/OutputTests/OutputTest.j b/Tests/Objective-J/Preprocessor/OutputTests/OutputTest.j index 3083447d2..302ce7823 100644 --- a/Tests/Objective-J/Preprocessor/OutputTests/OutputTest.j +++ b/Tests/Objective-J/Preprocessor/OutputTests/OutputTest.j @@ -18,6 +18,7 @@ var FILENAMES = [ "Misc/preprocess-if-directives", "Misc/regex-simple-char-classes", "Misc/empty-loops", + "Misc/empty-statements", ]; @implementation OutputTest : OJTestCase