From b562a789a04d23a611406f3bb3a9ab65dc42fe36 Mon Sep 17 00:00:00 2001 From: i22035 Date: Tue, 25 Jun 2024 22:35:28 +0200 Subject: [PATCH] Fixed Merge Conflicts and Bugs --- .../java/ast/members/ConstructorNode.java | 6 +-- .../crementexpressions/DecrementNode.java | 1 + .../crementexpressions/IncrementNode.java | 1 + .../MethodCallNode.java | 6 +-- src/main/java/ast/statements/BlockNode.java | 3 ++ .../java/parser/astBuilder/ASTBuilder.java | 47 ++++++++++++++++--- src/main/java/semantic/SemanticAnalyzer.java | 4 +- src/main/java/semantic/SemanticVisitor.java | 2 +- src/test/java/semantic/Mocker.java | 10 ++-- src/test/java/semantic/SemanticTest.java | 8 ++-- 10 files changed, 64 insertions(+), 24 deletions(-) diff --git a/src/main/java/ast/members/ConstructorNode.java b/src/main/java/ast/members/ConstructorNode.java index 0030ae2..a70ac85 100644 --- a/src/main/java/ast/members/ConstructorNode.java +++ b/src/main/java/ast/members/ConstructorNode.java @@ -11,17 +11,17 @@ public class ConstructorNode extends MethodNode { public AccessModifierNode accessType; public String identifier; public List parameters = new ArrayList<>(); - public BlockNode body; + public BlockNode block; public ConstructorNode(AccessModifierNode accessType, String identifier) { this.accessType = accessType; this.identifier = identifier; } - public ConstructorNode(String accessType, String identifier, BlockNode body) { + public ConstructorNode(String accessType, String identifier, BlockNode block) { this.accessType = new AccessModifierNode(accessType); this.identifier = identifier; - this.body = body; + this.block = block; } public void addParameter(ParameterNode parameterNode) { diff --git a/src/main/java/ast/statementexpressions/crementexpressions/DecrementNode.java b/src/main/java/ast/statementexpressions/crementexpressions/DecrementNode.java index 48cb82c..86adfce 100644 --- a/src/main/java/ast/statementexpressions/crementexpressions/DecrementNode.java +++ b/src/main/java/ast/statementexpressions/crementexpressions/DecrementNode.java @@ -10,6 +10,7 @@ public class DecrementNode implements IStatementExpressionNode { public AssignableNode assignableExpression; public DecrementNode(CrementType crementType, AssignableNode assignableExpression) { + this.crementType = crementType; this.assignableExpression = assignableExpression; } diff --git a/src/main/java/ast/statementexpressions/crementexpressions/IncrementNode.java b/src/main/java/ast/statementexpressions/crementexpressions/IncrementNode.java index f283db4..e826824 100644 --- a/src/main/java/ast/statementexpressions/crementexpressions/IncrementNode.java +++ b/src/main/java/ast/statementexpressions/crementexpressions/IncrementNode.java @@ -10,6 +10,7 @@ public class IncrementNode implements IStatementExpressionNode { public AssignableNode assignableExpression; public IncrementNode(CrementType crementType, AssignableNode assignableExpression) { + this.crementType = crementType; this.assignableExpression = assignableExpression; } diff --git a/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/MethodCallNode.java b/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/MethodCallNode.java index 5228ed2..b8b7bd3 100644 --- a/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/MethodCallNode.java +++ b/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/MethodCallNode.java @@ -12,7 +12,7 @@ public class MethodCallNode implements IStatementNode { public TargetNode target; public List chainedMethods = new ArrayList<>(); public String identifier; - public List expressions = new ArrayList<>(); + public List parameters = new ArrayList<>(); public MethodCallNode(TargetNode target, String identifier) { this.target = target; @@ -23,8 +23,8 @@ public class MethodCallNode implements IStatementNode { chainedMethods.add(chainedMethode); } - public void addExpression(IExpressionNode expression) { - expressions.add(expression); + public void addExpression(IExpressionNode parameter) { + parameters.add(parameter); } @Override diff --git a/src/main/java/ast/statements/BlockNode.java b/src/main/java/ast/statements/BlockNode.java index 9790f8d..ca14404 100644 --- a/src/main/java/ast/statements/BlockNode.java +++ b/src/main/java/ast/statements/BlockNode.java @@ -16,6 +16,9 @@ public class BlockNode implements ASTNode, Visitable { public void addStatement(IStatementNode statement) { statements.add(statement); + if(statement instanceof ReturnNode) { + hasReturnStatement = true; + } } @Override diff --git a/src/main/java/parser/astBuilder/ASTBuilder.java b/src/main/java/parser/astBuilder/ASTBuilder.java index bb1ba6c..224b59d 100644 --- a/src/main/java/parser/astBuilder/ASTBuilder.java +++ b/src/main/java/parser/astBuilder/ASTBuilder.java @@ -10,8 +10,7 @@ import ast.expressions.binaryexpressions.NonCalculationNode; import ast.expressions.unaryexpressions.MemberAccessNode; import ast.expressions.unaryexpressions.NotNode; import ast.expressions.unaryexpressions.UnaryNode; -import ast.members.ConstructorNode; -import ast.members.MemberNode; +import ast.members.*; import ast.parameters.ParameterNode; import ast.statementexpressions.AssignNode; import ast.statementexpressions.AssignableNode; @@ -23,6 +22,7 @@ import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNod import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode; import ast.statementexpressions.methodcallstatementnexpressions.TargetNode; import ast.statements.*; +import ast.type.AccessModifierNode; import ast.type.type.*; import ast.type.EnumValueNode; import ast.type.ValueNode; @@ -55,15 +55,47 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) { ConstructorNode constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement())); - for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { - constructorNode.addParameter((ParameterNode) visit(parameter)); + if(ctx.parameterList() != null) { + for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { + constructorNode.addParameter((ParameterNode) visit(parameter)); + } } return constructorNode; } + @Override + public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) { + if(ctx.MainMethodDeclaration() != null) { + return new MainMethodNode((BlockNode) visit(ctx.blockStatement())); + } else { + if(ctx.type() != null) { + MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement())); + if(ctx.parameterList() != null) { + for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { + methodNode.addParameter((ParameterNode) visit(parameter)); + } + } + return methodNode; + } else { + MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement())); + if(ctx.parameterList() != null) { + for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { + methodNode.addParameter((ParameterNode) visit(parameter)); + } + } + return methodNode; + } + } + } + + @Override + public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) { + return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText()); + } + @Override public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) { - return new ParameterNode(new TypeNode(ctx.type().getText()), ctx.Identifier().getText()); + return new ParameterNode(createTypeNode(ctx.type().getText()), ctx.Identifier().getText()); } @Override @@ -93,7 +125,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitLocalVariableDeclaration(SimpleJavaParser.LocalVariableDeclarationContext ctx) { - return new LocalVariableDeclarationNode(new TypeNode(ctx.type().getText()), ctx.Identifier().getText(), ctx.Assign().getText(), (IExpressionNode) visit(ctx.expression())); + return new LocalVariableDeclarationNode(createTypeNode(ctx.type().getText()), ctx.Identifier().getText(), ctx.Assign().getText(), (IExpressionNode) visit(ctx.expression())); } @Override @@ -102,6 +134,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { for(SimpleJavaParser.StatementContext statement : ctx.statement()) { blockNode.addStatement((IStatementNode) visit(statement)); } + if(!blockNode.hasReturnStatement) { + blockNode.addStatement(new ReturnNode(null)); + } return blockNode; } diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index 78d660d..4fc169a 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -228,10 +228,10 @@ public class SemanticAnalyzer implements SemanticVisitor { return null; } - @Override + /*@Override public TypeCheckResult analyze(ForNode toCheck) { return null; - } + }*/ @Override public TypeCheckResult analyze(AssignNode toCheck) { diff --git a/src/main/java/semantic/SemanticVisitor.java b/src/main/java/semantic/SemanticVisitor.java index 3119265..974d1de 100644 --- a/src/main/java/semantic/SemanticVisitor.java +++ b/src/main/java/semantic/SemanticVisitor.java @@ -38,7 +38,7 @@ public interface SemanticVisitor { TypeCheckResult analyze(ElseNode toCheck); - TypeCheckResult analyze(ForNode toCheck); + //TypeCheckResult analyze(ForNode toCheck); TypeCheckResult analyze(AssignNode toCheck); diff --git a/src/test/java/semantic/Mocker.java b/src/test/java/semantic/Mocker.java index f061efa..683b385 100644 --- a/src/test/java/semantic/Mocker.java +++ b/src/test/java/semantic/Mocker.java @@ -42,7 +42,7 @@ public class Mocker { //Statements //Block - methodNode.block = new ast.block.BlockStatementNode(); + methodNode.block = new ast.statements.BlockNode(); c.members.add(methodNode); @@ -57,7 +57,7 @@ public class Mocker { ClassNode c = new ClassNode(); MethodNode methodNode = new MethodNode(); - methodNode.block = new ast.block.BlockStatementNode(); + methodNode.block = new ast.statements.BlockNode(); methodNode.setType(new BaseType(TypeEnum.INT)); methodNode.setIdentifier("testMethod"); @@ -65,7 +65,7 @@ public class Mocker { c.members.add(methodNode); MethodNode methodNode1 = new MethodNode(); - methodNode1.block = new ast.block.BlockStatementNode(); + methodNode1.block = new ast.statements.BlockNode(); methodNode1.setType(new BaseType(TypeEnum.INT)); methodNode1.setIdentifier("testMethod"); @@ -83,13 +83,13 @@ public class Mocker { ClassNode c = new ClassNode(); MethodNode methodNode = new MethodNode(); - methodNode.block = new ast.block.BlockStatementNode(); + methodNode.block = new ast.statements.BlockNode(); methodNode.setIdentifier("testMethod"); c.members.add(methodNode); MethodNode methodNode1 = new MethodNode(); - methodNode1.block = new ast.block.BlockStatementNode(); + methodNode1.block = new ast.statements.BlockNode(); methodNode1.setIdentifier("testMethod1"); c.members.add(methodNode1); diff --git a/src/test/java/semantic/SemanticTest.java b/src/test/java/semantic/SemanticTest.java index 04fda3d..7b8ad6a 100644 --- a/src/test/java/semantic/SemanticTest.java +++ b/src/test/java/semantic/SemanticTest.java @@ -1,10 +1,10 @@ package semantic; import ast.*; -import ast.member.FieldNode; -import ast.member.MemberNode; -import ast.member.MethodNode; -import ast.parameter.ParameterNode; +import ast.members.FieldNode; +import ast.members.MemberNode; +import ast.members.MethodNode; +import ast.parameters.ParameterNode; import org.junit.jupiter.api.Test; import semantic.exeptions.AlreadyDeclearedException;