mirror of
https://github.com/irmen/prog8.git
synced 2024-10-16 18:23:59 +00:00
give an error when initializing an integer var with a float value instead of silently rounding
This commit is contained in:
parent
fa11a6e18b
commit
8f379e2262
@ -24,12 +24,17 @@ class VarConstantValueTypeAdjuster(private val program: Program, private val err
|
|||||||
try {
|
try {
|
||||||
val declConstValue = decl.value?.constValue(program)
|
val declConstValue = decl.value?.constValue(program)
|
||||||
if(declConstValue!=null && (decl.type==VarDeclType.VAR || decl.type==VarDeclType.CONST)
|
if(declConstValue!=null && (decl.type==VarDeclType.VAR || decl.type==VarDeclType.CONST)
|
||||||
&& declConstValue.inferType(program) isnot decl.datatype) {
|
&& declConstValue.type != decl.datatype) {
|
||||||
|
// avoid silent float roundings
|
||||||
|
if(decl.datatype in IntegerDatatypes && declConstValue.type==DataType.FLOAT) {
|
||||||
|
errors.err("refused silent rounding of float to avoid loss of precision", decl.value!!.position)
|
||||||
|
} else {
|
||||||
// cast the numeric literal to the appropriate datatype of the variable
|
// cast the numeric literal to the appropriate datatype of the variable
|
||||||
val cast = declConstValue.cast(decl.datatype)
|
val cast = declConstValue.cast(decl.datatype)
|
||||||
if(cast.isValid)
|
if (cast.isValid)
|
||||||
return listOf(IAstModification.ReplaceNode(decl.value!!, cast.valueOrZero(), decl))
|
return listOf(IAstModification.ReplaceNode(decl.value!!, cast.valueOrZero(), decl))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (x: UndefinedSymbolError) {
|
} catch (x: UndefinedSymbolError) {
|
||||||
errors.err(x.message, x.position)
|
errors.err(x.message, x.position)
|
||||||
}
|
}
|
||||||
|
@ -388,7 +388,16 @@ class NumericLiteralValue(val type: DataType, // only numerical types allowed
|
|||||||
numbervalue: Double, // can be byte, word or float depending on the type
|
numbervalue: Double, // can be byte, word or float depending on the type
|
||||||
override val position: Position) : Expression() {
|
override val position: Position) : Expression() {
|
||||||
override lateinit var parent: Node
|
override lateinit var parent: Node
|
||||||
val number: Double = if(type==DataType.FLOAT) numbervalue else round(numbervalue)
|
val number: Double by lazy {
|
||||||
|
if(type==DataType.FLOAT)
|
||||||
|
numbervalue
|
||||||
|
else {
|
||||||
|
val rounded = round(numbervalue)
|
||||||
|
if(rounded != numbervalue)
|
||||||
|
throw ExpressionError("refused silent rounding of float to avoid loss of precision", position)
|
||||||
|
rounded
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override val isSimple = true
|
override val isSimple = true
|
||||||
override fun copy() = NumericLiteralValue(type, number, position)
|
override fun copy() = NumericLiteralValue(type, number, position)
|
||||||
|
@ -9,9 +9,6 @@ optimize TODO in "Add assignment to initialize with zero" in StatementReorderer
|
|||||||
optimize TODO in after(assignment) in VariousCleanups
|
optimize TODO in after(assignment) in VariousCleanups
|
||||||
optimize: bitwise operations with a negative constant number -> replace the number by its positive 2 complement
|
optimize: bitwise operations with a negative constant number -> replace the number by its positive 2 complement
|
||||||
optimize: add some more constant folders mentioned in test.p8
|
optimize: add some more constant folders mentioned in test.p8
|
||||||
|
|
||||||
fix: give error when initializing an integer var with a float value
|
|
||||||
|
|
||||||
optimize: there is an optimizations in AsmOptimizer that can only be done correctly
|
optimize: there is an optimizations in AsmOptimizer that can only be done correctly
|
||||||
if it knows about regular ram vs io space ram distinction.
|
if it knows about regular ram vs io space ram distinction.
|
||||||
|
|
||||||
|
@ -5,13 +5,12 @@ main {
|
|||||||
|
|
||||||
sub start() {
|
sub start() {
|
||||||
|
|
||||||
ubyte xx = 1.234
|
ubyte xx = 1
|
||||||
ubyte yy = 2.234
|
ubyte yy = 2
|
||||||
uword aw
|
uword aw
|
||||||
byte bb
|
byte bb
|
||||||
float fl
|
float fl
|
||||||
|
|
||||||
|
|
||||||
; TODO add these constant folders:
|
; TODO add these constant folders:
|
||||||
|
|
||||||
; (X + C1) + (Y + C2) => (X + Y) + (C1 + C2)
|
; (X + C1) + (Y + C2) => (X + Y) + (C1 + C2)
|
||||||
|
Loading…
Reference in New Issue
Block a user