-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacro_processing.c
275 lines (256 loc) · 9.93 KB
/
macro_processing.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
/*********************************FILE__HEADER*********************************\
* File: macro_processing.c
* Authors: Daniel Brodsky & Lior Katav
* Date: August-2023
* Description: This source file contains the functionality for
* managing macro constructs in the our compiler program.
* It implements procedures for recognizing reserved
* keywords, validating macro definitions and performing
* a pre-processing step on assembly language input files.
* This preprocessing step identifies macro definitions,
* expands macros at their invocation, and writes the
* transformed code to an output file.
* These routines contribute to the larger task of
* parsing and interpreting the assembly language.
\******************************************************************************/
/******************************** Header Files ********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "macro_processing.h"
#include "macro.h"
#include "param_validation.h"
/**************************** Forward Declarations ****************************/
int isReservedKeyword(char *word, ProgramState *programState);
int isValidMacroDefinition(const char *line);
/************************* Functions Implementations *************************/
/**
* Preprocesses the given input file and writes the result to the output file.
* Preprocessing involves expanding any macros defined in the input file.
*
* @param input_file The name of the file to preprocess.
* @param output_file The name of the file to write the preprocessed code to.
* @param programState The current state of the program.
*
* @return SUCCESS if the preprocessing is successful, FAILURE otherwise.
*/
Status preProcess(const char *input_file, const char *output_file,
ProgramState *programState) {
/* Variable Initializations */
Status ret = SUCCESS;
int i, count_line = 0;
char line[1024];
char *ptr = NULL, *newline = NULL, *end = NULL;
FILE *inputFile = NULL, *outputFile = NULL;
Macro *macroToExpand = NULL;
Macro *currentMacro = NULL;
MacroVector *macroVector = new_macro_vector();
/* Open input and output files. If opening fails, return FAILURE */
inputFile = fopen(input_file, "r");
if (NULL == inputFile) {
return FAILURE;
}
outputFile = fopen(output_file, "w");
if (NULL == outputFile) {
if (inputFile != NULL) {
fclose(inputFile);
}
return FAILURE;
}
/* First pass: build the list of macros */
while (fgets(line, sizeof(line), inputFile)) {
count_line++;
/* Filter out comments and empty lines */
if (1 == filter_line(line)) {
continue;
}
/* Remove leading whitespaces */
ptr = line;
while (isspace((unsigned char) *ptr)) {
ptr++;
}
/* Check for macro definitions */
if (strncmp(ptr, "mcro", 4) == 0) {
ptr += 4;
while (isspace((unsigned char) *ptr)) {
ptr++;
}
end = ptr + strlen(ptr) - 1;
while (end > ptr && isspace((unsigned char) *end)) {
end--;
}
*(end + 1) = '\0';
if (!isReservedKeyword(ptr, programState)) {
if (!isValidMacroDefinition(
line)) { /*Check validity of entire line*/
fprintf(stderr,
"Error: Invalid macro definition '%s' on line %d.\n",
line, count_line);
ret = FAILURE;
} else {
currentMacro = new_macro(ptr);
push_back_macro(macroVector, currentMacro);
}
} else {
fprintf(stderr, "Error: Invalid macro name '%s' on line %d.\n",
ptr, count_line);
ret = FAILURE;
}
} else if (strncmp(ptr, "endmcro", 7) == 0) {
ptr += 7;
while (isspace((unsigned char) *ptr)) {
ptr++;
}
if (*ptr != '\0' && *ptr != '\n' && *ptr != '\r') {
fprintf(stderr,
"Error: Unexpected characters after 'endmcro' on line %d.\n",
count_line);
ret = FAILURE;
}
currentMacro = NULL;
} else if (currentMacro) {
char *command = my_strdup(line);
size_t length = strlen(command);
if (length > 0 && command[length - 1] == '\n') {
command[length - 1] = '\0';
}
push_back(currentMacro->commands, command);
}
}
/* Rewind the input file to the beginning for the second pass */
rewind(inputFile);
/* Second pass: output the file, expanding macros */
while (fgets(line, sizeof(line), inputFile)) {
if (1 == filter_line(line)) {
continue;
}
ptr = line;
/* Skip leading whitespaces */
while (isspace((unsigned char) *ptr)) {
ptr++;
}
/* Remove the newline at the end of the line, if present */
newline = strchr(ptr, '\n');
if (newline) {
*newline = '\0';
}
/* If the line starts with a macro definition or end of macro, skip it */
if (strncmp(ptr, "mcro", 4) == 0) {
do {
fgets(line, sizeof(line), inputFile);
ptr = line;
while (isspace((unsigned char) *ptr)) {
ptr++;
}
newline = strchr(ptr, '\n');
if (newline) {
*newline = '\0';
}
} while (strncmp(ptr, "endmcro", 7) != 0);
continue;
}
macroToExpand = NULL;
/* Iterate through all defined macros */
for (i = 0; i < macroVector->size; ++i) {
/** If the current line starts with the name of a macro, this indicates
* a macro invocation. The macro name is followed by either a space,
* newline or null character.
*
* strncmp is used to compare the first n characters of ptr and macro name,
* where n is the length of the macro name. If they are equal, strncmp returns 0.
*
* If such a macro invocation is detected, the pointer macroToExpand is set to the
* current macro, and the loop is exited early.
*/
if (strncmp(ptr, macroVector->macros[i]->name,
strlen(macroVector->macros[i]->name)) == 0 &&
(ptr[strlen(macroVector->macros[i]->name)] == ' ' ||
ptr[strlen(macroVector->macros[i]->name)] == '\n' ||
ptr[strlen(macroVector->macros[i]->name)] == '\0')) {
macroToExpand = macroVector->macros[i];
break;
}
}
if (macroToExpand) {
/* Expand the macro */
for (i = 0; i < macroToExpand->commands->size; ++i) {
char *expandedLine = macroToExpand->commands->items[i];
/* Remove leading spaces from expanded line */
while (isspace((unsigned char) *expandedLine)) {
expandedLine++;
}
fputs(expandedLine, outputFile);
fputs("\n", outputFile);
}
} else {
/* Otherwise, write the line to the output as is */
fputs(ptr, outputFile);
fputs("\n", outputFile);
}
}
/* Cleanup: Close files and free allocated memory */
fclose(inputFile);
fclose(outputFile);
for (i = 0; i < macroVector->size; ++i) {
free_macro(macroVector->macros[i]);
}
free(macroVector->macros);
free(macroVector);
return ret;
}
/******************************************************************************/
/**
* This function checks if a given word is a reserved keyword in the assembly language.
*
* @param word - the word to be checked.
* @param programState - the current state of the program.
*
* @return 1 if the word is a reserved keyword; 0 otherwise.
*
* The function checks if the word is a command, an instruction, a register, or a label.
* If any of these checks is true, the function returns 1, indicating that the word is a reserved keyword.
* If none of the checks is true, the function returns 0.
*/
int isReservedKeyword(char *word, ProgramState *programState) {
if (findCommand(word) != -1) {
return 1;
}
if (findInstruction(word) != -1) {
return 1;
}
if (isRegister(word)) {
return 1;
}
if (isLabel(word, programState)) {
return 1;
}
return 0;
}
/******************************************************************************/
/**
* This function checks if a given line is a valid macro definition in the assembly language.
*
* @param line - the line to be checked.
*
* @return 1 if the line is a valid macro definition; 0 otherwise.
*
* A valid macro definition must have exactly two space-separated words.
* The function counts the number of words in the line and returns 1 if there are exactly two words.
* If the number of words is not two, the function returns 0.
*/
int isValidMacroDefinition(const char *line) {
/* A valid macro definition has 2 space-separated words */
int words = 0;
while (*line) {
/* Skip spaces */
while (*line && isspace(*line)) line++;
if (!*line) break;
/* Found a word */
words++;
/* Skip the word */
while (*line && !isspace(*line)) line++;
}
return (words == 2);
}
/******************************************************************************/