diff --git a/compiler/res/prog8lib/virtual/floats.p8 b/compiler/res/prog8lib/virtual/floats.p8 index de861e09a..8e78bef79 100644 --- a/compiler/res/prog8lib/virtual/floats.p8 +++ b/compiler/res/prog8lib/virtual/floats.p8 @@ -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 {{ diff --git a/virtualmachine/src/prog8/vm/SysCalls.kt b/virtualmachine/src/prog8/vm/SysCalls.kt index c6485f8ce..e71afa77c 100644 --- a/virtualmachine/src/prog8/vm/SysCalls.kt +++ b/virtualmachine/src/prog8/vm/SysCalls.kt @@ -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) + } } } }