-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.c
429 lines (306 loc) · 8.6 KB
/
validator.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
#define _GNU_SOURCE /*For using strdup without compiler warnings*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "assembler.h"
#include "registers.h"
#include "parser.h"
#include "symbols.h"
#include "commands.h"
#define DATA_NOT_AN_INTEGER "One of the data parameters is not an integer."
#define NO__GUIDANCE_STRING "There is no guidance string."
#define COMMAND_DOES_NOT_SUPPORT_OPERAND "The command does not support this operand."
#define ADDRESSING_MODE_NOT_ALLOWED "The addressing mode is not allowed."
#define NOT_A_VALID_COMMAND_NAME "The command name is not valid."
#define COMMAND_REQUIRES_OPERAND "The command requires an operand."
#define NOT_A_VALID_IMMEDIATE_ADDRESSING_MODE "The operand is not a valid immediate addressing mode."
#define NOT_A_VALID_DIRECT_ADDRESSING_MODE "The operand is not a valid direct addressing mode."
#define NOT_A_VALID_INDEX_ADDRESSING_MODE "The operand is not a valid index addressing mode."
#define NOT_A_VALID_INDEX2D_ADDRESSING_MODE "The operand is not a valid index 2d addressing mode."
#define NOT_A_VALID_REGISTER_ADDRESSING_MODE "The operand is not a valid register addressing mode."
int isValidSymbol(char *symbol);
int isValidIntegetList(char *list);
int isInteger(char *value);
int isValidCommandName(char *commandName);
char *validateExternGuidance(CommandParts parts);
char *validateEntryGuidance(CommandParts parts);
char *validateDataGuidance(CommandParts parts);
char *validateStringGuidance(CommandParts parts);
char *validateCommandOperands(CommandParts parts);
char *validateCommandOperand(char *operand, short allowedAddressingModes);
/*Validates the the operand string is a valid operand in the given addressing mode */
char *validateAddressingMode(int addressingMode, char *operand);
char *validateImmediateAddressingMode(char *operand);
char *validateDirectAddressingMode(char *operand);
char *validateIndexAddressingMode(char *operand);
char *validateIndex2dAddressingMode(char *operand);
char *validateRegisterAddressingMode(char *operand);
/* Validates a given assembly command line */
/* returns NULL if the command line is valid */
/* or an error message otherwise. */
char *validateAssemblyLine(char *line)
{
CommandParts parts; /* Unfilled fields must be NULL in order for the function to work */
char *validationResult = NULL;
char *lineToValidate;
StatementType type;
lineToValidate = strdup(line); /*We work on a copy to keep the original line unchanged*/
parts = parseAssemblyLine(lineToValidate);
type = getStatementType(lineToValidate);
switch (type){
case EXTERNGUIDANCE:
validationResult = validateExternGuidance(parts);
break;
case ENTRYGUIDANCE:
validationResult = validateEntryGuidance(parts);
break;
case DATAGUIDANCE:
validationResult = validateDataGuidance(parts);
break;
case STRINGGUIDANCE:
validationResult = validateStringGuidance(parts);
break;
case COMMAND:
if(!isValidCommandName(parts.command))
{
validationResult = NOT_A_VALID_COMMAND_NAME;
}
else /* A valid command name */
{
validationResult = validateCommandOperands(parts);
}
break;
case COMMENT:
default:
break;
}
return validationResult;
}
/*Checks if the given string is a valid symbol */
int isValidSymbol(char *symbol)
{
int i=1, length;
length = strlen(symbol);
if (isRegister(symbol)) return 0; /*symbol cannot be a register*/
if (isValidCommandName(symbol)) return 0; /* symbol cannot be a command*/
if(length > MAX_SYMBOL_LENGTH) return 0;
if(isalpha(symbol[0]))
{
while(i < length)
{
if(isalpha(symbol[i]) || isdigit(symbol[i]))
{
i++;
} else
{
return FALSE;
}
}
} else
{
return FALSE;
}
return TRUE;
}
int isValidIntegetList(char *list)
{
char *currData;
int isValid = TRUE;
if(list == NULL) return 0;
if ((currData = extractFirstGuidanceData(list)) != NULL)
{
if (isInteger(currData))
{
while((currData = extractNextGuidanceData()) != NULL)
{
if(!isInteger(currData)) isValid = FALSE;
}
}
else
{
isValid = FALSE;
}
} else /*list is empty */
{
isValid = FALSE;
}
return isValid;
}
int isInteger(char *value)
{
/*Skip leading spaces*/
while(isspace(*value)) value++;
while (*value !='\0' && *value != '\n' && *value != EOF)
{
if(isdigit(*value)|| *value == '+' || *value == '-')
{
value++;
} else
{
return FALSE;
}
}
return TRUE;
}
int isValidCommandName(char *commandName)
{
Command command;
command = getCommandByCommandName(commandName);
return command.name != NULL;
}
char *validateExternGuidance(CommandParts parts)
{
char *result = NULL;
if(!isValidSymbol(parts.externSymbol))
{
result = SYMBOL_NOT_VALID;
}
return result;
}
char *validateEntryGuidance(CommandParts parts)
{
char *result = NULL;
if(!isValidSymbol(parts.entrySymbol))
{
result = SYMBOL_NOT_VALID;
}
return result;
}
char *validateDataGuidance(CommandParts parts)
{
char *result = NULL;
if(!isValidIntegetList(parts.data))
{
result = DATA_NOT_AN_INTEGER;
}
return result;
}
char *validateStringGuidance(CommandParts parts)
{
char *result = NULL;
if(parts.data == NULL)
{
result = NO__GUIDANCE_STRING;
}
return result;
}
char *validateCommandOperands(CommandParts parts)
{
char *result = NULL;
Command command;
command = getCommandByCommandName(parts.command);
/*Validate the source operand*/
result = validateCommandOperand(parts.sourceOperand, command.srcOperandAddressingModes);
if(result == NULL) /*Validate the destination operand */
{
result = validateCommandOperand(parts.destinationOperand, command.dstOperandAddressingModes);
}
return result;
}
char *validateCommandOperand(char *operand, short allowedAddressingModes)
{
char *result = NULL;
int addressingMode;
if(allowedAddressingModes == NO_OPERAND && operand != NULL) /*No addressing modes are allowed the the operand is not empty*/
{
result = COMMAND_DOES_NOT_SUPPORT_OPERAND;
} else if(allowedAddressingModes != NO_OPERAND && operand == NULL) /*The command requires an operand and the operand is empty*/
{
result = COMMAND_REQUIRES_OPERAND;
} else /*There are allowed addressing modes*/
{
if(operand != NULL)
{
addressingMode = parseAddressingMode(operand);
result = validateAddressingMode(addressingMode, operand);
if(result == NULL) /*Operand has a valid addressing mode form */
{
/*Check that the operand addressing mode is allowed for the given command */
if(!(allowedAddressingModes & convertToAddressingModeFlag(addressingMode)))
{
result = ADDRESSING_MODE_NOT_ALLOWED;
}
}
}
}
return result;
}
/*Validates the the operand string is a valid operand in the given addressing mode */
char *validateAddressingMode(int addressingMode, char *operand)
{
char *result = NULL;
switch(addressingMode)
{
case IMMEDIATE_ADDRESSING_MODE:
result = validateImmediateAddressingMode(operand);
break;
case DIRECT_ADDRESSING_MODE:
result = validateDirectAddressingMode(operand);
break;
case INDEX_ADDRESSING_MODE:
result = validateIndexAddressingMode(operand);
break;
case INDEX2D_ADDRESSING_MODE:
result = validateIndex2dAddressingMode(operand);
break;
case REGISTER_ADDRESSING_MODE:
result = validateRegisterAddressingMode(operand);
break;
default:
break;
}
return result;
}
char *validateImmediateAddressingMode(char *operand)
{
char *result = NULL;
if (!isInteger(extractImmediateAddressingModeValue(operand)))
{
result = NOT_A_VALID_IMMEDIATE_ADDRESSING_MODE;
}
return result;
}
char *validateDirectAddressingMode(char *operand)
{
char *result = NULL;
if (isValidSymbol(operand)== 0)
{
result = NOT_A_VALID_DIRECT_ADDRESSING_MODE;
}
return result;
}
char *validateIndexAddressingMode(char *operand)
{
char *symbol, *offset, *result;
result = NULL;
symbol = extractIndexAddressingOffset(operand);
offset = extractIndexAddressingOffset(operand);
if (!isValidSymbol(symbol) || !isValidSymbol(offset))
{
result = NOT_A_VALID_INDEX_ADDRESSING_MODE;
}
return result;
}
char *validateIndex2dAddressingMode(char *operand)
{
char *symbol, *offset1, *offset2, *result;
result = NULL;
symbol = extractIndex2dAddressingSymbol(operand);
offset1 = extractIndex2dAddressingOffset(operand);
offset2 = extractIndex2dAddressingRegister(operand);
if (!isValidSymbol(symbol) || !isValidSymbol(offset1) || !isRegister(offset2))
{
result = NOT_A_VALID_INDEX2D_ADDRESSING_MODE;
}
return result;
}
char *validateRegisterAddressingMode(char *operand)
{
char *result = NULL;
if (!isRegister(operand))
{
result = NOT_A_VALID_REGISTER_ADDRESSING_MODE;
}
return result;
}