euqals methods overriden in "Program, RefType, MethodDecl and FieldDecl" for AST comparison.

This commit is contained in:
Julian Murek 2024-05-14 15:34:26 +02:00
parent 35fba57efa
commit 3be8d9854a
11 changed files with 289 additions and 7 deletions

View File

@ -12,6 +12,7 @@ import org.objectweb.asm.Opcodes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
public class FieldDecl extends AbstractType implements Node {
@ -58,4 +59,13 @@ public class FieldDecl extends AbstractType implements Node {
return "L" + type + ";";
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FieldDecl fieldDecl = (FieldDecl) o;
return ( Objects.equals(type, fieldDecl.type)
&& Objects.equals(identifier, fieldDecl.identifier));
}
}

View File

@ -9,10 +9,7 @@ import abstractSyntaxTree.Statement.BlockStatement;
import abstractSyntaxTree.Statement.IStatement;
import org.objectweb.asm.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.*;
public class MethodDecl implements Node {
@ -160,4 +157,16 @@ public class MethodDecl implements Node {
}
return descriptor.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MethodDecl methodDecl = (MethodDecl) o;
return (Objects.equals(name, methodDecl.name)
&& Objects.equals(parameters, methodDecl.parameters)
&& Objects.equals(returnType, methodDecl.returnType)
&& Objects.equals(codeBlock, methodDecl.codeBlock));
}
}

View File

@ -10,6 +10,7 @@ import org.objectweb.asm.Opcodes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
public class RefType extends AbstractType implements Node {
@ -89,5 +90,16 @@ public class RefType extends AbstractType implements Node {
cw.visitEnd();
}
@Override
public boolean equals(Object o) {
System.out.println("Dont forget me ;)");
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RefType refType = (RefType) o;
return ( Objects.equals(name, refType.name)
&& Objects.equals(fieldDecls, refType.fieldDecls)
&& Objects.equals(methodDecls, refType.methodDecls)
&& Objects.equals(hasMain, refType.hasMain));
}
}

View File

@ -102,4 +102,14 @@ public class Program implements Node {
}
*/
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Program program = (Program) o;
System.out.println(classes);
System.out.println(program.classes);
return (Objects.equals(classes, program.classes));
}
}

View File

@ -0,0 +1,44 @@
package AST;
import ASTs.emptyClassAST;
import abstractSyntaxTree.Program;
import static org.junit.Assert.assertEquals;
import abstractSyntaxTree.Statement.BlockStatement;
import gen.DecafLexer;
import gen.DecafParser;
import org.antlr.v4.runtime.tree.ParseTree;
import org.junit.Test;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.antlr.v4.runtime.*;
import abstractSyntaxTree.ASTGenerator;
public class AstComparer {
private static String BASE_DIR;
public AstComparer(String base_directory){
BASE_DIR = base_directory + "/";
}
public void astComparison(String sourceFilePath, Program expectedAST) throws Exception {
// Read the source file
String content = new String(Files.readAllBytes(Paths.get(BASE_DIR + sourceFilePath)));
CharStream codeCharStream = CharStreams.fromString(content);
// Setup Lexer and Parser
DecafLexer lexer = new DecafLexer(codeCharStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
DecafParser parser = new DecafParser(tokens);
ParseTree tree = parser.program();
// Generate AST
ASTGenerator generator = new ASTGenerator();
Program generatedAST = (Program) generator.visit(tree);
// Assert that both ASTs are equal
assertEquals("The generated AST does not match the expected AST", expectedAST, generatedAST);
}
}

View File

@ -0,0 +1,28 @@
package AST;
import ASTs.emptyClassAST;
import abstractSyntaxTree.Program;
import org.junit.Test;
import java.io.File;
public class testAll {
AstComparer astComparer;
public testAll()
{
this.astComparer = new AstComparer("src/test/resources");
}
@Test
public void TestAstGeneration()
{
System.out.println("Current working directory: " + new File(".").getAbsolutePath());
Program testEmptyClassAST = emptyClassAST.getEmptyProgramm();
try {
astComparer.astComparison("basicClasses/emptyClass.java", testEmptyClassAST);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,24 @@
package ASTs;
import abstractSyntaxTree.*;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import java.util.ArrayList;
import java.util.List;
public class emptyClassAST {
public static Program getEmptyProgramm() {
List<FieldDecl> emptyFieldDeclList = new ArrayList<>();
List<MethodDecl> emptyMethodDeclList = new ArrayList<>();
RefType emptyClass = new RefType("emptyClass", emptyFieldDeclList, emptyMethodDeclList, false);
List<RefType> classes = new ArrayList<>();
classes.add(emptyClass);
return (new Program(classes));
}
}

View File

@ -0,0 +1,34 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import abstractSyntaxTree.Statement.IStatement;
import java.util.ArrayList;
import java.util.List;
public class emptyClassWithConstructorAST extends Program {
public emptyClassWithConstructorAST()
{
super(staticClasses());
}
private static List<RefType> staticClasses() {
List<FieldDecl> fieldDeclList = new ArrayList<>();
ParameterList emptyParameterList = new ParameterList(new ArrayList<>());
List<IStatement> emptyIStatementList = new ArrayList<>();
BlockStatement emptyBlockStatement = new BlockStatement(emptyIStatementList, "void");
MethodDecl constructor = new MethodDecl("emptyClassWithConstructor", "null", "emptyClassWithConstructor", emptyParameterList, emptyBlockStatement);
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(constructor);
RefType emptyClass = new RefType("emptyClass", fieldDeclList, methodDeclList, false);
List<RefType> classes = new ArrayList<>();
classes.add(emptyClass);
return classes;
}
}

View File

@ -0,0 +1,80 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Expression.InstVarExpression;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import abstractSyntaxTree.Statement.IStatement;
import abstractSyntaxTree.Statement.ReturnStatement;
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
import abstractSyntaxTree.StatementExpression.NewStatementExpression;
import java.util.ArrayList;
import java.util.List;
/*
public class fourClassesAST extends Program {
public fourClassesAST()
{
super(staticClasses());
}
private static List<RefType> staticClasses() {
/////////// Classes ///////////
///////// Class: FourClasses /////////
/////// Fields ///////
List<FieldDecl> fieldDeclList = new ArrayList<>();
ParameterList emptyParameterList = new ParameterList(new ArrayList<>());
/////// Methods ///////
// Main //
List<IStatement> emptyIStatementList = new ArrayList<>();
BlockStatement emptyBlockStatement = new BlockStatement(emptyIStatementList, "void");
MethodDecl constructor = new MethodDecl("emptyClassWithConstructor", "null", "emptyClassWithConstructor", emptyParameterList, emptyBlockStatement);
//IStatement
List<IStatement> fourClassesMainBlockIstatements = new ArrayList<>();
IExpression atntiLeft = new InstVarExpression("Test", "t");
IExpression atntiRight = new NewStatementExpression();
AssignStatementExpression atnti = new AssignStatementExpression(atntiLeft, "=", );
ReturnStatement fcmbiReturn = new ReturnStatement();
fourClassesMainBlockIstatements.add(atnti);
//BlockStatement
BlockStatement fourClassesMainBlock = new BlockStatement(fourClassesMainBlockIstatements, "int");
//Parameter
Parameter intI = new Parameter("int", "i");
List<Parameter> fourClassesMainParameterList = new ArrayList<>();
ParameterList fourClassesMainParameters = new ParameterList(fourClassesMainParameterList);
MethodDecl fourClassesMain = new MethodDecl("fourClasses", "int", "main", fourClassesMainParameters, fourClassesMainBlock);
List<MethodDecl> MethodDeclList = new ArrayList<>();
RefType emptyClass = new RefType("emptyClass", fieldDeclList, MethodDeclList, false);
List<RefType> classes = new ArrayList<>();
classes.add(emptyClass);
return classes;
}
}*/

View File

@ -5,7 +5,6 @@ import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Token;
import org.junit.Test;
import java.nio.file.Files;
import java.nio.file.Paths;
@ -40,8 +39,6 @@ public class JavaLexerTest {
}*/
// Compare tokens
//assertEquals("Number of tokens does not match expected", expectedTokens.size(), tokenList.size());
for (int i = 0; i < tokenList.size(); i++) {
Token token = tokenList.get(i);
String tokenData = String.format("%s: \"%s\"", lexer.getVocabulary().getSymbolicName(token.getType()), token.getText());

View File

@ -0,0 +1,34 @@
package Tokens;
import org.junit.Test;
public class TestAll {
JavaLexerTest testLexer;
public TestAll(){
testLexer = new JavaLexerTest("src/test/resources");
}
private static void main(String[] args){
TestAll tester = new TestAll();
}
@Test
public void testEmptyClass() throws Exception {
testLexer.testTokens("basicClasses/emptyClass.java", "basicClasses/emptyClass.tokens");
}
@Test
public void testEmptyClassWithConstructor() throws Exception {
testLexer.testTokens("basicClasses/EmptyClassWithConstructor.java", "basicClasses/EmptyClassWithConstructor.tokens");
}
@Test
public void testFourClasses() throws Exception {
testLexer.testTokens("basicClasses/FourClasses.java", "basicClasses/FourClasses.tokens");
}
@Test
public void testPublicEmptyClass() throws Exception {
testLexer.testTokens("basicClasses/PublicEmptyClass.java", "basicClasses/PublicEmptyClass.tokens");
}
}