-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine.cpp
1676 lines (1526 loc) · 42.3 KB
/
machine.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
#include <cmath>
#include "machine.h"
#include "types.h"
#include "to_hanzi.h"
#include "debug.h"
uint32_t machine_c::lex(std::string_view cjk, std::vector<command_t>& destination)
/*
takes a string view and trawls it codepoint by codepoint
pushes back results into destination vector
results are of the type command_t := struct { instruction, value }
*/
{
destination.clear();
uint32_t amount_written = 0;
std::string_view::iterator beg_itr = cjk.begin();
std::string_view::iterator end_itr = cjk.end();
if(beg_itr == end_itr) { return -1; }
if(!utf8::is_valid(cjk)) { return -1; }
std::string_view::iterator index = beg_itr;
while(index != end_itr)
{
uint32_t codepoint = utf8::next(index, end_itr);
switch(codepoint)
{
case 0x00 ... 0x1f:
//destination.push_back({ERROR,{0,0}});
// going to ignore these
break;
case 0x20 ... 0x2e79: // everything up to CJK is a comment for now
if(__DEBUG) { destination.push_back({COMM, {COMM_T, codepoint}}); }
break;
case 0x4f4e: // 低 - down/decrement = dai1
destination.push_back({DECREMENT, {0,0}});
break;
case 0x52a0: // 加 - add = gaa1
destination.push_back({ADD, {0,0}});
break;
case 0x6e1b: // 減 - sub = gam2
destination.push_back({SUB, {0,0}});
break;
case 0x9664: // 除 - div = cui3
destination.push_back({DIV, {0,0}});
break;
case 0x4e58: // 乘 - mul = sing4
destination.push_back({MUL, {0,0}});
break;
case 0x9918: // 餘 - remainder = jyu4
destination.push_back({MOD, {0,0}});
break;
case 0x57FA: // 基 - base = gei1
destination.push_back({LOG_E, {0,0}});
break;
case 0x6839: // 根 - root = gan1
destination.push_back({ROOT, {0,0}});
break;
case 0x51aa: // 冪 - exponent = mik6
destination.push_back({POW, {0,0}});
break;
case 0x5638: // 嘸 - nothing = m4
destination.push_back({NOTHING,{0,0}});
break;
case 0x5831: // 報 - report = bou3
destination.push_back({REPORT,{0,0}});
break;
case 0x589e: // 增 - add/expand = zang1
destination.push_back({INCREMENT,{0,0}});
break;
case 0x5b56: // 孖 - twin = maa1
destination.push_back({TWIN, {0,0}});
break;
case 0x5b89: // 安 - where[literary] = on1
destination.push_back({WHERE, {0,0}});
break;
case 0x63db: // 換 - swap = wun6
destination.push_back({SWAP, {0,0}});
break;
case 0x64f0: // 擰 - turn around/spin/rotate = ning6
destination.push_back({ROTATE, {0,0}});
break;
case 0x68c4: // 棄 - discard/abandon = hei3
destination.push_back({ABANDON,{0,0}});
break;
case 0x6b7b: // 死 - die = sei2
destination.push_back({DIE, {0, 0}});
break;
case 0x6bb2: // 殲 - destroy = cim1
destination.push_back({DESTROY,{0,0}});
break;
case 0x7d21: // 紡 - spin/swap3 = fong2
destination.push_back({SWAP3, {0,0}});
break;
case 0x843d: // 落 - fall = lok6
destination.push_back({FALL, {0,0}});
break;
case 0x8df3: // 跳 - jump = tiu3
destination.push_back({JUMP, {0,0}});
break;
case 0x96f6: // 零 - zero = leng4
destination.push_back({PSH_ZERO,{0, 0}});
break;
case 0x58f9: // 壹 - one = jat1
destination.push_back({PSH_ONE ,{0,0}});
break;
case 0x8cb3: // 貳 - two = ji6
destination.push_back({PSH_TWO ,{0,0}});
break;
case 0x53c3: // 參 - three = saam1
destination.push_back({PSH_THREE,{0,0}});
break;
case 0x8086: // 肆 - four = sei3
destination.push_back({PSH_FOUR,{0,0}});
break;
case 0x4f0d: // 伍 - five = ng5
destination.push_back({PSH_FIVE,{0,0}});
break;
case 0x9678: // 陸 - six = luk6
destination.push_back({PSH_SIX ,{0,0}});
break;
case 0x67d2: // 柒 - seven = cat1
destination.push_back({PSH_SEVEN,{0,0}});
break;
case 0x634c: // 捌 - eight = baat3
destination.push_back({PSH_EIGHT,{0,0}});
break;
case 0x7396: // 玖 - nine = gau2
destination.push_back({PSH_NINE,{0,0}});
break;
case 0x62fe: // 拾 - ten = sap6
destination.push_back({PSH_TEN ,{0,0}});
break;
case 0x53DB: // 叛 - betray = bun6
destination.push_back({BETRAY, {0,0}});
break;
case 0x6574: // 整 - round = zing2
destination.push_back({ROUND, {0,0}});
break;
case 0x5f4e: // 彎 - bend = waan1
destination.push_back({BEND, {0,0}});
break;
case 0x985e: // 類 - type = leoi6
destination.push_back({TYPE, {0,0}});
break;
case 0x5ba3: // 宣 - register = syun1
destination.push_back({REGISTER, {0,0}});
break;
case 0x7d42: // 終 - conclude = zung1
destination.push_back({CONCLUDE, {0,0}});
break;
case 0x55cc: // 嗌 - yell = jik1
destination.push_back({INVOKE, {0,0}});
break;
case 0x79fb: // 移 - transplant/move = ji4
destination.push_back({TRANSPL, {0,0}});
break;
case 0x8d8a: // 越 - superior/exceed = jyut6
destination.push_back({BIGGER, {0,0}});
break;
case 0x6562: // 敢 - can it be so? = gam2
destination.push_back({IS_IT_SO, {0,0}});
break;
case 0x968E: // 階 - steps/scale = gaai1
destination.push_back({FACTL, {0,0}});
break;
case 0x54e5: // 哥 - cousin/older bro = go1
destination.push_back({COUSIN, {0,0}});
break;
case 0x7b54: // 答 - respond/reply = daap3
destination.push_back({RESPOND, {0,0}});
break;
case 0x8b6f: // 譯 - interpret = jit6
destination.push_back({INTERPRET, {0,0}});
break;
case 0x8ade: // 諞 - swindle = pin5
destination.push_back({SWINDLE, {0,0}});
break;
default:
destination.push_back({LABEL, {LABL_T, codepoint}});
break;
}
amount_written++;
}
return amount_written;
}
uint32_t machine_c::run(int ticks)
/*
this method trawls the commands vector, executes the
commands one by one, and returns the number of commands
executed
arguments:
- int ticks := number of commands to run, default 1 << 23 - 1
means no stopping
when arg1 specified, a well-formed program should
return a number up to the number it was given, or less
when arg1 unspecified, it times out at 2^23 insns
to prevent infinite loops
return:
- int24_t := number of commands that were run
negative value means the program timed out
(this means that bit 23 was set in the return value
since there is no native 24-bit integer type in cpp)
(val & 0x80_00_00)
*/
{
if(commands.empty()) { return -1; } // naughty naughty!
response.clear();
int32_t elapsed = 0;
size_t command_ptr = 0;
size_t commands_sz = commands.size();
uint8_t temp_type= 0;
uint32_t temp_u32 = 0;
int32_t temp_i32 = 0;
uint64_t temp_u64 = 0;
int64_t temp_i64 = 0;
float temp_f32 = 0.0;
double temp_f64 = 0.0;
vm_t temp_vmt = {0,0};
while((elapsed < ticks) and (elapsed < 0x80'00'00))
{
elapsed++;
switch(commands[command_ptr].instruction)
{
/*
junk "instructions"
*/
case ZERO:
if(__DEBUG) { std::printf("debug: ZERO\n"); }
elapsed--;
break;
case COMM:
elapsed--;
if(__DEBUG) {
unsigned char u[5] = {0,0,0,0,0};
utf8::append(commands[command_ptr].value.value, u);
std::printf("debug: --- %s - %#x @ %d\n", u, commands[command_ptr].value.value, int(command_ptr));
}
break;
/*
instructions that modify machine internals
*/
case ROTATE: // 擰 swaps around A and B
if(__DEBUG) { std::printf("debug: 擰 ROTATE @ %d\n", int(command_ptr));}
if(swap_ab()) { break; }
else
{
std::printf("FATAL ERROR: 擰 @ %d after %d failed; terminating\n", int(command_ptr), elapsed);
goto exit_loop;
}
break;
/*
typesystem-related ops
*/
case TYPE: // 類 (?) -> (?) (typeid)
{
if(__DEBUG) { std::printf("debug: 類 TYPE @ %d\n", int(command_ptr));}
temp_vmt = peek_main();
push_side({TYPE_T, temp_vmt.type});
break;
}
case ROUND: // 整 (f24) -> (i24)
{
if(__DEBUG) { std::printf("debug: 整 ROUND @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
switch(temp_vmt.type)
{
case ERROR_T:
std::printf("CANNOT ROUND AN ERROR!\n");
break;
case INT24_T:
case UINT24_T:
case ADDR_T:
case INT48L_T:
break;
case F24_T:
temp_f32 = std::bit_cast<float>(temp_vmt.value << 8);
temp_vmt = {INT24_T, uint32_t(temp_f32)};
break;
default:
std::printf("type error: cannot round non-numeric typeid %#x\n", temp_vmt.type);
break;
}
push_main(temp_vmt);
break;
}
case BEND: // 彎 (i24) -> (f24)
// TODO : i48
{
if(__DEBUG) { std::printf("debug: 彎 BEND @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
switch(temp_vmt.type)
{
case ERROR_T:
std::printf("CANNOT BEMD AN ERROR!\n");
break;
case F24_T:
break;
case INT24_T:
case UINT24_T:
case ADDR_T:
temp_f32 = float(temp_vmt.value);
temp_vmt = {F24_T, std::bit_cast<uint32_t>(temp_f32) >> 8};
break;
default:
std::printf("type error: cannot bend non-numeric typeid %#x\n", temp_vmt.type);
break;
}
push_main(temp_vmt);
break;
}
/*
modifying stack contents without ops
*/
case ABANDON: // 棄 (i24) -> () pop without saving
if(__DEBUG) { std::printf("debug: 棄 ABANDON @ %d\n", int(command_ptr));}
remv_main();
break;
case DESTROY: // 殲 clear the current main stack
if(__DEBUG) { std::printf("debug: 殲 DESTROY @ %d\n", int(command_ptr));}
main -> clear();
break;
case PSH_ZERO: // 零 () -> (i24) push
if(__DEBUG) { std::printf("debug: 零 PSH_ZERO @ %d\n", int(command_ptr));}
push_main({INT24_T, 0x00});
break;
case PSH_ONE:
if(__DEBUG) { std::printf("debug: 壹 PSH_ONE @ %d\n", int(command_ptr));}
push_main({INT24_T, 0x01});
break;
case PSH_TWO:
if(__DEBUG) { std::printf("debug: 貳 PSH_TWO @ %d\n", int(command_ptr));}
push_main({INT24_T, 0x02});
break;
case PSH_THREE:
if(__DEBUG) { std::printf("debug: 參 PSH_THREE @ %d\n", int(command_ptr));}
push_main({INT24_T, 0x03});
break;
case PSH_FOUR:
if(__DEBUG) { std::printf("debug: 肆 PSH_FOUR @ %d\n", int(command_ptr));}
push_main({INT24_T, 0x04});
break;
case PSH_FIVE:
if(__DEBUG) { std::printf("debug: 伍 PSH_FIVE @ %d\n", int(command_ptr));}
push_main({INT24_T, 0x05});
break;
case PSH_SIX:
if(__DEBUG) { std::printf("debug: 陸 PSH_SIX @ %d\n", int(command_ptr));}
push_main({INT24_T, 0x06});
break;
case PSH_SEVEN:
if(__DEBUG) { std::printf("debug: 柒 PSH_SEVEN @ %d\n", int(command_ptr));}
push_main({INT24_T, 0x07});
break;
case PSH_EIGHT:
if(__DEBUG) { std::printf("debug: 捌 PSH_EIGHT @ %d\n", int(command_ptr));}
push_main({INT24_T, 0x08});
break;
case PSH_NINE:
if(__DEBUG) { std::printf("debug: 玖 PSH_NINE @ %d\n", int(command_ptr));}
push_main({INT24_T, 0x09});
break;
case PSH_TEN:
if(__DEBUG) { std::printf("debug: 拾 PSH_TEN @ %d\n", int(command_ptr));}
push_main({INT24_T, 0x0a});
break;
case TWIN: // 孖 (x) -> (x)(x) double the top of stack
if(__DEBUG) { std::printf("debug: 孖 TWIN @ %d\n", int(command_ptr));}
temp_vmt = peek_main();
push_main(temp_vmt);
break;
case COUSIN: // 哥 (x) -> (x) | (x) copy top of side to main
if(__DEBUG) { std::printf("debug: 哥 COUSIN @ %d\n", int(command_ptr));}
temp_vmt = peek_side();
push_main(temp_vmt);
break;
case TRANSPL: // 移 (x)a -> (x)b move from main to side
if(__DEBUG) { std::printf("debug: 移 TRANSPL @ %d\n", int(command_ptr));}
push_side(pop_main());
break;
case SWAP: // 換 - swap = wun6 (a)(b) -> (b)(a)
if(__DEBUG) { std::printf("debug: 換 SWAP @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
push_side(pop_main());
push_main(temp_vmt);
push_main(pop_side());
break;
case SWAP3: // 紡 (a)(b)(c) -> (c)(a)(b)
if(__DEBUG) { std::printf("debug: 紡 SWAP3 @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
push_side(pop_main());
push_side(pop_main());
push_main(temp_vmt);
push_main(pop_side());
push_main(pop_side());
break;
/* ---------------------------
arithmetic
*/
case INCREMENT:
/*
增 (i24) -> (i24);
(u24) -> (u24);
(f24) -> (f24);
(i48h)(i48l) -> (i48h)(i48l)
add 1
*/ // TODO : fix i48
{
if(__DEBUG) { std::printf("debug: 增 INCREMENT @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
switch(temp_vmt.type)
{
case ERROR_T:
std::printf("CANNOT INCREMENT AN ERROR!\n");
break;
case INT24_T:
case UINT24_T:
case ADDR_T:
temp_vmt.value += 1;
break;
case F24_T:
temp_f32
= std::bit_cast<float>(temp_vmt.value << 8);
temp_f32 += 1.0;
temp_vmt.value
= std::bit_cast<uint32_t>(temp_f32) >> 8;
break;
case INT48L_T:
temp_i64 = temp_vmt.value;
temp_vmt = pop_main();
if(temp_vmt.type != INT48H_T)
{
std::printf("MALFORMED I48!\n");
temp_vmt = {INT48L_T, uint32_t(temp_i64)};
break;
}
temp_i64 += uint64_t(temp_vmt.value) << 24;
temp_i64 += 1;
push_main({INT48H_T, uint32_t(temp_i64 >> 24)});
temp_vmt = {INT48L_T, uint32_t(temp_i64 & 0x7f'ff'ff)};
break;
default:
break;
}
push_main(temp_vmt);
temp_vmt = {0, 0};
break;
}
case DECREMENT:
/*
低 (i24) -> (i24);
(u24) -> (u24);
(f24) -> (f24);
(i48h)(i48l) -> (i48h)(i48l)
sub 1
*/ // TODO : fix i48
{
if(__DEBUG) { std::printf("debug: 低 DECREMENT @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
switch(temp_vmt.type)
{
case ERROR_T:
std::printf("CANNOT DECREMENT AN ERROR!\n");
break;
case INT24_T:
case UINT24_T:
case ADDR_T:
temp_vmt.value -= 1;
break;
case F24_T:
temp_f32
= std::bit_cast<float>(temp_vmt.value << 8);
temp_f32 -= 1.0;
temp_vmt.value
= std::bit_cast<uint32_t>(temp_f32) >> 8;
case INT48L_T:
temp_i64 = temp_vmt.value;
temp_vmt = pop_main();
if(temp_vmt.type != INT48H_T)
{
std::printf("MALFORMED I48!\n");
temp_vmt = {INT48L_T, uint32_t(temp_i64)};
break;
}
temp_i64 += uint64_t(temp_vmt.value) << 24;
temp_i64 -= 1;
push_main({INT48H_T, uint32_t(temp_i64 >> 24)});
temp_vmt = {INT48L_T, uint32_t(temp_i64 & 0x7f'ff'ff)};
break;
default:
break;
}
push_main(temp_vmt);
temp_vmt = {0, 0};
break;
}
case ADD:
/*
加 (i24)(i24) -> (i24);
(u24)(u24) -> (u24);
(f24)(f24) -> (f24);
(i48h)(i48l)
+ -----> (i48h)(i48l);
(i48h)(i48l)
add top two
*/ // TODO : i48
{
if(__DEBUG) { std::printf("debug: 加 ADD @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
switch(temp_vmt.type)
{
case ERROR_T:
std::printf("CANNOT ADD AN ERROR!\n");
break;
case INT24_T:
case UINT24_T:
case ADDR_T:
temp_type= temp_vmt.type;
temp_i32 = temp_vmt.value;
temp_vmt = pop_main();
if(temp_vmt.type == ERROR_T)
{
std::printf("insufficent arguments or unhandled error on stack\n");
break;
}
else if(temp_vmt.type > 0x06)
{
std::printf("type error: cannot add non-numeric typeid %#x\n", temp_vmt.type);
push_main(temp_vmt);
temp_vmt={temp_type, temp_i32};
break;
}
temp_i32 += temp_vmt.value;
temp_vmt = {temp_type, temp_i32};
break;
case F24_T:
temp_f32
= std::bit_cast<float>(temp_vmt.value << 8);
temp_vmt = pop_main();
if(temp_vmt.type == ERROR_T)
{
std::printf("insufficent arguments or unhandled error on stack\n");
break;
}
else if(temp_vmt.type > 0x06)
{
std::printf("type error: cannot add non-numeric typeid %#x\n", temp_vmt.type);
push_main(temp_vmt);
temp_vmt={temp_type, temp_i32};
break;
}
else if(temp_vmt.type < 0x05)
{
temp_f32 += float(temp_vmt.value);
} else
{ temp_f32 += std::bit_cast<float>(temp_vmt.value << 8); }
temp_vmt.value
= std::bit_cast<uint32_t>(temp_f32) >> 8;
temp_vmt.type = F24_T;
break;
}
push_main(temp_vmt);
temp_vmt = {0, 0};
break;
}
/*
case 0x9664: // 除 - div = cui3
destination.push_back({DIV, {0,0}});
break;
case 0x4e58: // 乘 - mul = sing4
destination.push_back({MUL, {0,0}});
break;
case 0x9918: // 餘 - remainder = jyu4
destination.push_back({MOD, {0,0}});
break;
*/
case SUB:
/*
減 (i24)(i24) -> (i24);
(u24)(u24) -> (u24);
(f24)(f24) -> (f24);
(i48h)(i48l)
- -----> (i48h)(i48l);
(i48h)(i48l)
sub top two
*/ // TODO : i48
{
if(__DEBUG) { std::printf("debug: 減 SUB @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
switch(temp_vmt.type)
{
case ERROR_T:
std::printf("CANNOT SUB AN ERROR!\n");
break;
case INT24_T:
case UINT24_T:
case ADDR_T:
temp_type= temp_vmt.type;
temp_i32 = temp_vmt.value;
temp_vmt = pop_main();
if(temp_vmt.type == ERROR_T)
{
std::printf("insufficent arguments or unhandled error on stack\n");
break;
}
else if(temp_vmt.type > 0x06)
{
std::printf("type error: cannot sub non-numeric typeid %#x\n", temp_vmt.type);
push_main(temp_vmt);
temp_vmt={temp_type, temp_i32};
break;
}
temp_i32 -= temp_vmt.value;
temp_vmt = {temp_type, temp_i32};
break;
case F24_T:
temp_f32
= std::bit_cast<float>(temp_vmt.value << 8);
temp_vmt = pop_main();
if(temp_vmt.type == ERROR_T)
{
std::printf("insufficent arguments or unhandled error on stack\n");
break;
}
else if(temp_vmt.type > 0x06)
{
std::printf("type error: cannot sub non-numeric typeid %#x\n", temp_vmt.type);
push_main(temp_vmt);
temp_vmt={temp_type, temp_i32};
break;
}
else if(temp_vmt.type < 0x05)
{
temp_f32 -= float(temp_vmt.value);
} else
{ temp_f32 -= std::bit_cast<float>(temp_vmt.value << 8); }
temp_vmt.value
= std::bit_cast<uint32_t>(temp_f32) >> 8;
temp_vmt.type = F24_T;
break;
}
push_main(temp_vmt);
temp_vmt = {0, 0};
break;
}
case DIV:
/*
除 (i24)(i24) -> (i24);
(u24)(u24) -> (u24);
(f24)(f24) -> (f24);
(i48h)(i48l)
/ -----> (i48h)(i48l);
(i48h)(i48l)
div top with top-1
*/ // TODO : i48
{
if(__DEBUG) { std::printf("debug: 除 DIV @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
switch(temp_vmt.type)
{
case ERROR_T:
std::printf("CANNOT DIV AN ERROR!\n");
break;
case INT24_T:
case UINT24_T:
case ADDR_T:
temp_type = temp_vmt.type;
temp_i32 = temp_vmt.value;
temp_vmt = pop_main();
if(temp_vmt.type == ERROR_T)
{
std::printf("insufficent arguments or unhandled error on stack\n");
break;
}
else if(temp_vmt.type > 0x06)
{
std::printf("type error: cannot div non-numeric typeid %#x\n", temp_vmt.type);
push_main(temp_vmt);
temp_vmt={temp_type, temp_i32};
break;
}
if(temp_vmt.value)
temp_i32 /= temp_vmt.value;
else
temp_i32 = 0xff'ff'ff;
temp_vmt = {temp_type, temp_i32};
break;
case F24_T:
temp_f32
= std::bit_cast<float>(temp_vmt.value << 8);
temp_vmt = pop_main();
if(temp_vmt.type == ERROR_T)
{
std::printf("insufficent arguments or unhandled error on stack\n");
break;
}
else if(temp_vmt.type > 0x06)
{
std::printf("type error: cannot div non-numeric typeid %#x\n", temp_vmt.type);
push_main(temp_vmt);
temp_vmt={temp_type, temp_i32};
break;
}
else if(temp_vmt.type < 0x05)
{
if(temp_vmt.value)
temp_f32 /= float(temp_vmt.value);
else
temp_f32 = std::bit_cast<float>(0x7F'80'00'00);
} else
{
if(temp_vmt.value)
temp_f32 /= std::bit_cast<float>(temp_vmt.value << 8);
else
temp_f32 = std::bit_cast<float>(0x7F'80'00'00);
}
temp_vmt.value
= std::bit_cast<uint32_t>(temp_f32) >> 8;
temp_vmt.type = F24_T;
break;
}
push_main(temp_vmt);
temp_vmt = {0, 0};
break;
}
case MUL:
/*
乘 (i24)(i24) -> (i24);
(u24)(u24) -> (u24);
(f24)(f24) -> (f24);
(i48h)(i48l)
* -----> (i48h)(i48l);
(i48h)(i48l)
mul top with top-1
*/ // TODO : i48
{
if(__DEBUG) { std::printf("debug: 乘 MUL @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
switch(temp_vmt.type)
{
case ERROR_T:
std::printf("CANNOT MUL AN ERROR!\n");
break;
case INT24_T:
case UINT24_T:
case ADDR_T:
temp_type = temp_vmt.type;
temp_i32 = temp_vmt.value;
temp_vmt = pop_main();
if(temp_vmt.type == ERROR_T)
{
std::printf("insufficent arguments or unhandled error on stack\n");
break;
}
else if(temp_vmt.type > 0x06)
{
std::printf("type error: cannot mul non-numeric typeid %#x\n", temp_vmt.type);
push_main(temp_vmt);
temp_vmt={temp_type, temp_i32};
break;
}
temp_i32 *= temp_vmt.value;
temp_vmt = {temp_type, temp_i32};
break;
case F24_T:
temp_f32
= std::bit_cast<float>(temp_vmt.value << 8);
temp_vmt = pop_main();
if(temp_vmt.type == ERROR_T)
{
std::printf("insufficent arguments or unhandled error on stack\n");
break;
}
else if(temp_vmt.type > 0x06)
{
std::printf("type error: cannot div non-numeric typeid %#x\n", temp_vmt.type);
push_main(temp_vmt);
temp_vmt={temp_type, temp_i32};
break;
}
else if(temp_vmt.type < 0x05)
{ temp_f32 *= float(temp_vmt.value); } else
{ temp_f32 *= std::bit_cast<float>(temp_vmt.value << 8); }
temp_vmt.value
= std::bit_cast<uint32_t>(temp_f32) >> 8;
temp_vmt.type = F24_T;
break;
}
push_main(temp_vmt);
temp_vmt = {0, 0};
break;
}
case MOD:
/*
餘 (i24)(i24) -> (i24);
(u24)(u24) -> (u24);
(f24)(f24) -> (f24);
(i48h)(i48l)
% -----> (i48h)(i48l);
(i48h)(i48l)
mod top with top-1
*/ // TODO : i48
{
if(__DEBUG) { std::printf("debug: 餘 MOD @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
switch(temp_vmt.type)
{
case ERROR_T:
std::printf("CANNOT MOD AN ERROR!\n");
break;
case INT24_T:
case UINT24_T:
case ADDR_T:
temp_type = temp_vmt.type;
temp_i32 = temp_vmt.value;
temp_vmt = pop_main();
if(temp_vmt.type == ERROR_T)
{
std::printf("insufficent arguments or unhandled error on stack\n");
break;
}
else if(temp_vmt.type > 0x06)
{
std::printf("type error: cannot mod non-numeric typeid %#x\n", temp_vmt.type);
push_main(temp_vmt);
temp_vmt={temp_type, temp_i32};
break;
}
temp_i32 %= temp_vmt.value;
temp_vmt = {temp_type, temp_i32};
break;
case F24_T:
temp_f32
= std::bit_cast<float>(temp_vmt.value << 8);
temp_vmt = pop_main();
if(temp_vmt.type == ERROR_T)
{
std::printf("insufficent arguments or unhandled error on stack\n");
break;
}
else if(temp_vmt.type > 0x06)
{
std::printf("type error: cannot mod non-numeric typeid %#x\n", temp_vmt.type);
push_main(temp_vmt);
temp_vmt={temp_type, temp_i32};
break;
}
else if(temp_vmt.type < 0x05)
{
temp_i32 = int(temp_f32 / float(temp_vmt.value));
temp_f32 = (temp_f32 / float(temp_vmt.value)) - temp_i32;
} else
{
temp_i32 = int(temp_f32 / std::bit_cast<float>(temp_vmt.value << 8));
temp_f32 = (temp_f32 / std::bit_cast<float>(temp_vmt.value << 8)) - temp_i32;
}
temp_vmt.value
= std::bit_cast<uint32_t>(temp_f32) >> 8;
temp_vmt.type = F24_T;
break;
}
push_main(temp_vmt);
temp_vmt = {0, 0};
break;
}
case LOG_E: // top log_e (numeric)->(f24)
// TODO : i48
{
if(__DEBUG) { std::printf("debug: 基 LOG_E @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
switch(temp_vmt.type)
{
case ERROR_T:
std::printf("CANNOT LOG_E AN ERROR!\n");
break;
case INT24_T:
case UINT24_T:
case ADDR_T:
temp_f32 = float(temp_vmt.value);
temp_f32 = std::log(temp_f32);
temp_vmt = {F24_T, std::bit_cast<uint32_t>(temp_f32) >> 8};
break;
case F24_T:
temp_f32 = std::bit_cast<float>(temp_vmt.value << 8);
temp_f32 = std::log(temp_f32);
temp_vmt = {F24_T, std::bit_cast<uint32_t>(temp_f32) >> 8};
break;
default:
std::printf("type error: cannot log_e non-numeric typeid %#x\n", temp_vmt.type);
break;
}
push_main(temp_vmt);
temp_vmt = {0, 0};
break;
}
case ROOT:
/*
(num)(num) -> (f24)
base root
*/
// TODO: i48
{
if(__DEBUG) { std::printf("debug: 根 ROOT @ %d\n", int(command_ptr));}
temp_vmt = pop_main();
temp_type = temp_vmt.type;
switch(temp_vmt.type)
{
case ERROR_T:
std::printf("CANNOT ROOT AN ERROR!\n");
push_main(temp_vmt);
goto end_root;
break;
case INT24_T:
case UINT24_T:
case ADDR_T:
temp_i32 = temp_vmt.value;
temp_f32 = float(temp_vmt.value);
break;
case F24_T:
temp_f32 = std::bit_cast<float>(temp_vmt.value << 8);
break;
default:
std::printf("type error: cannot root non-numeric typeid %#x\n", temp_vmt.type);
break;
}
temp_vmt = pop_main();