2021-07-13 07:47:31 +00:00
|
|
|
package prog8tests
|
|
|
|
|
2021-11-08 14:50:29 +00:00
|
|
|
import io.kotest.assertions.withClue
|
2021-11-07 23:16:58 +00:00
|
|
|
import io.kotest.core.spec.style.FunSpec
|
2021-11-08 14:50:29 +00:00
|
|
|
import io.kotest.matchers.shouldBe
|
|
|
|
import io.kotest.matchers.string.shouldContain
|
2021-11-29 22:36:41 +00:00
|
|
|
import io.kotest.matchers.types.instanceOf
|
2021-07-13 07:47:31 +00:00
|
|
|
import prog8.ast.base.DataType
|
2021-10-29 21:46:51 +00:00
|
|
|
import prog8.ast.base.Position
|
2021-07-13 07:47:31 +00:00
|
|
|
import prog8.ast.expressions.*
|
|
|
|
import prog8.ast.statements.ForLoop
|
|
|
|
import prog8.ast.statements.VarDecl
|
|
|
|
import prog8.compiler.target.C64Target
|
|
|
|
import prog8.compiler.target.Cx16Target
|
2021-10-29 03:28:02 +00:00
|
|
|
import prog8.compilerinterface.size
|
2021-10-29 00:42:10 +00:00
|
|
|
import prog8.compilerinterface.toConstantIntegerRange
|
2021-12-04 17:20:22 +00:00
|
|
|
import prog8tests.helpers.*
|
2021-10-29 03:28:02 +00:00
|
|
|
import prog8tests.helpers.ErrorReporterForTests
|
|
|
|
import prog8tests.helpers.assertFailure
|
|
|
|
import prog8tests.helpers.assertSuccess
|
|
|
|
import prog8tests.helpers.compileText
|
2021-07-13 07:47:31 +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 TestCompilerOnRanges: FunSpec({
|
2021-07-13 07:47:31 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testUByteArrayInitializerWithRange_char_to_char") {
|
2021-07-13 07:47:31 +00:00
|
|
|
val platform = Cx16Target
|
2021-11-11 02:03:21 +00:00
|
|
|
val result = compileText(platform, false, """
|
2021-07-13 07:47:31 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte[] cs = @'a' to 'z' ; values are computed at compile time
|
|
|
|
cs[0] = 23 ; keep optimizer from removing it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
|
|
|
|
2021-10-29 22:25:34 +00:00
|
|
|
val program = result.program
|
2021-10-10 22:01:26 +00:00
|
|
|
val startSub = program.entrypoint
|
2021-07-13 07:47:31 +00:00
|
|
|
val decl = startSub
|
|
|
|
.statements.filterIsInstance<VarDecl>()[0]
|
|
|
|
val rhsValues = (decl.value as ArrayLiteralValue)
|
|
|
|
.value // Array<Expression>
|
|
|
|
.map { (it as NumericLiteralValue).number.toInt() }
|
|
|
|
val expectedStart = platform.encodeString("a", true)[0].toInt()
|
|
|
|
val expectedEnd = platform.encodeString("z", false)[0].toInt()
|
|
|
|
val expectedStr = "$expectedStart .. $expectedEnd"
|
|
|
|
|
|
|
|
val actualStr = "${rhsValues.first()} .. ${rhsValues.last()}"
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue(".first .. .last") {
|
|
|
|
actualStr shouldBe expectedStr
|
|
|
|
}
|
|
|
|
withClue("rangeExpr.size()") {
|
|
|
|
(rhsValues.last() - rhsValues.first() + 1) shouldBe (expectedEnd - expectedStart + 1)
|
|
|
|
}
|
2021-07-13 07:47:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testFloatArrayInitializerWithRange_char_to_char") {
|
2021-07-13 07:47:31 +00:00
|
|
|
val platform = C64Target
|
|
|
|
val result = compileText(platform, optimize = false, """
|
|
|
|
%option enable_floats
|
|
|
|
main {
|
|
|
|
sub start() {
|
2021-09-12 16:59:53 +00:00
|
|
|
float[] cs = 'a' to 'z' ; values are computed at compile time
|
2021-07-13 07:47:31 +00:00
|
|
|
cs[0] = 23 ; keep optimizer from removing it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
|
|
|
|
2021-10-29 22:25:34 +00:00
|
|
|
val program = result.program
|
2021-10-10 22:01:26 +00:00
|
|
|
val startSub = program.entrypoint
|
2021-07-13 07:47:31 +00:00
|
|
|
val decl = startSub
|
|
|
|
.statements.filterIsInstance<VarDecl>()[0]
|
|
|
|
val rhsValues = (decl.value as ArrayLiteralValue)
|
|
|
|
.value // Array<Expression>
|
|
|
|
.map { (it as NumericLiteralValue).number.toInt() }
|
2021-09-12 16:59:53 +00:00
|
|
|
val expectedStart = platform.encodeString("a", false)[0].toInt()
|
2021-07-13 07:47:31 +00:00
|
|
|
val expectedEnd = platform.encodeString("z", false)[0].toInt()
|
|
|
|
val expectedStr = "$expectedStart .. $expectedEnd"
|
|
|
|
|
|
|
|
val actualStr = "${rhsValues.first()} .. ${rhsValues.last()}"
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue(".first .. .last") {
|
|
|
|
actualStr shouldBe expectedStr
|
|
|
|
}
|
|
|
|
withClue("rangeExpr.size()") {
|
|
|
|
rhsValues.size shouldBe (expectedEnd - expectedStart + 1)
|
|
|
|
}
|
2021-07-13 07:47:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
context("floatArrayInitializerWithRange") {
|
|
|
|
val combos = cartesianProduct(
|
|
|
|
listOf("", "42", "41"), // sizeInDecl
|
|
|
|
listOf("%option enable_floats", ""), // optEnableFloats
|
|
|
|
listOf(Cx16Target, C64Target), // platform
|
|
|
|
listOf(false, true) // optimize
|
|
|
|
)
|
2021-07-13 07:47:31 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
combos.forEach {
|
|
|
|
val (sizeInDecl, optEnableFloats, platform, optimize) = it
|
2021-07-13 07:47:31 +00:00
|
|
|
val displayName =
|
|
|
|
when (sizeInDecl) {
|
|
|
|
"" -> "no"
|
|
|
|
"42" -> "correct"
|
|
|
|
else -> "wrong"
|
|
|
|
} + " array size given" +
|
2021-11-07 23:16:58 +00:00
|
|
|
", " + (if (optEnableFloats == "") "without" else "with") + " %option enable_floats" +
|
|
|
|
", ${platform.name}, optimize: $optimize"
|
|
|
|
|
|
|
|
test(displayName) {
|
2021-07-13 07:47:31 +00:00
|
|
|
val result = compileText(platform, optimize, """
|
|
|
|
$optEnableFloats
|
|
|
|
main {
|
|
|
|
sub start() {
|
2021-11-07 23:16:58 +00:00
|
|
|
float[$sizeInDecl] cs = 1 to 42 ; values are computed at compile time
|
2021-07-13 07:47:31 +00:00
|
|
|
cs[0] = 23 ; keep optimizer from removing it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""")
|
2021-09-12 16:53:12 +00:00
|
|
|
if (optEnableFloats != "" && (sizeInDecl=="" || sizeInDecl=="42"))
|
2021-07-13 07:47:31 +00:00
|
|
|
result.assertSuccess()
|
2021-09-12 16:53:12 +00:00
|
|
|
else
|
2021-07-13 07:47:31 +00:00
|
|
|
result.assertFailure()
|
2021-11-07 23:16:58 +00:00
|
|
|
|
2021-07-13 07:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-07 23:16:58 +00:00
|
|
|
}
|
2021-07-13 07:47:31 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testForLoopWithRange_char_to_char") {
|
2021-07-13 07:47:31 +00:00
|
|
|
val platform = Cx16Target
|
2021-07-17 18:45:17 +00:00
|
|
|
val result = compileText(platform, optimize = true, """
|
2021-07-13 07:47:31 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte i
|
|
|
|
for i in @'a' to 'f' {
|
|
|
|
i += i ; keep optimizer from removing it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
|
|
|
|
2021-10-29 22:25:34 +00:00
|
|
|
val program = result.program
|
2021-10-10 22:01:26 +00:00
|
|
|
val startSub = program.entrypoint
|
2021-07-13 07:47:31 +00:00
|
|
|
val iterable = startSub
|
|
|
|
.statements.filterIsInstance<ForLoop>()
|
|
|
|
.map { it.iterable }[0]
|
|
|
|
val rangeExpr = iterable as RangeExpr
|
|
|
|
|
|
|
|
val expectedStart = platform.encodeString("a", true)[0].toInt()
|
|
|
|
val expectedEnd = platform.encodeString("f", false)[0].toInt()
|
|
|
|
val expectedStr = "$expectedStart .. $expectedEnd"
|
|
|
|
|
2021-10-29 14:20:53 +00:00
|
|
|
val intProgression = rangeExpr.toConstantIntegerRange()
|
2021-07-13 07:47:31 +00:00
|
|
|
val actualStr = "${intProgression?.first} .. ${intProgression?.last}"
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue(".first .. .last") {
|
|
|
|
actualStr shouldBe expectedStr
|
|
|
|
}
|
|
|
|
withClue("rangeExpr.size()") {
|
|
|
|
rangeExpr.size() shouldBe (expectedEnd - expectedStart + 1)
|
|
|
|
}
|
2021-07-13 07:47:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testForLoopWithRange_bool_to_bool") {
|
2021-07-17 18:45:17 +00:00
|
|
|
val platform = Cx16Target
|
|
|
|
val result = compileText(platform, optimize = true, """
|
2021-07-13 07:47:31 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte i
|
|
|
|
for i in false to true {
|
|
|
|
i += i ; keep optimizer from removing it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
|
|
|
|
2021-10-29 22:25:34 +00:00
|
|
|
val program = result.program
|
2021-10-10 22:01:26 +00:00
|
|
|
val startSub = program.entrypoint
|
2021-07-13 07:47:31 +00:00
|
|
|
val rangeExpr = startSub
|
|
|
|
.statements.filterIsInstance<ForLoop>()
|
|
|
|
.map { it.iterable }
|
|
|
|
.filterIsInstance<RangeExpr>()[0]
|
|
|
|
|
2021-11-08 14:50:29 +00:00
|
|
|
rangeExpr.size() shouldBe 2
|
2021-10-29 14:20:53 +00:00
|
|
|
val intProgression = rangeExpr.toConstantIntegerRange()
|
2021-11-08 14:50:29 +00:00
|
|
|
intProgression?.first shouldBe 0
|
|
|
|
intProgression?.last shouldBe 1
|
2021-07-13 07:47:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testForLoopWithRange_ubyte_to_ubyte") {
|
2021-07-17 18:45:17 +00:00
|
|
|
val platform = Cx16Target
|
|
|
|
val result = compileText(platform, optimize = true, """
|
2021-07-13 07:47:31 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte i
|
|
|
|
for i in 1 to 9 {
|
|
|
|
i += i ; keep optimizer from removing it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
|
|
|
|
2021-10-29 22:25:34 +00:00
|
|
|
val program = result.program
|
2021-10-10 22:01:26 +00:00
|
|
|
val startSub = program.entrypoint
|
2021-07-13 07:47:31 +00:00
|
|
|
val rangeExpr = startSub
|
|
|
|
.statements.filterIsInstance<ForLoop>()
|
|
|
|
.map { it.iterable }
|
|
|
|
.filterIsInstance<RangeExpr>()[0]
|
|
|
|
|
2021-11-08 14:50:29 +00:00
|
|
|
rangeExpr.size() shouldBe 9
|
2021-10-29 14:20:53 +00:00
|
|
|
val intProgression = rangeExpr.toConstantIntegerRange()
|
2021-11-08 14:50:29 +00:00
|
|
|
intProgression?.first shouldBe 1
|
|
|
|
intProgression?.last shouldBe 9
|
2021-07-13 07:47:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testForLoopWithRange_str_downto_str") {
|
2021-10-21 23:25:26 +00:00
|
|
|
val errors = ErrorReporterForTests()
|
2021-07-31 12:44:02 +00:00
|
|
|
compileText(Cx16Target, true, """
|
2021-07-13 07:47:31 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte i
|
|
|
|
for i in "start" downto "end" {
|
|
|
|
i += i ; keep optimizer from removing it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 23:25:26 +00:00
|
|
|
""", errors, false).assertFailure()
|
2021-11-08 14:50:29 +00:00
|
|
|
errors.errors.size shouldBe 2
|
2021-12-04 15:46:26 +00:00
|
|
|
errors.errors[0] shouldContain ".p8:5:30) range expression from value must be integer"
|
|
|
|
errors.errors[1] shouldContain ".p8:5:45) range expression to value must be integer"
|
2021-07-13 07:47:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testForLoopWithIterable_str") {
|
2021-07-13 07:47:31 +00:00
|
|
|
val result = compileText(Cx16Target, false, """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte i
|
|
|
|
for i in "something" {
|
|
|
|
i += i ; keep optimizer from removing it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
|
|
|
|
2021-10-29 22:25:34 +00:00
|
|
|
val program = result.program
|
2021-10-10 22:01:26 +00:00
|
|
|
val startSub = program.entrypoint
|
2021-07-13 07:47:31 +00:00
|
|
|
val iterable = startSub
|
|
|
|
.statements.filterIsInstance<ForLoop>()
|
|
|
|
.map { it.iterable }
|
|
|
|
.filterIsInstance<IdentifierReference>()[0]
|
|
|
|
|
2021-11-08 14:50:29 +00:00
|
|
|
iterable.inferType(program).getOr(DataType.UNDEFINED) shouldBe DataType.STR
|
2021-07-13 07:47:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testRangeExprNumericSize") {
|
2021-10-29 21:46:51 +00:00
|
|
|
val expr = RangeExpr(
|
|
|
|
NumericLiteralValue.optimalInteger(10, Position.DUMMY),
|
|
|
|
NumericLiteralValue.optimalInteger(20, Position.DUMMY),
|
|
|
|
NumericLiteralValue.optimalInteger(2, Position.DUMMY),
|
|
|
|
Position.DUMMY)
|
2021-11-08 14:50:29 +00:00
|
|
|
expr.size() shouldBe 6
|
2021-10-29 21:46:51 +00:00
|
|
|
expr.toConstantIntegerRange()
|
|
|
|
}
|
2021-11-29 22:36:41 +00:00
|
|
|
|
|
|
|
test("range with negative step should be constvalue") {
|
|
|
|
val result = compileText(C64Target, false, """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte[] array = 100 to 50 step -2
|
|
|
|
ubyte xx
|
|
|
|
for xx in 100 to 50 step -2 {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
|
|
|
val statements = result.program.entrypoint.statements
|
|
|
|
val array = (statements[0] as VarDecl).value
|
|
|
|
array shouldBe instanceOf<ArrayLiteralValue>()
|
|
|
|
(array as ArrayLiteralValue).value.size shouldBe 26
|
|
|
|
val forloop = (statements.dropLast(1).last() as ForLoop)
|
|
|
|
forloop.iterable shouldBe instanceOf<RangeExpr>()
|
|
|
|
(forloop.iterable as RangeExpr).step shouldBe NumericLiteralValue(DataType.UBYTE, -2.0, Position.DUMMY)
|
|
|
|
}
|
|
|
|
|
|
|
|
test("range with start/end variables should be ok") {
|
|
|
|
val result = compileText(C64Target, false, """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
byte from = 100
|
|
|
|
byte end = 50
|
|
|
|
byte xx
|
|
|
|
for xx in from to end step -2 {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
|
|
|
val statements = result.program.entrypoint.statements
|
|
|
|
val forloop = (statements.dropLast(1).last() as ForLoop)
|
|
|
|
forloop.iterable shouldBe instanceOf<RangeExpr>()
|
|
|
|
(forloop.iterable as RangeExpr).step shouldBe NumericLiteralValue(DataType.UBYTE, -2.0, Position.DUMMY)
|
|
|
|
}
|
2021-11-07 23:16:58 +00:00
|
|
|
})
|