Merge remote-tracking branch 'origin/master'

This commit is contained in:
StefanZ3 2024-05-31 11:55:58 +02:00
commit d4cc24239d
4 changed files with 51 additions and 24 deletions

View File

@ -7,7 +7,9 @@ class Example {
class Example2 {
boolean instVarBool;
int m(int n){
if(instVarBool){
boolean localBool;
localBool = true;
if(localBool){
return n;
}
return -1;

View File

@ -1,6 +1,12 @@
package abstractSyntaxTree.Expression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class IntConstantExpression extends AbstractType implements IExpression{
public int value;
@ -8,4 +14,14 @@ public class IntConstantExpression extends AbstractType implements IExpression{
public IntConstantExpression(int value) {
this.value = value;
}
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
}
}

View File

@ -24,7 +24,13 @@ public class LocalVarIdentifier implements IExpression{
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
TypeCheckResult result = new TypeCheckResult();
if (localVars.containsKey(identifier)) {
result.type = localVars.get(identifier);
} else {
throw new Exception("TypeCheck Exception: local var does not exist");
}
return result;
}
@Override

View File

@ -34,6 +34,8 @@ public class BlockStatement extends AbstractType implements IStatement {
}
for (IStatement statement : statements) {
// todo remove later if there are no null statement any more
if (statement != null) {
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
if (typeOfCurrentStatement.type.contains(",")) {
@ -65,6 +67,7 @@ public class BlockStatement extends AbstractType implements IStatement {
throw new Exception("TypeCheck Exception: Block returns the wrong type.");
}
}
}
result.type = this.returnType;
// todo check if the block returns the needed return type in every case
// todo ignore unreachable statements?