forked from mandrookin/yaforth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyaforth.cpp
More file actions
1338 lines (1184 loc) · 30.3 KB
/
Copy pathyaforth.cpp
File metadata and controls
1338 lines (1184 loc) · 30.3 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
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
// yaforth.cpp : This yet another Forth implementation
//
// With hope that this code useful stuff but without any warranty
//
// This file distributed under Xameleon Green License
//
//
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#include <direct.h>
#include <io.h>
#include <Windows.h>
#include <conio.h>
#define isatty _isatty
#else
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#define _getcwd getcwd
#endif
extern int read_key();
#include "yaforth.h"
#include "memory.h"
#include "generators.h"
#include <stdexcept>
//#define TRACE true
static std::stack<word_t> integer_stack;
static std::stack<word_t> return_stack;
static std::stack<word_t> condition_stack;
static std::vector<word_t> leave_counts;
static registry_t words;
static std::string func_name;
static ram_memory memory;
static int line_no;
static word_t base_address;
// Global internal states
options_t options;
state_t forth(const char* str);
uint32_t register_word(std::string &word, word_t address);
state_t register_variable(std::string &word);
state_t register_constant(std::string& word);
state_t execute();
bool is_compile_mode()
{
return !func_name.empty();
}
record_t* find_record(uint32_t h)
{
record_t* rec_ptr = nullptr;
if (words.count(h) > 0)
rec_ptr = &words[h];
return rec_ptr;
}
int get_stack_size()
{
return integer_stack.size();
}
#pragma region Snippets
state_t parse_number(std::string& number_str)
{
word_t n;
word_t base = memory.get(base_address);
int mask = 0;
switch (base)
{
case 2:
fprintf(stderr, "TODO: input in octal base\n");
return error;
case 8:
sscanf(number_str.c_str(), "%o ", &n);
break;
case 10:
n = std::stol(number_str);
break;
case 16:
sscanf(number_str.c_str(), "%x ", &n);
break;
default:
fprintf(stderr, "Only binary, octal, decimal and hexdecimal numbers allowed in this version\n");
return error;
}
memory.put(hash("^push"));
memory.put(n);
number_str.clear();
return neutral;
}
state_t show_words()
{
fprintf(stdout, "List of defined words:\n\n");
for (auto& word : words) {
fprintf(stdout, "%s ", word.second.NAME.c_str());
}
return neutral;
}
state_t register_function()
{
word_t w = memory.get();
return neutral;
}
state_t push_value()
{
word_t w = memory.get();
integer_stack.push(w);
#ifdef TRACE
printf("%d ", w);
#endif
return neutral;
}
state_t do_here()
{
word_t w = memory.get_current_address();
integer_stack.push(w);
#ifdef TRACE
printf("%d ", w);
#endif
return neutral;
}
state_t do_jump()
{
word_t w = memory.get();
memory.jump(w);
return neutral;
}
state_t to_R()
{
word_t n = integer_stack.top();
integer_stack.pop();
return_stack.push(n);
return neutral;
}
state_t from_R()
{
word_t n = return_stack.top();
return_stack.pop();
integer_stack.push(n);
return neutral;
}
state_t fetch_R()
{
word_t n = return_stack.top();
integer_stack.push(n);
return neutral;
}
state_t do_add()
{
word_t n1, n2;
n1 = integer_stack.top();
integer_stack.pop();
n2 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 + n2);
return neutral;
}
state_t do_sub()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 - n2);
return neutral;
}
state_t do_store()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
memory.set(n2, n1);
return neutral;
}
state_t do_fetch()
{
word_t n1;
n1 = integer_stack.top();
integer_stack.pop();
n1 = memory.get(n1);
integer_stack.push(n1);
return neutral;
}
state_t do_mul()
{
word_t n1, n2;
n1 = integer_stack.top();
integer_stack.pop();
n2 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 * n2);
return neutral;
}
state_t do_div()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 / n2);
return neutral;
}
state_t check_eq()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 == n2 ? -1 : 0);
return neutral;
}
state_t check_not_eq()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 != n2 ? -1 : 0);
return neutral;
}
state_t check_less()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 < n2 ? -1 : 0);
return neutral;
}
state_t check_more()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(signed(n1) > signed(n2) ? -1 : 0);
return neutral;
}
state_t op_and()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 & n2);
return neutral;
}
state_t op_or()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 | n2);
return neutral;
}
state_t op_xor()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 ^ n2);
return neutral;
}
state_t op_lshift()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 << n2);
return neutral;
}
state_t op_rshift()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 >> n2);
return neutral;
}
state_t invert()
{
word_t n = integer_stack.top();
integer_stack.pop();
integer_stack.push(~n);
return neutral;
}
state_t mod()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 % n2);
return neutral;
}
state_t divmod()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1 % n2);
integer_stack.push(n1 / n2);
return neutral;
}
state_t dup()
{
word_t n;
n = integer_stack.top();
integer_stack.push(n);
return neutral;
}
state_t swap()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n2);
integer_stack.push(n1);
return neutral;
}
state_t over()
{
word_t n1, n2;
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n1);
integer_stack.push(n2);
integer_stack.push(n1);
return neutral;
}
state_t rot()
{
word_t n1, n2, n3;
n3 = integer_stack.top();
integer_stack.pop();
n2 = integer_stack.top();
integer_stack.pop();
n1 = integer_stack.top();
integer_stack.pop();
integer_stack.push(n2);
integer_stack.push(n3);
integer_stack.push(n1);
return neutral;
}
state_t drop()
{
integer_stack.pop();
return neutral;
}
state_t do_dot()
{
if (integer_stack.empty())
{
printf("Stack underflow\n");
return error;;
}
word_t base = memory.get(base_address);
word_t val = integer_stack.top();
int mask = 0;
switch (base)
{
case 2:
mask = 0x80;
for (int i = 0; i < 8; i++)
{
fputc(mask & val ? '1' : '0', stdout);
mask >>= 1;
}
break;
case 8:
printf("0%o ", val);
break;
case 10:
printf("%d ", val);
break;
case 16:
printf("0x%x ", val);
break;
default:
fprintf(stderr,"Only binary, octal, decimal and hexdecimal numbers allowed in this version\n");
return error;
}
integer_stack.pop();
return neutral;
}
state_t get_key()
{
word_t ch = read_key();
integer_stack.push(ch);
return neutral;
}
state_t emit()
{
word_t ch = integer_stack.top();
integer_stack.pop();
putc(ch, stdout);
return neutral;
}
state_t do_print_string()
{
word_t a = memory.get_current_address();
a += 2;
word_t len = memory.get(a);
word_t word_count = (len + 3) / sizeof(word_t);
char* buffer = (char*)alloca((word_count + 1) << 2);
word_t* slots = (word_t*)buffer;
int i;
for (i = 0; i < word_count; i++)
slots[i] = memory.get(++a);
slots[i] = 0;
buffer[len] = 0;
printf("%s ", buffer);
fflush(stdout);
return neutral;
}
#pragma endregion
state_t do_begin()
{
word_t a = memory.get_current_address();
return_stack.push(a);
return_stack.push(a);
return neutral;
}
state_t do_until()
{
word_t i, a;
a = return_stack.top();
i = integer_stack.top();
integer_stack.pop();
if (i == -1 ) {
return_stack.pop();
return_stack.pop();
return neutral;
}
memory.jump(a);
return neutral;
}
state_t do_again()
{
word_t a;
a = return_stack.top();
memory.jump(a);
return neutral;
}
state_t do_loop()
{
word_t max, i;
// a = memory.get_current_address();
i = integer_stack.top();
integer_stack.pop();
max = integer_stack.top();
integer_stack.pop();
return_stack.push(max);
return_stack.push(i);
return neutral;
}
state_t prepare_enter_loop(word_t h)
{
word_t a;
memory.put(h);
a = memory.get_current_address();
return_stack.push(a); // loop start address
leave_counts.push_back(0);
return neutral;
}
state_t prepare_loop_exit(word_t h)
{
word_t ja = memory.get_current_address() + 2;
// update leave addresses
int idx = leave_counts.size();
if (idx > 0) {
word_t a;
--idx;
int count = leave_counts[idx];
while(count) {
a = return_stack.top();
return_stack.pop();
memory.set(a, ja);
--count;
}
leave_counts.pop_back();
}
word_t end;
word_t enter_loop = return_stack.top();
return_stack.pop();
memory.put(h);
memory.put(enter_loop);
end = memory.get_current_address();
if (end != ja) {
printf("something going wrong in prepare leave\n");
}
return neutral;
}
state_t do_leave()
{
return_stack.pop();
return_stack.pop();
word_t ra = memory.get();
memory.jump(ra);
return neutral;
}
state_t prepare_leave()
{
int idx = leave_counts.size();
if (idx < 1) {
printf("Misplaced operator 'leave'\n");
return error;
}
idx--;
word_t n = hash("^leave");
memory.put(n);
word_t a = memory.get_current_address();
memory.put(0);
return_stack.push(a);
leave_counts[idx]++;
return neutral;
}
state_t end_loop()
{
word_t a, i, max;
i = return_stack.top();
return_stack.pop();
max = return_stack.top();
i++;
if (i >= max) {
return_stack.pop();
a = memory.get();
return neutral;
}
return_stack.push(i);
a = memory.get();
memory.jump(a);
return neutral;
}
state_t end_plus_loop()
{
word_t a, i, max, step;
step = integer_stack.top();
integer_stack.pop();
i = return_stack.top();
return_stack.pop();
max = return_stack.top();
i+= step;
if (i >= max) {
return_stack.pop();
a = memory.get();
return neutral;
}
return_stack.push(i);
a = memory.get();
memory.jump(a);
return neutral;
}
state_t index_i()
{
word_t i = return_stack.top();
integer_stack.push(i);
return neutral;
}
state_t index_j()
{
word_t i = return_stack.top();
return_stack.pop();
word_t i_max = return_stack.top();
return_stack.pop();
word_t j = return_stack.top();
return_stack.push(i_max);
return_stack.push(i);
integer_stack.push(j);
return neutral;
}
state_t op_return()
{
word_t r = return_stack.top();
return_stack.pop();
memory.jump(r);
return neutral;
}
state_t do_condition()
{
word_t condition = integer_stack.top();
integer_stack.pop();
word_t a = memory.get();
if (condition == 0)
{
memory.jump(a);
}
return neutral;
}
state_t do_allot()
{
state_t state;
// debug
word_t ca = memory.get_current_address();
word_t ea = memory.get_execution_address();
ca = memory.get(ca);
ea = memory.get(ea);
record_t* ea_record = &words[ea];
record_t* ca_record = &words[ca];
if(memory.may_run())
state = execute();
word_t count = integer_stack.top();
integer_stack.pop();
if (count & 0x3) {
printf("TODO: fix variable alignment");
return error;
}
count >>= 2;
ca = memory.get_current_address() + count;
memory.jump(ca);
memory.update_execution_address();
return neutral;
}
#include <algorithm>
#include <cctype>
state_t check_item(std::string& item, state_t state)
{
uint32_t n, a, b;
uint32_t h = hash(item.c_str());
switch (h)
{
case hash(":"):
state = function_name;
break;
case hash(";"):
memory.put(h);
func_name.clear();
memory.update_execution_address();
state = neutral;
break;
case hash("if"):
memory.put(h);
n = memory.get_current_address();
condition_stack.push(n);
memory.put(0);
break;
case hash("else"):
n = hash("^jump");
memory.put(n);
b = memory.get_current_address();
memory.put(0);
a = memory.get_current_address();
n = condition_stack.top();
condition_stack.pop();
condition_stack.push(b);
memory.set(n, a);
break;
case hash("then"):
a = memory.get_current_address();
n = condition_stack.top();
condition_stack.pop();
memory.set(n, a);
break;
case hash("do"):
case hash("begin"):
prepare_enter_loop(h);
break;
case hash ("leave"):
prepare_leave();
break;
case hash("until"):
case hash("loop"):
case hash("+loop"):
case hash("again"):
prepare_loop_exit(h);
break;
case hash("variable"):
state = variable_state;
break;
case hash("constant"):
state = constant_value;;
break;
case hash("allot"):
state = do_allot();
break;
case hash(".\""):
state = print_string;
break;
case hash("\\"):
state = eol_comment;
break;
case hash("("):
state = forth_comment;
break;
case hash("bye"):
state = finish;
break;
case hash("hex"):
memory.put(h);
if (func_name.empty())
memory.set(base_address, 16);
break;
case hash("decimal"):
memory.put(h);
if(func_name.empty())
memory.set(base_address, 10);
break;
case hash("octal"):
memory.put(h);
if (func_name.empty())
memory.set(base_address, 8);
break;
default:
{
if (words.count(h) == 0)
{
std::string lowered = item;
std::transform(lowered.begin(), lowered.end(), lowered.begin(),
[](unsigned char c) { return std::tolower(c); });
uint32_t lower_h = hash(lowered.c_str());
if (words.count(lower_h) > 0) {
record_t* r = &words[lower_h];
if (r->TYPE == builtin || r->TYPE == variable || r->TYPE == user)
{
h = lower_h;
}
else {
printf("Warn: case conversion case word %s is %s\n", lowered.c_str(), item.c_str());
}
}
}
if (words.count(h) > 0)
{
record_t* r = &words[h];
switch (r->TYPE) {
case builtin:
case user:
memory.put(h);
break;
case constant:
memory.put(hash("^push"));
memory.put(r->CONSTANT);
break;
case variable:
memory.put(hash("^push"));
memory.put(r->ADDR);
break;
default:
return error;
}
}
else
{
char ch = item[0];
if (ch > 0)
{
if (isdigit(ch) || ch == '-') {
state = parse_number(item);
break;
}
if (ch >= 'a' && ch <= 'f' || ch >= 'A' && ch <= 'F') {
state = parse_number(item);
break;
}
}
printf("Line: %d Undefined name: %s\n", line_no, item.c_str());
state = error;
}
}
break;
}
item.clear();
return state;
}
#define CLEAN_AFTER_RUN
state_t execute()
{
word_t r;
#ifdef CLEAN_AFTER_RUN
word_t start = memory.get_current_address();
#endif
while (memory.running())
{
#ifdef TRACE
a = memory.get_current_address();
#endif
word_t cmd = memory.get();
if (words.count(cmd) == 0) {
printf("\nCommand not implemented\n");
return error;
}
record_t* rec = &words[cmd];
#ifdef TRACE
printf("\n%08x: %s ", a, rec->NAME.c_str());
#endif
if ( func_name.empty() && rec->MIN_STACK > integer_stack.size()) {
char err_buff[80];
snprintf(err_buff, 80, "%s[%u]: Stack underflow operation '%s'",
func_name.c_str(),
memory.get_current_address(),
rec->NAME.c_str()
);
r = memory.get_execution_address();
memory.jump(r);
throw std::out_of_range(std::string(err_buff));
}
switch (rec->TYPE)
{
case builtin:
rec->BLTN();
break;
case user:
r = memory.get_current_address();
return_stack.push(r);
memory.jump(rec->ADDR);
break;
case constant:
throw std::runtime_error("Constant data has no eXecute rights");
break;
case variable:
throw std::runtime_error("Variable has no eXecute rights");
break;
default:
throw std::runtime_error("Non implemented command type");
}
}
#ifdef CLEAN_AFTER_RUN
word_t stop = memory.get_finish();
for (int i = start; i < stop; i++)
{
memory.set(i, 0);
}
#endif
return neutral;
}
state_t forth(const char* str)
{
int ch;
std::string buffer;
int status = 0;
state_t state = neutral;
++line_no;
for (; state != error && state != done && state != finish;)
{
ch = *str++;
switch (state)
{
case neutral:
switch (ch)
{
case 0:
case '\n':
case '\t':
case ' ':
if (!buffer.empty()) {
#if TRACE
printf("TRACE %s\n", buffer.c_str());
#endif
state = check_item(buffer, state);
}
if (state == error || state == done)
continue;
if (func_name.empty() && (ch == '\n' || ch == 0)) {
if (memory.may_run())
state = execute();
}
if (ch == 0)
state = done;
continue;
default:
buffer += ch;
}
continue;
case eol_comment:
switch (ch)