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.

182 lines
4.0 KiB
C++

10 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 <cctype>
#include <cstring>
10 months ago
void jump(FuncList ip)
{
// IP is incremented before its next execution.
10 months ago
IP = ip - 1;
}
// LITERAL's run-time semantics: push the given value onto the stack.
10 months ago
static auto literall = WordWrap<[] {
10 months ago
push((Cell)*++IP);
10 months ago
}>;
10 months ago
void compileliteral()
{
comma((Cell)literall);
10 months ago
comma(pop());
10 months ago
}
bool haskey()
{
7 months ago
return *((char *)&DICT[DIdxInBuf]) < DICT[DIdxSrcLen];
10 months ago
}
void addkey(int k)
{
7 months ago
auto addr = DICT[DIdxSource] + (DICT[DIdxSrcLen]++);
auto ptr = reinterpret_cast<char *>(addr);
10 months ago
*ptr = static_cast<char>(k);
}
int key()
{
// Block until input is available.
10 months ago
while (!haskey())
getinput();
auto ptr = reinterpret_cast<char *>(DICT[DIdxSource]);
7 months ago
int idx = (*((char *)&DICT[DIdxInBuf]))++;
return ptr[idx];
10 months ago
}
Cell *comma(Cell n)
{
const auto ptr = reinterpret_cast<Cell *>(HERE);
*ptr = n;
HERE += sizeof(Cell);
return ptr;
}
Addr aligned(Addr addr)
{
return (addr + (sizeof(Cell) - 1)) & ~(sizeof(Cell) - 1);
}
void align()
{
HERE = aligned(HERE);
}
10 months ago
static void readword(int ch)
10 months ago
{
int k;
do {
k = key();
} while (k == ch);
10 months ago
// Collect the word's text.
10 months ago
char *ptr;
do {
ptr = reinterpret_cast<char *>(HERE);
*ptr = k;
++HERE;
if (!haskey())
break;
k = key();
} while (k != ch);
// Add a null terminator.
10 months ago
ptr = reinterpret_cast<char *>(HERE);
*ptr = '\0';
++HERE;
}
10 months ago
void word()
{
auto here = (char *)HERE;
++HERE;
10 months ago
readword(*sp());
here[0] = strlen(here + 1);
HERE = (Cell)here;
10 months ago
*sp() = HERE;
}
10 months ago
void colon()
{
// Collect (and store) the word's name.
10 months ago
align();
auto name = HERE;
10 months ago
readword(' ');
10 months ago
align();
// Build the Word structure.
10 months ago
comma(HERE + 4 * sizeof(Cell)); // exec ptr
comma(name); // name ptr
10 months ago
push((Cell)comma(0)); // link (to be set by semic())
comma(0); // immediate
// The word's execution begins with a prologue that technically performs
// the "call" to this word.
// By including this in the word's definition, execution can avoid caring
// about if it is running words or routines (i.e. pre-defined words).
10 months ago
comma((Cell)+[](FuncList *ip) {
++ip;
10 months ago
rpush((Cell)IP);
10 months ago
jump((FuncList)*ip);
});
// The actual function list will begin one Cell beyond here.
10 months ago
comma(HERE + sizeof(Cell));
10 months ago
DICT[DIdxCompXt] = *sp() - 2 * sizeof(Cell);
// Enter compiling state.
10 months ago
STATE = -1;
}
void semic()
{
// Add exit routine.
10 months ago
comma((Cell)fexit);
// Complete the new word's linkage to make it usable.
10 months ago
auto link = (Cell *)pop();
10 months ago
*link = LATEST;
LATEST = (Cell)(link - 2);
// Exit compilation state.
10 months ago
STATE = 0;
}
// TODO define in Forth? ": ' bl word find drop ;"
10 months ago
void tick()
{
// Get the name to look up.
10 months ago
auto name = (char *)HERE;
10 months ago
readword(' ');
10 months ago
// Look up the name and push the result.
10 months ago
int len = HERE - (Cell)name - 1;
auto word = find(name, len);
10 months ago
push((Cell)word);
10 months ago
// Deallocate `name`.
10 months ago
HERE = (Cell)name;
}