Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/main/java/TestClass.java
This commit is contained in:
Krauß, Josefine 2024-07-03 14:47:34 +02:00
commit 5d75c23f49
59 changed files with 281 additions and 115 deletions

BIN
CharArgument.class Normal file

Binary file not shown.

BIN
ClassFields.class Normal file

Binary file not shown.

BIN
ClassOne.class Normal file

Binary file not shown.

BIN
ClassTwo.class Normal file

Binary file not shown.

BIN
ConstructorParams.class Normal file

Binary file not shown.

BIN
ConstructorThisDot.class Normal file

Binary file not shown.

BIN
DivMethod.class Normal file

Binary file not shown.

BIN
FieldVar.class Normal file

Binary file not shown.

BIN
FieldWithExpr.class Normal file

Binary file not shown.

BIN
GetterFunction.class Normal file

Binary file not shown.

BIN
IfElseIfStatement.class Normal file

Binary file not shown.

Binary file not shown.

BIN
classWithMain.class Normal file

Binary file not shown.

BIN
getI.class Normal file

Binary file not shown.

View File

@ -29,6 +29,49 @@ public class Program implements Node {
this.classes = classes; this.classes = classes;
} }
public TypeCheckResult typeCheckWithoutMain() throws TypeCheckException {
this.typeContext = new HashMap<>();
this.methodContext = new HashMap<>();
for(RefType oneClass : classes){
// build type context
HashMap<String, String> classVars = new HashMap<>();
for (FieldDecl fieldDecl: oneClass.fieldDecls){
classVars.put(fieldDecl.identifier, fieldDecl.type);
}
typeContext.put(oneClass.name, classVars);
// build method context
HashMap<String, HashMap<String, ParameterList>> identifierAndMethod = new HashMap<>();
for (MethodDecl methodDecl : oneClass.methodDecls){
if(methodDecl.returnType == null) continue;
HashMap<String, ParameterList> returnTypeAndParameter = new HashMap<>();
returnTypeAndParameter.put(methodDecl.returnType, methodDecl.parameters);
identifierAndMethod.put(methodDecl.name, returnTypeAndParameter);
}
methodContext.put(oneClass.name, identifierAndMethod);
}
int mainCounter = 0;
// check if main exists
for(RefType oneClass : classes){
if(oneClass.hasMain)
mainCounter++;
}
if(mainCounter != 1) {
//throw new TypeCheckException("There is not 1 Main method.");
}
// typecheck each class
TypeCheckResult result = new TypeCheckResult();
for(RefType oneClass : classes){
oneClass.typeCheck(methodContext, typeContext);
}
result.type = "program";
return result;
}
public TypeCheckResult typeCheck() throws TypeCheckException { public TypeCheckResult typeCheck() throws TypeCheckException {
this.typeContext = new HashMap<>(); this.typeContext = new HashMap<>();

View File

@ -58,7 +58,7 @@ public class ByteCodeTester {
if(generateAST){ if(generateAST){
try { try {
abstractSyntaxTree.typeCheck(); abstractSyntaxTree.typeCheckWithoutMain();
} catch (Exception e){ } catch (Exception e){
System.out.println("Le Exception in le type-check"); System.out.println("Le Exception in le type-check");
//fail(); //fail();
@ -86,6 +86,7 @@ public class ByteCodeTester {
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
fail();
} }
} }
@ -125,10 +126,10 @@ public class ByteCodeTester {
Program abstractSyntaxTree = (Program) generator.visit(tree); Program abstractSyntaxTree = (Program) generator.visit(tree);
try { try {
abstractSyntaxTree.typeCheck(); abstractSyntaxTree.typeCheckWithoutMain();
} catch (Exception e){ } catch (Exception e){
System.out.println("Le Exception in le type-check"); System.out.println("Le Exception in le type-check");
//fail(); fail();
} }
try { try {

View File

@ -4,6 +4,7 @@ import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Parameter;
import static ByteCode.CompareByteCodeSyntax.compareMethod; import static ByteCode.CompareByteCodeSyntax.compareMethod;
@ -54,12 +55,16 @@ public class CompareByteCodeBehaviour {
try { try {
this.sortMethods(class1, class2); this.sortMethods(class1, class2);
// Create instances // Create instances
Constructor<?> constructor1 = class1.getDeclaredConstructor(); Constructor<?>[] constructors1 = class1.getDeclaredConstructors();
Constructor<?> constructor2 = class2.getDeclaredConstructor(); Constructor<?>[] constructors2 = class2.getDeclaredConstructors();
Constructor<?> constructor1 = constructors1[0];
Constructor<?> constructor2 = constructors2[0];
constructor1.setAccessible(true); constructor1.setAccessible(true);
constructor2.setAccessible(true); constructor2.setAccessible(true);
Object obj1 = constructor1.newInstance(); Object[] constructorArgs1 = getDefaultArguments(constructor1.getParameters());
Object obj2 = constructor2.newInstance(); Object[] constructorArgs2 = getDefaultArguments(constructor2.getParameters());
Object obj1 = constructor1.newInstance(constructorArgs1);
Object obj2 = constructor2.newInstance(constructorArgs2);
// Get methods // Get methods
Method[] methods1 = new Method[this.methodArray1.size()]; Method[] methods1 = new Method[this.methodArray1.size()];
@ -96,8 +101,9 @@ public class CompareByteCodeBehaviour {
} }
} catch (InstantiationException | IllegalAccessException | } catch (InstantiationException | IllegalAccessException |
NoSuchMethodException | InvocationTargetException e) { InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
return false;
} }
return true; return true;
} }
@ -110,10 +116,35 @@ public class CompareByteCodeBehaviour {
inputs[i] = 1; // example value inputs[i] = 1; // example value
} else if (parameterTypes[i] == String.class) { } else if (parameterTypes[i] == String.class) {
inputs[i] = "test"; // example value inputs[i] = "test"; // example value
} else if (parameterTypes[i] == char.class) {
inputs[i] = 'a';
} else if (parameterTypes[i] == boolean.class) {
inputs[i] = false;
} }
// Add more cases as needed for different parameter types // Add more cases as needed for different parameter types
} }
return inputs; return inputs;
} }
private Object[] getDefaultArguments(Parameter[] parameters) {
Object[] defaultArgs = new Object[parameters.length];
for (int i = 0; i < parameters.length; i++) {
Class<?> paramType = parameters[i].getType();
if (paramType.isPrimitive()) {
if (paramType == boolean.class) {
defaultArgs[i] = false;
} else if (paramType == int.class) {
defaultArgs[i] = 1;
} else if (paramType == char.class) {
defaultArgs[i] = '\u0000';
} else if (paramType == String.class) {
defaultArgs[i] = "baguette";
}
} else {
defaultArgs[i] = null; // Use null for non-primitive types
}
}
return defaultArgs;
}
} }

View File

@ -42,6 +42,122 @@ public class TestAll {
byteCodeTester.testClassFileFromScratch(classPath, javacode, className); byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
} }
@Test
public void testCharArgument() {
String classPath = "src/test/resources/SimpleTests/CharArgument.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "CharArgument";
String javacode = "src/test/resources/SimpleTests/CharArgument.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testClassFields() {
String classPath = "src/test/resources/SimpleTests/ClassFields.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "ClassFields";
String javacode = "src/test/resources/SimpleTests/ClassFields.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testConstructorParams() {
String classPath = "src/test/resources/SimpleTests/ConstructorParams.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "ConstructorParams";
String javacode = "src/test/resources/SimpleTests/ConstructorParams.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testConstructorThisDot() {
String classPath = "src/test/resources/SimpleTests/ConstructorThisDot.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "ConstructorThisDot";
String javacode = "src/test/resources/SimpleTests/ConstructorThisDot.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testDivMethod() {
String classPath = "src/test/resources/SimpleTests/DivMethod.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "DivMethod";
String javacode = "src/test/resources/SimpleTests/DivMethod.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
// We dont do null assignments
/*
@Test
public void testExplicitNullAssign() {
String classPath = "src/test/resources/SimpleTests/ExplicitNullAssign.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "ExplicitNullAssign";
String javacode = "src/test/resources/SimpleTests/ExplicitNullAssign.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
*/
@Test
public void testFieldVar() {
String classPath = "src/test/resources/SimpleTests/FieldVar.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "FieldVar";
String javacode = "src/test/resources/SimpleTests/FieldVar.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testFieldWithExpr() {
String classPath = "src/test/resources/SimpleTests/FieldWithExpr.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "FieldWithExpr";
String javacode = "src/test/resources/SimpleTests/FieldWithExpr.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testGetterFunction() {
String classPath = "src/test/resources/SimpleTests/GetterFunction.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "GetterFunction";
String javacode = "src/test/resources/SimpleTests/GetterFunction.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testIfElseIfStatement() {
String classPath = "src/test/resources/SimpleTests/IfElseIfStatement.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "IfElseIfStatement";
String javacode = "src/test/resources/SimpleTests/IfElseIfStatement.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test
public void testIfElseIfStatementWithOneReturn() {
String classPath = "src/test/resources/SimpleTests/IfElseIfStatementWithOneReturn.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "IfElseIfStatementWithOneReturn";
String javacode = "src/test/resources/SimpleTests/IfElseIfStatementWithOneReturn.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test @Test
public void testTwoClasses() { public void testTwoClasses() {
String jarPath = "src/test/resources/basicClasses/TwoClasses.jar"; String jarPath = "src/test/resources/basicClasses/TwoClasses.jar";
@ -51,6 +167,7 @@ public class TestAll {
byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames); byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames);
} }
@Test @Test
public void testFourClasses() { public void testFourClasses() {
String jarPath = "src/test/resources/basicClasses/FourClasses.jar"; String jarPath = "src/test/resources/basicClasses/FourClasses.jar";
@ -60,6 +177,15 @@ public class TestAll {
byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames); byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames);
} }
@Test
public void testDijkstra() {
String jarPath = "src/test/resources/Integration/Dijkstra.jar";
//Program ast = ClassWithMainASTTyped.getProgram();
String[] classNames = {"FourClasses", "Test", "Test2", "Test3"};
String javacode = "src/test/resources/Integration/Dijkstra.java";
byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames);
}

View File

@ -67,5 +67,10 @@ public class TestAll {
testLexer.testTokens("SimpleTests/FieldVar.java", "SimpleTests/FieldVar.tokens"); testLexer.testTokens("SimpleTests/FieldVar.java", "SimpleTests/FieldVar.tokens");
} }
@Test
public void testIfElseIfStatementWithOneReturn() throws Exception {
testLexer.testTokens("SimpleTests/IfElseIfStatementWithOneReturn.java", "SimpleTests/IfElseIfStatementWithOneReturn.tokens");
}
} }

View File

@ -8,10 +8,22 @@ import static org.junit.Assert.assertEquals;
public class TypeChecker { public class TypeChecker {
// Method to test if the TypeCheck returns the expected Result // Method to test if the TypeCheck returns the expected Result
static public void assertTypeCheckResultNoMain(boolean testForMain, Program programmToBeTested, Program correctAST, boolean expectedResult) {
assertTypeCheckResult(false, programmToBeTested, correctAST, expectedResult);
}
static public void assertTypeCheckResult(Program programmToBeTested, Program correctAST, boolean expectedResult) { static public void assertTypeCheckResult(Program programmToBeTested, Program correctAST, boolean expectedResult) {
assertTypeCheckResult(true, programmToBeTested, correctAST, expectedResult);
}
static public void assertTypeCheckResult(boolean testForMain, Program programmToBeTested, Program correctAST, boolean expectedResult) {
boolean actualResult; boolean actualResult;
try { try {
TypeCheckResult typeCheckResult = programmToBeTested.typeCheck(); if(testForMain){
programmToBeTested.typeCheck();
} else {
programmToBeTested.typeCheckWithoutMain();
}
actualResult = true; actualResult = true;
} catch (Exception e) { } catch (Exception e) {
actualResult = false; actualResult = false;

View File

@ -1,5 +1,3 @@
package Integration;
class Dijkstra { class Dijkstra {
void main() { void main() {
@ -29,33 +27,33 @@ class Dijkstra {
g.addVertex(v11); g.addVertex(v11);
g.addVertex(v12); g.addVertex(v12);
g.addEdge(v1, v2); // A-B g.addEdge(v1, v2);
g.addEdge(v1, v5); // A-E g.addEdge(v1, v5);
g.addEdge(v1, v10); // A-J g.addEdge(v1, v10);
g.addEdge(v2, v3); // B g.addEdge(v2, v3);
g.addEdge(v2, v7); g.addEdge(v2, v7);
g.addEdge(v3, v4); // C g.addEdge(v3, v4);
g.addEdge(v3, v5); g.addEdge(v3, v5);
g.addEdge(v4, v8); // D g.addEdge(v4, v8);
g.addEdge(v5, v6); // E g.addEdge(v5, v6);
g.addEdge(v5, v9); g.addEdge(v5, v9);
g.addEdge(v6, v7); // F g.addEdge(v6, v7);
g.addEdge(v7, v8); // G g.addEdge(v7, v8);
g.addEdge(v8, v12); // H g.addEdge(v8, v12);
g.addEdge(v8, v10); g.addEdge(v8, v10);
g.addEdge(v9, v10); // I g.addEdge(v9, v10);
g.addEdge(v10, v11); // J g.addEdge(v10, v11);
g.addEdge(v11, v12); // K g.addEdge(v11, v12);
g.getShortestPath(v1, v8); g.getShortestPath(v1, v8);
g.getShortestPath(v1, v12); g.getShortestPath(v1, v12);
@ -69,7 +67,7 @@ class Dijkstra {
class Graph { class Graph {
private VertexSet vertexList; public VertexSet vertexList;
public Graph(VertexSet vertexList) { public Graph(VertexSet vertexList) {
this.vertexList = vertexList; this.vertexList = vertexList;
@ -98,23 +96,25 @@ class Graph {
calcList.get(source.id).setPrevious(null); calcList.get(source.id).setPrevious(null);
Vertex current = calcList.get(source.id); Vertex current = calcList.get(source.id);
// Fill the list with the distances
while (current != null) { while (current != null) {
current.setVisited(true); current.setVisited(true);
// Search for every adjacent vertex
VertexSet currentAdjanceyList = current.getAdjanceyList(); VertexSet currentAdjanceyList = current.getAdjanceyList();
if (currentAdjanceyList != null) { if (currentAdjanceyList != null) {
for (int i = 0; i < currentAdjanceyList.size(); i = i + 1) { int i = 0;
int i = 0;
while (i < currentAdjanceyList.size()) {
Vertex adjancey = currentAdjanceyList.getFromIndex(i); Vertex adjancey = currentAdjanceyList.getFromIndex(i);
adjancey = calcList.get(adjancey.id); adjancey = calcList.get(adjancey.id);
if ((adjancey != null) && !adjancey.isVisited()) { if (adjancey != null) {
int distance = current.getDistance() + 1; if (!adjancey.isVisited()) {
if (distance < adjancey.getDistance()) { int distance = current.getDistance() + 1;
adjancey.setDistance(distance); if (distance < adjancey.getDistance()) {
adjancey.setPrevious(current); adjancey.setDistance(distance);
adjancey.setPrevious(current);
}
} }
} }
i = i + 1;
} }
} else { } else {
} }
@ -132,8 +132,8 @@ class Graph {
previous = previous.getPrevious(); previous = previous.getPrevious();
} }
}
} }
class Vertex { class Vertex {
public int id; public int id;
@ -177,7 +177,7 @@ class Vertex {
public Vertex(Vertex vertex) { public Vertex(Vertex vertex) {
this.id = vertex.id; this.id = vertex.id;
this.adjanceyList = vertex.adjanceyList; this.adjanceyList = vertex.adjanceyList;
this.distance = 10000; // No infinity so... this.distance = 10000;
this.previous = null; this.previous = null;
this.visited = false; this.visited = false;
} }
@ -202,8 +202,8 @@ class Vertex {
class VertexSet { class VertexSet {
private Vertex vertex; public Vertex vertex;
private VertexSet next; public VertexSet next;
public VertexSet(Vertex vertex) { public VertexSet(Vertex vertex) {
this.vertex = vertex; this.vertex = vertex;
@ -318,7 +318,7 @@ class VertexSet {
} }
public Vertex get(int index) { public Vertex get(int index) {
if ((vertex == null)) { if (vertex == null) {
return null; return null;
} else if (vertex.id == index) { } else if (vertex.id == index) {
return this.vertex; return this.vertex;

Binary file not shown.

Binary file not shown.

View File

@ -1,9 +1,7 @@
package SimpleTests;
class ClassFields { class ClassFields {
private int privateAccess; public int privateAccess;
public int publicAccess; public int publicAccess;
protected int protectedAccess; public int protectedAccess;
} }

Binary file not shown.

View File

@ -1,5 +1,3 @@
package SimpleTests;
class ConstructorParams { class ConstructorParams {
public ConstructorParams(int i) { public ConstructorParams(int i) {

View File

@ -1,5 +1,3 @@
package SimpleTests;
class ConstructorThisDot{ class ConstructorThisDot{
public int i; public int i;

View File

@ -1,17 +0,0 @@
package SimpleTests;
class DecTest {
void foo() {
for (int i = 10; i > 0; i--) {
System.out.println(i);
}
}
void bar() {
for (int i = 10; i > 0; --i) {
System.out.println(i);
}
}
}

Binary file not shown.

View File

@ -1,5 +1,3 @@
package SimpleTests;
class DivMethod { class DivMethod {
int foo(int i){ int foo(int i){

View File

@ -1,3 +0,0 @@
package SimpleTests;
class EmptyClass{}

View File

@ -1,5 +1,3 @@
package SimpleTests;
class ExplicitNullAssign { class ExplicitNullAssign {
ExplicitNullAssign e; ExplicitNullAssign e;

View File

@ -1,18 +0,0 @@
package SimpleTests;
class ExtendedNotTest {
boolean notequal(int a, int b) {
return !(a == b);
}
boolean multiple(boolean a, boolean b) {
return !(!a || b);
}
boolean notWithAssigns(boolean a) {
boolean b = !a;
return !b;
}
}

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +1,3 @@
package SimpleTests;
class FieldWithExpr { class FieldWithExpr {
public int x = 5; public int x = 5;

View File

@ -1,11 +0,0 @@
package SimpleTests;
class ForTest {
void foo() {
for (int i = 0; i < 10; i = i + 1) {
System.out.println(i);
}
}
}

Binary file not shown.

View File

@ -1,11 +1,9 @@
package SimpleTests;
class GetterFunction { class GetterFunction {
private int i; public int i;
public GetterFunction(int i) { public GetterFunction(int number) {
this.i = i; this.i = number;
} }
public int getI() { public int getI() {

Binary file not shown.

View File

@ -1,8 +1,6 @@
package SimpleTests;
class IfElseIfStatement { class IfElseIfStatement {
int foo(int i) { public int foo(int i) {
if (i == 1) { if (i == 1) {
return 10; return 10;
} else if (i == 2) { } else if (i == 2) {

View File

@ -1,5 +1,3 @@
package SimpleTests;
class IfElseIfStatementWithOneReturn { class IfElseIfStatementWithOneReturn {
int foo(int i) { int foo(int i) {

View File

@ -0,0 +1,14 @@
Class: "class"
Identifier: "IfElseIfStatementWithOneReturn"
OpenCurlyBracket: "{"
Int: "int"
Identifier: "foo"
OpenRoundBracket: "("
Int: "int"
ClosedRoundBracket: ")"
OpenCurlyBracket: "{"
Int: "int"
Identifier: "result"
Assign: "="
IntValue: "0"
Semicolon: ";"

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,13 +1,14 @@
class FourClasses { class FourClasses {
public int main(int i) { public int method(int i) {
Test t = new Test(i); Test t = new Test(i);
Test2 t2 = new Test2(t.y); Test2 t2 = new Test2(t.y);
return t2.test.test3.getX(); return t2.test.test3.getX();
} }
public static void main(String[] args){ public static void main(String[] args){
FourClasses fourClasses = new FourClasses();
fourClasses.method(1);
} }
} }

Binary file not shown.

Binary file not shown.