-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.c
More file actions
906 lines (778 loc) · 24.7 KB
/
parser.c
File metadata and controls
906 lines (778 loc) · 24.7 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
#include "error.h"
#include "list.h"
#include "parser.h"
#include "reader.h"
#include "utils.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Parser data, to be passed to the second phase translator. */
ParserData parser_data;
const char* command_names[] = {"mov", "cmp", "add", "sub", "not", "clr", "lea", "inc", "dec", "jmp", "bne", "red", "prn", "jsr", "rts", "stop"};
/* Commands definition. */
const Command commands[] = {
{1, 1, 1, 1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{0, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{0, 1, 1, 1, 0, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{0, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{0, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{0, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{0, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{0, 0, 0, 0, 1, 1, 1, 1, 0, 1},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
/**
* Current char holds the current char in the line being read.
*/
char* current_char;
/**
* Call-back function for list_destruct. Free a label.
*/
void _parser_free_label(void* data) {
Label* label = data;
free(label->label);
}
/**
* Call-back function for list_destruct. Free one line data.
*/
void _parser_free_line_data(void* data) {
LineData* line_data = data;
free(line_data->label_to_extract);
line_data->are = 0;
}
/**
* Call-back function for list_add_ordered(); Performs lexicographical
* comparison of two labels
*
* @param a
* Pointer to Label.
* @param Label b
* Pointer to Label.
*
* @return
* positive number if the first label's name is "larger" than the second's
* name.
*/
int _parser_compare_labels(void* a, void* b) {
Label* label = a;
Label* label2 = b;
return strcmp(label->label, label2->label);
}
/**
* Call-back function for list_add_ordered(); Issues error message when a
* duplicated label is declared.
*
* @param data
* Pointer to Label.
*/
void _parser_duplicated_label(void* data) {
Label* label = data;
error_set("Error", "Redeclaring label.\n", label->line);
}
/**
* Make sure a label doesn't have a register or instruction name.
*
* @param label
* The label string.
* @param line_num
* The line number.
*/
void _parser_validate_label(char* label, int line_num) {
int i;
/* Check if label name is a register name. */
if ((strlen(label) == 2 && label[0] == 'r' && (label[1] - '0') >= 0 && (label[1] - '0') <= MaxRegisterNumber) || !strcmp(label, "PC") || !strcmp(label, "SP") || !strcmp(label, "PSW")) {
error_set("Error", "Illegal label name, same as register.\n", line_num);
}
/* Check if label name is a command name. */
for(i = 0; i < CommandsAmount; i++) {
if (!strcmp(label, command_names[i])) {
error_set("Error", "Label name cannot be a command name.\n", line_num);
}
}
}
/**
* Free all of the parser variables.
*/
void parser_clean() {
/* Free all lists. */
list_destruct(parser_data.parser_symbols, &_parser_free_label);
list_destruct(parser_data.parser_entry_symbols, &_parser_free_label);
list_destruct(parser_data.data_list, &_parser_free_line_data);
list_destruct(parser_data.commands_list, &_parser_free_line_data);
parser_data.parser_symbols = NULL;
parser_data.parser_entry_symbols = NULL;
parser_data.data_list = NULL;
parser_data.commands_list = NULL;
current_char = NULL;
parser_data.errors = 0;
parser_data.IC = 0;
parser_data.DC = 0;
}
/**
* Check whether a line starts with a label.
* Returns the label as a string. The string must be freed by the invoker.
*/
char* _parser_get_line_label(const char* line, int line_num) {
int len = 0;
char* label;
const char* c = line;
label = (char*)malloc(MaxLabelSize + 1);
if (!label)
error_fatal(ErrorMemoryAlloc);
/* Iterate the first word in the line, until colon, space or end of line is
* found. */
/* Label must start label. */
if (!isalpha(*c)) {
return NULL;
}
/* All other characters must be alpha-numeric. */
while (isalnum(*c) && len < MaxLabelSize) {
c++;
len++;
}
if (line[len] == ':') {
/* Label found. */
strncpy(label, line, len);
label[len] = '\0';
return label;
}
return NULL;
}
/**
* Extract number for .data line.
*
* @param word
* First character after .data
* @param line_num
* The current line number.
*/
void extract_data_number(char* word, int const line_num) {
int num_of_param = 0, num_of_comma = 0;
long data_number;
LineData* line_data;
char* first_char;
while (*word != '\0') {
/* Find the beginning of the number */
NextWord(word);
if (*word == '\0' && num_of_param == 0)
error_set("Error", "Data line contains no data.\n", line_num);
if (*word == '\0')
continue;
/* Check for legal start of word. */
if (*word != '-' && *word != '+' && !isdigit(*word)) {
error_set("Error", "Data line contain illegal number.\n", line_num);
continue;
}
/* first_char will hold the first symbol in the number. */
first_char = word;
/* data_number will hold the extracted number. */
data_number = 0;
/* Extract the first digit. */
if (isdigit(*word))
data_number = *word - '0';
word++;
while (!IsBlank(*word) && *word != '\0' && *word != ',') {
if (!isdigit(*word)) {
error_set("Error", "Data line contain illegal number.\n", line_num);
continue;
}
else {
/* Add the next digit to data number (In base 10). */
data_number = 10 * data_number + *word - '0';
/* Make sure we're still inside the 20bit limit. */
if ((*first_char == '-' && data_number > -1 * MinDataNumber) || data_number > MaxDataNumber) {
error_set("Error", "Number is out of limit.\n", line_num);
while (!IsBlank(*word) && *word != '\0' && *word != ',')
word++;
continue;
}
}
word++;
}
/* Convert to two's complement negative */
if (*first_char == '-')
data_number = Complement - data_number;
/* Increment the data counter. */
parser_data.DC++;
/* Insert the line data to the list. */
NewLineData(line_data);
line_data->decimal_address = parser_data.DC;
line_data->machine_code.code = data_number;
parser_data.data_list = list_append(parser_data.data_list, line_data);
num_of_param++;
NextWord(word);
if (*word == ',') {
word++;
num_of_comma++;
}
}
if (num_of_comma != (num_of_param - 1)) {
error_set("Warning", "Data line contain spare comma at the end.\n", line_num);
}
}
/**
* Extract strings from .string lines.
*
* @param word
* The first character after ".string".
* @param line_num
* The line number
* @param line
* The line string.
*/
int extract_string(char* word, int const line_num, char* line) {
LineData* line_data;
char* current_char;
NextWord(word);
if (*word != '"') {
error_set("Error", "String expected after \".string\".\n", line_num);
return 0;
}
/* Set current char to end of line. */
current_char = line + strlen(line) - 1;
/* Find last non whitespace character. */
while (IsBlank(*current_char))
current_char--;
if (*(current_char) != '"' || current_char == word) {
error_set("Error", "String expected after \".string\".\n", line_num);
return 0;
}
/* Create a data line for every character. */
while (word + 1 < current_char) {
parser_data.DC++;
NewLineData(line_data);
line_data->decimal_address = parser_data.DC;
line_data->machine_code.code = *(word + 1);
parser_data.data_list = list_append(parser_data.data_list, line_data);
word++;
}
/* "String delimiter". */
parser_data.DC++;
NewLineData(line_data);
line_data->decimal_address = parser_data.DC;
line_data->machine_code.code = 0;
parser_data.data_list = list_append(parser_data.data_list, line_data);
return 0;
}
/**
* Used to extract labels from within a line; For .extern and .entry lines.
* Adds the labels to lists.
*
* @param word
* First character label.
* @param current_char
* One character after the end of the label.
* @param line_num
* The line number.
* @param line
* The line.
* @param line_type
* Specifies whether the line is .extern or .entry.
*/
void extract_label(char* word, char* current_char, int const line_num, char* line, LineType line_type) {
Label* label;
word = current_char;
NextWord(word);
if (!isalpha(*word)) {
error_set("Error", "Not a legal label.\n", line_num);
return;
}
current_char = line + strlen(line) - 1;
while (IsBlank(*current_char))
current_char--;
while (isalnum(*current_char))
current_char--;
if (current_char > word) {
error_set("Error", "Label expected.\n", line_num);
return;
}
/* Insert the label to the labels list or to the entry labels list. */
NewLabel(label);
label->label = (char*)malloc(MaxLabelSize + 1);
if (!label->label)
error_fatal(ErrorMemoryAlloc);
strcpy(label->label, word);
_parser_validate_label(label->label, line_num);
switch (line_type) {
case LineTypeEntry:
/* Add the label the entry labels list. */
label->line = line_num;
parser_data.parser_entry_symbols = list_add_ordered(parser_data.parser_entry_symbols, label, &_parser_compare_labels, &_parser_duplicated_label);
break;
case LineTypeExtern:
/* Add the label the labels list. */
label->label_type = LabelTypeExtern;
label->line = 0;
parser_data.parser_symbols = list_add_ordered(parser_data.parser_symbols, label, &_parser_compare_labels, &_parser_duplicated_label);
break;
}
}
/**
* Extracting relative addressing (Type 2).
*
* @param number
* String holding the number.
* @param line_num
* The line number.
*
* @return
* The extracted number.
*/
long extract_number(char number[MaxLabelSize + 1], const int line_num) {
long data_number;
int k = 0;
if (number[0] != '-' && number[0] != '+' && !isdigit(number[0])) {
error_set("Error", "Illegal operand, expect number after.\n", line_num);
return -1;
}
data_number = 0;
if (isdigit(number[k]))
data_number = number[k] - '0';
k++;
while (number[k] != '\0') {
if (!isdigit(number[k])) {
error_set("Error", "Illegal number.\n", line_num);
return -1;
}
else {
data_number = 10 * data_number + number[k] - '0';
if ((number[0] == '-' && data_number > -1 * MinDataNumber) || data_number > MaxDataNumber) {
error_set("Error", "Number out of limit.\n", line_num);
return -1;
}
}
k++;
}
/* Convert negatives to two's complement. */
if (number[0] == '-')
return Complement - data_number;
return data_number;
}
/**
* Verify that the first operand is legal, while incrementing the current char
* to the end of operand.
*
* @param operand
* Buffer for the extracted operand.
* @param line_num
* The line number.
*
* @return
* 1 when the operand is valid.
*/
int extract_operand(char* operand, int line_num) {
int i = 1;
if (*current_char != '#' && !isalpha(*current_char)) {
error_set("Error", "Illegal parameter.\n", line_num);
return 0;
}
*operand = *current_char;
current_char++;
while (isalnum(*current_char) && i <= MaxLabelSize) {
operand[i] = *current_char;
current_char++;
i++;
}
if (i == MaxLabelSize + 1) {
error_set("Error", "Illegal operand.\n", line_num);
return 0;
}
operand[i] = '\0';
/* Assuming no white chars at middle of operand. */
if ((*current_char != '{' && *current_char != ',' && *current_char != '\0' && !IsBlank(*current_char)) || (operand[0] == '#' && *current_char == '{')) {
error_set("Error", "Illegal parameter.\n", line_num);
return 0;
}
return 1;
}
/**
* Used for extracting the relative addressing value, while incrementing
* current_char the the end of the operand.
*
* @param operand_offset
* The first character after the opening parenthesis.
* @param line_num
* The line number.
*
* @return
* 1 if operand was found.
*/
int extract_operand_offset(char* operand_offset, int line_num) {
int i = 0;
/* Extracting offset for first parameter if any. */
current_char++;
if (*current_char=='+' || *current_char=='-') {
operand_offset[0] = *current_char;
current_char++;
i++;
}
while (isalnum(*current_char) && i <= MaxLabelSize) {
operand_offset[i] = *current_char;
current_char++;
i++;
}
if (i == MaxLabelSize + 1 || *current_char != '}') {
error_set("Error", "Illegal operand.\n", line_num);
return 0;
}
operand_offset[i] = '\0';
current_char++;
return 1;
}
/**
* Update operand data on a line.
*
* @param line_data
* LineData object to update.
* @param operand
* @param operand_offset
* @param work_on_src
* Whether to set the source or destination address.
*/
void update_operand(LineData* line_data, char* operand,char* operand_offset, int work_on_src) {
/* Index address. */
if (*operand_offset != '\0') {
if (work_on_src) {
line_data->machine_code.bits.src_address = 2;
if (*operand_offset == 'r' && operand_offset[1] >= '0' && operand_offset[1] <= MaxRegisterNumber + '0' && strlen(operand_offset) == 2)
line_data->machine_code.bits.src_reg = operand_offset[1] - '0';
}
else {
line_data->machine_code.bits.dest_address = 2;
if (*operand_offset == 'r' && operand_offset[1] >= '0' && operand_offset[1] <= MaxRegisterNumber + '0' && strlen(operand_offset) == 2)
line_data->machine_code.bits.dest_reg = operand_offset[1] - '0';
}
return;
}
/* Register address. */
if (*operand == 'r' && operand[1] >= '0' && operand[1] <= MaxRegisterNumber + '0' && strlen(operand) == 2) {
if (work_on_src) {
line_data->machine_code.bits.src_address = 3;
line_data->machine_code.bits.src_reg = operand[1] - '0';
}
else {
line_data->machine_code.bits.dest_address = 3;
line_data->machine_code.bits.dest_reg = operand[1] - '0';
}
return;
}
/* Direct address. */
if (*operand != '#') {
if (work_on_src)
line_data->machine_code.bits.src_address = 1;
else
line_data->machine_code.bits.dest_address = 1;
}
}
/**
* Add a line to the command lines list.
*
* @param operand
* @param operand_offset
* @param work_on_src
* Whether to set the source or destination address.
* @param command_index
* The command being performed.
* @param line_num
* @param addr
* Addressing type, either the source address or the destination address
* bits.
*
* @return
* 1 on success.
*/
int add_operand_lines (char *operand, char *operand_offset, int work_on_src, int command_index, int line_num, int addr) {
LineData* line_data = NULL;
switch (addr) {
case 0:
if ((!commands[command_index].src_imidiate_address && work_on_src) || (!commands[command_index].dest_imidiate_address && !work_on_src)) {
error_set("Error", "Illegal address.\n", line_num);
return 0;
}
parser_data.IC++;
NewLineData(line_data);
line_data->decimal_address = parser_data.IC;
line_data->are = 'a';
parser_data.commands_list = list_append(parser_data.commands_list, line_data);
if((line_data->machine_code.code = extract_number(&operand[1], line_num)) == -1)
return 0;
break;
case 1:
if ((!commands[command_index].src_direct_address && work_on_src) || (!commands[command_index].dest_direct_address && !work_on_src)) {
error_set("Error", "Illegal address.\n", line_num);
return 0;
}
parser_data.IC++;
NewLineData(line_data);
line_data->decimal_address = parser_data.IC;
line_data->label_to_extract = (char*)malloc(strlen(operand) + 1);
strcpy(line_data->label_to_extract, operand);
parser_data.commands_list = list_append(parser_data.commands_list, line_data);
break;
case 2:
if ((!commands[command_index].src_index_address && work_on_src) || (!commands[command_index].dest_index_address && !work_on_src)) {
error_set("Error", "Illegal address.\n", line_num);
return 0;
}
parser_data.IC++;
NewLineData(line_data);
line_data->decimal_address = parser_data.IC;
line_data->label_to_extract = (char*)malloc(strlen(operand) + 1);
strcpy(line_data->label_to_extract, operand);
parser_data.commands_list = list_append(parser_data.commands_list, line_data);
/* Adding offset address. */
if (isdigit(*operand_offset) || *operand_offset=='+' || *operand_offset=='-') {
parser_data.IC++;
NewLineData(line_data);
line_data->decimal_address = parser_data.IC;
line_data->are = 'a';
if((line_data->machine_code.code = extract_number(operand_offset, line_num)) == -1) {
error_set("Error", "Illegal address.\n", line_num);
return 0;
}
parser_data.commands_list = list_append(parser_data.commands_list, line_data);
}
else if (!(*operand_offset == 'r' && strlen(operand_offset) == 2 && *(operand_offset + 1) >= '0' && *(operand_offset + 1) <= MaxRegisterNumber + '0')) {
parser_data.IC++;
NewLineData(line_data);
line_data->decimal_address = parser_data.IC;
line_data->label_to_extract = (char*)malloc(strlen(operand_offset) + 1);
strcpy(line_data->label_to_extract, operand_offset);
parser_data.commands_list = list_append(parser_data.commands_list, line_data);
}
break;
case 3:
if ((!commands[command_index].src_direct_reg_address && work_on_src) || (!commands[command_index].dest_direct_reg_address && !work_on_src)) {
error_set("Error", "Illegal address.\n", line_num);
return 0;
}
break;
}
return 1;
}
/**
* Does the initial parsing of the assembly file.
*
* @return
* 0 when any errors where introduced during the parsing.
*/
int parse() {
/* Beginning of line*/
char* line;
/* Operands. */
char operand1[MaxLabelSize + 1], operand2[MaxLabelSize + 1];
/* Operands for type 2 addressing */
char operand1_offset[MaxLabelSize + 1], operand2_offset[MaxLabelSize + 1];
/* Line label. */
Label* label = NULL;
/* Holds all of the about the line being read. */
LineData* line_data = NULL;
char *word;
char command_type[MaxRegisterNumber];
int line_num = 0;
int command_index, j;
printf("Parsing %s.\n", reader_get_file_name(ReaderFileExtension));
while ((line = reader_get_line())) {
line_num++;
/* Commented-out line. */
if (*line == ';')
continue;
/* Create new label. */
NewLabel(label);
label->label = _parser_get_line_label(line, line_num);
if (label->label)
_parser_validate_label(label->label, line_num);
word = line;
/* Finding first word. */
NextWord(word);
/* Empty line. */
if (*word == '\0') {
free (label);
continue;
}
/* Find beginning of next word after label. */
if (label->label) {
word = line + strlen(label->label) + 1;
NextWord(word);
}
if (*word == '\0' && label->label) {
/* Assuming every label declaration must follow commands or
* declaration of data. */
error_set("Error", "Label with no command.\n", line_num);
free (label);
continue;
}
/* Find end of command. */
current_char = word + 1;
while (!IsBlank(*current_char) && *current_char != '/' && *current_char != '\0')
current_char++;
/* Update the label in the symbols list only it's a ".data", ".string"
* or an instruction "line." For ".extern" or ".entry", the label is
* ignored. */
/* Extracting label of .string or .data lines. */
if (((!strncmp(word, ".data", 5) && (current_char - word) == 5 && *current_char != '/')
|| (!strncmp(word, ".string", 7) && (current_char - word) == 7 && *current_char != '/'))
&& label->label) {
/* TODO: Explain +1 */
label->line = parser_data.DC + 1;
label->label_type = LabelTypeData;
/* Add the label to the labels list. */
parser_data.parser_symbols = list_add_ordered(parser_data.parser_symbols, label, &_parser_compare_labels, &_parser_duplicated_label);
}
/* Data line. */
if (!strncmp(word, ".data", 5) && (current_char - word) == 5 && *current_char != '/') {
extract_data_number(current_char, line_num);
continue;
}
/* String line. */
if (!strncmp(word, ".string", 7) && (current_char - word) == 7 && *current_char != '/') {
extract_string(current_char, line_num, line);
continue;
}
/* Entry label declaration line. */
if (!strncmp(word, ".entry", 6) && (current_char - word) == 6 && *current_char != '/') {
extract_label(word, current_char, line_num, line, LineTypeEntry);
continue;
}
/* External label declaration line. */
if (!strncmp(word, ".extern", 7) && (current_char - word) == 7 && *current_char != '/') {
extract_label(word, current_char, line_num, line, LineTypeExtern);
continue;
}
/* Insert a instruction line to the line data list. */
parser_data.IC++;
NewLineData(line_data);
line_data->decimal_address = parser_data.IC;
parser_data.commands_list = list_append(parser_data.commands_list, line_data);
/* 'a' is for Absolute. */
line_data->are = 'a';
line_data->is_instruction = 1;
/* Command line. */
if(label->label) {
label->line = parser_data.IC;
label->label_type = LabelTypeCommand;
parser_data.parser_symbols = list_add_ordered(parser_data.parser_symbols, label, &_parser_compare_labels, &_parser_duplicated_label);
}
if (strlen(line) > MaxLineSize) {
error_set("Error", "Line length exceeding 80 characters.\n", line_num);
continue;
}
/* Find command code. */
for (command_index = 0; command_index < CommandsAmount && strncmp(word, command_names[command_index], current_char - word); command_index++);
if (command_index == CommandsAmount) {
error_set("Error", "Unknown command.\n", line_num);
continue;
}
/* Set command code on the line. */
line_data->machine_code.bits.opcode = command_index;
/* Find '/0' or /1 , assuming /0 is not followed by other options. */
NextWord(current_char);
command_type[0] = *current_char;
if (*current_char != '\0') {
current_char++;
NextWord(current_char);
command_type[1] = *current_char;
command_type[2] = '\0';
}
if (!(command_type[0] == '/' && (command_type[1] == '0' || command_type[1] == '1'))) {
error_set("Error", "Type expected after command.\n", line_num);
continue;
}
if (*current_char != '\0')
current_char++;
/* Extract first operand. */
if (command_type[1] == '1') {
for (j = 0; j < 4; j++) {
NextWord(current_char);
command_type[2 + j] = *current_char;
command_type[2 + j + 1] = '\0';
if (*current_char != '\0')
current_char++;
}
if (!(command_type[0] == '/' &&
command_type[2] == '/' &&
command_type[4] == '/' &&
(command_type[5] == '0' || command_type[5] == '1') &&
(command_type[3] == '0' || command_type[3] == '1'))) {
error_set("Error", "Illegal command type.\n", line_num);
continue;
}
line_data->machine_code.bits.type = command_type[1] - '0';
line_data->machine_code.bits.comb = command_type[5] - '0' + 2 * (command_type[3] - '0');
}
/* Verify blank after type. */
if (!IsBlank(*current_char) && *current_char != '\0') {
error_set("Error", "Blank char is required after command.\n", line_num);
continue;
}
/* Find operands. */
operand1[0] = '\0';
operand2[0] = '\0';
operand1_offset[0] = '\0';
operand2_offset[0] = '\0';
NextWord(current_char);
if (*current_char!='\0') {
if (!extract_operand(operand1, line_num))
continue;
if (*current_char == '{')
if (!extract_operand_offset(operand1_offset, line_num))
continue;
}
NextWord(current_char);
/* Extract other operands. */
if (*current_char == ',') {
current_char++;
NextWord(current_char);
if (!extract_operand(operand2, line_num))
continue;
if (*current_char == '{')
if (!extract_operand_offset(operand2_offset, line_num))
continue;
}
NextWord(current_char);
if (*current_char != '\0' || (*operand1 == '#' && *operand1_offset != '\0') || (*operand2 == '#' && *operand2_offset != '\0')) {
error_set("Error", "Illegal parameters.\n", line_num);
continue;
}
/* No operand required. */
if (!commands[command_index].src_operand && !commands[command_index].dest_operand && *operand1 != '\0') {
error_set("Error", "No operands required.\n", line_num);
continue;
}
/* One operand required. */
if (!commands[command_index].src_operand && commands[command_index].dest_operand && (*operand1 == '\0' || *operand2 != '\0')) {
error_set("Error", "Exactly one operand required.\n", line_num);
continue;
}
/* Two operands required. */
if (commands[command_index].src_operand && commands[command_index].dest_operand && (*operand1 == '\0' || *operand2 == '\0')) {
error_set("Error", "Two operands required.\n", line_num);
continue;
}
/* Handling addressing. */
/* Two operands exist. */
if (*operand2 != '\0') {
update_operand(line_data, operand1, operand1_offset, 1);
update_operand(line_data, operand2, operand2_offset, 0);
if (!add_operand_lines(operand1, operand1_offset, 1, command_index, line_num, line_data->machine_code.bits.src_address))
continue;
if (!add_operand_lines(operand2, operand2_offset, 0, command_index, line_num, line_data->machine_code.bits.dest_address))
continue;
}
/* When one operand exists. */
if (*operand1 != '\0' && *operand2 == '\0') {
update_operand(line_data, operand1, operand1_offset, 0);
if (!add_operand_lines(operand1, operand1_offset, 0, command_index, line_num, line_data->machine_code.bits.dest_address))
continue;
}
}
return !parser_data.errors;
}