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
|
/**
* 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/>.
*/
#include "corewords.hpp"
#include "ctype.hpp"
#include "parser.hpp"
Error (*Parser::customParse)(State&, Word) = nullptr;
Error Parser::parse(State& state, const char *str)
{
auto addr = Dictionary::Input;
const auto len = static_cast<Cell>(strlen(str));
state.dict.write(addr, 0);
state.dict.write(Dictionary::SourceLen, len);
addr += sizeof(Cell);
while (*str)
state.dict.writebyte(addr++, *str++);
while (addr < Dictionary::Input + Dictionary::InputCells)
state.dict.writebyte(addr++, '\0');
return parseSource(state);
}
Error Parser::parseSource(State& state)
{
auto err = Error::none;
while (err == Error::none && state.dict.hasInput())
err = parseWord(state, state.dict.input());
return err;
}
Error Parser::parseWord(State& state, Word word)
{
bool imm;
Addr ins = state.dict.find(word);
if (ins == 0) {
auto cw = CoreWords::findi(state, word);
if (cw < 0) {
auto r = parseNumber(state, word);
if (r != Error::none)
return customParse ? customParse(state, word) : r;
else
return r;
} else {
ins = cw;
imm = ins == CoreWords::Semicolon;
}
} else {
imm = state.dict.read(ins) & Dictionary::Immediate;
ins = state.dict.getexec(ins);
}
if (state.compiling() && !imm)
state.dict.add(ins);
else if (auto stat = state.execute(ins); stat != Error::none)
return stat;
return Error::none;
}
Error Parser::parseNumber(State& state, Word word)
{
const auto base = state.dict.read(Dictionary::Base);
DoubleCell result = 0;
auto it = word.begin(&state.dict);
bool inv = *it == '-';
if (inv)
++it;
const auto end = word.end(&state.dict);
for (uint8_t c; it != end; ++it) {
c = *it;
if (isdigit(c)) {
result *= base;
result += c - '0';
} else if (isalpha(c) && base > 10) {
result *= base;
result += 10 + c - (isupper(c) ? 'A' : 'a');
} else {
return Error::noword;
}
}
if (inv)
result *= -1;
processLiteral(state, static_cast<Cell>(result));
return Error::none;
}
void Parser::processLiteral(State& state, Cell value)
{
if (state.compiling()) {
constexpr auto ins = CoreWords::findi("_lit");
const Cell maxlit = Dictionary::Begin - CoreWords::WordCount;
if (value >= 0 && value < maxlit)
value += CoreWords::WordCount;
else
state.dict.add(ins);
state.dict.add(value);
} else {
state.push(value);
}
}
|