fix array type checking crash when attempting to use str literal to initialize a byte array.

Fixes #34
This commit is contained in:
Irmen de Jong 2021-05-07 00:04:29 +02:00
parent 8736da1a21
commit dae59238cd
3 changed files with 21 additions and 11 deletions

View File

@ -563,8 +563,9 @@ internal class AstChecker(private val program: Program,
}
}
VarDeclType.MEMORY -> {
if(decl.arraysize!=null) {
val arraySize = decl.arraysize!!.constIndex() ?: 1
val arraysize = decl.arraysize
if(arraysize!=null) {
val arraySize = arraysize.constIndex() ?: 1
when(decl.datatype) {
DataType.ARRAY_B, DataType.ARRAY_UB ->
if(arraySize > 256)
@ -578,10 +579,9 @@ internal class AstChecker(private val program: Program,
else -> {}
}
}
if(decl.value is NumericLiteralValue) {
val value = decl.value as NumericLiteralValue
if (value.type !in IntegerDatatypes || value.number.toInt() < 0 || value.number.toInt() > 65535) {
val numvalue = decl.value as? NumericLiteralValue
if(numvalue!=null) {
if (numvalue.type !in IntegerDatatypes || numvalue.number.toInt() < 0 || numvalue.number.toInt() > 65535) {
err("memory address must be valid integer 0..\$ffff", decl.value?.position)
}
} else {
@ -599,9 +599,9 @@ internal class AstChecker(private val program: Program,
// array length limits and constant lenghts
if(decl.isArray) {
val length = decl.arraysize!!.constIndex()
val length = decl.arraysize?.constIndex()
if(length==null)
err("array length must be a constant")
err("array length must be known at compile-time")
else {
when (decl.datatype) {
DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B -> {

View File

@ -2,8 +2,10 @@
TODO
====
- mark vardecls 'shared' to indicate they should not be optimized away because they're shared with assembly code??
- github issue: make strings no longer immutable? fixes weird optimization issue. Deduplication via command line switch?
- possible idea: option to mark vardecls 'shared' to indicate they should not be optimized away because they're shared with assembly code?
However: who even needs variables declared in prog8 code that are only used by assembly???
- github issue: make strings no longer immutable? Deduplication selectable via command line switch?
- test all examples before release of the new version
- simplify cx16.joystick_get2() once this cx16 rom issue is resolved: https://github.com/commanderx16/x16-rom/issues/203

View File

@ -3,7 +3,15 @@
%option no_sysinit
main {
sub start() {
byte[] xs1 = "foo1" ; <<<<<<<<<<<<
str xs2 = "foo2" ; <<<<<<<<<<<<
sub start() {
txt.print(xs1)
stringopt()
}
sub stringopt() {
str message = "a"
txt.print(message)