From c229a831210c0b3c1fb24f9ccfd9e190f0f22a5e Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sun, 8 Dec 2024 18:08:56 -0500 Subject: fix . and u.; work on */ words --- main.cpp | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index b14d9e4..be00744 100644 --- a/main.cpp +++ b/main.cpp @@ -17,6 +17,7 @@ #include "sforth/forth.hpp" #include +#include #include #include #include @@ -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(forth.pop()), forth.base); + std::cout << buf << ' '; }, &dot> udot; constinit static sforth::native_word<"EMIT", [](auto) { std::cout << static_cast(forth.pop()); -- cgit v1.2.3