-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathForthMachine.cpp
4316 lines (3939 loc) · 144 KB
/
ForthMachine.cpp
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE
#define FILENAME(line) FILENAME_FOR_EXCEPTIONS("src/libawkward/forth/ForthMachine.cpp", line)
#include <sstream>
#include <stdexcept>
#include <chrono>
#include "awkward/forth/ForthMachine.h"
namespace awkward {
// ABI version must be increased whenever any interpretation of the bytecode changes.
#define ABI_VERSION 1
// Instruction values are preprocessor macros to be equally usable in 32-bit and
// 64-bit instruction sets.
// parser flags (parsers are combined bitwise and then bit-inverted to be negative)
#define READ_DIRECT 1
#define READ_REPEATED 2
#define READ_BIGENDIAN 4
// parser sequential values (starting in the fourth bit)
#define READ_MASK (~(-0x100) & (-0x8))
#define READ_BOOL (0x8 * 1)
#define READ_INT8 (0x8 * 2)
#define READ_INT16 (0x8 * 3)
#define READ_INT32 (0x8 * 4)
#define READ_INT64 (0x8 * 5)
#define READ_INTP (0x8 * 6)
#define READ_UINT8 (0x8 * 7)
#define READ_UINT16 (0x8 * 8)
#define READ_UINT32 (0x8 * 9)
#define READ_UINT64 (0x8 * 10)
#define READ_UINTP (0x8 * 11)
#define READ_FLOAT32 (0x8 * 12)
#define READ_FLOAT64 (0x8 * 13)
#define READ_VARINT (0x8 * 14)
#define READ_ZIGZAG (0x8 * 15)
#define READ_NBIT (0x8 * 16)
#define READ_TEXTINT (0x8 * 17)
#define READ_TEXTFLOAT (0x8 * 18)
#define READ_QUOTEDSTR (0x8 * 19)
// instructions from special parsing rules
#define CODE_LITERAL 0
#define CODE_HALT 1
#define CODE_PAUSE 2
#define CODE_IF 3
#define CODE_IF_ELSE 4
#define CODE_CASE_REGULAR 5
#define CODE_DO 6
#define CODE_DO_STEP 7
#define CODE_AGAIN 8
#define CODE_UNTIL 9
#define CODE_WHILE 10
#define CODE_EXIT 11
#define CODE_PUT 12
#define CODE_INC 13
#define CODE_GET 14
#define CODE_ENUM 15
#define CODE_ENUMONLY 16
#define CODE_PEEK 17
#define CODE_LEN_INPUT 18
#define CODE_POS 19
#define CODE_END 20
#define CODE_SEEK 21
#define CODE_SKIP 22
#define CODE_SKIPWS 23
#define CODE_WRITE 24
#define CODE_WRITE_ADD 25
#define CODE_WRITE_DUP 26
#define CODE_LEN_OUTPUT 27
#define CODE_REWIND 28
// generic builtin instructions
#define CODE_STRING 29
#define CODE_PRINT_STRING 30
#define CODE_PRINT 31
#define CODE_PRINT_CR 32
#define CODE_PRINT_STACK 33
#define CODE_I 34
#define CODE_J 35
#define CODE_K 36
#define CODE_DUP 37
#define CODE_DROP 38
#define CODE_SWAP 39
#define CODE_OVER 40
#define CODE_ROT 41
#define CODE_NIP 42
#define CODE_TUCK 43
#define CODE_ADD 44
#define CODE_SUB 45
#define CODE_MUL 46
#define CODE_DIV 47
#define CODE_MOD 48
#define CODE_DIVMOD 49
#define CODE_NEGATE 50
#define CODE_ADD1 51
#define CODE_SUB1 52
#define CODE_ABS 53
#define CODE_MIN 54
#define CODE_MAX 55
#define CODE_EQ 56
#define CODE_NE 57
#define CODE_GT 58
#define CODE_GE 59
#define CODE_LT 60
#define CODE_LE 61
#define CODE_EQ0 62
#define CODE_INVERT 63
#define CODE_AND 64
#define CODE_OR 65
#define CODE_XOR 66
#define CODE_LSHIFT 67
#define CODE_RSHIFT 68
#define CODE_FALSE 69
#define CODE_TRUE 70
// beginning of the user-defined dictionary
#define BOUND_DICTIONARY 71
const std::set<std::string> reserved_words_({
// comments
"(", ")", "\\", "\n", "",
// defining functions
":", ";", "recurse",
// declaring globals
"variable", "input", "output",
// manipulate control flow externally
"halt", "pause",
// conditionals
"if", "then", "else", "case", "of", "endof", "endcase",
// loops
"do", "loop", "+loop",
"begin", "again", "until", "while", "repeat",
// nonlocal exits
"exit",
// variable access
"!", "+!", "@",
// input actions
"enum", "enumonly", "peek", "len", "pos", "end", "seek", "skip", "skipws",
// output actions
"<-", "+<-", "stack", "rewind",
// print (for debugging)
".\"",
// user defined strings
"s\""
});
const std::set<std::string> input_parser_words_({
// single little-endian
"?->", "b->", "h->", "i->", "q->", "n->", "B->", "H->", "I->", "Q->", "N->",
"f->", "d->", "varint->", "zigzag->", "textint->", "textfloat->", "quotedstr->",
// single big-endian
"!h->", "!i->", "!q->", "!n->", "!H->", "!I->", "!Q->", "!N->",
"!f->", "!d->",
// multiple little-endian
"#?->", "#b->", "#h->", "#i->", "#q->", "#n->", "#B->", "#H->", "#I->", "#Q->", "#N->",
"#f->", "#d->", "#varint->", "#zigzag->", "#textint->", "#textfloat->", "#quotedstr->",
// multiple big-endian
"#!h->", "#!i->", "#!q->", "#!n->", "#!H->", "#!I->", "#!Q->", "#!N->",
"#!f->", "#!d->"
});
const std::map<std::string, util::dtype> output_dtype_words_({
{"bool", util::dtype::boolean},
{"int8", util::dtype::int8},
{"int16", util::dtype::int16},
{"int32", util::dtype::int32},
{"int64", util::dtype::int64},
{"uint8", util::dtype::uint8},
{"uint16", util::dtype::uint16},
{"uint32", util::dtype::uint32},
{"uint64", util::dtype::uint64},
{"float32", util::dtype::float32},
{"float64", util::dtype::float64}
});
const std::map<std::string, int64_t> generic_builtin_words_({
// print (for debugging)
{".", CODE_PRINT},
{"cr", CODE_PRINT_CR},
{".s", CODE_PRINT_STACK},
// loop variables
{"i", CODE_I},
{"j", CODE_J},
{"k", CODE_K},
// stack operations
{"dup", CODE_DUP},
{"drop", CODE_DROP},
{"swap", CODE_SWAP},
{"over", CODE_OVER},
{"rot", CODE_ROT},
{"nip", CODE_NIP},
{"tuck", CODE_TUCK},
// basic mathematical functions
{"+", CODE_ADD},
{"-", CODE_SUB},
{"*", CODE_MUL},
{"/", CODE_DIV},
{"mod", CODE_MOD},
{"/mod", CODE_DIVMOD},
{"negate", CODE_NEGATE},
{"1+", CODE_ADD1},
{"1-", CODE_SUB1},
{"abs", CODE_ABS},
{"min", CODE_MIN},
{"max", CODE_MAX},
// comparisons
{"=", CODE_EQ},
{"<>", CODE_NE},
{">", CODE_GT},
{">=", CODE_GE},
{"<", CODE_LT},
{"<=", CODE_LE},
{"0=", CODE_EQ0},
// bitwise operations
{"invert", CODE_INVERT},
{"and", CODE_AND},
{"or", CODE_OR},
{"xor", CODE_XOR},
{"lshift", CODE_LSHIFT},
{"rshift", CODE_RSHIFT},
// constants
{"false", CODE_FALSE},
{"true", CODE_TRUE}
});
template <typename T, typename I>
ForthMachineOf<T, I>::ForthMachineOf(const std::string& source,
int64_t stack_max_depth,
int64_t recursion_max_depth,
int64_t string_buffer_size,
int64_t output_initial_size,
double output_resize_factor)
: source_(source)
, output_initial_size_(output_initial_size)
, output_resize_factor_(output_resize_factor)
, stack_buffer_(new T[(size_t)stack_max_depth])
, stack_depth_(0)
, stack_max_depth_(stack_max_depth)
, string_buffer_(new char[(size_t)string_buffer_size])
, string_buffer_size_(string_buffer_size)
, current_inputs_()
, current_outputs_()
, is_ready_(false)
, current_which_(new int64_t[(size_t)recursion_max_depth])
, current_where_(new int64_t[(size_t)recursion_max_depth])
, recursion_current_depth_(0)
, recursion_max_depth_(recursion_max_depth)
, do_recursion_depth_(new int64_t[(size_t)recursion_max_depth])
, do_stop_(new int64_t[(size_t)recursion_max_depth])
, do_i_(new int64_t[(size_t)recursion_max_depth])
, do_current_depth_(0)
, current_error_(util::ForthError::none)
, count_instructions_(0)
, count_reads_(0)
, count_writes_(0)
, count_nanoseconds_(0)
{
std::vector<std::string> tokenized;
std::vector<std::pair<int64_t, int64_t>> linecol;
tokenize(tokenized, linecol);
compile(tokenized, linecol);
}
template <typename T, typename I>
ForthMachineOf<T, I>::~ForthMachineOf() {
delete [] stack_buffer_;
delete [] string_buffer_;
delete [] current_which_;
delete [] current_where_;
delete [] do_recursion_depth_;
delete [] do_stop_;
delete [] do_i_;
}
template <typename T, typename I>
int64_t
ForthMachineOf<T, I>::abi_version() const noexcept {
return ABI_VERSION;
}
template <typename T, typename I>
const std::string
ForthMachineOf<T, I>::source() const noexcept {
return source_;
}
template <typename T, typename I>
const std::vector<I>
ForthMachineOf<T, I>::bytecodes() const {
return bytecodes_;
}
template <typename T, typename I>
const std::vector<int64_t>
ForthMachineOf<T, I>::bytecodes_offsets() const {
return bytecodes_offsets_;
}
template <typename T, typename I>
const std::string
ForthMachineOf<T, I>::decompiled() const {
bool first = true;
std::stringstream out;
for (auto const& name : variable_names_) {
first = false;
out << "variable " << name << std::endl;
}
for (auto const& name : input_names_) {
first = false;
out << "input " << name << std::endl;
}
for (IndexTypeOf<int64_t> i = 0; i < output_names_.size(); i++) {
first = false;
out << "output " << output_names_[i] << " "
<< util::dtype_to_name(output_dtypes_[i]) << std::endl;
}
for (IndexTypeOf<int64_t> i = 0; i < dictionary_names_.size(); i++) {
if (!first) {
out << std::endl;
}
first = false;
int64_t segment_position = dictionary_bytecodes_[i] - BOUND_DICTIONARY;
out << ": " << dictionary_names_[i] << std::endl
<< (segment_nonempty(segment_position) ? " " : "")
<< decompiled_segment(segment_position, " ")
<< ";" << std::endl;
}
if (!first && bytecodes_offsets_[1] != 0) {
out << std::endl;
}
out << decompiled_segment(0);
return std::move(out.str());
}
template <typename T, typename I>
const std::string
ForthMachineOf<T, I>::decompiled_segment(int64_t segment_position,
const std::string& indent,
bool endline) const {
if (segment_position < 0 || (IndexTypeOf<int64_t>)segment_position + 1 >= bytecodes_offsets_.size()) {
throw std::runtime_error(
std::string("segment ") + std::to_string(segment_position)
+ std::string(" does not exist in the bytecode") + FILENAME(__LINE__));
}
std::stringstream out;
int64_t bytecode_position = bytecodes_offsets_[(IndexTypeOf<int64_t>)segment_position];
while (bytecode_position < bytecodes_offsets_[(IndexTypeOf<int64_t>)segment_position + 1]) {
if (bytecode_position != bytecodes_offsets_[(IndexTypeOf<int64_t>)segment_position]) {
out << indent;
}
out << decompiled_at(bytecode_position, indent);
bytecode_position += bytecodes_per_instruction(bytecode_position);
if (endline || bytecode_position < bytecodes_offsets_[(IndexTypeOf<int64_t>)segment_position + 1]) {
out << std::endl;
}
}
return std::move(out.str());
}
template <typename T, typename I>
const std::string
ForthMachineOf<T, I>::decompiled_at(int64_t bytecode_position,
const std::string& indent) const {
if (bytecode_position < 0 || (IndexTypeOf<int64_t>)bytecode_position >= bytecodes_.size()) {
throw std::runtime_error(
std::string("absolute position ") + std::to_string(bytecode_position)
+ std::string(" does not exist in the bytecode") + FILENAME(__LINE__));
}
I bytecode = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position];
I next_bytecode = -1;
if ((IndexTypeOf<int64_t>)bytecode_position + 1 < bytecodes_.size()) {
next_bytecode = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
}
if (bytecode < 0) {
I in_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
std::string in_name = input_names_[(IndexTypeOf<int64_t>)in_num];
std::string rep = (~bytecode & READ_REPEATED) ? "#" : "";
std::string big = ((~bytecode & READ_BIGENDIAN) != 0) ? "!" : "";
std::string rest;
int64_t next_pos = 2;
I nbits = 0;
switch (~bytecode & READ_MASK) {
case READ_BOOL:
rest = "?->";
break;
case READ_INT8:
rest = "b->";
break;
case READ_INT16:
rest = "h->";
break;
case READ_INT32:
rest = "i->";
break;
case READ_INT64:
rest = "q->";
break;
case READ_INTP:
rest = "n->";
break;
case READ_UINT8:
rest = "B->";
break;
case READ_UINT16:
rest = "H->";
break;
case READ_UINT32:
rest = "I->";
break;
case READ_UINT64:
rest = "Q->";
break;
case READ_UINTP:
rest = "N->";
break;
case READ_FLOAT32:
rest = "f->";
break;
case READ_FLOAT64:
rest = "d->";
break;
case READ_VARINT:
rest = "varint->";
break;
case READ_ZIGZAG:
rest = "zigzag->";
break;
case READ_NBIT:
nbits = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + (IndexTypeOf<int64_t>)next_pos];
next_pos++;
rest = std::to_string(nbits) + "bit->";
break;
case READ_TEXTINT:
rest = "textint->";
break;
case READ_TEXTFLOAT:
rest = "textfloat->";
break;
case READ_QUOTEDSTR:
rest = "quotedstr->";
break;
}
std::string arrow = rep + big + rest;
std::string out_name = "stack";
if (~bytecode & READ_DIRECT) {
I out_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + (IndexTypeOf<int64_t>)next_pos];
out_name = output_names_[(IndexTypeOf<int64_t>)out_num];
}
return in_name + std::string(" ") + arrow + std::string(" ") + out_name;
}
else if (bytecode >= BOUND_DICTIONARY && next_bytecode == CODE_AGAIN) {
int64_t body = bytecode - BOUND_DICTIONARY;
return std::move(std::string("begin\n")
+ (segment_nonempty(body) ? indent + " " : "")
+ decompiled_segment(body, indent + " ")
+ indent + "again");
}
else if (bytecode >= BOUND_DICTIONARY && next_bytecode == CODE_UNTIL) {
int64_t body = bytecode - BOUND_DICTIONARY;
return std::move(std::string("begin\n")
+ (segment_nonempty(body) ? indent + " " : "")
+ decompiled_segment(body, indent + " ")
+ indent + "until");
}
else if (bytecode >= BOUND_DICTIONARY && next_bytecode == CODE_WHILE) {
int64_t precondition = bytecode - BOUND_DICTIONARY;
int64_t postcondition = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 2] - BOUND_DICTIONARY;
return std::move(std::string("begin\n")
+ (segment_nonempty(precondition) ? indent + " " : "")
+ decompiled_segment(precondition, indent + " ")
+ indent + "while\n"
+ (segment_nonempty(postcondition) ? indent + " " : "")
+ decompiled_segment(postcondition, indent + " ")
+ indent + "repeat");
}
else if (bytecode >= BOUND_DICTIONARY) {
for (IndexTypeOf<int64_t> i = 0; i < dictionary_names_.size(); i++) {
if (dictionary_bytecodes_[i] == bytecode) {
return dictionary_names_[i];
}
}
return decompiled_segment(bytecode - BOUND_DICTIONARY, indent, false);
}
else {
switch (bytecode) {
case CODE_LITERAL: {
return std::to_string(bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1]);
}
case CODE_HALT: {
return "halt";
}
case CODE_PAUSE: {
return "pause";
}
case CODE_IF: {
int64_t consequent = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1] - BOUND_DICTIONARY;
return std::move(std::string("if\n")
+ (segment_nonempty(consequent) ? indent + " " : "")
+ decompiled_segment(consequent, indent + " ")
+ indent + "then");
}
case CODE_IF_ELSE: {
int64_t consequent = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1] - BOUND_DICTIONARY;
int64_t alternate = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 2] - BOUND_DICTIONARY;
return std::move(std::string("if\n")
+ (segment_nonempty(consequent) ? indent + " " : "")
+ decompiled_segment(consequent, indent + " ")
+ indent + "else\n"
+ (segment_nonempty(alternate) ? indent + " " : "")
+ decompiled_segment(alternate, indent + " ")
+ indent + "then");
}
case CODE_CASE_REGULAR: {
int64_t start = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1] - BOUND_DICTIONARY;
int64_t stop = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 2] - BOUND_DICTIONARY;
int64_t num_cases = stop - start;
std::stringstream out;
out << "case ( regular )\n";
for (int64_t i = 0; i < num_cases; i++) {
out << indent << " " << i << " of";
I consequent = (I)(start + i);
if (segment_nonempty(consequent)) {
out << "\n" << indent << " ";
out << decompiled_segment(consequent, indent + " ");
out << indent << " endof\n";
}
else {
out << " endof\n";
}
}
bool alt_nonempty = (bytecodes_offsets_[(IndexTypeOf<int64_t>)stop] + 1
!= bytecodes_offsets_[(IndexTypeOf<int64_t>)stop + 1]);
if (alt_nonempty) {
std::string alt_segment = decompiled_segment(stop, indent + " ");
// remove the last "drop" command, which is for execution but not visible in source code
alt_segment = alt_segment.substr(0, alt_segment.length() - indent.length() - 9);
out << indent << " ( default )\n" << indent << " " << alt_segment;
}
out << "endcase";
return std::move(out.str());
}
case CODE_DO: {
int64_t body = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1] - BOUND_DICTIONARY;
return std::move(std::string("do\n")
+ (segment_nonempty(body) ? indent + " " : "")
+ decompiled_segment(body, indent + " ")
+ indent + "loop");
}
case CODE_DO_STEP: {
int64_t body = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1] - BOUND_DICTIONARY;
return std::move(std::string("do\n")
+ (segment_nonempty(body) ? indent + " " : "")
+ decompiled_segment(body, indent + " ")
+ indent + "+loop");
}
case CODE_EXIT: {
return std::move(std::string("exit"));
}
case CODE_PUT: {
int64_t var_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return variable_names_[(IndexTypeOf<int64_t>)var_num] + " !";
}
case CODE_INC: {
int64_t var_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return variable_names_[(IndexTypeOf<int64_t>)var_num] + " +!";
}
case CODE_GET: {
int64_t var_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return variable_names_[(IndexTypeOf<int64_t>)var_num] + " @";
}
case CODE_ENUM: {
int64_t in_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
int64_t start = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 2];
int64_t stop = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 3];
std::stringstream out;
out << input_names_[(IndexTypeOf<int64_t>)in_num] << " enum";
for (int64_t i = start; i < stop; i++) {
out << " s\" " << strings_[(size_t)i] << "\"";
}
return out.str();
}
case CODE_ENUMONLY: {
int64_t in_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
int64_t start = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 2];
int64_t stop = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 3];
std::stringstream out;
out << input_names_[(IndexTypeOf<int64_t>)in_num] << " enumonly";
for (int64_t i = start; i < stop; i++) {
out << " s\" " << strings_[(size_t)i] << "\"";
}
return out.str();
}
case CODE_PEEK: {
int64_t in_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return input_names_[(IndexTypeOf<int64_t>)in_num] + " peek";
}
case CODE_LEN_INPUT: {
int64_t in_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return input_names_[(IndexTypeOf<int64_t>)in_num] + " len";
}
case CODE_POS: {
int64_t in_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return input_names_[(IndexTypeOf<int64_t>)in_num] + " pos";
}
case CODE_END: {
int64_t in_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return input_names_[(IndexTypeOf<int64_t>)in_num] + " end";
}
case CODE_SEEK: {
int64_t in_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return input_names_[(IndexTypeOf<int64_t>)in_num] + " seek";
}
case CODE_SKIP: {
int64_t in_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return input_names_[(IndexTypeOf<int64_t>)in_num] + " skip";
}
case CODE_SKIPWS: {
int64_t in_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return input_names_[(IndexTypeOf<int64_t>)in_num] + " skipws";
}
case CODE_WRITE: {
int64_t out_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return output_names_[(IndexTypeOf<int64_t>)out_num] + " <- stack";
}
case CODE_WRITE_ADD: {
int64_t out_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return output_names_[(IndexTypeOf<int64_t>)out_num] + " +<- stack";
}
case CODE_WRITE_DUP: {
int64_t out_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return output_names_[(IndexTypeOf<int64_t>)out_num] + " dup";
}
case CODE_LEN_OUTPUT: {
int64_t out_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return output_names_[(IndexTypeOf<int64_t>)out_num] + " len";
}
case CODE_REWIND: {
int64_t out_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return output_names_[(IndexTypeOf<int64_t>)out_num] + " rewind";
}
case CODE_STRING: {
int64_t string_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return "s\" " + strings_[(IndexTypeOf<int64_t>)string_num] + "\"";
}
case CODE_PRINT_STRING: {
int64_t string_num = bytecodes_[(IndexTypeOf<int64_t>)bytecode_position + 1];
return ".\" " + strings_[(IndexTypeOf<int64_t>)string_num] + "\"";
}
case CODE_PRINT: {
return ".";
}
case CODE_PRINT_CR: {
return "cr";
}
case CODE_PRINT_STACK: {
return ".s";
}
case CODE_I: {
return "i";
}
case CODE_J: {
return "j";
}
case CODE_K: {
return "k";
}
case CODE_DUP: {
return "dup";
}
case CODE_DROP: {
return "drop";
}
case CODE_SWAP: {
return "swap";
}
case CODE_OVER: {
return "over";
}
case CODE_ROT: {
return "rot";
}
case CODE_NIP: {
return "nip";
}
case CODE_TUCK: {
return "tuck";
}
case CODE_ADD: {
return "+";
}
case CODE_SUB: {
return "-";
}
case CODE_MUL: {
return "*";
}
case CODE_DIV: {
return "/";
}
case CODE_MOD: {
return "mod";
}
case CODE_DIVMOD: {
return "/mod";
}
case CODE_NEGATE: {
return "negate";
}
case CODE_ADD1: {
return "1+";
}
case CODE_SUB1: {
return "1-";
}
case CODE_ABS: {
return "abs";
}
case CODE_MIN: {
return "min";
}
case CODE_MAX: {
return "max";
}
case CODE_EQ: {
return "=";
}
case CODE_NE: {
return "<>";
}
case CODE_GT: {
return ">";
}
case CODE_GE: {
return ">=";
}
case CODE_LT: {
return "<";
}
case CODE_LE: {
return "<=";
}
case CODE_EQ0: {
return "0=";
}
case CODE_INVERT: {
return "invert";
}
case CODE_AND: {
return "and";
}
case CODE_OR: {
return "or";
}
case CODE_XOR: {
return "xor";
}
case CODE_LSHIFT: {
return "lshift";
}
case CODE_RSHIFT: {
return "rshift";
}
case CODE_FALSE: {
return "false";
}
case CODE_TRUE: {
return "true";
}
}
return std::move(std::string("(unrecognized bytecode ") + std::to_string(bytecode) + ")");
}
}
template <typename T, typename I>
const std::vector<std::string>
ForthMachineOf<T, I>::dictionary() const {
return dictionary_names_;
}
template <typename T, typename I>
int64_t
ForthMachineOf<T, I>::stack_max_depth() const noexcept {
return stack_max_depth_;
}
template <typename T, typename I>
int64_t
ForthMachineOf<T, I>::recursion_max_depth() const noexcept {
return recursion_max_depth_;
}
template <typename T, typename I>
int64_t
ForthMachineOf<T, I>::string_buffer_size() const noexcept {
return string_buffer_size_;
}
template <typename T, typename I>
int64_t
ForthMachineOf<T, I>::output_initial_size() const noexcept {
return output_initial_size_;
}
template <typename T, typename I>
double
ForthMachineOf<T, I>::output_resize_factor() const noexcept {
return output_resize_factor_;
}
template <typename T, typename I>
const std::vector<T>
ForthMachineOf<T, I>::stack() const {
std::vector<T> out;
for (int64_t i = 0; i < stack_depth_; i++) {
out.push_back(stack_buffer_[i]);
}
return out;
}
template <typename T, typename I>
T
ForthMachineOf<T, I>::stack_at(int64_t from_top) const noexcept {
return stack_buffer_[stack_depth_ - from_top];
}
template <typename T, typename I>
int64_t
ForthMachineOf<T, I>::stack_depth() const noexcept {
return stack_depth_;
}
template <typename T, typename I>
void
ForthMachineOf<T, I>::stack_clear() noexcept {
stack_depth_ = 0;
}
template <typename T, typename I>
const std::map<std::string, T>
ForthMachineOf<T, I>::variables() const {
std::map<std::string, T> out;
for (IndexTypeOf<int64_t> i = 0; i < variable_names_.size(); i++) {
out[variable_names_[i]] = variables_[i];
}
return out;
}
template <typename T, typename I>
const std::vector<std::string>
ForthMachineOf<T, I>::variable_index() const {
return variable_names_;
}
template <typename T, typename I>
T
ForthMachineOf<T, I>::variable_at(const std::string& name) const {
for (IndexTypeOf<int64_t> i = 0; i < variable_names_.size(); i++) {
if (variable_names_[i] == name) {
return variables_[i];
}
}
throw std::invalid_argument(
std::string("variable not found: ") + name + FILENAME(__LINE__)
);
}
template <typename T, typename I>
T
ForthMachineOf<T, I>::variable_at(int64_t index) const noexcept {
return variables_[(IndexTypeOf<int64_t>)index];
}
template <typename T, typename I>
bool
ForthMachineOf<T, I>::input_must_be_writable(const std::string& name) const {
for (IndexTypeOf<int64_t> i = 0; i < input_names_.size(); i++) {
if (input_names_[i] == name) {
return input_must_be_writable_[i];
}
}
throw std::invalid_argument(
std::string("input not found: ") + name + FILENAME(__LINE__)
);
}
template <typename T, typename I>
int64_t
ForthMachineOf<T, I>::input_position_at(const std::string& name) const {
for (IndexTypeOf<int64_t> i = 0;
i < input_names_.size() && i < current_inputs_.size();
i++) {
if (input_names_[i] == name) {
return current_inputs_[i].get()->pos();
}
}
throw std::invalid_argument(
std::string("input not found: ") + name + FILENAME(__LINE__)
);
}
template <typename T, typename I>
int64_t
ForthMachineOf<T, I>::input_position_at(int64_t index) const noexcept {
return current_inputs_[(IndexTypeOf<int64_t>)index].get()->pos();
}
template <typename T, typename I>
const std::map<std::string, std::shared_ptr<ForthOutputBuffer>>
ForthMachineOf<T, I>::outputs() const {
std::map<std::string, std::shared_ptr<ForthOutputBuffer>> out;
for (IndexTypeOf<int64_t> i = 0;
i < output_names_.size() && i < current_outputs_.size();
i++) {
out[output_names_[i]] = current_outputs_[i];
}
return out;
}
template <typename T, typename I>
const std::vector<std::string>
ForthMachineOf<T, I>::output_index() const noexcept {
return output_names_;
}
template <typename T, typename I>
const std::shared_ptr<ForthOutputBuffer>
ForthMachineOf<T, I>::output_at(const std::string& name) const {
for (IndexTypeOf<int64_t> i = 0;
i < output_names_.size() && i < current_outputs_.size();
i++) {
if (output_names_[i] == name) {
return current_outputs_[i];
}
}
throw std::invalid_argument(
std::string("output not found: ") + name + FILENAME(__LINE__)
);
}
template <typename T, typename I>
const std::shared_ptr<ForthOutputBuffer>
ForthMachineOf<T, I>::output_at(int64_t index) const noexcept {
return current_outputs_[(IndexTypeOf<int64_t>)index];
}
template <typename T, typename I>
const std::string
ForthMachineOf<T, I>::string_at(int64_t index) const noexcept {
return ((index >= 0 && index < (int64_t)strings_.size()) ?
strings_[(IndexTypeOf<int64_t>)index] : std::string("a string at ")
+ std::to_string(index) + std::string(" is undefined"));
}
template <typename T, typename I>
void
ForthMachineOf<T, I>::reset() {
stack_depth_ = 0;
for (IndexTypeOf<int64_t> i = 0; i < variables_.size(); i++) {
variables_[i] = 0;
}
current_inputs_.clear();
current_outputs_.clear();
is_ready_ = false;
recursion_current_depth_ = 0;
while (!recursion_target_depth_.empty()) {
recursion_target_depth_.pop();
}
do_current_depth_ = 0;
current_error_ = util::ForthError::none;
}
template <typename T, typename I>
void
ForthMachineOf<T, I>::begin(
const std::map<std::string, std::shared_ptr<ForthInputBuffer>>& inputs) {
reset();
current_inputs_ = std::vector<std::shared_ptr<ForthInputBuffer>>();
for (auto name : input_names_) {