Repository navigation
Expand file tree
/
Copy pathMain.cpp
More file actions
111 lines (86 loc) · 2.24 KB
/
Copy pathMain.cpp
File metadata and controls
111 lines (86 loc) · 2.24 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
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
#include <iostream>
#include <string>
#include <vector>
#include "Token.h"
#include "Lexer.h"
#include "Parser.h"
int main(int argc, char **argv)
{
if (argc > 3 || argc < 2)
{
std::cerr << "Path to file not given...\n";
return 1;
}
bool debug {false};
if (argc == 3)
{
std::string d {argv[2]};
if (d == "-d")
debug = true;
else
{
std::cerr << "Unknown 3rd argument (" << d << ")...\n";
return 1;
}
}
const char *path {argv[1]};
std::ifstream file {path};
const std::string contents
{
(std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>()
};
if (debug)
std::cout << "FILE:\n{\n\n" << contents << "\n\n}\n";
Lexer *lexer {new Lexer {}};
const std::vector<Tok> lexedTokens {};
try
{
std::vector<Tok> *wp {const_cast <std::vector<Tok> *> (&lexedTokens)};
*wp = lexer->Parse(contents.c_str(), debug);
}
catch (const std::runtime_error &e)
{
std::cerr << "Lexer error occurred:\n\t" << e.what() << "\n";
}
delete lexer;
if (debug)
std::cout << "\n" << lexedTokens.size() << " lexedTokens found:\n{\n\n";
if (debug)
{
for (size_t i {0}; i < lexedTokens.size(); ++i)
{
if (lexedTokens[i].type == EOL)
{
std::cout << "\n\tNEWLINE\n\n";
continue;
}
std::cout
<< "\t"
<< TokenTypeStrings[lexedTokens[i].type]
<< ": \""
<< lexedTokens[i].text
<< "\", ("
<< lexedTokens[i].lineNumber
<< ", "
<< lexedTokens[i].charIndex
<< ");\n";
}
std::cout << "\n}\n";
}
Parser *parser {new Parser {debug}};
if (debug)
std::cout << "Output: \n{\n\n";
try
{
parser->Parse(lexedTokens);
}
catch (const std::runtime_error &e)
{
std::cerr << "Parser error occurred: \n\t" << e.what() << "\n";
}
delete parser;
if (debug)
std::cout << "\n\n}\n";
return 0;
}