From 88a25207e981a9c00be7486bc26f04f298bf4a3d Mon Sep 17 00:00:00 2001 From: Maximilian Stahl Date: Wed, 26 Jun 2024 17:34:40 +0200 Subject: [PATCH] Added Interface and fixed bugs --- src/main/java/ast/statements/BlockNode.java | 6 +--- src/main/java/ast/statements/ElseNode.java | 8 ++++- src/main/java/ast/statements/IfElseNode.java | 8 ++++- src/main/java/ast/statements/IfNode.java | 9 ++++- .../java/ast/statements/StatementNode.java | 2 -- src/main/java/ast/statements/WhileNode.java | 14 +++++++- .../java/parser/astBuilder/ASTBuilder.java | 35 ++++++++++++------- src/main/resources/input/CompilerInput.java | 24 ++++++------- 8 files changed, 69 insertions(+), 37 deletions(-) delete mode 100644 src/main/java/ast/statements/StatementNode.java diff --git a/src/main/java/ast/statements/BlockNode.java b/src/main/java/ast/statements/BlockNode.java index ca14404..7a5a2ea 100644 --- a/src/main/java/ast/statements/BlockNode.java +++ b/src/main/java/ast/statements/BlockNode.java @@ -8,17 +8,13 @@ import visitor.Visitable; import java.util.ArrayList; import java.util.List; -public class BlockNode implements ASTNode, Visitable { +public class BlockNode implements IStatementNode, Visitable { public List statements = new ArrayList<>(); - public Boolean hasReturnStatement = false; public BlockNode() {} public void addStatement(IStatementNode statement) { statements.add(statement); - if(statement instanceof ReturnNode) { - hasReturnStatement = true; - } } @Override diff --git a/src/main/java/ast/statements/ElseNode.java b/src/main/java/ast/statements/ElseNode.java index 5b498cf..78b62f1 100644 --- a/src/main/java/ast/statements/ElseNode.java +++ b/src/main/java/ast/statements/ElseNode.java @@ -1,8 +1,10 @@ package ast.statements; import ast.ASTNode; +import semantic.SemanticVisitor; +import typechecker.TypeCheckResult; -public class ElseNode implements ASTNode { +public class ElseNode implements IStatementNode { BlockNode block; public ElseNode(BlockNode block) { @@ -10,4 +12,8 @@ public class ElseNode implements ASTNode { } + @Override + public TypeCheckResult accept(SemanticVisitor visitor) { + return null; + } } diff --git a/src/main/java/ast/statements/IfElseNode.java b/src/main/java/ast/statements/IfElseNode.java index 9d25390..34d40f3 100644 --- a/src/main/java/ast/statements/IfElseNode.java +++ b/src/main/java/ast/statements/IfElseNode.java @@ -1,11 +1,13 @@ package ast.statements; import ast.ASTNode; +import semantic.SemanticVisitor; +import typechecker.TypeCheckResult; import java.util.ArrayList; import java.util.List; -public class IfElseNode implements ASTNode { +public class IfElseNode implements IStatementNode { IfNode ifStatement; List elseIfStatements = new ArrayList<>(); ElseNode elseStatement; @@ -19,4 +21,8 @@ public class IfElseNode implements ASTNode { elseIfStatements.add(elseIfStament); } + @Override + public TypeCheckResult accept(SemanticVisitor visitor) { + return null; + } } diff --git a/src/main/java/ast/statements/IfNode.java b/src/main/java/ast/statements/IfNode.java index 3292a10..2f30788 100644 --- a/src/main/java/ast/statements/IfNode.java +++ b/src/main/java/ast/statements/IfNode.java @@ -2,8 +2,10 @@ package ast.statements; import ast.ASTNode; import ast.expressions.IExpressionNode; +import semantic.SemanticVisitor; +import typechecker.TypeCheckResult; -public class IfNode implements ASTNode { +public class IfNode implements IStatementNode { IExpressionNode expression; BlockNode block; @@ -11,4 +13,9 @@ public class IfNode implements ASTNode { this.expression = expression; this.block = block; } + + @Override + public TypeCheckResult accept(SemanticVisitor visitor) { + return null; + } } diff --git a/src/main/java/ast/statements/StatementNode.java b/src/main/java/ast/statements/StatementNode.java deleted file mode 100644 index 24985c1..0000000 --- a/src/main/java/ast/statements/StatementNode.java +++ /dev/null @@ -1,2 +0,0 @@ -package ast.statements;public class StatementNode { -} diff --git a/src/main/java/ast/statements/WhileNode.java b/src/main/java/ast/statements/WhileNode.java index fe9bfd2..01c1160 100644 --- a/src/main/java/ast/statements/WhileNode.java +++ b/src/main/java/ast/statements/WhileNode.java @@ -2,8 +2,10 @@ package ast.statements; import ast.ASTNode; import ast.expressions.IExpressionNode; +import semantic.SemanticVisitor; +import typechecker.TypeCheckResult; -public class WhileNode extends StatementNode implements ASTNode { +public class WhileNode implements IStatementNode { IExpressionNode expression; BlockNode block; @@ -11,4 +13,14 @@ public class WhileNode extends StatementNode implements ASTNode { this.expression = expression; this.block = block; } + + public void test() { + return; + + } + + @Override + public TypeCheckResult accept(SemanticVisitor visitor) { + return null; + } } diff --git a/src/main/java/parser/astBuilder/ASTBuilder.java b/src/main/java/parser/astBuilder/ASTBuilder.java index 224b59d..bf84fd4 100644 --- a/src/main/java/parser/astBuilder/ASTBuilder.java +++ b/src/main/java/parser/astBuilder/ASTBuilder.java @@ -108,6 +108,8 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { return visitBlockStatement(ctx.blockStatement()); } else if(ctx.whileStatement() != null) { return visitWhileStatement(ctx.whileStatement()); + } else if(ctx.doWhileStatement() != null) { + return visitDoWhileStatement(ctx.doWhileStatement()); } else if(ctx.forStatement() != null) { return visitForStatement(ctx.forStatement()); } else if(ctx.ifElseStatement() != null) { @@ -131,10 +133,15 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitBlockStatement(SimpleJavaParser.BlockStatementContext ctx) { BlockNode blockNode = new BlockNode(); + boolean hasReturnStatement = false; for(SimpleJavaParser.StatementContext statement : ctx.statement()) { - blockNode.addStatement((IStatementNode) visit(statement)); + IStatementNode statementNode = (IStatementNode) visit(statement); + if(statementNode instanceof ReturnNode) { + hasReturnStatement = true; + } + blockNode.addStatement(statementNode); } - if(!blockNode.hasReturnStatement) { + if(!hasReturnStatement) { blockNode.addStatement(new ReturnNode(null)); } return blockNode; @@ -161,28 +168,30 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) { - List statements = new ArrayList<>(); + List 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())); + int i = 0; + if(ctx.localVariableDeclaration() != null) { + statements.add((IStatementNode) visit(ctx.localVariableDeclaration())); + } else if(ctx.statementExpression(i) != null){ + statements.add((IStatementNode) visit(ctx.statementExpression(i))); + i++; } //condition IExpressionNode condition = (IExpressionNode) visit(ctx.expression()); //ink - IStatementNode increment = null; - if(ctx.statementExpression(1) != null){ - increment = (IStatementNode) visit(ctx.statementExpression(1)); + IStatementNode crement = null; + if(ctx.statementExpression(i) != null){ + crement = (IStatementNode) visit(ctx.statementExpression(i)); } BlockNode forBlock = (BlockNode) visit(ctx.blockStatement()); - if(increment != null){ - forBlock.addStatement((increment)); + if(crement != null){ + forBlock.addStatement((crement)); } WhileNode While = new WhileNode(condition, forBlock); @@ -190,7 +199,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { statements.add(While); BlockNode resultBlock = new BlockNode(); - for(StatementNode statement : statements) { + for(IStatementNode statement : statements) { resultBlock.addStatement((IStatementNode) statement); } diff --git a/src/main/resources/input/CompilerInput.java b/src/main/resources/input/CompilerInput.java index d850a3e..825490d 100644 --- a/src/main/resources/input/CompilerInput.java +++ b/src/main/resources/input/CompilerInput.java @@ -1,16 +1,14 @@ -public class CompilerInput { - - public int a; - - public static int testMethod(char x){ - return 0; - } - - public class Test { - - public static int testMethod(char x, int a){ - return 0; - } +public class Compiler { + public int add(int i, int j) { + return i+j; + } +} + +public class Node { + public void main() { + Compiler compiler = new Compiler(); + int i = compiler.add(5, 8); + return i; } }