Fixed error of constructor which is not named like class name

This commit is contained in:
ahmad 2024-07-01 19:13:56 +02:00
parent de9365d5c9
commit ad770213d3
2 changed files with 9 additions and 6 deletions

View File

@ -56,7 +56,7 @@ public class TypedClass implements TypedNode {
typedDeclarations.add(new TypedDeclaration(typedProgram, declaration));
}
for (Constructor constructor : c.constructors()) {
typedConstructors.add(new TypedConstructor(typedProgram, constructor));
typedConstructors.add(new TypedConstructor(typedProgram, constructor, this.className));
}
for (Method method : c.methods()) {

View File

@ -32,9 +32,14 @@ public class TypedConstructor implements TypedNode {
this.typedParameters = typedParameters;
this.typedBlock = typedBlock;
}
public TypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor, String className) {
convertToTypedConstructor(typedProgram, unTypedConstructor, className);
}
public void convertToTypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor) {
public void convertToTypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor, String className) {
if(!unTypedConstructor.className().equals(className)) {
throw new RuntimeException("Constructor name "+ unTypedConstructor.className() +" must be the same as class name" + className);
}
name = unTypedConstructor.className();
convertToTypedParameter(typedProgram, unTypedConstructor.params());
type = Type.VOID;
@ -59,9 +64,7 @@ public class TypedConstructor implements TypedNode {
return type;
}
public TypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor) {
convertToTypedConstructor(typedProgram, unTypedConstructor);
}
public boolean isLocalVariablePresent(String localVarName) {
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));