diff --git a/resources/bytecode/javFiles/Bug122.jav b/resources/bytecode/javFiles/Bug122.jav new file mode 100644 index 00000000..78a1c019 --- /dev/null +++ b/resources/bytecode/javFiles/Bug122.jav @@ -0,0 +1,12 @@ +import java.lang.Integer; +import java.lang.Boolean; + +class Bug122 { + void main() { + if (true) { + for (Integer i = 0; i < 10; i++) { + + } + } + } +} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index 7c06ee8d..7445e772 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -886,8 +886,12 @@ public class Codegen { case TargetFor _for: { state.enterScope(); var localCounter = state.localCounter; - if (_for.init() != null) - _for.init().forEach(e -> generate(state, e)); + if (_for.init() != null) { + for (var e : _for.init()) { + generate(state, e); + if (e instanceof TargetAssign) mv.visitInsn(POP); + } + } Label start = new Label(); Label end = new Label(); diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 64dad545..27f0bf6b 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -513,8 +513,9 @@ public class StatementGenerator { } else { return new ForStmt( stmt.getStart(), - convert(control.forInit().localVariableDeclaration()), - convert(control.expression()), control.forUpdate.expression().stream().map(this::convert).toList(), + control.forInit() != null ? convert(control.forInit().localVariableDeclaration()) : List.of(), + control.expression() != null ? convert(control.expression()) : null, + control.forUpdate != null ? control.forUpdate.expression().stream().map(this::convert).toList() : List.of(), convert(stmt.statement()) ); } diff --git a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java index 702432a0..9117a8d6 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java @@ -141,7 +141,12 @@ public class StatementToTargetExpression implements ASTVisitor { @Override public void visit(ForStmt forStmt) { - result = new TargetFor(forStmt.initializer.stream().map(converter::convert).toList(), converter.convert(forStmt.condition), forStmt.loopExpr.stream().map(converter::convert).toList(), converter.convert(forStmt.block)); + result = new TargetFor( + forStmt.initializer.stream().map(converter::convert).toList(), + forStmt.condition != null ? converter.convert(forStmt.condition) : null, + forStmt.loopExpr.stream().map(converter::convert).toList(), + converter.convert(forStmt.block) + ); } @Override diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index b65664e1..41ded156 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -129,7 +129,8 @@ public class TYPEStmt implements StatementVisitor { @Override public void visit(ForStmt forStmt) { forStmt.initializer.forEach(s -> s.accept(this)); - forStmt.condition.accept(this); + if (forStmt.condition != null) + forStmt.condition.accept(this); forStmt.loopExpr.forEach(e -> e.accept(this)); forStmt.block.accept(this); } diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index dc15ec80..4de13307 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -841,4 +841,11 @@ public class TestComplete { var clazz = classFiles.get("Op2"); var instance = clazz.getDeclaredConstructor().newInstance(); } + + @Test + public void testBug122() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug122.jav"); + var clazz = classFiles.get("Bug122"); + var instance = clazz.getDeclaredConstructor().newInstance(); + } }