// 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 TYPES_HPP #define TYPES_HPP #include #include #include using Cell = intptr_t; using Addr = uintptr_t; using Func = void (*)(); using FuncList = Func const *; static_assert(sizeof(Cell) == sizeof(Addr)); static_assert(sizeof(Cell) == sizeof(Func)); struct Word { FuncList list; const char *name; Word *link = nullptr; Cell imm = 0; constexpr Word(const char *n, FuncList l): list(l), name(n) {} constexpr Word& markImmediate() noexcept { imm = -1; return *this; } constexpr bool immediate() const noexcept { return imm; } }; static_assert(offsetof(Word, list) == 0); static_assert(offsetof(Word, name) == 1 * sizeof(Cell)); static_assert(offsetof(Word, link) == 2 * sizeof(Cell)); static_assert(offsetof(Word, imm) == 3 * sizeof(Cell)); static_assert(sizeof(Word) == 4 * sizeof(Cell)); template struct WordSet { std::array words; Word *latest; constexpr WordSet(Words... ws): words {ws...} { auto it = words.begin(); while (++it != words.end()) it->link = it - 1; latest = &*words.rbegin(); } }; template auto WordWrap = [] { constexpr static Func list[1] = { +[] { (funcs(), ...); } }; return list; }; #endif // TYPES_HPP