feat: working with Integer-Values but not Strings.
Some checks failed
Build and Test with Maven / Build-and-test-with-Maven (push) Has been cancelled

This commit is contained in:
Ruben 2024-07-17 16:41:38 +02:00
parent 0d572ed9b6
commit 5f944e441c
5 changed files with 26 additions and 8 deletions

View File

@ -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;
};

View File

@ -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);

View File

@ -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<Pattern> 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());
}

View File

@ -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);
}

View File

@ -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);
}