From 878794ffa78bfb3adf856adeeb50340a75fba872 Mon Sep 17 00:00:00 2001 From: ahmad Date: Sat, 29 Jun 2024 10:32:40 +0200 Subject: [PATCH] Fixed using same parameter in different methods or constructors --- .../typedast/typedclass/TypedClass.java | 44 ++++--------------- .../typedclass/TypedFieldVarAccess.java | 2 +- .../typedast/typedclass/TypedMethod.java | 1 + .../typedast/typedclass/TypedParameter.java | 4 +- 4 files changed, 13 insertions(+), 38 deletions(-) diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index aec8ef4..9c0ffea 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -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)) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java index 0bcc21a..3ab9a9e 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java @@ -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); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index 5754dac..674f4e9 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -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) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java b/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java index 6cc752a..67d106b 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java @@ -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"); } }