mirror of
https://github.com/irmen/prog8.git
synced 2025-01-12 04:30:03 +00:00
non strict bools should also replace not byte with byte==0
This commit is contained in:
parent
607275ec66
commit
c6bf57b390
@ -25,7 +25,8 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
|
|||||||
if(valueDt isnot decl.datatype) {
|
if(valueDt isnot decl.datatype) {
|
||||||
|
|
||||||
if(decl.isArray && !options.strictBool) {
|
if(decl.isArray && !options.strictBool) {
|
||||||
tryConvertBooleanArrays(decl, declValue, parent)
|
if(tryConvertBooleanArrays(decl, declValue, parent))
|
||||||
|
return noModifications
|
||||||
}
|
}
|
||||||
|
|
||||||
if(valueDt.isInteger && decl.isArray) {
|
if(valueDt.isInteger && decl.isArray) {
|
||||||
@ -53,7 +54,7 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
|
|||||||
return noModifications
|
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 valueNumber = declValue.constValue(program)
|
||||||
val valueArray = declValue as? ArrayLiteral
|
val valueArray = declValue as? ArrayLiteral
|
||||||
when (decl.datatype) {
|
when (decl.datatype) {
|
||||||
@ -61,6 +62,7 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
|
|||||||
if(valueNumber!=null) {
|
if(valueNumber!=null) {
|
||||||
decl.value = NumericLiteral.fromBoolean(valueNumber.number!=0.0, declValue.position)
|
decl.value = NumericLiteral.fromBoolean(valueNumber.number!=0.0, declValue.position)
|
||||||
decl.linkParents(parent)
|
decl.linkParents(parent)
|
||||||
|
return true
|
||||||
} else if(valueArray!=null) {
|
} else if(valueArray!=null) {
|
||||||
val newArray = valueArray.value.map {
|
val newArray = valueArray.value.map {
|
||||||
if(it.inferType(program).isBytes) {
|
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.value = ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_BOOL), newArray.toTypedArray(), valueArray.position)
|
||||||
decl.linkParents(parent)
|
decl.linkParents(parent)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DataType.ARRAY_B -> {
|
DataType.ARRAY_B -> {
|
||||||
if(valueNumber!=null) {
|
if(valueNumber!=null) {
|
||||||
decl.value = NumericLiteral(DataType.BYTE, if(valueNumber.asBooleanValue) 1.0 else 0.0, declValue.position)
|
decl.value = NumericLiteral(DataType.BYTE, if(valueNumber.asBooleanValue) 1.0 else 0.0, declValue.position)
|
||||||
decl.linkParents(parent)
|
decl.linkParents(parent)
|
||||||
|
return true
|
||||||
} else if(valueArray!=null) {
|
} else if(valueArray!=null) {
|
||||||
val newArray = valueArray.value.map {
|
val newArray = valueArray.value.map {
|
||||||
if(it.inferType(program).isBool) {
|
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.value = ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_B), newArray.toTypedArray(), valueArray.position)
|
||||||
decl.linkParents(parent)
|
decl.linkParents(parent)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DataType.ARRAY_UB -> {
|
DataType.ARRAY_UB -> {
|
||||||
if(valueNumber!=null) {
|
if(valueNumber!=null) {
|
||||||
decl.value = NumericLiteral(DataType.UBYTE, if(valueNumber.asBooleanValue) 1.0 else 0.0, declValue.position)
|
decl.value = NumericLiteral(DataType.UBYTE, if(valueNumber.asBooleanValue) 1.0 else 0.0, declValue.position)
|
||||||
decl.linkParents(parent)
|
decl.linkParents(parent)
|
||||||
|
return true
|
||||||
} else if(valueArray!=null) {
|
} else if(valueArray!=null) {
|
||||||
val newArray = valueArray.value.map {
|
val newArray = valueArray.value.map {
|
||||||
if(it.inferType(program).isBool) {
|
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.value = ArrayLiteral(InferredTypes.InferredType.known(DataType.ARRAY_UB), newArray.toTypedArray(), valueArray.position)
|
||||||
decl.linkParents(parent)
|
decl.linkParents(parent)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> { /* no casting possible */ }
|
else -> { /* no casting possible */ }
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun after(expr: BinaryExpression, parent: Node): Iterable<IAstModification> {
|
override fun after(expr: BinaryExpression, parent: Node): Iterable<IAstModification> {
|
||||||
@ -487,7 +495,6 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
|
|||||||
return noModifications
|
return noModifications
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun addTypecastOrCastedValueModification(
|
private fun addTypecastOrCastedValueModification(
|
||||||
modifications: MutableList<IAstModification>,
|
modifications: MutableList<IAstModification>,
|
||||||
expressionToCast: Expression,
|
expressionToCast: Expression,
|
||||||
|
@ -96,6 +96,17 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter,
|
|||||||
// +X --> X
|
// +X --> X
|
||||||
return listOf(IAstModification.ReplaceNode(expr, expr.expression, parent))
|
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
|
return noModifications
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
TODO
|
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
|
VM 6502 what
|
||||||
===== ====== =======
|
===== ====== =======
|
||||||
|
Loading…
x
Reference in New Issue
Block a user