diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 308c0528..693b0f37 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -749,16 +749,14 @@ public class StatementGenerator { // Check for localVar: if (localVars.get(expression) != null) { return new LocalVar(expression, localVars.get(expression), offset); - } else { - if (fields.get(expression) != null) {// PL 2018-11-01 fields eingefuegt, damit die fields immer die - // gleiche TPH bekommen - return new FieldVar(new This(offset), expression, fields.get(expression), offset); + } else if (fields.get(expression) != null) {// PL 2018-11-01 fields eingefuegt, damit die fields immer die + // gleiche TPH bekommen + return new FieldVar(new This(offset), expression, fields.get(expression), offset); - } else { - // kann eigentlich nicht vorkommen - // Dann Muss es ein Feld sein! - return new FieldVar(new This(offset), expression, TypePlaceholder.fresh(offset), offset); - } + } else { + // lokale Variable wurde ohne "var"-Keyword deklariert und direkt mit Wert versehen + localVars.put(expression, TypePlaceholder.fresh(offset)); + return new LocalVar(expression, localVars.get(expression), offset); } } return generateFieldVarOrClassname(expression, offset); diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 49c31e45..667c56db 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -220,12 +220,14 @@ public class SyntaxTreeGenerator { implementedInterfaces.addAll(convert(ctx.typeList(0), generics)); } // Ist Bit für 'sealed'-Modifier gesetzt - if ((modifiers & 4096) != 0 && !Objects.isNull(ctx.PERMITS())) { - // permitted subtypes sind letzte typeList (siehe Grammatikregel 'classDeclaration') - permittedSubtypes.addAll(convert(ctx.typeList(ctx.typeList().size() - 1), generics)); - } else { - // falls sealed modifier ohne 'permits'-List oder umgekehrt - throw new NotImplementedException("Invalid sealed class declaration"); + if ((modifiers & 4096) != 0) { + if (!Objects.isNull(ctx.PERMITS())) { + // permitted subtypes sind letzte typeList (siehe Grammatikregel 'classDeclaration') + permittedSubtypes.addAll(convert(ctx.typeList(ctx.typeList().size() - 1), generics)); + } else { + // falls sealed modifier ohne 'permits'-List oder umgekehrt + throw new NotImplementedException("Invalid sealed class declaration"); + } } return new ClassOrInterface(modifiers, name, fielddecl, Optional.of(this.generatePseudoConstructor(ctx.identifier().getText(), name, superClass, genericClassParameters, offset)), methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, permittedSubtypes, offset); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index 8b668738..6a69bd9d 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -294,7 +294,7 @@ public abstract class AbstractASTWalker implements ASTVisitor { @Override public void visit(SwitchLabel switchLabel) { - switchLabel.getExpression().accept(this); + } @Override diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/StatementVisitor.java b/src/main/java/de/dhbwstuttgart/syntaxtree/StatementVisitor.java index 480d2165..4361d703 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/StatementVisitor.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/StatementVisitor.java @@ -43,8 +43,22 @@ public interface StatementVisitor { void visit(ReturnVoid aReturn); + void visit(Switch switchStmt); + + void visit(SwitchBlock switchBlock); + + void visit(SwitchLabel switchLabel); + void visit(Break aBreak); + void visit(Yield aYield); + + void visit(Pattern aPattern); + + void visit(RecordPattern aRecordPattern); + + void visit(GuardedPattern aGuardedPattern); + void visit(StaticClassName staticClassName); void visit(Super aSuper); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Break.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Break.java index 46db60e4..06dd4e6a 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Break.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Break.java @@ -13,7 +13,7 @@ public class Break extends Statement { @Override public void accept(StatementVisitor visitor) { - this.accept((StatementVisitor) visitor); + visitor.visit(this); } } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Pattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Pattern.java index 42230a15..50a63ea3 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Pattern.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Pattern.java @@ -20,8 +20,7 @@ public class Pattern extends Expression { @Override public void accept(StatementVisitor visitor) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'accept'"); + visitor.visit(this); } } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Switch.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Switch.java index 1c5ab2d0..21382b80 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Switch.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Switch.java @@ -31,7 +31,7 @@ public class Switch extends Statement { @Override public void accept(StatementVisitor visitor) { - // visitor.visit(this); + visitor.visit(this); } } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchBlock.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchBlock.java index 349899de..39af78a8 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchBlock.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchBlock.java @@ -5,6 +5,8 @@ import java.util.List; import org.antlr.v4.runtime.Token; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; + public class SwitchBlock extends Block { private List labels = new ArrayList<>(); @@ -26,4 +28,13 @@ public class SwitchBlock extends Block { return defaultBlock; } + public List getLabels() { + return labels; + } + + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } + } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchExpression.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchExpression.java deleted file mode 100644 index 65ac46c9..00000000 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchExpression.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - -import org.antlr.v4.runtime.Token; - -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; - -public class SwitchExpression extends Expression { - - public SwitchExpression(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { - super(type, offset); - // TODO Auto-generated constructor stub - } - - @Override - public void accept(StatementVisitor visitor) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'accept'"); - } - -} diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchLabel.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchLabel.java index 3dcd28dc..fd94e8c3 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchLabel.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchLabel.java @@ -20,7 +20,7 @@ public class SwitchLabel extends Expression { this.defaultCase = true; } - Expression getExpression() { + public Expression getExpression() { return caseExpression; } @@ -30,8 +30,7 @@ public class SwitchLabel extends Expression { @Override public void accept(StatementVisitor visitor) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'accept'"); + visitor.visit(this); } } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 3d427d23..69078ccf 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -400,4 +400,76 @@ public class OutputGenerator implements ASTVisitor { public void visit(de.dhbwstuttgart.syntaxtree.statement.Literal literal) { out.append(literal.value); } + + @Override + public void visit(Switch switchStmt) { + out.append("switch("); + switchStmt.getSwitch().accept(this); + out.append("){\n"); + tab(); + for (SwitchBlock switchBlock : switchStmt.getBlocks()) { + switchBlock.accept(this); + } + untab(); + out.append(tabs); + out.append("}"); + } + + @Override + public void visit(SwitchBlock switchBlock) { + switchBlock.getLabels().stream().forEach((label) -> { + out.append(tabs); + label.accept(this); + }); + tab(); + switchBlock.getStatements().stream().forEach((stmt) -> { + out.append(tabs); + stmt.accept(this); + out.append(";\n"); + }); + out.append("\n"); + untab(); + } + + @Override + public void visit(SwitchLabel switchLabel) { + if (switchLabel.isDefault()) { + out.append("default"); + } else { + out.append("case "); + switchLabel.getExpression().accept(this); + } + out.append(":\n"); + } + + @Override + public void visit(Yield aYield) { + out.append("yield "); + aYield.retexpr.accept(this); + } + + @Override + public void visit(Pattern aPattern) { + aPattern.getType().accept(this); + out.append(" " + aPattern.getName()); + } + + @Override + public void visit(RecordPattern aRecordPattern) { + aRecordPattern.getType().accept(this); + for (Pattern subPattern : aRecordPattern.getSubPattern()) { + subPattern.accept(this); + } + out.append(aRecordPattern.getName()); + } + + @Override + public void visit(GuardedPattern aGuardedPattern) { + aGuardedPattern.getType().accept(this); + out.append(aGuardedPattern.getName()); + for (Expression cond : aGuardedPattern.getConditions()) { + out.append("&&"); + cond.accept(this); + } + } } \ No newline at end of file