forked from eugenechevski/CodeGeneratorSPL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler_main.c
More file actions
executable file
·120 lines (108 loc) · 3.2 KB
/
compiler_main.c
File metadata and controls
executable file
·120 lines (108 loc) · 3.2 KB
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
/* $Id: compiler_main.c,v 1.18 2024/11/13 20:33:01 leavens Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bof.h"
#include "lexer.h"
#include "parser.h"
#include "unparser.h"
#include "ast.h"
#include "utilities.h"
#include "symtab.h"
#include "scope_check.h"
#include "gen_code.h"
/* Print a usage message on stderr
and exit with failure. */
static void usage(const char *cmdname)
{
fprintf(stderr, "Usage: %s %s\n %s %s\n %s %s\n",
cmdname, "-l codeFilename.spl",
cmdname, "-u codeFilename.spl",
cmdname, " codeFilename.spl");
exit(EXIT_FAILURE);
}
// If the -l option is used, then output the tokens
// in the give file name to stdout,
// otherwise unparse the program given in the file name argument to stdout
int main(int argc, char *argv[])
{
// should the lexer's tokens be shown?
bool lexer_print_output = false;
// should the unparse of the AST be shown?
bool parser_unparse = false;
const char *cmdname = argv[0];
argc--;
argv++;
// possible options: -l, and -u
while (argc > 0 && strlen(argv[0]) >= 2 && argv[0][0] == '-')
{
if (strcmp(argv[0], "-l") == 0)
{
lexer_print_output = true;
argc--;
argv++;
}
else if (strcmp(argv[0], "-u") == 0)
{
parser_unparse = true;
argc--;
argv++;
}
else
{
// bad option!
usage(cmdname);
}
}
// give usage message if -l and other options are used
if (lexer_print_output && parser_unparse)
{
usage(cmdname);
}
// must have a file name
if (argc <= 0 || (strlen(argv[0]) >= 2 && argv[0][0] == '-'))
{
usage(cmdname);
}
// the name of the file
char *filename = argv[0];
char *lastdot = strrchr(filename, '.');
if (lastdot == NULL || strcmp(lastdot, ".spl") != 0)
{
bail_with_error("filename argument must end in .spl not %s",
filename);
}
char boffilename[BUFSIZ];
strncpy(boffilename, filename, BUFSIZ);
int len = strlen(boffilename);
assert(len < BUFSIZ); // it has to fit!
strncpy(boffilename + (len - 4), ".bof", 5);
// debug_print("Output going to %s\n", boffilename);
if (lexer_print_output)
{
// with the lexer_print_output option, nothing else is done
lexer_init(filename);
lexer_output();
return EXIT_SUCCESS;
}
// otherwise (if not lexer_print_outout) continue to parse etc.
block_t progast = parseProgram(filename);
if (parser_unparse)
{
unparseProgram(stdout, progast);
}
// build symbol table and...
symtab_initialize();
// check for duplicate declarations
// and record id-use information in the AST
scope_check_program(&progast);
if (parser_unparse)
{
return EXIT_SUCCESS;
}
// generate code from the ASTs
gen_code_initialize();
BOFFILE bf = bof_write_open(boffilename);
gen_code_program(bf, progast);
return EXIT_SUCCESS;
}