Created RecordPattern & started development with test cases
This commit is contained in:
parent
359f3e68ab
commit
492cbe48e9
19
resources/syntaxtreegenerator/javFiles/PatternMatching.jav
Normal file
19
resources/syntaxtreegenerator/javFiles/PatternMatching.jav
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import java.lang.Integer;
|
||||
import java.lang.String;
|
||||
import java.lang.Boolean;
|
||||
import java.lang.Object;
|
||||
|
||||
class SwitchStatement {
|
||||
|
||||
@ -30,12 +30,12 @@ class SwitchStatement {
|
||||
}
|
||||
}
|
||||
|
||||
/* boolean enclosedPattern(){
|
||||
var i = "Test";
|
||||
var j = switch(i){
|
||||
case (String s)->{ yield 0;}
|
||||
case Integer i ->{ yield 1;}
|
||||
};
|
||||
return (j==0);
|
||||
} */
|
||||
record Coordinates(double x, double y) {}
|
||||
|
||||
boolean recordPattern(Object obj){
|
||||
switch(obj){
|
||||
case Coordinates(double lat, double lon): return true;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -61,6 +61,8 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryPatternContext;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimarySuperContext;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryThisContext;
|
||||
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.ReturnstmtContext;
|
||||
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.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;
|
||||
@ -409,12 +412,24 @@ public class StatementGenerator {
|
||||
case TPatternContext tPattern:
|
||||
TypePatternContext typePattern = tPattern.typePattern();
|
||||
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:
|
||||
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) {
|
||||
Expression expr = convert(stmt.parExpression().expression());
|
||||
Statement block = convert(stmt.statement());
|
||||
|
@ -65,6 +65,8 @@ public class TypeGenerator {
|
||||
return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), typeContext.getStart());
|
||||
case "int":
|
||||
return new RefType(ASTFactory.createClass(Integer.class).getClassName(), typeContext.getStart());
|
||||
case "double":
|
||||
return new RefType(ASTFactory.createClass(Double.class).getClassName(), typeContext.getStart());
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
@ -9,9 +9,8 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
||||
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
public abstract class Expression extends TypableStatement
|
||||
{
|
||||
public Expression(RefTypeOrTPHOrWildcardOrGeneric type, Token offset){
|
||||
public abstract class Expression extends TypableStatement {
|
||||
public Expression(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) {
|
||||
super(type, offset);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user