Created AST node TypePattern and adapted test cases in TestNewFeatures

This commit is contained in:
luca9913 2023-06-28 22:52:18 +02:00
parent 54a836b734
commit 1a89920430
6 changed files with 76 additions and 12 deletions

View File

@ -9,6 +9,19 @@ Instanceof(){
return a instanceof java.lang.Integer; return a instanceof java.lang.Integer;
} }
void checkInstanceOfWithPattern(){
TPH b;
b = 4.0;
if(b instanceof d)
{
return d;
}
else
{
return Kein Double;
};
}
Instanceof(){ Instanceof(){
super(()); super(());
} }

View File

@ -1,8 +1,19 @@
import java.lang.Integer; import java.lang.Integer;
import java.lang.Double;
import java.lang.String;
public class Instanceof{ public class Instanceof{
void checkInstanceof() { void checkInstanceof() {
var a = 4; var a = 4;
return (a instanceof java.lang.Integer); 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";
}
}
} }

View File

@ -652,14 +652,14 @@ expression
// Java17 // Java17
pattern pattern
: primaryPattern : primaryPattern #pPattern
| guardedPattern | guardedPattern #gPattern
; ;
primaryPattern primaryPattern
: typePattern : typePattern #tPattern
| recordPattern | recordPattern #rPattern
| '(' pattern ')' | '(' pattern ')' #enclosedPattern
; ;
recordPattern recordPattern
@ -667,7 +667,7 @@ recordPattern
; ;
typePattern typePattern
: variableModifier* typeType? identifier : variableModifier* typeType identifier
; ;
recordStructurePattern recordStructurePattern

View File

@ -47,12 +47,14 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.MethodcallexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.NewinstanceexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.NewinstanceexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.NullLiteralContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.NullLiteralContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.OrexpressionContext; 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.PostfixexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrefixexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrefixexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryClassrefContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryClassrefContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryExpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryExpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryIdentifierContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryIdentifierContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryLiteralContext; 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.PrimarySuperContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryThisContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryThisContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryexpressionContext; 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.SwitchexpressionstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext; 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.ThrowstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.TrycatchblockContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.TrycatchblockContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.TrycatchresourceContext; 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.WhileloopContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.YieldstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.YieldstmtContext;
import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.ParameterList; import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.TypePattern;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.statement.Assign; import de.dhbwstuttgart.syntaxtree.statement.Assign;
import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide; import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide;
@ -705,10 +710,26 @@ public class StatementGenerator {
} }
private Expression convert(Java17Parser.InstanceofexpressionContext expression) { private Expression convert(Java17Parser.InstanceofexpressionContext expression) {
Expression left = convert(expression.expression());
Token offset = expression.getStart();
if (Objects.isNull(expression.pattern())) { 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 { } 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();
}
} }
} }

View File

@ -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);
}
}

View File

@ -3,26 +3,33 @@ package de.dhbwstuttgart.syntaxtree.statement;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.TypePattern;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class InstanceOf extends BinaryExpr { public class InstanceOf extends BinaryExpr {
private RefTypeOrTPHOrWildcardOrGeneric reftype; private RefTypeOrTPHOrWildcardOrGeneric reftype;
private String name = null;
public InstanceOf(Expression expr, RefTypeOrTPHOrWildcardOrGeneric reftype, Token offset) { public InstanceOf(Expression expr, RefTypeOrTPHOrWildcardOrGeneric reftype, Token offset) {
super(BinaryExpr.Operator.INSTOF, TypePlaceholder.fresh(offset), expr, new LocalVar("", reftype, reftype.getOffset()), offset); super(BinaryExpr.Operator.INSTOF, TypePlaceholder.fresh(offset), expr, new LocalVar("", reftype, reftype.getOffset()), offset);
this.reftype = reftype; this.reftype = reftype;
} }
public InstanceOf(Expression expr, RefTypeOrTPHOrWildcardOrGeneric reftype, String name, Token offset) { public InstanceOf(Expression expr, TypePattern pattern, Token offset) {
super(BinaryExpr.Operator.INSTOF, TypePlaceholder.fresh(offset), expr, new LocalVar(name, reftype, reftype.getOffset()), offset); super(BinaryExpr.Operator.INSTOF, TypePlaceholder.fresh(offset), expr, new LocalVar(pattern.getName(), pattern.getType(), pattern.getOffset()), offset);
this.reftype = reftype; this.reftype = pattern.getType();
this.name = pattern.getName();
} }
public RefTypeOrTPHOrWildcardOrGeneric getReftype() { public RefTypeOrTPHOrWildcardOrGeneric getReftype() {
return reftype; return reftype;
} }
public String getName() {
return name;
}
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);