diff options
Diffstat (limited to 'dictionary.hpp')
-rw-r--r-- | dictionary.hpp | 62 |
1 files changed, 9 insertions, 53 deletions
diff --git a/dictionary.hpp b/dictionary.hpp index 880b8a5..add7fc3 100644 --- a/dictionary.hpp +++ b/dictionary.hpp @@ -24,67 +24,23 @@ #include <cstddef> #include <string_view> -struct Dictionary +class Dictionary { +public: Addr here = 1; Addr latest = 0; virtual Cell read(Addr) const = 0; virtual int write(Addr, Cell) = 0; - Addr allot(Cell amount) { - Addr old = here; - here += amount; - return old; - } + Addr allot(Cell); + void add(Cell); + void addDefinition(std::string_view); + Addr find(std::string_view); + Addr getexec(Addr); - void add(Cell value) { - write(here++, value); - } - - void addDefinition(std::string_view str) { - add(str.size()); - for (char c : str) - add(c); - - if (here & 1) - allot(1); - } - - bool issame(Addr addr, std::string_view str, std::size_t n) { - if (str.size() != n) - return false; - - for (char c : str) { - if (read(addr++) != c) - return false; - } - - return true; - } - - Addr find(std::string_view str) { - if (latest == 0) - return 0; - - auto lt = latest; - do { - const auto l = read(lt); - const auto len = l & 0x1F; - - if (issame(lt + 1, str, len)) - return lt; - else - lt -= l >> 6; - } while (lt); - - return 0; - } - - Addr getexec(Addr addr) { - const auto len = read(addr) & 0x1F; - return ((addr + 1 + len) + 1) & ~1; - } +private: + bool issame(Addr, std::string_view, std::size_t); }; #endif // ALEEFORTH_DICTIONARY_HPP |