From bad5d26969626d4e0d593e92487833c604e1efa6 Mon Sep 17 00:00:00 2001 From: luca9913 Date: Mon, 17 Jul 2023 19:50:58 +0200 Subject: [PATCH] Extended instanceOfTest --- resources/syntaxtreegenerator/Instanceof.ast | 17 ++++++++++++++++- .../syntaxtreegenerator/javFiles/Instanceof.jav | 17 +++++++++++++++++ .../SyntaxTreeGenerator/StatementGenerator.java | 8 +++++++- .../syntaxtree/AbstractASTWalker.java | 4 +++- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/resources/syntaxtreegenerator/Instanceof.ast b/resources/syntaxtreegenerator/Instanceof.ast index b9b68ce7..7a1dd4e0 100644 --- a/resources/syntaxtreegenerator/Instanceof.ast +++ b/resources/syntaxtreegenerator/Instanceof.ast @@ -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(()); } -} \ No newline at end of file +} diff --git a/resources/syntaxtreegenerator/javFiles/Instanceof.jav b/resources/syntaxtreegenerator/javFiles/Instanceof.jav index 6401ef7c..c9a2d0b3 100644 --- a/resources/syntaxtreegenerator/javFiles/Instanceof.jav +++ b/resources/syntaxtreegenerator/javFiles/Instanceof.jav @@ -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; + } } \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 693b0f37..410ce933 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -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; @@ -799,7 +801,11 @@ public class StatementGenerator { } 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) { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index 6a69bd9d..26754b30 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -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