Compare commits

...

3 Commits

Author SHA1 Message Date
Krauß, Josefine
94b3830561 instvar special case 2024-07-02 15:36:27 +02:00
Krauß, Josefine
3dcaad62f9 Merge branch 'master' of https://gitea.hb.dhbw-stuttgart.de/i22022/NichtHaskell 2024-07-02 15:22:33 +02:00
Krauß, Josefine
62060462a9 instvar debugging 2024-07-02 15:21:46 +02:00
5 changed files with 12 additions and 4 deletions

View File

@ -100,6 +100,6 @@ public class Compiler {
System.out.println("No TypeCheck errors found."); System.out.println("No TypeCheck errors found.");
abstractSyntaxTree.codeGen(); abstractSyntaxTree.codeGen();
System.out.println("Your input was compiler. You can find the output at ???");// todo wo output? überhaupt hinschreiben? System.out.println("Your input was compiled. You can find the output at ???");// todo wo output? überhaupt hinschreiben?
} }
} }

View File

@ -5,6 +5,7 @@ class TestClass {
public TestClass(){i = 3456;} public TestClass(){i = 3456;}
int meth(int n){ int meth(int n){
Example e = new Example(); Example e = new Example();
e.example1 = new TestClass();
int abc = e.example1.m1(2).m2().m3(); int abc = e.example1.m1(2).m2().m3();
return 0; return 0;

View File

@ -8,17 +8,18 @@ public class TypeCheckHelper {
boolean type1Primitiv = Objects.equals(type1, "boolean") || Objects.equals(type1, "int") || Objects.equals(type1, "char"); boolean type1Primitiv = Objects.equals(type1, "boolean") || Objects.equals(type1, "int") || Objects.equals(type1, "char");
boolean type2Primitiv = Objects.equals(type2, "boolean") || Objects.equals(type2, "int") || Objects.equals(type2, "char"); boolean type2Primitiv = Objects.equals(type2, "boolean") || Objects.equals(type2, "int") || Objects.equals(type2, "char");
String result; String result = "class";
if(type1Primitiv && type2Primitiv){ if(type1Primitiv && type2Primitiv){
if(Objects.equals(type1, type2)){ if(Objects.equals(type1, type2)){
result = type1; result = type1;
}else{ }else{
throw new TypeCheckException("There is no upper bound between " + type1 + " and " + type2 + "."); throw new TypeCheckException("There is no upper bound between " + type1 + " and " + type2 + ".");
} }
}else if(type1Primitiv || type2Primitiv){ }else if(type1Primitiv ^ type2Primitiv){
throw new TypeCheckException("There is no upper bound between " + type1 + " and " + type2 + "."); throw new TypeCheckException("There is no upper bound between " + type1 + " and " + type2 + ".");
}else{ }else{
result = "class"; if(Objects.equals(type1, type2))
result = type1;
} }
return result; return result;
} }

View File

@ -31,6 +31,10 @@ public class InstVarExpression extends AbstractType implements IExpression{
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException { public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
if(this.receivers.get(0).identifier != null){
thisClass = localVars.get(this.receivers.get(0).identifier);
}
String varType = typeContext.get(thisClass).get(fieldName); String varType = typeContext.get(thisClass).get(fieldName);
if (varType == null) { if (varType == null) {
throw new TypeCheckException("Field " + fieldName + " was not found in class " + thisClass + "."); throw new TypeCheckException("Field " + fieldName + " was not found in class " + thisClass + ".");

View File

@ -45,6 +45,8 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
leftType.type = typeContext.get(thisClass).get(identifier); leftType.type = typeContext.get(thisClass).get(identifier);
} }
}else{ }else{
if(left instanceof InstVarExpression instVarExpression)
instVarExpression.thisClass = this.thisClass;
leftType = left.typeCheck(methodContext, typeContext, localVars); leftType = left.typeCheck(methodContext, typeContext, localVars);
} }
TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars); TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars);