From 260bcd3a55e08deae07336237b17cd49befc81ea Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 6 Dec 2020 17:02:56 +0100 Subject: [PATCH] added syntax error for non-constant array size declaration --- .../src/prog8/ast/processing/AstChecker.kt | 35 +++++++++++-------- examples/test.p8 | 6 ++++ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/compiler/src/prog8/ast/processing/AstChecker.kt b/compiler/src/prog8/ast/processing/AstChecker.kt index bd82085c7..be9a7f947 100644 --- a/compiler/src/prog8/ast/processing/AstChecker.kt +++ b/compiler/src/prog8/ast/processing/AstChecker.kt @@ -604,23 +604,28 @@ internal class AstChecker(private val program: Program, } } - // array length limits + // array length limits and constant lenghts if(decl.isArray) { - val length = decl.arraysize!!.constIndex() ?: 1 - when (decl.datatype) { - DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B -> { - if(length==0 || length>256) - err("string and byte array length must be 1-256") + val length = decl.arraysize!!.constIndex() + if(length==null) + err("array length must be a constant") + else { + when (decl.datatype) { + DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B -> { + if (length == 0 || length > 256) + err("string and byte array length must be 1-256") + } + DataType.ARRAY_UW, DataType.ARRAY_W -> { + if (length == 0 || length > 128) + err("word array length must be 1-128") + } + DataType.ARRAY_F -> { + if (length == 0 || length > 51) + err("float array length must be 1-51") + } + else -> { + } } - DataType.ARRAY_UW, DataType.ARRAY_W -> { - if(length==0 || length>128) - err("word array length must be 1-128") - } - DataType.ARRAY_F -> { - if(length==0 || length>51) - err("float array length must be 1-51") - } - else -> {} } } diff --git a/examples/test.p8 b/examples/test.p8 index feb2dda6a..416ad08de 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,6 +5,12 @@ main { sub start() { + const ubyte size = 100 + + ubyte[size+10] bytes + txt.print("hello\n") + ubyte dummy = bytes[0] + } }