-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.cpp
195 lines (185 loc) · 4.46 KB
/
Lexer.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
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
//
// Created by shachar Meir on 05/01/2020.
//
#include "Lexer.h"
//constructor
Lexer::Lexer(const std::string& filename) {
this->input_stream.open(filename);
if (!input_stream.is_open()) {
throw "The File could not have been opened";
}
}
//destructor
Lexer::~Lexer() {
this->input_stream.close();
}
//vector methods
void Lexer::printVector(std::vector<std::string> vector){
for(std::string s: vector){
std::cout<<s<<std::endl;
}
}
void Lexer::copyCommands( const std::vector<std::string>& vector) {
for(const std::string& s: vector){
this->commends.push_back(s);
}
}
//file methods
std::string Lexer::readLine() {
//creating input stream
std::string line;
std::getline(input_stream,line);
return line;
}
//public methods
std::vector<std::string> Lexer::lex() {
std::string line;
while (!(line = readLine()).empty()){
strip(line);
if (isOpenDataServer(line)){
handleDataServer(line);
}
else if (isConnectControlClient(line)){
handleConnectClient(line);
replace('('," ",line);
replace(')',"",line);
replace(','," ", line);
} else if(isPrint(line)){
handlePrint(line);
} else if(isSleep(line)){
handlePrint(line);
}
else if (!isAssign(line)){
replace('('," ",line);
replace(')',"",line);
replace(','," ", line);
}
makeConditionBetSpaces(line);
remove_redundant_signs(line);
std::vector<std::string> vector = split(' ', line);
if(isVar(line)){
handleVar(vector);
} else if(isCondition(line)){
handleCondition(vector);
} else if(isAssign(line)) {
handleAssign(vector);
}
//handleQuotation(vector);
this->copyCommands(vector);
}
//this->printVector(this->commends);
return this->commends;
}
//handle functions
void Lexer::handleVar(std::vector<std::string>& vector) {
std::vector<std::string> elements;
std::string expressionString;
bool is_equality = false;
int size = vector.size();
for (int kI = 0; kI < size; ++kI) {
if (!is_equality) {
if (vector[kI] != "sim") {
remove_quotation(vector[kI]);
elements.push_back(vector[kI]);
}
if (vector[kI] == "=") {
is_equality = true;
}
} else{
expressionString += vector[kI];
}
}
if (!expressionString.empty()){
elements.push_back(expressionString);
}
vector = elements;
}
void Lexer::handleCondition(std::vector<std::string>& vector) {
std::vector<std::string> newVec;
std::string expressionString1;
std::string expressionString2;
newVec.push_back(vector[0]);
int i =1;
while (!isLogicOperator(vector[i])){
expressionString1 += vector[i];
i++;
}
newVec.push_back(expressionString1);
newVec.push_back(vector[i]);
i++;
while (vector[i]!="{"){
expressionString2 += vector[i];
i++;
}
newVec.push_back(expressionString2);
newVec.push_back(vector[i]);
vector = newVec;
}
void Lexer::handleQuotation(std::vector<std::string>& vector) {
for (std::string& s: vector){
remove_quotation(s);
}
}
void Lexer::handleAssign(std::vector<std::string> &vector) {
std::vector<std::string> newVec;
std::string expressionString;
//adding the '='
newVec.push_back(vector[1]);
//adding var name
newVec.push_back(vector[0]);
//creating Expression string
for (unsigned int kI = 2; kI < vector.size() ; ++kI) {
expressionString += vector[kI];
}
newVec.push_back(expressionString);
vector = newVec;
}
void Lexer::handleDataServer(std::string &str) {
std::string result;
int i=0;
int j = str.size();
while (str.substr(i,1)!="("){
result += str.substr(i,1);
i++;
}
while (str.substr(j-1,1)!=")"){
result += str.substr(j-1,1);
j--;
}
std::string exp = str.substr(i+1, j-i-2);
remove_spaces(exp);
result = result + " " +exp;
str = result;
}
void Lexer::handleConnectClient(std::string& str) {
std::string result;
int i=0;
int j = str.size();
while (str.substr(i,1)!=","){
result += str.substr(i,1);
i++;
}
std::string exp = str.substr(i+1, j-i-2);
remove_spaces(exp);
result = result + " " +exp;
str = result;
}
void Lexer::handlePrint(std::string &str) {
std::string result;
int i=0;
int j = str.size();
while (str.substr(i,1)!="("){
result += str.substr(i,1);
i++;
}
while (str.substr(j-1,1)!=")"){
result += str.substr(j-1,1);
j--;
}
std::string exp = str.substr(i+1, j-i-2);
if (!isInQuotations(exp)) {
remove_spaces(exp);
}
result = result + " " +exp;
str = result;
}