-
Notifications
You must be signed in to change notification settings - Fork 98
/
stack.c
4849 lines (4125 loc) · 145 KB
/
stack.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
#include "flags.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include "defines.h"
#include "hashmap.h"
#include "parse.h"
#include "phase_1.h"
#include "phase_3.h"
#include "stack.h"
#include "include.h"
#include "printf.h"
#include "mersenne.h"
#include "main.h"
extern int g_input_number_error_msg, g_bankheader_status, g_input_float_mode, g_global_label_hint, g_is_data_stream_parser_enabled;
extern int g_source_index, g_source_file_size, g_parsed_int, g_macro_active, g_string_size, g_section_status, g_parse_floats;
extern char *g_buffer, *g_tmp, g_expanded_macro_string[256], g_label[MAX_NAME_LENGTH + 1];
extern struct map_t *g_defines_map;
extern struct active_file_info *g_active_file_info_first, *g_active_file_info_last, *g_active_file_info_tmp;
extern struct macro_runtime *g_macro_runtime_current;
extern struct section_def *g_sec_tmp;
extern struct export_def *g_export_first;
extern struct macro_runtime *g_macro_stack;
extern double g_parsed_double;
extern unsigned char g_asciitable[256];
extern int g_operand_hint, g_operand_hint_type, g_can_calculate_a_minus_b, g_expect_calculations, g_asciitable_defined;
extern int g_is_file_isolated_counter, g_force_add_namespace;
extern struct slot g_slots[256];
extern struct section_def *g_sections_first;
int g_latest_stack = 0, g_last_stack_id = 0, g_resolve_stack_calculations = YES, s_stack_calculations_max = 0;
int g_parsing_function_body = NO, g_fail_quetly_on_non_found_functions = NO, g_input_parse_if = NO, g_dsp_enable_label_address_conversion = YES;
int g_can_remember_converted_stack_items = YES;
static int s_delta_counter = 0, s_delta_section = -1, s_dsp_file_name_id = 0, s_dsp_line_number = 0;
static int s_converted_stack_items_count = 0;
static struct stack_item *s_delta_old_pointer = NULL;
static struct stack **s_stack_calculations = NULL;
static struct stack_item *s_converted_stack_items[64];
static struct stack_item *s_converted_stack_items_backups = NULL;
static int _resolve_string(struct stack_item *s, int *cannot_resolve);
PROFILE_GLOBALS_EXTERN();
static int _is_stack_condition(struct stack_item st[], int count) {
while (count > 0) {
if (st->type == STACK_ITEM_TYPE_OPERATOR && (st->value == SI_OP_COMPARE_EQ || st->value == SI_OP_COMPARE_NEQ || st->value == SI_OP_COMPARE_LT ||
st->value == SI_OP_COMPARE_GT || st->value == SI_OP_COMPARE_LTE || st->value == SI_OP_COMPARE_GTE ||
st->value == SI_OP_LOGICAL_AND || st->value == SI_OP_LOGICAL_OR))
return YES;
count--;
st++;
}
return NO;
}
static struct stack *_allocate_struct_stack(int items) {
struct stack *stack = calloc(sizeof(struct stack), 1);
if (stack == NULL) {
print_error(ERROR_STC, "Out of memory error while allocating room for a new calculation stack.\n");
return NULL;
}
init_stack_struct(stack);
stack->stacksize = items;
stack->stack_items = calloc(sizeof(struct stack_item) * items, 1);
if (stack->stack_items == NULL) {
free(stack);
print_error(ERROR_STC, "Out of memory error while allocating room for a new calculation stack.\n");
return NULL;
}
return stack;
}
void init_stack_struct(struct stack *s) {
s->stack_items = NULL;
s->id = -123456;
s->compressed_id = -123456;
s->position = STACK_POSITION_DEFINITION;
s->filename_id = -123456;
s->stacksize = 0;
s->linenumber = -123456;
s->type = STACK_TYPE_UNKNOWN;
s->bank = -123456;
s->slot = -123456;
s->relative_references = 0;
s->base = -123456;
/* NOTE! section_status is not really set anywhere, but phase_4.c uses it -> investigate */
s->section_status = 0;
s->section_id = -123456;
s->address = -123456;
s->special_id = 0;
s->bits_position = 0;
s->bits_to_define = 0;
s->is_function_body = NO;
s->is_bankheader_section = NO;
s->is_single_instance = NO;
s->has_been_calculated = NO;
s->value = 0.0;
}
int calculation_stack_insert(struct stack *s) {
if (g_bankheader_status == OFF) {
/* outside bankheader sections */
s->is_bankheader_section = NO;
}
else {
/* inside a bankheader section */
s->is_bankheader_section = YES;
}
s->id = g_last_stack_id;
s->section_status = g_section_status;
if (g_section_status == ON)
s->section_id = g_sec_tmp->id;
else
s->section_id = 0;
g_latest_stack = g_last_stack_id;
g_last_stack_id++;
if (g_latest_stack >= s_stack_calculations_max) {
/* enlarge the pointer array! */
s_stack_calculations_max += 4096;
s_stack_calculations = realloc(s_stack_calculations, sizeof(struct stack *) * s_stack_calculations_max);
if (s_stack_calculations == NULL) {
print_error(ERROR_NUM, "Out of memory error while trying to enlarge stack calculations pointer array!\n");
return FAILED;
}
}
s_stack_calculations[g_latest_stack] = s;
return SUCCEEDED;
}
void free_stack_calculations(void) {
int i;
for (i = 0; i < g_last_stack_id; i++) {
if (s_stack_calculations[i] != NULL)
delete_stack_calculation_struct(s_stack_calculations[i]);
}
free(s_stack_calculations);
s_stack_calculations = NULL;
g_latest_stack = 0;
g_last_stack_id = 0;
s_stack_calculations_max = 0;
free(s_converted_stack_items_backups);
s_converted_stack_items_backups = NULL;
}
void delete_stack_calculation_struct(struct stack *s) {
if (s == NULL)
print_error(ERROR_WRN, "Deleting a non-existing computation stack! Please submit a bug report!\n");
else {
s_stack_calculations[s->id] = NULL;
free(s->stack_items);
free(s);
}
}
struct stack *find_stack_calculation(int id, int print_error_message) {
struct stack *s;
if (id < 0 || id >= g_last_stack_id) {
if (print_error_message == YES)
print_error(ERROR_NUM, "Stack calculation %d is out of bounds [0, %d]! Please submit a bug report!\n", id, g_last_stack_id);
return NULL;
}
s = s_stack_calculations[id];
if (s == NULL) {
if (print_error_message == YES)
print_error(ERROR_NUM, "Stack calculation %d has gone missing! Please submit a bug report!\n", id);
}
return s;
}
int compress_stack_calculation_ids(void) {
struct stack **stack_calculations = NULL;
struct export_def *export_tmp;
int i, compressed_id = 0;
/* 1. give stack calculations new IDs */
/* pending calculations we'll export come first */
for (i = 0; i < g_last_stack_id; i++) {
struct stack *s = s_stack_calculations[i];
if (s == NULL)
continue;
if (s->is_function_body == YES)
continue;
s->compressed_id = compressed_id++;
}
/* next come the pending calculations we will not export */
for (i = 0; i < g_last_stack_id; i++) {
struct stack *s = s_stack_calculations[i];
if (s == NULL)
continue;
if (s->is_function_body == NO)
continue;
s->compressed_id = compressed_id++;
}
/* 2. reorder the stack calculations into a new pointer array */
if (s_stack_calculations_max > 0) {
stack_calculations = calloc(sizeof(struct stack *) * s_stack_calculations_max, 1);
if (stack_calculations == NULL) {
print_error(ERROR_NUM, "Out of memory error while trying to reorder stack calculations pointer array! s_stack_calculations_max = %d!\n", s_stack_calculations_max);
return FAILED;
}
for (i = 0; i < g_last_stack_id; i++) {
struct stack *s = s_stack_calculations[i];
if (s == NULL)
continue;
stack_calculations[s->compressed_id] = s;
}
}
/* 3. update all stack calculation IDs in the stack calculations */
for (i = 0; i < g_last_stack_id; i++) {
struct stack *s = s_stack_calculations[i];
int j;
if (s == NULL)
continue;
for (j = 0; j < s->stacksize; j++) {
if (s->stack_items[j].type == STACK_ITEM_TYPE_STACK) {
struct stack *s2 = s_stack_calculations[(int)s->stack_items[j].value];
s->stack_items[j].value = (double)s2->compressed_id;
}
}
}
/* 4. update all definitions that contain stack calculation IDs */
export_tmp = g_export_first;
while (export_tmp != NULL) {
struct definition *tmp_def;
hashmap_get(g_defines_map, export_tmp->name, (void*)&tmp_def);
if (tmp_def != NULL) {
if (tmp_def->type == DEFINITION_TYPE_STACK) {
struct stack *s = s_stack_calculations[(int)tmp_def->value];
if (s != NULL)
tmp_def->value = (double)s->compressed_id;
}
}
export_tmp = export_tmp->next;
}
/* 5. delete the old array, replace it with the new array */
free(s_stack_calculations);
s_stack_calculations = stack_calculations;
/* 6. finalize the ID changes */
for (i = 0; i < g_last_stack_id; i++) {
struct stack *s = s_stack_calculations[i];
if (s != NULL)
s->id = s->compressed_id;
}
return SUCCEEDED;
}
static int _break_before_value_or_string(int i, struct stack_item *si) {
/* we use this function to test if the previous item in the stack
is something that cannot be followed by a value or a string.
in such a case we'll stop adding items to this stack computation */
if (i <= 0)
return FAILED;
si = &si[i-1];
if (si->type == STACK_ITEM_TYPE_VALUE)
return SUCCEEDED;
if (si->type == STACK_ITEM_TYPE_STRING)
return SUCCEEDED;
if (si->type == STACK_ITEM_TYPE_LABEL)
return SUCCEEDED;
if (si->type == STACK_ITEM_TYPE_OPERATOR && si->value == SI_OP_RIGHT)
return SUCCEEDED;
return FAILED;
}
#if defined(WLA_DEBUG)
static void _debug_print_stack(int line_number, int stack_id, struct stack_item *ta, int count, int id, struct stack *stack) {
int k;
print_text(YES, "LINE %5d: ID = %d (STACK) CALCULATION ID = %d (c%d) ", line_number, id, stack_id, stack_id);
if (stack == NULL)
print_text(YES, "FB?: ");
else if (stack->is_function_body == YES)
print_text(YES, "FBy: ");
else
print_text(YES, "FBn: ");
for (k = 0; k < count; k++) {
char ar[] = "+-*()|&/^01%~<>!:<>";
int add_sign = YES;
if (ta[k].type == STACK_ITEM_TYPE_DELETED)
continue;
if (ta[k].type == STACK_ITEM_TYPE_OPERATOR) {
int value = (int)ta[k].value;
if (!(value == SI_OP_ROUND ||
value == SI_OP_CEIL ||
value == SI_OP_FLOOR ||
value == SI_OP_MIN ||
value == SI_OP_MAX ||
value == SI_OP_SQRT ||
value == SI_OP_COS ||
value == SI_OP_SIN ||
value == SI_OP_TAN ||
value == SI_OP_ACOS ||
value == SI_OP_ASIN ||
value == SI_OP_ATAN ||
value == SI_OP_ATAN2 ||
value == SI_OP_COSH ||
value == SI_OP_SINH ||
value == SI_OP_TANH ||
value == SI_OP_LOG ||
value == SI_OP_LOG10 ||
value == SI_OP_POW ||
value == SI_OP_LOW_BYTE ||
value == SI_OP_HIGH_BYTE ||
value == SI_OP_LOW_WORD ||
value == SI_OP_HIGH_WORD ||
value == SI_OP_BANK_BYTE ||
value == SI_OP_BANK ||
value == SI_OP_CLAMP ||
value == SI_OP_SIGN ||
value == SI_OP_ABS))
add_sign = NO;
}
if (add_sign == YES) {
if (ta[k].sign == SI_SIGN_POSITIVE)
print_text(YES, "+");
else
print_text(YES, "-");
}
if (ta[k].can_calculate_deltas == YES)
print_text(YES, "@");
else if (ta[k].can_calculate_deltas == NOT_APPLICABLE)
print_text(YES, "*");
if (ta[k].type == STACK_ITEM_TYPE_OPERATOR) {
int value = (int)ta[k].value;
if (value == SI_OP_SHIFT_LEFT)
print_text(YES, "<<");
else if (value == SI_OP_SHIFT_RIGHT)
print_text(YES, ">>");
else if (value == SI_OP_COMPARE_EQ)
print_text(YES, "==");
else if (value == SI_OP_COMPARE_NEQ)
print_text(YES, "!=");
else if (value == SI_OP_COMPARE_LTE)
print_text(YES, "<=");
else if (value == SI_OP_COMPARE_GTE)
print_text(YES, ">=");
else if (value == SI_OP_COMPARE_LT)
print_text(YES, "< (LT)");
else if (value == SI_OP_COMPARE_GT)
print_text(YES, "> (GT)");
else if (value == SI_OP_LOGICAL_OR)
print_text(YES, "||");
else if (value == SI_OP_LOGICAL_AND)
print_text(YES, "&&");
else if (value == SI_OP_LOW_WORD)
print_text(YES, "loword(a)");
else if (value == SI_OP_HIGH_WORD)
print_text(YES, "hiword(a)");
else if (value == SI_OP_BANK_BYTE)
print_text(YES, "bankbyte(a)");
else if (value == SI_OP_ROUND)
print_text(YES, "round(a)");
else if (value == SI_OP_CEIL)
print_text(YES, "ceil(a)");
else if (value == SI_OP_FLOOR)
print_text(YES, "floor(a)");
else if (value == SI_OP_MIN)
print_text(YES, "min(a,b)");
else if (value == SI_OP_MAX)
print_text(YES, "max(a,b)");
else if (value == SI_OP_SQRT)
print_text(YES, "sqrt(a)");
else if (value == SI_OP_ABS)
print_text(YES, "abs(a)");
else if (value == SI_OP_COS)
print_text(YES, "cos(a)");
else if (value == SI_OP_SIN)
print_text(YES, "sin(a)");
else if (value == SI_OP_TAN)
print_text(YES, "tan(a)");
else if (value == SI_OP_ACOS)
print_text(YES, "acos(a)");
else if (value == SI_OP_ASIN)
print_text(YES, "asin(a)");
else if (value == SI_OP_ATAN)
print_text(YES, "atan(a)");
else if (value == SI_OP_ATAN2)
print_text(YES, "atan2(a,b)");
else if (value == SI_OP_NEGATE)
print_text(YES, "negate(a)");
else if (value == SI_OP_COSH)
print_text(YES, "cosh(a)");
else if (value == SI_OP_SINH)
print_text(YES, "sinh(a)");
else if (value == SI_OP_TANH)
print_text(YES, "tanh(a)");
else if (value == SI_OP_LOG)
print_text(YES, "log(a)");
else if (value == SI_OP_LOG10)
print_text(YES, "log10(a)");
else if (value == SI_OP_POW)
print_text(YES, "pow(a,b)");
else if (value == SI_OP_CLAMP)
print_text(YES, "pow(v,min,max)");
else if (value == SI_OP_SIGN)
print_text(YES, "sign(a)");
else {
if (value >= (int)strlen(ar)) {
print_text(YES, "ERROR!\n");
print_text(YES, "_debug_print_stack(): ERROR: Unhandled SI_OP_* (%d)! Please submit a bug report!\n", value);
exit(1);
}
print_text(YES, "%c", ar[value]);
}
}
else if (ta[k].type == STACK_ITEM_TYPE_VALUE)
print_text(YES, "V(%f)", ta[k].value);
else if (ta[k].type == STACK_ITEM_TYPE_STACK)
print_text(YES, "C(%d)", (int)ta[k].value);
else if (ta[k].type == STACK_ITEM_TYPE_STRING)
print_text(YES, "S(%s)", ta[k].string);
else if (ta[k].type == STACK_ITEM_TYPE_LABEL)
print_text(YES, "L(%s)", ta[k].string);
else
print_text(YES, "?");
if (k < count-1)
print_text(YES, ", ");
}
print_text(YES, "\n");
}
#endif
int get_label_length(char *l) {
struct definition *tmp_def;
int length;
hashmap_get(g_defines_map, l, (void*)&tmp_def);
if (tmp_def != NULL) {
if (tmp_def->type == DEFINITION_TYPE_STRING)
return (int)strlen(tmp_def->string);
else {
print_error(ERROR_NUM, "Definition \"%s\" is not a string definition. .length returns 0 for that...\n", l);
return 0;
}
}
length = (int)strlen(l);
if (l[0] == '"' && l[length-1] == '"')
length -= 2;
return length;
}
static struct stack_item_priority_item s_stack_item_priority_items[] = {
{ SI_OP_LOGICAL_OR, 10 },
{ SI_OP_LOGICAL_AND, 20 },
{ SI_OP_OR, 30 },
{ SI_OP_XOR, 40 },
{ SI_OP_AND, 50 },
{ SI_OP_COMPARE_EQ, 60 },
{ SI_OP_COMPARE_NEQ, 60 },
{ SI_OP_COMPARE_LT, 70 },
{ SI_OP_COMPARE_GT, 70 },
{ SI_OP_COMPARE_LTE, 70 },
{ SI_OP_COMPARE_GTE, 70 },
{ SI_OP_SHIFT_LEFT, 80 },
{ SI_OP_SHIFT_RIGHT, 80 },
{ SI_OP_ADD, 90 },
{ SI_OP_SUB, 90 },
{ SI_OP_NEGATE, 100 },
{ SI_OP_MULTIPLY, 100 },
{ SI_OP_DIVIDE, 100 },
{ SI_OP_MODULO, 100 },
{ SI_OP_POWER, 100 },
{ SI_OP_LOW_BYTE, 110 },
{ SI_OP_HIGH_BYTE, 110 },
{ SI_OP_LOW_WORD, 110 },
{ SI_OP_HIGH_WORD, 110 },
{ SI_OP_BANK, 110 },
{ SI_OP_BANK_BYTE, 110 },
{ SI_OP_ROUND, 110 },
{ SI_OP_CEIL, 110 },
{ SI_OP_FLOOR, 110 },
{ SI_OP_MIN, 110 },
{ SI_OP_MAX, 110 },
{ SI_OP_SQRT, 110 },
{ SI_OP_ABS, 110 },
{ SI_OP_COS, 110 },
{ SI_OP_SIN, 110 },
{ SI_OP_TAN, 110 },
{ SI_OP_COSH, 110 },
{ SI_OP_SINH, 110 },
{ SI_OP_TANH, 110 },
{ SI_OP_ACOS, 110 },
{ SI_OP_ASIN, 110 },
{ SI_OP_ATAN, 110 },
{ SI_OP_ATAN2, 110 },
{ SI_OP_LOG, 110 },
{ SI_OP_LOG10, 110 },
{ SI_OP_POW, 110 },
{ SI_OP_CLAMP, 110 },
{ SI_OP_SIGN, 110 },
{ SI_OP_NOT, 120 },
{ 999, 999 }
};
static int _get_op_priority(int op) {
int i = 0;
while (s_stack_item_priority_items[i].op < 999) {
if (s_stack_item_priority_items[i].op == op)
return s_stack_item_priority_items[i].priority;
i++;
}
print_text(NO, "_get_op_priority(): No priority for OP %d! Please submit a bug report\n", op);
return 0;
}
static void _return_converted_stack_items(void) {
struct stack_item *b, *s;
int i;
for (i = 0; i < s_converted_stack_items_count; i++) {
b = &s_converted_stack_items_backups[i];
s = s_converted_stack_items[i];
/*
print_text(NO, "RETURN %s: %d %f\n", s->string, s->type, s->value);
*/
s->type = b->type;
s->sign = b->sign;
s->can_calculate_deltas = b->can_calculate_deltas;
s->has_been_replaced = b->has_been_replaced;
s->is_in_postfix = b->is_in_postfix;
s->value = b->value;
strcpy(s->string, b->string);
/*
print_text(NO, " NOW %s: %d %f\n", s->string, s->type, s->value);
*/
}
s_converted_stack_items_count = 0;
}
static int _remember_converted_stack_item(struct stack_item *s) {
struct stack_item *b;
if (g_can_remember_converted_stack_items == NO)
return SUCCEEDED;
if (s_converted_stack_items_count >= 64) {
print_text(NO, "_REMEMBER_CONVERTED_STACK_ITEM: Out of space! Please submit a bug report!\n");
return FAILED;
}
if (s_converted_stack_items_backups == NULL) {
s_converted_stack_items_backups = calloc(sizeof(struct stack_item) * 64, 1);
if (s_converted_stack_items_backups == NULL) {
print_error(ERROR_ERR, "Out of memory error while allocating s_converted_stack_items_backups.\n");
return FAILED;
}
}
/*
print_text(NO, "REMEMBER %s: %d %f\n", s->string, s->type, s->value);
*/
b = &s_converted_stack_items_backups[s_converted_stack_items_count];
s_converted_stack_items[s_converted_stack_items_count++] = s;
b->type = s->type;
b->sign = s->sign;
b->can_calculate_deltas = s->can_calculate_deltas;
b->has_been_replaced = s->has_been_replaced;
b->is_in_postfix = s->is_in_postfix;
b->value = s->value;
strcpy(b->string, s->string);
return SUCCEEDED;
}
static int _parse_function_asc(char *in, int *result, int *parsed_chars) {
int res, old_expect = g_expect_calculations, source_index_original = g_source_index, source_index_backup;
/* NOTE! we assume that 'in' is actually '&g_buffer[xyz]', so
let's update g_source_index for input_number() */
g_source_index = (int)(in - g_buffer);
source_index_backup = g_source_index;
g_expect_calculations = NO;
res = input_number();
g_expect_calculations = old_expect;
if (res != SUCCEEDED || g_parsed_int < 0 || g_parsed_int > 255) {
print_error(ERROR_NUM, "asc() requires an immediate value between 0 and 255.\n");
return FAILED;
}
if (g_buffer[g_source_index] != ')') {
print_error(ERROR_NUM, "Malformed \"asc(?)\" detected!\n");
return FAILED;
}
/* skip ')' */
g_source_index++;
/* count the parsed chars */
*parsed_chars = (int)(g_source_index - source_index_backup);
/* return g_source_index */
g_source_index = source_index_original;
if (g_asciitable_defined == 0) {
print_error(ERROR_WRN, "No .ASCIITABLE defined. Using the default n->n -mapping.\n");
*result = g_parsed_int;
}
else
*result = (int)g_asciitable[g_parsed_int];
return SUCCEEDED;
}
static int _parse_function_random(char *in, int *result, int *parsed_chars) {
int res, old_expect = g_expect_calculations, source_index_original = g_source_index, source_index_backup;
int min, max;
/* NOTE! we assume that 'in' is actually '&g_buffer[xyz]', so
let's update g_source_index for input_number() */
g_source_index = (int)(in - g_buffer);
source_index_backup = g_source_index;
g_expect_calculations = YES;
res = input_number();
if (res != SUCCEEDED) {
print_error(ERROR_NUM, "random() requires an immediate value for min.\n");
return FAILED;
}
min = g_parsed_int;
g_expect_calculations = YES;
res = input_number();
g_expect_calculations = old_expect;
if (res != SUCCEEDED) {
print_error(ERROR_NUM, "random() requires an immediate value for max.\n");
return FAILED;
}
max = g_parsed_int;
if (min >= max) {
print_error(ERROR_DIR, "random() needs that min < max.\n");
return FAILED;
}
if (g_buffer[g_source_index] != ')') {
print_error(ERROR_NUM, "Malformed \"random(?,?)\" detected!\n");
return FAILED;
}
/* skip ')' */
g_source_index++;
/* count the parsed chars */
*parsed_chars = (int)(g_source_index - source_index_backup);
/* return g_source_index */
g_source_index = source_index_original;
/* output the random number */
*result = (genrand_int32() % (max-min+1)) + min;
return SUCCEEDED;
}
static int _parse_function_defined(char *in, int *result, int *parsed_chars) {
int res, old_expect = g_expect_calculations, source_index_original = g_source_index, source_index_backup;
struct definition *d;
/* NOTE! we assume that 'in' is actually '&g_buffer[xyz]', so
let's update g_source_index for input_number() */
g_source_index = (int)(in - g_buffer);
source_index_backup = g_source_index;
g_expect_calculations = NO;
res = get_next_plain_string();
g_expect_calculations = old_expect;
if (res != SUCCEEDED) {
print_error(ERROR_NUM, "defined() requires a definition name string.\n");
return FAILED;
}
if (g_buffer[g_source_index] != ')') {
print_error(ERROR_NUM, "Malformed \"defined(?)\" detected!\n");
return FAILED;
}
/* skip ')' */
g_source_index++;
/* count the parsed chars */
*parsed_chars = (int)(g_source_index - source_index_backup);
/* return g_source_index */
g_source_index = source_index_original;
/* try to find the definition */
hashmap_get(g_defines_map, g_label, (void*)&d);
if (d != NULL)
*result = 1;
else
*result = 0;
return SUCCEEDED;
}
static int _parse_function_exists(char *in, int *result, int *parsed_chars) {
int res, old_expect = g_expect_calculations, source_index_original = g_source_index, source_index_backup;
FILE *f;
/* NOTE! we assume that 'in' is actually '&g_buffer[xyz]', so
let's update g_source_index for input_number() */
g_source_index = (int)(in - g_buffer);
source_index_backup = g_source_index;
g_expect_calculations = NO;
res = input_number();
g_expect_calculations = old_expect;
if (res != INPUT_NUMBER_ADDRESS_LABEL && res != INPUT_NUMBER_STRING) {
print_error(ERROR_NUM, "exists() requires a file name string.\n");
return FAILED;
}
if (g_buffer[g_source_index] != ')') {
print_error(ERROR_NUM, "Malformed \"exists(?)\" detected!\n");
return FAILED;
}
/* skip ')' */
g_source_index++;
/* count the parsed chars */
*parsed_chars = (int)(g_source_index - source_index_backup);
/* return g_source_index */
g_source_index = source_index_original;
f = fopen(g_label, "rb");
if (f == NULL)
*result = 0;
else {
*result = 1;
fclose(f);
}
return SUCCEEDED;
}
static int _parse_function_math1(char *in, int *type, double *value, char *string, int *parsed_chars, char *name) {
int res, source_index_original = g_source_index, source_index_backup, input_float_mode = g_input_float_mode;
/* NOTE! we assume that 'in' is actually '&g_buffer[xyz]', so
let's update g_source_index for input_number() */
g_source_index = (int)(in - g_buffer);
source_index_backup = g_source_index;
if (g_buffer[g_source_index] == ')') {
print_error(ERROR_STC, "%s is missing argument 1!\n", name);
return FAILED;
}
g_input_float_mode = ON;
res = input_number();
while (res == INPUT_NUMBER_EOL) {
next_line();
res = input_number();
}
g_input_float_mode = input_float_mode;
*type = res;
if (res == SUCCEEDED)
*value = g_parsed_int;
else if (res == INPUT_NUMBER_FLOAT)
*value = g_parsed_double;
else if (res == INPUT_NUMBER_ADDRESS_LABEL)
strcpy(string, g_label);
else if (res == INPUT_NUMBER_STACK)
*value = g_latest_stack;
else if (res == FAILED)
return FAILED;
else {
print_error(ERROR_STC, "Unhandled result type %d of argument 1 in %s!\n", res, name);
return FAILED;
}
if (g_buffer[g_source_index] != ')') {
print_error(ERROR_NUM, "Malformed \"%s\" detected!\n", name);
return FAILED;
}
/* skip ')' */
g_source_index++;
/* count the parsed chars */
*parsed_chars = (int)(g_source_index - source_index_backup);
/* return g_source_index */
g_source_index = source_index_original;
return SUCCEEDED;
}
static int _parse_function_math2(char *in, int *type_a, int *type_b, double *value_a, double *value_b, char *string_a, char *string_b, int *parsed_chars, char *name) {
int res, source_index_original = g_source_index, source_index_backup, input_float_mode = g_input_float_mode;
/* NOTE! we assume that 'in' is actually '&g_buffer[xyz]', so
let's update g_source_index for input_number() */
g_source_index = (int)(in - g_buffer);
source_index_backup = g_source_index;
/* a */
g_input_float_mode = ON;
res = input_number();
while (res == INPUT_NUMBER_EOL) {
next_line();
res = input_number();
}
*type_a = res;
if (res == SUCCEEDED)
*value_a = g_parsed_int;
else if (res == INPUT_NUMBER_FLOAT)
*value_a = g_parsed_double;
else if (res == INPUT_NUMBER_ADDRESS_LABEL)
strcpy(string_a, g_label);
else if (res == INPUT_NUMBER_STACK)
*value_a = g_latest_stack;
else if (res == FAILED)
return FAILED;
else {
print_error(ERROR_STC, "Unhandled result type %d of argument 1 in %s!\n", res, name);
return FAILED;
}
if (g_buffer[g_source_index] == ')') {
print_error(ERROR_STC, "%s is missing argument 2!\n", name);
return FAILED;
}
/* b */
res = input_number();
while (res == INPUT_NUMBER_EOL) {
next_line();
res = input_number();
}
g_input_float_mode = input_float_mode;
*type_b = res;
if (res == SUCCEEDED)
*value_b = g_parsed_int;
else if (res == INPUT_NUMBER_FLOAT)
*value_b = g_parsed_double;
else if (res == INPUT_NUMBER_ADDRESS_LABEL)
strcpy(string_b, g_label);
else if (res == INPUT_NUMBER_STACK)
*value_b = g_latest_stack;
else if (res == FAILED)
return FAILED;
else {
print_error(ERROR_STC, "Unhandled result type %d of argument 2 in %s!\n", res, name);
return FAILED;
}
if (g_buffer[g_source_index] != ')') {
print_error(ERROR_NUM, "Malformed \"%s\" detected!\n", name);
return FAILED;
}
/* skip ')' */
g_source_index++;
/* count the parsed chars */