]> code.bitgloo.com Git - clyne/sforth.git/commitdiff
if else then
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 26 Nov 2024 12:41:10 +0000 (07:41 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 26 Nov 2024 12:41:10 +0000 (07:41 -0500)
core.fth
forth.hpp

index 3f850b1bf517992892f2bca42b229cd0a9da0fe0..1a6f1dd6b755f0f5bdc0ebf7a6f5caa70468f162 100644 (file)
--- a/core.fth
+++ b/core.fth
 : +!        dup >r swap r> @ + swap ! ;
 : allot     dp +! ;
 : ,         here ! cell allot ;
+: [']       ' [ ' literal , ] ; immediate
 
 : 1+        1 + ;
 : 1-        1 - ;
 
+: if       ['] _jmp0 , here 0 , ; immediate
+: then     here swap ! ; immediate
+: else     ['] _jmp , here 0 , swap here swap ! ; immediate
+
index 3873c68639409ce6889ac7fb78c28ab830703098..a9c55df2e9f75df3382bae95edd7737e6b0825d9 100644 (file)
--- a/forth.hpp
+++ b/forth.hpp
@@ -282,6 +282,16 @@ struct forth
         auto f_semic = [](auto) { *fth.here++ = 0; fth.compiling = false; };
         auto f_comm  = [](auto) { fth.sourcei = npos; };
         auto f_cell  = [](auto) { fth.push(sizeof(cell)); };
+        auto f_jmp   = [](auto) {
+            auto ptr = ++fth.ip;
+            fth.ip = *reinterpret_cast<func **>(ptr) - 1;
+        };
+        auto f_jmp0  = [](auto) {
+            auto ptr = ++fth.ip;
+
+            if (fth.pop() == 0)
+                fth.ip = *reinterpret_cast<func **>(ptr) - 1;
+        };
 
         constexpr static word w_dict {"_d", f_dict};
         constexpr static word w_add {"+", f_add, &w_dict};
@@ -308,8 +318,10 @@ struct forth
         constexpr static word w_semic {";", f_semic, &w_colon, word_base::immediate};
         constexpr static word w_comm {"\\", f_comm, &w_semic, word_base::immediate};
         constexpr static word w_cell {"cell", f_cell, &w_comm};
+        constexpr static word w_jmp {"_jmp", f_jmp, &w_cell};
+        constexpr static word w_jmp0 {"_jmp0", f_jmp0, &w_jmp};
 
-        fth.latest = &w_cell;
+        fth.latest = &w_jmp0;
         fth.end = end_value;
     }