be smarter about certain implicit type casts

This commit is contained in:
Irmen de Jong 2020-06-03 23:55:41 +02:00
parent 02b12cc762
commit 33647a29d0
2 changed files with 25 additions and 45 deletions

View File

@ -42,11 +42,34 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke
if(targetItype.isKnown && valueItype.isKnown) {
val targettype = targetItype.typeOrElse(DataType.STRUCT)
val valuetype = valueItype.typeOrElse(DataType.STRUCT)
if (valuetype != targettype && valuetype isAssignableTo targettype) {
if (valuetype != targettype) {
if (valuetype isAssignableTo targettype) {
return listOf(IAstModification.ReplaceNode(
assignment.value,
TypecastExpression(assignment.value, targettype, true, assignment.value.position),
assignment))
} else {
fun castLiteral(cvalue: NumericLiteralValue): List<IAstModification.ReplaceNode> =
listOf(IAstModification.ReplaceNode(cvalue, cvalue.cast(targettype), cvalue.parent))
val cvalue = assignment.value.constValue(program)
if(cvalue!=null) {
val number = cvalue.number.toDouble()
// more complex comparisons if the type is different, but the constant value is compatible
if (valuetype == DataType.BYTE && targettype == DataType.UBYTE) {
if(number>0)
return castLiteral(cvalue)
} else if (valuetype == DataType.WORD && targettype == DataType.UWORD) {
if(number>0)
return castLiteral(cvalue)
} else if (valuetype == DataType.UBYTE && targettype == DataType.BYTE) {
if(number<0x80)
return castLiteral(cvalue)
} else if (valuetype == DataType.UWORD && targettype == DataType.WORD) {
if(number<0x8000)
return castLiteral(cvalue)
}
}
}
}
}
return emptyList()

View File

@ -6,51 +6,8 @@
main {
sub start() {
line(1111,22,3333,22)
line(1111,22,1111,22)
line(1111,22,666,22)
line(1111,22,1111,33)
line(1111,22,1111,22)
line(1111,22,1111,11)
;...
}
sub line(uword x1, ubyte y1, uword x2, ubyte y2) {
word dx
word dy
byte ix = 1
byte iy = 1
if x2>x1 {
dx = x2-x1 as word
} else {
ix = -1
dx = x1-x2 as word
}
if y2>y1 {
dy = y2-y1
} else {
iy = -1
dy = y1-y2
}
word dx2 = 2 * dx
word dy2 = 2 * dy
word d = 0
plotx = x1
c64scr.print("dx=")
c64scr.print_w(dx)
c64scr.print(" ix=")
c64scr.print_b(ix)
c64.CHROUT('\n')
c64scr.print("dy=")
c64scr.print_w(dy)
c64scr.print(" iy=")
c64scr.print_b(iy)
c64.CHROUT('\n')
}
uword plotx
}