diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2024-12-08 18:08:56 -0500 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2024-12-08 18:08:56 -0500 |
commit | c229a831210c0b3c1fb24f9ccfd9e190f0f22a5e (patch) | |
tree | de248eebf522de946c1944738e89b88e0d5e6de5 | |
parent | 8df11ef268f050461390d7070cb84d0d72dbfec6 (diff) |
fix . and u.; work on */ words
-rw-r--r-- | main.cpp | 23 | ||||
-rw-r--r-- | sforth/forth.hpp | 8 |
2 files changed, 13 insertions, 18 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()); diff --git a/sforth/forth.hpp b/sforth/forth.hpp index effba62..10b86bf 100644 --- a/sforth/forth.hpp +++ b/sforth/forth.hpp @@ -227,11 +227,11 @@ constexpr auto initialize() , S{"M*" }, [](auto) { dcell a = fthp->pop(); a *= fthp->pop(); - fthp->push(a, a >> 8 * sizeof(cell)); }, 0 + fthp->push(a, a >> (8 * sizeof(cell))); }, 0 , S{"UM*" }, [](auto) { daddr a = std::bit_cast<addr>(fthp->pop()); a *= std::bit_cast<addr>(fthp->pop()); - fthp->push(a, a >> 8 * sizeof(addr)); }, 0 + fthp->push(a, a >> (8 * sizeof(addr))); }, 0 , S{"/" }, [](auto) { fthp->top() /= fthp->pop(); }, 0 , S{"MOD" }, [](auto) { fthp->top() %= fthp->pop(); }, 0 , S{"AND" }, [](auto) { fthp->top() &= fthp->pop(); }, 0 @@ -328,6 +328,9 @@ constexpr auto initialize() fthp->sourcei = oldi; }, 0 >::word; constexpr static auto& dict2 = comp_dict<prologue, &dict1 + //, S{"*/MOD" }, S{">R M* R> SM/REM"}, 0 + , S{"*/" }, S{">R M* D>S R> /"}, 0 + , S{"/MOD" }, S{"2DUP MOD -ROT /"}, 0 , S{"RECURSE"}, S{"R> R> DUP >R SWAP >R >XT ,"}, word_base::immediate , S{">XT" }, S{"CELL+ DUP @ 127 AND + CELL+"}, 0 , S{"ALIGN" }, S{"HERE DUP ALIGNED SWAP - ALLOT"}, 0 @@ -362,6 +365,7 @@ constexpr auto initialize() , S{"CHAR+" }, S{"1 +" }, 0 , S{"-ROT" }, S{"ROT ROT"}, 0 , S{"2DROP" }, S{"DROP DROP"}, 0 + , S{"D>S" }, S{"DROP"}, 0 , S{"S>D" }, S{"DUP 0<"}, 0 , S{"0<" }, S{"0 <"}, 0 , S{"0<>" }, S{"0 <>"}, 0 |