Changes from weeks ago, needs review

This commit is contained in:
Daniel Holle 2023-10-17 12:26:40 +02:00
parent 56c360104b
commit 700ecf3e5d
20 changed files with 147 additions and 33 deletions

21
Switch.java Normal file
View File

@ -0,0 +1,21 @@
public class Switch {
public static void main(String[] args) {
CharSequence s = "some string";
/*var res = switch (s) {
case "AaAaAa" -> 20;
case "AaAaBB" -> 30;
case String y -> 40;
case CharSequence cs -> 30;
};*/
var i = s instanceof String s2;
//System.out.println(s2);
if (!(s instanceof String s2)) {
System.out.println(s2);
} else {
System.out.println(s2);
}
}
}

View File

@ -0,0 +1,22 @@
class Pair<U, T> {
U a;
T b;
Pair(U x, T y) {
a = x; b = y;
}
}
class Complex {
m(b) {
var c = b;
var d = c;
var e;
d = e;
var r1 = e;
var f = e;
var g;
f = g;
var r2 = g;
return new Pair<>(r1, r2);
}
}

View File

@ -0,0 +1,7 @@
public class InfReturn {
m(a) {
var ret;
a = ret;
return ret;
}
}

View File

@ -0,0 +1,8 @@
public class InfReturnII {
m(a, b) {
var ret;
a = ret;
b = ret;
return ret;
}
}

View File

@ -0,0 +1,7 @@
class InnerInf {
m(a, b) {
var i;
a = i;
b = i;
}
}

View File

@ -0,0 +1,7 @@
import java.lang.Integer;
import java.lang.Double;
class Overloading {
m(x) { return x + x; }
m(x) { return x || x; }
}

View File

@ -0,0 +1,8 @@
class RecursionCond {
m(a, b, c) {
if (1 == 2) {
b = m(a, b);
c = m(a, b);
} else return a;
}
}

View File

@ -0,0 +1,9 @@
import java.lang.Boolean;
import java.lang.Integer;
public class Test {
fac = (x) -> {
if (x == 1) { return 1; }
return x * fac.apply(x - 1);
};
}

View File

@ -1,8 +1,8 @@
import java.lang.Integer;
import java.lang.Double;
import java.lang.String;
public class Fac {
getFac(n) {
var res = 1;
var i = 1;

View File

@ -1,12 +1,18 @@
import java.lang.Integer;
import java.lang.Float;
import java.lang.Number;
record Point(Integer x, Integer y) {}
record Point(Number x, Number y) {}
public class OverloadPattern {
m(Point(Integer x, Integer y)) {
return x + y;
}
m(Point(Float x, Float y)) {
return x * y;
}
m(Integer x) {
return x;
}

View File

@ -0,0 +1,13 @@
import java.lang.Integer;
record Point(x, y) {}
class Switch2 {
m() {
var pt = new Point(10, 20);
return switch (pt) {
case Point(x, y) -> 10;
default -> 20;
};
}
}

View File

@ -3,16 +3,4 @@ public class Tph2 {
id3 (x) {
return id.apply(x);
}
/*
m(a,b){
var c = m2(a,b);
//m2(a,b);
return a;
}
m2(a,b){
return b;
}
*/
}

View File

@ -668,6 +668,11 @@ recordStructurePattern
: '(' recordComponentPatternList? ')'
;
recordComponentPatternElement
: pattern
| identifier
;
recordComponentPatternList
: pattern (',' pattern)*
;

View File

@ -1133,7 +1133,10 @@ public class Codegen {
else if (label instanceof TargetGuard guard)
types.add(Type.getObjectType(guard.inner().type().getInternalName()));
// TODO Same here we need to evaluate constant;
else throw new NotImplementedException();
else {
System.out.println(label);
throw new NotImplementedException();
}
}
mv.visitInvokeDynamicInsn("typeSwitch", "(Ljava/lang/Object;I)I", bootstrap, types.toArray());
@ -1330,6 +1333,7 @@ public class Codegen {
state.mv.visitInsn(DUP);
extractField(state, cp.type(), i, clazz);
state.mv.visitTypeInsn(CHECKCAST, subPattern.type().getInternalName());
state.mv.visitVarInsn(ASTORE, offset);
offset = bindLocalVariables(state, subPattern, offset, i);
}

View File

@ -189,7 +189,7 @@ public class SyntaxTreeGenerator {
Token offset = ctx.getStart();
GenericDeclarationList genericClassParameters;
if (ctx.genericDeclarationList() == null) {
genericClassParameters = new GenericDeclarationList(new ArrayList<>(), ctx.identifier().getStop());
genericClassParameters = new GenericDeclarationList(new ArrayList<>(), ctx.classBody().getStart());
} else {
genericClassParameters = TypeGenerator.convert(ctx.genericDeclarationList(), name, "", reg, generics);
}
@ -239,7 +239,7 @@ public class SyntaxTreeGenerator {
}
GenericDeclarationList genericClassParameters;
if (recordDeclaration.genericDeclarationList() == null) {
genericClassParameters = new GenericDeclarationList(new ArrayList<>(), recordDeclaration.identifier().getStop());
genericClassParameters = new GenericDeclarationList(new ArrayList<>(), recordDeclaration.recordBody().getStart());
} else {
genericClassParameters = TypeGenerator.convert(recordDeclaration.genericDeclarationList(), name, "", reg, generics);
}

View File

@ -301,7 +301,6 @@ public abstract class AbstractASTWalker implements ASTVisitor {
@Override
public void visit(Yield aYield) {
aYield.accept(this);
}
@Override

View File

@ -4,24 +4,20 @@ import java.io.IOException;
import java.lang.reflect.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import de.dhbwstuttgart.bytecode.JavaTXSignatureAttribute;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
import de.dhbwstuttgart.target.tree.type.TargetRefType;
import de.dhbwstuttgart.util.Pair;
import javassist.bytecode.SignatureAttribute;
import org.antlr.v4.runtime.Token;
import org.apache.commons.io.IOUtils;
import org.objectweb.asm.*;

View File

@ -381,7 +381,12 @@ public class TYPEStmt implements StatementVisitor {
}
@Override
public void visit(BoolExpression bool) {
public void visit(BoolExpression expr) {
constraintsSet.addUndConstraint(new Pair(bool, expr.getType(), PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(bool, expr.lexpr.getType(), PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(bool, expr.rexpr.getType(), PairOperator.EQUALSDOT));
// TODO
return;
}

View File

@ -57,7 +57,9 @@ public class AllgemeinTest {
//String className = "UseWildcardPair";
//String className = "Assign";
//String className = "StreamTest";
String className = "Iteration";
//String className = "Iteration";
String className = "Test";
//PL 2019-10-24: genutzt fuer unterschiedliche Tests
path = System.getProperty("user.dir")+"/resources/AllgemeinTest/" + className + ".jav";
//path = System.getProperty("user.dir")+"/src/test/resources/AllgemeinTest/Overloading_Generics.jav";
@ -66,10 +68,10 @@ public class AllgemeinTest {
///*
compiler = new JavaTXCompiler(
Lists.newArrayList(new File(path)),
Lists.newArrayList(new File(System.getProperty("user.dir")+"/resources/testBytecode/generatedBC/")));
Lists.newArrayList(new File(System.getProperty("user.dir")+"/resources/bytecode/classFiles/")));
//*/
compiler.generateBytecode(System.getProperty("user.dir")+"/resources/testBytecode/generatedBC/");
pathToClassFile = System.getProperty("user.dir")+"/resources/testBytecode/generatedBC/";
compiler.generateBytecode(System.getProperty("user.dir")+"/resources/bytecode/classFiles/");
pathToClassFile = System.getProperty("user.dir")+"/resources/bytecode/classFiles/";
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTest = loader.loadClass(className);
//classToTest = loader.loadClass("Overloading_Generics");

View File

@ -671,6 +671,13 @@ public class TestComplete {
assertEquals(swtch.invoke(instance, "Some string"), 0);
}
@Test
public void testSwitch2() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Switch2.jav");
var clazz = classFiles.get("Switch2");
var instance = clazz.getDeclaredConstructor().newInstance();
}
@Test
public void testStringSwitch() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SwitchString.jav");
@ -702,7 +709,7 @@ public class TestComplete {
var m1 = clazz.getDeclaredMethod("m", rec);
var m2 = clazz.getDeclaredMethod("m", Integer.class);
var pt = rec.getDeclaredConstructor(Integer.class, Integer.class).newInstance(10, 20);
var pt = rec.getDeclaredConstructor(Number.class, Number.class).newInstance(10, 20);
assertEquals(m1.invoke(instance, pt), 30);
assertEquals(m2.invoke(instance, 10), 10);
}