aboutsummaryrefslogtreecommitdiffstats
path: root/libalee/corewords.cpp
blob: b7681635e3e9ad81a15b84d2942905ccc9ddb5e0 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
 * 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 "alee.hpp"

#include <utility>

LIBALEE_SECTION
void find(State& state, Word word)
{
    Cell tok = 0;
    Cell imm = 0;

    if (auto j = state.dict.find(word); j > 0) {
        tok = state.dict.getexec(j);
        imm = (state.dict.read(j) & Dictionary::Immediate) ? 1 : -1;
    } else if (tok = CoreWords::findi(state, word); tok >= 0) {
        imm = (tok == CoreWords::Semicolon) ? 1 : -1;
    }

    state.push(tok);
    state.push(imm);
}

LIBALEE_SECTION
void CoreWords::run(Cell ins, State& state)
{
    Cell cell;
    DoubleCell dcell;

    Addr index = ins;

    auto& ip = state.ip();

    auto popd = [](State& s) {
        DoubleCell dcell = s.pop();
        dcell <<= sizeof(Cell) * 8;
        dcell |= static_cast<Addr>(s.pop());
        return dcell;
    };

    auto pushd = [](State& s, DoubleCell d) {
        s.push(static_cast<Cell>(d));
        s.push(static_cast<Cell>(d >> (sizeof(Cell) * 8)));
    };

execute:
    if (index >= Dictionary::Begin) {
        // must be calling a defined subroutine
        state.pushr(ip);
        ip = index;
        return;
    } else switch (index) {
    case 0: // _lit
        state.push(state.beyondip());
        break;
    case 1: // drop
        state.pop();
        break;
    case 2: // dup
        state.push(state.top());
        break;
    case 3: // swap
        std::swap(state.top(), state.pick(1));
        break;
    case 4: // pick
        state.push(state.pick(state.pop()));
        break;
    case 5: // sys
        user_sys(state);
        break;
    case 6: // add
        cell = state.pop();
        state.top() += cell;
        break;
    case 7: // sub
        cell = state.pop();
        state.top() -= cell;
        break;
    case 8: // mul ( n n -- d )
        cell = state.pop();
        dcell = state.pop() * cell;
        pushd(state, dcell);
        break;
    case 9: // div ( d n -- n )
        cell = state.pop();
        dcell = popd(state);
        state.push(static_cast<Cell>(dcell / cell));
        break;
    case 10: // mod ( d n -- n )
        cell = state.pop();
        dcell = popd(state);
        state.push(static_cast<Cell>(dcell % cell));
        break;
    case 11: // peek
        if (state.pop())
            state.push(state.dict.read(state.pop()));
        else
            state.push(state.dict.readbyte(state.pop()));
        break;
    case 12: // poke
        cell = state.pop();
        if (auto addr = state.pop(); cell)
            state.dict.write(addr, state.pop());
        else
            state.dict.writebyte(addr, state.pop() & 0xFFu);
        break;
    case 13: // pushr
        state.pushr(state.pop());
        break;
    case 14: // popr
        state.push(state.popr());
        break;
    case 15: // equal
        cell = state.pop();
        state.top() = state.top() == cell ? -1 : 0;
        break;
    case 16: // lt
        cell = state.pop();
        state.top() = state.top() < cell ? -1 : 0;
        break;
    case 17: // and
        cell = state.pop();
        state.top() &= cell;
        break;
    case 18: // or
        cell = state.pop();
        state.top() |= cell;
        break;
    case 19: // xor
        cell = state.pop();
        state.top() ^= cell;
        break;
    case 20: // shl
        cell = state.pop();
        reinterpret_cast<Addr&>(state.top()) <<= static_cast<Addr>(cell);
        break;
    case 21: // shr
        cell = state.pop();
        reinterpret_cast<Addr&>(state.top()) >>= static_cast<Addr>(cell);
        break;
    case 22: // colon
        state.push(state.dict.alignhere());
        state.dict.write(Dictionary::CompToken, state.top());
        while (!state.dict.hasInput())
            state.input();
        state.dict.addDefinition(state.dict.input());
        state.compiling(true);
        break;
    case 23: // tick
        while (!state.dict.hasInput())
            state.input();
        find(state, state.dict.input());
        break;
    case 24: // execute
        index = state.pop();
        goto execute;
    case 25: // exit
        ip = state.popr();
        state.verify(ip != 0, Error::exit);
        break;
    case 26: // semic
        state.dict.add(findi("exit"));
        state.compiling(false);

        cell = state.pop();
        dcell = cell - state.dict.latest();
        if (dcell > (1 << (sizeof(Cell) * 8 - 6)) - 1) {
            state.dict.write(static_cast<Addr>(cell) + sizeof(Cell), static_cast<Cell>(dcell));
            dcell = ((1 << (sizeof(Cell) * 8 - 6)) - 1);
        }
        state.dict.write(cell, (state.dict.read(cell) & 0x1F) | static_cast<Cell>(dcell << 6));
        state.dict.latest(cell);
        break;
    case 27: // _jmp0
        if (state.pop()) {
            state.beyondip();
            break;
        }
        [[fallthrough]];
    case 28: // _jmp
        ip = state.beyondip();
        return;
    case 29: // depth
        state.push(static_cast<Cell>(state.size()));
        break;
    case 30: // _rdepth
        state.push(static_cast<Cell>(state.rsize()));
        break;
    case 31: // _in
        state.input();
        break;
    case 32: // _ex
        {
        const auto st = state.save();
        ip = 0;
        Parser::parseSource(state);
        state.load(st);
        }
        break;
    case 33: // find
        cell = state.pop();
        find(state,
             Word::fromLength(static_cast<Addr>(cell + 1),
                              state.dict.readbyte(cell)));
        break;
    case 34: // _uma
        {
        const auto plus = state.pop();
        cell = state.pop();
        dcell = popd(state);
        dcell *= static_cast<Addr>(cell);
        dcell += static_cast<Addr>(plus);
        pushd(state, dcell);
        }
        break;
    case 35: // u<
        cell = state.pop();
        state.top() = static_cast<Addr>(state.top()) <
                      static_cast<Addr>(cell) ? -1 : 0;
        break;
    case 36: // um/mod
        cell = state.pop();
        dcell = popd(state);

        state.push(static_cast<Cell>(
            static_cast<DoubleAddr>(dcell) %
            static_cast<Addr>(cell)));
        state.push(static_cast<Cell>(
            static_cast<DoubleAddr>(dcell) /
            static_cast<Addr>(cell)));
        break;
    default:
        state.push(ins - WordCount);
        break;
    }

    ip += sizeof(Cell);
}

LIBALEE_SECTION
Cell CoreWords::findi(State& state, Word word)
{
    return findi(word.begin(&state.dict), word.size());
}