Add broken value matching
Some checks failed
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 40s
Some checks failed
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 40s
This commit is contained in:
parent
443b8b0c09
commit
f846142ee1
14
resources/bytecode/javFiles/SwitchNestedValue.jav
Normal file
14
resources/bytecode/javFiles/SwitchNestedValue.jav
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import java.lang.Object;
|
||||||
|
import java.lang.Integer;
|
||||||
|
|
||||||
|
public record R(Integer i) {}
|
||||||
|
|
||||||
|
public class SwitchNestedValue {
|
||||||
|
public main(r) {
|
||||||
|
return switch(r) {
|
||||||
|
case R(10) -> 1;
|
||||||
|
case R(20) -> 2;
|
||||||
|
case R(i) -> 3;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -109,6 +109,10 @@ public class Codegen {
|
|||||||
this.scope = this.scope.parent;
|
this.scope = this.scope.parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LocalVar createVariable(TargetType type) {
|
||||||
|
return createVariable("__var" + this.localCounter, type);
|
||||||
|
}
|
||||||
|
|
||||||
LocalVar createVariable(String name, TargetType type) {
|
LocalVar createVariable(String name, TargetType type) {
|
||||||
var local = new LocalVar(localCounter, name, type);
|
var local = new LocalVar(localCounter, name, type);
|
||||||
scope.add(local);
|
scope.add(local);
|
||||||
@ -1433,7 +1437,24 @@ public class Codegen {
|
|||||||
state.mv.visitTypeInsn(CHECKCAST, pat.type().getInternalName());
|
state.mv.visitTypeInsn(CHECKCAST, pat.type().getInternalName());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pat instanceof TargetTypePattern sp) {
|
if (pat instanceof TargetExpressionPattern ep) {
|
||||||
|
var cur = state.createVariable(pat.type());
|
||||||
|
state.mv.visitVarInsn(ASTORE, cur.index);
|
||||||
|
|
||||||
|
var expr = new Equal(pat.type(), new TargetLocalVar(cur.type, cur.name), ep.expression());
|
||||||
|
generate(state, expr);
|
||||||
|
|
||||||
|
var cont = new Label();
|
||||||
|
state.mv.visitJumpInsn(IFNE, cont);
|
||||||
|
for (var i = 0; i < depth - 1; i++) {
|
||||||
|
state.mv.visitInsn(POP);
|
||||||
|
}
|
||||||
|
|
||||||
|
state.mv.visitVarInsn(ALOAD, state.switchResultValue.peek());
|
||||||
|
state.mv.visitLdcInsn(index + 1);
|
||||||
|
state.mv.visitJumpInsn(GOTO, start);
|
||||||
|
state.mv.visitLabel(cont);
|
||||||
|
} else if (pat instanceof TargetTypePattern sp) {
|
||||||
var local = state.createVariable(sp.name(), sp.type());
|
var local = state.createVariable(sp.name(), sp.type());
|
||||||
state.mv.visitVarInsn(ASTORE, local.index);
|
state.mv.visitVarInsn(ASTORE, local.index);
|
||||||
} else if (pat instanceof TargetComplexPattern cp) {
|
} else if (pat instanceof TargetComplexPattern cp) {
|
||||||
|
@ -10,6 +10,8 @@ public interface ASTVisitor extends StatementVisitor{
|
|||||||
|
|
||||||
void visit(FormalParameter formalParameter);
|
void visit(FormalParameter formalParameter);
|
||||||
|
|
||||||
|
void visit(LiteralPattern literalPattern);
|
||||||
|
|
||||||
void visit(GenericDeclarationList genericTypeVars);
|
void visit(GenericDeclarationList genericTypeVars);
|
||||||
|
|
||||||
void visit(Field field);
|
void visit(Field field);
|
||||||
|
@ -37,6 +37,11 @@ public abstract class AbstractASTWalker implements ASTVisitor {
|
|||||||
formalParameter.getType().accept((ASTVisitor) this);
|
formalParameter.getType().accept((ASTVisitor) this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(LiteralPattern literalPattern) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(GenericDeclarationList genericTypeVars) {
|
public void visit(GenericDeclarationList genericTypeVars) {
|
||||||
Iterator<GenericTypeVar> genericIterator = genericTypeVars.iterator();
|
Iterator<GenericTypeVar> genericIterator = genericTypeVars.iterator();
|
||||||
|
@ -57,6 +57,11 @@ public class OutputGenerator implements ASTVisitor {
|
|||||||
out.append(formalParameter.getName());
|
out.append(formalParameter.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(LiteralPattern literalPattern) {
|
||||||
|
out.append(literalPattern.value);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(GenericDeclarationList genericTypeVars) {
|
public void visit(GenericDeclarationList genericTypeVars) {
|
||||||
Iterator<GenericTypeVar> genericIterator = genericTypeVars.iterator();
|
Iterator<GenericTypeVar> genericIterator = genericTypeVars.iterator();
|
||||||
|
@ -568,6 +568,11 @@ public class StatementToTargetExpression implements ASTVisitor {
|
|||||||
result = new TargetTypePattern(converter.convert(aPattern.getType()), aPattern.getName());
|
result = new TargetTypePattern(converter.convert(aPattern.getType()), aPattern.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(LiteralPattern literalPattern) {
|
||||||
|
result = new TargetExpressionPattern(converter.convert(new Literal(literalPattern.getType(), literalPattern.value, literalPattern.getOffset())));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(ExpressionPattern aPattern) {
|
public void visit(ExpressionPattern aPattern) {
|
||||||
result = converter.convert(aPattern.getExpression());
|
result = converter.convert(aPattern.getExpression());
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package de.dhbwstuttgart.target.tree.expression;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.target.tree.type.TargetType;
|
||||||
|
|
||||||
|
public record TargetExpressionPattern(TargetExpression expression) implements TargetPattern {
|
||||||
|
@Override
|
||||||
|
public TargetPattern withType(TargetType type) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TargetType type() {
|
||||||
|
return expression.type();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TargetPattern withName(String name) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,7 @@ package de.dhbwstuttgart.target.tree.expression;
|
|||||||
|
|
||||||
import de.dhbwstuttgart.target.tree.type.TargetType;
|
import de.dhbwstuttgart.target.tree.type.TargetType;
|
||||||
|
|
||||||
public sealed interface TargetPattern extends TargetExpression permits TargetComplexPattern, TargetGuard, TargetTypePattern {
|
public sealed interface TargetPattern extends TargetExpression permits TargetComplexPattern, TargetExpressionPattern, TargetGuard, TargetTypePattern {
|
||||||
default String name() {
|
default String name() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class TypeInsertPlacer extends AbstractASTWalker{
|
public class TypeInsertPlacer extends AbstractASTWalker {
|
||||||
Set<TypeInsert> inserts = new HashSet<>();
|
Set<TypeInsert> inserts = new HashSet<>();
|
||||||
private ResultSet withResults;
|
private ResultSet withResults;
|
||||||
String pkgName;
|
String pkgName;
|
||||||
|
@ -782,6 +782,24 @@ public class TestComplete {
|
|||||||
assertEquals(swtch.invoke(instance, listWithOneElement), 10);
|
assertEquals(swtch.invoke(instance, listWithOneElement), 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSwitchNestedValue() throws Exception {
|
||||||
|
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SwitchNestedValue.jav");
|
||||||
|
var clazz = classFiles.get("SwitchNestedValue");
|
||||||
|
var rec = classFiles.get("R");
|
||||||
|
var instance = clazz.getDeclaredConstructor().newInstance();
|
||||||
|
var m = clazz.getDeclaredMethod("main", rec);
|
||||||
|
|
||||||
|
var ctor = rec.getDeclaredConstructor(Integer.class);
|
||||||
|
var r1 = ctor.newInstance(10);
|
||||||
|
var r2 = ctor.newInstance(20);
|
||||||
|
var r3 = ctor.newInstance(30);
|
||||||
|
|
||||||
|
assertEquals(m.invoke(instance, r1), 1);
|
||||||
|
assertEquals(m.invoke(instance, r2), 2);
|
||||||
|
assertEquals(m.invoke(instance, r3), 3);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGenericRecordSwitchCase() throws Exception {
|
public void testGenericRecordSwitchCase() throws Exception {
|
||||||
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "GenericRecordSwitchCase.jav");
|
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "GenericRecordSwitchCase.jav");
|
||||||
|
Loading…
Reference in New Issue
Block a user