-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.cpp
More file actions
214 lines (176 loc) · 7.16 KB
/
lexer.cpp
File metadata and controls
214 lines (176 loc) · 7.16 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <regex>
#include <vector>
#include "utils.h"
#include "lexer.h"
#include "wordlist.h"
const std::vector<std::string> variable_types = {"int", "float", "double", "char", "bool", "string"};
std::vector<std::string> find_includes(std::vector<std::string> &tokens){
std::vector<std::string> includes;
for(int i = 0; i < tokens.size(); i++){
if(tokens[i] == "friend"){
includes.push_back(tokens[i+1]);
tokens[i+1] = "\"" + tokens[i+1] + ".h" + "\"";
}
}
return includes;
}
std::vector<std::string> find_classes(std::vector<std::string> &tokens){
std::vector<std::string> classes;
std::vector<std::pair<int, std::string>> indexes_to_insert;
for(int i = 0; i < tokens.size(); i++){
if(tokens[i] == "class"){
classes.push_back(tokens[i+1]);
int arguments_start = i+2;
int arguments_end = i+2;
int j =0;
while(i+j < tokens.size()){
j++;
if(tokens[i+j] == "{"){
indexes_to_insert.push_back(std::make_pair(i + j, "public: \n"));
arguments_end = i+j-1;
}
std::string arguments = "";
for(int k = arguments_start; k <= arguments_end; k++){
arguments += tokens[k] + " ";
}
indexes_to_insert.push_back(std::make_pair(i + j+1, arguments));
arguments.clear();
}
}
}
for(int i = 0; i < indexes_to_insert.size(); i++){
tokens.insert(tokens.begin() + indexes_to_insert[i].first, indexes_to_insert[i].second);
}
indexes_to_insert.clear();
return classes;
}
std::vector<std::string> find_namespaces(std::vector<std::string> &tokens){
std::vector<std::string> namespaces;
for(int i = 0; i < tokens.size(); i++){
if(tokens[i] == "namespace"){
namespaces.push_back(tokens[i+1]);
}
}
return namespaces;
}
std::vector<std::string> find_functions(std::vector<std::string> &tokens){
std::vector<std::string> functions;
for(int i = 0; i < tokens.size(); i++){
if(tokens[i] == "("){
functions.push_back(tokens[i-1]);
}
}
return functions;
}
std::vector<std::string> find_variables(std::vector<std::string> &tokens, std::vector<std::string> classes){
std::vector<std::string> variables;
for(int i = 0; i < tokens.size(); i++){
if(std::find(variable_types.begin(), variable_types.end(), tokens[i]) != variable_types.end()){
int j = 1;
while(tokens[i+j] == "*"){
j++;
}
variables.push_back(tokens[i+j]);
}
else if(std::find(classes.begin(), classes.end(), tokens[i]) != classes.end()){
int j = 1;
if(tokens[i+1] == "<"){
while(tokens[i+j] != ">"){
j++;
}
}
while(tokens[i+j] == "*"){
j++;
}
variables.push_back(tokens[i+j]);
}
}
return variables;
}
std::vector<std::string> format_strings(std::vector<std::string> &tokens){
std::vector<std::string> updated_tokens = tokens;
std::vector<int> indexes_to_remove;
std::vector<int> indexes_to_insert;
for(int i = 0; i < tokens.size(); i++){
if(is_string(tokens[i]) && tokens[i-1] == "f"){
std::string str = strip(tokens[i], '\"');
std::string formatted_str = "\"\"";
int last_index = 0;
for(int j = 0; j < str.size(); j++){
if(str[j] == '{'){
formatted_str += " + \"" + str.substr(last_index, j - last_index) +"\"";
int k = j;
while(k < str.size() && str[k] != '}'){
k++;
}
std::string code_string = str.substr(j + 1, k - j - 1);
std::vector<std::string> code_tokens = lexical_analyzer(code_string);
std::string code_str = assemble_tokens(format_strings(code_tokens));
formatted_str += " + ponystring(" + code_str + ")";
last_index = k + 1;
}
}
formatted_str = " ponystring(" + formatted_str + ")";
updated_tokens[i] = formatted_str;
indexes_to_remove.push_back(i-1);
}
}
for(int i = 0; i < indexes_to_remove.size(); i++){
updated_tokens.erase(updated_tokens.begin() + indexes_to_remove[i]);
}
tokens = updated_tokens;
return tokens;
}
std::vector<std::string> translate_tokens(std::vector<std::string> &tokens, std::vector<std::string> variables, std::vector<std::string> classes, std::vector<std::string> namespaces, std::vector<std::string> includes){
std::vector<std::string> symbols = {"+", "-", "*", "/", "=", ".", ",", ";", "(", ")", "[", "]", "{", "}", ":", "<", ">"};
for(int i = 0; i < tokens.size(); i++){
if (!is_vector_contain_string(variables, tokens[i]) && tokens[i+1] != "(" && !is_vector_contain_string(classes, tokens[i])
&& !is_vector_contain_string(namespaces, tokens[i]) && !is_vector_contain_string(includes, tokens[i])
&& !is_vector_contain_string(symbols, tokens[i]) && tokens[i] != "\n" && !isDigit(tokens[i]) && !is_string(tokens[i]) && !is_type(tokens[i]))
{
if(wordlist.find(tokens[i]) != wordlist.end()){
tokens[i] = wordlist.at(tokens[i]);
}
}
}
return tokens;
}
std::vector<std::string> lexical_analyzer(std::string str)
{
std::regex reg("<.*?>|\\b(?:int|#include|using|namespace|void|return|string|public|private|protected|byte|bool|neigh|read|class)\\b|\\w+|\".*?\"|[.,!?;:(){}\\[\\]=+\\-*/<>]|[\n]");
std::sregex_iterator begin(str.begin(), str.end(), reg);
std::sregex_iterator end;
std::vector<std::string> tokens;
for (std::sregex_iterator it = begin; it != end; ++it) {
std::smatch match = *it;
tokens.push_back(match.str());
}
return tokens;
}
std::string assemble_tokens(std::vector<std::string> tokens){
std::string assembled_code = "";
std::string last_token = "";
std::vector<std::string> symbols = {"+", "-", "*", "/", "=", ".", ",", ";", "(", ")", "[", "]", "{", "}", ":", "<", ">"};
for(std::string token : tokens){
if(token == "\n")
assembled_code += "\n";
else if (is_string(token) && last_token != "#include")
assembled_code += "ponystring(" + token + ")";
else if (assembled_code[-1] == '\n')
assembled_code += token;
else if(std::find(symbols.begin(), symbols.end(), token) != symbols.end())
assembled_code += token;
else if(isDigit(token) && last_token == ".")
assembled_code += " " + token;
else
assembled_code += " " + token;
last_token = token;
}
return assembled_code;
}
std::vector<std::string> remove_comments(const std::vector<std::string>& tokens) {
std::string code = assemble_tokens(tokens);
code = std::regex_replace(code, std::regex("//.*"), "");
code = std::regex_replace(code, std::regex("/\\*(.*?)\\*/"), "", std::regex_constants::format_first_only);
return lexical_analyzer(code);
}