Fix single expressions not converting to blocks. Fixes #123

This commit is contained in:
Daniel Holle 2024-03-04 11:30:36 +01:00
parent f11d4b0716
commit 59585296b0
5 changed files with 30 additions and 4 deletions

View File

@ -0,0 +1,13 @@
import java.lang.Boolean;
import java.lang.Integer;
class Bug123 {
Boolean works(){
if(true) return true;
else return false;
}
void fails(){
Boolean a = true;
if(true) a = false;
}
}

View File

@ -633,7 +633,6 @@ public class StatementGenerator {
/* /*
*************** + Expression Conversions: *************** + Expression Conversions:
*/ */
private Expression convert(Java17Parser.ExpressionContext expression) { private Expression convert(Java17Parser.ExpressionContext expression) {
switch (expression) { switch (expression) {
case PrimaryExpression2Context primary: case PrimaryExpression2Context primary:

View File

@ -380,6 +380,13 @@ public class ASTToTargetAST {
return new TargetBlock(block.statements.stream().map(this::convert).toList()); return new TargetBlock(block.statements.stream().map(this::convert).toList());
} }
protected TargetBlock convertWrapInBlock(Expression expression) {
var res = convert(expression);
if (!(res instanceof TargetBlock))
return new TargetBlock(List.of(res));
return (TargetBlock) res;
}
protected TargetExpression convert(Expression expr) { protected TargetExpression convert(Expression expr) {
var converter = new StatementToTargetExpression(this); var converter = new StatementToTargetExpression(this);
expr.accept(converter); expr.accept(converter);

View File

@ -145,18 +145,18 @@ public class StatementToTargetExpression implements ASTVisitor {
forStmt.initializer.stream().map(converter::convert).toList(), forStmt.initializer.stream().map(converter::convert).toList(),
forStmt.condition != null ? converter.convert(forStmt.condition) : null, forStmt.condition != null ? converter.convert(forStmt.condition) : null,
forStmt.loopExpr.stream().map(converter::convert).toList(), forStmt.loopExpr.stream().map(converter::convert).toList(),
converter.convert(forStmt.block) converter.convertWrapInBlock(forStmt.block)
); );
} }
@Override @Override
public void visit(ForEachStmt forEachStmt) { public void visit(ForEachStmt forEachStmt) {
result = new TargetForEach(converter.convert(forEachStmt.statement), converter.convert(forEachStmt.expression), converter.convert(forEachStmt.block)); result = new TargetForEach(converter.convert(forEachStmt.statement), converter.convert(forEachStmt.expression), converter.convertWrapInBlock(forEachStmt.block));
} }
@Override @Override
public void visit(IfStmt ifStmt) { public void visit(IfStmt ifStmt) {
result = new TargetIf(converter.convert(ifStmt.expr), converter.convert(ifStmt.then_block), ifStmt.else_block != null ? converter.convert(ifStmt.else_block) : null); result = new TargetIf(converter.convert(ifStmt.expr), converter.convertWrapInBlock(ifStmt.then_block), ifStmt.else_block != null ? converter.convertWrapInBlock(ifStmt.else_block) : null);
} }
@Override @Override

View File

@ -848,4 +848,11 @@ public class TestComplete {
var clazz = classFiles.get("Bug122"); var clazz = classFiles.get("Bug122");
var instance = clazz.getDeclaredConstructor().newInstance(); var instance = clazz.getDeclaredConstructor().newInstance();
} }
@Test
public void testBug123() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug123.jav");
var clazz = classFiles.get("Bug123");
var instance = clazz.getDeclaredConstructor().newInstance();
}
} }