]> code.bitgloo.com Git - clyne/sforth.git/commitdiff
add base
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 26 Nov 2024 12:48:52 +0000 (07:48 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 26 Nov 2024 12:48:52 +0000 (07:48 -0500)
core.fth
forth.hpp
main.cpp

index 1a6f1dd6b755f0f5bdc0ebf7a6f5caa70468f162..ed28e1f252746de5b481ef821829eb50bc770626 100644 (file)
--- a/core.fth
+++ b/core.fth
@@ -16,6 +16,7 @@
 : ip        [ _d cell+ cell+ ] literal ;
 : here      dp @ ;
 : unused    [ _d 8 cells + ] literal @ here - ;
+: base      [ _d 9 cells + ] literal ;
 : latest    [ _d 4 cells + ] literal @ ;
 
 \ : dup       sp@ @ ;
 : 1+        1 + ;
 : 1-        1 - ;
 
-: if       ['] _jmp0 , here 0 , ; immediate
-: then     here swap ! ; immediate
-: else     ['] _jmp , here 0 , swap here swap ! ; immediate
+: if        ['] _jmp0 , here 0 , ; immediate
+: then      here swap ! ; immediate
+: else      ['] _jmp , here 0 , swap here swap ! ; immediate
+
+: decimal   10 base ! ;
+: hex       16 base ! ;
 
index a9c55df2e9f75df3382bae95edd7737e6b0825d9..4aad2634a5430a73a6acd112c5285b157fc24c10 100644 (file)
--- a/forth.hpp
+++ b/forth.hpp
@@ -172,7 +172,8 @@ struct forth
 
             if (auto ent = get(word); !ent) {
                 cell n;
-                const auto [p, e] = std::from_chars(word.cbegin(), word.cend(), n);
+                const auto [p, e] = std::from_chars(word.cbegin(), word.cend(),
+                    n, base);
 
                 assert<error::word_not_found>(e == std::errc() && p == word.cend());
 
@@ -356,6 +357,7 @@ struct forth
     std::size_t sourcei = npos;
     cell compiling = false;
     cell *end = nullptr;
+    cell base = 10;
     std::array<cell, data_size> dstack;
     std::array<cell, return_size> rstack;
 };
@@ -369,6 +371,7 @@ static_assert(offsetof(forth, source)    == 5 * sizeof(forth::cell));
 static_assert(offsetof(forth, sourcei)   == 6 * sizeof(forth::cell));
 static_assert(offsetof(forth, compiling) == 7 * sizeof(forth::cell));
 static_assert(offsetof(forth, end)       == 8 * sizeof(forth::cell));
+static_assert(offsetof(forth, base)      == 9 * sizeof(forth::cell));
 
 #endif // SFORTH_HPP
 
index d9cb5c5aeb50e2bdc634e9d730f18e76deea4d03..2788b49262e779229d0e433565ea573f7ca78c02 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -32,7 +32,11 @@ int main(int argc, const char *argv[])
     std::span args (argv + 1, argc - 1);
 
     forth::initialize<&fth>(dict.end());
-    fth->add(".", [](auto) { std::cout << fth->pop() << ' '; });
+    fth->add(".", [](auto) {
+        char buf[32];
+        std::to_chars(buf, buf + sizeof(buf), fth->pop(), fth->base);
+        std::cout << buf << ' ';
+    });
     fth->add("emit", [](auto) { std::cout << static_cast<char>(fth->pop()); });
 
     for (auto arg : args) {