-
Notifications
You must be signed in to change notification settings - Fork 0
/
coop.c
367 lines (355 loc) · 8.64 KB
/
coop.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
/*
* Author: Michael Henry Jennings
* v0.0 (skeleton code and specifications): 1/2/2024
* v0.1 (expanded specifications): 1/3/2024
* v0.2 (some features working): 1/7/2024
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define CLASS "class"
#define STRUCT "struct"
#define THIS "this"
char* read(FILE *file) {
// read and return contents of a file
unsigned int length = 0;
unsigned int allocated = 16;
char* start = malloc(allocated * sizeof(char));
char* current = start;
while ((*(current++) = fgetc(file)) != EOF) {
if (++length == allocated) {
start = realloc(start, (allocated *= 2) * sizeof(char));
current = start + length;
}
}
start = realloc(start, length * sizeof(char));
*(start + length) = '\0';
return start;
}
int letter(char character) {
return isalpha(character) || character == '_';
}
char* read_word(char *text) {
char *start = text;
int len = 1;
while (letter(*(++text))) {
len++;
}
char temp = *text;
*text = '\0';
char *word = malloc(len * sizeof(char));
strcpy(word, start);
*text = temp;
return word;
}
int count_instance_variables(char *text) {
int count = 0;
while (*text != ')') {
if (*text == ',') {
count++;
}
text++;
}
return ++count;
}
int function(char *text) {
while (letter(*text)) {
text++;
}
while (isspace(*text)) {
text++;
}
return *text == '(';
}
int function_declaration(char *text) {
while (letter(*text)) {
text++;
}
while (isspace(*text)) {
text++;
}
if (*(text++) != '(') {
return 0;
}
while (*(text++) != ')') {
text++;
}
while (isspace(*text)) {
text++;
}
if (*text != '{') {
return 0;
}
return 1;
}
char *get_extension(char* filename) {
// find last character, storing last period
char *start = NULL;
while (*filename != '\0') {
if (*(filename++) == '.') {
start = filename;
}
}
// return pointer to start of extension
if (start == NULL) {
return NULL;
}
return start;
}
int matches(char* text, char* pattern) {
while(*pattern != '\0') {
if (*(pattern++) != *(text++)) {
return 0;
}
}
return isspace(*text);
}
char* process_chain(char *text, FILE *file, char** instance_variables, int num_instance_variables) {
char *start = text;
char *vacancy = text;
int was_dot = 0;
while (letter(*text) || *text == '.' || *text == '(') {
if (*text == '.') {
was_dot = 1;
}
if (*(text++) == '(') {
if (was_dot) {
vacancy = text;
}
int counter = 1;
while (counter > 0) {
if (*text == '(') {
counter++;
}
if (*(text++) == ')') {
counter--;
}
}
}
}
char* function = vacancy;
while (*function != '.' && function >= start) {
function--;
}
char temp = *vacancy;
*vacancy = '\0';
fputs(++function, file);
*vacancy = temp;
if (function != start) {
temp = *(--function);
*function = '\0';
process_chain(start, file, instance_variables, num_instance_variables);
*function = temp;
} else {
char *word = read_word(start);
for (int j = 0; j < num_instance_variables; j++) {
if (!strcmp(word, instance_variables[j])) {
fputs(THIS, file);
fputc('.', file);
}
}
free(word);
}
while (isspace(*vacancy)) {
fputc(*(vacancy++), file);
}
if (*vacancy != ')' && vacancy != start) {
fputs(", ", file);
}
while (vacancy < text) {
if (*vacancy == '(') {
while (*vacancy != ')') {
if (letter(*vacancy)) {
vacancy = process_chain(vacancy, file, instance_variables, num_instance_variables);
} else {
fputc(*(vacancy++), file);
}
}
} else {
fputc(*(vacancy++), file);
}
}
return text;
}
int main(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
if (!strcmp(get_extension(argv[i]), "coop")) { //.coop file
// open .coop file for reading
FILE *coopfile;
if ((coopfile = fopen(argv[i], "r")) == NULL) {
fprintf(stderr, "Error: .coop file cannot be read from.");
return -1;
}
// open corresponding .c file for writing
FILE *cfile;
char* end_c_file = argv[i];
while (*(++end_c_file + 3) != '\0') {}
*end_c_file = '\0';
if ((cfile = fopen(argv[i], "w")) == NULL) {
fprintf(stderr, "Error: corresponding .c file cannot be written to.");
return -2;
}
// setup
int classes_allocated = 16;
char **classes = malloc(sizeof(char*) * classes_allocated);
int num_classes = 0;
char *classname = NULL;
char **instance_variables = malloc(sizeof(char*));
int num_instance_variables = 0;
int brace_level = 0;
int in_constructor = 0;
char* cooptext = read(coopfile);
// scan entire file for coop syntax
while (*cooptext != '\0') {
if (letter(*cooptext)) {
char *next_word = read_word(cooptext);
if (!strcmp(next_word, CLASS)) {
// found new class definition
cooptext += strlen(CLASS);
while (!letter(*cooptext)) {
cooptext++;
}
// process class name
classname = read_word(cooptext);
if (classes_allocated == num_classes) {
classes = realloc(*classes, classes_allocated *= 2);
}
classes[num_classes] = classname;
num_classes++;
while (letter(*cooptext)) {
cooptext++;
}
while (*(cooptext++) != '(') {}
// process instance variables; define struct
fputs(STRUCT, cfile);
fputc(' ', cfile);
fputs(classname, cfile);
fputs(" {\n", cfile);
num_instance_variables = count_instance_variables(cooptext);
for (int j = 0; j < num_instance_variables; j++) {
fputc('\t', cfile);
while (!letter(*cooptext)) {
cooptext++;
}
while (letter(*cooptext)) {
fputc(*cooptext, cfile);
cooptext++;
}
while (!letter(*cooptext)) {
fputc(*cooptext, cfile);
cooptext++;
}
instance_variables[j] = read_word(cooptext);
while (letter(*cooptext)) {
fputc(*cooptext, cfile);
cooptext++;
}
fputs(";\n", cfile);
}
fputs("};\n\n", cfile);
while (*(cooptext++) != '{') {}
while (isspace(*cooptext)) {
cooptext++;
}
brace_level++;
} else {
int match = 0;
if (!function(cooptext)) {
fflush(stderr);
for (int i = 0; i < num_classes; i++) {
if (!strcmp(next_word, classes[i])) {
fputs(STRUCT, cfile);
fputc(' ', cfile);
match++;
break;
}
}
}
if (brace_level && !match) {
if (function_declaration(cooptext) && brace_level == 1) {
if (strcmp(next_word, classname)) {
// handle instance method
fputs(next_word, cfile);
cooptext += strlen(next_word);
while (*cooptext != '(') {
fputc(*(cooptext++), cfile);
}
fputc(*(cooptext++), cfile);
fputs(STRUCT, cfile);
fputc(' ', cfile);
fputs(classname, cfile);
fputc(' ', cfile);
fputs(THIS, cfile);
while (isspace(*cooptext)) {
fputc(*(cooptext++), cfile);
}
if (*cooptext != ')') {
fputs(", ", cfile);
}
while (*cooptext != '{') {
fputc(*(cooptext++), cfile);
}
} else {
// handle constructor
fputs(STRUCT, cfile);
fputc(' ', cfile);
fputs(classname, cfile);
fputc(' ', cfile);
while (*cooptext != '{') {
fputc(*(cooptext++), cfile);
}
fputc(*(cooptext++), cfile);
brace_level++;
fputc('\n', cfile);
fputs(STRUCT, cfile);
fputc(' ', cfile);
fputs(classname, cfile);
fputc(' ', cfile);
fputs(THIS, cfile);
fputc(';', cfile);
in_constructor = 1;
}
} else {
cooptext = process_chain(cooptext, cfile, instance_variables, num_instance_variables);
}
} else {
cooptext = process_chain(cooptext, cfile, instance_variables, num_instance_variables);
}
}
free(next_word);
} else {
if (brace_level) {
if (*cooptext == '{') {
brace_level++;
} else if (*cooptext == '}') {
if (--brace_level == 0) {
for (int j = 0; j < num_instance_variables; j++) {
free(instance_variables[j]);
}
free(instance_variables);
num_instance_variables = 0;
classname = NULL;
cooptext++;
continue;
}
if (brace_level == 1 && in_constructor) {
fputs("return ", cfile);
fputs(THIS, cfile);
fputs(";\n", cfile);
in_constructor = 0;
}
}
}
fputc(*(cooptext++), cfile);
}
}
fclose(coopfile);
fclose(cfile);
// call subroutines to alter the file's text
// create and write to .c file of same prefix
}
// ignore other file types
}
return 0;
}