mirror of
https://github.com/irmen/prog8.git
synced 2024-12-22 18:30:01 +00:00
fix dt compiler crash
This commit is contained in:
parent
08865dbb4e
commit
e0e01f794e
@ -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
|
||||
|
@ -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))
|
||||
}
|
||||
|
@ -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("""
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user