-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre.c
444 lines (435 loc) · 14.1 KB
/
pre.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
#include "helpers.h"
int check_first_group(list *curr){
int flag = 1;
if (curr->word[0] == ';'){
return TRUE;
}
/*syntactly checking for the existence of enough words*/
if(!curr->next){
printf("not enough arguments\n");
return FALSE;
}else if (!curr->next->next){
printf("not enough arguments\n");
return FALSE;
}else if (!curr->next->next->next){
printf("not enough arguments\n");
if(curr->next->next->word[0] !=','){
printf("could be missing a comma\n");
}
return FALSE;
/*not too many though*/
}else if (curr->next->next->next->next){
printf("too many arguments\n");
return FALSE;
}
/*checking for comma words*/
if(curr->next->next->word[0] !=','){
printf("could be missing a comma\n");
flag = 0;
}
/*checking if a var starts with a number*/
if (isdigit(curr->next->word[0])){
printf("var names should not begin with numbers\n");
flag = 0;
}
/*rechecking for # in cases of - and +*/
if (isdigit(curr->next->next->next->word[0])|| curr->next->next->next->word[0] == '-'||curr->next->next->next->word[0] == '+'){
printf("numbers sould be used stricly with '%c' marking behind them while not being inserted via data\n",'#');
flag = 0;
}
if (flag){
return TRUE;
}
return FALSE;
}
int check_second_group(list *curr){
if (curr->word[0] == ';'){
return TRUE;
}
/*syntactly checking for the existence of enough words*/
if(!curr->next){
printf("not enough arguments loacking operand\n");
return FALSE;
}if(!strcmp(curr->next->word,",")){
printf("comma in second type operation\n");
if (curr->next->next!= NULL){
printf("too many arguments\n");
return FALSE;
}
return FALSE;
}else if (curr->next->next){
/*Can't have a comma*/
if(!strcmp(curr->next->next->word,",")){
printf("comma in second type operation\n");
}
/*not too many though*/
printf("too many arguments arguments\n");
return FALSE;
}
return TRUE;
}
int check_third_group(list *curr){
/*just needs to check that it has only the operation as an argument*/
if(curr->next){
printf("too many arguments\n");
return FALSE;
}
return TRUE;
}
int valid_line(list *head){
int flager =1;
list *curr = head;
list *curr2 = head;
/*not checking the symbols(for lack of a better word) written in the lines beginnig*/
if (curr->word[strlen(curr->word)-1]==':'){
curr = curr->next;
}
/*splitting the lines into multiples of cases*/
/*checking the .define .data and such...*/
/*each of them is checked by thier design of code*/
switch(conv_enum2(curr->word)){
case define:
if(curr->next==NULL){
printf("not enough arguments\n");
return FALSE;
}else if (!curr->next->next){
printf("not enough arguments\n");
return FALSE;
}else if (curr->next->next->next==NULL){
printf("not enough arguments\n");
if(curr->next->next->word[0] !='='){
printf("no clear assigment\n");
}
return FALSE;
}else if (curr->next->next->next->next != NULL){
printf("too many arguments\n");
return FALSE;
}
if(curr->next->next->word[0] !='='){
printf("no clear assignment\n");
flager = 0;
}
if (isdigit(curr->next->word[0])){
printf("var names should not begin with numbers\n");
flager = 0;
}
if (flager){
return TRUE;
}
return FALSE;
case dat:
if(curr->next==NULL){
printf("not enough arguments\n");
return FALSE;
}
curr2 = curr->next;
/*checking while it has commas and nexts if has the other as a complement*/
while (curr2->next){
if(strcmp(curr2->word,",") && curr2->next->next){
if(strcmp(curr2->next->word,",")){
printf("missing a comma\n");
return FALSE;
}
}
curr2 = curr2->next;
}
return TRUE;
case str:
if(curr->next==NULL){
printf("not enough arguments\n");
return FALSE;
}
if(curr->next->word[0]!='"'){
printf("missing quotation marks\n");
return FALSE;
}
if(curr->next->word[strlen(curr->next->word)-2]!='"'){
printf("missing quotation marks\n");
return FALSE;
}
if(curr->next->next){
printf("too many argumants\n");
return FALSE;
}
return TRUE;
case ent:
if(curr->next==NULL){
printf("not enough arguments\n");
return FALSE;
}
if(curr->next->next){
printf("too many arguments\n");
return FALSE;
}
return TRUE;
case ext:
if(curr->next==NULL){
printf("not enough arguments\n");
return FALSE;
}
if(curr->next->next){
printf("too many arguments\n");
return FALSE;
}
return TRUE;
case None:
break;
}
/*checking for operations, and splitting them into the three groups - should be syntacticly different*/
switch(conv_enum(curr->word)){
case mov:
return check_first_group(curr);
case cmp:
return check_first_group(curr);
case add:
return check_first_group(curr);
case sub:
return check_first_group(curr);
case not:
return check_second_group(curr);
case clr:
return check_second_group(curr);
case lea:
return check_first_group(curr);
case inc:
return check_second_group(curr);
case dec:
return check_second_group(curr);
case jmp:
return check_second_group(curr);
case bne:
return check_second_group(curr);
case red:
return check_second_group(curr);
case prn:
return check_second_group(curr);
case jsr:
return check_second_group(curr);
case rts:
return check_third_group(curr);
case stop:
return check_third_group(curr);
case NONE:
/*it reached the end of the line starts it should look for and found nothing so it's a bad line*/
if (conv_enum(curr->next->next->word)==NONE && conv_enum2(curr->next->next->word)==None){
printf("unrecognizable command\n");
return FALSE;
}else{
return TRUE;
}
}
return TRUE;
}
void append(list *head, char word[]){
/*as the name suggests it appends to the head of the code list*/
list * current = head;
while (current->next) {
current = current->next;
}
current->next = (list *)malloc(sizeof(list));
strcpy(current->next->word,word);
current->next->next = NULL;
}
int prerun(list *head_tot, FILE *fp){
int validFLAG = TRUE;
int cntwrd = 0;
int cntln = 0;
struct list *head = (list *)malloc(sizeof(list));
list * current = head;
int valid;
int i=0;
int j=0;
int flag=0;
/*32 chars + 1 for \n*/
char word[namelen + 1];
int u,isdigflag = 0;
char line[linelen];
list *curr_tot = head ;
if (head == NULL) {
printf("error whatever could'nt assgin mem");
}
/*while not EOF*/
while(fgets(line,linelen,fp)){
if(line[0]=='\n' || line[0]=='\0'){
continue;
/*dump empty lines*/
}
cntln++;
if (line[0] == ';'){
/*ignoring semi colon*/
}else{
while(line[i]!='\n'){
/*checking for special letters in the line to break to things when i get to them*/
switch(line[i]){
/*space is not a word so append the word to the list and go forward*/
case ' ':
/*if there where no previous valid characters in the line*/
if(!flag){
i++;
continue;
/*if there where no previous words in the line*/
}else if(!cntwrd && flag){
strcpy(head->word,word);
head->next = NULL;
}else if(strcmp(word,"")){
/*checking for spacy colos...*/
if (word[0] == ':'){
printf("colon without anything attached, error in line %d\n",cntln);
append(head,word);
validFLAG = FALSE;
}else{
append(head,word);
}
}
if(flag){
cntwrd++;
j=0;
/*initing the word string*/
memset(word, '\0', sizeof word);
break;
}
case '\t':
/*same as space*/
if(!flag){
break;
}else if(!cntwrd && flag){
strcpy(head->word,word);
head->next = NULL;
}else if (strcmp(word,"")){
if (word[0] == ':'){
printf("colon without anything attached, error in line %d\n",cntln);
validFLAG = FALSE;
break;
}
if(strcmp(word,"")){
append(head,word);
}
}
if (flag){
cntwrd++;
j=0;
memset(word, '\0', sizeof word);
break;
}
case ',':
/*unlike the two above comma is a word so it appends it as well, everything else is similar*/
if(!cntwrd && flag){
strcpy(head->word,word);
head->next = NULL;
append(head,",");
}else if (!flag){
printf("error in line %d, comma at line beginning\n",cntln);
validFLAG = FALSE;
break;
}else if(cntwrd && flag){
if (word[0] == ':'){
printf("colon without anything attached, error in line %d\n",cntln);
validFLAG = FALSE;
}
if(strcmp(word,"")){
append(head,word);
}
append(head,",");
}
if(flag){
cntwrd++;
j=0;
memset(word, '\0', sizeof word);
break;
}
case '=':
/*same as comma above*/
if(!cntwrd && flag){
strcpy(head->word,word);
head->next = NULL;
append(head,"=");
}else if (!flag){
printf("error in line %d, = at line beginning\n",cntln);
break;
}else if(cntwrd && flag){
if (word[0] == ':'){
printf("colon without anything attached, error in line %d\n",cntln);
validFLAG = FALSE;
}
if(strcmp(word,"")){
append(head,word);
}
append(head,"=");
}
if(flag){
cntwrd++;
j=0;
memset(word, '\0', sizeof word);
break;
}
case '[':
/*not an appending checkmark but rather a syntax err checking checkmark, checking to see if what's inside the parentheses is valid*/
u=i+1;
/*starts with a number flag*/
if(isdigit(line[u])){
isdigflag=1;
}
while(line[u]!=']'){
/*sign that's not a number nor a letter*/
if(!isdigit(line[u]) && !isalpha(line[u])){
printf("unallowed sign inside parentheses, error in line %d\n",cntln);
validFLAG =FALSE;
}
/*if it starts with a number it can't continue with letters...*/
if(isalpha(line[u]) && isdigflag){
printf("number with letters, error in line %d\n",cntln);
isdigflag=0;
validFLAG = FALSE;
}
u++;
}
flag++ ;
word[j]=line[i];
isdigflag=0;
j++;
break;
default:
/*defult case of non special letters*/
flag++ ;
word[j]=line[i];
j++;
break;
}
i++;
}
/*appending the last word, becasue I'm checking until line[i]=='\n' so I still have a buffed word to append*/
word[j]='\n';
/*if the '\n' is a word for itself then I append it to the last word of the line*/
if(!strcmp(word,"\n")){
while (current->next) {
current = current->next;
}
strcpy(word,current->word);
word[strlen(word)]='\n';
strcpy(current->word,word);
}else{
append(head,word);
}
/*appending the line to the codes entirity*/
curr_tot = head;
while(curr_tot){
append(head_tot,curr_tot->word);
curr_tot=curr_tot->next;
}
free(curr_tot);
memset(word, '\0', sizeof word);
memset(line, '\0', sizeof line);
i=0;
j=0;
flag=0;
cntwrd = 0;
/*sending line into valid_line func in order to check it's validity as a line*/
valid = valid_line(head);
if (!valid){
printf("error in line %d expanded above\n", cntln);
validFLAG = 0;
}
}
}
return validFLAG;
}