-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
96 lines (77 loc) · 1.96 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "includes.h"
#include "symbols.h"
extern int yydebug;
extern int yyparse();
extern FILE *yyin;
int main(int argc, char *argv[]) {
/* initialise the symbol table */
symbolTable_init(&s);
if (argc == 2) {
/* open source file */
FILE *source = fopen(argv[1], "r");
/* if source file cannot be opened */
if (!source) {
printf("Feedback: Could not open source file\n");
} else {
/* write to the console */
output = stdout;
/* invoke compiler */
yyin = source;
yyparse();
fclose(source);
}
} else if (argc == 3 && strcmp(argv[2], "-debug") != 0) {
/* open source file */
FILE *source = fopen(argv[1], "r");
/* if source file cannot be opened */
if (!source) {
printf("Feedback: Could not open source file\n");
} else {
/* set output file */
char fileName[sizeof(argv[2]) + 5];
strcpy(fileName, argv[2]);
strcat(fileName, ".cpp");
/* write to output file */
output = fopen(fileName, "w");
if (!output) {
fprintf(stderr, "Feedback: Could not write to target file\n");
exit(1);
}
/* invoke compiler */
yyin = source;
yyparse();
fclose(source);
}
} else if (argc == 4 && strcmp(argv[3], "-debug") == 0) {
/* enable compiler debug mode */
yydebug = 1;
/* open source file */
FILE *source = fopen(argv[1], "r");
/* if source file cannot be opened */
if (!source) {
printf("Feedback: Could not open source file\n");
} else {
/* set output file */
char fileName[sizeof(argv[2]) + 5];
strcpy(fileName, argv[2]);
strcat(fileName, ".cpp");
/* write to output file */
output = fopen(fileName, "w");
if (!output) {
fprintf(stderr, "Feedback: Could not write to target file\n");
exit(1);
}
/* invoke compiler */
yyin = source;
yyparse();
fclose(source);
}
} else {
/* print compiler usage instruction */
printf("\nUSAGE: ./feedbc <source> <output> \n");
}
return 0;
}