From fa7a331a6655fb0a54756d98dfb8eabf1a8cf7c1 Mon Sep 17 00:00:00 2001 From: luca9913 Date: Tue, 11 Jul 2023 22:15:35 +0200 Subject: [PATCH] Beginning of switchExpression for AST --- .../syntaxtreegenerator/javFiles/Switch.jav | 20 +++++++++---- .../parser/antlr/Java17Parser.g4 | 8 ++++-- .../StatementGenerator.java | 28 +++++++++++++++++-- .../syntaxtree/statement/Switch.java | 12 +++++++- 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/resources/syntaxtreegenerator/javFiles/Switch.jav b/resources/syntaxtreegenerator/javFiles/Switch.jav index 20ff2a4f..6c0f68aa 100644 --- a/resources/syntaxtreegenerator/javFiles/Switch.jav +++ b/resources/syntaxtreegenerator/javFiles/Switch.jav @@ -5,7 +5,7 @@ import java.lang.Object; class SwitchStatement { - boolean switchStandard(){ + switchStandard(){ str = "SwitchMe"; switch(str){ case String s: return true; @@ -13,7 +13,7 @@ class SwitchStatement { } } - boolean switchInteger(){ + switchInteger(){ i = 5; switch(i){ case Integer j: @@ -23,7 +23,7 @@ class SwitchStatement { return (i==0); } - boolean guardedPattern(){ + guardedPattern(){ var i = 1; switch(i){ case Integer j && j == 1: return true; @@ -33,10 +33,20 @@ class SwitchStatement { record Coordinates(double x, double y) {} - boolean recordPattern(Object obj){ + recordPattern(Object obj){ switch(obj){ case Coordinates(double lat, double lon): return true; default: return false; } } -} \ No newline at end of file +} + +record SwitchExpression(int x, int y){ + + boolean switchStandard(str){ + return switch(str){ + case String s -> yield true; + default -> yield false; + }; + } +} \ No newline at end of file diff --git a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 index 54dca0d8..ae7ad6dc 100644 --- a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 +++ b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 @@ -530,7 +530,7 @@ statement | YIELD expression ';' #yieldstmt // Java17 | SEMI #semistmt | statementExpression=expression ';' #stmtexpression - | switchExpression ';'? #switchexpressionstmt // Java17 + | switchExpression ';' #switchexpressionstmt // Java17 | identifierLabel=identifier ':' statement #labeledstmt ; @@ -708,8 +708,10 @@ switchExpression // Java17 switchLabeledRule - : CASE (expressionList | NULL_LITERAL | pattern) (ARROW | COLON) switchRuleOutcome - | DEFAULT (ARROW | COLON) switchRuleOutcome + : CASE expressionList (ARROW | COLON) switchRuleOutcome #labeledRuleExprList + | CASE NULL_LITERAL (ARROW | COLON) switchRuleOutcome #labeledRuleNull + | CASE pattern (ARROW | COLON) switchRuleOutcome #labeledRulePattern + | DEFAULT (ARROW | COLON) switchRuleOutcome #labeledRuleDefault ; // Java17 diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index e80a9241..ae64b9a7 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -74,6 +74,7 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabelConstContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabelContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabelDefaultContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabelPatternContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabeledRuleContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchexpressionstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext; @@ -340,15 +341,36 @@ public class StatementGenerator { } private Statement convert(Java17Parser.SwitchstmtContext stmt) { + Expression switched = convert(stmt.parExpression().expression()); List switchBlocks = new ArrayList<>(); for (SwitchBlockStatementGroupContext blockstmt : stmt.switchBlockStatementGroup()) { switchBlocks.add(convert(blockstmt)); } - return new Switch(switchBlocks, convert(stmt.parExpression().expression()).getType(), true, stmt.getStart()); + return new Switch(switched, switchBlocks, switched.getType(), true, stmt.getStart()); } private Statement convert(Java17Parser.SwitchexpressionstmtContext switchexpression) { - // TODO + Expression switchExpr = convert(switchexpression.switchExpression()); + if (switchExpr instanceof Switch s) { + s.setStatement(); + return s; + } else { + // sollte nie vorkommen, da convert(Java17Parser.SwitchExpressionContext switchExpression) eine Instanz von Switch zurückgibt + throw new RuntimeException(); + } + } + + private Expression convert(Java17Parser.SwitchExpressionContext switchExpression) { + Expression switched = convert(switchExpression.parExpression().expression()); + List switchBlocks = new ArrayList<>(); + Token offset = switchExpression.getStart(); + for (SwitchLabeledRuleContext labeledRule : switchExpression.switchLabeledRule()) { + switchBlocks.add(convert(labeledRule)); + } + return new Switch(switched, switchBlocks, TypePlaceholder.fresh(offset), false, offset); + } + + private SwitchBlock convert(Java17Parser.SwitchLabeledRuleContext labeledRule) { throw new NotImplementedException(); } @@ -384,7 +406,7 @@ public class StatementGenerator { }; Token offset = switchLabel.getStart(); if (Objects.isNull(caseExpression)) { - return new SwitchLabel(null, TypePlaceholder.fresh(offset), offset); + return new SwitchLabel(TypePlaceholder.fresh(offset), offset); } else { return new SwitchLabel(caseExpression, caseExpression.getType(), offset); } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Switch.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Switch.java index 452f8298..1c5ab2d0 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Switch.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Switch.java @@ -10,15 +10,25 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; public class Switch extends Statement { + private Expression switchedExpression; private List blocks = new ArrayList<>(); - public Switch(List blocks, RefTypeOrTPHOrWildcardOrGeneric type, Boolean isStatement, Token offset) { + public Switch(Expression switched, List blocks, RefTypeOrTPHOrWildcardOrGeneric type, Boolean isStatement, Token offset) { super(type, offset); if (isStatement) setStatement(); + this.switchedExpression = switched; this.blocks = blocks; } + public Expression getSwitch() { + return switchedExpression; + } + + public List getBlocks() { + return blocks; + } + @Override public void accept(StatementVisitor visitor) { // visitor.visit(this);