-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
67 lines (53 loc) · 1.4 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
#include "defs.h"
#define extern_
#include "data.h"
#undef extern_
#include "decl.h"
#include <unistd.h>
static void init() {
Line = 1;
Putback = '\n';
}
char *alter_suffix(char *str, char suffix) {
char *posn;
char *newstr;
if ((newstr = strdup(str)) == NULL)
return (NULL);
if ((posn = strrchr(newstr, '.')) == NULL)
return (NULL);
posn++;
if (*posn == '\0')
return (NULL);
*posn++ = suffix;
*posn = '\0';
return (newstr);
}
static char *do_compile(char *filename) {
Infilename = filename;
Outfilename = alter_suffix(filename, 's');
if (Outfilename == NULL) {
fprintf(stderr, "[Error]: %s has no suffix, try .c on the end\n", filename);
exit(1);
}
if ((Infile = fopen(Infilename, "r")) == NULL) {
fprintf(stderr, "[Error]: unable to open %s: %s\n", Infilename, strerror(errno));
exit(1);
}
if ((Outfile = fopen(Outfilename, "w")) == NULL) {
fprintf(stderr, "[Error]: unable to create %s: %s\n", Outfilename, strerror(errno));
exit(1);
}
clear_symtable();
init();
scan(&Token);
genpreamble();
global_declarations();
genpostamble();
fclose(Outfile);
freestaticsyms();
return (Outfilename);
}
int main(int argc, char* argv[]) {
do_compile(argv[1]);
return (0);
}