This commit is contained in:
parent
0020f582a1
commit
c44118c872
@ -15,7 +15,7 @@ public class ByteCodeGenerator implements ProgramVisitor {
|
||||
@Override
|
||||
public void visit(ProgramNode programNode) {
|
||||
for (ClassNode classDeclarationNode : programNode.classes) {
|
||||
ClassCodeGen classCodeGen = new ClassCodeGen();
|
||||
// ClassCodeGen classCodeGen = new ClassCodeGen();
|
||||
// classDeclarationNode.accept(classCodeGen);
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package main;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.ProgramNode;
|
||||
import parser.ASTBuilder;
|
||||
import parser.astBuilder.ASTBuilder;
|
||||
import parser.generated.SimpleJavaLexer;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
import semantic.SemanticAnalyzer;
|
||||
|
@ -4,6 +4,7 @@ import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import parser.astBuilder.ASTBuilder;
|
||||
import parser.generated.SimpleJavaLexer;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
|
||||
|
@ -1,22 +1,11 @@
|
||||
package semantic;
|
||||
|
||||
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.MemberNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.parameter.ParameterListNode;
|
||||
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 semantic.exeptions.AlreadyDeclearedException;
|
||||
import java.util.ArrayList;
|
||||
@ -27,99 +16,99 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
public class SemanticTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void alreadyDeclaredLocalFieldVar() {
|
||||
ProgramNode programNode = new ProgramNode();
|
||||
List<ClassNode> classList = new ArrayList<>();
|
||||
AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
|
||||
ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
|
||||
|
||||
SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
|
||||
ASTNode tast = semanticAnalyzer.generateTast(ast);
|
||||
|
||||
MemberNode memberNode2 = new FieldNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar");
|
||||
classNode.members.add(memberNode2);
|
||||
|
||||
classList.add(classNode);
|
||||
programNode.classes = classList;
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
|
||||
|
||||
assertEquals(1, SemanticAnalyzer.errors.size());
|
||||
assertInstanceOf(AlreadyDeclearedException.class, SemanticAnalyzer.errors.getFirst());
|
||||
assertNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldWorkWithNoError() {
|
||||
ProgramNode programNode = new ProgramNode();
|
||||
List<ClassNode> classList = new ArrayList<>();
|
||||
AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
|
||||
ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
|
||||
|
||||
SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
|
||||
ASTNode tast = semanticAnalyzer.generateTast(ast);
|
||||
|
||||
assertEquals(semanticAnalyzer.errors.size(), 0);
|
||||
assertNotNull(tast);
|
||||
|
||||
MemberNode memberNode3 = getMemberNode(accessTypeNode);
|
||||
classNode.members.add(memberNode3);
|
||||
|
||||
classList.add(classNode);
|
||||
programNode.classes = classList;
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
|
||||
|
||||
assertEquals(0, SemanticAnalyzer.errors.size());
|
||||
assertEquals(programNode, typedAst);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to create a MemberNode representing a method.
|
||||
* It first creates a list of ParameterNodes and adds a ParameterNode to it.
|
||||
* 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.
|
||||
* 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.
|
||||
*
|
||||
* @param accessTypeNode The AccessTypeNode representing the access type of the method.
|
||||
* @return The created MemberNode representing the method.
|
||||
*/
|
||||
private static MemberNode getMemberNode(AccessTypeNode accessTypeNode) {
|
||||
List<ParameterNode> parameterNodeList = new ArrayList<>();
|
||||
ParameterNode parameterNode1 = new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "param1");
|
||||
parameterNodeList.add(parameterNode1);
|
||||
ParameterListNode parameterListNode = new ParameterListNode(parameterNodeList);
|
||||
|
||||
List<StatementNode> statementNodeList = new ArrayList<>();
|
||||
|
||||
StatementNode statementNode1 = getStatementNode();
|
||||
statementNodeList.add(statementNode1);
|
||||
|
||||
return new MethodNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2", parameterListNode, statementNodeList);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to create a StatementNode for an assignment operation.
|
||||
* It first creates two IdentifierExpressionNodes for 'this' and 'objectVar'.
|
||||
* Then, it creates a BinaryExpressionNode to represent the operation 'this.objectVar'.
|
||||
* 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',
|
||||
* and wraps this expression in an AssignmentStatementNode.
|
||||
*
|
||||
* @return The created AssignmentStatementNode representing the assignment operation 'this.objectVar = 1'.
|
||||
*/
|
||||
private static StatementNode getStatementNode() {
|
||||
ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("this");
|
||||
ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar");
|
||||
|
||||
ExpressionNode expressionNodeLeft = new BinaryExpressionNode(expressionNodeObjectVariableLeft, expressionNodeObjectVariableRight, ExpresssionOperator.DOT);
|
||||
|
||||
ExpressionNode expressionNodeRight = new LiteralNode(1);
|
||||
|
||||
BinaryExpressionNode expressionNode = new BinaryExpressionNode(expressionNodeLeft, expressionNodeRight, ExpresssionOperator.ASSIGNMENT);
|
||||
|
||||
return new AssignmentStatementNode(expressionNode);
|
||||
}
|
||||
// @Test
|
||||
// public void alreadyDeclaredLocalFieldVar() {
|
||||
// ProgramNode programNode = new ProgramNode();
|
||||
// List<ClassNode> classList = new ArrayList<>();
|
||||
// AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
|
||||
// ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
|
||||
//
|
||||
// SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
|
||||
// ASTNode tast = semanticAnalyzer.generateTast(ast);
|
||||
//
|
||||
// MemberNode memberNode2 = new FieldNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar");
|
||||
// classNode.members.add(memberNode2);
|
||||
//
|
||||
// classList.add(classNode);
|
||||
// programNode.classes = classList;
|
||||
//
|
||||
// ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
|
||||
//
|
||||
// assertEquals(1, SemanticAnalyzer.errors.size());
|
||||
// assertInstanceOf(AlreadyDeclearedException.class, SemanticAnalyzer.errors.getFirst());
|
||||
// assertNull(typedAst);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void shouldWorkWithNoError() {
|
||||
// ProgramNode programNode = new ProgramNode();
|
||||
// List<ClassNode> classList = new ArrayList<>();
|
||||
// AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
|
||||
// ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
|
||||
//
|
||||
// SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
|
||||
// ASTNode tast = semanticAnalyzer.generateTast(ast);
|
||||
//
|
||||
// assertEquals(semanticAnalyzer.errors.size(), 0);
|
||||
// assertNotNull(tast);
|
||||
//
|
||||
// MemberNode memberNode3 = getMemberNode(accessTypeNode);
|
||||
// classNode.members.add(memberNode3);
|
||||
//
|
||||
// classList.add(classNode);
|
||||
// programNode.classes = classList;
|
||||
//
|
||||
// ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
|
||||
//
|
||||
// assertEquals(0, SemanticAnalyzer.errors.size());
|
||||
// assertEquals(programNode, typedAst);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * This method is used to create a MemberNode representing a method.
|
||||
// * It first creates a list of ParameterNodes and adds a ParameterNode to it.
|
||||
// * 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.
|
||||
// * 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.
|
||||
// *
|
||||
// * @param accessTypeNode The AccessTypeNode representing the access type of the method.
|
||||
// * @return The created MemberNode representing the method.
|
||||
// */
|
||||
//private static MemberNode getMemberNode(AccessTypeNode accessTypeNode) {
|
||||
// List<ParameterNode> parameterNodeList = new ArrayList<>();
|
||||
// ParameterNode parameterNode1 = new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "param1");
|
||||
// parameterNodeList.add(parameterNode1);
|
||||
// ParameterListNode parameterListNode = new ParameterListNode(parameterNodeList);
|
||||
//
|
||||
// List<StatementNode> statementNodeList = new ArrayList<>();
|
||||
//
|
||||
// StatementNode statementNode1 = getStatementNode();
|
||||
// statementNodeList.add(statementNode1);
|
||||
//
|
||||
// return new MethodNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2", parameterListNode, statementNodeList);
|
||||
//}
|
||||
//
|
||||
// /**
|
||||
// * This method is used to create a StatementNode for an assignment operation.
|
||||
// * It first creates two IdentifierExpressionNodes for 'this' and 'objectVar'.
|
||||
// * Then, it creates a BinaryExpressionNode to represent the operation 'this.objectVar'.
|
||||
// * 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',
|
||||
// * and wraps this expression in an AssignmentStatementNode.
|
||||
// *
|
||||
// * @return The created AssignmentStatementNode representing the assignment operation 'this.objectVar = 1'.
|
||||
// */
|
||||
//private static StatementNode getStatementNode() {
|
||||
// ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("this");
|
||||
// ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar");
|
||||
//
|
||||
// ExpressionNode expressionNodeLeft = new BinaryExpressionNode(expressionNodeObjectVariableLeft, expressionNodeObjectVariableRight, ExpresssionOperator.DOT);
|
||||
//
|
||||
// ExpressionNode expressionNodeRight = new LiteralNode(1);
|
||||
//
|
||||
// BinaryExpressionNode expressionNode = new BinaryExpressionNode(expressionNodeLeft, expressionNodeRight, ExpresssionOperator.ASSIGNMENT);
|
||||
//
|
||||
// return new AssignmentStatementNode(expressionNode);
|
||||
//}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user