-
Notifications
You must be signed in to change notification settings - Fork 1
/
kitty.c
139 lines (128 loc) · 3.69 KB
/
kitty.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <stdio.h>
#include <string.h>
#include "parserscanner/y.tab.h"
#include "parserscanner/kittytree.h"
#include "typechecker/kittyprinter.h"
#include "typechecker/kittyweed.h"
#include "typechecker/kittycollect.h"
#include "typechecker/kittymulticollect.h"
#include "typechecker/kittycheck.h"
#include "kittyir.h"
#include "kittyemit.h"
#include "peephole.h"
#ifndef SUCCESS_AND_FAILURE
#define SUCCESS_AND_FAILURE
#define COMPILATION_SUCCESS 0
#define COMPILATION_FAILURE 1
#endif
#ifndef MAIN_ARGUMENTS
#define MAIN_ARGUMENTS
#define INVALID_ARGUMENTS -1
#define NO_ARGUMENTS 0
#define PEEPHOLE_STATISTICS 1
#define PRETTY_PRINTER 2
#define NO_OPTIMIZATION 3
#define RUNTIME_CHECKS 4
#endif
#ifndef PARSE_MSGS
#define PARSE_MSGS
#define PARSE_SUCCESS 0
#define PARSE_ERROR 1
#define PARSE_OUT_OF_MEM 2
#endif
int main_argument_decider(int argc, char **argStrings) {
if ( argc == 2 ) {
if (strcmp(argStrings[1], "--print") == 0 ||
strcmp(argStrings[1], "-p") == 0) {
return PRETTY_PRINTER;
} else if (strcmp(argStrings[1], "--histogram") == 0 ||
strcmp(argStrings[1], "-h") == 0) {
return PEEPHOLE_STATISTICS;
} else if (strcmp(argStrings[1], "--nooptimization") == 0 ||
strcmp(argStrings[1],"-n") == 0){
return NO_OPTIMIZATION;
} else if ( strcmp(argStrings[1], "-rtc") == 0 ||
strcmp(argStrings[1],"--runtimechecks") == 0) {
return RUNTIME_CHECKS;
}
} else if ( argc == 1 ) {
return NO_ARGUMENTS;
}
return INVALID_ARGUMENTS;
}
int lineno = 1;
BODY *_main_; // root of the AST
linked_list *ir_lines; // list of the IR lines of code
stackT *functionStack; //Stack for functions
stackT *loopStack; //Stack for keeping track of loops
//used with break/continue
int main ( int argc, char *argv[] ) {
if ( argc > 2 ) {
fprintf(stderr, "Too many flags parsed to compiler.\n"
"\nCorrect usage:\n\t./kitty \"flag\" "
"< \"input_file\" > \"output_file\" \n\nValid "
"flags are: \"--print\",\"-p\" OR \"--histogram\""
",\"-h\" OR \"--nooptimization\",\"-n\" "
"OR \"--runtimechecks\",\"-rtc\" \n");
exit(1);
}
fprintf(stderr, "%s\n", "Initializing parsing phase");
switch ( yyparse() ){
case PARSE_ERROR:
fprintf(stderr, "Error: Parse error detected\n");
return COMPILATION_FAILURE;
case PARSE_OUT_OF_MEM:
fprintf(stderr, "Error: Parse out of memory\n");
return COMPILATION_FAILURE;
case PARSE_SUCCESS:
functionStack = stackInit();
loopStack = stackInit();
ir_lines = initialize_list();
begin_collect(_main_);
begin_multi_collect(_main_);
begin_check(_main_);
begin_weed(_main_);
switch ( main_argument_decider(argc, argv) ) {
case PRETTY_PRINTER:
printer_body(_main_);
break;
case PEEPHOLE_STATISTICS:
IR_build(_main_, 0);
begin_peephole( 1 );
IR_printer(ir_lines);
break;
case NO_OPTIMIZATION:
IR_build(_main_, 0);
IR_printer(ir_lines);
break;
case NO_ARGUMENTS:
IR_build(_main_, 0);
begin_peephole( 0 );
IR_printer(ir_lines);
break;
case RUNTIME_CHECKS:
IR_build(_main_, 1);
begin_peephole( 0 );
IR_printer(ir_lines);
break;
case INVALID_ARGUMENTS:
fprintf(stderr, "Invalid flags parsed to compiler.\n"
"\nCorrect usage:\n\t./kitty \"flag\" "
"< \"input_file\" > \"output_file\" \n\nValid "
"flags are: \"--print\",\"-p\" OR \"--histogram\""
",\"-h\" OR \"--nooptimization\",\"-n\" "
"OR \"--runtimechecks\",\"-rtc\" \n");
break;
default:
break;
}
stackDestroy(functionStack);
stackDestroy(loopStack);
terminate_list(&ir_lines);
break;
default:
fprintf(stderr, "Error: Fatal error in parsing \n");
return COMPILATION_FAILURE;
}
return COMPILATION_SUCCESS;
}