This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
generated from dthain/compilerbook-starter-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bminor.c
80 lines (73 loc) · 1.6 KB
/
bminor.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "encoder.h"
#include "scanner.h"
#include "parser.h"
#include "printer.h"
#include "resolver.h"
#include "typechecker.h"
#include "codegen.h"
void usage(int exit_code)
{
printf("Usage of bminor.\n");
exit(exit_code);
}
int main(int argc, char* argv[])
{
// Parse command line arguments
char* option, * filename;
switch (argc)
{
case 1:
usage(EXIT_FAILURE);
case 2:
if (strcmp(argv[1], "--help") == 0)
usage(EXIT_SUCCESS);
else usage(EXIT_FAILURE);
case 3:
option = argv[1];
filename = argv[2];
break;
default:
fprintf(stderr, "Too many arguments.\n");
usage(EXIT_FAILURE);
}
// Open input file
FILE* fp = fopen(filename, "r");
if (fp == NULL)
{
fprintf(stderr, "Failed to open file %s\n", filename);
return EXIT_FAILURE;
}
// Perform the requested operation
if (strcmp(option, "--encode") == 0)
return decode(fp) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
else if (strcmp(option, "--scan") == 0)
return scan(fp) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
struct decl* d = parse(fp);
if (d == NULL)
{
fprintf(stderr, "Failed to parse file %s\n", filename);
return EXIT_FAILURE;
}
fclose(fp);
if (strcmp(option, "--parse") == 0)
return EXIT_SUCCESS;
else if (strcmp(option, "--print") == 0)
{
print(d);
return EXIT_SUCCESS;
}
else if (strcmp(option, "--resolve") == 0)
return resolve(d);
else if (strcmp(option, "--typecheck") == 0)
return typecheck(d);
else if (strcmp(option, "--codegen") == 0)
return codegen(d);
else
{
fprintf(stderr, "Unknown option '%s'\n", option);
usage(EXIT_FAILURE);
}
}