Refactor Patterns

This commit is contained in:
Daniel Holle 2023-08-17 15:56:16 +02:00
parent 6025e17186
commit ee359da751
19 changed files with 247 additions and 151 deletions

View File

@ -8,6 +8,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.type.Void;
import org.antlr.v4.runtime.Token;
@ -93,8 +94,6 @@ 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;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.statement.Assign;
import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide;
@ -108,7 +107,6 @@ import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
import de.dhbwstuttgart.syntaxtree.statement.GuardedPattern;
import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
@ -118,7 +116,6 @@ import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.statement.NewClass;
import de.dhbwstuttgart.syntaxtree.statement.Receiver;
import de.dhbwstuttgart.syntaxtree.statement.RecordPattern;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
@ -128,7 +125,6 @@ import de.dhbwstuttgart.syntaxtree.statement.Switch;
import de.dhbwstuttgart.syntaxtree.statement.SwitchBlock;
import de.dhbwstuttgart.syntaxtree.statement.SwitchLabel;
import de.dhbwstuttgart.syntaxtree.statement.This;
import de.dhbwstuttgart.syntaxtree.statement.Pattern;
import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr;
import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
import de.dhbwstuttgart.syntaxtree.statement.Yield;
@ -387,13 +383,13 @@ public class StatementGenerator {
List<SwitchLabel> labels = switch (labeledRule.switchLabelCase()) {
case LabeledRuleExprListContext exprList -> {
List<SwitchLabel> labelList = exprList.expressionList().expression().stream().map((exprctx) -> {
Expression expr = convert(exprctx);
Pattern expr = new ExpressionPattern(convert(exprctx), exprList.getStart());
return new SwitchLabel(expr, expr.getType(), exprList.getStart());
}).toList();
yield labelList;
}
case LabeledRulePatternContext pattern -> {
Expression p = convert(pattern.pattern());
Pattern p = convert(pattern.pattern());
yield Arrays.asList(new SwitchLabel(p, p.getType(), pattern.getStart()));
}
case LabeledRuleDefaultContext def -> {
@ -436,9 +432,9 @@ public class StatementGenerator {
private SwitchLabel convert(SwitchLabelContext switchLabel) {
RefTypeOrTPHOrWildcardOrGeneric type = null;
Expression caseExpression = switch (switchLabel) {
Pattern caseExpression = switch (switchLabel) {
case SwitchLabelConstContext cons -> {
yield convert(cons.constantExpression);
yield new ExpressionPattern(convert(cons.constantExpression), cons.getStart());
}
case SwitchLabelPatternContext pattern -> {
yield convert(pattern.pattern());
@ -459,7 +455,7 @@ public class StatementGenerator {
}
}
private Expression convert(PatternContext pattern) {
private Pattern convert(PatternContext pattern) {
return switch (pattern) {
case PPatternContext pPattern -> {
yield convert(pPattern.primaryPattern());
@ -473,14 +469,14 @@ public class StatementGenerator {
};
}
private Pattern convert(PrimaryPatternContext pPattern) {
private TypePattern convert(PrimaryPatternContext pPattern) {
switch (pPattern) {
case TPatternContext tPattern:
TypePatternContext typePattern = tPattern.typePattern();
var text = typePattern.identifier().getText();
var type = TypeGenerator.convert(typePattern.typeType(), reg, generics);
localVars.put(text, type);
return new Pattern(text, type, typePattern.getStart());
return new TypePattern(text, type, typePattern.getStart());
case RPatternContext rPattern:
RecordPatternContext recordPattern = rPattern.recordPattern();
return convert(recordPattern);
@ -492,8 +488,8 @@ public class StatementGenerator {
private RecordPattern convert(RecordPatternContext recordPatternCtx) {
List<PatternContext> subPatternCtx = recordPatternCtx.recordStructurePattern().recordComponentPatternList().pattern();
List<Pattern> subPattern = subPatternCtx.stream().map((patternCtx) -> {
return (Pattern) convert(patternCtx);
List<TypePattern> subPattern = subPatternCtx.stream().map((patternCtx) -> {
return (TypePattern) convert(patternCtx);
}).collect(Collectors.toList());
IdentifierContext identifierCtx = recordPatternCtx.identifier();
var text = (identifierCtx != null) ? identifierCtx.getText() : null;
@ -892,7 +888,7 @@ public class StatementGenerator {
String localVarName = typePatternCtx.identifier().getText();
RefTypeOrTPHOrWildcardOrGeneric localVarType = TypeGenerator.convert(typePatternCtx.typeType(), reg, generics);
localVars.put(localVarName, localVarType);
return new InstanceOf(left, new Pattern(localVarName, localVarType, typePatternCtx.getStart()), offset);
return new InstanceOf(left, new TypePattern(localVarName, localVarType, typePatternCtx.getStart()), offset);
default:
throw new NotImplementedException();
}

View File

@ -31,4 +31,13 @@ public interface ASTVisitor extends StatementVisitor{
void visit(ExtendsWildcardType extendsWildcardType);
void visit(GenericRefType genericRefType);
void visit(TypePattern aPattern);
void visit(ExpressionPattern aPattern);
void visit(RecordPattern aRecordPattern);
void visit(GuardedPattern aGuardedPattern);
}

View File

@ -305,7 +305,12 @@ public abstract class AbstractASTWalker implements ASTVisitor {
}
@Override
public void visit(Pattern aPattern) {
public void visit(TypePattern aPattern) {
}
@Override
public void visit(ExpressionPattern aPattern) {
}

View File

@ -0,0 +1,23 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token;
public class ExpressionPattern extends Pattern {
private final Expression expression;
public ExpressionPattern(Expression expression, Token offset) {
super(expression.getType(), offset);
this.expression = expression;
}
public Expression getExpression() {
return expression;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -3,18 +3,12 @@ package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token;
public class FormalParameter extends SyntaxTreeNode {
private RefTypeOrTPHOrWildcardOrGeneric type;
public class FormalParameter extends Pattern {
private String name;
public FormalParameter(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(offset);
super(type, offset);
this.name = name;
this.type = type;
}
public RefTypeOrTPHOrWildcardOrGeneric getType() {
return type;
}
public String getName() {

View File

@ -0,0 +1,29 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import org.antlr.v4.runtime.Token;
public class GuardedPattern extends Pattern {
private final Expression condition;
private final TypePattern nested;
public GuardedPattern(Expression condition, TypePattern nested, Token offset) {
super(nested.getType(), offset);
this.condition = condition;
this.nested = nested;
}
public Expression getCondition() {
return condition;
}
public TypePattern getNestedPattern() {
return nested;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -0,0 +1,17 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token;
public abstract class Pattern extends SyntaxTreeNode {
private final RefTypeOrTPHOrWildcardOrGeneric type;
public Pattern(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(offset);
this.type = type;
}
public RefTypeOrTPHOrWildcardOrGeneric getType(){
return type;
}
}

View File

@ -1,36 +1,35 @@
package de.dhbwstuttgart.syntaxtree.statement;
package de.dhbwstuttgart.syntaxtree;
import java.util.ArrayList;
import java.util.List;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class RecordPattern extends Pattern {
public class RecordPattern extends TypePattern {
private List<Pattern> subPattern = new ArrayList<>();
private List<TypePattern> subPattern = new ArrayList<>();
public RecordPattern(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(name, type, offset);
}
public RecordPattern(List<Pattern> subPattern, String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
public RecordPattern(List<TypePattern> subPattern, String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(name, type, offset);
this.subPattern = subPattern;
}
public List<Pattern> getSubPattern() {
public List<TypePattern> getSubPattern() {
return this.subPattern;
}
public void addSubPattern(Pattern newPattern) {
public void addSubPattern(TypePattern newPattern) {
this.subPattern.add(newPattern);
}
@Override
public void accept(StatementVisitor visitor) {
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}

View File

@ -52,13 +52,7 @@ public interface StatementVisitor {
void visit(Break aBreak);
void visit(Yield aYield);
void visit(Pattern aPattern);
void visit(RecordPattern aRecordPattern);
void visit(GuardedPattern aGuardedPattern);
void visit(StaticClassName staticClassName);
void visit(Super aSuper);

View File

@ -0,0 +1,26 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class TypePattern extends Pattern {
private final String name;
public TypePattern(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(type, offset);
this.name = name;
}
public String getName() {
return name;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,33 +0,0 @@
package de.dhbwstuttgart.syntaxtree.statement;
import java.util.List;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class GuardedPattern extends Expression {
private final Expression condition;
private final Pattern nested;
public GuardedPattern(Expression condition, Pattern nested, Token offset) {
super(nested.getType(), offset);
this.condition = condition;
this.nested = nested;
}
public Expression getCondition() {
return condition;
}
public Pattern getNestedPattern() {
return nested;
}
@Override
public void accept(StatementVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.TypePattern;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
@ -7,14 +8,14 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class InstanceOf extends BinaryExpr {
private Pattern pattern;
private TypePattern pattern;
public InstanceOf(Expression expr, RefTypeOrTPHOrWildcardOrGeneric reftype, Token offset) {
super(BinaryExpr.Operator.INSTOF, TypePlaceholder.fresh(offset), expr, new LocalVar("", reftype, reftype.getOffset()), offset);
this.pattern = new Pattern(null, reftype, offset);
this.pattern = new TypePattern(null, reftype, offset);
}
public InstanceOf(Expression expr, Pattern pattern, Token offset) {
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.pattern = pattern;
}
@ -27,7 +28,7 @@ public class InstanceOf extends BinaryExpr {
return pattern.getName();
}
public Pattern gPattern() {
public TypePattern gPattern() {
return pattern;
}

View File

@ -1,26 +0,0 @@
package de.dhbwstuttgart.syntaxtree.statement;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class Pattern extends Expression {
private String name;
public Pattern(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(type, offset);
this.name = name;
}
public String getName() {
return name;
}
@Override
public void accept(StatementVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.Pattern;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
@ -7,12 +8,12 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class SwitchLabel extends Expression {
private Expression caseExpression;
private Pattern pattern;
private Boolean defaultCase = false;
public SwitchLabel(Expression caseExpression, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
public SwitchLabel(Pattern pattern, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(type, offset);
this.caseExpression = caseExpression;
this.pattern = pattern;
}
public SwitchLabel(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
@ -20,8 +21,8 @@ public class SwitchLabel extends Expression {
this.defaultCase = true;
}
public Expression getExpression() {
return caseExpression;
public Pattern getPattern() {
return pattern;
}
public Boolean isDefault() {

View File

@ -439,7 +439,7 @@ public class OutputGenerator implements ASTVisitor {
out.append("default");
} else {
out.append("case ");
switchLabel.getExpression().accept(this);
switchLabel.getPattern().accept(this);
}
out.append(":\n");
}
@ -453,16 +453,23 @@ public class OutputGenerator implements ASTVisitor {
}
@Override
public void visit(Pattern aPattern) {
public void visit(TypePattern aPattern) {
aPattern.getType().accept(this);
out.append(" " + aPattern.getName());
}
@Override
public void visit(ExpressionPattern aPattern) {
aPattern.getType().accept(this);
out.append(" ");
aPattern.getExpression().accept(this);
}
@Override
public void visit(RecordPattern aRecordPattern) {
aRecordPattern.getType().accept(this);
out.append("(");
List<Pattern> subPatterns = aRecordPattern.getSubPattern();
List<TypePattern> subPatterns = aRecordPattern.getSubPattern();
int i;
for (i = 0; i < subPatterns.size() - 1; i++) {
subPatterns.get(i).accept(this);

View File

@ -41,6 +41,12 @@ public class ASTToTargetAST {
return all.stream().map(generics -> new GenericsResult(generics.javaGenerics)).toList();
}
public TargetExpression convert(Pattern pattern) {
var converter = new StatementToTargetExpression(this);
pattern.accept(converter);
return converter.result;
}
record Generics(JavaGenerics javaGenerics, TxGenerics txGenerics) {
}

View File

@ -3,8 +3,9 @@ package de.dhbwstuttgart.target.generate;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.target.tree.MethodParameter;
import de.dhbwstuttgart.target.tree.expression.*;
import de.dhbwstuttgart.target.tree.type.*;
@ -14,7 +15,7 @@ import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class StatementToTargetExpression implements StatementVisitor {
public class StatementToTargetExpression implements ASTVisitor {
public StatementToTargetExpression(ASTToTargetAST converter) {
this.converter = converter;
@ -357,7 +358,7 @@ public class StatementToTargetExpression implements StatementVisitor {
@Override
public void visit(SwitchLabel switchLabel) {
result = converter.convert(switchLabel.getExpression());
result = converter.convert(switchLabel.getPattern());
}
@Override
@ -366,10 +367,85 @@ public class StatementToTargetExpression implements StatementVisitor {
}
@Override
public void visit(Pattern aPattern) {
public void visit(SourceFile sourceFile) {
}
@Override
public void visit(GenericTypeVar genericTypeVar) {
}
@Override
public void visit(FormalParameter formalParameter) {
}
@Override
public void visit(GenericDeclarationList genericTypeVars) {
}
@Override
public void visit(Field field) {
}
@Override
public void visit(de.dhbwstuttgart.syntaxtree.Method field) {
}
@Override
public void visit(Constructor field) {
}
@Override
public void visit(ParameterList formalParameters) {
}
@Override
public void visit(ClassOrInterface classOrInterface) {
}
@Override
public void visit(RefType refType) {
}
@Override
public void visit(SuperWildcardType superWildcardType) {
}
@Override
public void visit(TypePlaceholder typePlaceholder) {
}
@Override
public void visit(ExtendsWildcardType extendsWildcardType) {
}
@Override
public void visit(GenericRefType genericRefType) {
}
@Override
public void visit(TypePattern aPattern) {
result = new SimplePattern(converter.convert(aPattern.getType()), aPattern.getName());
}
@Override
public void visit(ExpressionPattern aPattern) {
result = converter.convert(aPattern.getExpression());
}
@Override
public void visit(RecordPattern aRecordPattern) {
result = new ComplexPattern(

View File

@ -1,6 +1,9 @@
package de.dhbwstuttgart.target.generate;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.syntaxtree.GuardedPattern;
import de.dhbwstuttgart.syntaxtree.TypePattern;
import de.dhbwstuttgart.syntaxtree.RecordPattern;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.statement.*;
@ -186,19 +189,4 @@ public abstract class TracingStatementVisitor implements StatementVisitor {
public void visit(Yield aYield) {
}
@Override
public void visit(Pattern aPattern) {
}
@Override
public void visit(RecordPattern aRecordPattern) {
}
@Override
public void visit(GuardedPattern aGuardedPattern) {
}
}

View File

@ -31,7 +31,7 @@ import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt;
import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
import de.dhbwstuttgart.syntaxtree.statement.ForStmt;
import de.dhbwstuttgart.syntaxtree.statement.GuardedPattern;
import de.dhbwstuttgart.syntaxtree.GuardedPattern;
import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
@ -41,8 +41,8 @@ import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.statement.NewArray;
import de.dhbwstuttgart.syntaxtree.statement.NewClass;
import de.dhbwstuttgart.syntaxtree.statement.Pattern;
import de.dhbwstuttgart.syntaxtree.statement.RecordPattern;
import de.dhbwstuttgart.syntaxtree.TypePattern;
import de.dhbwstuttgart.syntaxtree.RecordPattern;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
@ -739,7 +739,7 @@ public class TYPEStmt implements StatementVisitor {
RefTypeOrTPHOrWildcardOrGeneric caseExpressionType = null;
for (var child : switchStmt.getBlocks()) {
for (var label : child.getLabels()) {
if (!(label.getExpression() instanceof Pattern) && !(label.getType() instanceof Void)) {
if (!(label.getPattern() instanceof TypePattern) && !(label.getType() instanceof Void)) {
if (caseExpressionType != null && !caseExpressionType.equals(label.getType()))
throw new TypeinferenceException("Case labels must all have the same Type if they are expressions", label);
caseExpressionType = label.getType();
@ -749,8 +749,8 @@ public class TYPEStmt implements StatementVisitor {
if (caseExpressionType == null) {
for (var child : switchStmt.getBlocks()) {
for (var label : child.getLabels()) {
if (label.getExpression() instanceof Pattern) {
constraintsSet.addUndConstraint(new Pair(label.getExpression().getType(), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT));
if (label.getPattern() instanceof TypePattern) {
constraintsSet.addUndConstraint(new Pair(label.getPattern().getType(), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT));
}
}
@ -782,20 +782,4 @@ public class TYPEStmt implements StatementVisitor {
constraintsSet.addUndConstraint(new Pair(aYield.getType(), switchStack.peek().getType(), PairOperator.EQUALSDOT));
// TODO Auto-generated method stub
}
@Override
public void visit(Pattern aPattern) {
// TODO Auto-generated method stub
}
@Override
public void visit(RecordPattern aRecordPattern) {
// TODO Auto-generated method stub
}
@Override
public void visit(GuardedPattern aGuardedPattern) {
// TODO Auto-generated method stub
}
}