fixed another localvaridentifier that just isnt local actually

This commit is contained in:
Krauß, Josefine 2024-06-27 09:30:31 +02:00
parent ed2f64eff9
commit 6365e994d2
4 changed files with 17 additions and 3 deletions

View File

@ -1,9 +1,11 @@
class Example1 { class Example1 {
int instVar;
Example e; Example e;
public Example1(){} public Example1(){}
int meth(int n){ int meth(int n){
Example e = new Example(); Example e = new Example();
int i = m1(2).m2().m3(); instVar = 2;
int i = m1(instVar).m2().m3();
return 0; return 0;
} }

View File

@ -17,6 +17,7 @@ import java.util.Objects;
public class LocalVarIdentifier extends AbstractType implements IExpression{ public class LocalVarIdentifier extends AbstractType implements IExpression{
String identifier; String identifier;
public String thisClass;
public LocalVarIdentifier(String identifier){ public LocalVarIdentifier(String identifier){
this.identifier = identifier; this.identifier = identifier;
} }
@ -31,6 +32,12 @@ public class LocalVarIdentifier extends AbstractType implements IExpression{
if (localVars.containsKey(identifier)) { if (localVars.containsKey(identifier)) {
result.type = localVars.get(identifier); result.type = localVars.get(identifier);
} else { } else {
// check if instvar
result.type = typeContext.get(thisClass).get(identifier);
if(result.type != null){
setTypeCheckResult(result);
return result;
}
throw new TypeCheckException("Local var " + identifier + " does not exist."); throw new TypeCheckException("Local var " + identifier + " does not exist.");
} }
setTypeCheckResult(result); setTypeCheckResult(result);

View File

@ -67,9 +67,8 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
currentType = (String) methodContext.get(currentType).get(receivingMethods.get(i).methodName).keySet().toArray()[0]; currentType = (String) methodContext.get(currentType).get(receivingMethods.get(i).methodName).keySet().toArray()[0];
if(currentType == null) if(currentType == null)
throw new TypeCheckException("The method " + methodName + " was not found in "+ classToSearchMethodIn + "."); throw new TypeCheckException("The method " + methodName + " was not found in "+ classToSearchMethodIn + ".");
receivingMethods.get(i).thisClass = this.thisClass;
receivingMethods.get(i).checkParameters(methodContext, typeContext, localVars); receivingMethods.get(i).checkParameters(methodContext, typeContext, localVars);
System.out.println(currentType);
} }
currentType = (String) methodContext.get(currentType).get(methodName).keySet().toArray()[0]; currentType = (String) methodContext.get(currentType).get(methodName).keySet().toArray()[0];

View File

@ -2,8 +2,10 @@ package abstractSyntaxTree.StatementExpression;
import TypeCheck.TypeCheckException; import TypeCheck.TypeCheckException;
import abstractSyntaxTree.Expression.IExpression; import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Expression.LocalVarIdentifier;
import abstractSyntaxTree.Node; import abstractSyntaxTree.Node;
import abstractSyntaxTree.Parameter.ParameterList; import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Statement.LocalVarDecl;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -11,6 +13,7 @@ import java.util.List;
public class ReceivingMethod implements Node { public class ReceivingMethod implements Node {
public String methodName; public String methodName;
public List<IExpression> arguments; public List<IExpression> arguments;
public String thisClass;
public ReceivingMethod(String methodName, List<IExpression> arguments) { public ReceivingMethod(String methodName, List<IExpression> arguments) {
this.methodName = methodName; this.methodName = methodName;
@ -18,6 +21,9 @@ public class ReceivingMethod implements Node {
} }
void checkParameters(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException { void checkParameters(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
for (IExpression parameter : arguments){ for (IExpression parameter : arguments){
if(parameter instanceof LocalVarIdentifier localVarIdentifier){
localVarIdentifier.thisClass = thisClass;
}
parameter.typeCheck(methodContext, typeContext, localVars); parameter.typeCheck(methodContext, typeContext, localVars);
} }
} }