From 677c784b6d8ae0e115b7e29bc39f94bc54c36722 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Thu, 17 Aug 2023 13:26:30 +0200 Subject: [PATCH] Allow single expressions in switch expression --- resources/bytecode/javFiles/Switch.jav | 10 +++++----- .../de/dhbwstuttgart/parser/antlr/Java17Parser.g4 | 2 +- .../parser/SyntaxTreeGenerator/StatementGenerator.java | 9 +++------ .../target/tree/expression/TargetYield.java | 6 ++++++ 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/resources/bytecode/javFiles/Switch.jav b/resources/bytecode/javFiles/Switch.jav index 03308375..9c0e741f 100644 --- a/resources/bytecode/javFiles/Switch.jav +++ b/resources/bytecode/javFiles/Switch.jav @@ -7,11 +7,11 @@ record Rec(Integer a, Object b) {} public class Switch { main(o) { return switch (o) { - case Rec(Integer a, Integer b) -> { yield a + b; } - case Rec(Integer a, Float b) -> { yield a + 10; } - case Rec(Integer a, Rec(Integer b, Integer c)) -> { yield a + b + c; } - case Integer i -> { yield i; } - default -> { yield 0; } + case Rec(Integer a, Integer b) -> a + b; + case Rec(Integer a, Float b) -> a + 10; + case Rec(Integer a, Rec(Integer b, Integer c)) -> a + b + c; + case Integer i -> i; + default -> 0; }; } } \ 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 d68619cf..d2bdf496 100644 --- a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 +++ b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 @@ -725,7 +725,7 @@ guardedPattern // Java17 switchRuleOutcome : block - | blockStatement* + | expression ';' ; classType diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index ab89d57b..fc75f9f3 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -408,12 +408,9 @@ public class StatementGenerator { Block block; if (Objects.isNull(outcome.block())) { List stmts = new ArrayList<>(); - if (outcome.blockStatement().size() == 1) isSingleExpression = true; - outcome.blockStatement().stream().forEach((stmt) -> { - stmts.addAll(convert(stmt)); - }); - block = new Block(stmts, outcome.blockStatement(0).getStart()); - + stmts.add(new Yield(convert(outcome.expression()), outcome.expression().start)); + isSingleExpression = true; + block = new Block(stmts, outcome.expression().getStart()); } else { block = convert(outcome.block(), false); } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetYield.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetYield.java index 108e7710..10e85bb3 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetYield.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetYield.java @@ -1,4 +1,10 @@ package de.dhbwstuttgart.target.tree.expression; +import de.dhbwstuttgart.target.tree.type.TargetType; + public record TargetYield(TargetExpression expression) implements TargetExpression { + @Override + public TargetType type() { + return expression.type(); + } }