// sprit-forth: A portable subroutine-threaded Forth. // Copyright (C) 2023 Clyne Sullivan // // 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 // the Free Software Foundation; either version 2 of the License, or (at your // option) any later version. // // This library is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for // more details. // // You should have received a copy of the GNU Library General Public License // along with this library; if not, write to the Free Software Foundation, Inc., // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. #ifndef STATE_HPP #define STATE_HPP #include "types.hpp" constexpr Addr DS = 16; /** Data stack size */ constexpr Addr RS = 16; /** Return stack size */ constexpr auto DictSize = 2048u; /** Dictionary size */ constexpr Addr DIdxBase = 0; constexpr Addr DIdxHere = 1; constexpr Addr DIdxLatest = 2; constexpr Addr DIdxState = 3; constexpr Addr DIdxCompXt = 4; constexpr Addr DIdxSource = 5; 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 DICT; 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 */ void push(Cell); [[nodiscard]] Cell pop(); [[nodiscard]] Cell *sp(); void rpush(Cell); [[nodiscard]] Cell rpop(); [[nodiscard]] Cell *rp(); /** * 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; } /** * Begins execution with the given function pointer list. * @param list Function pointer list to execute */ [[nodiscard]] Error executor(FuncList *list); /** * 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 */ Word *find(const char *s, int len); #endif // STATE_HPP