-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (59 loc) · 2.04 KB
/
main.cpp
File metadata and controls
66 lines (59 loc) · 2.04 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
#include "antlr4-runtime.h"
#include "CProgCSTVisitor.h"
#include "CProgLexer.h"
#include "CProgParser.h"
#include "Options.h"
#include "Writer.h"
#include "IR.h"
#include "CProgAST.h"
#include <istream>
#include <iostream>
#include <string>
using namespace std;
using namespace antlr4;
int main(int argc, char **argv)
{
Options options;
if (!options.parseOptions(argc, argv))
{
cout << "usage : " << argv[0] << " [options] <input_file>" << endl
<< "[options] : -o <output_file> | -O | -a | --help" << endl;
return 1;
}
if (options.help)
{
cout << argv[0] << " [options] <input_file>" << endl
<< "[options] : -o <output_file> | -O | -a | --help" << endl << endl
<< "-o <output_file> : définit le nom du fichier de sortie" << endl
<< "-O : optimise la représentation en mémoire du programme" << endl
<< "-a : s'arrête avant la génération du fichier assembleur" << endl
<< "--help : affiche l'utilisation du programme" << endl << endl
<< "Comportement par défaut :" << endl
<< argv[0] << " <input_file>" << endl
<< "analyse le fichier d'entrée, renvoie toutes les erreurs trouvées sur la sortie" << endl
<< "standard et génère un fichier assembleur nomme brutus.s s'il ne trouve pas d'erreur" << endl;
return 0;
}
ifstream file(options.input_file);
if (!file)
{
Writer::error() << "cannot open " << options.input_file << std::endl;
return 1;
}
ANTLRInputStream input(file);
CProgLexer lexer(&input);
CommonTokenStream tokens(&lexer);
CProgParser parser(&tokens);
tree::ParseTree *tree = parser.program();
Writer writer(options);
CProgCSTVisitor visitor;
CProgASTProgram *ast = visitor.visit(tree).as<CProgASTProgram*>();
if(!ast)
return 1;
IR ir(writer, options.input_file);
ast->build_ir(ir);
// ir.print_debug_infos();
if(!writer.error_occurred && options.generate_assembly)
ir.gen_asm();
return writer.error_occurred;
}