mirror of
https://github.com/irmen/prog8.git
synced 2025-10-25 22:17:23 +00:00
fix crash: byte c = if a < b -1 else 1 "both values should be the same type"
This commit is contained in:
@@ -383,6 +383,25 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
|
||||
return noModifications
|
||||
}
|
||||
|
||||
override fun after(ifExpr: IfExpression, parent: Node): Iterable<IAstModification> {
|
||||
val trueDt = ifExpr.truevalue.inferType(program)
|
||||
val falseDt = ifExpr.falsevalue.inferType(program)
|
||||
if (trueDt != falseDt) {
|
||||
val (commonDt, toFix) = BinaryExpression.commonDatatype(
|
||||
trueDt.getOr(DataType.UNDEFINED),
|
||||
falseDt.getOr(DataType.UNDEFINED),
|
||||
ifExpr.truevalue,
|
||||
ifExpr.falsevalue
|
||||
)
|
||||
if (toFix != null) {
|
||||
val modifications = mutableListOf<IAstModification>()
|
||||
addTypecastOrCastedValueModification(modifications, toFix, commonDt, ifExpr)
|
||||
return modifications
|
||||
}
|
||||
}
|
||||
return noModifications
|
||||
}
|
||||
|
||||
private fun adjustRangeDts(
|
||||
range: RangeExpression,
|
||||
fromConst: NumericLiteral?,
|
||||
|
||||
@@ -887,4 +887,26 @@ main {
|
||||
errors.errors[0] shouldContain "17:16: type UBYTE of return value doesn't match subroutine's return type BYTE"
|
||||
errors.errors[1] shouldContain "20:16: type UWORD of return value doesn't match subroutine's return type WORD"
|
||||
}
|
||||
|
||||
test("if-expression adjusts different value types to common type") {
|
||||
val src="""
|
||||
main {
|
||||
sub start() {
|
||||
cx16.r0sL = if cx16.r0L < cx16.r1L -1 else 1
|
||||
}
|
||||
}"""
|
||||
|
||||
val errors = ErrorReporterForTests()
|
||||
val result = compileText(C64Target(), false, src, writeAssembly = false, errors = errors)!!
|
||||
val program = result.compilerAst
|
||||
val st = program.entrypoint.statements
|
||||
st.size shouldBe 1
|
||||
val assign = st[0] as Assignment
|
||||
assign.target.inferType(program).getOr(DataType.UNDEFINED) shouldBe DataType.BYTE
|
||||
val ifexpr = assign.value as IfExpression
|
||||
ifexpr.truevalue.inferType(program).getOr(DataType.UNDEFINED) shouldBe DataType.BYTE
|
||||
ifexpr.falsevalue.inferType(program).getOr(DataType.UNDEFINED) shouldBe DataType.BYTE
|
||||
ifexpr.truevalue shouldBe instanceOf<NumericLiteral>()
|
||||
ifexpr.falsevalue shouldBe instanceOf<NumericLiteral>()
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,26 +1,8 @@
|
||||
%import textio
|
||||
%option no_sysinit
|
||||
|
||||
main {
|
||||
sub start() {
|
||||
myblock.printit()
|
||||
myblock2.printit2()
|
||||
}
|
||||
}
|
||||
|
||||
myblock {
|
||||
sub printit() {
|
||||
txt.print_uwhex(&printit, true)
|
||||
txt.nl()
|
||||
txt.print_uwhex(&myblock, true)
|
||||
txt.nl()
|
||||
}
|
||||
}
|
||||
|
||||
myblock2 {
|
||||
sub printit2() {
|
||||
txt.print_uwhex(&printit2, true)
|
||||
txt.nl()
|
||||
txt.print_uwhex(&myblock2, true)
|
||||
txt.nl()
|
||||
cx16.r0s = if cx16.r0L < cx16.r1L -1 else 1
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user