Extended instanceOfTest

This commit is contained in:
luca9913 2023-07-17 19:50:58 +02:00
parent 951d741d90
commit f5b843ec11
4 changed files with 48 additions and 4 deletions

View File

@ -22,8 +22,23 @@ Instanceof(){
};
}
void checkInstanceOfWithGuardedPattern(){
TPH obj;
obj = test;
TPH flag;
if(obj instanceof s op s.length Signature: [TPH]() op 5)
{
flag = s.contains Signature: [TPH, TPH](jdk);
};
return;
}
java.lang.Boolean equals(java.lang.Object o){
return o instanceof other op x op other.x op y op other.y;
}
Instanceof(){
super(());
}
}
}

View File

@ -1,6 +1,7 @@
import java.lang.Integer;
import java.lang.Double;
import java.lang.String;
import java.lang.Object;
public class Instanceof{
void checkInstanceof() {
@ -16,4 +17,20 @@ public class Instanceof{
return "Kein Double";
}
}
void checkInstanceOfWithGuardedPattern(){
var obj = "test";
var flag;
if (obj instanceof String s && s.length() > 5) {
flag = s.contains("jdk");
}
}
record Point(int x, int y){ }
boolean equals(Object o) {
return (o instanceof Point other)
&& x == other.x
&& y == other.y;
}
}

View File

@ -91,6 +91,7 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.TypePatternContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.WhileloopContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.YieldstmtContext;
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.ParameterList;
@ -100,6 +101,7 @@ import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide;
import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.BoolExpression;
import de.dhbwstuttgart.syntaxtree.statement.Break;
import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
@ -795,11 +797,19 @@ public class StatementGenerator {
}
private Expression convert(Java17Parser.OrexpressionContext expression) {
throw new NotImplementedException();
if (expression.expression().size() != 2) {
throw new NotImplementedException();
} else {
return new BoolExpression(BoolExpression.Operator.OR, new RefType(new JavaClassName("java.lang.Boolean"), expression.getStart()), convert(expression.expression(0)), convert(expression.expression(1)), expression.getStart());
}
}
private Expression convert(Java17Parser.AndexpressionContext expression) {
throw new NotImplementedException();
if (expression.expression().size() != 2) {
throw new NotImplementedException();
} else {
return new BoolExpression(BoolExpression.Operator.AND, new RefType(new JavaClassName("java.lang.Boolean"), expression.getStart()), convert(expression.expression(0)), convert(expression.expression(1)), expression.getStart());
}
}
private Statement convert(AssignexpressionContext expr) {

View File

@ -5,6 +5,7 @@ import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.type.*;
import java.util.Iterator;
import java.util.Objects;
public abstract class AbstractASTWalker implements ASTVisitor {
@Override
@ -168,7 +169,8 @@ public abstract class AbstractASTWalker implements ASTVisitor {
@Override
public void visit(IfStmt ifStmt) {
ifStmt.then_block.accept(this);
ifStmt.else_block.accept(this);
if (!Objects.isNull(ifStmt.else_block))
ifStmt.else_block.accept(this);
}
@Override