diff --git a/codeOptimizers/src/prog8/optimizer/ConstantIdentifierReplacer.kt b/codeOptimizers/src/prog8/optimizer/ConstantIdentifierReplacer.kt index a1208b55d..51709be3e 100644 --- a/codeOptimizers/src/prog8/optimizer/ConstantIdentifierReplacer.kt +++ b/codeOptimizers/src/prog8/optimizer/ConstantIdentifierReplacer.kt @@ -28,6 +28,7 @@ class VarConstantValueTypeAdjuster(private val program: Program, private val err errors.err("refused rounding of float to avoid loss of precision", decl.value!!.position) } else { // cast the numeric literal to the appropriate datatype of the variable + declConstValue.linkParents(decl) val cast = declConstValue.cast(decl.datatype) if (cast.isValid) return listOf(IAstModification.ReplaceNode(decl.value!!, cast.valueOrZero(), decl)) diff --git a/compiler/test/ast/TestVariousCompilerAst.kt b/compiler/test/ast/TestVariousCompilerAst.kt index a6918bc28..65c1fb3c1 100644 --- a/compiler/test/ast/TestVariousCompilerAst.kt +++ b/compiler/test/ast/TestVariousCompilerAst.kt @@ -376,6 +376,17 @@ main { &ubyte[8] array = &mappedvar cx16.r0 = &array } +}""" + compileText(VMTarget(), optimize=false, src, writeAssembly=false) shouldNotBe null + } + + test("sizeof number const evaluation in vardecl") { + val src=""" +main { + sub start() { + uword @shared size1 = sizeof(22222) + uword @shared size2 = sizeof(2.2) + } }""" compileText(VMTarget(), optimize=false, src, writeAssembly=false) shouldNotBe null } diff --git a/examples/test.p8 b/examples/test.p8 index d9516542f..2b70fcc33 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,15 +1,18 @@ %import textio -%import floats -%zeropage basicsafe main { sub start() { - txt.print("enter number: ") - str buffer = "???????????????????????????" - void txt.input_chars(buffer) - float value = floats.parse_f(buffer) + uword size1 = sizeof(22222) + txt.print_uw(size1) txt.nl() - floats.print_f(value) + uword size2 = sizeof(2.2) + txt.print_uw(size2) + txt.nl() + cx16.r0 = sizeof(22222) + txt.print_uw(cx16.r0) + txt.nl() + cx16.r0 = sizeof(2.2) + txt.print_uw(cx16.r0) txt.nl() } }