-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
88 lines (75 loc) · 2.51 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <memory>
#include <fstream>
#include <iostream>
#include "parser/Parser.H"
#include "parser/Printer.H"
#include "parser/Absyn.H"
#include "functions_types.h"
#include "type_check.h"
#include "return_check.h"
#include "jvm_compiler.h"
int main(const int argc, const char ** argv)
{
FILE *input;
if (argc != 2) {
std::cerr << "ERROR\n";
std::cout << "Please specify exactly one file to compile.\n";
exit(1);
}
std::string out_filename = argv[1];
std::string class_name = argv[1];
auto pos = out_filename.rfind(".lat");
if((pos != std::string::npos) && (pos == out_filename.size() - 4)){
out_filename.replace(pos, 4, ".j");
class_name.replace(pos, 4, "");
}else{
std::cerr << "ERROR\n";
std::cout << "Filename must end with `.lat'.\n";
exit(1);
}
input = fopen(argv[1], "r");
if (!input) {
std::cerr << "ERROR\n";
std::cout << "Error opening input file.\n";
exit(1);
}
latte_parser::Program *parse_tree = latte_parser::pProgram(input);
if (!parse_tree) {
return 1;
}
{
/* Functions load */
latte_type_check::FunTypesLoader functions_types;
parse_tree->accept(&functions_types);
if(! functions_types.getErrors().empty()){
std::cerr << "ERROR\n";
std::cout << functions_types.getErrors().toString();
return 1;
}
/* Type check */
latte_type_check::TypeCheck type_check(functions_types.getResultEnv());
parse_tree->accept(&type_check);
if(! type_check.getErrors().empty()){
std::cerr << "ERROR\n";
std::cout << type_check.getErrors().toString();
return 1;
}
/* Return check */
latte_type_check::ReturnCheck return_check(functions_types.getResultEnv());
parse_tree->accept(&return_check);
if(! return_check.getErrors().empty()){
std::cerr << "ERROR\n";
std::cout << return_check.getErrors().toString();
return 1;
}
std::cerr << "OK\n";
std::shared_ptr<std::ostream> output(
new std::ofstream(out_filename, std::ios_base::out | std::ios_base::trunc));
/* Compilation starts here */
latte_compile::JVMCompiler jvm_compiler(class_name, output, functions_types.getResultEnv());
parse_tree->accept(&jvm_compiler);
std::cout << "Generated: " << out_filename << "\n";
}
return 0;
}