diff --git a/resources/bytecode/javFiles/SwitchInfered.jav b/resources/bytecode/javFiles/SwitchInfered.jav index d11c760d..cf0e8b40 100644 --- a/resources/bytecode/javFiles/SwitchInfered.jav +++ b/resources/bytecode/javFiles/SwitchInfered.jav @@ -2,14 +2,12 @@ import java.lang.Integer; import java.lang.Object; import java.lang.Float; -public record Rec(Integer a, Object b) {} +public record Rec(Object a, Object b) {} public class SwitchInfered { public main(o) { return switch (o) { case Rec(a, b) -> a + b; - case Rec(Integer a, Float b) -> a + 10; - case Rec(Integer a, Rec(Integer b, Integer c)) -> a + b + c; case Integer i -> i; default -> 0; }; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index 8df566b9..38c95ea4 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -1404,6 +1404,9 @@ public class Codegen { for (var i = 0; i < depth; i++) { state.mv.visitInsn(POP); } + + + state.mv.visitVarInsn(ALOAD, state.switchResultValue.peek()); state.mv.visitLdcInsn(index + 1); state.mv.visitJumpInsn(GOTO, start); diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 11716318..ad007f60 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -457,7 +457,7 @@ public class StatementGenerator { case TPatternContext tPattern: TypePatternContext typePattern = tPattern.typePattern(); var text = typePattern.identifier().getText(); - var type = typePattern.typeType() == null ? TypePlaceholder.fresh(typePattern.getStart()) : TypeGenerator.convert(typePattern.typeType(), reg, generics); + var type = typePattern.typeType() == null ? TypePlaceholder.fresh(typePattern.getStart()) : TypeGenerator.convert(typePattern.typeType(), reg, generics); localVars.put(text, type); return new FormalParameter(text, type, typePattern.getStart()); case RPatternContext rPattern: @@ -474,7 +474,8 @@ public class StatementGenerator { List subPattern = subPatternCtx.stream().map(this::convert).collect(Collectors.toList()); IdentifierContext identifierCtx = recordPatternCtx.identifier(); var text = (identifierCtx != null) ? identifierCtx.getText() : null; - var type = TypeGenerator.convert(recordPatternCtx.typeType(), reg, generics); + //Hier evtl. Typ anpassen -> wenn kein Typ bekannt ist push neuen Typ auf Hashtable + var type = recordPatternCtx.typeType() == null ? TypePlaceholder.fresh(recordPatternCtx.getStart()) : TypeGenerator.convert(recordPatternCtx.typeType(), reg, generics); if (text != null) localVars.put(text, type); return new RecordPattern(subPattern, text, type, recordPatternCtx.getStart()); } diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 9d318ba6..e90837e3 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -3,16 +3,19 @@ import de.dhbwstuttgart.environment.ByteArrayClassLoader; import org.junit.Ignore; import org.junit.Test; +import java.io.*; import java.lang.reflect.*; import java.util.AbstractList; import java.util.Arrays; import java.util.List; import java.util.Vector; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; import targetast.TestCodegen; import static org.junit.Assert.*; -import static targetast.TestCodegen.generateClassFiles; +import static targetast.TestCodegen.generateClassFiles; public class TestComplete { @Test @@ -678,8 +681,16 @@ public class TestComplete { public void testSwitchInfered() throws Exception { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SwitchInfered.jav"); var clazz = classFiles.get("SwitchInfered"); + var instance = clazz.getDeclaredConstructor().newInstance(); var swtch = clazz.getDeclaredMethod("main", Object.class); + + var record = classFiles.get("Rec"); + var ctor = record.getDeclaredConstructor(Object.class, Object.class); + var r1 = ctor.newInstance(1, 2); + + + assertEquals(swtch.invoke(instance, r1), 3); } @Ignore("Not implemented") @@ -733,6 +744,7 @@ public class TestComplete { var clazz = classFiles.get("Interfaces"); var instance = clazz.getDeclaredConstructor().newInstance(); } + @Test public void testStatic() throws Exception { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Static.jav"); @@ -758,7 +770,7 @@ public class TestComplete { var m = clazz.getDeclaredMethod("m"); assertEquals(m.invoke(instance), 6); } - + @Test public void testLambdaRunnable() throws Exception { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "LambdaRunnable.jav"); @@ -939,7 +951,7 @@ public class TestComplete { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "While.jav"); var clazz = classFiles.get("While"); var instance = clazz.getDeclaredConstructor().newInstance(); - assertEquals(clazz.getDeclaredMethod("m", Integer.class).invoke(instance, 5), 5); + assertEquals(clazz.getDeclaredMethod("m", Integer.class).invoke(instance, 5), 5); assertEquals(clazz.getDeclaredMethod("m2").invoke(instance), 10); } diff --git a/src/test/java/targetast/TestCodegen.java b/src/test/java/targetast/TestCodegen.java index 3d698169..10af0bf5 100644 --- a/src/test/java/targetast/TestCodegen.java +++ b/src/test/java/targetast/TestCodegen.java @@ -22,6 +22,7 @@ import static org.junit.Assert.*; import org.objectweb.asm.Opcodes; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -83,6 +84,9 @@ public class TestCodegen { public static Class generateClass(TargetStructure clazz, IByteArrayClassLoader classLoader, ASTToTargetAST converter) throws IOException { Codegen codegen = new Codegen(clazz, converter.compiler, converter); var code = codegen.generate(); + try (FileOutputStream fos = new FileOutputStream("C:\\Users\\ruben\\Desktop\\asd\\test.class")) { + fos.write(code); + } writeClassFile(clazz.qualifiedName().getClassName(), code); return classLoader.loadClass(code); }