mirror of
https://github.com/irmen/prog8.git
synced 2025-02-25 04:29:36 +00:00
fixed possible type mismatch error in when statements
This commit is contained in:
parent
6938c79f88
commit
7c219d235c
@ -404,17 +404,35 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
|
||||
}
|
||||
|
||||
override fun after(whenChoice: WhenChoice, parent: Node): Iterable<IAstModification> {
|
||||
val conditionDt = (whenChoice.parent as When).condition.inferType(program)
|
||||
if((parent as When).condition.inferType(program).isWords) {
|
||||
val values = whenChoice.values
|
||||
values?.toTypedArray()?.withIndex()?.forEach { (index, value) ->
|
||||
val num = value.constValue(program)
|
||||
if(num!=null && num.type in ByteDatatypes) {
|
||||
val wordNum = NumericLiteral(if(num.type==DataType.UBYTE) DataType.UWORD else DataType.WORD, num.number, num.position)
|
||||
wordNum.parent = num.parent
|
||||
values[index] = wordNum
|
||||
val valueDt = value.inferType(program)
|
||||
if(valueDt!=conditionDt) {
|
||||
val castedValue = (value as NumericLiteral).cast(conditionDt.getOr(DataType.UNDEFINED))
|
||||
if(castedValue.isValid) {
|
||||
values[index] = castedValue.valueOrZero()
|
||||
} else {
|
||||
errors.err("choice value datatype differs from condition value", value.position)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
val values = whenChoice.values
|
||||
values?.toTypedArray()?.withIndex()?.forEach { (index, value) ->
|
||||
val valueDt = value.inferType(program)
|
||||
if(valueDt!=conditionDt) {
|
||||
val castedValue = (value as NumericLiteral).cast(conditionDt.getOr(DataType.UNDEFINED))
|
||||
if(castedValue.isValid) {
|
||||
values[index] = castedValue.valueOrZero()
|
||||
} else {
|
||||
errors.err("choice value datatype differs from condition value", value.position)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return noModifications
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
TODO
|
||||
====
|
||||
|
||||
- IR: optimize WHEN codegen? translate(whenStmt: PtWhen)
|
||||
- IR: instructions that do type conversion (SZ etc, CONCAT, SGN) should put the result in a DIFFERENT register.
|
||||
- IR: reduce the number of branch instructions (gradually), replace with CMP(I) + status branch instruction
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user