From 092002a49f6a4a59200eb674cfad6657890d6ce3 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 24 Nov 2023 16:23:40 -0500 Subject: initial upload --- source/types.hpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 source/types.hpp (limited to 'source/types.hpp') diff --git a/source/types.hpp b/source/types.hpp new file mode 100644 index 0000000..03e9f2a --- /dev/null +++ b/source/types.hpp @@ -0,0 +1,85 @@ +// 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 + -- cgit v1.2.3