forked from JavaTX/JavaCompilerCore
Refactor Patterns
This commit is contained in:
parent
6025e17186
commit
ee359da751
@ -8,6 +8,7 @@ import java.util.Map;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.syntaxtree.*;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.Void;
|
import de.dhbwstuttgart.syntaxtree.type.Void;
|
||||||
import org.antlr.v4.runtime.Token;
|
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.GenericsRegistry;
|
||||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||||
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
|
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.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;
|
||||||
@ -108,7 +107,6 @@ import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
|
|||||||
import de.dhbwstuttgart.syntaxtree.statement.Expression;
|
import de.dhbwstuttgart.syntaxtree.statement.Expression;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
|
import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
|
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.GuardedPattern;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
|
import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
|
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
|
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.MethodCall;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.NewClass;
|
import de.dhbwstuttgart.syntaxtree.statement.NewClass;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Receiver;
|
import de.dhbwstuttgart.syntaxtree.statement.Receiver;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.RecordPattern;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Return;
|
import de.dhbwstuttgart.syntaxtree.statement.Return;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
|
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Statement;
|
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.SwitchBlock;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.SwitchLabel;
|
import de.dhbwstuttgart.syntaxtree.statement.SwitchLabel;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.This;
|
import de.dhbwstuttgart.syntaxtree.statement.This;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Pattern;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr;
|
import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
|
import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Yield;
|
import de.dhbwstuttgart.syntaxtree.statement.Yield;
|
||||||
@ -387,13 +383,13 @@ public class StatementGenerator {
|
|||||||
List<SwitchLabel> labels = switch (labeledRule.switchLabelCase()) {
|
List<SwitchLabel> labels = switch (labeledRule.switchLabelCase()) {
|
||||||
case LabeledRuleExprListContext exprList -> {
|
case LabeledRuleExprListContext exprList -> {
|
||||||
List<SwitchLabel> labelList = exprList.expressionList().expression().stream().map((exprctx) -> {
|
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());
|
return new SwitchLabel(expr, expr.getType(), exprList.getStart());
|
||||||
}).toList();
|
}).toList();
|
||||||
yield labelList;
|
yield labelList;
|
||||||
}
|
}
|
||||||
case LabeledRulePatternContext pattern -> {
|
case LabeledRulePatternContext pattern -> {
|
||||||
Expression p = convert(pattern.pattern());
|
Pattern p = convert(pattern.pattern());
|
||||||
yield Arrays.asList(new SwitchLabel(p, p.getType(), pattern.getStart()));
|
yield Arrays.asList(new SwitchLabel(p, p.getType(), pattern.getStart()));
|
||||||
}
|
}
|
||||||
case LabeledRuleDefaultContext def -> {
|
case LabeledRuleDefaultContext def -> {
|
||||||
@ -436,9 +432,9 @@ public class StatementGenerator {
|
|||||||
|
|
||||||
private SwitchLabel convert(SwitchLabelContext switchLabel) {
|
private SwitchLabel convert(SwitchLabelContext switchLabel) {
|
||||||
RefTypeOrTPHOrWildcardOrGeneric type = null;
|
RefTypeOrTPHOrWildcardOrGeneric type = null;
|
||||||
Expression caseExpression = switch (switchLabel) {
|
Pattern caseExpression = switch (switchLabel) {
|
||||||
case SwitchLabelConstContext cons -> {
|
case SwitchLabelConstContext cons -> {
|
||||||
yield convert(cons.constantExpression);
|
yield new ExpressionPattern(convert(cons.constantExpression), cons.getStart());
|
||||||
}
|
}
|
||||||
case SwitchLabelPatternContext pattern -> {
|
case SwitchLabelPatternContext pattern -> {
|
||||||
yield convert(pattern.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) {
|
return switch (pattern) {
|
||||||
case PPatternContext pPattern -> {
|
case PPatternContext pPattern -> {
|
||||||
yield convert(pPattern.primaryPattern());
|
yield convert(pPattern.primaryPattern());
|
||||||
@ -473,14 +469,14 @@ public class StatementGenerator {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private Pattern convert(PrimaryPatternContext pPattern) {
|
private TypePattern convert(PrimaryPatternContext pPattern) {
|
||||||
switch (pPattern) {
|
switch (pPattern) {
|
||||||
case TPatternContext tPattern:
|
case TPatternContext tPattern:
|
||||||
TypePatternContext typePattern = tPattern.typePattern();
|
TypePatternContext typePattern = tPattern.typePattern();
|
||||||
var text = typePattern.identifier().getText();
|
var text = typePattern.identifier().getText();
|
||||||
var type = TypeGenerator.convert(typePattern.typeType(), reg, generics);
|
var type = TypeGenerator.convert(typePattern.typeType(), reg, generics);
|
||||||
localVars.put(text, type);
|
localVars.put(text, type);
|
||||||
return new Pattern(text, type, typePattern.getStart());
|
return new TypePattern(text, type, typePattern.getStart());
|
||||||
case RPatternContext rPattern:
|
case RPatternContext rPattern:
|
||||||
RecordPatternContext recordPattern = rPattern.recordPattern();
|
RecordPatternContext recordPattern = rPattern.recordPattern();
|
||||||
return convert(recordPattern);
|
return convert(recordPattern);
|
||||||
@ -492,8 +488,8 @@ public class StatementGenerator {
|
|||||||
|
|
||||||
private RecordPattern convert(RecordPatternContext recordPatternCtx) {
|
private RecordPattern convert(RecordPatternContext recordPatternCtx) {
|
||||||
List<PatternContext> subPatternCtx = recordPatternCtx.recordStructurePattern().recordComponentPatternList().pattern();
|
List<PatternContext> subPatternCtx = recordPatternCtx.recordStructurePattern().recordComponentPatternList().pattern();
|
||||||
List<Pattern> subPattern = subPatternCtx.stream().map((patternCtx) -> {
|
List<TypePattern> subPattern = subPatternCtx.stream().map((patternCtx) -> {
|
||||||
return (Pattern) convert(patternCtx);
|
return (TypePattern) convert(patternCtx);
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
IdentifierContext identifierCtx = recordPatternCtx.identifier();
|
IdentifierContext identifierCtx = recordPatternCtx.identifier();
|
||||||
var text = (identifierCtx != null) ? identifierCtx.getText() : null;
|
var text = (identifierCtx != null) ? identifierCtx.getText() : null;
|
||||||
@ -892,7 +888,7 @@ public class StatementGenerator {
|
|||||||
String localVarName = typePatternCtx.identifier().getText();
|
String localVarName = typePatternCtx.identifier().getText();
|
||||||
RefTypeOrTPHOrWildcardOrGeneric localVarType = TypeGenerator.convert(typePatternCtx.typeType(), reg, generics);
|
RefTypeOrTPHOrWildcardOrGeneric localVarType = TypeGenerator.convert(typePatternCtx.typeType(), reg, generics);
|
||||||
localVars.put(localVarName, localVarType);
|
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:
|
default:
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
@ -31,4 +31,13 @@ public interface ASTVisitor extends StatementVisitor{
|
|||||||
void visit(ExtendsWildcardType extendsWildcardType);
|
void visit(ExtendsWildcardType extendsWildcardType);
|
||||||
|
|
||||||
void visit(GenericRefType genericRefType);
|
void visit(GenericRefType genericRefType);
|
||||||
|
|
||||||
|
void visit(TypePattern aPattern);
|
||||||
|
|
||||||
|
void visit(ExpressionPattern aPattern);
|
||||||
|
|
||||||
|
void visit(RecordPattern aRecordPattern);
|
||||||
|
|
||||||
|
void visit(GuardedPattern aGuardedPattern);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -305,7 +305,12 @@ public abstract class AbstractASTWalker implements ASTVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(Pattern aPattern) {
|
public void visit(TypePattern aPattern) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ExpressionPattern aPattern) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -3,18 +3,12 @@ package de.dhbwstuttgart.syntaxtree;
|
|||||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
|
||||||
public class FormalParameter extends SyntaxTreeNode {
|
public class FormalParameter extends Pattern {
|
||||||
private RefTypeOrTPHOrWildcardOrGeneric type;
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
public FormalParameter(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
|
public FormalParameter(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
|
||||||
super(offset);
|
super(type, offset);
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public RefTypeOrTPHOrWildcardOrGeneric getType() {
|
|
||||||
return type;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
17
src/main/java/de/dhbwstuttgart/syntaxtree/Pattern.java
Normal file
17
src/main/java/de/dhbwstuttgart/syntaxtree/Pattern.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,36 +1,35 @@
|
|||||||
package de.dhbwstuttgart.syntaxtree.statement;
|
package de.dhbwstuttgart.syntaxtree;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
|
||||||
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
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) {
|
public RecordPattern(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
|
||||||
super(name, type, 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);
|
super(name, type, offset);
|
||||||
this.subPattern = subPattern;
|
this.subPattern = subPattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Pattern> getSubPattern() {
|
public List<TypePattern> getSubPattern() {
|
||||||
return this.subPattern;
|
return this.subPattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSubPattern(Pattern newPattern) {
|
public void addSubPattern(TypePattern newPattern) {
|
||||||
this.subPattern.add(newPattern);
|
this.subPattern.add(newPattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(StatementVisitor visitor) {
|
public void accept(ASTVisitor visitor) {
|
||||||
visitor.visit(this);
|
visitor.visit(this);
|
||||||
}
|
}
|
||||||
|
|
@ -52,13 +52,7 @@ public interface StatementVisitor {
|
|||||||
void visit(Break aBreak);
|
void visit(Break aBreak);
|
||||||
|
|
||||||
void visit(Yield aYield);
|
void visit(Yield aYield);
|
||||||
|
|
||||||
void visit(Pattern aPattern);
|
|
||||||
|
|
||||||
void visit(RecordPattern aRecordPattern);
|
|
||||||
|
|
||||||
void visit(GuardedPattern aGuardedPattern);
|
|
||||||
|
|
||||||
void visit(StaticClassName staticClassName);
|
void visit(StaticClassName staticClassName);
|
||||||
|
|
||||||
void visit(Super aSuper);
|
void visit(Super aSuper);
|
||||||
|
26
src/main/java/de/dhbwstuttgart/syntaxtree/TypePattern.java
Normal file
26
src/main/java/de/dhbwstuttgart/syntaxtree/TypePattern.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,6 @@
|
|||||||
package de.dhbwstuttgart.syntaxtree.statement;
|
package de.dhbwstuttgart.syntaxtree.statement;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.syntaxtree.TypePattern;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
|
||||||
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
|
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
|
||||||
@ -7,14 +8,14 @@ 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 Pattern pattern;
|
private TypePattern pattern;
|
||||||
|
|
||||||
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.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);
|
super(BinaryExpr.Operator.INSTOF, TypePlaceholder.fresh(offset), expr, new LocalVar(pattern.getName(), pattern.getType(), pattern.getOffset()), offset);
|
||||||
this.pattern = pattern;
|
this.pattern = pattern;
|
||||||
}
|
}
|
||||||
@ -27,7 +28,7 @@ public class InstanceOf extends BinaryExpr {
|
|||||||
return pattern.getName();
|
return pattern.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pattern gPattern() {
|
public TypePattern gPattern() {
|
||||||
return pattern;
|
return pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,5 +1,6 @@
|
|||||||
package de.dhbwstuttgart.syntaxtree.statement;
|
package de.dhbwstuttgart.syntaxtree.statement;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.syntaxtree.Pattern;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
|
||||||
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
|
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
|
||||||
@ -7,12 +8,12 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
|||||||
|
|
||||||
public class SwitchLabel extends Expression {
|
public class SwitchLabel extends Expression {
|
||||||
|
|
||||||
private Expression caseExpression;
|
private Pattern pattern;
|
||||||
private Boolean defaultCase = false;
|
private Boolean defaultCase = false;
|
||||||
|
|
||||||
public SwitchLabel(Expression caseExpression, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
|
public SwitchLabel(Pattern pattern, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
|
||||||
super(type, offset);
|
super(type, offset);
|
||||||
this.caseExpression = caseExpression;
|
this.pattern = pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SwitchLabel(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
|
public SwitchLabel(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
|
||||||
@ -20,8 +21,8 @@ public class SwitchLabel extends Expression {
|
|||||||
this.defaultCase = true;
|
this.defaultCase = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Expression getExpression() {
|
public Pattern getPattern() {
|
||||||
return caseExpression;
|
return pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean isDefault() {
|
public Boolean isDefault() {
|
||||||
|
@ -439,7 +439,7 @@ public class OutputGenerator implements ASTVisitor {
|
|||||||
out.append("default");
|
out.append("default");
|
||||||
} else {
|
} else {
|
||||||
out.append("case ");
|
out.append("case ");
|
||||||
switchLabel.getExpression().accept(this);
|
switchLabel.getPattern().accept(this);
|
||||||
}
|
}
|
||||||
out.append(":\n");
|
out.append(":\n");
|
||||||
}
|
}
|
||||||
@ -453,16 +453,23 @@ public class OutputGenerator implements ASTVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(Pattern aPattern) {
|
public void visit(TypePattern aPattern) {
|
||||||
aPattern.getType().accept(this);
|
aPattern.getType().accept(this);
|
||||||
out.append(" " + aPattern.getName());
|
out.append(" " + aPattern.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ExpressionPattern aPattern) {
|
||||||
|
aPattern.getType().accept(this);
|
||||||
|
out.append(" ");
|
||||||
|
aPattern.getExpression().accept(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(RecordPattern aRecordPattern) {
|
public void visit(RecordPattern aRecordPattern) {
|
||||||
aRecordPattern.getType().accept(this);
|
aRecordPattern.getType().accept(this);
|
||||||
out.append("(");
|
out.append("(");
|
||||||
List<Pattern> subPatterns = aRecordPattern.getSubPattern();
|
List<TypePattern> subPatterns = aRecordPattern.getSubPattern();
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < subPatterns.size() - 1; i++) {
|
for (i = 0; i < subPatterns.size() - 1; i++) {
|
||||||
subPatterns.get(i).accept(this);
|
subPatterns.get(i).accept(this);
|
||||||
|
@ -41,6 +41,12 @@ public class ASTToTargetAST {
|
|||||||
return all.stream().map(generics -> new GenericsResult(generics.javaGenerics)).toList();
|
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) {
|
record Generics(JavaGenerics javaGenerics, TxGenerics txGenerics) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,8 +3,9 @@ package de.dhbwstuttgart.target.generate;
|
|||||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||||
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
|
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
|
||||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||||
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
|
import de.dhbwstuttgart.syntaxtree.*;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.*;
|
import de.dhbwstuttgart.syntaxtree.statement.*;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.*;
|
||||||
import de.dhbwstuttgart.target.tree.MethodParameter;
|
import de.dhbwstuttgart.target.tree.MethodParameter;
|
||||||
import de.dhbwstuttgart.target.tree.expression.*;
|
import de.dhbwstuttgart.target.tree.expression.*;
|
||||||
import de.dhbwstuttgart.target.tree.type.*;
|
import de.dhbwstuttgart.target.tree.type.*;
|
||||||
@ -14,7 +15,7 @@ import java.util.*;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import java.util.stream.StreamSupport;
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
public class StatementToTargetExpression implements StatementVisitor {
|
public class StatementToTargetExpression implements ASTVisitor {
|
||||||
|
|
||||||
public StatementToTargetExpression(ASTToTargetAST converter) {
|
public StatementToTargetExpression(ASTToTargetAST converter) {
|
||||||
this.converter = converter;
|
this.converter = converter;
|
||||||
@ -357,7 +358,7 @@ public class StatementToTargetExpression implements StatementVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(SwitchLabel switchLabel) {
|
public void visit(SwitchLabel switchLabel) {
|
||||||
result = converter.convert(switchLabel.getExpression());
|
result = converter.convert(switchLabel.getPattern());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -366,10 +367,85 @@ public class StatementToTargetExpression implements StatementVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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());
|
result = new SimplePattern(converter.convert(aPattern.getType()), aPattern.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ExpressionPattern aPattern) {
|
||||||
|
result = converter.convert(aPattern.getExpression());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(RecordPattern aRecordPattern) {
|
public void visit(RecordPattern aRecordPattern) {
|
||||||
result = new ComplexPattern(
|
result = new ComplexPattern(
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package de.dhbwstuttgart.target.generate;
|
package de.dhbwstuttgart.target.generate;
|
||||||
|
|
||||||
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
|
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.StatementVisitor;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.*;
|
import de.dhbwstuttgart.syntaxtree.statement.*;
|
||||||
|
|
||||||
@ -186,19 +189,4 @@ public abstract class TracingStatementVisitor implements StatementVisitor {
|
|||||||
public void visit(Yield aYield) {
|
public void visit(Yield aYield) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(Pattern aPattern) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(RecordPattern aRecordPattern) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(GuardedPattern aGuardedPattern) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt;
|
|||||||
import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
|
import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
|
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.ForStmt;
|
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.IfStmt;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
|
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
|
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.MethodCall;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.NewArray;
|
import de.dhbwstuttgart.syntaxtree.statement.NewArray;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.NewClass;
|
import de.dhbwstuttgart.syntaxtree.statement.NewClass;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Pattern;
|
import de.dhbwstuttgart.syntaxtree.TypePattern;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.RecordPattern;
|
import de.dhbwstuttgart.syntaxtree.RecordPattern;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Return;
|
import de.dhbwstuttgart.syntaxtree.statement.Return;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
|
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Statement;
|
import de.dhbwstuttgart.syntaxtree.statement.Statement;
|
||||||
@ -739,7 +739,7 @@ public class TYPEStmt implements StatementVisitor {
|
|||||||
RefTypeOrTPHOrWildcardOrGeneric caseExpressionType = null;
|
RefTypeOrTPHOrWildcardOrGeneric caseExpressionType = null;
|
||||||
for (var child : switchStmt.getBlocks()) {
|
for (var child : switchStmt.getBlocks()) {
|
||||||
for (var label : child.getLabels()) {
|
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()))
|
if (caseExpressionType != null && !caseExpressionType.equals(label.getType()))
|
||||||
throw new TypeinferenceException("Case labels must all have the same Type if they are expressions", label);
|
throw new TypeinferenceException("Case labels must all have the same Type if they are expressions", label);
|
||||||
caseExpressionType = label.getType();
|
caseExpressionType = label.getType();
|
||||||
@ -749,8 +749,8 @@ public class TYPEStmt implements StatementVisitor {
|
|||||||
if (caseExpressionType == null) {
|
if (caseExpressionType == null) {
|
||||||
for (var child : switchStmt.getBlocks()) {
|
for (var child : switchStmt.getBlocks()) {
|
||||||
for (var label : child.getLabels()) {
|
for (var label : child.getLabels()) {
|
||||||
if (label.getExpression() instanceof Pattern) {
|
if (label.getPattern() instanceof TypePattern) {
|
||||||
constraintsSet.addUndConstraint(new Pair(label.getExpression().getType(), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT));
|
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));
|
constraintsSet.addUndConstraint(new Pair(aYield.getType(), switchStack.peek().getType(), PairOperator.EQUALSDOT));
|
||||||
// TODO Auto-generated method stub
|
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user