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.

94 lines
2.7 KiB
C++

// sprit-forth: A portable subroutine-threaded Forth.
// 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
// 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);
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;
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];
State() {
dict[DIdxBase] = 10;
dict[DIdxHere] = (Cell)&dict[DIdxBegin];
dict[DIdxLatest] = 0;//(Cell)&wordset.back();
dict[DIdxState] = 0;
}
void add(Word& word) noexcept {
word.link = latest;
latest = &word;
}
auto dictdata() noexcept {
return dict.data();
}
void push(Cell);
[[nodiscard]] Cell pop();
void rpush(Cell);
[[nodiscard]] Cell rpop();
/**
* 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);
};
extern State Forth;
#endif // STATE_HPP