diff options
Diffstat (limited to 'main.cpp')
-rw-r--r-- | main.cpp | 23 |
1 files changed, 7 insertions, 16 deletions
@@ -17,6 +17,7 @@ #include "sforth/forth.hpp" #include <array> +#include <charconv> #include <fstream> #include <iostream> #include <span> @@ -24,25 +25,15 @@ constinit static sforth::forth<4096> forth {sforth::initialize<&forth>()}; -static void putu(sforth::addr v) -{ - char buf[32] = {}; - auto ptr = buf + sizeof(buf); - *--ptr = '\0'; - do { - *--ptr = "0123456789abcdefghijklmnopqrstuvwxyz"[v % forth.base]; - } while (v /= forth.base); - std::cout << ptr << ' '; -} - constinit static sforth::native_word<".", [](auto) { - sforth::addr v = forth.pop(); - if (v & (1 << (8 * sizeof(sforth::cell) - 1))) - std::cout << '-'; - putu(v); + char buf[8 * sizeof(sforth::cell) + 1] = {}; + std::to_chars(buf, buf + sizeof(buf), forth.pop(), forth.base); + std::cout << buf << ' '; }> dot; constinit static sforth::native_word<"U.", [](auto) { - putu(forth.pop()); + char buf[8 * sizeof(sforth::cell) + 1] = {}; + std::to_chars(buf, buf + sizeof(buf), std::bit_cast<sforth::addr>(forth.pop()), forth.base); + std::cout << buf << ' '; }, &dot> udot; constinit static sforth::native_word<"EMIT", [](auto) { std::cout << static_cast<char>(forth.pop()); |