forked from JavaTX/JavaCompilerCore
Added SwitchExpression to AST
This commit is contained in:
parent
fa7a331a66
commit
3ed6edc323
@ -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
|
||||
|
@ -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<SwitchLabel> labels = switch (labeledRule.switchLabelCase()) {
|
||||
case LabeledRuleExprListContext exprList -> {
|
||||
List<SwitchLabel> 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<Statement> 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:
|
||||
|
@ -11,13 +11,13 @@ public class SwitchBlock extends Block {
|
||||
|
||||
private Boolean defaultBlock = false;
|
||||
|
||||
public SwitchBlock(List<SwitchLabel> labels, List<Statement> statements, Token offset) {
|
||||
super(statements, offset);
|
||||
public SwitchBlock(List<SwitchLabel> labels, Block statements, Token offset) {
|
||||
super(statements.getStatements(), offset);
|
||||
this.labels = labels;
|
||||
}
|
||||
|
||||
public SwitchBlock(List<SwitchLabel> labels, List<Statement> statements, Boolean isDefault, Token offset) {
|
||||
super(statements, offset);
|
||||
public SwitchBlock(List<SwitchLabel> labels, Block statements, Boolean isDefault, Token offset) {
|
||||
super(statements.getStatements(), offset);
|
||||
this.labels = labels;
|
||||
this.defaultBlock = isDefault;
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user