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
130
131
132
133
134
135
136
137
138
139
|
//
/// @file types.hpp
/// @brief Defines common types used throughout Alee Forth.
//
// Alee Forth: A portable and concise Forth implementation in modern C++.
// Copyright (C) 2023 Clyne Sullivan <clyne@bitgloo.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef ALEEFORTH_TYPES_HPP
#define ALEEFORTH_TYPES_HPP
#include <cstdint>
#include <iterator>
/** Dictionary address type. Must be equivalent to "unsigned Cell". */
using Addr = uint16_t;
/** Data cell type. Dictionary is basically an array of this type. */
using Cell = int16_t;
/** Double-width cell type. Must be twice the size of Cell. */
using DoubleCell = int32_t;
/** Double-width addr type. Must be twice the size of Addr. Used by um/mod. */
using DoubleAddr = uint32_t;
struct Dictionary;
struct State;
/**
* Error enum to report success or failure of parsing or execution.
*/
enum class Error : int {
none = 0, /** No error */
push, /** Could not push (data stack overflow) */
pop, /** Could not pop (data stack underflow) */
pushr, /** Could not push (return stack overflow) */
popr, /** Could not pop (return stack underflow) */
top, /** Could not fetch data stack top (data stack underflow) */
pick, /** Could not pick data stack value (data stack underflow) */
exit, /** No error, exited from State::execute() */
noword /** Parsing failed because the word was not found */
};
/**
* @class Word
* Stores the beginning and end indices of a dictionary-defined word.
*/
class Word
{
/** Beginning (inclusive) index */
Addr start;
/** End (exclusive) index */
Addr wend;
public:
struct iterator;
/**
* Constructs a word with the given start and end indices.
* @param s Start/beginning index
* @param e (past-the-)end index
*/
constexpr explicit Word(Addr s = 0, Addr e = 0):
start(s), wend(e) {}
/**
* Constructs a word using beginning index and length.
* @param s Beginning/start index of word
* @param l Count of bytes until end of word
* @return Resulting Word object
*/
LIBALEE_SECTION
static constexpr Word fromLength(Addr s, Addr l) {
return Word(s, s + l);
}
/** Returns the size of this word in bytes. */
Addr size() const noexcept;
/**
* Creates a beginning iterator for the word.
* @param dict Pointer to dictionary object containing this word
* @return Iterator pointing to this word's beginning
*/
iterator begin(const Dictionary *dict);
/**
* Creates an end iterator for the word.
* @param dict Pointer to dictionary object containing this word
* @return Iterator pointing to past-the-end of this word
*/
iterator end(const Dictionary *dict);
/**
* Forward-iterator for iterating through the letters of this word.
*/
struct iterator {
using iterator_category = std::input_iterator_tag;
using value_type = uint8_t;
using pointer = void;
using reference = void;
using difference_type = Cell;
/** Iterator's current address within its containing dictionary. */
Addr addr;
/** Pointer to dictionary that contains this word. */
const Dictionary *dict;
/**
* Constructs a word iterator.
* @param a The address the iterator points to
* @param d The dictionary that contains this word
*/
constexpr iterator(Addr a, const Dictionary *d):
addr(a), dict(d) {}
/** Prefix increment */
iterator& operator++();
/** Postfix increment */
iterator operator++(int);
/** Returns value pointed to by iterator */
value_type operator*();
/** Iterator comparison function (case-insensitive) */
bool operator!=(const iterator&);
};
};
#endif // ALEEFORTH_TYPES_HPP
|