From c37844f8d7c9246676184c8c883b9251d226f287 Mon Sep 17 00:00:00 2001 From: cosentino-smeup <32836304+cosentino-smeup@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:36:29 +0000 Subject: [PATCH 1/3] add SPLAT_ALL_INDICATORS for *IN implementation --- .../src/main/antlr/RpgLexer.g4 | 2 ++ .../src/main/antlr/RpgParser.g4 | 1 + .../interpreter/ExpressionEvaluation.kt | 17 +++++++++++++++-- .../smeup/rpgparser/parsing/ast/indicators.kt | 4 +--- .../rpgparser/parsing/parsetreetoast/misc.kt | 5 +++++ .../rpgparser/evaluation/InterpreterTest.kt | 7 +++++++ .../src/test/resources/MOVEAIN.rpgle | 13 +++++++++++++ 7 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 rpgJavaInterpreter-core/src/test/resources/MOVEAIN.rpgle diff --git a/rpgJavaInterpreter-core/src/main/antlr/RpgLexer.g4 b/rpgJavaInterpreter-core/src/main/antlr/RpgLexer.g4 index 764f54437..cacccaf10 100644 --- a/rpgJavaInterpreter-core/src/main/antlr/RpgLexer.g4 +++ b/rpgJavaInterpreter-core/src/main/antlr/RpgLexer.g4 @@ -316,6 +316,7 @@ SPLAT_FILE: '*'[fF][iI][lL][eE]; SPLAT_GETIN: '*'[gG][eE][tT][iI][nN]; SPLAT_HIVAL: '*'[hH][iI][vV][aA][lL]; SPLAT_INIT: '*'[iI][nN][iI][tT]; +SPLAT_ALL_INDICATORS: '*' [iI] [nN] [ ] [ ]; SPLAT_INDICATOR : ( '*' [iI] [nN] [0-9] [0-9] | '*' [iI] [nN] [a-zA-Z] [a-zA-Z] | '*' [iI] [nN] '(' [0-9] [0-9] ')' ); @@ -1032,6 +1033,7 @@ CS_Factor2_SPLAT_GETIN : SPLAT_GETIN {35+6<= getCharPositionInLine() && getCharP CS_Factor2_SPLAT_HIVAL : SPLAT_HIVAL {35+6<= getCharPositionInLine() && getCharPositionInLine()<=48}? -> type(SPLAT_HIVAL); CS_Factor2_SPLAT_INIT : SPLAT_INIT {35+5<= getCharPositionInLine() && getCharPositionInLine()<=48}? -> type(SPLAT_INIT); CS_Factor2_SPLAT_INDICATOR : SPLAT_INDICATOR {35+4<= getCharPositionInLine() && getCharPositionInLine()<=48}? -> type(SPLAT_INDICATOR); +CS_Factor2_SPLAT_ALL_INDICATORS : SPLAT_ALL_INDICATORS {35+4<= getCharPositionInLine() && getCharPositionInLine()<=48}? -> type(SPLAT_ALL_INDICATORS); CS_Factor2_SPLAT_INZSR : SPLAT_INZSR {35+6<= getCharPositionInLine() && getCharPositionInLine()<=48}? -> type(SPLAT_INZSR); CS_Factor2_SPLAT_IN : SPLAT_IN {35+3<= getCharPositionInLine() && getCharPositionInLine()<=48}? -> type(SPLAT_IN); CS_Factor2_SPLAT_JOBRUN : SPLAT_JOBRUN {35+7<= getCharPositionInLine() && getCharPositionInLine()<=48}? -> type(SPLAT_JOBRUN); diff --git a/rpgJavaInterpreter-core/src/main/antlr/RpgParser.g4 b/rpgJavaInterpreter-core/src/main/antlr/RpgParser.g4 index 8d7c243e7..601d94e5a 100644 --- a/rpgJavaInterpreter-core/src/main/antlr/RpgParser.g4 +++ b/rpgJavaInterpreter-core/src/main/antlr/RpgParser.g4 @@ -2581,6 +2581,7 @@ SPLAT_ALL | SPLAT_HIVAL | SPLAT_INIT | SPLAT_INDICATOR + | SPLAT_ALL_INDICATORS | SPLAT_INZSR | SPLAT_IN | SPLAT_JOBRUN diff --git a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/ExpressionEvaluation.kt b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/ExpressionEvaluation.kt index 57d876596..7d9f012aa 100644 --- a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/ExpressionEvaluation.kt +++ b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/ExpressionEvaluation.kt @@ -29,6 +29,7 @@ import java.math.MathContext import java.math.RoundingMode import java.time.temporal.ChronoUnit import java.time.ZoneId +import java.util.HashMap import kotlin.math.abs import kotlin.math.sqrt @@ -676,8 +677,20 @@ class ExpressionEvaluation( override fun eval(expression: AssignmentExpr) = throw RuntimeException("AssignmentExpr should be handled by the interpreter: $expression") - override fun eval(expression: GlobalIndicatorExpr) = - throw RuntimeException("PredefinedGlobalIndicatorExpr should be handled by the interpreter: $expression") + override fun eval(expression: GlobalIndicatorExpr): Value { + + for (i in 1..99) + if (interpreterStatus.indicators[i] == null) + interpreterStatus.indicators[i] = BooleanValue(false) + val ret = interpreterStatus.indicators.map { it.value } + return StringValue( + ret.map { + if (it.value) { + 1 } else { + 0 + } + }.joinToString { it.toString() }) + } override fun eval(expression: ParmsExpr): Value { return IntValue(interpreterStatus.params.toLong()) diff --git a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/ast/indicators.kt b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/ast/indicators.kt index 652239edf..a95dc634d 100644 --- a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/ast/indicators.kt +++ b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/ast/indicators.kt @@ -22,9 +22,7 @@ data class IndicatorExpr(val index: IndicatorKey, override val position: Positio @Serializable data class GlobalIndicatorExpr(override val position: Position? = null) : AssignableExpression(position) { - override fun size(): Int { - TODO("not implemented") // To change body of created functions use File | Settings | File Templates. - } + override fun size(): Int = 99 override fun evalWith(evaluator: Evaluator): Value = evaluator.eval(this) } diff --git a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/parsetreetoast/misc.kt b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/parsetreetoast/misc.kt index dcd4189c7..a489f75ed 100644 --- a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/parsetreetoast/misc.kt +++ b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/parsetreetoast/misc.kt @@ -667,6 +667,11 @@ internal fun SymbolicConstantsContext.toAst(conf: ToAstConfiguration = ToAstConf position = position ) } + this.SPLAT_ALL_INDICATORS() != null -> { + GlobalIndicatorExpr( + position = position + ) + } this.SPLAT_ALL() != null -> { val content: LiteralContext = this.parent.getChild(1) as LiteralContext AllExpr(content.toAst(conf), position) diff --git a/rpgJavaInterpreter-core/src/test/kotlin/com/smeup/rpgparser/evaluation/InterpreterTest.kt b/rpgJavaInterpreter-core/src/test/kotlin/com/smeup/rpgparser/evaluation/InterpreterTest.kt index f70d2ec78..73d41a937 100644 --- a/rpgJavaInterpreter-core/src/test/kotlin/com/smeup/rpgparser/evaluation/InterpreterTest.kt +++ b/rpgJavaInterpreter-core/src/test/kotlin/com/smeup/rpgparser/evaluation/InterpreterTest.kt @@ -2202,4 +2202,11 @@ Test 6 val expected = listOf("A1_OK", "A2_OK", "A3_OK", "N1_OK", "N2_OK", "N3_OK", "DS_OK", "DSA1_OK", "DSA2_OK") assertEquals(expected, "RESET01".outputOf()) } + + @Test + fun executeMOVEAIN() { + val expected = listOf("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000") + assertASTCanBeProduced("MOVEAIN", printTree = true) + assertEquals(expected, "MOVEAIN".outputOf()) + } } diff --git a/rpgJavaInterpreter-core/src/test/resources/MOVEAIN.rpgle b/rpgJavaInterpreter-core/src/test/resources/MOVEAIN.rpgle new file mode 100644 index 000000000..2254cbfe2 --- /dev/null +++ b/rpgJavaInterpreter-core/src/test/resources/MOVEAIN.rpgle @@ -0,0 +1,13 @@ + D* £DBG_Ind S 99 + D £DBG_Sch S 1 DIM(99) + * + C MOVEL *ON *IN44 + C SETON 88 + C* MOVEA(P) *IN £DBG_Ind + C MOVEA(P) *IN £DBG_Sch + * Expected: + * '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000' + * + C* £DBG_Ind DSPLY + C £DBG_Sch DSPLY + C SETON LR \ No newline at end of file From 7c7c7806d14f84a5823803396f931546d2c02b5b Mon Sep 17 00:00:00 2001 From: cosentino-smeup <32836304+cosentino-smeup@users.noreply.github.com> Date: Wed, 17 Jan 2024 16:53:18 +0000 Subject: [PATCH 2/3] remove GlobalIndicatorExpr separator --- .../interpreter/ExpressionEvaluation.kt | 3 +-- .../rpgparser/evaluation/InterpreterTest.kt | 5 +++-- .../src/test/resources/MOVEAIN.rpgle | 20 ++++++++++++------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/ExpressionEvaluation.kt b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/ExpressionEvaluation.kt index 7d9f012aa..ba4c58f05 100644 --- a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/ExpressionEvaluation.kt +++ b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/ExpressionEvaluation.kt @@ -29,7 +29,6 @@ import java.math.MathContext import java.math.RoundingMode import java.time.temporal.ChronoUnit import java.time.ZoneId -import java.util.HashMap import kotlin.math.abs import kotlin.math.sqrt @@ -689,7 +688,7 @@ class ExpressionEvaluation( 1 } else { 0 } - }.joinToString { it.toString() }) + }.joinToString(separator = "") { it.toString() }) } override fun eval(expression: ParmsExpr): Value { diff --git a/rpgJavaInterpreter-core/src/test/kotlin/com/smeup/rpgparser/evaluation/InterpreterTest.kt b/rpgJavaInterpreter-core/src/test/kotlin/com/smeup/rpgparser/evaluation/InterpreterTest.kt index 73d41a937..d1a6143df 100644 --- a/rpgJavaInterpreter-core/src/test/kotlin/com/smeup/rpgparser/evaluation/InterpreterTest.kt +++ b/rpgJavaInterpreter-core/src/test/kotlin/com/smeup/rpgparser/evaluation/InterpreterTest.kt @@ -2205,8 +2205,9 @@ Test 6 @Test fun executeMOVEAIN() { - val expected = listOf("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000") - assertASTCanBeProduced("MOVEAIN", printTree = true) + val expected = listOf( + "111000000000000000000000000000000000000000010000000000000000000000000000000000000000000100000000000", + "001000000000000000000000000000000000000000010000000000000000000000000000000000000000000100000000000") assertEquals(expected, "MOVEAIN".outputOf()) } } diff --git a/rpgJavaInterpreter-core/src/test/resources/MOVEAIN.rpgle b/rpgJavaInterpreter-core/src/test/resources/MOVEAIN.rpgle index 2254cbfe2..12fcd9bf6 100644 --- a/rpgJavaInterpreter-core/src/test/resources/MOVEAIN.rpgle +++ b/rpgJavaInterpreter-core/src/test/resources/MOVEAIN.rpgle @@ -1,13 +1,19 @@ - D* £DBG_Ind S 99 - D £DBG_Sch S 1 DIM(99) + D £DBG_Ind S 99 * C MOVEL *ON *IN44 + C MOVE *ON *IN03 C SETON 88 - C* MOVEA(P) *IN £DBG_Ind - C MOVEA(P) *IN £DBG_Sch + C SETON 0102 + C MOVEA(P) *IN £DBG_Ind * Expected: - * '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000' + * '111000000000000000000000000000000000000000010000000000000000000000000000000000000000000100000000000' + C £DBG_Ind DSPLY + * + C SETOFF 0102 + C* MOVEL '1' *IN03 + C MOVEA(P) *IN £DBG_Ind + * Expected: + * '000000000000000000000000000000000000000000010000000000000000000000000000000000000000000100000000000' + C £DBG_Ind DSPLY * - C* £DBG_Ind DSPLY - C £DBG_Sch DSPLY C SETON LR \ No newline at end of file From 72ad6fde091d7bbd80b25e0d8fa59dca683ff83f Mon Sep 17 00:00:00 2001 From: cosentino-smeup <32836304+cosentino-smeup@users.noreply.github.com> Date: Tue, 23 Jan 2024 12:05:04 +0000 Subject: [PATCH 3/3] implement a performance improvement required in the code review --- .../interpreter/ExpressionEvaluation.kt | 17 +++++------------ .../smeup/rpgparser/parsing/ast/indicators.kt | 2 +- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/ExpressionEvaluation.kt b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/ExpressionEvaluation.kt index ba4c58f05..977bea098 100644 --- a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/ExpressionEvaluation.kt +++ b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/ExpressionEvaluation.kt @@ -677,18 +677,11 @@ class ExpressionEvaluation( throw RuntimeException("AssignmentExpr should be handled by the interpreter: $expression") override fun eval(expression: GlobalIndicatorExpr): Value { - - for (i in 1..99) - if (interpreterStatus.indicators[i] == null) - interpreterStatus.indicators[i] = BooleanValue(false) - val ret = interpreterStatus.indicators.map { it.value } - return StringValue( - ret.map { - if (it.value) { - 1 } else { - 0 - } - }.joinToString(separator = "") { it.toString() }) + val value = StringBuilder() + for (i in IndicatorType.Predefined.range) { + value.append(if (interpreterStatus.indicators[i] == null) "0" else if (interpreterStatus.indicators[i]!!.value) "1" else "0") + } + return StringValue(value.toString()) } override fun eval(expression: ParmsExpr): Value { diff --git a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/ast/indicators.kt b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/ast/indicators.kt index a95dc634d..c2285745a 100644 --- a/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/ast/indicators.kt +++ b/rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/ast/indicators.kt @@ -22,7 +22,7 @@ data class IndicatorExpr(val index: IndicatorKey, override val position: Positio @Serializable data class GlobalIndicatorExpr(override val position: Position? = null) : AssignableExpression(position) { - override fun size(): Int = 99 + override fun size(): Int = IndicatorType.Predefined.range.last override fun evalWith(evaluator: Evaluator): Value = evaluator.eval(this) }