-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
66 lines (46 loc) · 1.49 KB
/
main.cpp
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
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "error.hpp"
#include "lex.hpp"
#include "parse.hpp"
#include "semantics.hpp"
#include "codegen.hpp"
int main( int argc, char *argv[] ) {
if ( argc != 2 ) {
fprintf( stderr, "Usage: %s <filename>\n\n", argv[0] );
return EXIT_FAILURE;
}
// open file argument and give the FILE * to the lexer
{
// NOTE: if I need to close this instead of letting OS take care of it, I'll have to move this declaration
// to a scope where I can close it after all lexing is done. For now, OS can clean up at the end.
extern FILE *fp; // in `lex.cpp` - only lexer cares abt it after this
if ( !( fp = fopen( argv[1], "r" ) ) ) {
fprintf( stderr, "Could not open file '%s': %s\n", argv[1], strerror( errno ) );
return EXIT_FAILURE;
}
}
#if MILESTONE == 1
// print each token except EOF
for ( Tokinfo t = lex(); t.token != TOKEN_EOF; t = lex() )
printf( "%s\t[%s] @ line %d\n", token_to_string( t.token ), t.lexeme.data(), t.linenum );
#elif MILESTONE == 2
auto root = parse();
printast( root );
puts( "" );
#elif MILESTONE == 3
auto root = parse();
checksemantics( root );
printast( root );
puts( "" );
#elif MILESTONE == 4
auto root = parse();
checksemantics( root );
gen_code( root );
#else
fprintf( stderr, "Invalid milestone number" );
return EXIT_FAILURE;
#endif
return EXIT_SUCCESS;
}