From 1a89920430f59a8556d06e12b6840fdbf181f800 Mon Sep 17 00:00:00 2001 From: luca9913 Date: Wed, 28 Jun 2023 22:52:18 +0200 Subject: [PATCH] Created AST node TypePattern and adapted test cases in TestNewFeatures --- resources/syntaxtreegenerator/Instanceof.ast | 13 ++++++++++ .../javFiles/Instanceof.jav | 13 +++++++++- .../parser/antlr/Java17Parser.g4 | 12 ++++----- .../StatementGenerator.java | 25 +++++++++++++++++-- .../dhbwstuttgart/syntaxtree/TypePattern.java | 12 +++++++++ .../syntaxtree/statement/InstanceOf.java | 13 +++++++--- 6 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 src/main/java/de/dhbwstuttgart/syntaxtree/TypePattern.java diff --git a/resources/syntaxtreegenerator/Instanceof.ast b/resources/syntaxtreegenerator/Instanceof.ast index 993d7740..b9b68ce7 100644 --- a/resources/syntaxtreegenerator/Instanceof.ast +++ b/resources/syntaxtreegenerator/Instanceof.ast @@ -9,6 +9,19 @@ Instanceof(){ return a instanceof java.lang.Integer; } + void checkInstanceOfWithPattern(){ + TPH b; + b = 4.0; + if(b instanceof d) + { + return d; + } + else + { + return Kein Double; + }; + } + Instanceof(){ super(()); } diff --git a/resources/syntaxtreegenerator/javFiles/Instanceof.jav b/resources/syntaxtreegenerator/javFiles/Instanceof.jav index 5b383437..6401ef7c 100644 --- a/resources/syntaxtreegenerator/javFiles/Instanceof.jav +++ b/resources/syntaxtreegenerator/javFiles/Instanceof.jav @@ -1,8 +1,19 @@ import java.lang.Integer; +import java.lang.Double; +import java.lang.String; public class Instanceof{ - void checkInstanceof() { + void checkInstanceof() { var a = 4; return (a instanceof java.lang.Integer); } + + void checkInstanceOfWithPattern(){ + var b = 4.0; + if(b instanceof java.lang.Double d){ + return d; + }else{ + return "Kein Double"; + } + } } \ No newline at end of file diff --git a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 index 966b4458..bc5f8f93 100644 --- a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 +++ b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 @@ -652,14 +652,14 @@ expression // Java17 pattern - : primaryPattern - | guardedPattern + : primaryPattern #pPattern + | guardedPattern #gPattern ; primaryPattern - : typePattern - | recordPattern - | '(' pattern ')' + : typePattern #tPattern + | recordPattern #rPattern + | '(' pattern ')' #enclosedPattern ; recordPattern @@ -667,7 +667,7 @@ recordPattern ; typePattern - : variableModifier* typeType? identifier + : variableModifier* typeType identifier ; recordStructurePattern diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index ca965b95..ada763a2 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -47,12 +47,14 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.MethodcallexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.NewinstanceexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.NullLiteralContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.OrexpressionContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.PPatternContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PostfixexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrefixexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryClassrefContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryExpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryIdentifierContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryLiteralContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryPatternContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimarySuperContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryThisContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryexpressionContext; @@ -65,15 +67,18 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.StringLiteralContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchexpressionstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.TPatternContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ThrowstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.TrycatchblockContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.TrycatchresourceContext; +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.JavaClassRegistry; import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.ParameterList; +import de.dhbwstuttgart.syntaxtree.TypePattern; import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; import de.dhbwstuttgart.syntaxtree.statement.Assign; import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide; @@ -705,10 +710,26 @@ public class StatementGenerator { } private Expression convert(Java17Parser.InstanceofexpressionContext expression) { + Expression left = convert(expression.expression()); + Token offset = expression.getStart(); if (Objects.isNull(expression.pattern())) { - return new InstanceOf(convert(expression.expression()), TypeGenerator.convert(expression.typeType(), reg, generics), expression.getStart()); + return new InstanceOf(left, TypeGenerator.convert(expression.typeType(), reg, generics), offset); } else { - throw new NotImplementedException(); + switch (expression.pattern()) { + case PPatternContext primaryPattern: + switch (primaryPattern.primaryPattern()) { + case TPatternContext typePattern: + TypePatternContext typePatternCtx = typePattern.typePattern(); + String localVarName = typePatternCtx.identifier().getText(); + RefTypeOrTPHOrWildcardOrGeneric localVarType = TypeGenerator.convert(typePatternCtx.typeType(), reg, generics); + localVars.put(localVarName, localVarType); + return new InstanceOf(left, new TypePattern(localVarName, localVarType, typePatternCtx.getStart()), offset); + default: + throw new NotImplementedException(); + } + default: + throw new NotImplementedException(); + } } } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/TypePattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/TypePattern.java new file mode 100644 index 00000000..3a6ac754 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/TypePattern.java @@ -0,0 +1,12 @@ +package de.dhbwstuttgart.syntaxtree; + +import org.antlr.v4.runtime.Token; + +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class TypePattern extends FormalParameter { + // TypePattern und FormalParameter sind exakt gleich aufgebaut + public TypePattern(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { + super(name, type, offset); + } +} diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java index f3ac9a86..fe4cf9b0 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java @@ -3,26 +3,33 @@ package de.dhbwstuttgart.syntaxtree.statement; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.TypePattern; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; public class InstanceOf extends BinaryExpr { private RefTypeOrTPHOrWildcardOrGeneric reftype; + private String name = null; public InstanceOf(Expression expr, RefTypeOrTPHOrWildcardOrGeneric reftype, Token offset) { super(BinaryExpr.Operator.INSTOF, TypePlaceholder.fresh(offset), expr, new LocalVar("", reftype, reftype.getOffset()), offset); this.reftype = reftype; } - public InstanceOf(Expression expr, RefTypeOrTPHOrWildcardOrGeneric reftype, String name, Token offset) { - super(BinaryExpr.Operator.INSTOF, TypePlaceholder.fresh(offset), expr, new LocalVar(name, reftype, reftype.getOffset()), offset); - this.reftype = reftype; + public InstanceOf(Expression expr, TypePattern pattern, Token offset) { + super(BinaryExpr.Operator.INSTOF, TypePlaceholder.fresh(offset), expr, new LocalVar(pattern.getName(), pattern.getType(), pattern.getOffset()), offset); + this.reftype = pattern.getType(); + this.name = pattern.getName(); } public RefTypeOrTPHOrWildcardOrGeneric getReftype() { return reftype; } + public String getName() { + return name; + } + @Override public void accept(StatementVisitor visitor) { visitor.visit(this);