diff options
Diffstat (limited to 'source/state.hpp')
-rw-r--r-- | source/state.hpp | 94 |
1 files changed, 48 insertions, 46 deletions
diff --git a/source/state.hpp b/source/state.hpp index b6e5bcf..5667875 100644 --- a/source/state.hpp +++ b/source/state.hpp @@ -1,5 +1,5 @@ // sprit-forth: A portable subroutine-threaded Forth. -// Copyright (C) 2023 Clyne Sullivan <clyne@bitgloo.com> +// Copyright (C) 2024 Clyne Sullivan <clyne@bitgloo.com> // // This library is free software; you can redistribute it and/or modify it // under the terms of the GNU Library General Public License as published by @@ -34,58 +34,60 @@ constexpr Addr DIdxSrcLen = 6; constexpr Addr DIdxInBuf = 7; constexpr Addr DIdxBegin = DIdxInBuf + 80 * sizeof(char); -/** - * Memory chunk used to store the dictoinary and stacks. - */ -extern std::array<Cell, DictSize> DICT; +class State +{ + /** + * Memory chunk used to store the dictoinary and stacks. + */ + std::array<Cell, DictSize> dict; + std::array<Cell, DS> ds; + std::array<Cell, RS> rs; -extern Cell& BASE; -extern Cell& HERE; /** Linked to HERE's storage in DICT. */ -extern Cell& LATEST; /** Linked to LATEST's storage in DICT. */ -extern Cell& STATE; /** Linked to STATE's storage in DICT. */ -extern FuncList IP; /** Instruction pointer */ +public: + Cell *sp = ds.begin(); + Cell *rp = rs.begin(); + Cell& base = dict[DIdxBase]; + Cell& here = dict[DIdxHere]; + Word *& latest = *(Word **)&dict[DIdxLatest]; + Cell& state = dict[DIdxState]; + Cell& compxt = dict[DIdxCompXt]; + Cell& source = dict[DIdxSource]; + Cell& sourceu = dict[DIdxSrcLen]; + unsigned char& sourcei = *(unsigned char *)&dict[DIdxInBuf]; -void push(Cell); -[[nodiscard]] Cell pop(); -[[nodiscard]] Cell *sp(); + State() { + dict[DIdxBase] = 10; + dict[DIdxHere] = (Cell)&dict[DIdxBegin]; + dict[DIdxLatest] = 0;//(Cell)&wordset.back(); + dict[DIdxState] = 0; + } -void rpush(Cell); -[[nodiscard]] Cell rpop(); -[[nodiscard]] Cell *rp(); + void add(Word& word) noexcept { + word.link = latest; + latest = &word; + } -/** - * Initializes the dictionary to default values. - * @param wordset The initial WordSet of pre-defined words. - */ -inline void initialize(const auto& wordset) -{ - DICT[DIdxBase] = 10; - DICT[DIdxHere] = (Cell)&DICT[DIdxBegin]; - DICT[DIdxLatest] = (Cell)wordset.latest; - DICT[DIdxState] = 0; -} + auto dictdata() noexcept { + return dict.data(); + } + + void push(Cell); + [[nodiscard]] Cell pop(); -/** - * Begins execution with the given function pointer list. - * @param list Function pointer list to execute - */ -[[nodiscard]] -Error executor(FuncList *list); + void rpush(Cell); + [[nodiscard]] Cell rpop(); -/** - * Executes the given word by calling executor on its definition. - * @param word The word to execute - */ -[[nodiscard]] -Error execute1(Word *word); + /** + * Looks up the definition of the given word. + * @param s The name of the word to find + * @param len The character count of the word's name + * @return Pointer to the word's definition or nullptr if not found + */ + [[nodiscard]] + Word *find(const char *s, int len); +}; -/** - * Looks up the definition of the given word. - * @param s The name of the word to find - * @param len The character count of the word's name - * @return Pointer to the word's definition or nullptr if not found - */ -Word *find(const char *s, int len); +extern State Forth; #endif // STATE_HPP |