-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMacroprocessor.c
296 lines (272 loc) · 7.09 KB
/
Macroprocessor.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct symbol {
char name[10];
char value[10];
};
struct symbol def_symbols[50];
struct mac {
char mname[10];
int nparams;
char p[3][10];
char mbody[1000];
};
struct mac macros[20];
struct param {
char dp[3][10];
char rp[3][10];
};
struct param PT[5];
int main( void ){
int symbolnum=0;
int macronum=0;
int paramnum=0;
int i,j,a;
char word[100];
char chr;
FILE *fps, *fpe;
fps=fopen("SourceCode.txt","r");
fpe=fopen("ExpandedCode.txt","w");
if ( fps==NULL )
printf("Error opening SourceCode");
if ( fpe==NULL )
printf("Error opening ExpandedCode");
fscanf(fps,"%s",word);
while (strcmp(word,"@DEFINE")==0 || strcmp(word,"@MACRO")==0){ //Reading define and macro definitions and printing-Step1
if(strcmp(word,"@DEFINE")==0){
fscanf(fps,"%s %s",def_symbols[symbolnum].name,def_symbols[symbolnum].value);
printf("Def-Symbols Table:\n");
printf("Symbol Name:\t\t%s\n",def_symbols[symbolnum].name);
printf("Replacement Text:\t%s\n\n",def_symbols[symbolnum].value);
symbolnum++;
}
else if(strcmp(word,"@MACRO")==0){
macros[macronum].nparams=0;
fscanf(fps,"%s",macros[macronum].mname);
chr=fgetc(fps);
if(chr==' ' || chr=='\n'){
chr=fgetc(fps);
}
a=0;
i=0;
j=0;
if(chr=='('){ //Reading macroparameters and inserting in table
while(chr!=')'){
chr=fgetc(fps);
if (chr==',' || chr==')'){
i++;
j=0;
macros[macronum].nparams++;
}
if(chr==','){
chr=fgetc(fps);
}
if(chr==' '){
chr=fgetc(fps);
}if(chr!=')'){
macros[macronum].p[i][j]=chr;
j++;
}
}
}
chr=fgetc(fps);
while (chr!='@'){ //Reading macrobody and inserting in table
macros[macronum].mbody[a]=chr;
chr=fgetc(fps);
a++;
if(chr=='@'){
char chr2=chr;
chr=fgetc(fps);
if(chr=='M'){
macros[macronum].mbody[a]=chr2;
a++;
}else{
break;
}
}
}
macros[macronum].mbody[a-1]='\0';
printf("Macros Table:\n");
printf("Macro Name:\t\t%s\n",macros[macronum].mname);
printf("Number of Parameters:\t%d\n",macros[macronum].nparams);
if (macros[macronum].nparams==1)
printf("Dummy Parameters:\t%s\n",macros[macronum].p[0]);
else if (macros[macronum].nparams==2){
printf("Dummy Parameters:\t%s, ",macros[macronum].p[0]);
printf("%s\n",macros[macronum].p[1]);
}
else if (macros[macronum].nparams==3){
printf("Dummy Parameters:\t%s, ",macros[macronum].p[0]);
printf("%s, ",macros[macronum].p[1]);
printf("%s\n",macros[macronum].p[2]);
}
printf("Macro Body:\n%s",macros[macronum].mbody);
macronum++;
}
fscanf(fps,"%s",word);
if(strcmp(word,"NDM")==0){
fscanf(fps,"%s",word);
}
}
fputc('\t',fpe);
fprintf(fpe, "%s",word);
while (!feof(fps)){ //Reading the rest of the input file-Step2
chr=fgetc(fps);
while (chr!='@' || feof(fps)){
fputc(chr,fpe);
chr=fgetc(fps);
if(feof(fps)){return 0;}
}
fscanf(fps,"%s",word);
if(strcmp(word,"MCALL")==0){ //If there is a macrocall
fscanf(fps,"%s",word);
int x;
int currentmacro=-1;
for(x=0; x<macronum; x++){
if(strcmp(word,macros[x].mname)==0){
currentmacro=x;
}
}
if(currentmacro==-1){
printf("Error:There is no macro with this name");
}
else{ //Creating paramTable and filling it
int index=0;
while(macros[currentmacro].p[0][index]!='\0'){ //inserting dummy params
PT[paramnum].dp[0][index]=macros[currentmacro].p[0][index];
index++;
}PT[paramnum].dp[0][index]='\0';
index=0;
while(macros[currentmacro].p[1][index]!='\0'){
PT[paramnum].dp[1][index]=macros[currentmacro].p[1][index];
index++;
}PT[paramnum].dp[1][index]='\0';
index=0;
while(macros[currentmacro].p[2][index]!='\0'){
PT[paramnum].dp[2][index]=macros[currentmacro].p[2][index];
index++;
}PT[paramnum].dp[2][index]='\0';
chr=fgetc(fps);
if(chr==' ' || chr=='\n' || chr=='\t'){
chr=fgetc(fps);
}
a=0;
i=0;
j=0;
if(chr=='('){ //inserting real params
while(chr!=')'){
chr=fgetc(fps);
if (chr==',' || chr==')'){
i++;
j=0;
}
if(chr==','){
chr=fgetc(fps);
}
if(chr==' '){
chr=fgetc(fps);
}
if(chr!=')'){
PT[paramnum].rp[i][j]=chr;
j++;
}
}
}
int index1=2;
int index2=0;
char dummy[10];
while(macros[currentmacro].mbody[index1+1]!='\0'){ //Writing macrobody to ExpandedCode file
fputc(macros[currentmacro].mbody[index1],fpe);
index1++;
if(macros[currentmacro].mbody[index1-1]==' ' || macros[currentmacro].mbody[index1-1]=='\t'
&& macros[currentmacro].mbody[index1-2]!='\n'){
while(macros[currentmacro].mbody[index1]!='\n'){
if(macros[currentmacro].mbody[index1]==','
|| macros[currentmacro].mbody[index1]=='\n')
break;
dummy[index2]=macros[currentmacro].mbody[index1];
index1++;
index2++;
}dummy[index2]='\0';
if(strcmp(dummy,PT[paramnum].dp[0])==0){ //substituting
fprintf(fpe, "%s",PT[paramnum].rp[0]);
}
else if(strcmp(dummy,PT[paramnum].dp[1])==0){
fprintf(fpe, "%s",PT[paramnum].rp[1]);
}
else if(strcmp(dummy,PT[paramnum].dp[2])==0){
fprintf(fpe, "%s",PT[paramnum].rp[2]);
}else{
fprintf(fpe, "%s",dummy);
}
index2=0;
}
}paramnum++;
}
}
else if(strcmp(word,"IF")==0){ //If there is @IF
chr=fgetc(fps);chr=fgetc(fps);
int index3=0;
char name[10];char value[10];
char condition;int ok=0;
chr=fgetc(fps);
while(chr!='='){
if(chr=='>' || chr=='<')
break;
name[index3]=chr;
chr=fgetc(fps);
index3++;
}name[index3]='\0';
condition=chr;
index3=0;
chr=fgetc(fps);
while(chr!=')'){
value[index3]=chr;
chr=fgetc(fps);
index3++;
}value[index3]='\0';
index3=0;
chr=fgetc(fps);
int xx;
int currentname=-1;
for(xx=0; xx<symbolnum; xx++){
if(strcmp(name,def_symbols[xx].name)==0){
currentname=xx;
break;
}
}
if(currentname!=-1){ //Checking
if(condition=='=' && (strcmp(def_symbols[xx].value,value)==0)){
ok=1;
}
else if(condition=='<' && (strcmp(def_symbols[xx].value,value)>0)){
ok=1;
}
else if(condition=='>' && (strcmp(def_symbols[xx].value,value)<0)){
ok=1;
}
if(ok!=1){
fscanf(fps,"%s",word);
while(strcmp(word,"@ELSE")!=0){
fscanf(fps,"%s",word);
}
chr=fgetc(fps);
}
chr=fgetc(fps);chr=fgetc(fps);
}
}
else if(strcmp(word,"ELSE")==0){
fscanf(fps,"%s",word);
while(strcmp(word,"@ENDIF")!=0){
fscanf(fps,"%s",word);
}
chr=fgetc(fps);chr=fgetc(fps);
}else if(strcmp(word,"ENDIF")==0){
chr=fgetc(fps);chr=fgetc(fps);
}
}
fclose(fps);
fclose(fpe);
return 0;
}