-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.cpp
More file actions
439 lines (366 loc) · 10.2 KB
/
Copy pathcontrol.cpp
File metadata and controls
439 lines (366 loc) · 10.2 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//-----------------------------------------------------------------------------
// C++ implementation of a Forth interpreter
// Copyright (c) Paulo Custodio, 2020-2026
// License: GPL3 https://www.gnu.org/licenses/gpl-3.0.html
//-----------------------------------------------------------------------------
#include "control.h"
#include "dict.h"
#include "errors.h"
#include "locals.h"
#include "vm.h"
#include <vector>
void f_colon() {
if (cs_ddepth() > 0) {
error(Error::CompilerNesting);
}
vm.locals.clear();
cs_dpush(mk_dcell(POS_COLON_START, 0));
vm.dict.parse_create(idXDOCOL, F_SMUDGE);
vm.user->STATE = STATE_COMPILE;
if (vm.user->TRACE) {
vm.cs_stack.print_debug();
}
}
void f_colon_noname() {
if (cs_ddepth() > 0) {
error(Error::CompilerNesting);
}
vm.locals.clear();
cs_dpush(mk_dcell(POS_COLON_START, 0));
vm.dict.create("", F_SMUDGE, idXDOCOL);
Header* header = reinterpret_cast<Header*>(
mem_char_ptr(vm.latest_word));
vm.user->STATE = STATE_COMPILE;
push(header->xt());
if (vm.user->TRACE) {
vm.cs_stack.print_debug();
}
}
void f_semicolon() {
if (cs_ddepth() != 1) {
error(Error::CompilerNesting);
}
dint pos_addr = cs_dpeek();
if (dcell_hi(pos_addr) != POS_COLON_START) {
error(Error::CompilerNesting);
}
cs_dpop();
vm.locals.clear();
comma(xtEXIT);
Header* header = reinterpret_cast<Header*>(
mem_char_ptr(vm.latest_word));
header->flags.smudge = false;
vm.user->STATE = STATE_INTERPRET;
if (vm.user->TRACE) {
vm.cs_stack.print_debug();
}
}
void f_recurse() {
Header* header = reinterpret_cast<Header*>(
mem_char_ptr(vm.latest_word));
comma(header->xt());
}
static void comma_fwd_jump(uint xt_jump, int pos) {
if (pos != POS_IF_FWD &&
pos != POS_ELSE_FWD &&
pos != POS_WHILE_FWD &&
pos != POS_DO_FWD &&
pos != POS_LEAVE_FWD &&
pos != POS_OF_FWD &&
pos != POS_ENDOF_FWD) {
error(Error::ControlStructureMismatch);
}
comma(xt_jump);
cs_dpush(mk_dcell(pos, vm.here));
comma(0);
if (vm.user->TRACE) {
vm.cs_stack.print_debug();
}
}
static void resolve_fwd_jump() {
if (cs_ddepth() < 1) {
error(Error::ControlStructureMismatch);
}
dint pos_patch = cs_dpeek();
if (dcell_hi(pos_patch) != POS_IF_FWD &&
dcell_hi(pos_patch) != POS_ELSE_FWD &&
dcell_hi(pos_patch) != POS_WHILE_FWD &&
dcell_hi(pos_patch) != POS_DO_FWD &&
dcell_hi(pos_patch) != POS_LEAVE_FWD &&
dcell_hi(pos_patch) != POS_OF_FWD &&
dcell_hi(pos_patch) != POS_ENDOF_FWD) {
error(Error::ControlStructureMismatch);
}
cs_dpop();
int dist = vm.here - dcell_lo(pos_patch);
store(dcell_lo(pos_patch), dist);
if (vm.user->TRACE) {
vm.cs_stack.print_debug();
}
}
static void mark_target_back_jump(int pos) {
if (pos != POS_BEGIN_BACK &&
pos != POS_DO_BACK) {
error(Error::ControlStructureMismatch);
}
uint addr = vm.here;
cs_dpush(mk_dcell(pos, addr));
if (vm.user->TRACE) {
vm.cs_stack.print_debug();
}
}
static void resolve_back_jump(uint xt_jump) {
if (cs_ddepth() < 1) {
error(Error::ControlStructureMismatch);
}
dint pos_patch = cs_dpeek();
if (dcell_hi(pos_patch) != POS_BEGIN_BACK &&
dcell_hi(pos_patch) != POS_DO_BACK) {
error(Error::ControlStructureMismatch);
}
cs_dpop();
comma(xt_jump);
int dist = dcell_lo(pos_patch) - vm.here;
comma(dist);
}
static bool search_resolve_fwd_jump(int pos1, int pos2 = -1, int pos3 = -1) {
std::vector<dint> save;
bool resolved = false;
while (cs_ddepth() > 0) {
dint pos_target = cs_dpeek();
if (dcell_hi(pos_target) == pos1 ||
dcell_hi(pos_target) == pos2 ||
dcell_hi(pos_target) == pos3) {
resolve_fwd_jump();
resolved = true;
break;
}
else {
save.push_back(cs_dpop());
}
}
while (!save.empty()) {
cs_dpush(save.back());
save.pop_back();
}
if (vm.user->TRACE) {
vm.cs_stack.print_debug();
}
return resolved;
}
static bool resolve_all_fwd_jumps(int stop,
int pos1, int pos2 = -1, int pos3 = -1) {
bool resolved = false;
while (cs_ddepth() > 0) {
dint pos_target = cs_dpeek();
if (dcell_hi(pos_target) == stop) {
cs_dpop(); // found the end
break;
}
else if (dcell_hi(pos_target) == pos1 ||
dcell_hi(pos_target) == pos2 ||
dcell_hi(pos_target) == pos3) {
resolve_fwd_jump();
resolved = true;
}
else {
resolved = false; // other jumps unresolved
break;
}
}
if (vm.user->TRACE) {
vm.cs_stack.print_debug();
}
return resolved;
}
static bool search_resolve_back_jump(int jump_xt,
int pos1, int pos2 = -1, int pos3 = -1) {
std::vector<dint> save;
bool resolved = false;
while (cs_ddepth() > 0) {
dint pos_target = cs_dpeek();
if (dcell_hi(pos_target) == pos1 ||
dcell_hi(pos_target) == pos2 ||
dcell_hi(pos_target) == pos3) {
resolve_back_jump(jump_xt);
resolved = true;
break;
}
else {
save.push_back(cs_dpop());
}
}
while (!save.empty()) {
cs_dpush(save.back());
save.pop_back();
}
if (vm.user->TRACE) {
vm.cs_stack.print_debug();
}
return resolved;
}
void f_ahead() {
comma_fwd_jump(xtBRANCH, POS_IF_FWD);
}
void f_if() {
comma_fwd_jump(xtZBRANCH, POS_IF_FWD);
}
void f_else() {
// make forward jump to THEN
comma_fwd_jump(xtBRANCH, POS_ELSE_FWD);
// patch forward jump at IF, ELSE or WHILE
dint save_fwd_jump = cs_dpop();
if (!search_resolve_fwd_jump(POS_IF_FWD, POS_ELSE_FWD, POS_WHILE_FWD)) {
error(Error::ControlStructureMismatch);
}
cs_dpush(save_fwd_jump);
}
void f_then() {
// patch IF or ELSE or WHILE
if (!search_resolve_fwd_jump(POS_IF_FWD, POS_ELSE_FWD, POS_WHILE_FWD)) {
error(Error::ControlStructureMismatch);
}
}
void f_begin() {
mark_target_back_jump(POS_BEGIN_BACK);
}
void f_again() {
// resolve jump to BEGIN
if (!search_resolve_back_jump(xtBRANCH, POS_BEGIN_BACK)) {
error(Error::ControlStructureMismatch);
}
}
void f_until() {
// resolve jump to BEGIN
if (!search_resolve_back_jump(xtZBRANCH, POS_BEGIN_BACK)) {
error(Error::ControlStructureMismatch);
}
}
void f_while() {
comma_fwd_jump(xtZBRANCH, POS_WHILE_FWD);
}
void f_repeat() {
// go back in the stack looking for BEGIN (there can be more than one WHILE)
if (!search_resolve_back_jump(xtBRANCH, POS_BEGIN_BACK)) {
error(Error::ControlStructureMismatch);
}
// fix WHILE
if (!search_resolve_fwd_jump(POS_WHILE_FWD, POS_IF_FWD)) {
error(Error::ControlStructureMismatch);
}
}
void f_do() {
cs_dpush(mk_dcell(POS_DO_START, 0)); // mark start of loop
comma_fwd_jump(xtXDO, POS_DO_FWD);
mark_target_back_jump(POS_DO_BACK);
}
void f_xdo() {
int start = pop();
int limit = pop();
r_push(limit);
r_push(start);
vm.ip += CELL_SZ;
}
void f_query_do() {
cs_dpush(mk_dcell(POS_DO_START, 0)); // mark start of loop
comma_fwd_jump(xtXQUERY_DO, POS_DO_FWD);
mark_target_back_jump(POS_DO_BACK);
}
void f_xquery_do() {
int start = pop();
int limit = pop();
if (start != limit) {
r_push(limit);
r_push(start);
vm.ip += CELL_SZ;
}
else {
vm.ip += fetch(vm.ip);
}
}
static void f_loop_plus_loop(uint xt_jump) {
if (!search_resolve_back_jump(xt_jump, POS_DO_BACK)) {
error(Error::ControlStructureMismatch);
}
if (!resolve_all_fwd_jumps(POS_DO_START, POS_DO_FWD, POS_LEAVE_FWD)) {
error(Error::ControlStructureMismatch);
}
}
void f_loop() {
f_loop_plus_loop(xtXLOOP);
}
void f_plus_loop() {
f_loop_plus_loop(xtXPLUS_LOOP);
}
// ANS Forth expects +LOOP to check if the index crossed the boundary
static void f_xloop_step(int step) {
int old_i = r_pop();
int limit = r_pop();
int new_i = old_i + step;
int old_diff = old_i - limit;
// crossing code lifted from pforth/Gforth
// (x^y)<0 is equivalent to (x<0) != (y<0)
bool crossed =
(((old_diff ^ (old_diff + step)) // is the limit crossed?
& (old_diff ^ step)) // is it a wrap-around?
< 0);
if (!crossed) { // loop
vm.ip += fetch(vm.ip);
r_push(limit);
r_push(new_i);
}
else { // skip
vm.ip += CELL_SZ;
}
}
void f_xloop() {
f_xloop_step(1);
}
void f_xplus_loop() {
f_xloop_step(pop());
}
void f_leave() {
comma_fwd_jump(xtXLEAVE, POS_LEAVE_FWD);
}
void f_xleave() {
f_xunloop();
vm.ip += fetch(vm.ip); // jump to end
}
void f_unloop() {
comma(xtXUNLOOP);
}
void f_xunloop() {
r_pop(); // drop I
r_pop(); // drop limit
}
void f_case() {
cs_dpush(mk_dcell(POS_CASE_START, 0)); // mark start of case
if (vm.user->TRACE) {
vm.cs_stack.print_debug();
}
}
void f_of() {
comma_fwd_jump(xtXOF, POS_OF_FWD);
}
void f_xof() {
int b = pop();
int a = pop();
if (a != b) {
push(a); // keep selector in stack
vm.ip += fetch(vm.ip);
}
else {
vm.ip += CELL_SZ; // drop selector and execute code
}
}
void f_endof() {
comma_fwd_jump(xtBRANCH, POS_ENDOF_FWD); // jump to ENDCASE
if (!search_resolve_fwd_jump(POS_OF_FWD)) {
error(Error::ControlStructureMismatch);
}
}
void f_endcase() {
comma(xtDROP); // discard selector
// ignore the status as a CASE without OF has no fwd jumps
resolve_all_fwd_jumps(POS_CASE_START, POS_ENDOF_FWD);
}