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.

72 lines
2.1 KiB
C++

12 months ago
// 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.
#include "core.hpp"
#include "state.hpp"
//#include <csetjmp>
#include <cstring>
//static std::jmp_buf jmpbuf;
Cell *SP = DICT.data() + DICT.size() - DS;
Cell *RP = DICT.data() + DICT.size() - DS - RS;
FuncList IP = nullptr;
std::array<Cell, 2048> DICT;
Cell& HERE = DICT[DIdxHere];
Cell& LATEST = DICT[DIdxLatest];
Cell& STATE = DICT[DIdxState];
void executor(FuncList *list)
{
/*if (setjmp(jmpbuf) == 0)*/ {
// Execute the first bit of "word".
// If it is a "WordWrap", it will exit without changing IP.
// If it is a defined word, IP will be set to the word's body.
auto ip = (FuncList)list;
auto po = (void (**)(FuncList))*ip;
// Pass in po's location so the call can fetch beyond itself.
(*po)((FuncList)*ip);
while (IP) {
++IP;
auto po = (void (**)(FuncList))*IP;
(*po)((FuncList)*IP);
}
//std::longjmp(jmpbuf, 1);
}
}
void execute1(Word *word)
{
IP = 0;
executor(&word->list);
}
Word *find(const char *s, int len)
{
for (auto w = (Word *)LATEST; w; w = w->link) {
if (len == (int)strlen(w->name) && strncmp(s, w->name, len) == 0)
return w;
}
return nullptr;
}