non strict bools should also replace not byte with byte==0

This commit is contained in:
Irmen de Jong 2024-03-04 23:24:46 +01:00
parent 607275ec66
commit c6bf57b390
3 changed files with 26 additions and 3 deletions

View File

@ -25,7 +25,8 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
if(valueDt isnot decl.datatype) {
if(decl.isArray && !options.strictBool) {
tryConvertBooleanArrays(decl, declValue, parent)
if(tryConvertBooleanArrays(decl, declValue, parent))
return noModifications
}
if(valueDt.isInteger && decl.isArray) {
@ -53,7 +54,7 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
return noModifications
}
private fun tryConvertBooleanArrays(decl: VarDecl, declValue: Expression, parent: Node) {
private fun tryConvertBooleanArrays(decl: VarDecl, declValue: Expression, parent: Node): Boolean {
val valueNumber = declValue.constValue(program)
val valueArray = declValue as? ArrayLiteral
when (decl.datatype) {
@ -61,6 +62,7 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
if(valueNumber!=null) {
decl.value = NumericLiteral.fromBoolean(valueNumber.number!=0.0, declValue.position)
decl.linkParents(parent)
return true
} else if(valueArray!=null) {
val newArray = valueArray.value.map {
if(it.inferType(program).isBytes) {
@ -71,12 +73,14 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
}
decl.value = ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_BOOL), newArray.toTypedArray(), valueArray.position)
decl.linkParents(parent)
return true
}
}
DataType.ARRAY_B -> {
if(valueNumber!=null) {
decl.value = NumericLiteral(DataType.BYTE, if(valueNumber.asBooleanValue) 1.0 else 0.0, declValue.position)
decl.linkParents(parent)
return true
} else if(valueArray!=null) {
val newArray = valueArray.value.map {
if(it.inferType(program).isBool) {
@ -87,12 +91,14 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
}
decl.value = ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_B), newArray.toTypedArray(), valueArray.position)
decl.linkParents(parent)
return true
}
}
DataType.ARRAY_UB -> {
if(valueNumber!=null) {
decl.value = NumericLiteral(DataType.UBYTE, if(valueNumber.asBooleanValue) 1.0 else 0.0, declValue.position)
decl.linkParents(parent)
return true
} else if(valueArray!=null) {
val newArray = valueArray.value.map {
if(it.inferType(program).isBool) {
@ -103,10 +109,12 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
}
decl.value = ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_UB), newArray.toTypedArray(), valueArray.position)
decl.linkParents(parent)
return true
}
}
else -> { /* no casting possible */ }
}
return false
}
override fun after(expr: BinaryExpression, parent: Node): Iterable<IAstModification> {
@ -487,7 +495,6 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
return noModifications
}
private fun addTypecastOrCastedValueModification(
modifications: MutableList<IAstModification>,
expressionToCast: Expression,

View File

@ -96,6 +96,17 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter,
// +X --> X
return listOf(IAstModification.ReplaceNode(expr, expr.expression, parent))
}
if(!options.strictBool && expr.operator=="not") {
if(expr.expression.inferType(program).isBytes) {
// not bytevalue --> bytevalue==0
val cmp = BinaryExpression(expr.expression, "==",
NumericLiteral(expr.expression.inferType(program).getOr(DataType.UNDEFINED), 0.0, expr.expression.position),
expr.expression.position)
return listOf(IAstModification.ReplaceNode(expr, cmp, parent))
}
}
return noModifications
}

View File

@ -1,6 +1,11 @@
TODO
====
the not changed (master branch) Petaxian compiled with -nostrictbool is a lot smaller than the updated (boolean branch) compiled without.
What is the difference!
===== ====== =======
VM 6502 what
===== ====== =======