fix compiler error when initializing var with memory(...) in block scope instead of subroutine

This commit is contained in:
Irmen de Jong 2021-04-17 15:49:41 +02:00
parent e680de05ea
commit e93701f50e
4 changed files with 20 additions and 23 deletions

View File

@ -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

View File

@ -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 ;-)

View File

@ -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

View File

@ -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")
}
}