diff --git a/corewords.cpp b/corewords.cpp index 6aac65e..d016ba1 100644 --- a/corewords.cpp +++ b/corewords.cpp @@ -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; diff --git a/corewords.hpp b/corewords.hpp index 7edc139..0b84b0e 100644 --- a/corewords.hpp +++ b/corewords.hpp @@ -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