2024-09-08 14:03:57 +00:00
|
|
|
package prog8tests.compiler
|
2021-06-28 11:46:05 +00:00
|
|
|
|
2021-11-08 14:50:29 +00:00
|
|
|
import io.kotest.assertions.fail
|
2021-11-07 23:16:58 +00:00
|
|
|
import io.kotest.assertions.withClue
|
|
|
|
import io.kotest.core.spec.style.FunSpec
|
|
|
|
import io.kotest.matchers.shouldBe
|
|
|
|
import io.kotest.matchers.types.instanceOf
|
2021-06-28 11:46:05 +00:00
|
|
|
import prog8.ast.IFunctionCall
|
|
|
|
import prog8.ast.expressions.IdentifierReference
|
2022-02-10 23:21:40 +00:00
|
|
|
import prog8.ast.expressions.NumericLiteral
|
2022-03-10 21:38:16 +00:00
|
|
|
import prog8.ast.statements.VarDeclType
|
2024-09-04 19:49:47 +00:00
|
|
|
import prog8.code.core.BaseDataType
|
2024-09-08 12:38:34 +00:00
|
|
|
import prog8.code.core.DataType
|
2022-03-10 21:38:16 +00:00
|
|
|
import prog8.code.core.Encoding
|
2022-03-11 19:35:25 +00:00
|
|
|
import prog8.code.target.Cx16Target
|
2021-10-10 22:22:04 +00:00
|
|
|
import prog8tests.helpers.compileText
|
2021-06-28 11:46:05 +00:00
|
|
|
|
|
|
|
|
2021-06-28 16:49:01 +00:00
|
|
|
/**
|
|
|
|
* ATTENTION: this is just kludge!
|
|
|
|
* They are not really unit tests, but rather tests of the whole process,
|
|
|
|
* from source file loading all the way through to running 64tass.
|
|
|
|
*/
|
2021-11-07 23:16:58 +00:00
|
|
|
class TestCompilerOnCharLit: FunSpec({
|
2021-06-28 11:46:05 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testCharLitAsRomsubArg") {
|
2022-02-06 20:29:06 +00:00
|
|
|
val platform = Cx16Target()
|
2021-07-13 07:47:31 +00:00
|
|
|
val result = compileText(platform, false, """
|
|
|
|
main {
|
|
|
|
romsub ${"$"}FFD2 = chrout(ubyte ch @ A)
|
|
|
|
sub start() {
|
|
|
|
chrout('\n')
|
|
|
|
}
|
|
|
|
}
|
2022-03-07 20:41:12 +00:00
|
|
|
""")!!
|
2021-06-28 11:46:05 +00:00
|
|
|
|
2023-02-09 00:46:23 +00:00
|
|
|
val program = result.compilerAst
|
2021-10-10 22:01:26 +00:00
|
|
|
val startSub = program.entrypoint
|
2021-06-28 11:46:05 +00:00
|
|
|
val funCall = startSub.statements.filterIsInstance<IFunctionCall>()[0]
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
withClue("char literal should have been replaced by ubyte literal") {
|
2022-02-10 23:21:40 +00:00
|
|
|
funCall.args[0] shouldBe instanceOf<NumericLiteral>()
|
2021-11-07 23:16:58 +00:00
|
|
|
}
|
2022-02-10 23:21:40 +00:00
|
|
|
val arg = funCall.args[0] as NumericLiteral
|
2024-09-04 19:49:47 +00:00
|
|
|
arg.type shouldBe BaseDataType.UBYTE
|
2022-01-18 20:21:49 +00:00
|
|
|
arg.number shouldBe platform.encodeString("\n", Encoding.PETSCII)[0].toDouble()
|
2021-06-28 11:46:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testCharVarAsRomsubArg") {
|
2022-02-06 20:29:06 +00:00
|
|
|
val platform = Cx16Target()
|
2021-07-13 07:47:31 +00:00
|
|
|
val result = compileText(platform, false, """
|
|
|
|
main {
|
|
|
|
romsub ${"$"}FFD2 = chrout(ubyte ch @ A)
|
|
|
|
sub start() {
|
|
|
|
ubyte ch = '\n'
|
|
|
|
chrout(ch)
|
|
|
|
}
|
|
|
|
}
|
2022-03-07 20:41:12 +00:00
|
|
|
""")!!
|
2021-07-11 16:18:27 +00:00
|
|
|
|
2023-02-09 00:46:23 +00:00
|
|
|
val program = result.compilerAst
|
2021-10-10 22:01:26 +00:00
|
|
|
val startSub = program.entrypoint
|
2021-06-28 11:46:05 +00:00
|
|
|
val funCall = startSub.statements.filterIsInstance<IFunctionCall>()[0]
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
funCall.args[0] shouldBe instanceOf<IdentifierReference>()
|
2021-06-28 11:46:05 +00:00
|
|
|
val arg = funCall.args[0] as IdentifierReference
|
|
|
|
val decl = arg.targetVarDecl(program)!!
|
2021-11-07 23:16:58 +00:00
|
|
|
decl.type shouldBe VarDeclType.VAR
|
2024-09-08 12:38:34 +00:00
|
|
|
decl.datatype shouldBe DataType.forDt(BaseDataType.UBYTE)
|
2021-06-28 11:46:05 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
withClue("initializer value should have been moved to separate assignment"){
|
|
|
|
decl.value shouldBe null
|
|
|
|
}
|
2022-01-10 23:34:44 +00:00
|
|
|
val assignInitialValue = decl.findInitializer(program)!!
|
2021-11-07 23:16:58 +00:00
|
|
|
assignInitialValue.target.identifier!!.nameInSource shouldBe listOf("ch")
|
|
|
|
withClue("char literal should have been replaced by ubyte literal") {
|
2022-02-10 23:21:40 +00:00
|
|
|
assignInitialValue.value shouldBe instanceOf<NumericLiteral>()
|
2021-11-07 23:16:58 +00:00
|
|
|
}
|
2022-02-10 23:21:40 +00:00
|
|
|
val initializerValue = assignInitialValue.value as NumericLiteral
|
2024-09-04 19:49:47 +00:00
|
|
|
initializerValue.type shouldBe BaseDataType.UBYTE
|
2022-01-18 20:21:49 +00:00
|
|
|
initializerValue.number shouldBe platform.encodeString("\n", Encoding.PETSCII)[0].toDouble()
|
2021-06-28 11:46:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testCharConstAsRomsubArg") {
|
2022-02-06 20:29:06 +00:00
|
|
|
val platform = Cx16Target()
|
2021-07-13 07:47:31 +00:00
|
|
|
val result = compileText(platform, false, """
|
|
|
|
main {
|
|
|
|
romsub ${"$"}FFD2 = chrout(ubyte ch @ A)
|
|
|
|
sub start() {
|
|
|
|
const ubyte ch = '\n'
|
|
|
|
chrout(ch)
|
|
|
|
}
|
|
|
|
}
|
2022-03-07 20:41:12 +00:00
|
|
|
""")!!
|
2021-07-11 16:18:27 +00:00
|
|
|
|
2023-02-09 00:46:23 +00:00
|
|
|
val program = result.compilerAst
|
2021-10-10 22:01:26 +00:00
|
|
|
val startSub = program.entrypoint
|
2021-06-28 11:46:05 +00:00
|
|
|
val funCall = startSub.statements.filterIsInstance<IFunctionCall>()[0]
|
|
|
|
|
|
|
|
// Now, both is ok for the arg: a) still the IdRef or b) replaced by numeric literal
|
|
|
|
when (val arg = funCall.args[0]) {
|
|
|
|
is IdentifierReference -> {
|
|
|
|
val decl = arg.targetVarDecl(program)!!
|
2021-11-07 23:16:58 +00:00
|
|
|
decl.type shouldBe VarDeclType.CONST
|
2024-09-08 12:38:34 +00:00
|
|
|
decl.datatype shouldBe DataType.forDt(BaseDataType.UBYTE)
|
2022-02-10 23:21:40 +00:00
|
|
|
(decl.value as NumericLiteral).number shouldBe platform.encodeString("\n", Encoding.PETSCII)[0]
|
2021-06-28 11:46:05 +00:00
|
|
|
}
|
2022-02-10 23:21:40 +00:00
|
|
|
is NumericLiteral -> {
|
2022-01-18 20:21:49 +00:00
|
|
|
arg.number shouldBe platform.encodeString("\n", Encoding.PETSCII)[0].toDouble()
|
2021-06-28 11:46:05 +00:00
|
|
|
}
|
2021-11-07 23:16:58 +00:00
|
|
|
else -> fail("invalid arg type") // funCall.args[0] shouldBe instanceOf<IdentifierReference>() // make test fail
|
2021-06-28 11:46:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-13 07:47:31 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
})
|