Merge remote-tracking branch 'origin/NewParser' into NewParser
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
Purplumbi504 2024-07-02 17:51:11 +02:00
commit cfde5219a4
2 changed files with 47 additions and 28 deletions

View File

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

View File

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