-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.cpp
75 lines (55 loc) · 1.86 KB
/
Driver.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
#include "Driver.h"
#include <fstream>
#include <sstream>
#include "Ast2GL.h"
#include "PrettyPrinter.h"
Metal::Driver::Driver()
{
}
void Metal::Driver::convert(const std::string & filename, const std::string & prettyPrintedCompareFile)
{
if(filename.empty())
return;
std::ifstream stream(filename.c_str());
_scanner = std::shared_ptr<Scanner>(new Scanner(filename));
_parser = std::shared_ptr<Parser>(new Parser(*_scanner.get(), *this));
int parseResult = -1;
try {
parseResult = _parser->parse();
if(parseResult == 0 )
{
if(!prettyPrintedCompareFile.empty()) {
printf("\n---------BEGIN PRETTY PRINTING FILE----------\n");
PrettyPrinter printer;
// printing compare file
const std::string result = printer.print(_root);
printf("\tWriting reference compare shader: %s\n", prettyPrintedCompareFile.c_str() );
std::ofstream stream(prettyPrintedCompareFile, std::ofstream::out | std::ofstream::trunc );
stream << result;
stream.close();
printf("\n---------END PRETTY PRINTING FILE----------\n");
}
if(_root != nullptr)
{
printf("\n--------- BEGIN TRANSFORMING FILE ----------\n");
Ast2GL transpiler;
transpiler.convert(_root);
printf("\n--------- END TRANSFORMING FILE ----------\n");
}
}
}
catch(const std::bad_alloc & e) {
std::cerr << "Bad Alloc: " << e.what() << std::endl;
}
catch(const std::exception & e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
catch(...) {
std::cerr << "Unknown exception thrown while parsing file" << std::endl;
}
// make sure the parser is not holding any references to the scanner anymore
_parser = std::shared_ptr<Parser>();
// remove the scanner so it is not pointing to the stream anymore, if it is
_scanner = std::shared_ptr<Scanner>();
std::cout << "ParseResult = " << parseResult << std::endl;
}