From 61284de8f68bdefbb9de5b9629eaca333f36ffd0 Mon Sep 17 00:00:00 2001 From: Davide Palladino Date: Wed, 4 Dec 2024 11:46:32 +0100 Subject: [PATCH] Applied refactoring --- .../interpreter/internal_interpreter.kt | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/internal_interpreter.kt b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/internal_interpreter.kt index 7f30e5b04..dc1be11ef 100644 --- a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/internal_interpreter.kt +++ b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/internal_interpreter.kt @@ -287,17 +287,9 @@ open class InternalInterpreter( * In accord to documentation (see https://www.ibm.com/docs/en/i/7.5?topic=codes-plist-identify-parameter-list): * when control transfers to called program, at the beginning, the contents of the Result field is placed in * the Factor 1 field. - * TODO: IS NECESSARY A REFACTORING!!! */ - compilationUnit.entryPlist?.params - ?.filter { plistParam -> plistParam.factor1 is DataRefExpr } - ?.any { plistParamFiltered -> (plistParamFiltered.factor1 as DataRefExpr).variable.name.equals(it.name, true) } == true -> { - - val resultName = compilationUnit.entryPlist?.params - ?.filter { plistParam -> plistParam.factor1 is DataRefExpr } - ?.firstOrNull { plistParamFiltered -> (plistParamFiltered.factor1 as DataRefExpr).variable.name.equals(it.name, true) } - ?.result?.name - + it.isInPlist(compilationUnit) -> { + val resultName = it.getResultNameByFactor1(compilationUnit) if (resultName == null || initialValues[resultName] is NullValue) { blankValue(it) } else { @@ -405,6 +397,45 @@ open class InternalInterpreter( } } + /** + * Retrieves the result name associated with the current `AbstractDataDefinition` instance + * from the parameter list (PList) of the specified `CompilationUnit`. + * + * This function searches the PList for the first parameter where `factor1` is of type `DataRefExpr` + * and its variable name matches the name of the current `AbstractDataDefinition` (case-insensitively). + * If such a parameter is found, its associated result name is returned. + * + * @param compilationUnit the compilation unit whose entry PList is to be checked + * @return the result name associated with the matching parameter, or `null` if no match is found + */ + private fun AbstractDataDefinition.getResultNameByFactor1(compilationUnit: CompilationUnit): String? { + val resultName = compilationUnit.entryPlist?.params + ?.filter { plistParam -> plistParam.factor1 is DataRefExpr } + ?.firstOrNull { plistParamFiltered -> + (plistParamFiltered.factor1 as DataRefExpr).variable.name.equals( + this.name, + true + ) + } + ?.result?.name + return resultName + } + + /** + * Checks if the current `AbstractDataDefinition` instance is present in the parameter list (PList) + * of the specified `CompilationUnit`. + * + * This function evaluates whether the `AbstractDataDefinition` matches any parameter in the PList + * by comparing their names (case-insensitively). Parameters in the PList are filtered to include + * only those with a `factor1` of type `DataRefExpr`. + * + * @param compilationUnit the compilation unit whose entry PList is to be checked + * @return `true` if the `AbstractDataDefinition` is present in the PList, otherwise `false` + */ + private fun AbstractDataDefinition.isInPlist(compilationUnit: CompilationUnit) = compilationUnit.entryPlist?.params + ?.filter { plistParam -> plistParam.factor1 is DataRefExpr } + ?.any { plistParamFiltered -> (plistParamFiltered.factor1 as DataRefExpr).variable.name.equals(this.name, true) } == true + private fun toArrayValue(compileTimeArray: CompileTimeArray, arrayType: ArrayType): Value { // It is not clear why the compileTimeRecordsPerLine on the array type is null // probably it is an error during the ast processing.