Updated TypedAssignment and add TODO

This commit is contained in:
ahmad 2024-05-12 19:56:00 +02:00
parent cef0c5ba86
commit de51bcd825
2 changed files with 35 additions and 13 deletions

View File

@ -16,9 +16,8 @@ import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
public class TypedAssignment implements TypedStatement {
private String varName;
private TypedExpression value;
private TypedExpression recursiveOwnerChain;
private TypedFieldVarAccess location;
private Type type;
public TypedAssignment(TypedClass clas, Assignment untyped) {
@ -26,30 +25,29 @@ public class TypedAssignment implements TypedStatement {
}
public void convertToTypedAssignment(TypedClass clas, Assignment untyped) {
varName = untyped.location().id();
value = convertExpression(clas, untyped.value());
recursiveOwnerChain = convertExpression( clas, untyped.location().recursiveOwnerChain());
location = new TypedFieldVarAccess(clas, untyped.location());
}
@Override
public Type typeCheck(TypedClass clas) {
Type typeLeft = null;
if (clas.isThereField(varName)) {
typeLeft = clas.getFieldType(varName);
if (clas.isThereField(location.getName())) {
typeLeft = clas.getFieldType(location.getName());
} else {
if (clas.isCurrentMethodPresent() && !clas.isCurrentConstructorPresent()) {
if (clas.getCurrentMethod().isLocalVariableInMethod(varName)) {
typeLeft = clas.getCurrentMethod().getLocalVariableType(varName);
if (clas.getCurrentMethod().isLocalVariableInMethod(location.getName())) {
typeLeft = clas.getCurrentMethod().getLocalVariableType(location.getName());
} else {
throw new RuntimeException("Variable " + varName + " not declared in method");
throw new RuntimeException("Variable " + location.getName() + " not declared in method");
}
}
if (!clas.isCurrentMethodPresent() && clas.isCurrentConstructorPresent()) {
if (clas.getCurrentConstructor().isLocalVariableInConstructor(varName)) {
typeLeft = clas.getCurrentConstructor().getLocalVariableType(varName);
if (clas.getCurrentConstructor().isLocalVariableInConstructor(location.getName())) {
typeLeft = clas.getCurrentConstructor().getLocalVariableType(location.getName());
} else {
throw new RuntimeException("Variable " + varName + " not declared in constructor");
throw new RuntimeException("Variable " + location.getName() + " not declared in constructor");
}
}
}

View File

@ -12,6 +12,8 @@ import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
//TODO: test this after fixing error from parser
@Data
@NoArgsConstructor
public class TypedMethodCall implements TypedExpression, TypedStatement {
@ -28,11 +30,33 @@ public class TypedMethodCall implements TypedExpression, TypedStatement {
for (Expression arg : unTypedMethodCall.args()) {
args.add(convertExpression(clas, arg));
}
recipient.getName();
}
@Override
public Type typeCheck(TypedClass clas) {
//TODO: implement this
/* if (clas.isCurrentMethodPresent()) {
List<TypedMethod> methods = clas.getTypedMethods().stream()
.filter(method -> method.getName().equals(name))
.toList();
for (TypedMethod method : methods) {
if (method.getTypedParameters().size() == args.size()) {
boolean allMatch = true;
for (int i = 0; i < args.size(); i++) {
if (!args.get(i).typeCheck(clas).equals(method.getTypedParameters().get(i).getType())) {
allMatch = false;
break;
}
}
if (allMatch) {
return method.getReturnType();
}
}
}
} */
return null;
}