Added nestedPatterns

This commit is contained in:
luca9913 2023-07-12 21:54:17 +02:00
parent 3ed6edc323
commit 1643412f1b
4 changed files with 29 additions and 9 deletions

View File

@ -4,9 +4,9 @@ record Point(int x, int y) {}
interface Shape {} interface Shape {}
record ColoredPoint(Point pt, String color) {} record ColoredPoint(Point pt, String color) {}
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) implements Shape {} record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) implements Shape {}
sealed class Color permits Blue, Red { sealed class Color permits Blue, Red {}
class Blue extends Color {}
} class Red extends Color {}
class PatternMatching { class PatternMatching {
void printColorOfUpperLeftPoint(Shape shape) void printColorOfUpperLeftPoint(Shape shape)
@ -14,6 +14,6 @@ void printColorOfUpperLeftPoint(Shape shape)
switch (shape) { switch (shape) {
case Rectangle(ColoredPoint(Point pt, String color), ColoredPoint lowerRight) -> System.out.println("x: " + pt.x() + " / color: " + color + " / lowerRight: " + lowerRight); 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"); default -> System.out.println("not a rectangle");
} };
} }
} }

View File

@ -530,7 +530,6 @@ statement
| YIELD expression ';' #yieldstmt // Java17 | YIELD expression ';' #yieldstmt // Java17
| SEMI #semistmt | SEMI #semistmt
| statementExpression=expression ';' #stmtexpression | statementExpression=expression ';' #stmtexpression
| switchExpression ';' #switchexpressionstmt // Java17
| identifierLabel=identifier ':' statement #labeledstmt | identifierLabel=identifier ':' statement #labeledstmt
; ;

View File

@ -81,7 +81,6 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabelPatternContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabeledRuleContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabeledRuleContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchRuleOutcomeContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchRuleOutcomeContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchexpressionstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.TPatternContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.TPatternContext;
@ -193,8 +192,6 @@ public class StatementGenerator {
return convert(dowhileloop); return convert(dowhileloop);
case SwitchstmtContext switchstmt: case SwitchstmtContext switchstmt:
return convert(switchstmt); return convert(switchstmt);
case SwitchexpressionstmtContext switchexpression:
return convert(switchexpression);
case ReturnstmtContext returnstmt: case ReturnstmtContext returnstmt:
return convert(returnstmt); return convert(returnstmt);
case YieldstmtContext yieldstmt: case YieldstmtContext yieldstmt:
@ -274,6 +271,10 @@ public class StatementGenerator {
ret = convert(prefix); ret = convert(prefix);
ret.setStatement(); ret.setStatement();
return ret; return ret;
case SwitchexpressionContext switchexpr:
ret = convert(switchexpr);
ret.setStatement();
return ret;
default: default:
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -356,7 +357,8 @@ public class StatementGenerator {
return new Switch(switched, switchBlocks, switched.getType(), true, stmt.getStart()); return new Switch(switched, switchBlocks, switched.getType(), true, stmt.getStart());
} }
private Statement convert(Java17Parser.SwitchexpressionstmtContext switchexpression) { // Um switchExpressions als Statement zu behandeln
private Statement convert(Java17Parser.SwitchexpressionContext switchexpression) {
Expression switchExpr = convert(switchexpression.switchExpression()); Expression switchExpr = convert(switchexpression.switchExpression());
if (switchExpr instanceof Switch s) { if (switchExpr instanceof Switch s) {
s.setStatement(); s.setStatement();

View File

@ -87,6 +87,25 @@ public class TestNewFeatures {
fail("An error occured while generating the AST for applyLambda.jav"); fail("An error occured while generating the AST for applyLambda.jav");
} }
} }
@Test
public void patternMatching() {
try {
/*
* FileInputStream fileIn = new FileInputStream(javFiles.get("PatternMatching")[1]); String expectedAST = new String(fileIn.readAllBytes()); fileIn.close(); expectedAST = expectedAST.replaceAll("TPH [A-Z]+", "TPH");
*/
File srcfile = javFiles.get("PatternMatching")[0];
JavaTXCompiler compiler = new JavaTXCompiler(srcfile);
String resultingAST = new String(ASTPrinter.print(compiler.sourceFiles.get(srcfile)));
resultingAST = resultingAST.replaceAll("TPH [A-Z]+", "TPH");
// System.out.println("Expected:\n" + new String(expectedAST));
System.out.println("Result:\n" + new String(resultingAST));
// assertEquals("Comparing expected and resulting AST for PatternMatching.jav", expectedAST, resultingAST);
} catch (Exception exc) {
exc.printStackTrace();
fail("An error occured while generating the AST for PatternMatching.jav");
}
}
} }
class JavFilter implements FileFilter { class JavFilter implements FileFilter {