From 3ed6edc32303921fdd7550e35fd447f3b68bb449 Mon Sep 17 00:00:00 2001 From: luca9913 Date: Wed, 12 Jul 2023 21:43:50 +0200 Subject: [PATCH] Added SwitchExpression to AST --- .../parser/antlr/Java17Parser.g4 | 12 +++-- .../StatementGenerator.java | 52 ++++++++++++++++--- .../syntaxtree/statement/SwitchBlock.java | 8 +-- .../syntaxtree/statement/SwitchLabel.java | 9 ++-- .../syntaxtree/statement/Yield.java | 18 +++++++ 5 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 src/main/java/de/dhbwstuttgart/syntaxtree/statement/Yield.java diff --git a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 index ae7ad6dc..53439980 100644 --- a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 +++ b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 @@ -708,10 +708,14 @@ switchExpression // Java17 switchLabeledRule - : CASE expressionList (ARROW | COLON) switchRuleOutcome #labeledRuleExprList - | CASE NULL_LITERAL (ARROW | COLON) switchRuleOutcome #labeledRuleNull - | CASE pattern (ARROW | COLON) switchRuleOutcome #labeledRulePattern - | DEFAULT (ARROW | COLON) switchRuleOutcome #labeledRuleDefault + : switchLabelCase switchRuleOutcome + ; + +switchLabelCase + : CASE expressionList (ARROW | COLON) #labeledRuleExprList + | CASE NULL_LITERAL (ARROW | COLON) #labeledRuleNull + | CASE pattern (ARROW | COLON) #labeledRulePattern + | DEFAULT (ARROW | COLON) #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 ae64b9a7..d7acc655 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -20,6 +20,7 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.AssignexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.BitwiseandexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.BitwiseorexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.BitwisexorexpressionContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.BlockStatementContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.BlockstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.BoolLiteralContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.BreakstmtContext; @@ -39,6 +40,9 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.GuardedPatternContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.IdentifierContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.InstanceofexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.IntLiteralContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.LabeledRuleDefaultContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.LabeledRuleExprListContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.LabeledRulePatternContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.LabeledstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.LambdaLVTIParameterContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.LambdaexpressionContext; @@ -75,6 +79,8 @@ 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.SwitchRuleOutcomeContext; +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.SynchronizedstmtContext; @@ -124,6 +130,7 @@ import de.dhbwstuttgart.syntaxtree.statement.This; import de.dhbwstuttgart.syntaxtree.statement.Pattern; import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr; import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; +import de.dhbwstuttgart.syntaxtree.statement.Yield; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; @@ -371,12 +378,43 @@ public class StatementGenerator { } private SwitchBlock convert(Java17Parser.SwitchLabeledRuleContext labeledRule) { - throw new NotImplementedException(); + Boolean isDefault = false; + List labels = switch (labeledRule.switchLabelCase()) { + case LabeledRuleExprListContext exprList -> { + List labelList = exprList.expressionList().expression().stream().map((exprctx) -> { + Expression expr = convert(exprctx); + return new SwitchLabel(expr, expr.getType(), exprList.getStart()); + }).toList(); + yield labelList; + } + case LabeledRulePatternContext pattern -> { + Expression p = convert(pattern.pattern()); + yield Arrays.asList(new SwitchLabel(p, p.getType(), pattern.getStart())); + } + case LabeledRuleDefaultContext def -> { + isDefault = true; + yield Arrays.asList(new SwitchLabel(TypePlaceholder.fresh(def.getStart()), def.getStart())); + } + default -> throw new NotImplementedException(); + }; + Token offset = labeledRule.getStart(); + SwitchRuleOutcomeContext outcome = labeledRule.switchRuleOutcome(); + Block block; + if (Objects.isNull(outcome.block())) { + List stmts = new ArrayList<>(); + outcome.blockStatement().stream().forEach((stmt) -> { + stmts.addAll(convert(stmt)); + }); + block = new Block(stmts, outcome.blockStatement(0).getStart()); + + } else { + block = convert(outcome.block(), false); + } + return new SwitchBlock(labels, block, isDefault, offset); } private Statement convert(Java17Parser.YieldstmtContext yieldstmt) { - // TODO - throw new NotImplementedException(); + return new Yield(convert(yieldstmt.expression()), yieldstmt.getStart()); } private SwitchBlock convert(Java17Parser.SwitchBlockStatementGroupContext stmt) { @@ -388,7 +426,7 @@ public class StatementGenerator { stmt.blockStatement().stream().forEach((blockStmt) -> { block.addAll(convert(blockStmt)); }); - return new SwitchBlock(labels, block, stmt.getStart()); + return new SwitchBlock(labels, new Block(block, stmt.blockStatement(0).getStart()), stmt.getStart()); } private SwitchLabel convert(SwitchLabelContext switchLabel) { @@ -410,7 +448,6 @@ public class StatementGenerator { } else { return new SwitchLabel(caseExpression, caseExpression.getType(), offset); } - } private Pattern convert(PatternContext pattern) { @@ -605,9 +642,8 @@ public class StatementGenerator { return convert(comparison); case InstanceofexpressionContext instanceOf: return convert(instanceOf); - /* - * TODO: case SwitchexpressionContext switchexpression: - */ + case SwitchexpressionContext switchexpression: + return convert(switchexpression.switchExpression()); case EqualityexpressionContext equal: return convert(equal); case AssignexpressionContext assignment: diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchBlock.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchBlock.java index 7a61ebd6..349899de 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchBlock.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchBlock.java @@ -11,13 +11,13 @@ public class SwitchBlock extends Block { private Boolean defaultBlock = false; - public SwitchBlock(List labels, List statements, Token offset) { - super(statements, offset); + public SwitchBlock(List labels, Block statements, Token offset) { + super(statements.getStatements(), offset); this.labels = labels; } - public SwitchBlock(List labels, List statements, Boolean isDefault, Token offset) { - super(statements, offset); + public SwitchBlock(List labels, Block statements, Boolean isDefault, Token offset) { + super(statements.getStatements(), offset); this.labels = labels; this.defaultBlock = isDefault; } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchLabel.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchLabel.java index d5e8b1f8..3dcd28dc 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchLabel.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchLabel.java @@ -15,10 +15,13 @@ public class SwitchLabel extends Expression { this.caseExpression = caseExpression; } - public SwitchLabel(Expression caseExpression, Boolean def, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { + public SwitchLabel(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { super(type, offset); - this.caseExpression = caseExpression; - this.defaultCase = def; + this.defaultCase = true; + } + + Expression getExpression() { + return caseExpression; } public Boolean isDefault() { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Yield.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Yield.java new file mode 100644 index 00000000..cd16b216 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Yield.java @@ -0,0 +1,18 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import org.antlr.v4.runtime.Token; + +import de.dhbwstuttgart.syntaxtree.StatementVisitor; + +public class Yield extends Return { + + public Yield(Expression retExpr, Token offset) { + super(retExpr, offset); + } + + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } + +}