Beginning of switchExpression for AST

This commit is contained in:
luca9913 2023-07-11 22:15:35 +02:00
parent 939d402b1e
commit fa7a331a66
4 changed files with 56 additions and 12 deletions

View File

@ -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;
}
}
}
}
record SwitchExpression(int x, int y){
boolean switchStandard(str){
return switch(str){
case String s -> yield true;
default -> yield false;
};
}
}

View File

@ -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

View File

@ -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<SwitchBlock> 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<SwitchBlock> 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);
}

View File

@ -10,15 +10,25 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class Switch extends Statement {
private Expression switchedExpression;
private List<SwitchBlock> blocks = new ArrayList<>();
public Switch(List<SwitchBlock> blocks, RefTypeOrTPHOrWildcardOrGeneric type, Boolean isStatement, Token offset) {
public Switch(Expression switched, List<SwitchBlock> 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<SwitchBlock> getBlocks() {
return blocks;
}
@Override
public void accept(StatementVisitor visitor) {
// visitor.visit(this);