From c6bf57b39081fa9dc7dbb15ea3345dbe23597754 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 4 Mar 2024 23:24:46 +0100 Subject: [PATCH] non strict bools should also replace not byte with byte==0 --- .../prog8/compiler/astprocessing/TypecastsAdder.kt | 13 ++++++++++--- .../prog8/compiler/astprocessing/VariousCleanups.kt | 11 +++++++++++ docs/source/todo.rst | 5 +++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt b/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt index 590a212b9..e92887577 100644 --- a/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt +++ b/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt @@ -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 { @@ -487,7 +495,6 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val return noModifications } - private fun addTypecastOrCastedValueModification( modifications: MutableList, expressionToCast: Expression, diff --git a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt index fc996cf5d..bfaa113dc 100644 --- a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt +++ b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt @@ -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 } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 9ee94cf40..4a7410dd2 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -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 ===== ====== =======