From e93701f50ec9e8b2972ee02311f311535bdcf4a8 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 17 Apr 2021 15:49:41 +0200 Subject: [PATCH] fix compiler error when initializing var with memory(...) in block scope instead of subroutine --- .../astprocessing/StatementReorderer.kt | 19 +++++++++++-------- docs/source/todo.rst | 2 ++ examples/animals.p8 | 6 ++---- examples/test.p8 | 16 +++++----------- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt b/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt index cf69d28f4..82a9e6b77 100644 --- a/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt +++ b/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt @@ -198,15 +198,18 @@ internal class StatementReorderer(val program: Program, val errors: IErrorReport if(declValue!=null && decl.type== VarDeclType.VAR && decl.datatype in NumericDatatypes) { val declConstValue = declValue.constValue(program) if(declConstValue==null) { - // move the vardecl (without value) to the scope of the defining subroutine and put a regular assignment in its place here. - decl.value = null - decl.allowInitializeWithZero = false - val target = AssignTarget(IdentifierReference(listOf(decl.name), decl.position), null, null, decl.position) - val assign = Assignment(target, declValue, decl.position) - return listOf( + val subroutine = decl.definingSubroutine() as? INameScope + if(subroutine!=null) { + // move the vardecl (without value) to the scope of the defining subroutine and put a regular assignment in its place here. + decl.value = null + decl.allowInitializeWithZero = false + val target = AssignTarget(IdentifierReference(listOf(decl.name), decl.position), null, null, decl.position) + val assign = Assignment(target, declValue, decl.position) + return listOf( IAstModification.ReplaceNode(decl, assign, parent), - IAstModification.InsertFirst(decl, decl.definingSubroutine() as INameScope) - ) + IAstModification.InsertFirst(decl, subroutine) + ) + } } } return noModifications diff --git a/docs/source/todo.rst b/docs/source/todo.rst index c33f96647..a9ff5829d 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,6 +2,8 @@ TODO ==== +- allow labels in blocks instead of only in subroutines? + - hoist all variable declarations up to the subroutine scope *before* even the constant folding takes place (to avoid undefined symbol errors when referring to a variable from another nested scope in the subroutine) - c64: make the graphics.BITMAP_ADDRESS configurable (VIC banking) - get rid of all other TODO's in the code ;-) diff --git a/examples/animals.p8 b/examples/animals.p8 index c862d30c1..b269c3db3 100644 --- a/examples/animals.p8 +++ b/examples/animals.p8 @@ -8,8 +8,8 @@ main { const ubyte database_size = 100 - uword animal_names_buf - uword questions_buf + uword animal_names_buf = memory("animalnames", 500) + uword questions_buf = memory("questions", 2000) uword animal_names_ptr uword questions_ptr @@ -23,8 +23,6 @@ main { sub start() { ; initialize the database - animal_names_buf = memory("animalnames", 500) - questions_buf = memory("questions", 2000) animal_names_ptr = animal_names_buf questions_ptr = questions_buf diff --git a/examples/test.p8 b/examples/test.p8 index 1baa78128..29ce30d11 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,17 +1,11 @@ %import textio -%import gfx2 -%zeropage basicsafe +%zeropage dontuse main { - sub start() { - repeat { - sys.waitvsync() - ubyte joy = lsb(cx16.joystick_get2(0)) - txt.print_ubbin(joy,1) - txt.nl() - } + uword hash_buckets = memory("buckets", 128*32*2) - repeat { - } + sub start() { + txt.print_uwhex(hash_buckets,true) + txt.print("ok") } }