From 6f7322150fe117d9c2290debb770ae28c65b8aad Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 31 Jul 2019 00:14:12 +0200 Subject: [PATCH] fix string literal replacing by identifierref --- .../ast/processing/AstIdentifiersChecker.kt | 8 +++-- examples/test.p8 | 30 +++++++++---------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt index 114c31a18..438c868e8 100644 --- a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt +++ b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt @@ -236,6 +236,7 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi override fun visit(refLiteral: ReferenceLiteralValue): Expression { val litval = super.visit(refLiteral) if(litval is ReferenceLiteralValue) { + val vardecl = litval.parent as? VarDecl if (litval.isString) { // intern the string; move it into the heap if (litval.str!!.length !in 1..255) @@ -243,8 +244,11 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi else { litval.addToHeap(program.heap) } + return if(vardecl!=null) + litval + else + makeIdentifierFromRefLv(litval) // replace the literal string by a identifier reference. } else if (litval.isArray) { - val vardecl = litval.parent as? VarDecl if (vardecl!=null) { return fixupArrayDatatype(litval, vardecl, program.heap) } else { @@ -253,7 +257,7 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi val datatype = determineArrayDt(litval.array!!) ?: return litval val litval2 = litval.cast(datatype)!! litval2.parent = litval.parent - // finally, replace the literal by a identifier reference. + // finally, replace the literal array by a identifier reference. return makeIdentifierFromRefLv(litval2) } } diff --git a/examples/test.p8 b/examples/test.p8 index 1e32d2377..986464efc 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,15 +5,15 @@ main { sub start() { -; byte bvar -; ubyte var2 -; -; for A in "hello" { -; c64scr.print_ub(A) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; + byte bvar + ubyte var2 + + for A in "hello" { + c64scr.print_ub(A) + c64.CHROUT(',') + } + c64.CHROUT('\n') + ; for A in [1,3,5,99] { ; c64scr.print_ub(A) ; c64.CHROUT(',') @@ -26,12 +26,6 @@ main { ; } ; c64.CHROUT('\n') - for float fl in [1.1, 2.2, 5.5, 99.99] { - c64flt.print_f(fl) - c64.CHROUT(',') - } - c64.CHROUT('\n') - ; for var2 in 10 to 20 { ; c64scr.print_ub(var2) ; c64.CHROUT(',') @@ -48,6 +42,12 @@ main { ; c64scr.print_b(bvar) ; c64.CHROUT(',') ; } +; c64.CHROUT('\n') + +; for float fl in [1.1, 2.2, 5.5, 99.99] { +; c64flt.print_f(fl) +; c64.CHROUT(',') +; } ; c64.CHROUT('\n') } }