fix dt compiler crash

This commit is contained in:
Irmen de Jong 2024-11-17 17:39:36 +01:00
parent 08865dbb4e
commit e0e01f794e
3 changed files with 25 additions and 5 deletions

View File

@ -148,9 +148,11 @@ internal class NotExpressionAndIfComparisonExprChanger(val program: Program, val
// not(~x) -> x!=0
if((expr.expression as? PrefixExpression)?.operator=="~") {
val x = (expr.expression as PrefixExpression).expression
val dt = x.inferType(program).getOrElse { throw FatalAstException("invalid dt ${x.position}") }
val notZero = BinaryExpression(x, "!=", NumericLiteral(dt, 0.0, expr.position), expr.position)
return listOf(IAstModification.ReplaceNode(expr, notZero, parent))
val dt = x.inferType(program).getOr(DataType.UNDEFINED)
if(dt != DataType.UNDEFINED) {
val notZero = BinaryExpression(x, "!=", NumericLiteral(dt, 0.0, expr.position), expr.position)
return listOf(IAstModification.ReplaceNode(expr, notZero, parent))
}
}
// not X <compare> Y -> X <invertedcompare> Y

View File

@ -58,8 +58,8 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter,
return listOf(IAstModification.ReplaceNode(typecast, typecast.expression, parent))
if(parent is Assignment) {
val targetDt = parent.target.inferType(program).getOrElse { throw FatalAstException("invalid dt ${parent.target.position}") }
if(sourceDt istype targetDt) {
val targetDt = parent.target.inferType(program).getOr(DataType.UNDEFINED)
if(targetDt!=DataType.UNDEFINED && sourceDt istype targetDt) {
// we can get rid of this typecast because the type is already the target type
return listOf(IAstModification.ReplaceNode(typecast, typecast.expression, parent))
}

View File

@ -1,6 +1,7 @@
package prog8tests.ast
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.comparables.shouldBeGreaterThan
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.string.shouldContain
@ -459,6 +460,23 @@ main {
}
context("various") {
test("no crash for all sorts of undefined variables in complex expression") {
val src = """
%import floats
main {
sub start() {
x_position = (((floats.cos(CORNER_ANGLE + theta) * distance_to_corner) + (position_offset as float)) * 256.0) as word
}
}"""
val errors = ErrorReporterForTests()
compileText(C64Target(), optimize=false, src, writeAssembly=false, errors = errors) shouldBe null
errors.errors.size shouldBeGreaterThan 1
errors.clear()
compileText(C64Target(), optimize=true, src, writeAssembly=false, errors = errors) shouldBe null
errors.errors.size shouldBeGreaterThan 1
}
test("symbol names in inline assembly blocks") {
val names1 = InlineAssembly("""