vm: added floats.str_f()

This commit is contained in:
Irmen de Jong 2023-12-07 23:10:27 +01:00
parent a01c0a283d
commit d91ca8b197
2 changed files with 24 additions and 3 deletions

View File

@ -10,7 +10,7 @@ floats {
sub print_f(float value) {
; ---- prints the floating point value (without a newline).
; ---- prints the floating point value (without a newline and no leading spaces).
%ir {{
loadm.f fr65535,floats.print_f.value
syscall 25 (fr65535.f)
@ -18,6 +18,18 @@ sub print_f(float value) {
}}
}
sub str_f(float value) -> str {
; ---- converts the floating point value to a string (no leading spaces)
str @shared buffer=" "*20
%ir {{
load.w r65535,floats.str_f.buffer
loadm.f fr65535,floats.str_f.value
syscall 47 (r65535.w, fr65535.f)
load.w r0,floats.str_f.buffer
returnr.w r0
}}
}
sub parse_f(str value) -> float {
; -- parse a string value of a number to float
%ir {{

View File

@ -52,8 +52,9 @@ SYSCALLS:
42 = CLAMP_UWORD
43 = CLAMP_FLOAT
44 = ATAN
45 = STR_TO_FLOAT
45 = str to float
46 = MUL16_LAST_UPPER
47 = float to str
*/
enum class Syscall {
@ -103,7 +104,8 @@ enum class Syscall {
CLAMP_FLOAT,
ATAN,
STR_TO_FLOAT,
MUL16_LAST_UPPER
MUL16_LAST_UPPER,
FLOAT_TO_STR
;
companion object {
@ -509,6 +511,13 @@ object SysCalls {
Syscall.MUL16_LAST_UPPER -> {
returnValue(callspec.returns!!, vm.mul16_last_upper, vm)
}
Syscall.FLOAT_TO_STR -> {
val (buffer, number) = getArgValues(callspec.arguments, vm)
val bufferAddr = (buffer as UShort).toShort().toInt()
val numf = number as Double
val numStr = if(numf.toInt().toDouble()==numf) numf.toInt().toString() else numf.toString()
vm.memory.setString(bufferAddr, numStr, true)
}
}
}
}