diff --git a/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt b/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt index a31fc7ab3..fa94cf85b 100644 --- a/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt +++ b/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt @@ -404,17 +404,35 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val } override fun after(whenChoice: WhenChoice, parent: Node): Iterable { + 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 } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 7f6f6034a..6c9a6759a 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -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