Modified ASTPrinter to support new AST nodes for tests

This commit is contained in:
luca9913 2023-07-15 21:41:27 +02:00
parent c0c46e197f
commit 75789e574e
11 changed files with 118 additions and 44 deletions

View File

@ -749,16 +749,14 @@ public class StatementGenerator {
// Check for localVar: // Check for localVar:
if (localVars.get(expression) != null) { if (localVars.get(expression) != null) {
return new LocalVar(expression, localVars.get(expression), offset); return new LocalVar(expression, localVars.get(expression), offset);
} else { } else if (fields.get(expression) != null) {// PL 2018-11-01 fields eingefuegt, damit die fields immer die
if (fields.get(expression) != null) {// PL 2018-11-01 fields eingefuegt, damit die fields immer die // gleiche TPH bekommen
// gleiche TPH bekommen return new FieldVar(new This(offset), expression, fields.get(expression), offset);
return new FieldVar(new This(offset), expression, fields.get(expression), offset);
} else { } else {
// kann eigentlich nicht vorkommen // lokale Variable wurde ohne "var"-Keyword deklariert und direkt mit Wert versehen
// Dann Muss es ein Feld sein! localVars.put(expression, TypePlaceholder.fresh(offset));
return new FieldVar(new This(offset), expression, TypePlaceholder.fresh(offset), offset); return new LocalVar(expression, localVars.get(expression), offset);
}
} }
} }
return generateFieldVarOrClassname(expression, offset); return generateFieldVarOrClassname(expression, offset);

View File

@ -220,12 +220,14 @@ public class SyntaxTreeGenerator {
implementedInterfaces.addAll(convert(ctx.typeList(0), generics)); implementedInterfaces.addAll(convert(ctx.typeList(0), generics));
} }
// Ist Bit für 'sealed'-Modifier gesetzt // Ist Bit für 'sealed'-Modifier gesetzt
if ((modifiers & 4096) != 0 && !Objects.isNull(ctx.PERMITS())) { if ((modifiers & 4096) != 0) {
// permitted subtypes sind letzte typeList (siehe Grammatikregel 'classDeclaration') if (!Objects.isNull(ctx.PERMITS())) {
permittedSubtypes.addAll(convert(ctx.typeList(ctx.typeList().size() - 1), generics)); // permitted subtypes sind letzte typeList (siehe Grammatikregel 'classDeclaration')
} else { permittedSubtypes.addAll(convert(ctx.typeList(ctx.typeList().size() - 1), generics));
// falls sealed modifier ohne 'permits'-List oder umgekehrt } else {
throw new NotImplementedException("Invalid sealed class declaration"); // 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); 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);

View File

@ -294,7 +294,7 @@ public abstract class AbstractASTWalker implements ASTVisitor {
@Override @Override
public void visit(SwitchLabel switchLabel) { public void visit(SwitchLabel switchLabel) {
switchLabel.getExpression().accept(this);
} }
@Override @Override

View File

@ -43,8 +43,22 @@ public interface StatementVisitor {
void visit(ReturnVoid aReturn); void visit(ReturnVoid aReturn);
void visit(Switch switchStmt);
void visit(SwitchBlock switchBlock);
void visit(SwitchLabel switchLabel);
void visit(Break aBreak); 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(StaticClassName staticClassName);
void visit(Super aSuper); void visit(Super aSuper);

View File

@ -13,7 +13,7 @@ public class Break extends Statement {
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
this.accept((StatementVisitor) visitor); visitor.visit(this);
} }
} }

View File

@ -20,8 +20,7 @@ public class Pattern extends Expression {
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
// TODO Auto-generated method stub visitor.visit(this);
throw new UnsupportedOperationException("Unimplemented method 'accept'");
} }
} }

View File

@ -31,7 +31,7 @@ public class Switch extends Statement {
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
// visitor.visit(this); visitor.visit(this);
} }
} }

View File

@ -5,6 +5,8 @@ import java.util.List;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
public class SwitchBlock extends Block { public class SwitchBlock extends Block {
private List<SwitchLabel> labels = new ArrayList<>(); private List<SwitchLabel> labels = new ArrayList<>();
@ -26,4 +28,13 @@ public class SwitchBlock extends Block {
return defaultBlock; return defaultBlock;
} }
public List<SwitchLabel> getLabels() {
return labels;
}
@Override
public void accept(StatementVisitor visitor) {
visitor.visit(this);
}
} }

View File

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

View File

@ -20,7 +20,7 @@ public class SwitchLabel extends Expression {
this.defaultCase = true; this.defaultCase = true;
} }
Expression getExpression() { public Expression getExpression() {
return caseExpression; return caseExpression;
} }
@ -30,8 +30,7 @@ public class SwitchLabel extends Expression {
@Override @Override
public void accept(StatementVisitor visitor) { public void accept(StatementVisitor visitor) {
// TODO Auto-generated method stub visitor.visit(this);
throw new UnsupportedOperationException("Unimplemented method 'accept'");
} }
} }

View File

@ -400,4 +400,76 @@ public class OutputGenerator implements ASTVisitor {
public void visit(de.dhbwstuttgart.syntaxtree.statement.Literal literal) { public void visit(de.dhbwstuttgart.syntaxtree.statement.Literal literal) {
out.append(literal.value); 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);
}
}
} }