Merge branch 'Endabgabe' of https://gitea.hb.dhbw-stuttgart.de/i22005/JavaCompiler into Endabgabe
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
Lucas 2024-07-05 13:25:01 +02:00
commit e0286e0840
16 changed files with 111 additions and 165 deletions

View File

@ -4,5 +4,6 @@ public enum TypeEnum {
VOID, VOID,
INT, INT,
CHAR, CHAR,
BOOL BOOL,
NULL
} }

View File

@ -267,7 +267,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override @Override
public void visit(MemberAccessNode memberAccessNode) { public void visit(MemberAccessNode memberAccessNode) {
// Only used to get, not to put // Only used to get, not to put
int localVarIndex = localVariables.indexOf("memberAccessNode.identifier"); // TODO: Anführungszeichen entfernen int localVarIndex = localVariables.indexOf("memberAccessNode.identifier"); // TODO
if (localVarIndex >= 0) { // local var object if (localVarIndex >= 0) { // local var object
methodVisitor.visitVarInsn(ALOAD, localVarIndex); methodVisitor.visitVarInsn(ALOAD, localVarIndex);
} else { // this field } else { // this field
@ -736,6 +736,10 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} else { // this field } else { // this field
methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitVarInsn(ALOAD, 0);
} }
for (IExpressionNode expressionNode : methodCallNode.parameters) {
expressionNode.accept(this);
parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
}
if(methodCallNode.type == null) { if(methodCallNode.type == null) {
methodCallNode.type = new BaseType(TypeEnum.INT); methodCallNode.type = new BaseType(TypeEnum.INT);
} }

View File

@ -41,8 +41,8 @@ public class Main {
} }
else { else {
try { try {
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/endabgabeTests/working/Loops.java")); CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/finalTest/Car.java"));
compileFile(codeCharStream, "src/test/resources/input/endabgabeTests"); compileFile(codeCharStream, "src/test/resources/input/finalTest");
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage()); System.err.println("Error reading the file: " + e.getMessage());
} }

Binary file not shown.

View File

@ -254,12 +254,11 @@ public class SemanticAnalyzer implements SemanticVisitor {
return new TypeCheckResult(true, type); return new TypeCheckResult(true, type);
} else if (currentScope.getLocalVar(toCheck.identifier) != null) { } else if (currentScope.getLocalVar(toCheck.identifier) != null) {
var type = currentScope.getLocalVar(toCheck.identifier); var type = currentScope.getLocalVar(toCheck.identifier);
toCheck.setTypeNode(type);
return new TypeCheckResult(true, type); return new TypeCheckResult(true, type);
} }
} }
return new TypeCheckResult(true, null); return new TypeCheckResult(true, null);
} }
@Override @Override
@ -430,8 +429,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(NewDeclarationNode toCheck) { public TypeCheckResult analyze(NewDeclarationNode toCheck) {
if (context.containsClass(toCheck.identifier)) { if (context.containsClass(toCheck.identifier)) {
for(var t : toCheck.expressions) {
t.accept(this);
}
return new TypeCheckResult(true, new ReferenceType(toCheck.identifier)); return new TypeCheckResult(true, new ReferenceType(toCheck.identifier));
} else { } else {
throw new RuntimeException("Cannot find class " + toCheck.identifier); throw new RuntimeException("Cannot find class " + toCheck.identifier);
@ -683,6 +684,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
return new TypeCheckResult(true, new BaseType(TypeEnum.INT)); return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
} }
case CHAR_VALUE -> { case CHAR_VALUE -> {
valueNode.value = valueNode.value.replace("'", "");
return new TypeCheckResult(true, new BaseType(TypeEnum.CHAR)); return new TypeCheckResult(true, new BaseType(TypeEnum.CHAR));
} }
case BOOLEAN_VALUE -> { case BOOLEAN_VALUE -> {

View File

@ -403,7 +403,7 @@ class AstBuilderTest {
MemberNode testClassObject = new FieldNode(new AccessModifierNode("public"), new ReferenceType("SelfReference"),"selfReference"); MemberNode testClassObject = new FieldNode(new AccessModifierNode("public"), new ReferenceType("SelfReference"),"selfReference");
BlockNode testMethod1Block = new BlockNode(); BlockNode testMethod1Block = new BlockNode();
testMethod1Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(new TargetNode(true), "testMethod2")))); testMethod1Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(null, "testMethod2"))));
MethodNode testMethod1 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod1", testMethod1Block); MethodNode testMethod1 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod1", testMethod1Block);
BlockNode testMethod2Block = new BlockNode(); BlockNode testMethod2Block = new BlockNode();

View File

@ -352,34 +352,6 @@ public class SemanticTest {
assertNotNull(typedAst); assertNotNull(typedAst);
} }
@Test
@DisplayName("Null Test")
public void nullTest() {
BlockNode blockCon = new BlockNode();
MemberAccessNode memberAccess = new MemberAccessNode(true);
memberAccess.addIdentifier("a");
AssignableNode assignable = new AssignableNode(memberAccess);
blockCon.addStatement(new AssignNode(assignable, new UnaryNode(new ValueNode(EnumValueNode.NULL_VALUE, "null"))));
blockCon.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "Null", blockCon);
ClassNode class1 = new ClassNode("public", "Null");
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
class1.addMember(constructor);
ProgramNode abstractSyntaxTree = new ProgramNode();
abstractSyntaxTree.addClass(class1);
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
for (Exception runtimeException : SemanticAnalyzer.errors) {
runtimeException.printStackTrace();
}
assertTrue(SemanticAnalyzer.errors.isEmpty());
assertNotNull(typedAst);
}
@Test @Test
@DisplayName("Self Reference Test") @DisplayName("Self Reference Test")
public void selfReferenceTest() { public void selfReferenceTest() {

View File

@ -1,25 +0,0 @@
public class Main {
public static void main(String[] args) {
Car myCar = new Car(2020);
int tires = 0;
int year = myCar.getYear();
if (year == 2020) {
tires = 4;
} else {
tires = 2;
}
}
}
class Car {
private int year;
public Car(int year) {
this.year = year;
}
public int getYear() {
return this.year;
}
}

View File

@ -1,36 +0,0 @@
public class ControlStructures {
public int sum(int a, int b) {
return a + b;
}
public char checkNumber(int num) {
if (num > 0) {
return 'p';
} else if (num < 0) {
return 'n';
} else {
return 'z';
}
}
public void printNumbersUpTo(int limit) {
int even = 0;
int uneven = 0;
int i = 0;
while (i < limit) {
if ((i % 2) == 0) {
even++;
} else {
uneven = uneven + 1;
}
i++;
}
}
public static void main(String[] args) {
ControlStructures cs = new ControlStructures();
cs.printNumbersUpTo(5);
int result = cs.sum(5, 5);
}
}

View File

@ -1,6 +0,0 @@
public class Main {
public static void main(String[] args) {
Loops loop = new Loops();
System.out.println(loop.For(6));
}
}

View File

@ -1,11 +0,0 @@
public class Person {
private int age;
public Person(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
}

View File

@ -1,25 +0,0 @@
public class Calculation {
public int add(int a, int b) {
return a + b;
}
public int sub(int a, int b) {
return a - b;
}
public int mul(int a, int b) {
return a * b;
}
public int div(int a, int b) {
return a / b;
}
public int mod(int a, int b) {
return a % b;
}
public int complexCalculation() {
return 3 - 2 * 2 + 5;
}
}

View File

@ -1 +0,0 @@
public class EmptyClass {}

View File

@ -1,24 +0,0 @@
public class Loops {
public boolean If(int a, int b) {
if(a == b) {
return true;
} else {
return false;
}
}
public int While(int a) {
int count = 0;
while(count < a) {
count++;
}
return count;
}
public int For(int a) {
for(int i = 0; i < a; i++) {
}
return i + 2;
}
}

View File

@ -0,0 +1,63 @@
public class Car {
public int ps;
public char brand;
public int tires;
public int drivenKilometers;
public Car(int horsePower) {
this.ps = horsePower;
this.tires = 4;
}
public int getTires() {
return this.tires;
}
public boolean isSuperCar() {
if(this.ps > 300) {
return true;
} else {
return false;
}
}
public void tune(int horsePower) {
this.ps = this.ps + horsePower;
}
public void tune() {
this.ps = this.ps + 50;
}
public void driveCircels(int circels) {
for(int i = 0; i < circels; i++) {
this.drivenKilometers = this.drivenKilometers + 5;
}
}
public void isTunable(boolean hasEngine, boolean hasTires) {
if(hasEngine && hasTires) {
this.tune(5);
}
}
public char race() {
int enemyHorsePower = 200;
char win = 'W';
char lose = 'L';
Car enemy = new Car(ps);
if(this.ps > enemyHorsePower) {
return win;
} else {
return lose;
}
}
public int refuel(int currentTank, int maxTank){
int tank = currentTank;
do{
tank++;
}while(tank<maxTank);
return tank;
}
}

View File

@ -0,0 +1,32 @@
public class Main {
public static void main(String[] args) {
Car vw = new Car(50);
System.out.println(vw.getTires());
System.out.println(vw.isSuperCar());
System.out.println("--------------------------------------");
vw.tune(300);
System.out.println(vw.ps);
System.out.println(vw.isSuperCar());
System.out.println("--------------------------------------");
System.out.println(vw.drivenKilometers);
vw.driveCircels(25);
System.out.println(vw.drivenKilometers);
System.out.println("--------------------------------------");
vw.isTunable(true, true);
System.out.println(vw.ps);
System.out.println("--------------------------------------");
System.out.println(vw.race());
System.out.println("--------------------------------------");
System.out.println(vw.refuel(10, 20));
}
}