2021-06-28 13:46:05 +02:00
|
|
|
package prog8tests
|
|
|
|
|
2021-11-08 15:50:29 +01:00
|
|
|
import io.kotest.assertions.fail
|
2021-11-08 00:16:58 +01: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 13:46:05 +02:00
|
|
|
import prog8.ast.IFunctionCall
|
|
|
|
import prog8.ast.base.DataType
|
|
|
|
import prog8.ast.base.VarDeclType
|
|
|
|
import prog8.ast.expressions.IdentifierReference
|
|
|
|
import prog8.ast.expressions.NumericLiteralValue
|
2021-12-28 14:23:36 +01:00
|
|
|
import prog8.codegen.target.Cx16Target
|
2021-10-11 00:22:04 +02:00
|
|
|
import prog8tests.helpers.assertSuccess
|
|
|
|
import prog8tests.helpers.compileText
|
2021-06-28 13:46:05 +02:00
|
|
|
|
|
|
|
|
2021-06-28 18:49:01 +02: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-08 00:16:58 +01:00
|
|
|
class TestCompilerOnCharLit: FunSpec({
|
2021-06-28 13:46:05 +02:00
|
|
|
|
2021-11-08 00:16:58 +01:00
|
|
|
test("testCharLitAsRomsubArg") {
|
2021-07-11 18:18:27 +02:00
|
|
|
val platform = Cx16Target
|
2021-07-13 09:47:31 +02:00
|
|
|
val result = compileText(platform, false, """
|
|
|
|
main {
|
|
|
|
romsub ${"$"}FFD2 = chrout(ubyte ch @ A)
|
|
|
|
sub start() {
|
|
|
|
chrout('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
2021-06-28 13:46:05 +02:00
|
|
|
|
2021-10-30 00:25:34 +02:00
|
|
|
val program = result.program
|
2021-10-11 00:01:26 +02:00
|
|
|
val startSub = program.entrypoint
|
2021-06-28 13:46:05 +02:00
|
|
|
val funCall = startSub.statements.filterIsInstance<IFunctionCall>()[0]
|
|
|
|
|
2021-11-08 00:16:58 +01:00
|
|
|
withClue("char literal should have been replaced by ubyte literal") {
|
|
|
|
funCall.args[0] shouldBe instanceOf<NumericLiteralValue>()
|
|
|
|
}
|
2021-06-28 13:46:05 +02:00
|
|
|
val arg = funCall.args[0] as NumericLiteralValue
|
2021-11-08 00:16:58 +01:00
|
|
|
arg.type shouldBe DataType.UBYTE
|
2021-11-21 00:07:17 +01:00
|
|
|
arg.number shouldBe platform.encodeString("\n", false)[0].toDouble()
|
2021-06-28 13:46:05 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:16:58 +01:00
|
|
|
test("testCharVarAsRomsubArg") {
|
2021-07-11 18:18:27 +02:00
|
|
|
val platform = Cx16Target
|
2021-07-13 09:47:31 +02:00
|
|
|
val result = compileText(platform, false, """
|
|
|
|
main {
|
|
|
|
romsub ${"$"}FFD2 = chrout(ubyte ch @ A)
|
|
|
|
sub start() {
|
|
|
|
ubyte ch = '\n'
|
|
|
|
chrout(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
2021-07-11 18:18:27 +02:00
|
|
|
|
2021-10-30 00:25:34 +02:00
|
|
|
val program = result.program
|
2021-10-11 00:01:26 +02:00
|
|
|
val startSub = program.entrypoint
|
2021-06-28 13:46:05 +02:00
|
|
|
val funCall = startSub.statements.filterIsInstance<IFunctionCall>()[0]
|
|
|
|
|
2021-11-08 00:16:58 +01:00
|
|
|
funCall.args[0] shouldBe instanceOf<IdentifierReference>()
|
2021-06-28 13:46:05 +02:00
|
|
|
val arg = funCall.args[0] as IdentifierReference
|
|
|
|
val decl = arg.targetVarDecl(program)!!
|
2021-11-08 00:16:58 +01:00
|
|
|
decl.type shouldBe VarDeclType.VAR
|
|
|
|
decl.datatype shouldBe DataType.UBYTE
|
2021-06-28 13:46:05 +02:00
|
|
|
|
|
|
|
// TODO: assertIs<CharLiteral>(decl.value,
|
|
|
|
// "char literals should be kept until code gen")
|
|
|
|
// val initializerValue = decl.value as CharLiteral
|
|
|
|
// assertEquals('\n', (initializerValue as CharLiteral).value)
|
|
|
|
|
2021-11-08 00:16:58 +01:00
|
|
|
withClue("initializer value should have been moved to separate assignment"){
|
|
|
|
decl.value shouldBe null
|
|
|
|
}
|
2022-01-11 00:34:44 +01:00
|
|
|
val assignInitialValue = decl.findInitializer(program)!!
|
2021-11-08 00:16:58 +01:00
|
|
|
assignInitialValue.target.identifier!!.nameInSource shouldBe listOf("ch")
|
|
|
|
withClue("char literal should have been replaced by ubyte literal") {
|
|
|
|
assignInitialValue.value shouldBe instanceOf<NumericLiteralValue>()
|
|
|
|
}
|
2021-11-01 00:24:15 +01:00
|
|
|
val initializerValue = assignInitialValue.value as NumericLiteralValue
|
2021-11-08 00:16:58 +01:00
|
|
|
initializerValue.type shouldBe DataType.UBYTE
|
2021-11-21 00:07:17 +01:00
|
|
|
initializerValue.number shouldBe platform.encodeString("\n", false)[0].toDouble()
|
2021-06-28 13:46:05 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:16:58 +01:00
|
|
|
test("testCharConstAsRomsubArg") {
|
2021-07-11 18:18:27 +02:00
|
|
|
val platform = Cx16Target
|
2021-07-13 09:47:31 +02:00
|
|
|
val result = compileText(platform, false, """
|
|
|
|
main {
|
|
|
|
romsub ${"$"}FFD2 = chrout(ubyte ch @ A)
|
|
|
|
sub start() {
|
|
|
|
const ubyte ch = '\n'
|
|
|
|
chrout(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
2021-07-11 18:18:27 +02:00
|
|
|
|
2021-10-30 00:25:34 +02:00
|
|
|
val program = result.program
|
2021-10-11 00:01:26 +02:00
|
|
|
val startSub = program.entrypoint
|
2021-06-28 13:46:05 +02: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-08 00:16:58 +01:00
|
|
|
decl.type shouldBe VarDeclType.CONST
|
|
|
|
decl.datatype shouldBe DataType.UBYTE
|
2021-11-16 23:52:30 +01:00
|
|
|
(decl.value as NumericLiteralValue).number shouldBe platform.encodeString("\n", false)[0]
|
2021-06-28 13:46:05 +02:00
|
|
|
}
|
|
|
|
is NumericLiteralValue -> {
|
2021-11-21 00:07:17 +01:00
|
|
|
arg.number shouldBe platform.encodeString("\n", false)[0].toDouble()
|
2021-06-28 13:46:05 +02:00
|
|
|
}
|
2021-11-08 00:16:58 +01:00
|
|
|
else -> fail("invalid arg type") // funCall.args[0] shouldBe instanceOf<IdentifierReference>() // make test fail
|
2021-06-28 13:46:05 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-13 09:47:31 +02:00
|
|
|
|
2021-11-08 00:16:58 +01:00
|
|
|
})
|