Skip to content

Commit

Permalink
R2js: fix a bug on 'else if' without brackets.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas CHABALIER committed Mar 27, 2024
1 parent e4e1e0b commit 8fb8a73
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/main/java/org/math/R/AbstractR2jsSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -1227,21 +1227,29 @@ private static String addIfElseBrackets(String expr) {
ifSb.append(ifArg);
ifSb.append(")");

//if (!result.substring(endIndex + 1).trim().startsWith("{") && expr.indexOf("else", endIndex) >= 0) {


int elseIndex = getNextElseIndex(result, endIndex);

boolean dontCloseBrack = false;

if (elseIndex > 0) {
String ifStatement = result.substring(endIndex + 1, elseIndex).trim();
boolean addIfBrackets = !ifStatement.startsWith("{");
if(addIfBrackets) ifSb.append("{");
ifSb.append(ifStatement);
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 @@ -1260,8 +1268,7 @@ private static String addIfElseBrackets(String expr) {

StringBuilder sb = new StringBuilder();
sb.append(result.substring(0, startIndex));
sb.append(ifSb.toString());
//if(!dontCloseBrack && !ifSb.toString().trim().endsWith("}")) sb.append("}");
sb.append(ifSb);
result = sb.toString();

// Search the next "if"
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/math/R/R2jsSessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,15 @@ public void testFunctions() throws RException {
//assert Double.parseDouble( js.eval("fahrenheit_to_celsius(32.0);\n").toString()) == 0;
//assert Double.parseDouble( js.eval("kelvin_to_celsius(fahrenheit_to_kelvin(32.0))").toString()) == 32;
//assert Double.parseDouble( js.eval("kelvin_to_celsius(0)").toString()) == -273.15;

engine.eval("f12 <- function() { if (TRUE) {\n" +
" d <- 1+1\n" +
" } else if (!TRUE) {\n" +
" d <- 2+2\n" +
" } " +
"return d}");
assert (Double) engine.eval("f12()") == 2;

engine.eval("f <- function(x,bool1=TRUE,bool2=TRUE) {\n if (!bool1) {\n a <- 1; b <- 2\n } else {\n a <- 3; b <- 4\n }\n if (bool2) {\n c<-a*1000 + b*100\n } else if (!bool2) {\n c<--a*1000 - b*100\n }\n result <- c + x\n return(result)\n }");
assert (Double) engine.eval("f(1, TRUE, TRUE)") == 3401;
assert (Double) engine.eval("f(3)") == 3403;
Expand Down

0 comments on commit 8fb8a73

Please sign in to comment.