From ee359da751a93e54db2545d784dc97f7bccd67be Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Thu, 17 Aug 2023 15:56:16 +0200 Subject: [PATCH] Refactor Patterns --- .../StatementGenerator.java | 26 +++--- .../dhbwstuttgart/syntaxtree/ASTVisitor.java | 9 ++ .../syntaxtree/AbstractASTWalker.java | 7 +- .../syntaxtree/ExpressionPattern.java | 23 +++++ .../syntaxtree/FormalParameter.java | 10 +-- .../syntaxtree/GuardedPattern.java | 29 +++++++ .../de/dhbwstuttgart/syntaxtree/Pattern.java | 17 ++++ .../{statement => }/RecordPattern.java | 15 ++-- .../syntaxtree/StatementVisitor.java | 8 +- .../dhbwstuttgart/syntaxtree/TypePattern.java | 26 ++++++ .../syntaxtree/statement/GuardedPattern.java | 33 -------- .../syntaxtree/statement/InstanceOf.java | 9 +- .../syntaxtree/statement/Pattern.java | 26 ------ .../syntaxtree/statement/SwitchLabel.java | 11 +-- .../syntaxtree/visual/OutputGenerator.java | 13 ++- .../target/generate/ASTToTargetAST.java | 6 ++ .../generate/StatementToTargetExpression.java | 84 ++++++++++++++++++- .../generate/TracingStatementVisitor.java | 18 +--- .../typeinference/typeAlgo/TYPEStmt.java | 28 ++----- 19 files changed, 247 insertions(+), 151 deletions(-) create mode 100644 src/main/java/de/dhbwstuttgart/syntaxtree/ExpressionPattern.java create mode 100644 src/main/java/de/dhbwstuttgart/syntaxtree/GuardedPattern.java create mode 100644 src/main/java/de/dhbwstuttgart/syntaxtree/Pattern.java rename src/main/java/de/dhbwstuttgart/syntaxtree/{statement => }/RecordPattern.java (51%) create mode 100644 src/main/java/de/dhbwstuttgart/syntaxtree/TypePattern.java delete mode 100644 src/main/java/de/dhbwstuttgart/syntaxtree/statement/GuardedPattern.java delete mode 100644 src/main/java/de/dhbwstuttgart/syntaxtree/statement/Pattern.java diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 44834645..e15cc721 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -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 labels = switch (labeledRule.switchLabelCase()) { case LabeledRuleExprListContext exprList -> { List 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 subPatternCtx = recordPatternCtx.recordStructurePattern().recordComponentPatternList().pattern(); - List subPattern = subPatternCtx.stream().map((patternCtx) -> { - return (Pattern) convert(patternCtx); + List 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(); } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/ASTVisitor.java b/src/main/java/de/dhbwstuttgart/syntaxtree/ASTVisitor.java index 5f77601d..508d08e0 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/ASTVisitor.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/ASTVisitor.java @@ -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); + } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index 26754b30..58c5ab04 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -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) { } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/ExpressionPattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/ExpressionPattern.java new file mode 100644 index 00000000..5cdb2995 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/ExpressionPattern.java @@ -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); + } +} diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/FormalParameter.java b/src/main/java/de/dhbwstuttgart/syntaxtree/FormalParameter.java index 58291293..eac04f2b 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/FormalParameter.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/FormalParameter.java @@ -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() { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/GuardedPattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/GuardedPattern.java new file mode 100644 index 00000000..c26def76 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/GuardedPattern.java @@ -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); + } +} diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/Pattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/Pattern.java new file mode 100644 index 00000000..22441b36 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/Pattern.java @@ -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; + } +} diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/RecordPattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/RecordPattern.java similarity index 51% rename from src/main/java/de/dhbwstuttgart/syntaxtree/statement/RecordPattern.java rename to src/main/java/de/dhbwstuttgart/syntaxtree/RecordPattern.java index 64b6d22b..ca912f73 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/RecordPattern.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/RecordPattern.java @@ -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 subPattern = new ArrayList<>(); + private List subPattern = new ArrayList<>(); public RecordPattern(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { super(name, type, offset); } - public RecordPattern(List subPattern, String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { + public RecordPattern(List subPattern, String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { super(name, type, offset); this.subPattern = subPattern; } - public List getSubPattern() { + public List 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); } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/StatementVisitor.java b/src/main/java/de/dhbwstuttgart/syntaxtree/StatementVisitor.java index 4361d703..737e984e 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/StatementVisitor.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/StatementVisitor.java @@ -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); 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..8de43b06 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/TypePattern.java @@ -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); + } + +} diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/GuardedPattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/GuardedPattern.java deleted file mode 100644 index 9e7bb485..00000000 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/GuardedPattern.java +++ /dev/null @@ -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); - } -} diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java index 55d5e6da..db5ce52e 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java @@ -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; } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Pattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Pattern.java deleted file mode 100644 index 50a63ea3..00000000 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Pattern.java +++ /dev/null @@ -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); - } - -} diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchLabel.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchLabel.java index fd94e8c3..0ba92819 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchLabel.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchLabel.java @@ -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() { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 26c3dc8c..d9f0cc54 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -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 subPatterns = aRecordPattern.getSubPattern(); + List subPatterns = aRecordPattern.getSubPattern(); int i; for (i = 0; i < subPatterns.size() - 1; i++) { subPatterns.get(i).accept(this); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 377a89fd..fa63b7a7 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -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) { } diff --git a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java index a800410a..300f1f6e 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java @@ -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( diff --git a/src/main/java/de/dhbwstuttgart/target/generate/TracingStatementVisitor.java b/src/main/java/de/dhbwstuttgart/target/generate/TracingStatementVisitor.java index 4c0bd88e..e1a9dd43 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/TracingStatementVisitor.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/TracingStatementVisitor.java @@ -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) { - - } } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index e7003adc..dc679eb1 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -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 - } - }