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

@ -1,11 +1,11 @@
<component name="libraryTable">
<library name="eclipse.lsp4j" type="repository">
<properties maven-id="org.eclipse.lsp4j:org.eclipse.lsp4j:0.23.1" />
<properties maven-id="org.eclipse.lsp4j:org.eclipse.lsp4j:0.24.0" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/eclipse/lsp4j/org.eclipse.lsp4j/0.23.1/org.eclipse.lsp4j-0.23.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/eclipse/lsp4j/org.eclipse.lsp4j.jsonrpc/0.23.1/org.eclipse.lsp4j.jsonrpc-0.23.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/google/code/gson/gson/2.11.0/gson-2.11.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/google/errorprone/error_prone_annotations/2.27.0/error_prone_annotations-2.27.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/eclipse/lsp4j/org.eclipse.lsp4j/0.24.0/org.eclipse.lsp4j-0.24.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/eclipse/lsp4j/org.eclipse.lsp4j.jsonrpc/0.24.0/org.eclipse.lsp4j.jsonrpc-0.24.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/google/code/gson/gson/2.12.1/gson-2.12.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/google/errorprone/error_prone_annotations/2.36.0/error_prone_annotations-2.36.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />

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
}
})

View File

@ -3,23 +3,10 @@
main {
sub start() {
const uword x=128
const uword MAX_CAVE_WIDTH = 440 ; word here to avoid having to cast to word all the time
const uword MAX_CAVE_WIDTH = 40 ; word here to avoid having to cast to word all the time
ubyte @shared width
if cx16.r0L==x
if cx16.r0L > MAX_CAVE_WIDTH
return
if cx16.r0L>x
return
if cx16.r0L<x
return
if width<3 or width>MAX_CAVE_WIDTH ; TODO optimize this too
return
cx16.r0L = x ; already gets optimized to a byte value
}
}

View File

@ -3,4 +3,4 @@ org.gradle.console=rich
org.gradle.parallel=true
org.gradle.daemon=true
kotlin.code.style=official
version=11.1
version=11.2-SNAPSHOT