mirror of
https://github.com/irmen/prog8.git
synced 2025-04-08 09:37:35 +00:00
fix cast error and vm float parsing
This commit is contained in:
parent
afbc91d1fc
commit
ee782e92ac
@ -564,6 +564,8 @@ class NumericLiteral(val type: DataType, // only numerical types allowed
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if(targettype== DataType.FLOAT)
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if(targettype==DataType.LONG)
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if(targettype==DataType.BOOL)
|
||||
return CastValue(true, fromBoolean(number!=0.0, position))
|
||||
}
|
||||
@ -586,6 +588,8 @@ class NumericLiteral(val type: DataType, // only numerical types allowed
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if(targettype==DataType.BOOL)
|
||||
return CastValue(true, fromBoolean(number!=0.0, position))
|
||||
if(targettype==DataType.LONG)
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
}
|
||||
DataType.UWORD -> {
|
||||
if(targettype== DataType.BYTE && number <= 127)
|
||||
@ -598,6 +602,8 @@ class NumericLiteral(val type: DataType, // only numerical types allowed
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if(targettype==DataType.BOOL)
|
||||
return CastValue(true, fromBoolean(number!=0.0, position))
|
||||
if(targettype==DataType.LONG)
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
}
|
||||
DataType.WORD -> {
|
||||
if(targettype== DataType.BYTE && number >= -128 && number <=127)
|
||||
@ -618,6 +624,8 @@ class NumericLiteral(val type: DataType, // only numerical types allowed
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if(targettype==DataType.BOOL)
|
||||
return CastValue(true, fromBoolean(number!=0.0, position))
|
||||
if(targettype==DataType.LONG)
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
}
|
||||
DataType.FLOAT -> {
|
||||
try {
|
||||
@ -629,6 +637,8 @@ class NumericLiteral(val type: DataType, // only numerical types allowed
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if (targettype == DataType.UWORD && number >= 0 && number <= 65535)
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if(targettype==DataType.LONG && number >=0 && number <= 2147483647)
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if(targettype==DataType.BOOL)
|
||||
return CastValue(true, fromBoolean(number!=0.0, position))
|
||||
} catch (x: ExpressionError) {
|
||||
@ -639,7 +649,22 @@ class NumericLiteral(val type: DataType, // only numerical types allowed
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
}
|
||||
DataType.LONG -> {
|
||||
/* ignore this cast, LONG can't be used. Error will be given elsewhere */
|
||||
try {
|
||||
if (targettype == DataType.BYTE && number >= -128 && number <= 127)
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if (targettype == DataType.UBYTE && number >= 0 && number <= 255)
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if (targettype == DataType.WORD && number >= -32768 && number <= 32767)
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if (targettype == DataType.UWORD && number >= 0 && number <= 65535)
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
if(targettype==DataType.BOOL)
|
||||
return CastValue(true, fromBoolean(number!=0.0, position))
|
||||
if(targettype== DataType.FLOAT)
|
||||
return CastValue(true, NumericLiteral(targettype, number, position))
|
||||
} catch (x: ExpressionError) {
|
||||
return CastValue(false, null)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
throw FatalAstException("type cast of weird type $type")
|
||||
|
@ -376,8 +376,18 @@ object SysCalls {
|
||||
}
|
||||
Syscall.STR_TO_FLOAT -> {
|
||||
val stringAddr = getArgValues(callspec.arguments, vm).single() as UShort
|
||||
val memstring = vm.memory.getString(stringAddr.toInt())
|
||||
returnValue(callspec.returns!!, memstring.toDouble(), vm)
|
||||
val memstring = vm.memory.getString(stringAddr.toInt()).replace(" ", "")
|
||||
val result = if(memstring.isEmpty())
|
||||
0.0
|
||||
else {
|
||||
val trimmed = memstring.takeWhile { it in " +-0123456789.eE" }
|
||||
try {
|
||||
trimmed.toDouble()
|
||||
} catch(x: NumberFormatException) {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
returnValue(callspec.returns!!, result, vm)
|
||||
}
|
||||
Syscall.COMPARE_STRINGS -> {
|
||||
val (firstV, secondV) = getArgValues(callspec.arguments, vm)
|
||||
|
Loading…
x
Reference in New Issue
Block a user