example files for presentation

This commit is contained in:
Krauß, Josefine 2024-07-04 11:04:33 +02:00
parent 7e8c297d9a
commit 7fb7cea2f7
6 changed files with 93 additions and 13 deletions

View File

@ -0,0 +1,43 @@
public class FieldAccessAndMethodCalls {
public static void main(String[] args) {
Class1 c1 = new Class1();
int i = c1.c2.c3.m3(1).m2().m1();
print(i);
}
}
public class Class1{
int i1;
Class2 c2;
public Class1{
this.c2 = new Class2();
}
public int m1(){
return i1;
}
}
public class Class2{
int i2;
Class3 c3;
public Class3{
this.c3 = new Class3();
}
public Class1 m2(){
Class1 c1 = new Class2();
c1.i1 = i2;
return c1;
}
}
public class Class3{
int i3;
public Class3(int i){
this.i1 = i;
}
public Class2 m3(int i){
Class2 c2 = new Class2();
c2.i2 = i;
return c2;
}
}

View File

@ -22,10 +22,11 @@ public class Compiler {
if (args.length < 1) { if (args.length < 1) {
System.out.println("Usage: java -jar Compiler.jar <file_path> [--suppress-details]"); System.out.println("Usage: java -jar Compiler.jar <file_path> [--suppress-details]");
return; // return;
} }
String filePath = args[0]; //String filePath = args[0];
String filePath = "C:/Users/dh10krj/Downloads/TestClass.java";
boolean suppressDetails = false; boolean suppressDetails = false;
@ -94,16 +95,19 @@ public class Compiler {
}catch (Exception e){ }catch (Exception e){
System.out.println("A unexpected error occurred in TypeCheck."); System.out.println("A unexpected error occurred in TypeCheck.");
System.out.println(e); System.out.println(e);
return;
} }
if(!suppressDetails) if(!suppressDetails)
System.out.println("No TypeCheck errors found."); System.out.println("No TypeCheck errors found.");
abstractSyntaxTree.codeGen();//todo remove
try { try {
abstractSyntaxTree.codeGen(); abstractSyntaxTree.codeGen();
}catch (Exception e){ }catch (Exception e){
System.out.println("A error occurred during code generation. Your input was not compiled."); System.out.println("A error occurred during code generation. Your input was not compiled.");
System.out.println(e); System.out.println(e);
return;
} }
System.out.println("Your input was compiled. You can find the output in your current working directory."); System.out.println("Your input was compiled. You can find the output in your current working directory.");
} }

View File

@ -1,13 +1,40 @@
class Example1b { class FieldAccessAndMethodCalls {
public int fak(int number){ public static void main(String[] args) {
if(number < 0){ Class1 c1 = new Class1();
return 1; int i = c1.c2.c3.m3(1).m2().m1();
} System.out.println(i);
int factorial = 1; }
int i = 0; }
while(i < number){
factorial = factorial * i; class Class1{
} int i1;
return factorial; Class2 c2;
public Class1() {
this.c2 = new Class2();
}
public int m1(){
return i1;
}
}
class Class2{
int i2;
Class3 c3;
public Class2(){
this.c3 = new Class3();
}
public Class1 m2(){
Class1 c1 = new Class1();
c1.i1 = i2;
return c1;
}
}
class Class3{
int i3;
public Class2 m3(int i){
Class2 c2 = new Class2();
c2.i2 = i;
return c2;
} }
} }

View File

@ -5,6 +5,7 @@ import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType; import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression; import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Expression.InstVarExpression; import abstractSyntaxTree.Expression.InstVarExpression;
import abstractSyntaxTree.Expression.LocalVarIdentifier;
import abstractSyntaxTree.Parameter.ParameterList; import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.StatementExpression.MethodCallStatementExpression; import abstractSyntaxTree.StatementExpression.MethodCallStatementExpression;
import org.objectweb.asm.*; import org.objectweb.asm.*;
@ -33,6 +34,8 @@ public class ReturnStatement extends AbstractType implements IStatement{
methodCallStatementExpression.thisClass = this.thisClass; methodCallStatementExpression.thisClass = this.thisClass;
else if(expression instanceof InstVarExpression instVarExpression) else if(expression instanceof InstVarExpression instVarExpression)
instVarExpression.thisClass = this.thisClass; instVarExpression.thisClass = this.thisClass;
else if(expression instanceof LocalVarIdentifier localVarIdentifier)
localVarIdentifier.thisClass = this.thisClass;
TypeCheckResult typedExpression = expression.typeCheck(methodContext, typeContext, localVars); TypeCheckResult typedExpression = expression.typeCheck(methodContext, typeContext, localVars);
result.type = typedExpression.type; result.type = typedExpression.type;
} }

View File

@ -36,6 +36,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
TypeCheckResult leftType; TypeCheckResult leftType;
if (left instanceof LocalVarIdentifier localVarIdentifier) { if (left instanceof LocalVarIdentifier localVarIdentifier) {
localVarIdentifier.thisClass = thisClass;
leftType = new TypeCheckResult(); leftType = new TypeCheckResult();
String identifier = localVarIdentifier.getIdentifier(); String identifier = localVarIdentifier.getIdentifier();
leftType.type = localVars.get(identifier); leftType.type = localVars.get(identifier);
@ -51,6 +52,8 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
} }
if(right instanceof MethodCallStatementExpression methodCallStatementExpression) if(right instanceof MethodCallStatementExpression methodCallStatementExpression)
methodCallStatementExpression.thisClass = this.thisClass; methodCallStatementExpression.thisClass = this.thisClass;
if(right instanceof LocalVarIdentifier localVarIdentifierRight)
localVarIdentifierRight.thisClass = this.thisClass;
TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars); TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars);