Compare commits
No commits in common. "b80cc726c86a15cf6588f04c978844d94bb5f0bc" and "a84d1ffdd75411a5a3e205e1141b345dcb4f549b" have entirely different histories.
b80cc726c8
...
a84d1ffdd7
@ -1,15 +0,0 @@
|
|||||||
import java.util.ArrayList;
|
|
||||||
import java.lang.Integer;
|
|
||||||
|
|
||||||
public class ForEach {
|
|
||||||
m() {
|
|
||||||
var list = new ArrayList<>();
|
|
||||||
list.add(1); list.add(2); list.add(3);
|
|
||||||
|
|
||||||
var sum = 0;
|
|
||||||
for (var i : list) {
|
|
||||||
sum = sum + i;
|
|
||||||
}
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,7 +2,6 @@ package de.dhbwstuttgart.bytecode;
|
|||||||
|
|
||||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||||
import de.dhbwstuttgart.parser.NullToken;
|
|
||||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||||
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||||
@ -919,9 +918,6 @@ public class Codegen {
|
|||||||
state.localCounter = localCounter;
|
state.localCounter = localCounter;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TargetForEach forEach:
|
|
||||||
generateForEach(forEach, state);
|
|
||||||
break;
|
|
||||||
case TargetWhile _while: {
|
case TargetWhile _while: {
|
||||||
Label start = new Label();
|
Label start = new Label();
|
||||||
Label end = new Label();
|
Label end = new Label();
|
||||||
@ -1045,35 +1041,6 @@ public class Codegen {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateForEach(TargetForEach forEach, State state) {
|
|
||||||
state.enterScope();
|
|
||||||
TargetVarDecl vd = (TargetVarDecl) forEach.vardecl();
|
|
||||||
var localVar = state.createVariable(vd.name(), vd.varType());
|
|
||||||
|
|
||||||
var expr = forEach.expression();
|
|
||||||
// TODO Check for arrays
|
|
||||||
var mv = state.mv;
|
|
||||||
generate(state, expr);
|
|
||||||
mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/Iterable", "iterator", "()Ljava/util/Iterator;", true);
|
|
||||||
var start = new Label();
|
|
||||||
var end = new Label();
|
|
||||||
mv.visitLabel(start);
|
|
||||||
mv.visitInsn(DUP);
|
|
||||||
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z", true);
|
|
||||||
mv.visitJumpInsn(IFEQ, end);
|
|
||||||
mv.visitInsn(DUP);
|
|
||||||
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;", true);
|
|
||||||
mv.visitVarInsn(ASTORE, localVar.index);
|
|
||||||
|
|
||||||
generate(state, forEach.body());
|
|
||||||
|
|
||||||
mv.visitJumpInsn(GOTO, start);
|
|
||||||
mv.visitLabel(end);
|
|
||||||
mv.visitInsn(POP);
|
|
||||||
|
|
||||||
state.exitScope();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void generateInstanceOf(State state, TargetInstanceOf instanceOf) {
|
private void generateInstanceOf(State state, TargetInstanceOf instanceOf) {
|
||||||
var mv = state.mv;
|
var mv = state.mv;
|
||||||
|
|
||||||
|
@ -490,32 +490,15 @@ public class StatementGenerator {
|
|||||||
|
|
||||||
private Statement convert(Java17Parser.ForloopContext stmt) {
|
private Statement convert(Java17Parser.ForloopContext stmt) {
|
||||||
var control = stmt.forControl();
|
var control = stmt.forControl();
|
||||||
if (control.enhancedForControl() != null) {
|
var block = convert(stmt.statement());
|
||||||
var forCtrl = control.enhancedForControl();
|
if (control.enhancedForControl() != null)
|
||||||
var name = forCtrl.variableDeclaratorId().identifier();
|
throw new NotImplementedException();
|
||||||
|
else {
|
||||||
RefTypeOrTPHOrWildcardOrGeneric type;
|
|
||||||
if (Objects.isNull(forCtrl.typeType()) || !Objects.isNull(forCtrl.VAR())) {
|
|
||||||
type = TypePlaceholder.fresh(forCtrl.getStart());
|
|
||||||
} else {
|
|
||||||
type = TypeGenerator.convert(forCtrl.typeType(), reg, generics);
|
|
||||||
}
|
|
||||||
|
|
||||||
var vardecl = new LocalVarDecl(name.getText(), type, name.getStart());
|
|
||||||
this.localVars.put(name.getText(), type);
|
|
||||||
|
|
||||||
return new ForEachStmt(
|
|
||||||
stmt.getStart(),
|
|
||||||
vardecl,
|
|
||||||
convert(forCtrl.expression()),
|
|
||||||
convert(stmt.statement())
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
return new ForStmt(
|
return new ForStmt(
|
||||||
stmt.getStart(),
|
stmt.getStart(),
|
||||||
convert(control.forInit().localVariableDeclaration()),
|
convert(control.forInit().localVariableDeclaration()),
|
||||||
convert(control.expression()), control.forUpdate.expression().stream().map(this::convert).toList(),
|
convert(control.expression()), control.forUpdate.expression().stream().map(this::convert).toList(),
|
||||||
convert(stmt.statement())
|
block
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -166,11 +166,6 @@ public abstract class AbstractASTWalker implements ASTVisitor {
|
|||||||
forStmt.block.accept(this);
|
forStmt.block.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(ForEachStmt forEachStmt) {
|
|
||||||
forEachStmt.block.accept(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(IfStmt ifStmt) {
|
public void visit(IfStmt ifStmt) {
|
||||||
ifStmt.then_block.accept(this);
|
ifStmt.then_block.accept(this);
|
||||||
|
@ -25,8 +25,6 @@ public interface StatementVisitor {
|
|||||||
|
|
||||||
void visit(ForStmt forStmt);
|
void visit(ForStmt forStmt);
|
||||||
|
|
||||||
void visit(ForEachStmt forEachStmt);
|
|
||||||
|
|
||||||
void visit(IfStmt ifStmt);
|
void visit(IfStmt ifStmt);
|
||||||
|
|
||||||
void visit(InstanceOf instanceOf);
|
void visit(InstanceOf instanceOf);
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
package de.dhbwstuttgart.syntaxtree.statement;
|
|
||||||
|
|
||||||
import de.dhbwstuttgart.parser.NullToken;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.type.Void;
|
|
||||||
import org.antlr.v4.runtime.Token;
|
|
||||||
|
|
||||||
public class ForEachStmt extends Statement {
|
|
||||||
|
|
||||||
public final Statement statement;
|
|
||||||
public final Expression expression;
|
|
||||||
public final Statement block;
|
|
||||||
|
|
||||||
public ForEachStmt(Token offset, Statement stmt, Expression expression, Statement block) {
|
|
||||||
super(new Void(new NullToken()), offset);
|
|
||||||
this.statement = stmt;
|
|
||||||
this.expression = expression;
|
|
||||||
this.block = block;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void accept(StatementVisitor visitor) {
|
|
||||||
visitor.visit(this);
|
|
||||||
}
|
|
||||||
}
|
|
@ -244,11 +244,6 @@ public class OutputGenerator implements ASTVisitor {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(ForEachStmt forEachStmt) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(IfStmt ifStmt) {
|
public void visit(IfStmt ifStmt) {
|
||||||
out.append("if(");
|
out.append("if(");
|
||||||
|
@ -144,11 +144,6 @@ public class StatementToTargetExpression implements ASTVisitor {
|
|||||||
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(), converter.convert(forStmt.condition), forStmt.loopExpr.stream().map(converter::convert).toList(), converter.convert(forStmt.block));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(ForEachStmt forEachStmt) {
|
|
||||||
result = new TargetForEach(converter.convert(forEachStmt.statement), converter.convert(forEachStmt.expression), converter.convert(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.convert(ifStmt.then_block), ifStmt.else_block != null ? converter.convert(ifStmt.else_block) : null);
|
||||||
|
@ -66,11 +66,6 @@ public abstract class TracingStatementVisitor implements StatementVisitor {
|
|||||||
forStmt.block.accept(this);
|
forStmt.block.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(ForEachStmt forEachStmt) {
|
|
||||||
forEachStmt.block.accept(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(IfStmt ifStmt) {
|
public void visit(IfStmt ifStmt) {
|
||||||
ifStmt.then_block.accept(this);
|
ifStmt.then_block.accept(this);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package de.dhbwstuttgart.target.tree.expression;
|
package de.dhbwstuttgart.target.tree.expression;
|
||||||
|
|
||||||
public record TargetForEach(TargetExpression vardecl, TargetExpression expression, TargetExpression body) implements TargetExpression {
|
public record TargetForEach(TargetExpression vardecl, TargetExpression list) implements TargetExpression {
|
||||||
}
|
}
|
||||||
|
@ -128,15 +128,6 @@ public class TYPEStmt implements StatementVisitor {
|
|||||||
forStmt.block.accept(this);
|
forStmt.block.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(ForEachStmt forEachStmt) {
|
|
||||||
var iterableType = new RefType(ASTFactory.createClass(java.lang.Iterable.class).getClassName(), Arrays.asList(forEachStmt.statement.getType()), new NullToken());
|
|
||||||
constraintsSet.addUndConstraint(new Pair(forEachStmt.expression.getType(), iterableType, PairOperator.SMALLERDOT));
|
|
||||||
forEachStmt.statement.accept(this);
|
|
||||||
forEachStmt.expression.accept(this);
|
|
||||||
forEachStmt.block.accept(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(IfStmt ifStmt) {
|
public void visit(IfStmt ifStmt) {
|
||||||
RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken());
|
RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken());
|
||||||
|
@ -737,15 +737,6 @@ public class TestComplete {
|
|||||||
var m = clazz.getDeclaredMethod("m", Integer.class);
|
var m = clazz.getDeclaredMethod("m", Integer.class);
|
||||||
assertEquals(m.invoke(instance, 10), 60);
|
assertEquals(m.invoke(instance, 10), 60);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testForEach() throws Exception {
|
|
||||||
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "ForEach.jav");
|
|
||||||
var clazz = classFiles.get("ForEach");
|
|
||||||
var instance = clazz.getDeclaredConstructor().newInstance();
|
|
||||||
var m = clazz.getDeclaredMethod("m");
|
|
||||||
assertEquals(m.invoke(instance), 6);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLambdaRunnable() throws Exception {
|
public void testLambdaRunnable() throws Exception {
|
||||||
|
Loading…
Reference in New Issue
Block a user