Fix errors in if Statments.

For and Do While loop as While loop.
This commit is contained in:
Purplumbi504 2024-06-16 13:15:39 +02:00
parent 3639b2a4f8
commit 12ab402746
39 changed files with 255 additions and 194 deletions

19
.idea/ANTLRv4ToolGrammarProperties.xml generated Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ANTLRv4ToolGrammarProperties">
<option name="perGrammarGenerationSettings">
<list>
<PerGrammarGenerationSettings>
<option name="fileName" value="$PROJECT_DIR$/src/main/java/parser/grammar/SimpleJava.g4" />
<option name="autoGen" value="true" />
<option name="outputDir" value="C:\Users\janni\Desktop\NichtHaskell2.0\src\main\java" />
<option name="libDir" value="" />
<option name="encoding" value="" />
<option name="pkg" value="parser.generated" />
<option name="language" value="" />
<option name="generateVisitor" value="true" />
</PerGrammarGenerationSettings>
</list>
</option>
</component>
</project>

View File

@ -1,5 +1,20 @@
package ast;
public interface ASTNode { }
import bytecode.visitor.ClassVisitor;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
public interface ASTNode {
//Todo: @BruderJohn & @i22007 Interface anwenden + geeignetetn Methodename.
/*
Typecheck:
public TypeCheckResult acceptType(SemanticVisitor visitor);
Bytecode:
public void accepByteCode(ClassVisitor classVisitor);
*/
}

View File

@ -1,11 +1,10 @@
package ast;
import ast.type.AccessModifierNode;
import ast.type.EnumAccessModifierNode;
import bytecode.visitor.ClassVisitor;
import ast.member.ConstructorNode;
import ast.member.MemberNode;
import ast.member.MethodNode;
import ast.members.ConstructorNode;
import ast.members.MemberNode;
import ast.members.MethodNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import visitor.Visitable;

View File

@ -1,4 +1,4 @@
package ast.expression;
package ast.expressions;
public class AssignableExpressionNode {
}

View File

@ -1,4 +1,4 @@
package ast.expression;
package ast.expressions;
public class ExpressionNode {
}

View File

@ -1,7 +1,7 @@
package ast.member;
package ast.members;
import ast.block.BlockNode;
import ast.parameter.ParameterNode;
import ast.statements.BlockStatementNode;
import ast.parameters.ParameterNode;
import ast.type.AccessModifierNode;
import bytecode.visitor.MethodVisitor;
import visitor.Visitable;
@ -13,9 +13,9 @@ public class ConstructorNode extends MethodNode implements Visitable {
public AccessModifierNode accessType;
public String identifier;
public List<ParameterNode> parameters = new ArrayList<>();
public BlockNode body;
public BlockStatementNode body;
public ConstructorNode(String accessType, String identifier, BlockNode body) {
public ConstructorNode(String accessType, String identifier, BlockStatementNode body) {
this.accessType = new AccessModifierNode(accessType);
this.identifier = identifier;
this.body = body;

View File

@ -1,4 +1,4 @@
package ast.member;
package ast.members;
import ast.type.AccessModifierNode;
import ast.type.TypeNode;

View File

@ -1,4 +1,4 @@
package ast.member;
package ast.members;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSubTypes;

View File

@ -1,4 +1,4 @@
package ast.member;
package ast.members;
import bytecode.visitor.MethodVisitor;
import semantic.SemanticVisitor;

View File

@ -1,4 +1,4 @@
package ast.parameter;
package ast.parameters;
import ast.ASTNode;
import ast.type.TypeNode;

View File

@ -1,23 +0,0 @@
package ast.statement;
import ast.ASTNode;
import ast.expression.ExpressionNode;
public class ForStatementNode implements ASTNode {
ExpressionNode statementExpressionInit;
StatementNode localVariableDeclarationInit;
ExpressionNode expression;
ExpressionNode statementExpression;
public ForStatementNode(ExpressionNode statementExpressionInit, ExpressionNode expression, ExpressionNode statementExpression) {
this.statementExpressionInit = statementExpressionInit;
this.expression = expression;
this.statementExpression = statementExpression;
}
public ForStatementNode(StatementNode localVariableDeclarationInit, ExpressionNode expression, ExpressionNode statementExpression) {
this.localVariableDeclarationInit = localVariableDeclarationInit;
this.expression = expression;
this.statementExpression = statementExpression;
}
}

View File

@ -1,2 +0,0 @@
package ast.statement;public class StatementNode {
}

View File

@ -1,15 +0,0 @@
package ast.statement;
import ast.ASTNode;
import ast.block.BlockNode;
import ast.expression.ExpressionNode;
public class WhileStatementNode implements ASTNode {
ExpressionNode expression;
BlockNode block;
public WhileStatementNode(ExpressionNode expression, BlockNode block) {
this.expression = expression;
this.block = block;
}
}

View File

@ -1,12 +0,0 @@
package ast.statement.ifstatement;
import ast.ASTNode;
import ast.block.BlockNode;
public class ElseStatementNode implements ASTNode {
BlockNode block;
public ElseStatementNode(BlockNode block) {
this.block = block;
}
}

View File

@ -1,20 +0,0 @@
package ast.statement.ifstatement;
import ast.ASTNode;
import java.util.ArrayList;
import java.util.List;
public class IfElseStatementNode implements ASTNode {
IfStatementNode ifStatement;
List<ElseStatementNode> elseStatements = new ArrayList<>();
public IfElseStatementNode(IfStatementNode ifStatement) {
this.ifStatement = ifStatement;
}
public void addElseStatement(ElseStatementNode elseStatement) {
elseStatements.add(elseStatement);
}
}

View File

@ -1,15 +0,0 @@
package ast.statement.ifstatement;
import ast.ASTNode;
import ast.block.BlockNode;
import ast.expression.ExpressionNode;
public class IfStatementNode implements ASTNode {
ExpressionNode expression;
BlockNode block;
public IfStatementNode(ExpressionNode expression, BlockNode block) {
this.expression = expression;
this.block = block;
}
}

View File

@ -1,7 +0,0 @@
package ast.statement.statementexpression.methodcallstatementnexpression;
public class TargetNode {
String thisTarget;
}

View File

@ -1,8 +1,8 @@
package ast.statement.statementexpression;
package ast.statementexpressions;
import ast.ASTNode;
import ast.expression.AssignableExpressionNode;
import ast.expression.ExpressionNode;
import ast.expressions.AssignableExpressionNode;
import ast.expressions.ExpressionNode;
public class AssignStatementExpressionNode implements ASTNode {
AssignableExpressionNode assignable;

View File

@ -1,7 +1,7 @@
package ast.statement.statementexpression;
package ast.statementexpressions;
import ast.ASTNode;
import ast.expression.ExpressionNode;
import ast.expressions.ExpressionNode;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,7 +1,7 @@
package ast.statement.statementexpression;
package ast.statementexpressions;
import ast.ASTNode;
import ast.expression.ExpressionNode;
import ast.expressions.ExpressionNode;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,7 +1,7 @@
package ast.statement.statementexpression.methodcallstatementnexpression;
package ast.statementexpressions.methodcallstatementnexpression;
import ast.ASTNode;
import ast.expression.ExpressionNode;
import ast.expressions.ExpressionNode;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,7 +1,7 @@
package ast.statement.statementexpression.methodcallstatementnexpression;
package ast.statementexpressions.methodcallstatementnexpression;
import ast.ASTNode;
import ast.expression.ExpressionNode;
import ast.expressions.ExpressionNode;
import java.util.ArrayList;
import java.util.List;

View File

@ -0,0 +1,7 @@
package ast.statementexpressions.methodcallstatementnexpression;
public class TargetNode {
String thisTarget;
}

View File

@ -1,16 +1,13 @@
package ast.block;
package ast.statements;
import ast.ASTNode;
import ast.statement.StatementNode;
import java.util.ArrayList;
import java.util.List;
public class BlockNode implements ASTNode {
public class BlockStatementNode extends StatementNode implements ASTNode {
public List<StatementNode> statements = new ArrayList<>();
public BlockNode() {}
public void addStatement(StatementNode statement) {
statements.add(statement);
}

View File

@ -0,0 +1,13 @@
package ast.statements;
import ast.ASTNode;
public class ElseStatementNode implements ASTNode {
BlockStatementNode block;
public ElseStatementNode(BlockStatementNode block) {
this.block = block;
}
}

View File

@ -0,0 +1,22 @@
package ast.statements;
import ast.ASTNode;
import java.util.ArrayList;
import java.util.List;
public class IfElseStatementNode implements ASTNode {
IfStatementNode ifStatement;
List<IfStatementNode> elseIfStatements = new ArrayList<>();
ElseStatementNode elseStatement;
public IfElseStatementNode(IfStatementNode ifStatement, ElseStatementNode elseStatementNode) {
this.ifStatement = ifStatement;
this.elseStatement = elseStatementNode;
}
public void addElseIfStatement(IfStatementNode elseIfStament) {
elseIfStatements.add(elseIfStament);
}
}

View File

@ -0,0 +1,14 @@
package ast.statements;
import ast.ASTNode;
import ast.expressions.ExpressionNode;
public class IfStatementNode implements ASTNode {
ExpressionNode expression;
BlockStatementNode block;
public IfStatementNode(ExpressionNode expression, BlockStatementNode block) {
this.expression = expression;
this.block = block;
}
}

View File

@ -1,7 +1,7 @@
package ast.statement;
package ast.statements;
import ast.ASTNode;
import ast.expression.ExpressionNode;
import ast.expressions.ExpressionNode;
import ast.type.TypeNode;
public class LocalVariableDeclarationNode implements ASTNode {

View File

@ -1,11 +1,7 @@
package ast.statement;
package ast.statements;
import ast.ASTNode;
import ast.expression.ExpressionNode;
import ast.type.TypeNode;
import java.util.ArrayList;
import java.util.List;
import ast.expressions.ExpressionNode;
public class ReturnStatementNode implements ASTNode {
public ExpressionNode expression;

View File

@ -0,0 +1,2 @@
package ast.statements;public class StatementNode {
}

View File

@ -0,0 +1,14 @@
package ast.statements;
import ast.ASTNode;
import ast.expressions.ExpressionNode;
public class WhileStatementNode extends StatementNode implements ASTNode {
ExpressionNode expression;
BlockStatementNode block;
public WhileStatementNode(ExpressionNode expression, BlockStatementNode block) {
this.expression = expression;
this.block = block;
}
}

View File

@ -1,9 +1,9 @@
package bytecode;
import ast.ClassNode;
import ast.member.FieldNode;
import ast.member.MemberNode;
import ast.member.MethodNode;
import ast.members.FieldNode;
import ast.members.MemberNode;
import ast.members.MethodNode;
import ast.type.BaseTypeNode;
import bytecode.visitor.ClassVisitor;
import java.io.File;

View File

@ -1,6 +1,6 @@
package bytecode;
import ast.parameter.ParameterNode;
import ast.parameters.ParameterNode;
import ast.type.*;
import org.objectweb.asm.Opcodes;
import ast.type.BaseTypeNode;

View File

@ -1,8 +1,8 @@
package bytecode;
import ast.member.ConstructorNode;
import ast.member.MethodNode;
import ast.parameter.ParameterNode;
import ast.members.ConstructorNode;
import ast.members.MethodNode;
import ast.parameters.ParameterNode;
import ast.type.BaseTypeNode;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;

View File

@ -1,7 +1,7 @@
package bytecode.visitor;
import ast.ClassNode;
import ast.member.FieldNode;
import ast.members.FieldNode;
public interface ClassVisitor {
void visit(ClassNode classNode);

View File

@ -1,7 +1,7 @@
package bytecode.visitor;
import ast.member.ConstructorNode;
import ast.member.MethodNode;
import ast.members.ConstructorNode;
import ast.members.MethodNode;
public interface MethodVisitor {
void visit(ConstructorNode constructorNode);

View File

@ -1,24 +1,27 @@
package parser.astBuilder;
import ast.*;
import ast.block.BlockNode;
import ast.expression.AssignableExpressionNode;
import ast.expression.ExpressionNode;
import ast.statement.ifstatement.ElseStatementNode;
import ast.statement.ifstatement.IfElseStatementNode;
import ast.member.ConstructorNode;
import ast.member.MemberNode;
import ast.parameter.ParameterNode;
import ast.statement.*;
import ast.statement.ifstatement.IfStatementNode;
import ast.statement.statementexpression.AssignStatementExpressionNode;
import ast.statement.statementexpression.CrementExpressionStatementExpressionNode;
import ast.statement.statementexpression.methodcallstatementnexpression.ChainedMethodNode;
import ast.statement.statementexpression.methodcallstatementnexpression.MethodCallStatementExpressionNode;
import ast.statement.statementexpression.methodcallstatementnexpression.TargetNode;
import ast.statements.BlockStatementNode;
import ast.expressions.AssignableExpressionNode;
import ast.expressions.ExpressionNode;
import ast.statements.ElseStatementNode;
import ast.statements.IfElseStatementNode;
import ast.members.ConstructorNode;
import ast.members.MemberNode;
import ast.parameters.ParameterNode;
import ast.statements.*;
import ast.statements.IfStatementNode;
import ast.statementexpressions.AssignStatementExpressionNode;
import ast.statementexpressions.CrementExpressionStatementExpressionNode;
import ast.statementexpressions.methodcallstatementnexpression.ChainedMethodNode;
import ast.statementexpressions.methodcallstatementnexpression.MethodCallStatementExpressionNode;
import ast.statementexpressions.methodcallstatementnexpression.TargetNode;
import ast.type.*;
import parser.generated.*;
import java.util.ArrayList;
import java.util.List;
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitProgram(SimpleJavaParser.ProgramContext ctx) {
@ -41,7 +44,7 @@ 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.block()));
ConstructorNode constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockStatementNode) visit(ctx.block()));
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
constructorNode.addParameter((ParameterNode) visit(parameter));
}
@ -59,8 +62,8 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return visitReturnStatement(ctx.returnStatement());
} else if(ctx.localVariableDeclaration() != null) {
return visitLocalVariableDeclaration(ctx.localVariableDeclaration());
} else if(ctx.block() != null) {
return visitBlock(ctx.block());
} else if(ctx.blockStatement() != null) {
return visitBlockStatement(ctx.blockStatement());
} else if(ctx.whileStatement() != null) {
return visitWhileStatement(ctx.whileStatement());
} else if(ctx.forStatement() != null) {
@ -84,8 +87,8 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
}
@Override
public ASTNode visitBlock(SimpleJavaParser.BlockContext ctx) {
BlockNode blockNode = new BlockNode();
public ASTNode visitBlockStatement(SimpleJavaParser.BlockStatementContext ctx) {
BlockStatementNode blockNode = new BlockStatementNode();
for(SimpleJavaParser.StatementContext statement : ctx.statement()) {
blockNode.addStatement((StatementNode) visit(statement));
}
@ -94,36 +97,86 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitWhileStatement(SimpleJavaParser.WhileStatementContext ctx) {
return new WhileStatementNode((ExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.block()));
return new WhileStatementNode((ExpressionNode) visit(ctx.expression()), (BlockStatementNode) visit(ctx.blockStatement()));
}
@Override
public ASTNode visitDoWhileStatement(SimpleJavaParser.DoWhileStatementContext ctx) {
ExpressionNode condition = (ExpressionNode) visit(ctx.expression());
BlockStatementNode doBlock = (BlockStatementNode) visit(ctx.blockStatement());
WhileStatementNode whileStatement = new WhileStatementNode(condition, doBlock);
BlockStatementNode resultBlock = new BlockStatementNode();
resultBlock.addStatement(doBlock);
resultBlock.addStatement(whileStatement);
return resultBlock;
}
@Override
public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) {
if(ctx.statementExpression(0) != null) {
return new ForStatementNode((ExpressionNode) visit(ctx.statementExpression(0)), (ExpressionNode) visit(ctx.expression()), (ExpressionNode) visit(ctx.statementExpression(1)));
} else if(ctx.localVariableDeclaration() != null) {
return new ForStatementNode((StatementNode) visit(ctx.localVariableDeclaration()), (ExpressionNode) visit(ctx.expression()), (ExpressionNode) visit(ctx.statementExpression(1)));
List<StatementNode> statements = new ArrayList<>();
//init
if(ctx.statementExpression(0) != null){
statements.add((StatementNode) visit(ctx.statementExpression(0)));
} else if (ctx.localVariableDeclaration() != null) {
statements.add((StatementNode) visit(ctx.localVariableDeclaration()));
}
return null;
//condition
ExpressionNode condition = (ExpressionNode) visit(ctx.expression());
//ink
StatementNode increment = null;
if(ctx.statementExpression(1) != null){
increment = (StatementNode) visit(ctx.statementExpression(1));
}
BlockStatementNode forBlock = (BlockStatementNode) visit(ctx.blockStatement());
if(increment != null){
forBlock.addStatement((increment));
}
WhileStatementNode whileStatement = new WhileStatementNode(condition, forBlock);
statements.add(whileStatement);
BlockStatementNode resultBlock = new BlockStatementNode();
for(StatementNode statement : statements) {
resultBlock.addStatement(statement);
}
return resultBlock;
}
@Override
public ASTNode visitIfElseStatement(SimpleJavaParser.IfElseStatementContext ctx) {
IfElseStatementNode ifElseStatementNode = new IfElseStatementNode((IfStatementNode) visit(ctx.ifStatement()));
for(SimpleJavaParser.ElseStatementContext elseStatement : ctx.elseStatement()) {
ifElseStatementNode.addElseStatement((ElseStatementNode) visit(elseStatement));
IfElseStatementNode ifElseStatementNode = new IfElseStatementNode((IfStatementNode) visit(ctx.ifStatement()),
(ElseStatementNode) visit(ctx.elseStatement()));
for (SimpleJavaParser.ElseIfStatementContext elseIfStatement : ctx.elseIfStatement()){
ifElseStatementNode.addElseIfStatement(((IfStatementNode) visit(elseIfStatement)));
}
return ifElseStatementNode;
}
@Override
public ASTNode visitIfStatement(SimpleJavaParser.IfStatementContext ctx) {
return new IfStatementNode((ExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.block()));
return new IfStatementNode((ExpressionNode) visit(ctx.expression()), (BlockStatementNode) visit(ctx.blockStatement()));
}
@Override
public ASTNode visitElseIfStatement(SimpleJavaParser.ElseIfStatementContext ctx) {
return new IfStatementNode((ExpressionNode) visit(ctx.expression()), (BlockStatementNode) visit(ctx.blockStatement()));
}
@Override
public ASTNode visitElseStatement(SimpleJavaParser.ElseStatementContext ctx) {
return new ElseStatementNode((BlockNode) visit(ctx.block()));
return new ElseStatementNode((BlockStatementNode) visit(ctx.blockStatement()));
}
@Override

View File

@ -6,9 +6,9 @@ program: classDeclaration+;
classDeclaration: AccessModifier? 'class' Identifier OpenCurlyBracket memberDeclaration* ClosedCurlyBracket;
memberDeclaration: constructorDeclaration | fieldDeclaration | methodDeclaration;
constructorDeclaration: AccessModifier? Identifier OpenRoundBracket parameterList? ClosedRoundBracket block;
constructorDeclaration: AccessModifier? Identifier OpenRoundBracket parameterList? ClosedRoundBracket blockStatement;
fieldDeclaration: AccessModifier? type Identifier Semicolon;
methodDeclaration: MainMethodDeclaration block | AccessModifier? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket block;
methodDeclaration: MainMethodDeclaration blockStatement | AccessModifier? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket blockStatement;
parameterList: parameter (Comma parameter)*;
parameter: type Identifier;
@ -17,23 +17,26 @@ argumentList: (expression (Comma expression)*)?;
// Anweisungen
statement: returnStatement Semicolon
| localVariableDeclaration Semicolon
| block
| blockStatement
| whileStatement
| doWhileStatement
| forStatement
| ifElseStatement
| statementExpression Semicolon;
block: OpenCurlyBracket statement* ClosedCurlyBracket;
blockStatement: OpenCurlyBracket statement* ClosedCurlyBracket;
returnStatement: Return (expression)?;
localVariableDeclaration: type Identifier (Assign expression)?;
whileStatement: While OpenRoundBracket expression ClosedRoundBracket block;
forStatement: For OpenRoundBracket (statementExpression | localVariableDeclaration) Semicolon (expression)? Semicolon (statementExpression)? ClosedRoundBracket block;
whileStatement: While OpenRoundBracket expression ClosedRoundBracket blockStatement;
doWhileStatement: Do blockStatement While OpenRoundBracket expression ClosedRoundBracket Semicolon;
forStatement: For OpenRoundBracket (statementExpression | localVariableDeclaration) Semicolon (expression)? Semicolon (statementExpression)? ClosedRoundBracket blockStatement;
ifElseStatement: ifStatement elseStatement*;
ifStatement: If OpenRoundBracket expression ClosedRoundBracket block;
elseStatement: Else block;
ifElseStatement: ifStatement elseIfStatement* elseStatement?;
ifStatement: If OpenRoundBracket expression ClosedRoundBracket blockStatement;
elseIfStatement: Else If OpenRoundBracket expression ClosedRoundBracket blockStatement;
elseStatement: Else blockStatement;
statementExpression: assign | newDeclaration | methodCall | crementExpression;
assign: assignableExpression Assign expression;
@ -144,6 +147,7 @@ Comma: ',';
Class: 'class';
This: 'this';
While: 'while';
Do: 'do';
If: 'if';
Else: 'else';
For: 'for';

View File

@ -2,17 +2,17 @@ package semantic;
import ast.ClassNode;
import ast.expression.LiteralNode;
import ast.expressions.LiteralNode;
import ast.ProgramNode;
import ast.expression.BinaryExpressionNode;
import ast.expression.IdentifierExpressionNode;
import ast.expression.InstVar;
import ast.expression.UnaryExpressionNode;
import ast.member.FieldNode;
import ast.member.MethodNode;
import ast.statement.*;
import ast.expression.This;
import ast.statement.ifstatement.IfStatementNode;
import ast.expressions.BinaryExpressionNode;
import ast.expressions.IdentifierExpressionNode;
import ast.expressions.InstVar;
import ast.expressions.UnaryExpressionNode;
import ast.members.FieldNode;
import ast.members.MethodNode;
import ast.statements.*;
import ast.expressions.This;
import ast.statements.IfStatementNode;
import typechecker.TypeCheckResult;
public interface SemanticVisitor {