Compare commits

...

2 Commits

Author SHA1 Message Date
Krauß, Josefine
194ff3fcf7 Merge remote-tracking branch 'origin/master' 2024-06-30 15:25:21 +02:00
Krauß, Josefine
f5dcd4d79f typecheck for PrintStatement 2024-06-30 15:24:47 +02:00
3 changed files with 16 additions and 2 deletions

View File

@ -60,6 +60,9 @@ public class BlockStatement extends AbstractType implements IStatement {
if(statement instanceof LocalVarDecl localVarDecl){
localVarDecl.thisClass = thisClass;
}
if (statement instanceof PrintStatement printStatement) {
printStatement.thisClass = thisClass;
}
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
if(statement instanceof LocalVarDecl localVarDecl){

View File

@ -8,9 +8,11 @@ import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Objects;
public class PrintStatement extends AbstractType implements IStatement {
String variableName;
public String thisClass;
public PrintStatement(String variableName) {
this.variableName = variableName;
@ -18,7 +20,16 @@ public class PrintStatement extends AbstractType implements IStatement {
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
return null;
TypeCheckResult result = new TypeCheckResult();
String typeOfVar = localVars.get(variableName);
if (typeOfVar == null) {
typeOfVar = typeContext.get(thisClass).get(variableName);
}
if (!Objects.equals(typeOfVar, "int"))
throw new TypeCheckException("The variable to be printed is " + typeOfVar + " but should be int.");
result.type = typeOfVar;
setTypeCheckResult(result);
return result;
}
@Override

View File

@ -63,7 +63,7 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
currentType = thisClass;
}
//if classToSearchMethodIn does not conatin method, throw exception. go through list and check each
//if classToSearchMethodIn does not contain method, throw exception. go through list and check each
for (int i = 0; i < receivingMethods.size(); i++) {
currentType = (String) methodContext.get(currentType).get(receivingMethods.get(i).methodName).keySet().toArray()[0];