-
Notifications
You must be signed in to change notification settings - Fork 98
/
parse.c
2624 lines (2154 loc) · 73.7 KB
/
parse.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 <time.h>
#include "defines.h"
#include "parse.h"
#include "phase_1.h"
#include "stack.h"
#include "include.h"
#include "printf.h"
#include "mersenne.h"
int g_input_number_error_msg = YES, g_ss, g_string_size, g_input_float_mode = OFF, g_parse_floats = YES;
int g_expect_calculations = YES, g_input_allow_leading_hashtag = NO, g_input_has_leading_hashtag = NO, g_input_parse_special_chars = YES;
int g_input_allow_leading_ampersand = NO, g_plus_and_minus_ends_label = NO, g_get_next_token_use_substitution = YES;
int g_newline_beginning = ON, g_parsed_double_decimal_numbers = 0, g_operand_hint, g_operand_hint_type;
int g_input_number_turn_values_into_strings = NO, g_force_add_namespace = NO, g_force_ignore_namespace = NO;
char g_label[MAX_NAME_LENGTH + 1];
char g_expanded_macro_string[MAX_NAME_LENGTH + 1];
double g_parsed_double;
#if defined(SPC700)
int g_input_number_expects_dot = NO;
#endif
extern int g_source_index, g_source_file_size, g_parsed_int, g_macro_active, g_sizeof_g_tmp;
extern char *g_buffer, *g_tmp, g_current_directive[MAX_NAME_LENGTH + 1], *g_label_stack[256];
extern unsigned char g_asciitable[256];
extern struct active_file_info *g_active_file_info_first, *g_active_file_info_last, *g_active_file_info_tmp;
extern struct map_t *g_defines_map;
extern struct macro_runtime *g_macro_stack, *g_macro_runtime_current;
extern struct function *g_functions_first;
extern struct map_t *g_namespace_map;
extern struct namespace *g_namespaces_first;
extern int g_latest_stack, g_asciitable_defined, g_global_label_hint, g_parsing_function_body, g_resolve_stack_calculations;
extern int g_is_file_isolated_counter, g_can_remember_converted_stack_items;
int parse_string_length(char *end);
PROFILE_GLOBALS_EXTERN();
int is_string_ending_with(char *s, char *e) {
int length_s, length_e, k;
if (s == NULL || e == NULL)
return -1;
length_s = (int)strlen(s);
length_e = (int)strlen(e);
if (length_e > length_s)
return -1;
for (k = 0; k < length_e; k++) {
if (s[length_s - length_e + k] != e[k])
return -1;
}
return 1;
}
int strcaselesscmpn(char *s1, char *s2, int length) {
int n = 0;
if (s1 == NULL || s2 == NULL)
return 0;
while (*s1 != 0 && *s2 != 0) {
if (toupper((int)*s1) != toupper((int)*s2))
return 1;
s1++;
s2++;
n++;
if (n >= length)
return 0;
}
if (*s2 != 0)
return 1;
if (*s1 != 0)
return -1;
return 0;
}
int compare_next_token(char *token) {
int ii, t, d, k, length;
char e;
length = (int)strlen(token);
/* skip white space */
ii = g_source_index;
for (e = g_buffer[ii]; e == ' ' || e == ',' || e == '\\' || e == 0x0A; e = g_buffer[++ii]) {
if (e == '\\' && g_buffer[ii + 1] != 0x0A)
break;
}
/* HACK: if token == '=', then we'll take this route. in general compare_next_token() should be
called with an alphabetical string... */
if (length == 1 && token[0] == '=') {
if (e == '=')
return SUCCEEDED;
}
/* MACRO mode? */
if (g_macro_active != 0 && e == '\\') {
if (g_buffer[ii + 1] == '@') {
char tmp_buffer[64];
snprintf(tmp_buffer, sizeof(tmp_buffer), "%d", g_macro_runtime_current->macro->calls - 1);
e = tmp_buffer[0];
for (t = 0; t < length && e != 0; ) {
if (toupper((int)token[t]) != toupper((int)e))
return FAILED;
t++;
e = tmp_buffer[t];
}
}
else if (g_buffer[ii + 1] == '?') {
/* TODO: do we need to implement this? */
return FAILED;
}
else {
for (d = 0, k = 0; k < 16; k++) {
e = g_buffer[++ii];
if (e >= '0' && e <= '9')
d = (d * 10) + e - '0';
else
break;
}
if (d > g_macro_runtime_current->supplied_arguments) {
if (g_input_number_error_msg == YES) {
print_error(ERROR_NONE, "COMPARE_NEXT_SYMBOL: Macro \"%s\" wasn't called with enough arguments.\n", g_macro_runtime_current->macro->name);
}
return FAILED;
}
ii = g_macro_runtime_current->argument_data[d - 1]->start;
e = g_buffer[ii];
for (t = 0; t < length && e != ' ' && e != ',' && e != 0x0A; ) {
if (toupper((int)token[t]) != toupper((int)e))
return FAILED;
t++;
e = g_buffer[++ii];
}
}
}
/* not in MACRO mode */
else {
for (t = 0; t < length && e != ' ' && e != ',' && e != 0x0A; ) {
if (toupper((int)token[t]) != toupper((int)e))
return FAILED;
t++;
e = g_buffer[++ii];
}
/* token has been terminated, but the other string has not? */
if (t == length && e != ' ' && e != ',' && e != 0x0A)
return FAILED;
}
if (t == length)
return SUCCEEDED;
return FAILED;
}
int should_we_add_namespace(void) {
if (g_force_ignore_namespace == YES)
return NO;
if (g_is_file_isolated_counter > 0 || g_force_add_namespace == YES)
return YES;
if (g_macro_active != 0) {
struct macro_runtime *mrt = &g_macro_stack[g_macro_active - 1];
if (mrt->macro->namespace[0] != 0)
return YES;
}
return NO;
}
static char *s_arg_type_labels[] = {
"ARG_IMMEDIATE",
"ARG_NUMBER",
"ARG_LABEL",
"ARG_STRING",
"ARG_PENDING_CALCULATION",
NULL
};
int is_macro_arg_type_label(char *label) {
int i = 0;
/* quick exit */
if (label[0] != 'A')
return NO;
while (s_arg_type_labels[i] != NULL) {
if (strcaselesscmp(s_arg_type_labels[i], label) == 0)
return YES;
i++;
}
return NO;
}
static int _add_namespace_to_a_label(char *output, int sizeof_output, char *input, int is_sizeof) {
int length = (int)strlen(input);
if (is_sizeof == YES)
length += 8;
if (length >= sizeof_output - 1) {
print_error(ERROR_NUM, "The label with the namespace is too long (max %d characters allowed). Please adjust MAX_NAME_LENGTH in shared.h and recompile WLA.\n", MAX_NAME_LENGTH);
return FAILED;
}
if (is_sizeof == YES)
snprintf(output, sizeof_output, "_sizeof_%s", input);
else
strcpy(output, input);
return SUCCEEDED;
}
int add_namespace_to_a_label(char *label, int sizeof_label, int add_outside_macros) {
char namespace_tmp[MAX_NAME_LENGTH + 1], label_tmp[MAX_NAME_LENGTH + 1];
struct definition *tmp_def;
int i, is_sizeof = NO;
if (g_force_ignore_namespace == YES)
return SUCCEEDED;
strcpy(label_tmp, label);
/* don't add namespace to some specific labels */
if (strcaselesscmp(label, "_out") == 0)
return SUCCEEDED;
if (label[0] == '\\' || label[0] == '@' || label[0] == '-' || label[0] == '+')
return SUCCEEDED;
if (label[0] == '_') {
if (strncmp(label, "_sizeof_", 8) == 0) {
is_sizeof = YES;
strcpy(label_tmp, &label[8]);
}
else
return SUCCEEDED;
}
if (g_macro_active != 0) {
if (is_macro_arg_type_label(label) == YES)
return SUCCEEDED;
}
/* does the label already contain a namespace? */
i = 0;
while (1) {
namespace_tmp[i] = label_tmp[i];
if (label_tmp[i] == '.') {
if (strcaselesscmp(&label_tmp[i], ".length") != 0) {
/* we have a dot in the name -> check if we know the namespace */
struct namespace *namespace = g_namespaces_first;
namespace_tmp[i] = 0;
while (namespace != NULL) {
if (strcmp(namespace_tmp, namespace->name) == 0) {
/* we know the namespace -> don't add it! */
return SUCCEEDED;
}
namespace = namespace->next;
}
/* it is not probably a namespace so proceed adding the namespace to the label */
break;
}
}
if (label_tmp[i] == 0)
break;
i++;
}
namespace_tmp[0] = 0;
/* label reference inside a namespaced .MACRO? */
if (g_macro_active != 0) {
struct macro_runtime *mrt = &g_macro_stack[g_macro_active - 1];
if (mrt->macro->namespace[0] != 0) {
/* yes! add the namespace! */
if (strlen(mrt->macro->namespace) + strlen(label_tmp) >= MAX_NAME_LENGTH) {
print_error(ERROR_NUM, "The label with the namespace is too long (max %d characters allowed). Please adjust MAX_NAME_LENGTH in shared.h and recompile WLA.\n", MAX_NAME_LENGTH);
return FAILED;
}
snprintf(namespace_tmp, sizeof(namespace_tmp), "%s.%s", mrt->macro->namespace, label_tmp);
}
}
else if (add_outside_macros == YES) {
strcpy(namespace_tmp, label_tmp);
add_namespace_to_string(namespace_tmp, sizeof(namespace_tmp), "add_namespace_to_a_label()");
}
if (namespace_tmp[0] == 0)
return SUCCEEDED;
if (g_force_add_namespace == YES)
return _add_namespace_to_a_label(label, sizeof_label, namespace_tmp, is_sizeof);
/* do we find the label with the namespace in defines? */
if (is_sizeof == YES) {
snprintf(label_tmp, sizeof(label_tmp), "_sizeof_%s", namespace_tmp);
hashmap_get(g_defines_map, label_tmp, (void*)&tmp_def);
}
else
hashmap_get(g_defines_map, namespace_tmp, (void*)&tmp_def);
if (tmp_def != NULL) {
/* yes! */
return _add_namespace_to_a_label(label, sizeof_label, namespace_tmp, is_sizeof);
}
/* do we find the label without the namespace in defines? */
hashmap_get(g_defines_map, label, (void*)&tmp_def);
if (tmp_def != NULL) {
/* yes! */
return SUCCEEDED;
}
return _add_namespace_to_a_label(label, sizeof_label, namespace_tmp, is_sizeof);
}
int input_next_string(void) {
int k, curly_braces = 0;
char e;
/* skip white space */
for (e = g_buffer[g_source_index++]; e == ' ' || e == ','; e = g_buffer[g_source_index++])
;
if (e == 0x0A)
return INPUT_NUMBER_EOL;
/* we assume it is a label */
g_label[0] = e;
for (k = 1; k < MAX_NAME_LENGTH; k++) {
e = g_buffer[g_source_index++];
if (e == '{')
curly_braces++;
else if (e == '}')
curly_braces++;
else if (e == 0x0A || e == ',') {
g_source_index--;
break;
}
else if (e == '(' || e == ')') {
g_source_index--;
break;
}
else if (curly_braces <= 0 && e == ' ')
break;
g_label[k] = e;
}
if (k == MAX_NAME_LENGTH) {
if (g_input_number_error_msg == YES) {
print_error(ERROR_NUM, "The string is too long (max %d characters allowed). Please adjust MAX_NAME_LENGTH in shared.h and recompile WLA.\n", MAX_NAME_LENGTH);
}
return FAILED;
}
g_label[k] = 0;
/* expand e.g., \1 and \@ */
if (g_macro_active != 0) {
if (expand_macro_arguments(g_label, NULL, NULL) == FAILED)
return FAILED;
}
if (expand_variables_inside_string(g_label, sizeof(g_label), &k) == FAILED)
return FAILED;
if (should_we_add_namespace() == YES) {
if (add_namespace_to_a_label(g_label, sizeof(g_label), YES) == FAILED)
return FAILED;
}
return SUCCEEDED;
}
static int _input_number_return_definition(struct definition *def) {
if (def->type == DEFINITION_TYPE_VALUE) {
g_parsed_int = (int)def->value;
g_parsed_double = def->value;
if (g_operand_hint == HINT_NONE) {
if (g_parsed_int > 0xFFFFFF)
g_operand_hint = HINT_32BIT;
else if (g_parsed_int > 0xFFFF && g_parsed_int <= 0xFFFFFF)
g_operand_hint = HINT_24BIT;
else if (g_parsed_int > 0xFF)
g_operand_hint = HINT_16BIT;
else
g_operand_hint = HINT_8BIT;
g_operand_hint_type = HINT_TYPE_DEDUCED;
#if defined(MC6809)
/* 5-bit values need this */
if (g_parsed_int >= -16 && g_parsed_int <= 15) {
g_operand_hint = HINT_NONE;
g_operand_hint_type = HINT_TYPE_NONE;
}
#endif
}
if (g_input_float_mode == ON && g_parsed_double - (int)g_parsed_double != 0.0)
return INPUT_NUMBER_FLOAT;
return SUCCEEDED;
}
else if (def->type == DEFINITION_TYPE_STACK) {
/* wrap the referenced, existing stack calculation inside a new stack calculation as stack
calculation contains a write. the 2nd, 3rd etc. reference don't do anything by themselves.
but wrapping creates a new stack calculation that also makes a write */
stack_create_stack_stack((int)def->value);
return INPUT_NUMBER_STACK;
}
else if (def->type == DEFINITION_TYPE_ADDRESS_LABEL) {
if (g_label[0] == ':') {
/* we need to keep the ':' prefix */
if (strlen(def->string) >= MAX_NAME_LENGTH-1) {
if (g_input_number_error_msg == YES) {
print_error(ERROR_NUM, "The label is too long (max %d characters allowed). Please adjust MAX_NAME_LENGTH in shared.h and recompile WLA.\n", MAX_NAME_LENGTH);
}
return FAILED;
}
snprintf(g_label, sizeof(g_label), ":%.254s", def->string);
g_string_size = def->size + 1;
}
else {
g_string_size = def->size;
memcpy(g_label, def->string, g_string_size);
g_label[g_string_size] = 0;
}
}
else {
g_string_size = def->size;
memcpy(g_label, def->string, g_string_size);
g_label[g_string_size] = 0;
return INPUT_NUMBER_STRING;
}
/* are labels 16-bit by default? */
if (g_operand_hint_type != HINT_TYPE_GIVEN && g_global_label_hint == HINT_16BIT) {
g_operand_hint = HINT_16BIT;
g_operand_hint_type = HINT_TYPE_GIVEN;
}
process_special_labels(g_label);
return INPUT_NUMBER_ADDRESS_LABEL;
}
int expand_variables_inside_string(char *label, int max_size, int *length) {
char tmp[MAX_NAME_LENGTH + 1], formatting[8], substitution[MAX_NAME_LENGTH + 1], local[MAX_NAME_LENGTH + 1];
int size, i, k, substitutions = 0, max_size_tmp, p, parsed_int, input_size = 0;
int q, size_substitution;
/* quick exit if no curly braces are found */
i = 0;
k = NO;
while (label[i] != 0) {
if (label[i] == '{')
k = YES;
i++;
input_size++;
}
if (k == NO)
return SUCCEEDED;
input_size++;
/* copy label -> local as label might be g_label that stack calculator uses */
if (input_size > (int)sizeof(local)) {
print_error(ERROR_NUM, "Buffer for substitution is too small! Please submit a bug report!\n");
return FAILED;
}
strcpy(local, label);
max_size_tmp = sizeof(tmp);
if (length == NULL)
size = input_size;
else
size = *length;
for (i = 0, k = 0; i < size && k < max_size_tmp; i++) {
if (local[i] == '{') {
int use_formatting = NO;
i++;
/* skip whitespace */
while (i < size && local[i] == ' ')
i++;
/* do we have formatting? */
if (i+5 < size && local[i] == '%' && local[i+1] == '.') {
/* yes! */
int f = 0, n;
char c;
use_formatting = YES;
formatting[f++] = '%';
i++;
if (local[i] != '.') {
print_error(ERROR_NUM, "The formatting string must begin with \"%%.\".\n");
return FAILED;
}
formatting[f++] = '.';
i++;
for (n = 0; n < 3; n++, i++) {
c = local[i];
if (c == '{')
break;
if (!((c >= '0' && c <= '9') || c == 'x' || c == 'X' || c == 'd' || c == 'i')) {
print_error(ERROR_NUM, "Unsupported formatting symbol '%c'.\n", c);
return FAILED;
}
formatting[f++] = c;
}
if (local[i] != '{') {
print_error(ERROR_NUM, "Error in formatting. Formatting string is too long?\n");
return FAILED;
}
formatting[f] = 0;
i++;
}
/* launch stack calculator */
p = stack_calculate(&local[i], &parsed_int, &i, YES);
i++;
if (p == STACK_CALCULATE_DELAY || p == INPUT_NUMBER_STACK) {
print_error(ERROR_NUM, "Postponed calculation is not suitable for substitution as we need immediate results.\n");
return FAILED;
}
if (p == SUCCEEDED || p == INPUT_NUMBER_FLOAT) {
if (p == INPUT_NUMBER_FLOAT)
parsed_int = (int)g_parsed_double;
if (use_formatting == YES)
snprintf(substitution, sizeof(substitution), formatting, parsed_int);
else
snprintf(substitution, sizeof(substitution), "%d", parsed_int);
}
else if (p == STACK_RETURN_STRING) {
if (use_formatting == YES) {
print_error(ERROR_NUM, "Cannot use formatting with strings.\n");
return FAILED;
}
strcpy(substitution, g_label);
}
else if (p == STACK_RETURN_LABEL) {
print_error(ERROR_NUM, "Label \"%s\" cannot be used in a substitution here as its value is unknown.\n", g_label);
return FAILED;
}
else if (p == FAILED)
return FAILED;
else {
print_error(ERROR_NUM, "Unhandled return type %d from stack_calculate()! Please submit a bug report!\n", p);
return FAILED;
}
/* perform substitution */
size_substitution = (int)strlen(substitution);
for (q = 0; k < max_size_tmp && q < size_substitution; q++)
tmp[k++] = substitution[q];
substitutions++;
if (use_formatting == YES) {
/* skip whitespace */
while (i+1 < size && local[i+1] == ' ')
i++;
if (i+1 < size && local[i+1] == '}')
i++;
else {
print_error(ERROR_NUM, "The end of the substitution is missing a '}'. Got '%c' instead.\n", local[i+1]);
return FAILED;
}
}
continue;
}
tmp[k++] = local[i];
}
if (substitutions > 0) {
/* terminate the string with substitutions */
if (k < max_size_tmp)
tmp[k] = 0;
else {
print_error(ERROR_NUM, "Cannot perform substitutions for string \"%s\", buffer is too small.\n", local);
return FAILED;
}
/* copy back the string with substitutions */
if (k < max_size) {
strcpy(label, tmp);
if (length != NULL)
*length = k;
}
else {
print_error(ERROR_NUM, "Cannot perform substitutions for string \"%s\", buffer is too small.\n", local);
return FAILED;
}
}
return SUCCEEDED;
}
static int _parse_value_into_string(char e) {
int k;
/* override! */
g_label[0] = e;
for (k = 1; k < MAX_NAME_LENGTH; k++, g_source_index++) {
e = g_buffer[g_source_index];
g_label[k] = e;
if (e >= '0' && e <= '9')
continue;
else if (e >= 'a' && e <= 'z')
continue;
else if (e >= 'A' && e <= 'Z')
continue;
break;
}
g_label[k] = 0;
g_string_size = k;
return INPUT_NUMBER_STRING;
}
#if defined(PROFILE_FUNCTIONS)
int _input_number(void) {
#else
int input_number(void) {
#endif
char label_tmp[MAX_NAME_LENGTH + 1];
unsigned char e, ee, check_if_a_definition = YES, can_have_calculations = YES, use_substitution = NO;
int k, p, q, spaces = 0, curly_braces = 0;
double decimal_mul;
#if defined(SPC700)
int dot = 0;
#endif
g_operand_hint = HINT_NONE;
g_operand_hint_type = HINT_TYPE_NONE;
g_input_has_leading_hashtag = NO;
/* skip white space */
for (e = g_buffer[g_source_index++]; e == ' ' || e == ',' || e == '\\'; e = g_buffer[g_source_index++]) {
if (e == '\\' && g_buffer[g_source_index] == 0xA) {
g_source_index++;
next_line();
}
else if (e == '\\' && g_buffer[g_source_index] != 0xA)
break;
}
if (e == 0x0A)
return INPUT_NUMBER_EOL;
/* using substitution with quoted strings? */
if (e == '{') {
int old_position = g_source_index;
unsigned char old_e = e;
/* skip white space */
while (g_buffer[g_source_index] == ' ')
g_source_index++;
e = g_buffer[g_source_index++];
if (e == '"') {
/* later when we parse a quoted string we'll check that it's followed by a '}', and
we'll use substitution in that quoted string... */
use_substitution = YES;
}
else {
/* roll back */
e = old_e;
g_source_index = old_position;
}
}
/* are we parsing a macro argument, and could it begin with a '#' (ARG_IMMEDIATE)? */
if (g_input_allow_leading_hashtag == YES) {
if (e == '#') {
g_input_has_leading_hashtag = YES;
e = g_buffer[g_source_index++];
}
}
/* are we parsing a macro argument, and could it begin with a '&' (return
label as it is, don't check if it's a definition) */
if (g_input_allow_leading_ampersand == YES) {
if (e == '&') {
check_if_a_definition = NO;
can_have_calculations = NO;
e = g_buffer[g_source_index++];
}
}
if (g_expect_calculations == YES && can_have_calculations == YES) {
/* check the type of the expression */
p = g_source_index;
ee = e;
while (ee != 0x0A) {
if (ee == '{')
curly_braces++;
else if (ee == '}')
curly_braces--;
/* string / symbol -> no calculating */
else if (ee == ',')
break;
else if (ee == ' ')
spaces++;
else if (curly_braces <= 0 && (ee == '-' || ee == '+' || ee == '*' || ee == '/' || ee == '&' || ee == '|' || ee == '^' || ee == '(' ||
ee == '<' || ee == '>' || ee == '#' || ee == '~' || ee == ':' || ee == '!' || (ee == '=' && g_buffer[p] == '='))) {
if (ee == ':' && spaces > 0)
break;
/* launch stack calculator */
p = stack_calculate(&g_buffer[g_source_index - 1], &g_parsed_int, &g_source_index, NO);
if (p == STACK_CALCULATE_DELAY)
break;
else if (p == STACK_RETURN_LABEL)
return INPUT_NUMBER_ADDRESS_LABEL;
else if (p == STACK_RETURN_STRING)
return INPUT_NUMBER_STRING;
else
return p;
}
ee = g_buffer[p];
p++;
}
}
/* MACRO */
if (g_macro_active != 0 && e == '?' && g_buffer[g_source_index] >= '1' && g_buffer[g_source_index] <= '9') {
struct macro_argument *ma;
for (g_parsed_int = 0, k = 0; k < 4; k++) {
e = g_buffer[g_source_index++];
if (e >= '0' && e <= '9')
g_parsed_int = (g_parsed_int * 10) + (e - '0');
else {
g_source_index--;
break;
}
}
if (g_parsed_int > g_macro_runtime_current->supplied_arguments) {
print_error(ERROR_NUM, "Referencing argument number %d inside macro \"%s\". The macro has only %d arguments.\n", g_parsed_int, g_macro_runtime_current->macro->name, g_macro_runtime_current->supplied_arguments);
return FAILED;
}
if (g_parsed_int == 0) {
print_error(ERROR_NUM, "Referencing argument number %d inside macro \"%s\". Macro arguments are counted from 1.\n", g_parsed_int, g_macro_runtime_current->macro->name);
return FAILED;
}
/* use the macro argument to find its definition */
ma = g_macro_runtime_current->argument_data[g_parsed_int - 1];
k = ma->type;
if (k == INPUT_NUMBER_ADDRESS_LABEL) {
struct definition *tmp_def;
strcpy(g_label, ma->string);
hashmap_get(g_defines_map, g_label, (void*)&tmp_def);
if (tmp_def != NULL)
return _input_number_return_definition(tmp_def);
else {
print_error(ERROR_NUM, "Cannot find definition for \"%s\".\n", g_label);
return FAILED;
}
}
else {
print_error(ERROR_ERR, "? can be only used to evaluate definitions.\n");
return FAILED;
}
}
if (g_macro_active != 0 && e == '\\') {
struct macro_argument *ma;
int exit_here = YES;
int start_i = g_source_index;
unsigned char start_e = e;
if (g_buffer[g_source_index] == '@') {
g_source_index++;
g_parsed_int = g_macro_runtime_current->macro->calls - 1;
if (g_buffer[g_source_index] != ' ' && g_buffer[g_source_index] != 0xA && g_buffer[g_source_index] != ',')
exit_here = NO;
else
return SUCCEEDED;
}
else if (g_buffer[g_source_index] >= '0' && g_buffer[g_source_index] <= '9') {
for (g_parsed_int = 0, k = 0; k < 4; k++) {
e = g_buffer[g_source_index++];
if (e >= '0' && e <= '9')
g_parsed_int = (g_parsed_int * 10) + (e - '0');
else {
g_source_index--;
break;
}
}
if (g_buffer[g_source_index] != ' ' && g_buffer[g_source_index] != 0xA && g_buffer[g_source_index] != ',' && g_buffer[g_source_index] != '.')
exit_here = NO;
}
else
exit_here = NO;
if (exit_here == YES) {
if (g_parsed_int > g_macro_runtime_current->supplied_arguments) {
print_error(ERROR_NUM, "Referencing argument number %d inside macro \"%s\". The macro has only %d arguments.\n", g_parsed_int, g_macro_runtime_current->macro->name, g_macro_runtime_current->supplied_arguments);
return FAILED;
}
if (g_parsed_int == 0) {
print_error(ERROR_NUM, "Referencing argument number %d inside macro \"%s\". Macro arguments are counted from 1.\n", g_parsed_int, g_macro_runtime_current->macro->name);
return FAILED;
}
/* return the macro argument */
ma = g_macro_runtime_current->argument_data[g_parsed_int - 1];
k = ma->type;
g_input_has_leading_hashtag = ma->has_leading_hashtag;
if (k == INPUT_NUMBER_ADDRESS_LABEL) {
strcpy(g_label, ma->string);
process_special_labels(g_label);
}
else if (k == INPUT_NUMBER_STRING) {
strcpy(g_label, ma->string);
g_string_size = (int)strlen(ma->string);
}
else if (k == INPUT_NUMBER_STACK)
g_latest_stack = (int)ma->value;
else if (k == SUCCEEDED) {
g_parsed_int = (int)ma->value;
g_parsed_double = ma->value;
}
else if (k == INPUT_NUMBER_FLOAT) {
g_parsed_int = (int)ma->value;
g_parsed_double = ma->value;
}
else {
print_error(ERROR_ERR, "Macro argument list has been corrupted! Please send a bug report!\n");
return FAILED;
}
/* does the MACRO argument number end with a .b/.w/.l/.d? */
if (e == '.') {
e = g_buffer[g_source_index+1];
if (e == 'b' || e == 'B') {
g_operand_hint = HINT_8BIT;
g_operand_hint_type = HINT_TYPE_GIVEN;
g_source_index += 2;
}
else if (e == 'w' || e == 'W') {
g_operand_hint = HINT_16BIT;
g_operand_hint_type = HINT_TYPE_GIVEN;
g_source_index += 2;
}
else if (e == 'l' || e == 'L') {
g_operand_hint = HINT_24BIT;
g_operand_hint_type = HINT_TYPE_GIVEN;
g_source_index += 2;
}
else if (e == 'd' || e == 'D') {
g_operand_hint = HINT_32BIT;
g_operand_hint_type = HINT_TYPE_GIVEN;
g_source_index += 2;
}
}
if (k == INPUT_NUMBER_FLOAT) {
if (g_input_float_mode == ON)
return INPUT_NUMBER_FLOAT;
else
return SUCCEEDED;
}
else
return k;
}
else {
g_source_index = start_i;
e = start_e;
}
}
/* is it a hexadecimal value? */
g_parsed_int = 0;
if (e >= '0' && e <= '9') {
for (k = 0; 1; k++) {
if (g_buffer[g_source_index+k] >= '0' && g_buffer[g_source_index+k] <= '9')
continue;
if (g_buffer[g_source_index+k] >= 'a' && g_buffer[g_source_index+k] <= 'f')
continue;
if (g_buffer[g_source_index+k] >= 'A' && g_buffer[g_source_index+k] <= 'F')
continue;
if (g_buffer[g_source_index+k] == 'h' || g_buffer[g_source_index+k] == 'H') {
g_parsed_int = 1;
break;
}
break;
}
}
if (e == '$' || g_parsed_int == 1 || (e == '0' && (g_buffer[g_source_index] == 'x' || g_buffer[g_source_index] == 'X'))) {
if (g_parsed_int == 1)
g_source_index--;
else if (e == '0')
g_source_index++;
for (g_parsed_int = 0, k = 0; k < 8; k++, g_source_index++) {
e = g_buffer[g_source_index];
if (e >= '0' && e <= '9')
g_parsed_int = (g_parsed_int << 4) + e - '0';
else if (e >= 'A' && e <= 'F')
g_parsed_int = (g_parsed_int << 4) + e - 'A' + 10;
else if (e >= 'a' && e <= 'f')
g_parsed_int = (g_parsed_int << 4) + e - 'a' + 10;
else if (e == 'h' || e == 'H') {
g_source_index++;
break;
}
else
break;
}
e = g_buffer[g_source_index];