mirror of
https://github.com/irmen/prog8.git
synced 2025-01-10 20:30:23 +00:00
fix dt compiler crash
This commit is contained in:
parent
08865dbb4e
commit
e0e01f794e
@ -148,10 +148,12 @@ internal class NotExpressionAndIfComparisonExprChanger(val program: Program, val
|
|||||||
// not(~x) -> x!=0
|
// not(~x) -> x!=0
|
||||||
if((expr.expression as? PrefixExpression)?.operator=="~") {
|
if((expr.expression as? PrefixExpression)?.operator=="~") {
|
||||||
val x = (expr.expression as PrefixExpression).expression
|
val x = (expr.expression as PrefixExpression).expression
|
||||||
val dt = x.inferType(program).getOrElse { throw FatalAstException("invalid dt ${x.position}") }
|
val dt = x.inferType(program).getOr(DataType.UNDEFINED)
|
||||||
|
if(dt != DataType.UNDEFINED) {
|
||||||
val notZero = BinaryExpression(x, "!=", NumericLiteral(dt, 0.0, expr.position), expr.position)
|
val notZero = BinaryExpression(x, "!=", NumericLiteral(dt, 0.0, expr.position), expr.position)
|
||||||
return listOf(IAstModification.ReplaceNode(expr, notZero, parent))
|
return listOf(IAstModification.ReplaceNode(expr, notZero, parent))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// not X <compare> Y -> X <invertedcompare> Y
|
// not X <compare> Y -> X <invertedcompare> Y
|
||||||
val subBinExpr = expr.expression as? BinaryExpression
|
val subBinExpr = expr.expression as? BinaryExpression
|
||||||
|
@ -58,8 +58,8 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter,
|
|||||||
return listOf(IAstModification.ReplaceNode(typecast, typecast.expression, parent))
|
return listOf(IAstModification.ReplaceNode(typecast, typecast.expression, parent))
|
||||||
|
|
||||||
if(parent is Assignment) {
|
if(parent is Assignment) {
|
||||||
val targetDt = parent.target.inferType(program).getOrElse { throw FatalAstException("invalid dt ${parent.target.position}") }
|
val targetDt = parent.target.inferType(program).getOr(DataType.UNDEFINED)
|
||||||
if(sourceDt istype targetDt) {
|
if(targetDt!=DataType.UNDEFINED && sourceDt istype targetDt) {
|
||||||
// we can get rid of this typecast because the type is already the target type
|
// we can get rid of this typecast because the type is already the target type
|
||||||
return listOf(IAstModification.ReplaceNode(typecast, typecast.expression, parent))
|
return listOf(IAstModification.ReplaceNode(typecast, typecast.expression, parent))
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package prog8tests.ast
|
package prog8tests.ast
|
||||||
|
|
||||||
import io.kotest.core.spec.style.FunSpec
|
import io.kotest.core.spec.style.FunSpec
|
||||||
|
import io.kotest.matchers.comparables.shouldBeGreaterThan
|
||||||
import io.kotest.matchers.shouldBe
|
import io.kotest.matchers.shouldBe
|
||||||
import io.kotest.matchers.shouldNotBe
|
import io.kotest.matchers.shouldNotBe
|
||||||
import io.kotest.matchers.string.shouldContain
|
import io.kotest.matchers.string.shouldContain
|
||||||
@ -459,6 +460,23 @@ main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
context("various") {
|
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") {
|
test("symbol names in inline assembly blocks") {
|
||||||
val names1 = InlineAssembly("""
|
val names1 = InlineAssembly("""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user