forked from JavaTX/JavaCompilerCore
Created AST node TypePattern and adapted test cases in TestNewFeatures
This commit is contained in:
parent
54a836b734
commit
1a89920430
@ -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(());
|
||||
}
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
12
src/main/java/de/dhbwstuttgart/syntaxtree/TypePattern.java
Normal file
12
src/main/java/de/dhbwstuttgart/syntaxtree/TypePattern.java
Normal 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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user