forked from JavaTX/JavaCompilerCore
Beginning of switchExpression for AST
This commit is contained in:
parent
939d402b1e
commit
fa7a331a66
@ -5,7 +5,7 @@ import java.lang.Object;
|
|||||||
|
|
||||||
class SwitchStatement {
|
class SwitchStatement {
|
||||||
|
|
||||||
boolean switchStandard(){
|
switchStandard(){
|
||||||
str = "SwitchMe";
|
str = "SwitchMe";
|
||||||
switch(str){
|
switch(str){
|
||||||
case String s: return true;
|
case String s: return true;
|
||||||
@ -13,7 +13,7 @@ class SwitchStatement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean switchInteger(){
|
switchInteger(){
|
||||||
i = 5;
|
i = 5;
|
||||||
switch(i){
|
switch(i){
|
||||||
case Integer j:
|
case Integer j:
|
||||||
@ -23,7 +23,7 @@ class SwitchStatement {
|
|||||||
return (i==0);
|
return (i==0);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean guardedPattern(){
|
guardedPattern(){
|
||||||
var i = 1;
|
var i = 1;
|
||||||
switch(i){
|
switch(i){
|
||||||
case Integer j && j == 1: return true;
|
case Integer j && j == 1: return true;
|
||||||
@ -33,10 +33,20 @@ class SwitchStatement {
|
|||||||
|
|
||||||
record Coordinates(double x, double y) {}
|
record Coordinates(double x, double y) {}
|
||||||
|
|
||||||
boolean recordPattern(Object obj){
|
recordPattern(Object obj){
|
||||||
switch(obj){
|
switch(obj){
|
||||||
case Coordinates(double lat, double lon): return true;
|
case Coordinates(double lat, double lon): return true;
|
||||||
default: return false;
|
default: return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
record SwitchExpression(int x, int y){
|
||||||
|
|
||||||
|
boolean switchStandard(str){
|
||||||
|
return switch(str){
|
||||||
|
case String s -> yield true;
|
||||||
|
default -> yield false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -530,7 +530,7 @@ statement
|
|||||||
| YIELD expression ';' #yieldstmt // Java17
|
| YIELD expression ';' #yieldstmt // Java17
|
||||||
| SEMI #semistmt
|
| SEMI #semistmt
|
||||||
| statementExpression=expression ';' #stmtexpression
|
| statementExpression=expression ';' #stmtexpression
|
||||||
| switchExpression ';'? #switchexpressionstmt // Java17
|
| switchExpression ';' #switchexpressionstmt // Java17
|
||||||
| identifierLabel=identifier ':' statement #labeledstmt
|
| identifierLabel=identifier ':' statement #labeledstmt
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -708,8 +708,10 @@ switchExpression
|
|||||||
|
|
||||||
// Java17
|
// Java17
|
||||||
switchLabeledRule
|
switchLabeledRule
|
||||||
: CASE (expressionList | NULL_LITERAL | pattern) (ARROW | COLON) switchRuleOutcome
|
: CASE expressionList (ARROW | COLON) switchRuleOutcome #labeledRuleExprList
|
||||||
| DEFAULT (ARROW | COLON) switchRuleOutcome
|
| CASE NULL_LITERAL (ARROW | COLON) switchRuleOutcome #labeledRuleNull
|
||||||
|
| CASE pattern (ARROW | COLON) switchRuleOutcome #labeledRulePattern
|
||||||
|
| DEFAULT (ARROW | COLON) switchRuleOutcome #labeledRuleDefault
|
||||||
;
|
;
|
||||||
|
|
||||||
// Java17
|
// Java17
|
||||||
|
@ -74,6 +74,7 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabelConstContext;
|
|||||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabelContext;
|
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabelContext;
|
||||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabelDefaultContext;
|
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabelDefaultContext;
|
||||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchLabelPatternContext;
|
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.SwitchexpressionstmtContext;
|
||||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchstmtContext;
|
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchstmtContext;
|
||||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext;
|
import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext;
|
||||||
@ -340,15 +341,36 @@ public class StatementGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Statement convert(Java17Parser.SwitchstmtContext stmt) {
|
private Statement convert(Java17Parser.SwitchstmtContext stmt) {
|
||||||
|
Expression switched = convert(stmt.parExpression().expression());
|
||||||
List<SwitchBlock> switchBlocks = new ArrayList<>();
|
List<SwitchBlock> switchBlocks = new ArrayList<>();
|
||||||
for (SwitchBlockStatementGroupContext blockstmt : stmt.switchBlockStatementGroup()) {
|
for (SwitchBlockStatementGroupContext blockstmt : stmt.switchBlockStatementGroup()) {
|
||||||
switchBlocks.add(convert(blockstmt));
|
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) {
|
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();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -384,7 +406,7 @@ public class StatementGenerator {
|
|||||||
};
|
};
|
||||||
Token offset = switchLabel.getStart();
|
Token offset = switchLabel.getStart();
|
||||||
if (Objects.isNull(caseExpression)) {
|
if (Objects.isNull(caseExpression)) {
|
||||||
return new SwitchLabel(null, TypePlaceholder.fresh(offset), offset);
|
return new SwitchLabel(TypePlaceholder.fresh(offset), offset);
|
||||||
} else {
|
} else {
|
||||||
return new SwitchLabel(caseExpression, caseExpression.getType(), offset);
|
return new SwitchLabel(caseExpression, caseExpression.getType(), offset);
|
||||||
}
|
}
|
||||||
|
@ -10,15 +10,25 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
|||||||
|
|
||||||
public class Switch extends Statement {
|
public class Switch extends Statement {
|
||||||
|
|
||||||
|
private Expression switchedExpression;
|
||||||
private List<SwitchBlock> blocks = new ArrayList<>();
|
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);
|
super(type, offset);
|
||||||
if (isStatement)
|
if (isStatement)
|
||||||
setStatement();
|
setStatement();
|
||||||
|
this.switchedExpression = switched;
|
||||||
this.blocks = blocks;
|
this.blocks = blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Expression getSwitch() {
|
||||||
|
return switchedExpression;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SwitchBlock> getBlocks() {
|
||||||
|
return blocks;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(StatementVisitor visitor) {
|
public void accept(StatementVisitor visitor) {
|
||||||
// visitor.visit(this);
|
// visitor.visit(this);
|
||||||
|
Loading…
Reference in New Issue
Block a user