-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextAnalyzer.cpp
More file actions
161 lines (153 loc) · 5.94 KB
/
TextAnalyzer.cpp
File metadata and controls
161 lines (153 loc) · 5.94 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
//
// Created by root on 31/12/18.
//
#include "TextAnalyzer.h"
#include <fstream>
#include <iostream>
#include <algorithm>
#include <unistd.h>
#include "DataCommands.h"
#include "Command.h"
using namespace std;
// Separating a line into words.
vector<string> TextAnalyzer::lexer(string &toSeparate) {
vector<string> separated;
addSpaces(toSeparate);
unsigned long index = findMinIndexToSeparate(toSeparate);
string sub;
while (index != string::npos) {
sub = toSeparate.substr(0, index);
if ((sub.empty()) || (sub == "\f") || (sub == "\n") || (sub == "\r") || (sub == "\t") || (sub == "\v")
|| (sub == ",")) {
toSeparate.erase(0, index + 1);
index = findMinIndexToSeparate(toSeparate);
continue;
}
separated.push_back(sub);
toSeparate.erase(0, index + 1);
//there is "=" and no "bind" - complex expression - break, then take the remains of the line as a whole
if (sub == "=") {
if (toSeparate.find("bind") == string::npos) {
break;
}
} else if (sub == "print" || sub == "sleep") {
//complex expression - break, then take the remains of the line as a whole
break;
} else if (sub == "if" || sub == "while") {
//cut upto the operator
short opLen;
index = findOperator(toSeparate, opLen);
sub = toSeparate.substr(0, index);
toSeparate.erase(0, index);
separated.push_back(sub);
//cut operator
separated.push_back(toSeparate.substr(0, static_cast<unsigned long>(opLen)));
toSeparate.erase(0, static_cast<unsigned long>(opLen));
//cut from the operator - break, then take the remains of the line as a whole
break;
} else if (sub == "connect") {
//cut IP
index = findMinIndexToSeparate(toSeparate);
sub = toSeparate.substr(0, index);
toSeparate.erase(0, index);
separated.push_back(sub);
break;
} else if (sub == "openDataServer") {
index = 0;
bool wasNum = false;
while (!wasNum) {
while (isspace(toSeparate[index])) {
++index;
}
while (isdigit(toSeparate[index]) || (((toSeparate[index] >= 'a') && (toSeparate[index] <= 'z')) ||
((toSeparate[index] >= 'A') && (toSeparate[index] <= 'Z')))) {
wasNum = true;
++index;
}
while (isspace(toSeparate[index])) {
++index;
}
if (toSeparate[index] == '+' || toSeparate[index] == '-' || toSeparate[index] == '*' ||
toSeparate[index] == '/') {//operator
wasNum = false;
}
++index;
}
//there was a num without an operator
--index;
sub = toSeparate.substr(0, index);
toSeparate.erase(0, index);
separated.push_back(sub);
break;
}
index = findMinIndexToSeparate(toSeparate);
}
index = min(toSeparate.find('{'), toSeparate.find('}')); //search for '{' or '}'
if (index != string::npos) { // '{' or '}' was found
//separate upto '{' or '}'
sub = toSeparate.substr(0, index);
toSeparate.erase(0, index);
separated.push_back(sub);
}
if (!((toSeparate.empty()) || (toSeparate == "\f") || (toSeparate == "\n") || (toSeparate == "\r") ||
(toSeparate == "\t") || (toSeparate == "\v") || (toSeparate == ","))) {
separated.push_back(toSeparate);
}
return separated;
}
void TextAnalyzer::parse(vector<string> &separated, bool &shouldStop) {
DataCommands *dataCommands;
dataCommands = new DataCommands(separated);
unordered_map<string, Command *> stringsToCommands = dataCommands->getStringsToCommands();
auto it1 = separated.begin();
Command *command;
while (it1 != separated.end()) {
auto it2 = stringsToCommands.find(*it1);
if (it2 == stringsToCommands.end()) {
it1++;
continue;
}
command = it2->second;
command->doCommand();
if ((*it1 == "while") || (*it1 == "if")){
it1 = separated.begin();
it1 += dataCommands->getIndex();
continue;
}
it1++;
}
}
unsigned long TextAnalyzer::findMinIndexToSeparate(const string &str) {
vector<unsigned long> tmpVec = {str.find(' '), str.find('\f'), str.find('\n'), str.find('\r'),
str.find('\t'), str.find('\v'), str.find(',')};
auto it = min_element(tmpVec.begin(), tmpVec.end());
unsigned long index = *(it);
return index;
}
unsigned long TextAnalyzer::findOperator(string &toSeparate, short &opLen) {
opLen = 2;
vector<unsigned long> tmpVec1 = {toSeparate.find("=="), toSeparate.find("!="), toSeparate.find(">="), toSeparate
.find("<="), toSeparate.find("!=")};
auto it1 = min_element(tmpVec1.begin(), tmpVec1.end());
unsigned long index = *(it1);
if (index == string::npos) {// ==,!=,>=,<=,!= wasn't found - search =,<,>
vector<unsigned long> tmpVec2 = {toSeparate.find('='), toSeparate.find('<'), toSeparate.find('>'), toSeparate
.find('{'), toSeparate.find('}'), toSeparate.find('('), toSeparate.find(')')};
auto it2 = min_element(tmpVec2.begin(), tmpVec2.end());
index = *(it2);
opLen = 1;
}
return index;
}
void TextAnalyzer::addSpaces(string &toSeparate) {
short opLen;
unsigned long index = findOperator(toSeparate, opLen);
if (index != string::npos) {
toSeparate.insert(index, " ");
if (opLen == 2) {
toSeparate.insert(index + 3, " ");
} else {
toSeparate.insert(index + 2, " ");
}
}
}