Fixed using same parameter in different methods or constructors

This commit is contained in:
ahmad 2024-06-29 10:32:40 +02:00
parent 3b0649f5cd
commit 878794ffa7
4 changed files with 13 additions and 38 deletions

View File

@ -98,37 +98,20 @@ public class TypedClass implements TypedNode {
return type;
}
public boolean isParameterWitNameInMethod(String parameterName) {
for (TypedMethod m : typedMethods) {
for (TypedParameter p : m.getTypedParameters()) {
if (p.getParaName().equals(parameterName)) {
return true;
}
}
}
return false;
public boolean isParameterWitNameInCurrentMethod(String parameterName) {
return this.getCurrentMethod().getTypedParameters().stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName));
}
public boolean isParameterWitNameInConstructor(String parameterName) {
for (TypedConstructor c : typedConstructors) {
for (TypedParameter p : c.getTypedParameters()) {
if (p.getParaName().equals(parameterName)) {
return true;
}
}
}
return false;
public boolean isParameterWitNameInCurrentConstructor(String parameterName) {
return this.getCurrentConstructor().getTypedParameters().stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName));
}
public Type getParameterTypeInCurrentMethod(String parameterName) {
for (TypedMethod m : typedMethods) {
for (TypedParameter p : m.getTypedParameters()) {
if (p.getParaName().equals(parameterName)) {
return p.getType();
}
}
}
return null;
return getCurrentMethod().getTypedParameters().stream().filter(parameter -> parameter.getParaName().equals(parameterName)).findFirst().get().getType();
}
public Type getParameterTypeInCurrentConstructor(String parameterName) {
return getCurrentConstructor().getTypedParameters().stream().filter(parameter -> parameter.getParaName().equals(parameterName)).findFirst().get().getType();
}
public boolean isMethodOfCurrentClass(String methodName) {
@ -149,15 +132,6 @@ public class TypedClass implements TypedNode {
return null;
}
public Type getParameterTypeInCurrentConstructor(String parameterName) {
for (TypedParameter p : currentConstructor.getTypedParameters()) {
if (p.getParaName().equals(parameterName)) {
return p.getType();
}
}
return null;
}
public boolean isThereField(String fieldName) {
for (TypedDeclaration f : typedDeclarations) {
if (f.getName().equals(fieldName)) {

View File

@ -84,7 +84,7 @@ public class TypedFieldVarAccess implements TypedExpression {
}
private Type checkMethodVariableType(TypedProgram typedProgram) {
if (typedProgram.getCurrentClass().isParameterWitNameInMethod(name)) {
if (typedProgram.getCurrentClass().isParameterWitNameInCurrentMethod(name)) {
type = typedProgram.getCurrentClass().getParameterTypeInCurrentMethod(name);
} else if (typedProgram.getCurrentClass().getCurrentMethod().isLocalVariablePresent(name)) {
type = typedProgram.getCurrentClass().getCurrentMethod().getLocalVariableType(name);

View File

@ -52,6 +52,7 @@ public class TypedMethod implements TypedNode {
public void convertToTypedBlock(TypedProgram typedProgram, Method unTypedMethod) {
typedBlock = new TypedBlock(typedProgram, unTypedMethod.block());
typeCheck(typedProgram);
localVariables.clear();
}
public boolean isLocalVariablePresent(String localVarName) {

View File

@ -35,11 +35,11 @@ public class TypedParameter implements TypedNode {
public Type typeCheck(TypedProgram typedProgram) {
if (typedProgram.getCurrentClass().isCurrentMethodPresent()) {
if (typedProgram.getCurrentClass().isParameterWitNameInMethod(paraName)) {
if (typedProgram.getCurrentClass().isParameterWitNameInCurrentMethod(paraName)) {
throw new RuntimeException("Parameter " + paraName + " already exists");
}
} else if (typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
if (typedProgram.getCurrentClass().isParameterWitNameInConstructor(paraName)) {
if (typedProgram.getCurrentClass().isParameterWitNameInCurrentConstructor(paraName)) {
throw new RuntimeException("Parameter " + paraName + " already exists");
}
}