]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
add else
authorClyne Sullivan <clyne@bitgloo.com>
Fri, 10 Feb 2023 01:30:09 +0000 (20:30 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Fri, 10 Feb 2023 01:30:09 +0000 (20:30 -0500)
corewords.cpp
corewords.hpp

index 6aac65eb75adaa1107ea958a8ab518fcb8327ccf..d016ba1a6a8a5ae929cd5771140b3a7371e6254e 100644 (file)
@@ -52,8 +52,9 @@ Func CoreWords::get(int index)
     case 28: return op_const;
     case 29: return op_if;
     case 30: return op_then;
-    case 31: return op_literal;
-    case 32: return op_jump;
+    case 31: return op_else;
+    case 32: return op_literal;
+    case 33: return op_jump;
     default: return nullptr;
     }
 }
@@ -273,7 +274,22 @@ int CoreWords::op_then(State& state)
 {
     if (state.compiling) {
         const auto ifaddr = state.pop();
+        if (state.dict.read(ifaddr) == 0)
+            state.dict.write(ifaddr, state.dict.here);
+    }
+
+    return 0;
+}
+
+int CoreWords::op_else(State& state)
+{
+    if (state.compiling) {
+        const auto ifaddr = state.pop();
+        state.push(state.dict.here);
+        state.dict.add(0);
         state.dict.write(ifaddr, state.dict.here);
+    } else {
+        state.ip = state.beyondip() - 1;
     }
 
     return 0;
index 7edc1398762c798b0a6a1ddf5bef6c6f3b151e64..0b84b0e9495d7d741316cbcc7ea416598b937ff6 100644 (file)
@@ -27,7 +27,7 @@ int user_sys(State&);
 class CoreWords
 {
 public:
-    constexpr static std::size_t VisibleWordCount = 31;             // size
+    constexpr static std::size_t VisibleWordCount = 32;             // size
     constexpr static auto HiddenWordLiteral = VisibleWordCount;     // index
     constexpr static auto HiddenWordJump    = VisibleWordCount + 1; // index
     constexpr static auto WordCount         = HiddenWordJump + 1;   // size
@@ -47,7 +47,7 @@ private:
         "=\0<\0allot\0&\0|\0"
         "^\0<<\0>>\0(\0:\0"
         ";\1here\0imm\0const\0"
-       "if\1then\1";
+       "if\1then\1else\1";
 
     static Func get(int);
 
@@ -84,6 +84,7 @@ private:
     static int op_jump(State&);
     static int op_if(State&);
     static int op_then(State&);
+    static int op_else(State&);
 };
 
 #endif // ALEEFORTH_COREWORDS_HPP