-
Notifications
You must be signed in to change notification settings - Fork 0
/
notebook_app.cpp
349 lines (304 loc) · 8.13 KB
/
notebook_app.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include"notebook_app.hpp"
#include "semantic_error.hpp"
#include "startup_config.hpp"
#include <sstream>
#include <fstream>
#include <iostream>
#include <QWidget>
#include <QLayout>
#include <QDebug>
void NotebookApp::ProcessData(MessageQueueStr & msgIn, MessageQueueData & msgOut, Interpreter & interp)
{
while (1)
{
std::string popMessage;
msgIn.wait_and_pop(popMessage);
if (popMessage == "%stop") break;
std::istringstream expMsg(popMessage);
Data OutputData;
if (!interp.parseStream(expMsg))
{
OutputData.ErrMsg = "Error: Invalid Expression. Could not parse.";
OutputData.valid = false;
msgOut.push(OutputData);
}
else
{
try
{
Expression exp = interp.evaluate();
OutputData.Exp = exp;
OutputData.valid = true;
msgOut.push(OutputData);
}
catch (const SemanticError & ex)
{
OutputData.ErrMsg = ex.what();
OutputData.valid = false;
msgOut.push(OutputData);
}
}
}
}
void NotebookApp::makeConnection()
{
QObject::connect(input, SIGNAL(Changed(std::string)), this, SLOT(process(std::string)));
QObject::connect(this, SIGNAL(ClearScene()), output, SLOT(RecieveClearScene()));
QObject::connect(this, SIGNAL(ErrorMessage(std::string)), output, SLOT(RecieveError(std::string)));
QObject::connect(this, SIGNAL(validOutput(std::string)), output, SLOT(RecieveValidOutput(std::string)));
QObject::connect(this, SIGNAL(drawPoint(double, double, double)), output, SLOT(RecieveDrawPoint(double, double, double)));
QObject::connect(this, SIGNAL(drawLine(double, double, double, double, double)), output, SLOT(RecieveDrawLine(double, double, double, double , double)));
QObject::connect(this, SIGNAL(drawString(double, double, double, double, std::string)), output, SLOT(RecieveDrawString(double, double, double, double, std::string)));
QObject::connect(start, SIGNAL(clicked()), this, SLOT(handleStartButton()));
QObject::connect(stop, SIGNAL(clicked()), this, SLOT(handleStopButton()));
QObject::connect(reset, SIGNAL(clicked()), this, SLOT(handleResetButton()));
QObject::connect(interrupt, SIGNAL(clicked()), this, SLOT(handleInterruptButton()));
}
void NotebookApp::handleStartButton()
{
if (thRunning == false)
{
thRunning = true;
worker = std::thread(ProcessData, std::ref(msgIn), std::ref(msgOut), std::ref(interp));
}
}
void NotebookApp::handleStopButton()
{
if (thRunning == true)
{
msgIn.push("%stop");
worker.join();
thRunning = false;
}
else
{
emit ErrorMessage("Error: interpreter kernel not running");
}
}
void NotebookApp::handleResetButton()
{
interp = Interpreter();
startUp();
if (thRunning == false)
{
thRunning = true;
worker = std::thread(ProcessData, std::ref(msgIn), std::ref(msgOut), std::ref(interp));
}
}
void NotebookApp::handleInterruptButton()
{
msgIn.push("%stop");
interp.setInterrupSig(&InterruptionMessage);
emit ErrorMessage("Error: interpreter kernel interrupted");
}
NotebookApp::NotebookApp(QWidget * parent) : QWidget(parent)
{
input = new InputWidget(this);
output = new OutputWidget(this);
start = new QPushButton("Start Kernel", this);
start->setObjectName("start");
stop = new QPushButton("Stop Kernel", this);
stop->setObjectName("stop");
reset = new QPushButton("Reset Kernel", this);
reset->setObjectName("reset");
interrupt = new QPushButton("Interrupt", this);
interrupt->setObjectName("interrupt");
auto HQlayout = new QHBoxLayout();
HQlayout->addWidget(start);
HQlayout->addWidget(stop);
HQlayout->addWidget(reset);
HQlayout->addWidget(interrupt);
auto layout = new QVBoxLayout();
layout->addLayout(HQlayout);
layout->addWidget(input);
layout->addWidget(output);
output->setParent(this);
setLayout(layout);
startUp();
makeConnection();
thRunning = true;
timer = QObject::startTimer(5);
worker = std::thread(ProcessData, std::ref(msgIn), std::ref(msgOut), std::ref(interp));
}
void NotebookApp::startUp()
{
std::ifstream in(STARTUP_FILE);
std::string inputLine;
if (!in)
{
emit ErrorMessage("Error: Can't open file " + STARTUP_FILE + ".");
}
else if (!interp.parseStream(in)) {
emit ErrorMessage("Error: Invalid Expression.Could not parse.");
}
else {
try {
Expression exp = interp.evaluate();
}
catch (const SemanticError & ex) {
emit ErrorMessage(ex.what());
}
}
}
void NotebookApp::processPoint(Expression exp)
{
Expression size = exp.getProperty("size");
double pointsize = size.head().asNumber();
double x = exp.tailConstBegin()->head().asNumber();
double y = exp.tail()->head().asNumber();
if (pointsize < 0)
{
emit ErrorMessage("diameter of at least one point is negative");
return;
}
emit drawPoint(x, y, pointsize);
}
void NotebookApp::processLine(Expression exp)
{
Expression thickness = exp.getProperty("thickness");
double thickness_size = thickness.head().asNumber();
if (thickness_size < 0)
{
emit ErrorMessage("thickness of at least a line is negative");
return ;
}
double coordinate[4]; int i = 0;
auto point1 = exp.tailConstBegin();
auto point2 = exp.tail();
for (auto a = point1->tailConstBegin(); a != point1->tailConstEnd();a++)
{
coordinate[i] = a->head().asNumber();
i++;
}
for (auto a = point2->tailConstBegin(); a != point2->tailConstEnd();a++)
{
coordinate[i] = a->head().asNumber();
i++;
}
emit drawLine(coordinate[0], coordinate[1], coordinate[2], coordinate[3], thickness_size);
}
void NotebookApp::processText(Expression exp)
{
Expression position = exp.getProperty("position");
Expression point = position.getProperty("object-name");
Expression rotation = exp.getProperty("text-rotation");
Expression text_scale = exp.getProperty("text-scale");
if (point.head().asStringConstant() != "point")
{
emit ErrorMessage("position of text is not a point");
return;
}
double x = position.tailConstBegin()->head().asNumber();
double y = position.tail()->head().asNumber();
double angle = rotation.head().asNumber();
double scale = text_scale.head().asNumber();
if (scale < 1) scale = 1;
std::string text_message = exp.head().asStringConstant();
emit drawString(x, y, angle, scale, text_message);
}
NotebookApp::~NotebookApp()
{
if (thRunning == true)
{
msgIn.push("%stop");
worker.join();
}
delete input;
delete output;
input = nullptr;
output = nullptr;
}
void NotebookApp::testNoteBook()
{
std::string check = input->toPlainText().toStdString();
process(check);
}
Expression NotebookApp::AnalyzeOutput(const Expression & exp)
{
if (exp.isHeadSymbol())
if (exp.head().asSymbol() == "lambda")
return exp;
Expression expPropertyType = exp.getProperty("object-name");
if (exp.head().isNone())
{
emit ErrorMessage("NONE");
return exp;
}
else if (expPropertyType.head().asStringConstant() == "point")
{
(processPoint(exp));
return exp;
}
else if (expPropertyType.head().asStringConstant() == "line")
{
(processLine(exp));
return exp;
}
else if (expPropertyType.head().asStringConstant() == "text")
{
(processText(exp));
return exp;
}
else if (exp.isHeadSymbol() && (exp.head().asSymbol() == "list") && (exp.propertySize() == 0))
{
if (!exp.isTailEmpty())
for (auto a = exp.tailConstBegin(); a != exp.tailConstEnd(); a++)
AnalyzeOutput(*a);
}
else
{
std::ostringstream text;
text << exp;
emit validOutput(text.str());
}
return exp;
}
//void NotebookApp::process(std::string line)
//{
// emit ClearScene();
// if (!line.empty())
// {
// std::istringstream expression(line);
//
// if (!interp.parseStream(expression))
// emit ErrorMessage("Error: Invalid Expression.Could not parse.");
// else {
// try {
// Expression exp = interp.evaluate();
// AnalyzeOutput(exp);
// }
// catch (const SemanticError & ex) {
// emit ErrorMessage(ex.what());
// }
// }
// }
//}
void NotebookApp::process(std::string line)
{
emit ClearScene();
if (!line.empty() && thRunning == true)
{
Data result;
msgIn.push(line);
}
if (!line.empty() && !worker.joinable()) // thread is not working
{
emit ErrorMessage("Error: interpreter kernel not running");
}
}
void NotebookApp::timerEvent(QTimerEvent *event) {
Data result;
if (msgOut.try_pop(result))
{
if (result.valid)
{
AnalyzeOutput(result.Exp);
}
else
{
emit ErrorMessage(result.ErrMsg);
}
}
else
QObject::timerEvent(event);
}