-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.c
381 lines (306 loc) · 14.4 KB
/
project.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int debug = 0; // debug is used to determine that if we are in debug mode or not
struct mac {
char mname[8]; // macro name
char param[10][4]; // maximum 10 parameters and each parameter is maximum 3 characters
char macro[256]; // macro body
};
struct mac buffer[10]; // memory buffer for 10 macros
int m_count = 0; // number of macros
char field[10][7]; // maximum 10 fields in a line and each field is maximum 6 characters
int field_count = 0; // number of fields
int current_line = 0; // number of the current line that is being read
struct pt {
char mname[8]; // macro name
int nparams; // number of parameters
char dummy[10][4]; // maximum 10 parameters and each parameter is maximum 3 characters
char actual[10][4]; // maximum 10 parameters and each parameter is maximum 3 characters
};
FILE* output; // output file
struct pt PT; // structure for parameters
char temp[10][5]; // temp array is used instead of actual array since actual array causes problems with 4 digit parameter names like '100H'
int read(const char* filename) {
FILE* file = fopen(filename, "r");
if(file == NULL) {
printf("Failed to open file in read function: %s\n", filename);
return 0;
}
char line[256]; // actual line from the file
char line_copy[256]; // copy line that is used to make token work correctly
int macro_start = 0; // macro start is used to determine that if we are reading the beginning of a macro or the end of a macro
for(int i = 0; i < 10; i++) { // set read arrays to null
for(int j = 0; j < 8; j++)
buffer[i].mname[j] = '\0';
for(int j = 0; j < 10; j++) {
for(int k = 0; k < 4; k++)
buffer[i].param[j][k] = '\0';
}
for(int j = 0; j < 256; j++)
buffer[i].macro[j] = '\0';
}
while(fgets(line, sizeof(line), file)) {
if(line[0] == 'P' && line[1] == 'R' && line[2] == 'O' && line[3] == 'G') { // read function reads file until 'PROG' part
fclose(file);
if(debug) {
printf("Macro count: %d\n\n", m_count);
for(int i = 0; i < m_count; i++)
printf("Macro name %d: %s\n", i+1, buffer[i].mname);
printf("\n");
for(int i = 0; i < m_count; i++) {
printf("Macro parameters of %s:\n", buffer[i].mname);
for(int j = 0; j < 10; j++) {
if(buffer[i].param[j][0] != '\0')
printf("Macro parameter %d: %s\n", j+1, buffer[i].param[j]);
}
printf("\n");
}
for(int i=0; i < m_count; i++)
printf("Macro body of %s:\n%s\n", buffer[i].mname, buffer[i].macro);
}
return m_count;
}
if(line[0] == '#') { // detect lines that start with '#'
if(macro_start == 0) {
macro_start = 1;
strcpy(line_copy, line);
sscanf(strtok(line_copy, ":"), "#%s", buffer[m_count].mname); // read first line until ':' and save macro name into 'mname'
char* token = strtok(line, " "); // token reads line after 'MACRO' word
token = strtok(NULL, " ");
token = strtok(NULL, " ");
int parameter_count = 0; // number of parameters
while(token != NULL) {
token[strlen(token) - 1] = '\0'; // remove ',' or '\n' from macro parameter
strcpy(buffer[m_count].param[parameter_count], token); // save macro parameter into 'param'
parameter_count++;
token = strtok(NULL, " "); // read next macro parameter
}
}
else {
macro_start = 0;
m_count++;
}
}
else if(macro_start == 1)
strcat(buffer[m_count].macro, line); // save macro body into 'macro'
}
}
void parse(const char* filename) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Failed to open file in parse function: %s\n", filename);
return;
}
char line[256];
int inside_current_line = 0; // the number of the current line inside the function
for(int i = 0; i < 10; i++) { // set parse arrays to null
for(int j = 0; j < 7; j++)
field[i][j] = '\0';
}
while(fgets(line, sizeof(line), file)) {
line[strcspn(line, "\n")] = '\0'; // remove '\n' from line
field_count = 0;
inside_current_line++;
char* token = strtok(line, " "); // token reads line word by word by spaces
while(token != NULL && field_count < 10) {
strcpy(field[field_count], token);
field_count++;
token = strtok(NULL, " \t\n(),=’");
}
if(inside_current_line == current_line) { // break the loop when function reads specific line
if(field[0][0] == '#' && field[0][1] != 'i' && field[0][2] != 'f') {
int field_len_of_first = 0;
for(int i = 0; i < sizeof(field[0])/sizeof(field[0][0]); i++) { // get the visible size
if(field[0][i] == '\0')
break;
field_len_of_first++;
}
for(int i = 0; i < field_len_of_first; i++) { // remove '#' from macro name
if(i != field_len_of_first - 1)
field[0][i] = field[0][i + 1];
else
field[0][field_len_of_first - 1] = '\0';
}
}
break;
}
}
if(debug) {
printf("Field of line %d: ", current_line);
for(int i = 0; i < field_count; i++)
printf("%s ", field[i]);
printf("\n");
}
fclose(file);
}
void createPT() {
for(int i = 0; i < 8; i++) // set createPT arrays to null
PT.mname[i] = '\0';
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 4; j++) {
PT.dummy[i][j] = '\0';
PT.actual[i][j] = '\0';
}
}
int index = 0;
if(!strcmp(field[0], "#if"))
index = 3;
strcpy(PT.mname, field[index]); // get macro name from field array
PT.nparams = field_count - (index + 1); // get parameter count from field count
for(int i = 0; i < 10; i++) { // get dummy parameters from macro array
if(!strcmp(PT.mname, buffer[i].mname))
for(int j = 0; j < PT.nparams; j++)
strcpy(PT.dummy[j], buffer[i].param[j]);
}
for(int i = 1; i < field_count - index; i++) // get actual parameters from field array
strcpy(PT.actual[i - 1], field[i + index]);
for(int i = 0; i < 10; i++) { // copy actual parameters to temp array
for(int j = 0; j < 4; j++) {
temp[i][j] = PT.actual[i][j];
temp[i][4] = '\0';
}
}
if(debug) {
printf("\nMacro name: %s\n", PT.mname);
printf("Parameter count: %d\n", PT.nparams);
for(int i = 0; i < PT.nparams; i++)
printf("Dummy parameter %d: %s\n", i+1, PT.dummy[i]);
for(int i = 0; i < PT.nparams; i++)
printf("Actual parameter %d: %s\n", i+1, temp[i]);
}
}
void expand() {
createPT();
output = fopen("f1.asm", "a");
int macro_number; // macro number is used to determine which macro is used in 'PROG' part
for(int i = 0; i < m_count; i++) {
if(!strcmp(PT.mname, buffer[i].mname))
macro_number = i;
}
char macro_copy[256] = {'\0'}; // macro_copy is used to keep the original macro body unchanged
strcpy(macro_copy, buffer[macro_number].macro);
char* dummyParams[4] = {PT.dummy[0], PT.dummy[1], PT.dummy[2]};
char* actualParams[4] = {temp[0], temp[1], temp[2]};
char new_code[256] = {'\0'}; // new and expanded macro body
char temp_code[256] = {'\0'}; // temp macro body
char* start_position = ¯o_copy[0]; // beginning of the concatenation
char* end_position = ¯o_copy[255]; // end of the macro body
char* current_position = ¯o_copy[0]; // end of the concatenation
int i = 0;
while(i < 3) {
char name[4] = {'\0'}; // name of the current dummy parameter
strcpy(name, dummyParams[i]);
int len = strlen(name); // length of the current dummy parameter
if(current_position != NULL)
current_position = strstr(current_position, dummyParams[i]); // find next dummy parameter
if(current_position != NULL && end_position - current_position >= 0) {
if(len == 1) {
if(*(current_position - 1) == ' ' && *current_position == name[0] && (*(current_position + 1) == '\n' || *(current_position + 1) == '\0')) { // find result matches the current dummy parameter
strncat(new_code, start_position, current_position - start_position);
strncat(new_code, actualParams[i], strlen(actualParams[i]));
current_position = current_position + strlen(dummyParams[i]);
start_position = current_position;
}
else { // find result does not match the current dummy parameter
strncat(new_code, start_position, current_position - start_position + 1);
start_position = current_position + 1;
current_position = strstr(current_position + 1, dummyParams[i]);
}
}
else if(len == 2) {
if(*(current_position - 1) == ' ' && *current_position == name[0] && *(current_position + 1) == name[1] && (*(current_position + 2) == '\n' || *(current_position + 2) == '\0')) { // find result matches the current dummy parameter
strncat(new_code, start_position, current_position - start_position);
strncat(new_code, actualParams[i], strlen(actualParams[i]));
current_position = current_position + strlen(dummyParams[i]);
start_position = current_position;
}
else { // find result does not match the current dummy parameter
strncat(new_code, start_position, current_position - start_position + 1);
start_position = current_position + 1;
current_position = strstr(current_position + 1, dummyParams[i]);
}
}
else if(len == 3) {
if(*(current_position - 1) == ' ' && *current_position == name[0] && *(current_position + 1) == name[1] && *(current_position + 2) == name[2] && (*(current_position + 3) == '\n' || *(current_position + 3) == '\0')) { // find result matches the current dummy parameter
strncat(new_code, start_position, current_position - start_position);
strncat(new_code, actualParams[i], strlen(actualParams[i]));
current_position = current_position + strlen(dummyParams[i]);
start_position = current_position;
}
else { // find result does not match the current dummy parameter
strncat(new_code, start_position, current_position - start_position + 1);
start_position = current_position + 1;
current_position = strstr(current_position + 1, dummyParams[i]);
}
}
}
else { // copy new code to temp code and start searching for the next dummy parameter
strncat(new_code, start_position, end_position - start_position);
strcpy(temp_code, new_code);
i++;
start_position = &temp_code[0];
end_position = &temp_code[255];
current_position = &temp_code[0];
if(i < 3) {
for(int j = 0; j < 256; j++)
new_code[j] = '\0';
}
}
}
fprintf(output, "\n%s\n", new_code);
if(debug)
printf("\nExpanded body:\n%s\n", new_code);
}
void is_macro(int argc, char argv[], const char* line) {
output = fopen("f1.asm", "a");
if(line[0] == '#' && line[1] != 'i') // macro lines
expand();
else if(line[0] == '#' && line[1] == 'i' && line[2] == 'f') { // condition lines
int argument_number = atoi(&field[1][1]); // the index of the condition number in the argv array
int current_number = 0; // the current index
char expected_value[256] = {'\0'}; // expected value for the condition
strcpy(expected_value, field[2]);
char entered_value[256] = {'\0'}; // entered value for the condition by the user
int index = 0;
for(int i = 0; i < 256; i++) { // get entered value from argv array
if(argv[i] == 0) {
current_number++;
while(argument_number == current_number) {
i++;
if(argv[i] == 0)
break;
entered_value[index] = argv[i];
index++;
}
}
}
if(argument_number >= 1 && argument_number < argc && !strcmp(expected_value, entered_value)) // if the entered value satisfies the condition expand the macro
expand();
if(debug) {
printf("argc: %d\n", argc);
printf("Argument number: %d\n", argument_number);
printf("Expected value: %s\n", expected_value);
printf("Entered value: %s\n\n", entered_value);
}
}
else
fprintf(output, "%s", line);
fclose(output);
}
int main(int argc, char* argv[]) {
debug = 0;
FILE* file = fopen(argv[1], "r");
char line[256];
int prog_start = 0; // parse and is_macro function reads file after 'PROG' part
read(argv[1]);
while(fgets(line, sizeof(line), file)) { // read file line by line
current_line++;
if(line[0] == 'P' && line[1] == 'R' && line[2] == 'O' && line[3] == 'G')
prog_start = 1;
if(prog_start) {
parse(argv[1]);
is_macro(argc, *argv, line);
}
}
};