aboutsummaryrefslogtreecommitdiffstats
path: root/source/state.cpp
blob: 03ad95d94664bbc1f472b5db9f291e2da68799f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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>

FuncList IP = nullptr;
std::array<Cell, DictSize> DICT;

Cell& BASE   = DICT[DIdxBase];
Cell& HERE   = DICT[DIdxHere];
Cell& LATEST = DICT[DIdxLatest];
Cell& STATE  = DICT[DIdxState];

static std::jmp_buf jmpbuf;
static Cell *SP = DICT.data() + DICT.size() - DS;
static Cell *RP = DICT.data() + DICT.size() - DS - RS;

void push(Cell value) {
    if (SP >= DICT.data() + DICT.size())
        std::longjmp(jmpbuf, static_cast<int>(Error::push));

    *++SP = value;
}

Cell pop() {
    if (SP - 1 < DICT.data() + DICT.size() - DS)
        std::longjmp(jmpbuf, static_cast<int>(Error::pop));

    return *SP--;
}

Cell *sp() {
    return SP;
}

void rpush(Cell value) {
    if (RP >= DICT.data() + DICT.size() - DS)
        std::longjmp(jmpbuf, static_cast<int>(Error::rpush));

    *++RP = value;
}

Cell rpop() {
    if (RP - 1 < DICT.data() + DICT.size() - DS - RS)
        std::longjmp(jmpbuf, static_cast<int>(Error::rpop));

    return *RP--;
}

Cell *rp() {
    return RP;
}

Error executor(FuncList *list)
{
    auto result = static_cast<Error>(setjmp(jmpbuf));
    FuncList body;

    if (static_cast<int>(result) == 0) {
        result = Error::none;

        // We are given the pointer to a list of function pointers.
        // Dereference once to retrieve the function pointer list.
        // We do not work with IP initially since it needs to be set to zero if
        // this is a top-level call/execution.
        body = *list;

        // Enter the execution loop.
        goto entry;

        // Execution continues so long as IP is not zero.
        // If this is a top-level execution of a pre-defined word, then IP will
        // remain zero'd and the loop will immediately exit.
        // If this is a defined word's execution, then its "call" will overwrite
        // IP (and push the initial zero-IP to the return stack); execution will
        // continue until we return to the zero-IP.
        while (IP) {
            // Retrieve next function pointer list.
            body = (FuncList)*++IP;
entry:
            // Dereference `body` to get the first function in the list.
            // This is casted to take a FuncList as an argument since defined
            // words need to know their addresses so that they can perform
            // their "calls".
            // If the word is pre-defined then the argument will simply be
            // ignored.
            auto func = (void (*)(FuncList))*body;
            func(body);
        }
    }

    return result;
}

Error execute1(Word *word)
{
    // IP must initially be zero if executing a word at the top level.
    IP = 0;
    return 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;
}