-
Notifications
You must be signed in to change notification settings - Fork 1
/
FACT_comp.c
1215 lines (1057 loc) · 42.3 KB
/
FACT_comp.c
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
/* This file is part of FACT.
*
* FACT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FACT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FACT. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FACT.h"
#include "FACT_vm.h"
#include "FACT_opcodes.h"
#include "FACT_parser.h"
#include "FACT_lexer.h"
#include "FACT_mpc.h"
#include "FACT_alloc.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
/* HIC SVNT DRACONES
* Let me elaborate: this code is really bad. A mix of gobbeldy gook and such.
* I'm guessing it's almost impossible to understand. Someday I'll fix it.
* Hopefully.
*/
/* Intermediete format created by the compiler. Essentially a tree. */
struct inter_node {
enum {
INSTRUCTION,
GROUPING
} node_type;
size_t line; /* Line the instruction relates to. 0 = none. */
struct inter_node *next;
union {
struct { /* Single instruction. */
enum Furlow_opcode inst_val;
struct inst_arg { /* Arguments. */
enum {
NO_ARG = 0,
REG_VAL,
ADDR_VAL,
INT_VAL,
STR_VAL
} arg_type;
union {
size_t addr;
unsigned char reg;
char *str;
} arg_val;
} args[4]; /* Max of four arguments. */
} inst;
struct { /* Grouping. */
struct inter_node **children;
size_t num_children;
} grouping;
} node_val;
};
static char *strlen_dq (char *);
static char *strlen_sq (char *);
static struct inter_node *create_node ();
static struct inter_node *compile_tree (FACT_tree_t, size_t, size_t, bool);
static struct inter_node *compile_args (FACT_tree_t);
static struct inter_node *compile_array_dec (FACT_tree_t);
static struct inter_node *compile_str_dq (char *, size_t);
static struct inter_node *compile_str_sq (char *, size_t);
static struct inter_node *begin_temp_scope ();
static struct inter_node *end_temp_scope ();
static struct inter_node *set_return_val ();
static void load (struct inter_node *, struct inter_node *, const char *);
static size_t weight (struct inter_node *);
static inline void spread (char *, size_t);
static inline void push_const (struct inter_node *, char *);
void FACT_compile (FACT_tree_t tree, const char *file_name, bool set_rx)
{
/* Lock the program for offset consistency. */
Furlow_lock_program ();
/* Compile and load. */
load (compile_tree (tree, 1, 0, set_rx), NULL, file_name);
/* Unlock the program. */
Furlow_unlock_program ();
}
static inline void add_instruction (struct inter_node *group, int inst,
struct inst_arg arg1,
struct inst_arg arg2,
struct inst_arg arg3) /* Add an instruction to a group. */
{
int inst_n;
inst_n = group->node_val.grouping.num_children++;
group->node_val.grouping.children[inst_n] = create_node ();
group->node_val.grouping.children[inst_n]->node_type = INSTRUCTION;
group->node_val.grouping.children[inst_n]->node_val.inst.inst_val = inst;
group->node_val.grouping.children[inst_n]->node_val.inst.args[0] = arg1;
group->node_val.grouping.children[inst_n]->node_val.inst.args[1] = arg2;
group->node_val.grouping.children[inst_n]->node_val.inst.args[2] = arg3;
}
static inline void set_child (struct inter_node *group, struct inter_node *val)
{
int inst_n;
inst_n = group->node_val.grouping.num_children++;
group->node_val.grouping.children[inst_n] = val;
}
/* The following functions are inlines that facilitate the passing of
* arguments to add_instruction.
*/
static inline struct inst_arg ignore (void) /* No argument */
{
struct inst_arg ret;
ret.arg_type = NO_ARG;
return ret;
}
static inline struct inst_arg reg_arg (unsigned char reg_num) /* Register value. */
{
struct inst_arg ret;
ret.arg_type = REG_VAL;
ret.arg_val.reg = reg_num;
return ret;
}
static inline struct inst_arg addr_arg (size_t node_num) /* Address value. */
{
struct inst_arg ret;
ret.arg_type = ADDR_VAL;
ret.arg_val.addr = node_num;
return ret;
}
static inline struct inst_arg int_arg (size_t num) /* Integer value. */
{
struct inst_arg ret;
ret.arg_type = INT_VAL;
ret.arg_val.addr = num; /* Just use addr. I'm so sleepy. */
return ret;
}
static inline struct inst_arg str_arg (char *str_val) /* String value. */
{
struct inst_arg ret;
ret.arg_type = STR_VAL;
ret.arg_val.str = str_val;
return ret;
}
/* Since I do not know of any compilation techniques, so it's sort of just ad-hoc.
* TODO: add argument checking for functions.
* Also, clean this up with some macros/inline functions.
*/
static struct inter_node *compile_tree (FACT_tree_t curr,
size_t s_count,
size_t l_count,
bool set_rx) /* Compile a tree, recursively. */
{
struct inter_node *res;
char *dims_str, *elems_str;
size_t i, j;
size_t dims, elems;
FACT_tree_t n;
static Furlow_opc_t lookup_table [] = {
[E_ADD] = ADD,
[E_SUB] = SUB,
[E_MUL] = MUL,
[E_DIV] = DIV,
[E_MOD] = MOD,
[E_ADD_AS] = ADD,
[E_SUB_AS] = SUB,
[E_MUL_AS] = MUL,
[E_DIV_AS] = DIV,
[E_MOD_AS] = MOD,
[E_NE] = CNE,
[E_EQ] = CEQ,
[E_MT] = CMT,
[E_ME] = CME,
[E_LT] = CLT,
[E_LE] = CLE,
[E_LOCAL_CHECK] = IS_AUTO,
[E_GLOBAL_CHECK] = IS_DEF,
};
if (curr == NULL)
return NULL;
res = create_node ();
res->line = curr->line;
switch (curr->id.id) {
case E_VAR:
res->node_type = INSTRUCTION;
if (!strcmp (curr->id.lexem, "this"))
res->node_val.inst.inst_val = THIS;
else if (!strcmp (curr->id.lexem, "lambda"))
res->node_val.inst.inst_val = LAMBDA;
else {
res->node_val.inst.inst_val = VAR;
res->node_val.inst.args[0] = str_arg (curr->id.lexem);
}
break;
case E_NUM:
#if 0
res->node_type = INSTRUCTION;
res->node_val.inst.inst_val = CONSTS;
res->node_val.inst.args[0] = str_arg (curr->id.lexem);
#endif
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *));
push_const (res, curr->id.lexem);
break;
case E_SQ:
case E_DQ:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 6);
/* Push the length of the string, as a string. */
/*
add_instruction (res, CONST, str_arg ((curr->id.id == E_SQ)
? strlen_sq (curr->children[0]->id.lexem)
: strlen_dq (curr->children[0]->id.lexem)), ignore (), ignore ());
*/
push_const (res, ((curr->id.id == E_SQ)
? strlen_sq (curr->children[0]->id.lexem)
: strlen_dq (curr->children[0]->id.lexem)));
// add_instruction (res, CONSTS, str_arg ("1") , ignore () , ignore ()); /* Push one to the stack. */
add_instruction (res, CONSTU, int_arg (1), ignore (), ignore ());
add_instruction (res, NEW_N, reg_arg (R_POP), ignore () , ignore ()); /* Create an anonymous array. */
add_instruction (res, REF , reg_arg (R_TOP), reg_arg (R_A), ignore ()); /* Set the A register to the array. */
// add_instruction (res, CONST, str_arg ("0") , ignore () , ignore ()); /* Push the string index to the stack (used for setting the string). */
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
/* Set the array to the string. */
set_child (res, ((curr->id.id == E_SQ)
? compile_str_sq (curr->children[0]->id.lexem, 0)
: compile_str_dq (curr->children[0]->id.lexem, 0)));
break;
case E_LOCAL_CHECK:
case E_GLOBAL_CHECK:
res->node_type = INSTRUCTION;
res->node_val.inst.inst_val = lookup_table[curr->id.id];
res->node_val.inst.args[0] = str_arg (curr->children[0]->id.lexem);
break;
case E_NEG:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 2);
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
add_instruction (res, NEG, reg_arg (R_TOP), ignore (), ignore ());
break;
case E_ADD:
case E_MUL:
case E_NE:
case E_EQ:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 4);
/* Create a temporary variable. */
// add_instruction (res, CONST, str_arg ("0"), ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
/* Compile the arguments. */
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
set_child (res, compile_tree (curr->children[1], 0, 0, set_rx));
add_instruction (res, lookup_table[curr->id.id], reg_arg (R_POP), reg_arg (R_POP), reg_arg (R_TOP));
break;
case E_SUB:
case E_DIV:
case E_MOD:
case E_MT:
case E_ME:
case E_LT:
case E_LE:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 4);
/* Create a temporary variable. */
// add_instruction (res, CONST, str_arg ("0"), ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
/* Compile the arguments. */
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
set_child (res, compile_tree (curr->children[1], 0, 0, set_rx));
add_instruction (res, lookup_table[curr->id.id], reg_arg (R_POP), reg_arg (R_POP), reg_arg (R_TOP));
break;
case E_ADD_AS:
case E_SUB_AS:
case E_MUL_AS:
case E_DIV_AS:
case E_MOD_AS:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 3);
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
set_child (res, compile_tree (curr->children[1], 0, 0, set_rx));
add_instruction (res, lookup_table[curr->id.id], reg_arg (R_POP), reg_arg (R_TOP), reg_arg (R_TOP));
break;
case E_AND:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 7);
// add_instruction (res, CONST, str_arg ("0"), ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
add_instruction (res, JIF, reg_arg (R_POP), addr_arg (6), ignore ());
set_child (res, compile_tree (curr->children[1], 0, 0, set_rx));
add_instruction (res, JIF , reg_arg (R_POP), addr_arg (6), ignore ());
add_instruction (res, DROP , ignore () , ignore () , ignore ());
// add_instruction (res, CONST, str_arg ("1") , ignore () , ignore ());
add_instruction (res, CONSTU, int_arg (1), ignore (), ignore ());
break;
case E_OR:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 7);
// add_instruction (res, CONST, str_arg ("1"), ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (1), ignore (), ignore ());
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
add_instruction (res, JIT, reg_arg (R_POP), addr_arg (6), ignore ());
set_child (res, compile_tree (curr->children[1], 0, 0, set_rx));
add_instruction (res, JIT , reg_arg (R_POP), addr_arg (6), ignore ());
add_instruction (res, DROP , ignore () , ignore () , ignore ());
// add_instruction (res, CONST, str_arg ("0") , ignore () , ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
break;
case E_ARRAY_ELEM:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 3);
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
set_child (res, compile_tree (curr->children[1], 0, 0, set_rx));
add_instruction (res, ELEM, reg_arg (R_POP), reg_arg (R_POP), ignore ());
break;
case E_OP_BRACK:
/* This isn't really working yet for scope arrays.
* 0: [First element]
* 1: jis,$top,@6
* 2: const,%1
* 3: dup
* 4: new_n,$pop
* 5: jmp,@9
* 6: const,%1
* 7: dup
* 8: new_s,$pop
* 9: ref,$top,$A
* 10: swap
* 11: const,%0
* 12: elem,$A,$pop
* 13: swap
* 14: sto,$pop,$pop
* 15: [The rest]
*/
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 16);
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
/* If the first element is a number: */
add_instruction (res, JIS , reg_arg (R_TOP), addr_arg (5), ignore ());
// add_instruction (res, CONST, str_arg ("1") , ignore () , ignore ());
add_instruction (res, CONSTU, int_arg (1), ignore (), ignore ());
add_instruction (res, DUP , ignore () , ignore () , ignore ());
add_instruction (res, NEW_N, reg_arg (R_POP), ignore () , ignore ());
add_instruction (res, JMP , addr_arg (8) , ignore () , ignore ());
/* If the first element is a scope: */
// add_instruction (res, CONST, str_arg ("1") , ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (1), ignore (), ignore ());
add_instruction (res, DUP , ignore () , ignore (), ignore ());
add_instruction (res, NEW_S, reg_arg (R_POP), ignore (), ignore ());
/* For all cases: */
add_instruction (res, REF , reg_arg (R_TOP), reg_arg (R_A) , ignore ());
add_instruction (res, SWAP , ignore () , ignore () , ignore ());
// add_instruction (res, CONST, str_arg ("0") , ignore () , ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
add_instruction (res, ELEM , reg_arg (R_A) , reg_arg (R_POP), ignore ());
add_instruction (res, SWAP , ignore () , ignore () , ignore ());
add_instruction (res, STO , reg_arg (R_POP), reg_arg (R_POP), ignore ());
set_child (res, compile_array_dec (curr->children[1]));
break;
case E_IN:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 5);
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
add_instruction (res, USE, reg_arg (R_POP), ignore (), ignore ());
set_child (res, compile_tree (curr->children[1], 0, 0, set_rx));
add_instruction (res, EXIT, ignore (), ignore (), ignore ());
add_instruction (res, DROP, ignore (), ignore (), ignore ());
break;
case E_IMP_DEF:
/* jis,%top,
* const,$0
* def_n,%pop,var_name
* jmp,@2
* const,$0
* def_s,%pop,var_name
*/
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 10);
set_child (res, compile_tree (curr->children[1], 0, 0, set_rx));
add_instruction (res, DUP, ignore (), ignore (), ignore ());
add_instruction (res, JIS, reg_arg (R_TOP), addr_arg (5), ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
add_instruction (res, DEF_N, reg_arg (R_POP), str_arg (curr->children[0]->id.lexem), ignore ());
add_instruction (res, JMP, addr_arg (7), ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
add_instruction (res, DEF_S, reg_arg (R_POP), str_arg (curr->children[0]->id.lexem), ignore ());
add_instruction (res, SWAP, ignore (), ignore (), ignore ());
add_instruction (res, STO, reg_arg (R_POP), reg_arg (R_POP), ignore ());
break;
case E_FUNC_CALL:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 12);
/* Compile the arguments being passed. */
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
add_instruction (res, LAMBDA, ignore (), ignore (), ignore ()); /* Create a lambda scope. */
/* Compile the function being called. */
set_child (res, compile_tree (curr->children[1], 0, 0, set_rx));
/* Set the up variable of the lambda scope to it. */
add_instruction (res, REF , reg_arg (R_POP), reg_arg (R_A) , ignore ());
add_instruction (res, USE , reg_arg (R_POP), ignore () , ignore ()); /* Briefly enter the scope to do so. */
// add_instruction (res, CONST, str_arg ("0") , ignore () , ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
add_instruction (res, DEF_S, reg_arg (R_POP), str_arg ("up") , ignore ());
add_instruction (res, STO , reg_arg (R_A) , reg_arg (R_POP), ignore ());
add_instruction (res, EXIT , ignore () , ignore () , ignore ());
add_instruction (res, SET_F, reg_arg (R_A) , reg_arg (R_TOP), ignore ()); /* Set the lambda scope's code address and call it. */
add_instruction (res, NAME , reg_arg (R_A) , reg_arg (R_TOP), ignore ()); /* Change the lambda scope's name to the function being called. */
add_instruction (res, CALL , reg_arg (R_POP), ignore () , ignore ());
break;
case E_FUNC_DEF:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 7);
add_instruction (res, JMP, addr_arg (4), ignore (), ignore ());
set_child (res, compile_args (curr->children[1]));
set_child (res, compile_tree (curr->children[2], 1, 0, set_rx));
// add_instruction (res, CONST, str_arg ("0"), ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
add_instruction (res, RET, ignore (), ignore (), ignore ());
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
add_instruction (res, SET_C, reg_arg (R_TOP), addr_arg (1), ignore ());
break;
case E_DEFUNC: /* Yes, there is a difference between this and FUNC_DEF. */
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 8);
add_instruction (res, JMP, addr_arg (4), ignore (), ignore ());
/* This is a little messed up because of how quick this was implemented. */
set_child (res, compile_args (curr->children[0]->children[1]));
set_child (res, compile_tree (curr->children[0]->children[2], 1, 0, set_rx));
// add_instruction (res, CONST, str_arg ("0"), ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
add_instruction (res, RET, ignore (), ignore (), ignore ());
// add_instruction (res, CONST, str_arg ("0"), ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
add_instruction (res, DEF_S, reg_arg (R_POP), str_arg (curr->children[0]->id.lexem), ignore ());
add_instruction (res, SET_C, reg_arg (R_TOP), addr_arg (1), ignore ());
break;
case E_RETURN:
/* Make sure that s_count != 0, so that we know we are in a scope that can return. */
assert (s_count != 0); /* Throw a compilation error here. */
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * (5 + (s_count - 1) * 2));
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
add_instruction (res, DUP, ignore (), ignore (), ignore ());
add_instruction (res, SWAP, ignore (), ignore (), ignore ());
add_instruction (res, DROP, ignore (), ignore (), ignore ());
/* Exit out of all the lambda scopes we need to before returning. */
for (i = 0; i < s_count - 1; i++) {
add_instruction (res, EXIT, ignore (), ignore (), ignore ());
add_instruction (res, DROP, ignore (), ignore (), ignore ());
}
add_instruction (res, RET, ignore (), ignore (), ignore ());
break;
case E_GIVE:
/* Make sure that s_count != 0, so that we know we are in a scope that can return. */
assert (s_count != 0); /* Throw a compilation error here. */
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * (2 + (s_count - 1) * 2));
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
/* Exit out of all the lambda scopes we need to before returning. */
for (i = 0; i < s_count - 1; i++) {
add_instruction (res, EXIT, ignore (), ignore (), ignore ());
add_instruction (res, DROP, ignore (), ignore (), ignore ());
}
add_instruction (res, RET, ignore (), ignore (), ignore ());
break;
case E_BREAK:
/* l_count != 0 for breaks. */
assert (l_count != 0);
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *)
* ((1 + s_count - l_count) * 2));
/* Exit out of all the lambda scopes we need to before breaking. */
for (i = 0; i < s_count - l_count; i++) {
add_instruction (res, EXIT, ignore (), ignore (), ignore ());
add_instruction (res, DROP, ignore (), ignore (), ignore ());
}
add_instruction (res, GOTO, reg_arg (R_POP), ignore (), ignore ());
break;
case E_SET:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 3);
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
set_child (res, compile_tree (curr->children[1], 0, 0, set_rx));
add_instruction (res, STO, reg_arg (R_POP), reg_arg (R_TOP), ignore ());
break;
case E_NUM_DEF:
case E_SCOPE_DEF:
/* Count the number of dimensions the variable has. */
dims = 0;
for (n = curr->children[0]; n != NULL; n = n->next)
dims++;
res->node_type = GROUPING;
if (dims == 0) { /* If there are no dimensions. */
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 2);
// add_instruction (res, CONST, str_arg ("0"), ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
} else {
/* Convert dims to a string. */
i = 0;
dims_str = NULL;
do {
dims_str = FACT_realloc (dims_str, i + 2);
dims_str[i++] = (dims % 10) + '0';
} while ((dims /= 10) > 0);
dims_str[i--] = '\0';
/* Reverse the string. Use XOR swap because I'd don't want to declare
* another variable in the case block.
*/
for (j = 0; j < i; j++, i--) {
dims_str[j] ^= dims_str[i];
dims_str[i] ^= dims_str[j];
dims_str[j] ^= dims_str[i];
}
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 3);
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
// add_instruction (res, CONST, str_arg (dims_str), ignore (), ignore ());
push_const (res, dims_str);
}
add_instruction (res, curr->id.id == E_NUM_DEF ? DEF_N : DEF_S,
reg_arg (R_POP), str_arg (curr->children[1]->id.lexem), ignore ());
break;
case E_CONST:
res->node_type = GROUPING;
if (curr->children[1] != NULL && curr->children[1]->id.id == E_SET) {
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 6);
// add_instruction (res, CONST, str_arg ("0"), ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
add_instruction (res, NEW_N, reg_arg (R_POP), ignore (), ignore ());
set_child (res, compile_tree (curr->children[2], 0, 0, set_rx));
add_instruction (res, STO, reg_arg (R_POP), reg_arg (R_TOP), ignore ());
} else {
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 10);
add_instruction (res, JMP, addr_arg (4), ignore (), ignore ());
set_child (res, compile_args (curr->children[1]));
set_child (res, compile_tree (curr->children[2], 1, 0, set_rx));
// add_instruction (res, CONST, str_arg ("0"), ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
add_instruction (res, RET, ignore (), ignore (), ignore ());
// add_instruction (res, CONST, str_arg ("0"), ignore (), ignore ());
add_instruction (res, CONSTU, int_arg (0), ignore (), ignore ());
add_instruction (res, NEW_S, reg_arg (R_POP), ignore (), ignore ());
add_instruction (res, SET_C, reg_arg (R_TOP), addr_arg (1), ignore ());
}
add_instruction (res, LOCK, reg_arg (R_TOP), ignore (), ignore ());
add_instruction (res, GLOBAL, reg_arg (R_TOP), str_arg (curr->children[0]->id.lexem), ignore ());
break;
case E_IF:
res->node_type = GROUPING;
if (curr->children[2] != NULL) {
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 5);
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
add_instruction (res, JIF, reg_arg (R_POP), addr_arg (3), ignore ());
set_child (res, compile_tree (curr->children[1], s_count, l_count, set_rx));
add_instruction (res, JMP, addr_arg (4), ignore (), ignore ());
set_child (res, compile_tree (curr->children[2], s_count, l_count, set_rx));
} else {
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 3);
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
add_instruction (res, JIF, reg_arg (R_POP), addr_arg (2), ignore ());
set_child (res, compile_tree (curr->children[1], s_count, l_count, set_rx));
}
break;
case E_WHILE:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 10);
/* Set the break point. */
add_instruction (res, JMP_PNT, addr_arg (6), ignore (), ignore ()); /* 0 */
set_child (res, begin_temp_scope ()); /* 1 */
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx)); /* 2 */
if (curr->children[0] == NULL)
set_child (res, NULL); /* 3 */
else
add_instruction (res, JIF, reg_arg (R_POP), addr_arg (6), ignore ()); /* 3 */
/* Do not create a new scope for brackets. */
if (curr->children[1] == NULL) {
set_child (res, NULL); /* 4 */
set_child (res, NULL); /* 5 */
} else if (curr->children[1]->id.id == E_OP_CURL) {
set_child (res, compile_tree (curr->children[1]->children[0], /* 4 */
s_count + 1, s_count + 1, set_rx));
set_child (res, NULL); /* 5 */
} else {
set_child (res, compile_tree (curr->children[1], /* 4 */
s_count + 1, s_count + 1, set_rx));
/* Drop the return value of every statement. */
add_instruction (res, DROP, ignore (), ignore (), ignore ()); /* 5 */
}
add_instruction (res, JMP, addr_arg (2), ignore (), ignore ()); /* 6 */
add_instruction (res, DROP, ignore (), ignore (), ignore ()); /* 7 */
set_child (res, end_temp_scope ()); /* 8 */
set_child (res, set_return_val ()); /* 9 */
break;
case E_FOR:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 12);
/* Set the break point. */
add_instruction (res, JMP_PNT, addr_arg (8), ignore (), ignore ());
set_child (res, begin_temp_scope ());
set_child (res, compile_tree (curr->children[0], 0, 0, set_rx));
set_child (res, compile_tree (curr->children[1], 0, 0, set_rx));
if (curr->children[1] == NULL)
set_child (res, NULL);
else
add_instruction (res, JIF, reg_arg (R_POP), addr_arg (8), ignore ());
if (curr->children[3] == NULL)
set_child (res, NULL);
/* Do not create a new scope for brackets. */
else if (curr->children[3]->id.id == E_OP_CURL)
set_child (res, compile_tree (curr->children[3]->children[0], s_count + 1, s_count + 1, set_rx));
else
set_child (res, compile_tree (curr->children[3], s_count + 1, s_count + 1, set_rx));
set_child (res, compile_tree (curr->children[2], 0, 0, set_rx));
if (curr->children[2] != NULL)
/* Drop the return value of every statement. */
add_instruction (res, DROP, ignore (), ignore (), ignore ());
else
set_child (res, NULL);
add_instruction (res, JMP, addr_arg (3), ignore (), ignore ());
add_instruction (res, DROP, ignore (), ignore (), ignore ());
set_child (res, end_temp_scope ());
set_child (res, set_return_val ());
break;
case E_CATCH:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 6);
add_instruction (res, TRAP_B, addr_arg (3), ignore (), ignore ());
set_child (res, compile_tree (curr->children[0], s_count, l_count, set_rx));
add_instruction (res, TRAP_E, ignore (), ignore (), ignore ());
add_instruction (res, JMP, addr_arg (5), ignore (), ignore ());
add_instruction (res, TRAP_E, ignore (), ignore (), ignore ());
set_child (res, compile_tree (curr->children[1], s_count, l_count, set_rx));
break;
case E_THREAD:
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 3);
add_instruction (res, SPRT, addr_arg (2), ignore (), ignore ());
set_child (res, compile_tree (curr->children[0], 0, 0, false));
add_instruction (res, DIE, ignore (), ignore (), ignore ());
break;
case E_OP_CURL: /* This REALLY needs to be optimized. */
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 3);
set_child (res, begin_temp_scope ());
set_child (res, compile_tree (curr->children[0], 1 + s_count, l_count, set_rx));
set_child (res, end_temp_scope ());
break;
case E_END:
case E_SEMI:
case E_CL_CURL:
/* Per every expression, pop the top element off the stack and set the
* X register to it (only set to X if set_rx is true). That way the stack
* will never overflow and the return value can still be checked.
*/
res->node_type = INSTRUCTION;
if (set_rx) {
res->node_val.inst.inst_val = REF;
res->node_val.inst.args[0].arg_type = REG_VAL;
res->node_val.inst.args[0].arg_val.reg = R_POP;
res->node_val.inst.args[1].arg_type = REG_VAL;
res->node_val.inst.args[1].arg_val.reg = R_X;
} else
res->node_val.inst.inst_val = DROP;
break;
default:
abort ();
}
/* Compile the next statement. */
res->next = compile_tree (curr->next, s_count, l_count, set_rx);
return res;
}
static char *strlen_dq (char *str) /* Get the length of a string and return it as a string of digits in base 10. */
{
size_t i, j;
size_t res_len;
char hold;
char *res;
res = FACT_malloc (2);
res_len = 2;
res[0] = '0';
res[1] = '\0';
for (i = 0; str[i] != '\0'; i++) {
/* Adjust for escape sequences. */
if (str[i] == '\\'
&& (str[i + 1] == '"'
|| str[i + 1] == 'r'
|| str[i + 1] == 'n'
|| str[i + 1] == 't'
|| str[i + 1] == '\\'))
i++;
/* Carry the one over. */
for (j = 0; j < res_len - 1; j++) {
if (res[j] != '9') {
res[j]++;
break;
} else
res[j] = '0'; /* Continue to carry over. */
}
if (j == res_len - 1) {
/* A new digit is needed. */
res = FACT_realloc (res, ++res_len);
res[j] = '1';
res[j + 1] = '\0';
}
}
/* Reverse the string. */
for (i = 0, j = res_len - 2; i < j; i++, j--) {
hold = res[j];
res[j] = res[i];
res[i] = hold;
}
return res;
}
static char *strlen_sq (char *str) /* Get the length of a string and return it as a string of digits in base 10.
* I know, that's pretty stupid.
*/
{
size_t i, j;
size_t res_len;
char hold;
char *res;
res = FACT_malloc (2);
res_len = 2;
res[0] = '0';
res[1] = '\0';
for (i = 0; str[i] != '\0'; i++) {
/* Adjust for escape sequences. */
if (str[i] == '\\'
&& (str[i + 1] == '\'' || str[i + 1] == '\\'))
i++;
/* Carry the one over. */
for (j = 0; j < res_len - 1; j++) {
if (res[j] != '9') {
res[j]++;
break;
} else
res[j] = '0'; /* Continue to carry over. */
}
if (j == res_len - 1) {
/* A new digit is needed. */
res = FACT_realloc (res, ++res_len);
res[j] = '1';
res[j + 1] = '\0';
}
}
/* Reverse the string. */
for (i = 0, j = res_len - 2; i < j; i++, j--) {
hold = res[j];
res[j] = res[i];
res[i] = hold;
}
return res;
}
static struct inter_node *compile_str_dq (char *str, size_t i) /* Compile a double-quotation string. */
{
char new;
char *c_val;
struct inter_node *res;
if (str[i] == '\0') {
/* End of the string. Drop the index. */
res = create_node ();
res->node_type = INSTRUCTION;
res->node_val.inst.inst_val = DROP;
return res;
}
if (str[i] == '\\') { /* Fix escape sequences. */
++i;
switch (str[i]) {
case '\0':
default:
--i;
/* Fall through. */
case '\\':
new = '\\';
break;
case '"':
new = '"';
break;
case 'n': /* Newline. */
new = '\n';
break;
case 'r': /* Carraige return. */
new = '\r';
break;
case 't': /* Tab. */
new = '\t';
break;
}
} else
new = str[i];
/* Convert the character value to a string. */
c_val = FACT_malloc (4);
c_val[0] = (new / 100 % 10) + '0';
c_val[1] = (new / 10 % 10) + '0';
c_val[2] = (new % 10) + '0';
c_val[3] = '\0';
res = create_node ();
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 6);
add_instruction (res, DUP, ignore (), ignore (), ignore ());
add_instruction (res, ELEM, reg_arg (R_A), reg_arg (R_POP), ignore ());
// add_instruction (res, CONST, str_arg (c_val), ignore (), ignore ());
push_const (res, c_val);
add_instruction (res, STO, reg_arg (R_POP), reg_arg (R_POP), ignore ());
add_instruction (res, INC, reg_arg (R_TOP), ignore (), ignore ());
/* Increase the index one and compile the next character. */
set_child (res, compile_str_dq (str, i + 1));
return res;
}
static struct inter_node *compile_str_sq (char *str, size_t i) /* Compile a single-quotation string. */
{
char new;
char *c_val;
struct inter_node *res;
if (str[i] == '\0') {
/* End of the string. Drop the index. */
res = create_node ();
res->node_type = INSTRUCTION;
res->node_val.inst.inst_val = DROP;
return res;
}
if (str[i] == '\\') { /* Fix escape sequences. */
++i;
switch (str[i]) {
case '\0':
default:
--i;
/* Fall through. */
case '\\':
new = '\\';
break;
case '\'':
new = '\'';
break;
}
} else
new = str[i];
/* Convert the character value to a string. This is kind of dumb. */
c_val = FACT_malloc (4);
c_val[0] = (new / 100 % 10) + '0';
c_val[1] = (new / 10 % 10) + '0';
c_val[2] = (new % 10) + '0';
c_val[3] = '\0';
res = create_node ();
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 6);
add_instruction (res, DUP, ignore (), ignore (), ignore ());
add_instruction (res, ELEM, reg_arg (R_A), reg_arg (R_POP), ignore ());
// add_instruction (res, CONST, str_arg (c_val), ignore (), ignore ());
push_const (res, c_val);
add_instruction (res, STO, reg_arg (R_POP), reg_arg (R_POP), ignore ());
add_instruction (res, INC, reg_arg (R_TOP), ignore (), ignore ());
/* Increase the index one and compile the next character. */
set_child (res, compile_str_sq (str, i + 1));
return res;
}
static struct inter_node *compile_array_dec (FACT_tree_t curr)
{
struct inter_node *res;
if (curr == NULL)
return NULL;
res = create_node ();
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 3);
set_child (res, compile_tree (curr->children[0], 0, 0, false));
add_instruction (res, APPEND, reg_arg (R_POP), reg_arg (R_TOP), ignore ());
set_child (res, compile_array_dec (curr->children[1]));
return res;
}
static struct inter_node *begin_temp_scope (void) /* Create a temporary scope with the up variable set. */
{
struct inter_node *res;
res = create_node ();
res->node_type = GROUPING;
res->node_val.grouping.children = FACT_malloc (sizeof (struct inter_node *) * 7);
/* Set the A register to the current scope for later use. */
add_instruction (res, THIS, ignore (), ignore (), ignore ());
add_instruction (res, REF, reg_arg (R_POP), reg_arg (R_A), ignore ());
add_instruction (res, LAMBDA, ignore (), ignore (), ignore ()); /* Create an anonymous scope. */