mirror of
https://github.com/irmen/prog8.git
synced 2024-11-22 00:31:56 +00:00
fix optimization ast parent linkage problem
This commit is contained in:
parent
94263c43d0
commit
2675623aea
@ -77,8 +77,9 @@ class VarConstantValueTypeAdjuster(
|
||||
// variable is never written to, so it can be replaced with a constant, IF the value is a constant
|
||||
errors.info("variable '${decl.name}' is never written to and was replaced by a constant", decl.position)
|
||||
val const = VarDecl(VarDeclType.CONST, decl.origin, decl.datatype, decl.zeropage, decl.arraysize, decl.name, decl.names, declValue, decl.sharedWithAsm, decl.splitArray, decl.position)
|
||||
decl.value = null
|
||||
return listOf(
|
||||
IAstModification.ReplaceNode(decl, const, parent),
|
||||
IAstModification.ReplaceNode(decl, const, parent)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -793,6 +793,18 @@ main {
|
||||
(expr.right as? NumericLiteral)?.number shouldBe 10.0
|
||||
}
|
||||
|
||||
test("var to const inside typecast") {
|
||||
val src="""
|
||||
main {
|
||||
uword variable ; will be made a const
|
||||
|
||||
sub start() {
|
||||
cx16.r0 = msb(variable) as uword
|
||||
}
|
||||
}"""
|
||||
compileText(VMTarget(), true, src, writeAssembly = false) shouldNotBe null
|
||||
}
|
||||
|
||||
test("De Morgan's laws") {
|
||||
val src="""
|
||||
main {
|
||||
|
@ -375,6 +375,7 @@ class TypecastExpression(var expression: Expression, var type: DataType, val imp
|
||||
override fun inferType(program: Program) = InferredTypes.knownFor(type)
|
||||
override fun constValue(program: Program): NumericLiteral? {
|
||||
val cv = expression.constValue(program) ?: return null
|
||||
cv.linkParents(parent)
|
||||
val cast = cv.cast(type, implicit)
|
||||
return if(cast.isValid) {
|
||||
val newval = cast.valueOrZero()
|
||||
|
Loading…
Reference in New Issue
Block a user