diff options
Diffstat (limited to 'source/core.cpp')
-rw-r--r-- | source/core.cpp | 88 |
1 files changed, 43 insertions, 45 deletions
diff --git a/source/core.cpp b/source/core.cpp index e2da109..437c700 100644 --- a/source/core.cpp +++ b/source/core.cpp @@ -1,5 +1,5 @@ // sprit-forth: A portable subroutine-threaded Forth. -// Copyright (C) 2023 Clyne Sullivan <clyne@bitgloo.com> +// 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 @@ -16,36 +16,35 @@ // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. #include "core.hpp" -#include "state.hpp" #include <cctype> #include <cstring> +static State state; + void jump(FuncList ip) { // IP is incremented before its next execution. - IP = ip - 1; + Exec.ip = ip - 1; } -// LITERAL's run-time semantics: push the given value onto the stack. -static auto literall = WordWrap<[] { - push((Cell)*++IP); -}>; - void compileliteral() { - comma((Cell)literall); - comma(pop()); + // LITERAL's run-time semantics: push the given value onto the stack. + comma((Cell)WordWrap<[] { + Forth.push((Cell)*++Exec.ip); + }>); + comma(Forth.pop()); } bool haskey() { - return *((char *)&DICT[DIdxInBuf]) < DICT[DIdxSrcLen]; + return Forth.sourcei < Forth.sourceu; } void addkey(int k) { - auto addr = DICT[DIdxSource] + (DICT[DIdxSrcLen]++); + auto addr = Forth.source + Forth.sourceu++; auto ptr = reinterpret_cast<char *>(addr); *ptr = static_cast<char>(k); } @@ -56,16 +55,16 @@ int key() while (!haskey()) getinput(); - auto ptr = reinterpret_cast<char *>(DICT[DIdxSource]); - int idx = (*((char *)&DICT[DIdxInBuf]))++; + auto ptr = reinterpret_cast<char *>(Forth.source); + int idx = Forth.sourcei++; return ptr[idx]; } Cell *comma(Cell n) { - const auto ptr = reinterpret_cast<Cell *>(HERE); + const auto ptr = reinterpret_cast<Cell *>(Forth.here); *ptr = n; - HERE += sizeof(Cell); + Forth.here += sizeof(Cell); return ptr; } @@ -76,7 +75,7 @@ Addr aligned(Addr addr) void align() { - HERE = aligned(HERE); + Forth.here = aligned(Forth.here); } static void readword(int ch) @@ -89,9 +88,9 @@ static void readword(int ch) // Collect the word's text. char *ptr; do { - ptr = reinterpret_cast<char *>(HERE); + ptr = reinterpret_cast<char *>(Forth.here); *ptr = k; - ++HERE; + ++Forth.here; if (!haskey()) break; @@ -100,36 +99,37 @@ static void readword(int ch) } while (k != ch); // Add a null terminator. - ptr = reinterpret_cast<char *>(HERE); + ptr = reinterpret_cast<char *>(Forth.here); *ptr = '\0'; - ++HERE; + ++Forth.here; } void word() { - auto here = (char *)HERE; - ++HERE; + auto here = (char *)Forth.here; + ++Forth.here; - readword(*sp()); + readword(*Forth.sp); here[0] = strlen(here + 1); - HERE = (Cell)here; - *sp() = HERE; + Forth.here = (Cell)here; + *Forth.sp = Forth.here; } void colon() { // Collect (and store) the word's name. align(); - auto name = HERE; + auto name = Forth.here; readword(' '); align(); // Build the Word structure. - comma(HERE + 4 * sizeof(Cell)); // exec ptr - comma(name); // name ptr - push((Cell)comma(0)); // link (to be set by semic()) - comma(0); // immediate + Forth.push(Forth.here); + comma(Forth.here + 4 * sizeof(Cell)); // exec ptr + comma(name); // name ptr + comma(0); // link + comma(0); // immediate // The word's execution begins with a prologue that technically performs // the "call" to this word. @@ -137,16 +137,15 @@ void colon() // about if it is running words or routines (i.e. pre-defined words). comma((Cell)+[](FuncList *ip) { ++ip; - rpush((Cell)IP); + Forth.rpush((Cell)Exec.ip); jump((FuncList)*ip); }); // The actual function list will begin one Cell beyond here. - comma(HERE + sizeof(Cell)); - - DICT[DIdxCompXt] = *sp() - 2 * sizeof(Cell); + comma(Forth.here + sizeof(Cell)); // Enter compiling state. - STATE = -1; + Forth.compxt = *Forth.sp; + Forth.state = -1; } void semic() @@ -155,27 +154,26 @@ void semic() comma((Cell)fexit); // Complete the new word's linkage to make it usable. - auto link = (Cell *)pop(); - *link = LATEST; - LATEST = (Cell)(link - 2); + auto word = reinterpret_cast<Word *>(Forth.pop()); + Forth.add(*word); // Exit compilation state. - STATE = 0; + Forth.state = 0; } // TODO define in Forth? ": ' bl word find drop ;" void tick() { // Get the name to look up. - auto name = (char *)HERE; + auto name = Forth.here; readword(' '); // Look up the name and push the result. - int len = HERE - (Cell)name - 1; - auto word = find(name, len); - push((Cell)word); + int len = Forth.here - name - 1; + auto word = Forth.find((char *)name, len); + Forth.push((Cell)word); // Deallocate `name`. - HERE = (Cell)name; + Forth.here = name; } |