-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathline_parser.c
More file actions
1238 lines (1019 loc) · 37.1 KB
/
line_parser.c
File metadata and controls
1238 lines (1019 loc) · 37.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "data_structures.h"
#include "number_conversions.h"
#define MAX_DIGITS_SIZE 5
/* strcmp_lower -
receives: 2 strings for non-case-sensitive strcmp.
returns: strcmp result on tolower(word1), word2.
*/
int strcmp_lower(char *word1, char *word2)
{
int i = 0;
char word1_lower[MAX_STRING_SIZE];
while (word1[i] != '\0') {
word1_lower[i] = tolower(word1[i]);
i++;
}
word1_lower[i] = '\0';
return strcmp(word1_lower,word2);
}
/* is_current_word_empty -
receives: the current word obtained from list.
returns: 1 if the word is empty, 0 if not.
NOTE: The used of this function is in order to handle empty spaces or tabs between words in the line from the source file. */
int is_current_word_empty(char *word)
{
return strlen(word) == 0 ? TRUE : FALSE;
}
/* get_next_word -
receives: a pointer to the line to take the word from, a pointer to the current_word, and the position in the line where last time the function was called returned.
returns: the position where we stopped in the line.
NOTE: in case of ":" or "." - these will be returned as part of the word. The reason is that these characters will assist
with symbol type and commands recognition. They must be removed afterwards, in other function.
NOTE2: The parameter last_position should be -1 when a new line is parsed. */
int get_next_word(char *current_word, char *line, int last_position)
{
int position = last_position+1;
int i = 0;
if (line[position] == '\n' || line[position] == EOF || line[position] == '\0')
{
current_word[i] = '\0';
return position;
}
while(line[position] != ' ' && line[position] != ',' && line[position] != '[' && line[position] != ']' && line[position] != '\n' && line[position] != EOF && line[position] != '\0'
&& line[position] != '\t')
{
current_word[i] = line[position];
i++;
position++;
}
current_word[i] = '\0';
if(is_current_word_empty(current_word))
position = get_next_word(current_word, line, position);
return position;
}
/* is_saved_word -
receives: the current word to check if it is a saved word in the language or not.
returns 1 if the word is a saved word, 0 if not. */
int is_saved_word(char *current_word, int line_number, int *syntax_errors, int print_err)
{
int i;
for(i = 0; i < NUM_OF_SAVED_WORDS; i++)
{
if (strcmp_lower(current_word,saved_languages_words[i])==0) {
if (print_err) /*print error will also set syntax errors flag if a the symbol is forbidden to be a saved word*/
{
printf("Error in line %d - the word %s is a saved word in the language\n", line_number, current_word);
*syntax_errors = TRUE;
}
return TRUE;
}
}
return FALSE;
}
/* is alphanumeric
receivces: string
returns: TRUE if string consists only alphanumeric characters, else false*/
int is_alphanumeric(char *str) {
int i = 0;
while (str[i] != '\0') {
if (!isalnum(str[i]))
return FALSE;
i++;
}
return TRUE;
}
/* is_command -
*/
int is_command(char *curr_word)
{
if (strcmp_lower(curr_word, ".data") == 0 || strcmp_lower(curr_word, ".mat") == 0 || strcmp_lower(curr_word, ".string") == 0
|| strcmp_lower(curr_word, ".entry") == 0 || strcmp_lower(curr_word, ".extern") == 0)
return TRUE;
else
return FALSE;
}
/* is_symbol -
receives: the current word to parse,
returns: 1 if it's a symbol, 0 if not. */
int is_symbol(char *current_word, int line_number, int *syntax_errors, int begining_of_sentence, int print_err)
{
int length = strlen(current_word);
if(!isalpha(current_word[0]) && !is_command(current_word)) {
if (current_word[0] == '.')
{
fprintf(stderr, "Error in line %d - unknown command\n", line_number);
}
else
{
fprintf(stderr, "Error in line %d - symbol cannot start with a non alphabetic character\n", line_number);
}
*syntax_errors = TRUE;
return FALSE;
}
else if (begining_of_sentence) {
if (length > 30)
{
fprintf(stderr, "Error in line %d - The word is too long to be a symbol\n", line_number);
return FALSE;
}
if (current_word[length-1] == ':') {
current_word[length-1] = '\0';
if (is_saved_word(current_word, line_number, syntax_errors, print_err))
{
current_word[length - 1] = ':';
return FALSE;
}
else
return TRUE;
}
else
{
if (!is_saved_word(current_word, line_number, syntax_errors, 0)) {
fprintf(stderr, "Error in line %d - the symbol is missing ':'\n", line_number);
*syntax_errors = 1;
}
return FALSE;
}
}
else if (length <= 30 && is_alphanumeric(current_word))
return !is_saved_word(current_word, line_number, syntax_errors, print_err);
else
return FALSE;
}
/* is_symbol_operand - operand which is not #<NUM> must start with an alphabetic char (either symbol, or mat name or r<NUM>)
ONLY ERROR PRINT IS CHANGED IN THIS FUNC
receives: the current word to parse,
returns: 1 if it's a symbol, 0 if not. */
int is_symbol_operand(char *current_word, int line_number, int *syntax_errors, int begining_of_sentence, int print_err)
{
int length = strlen(current_word);
if (!isalpha(current_word[0]) && !is_command(current_word)) {
fprintf(stderr, "Error in line %d - operand cannot start with a non alphabetic character\n", line_number);
*syntax_errors = TRUE;
return FALSE;
}
else if (begining_of_sentence) {
if (current_word[length - 1] == ':' && length <= 30) {
current_word[length - 1] = '\0';
return !is_saved_word(current_word, line_number, syntax_errors, print_err);
}
else
{
if (!is_saved_word(current_word, line_number, syntax_errors, 0)) {
fprintf(stderr, "Error in line %d - the symbol is missing ':'\n", line_number);
*syntax_errors = 1;
}
return FALSE;
}
}
else if (length <= 30 && is_alphanumeric(current_word))
return !is_saved_word(current_word, line_number, syntax_errors, print_err);
else
return FALSE;
}
/* is_store_command -
receives: the current word to parse,
returns: 1 if the word is .data/.string/.mat, 0 if not. */
int is_store_command(char *current_word)
{
if (strcmp_lower(current_word, ".data") == 0 || strcmp_lower(current_word, ".mat") == 0 || strcmp_lower(current_word, ".string") == 0)
{
strncpy(current_word, current_word + 1, strlen(current_word) - 1); /* removes the "." from sentence. */
current_word[strlen(current_word) - 1] = '\0'; /* in order to strcmp to be accurate */
return TRUE;
}
return FALSE;
}
/* detect_store_command -
receives: the current sentence struct and the current word.
If the current word matches to one of the store commands known, it is stored in the guidance_command variable of the
sentence struct. */
void detect_store_command(sentence *parsed, char *current_word)
{
if (strcmp_lower(current_word, "data") == 0)
parsed->guidance_command = NUM;
else if (strcmp_lower(current_word, "mat") == 0)
parsed->guidance_command = MAT;
else if (strcmp_lower(current_word, "string") == 0)
parsed->guidance_command = STRING;
return;
}
/* is_extern_or_entry_command -
receives: the current word to parse,
returns: 1 if the word is .extern/.entry, 0 if not. */
int is_extern_or_entry_command(char *current_word)
{
if (strcmp_lower(current_word, ".extern") == 0 || strcmp_lower(current_word, ".entry") == 0)
{
strncpy(current_word, current_word + 1, strlen(current_word) - 1); /* removes '.' from the beginning */
current_word[strlen(current_word) - 1] = '\0'; /* in order to strcmp to be accurate */
return TRUE;
}
return FALSE;
}
/* is_extern -
receives: the current word after we detected it is "extern" or "entry",
returns: 1 if it's "extern", 0 if not.
*/
int is_extern(char *current_word)
{
return (strcmp_lower(current_word, "extern") == 0 ? TRUE : FALSE);
}
/* skip_spaces -
receives: line and last position.
returns the last postion where no empty spaces or \t are found. */
int skip_spaces(char *line, int last_position) {
while (line[last_position] == ' ' || line[last_position] == '\t')
last_position++;
return last_position;
}
/* get_next_member -
receives: a pointer to the temp member, the line to parse, line number, last position in line and if we are expecting to a comma 1, or 0 if we don't.
returns the new position and edits the temp_member array with the current number found.*/
int get_next_member(char *temp_member, char *line, int line_number, int last_position, int *syntax_errors, int *expecting_comma)
{
int i = 0;
int new_position = skip_spaces(line, last_position);
int number_ended = 0; /*indication that no more digits are expected (for 3,4 4,5 example)*/
while (line[new_position] != '\0' && line[new_position] != '\n' && line[new_position] != EOF && line[new_position] != '\t') {
/*handle:
2. add flag 'number ended' and prevent number after space
3. check if finished with comma in outer func*/
if (line[new_position] == '-' || line[new_position] == '+') {
if (i == 0) {
temp_member[i] = line[new_position];
i++;
}
else
{
fprintf(stderr, "Error in line %d - valid number expected\n", line_number);
*syntax_errors = 1;
}
}
else if (isdigit(line[new_position]) && number_ended == 0) {
*expecting_comma = 1; /* we can get a comma after getting at least 1 digit */
temp_member[i] = line[new_position];
i++;
}
else if (isdigit(line[new_position]) && number_ended == 1) {
fprintf(stderr, "Error in line %d - expecting comma\n", line_number);
*syntax_errors = 1;
return new_position;
}
else if (line[new_position] == ',') {
if (*expecting_comma == 1)
{
*expecting_comma = 0;
new_position++;
break;
}
else
/* in case the "," comes after expecting_number = 1 it will be an error. */
{
fprintf(stderr, "Error in line %d - redundant ','.\n", line_number);
*syntax_errors = 1;
}
}
else if (line[new_position] == ' ') {
number_ended = 1;
}
else
{
fprintf(stderr, "Error in line %d - only integers can come after .data / .mat store command\n", line_number);
*syntax_errors = 1;
}
new_position++;
}
temp_member[i] = '\0';
if (is_current_word_empty(temp_member) && line[new_position] != '\0' && line[new_position] != '\n' && line[new_position] != EOF)
new_position = get_next_member(temp_member, line, line_number, new_position + 1, syntax_errors, expecting_comma);
return new_position;
}
/* verify_and_save_numbers -
receives: the sentence struct were the parse is stored, the last position of the line, the line number and pointer to
syntax errors.
The function stores a number (positive or negative) into the data array of the sentence struct and the number of
numbers stored. */
void verify_and_save_numbers(sentence *parsed, char * line, int last_position, int line_number, int *syntax_errors)
{
char temp_member[MAX_DIGITS_SIZE]; /* The range of numbers to be coded is from -512 to 511. The most big array can
include "-512'\0'" and thus, we want to define MAX_DIGITS_SIZE to 5. */
int expecting_comma = 0;
int new_position = skip_spaces(line, last_position); /* initializes position */
int j = 0;
int number;
while (line[new_position] != '\0' && line[new_position] != '\n' && line[new_position] != EOF && j < MAX_DATA_ARR_SIZE && line[new_position] != '\t') {
new_position = get_next_member(temp_member, line, line_number, new_position, syntax_errors, &expecting_comma);
number = atoi(temp_member);
if (number > 511 || number < -512) {
fprintf(stderr, "Error in line %d - the range of numbers that can be translated with assembler that works with 10 bits is from -512 to 511. Number %d cannot be stored.\n", line_number, number);
*syntax_errors = 1;
}
else {
parsed->data_arr[j] = number;
}
j++;
}
/*if expecting_comma == 0 it means that the last member encountered was a comma, or when we don't have any numbers.
We reach to this "if" when the line ends and the data isn't completed */
if (expecting_comma == 0)
{
fprintf(stderr, "Error in line %d - missing data parameters\n", line_number);
*syntax_errors = 1;
}
parsed->data_arr_num_of_params = j;
return;
}
void mat_range_is_valid(int range, int max_possible, int line_number, int *syntax_errors) {
if (range < 0 || range > max_possible) {
fprintf(stderr, "Error in line %d - data can\'t end with a comma\n", line_number);
*syntax_errors = 1;
}
return;
}
/* verify_and_save_matrix -
receives: the sentence struct were the parse is stored, the last position of the line, the line number and pointer to
syntax errors.
The function stores the numbers of a matrix and it's size in parsed->mat_num_of_rows/cols and the numbers itself in an array:
parsed->mat. */
void verify_and_save_matrix(sentence *parsed, char * line, int last_position, int line_number, int *syntax_errors)
{
int expecting_open_bracket = 1;
int expecting_number = 0;
int expecting_comma = 0;
int num_of_brackets = 0;
char temp_member[MAX_DIGITS_SIZE];
int i = 0;
int num_of_numbers_to_expect_to;
int new_position;
int number;
int mat_arr_idx = 0;
/* new_position = get_next_word(parsed->symbol,line,last_position); */
new_position = skip_spaces(line, last_position);
while (num_of_brackets < 4) {
if (expecting_open_bracket) {
if (line[new_position] != '[') {
fprintf(stderr, "Error in line %d - missing '[' as expected.\n", line_number);
*syntax_errors = 1;
}
else {
expecting_open_bracket = 0;
expecting_number = 1;
num_of_brackets++;
}
}
else if (expecting_number) {
if (isdigit(line[new_position])) {
temp_member[i] = line[new_position];
i++;
}
else if (line[new_position] == ']') {
num_of_brackets++;
temp_member[i] = '\0';
if (num_of_brackets == 2) {
parsed->mat_num_of_rows = atoi(temp_member);
i = 0;
memset(temp_member, 0, sizeof(MAX_DIGITS_SIZE)); /* like &temp_arr[0] */
expecting_open_bracket = 1;
mat_range_is_valid(parsed->mat_num_of_rows,MAX_ROWS,line_number,syntax_errors);
}
if (num_of_brackets == 4) {
parsed->mat_num_of_cols = atoi(temp_member);
expecting_number = 0;
mat_range_is_valid(parsed->mat_num_of_cols, MAX_COLS, line_number, syntax_errors);
}
}
}
else
{
fprintf(stderr, "Error in line %d - matrix declaration not as expected\n", line_number);
*syntax_errors = 1;
}
new_position++;
new_position = skip_spaces(line, new_position);
}
/* end of veryifing matrix size declaration */
num_of_numbers_to_expect_to = parsed->mat_num_of_rows * parsed->mat_num_of_cols;
if (num_of_numbers_to_expect_to > MAX_ROWS * MAX_COLS) {
fprintf(stderr, "Error in line %d - the matrix exceeds the possible matrix size supported\n", line_number);
return;
}
new_position = skip_spaces(line, new_position);
if (line[new_position] == '\0' || line[new_position] == '\n' || line[new_position] == EOF)
return;
while (line[new_position] != '\0' && line[new_position] != '\n' && line[new_position] != '\t' && line[new_position] != EOF && mat_arr_idx < (MAX_ROWS * MAX_COLS)) {
new_position = get_next_member(temp_member, line, line_number, new_position, syntax_errors, &expecting_comma);
number = atoi(temp_member);
if (number > 511 || number < -512) {
fprintf(stderr, "Error in line %d - the range of numbers that can be translated with assembler that works with 10 bits is from -512 to 511. Number %d cannot be stored.\n", line_number, number);
*syntax_errors = 1;
}
else {
parsed->mat[mat_arr_idx] = number;
}
mat_arr_idx++;
}
/*if expecting_comma==0 it means that last time we saw a comma and line ended*/
if (expecting_comma == 0)
fprintf(stderr, "Error in line %d - data can\'t end with a comma\n", line_number);
/* end of storing matrix numbers into the matrix array */
if (num_of_numbers_to_expect_to < mat_arr_idx)
fprintf(stderr, "Error in line %d - too many numbers for the declared size of matrix\n", line_number);
/* if (num_of_numbers_to_expect_to != mat_arr_idx)
fprintf(stderr, "Error in line %d - wrong ammount of numbers for the declared matrix\n", line_number);
*/
return;
}
/* verify_and_save_string -
receives: the sentence struct, the line to parse, the last position in the line, the line number and a pointer to syntax errors.
The function parses a string and saves it to sentence->string */
void verify_and_save_string(sentence *parsed, char *line, int last_position, int line_number, int *syntax_errors)
{
char curr_char = line[last_position];
int str_idx = 0;
/*string should have the following format:
1. space
2. "
3. charachters
4. "
5. end of line
*/
/*1 - space*/
if (curr_char != ' ') {
fprintf(stderr, "Error in line %d - ' ' expected after '.string' cmd\n", line_number);
*syntax_errors = 1;
}
if (curr_char == '\0' && curr_char == EOF && curr_char == '\n')
return;
/*skip spaces*/
while (curr_char == ' ') {
last_position++;
curr_char = line[last_position];
}
/*2 - "*/
if (curr_char != '\"') {
fprintf(stderr, "Error in line %d - '\"' expected at the beginning of the string\n", line_number);
*syntax_errors = 1;
}
if (curr_char == '\0' && curr_char == EOF && curr_char == '\n')
return;
last_position++;
curr_char = line[last_position];
/*3 - charachters*/
while (curr_char != '\0' && curr_char != EOF && curr_char != '\n' && curr_char != '\"') {
parsed->string[str_idx] = curr_char;
str_idx++;
last_position++;
curr_char = line[last_position];
}
parsed->string[str_idx] = '\0';
/*4 - "*/
if (curr_char != '\"') {
fprintf(stderr, "Error in line %d - '\"' expected at the end of the string\n", line_number);
*syntax_errors = 1;
}
if (curr_char == '\0' && curr_char == EOF && curr_char == '\n')
return;
last_position++;
curr_char = line[last_position];
/*skip spaces*/
while (curr_char == ' ') {
last_position++;
curr_char = line[last_position];
}
/*5 - end of line/file*/
if (curr_char != '\0' && curr_char != EOF && curr_char != '\n') {
fprintf(stderr, "Error in line %d - unexpected characters after string ended\n", line_number);
*syntax_errors = 1;
}
return;
}
/* parse_data_by_its_type -
receives: the current sentence struct, the current word and the last position.
it routes to the relevant function for parsing according the guidance command detected and stored in the guidance command
variable in the sentence struct. */
void parse_data_by_its_type(sentence *parsed, char *line, int last_position, int line_number, int *syntax_errors)
{
if(parsed->guidance_command == NUM)
verify_and_save_numbers(parsed, line, last_position, line_number, syntax_errors);
else if(parsed->guidance_command == MAT)
verify_and_save_matrix(parsed, line, last_position, line_number, syntax_errors);
else if (parsed->guidance_command == STRING)
verify_and_save_string(parsed, line, last_position, line_number, syntax_errors);
return;
}
/* detect_opcode -
receives: a pointer to the current word.
returns: TRUE if the current word is an opcode, FALSE if it isn't. */
int detect_opcode(char *current_word) {
int opcode_idx = 0;
while (opcode_idx < NUM_OF_OPCODES) {
if (strcmp_lower(current_word, opcodes_table[opcode_idx].opcode) == 0)
return TRUE;
opcode_idx++;
}
return FALSE;
}
/* get_next_operand -
receives: a pointer to current word, the line being parsed and the last position in the line.
The function copies into current word the word that is supposed to be an operand. Verification for operand is doing in other functions.
returns: the position where the function ended to read.*/
int get_next_operand(char *current_word, char *line, int last_position)
{
int position = last_position;
int i = 0;
if (line[position] == ',')
{
current_word[0] = ',';
current_word[1] = '\0';
return position;
}
if (line[position] == '\n' || line[position] == EOF || line[position] == '\0')
{
current_word[i] = '\0';
return position;
}
while (line[position] != ',' && line[position] != '\n' && line[position] != ' ' && line[position] != EOF && line[position] != '\0' && line[position] != '\t')
{
current_word[i] = line[position];
i++;
position++;
}
current_word[i] = '\0';
/* if (is_current_word_empty(current_word) && line[position] != '\0' && line[position] != '\n' && line[position] != EOF)
position = get_next_operand(current_word, line, position); */
if (is_current_word_empty(current_word))
position = get_next_operand(current_word, line, position+1);
return position;
}
/* valid_reg_digit -
receives: a char.
returns: 1 if the char is a number between 0 to 7, 0 if not.
*/
int valid_reg_digit(char digit_char) {
if (digit_char < '0' || digit_char > '7')
return 0;
else
return 1;
}
/* get_matrix -
receives: the word from the line, a line number, a pointer to syntax errors.
Returns a struct of type matrix that holds the registers and the matrix name.*/
mat* get_matrix(char *word, int line_number, int *syntax_errors) {
int i = 0;
mat *my_mat;
my_mat = (mat*)malloc(sizeof(mat));
strcpy(my_mat->reg_row, "");
strcpy(my_mat->reg_col, "");
strcpy(my_mat->mat_name, "");
/*When this function is called, We don't know if word is a matrix or not
we'll parse the word as a speculative matrix, and return NULL if it's not*/
/*mat name*/
while (word[i] != '[' && word[i] != '\0' && word[i] != '\t' && word[i] != ' ') {
my_mat->mat_name[i] = word[i];
i++;
}
my_mat->mat_name[i] = '\0';
i = skip_spaces(word, i);
/*check it didn't end yet*/
if (word[i] == '\0' || word[i] == EOF || word[i] == '\n') {
/*fprintf(stderr, "Error in line %d - mat range expected\n", line_number);
*syntax_errors = 1;*/
return NULL;
}
/*check mat_name is valid symbol*/
if (!is_symbol(my_mat->mat_name, line_number, syntax_errors, FALSE, FALSE) || is_saved_word(my_mat->mat_name, line_number, syntax_errors, TRUE)) {
/*fprintf(stderr, "Error in line %d - invalid name of matrix\n", line_number);
*syntax_errors = 1;*/
return NULL;
}
/*open brackets*/
if (word[i] != '[') {
/*fprintf(stderr, "Error in line %d - expected open brackets\n", line_number);
*syntax_errors = 1;*/
return NULL;
}
/*Here we saw open brackets and we assume this should be a matrix, hence we'll print errors about mat syntax*/
i++;
i = skip_spaces(word, i);
/*we expact:
1. first reg
2. close and open brackets
3. second reg
4. close brackets*/
/*first reg*/
if (tolower(word[i]) != 'r') {
fprintf(stderr, "Error in line %d - expecting register as first matrix range\n", line_number);
*syntax_errors = 1;
return NULL;
}
my_mat->reg_row[0] = 'r';
i++;
if (valid_reg_digit(word[i]) && word[i+1]==']') {
my_mat->reg_row[1] = word[i];
my_mat->reg_row[2] = '\0';
}
else {
fprintf(stderr, "Error in line %d - invalid reg num\n", line_number);
*syntax_errors = 1;
return NULL;
}
i++;
if (word[i] != ']') {
fprintf(stderr, "Error in line %d - closing brackets expected\n", line_number);
*syntax_errors = 1;
return NULL;
}
i++;
if (word[i] != '[') {
fprintf(stderr, "Error in line %d - opening brackets expected\n", line_number);
*syntax_errors = 1;
return NULL;
}
i++;
/*second reg*/
if (word[i] != 'r') {
fprintf(stderr, "Error in line %d - expecting register as second matrix range\n", line_number);
*syntax_errors = 1;
return NULL;
}
my_mat->reg_col[0] = 'r';
i++;
if (valid_reg_digit(word[i]) && word[i+1]==']') {
my_mat->reg_col[1] = word[i];
my_mat->reg_col[2] = '\0';
}
else {
fprintf(stderr, "Error in line %d - invalid reg num\n", line_number);
*syntax_errors = 1;
return NULL;
}
i++;
if (word[i] != ']') {
fprintf(stderr, "Error in line %d - expecting closing brackets\n", line_number);
*syntax_errors = 1;
return NULL;
}
i++;
i = skip_spaces(word, i);
if (word[i] != '\0' && word[i] != EOF && word[i] != '\n') {
fprintf(stderr, "Error in line %d - unexpected characters after matrix usage\n", line_number);
*syntax_errors = 1;
return NULL;
}
return my_mat;
}
/* detect_operand - */
void detect_operand(char operand_position, sentence *parsed, char *temp_word, int line_number, int *syntax_errors, int *operands_in_sentence, int *temp_operand_type)
{
mat *temp_mat;
if (temp_word[0] == '#') {
*temp_operand_type = 0;
*operands_in_sentence = *operands_in_sentence + 1;
strncpy(temp_word, temp_word + 1, strlen(temp_word) - 1);
temp_word[strlen(temp_word) - 1] = '\0';
if (!atoi(temp_word) && strcmp(temp_word,"0")!= 0) {
fprintf(stderr, "Error in line %d - the value after an immediate operand must be a number\n", line_number);
return;
}
else if (operand_position == 'a') /* if we are checking the operand in the 1st place after the opcode */
{
parsed->immediate_operand_a = atoi(temp_word);
strcpy(parsed->source_operand_type, IMMEDIATE_OPERAND_TYPE);
}
else /* if we are checking the operand in the 2nd place after the opcode */
{
parsed->immediate_operand_b = atoi(temp_word);
strcpy(parsed->dest_operand_type, IMMEDIATE_OPERAND_TYPE);
}
if ((atoi(temp_word) > 127 || atoi(temp_word) < -128)) {
fprintf(stderr, "Error in line %d - the value after an immediate operand must be a number\n", line_number);
*syntax_errors = TRUE;
return;
}
}
else if (is_symbol_operand(temp_word, line_number, syntax_errors, FALSE, FALSE)) {
*temp_operand_type = 1;
*operands_in_sentence = *operands_in_sentence + 1;
if (operand_position == 'a') {
strcpy(parsed->operand_1, temp_word);
strcpy(parsed->source_operand_type, DIRECT_OPERAND_TYPE);
}
else
{
strcpy(parsed->operand_2, temp_word);
strcpy(parsed->dest_operand_type, DIRECT_OPERAND_TYPE);
}
}
else if ((temp_mat = get_matrix(temp_word, line_number, syntax_errors))) {
*temp_operand_type = 2;
*operands_in_sentence = *operands_in_sentence + 1;
if (operand_position == 'a') /* if we are checking the operand in the 1st place after the opcode */
{
strcpy(parsed->operand_1, temp_mat->mat_name);
strcpy(parsed->matrix_row_operand_a, temp_mat->reg_row);
strcpy(parsed->matrix_col_operand_a, temp_mat->reg_col);
strcpy(parsed->source_operand_type, MATRIX_OPERAND_TYPE);
}
else /* if we are checking the operand in the 2nd place after the opcode */
{
strcpy(parsed->operand_2, temp_mat->mat_name);
strcpy(parsed->matrix_row_operand_b, temp_mat->reg_row);
strcpy(parsed->matrix_col_operand_b, temp_mat->reg_col);
strcpy(parsed->dest_operand_type, MATRIX_OPERAND_TYPE);
}
free(temp_mat);
}
else if (temp_word[0] == 'r' && valid_reg_digit(temp_word[1]))
{
if (strlen(temp_word) == 2)
{
*temp_operand_type = 3;
*operands_in_sentence = *operands_in_sentence + 1;
if (operand_position == 'a')
{
strcpy(parsed->operand_1, temp_word);
strcpy(parsed->source_operand_type, REGISTER_OPERAND_TYPE);
}
else
{
strcpy(parsed->operand_2, temp_word);
strcpy(parsed->dest_operand_type, REGISTER_OPERAND_TYPE);
}
}
}
return;
}
/* check_destination_address_type -
receives: the current external iteration and the operand type.
The function checks over the operands in the opcodes table that the type is one of it's destination address types.
returns: true if supported, false if not.
*/
int check_destination_address_type(int ext_iteration, int op_type)
{
int j;
for (j = 0; opcodes_table[ext_iteration].destination_operand_types[j] != -1; j++) {
if (opcodes_table[ext_iteration].destination_operand_types[j] == op_type)
return TRUE;
}
return FALSE;
}
/* check_source_address_type -
receives: the current external iteration and the operand type.
The function checks over the operands in the opcodes table that the type is one of it's destination address types.
returns: true if supported, false if not.*/
int check_source_address_type(int ext_iteration, int op_type)
{
int j;
for (j = 0; opcodes_table[ext_iteration].source_operand_types[j] != -1; j++) {
if (opcodes_table[ext_iteration].source_operand_types[j] == op_type)
return TRUE;
}
return FALSE;
}
/* validate_operand_for_opcode -
receives: the sentence struct, the type of operand in source, the type of operand in destination (-999 should be passed if empty),
the line number and a pointer of type int to syntax error.
The function validates the operand according the definitions in opcodes_table in data_structures.c.
If the qty_of_ops is equal to the ammount of operands that the opcode supports,
If op_a is equal to one of the supported operands types for source, if op_b is equal to one of the operands types for destination - the function returns TRUE.
Else, it returns FALSE. */
int validate_operand_for_opcode(sentence *parsed, int op_a, int op_b, int qty_of_ops, int line_number, int *syntax_errors)
{
int i;
int operand_error = 0;
for (i = 0; i < NUM_OF_OPCODES; i++)
{
if (strcmp_lower(parsed->opcode, opcodes_table[i].opcode) == 0) {
if (opcodes_table[i].qty_of_supported_operands < qty_of_ops) {
fprintf(stderr, "Error in line %d - too many operands for the opcode %s\n", line_number, parsed->opcode);
*syntax_errors = 1;
return FALSE;
}
else if (opcodes_table[i].qty_of_supported_operands > qty_of_ops) {
fprintf(stderr, "Error in line %d - not enough valid operands\n", line_number);
*syntax_errors = 1;
return FALSE;
}
if (qty_of_ops == 1) /* if there is a single operand, it is categorized as destination operand */
{
if (check_destination_address_type(i, op_a) == FALSE) {
fprintf(stderr, "Error in line %d - the address type of the destination operand doesn't match to the opcode %s\n", line_number, parsed->opcode);
return FALSE;
}
}
else if (qty_of_ops == 2)
{
if (check_source_address_type(i, op_a) == FALSE) {
fprintf(stderr, "Error in line %d - the address type of the source operand doesn't match to the opcode %s\n", line_number, parsed->opcode);
*syntax_errors = 1;
operand_error = 1;
}
if (check_destination_address_type(i, op_b) == FALSE) {
fprintf(stderr, "Error in line %d - the address type of the destination operand doesn't match to the opcode %s\n", line_number, parsed->opcode);
*syntax_errors = 1;
return FALSE;
}
}
}
}
if (operand_error)