Skip to content

Commit

Permalink
fix array type checking crash when attempting to use str literal to i…
Browse files Browse the repository at this point in the history
…nitialize a byte array.

Fixes irmen#34
  • Loading branch information
irmen committed May 6, 2021
1 parent 8736da1 commit dae5923
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
16 changes: 8 additions & 8 deletions compiler/src/prog8/compiler/astprocessing/AstChecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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 -> {
Expand Down
6 changes: 4 additions & 2 deletions docs/source/todo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion examples/test.p8
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dae5923

Please sign in to comment.