aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2024-12-01 16:26:33 -0500
committerClyne Sullivan <clyne@bitgloo.com>2024-12-01 16:26:33 -0500
commitdac4c1baab68e81dd3194f10be011574f4cb8492 (patch)
tree066152bfd85108634261fbc7e6cb9290c433664a
parent5de7639632e91ba73a956bcba2a4dc3ea389c0d2 (diff)
fix some bad num/str conversion bugs
-rw-r--r--main.cpp5
-rw-r--r--sforth/types.hpp4
2 files changed, 6 insertions, 3 deletions
diff --git a/main.cpp b/main.cpp
index f5c93a7..d30b815 100644
--- a/main.cpp
+++ b/main.cpp
@@ -22,16 +22,19 @@
#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) {
diff --git a/sforth/types.hpp b/sforth/types.hpp
index 3d26581..57c37c5 100644
--- a/sforth/types.hpp
+++ b/sforth/types.hpp
@@ -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;