fix compiler error caused by removal of string symbol in txt.print() optimization

This commit is contained in:
Irmen de Jong 2023-11-17 19:34:19 +01:00
parent 2637939e62
commit f21adaa3ef
3 changed files with 20 additions and 10 deletions

View File

@ -45,11 +45,7 @@ class StatementOptimizer(private val program: Program,
mutableListOf(NumericLiteral(DataType.UBYTE, firstCharEncoded.toDouble(), pos)), mutableListOf(NumericLiteral(DataType.UBYTE, firstCharEncoded.toDouble(), pos)),
functionCallStatement.void, pos functionCallStatement.void, pos
) )
val stringDecl = string.parent as VarDecl return listOf(IAstModification.ReplaceNode(functionCallStatement, chrout, parent))
return listOf(
IAstModification.ReplaceNode(functionCallStatement, chrout, parent),
IAstModification.Remove(stringDecl, stringDecl.parent as IStatementContainer)
)
} else if (string.value.length == 2) { } else if (string.value.length == 2) {
val firstTwoCharsEncoded = options.compTarget.encodeString(string.value.take(2), string.encoding) val firstTwoCharsEncoded = options.compTarget.encodeString(string.value.take(2), string.encoding)
val chrout1 = FunctionCallStatement( val chrout1 = FunctionCallStatement(
@ -62,11 +58,9 @@ class StatementOptimizer(private val program: Program,
mutableListOf(NumericLiteral(DataType.UBYTE, firstTwoCharsEncoded[1].toDouble(), pos)), mutableListOf(NumericLiteral(DataType.UBYTE, firstTwoCharsEncoded[1].toDouble(), pos)),
functionCallStatement.void, pos functionCallStatement.void, pos
) )
val stringDecl = string.parent as VarDecl
return listOf( return listOf(
IAstModification.InsertBefore(functionCallStatement, chrout1, parent as IStatementContainer), IAstModification.InsertBefore(functionCallStatement, chrout1, parent as IStatementContainer),
IAstModification.ReplaceNode(functionCallStatement, chrout2, parent), IAstModification.ReplaceNode(functionCallStatement, chrout2, parent)
IAstModification.Remove(stringDecl, stringDecl.parent as IStatementContainer)
) )
} }
} }

View File

@ -16,6 +16,7 @@ import prog8.code.core.DataType
import prog8.code.core.Position import prog8.code.core.Position
import prog8.code.target.C64Target import prog8.code.target.C64Target
import prog8.code.target.Cx16Target import prog8.code.target.Cx16Target
import prog8.code.target.VMTarget
import prog8tests.helpers.* import prog8tests.helpers.*
@ -758,4 +759,21 @@ main {
}""" }"""
compileText(C64Target(), true, text, writeAssembly = false) shouldNotBe null compileText(C64Target(), true, text, writeAssembly = false) shouldNotBe null
} }
test("replacing string print by chrout with referenced string elsewhere shouldn't give string symbol error") {
val text="""
%import textio
main {
sub start() {
str key = "test"
txt.print(":")
if key != ":" {
cx16.r0++
}
}
}
"""
compileText(VMTarget(), true, text, writeAssembly = false) shouldNotBe null
}
}) })

View File

@ -2,8 +2,6 @@
TODO TODO
==== ====
- fix discord string bug for if key!="." : "ERROR undefined symbol: prog8_interned_strings.string_3"
- txt.waitkey() should return the pressed key? Also on atari. - txt.waitkey() should return the pressed key? Also on atari.
- [on branch: shortcircuit] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 .... - [on branch: shortcircuit] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 ....