forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
53 lines (45 loc) · 1.33 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
#include "defs.h"
#define extern_
#include "data.h"
#undef extern_
#include "decl.h"
#include <errno.h>
// Compiler setup and top-level execution
// Copyright (c) 2019 Warren Toomey, GPL3
// Initialise global variables
static void init() {
Line = 1;
Putback = '\n';
Globs = 0;
}
// Print out a usage if started incorrectly
static void usage(char *prog) {
fprintf(stderr, "Usage: %s infile\n", prog);
exit(1);
}
// Main program: check arguments and print a usage
// if we don't have an argument. Open up the input
// file and call scanfile() to scan the tokens in it.
int main(int argc, char *argv[]) {
if (argc != 2)
usage(argv[0]);
init();
// Open up the input file
if ((Infile = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno));
exit(1);
}
// Create the output file
if ((Outfile = fopen("out.s", "w")) == NULL) {
fprintf(stderr, "Unable to create out.s: %s\n", strerror(errno));
exit(1);
}
// For now, ensure that void printint() is defined
addglob("printint", P_CHAR, S_FUNCTION, 0);
scan(&Token); // Get the first token from the input
genpreamble(); // Output the preamble
global_declarations(); // Parse the global declarations
genpostamble(); // Output the postamble
fclose(Outfile); // Close the output file and exit
return (0);
}