Merge branch 'refs/heads/main' into codeGen

This commit is contained in:
404Simon 2024-05-14 14:56:45 +02:00
commit 34efc2847b
16 changed files with 429 additions and 58 deletions

21
pom.xml
View File

@ -55,6 +55,27 @@
<version>4.13.1</version>
</dependency>
<!-- ANTLR end -->
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<!-- JUnit 5 end -->
</dependencies>
<build>

View File

@ -7,7 +7,6 @@ import de.maishai.typedast.CodeGenUtils;
import de.maishai.typedast.typedclass.TypedClass;
import org.antlr.v4.runtime.*;
import java.io.FileOutputStream;
import java.io.IOException;
/**
@ -41,8 +40,18 @@ public class Compiler {
CommonTokenStream tokens = new CommonTokenStream(lexer);
DecafParser parser = new DecafParser(tokens);
DecafParser.ClassContext tree = parser.class_(); //Parsen
Class ast = ASTGenerator.generateAST(tree);
return ast;
return ASTGenerator.generateAST(tree);
}
public static Class generateASTFromFile(String sourcePath) {
ANTLRInputStream antlrInputStream;
try {
antlrInputStream = new ANTLRFileStream(sourcePath);
} catch (IOException e) {
System.out.println("Ungültiger Dateipfad D:");
throw new RuntimeException("Ungültiger Dateipfad D:");
}
return generateAST(antlrInputStream.toString());
}
public static TypedClass generateTypedASTFromAst(Class ast) {

View File

@ -1,5 +1,6 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.IfElse;
import de.maishai.ast.records.Method;
import de.maishai.typedast.*;
import lombok.Data;
@ -59,13 +60,39 @@ public class TypedMethod implements TypedNode {
return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType();
}
private boolean hasEvenReturnsInIfElseBlocks() {
List<TypedIfElse> typedIfElses = new ArrayList<>();
for (var stmt : typedBlock.getStmts()) {
if (stmt instanceof TypedIfElse ifElse) {
for (var stmt2 : ifElse.getIfTypedBlock().getStmts()) {
if (stmt2 instanceof TypedReturn) {
typedIfElses.add(ifElse);
}
}
for (var stmt2 : ifElse.getElseTypedBlock().getStmts()) {
if (stmt2 instanceof TypedReturn) {
typedIfElses.add(ifElse);
}
}
}
}
if (typedIfElses.size() % 2 == 0) {
return true;
} else {
return false;
}
}
@Override
public Type typeCheck(TypedClass clas) {
if(returnType != Type.VOID){
if(typedBlock.typeCheck(clas).getKind() != returnType.getKind()){
throw new RuntimeException("Method " + name + " must return " + returnType);
}
if (returnType != Type.VOID && !hasEvenReturnsInIfElseBlocks()) {
if (typedBlock.typeCheck(clas).getKind() != returnType.getKind()) {
if (hasEvenReturnsInIfElseBlocks()) {
throw new RuntimeException("Method " + name + " must have even returns in if else blocks");
} else {
throw new RuntimeException("Method " + name + " must return " + returnType.getKind());
}
}
}
return returnType;
}

View File

@ -111,7 +111,7 @@ public class AbstractSyntax_ClassWithConstructor {
"j"),
Operator.LT,
new FieldVarAccess(
false,
true,
null,
"x")
),

View File

@ -0,0 +1,128 @@
//public class ClassWithConstructorWithCodeInComments {
// int x;
// public ClassWithConstructorWithCodeInComments() {
// this.x = 10;
// int i;
// for (i = 0; i < 6; i = i + 1) {
// this.x = this.x * this.x;
//// int j;
//// for (j = 0; j < this.x; j += 1) {
//// this.x = this.x * this.x;
//// }
// }
// }
//}
import de.maishai.ast.Operator;
import de.maishai.ast.records.Class;
import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import java.util.List;
public class AbstractSyntax_ClassWithConstructorWithCodeInComments {
public static Class get() {
List<Declaration> declarations = List.of(
new Declaration(
"x",
Type.INT
)
);
List<Method> methods = List.of();
List<Constructor> constructors = getConstructors();
return new Class(
"ClassWithConstructorWithCodeInComments",
declarations,
methods,
constructors
);
}
private static List<Constructor> getConstructors() {
return List.of(getConstructor1());
}
private static Constructor getConstructor1() {
List<Parameter> parameters = List.of();
List<Declaration> localVariables = List.of(
new Declaration(
"i",
Type.INT
)
);
List<Statement> statementList = List.of(
new Assignment(
new FieldVarAccess(
true,
null,
"x"),
new IntLiteral(10)
),
new For(
new Assignment(
new FieldVarAccess(
false,
null,
"i"),
new IntLiteral(0)
),
new Binary(
new FieldVarAccess(
false,
null,
"i"),
Operator.LT,
new IntLiteral(6)
),
new Assignment(
new FieldVarAccess(
false,
null,
"i"),
new Binary(
new FieldVarAccess(
false,
null,
"i"),
Operator.ADD,
new IntLiteral(1)
)
),
new Block(
List.of(),
List.of(
new Assignment(
new FieldVarAccess(
true,
null,
"x"),
new Binary(
new FieldVarAccess(
true,
null,
"x"),
Operator.MUL,
new FieldVarAccess(
true,
null,
"x")
)
)
)
)
)
);
Block block = new Block(
localVariables,
statementList
);
return new Constructor(
"ClassWithConstructorWithCodeInComments",
parameters,
block
);
}
}

View File

@ -1,6 +1,6 @@
//public class ClassWithConstructorWithParameters {
// int x;
// public classWithConstructorWithParameters(int startValue, int repetitions) {
// public ClassWithConstructorWithParameters(int startValue, int repetitions) {
// this.x = startValue;
// while (repetitions > 0) {
// int innerRepetitions;
@ -158,7 +158,7 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
)
);
return new Constructor(
"classWithConstructorWithParameters",
"ClassWithConstructorWithParameters",
parameters,
block
);

View File

@ -2,10 +2,8 @@
// int x;
//}
import de.maishai.ast.records.*;
import de.maishai.ast.records.Class;
import de.maishai.ast.records.Constructor;
import de.maishai.ast.records.Declaration;
import de.maishai.ast.records.Method;
import de.maishai.typedast.Type;
import java.util.List;
@ -19,9 +17,19 @@ public class AbstractSyntax_ClassWithField {
)
);
List<Method> methods = List.of();
List<Constructor> constructors = List.of();
List<Constructor> constructors =
List.of(
new Constructor(
"ClassWithField",
List.of(),
new Block(
List.of(),
List.of()
)
)
);
return new Class(
"ClassWithAssignment",
"ClassWithField",
declarations,
methods,
constructors

View File

@ -17,7 +17,16 @@ public class AbstractSyntax_ClassWithMethod {
"ClassWithMethod",
List.of(),
methods,
List.of()
List.of(
new Constructor(
"ClassWithMethod",
List.of(),
new Block(
List.of(),
List.of()
)
)
)
);
}

View File

@ -1,18 +0,0 @@
//class OnlyClass {
//}
import de.maishai.ast.records.*;
import de.maishai.ast.records.Class;
import java.util.List;
public class AbstractSyntax_OnlyClass {
public static Class get() {
return new Class(
"OnlyClass",
List.of(),
List.of(),
List.of()
);
}
}

View File

@ -1,7 +1,9 @@
//public class PublicClass {
//}
import de.maishai.ast.records.Block;
import de.maishai.ast.records.Class;
import de.maishai.ast.records.Constructor;
import java.util.List;
@ -11,7 +13,17 @@ public class AbstractSyntax_PublicClass {
"PublicClass",
List.of(),
List.of(),
List.of()
List.of(
new Constructor(
"PublicClass",
List.of(),
new Block(
List.of(),
List.of()
)
)
)
);
}
}

View File

@ -0,0 +1,14 @@
public class ClassWithConstructorWithCodeInComments {
int x;
public ClassWithConstructorWithCodeInComments() {
this.x = 10;
int i;
for (i = 0; i < 6; i = i + 1) {
this.x = this.x * this.x;
// int j;
// for (j = 0; j < this.x; j += 1) {
// this.x = this.x * this.x;
// }
}
}
}

View File

@ -1,2 +0,0 @@
class OnlyClass {
}

View File

@ -0,0 +1,128 @@
//public class ClassWithConstructorWithCodeInComments {
// int x;
// public ClassWithConstructorWithCodeInComments() {
// this.x = 10;
// int i;
// for (i = 0; i < 6; i = i + 1) {
// this.x = this.x * this.x;
//// int j;
//// for (j = 0; j < this.x; j += 1) {
//// this.x = this.x * this.x;
//// }
// }
// }
//}
import de.maishai.ast.Operator;
import de.maishai.ast.records.Class;
import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import java.util.List;
public class TypedAbstractSyntax_ClassWithConstructorWithCodeInComments {
// public static Class get() {
// List<Declaration> declarations = List.of(
// new Declaration(
// "x",
// Type.INT
// )
// );
// List<Method> methods = List.of();
// List<Constructor> constructors = getConstructors();
// return new Class(
// "ClassWithConstructorWithCodeInComments",
// declarations,
// methods,
// constructors
// );
// }
//
// private static List<Constructor> getConstructors() {
// return List.of(getConstructor1());
// }
//
// private static Constructor getConstructor1() {
// List<Parameter> parameters = List.of();
//
// List<Declaration> localVariables = List.of(
// new Declaration(
// "i",
// Type.INT
// )
// );
// List<Statement> statementList = List.of(
// new Assignment(
// new FieldVarAccess(
// true,
// null,
// "x"),
// new IntLiteral(10)
// ),
// new For(
// new Assignment(
// new FieldVarAccess(
// false,
// null,
// "i"),
// new IntLiteral(0)
// ),
// new Binary(
// new FieldVarAccess(
// false,
// null,
// "i"),
// Operator.LT,
// new IntLiteral(6)
// ),
// new Assignment(
// new FieldVarAccess(
// false,
// null,
// "i"),
// new Binary(
// new FieldVarAccess(
// false,
// null,
// "i"),
// Operator.ADD,
// new IntLiteral(1)
// )
// ),
// new Block(
// List.of(),
// List.of(
// new Assignment(
// new FieldVarAccess(
// true,
// null,
// "x"),
// new Binary(
// new FieldVarAccess(
// true,
// null,
// "x"),
// Operator.MUL,
// new FieldVarAccess(
// true,
// null,
// "x")
// )
// )
// )
// )
// )
//
// );
// Block block = new Block(
// localVariables,
// statementList
// );
//
// return new Constructor(
// "ClassWithConstructorWithCodeInComments",
// parameters,
// block
// );
// }
}

View File

@ -1,19 +0,0 @@
//class OnlyClass {
//}
import de.maishai.typedast.typedclass.*;
import java.util.List;
public class TypedAbstractSyntax_OnlyClass {
// public static TypedClass get() {
// TypedClass typedClass = new TypedClass();
// typedClass.setIsPublic(false);
// typedClass.setTypedId(new TypedId("OnlyClass"));
// typedClass.setTypedFields(List.of());
// typedClass.setTypedMethods(List.of());
// typedClass.setTypedMainMethod(null);
// typedClass.setTypedConstructors(List.of());
// return typedClass;
// }
}

View File

@ -1,5 +1,3 @@
public class E2ETests {
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,56 @@
import de.maishai.Compiler;
import de.maishai.ast.records.Class;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ScannerParserTests {
@Test
public void testPublicClass() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/PublicClass.java");
assertEquals(AbstractSyntax_PublicClass.get(), resultAst);
}
@Test
public void testClassWithField() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithField.java");
assertEquals(AbstractSyntax_ClassWithField.get(), resultAst);
}
@Test
public void testClassWithConstructor() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructor.java");
assertEquals(AbstractSyntax_ClassWithConstructor.get(), resultAst);
}
@Test
public void testClassWithMethod() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithMethod.java");
assertEquals(AbstractSyntax_ClassWithMethod.get(), resultAst);
}
@Test
public void testClassWithConstructorWithCodeInComments() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructorWithCodeInComments.java");
assertEquals(AbstractSyntax_ClassWithConstructorWithCodeInComments.get(), resultAst);
}
@Test
public void testClassWithConstructorWithParameters() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructorWithParameters.java");
assertEquals(AbstractSyntax_ClassWithConstructorWithParameters.get(), resultAst);
}
@Test
public void testClassWithMethodAndField() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithMethodAndField.java");
assertEquals(AbstractSyntax_ClassWithMethodAndField.get(), resultAst);
}
// @Test
// public void testClassWithConstructorAndMethodCall() {
// Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructorAndMethodCall.java");
// assertEquals(AbstractSyntax_ClassWithConstructorAndMethodCall.get(), resultAst);
// }
}