]> code.bitgloo.com Git - clyne/sforth.git/commitdiff
fix . and u.; work on */ words
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 8 Dec 2024 23:08:56 +0000 (18:08 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 8 Dec 2024 23:08:56 +0000 (18:08 -0500)
main.cpp
sforth/forth.hpp

index b14d9e4b02da8ae6890902021aea6ffa42e80154..be00744254085082030f1438989cfe7a4c4947bd 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -17,6 +17,7 @@
 #include "sforth/forth.hpp"
 
 #include <array>
+#include <charconv>
 #include <fstream>
 #include <iostream>
 #include <span>
 
 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());
index effba62790ba31c6abe6c2aa2e21c509626da02f..10b86bf0f98093f682fb4c1ccdef0317fd91d0cd 100644 (file)
@@ -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