-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystem.cpp
More file actions
173 lines (166 loc) · 6.16 KB
/
System.cpp
File metadata and controls
173 lines (166 loc) · 6.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
#include "System.h"
void System::operate() {
const string openRegExpr = R"(^open \"((?:[^\"\s]|[ ])+)|(\"([^\"]+)\")\"$)";
const string saveRegExpr = R"(^save ([1-9]\d*|0) \"((?:[^\"\s]|[ ])+)|(\"([^\"]+)\")\"$)";
const string quitRegExpr = R"(^quit$)";
const string readRegExpr = R"(^read$)";
const string printRegExpr = R"(^print ([1-9]\d*|0)$)";
const string startRegExpr = R"(^start ([1-9]\d*|0)$)";
const string startForRegExpr = R"(^startFor ([1-9]\d*|0) \"([\w !@#$%^&*()+-_]*)\"$)";
const string composeRegExpr = R"(^compose ([1-9]\d*|0) ([1-9]\d*|0)$)";
const string whileRegExpr = R"(^while ([1-9]\d*|0) ([1-9]\d*|0)$)";
const string ifRegExpr = R"(^if ([1-9]\d*|0) ([1-9]\d*|0) ([1-9]\d*|0)$)";
const regex openPattern = regex(openRegExpr);
const regex savePattern = regex(saveRegExpr);
const regex readPattern = regex(readRegExpr);
const regex quitPattern = regex(quitRegExpr);
const regex printPattern = regex(printRegExpr);
const regex composePattern = regex(composeRegExpr);
const regex whilePattern = regex(whileRegExpr);
const regex ifPattern = regex(ifRegExpr);
const regex startPattern = regex(startRegExpr);
const regex startForPattern = regex(startForRegExpr);
cout << BRIGHT_BLUE_TEXT << INDENT << "- Welcome to TuringMachinizator -" << endl << RESET_COLORING;
sregex_iterator iter;
const sregex_iterator end; //end-of-sequence iterator used to determine whether matches were found
string command;
while (true) {
cout << '>';
getline(cin, command);
if ((iter = sregex_iterator(command.begin(), command.end(), quitPattern)) != end) { //quit
break;
}
else if ((iter = sregex_iterator(command.begin(), command.end(), openPattern)) != end) { //open
string filename = (*iter)[1];
ifstream ifs(filename);
if (ifs.good()) {
parser.parseFromFile(ifs);
try {
Machine m = builder.getMachine();
machines.push_back(m);
}
catch (invalid_argument ia) {
}
ifs.close();
}
else {
cerr << RED_TEXT << "Loading machine from \"" << filename << "\" was not successful!" << RESET_COLORING << endl;
}
}
else if ((iter = sregex_iterator(command.begin(), command.end(), savePattern)) != end) { // save
unsigned id = std::stoi((*iter)[1]);
string filename = (*iter)[2];
ofstream ofs(filename);
if (ofs.good()) {
try {
Machine m = getMachine(id);
m.save(ofs);
cout << GREEN_TEXT << "Machine " << m.getId() << " was saved in \"" << filename << "\"!" << RESET_COLORING << endl;
ofs.close();
}
catch (invalid_argument ia) {
cerr << RED_TEXT << ia.what() << RESET_COLORING << endl;
ofs.close();
std::remove(filename.c_str());
}
}
else {
cerr << RED_TEXT << "Cannot save machine to \"" << filename << "\" !" << RESET_COLORING << endl;
ofs.close();
std::remove(filename.c_str());
}
}
else if ((iter = sregex_iterator(command.begin(), command.end(), readPattern)) != end) { // read
try {
parser.parseFromConsole();
Machine m = builder.getMachine();
machines.push_back(m);
}
catch (invalid_argument ia) {
}
}
else if ((iter = sregex_iterator(command.begin(), command.end(), printPattern)) != end) { // print
unsigned id = std::stoi((*iter)[1]);
try {
getMachine(id).print();
} catch (invalid_argument ia) {
cerr << RED_TEXT << ia.what() << RESET_COLORING << endl;
}
}
else if ((iter = sregex_iterator(command.begin(), command.end(), composePattern)) != end) { // compose
unsigned id1 = std::stoi((*iter)[1]);
unsigned id2 = std::stoi((*iter)[2]);
try {
Machine composed = getMachine(id1).after(getMachine(id2));
machines.push_back(composed);
cout << GREEN_TEXT << "Composition machine with id " << composed.getId() << " was added!" << RESET_COLORING << endl;;
}
catch (invalid_argument ia) {
cerr << RED_TEXT << ia.what() << RESET_COLORING << endl;
}
}
else if ((iter = sregex_iterator(command.begin(), command.end(), whilePattern)) != end) { // while
unsigned id1 = std::stoi((*iter)[1]);
unsigned id2 = std::stoi((*iter)[2]);
try {
Machine whileMachine = getMachine(id1).whileMachine(getMachine(id2));
machines.push_back(whileMachine);
cout << GREEN_TEXT << "While machine with id " << whileMachine.getId() << " was added!" << RESET_COLORING << endl;
}
catch (invalid_argument ia) {
cerr << RED_TEXT << ia.what() << RESET_COLORING << endl;
}
}
else if ((iter = sregex_iterator(command.begin(), command.end(), startPattern)) != end) { // start
unsigned id = std::stoi((*iter)[1]);
try {
cout << BRIGHT_BLUE_TEXT << "Tape: " << getMachine(id).executeAndGetTape() << RESET_COLORING << endl;
}
catch (invalid_argument ia) {
cerr << RED_TEXT << ia.what() << RESET_COLORING << endl;
}
}
else if ((iter = sregex_iterator(command.begin(), command.end(), startForPattern)) != end) { // startFor
unsigned id = std::stoi((*iter)[1]);
string tape_load = (*iter)[2];
try {
Machine m = getMachine(id);
string res = m.executeAndGetTape(tape_load);
cout << GREEN_TEXT << "Machine " + std::to_string(m.getId()) + " executed succesfully" << RESET_COLORING << endl;
cout << BRIGHT_CYAN_TEXT << "Tape: " << res << RESET_COLORING << endl;
}
catch (invalid_argument ia) {
cerr << RED_TEXT << ia.what() << RESET_COLORING << endl;
}
}
else if ((iter = sregex_iterator(command.begin(), command.end(), ifPattern)) != end) { // if
unsigned id1 = std::stoi((*iter)[1]);
unsigned id2 = std::stoi((*iter)[2]);
unsigned id3 = std::stoi((*iter)[3]);
try {
if (getMachine(id1).execute()) {
cout << BRIGHT_BLUE_TEXT << "Tape: " << getMachine(id2).executeAndGetTape() << RESET_COLORING << endl;
}
else {
cout << BRIGHT_BLUE_TEXT << "Tape: " << getMachine(id2).executeAndGetTape() << RESET_COLORING << endl;
}
}
catch (invalid_argument ia) {
cerr << RED_TEXT << ia.what() << RESET_COLORING << endl;
}
}
else {
cerr << RED_TEXT << "Ivalid command!" << RESET_COLORING << endl;
}
}
}
Machine& System::getMachine(unsigned id) {
for (auto& m : machines) {
if (m.getId() == id) {
return m;
}
}
string err = "Machine with id " + std::to_string(id);
err +=" does not exist!";
throw invalid_argument(err);
}