-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
176 lines (149 loc) · 5.26 KB
/
Copy pathmain.cpp
File metadata and controls
176 lines (149 loc) · 5.26 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// #define _GLIBCXX_DEBUG
#ifdef __linux__
#define USE_COLORS
#endif
// #define __OUT_TERMINALS
// #define __DEBUG
#include <iostream>
#include <deque>
#include <ctime>
#include "lexer/yaplex.h"
#include "parser/yapser.h"
#include "interpreter/yapliter.h"
int main(int argc, char* argv[])
{
if ( argv[1] == nullptr
|| strlen(argv[1]) <= 5
|| ( strlen(argv[1]) >= 6
&& argv[1][strlen(argv[1]) - 1] != 'l'
&& argv[1][strlen(argv[1]) - 2] != 'p'
&& argv[1][strlen(argv[1]) - 3] != 'a'
&& argv[1][strlen(argv[1]) - 4] != 'y'
&& argv[1][strlen(argv[1]) - 5] != '.'
)
)
{
std::cout << "<< Usage: yapl [source].yapl >>\n";
return 1;
}
std::vector< std::shared_ptr<Token> > tokens;
float __start_tokenize = clock();
if (tokenize(static_cast<std::string>(argv[1]), tokens))
{
return 1;
}
float __end_tokenize = clock();
float __tokenize_time = 1000 * (__end_tokenize - __start_tokenize) / CLOCKS_PER_SEC;
#ifdef __DEBUG
std::cout << "|---------------------------------------------------------------------------------------------------------|\n";
std::cout << std::right << std::setw(40) << "TOKENS:\n";
std::cout << "|---------------------------------------------------------------------------------------------------------|\n\n";
for ( const auto& token : tokens )
{
std::cout << "\t";
std::cout << std::left << std::setw(30) << "< " + token->get_type() + " >";
std::cout << std::left << std::setw(30) << "< " + token->get_value() + " >";
std::cout << std::left << std::setw(30) << "< " + std::to_string(token->get_line()) + " >";
std::cout << std::left << std::setw(30) << "< " + std::to_string(token->get_pos()) + " >";
std::cout << std::endl;
std::cout << std::endl;
}
#endif
std::cout << "|---------------------------------------------------------------------------------------------------------|\n";
std::cout << std::right << std::setw(40) << "TOTAL: " << tokens.size() << "\t";
std::cout << "TOKENIZE TIME: " << __tokenize_time << " ms.\n";
std::cout << "|---------------------------------------------------------------------------------------------------------|\n\n\n";
Parser parser(tokens, static_cast<std::string>(argv[1]));
std::vector<__function> __function_table;
std::vector<__pack> __pack_table;
std::string entry_func_name;
std::string exit_func_name;
__start_tokenize = clock();
bool parse_success = parser.parse(__function_table, __pack_table, entry_func_name, exit_func_name);
__end_tokenize = clock();
float __parser_time = 1000 * (__end_tokenize - __start_tokenize) / CLOCKS_PER_SEC;
if ( parse_success )
{
std::cout << "\n\n";
std::cout << "|---------------------------------------------------------------------------------------------------------|\n";
#ifdef USE_COLORS
std::cout << "\x1b[32;40m" << std::setw(40) << "CODE IS CORRECT!"
<< std::setw(20) << "PARSE TIME: " << __parser_time << " ms."
<< std::setw(42) << "\x1b[0m\n";
#else
std::cout << std::setw(40) << "CODE IS CORRECT!"
<< std::setw(20) << "PARSE TIME: " << __parser_time << " ms."
<< std::setw(42) << "\n";
#endif
std::cout << "|---------------------------------------------------------------------------------------------------------|\n";
#ifdef USE_COLORS
std::cout << "\n\n\x1b[32;40m" << std::setw(10) << "Starting program..." << "\x1b[0m";
std::cout << "\n\x1b[32;40m" << std::setw(10) << "^^^^^^^^^^^^^^^^^^^" << "\x1b[0m\n\n";
#else
std::cout << "\n\n" << std::setw(10) << "Starting program...";
std::cout << "\n" << std::setw(10) << "^^^^^^^^^^^^^^^^^^^" << "\n\n";
#endif
for (const auto &ent: __function_table)
{
if (ent.__NAME == entry_func_name)
{
__start_tokenize = clock();
execute_rpn(ent.__RPN, __function_table, __pack_table, entry_func_name, {});
__end_tokenize = clock();
float __execution_time = 1000 * (__end_tokenize - __start_tokenize) / CLOCKS_PER_SEC;
for (const auto &exxt : __function_table)
{
if (exxt.__NAME == exit_func_name)
{
execute_rpn(exxt.__RPN, __function_table, __pack_table, exit_func_name, {});
break;
}
}
#ifdef USE_COLORS
std::cout << "\n\n\x1b[32;40m"
<< "Program executing time (without end() function) -> "
<< __execution_time
<< " ms."
<< "\x1b[0m\n";
#else
std::cout << "\n\n"
<< "Program executing time (without end() function) -> "
<< __execution_time
<< " ms."
<< "\n";
#endif
break;
}
}
}
else
{
std::cout << "\n\n";
std::cout << "|---------------------------------------------------------------------------------------------------------|\n";
#ifdef USE_COLORS
std::cout << std::setw(40) << "\x1b[31;1mFAIL!\x1b[0m\n";
#else
std::cout << std::setw(40) << "FAIL!\n";
#endif
std::cout << "|---------------------------------------------------------------------------------------------------------|\n";
}
#ifdef __OUT_TERMINALS
std::ofstream out;
out.open("tokens_1.txt");
if ( !out.is_open() )
{
#ifdef USE_COLORS
std::cout << "\x1b[31;1mError writing lexer terminals...\x1b[0m\n";
#else
std::cout << "Error writing lexer terminals...\n";
#endif
return 1;
}
for (auto i : tokens)
{
out << (*i).get_value() << std::endl;
}
out.close();
#endif
return 0;
}