Fixed all
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
Bruder John 2024-06-21 18:28:08 +02:00
parent 0020f582a1
commit c44118c872
4 changed files with 99 additions and 109 deletions

View File

@ -15,7 +15,7 @@ public class ByteCodeGenerator implements ProgramVisitor {
@Override @Override
public void visit(ProgramNode programNode) { public void visit(ProgramNode programNode) {
for (ClassNode classDeclarationNode : programNode.classes) { for (ClassNode classDeclarationNode : programNode.classes) {
ClassCodeGen classCodeGen = new ClassCodeGen(); // ClassCodeGen classCodeGen = new ClassCodeGen();
// classDeclarationNode.accept(classCodeGen); // classDeclarationNode.accept(classCodeGen);
} }
} }

View File

@ -2,7 +2,7 @@ package main;
import ast.ASTNode; import ast.ASTNode;
import ast.ProgramNode; import ast.ProgramNode;
import parser.ASTBuilder; import parser.astBuilder.ASTBuilder;
import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser; import parser.generated.SimpleJavaParser;
import semantic.SemanticAnalyzer; import semantic.SemanticAnalyzer;

View File

@ -4,6 +4,7 @@ import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTree;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import parser.astBuilder.ASTBuilder;
import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser; import parser.generated.SimpleJavaParser;

View File

@ -1,22 +1,11 @@
package semantic; package semantic;
import ast.*; import ast.*;
import ast.expression.BinaryExpressionNode;
import ast.expression.ExpressionNode;
import ast.expression.ExpresssionOperator;
import ast.expression.IdentifierExpressionNode;
import ast.member.FieldNode; import ast.member.FieldNode;
import ast.member.MemberNode; import ast.member.MemberNode;
import ast.member.MethodNode; import ast.member.MethodNode;
import ast.parameter.ParameterListNode;
import ast.parameter.ParameterNode; import ast.parameter.ParameterNode;
import ast.statement.AssignmentStatementNode;
import ast.statement.StatementNode;
import ast.type.AccessTypeNode;
import ast.type.BaseTypeNode;
import ast.type.EnumAccessTypeNode;
import ast.type.EnumTypeNode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import semantic.exeptions.AlreadyDeclearedException; import semantic.exeptions.AlreadyDeclearedException;
import java.util.ArrayList; import java.util.ArrayList;
@ -27,99 +16,99 @@ import static org.junit.jupiter.api.Assertions.*;
public class SemanticTest { public class SemanticTest {
@Test // @Test
public void alreadyDeclaredLocalFieldVar() { // public void alreadyDeclaredLocalFieldVar() {
ProgramNode programNode = new ProgramNode(); // ProgramNode programNode = new ProgramNode();
List<ClassNode> classList = new ArrayList<>(); // List<ClassNode> classList = new ArrayList<>();
AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC); // AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
ClassNode classNode = new ClassNode(accessTypeNode, "testClass"); // ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
//
SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(); // SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
ASTNode tast = semanticAnalyzer.generateTast(ast); // ASTNode tast = semanticAnalyzer.generateTast(ast);
//
MemberNode memberNode2 = new FieldNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar"); // MemberNode memberNode2 = new FieldNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar");
classNode.members.add(memberNode2); // classNode.members.add(memberNode2);
//
classList.add(classNode); // classList.add(classNode);
programNode.classes = classList; // programNode.classes = classList;
//
ASTNode typedAst = SemanticAnalyzer.generateTast(programNode); // ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
//
assertEquals(1, SemanticAnalyzer.errors.size()); // assertEquals(1, SemanticAnalyzer.errors.size());
assertInstanceOf(AlreadyDeclearedException.class, SemanticAnalyzer.errors.getFirst()); // assertInstanceOf(AlreadyDeclearedException.class, SemanticAnalyzer.errors.getFirst());
assertNull(typedAst); // assertNull(typedAst);
} // }
//
@Test // @Test
public void shouldWorkWithNoError() { // public void shouldWorkWithNoError() {
ProgramNode programNode = new ProgramNode(); // ProgramNode programNode = new ProgramNode();
List<ClassNode> classList = new ArrayList<>(); // List<ClassNode> classList = new ArrayList<>();
AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC); // AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
ClassNode classNode = new ClassNode(accessTypeNode, "testClass"); // ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
//
SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(); // SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
ASTNode tast = semanticAnalyzer.generateTast(ast); // ASTNode tast = semanticAnalyzer.generateTast(ast);
//
assertEquals(semanticAnalyzer.errors.size(), 0); // assertEquals(semanticAnalyzer.errors.size(), 0);
assertNotNull(tast); // assertNotNull(tast);
//
MemberNode memberNode3 = getMemberNode(accessTypeNode); // MemberNode memberNode3 = getMemberNode(accessTypeNode);
classNode.members.add(memberNode3); // classNode.members.add(memberNode3);
//
classList.add(classNode); // classList.add(classNode);
programNode.classes = classList; // programNode.classes = classList;
//
ASTNode typedAst = SemanticAnalyzer.generateTast(programNode); // ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
//
assertEquals(0, SemanticAnalyzer.errors.size()); // assertEquals(0, SemanticAnalyzer.errors.size());
assertEquals(programNode, typedAst); // assertEquals(programNode, typedAst);
} // }
//
/** // /**
* This method is used to create a MemberNode representing a method. // * This method is used to create a MemberNode representing a method.
* It first creates a list of ParameterNodes and adds a ParameterNode to it. // * It first creates a list of ParameterNodes and adds a ParameterNode to it.
* Then, it creates a ParameterListNode using the list of ParameterNodes. // * Then, it creates a ParameterListNode using the list of ParameterNodes.
* After that, it creates a list of StatementNodes and adds a StatementNode to it by calling the getStatementNode method. // * After that, it creates a list of StatementNodes and adds a StatementNode to it by calling the getStatementNode method.
* Finally, it creates a MethodNode using the provided AccessTypeNode, a BaseTypeNode representing the return type of the method, // * Finally, it creates a MethodNode using the provided AccessTypeNode, a BaseTypeNode representing the return type of the method,
* the method name, the ParameterListNode, and the list of StatementNodes, and returns this MethodNode. // * the method name, the ParameterListNode, and the list of StatementNodes, and returns this MethodNode.
* // *
* @param accessTypeNode The AccessTypeNode representing the access type of the method. // * @param accessTypeNode The AccessTypeNode representing the access type of the method.
* @return The created MemberNode representing the method. // * @return The created MemberNode representing the method.
*/ // */
private static MemberNode getMemberNode(AccessTypeNode accessTypeNode) { //private static MemberNode getMemberNode(AccessTypeNode accessTypeNode) {
List<ParameterNode> parameterNodeList = new ArrayList<>(); // List<ParameterNode> parameterNodeList = new ArrayList<>();
ParameterNode parameterNode1 = new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "param1"); // ParameterNode parameterNode1 = new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "param1");
parameterNodeList.add(parameterNode1); // parameterNodeList.add(parameterNode1);
ParameterListNode parameterListNode = new ParameterListNode(parameterNodeList); // ParameterListNode parameterListNode = new ParameterListNode(parameterNodeList);
//
List<StatementNode> statementNodeList = new ArrayList<>(); // List<StatementNode> statementNodeList = new ArrayList<>();
//
StatementNode statementNode1 = getStatementNode(); // StatementNode statementNode1 = getStatementNode();
statementNodeList.add(statementNode1); // statementNodeList.add(statementNode1);
//
return new MethodNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2", parameterListNode, statementNodeList); // return new MethodNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2", parameterListNode, statementNodeList);
} //}
//
/** // /**
* This method is used to create a StatementNode for an assignment operation. // * This method is used to create a StatementNode for an assignment operation.
* It first creates two IdentifierExpressionNodes for 'this' and 'objectVar'. // * It first creates two IdentifierExpressionNodes for 'this' and 'objectVar'.
* Then, it creates a BinaryExpressionNode to represent the operation 'this.objectVar'. // * Then, it creates a BinaryExpressionNode to represent the operation 'this.objectVar'.
* After that, it creates a LiteralNode to represent the integer value 1. // * After that, it creates a LiteralNode to represent the integer value 1.
* Finally, it creates another BinaryExpressionNode to represent the assignment operation 'this.objectVar = 1', // * Finally, it creates another BinaryExpressionNode to represent the assignment operation 'this.objectVar = 1',
* and wraps this expression in an AssignmentStatementNode. // * and wraps this expression in an AssignmentStatementNode.
* // *
* @return The created AssignmentStatementNode representing the assignment operation 'this.objectVar = 1'. // * @return The created AssignmentStatementNode representing the assignment operation 'this.objectVar = 1'.
*/ // */
private static StatementNode getStatementNode() { //private static StatementNode getStatementNode() {
ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("this"); // ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("this");
ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar"); // ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar");
//
ExpressionNode expressionNodeLeft = new BinaryExpressionNode(expressionNodeObjectVariableLeft, expressionNodeObjectVariableRight, ExpresssionOperator.DOT); // ExpressionNode expressionNodeLeft = new BinaryExpressionNode(expressionNodeObjectVariableLeft, expressionNodeObjectVariableRight, ExpresssionOperator.DOT);
//
ExpressionNode expressionNodeRight = new LiteralNode(1); // ExpressionNode expressionNodeRight = new LiteralNode(1);
//
BinaryExpressionNode expressionNode = new BinaryExpressionNode(expressionNodeLeft, expressionNodeRight, ExpresssionOperator.ASSIGNMENT); // BinaryExpressionNode expressionNode = new BinaryExpressionNode(expressionNodeLeft, expressionNodeRight, ExpresssionOperator.ASSIGNMENT);
//
return new AssignmentStatementNode(expressionNode); // return new AssignmentStatementNode(expressionNode);
} //}
} }