Created RecordPattern & started development with test cases

This commit is contained in:
luca9913 2023-07-06 22:15:40 +02:00
parent 359f3e68ab
commit 492cbe48e9
6 changed files with 78 additions and 12 deletions

View File

@ -0,0 +1,19 @@
import java.lang.String;
record Point(int x, int y) {}
interface Shape {}
record ColoredPoint(Point pt, String color) {}
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) implements Shape {}
sealed class Color permits Blue, Red {
}
class PatternMatching {
void printColorOfUpperLeftPoint(Shape shape)
{
switch (shape) {
case Rectangle(ColoredPoint(Point pt, String color), ColoredPoint lowerRight) -> System.out.println("x: " + pt.x() + " / color: " + color + " / lowerRight: " + lowerRight);
default -> System.out.println("not a rectangle");
}
}
}

View File

@ -1,6 +1,6 @@
import java.lang.Integer; import java.lang.Integer;
import java.lang.String; import java.lang.String;
import java.lang.Boolean; import java.lang.Object;
class SwitchStatement { class SwitchStatement {
@ -30,12 +30,12 @@ class SwitchStatement {
} }
} }
/* boolean enclosedPattern(){ record Coordinates(double x, double y) {}
var i = "Test";
var j = switch(i){ boolean recordPattern(Object obj){
case (String s)->{ yield 0;} switch(obj){
case Integer i ->{ yield 1;} case Coordinates(double lat, double lon): return true;
}; default: return false;
return (j==0); }
} */ }
} }

View File

@ -61,6 +61,8 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryPatternContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimarySuperContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimarySuperContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryThisContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryThisContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.RPatternContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.RecordPatternContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.RelationalexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.RelationalexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ReturnstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ReturnstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SemistmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SemistmtContext;
@ -108,6 +110,7 @@ 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;
@ -409,12 +412,24 @@ public class StatementGenerator {
case TPatternContext tPattern: case TPatternContext tPattern:
TypePatternContext typePattern = tPattern.typePattern(); TypePatternContext typePattern = tPattern.typePattern();
return new Pattern(typePattern.identifier().getText(), TypeGenerator.convert(typePattern.typeType(), reg, generics), typePattern.getStart()); return new Pattern(typePattern.identifier().getText(), TypeGenerator.convert(typePattern.typeType(), reg, generics), typePattern.getStart());
case RPatternContext rPattern:
RecordPatternContext recordPattern = rPattern.recordPattern();
return convert(recordPattern);
default: default:
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
private RecordPattern convert(RecordPatternContext recordPatternCtx) {
List<PatternContext> subPatternCtx = recordPatternCtx.recordStructurePattern().recordComponentPatternList().pattern();
List<Pattern> subPattern = subPatternCtx.stream().map((patternCtx) -> {
return convert(patternCtx);
}).collect(Collectors.toList());
IdentifierContext identifierCtx = recordPatternCtx.identifier();
return new RecordPattern(subPattern, (identifierCtx != null) ? identifierCtx.getText() : null, TypeGenerator.convert(recordPatternCtx.typeType(), reg, generics), recordPatternCtx.getStart());
}
private Statement convert(Java17Parser.WhileloopContext stmt) { private Statement convert(Java17Parser.WhileloopContext stmt) {
Expression expr = convert(stmt.parExpression().expression()); Expression expr = convert(stmt.parExpression().expression());
Statement block = convert(stmt.statement()); Statement block = convert(stmt.statement());

View File

@ -65,6 +65,8 @@ public class TypeGenerator {
return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), typeContext.getStart()); return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), typeContext.getStart());
case "int": case "int":
return new RefType(ASTFactory.createClass(Integer.class).getClassName(), typeContext.getStart()); return new RefType(ASTFactory.createClass(Integer.class).getClassName(), typeContext.getStart());
case "double":
return new RefType(ASTFactory.createClass(Double.class).getClassName(), typeContext.getStart());
default: default:
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -9,9 +9,8 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
public abstract class Expression extends TypableStatement public abstract class Expression extends TypableStatement {
{ public Expression(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
public Expression(RefTypeOrTPHOrWildcardOrGeneric type, Token offset){
super(type, offset); super(type, offset);
} }
} }

View File

@ -0,0 +1,31 @@
package de.dhbwstuttgart.syntaxtree.statement;
import java.util.ArrayList;
import java.util.List;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class RecordPattern extends Pattern {
private List<Pattern> 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) {
super(name, type, offset);
this.subPattern = subPattern;
}
public List<Pattern> getSubPattern() {
return this.subPattern;
}
public void addSubPattern(Pattern newPattern) {
this.subPattern.add(newPattern);
}
}