Remove TypePattern and replace it with FormalParameter

This commit is contained in:
Daniel Holle 2023-08-17 16:25:19 +02:00
parent 011b272af6
commit bffc7f9f1c
11 changed files with 22 additions and 73 deletions

View File

@ -469,14 +469,14 @@ public class StatementGenerator {
};
}
private TypePattern convert(PrimaryPatternContext pPattern) {
private FormalParameter 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 TypePattern(text, type, typePattern.getStart());
return new FormalParameter(text, type, typePattern.getStart());
case RPatternContext rPattern:
RecordPatternContext recordPattern = rPattern.recordPattern();
return convert(recordPattern);
@ -488,9 +488,7 @@ public class StatementGenerator {
private RecordPattern convert(RecordPatternContext recordPatternCtx) {
List<PatternContext> subPatternCtx = recordPatternCtx.recordStructurePattern().recordComponentPatternList().pattern();
List<TypePattern> subPattern = subPatternCtx.stream().map((patternCtx) -> {
return (TypePattern) convert(patternCtx);
}).collect(Collectors.toList());
List<Pattern> subPattern = subPatternCtx.stream().map(this::convert).collect(Collectors.toList());
IdentifierContext identifierCtx = recordPatternCtx.identifier();
var text = (identifierCtx != null) ? identifierCtx.getText() : null;
var type = TypeGenerator.convert(recordPatternCtx.typeType(), reg, generics);
@ -888,7 +886,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 TypePattern(localVarName, localVarType, typePatternCtx.getStart()), offset);
return new InstanceOf(left, new FormalParameter(localVarName, localVarType, typePatternCtx.getStart()), offset);
default:
throw new NotImplementedException();
}

View File

@ -31,8 +31,6 @@ public interface ASTVisitor extends StatementVisitor{
void visit(ExtendsWildcardType extendsWildcardType);
void visit(GenericRefType genericRefType);
void visit(TypePattern aPattern);
void visit(ExpressionPattern aPattern);

View File

@ -304,11 +304,6 @@ public abstract class AbstractASTWalker implements ASTVisitor {
aYield.accept(this);
}
@Override
public void visit(TypePattern aPattern) {
}
@Override
public void visit(ExpressionPattern aPattern) {

View File

@ -6,9 +6,9 @@ import org.antlr.v4.runtime.Token;
public class GuardedPattern extends Pattern {
private final Expression condition;
private final TypePattern nested;
private final Pattern nested;
public GuardedPattern(Expression condition, TypePattern nested, Token offset) {
public GuardedPattern(Expression condition, Pattern nested, Token offset) {
super(nested.getType(), offset);
this.condition = condition;
this.nested = nested;
@ -18,7 +18,7 @@ public class GuardedPattern extends Pattern {
return condition;
}
public TypePattern getNestedPattern() {
public Pattern getNestedPattern() {
return nested;
}

View File

@ -7,24 +7,24 @@ import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class RecordPattern extends TypePattern {
public class RecordPattern extends FormalParameter {
private List<TypePattern> subPattern = new ArrayList<>();
private List<Pattern> subPattern = new ArrayList<>();
public RecordPattern(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(name, type, offset);
}
public RecordPattern(List<TypePattern> subPattern, String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
public RecordPattern(List<Pattern> subPattern, String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
super(name, type, offset);
this.subPattern = subPattern;
}
public List<TypePattern> getSubPattern() {
public List<Pattern> getSubPattern() {
return this.subPattern;
}
public void addSubPattern(TypePattern newPattern) {
public void addSubPattern(Pattern newPattern) {
this.subPattern.add(newPattern);
}

View File

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

View File

@ -452,12 +452,6 @@ public class OutputGenerator implements ASTVisitor {
aYield.getType().accept(this);
}
@Override
public void visit(TypePattern aPattern) {
aPattern.getType().accept(this);
out.append(" " + aPattern.getName());
}
@Override
public void visit(ExpressionPattern aPattern) {
aPattern.getType().accept(this);
@ -469,7 +463,7 @@ public class OutputGenerator implements ASTVisitor {
public void visit(RecordPattern aRecordPattern) {
aRecordPattern.getType().accept(this);
out.append("(");
List<TypePattern> subPatterns = aRecordPattern.getSubPattern();
List<Pattern> subPatterns = aRecordPattern.getSubPattern();
int i;
for (i = 0; i < subPatterns.size() - 1; i++) {
subPatterns.get(i).accept(this);

View File

@ -376,11 +376,6 @@ public class StatementToTargetExpression implements ASTVisitor {
}
@Override
public void visit(FormalParameter formalParameter) {
}
@Override
public void visit(GenericDeclarationList genericTypeVars) {
@ -437,7 +432,7 @@ public class StatementToTargetExpression implements ASTVisitor {
}
@Override
public void visit(TypePattern aPattern) {
public void visit(FormalParameter aPattern) {
result = new SimplePattern(converter.convert(aPattern.getType()), aPattern.getName());
}

View File

@ -1,9 +1,6 @@
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.*;

View File

@ -31,7 +31,6 @@ 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.GuardedPattern;
import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
@ -41,8 +40,6 @@ 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.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 +736,7 @@ public class TYPEStmt implements StatementVisitor {
RefTypeOrTPHOrWildcardOrGeneric caseExpressionType = null;
for (var child : switchStmt.getBlocks()) {
for (var label : child.getLabels()) {
if (!(label.getPattern() instanceof TypePattern) && !(label.getType() instanceof Void)) {
if (!(label.getPattern() instanceof FormalParameter) && !(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,7 +746,7 @@ public class TYPEStmt implements StatementVisitor {
if (caseExpressionType == null) {
for (var child : switchStmt.getBlocks()) {
for (var label : child.getLabels()) {
if (label.getPattern() instanceof TypePattern) {
if (label.getPattern() instanceof FormalParameter) {
constraintsSet.addUndConstraint(new Pair(label.getPattern().getType(), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT));
}
}