]> code.bitgloo.com Git - clyne/sforth.git/commitdiff
fix some bad num/str conversion bugs
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 1 Dec 2024 21:26:33 +0000 (16:26 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 1 Dec 2024 21:26:33 +0000 (16:26 -0500)
main.cpp
sforth/types.hpp

index f5c93a7969690d214edce018e9854eebfd32e437..d30b8152336aec92627858f812df70ece7636290 100644 (file)
--- a/main.cpp
+++ b/main.cpp
 #include <span>
 #include <string>
 
-constinit static sforth::forth<1024> forth {sforth::initialize<&forth>()};
+constinit static sforth::forth<2048> forth {sforth::initialize<&forth>()};
 
 constinit static sforth::native_word<".", [](auto) {
     char buf[32] = {};
     auto ptr = buf + sizeof(buf);
     auto v = forth.pop();
+    bool neg = v < 0;
+    if (neg) v = -v;
     *--ptr = '\0';
     do {
         *--ptr = "0123456789abcdefghijklmnopqrstuvwxyz"[v % forth.base];
     } while (v /= forth.base);
+    if (neg) *--ptr = '-';
     std::cout << ptr << ' ';
 }> dot;
 constinit static sforth::native_word<"emit", [](auto) {
index 3d26581816bad1950c53ccef1b98748d6e49ca11..57c37c5779b0b17ab46ca26c4c151360220d437b 100644 (file)
@@ -46,12 +46,12 @@ constexpr std::optional<T> from_chars(std::string_view sv, int base = 10)
         } else if (ch >= 'A' && ch <= 'Z') {
             if (base > 10 && ch - 'A' < base - 10) {
                 res *= base;
-                res += ch - 'A';
+                res += ch - 'A' + 10;
             } else return {};
         } else if (ch >= 'a' && ch <= 'z') {
             if (base > 10 && ch - 'a' < base - 10) {
                 res *= base;
-                res += ch - 'a';
+                res += ch - 'a' + 10;
             } else return {};
         } else if (ch == '-' && res == 0) {
             neg = true;