Remove non-valued return

(cherry picked from commit 42e515f43523ffbfe0b28c2f3f5c342e4d4b1c1d)
Dieser Commit ist enthalten in:
Octavia Togami 2020-02-25 16:13:44 -08:00 committet von MattBDev
Ursprung 8c385b0593
Commit 6d9f30e6a6
4 geänderte Dateien mit 31 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -114,7 +114,7 @@ breakStatement : BREAK ;
continueStatement : CONTINUE ;
returnStatement : RETURN value=expression? ;
returnStatement : RETURN value=expression ;
switchStatement : SWITCH '(' target=expression ')' '{' (labels+=switchLabel ':' bodies+=statements )+ '}' ;

Datei anzeigen

@ -34,6 +34,8 @@ import java.time.Instant;
import java.util.List;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Compiles and evaluates expressions.
*
@ -119,7 +121,9 @@ public class Expression {
Instant deadline = Instant.now().plusMillis(timeout);
// evaluation exceptions are thrown out of this method
return compiledExpression.execute(new ExecutionData(slots, functions, deadline));
Double result = compiledExpression.execute(new ExecutionData(slots, functions, deadline));
checkNotNull(result, "Expression must result in a value");
return result;
}
public void optimize() {

Datei anzeigen

@ -227,19 +227,12 @@ class CompilingVisitor extends ExpressionBaseVisitor<MethodHandle> {
@Override
public MethodHandle visitReturnStatement(ExpressionParser.ReturnStatementContext ctx) {
// MH:returnValue = (ExecutionData)Double
MethodHandle returnValue;
if (ctx.value != null) {
returnValue = evaluate(ctx.value).handle;
} else {
returnValue = defaultResult();
}
return MethodHandles.filterArguments(
// take the (Double)Double return statement
RETURN_STATEMENT_BASE,
0,
// map the Double back to ExecutionData via the returnValue
returnValue
evaluate(ctx.value).handle
);
}

Datei anzeigen

@ -103,6 +103,12 @@ class ExpressionTest extends BaseExpressionTest {
() -> compile("x()"));
assertEquals(0, e.getPosition(), "Error position");
}
{
// verify that you must return a value
ExpressionException e = assertThrows(ExpressionException.class,
() -> compile("return"));
assertEquals(6, e.getPosition(), "Error position");
}
assertThrows(ExpressionException.class,
() -> compile("("));
assertThrows(ExpressionException.class,
@ -156,6 +162,24 @@ class ExpressionTest extends BaseExpressionTest {
public void testWhile() throws ExpressionException {
checkTestCase("c=5; a=0; while (c > 0) { ++a; --c; } a", 5);
checkTestCase("c=5; a=0; do { ++a; --c; } while (c > 0); a", 5);
checkTestCase("" +
"c=5;" +
"a=0;" +
"while (c > 0) {" +
" ++a;" +
" --c;" +
" if (c == 1) break;" +
"}" +
"a", 4);
checkTestCase("" +
"c=5;" +
"a=0;" +
"while (c > 0) {" +
" ++a;" +
" if (a < 5) continue;" +
" --c;" +
"}" +
"a", 9);
}
@Test