Skip to content

Commit

Permalink
Merge pull request #565 from smeup/bugfix/LS24003177/comptime-define-…
Browse files Browse the repository at this point in the history
…on-subroutine

Bugfix/ls24003177/comptime define on subroutine
  • Loading branch information
lanarimarco committed Jul 12, 2024
2 parents 85f87d6 + 03999c3 commit 8402a60
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package com.smeup.rpgparser.interpreter

import com.smeup.rpgparser.RpgParser
import com.smeup.rpgparser.RpgParser.Cspec_fixedContext
import com.smeup.rpgparser.RpgParser.Parm_fixedContext
import com.smeup.rpgparser.RpgParser.StatementContext
import com.smeup.rpgparser.RpgParser.*
import com.smeup.rpgparser.execution.MainExecutionContext
import com.smeup.rpgparser.parsing.ast.*
import com.smeup.rpgparser.parsing.facade.findAllDescendants
Expand Down Expand Up @@ -151,7 +149,7 @@ open class BaseCompileTimeInterpreter(
if (it.name.equals(declName, ignoreCase = true)) {
return it.elementSize()
}
val field = it.fields.find { it.name.equals(declName, ignoreCase = true) }
val field = it.fields.find { field -> field.name.equals(declName, ignoreCase = true) }
if (field != null) return (field.elementSize() /*/ field.declaredArrayInLine!!*/)
}

Expand All @@ -176,7 +174,11 @@ open class BaseCompileTimeInterpreter(
}
}
it.cspec_fixed() != null -> {
val size = it.cspec_fixed().findType(declName, conf)?.size
val size = it.cspec_fixed().findType(
declName = declName,
lookupIn = statements,
conf = conf
)?.size
if (size != null) return size
}
it.dcl_ds() != null -> {
Expand Down Expand Up @@ -281,7 +283,11 @@ open class BaseCompileTimeInterpreter(
}
}
it.cspec_fixed() != null -> {
val type = it.cspec_fixed().findType(declName, conf)
val type = it.cspec_fixed().findType(
declName = declName,
lookupIn = statements,
conf = conf
)
if (type != null) return type
}
it.block() != null -> {
Expand All @@ -304,11 +310,16 @@ open class BaseCompileTimeInterpreter(
}
}

private fun Cspec_fixedContext.findType(declName: String, conf: ToAstConfiguration): Type? {
private fun Cspec_fixedContext.findType(
declName: String,
lookupIn: List<StatementContext>? = null,
conf: ToAstConfiguration = ToAstConfiguration()
): Type? {
val targetStatementsPool = lookupIn ?: rContext().getUnwrappedStatements()
return when (val ast = this.toAst(conf)) {
is DefineStmt -> {
if (declName != ast.newVarName) return null
val type = findType(rContext().statement(), ast.originalName, conf)
if (!declName.equals(ast.newVarName, ignoreCase = true)) return null
val type = findType(targetStatementsPool, ast.originalName, conf)
type
}
is StatementThatCanDefineData -> {
Expand All @@ -333,8 +344,13 @@ open class BaseCompileTimeInterpreter(
procedureContext.subprocedurestatement().mapNotNull { it.statement() })
}
}
statements.addAll(this.statement() + this.subroutine().flatMap { it.statement() })
statements.addAll(this.getUnwrappedStatements())

return statements.toList()
}

/**
* Retrieve the list of all statements including those contained in subroutines
*/
private fun RContext.getUnwrappedStatements() = this.statement() + this.subroutine().flatMap { it.statement() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,14 @@ open class MULANGT02ConstAndDSpecTest : MULANGTTest() {
val expected = listOf("ok")
assertEquals(expected, "smeup/MUDRNRAPU00230".outputOf(configuration = smeupConfig))
}

/**
* Comptime DEFINE support based on a comptime resolution inside a subroutine
* @see #LS24003177
*/
@Test
fun executeMUDRNRAPU00231() {
val expected = listOf("ok")
assertEquals(expected, "smeup/MUDRNRAPU00231".outputOf(configuration = smeupConfig))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
D £DBG_Str S 2

D CSDS DS
D $OAVT1 LIKE(§OAVT1)
D $OAVP1 LIKE(§OAVP1)
D $OAVT2 LIKE(§OAVT2)
D $OAVP2 LIKE(§OAVP2)
D $OAVAT LIKE(§OAVAT)
D CSNR 5S 0

C *LIKE DEFINE £OAVT1 §OAVT1
C *LIKE DEFINE £OAVP1 §OAVP1
C *LIKE DEFINE £OAVT2 §OAVT2
C *LIKE DEFINE £OAVP2 §OAVP2
C *LIKE DEFINE £OAVAT §OAVAT

C £INIZI BEGSR
C *ENTRY PLIST
C PARM £OAVT1 2
C PARM £OAVP1 10
C PARM £OAVT2 2
C PARM £OAVP2 10
C PARM £OAVAT 15
C ENDSR

C EVAL £DBG_Str='ok'
C £DBG_Str DSPLY

0 comments on commit 8402a60

Please sign in to comment.