allow comparisons against constant values with different type

This commit is contained in:
Irmen de Jong
2025-02-17 21:04:24 +01:00
parent a36bdc54fd
commit d06e9ea7f6
5 changed files with 21 additions and 32 deletions

View File

@@ -1276,17 +1276,6 @@ internal class AstChecker(private val program: Program,
// str+str and str*number have already been const evaluated before we get here.
errors.err("no computational or logical expressions with strings or arrays are possible", expr.position)
}
} else {
if(expr.left is TypecastExpression && expr.right is NumericLiteral && !(expr.right.inferType(program) issimpletype BaseDataType.FLOAT)) {
val origLeftDt = (expr.left as TypecastExpression).expression.inferType(program).getOrUndef()
if(rightDt.largerSizeThan(origLeftDt) && !(expr.right as NumericLiteral).cast(origLeftDt.base, true).isValid)
errors.err("operands are not the same type", expr.right.position)
}
if(expr.right is TypecastExpression && expr.left is NumericLiteral && !(expr.left.inferType(program) issimpletype BaseDataType.FLOAT)) {
val origRightDt = (expr.right as TypecastExpression).expression.inferType(program).getOrUndef()
if(leftDt.largerSizeThan(origRightDt) && !(expr.left as NumericLiteral).cast(origRightDt.base, true).isValid)
errors.err("operands are not the same type", expr.right.position)
}
}
if(leftDt.isBool || rightDt.isBool ||

View File

@@ -954,4 +954,17 @@ main {
(v4.right as NumericLiteral).type shouldBe BaseDataType.UWORD
(v4.right as NumericLiteral).number shouldBe 5
}
test("allow comparisons against constant values with different type") {
val src = """
main {
sub start() {
const uword MAX_CAVE_WIDTH = 440 ; word here to avoid having to cast to word all the time
if cx16.r0L > MAX_CAVE_WIDTH
return
}
}"""
compileText(C64Target(), false, src, writeAssembly = false) shouldNotBe null
}
})