-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
539 lines (480 loc) · 13.4 KB
/
Copy pathparser.cpp
File metadata and controls
539 lines (480 loc) · 13.4 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//-----------------------------------------------------------------------------
// C++ implementation of a Forth interpreter
// Copyright (c) Paulo Custodio, 2020-2026
// License: GPL3 https://www.gnu.org/licenses/gpl-3.0.html
//-----------------------------------------------------------------------------
#include "errors.h"
#include "forth.h"
#include "parser.h"
#include "vm.h"
// ignore all control characters as spaces
bool is_space(char c) {
return c >= 0 && c <= BL;
}
bool is_print(char c) {
return c >= BL && c < 0x7f;
}
static bool only_blanks(const char* str, uint size) {
for (uint i = 0; i < size; ++i) {
if (!is_space(str[i])) {
return false;
}
}
return true;
}
// return digit value of character, or -1 if not a digit
static int char_digit(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
else if (c >= 'A' && c <= 'Z') {
return c - 'A' + 10;
}
else if (c >= 'a' && c <= 'z') {
return c - 'a' + 10;
}
else {
return -1; // not a digit
}
}
static void skip_blanks() {
const char* buffer = vm.input.buffer();
while (vm.user->TO_IN < vm.user->NR_IN && is_space(buffer[vm.user->TO_IN])) {
++vm.user->TO_IN;
}
}
static int skip_to_delimiter(char delimiter, bool& found) {
found = false;
const char* buffer = vm.input.buffer();
int end = vm.user->TO_IN;
if (delimiter == BL) {
while (vm.user->TO_IN < vm.user->NR_IN && !is_space(buffer[vm.user->TO_IN])) {
++vm.user->TO_IN;
}
end = vm.user->TO_IN;
if (vm.user->TO_IN < vm.user->NR_IN && is_space(buffer[vm.user->TO_IN])) {
found = true;
++vm.user->TO_IN; // skip delimiter
}
}
else {
while (vm.user->TO_IN < vm.user->NR_IN && buffer[vm.user->TO_IN] != delimiter) {
++vm.user->TO_IN;
}
end = vm.user->TO_IN;
if (vm.user->TO_IN < vm.user->NR_IN && buffer[vm.user->TO_IN] == delimiter) {
found = true;
++vm.user->TO_IN; // skip delimiter
}
}
return end; // end of word, char before delimiter
}
const char* parse_word(uint& size, char delimiter) {
if (delimiter == BL) {
skip_blanks(); // skip blanks before word
}
const char* buffer = vm.input.buffer();
uint start = vm.user->TO_IN;
bool found;
uint end = skip_to_delimiter(delimiter, found);
size = end - start;
const char* word = &buffer[start];
return word;
}
CString* parse_cword(char delimiter) {
uint size = 0;
const char* word = parse_word(size, delimiter);
CString* cword = vm.wordbuf.append_cstring(word, size);
return cword;
}
std::string parse_backslash_string() {
const char* buffer = vm.input.buffer();
std::string message;
for (; vm.user->TO_IN < vm.user->NR_IN; ++vm.user->TO_IN) {
if (buffer[vm.user->TO_IN] == '\"') {
++vm.user->TO_IN;
break;
}
switch (buffer[vm.user->TO_IN]) {
case '\\':
++vm.user->TO_IN;
if (vm.user->TO_IN < vm.user->NR_IN) {
switch (buffer[vm.user->TO_IN]) {
case 'a':
message.push_back('\a');
break;
case 'b':
message.push_back('\b');
break;
case 'e':
message.push_back('\x1B');
break;
case 'f':
message.push_back('\f');
break;
case 'l':
message.push_back('\n');
break;
case 'm':
message.push_back('\r');
message.push_back('\n');
break;
case 'n':
message.push_back('\n');
break;
case 'q':
message.push_back('"');
break;
case 'r':
message.push_back('\r');
break;
case 't':
message.push_back('\t');
break;
case 'v':
message.push_back('\v');
break;
case 'z':
message.push_back('\0');
break;
case '"':
message.push_back('"');
break;
case '\\':
message.push_back('\\');
break;
case 'x':
++vm.user->TO_IN;
if (vm.user->TO_IN + 1 < vm.user->NR_IN &&
isxdigit(buffer[vm.user->TO_IN]) &&
isxdigit(buffer[vm.user->TO_IN + 1])
) {
std::string hex_str = std::string(&buffer[vm.user->TO_IN],
&buffer[vm.user->TO_IN + 2]);
++vm.user->TO_IN;
int char_value = std::stoi(hex_str, nullptr, 16);
message.push_back(char_value);
}
break;
default:
break; // ignore all other escape chars
}
}
break;
default:
message.push_back(buffer[vm.user->TO_IN]);
}
}
return message;
}
static bool parse_sign(const char*& p, const char* end, int& sign) {
bool found_sign = false;
while (p < end) {
switch (*p) {
case '-':
found_sign = true;
sign *= -1;
p++;
break;
case '+':
found_sign = true;
sign *= 1;
p++;
break;
default:
return found_sign;
}
}
return found_sign;
}
static int parse_digits(const char*& p, const char* end, int base,
dint& value) {
int num_digits = 0;
while (p < end) {
int digit = char_digit(*p);
if (digit >= 0 && digit < base) {
++p;
++num_digits;
value = value * base + digit;
}
else {
break;
}
}
return num_digits;
}
bool parse_number(const std::string& text, bool& is_double, dint& value) {
return parse_number(text.c_str(), static_cast<uint>(text.size()), is_double,
value);
}
// parse number with optional sign (+-)
// accept $ # as hex prefixes, % as binary prefix, 'a' as quoted character
// skip punctuation ( , . + - / : )
// if punctuation found, set DPL to number of digits after last punctuation, return double cell
// return true if ok, false if error
bool parse_number(const char* text, uint size, bool& is_double, dint& value) {
static const std::string punctuation = ",.+-/:";
// init output vars
int sign = 1;
int base = vm.user->BASE;
vm.user->DPL = 0;
const char* p = text;
const char* end = text + size;
is_double = false;
value = 0;
// check sign before number prefix
parse_sign(p, end, sign);
if (p >= end) {
return false; // no number
}
// check base prefix
switch (*p) {
case '#':
base = 10;
p++;
break;
case '$':
base = 16;
p++;
break;
case '%':
base = 2;
p++;
break;
case '\'':
if (end - p == 3 && p[2] == '\'') {
value = sign * static_cast<unsigned char>(p[1]);
is_double = false;
return true;
}
break;
}
// check sign after number prefix
parse_sign(p, end, sign);
// collect digits
bool found_digits = false;
while (p < end) {
int num_digits = parse_digits(p, end, base, value);
if (num_digits > 0) {
found_digits = true;
if (is_double) {
vm.user->DPL += num_digits;
}
}
else if (punctuation.find(*p) != std::string::npos) {
++p;
vm.user->DPL = 0;
is_double = true;
}
else {
return false; // digit not in BASE
}
}
if (!found_digits) {
return false; // no digits found
}
value *= sign;
return true;
}
bool parse_float(const std::string& text, double& value, bool needs_exp) {
return parse_float(text.c_str(), static_cast<uint>(text.size()), value,
needs_exp);
}
bool parse_float(const char* text, uint size, double& value, bool needs_exp) {
value = 0.0;
if (vm.user->BASE != 10) {
return false;
}
const char* p = text;
const char* end = text + size;
int sign = 1;
int exp_sign = 1;
dint dummy = 0;
// start with a sign, digits*, '.'?, digits*
parse_sign(p, end, sign);
const char* start_mantissa = p;
int num_digits = parse_digits(p, end, 10, dummy);
if (p < end && *p == '.') {
++p;
num_digits += parse_digits(p, end, 10, dummy);
}
const char* end_mantissa = p;
if (num_digits == 0) {
return false;
}
// check for mandatory exponent
if (needs_exp && p >= end) {
return false;
}
// optional exponent 'e' 'd' or sign, digits+
if (p >= end) {
std::string number = sign < 0 ? "-" : "";
number += std::string(start_mantissa, end_mantissa);
value = std::stod(number);
return true;
}
else {
if (toupper(*p) != 'D' && toupper(*p) != 'E' &&
*p != '-' && *p != '+') {
return false;
}
if (toupper(*p) == 'D' || toupper(*p) == 'E') {
++p;
}
parse_sign(p, end, exp_sign);
const char* start_exponent = p;
int num_digits_exp = parse_digits(p, end, 10, dummy);
const char* end_exponent = p;
if (p < end) {
return false; // extra characters after number
}
else {
std::string number = sign < 0 ? "-" : "";
number += std::string(start_mantissa, end_mantissa);
if (num_digits_exp > 0) {
number += "e";
number += exp_sign < 0 ? "-" : "";
number += std::string(start_exponent, end_exponent);
}
value = std::stod(number);
return true;
}
}
}
int f_word(char delimiter) {
CString* word = parse_cword(delimiter);
return mem_addr(word);
}
void f_parse(char delimiter) {
uint size;
const char* word = parse_word(size, delimiter);
push(mem_addr(word));
push(size);
}
void f_parse_name() {
uint size;
const char* word = parse_word(size, BL);
push(mem_addr(word));
push(size);
}
int f_char(char delimiter) {
uint size = 0;
const char* str = parse_word(size, delimiter);
if (size == 0) {
return 0;
}
else {
return *str;
}
}
void f_bracket_char(char delimiter) {
int c = f_char(delimiter);
comma(xtXLITERAL);
comma(c);
}
static int _number(bool do_error) {
uint size = pop();
uint addr = pop();
dint value;
bool is_double;
if (parse_number(mem_char_ptr(addr, size), size, is_double, value)) {
if (is_double) {
push(dcell_lo(value));
push(dcell_hi(value));
return 2;
}
else {
push(dcell_lo(value));
return 1;
}
}
else {
if (do_error)
error(Error::InvalidNumericArgument, std::string(mem_char_ptr(addr, size),
mem_char_ptr(addr, size) + size));
return 0;
}
}
void f_number_q() {
int num_cells = _number(false);
push(num_cells);
}
void f_number() {
_number(true);
}
void f_to_number() {
uint size = pop();
uint addr = pop();
udint n = (udint)dpop();
int digit;
while (size > 0 && (digit = char_digit(cfetch(addr))) >= 0
&& digit < vm.user->BASE) {
n = n * vm.user->BASE + digit;
addr++;
size--;
}
dpush(n);
push(addr);
push(size);
}
void f_convert() {
uint addr = pop() + 1;
udint n = (udint)dpop();
int digit;
while ((digit = char_digit(cfetch(addr))) >= 0 &&
digit < vm.user->BASE) {
n = n * vm.user->BASE + digit;
addr++;
}
dpush(n);
push(addr);
}
void f_open_paren() {
if (vm.input.source_id() != 0) {
bool found = false;
while (!found) {
skip_to_delimiter(')', found);
if (found) {
break;
}
else if (vm.input.refill()) {
continue;
}
else {
break;
}
}
}
else {
bool found;
skip_to_delimiter(')', found);
}
}
void f_backslash() {
if (vm.user->BLK > 0) {
vm.user->TO_IN = (vm.user->TO_IN + BLOCK_COLS - 1) & ~(BLOCK_COLS - 1);
}
else {
bool found;
skip_to_delimiter(CR, found);
}
}
void f_to_float() {
uint size = pop();
uint addr = pop();
const char* str = mem_char_ptr(addr, size);
if (only_blanks(str, size)) {
fpush(0.0);
push(F_TRUE);
}
else {
double value = 0.0;
if (parse_float(str, size, value, false)) {
fpush(value);
push(F_TRUE);
}
else {
push(F_FALSE);
}
}
}