aboutsummaryrefslogtreecommitdiffstats
path: root/source/state.hpp
blob: 17d8e694ddcd293f4913df2f56d7a73b0d0dd376 (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
// 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.

#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 = 4096u; /** 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);

/**
 * Memory chunk used to store the dictoinary and stacks.
 */
extern std::array<Cell, DictSize> DICT;

extern Cell& HERE;   /** Linked to HERE's storage in DICT. */
extern Cell& LATEST; /** Linked to LATEST's storage in DICT. */
extern Cell& STATE;  /** Linked to STATE's storage in DICT. */

extern Cell *SP;    /** Data stack pointer */
extern Cell *RP;    /** Return stack pointer */
extern FuncList IP; /** Instruction pointer */

/**
 * Initializes the dictionary to default values.
 * @param wordset The initial WordSet of pre-defined words.
 */
inline void initialize(const auto& wordset)
{
    DICT[DIdxBase]   = 10;
    DICT[DIdxHere]   = (Cell)&DICT[DIdxBegin];
    DICT[DIdxLatest] = (Cell)wordset.latest;
    DICT[DIdxState]  = 0;
}

/**
 * Begins execution with the given function pointer list.
 * @param list Function pointer list to execute
 */
void executor(FuncList *list);

/**
 * Executes the given word by calling executor on its definition.
 * @param word The word to execute
 */
void execute1(Word *word);

/**
 * 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
 */
Word *find(const char *s, int len);

#endif // STATE_HPP