fix a compiler crash in certain vardecl initialization expressions

This commit is contained in:
Irmen de Jong 2023-11-28 21:01:58 +01:00
parent fde136fb7b
commit 8dfa0bc38c
3 changed files with 22 additions and 7 deletions

View File

@ -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) errors.err("refused rounding of float to avoid loss of precision", decl.value!!.position)
} else { } else {
// cast the numeric literal to the appropriate datatype of the variable // cast the numeric literal to the appropriate datatype of the variable
declConstValue.linkParents(decl)
val cast = declConstValue.cast(decl.datatype) val cast = declConstValue.cast(decl.datatype)
if (cast.isValid) if (cast.isValid)
return listOf(IAstModification.ReplaceNode(decl.value!!, cast.valueOrZero(), decl)) return listOf(IAstModification.ReplaceNode(decl.value!!, cast.valueOrZero(), decl))

View File

@ -376,6 +376,17 @@ main {
&ubyte[8] array = &mappedvar &ubyte[8] array = &mappedvar
cx16.r0 = &array 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 compileText(VMTarget(), optimize=false, src, writeAssembly=false) shouldNotBe null
} }

View File

@ -1,15 +1,18 @@
%import textio %import textio
%import floats
%zeropage basicsafe
main { main {
sub start() { sub start() {
txt.print("enter number: ") uword size1 = sizeof(22222)
str buffer = "???????????????????????????" txt.print_uw(size1)
void txt.input_chars(buffer)
float value = floats.parse_f(buffer)
txt.nl() 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() txt.nl()
} }
} }