Fixes and changed For Builder

This commit is contained in:
Maximilian Stahl 2024-07-02 17:36:48 +02:00
parent dcd20a2394
commit 02e5f3a729
2 changed files with 47 additions and 28 deletions

View File

@ -1,6 +1,5 @@
package ast.statements;
import ast.ASTNode;
import ast.expressions.IExpressionNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
@ -14,11 +13,6 @@ public class WhileNode implements IStatementNode {
this.block = block;
}
public void test() {
return;
}
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return null;

View File

@ -14,6 +14,7 @@ import ast.members.*;
import ast.parameters.ParameterNode;
import ast.statementexpressions.AssignNode;
import ast.statementexpressions.AssignableNode;
import ast.statementexpressions.IStatementExpressionNode;
import ast.statementexpressions.NewDeclarationNode;
import ast.statementexpressions.crementexpressions.CrementType;
import ast.statementexpressions.crementexpressions.DecrementNode;
@ -75,7 +76,12 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) {
ConstructorNode constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
ConstructorNode constructorNode;
if(ctx.AccessModifier() != null) {
constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
} else {
constructorNode = new ConstructorNode("public", ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
}
if(ctx.parameterList() != null) {
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
constructorNode.addParameter((ParameterNode) visit(parameter));
@ -177,7 +183,12 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitLocalVariableDeclaration(SimpleJavaParser.LocalVariableDeclarationContext ctx) {
if(ctx.Assign() != null) {
return new LocalVariableDeclarationNode(createTypeNode(ctx.type().getText()), ctx.Identifier().getText(), ctx.Assign().getText(), (IExpressionNode) visit(ctx.expression()));
} else {
return new LocalVariableDeclarationNode(createTypeNode(ctx.type().getText()), ctx.Identifier().getText(), null, null);
}
}
@Override
@ -209,10 +220,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) {
List<IStatementNode> statements = new ArrayList<>();
//init
// Initialisierung
int i = 0;
if (ctx.localVariableDeclaration() != null) {
statements.add((IStatementNode) visit(ctx.localVariableDeclaration()));
@ -221,31 +231,46 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
i++;
}
//condition
// Bedingung
IExpressionNode condition = (IExpressionNode) visit(ctx.expression());
//ink
IStatementNode crement = null;
// Inkrement
IStatementExpressionNode crement = null;
boolean isPrefix = false;
if (ctx.statementExpression(i) != null) {
crement = (IStatementNode) visit(ctx.statementExpression(i));
crement = (IStatementExpressionNode) visit(ctx.statementExpression(i));
if (crement instanceof IncrementNode) {
isPrefix = ((IncrementNode) crement).crementType == CrementType.PREFIX;
} else if (crement instanceof DecrementNode) {
isPrefix = ((DecrementNode) crement).crementType == CrementType.PREFIX;
}
}
BlockNode forBlock = new BlockNode();
BlockNode forBlock = (BlockNode) visit(ctx.blockStatement());
BlockNode forStatements = (BlockNode) visit(ctx.blockStatement());
if(forStatements != null) {
forBlock.addStatement((IStatementNode) forStatements);
// While-Schleife
BlockNode whileBody = new BlockNode();
// Prä-Inkrement: Das Inkrement kommt vor dem Block
if (crement != null && isPrefix) {
whileBody.addStatement((IStatementNode) crement);
}
if(crement != null){
BlockNode forCrement = new BlockNode();
forCrement.addStatement((crement));
forBlock.addStatement(forCrement);
// Block Statements der For-Schleife in den While-Block kopieren
for (IStatementNode statement : forBlock.statements) {
whileBody.addStatement(statement);
}
WhileNode While = new WhileNode(condition, forBlock);
// Post-Inkrement: Das Inkrement kommt nach dem Block
if (crement != null && !isPrefix) {
whileBody.addStatement((IStatementNode) crement);
}
statements.add(While);
// Bedingung der While-Schleife
WhileNode whileNode = new WhileNode(condition, whileBody);
statements.add(whileNode);
BlockNode resultBlock = new BlockNode();
for (IStatementNode statement : statements) {