Skip to content

Commit

Permalink
Merge pull request #416 from smeup/bugfix/NW23001440/CHECK_BIF
Browse files Browse the repository at this point in the history
Bugfix/nw23001440/check bif
  • Loading branch information
lanarimarco authored Feb 13, 2024
2 parents 7ed67ca + 9a6e99b commit 5d1b97a
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interface Evaluator {
fun eval(expression: NotExpr): BooleanValue
fun eval(expression: ScanExpr): Value
fun eval(expression: SubstExpr): Value
fun eval(expression: CheckExpr): Value
fun eval(expression: SubarrExpr): Value
fun eval(expression: LenExpr): Value
fun eval(expression: OffRefExpr): BooleanValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,28 @@ class ExpressionEvaluation(
val result = source.indexOf(value, startIndex)
return IntValue(if (result == -1) 0 else result.toLong() + 1)
}
override fun eval(expression: CheckExpr): Value {
var startpos = 0
if (expression.start != null) {
startpos = expression.start.evalWith(this).asInt().value.toInt()
if (startpos > 0) {
startpos -= 1
}
}
val comparator = expression.value.evalWith(this).asString().value
val base = expression.source.evalWith(this).asString().value.toCharArray()

var result = 0
for (i in startpos until base.size) {
val currChar = base[i]
if (!comparator.contains(currChar)) {
result = i + 1
break
}
}

return IntValue(result.toLong())
}

override fun eval(expression: SubstExpr): Value {
val length = if (expression.length != null) expression.length.evalWith(this).asInt().value.toInt() else 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ data class ScanExpr(
override fun evalWith(evaluator: Evaluator): Value = evaluator.eval(this)
}

// %CHECK
@Serializable
data class CheckExpr(
var value: Expression,
val source: Expression,
val start: Expression? = null,
override val position: Position? = null
) : Expression(position) {
override fun evalWith(evaluator: Evaluator): Value = evaluator.eval(this)
}

// %XLATE
@Serializable
data class TranslateExpr(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private val modules = SerializersModule {
subclass(AssignmentExpr::class)
subclass(BlanksRefExpr::class)
subclass(CharExpr::class)
subclass(CheckExpr::class)
subclass(DataRefExpr::class)
subclass(DecExpr::class)
subclass(DiffExpr::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal fun RpgParser.BifContext.toAst(conf: ToAstConfiguration = ToAstConfigur
this.bif_lookup() != null -> this.bif_lookup().toAst(conf)
this.bif_xlate() != null -> this.bif_xlate().toAst(conf)
this.bif_scan() != null -> this.bif_scan().toAst(conf)
this.bif_check() != null -> this.bif_check().toAst(conf)
this.bif_trim() != null -> this.bif_trim().toAst(conf)
this.bif_trimr() != null -> this.bif_trimr().toAst(conf)
this.bif_triml() != null -> this.bif_triml().toAst(conf)
Expand Down Expand Up @@ -213,6 +214,15 @@ internal fun RpgParser.Bif_scanContext.toAst(conf: ToAstConfiguration = ToAstCon
)
}

internal fun RpgParser.Bif_checkContext.toAst(conf: ToAstConfiguration = ToAstConfiguration()): CheckExpr {
return CheckExpr(
value = this.comparator.toAst(conf),
source = this.base.toAst(conf),
start = this.start?.toAst(conf),
toPosition(conf.considerPosition)
)
}

internal fun RpgParser.Bif_xlateContext.toAst(conf: ToAstConfiguration = ToAstConfiguration()): TranslateExpr {
return TranslateExpr(
this.from.toAst(conf),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2222,4 +2222,9 @@ Test 6
val expected = listOf("0", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "1")
assertEquals(expected, "INDIC02".outputOf())
}

@Test
fun executeBIFCHECK() {
assertEquals(listOf("ok"), outputOf("BIFCHECK"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,42 @@ open class SmeupInterpreterTest : AbstractTest() {
"CALL(MULANGTC30, 7 , 0)")
assertEquals(expected, "smeup/T10_A60_P04-P07".outputOf())
}

@Test
fun executeT04_A15_P01() {
val expected = listOf<String>(
"P01_01(8)",
"P01_02(13)",
"P01_03(16)",
"P01_04(2)",
"P01_05(1)",
"P01_06(1)",
"P01_07(0)",
"P01_08(2)",
"P01_09(2)",
"P01_10(1)",
"P01_11(2)",
"P01_12(0)"
)
assertEquals(expected, "smeup/T04_A15_P01".outputOf())
}

@Test
fun executeT04_A15_P02() {
val expected = listOf<String>(
"P02_01(10)",
"P02_02(6)"
)
assertEquals(expected, "smeup/T04_A15_P02".outputOf())
}

@Test
fun executeT04_A15_P03() {
val expected = listOf<String>(
"P03_01(0)",
"P03_02(1)",
"P03_03(ok)"
)
assertEquals(expected, "smeup/T04_A15_P03".outputOf())
}
}
15 changes: 15 additions & 0 deletions rpgJavaInterpreter-core/src/test/resources/BIFCHECK.rpgle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
D name S 30A
D name2 S 4A
D pos S 5U 0
D £DBG_Str S 30
D £DBG_Pas S 3
D alphabet C 'ABCDEFGHIJKLMNOPQRSTUVWXYZ +
D abcdefghijklmnopqrstuvwxyz +
D 0123456789+/'
*
C IF %CHECK(alphabet: 'Antonio Cosentino 123')= 0
C EVAL £DBG_Str='ok'
C ELSE
C EVAL £DBG_Str='ko'
C ENDIF
C £DBG_Str DSPLY
68 changes: 68 additions & 0 deletions rpgJavaInterpreter-core/src/test/resources/smeup/T04_A15_P01.rpgle
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
D name S 30A
D name2 S 4A
D pos S 5U 0
D £DBG_Str S 30
D £DBG_Pas S 3
C EVAL £DBG_Pas='P01'
*
C EVAL name = ' Amit Jaiswal'
C EVAL pos = %check(' ' : name)
C EVAL £DBG_Str='P01_01('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name = ' Amit Jaiswal'
C EVAL pos = %check(' Atim' : name)
C EVAL £DBG_Str='P01_02('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name = ' Amit Jaiswal'
C EVAL pos = %check(' Amit Jai' : name)
C EVAL £DBG_Str='P01_03('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name = 'Amit Jaiswal'
C EVAL pos = %check('A' : name)
C EVAL £DBG_Str='P01_04('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name = 'Amit Jaiswal'
C EVAL pos = %check('a' : name)
C EVAL £DBG_Str='P01_05('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name = 'Amit Jaiswal'
C EVAL pos = %check('ab' : name)
C EVAL £DBG_Str='P01_06('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name2 = 'abab'
C EVAL pos = %check('ab' : name2)
C EVAL £DBG_Str='P01_07('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name2 = 'abab'
C EVAL pos = %check('ac' : name2)
C EVAL £DBG_Str='P01_08('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name2 = 'abab'
C EVAL pos = %check('acd' : name2)
C EVAL £DBG_Str='P01_09('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name2 = 'abab'
C EVAL pos = %check('cd' : name2)
C EVAL £DBG_Str='P01_10('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name2 = 'abab'
C EVAL pos = %check('ca' : name2)
C EVAL £DBG_Str='P01_11('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name2 = 'abab'
C EVAL pos = %check('cab' : name2)
C EVAL £DBG_Str='P01_12('+%char(pos)+')'
C £DBG_Str DSPLY
*
C SETON LR
18 changes: 18 additions & 0 deletions rpgJavaInterpreter-core/src/test/resources/smeup/T04_A15_P02.rpgle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
D name S 30A
D pos S 5U 0
D £DBG_Str S 30
D £DBG_Pas S 3
*
C EVAL £DBG_Pas='P02'
*
C EVAL name = 'Amit Jaiswal'
C EVAL pos = %check('Jais' : name:6)
C EVAL £DBG_Str='P02_01('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name = 'Amit Jaiswal'
C EVAL pos = %check('A' : name:6)
C EVAL £DBG_Str='P02_02('+%char(pos)+')'
C £DBG_Str DSPLY
*
C SETON LR
28 changes: 28 additions & 0 deletions rpgJavaInterpreter-core/src/test/resources/smeup/T04_A15_P03.rpgle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
D name S 30A
D pos S 5U 0
D £DBG_Str S 30
D £DBG_Pas S 3
D alphabet C 'ABCDEFGHIJKLMNOPQRSTUVWXYZ +
D abcdefghijklmnopqrstuvwxyz +
D 0123456789+/'
*
C EVAL £DBG_Pas='P03'
*
C EVAL name = 'Amit Jaiswal'
C EVAL pos = %check(alphabet: name)
C EVAL £DBG_Str='P03_01('+%char(pos)+')'
C £DBG_Str DSPLY
*
C EVAL name = '££ t abc1234'
C EVAL pos = %check(alphabet: name)
C EVAL £DBG_Str='P03_02('+%char(pos)+')'
C £DBG_Str DSPLY
*
C IF %CHECK(alphabet: 'Antonio Cosentino')= 0
C EVAL £DBG_Str='P03_03(ok)'
C ELSE
C EVAL £DBG_Str='P03_03(ko)'
C ENDIF
C £DBG_Str DSPLY
*
C SETON LR

0 comments on commit 5d1b97a

Please sign in to comment.