Skip to content

Commit

Permalink
Merge pull request #651 from smeup/bugfix/LS24004654/wrong-result-for…
Browse files Browse the repository at this point in the history
…-lookup

Bugfix/LS24004654/Use CTDATA for DS field declared as Array
  • Loading branch information
lanarimarco authored Nov 6, 2024
2 parents 0c8ac74 + b9bd836 commit 4577d5b
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ data class FieldDefinition(
}
}

fun isCompileTimeArray() = type is ArrayType && type.compileTimeArray()

fun toDataStructureValue(value: Value) = type.toDataStructureValue(value)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,14 @@ open class InternalInterpreter(
}
}
}
is FieldDefinition -> {
if (it.isCompileTimeArray()) {
value = toArrayValue(
compilationUnit.compileTimeArray(index++),
(it.type as ArrayType)
)
}
}
is InStatementDataDefinition -> {
value = if (it.parent is PlistParam) {
when (it.name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,8 @@ data class FieldInfo(
var explicitElementType: Type? = null,
val initializationValue: Expression? = null,
val descend: Boolean = false,
val position: Position?
val position: Position?,
val compileTimeRecordsPerLine: Int? = null
) {

var startOffset: Int? = explicitStartOffset // these are mutable as they can be calculated using next
Expand Down Expand Up @@ -672,7 +673,7 @@ data class FieldInfo(
return if (arraySizeDeclared == null) {
elementType
} else {
ArrayType(elementType, arraySizeDeclared!!)
ArrayType(elementType, arraySizeDeclared!!, compileTimeRecordsPerLine)
}
}

Expand Down Expand Up @@ -871,6 +872,23 @@ private fun RpgParser.Parm_fixedContext.toFieldInfo(
// Set the SORTA order
val descend = this.keyword().find { it.keyword_descend() != null } != null

// Set the CTDATA flag
val compileTimeArray = this.keyword().find { it.keyword_ctdata() != null } != null

// Set the PERRCD flag
val elementsPerLineExpression = this.keyword().find { it.keyword_perrcd() != null }.let { it?.keyword_perrcd()?.simpleExpression()?.toAst(conf) }

// Calculating compile time records per line
var compileTimeRecordsPerLine: Int? = null
if (compileTimeArray) {
if (elementsPerLineExpression != null) {
compileTimeRecordsPerLine = conf.compileTimeInterpreter.evaluate(this.rContext(), elementsPerLineExpression).asInt().value.toInt()
} else {
compileTimeRecordsPerLine = 1
}
require(compileTimeRecordsPerLine > 0)
}

if (overlay != null) {
this.name
val pos = overlay.keyword_overlay().pos
Expand Down Expand Up @@ -922,7 +940,9 @@ private fun RpgParser.Parm_fixedContext.toFieldInfo(
arraySizeDeclaredOnThisField = this.arraySizeDeclared(conf),
initializationValue = initializationValue,
descend = descend,
position = this.toPosition(conf.considerPosition))
position = this.toPosition(conf.considerPosition),
compileTimeRecordsPerLine = compileTimeRecordsPerLine
)
}

fun RpgParser.Dcl_dsContext.declaredSize(): Int? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,12 @@ private fun Dcl_dsContext.useExtName(): Boolean {
}

internal fun EndSourceContext.toAst(conf: ToAstConfiguration = ToAstConfiguration()): CompileTimeArray {

fun cName(s: String) =
if (s.contains("**CTDATA")) {
s.substringAfter("**CTDATA ").replace("\\s".toRegex(), "")
} else {
s.replace(s, "")
}
fun cName(s: String) =
if (s.trim().matches(Regex("^\\*\\*[\\s]*(CTDATA)*([\\s]+[\\S]+)\$"))) {
s.substringAfter("**").trim().substringAfter("CTDATA").trim()
} else {
s.replace(s, "")
}

return CompileTimeArray(
cName(this.endSourceHead().text), // TODO: change grammar to get **CTDATA name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,4 +693,54 @@ open class MULANGT02ConstAndDSpecTest : MULANGTTest() {
val expected = listOf("10.000000")
assertEquals(expected, "smeup/MUDRNRAPU00132".outputOf(configuration = smeupConfig))
}

/**
* DS field declared as Array and CTDATA. In this case between CTDATA and its name there is more space.
* @see #LS24004654
*/
@Test
fun executeMUDRNRAPU00150() {
val expected = listOf("*SCPAccesso da script 00S")
assertEquals(expected, "smeup/MUDRNRAPU00150".outputOf(configuration = smeupConfig))
}

/**
* DS field declared as Array and CTDATA. In this case there isn't CTDATA but more space between name and stars.
* @see #LS24004654
*/
@Test
fun executeMUDRNRAPU00151() {
val expected = listOf("*SCPAccesso da script 00S")
assertEquals(expected, "smeup/MUDRNRAPU00151".outputOf(configuration = smeupConfig))
}

/**
* DS field declared as Array and CTDATA. In this case there is only CTDATA.
* @see #LS24004654
*/
@Test
fun executeMUDRNRAPU00152() {
val expected = listOf("*SCPAccesso da script 00S")
assertEquals(expected, "smeup/MUDRNRAPU00152".outputOf(configuration = smeupConfig))
}

/**
* DS field declared as Array and CTDATA. In this case there is only the name.
* @see #LS24004654
*/
@Test
fun executeMUDRNRAPU00153() {
val expected = listOf("*SCPAccesso da script 00S")
assertEquals(expected, "smeup/MUDRNRAPU00153".outputOf(configuration = smeupConfig))
}

/**
* DS field declared as Array and CTDATA. In this case there are only the stars.
* @see #LS24004654
*/
@Test
fun executeMUDRNRAPU00154() {
val expected = listOf("*SCPAccesso da script 00S")
assertEquals(expected, "smeup/MUDRNRAPU00154".outputOf(configuration = smeupConfig))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,14 @@ open class MULANGT15BaseBif1Test : MULANGTTest() {
val expected = listOf("10000", "10000", "10000", "10000")
assertEquals(expected, "smeup/MUDRNRAPU00253".outputOf(configuration = smeupConfig))
}

/**
* DS field declared as Array and CTDATA. In this case the error is manifested as side effect of `%LOOKUP`.
* @see #LS24004654
*/
@Test
fun executeMUDRNRAPU00149() {
val expected = listOf("1", "*SCP")
assertEquals(expected, "smeup/MUDRNRAPU00149".outputOf(configuration = smeupConfig))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
V* ==============================================================
V* 30/10/2024 APU001 Creation
V* 31/10/2024 APU001 Edited PROGRAM GOAL
V* 06/11/2024 APU001 Edited PROGRAM GOAL
V* ==============================================================
O * PROGRAM GOAL
O * DS field declared as Array and CTDATA. In this case the error
O * is manifested as side effect of `%LOOKUP`.
V* ==============================================================
O * JARIKO ANOMALY
O * Before the fix, the error occurred was
O * `Failed requirement'
V* ==============================================================
D £IQ5D DS 5000
D £IQ5DCD 15

D DS
DIQ5 42 DIM(3) CTDATA PERRCD(1)
D IQ5CD 4 OVERLAY(IQ5:1)
D IQ5DE 30 OVERLAY(IQ5:*NEXT)
D IQ5PG 2 OVERLAY(IQ5:*NEXT)

D$X S 5 0
DNQ5 S 5 0 INZ(3)
DA4 S 4

C EVAL A4='*SCP'
C EVAL $X=%LOOKUP(A4:IQ5CD:1:NQ5)
C $X DSPLY
C EVAL £IQ5DCD=IQ5CD($X) #Failed requirement. Depends from %LOOKUP result
C £IQ5DCD DSPLY

C SETON LR

** Descrizione PgPref.D
*SCPAccesso da script 00S
*IQ2Accesso da Pgm £IQ2 18Q
*SETAccesso da SCP_NAV 17T
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
V* ==============================================================
V* 31/10/2024 APU001 Creation
V* 06/11/2024 APU001 Simplification
V* ==============================================================
O * PROGRAM GOAL
O * DS field declared as Array and CTDATA. In this case between
O * CTDATA and its name there is more space.
V* ==============================================================
D DS
DIQ5 37 DIM(3) CTDATA PERRCD(1)

C IQ5(1) DSPLY
C SETON LR

** CTDATA IQ5
*SCPAccesso da script 00S
*IQ2Accesso da Pgm £IQ2 18Q
*SETAccesso da SCP_NAV 17T
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
V* ==============================================================
V* 31/10/2024 APU001 Creation
V* 06/11/2024 APU001 Simplification
V* ==============================================================
O * PROGRAM GOAL
O * DS field declared as Array and CTDATA. In this case there
O * isn't CTDATA but more space between name and stars.
V* ==============================================================
D DS
DIQ5 37 DIM(3) CTDATA PERRCD(1)

C IQ5(1) DSPLY
C SETON LR

** IQ5
*SCPAccesso da script 00S
*IQ2Accesso da Pgm £IQ2 18Q
*SETAccesso da SCP_NAV 17T
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
V* ==============================================================
V* 31/10/2024 APU001 Creation
V* 06/11/2024 APU001 Simplification
V* ==============================================================
O * PROGRAM GOAL
O * DS field declared as Array and CTDATA. In this case there
O * is only CTDATA.
V* ==============================================================
D DS
DIQ5 37 DIM(3) CTDATA PERRCD(1)

C IQ5(1) DSPLY
C SETON LR

** CTDATA
*SCPAccesso da script 00S
*IQ2Accesso da Pgm £IQ2 18Q
*SETAccesso da SCP_NAV 17T
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
V* ==============================================================
V* 31/10/2024 APU001 Creation
V* 06/11/2024 APU001 Simplification
V* ==============================================================
O * PROGRAM GOAL
O * DS field declared as Array and CTDATA. In this case there is
O * only the name.
V* ==============================================================
D DS
DIQ5 37 DIM(3) CTDATA PERRCD(1)

C IQ5(1) DSPLY
C SETON LR

** IQ5
*SCPAccesso da script 00S
*IQ2Accesso da Pgm £IQ2 18Q
*SETAccesso da SCP_NAV 17T
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
V* ==============================================================
V* 31/10/2024 APU001 Creation
V* 06/11/2024 APU001 Simplification
V* ==============================================================
O * PROGRAM GOAL
O * DS field declared as Array and CTDATA. In this case there are
O * only the stars
V* ==============================================================
D DS
DIQ5 37 DIM(3) CTDATA PERRCD(1)

C IQ5(1) DSPLY
C SETON LR

**
*SCPAccesso da script 00S
*IQ2Accesso da Pgm £IQ2 18Q
*SETAccesso da SCP_NAV 17T

0 comments on commit 4577d5b

Please sign in to comment.