Fix and Final Test File

This commit is contained in:
Maximilian Stahl 2024-07-05 13:23:09 +02:00
parent 5a12d61623
commit 30d3cfa44a
16 changed files with 110 additions and 169 deletions

View File

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

View File

@ -437,6 +437,8 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
fieldOrObjectVarCrementAssign(assignNode); // assign
decrementNode.accept(this); // crement
}
} else {
assignFieldOrObjectVar(assignNode);
}
} else {
assignFieldOrObjectVar(assignNode);
@ -723,10 +725,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override
public void visit(MethodCallNode methodCallNode) {
List<ParameterNode> parameterNodes = new ArrayList<>();
for (IExpressionNode expressionNode : methodCallNode.parameters) {
expressionNode.accept(this);
parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
}
int localVarIndex = -1;
if (methodCallNode.target.memberAccess.identifiers.size() > 1) {
localVarIndex = localVariables.indexOf(methodCallNode.target.memberAccess.identifiers.get(1));
@ -736,8 +734,9 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} else { // this field
methodVisitor.visitVarInsn(ALOAD, 0);
}
if (methodCallNode.type == null) {
methodCallNode.type = new BaseType(TypeEnum.INT);
for (IExpressionNode expressionNode : methodCallNode.parameters) {
expressionNode.accept(this);
parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
}
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, methodCallNode.target.memberAccess.identifiers.get(0), methodCallNode.identifier, mapper.generateMethodDescriptor(methodCallNode.type, parameterNodes), false);
}

View File

@ -41,8 +41,8 @@ public class Main {
}
else {
try {
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/endabgabeTests/working/Loops.java"));
compileFile(codeCharStream, "src/test/resources/input/endabgabeTests");
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/finalTest/Car.java"));
compileFile(codeCharStream, "src/test/resources/input/finalTest");
} catch (IOException e) {
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);
} else if (currentScope.getLocalVar(toCheck.identifier) != null) {
var type = currentScope.getLocalVar(toCheck.identifier);
toCheck.setTypeNode(type);
return new TypeCheckResult(true, type);
}
}
return new TypeCheckResult(true, null);
}
@Override
@ -430,8 +429,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override
public TypeCheckResult analyze(NewDeclarationNode toCheck) {
if (context.containsClass(toCheck.identifier)) {
for(var t : toCheck.expressions) {
t.accept(this);
}
return new TypeCheckResult(true, new ReferenceType(toCheck.identifier));
} else {
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));
}
case CHAR_VALUE -> {
valueNode.value = valueNode.value.replace("'", "");
return new TypeCheckResult(true, new BaseType(TypeEnum.CHAR));
}
case BOOLEAN_VALUE -> {

View File

@ -403,7 +403,7 @@ class AstBuilderTest {
MemberNode testClassObject = new FieldNode(new AccessModifierNode("public"), new ReferenceType("SelfReference"),"selfReference");
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);
BlockNode testMethod2Block = new BlockNode();

View File

@ -352,34 +352,6 @@ public class SemanticTest {
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
@DisplayName("Self Reference Test")
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));
}
}