You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.2 KiB
C++
86 lines
2.2 KiB
C++
// sprit-forth: A portable subroutine-threaded Forth.
|
|
// Copyright (C) 2023 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
|
|
// 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 <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
|
|
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<typename... Words>
|
|
struct WordSet
|
|
{
|
|
std::array<Word, sizeof...(Words)> 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... funcs>
|
|
auto WordWrap = [] {
|
|
constexpr static Func list[1] = {
|
|
+[] { (funcs(), ...); }
|
|
};
|
|
|
|
return list;
|
|
};
|
|
|
|
#endif // TYPES_HPP
|
|
|