Skip to content

Commit

Permalink
R2js: Fix another if/else brackets case.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas CHABALIER committed Mar 28, 2024
1 parent 8fb8a73 commit 72e9ee5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/main/java/org/math/R/AbstractR2jsSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -1237,19 +1237,7 @@ private static String addIfElseBrackets(String expr) {
if(addIfBrackets) ifSb.append("}");
ifSb.append(" else ");

// If this is a "else if", "else" stop when the next "if" stop
// Otherwise "else" stop at the next "}"
int stopElseStatement = getNextExpressionLastIndex(result, elseIndex, "}");
int elseIfIdx = result.indexOf("else if", elseIndex);
if(elseIndex == elseIfIdx) {
int nextIdOpenBracket = result.indexOf("{", elseIndex);
int nextCloseBracket = result.indexOf("}", elseIndex);
if(nextCloseBracket<nextIdOpenBracket) {
stopElseStatement = nextCloseBracket;
} else {
stopElseStatement = getNextExpressionLastIndex(result, nextIdOpenBracket + 1, "}");
}
}
String elseStatement = result.substring(elseIndex + 4, stopElseStatement+1).trim();
boolean addElseBrackets = !elseStatement.startsWith("{");
if(addElseBrackets) ifSb.append("{");
Expand All @@ -1259,6 +1247,13 @@ private static String addIfElseBrackets(String expr) {
} else {
int stopIdx = getNextExpressionLastIndex(result, endIndex, "}")+1;
String ifStatement = result.substring(endIndex + 1, stopIdx).trim();
if(!ifStatement.startsWith("{")) {
int endOfLine = getNextExpressionLastIndex(result, endIndex, "\n")+1;
if(endOfLine<stopIdx) {
stopIdx = endOfLine;
ifStatement = result.substring(endIndex + 1, stopIdx).trim();
}
}
boolean addIfBrackets = !ifStatement.startsWith("{");
if(addIfBrackets) ifSb.append("{");
ifSb.append(ifStatement);
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/math/R/R2jsSessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,7 @@ public void testSetWDFunctions() throws RException {
@Test
public void testIfFunction() throws RException, ScriptException {
engine.debug_js = true;

engine.voidEval("f1 <- function() { return 4}");
assertEquals((Double) engine.eval("if(f1() == 2) f1() else f1()+1"), 5, epsilon);
assertEquals((Double) engine.eval("if(f1() >2) { 3 } else { 4 }"), 3, epsilon);
Expand All @@ -1418,6 +1419,13 @@ public void testIfFunction() throws RException, ScriptException {

engine.eval("f8 <- function() {if(TRUE) if(FALSE) 34 else if(TRUE) 1 else 2}");
assertEquals(1,(Double) engine.eval( "f8()"), epsilon);

engine.eval("f9 <- function() {if(FALSE) 12 else if(FALSE) 13 else if(TRUE) 14}");
assertEquals(14,(Double) engine.eval( "f9()"), epsilon);

engine.eval("f10 <- function() {if(FALSE) { 12 } else if(FALSE) { 13 } else if(TRUE) { 14 }}");
assertEquals(14,(Double) engine.eval( "f10()"), epsilon);

}

@Test
Expand Down

0 comments on commit 72e9ee5

Please sign in to comment.