-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotebook_app.cpp
More file actions
302 lines (225 loc) · 8.41 KB
/
notebook_app.cpp
File metadata and controls
302 lines (225 loc) · 8.41 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
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
#include "notebook_app.hpp"
#include "semantic_error.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <QString>
#include <QLayout>
#include <QDebug>
NotebookApp::NotebookApp(QWidget * parent) : QWidget(parent){
m_input_widget = new InputWidget();
//m_input_widget->setObjectName("input");
m_input_widget->setParent(parent);
m_output_widget = new OutputWidget();
//m_input_widget->setObjectName("output");
m_output_widget->setParent(parent);
/*--- Run Startup Script ---*/
startup(m_interp);
/*--- Start Interpreter Kernel Thread ---*/
//m_queue_in = &InputQueue();
//m_queue_out = &OutputQueue();
//m_interp = new Interpreter();
//m_interp = Interpreter(&m_queue_in, &m_queue_out);
//std::thread kernelThread(&Interpreter::threadEvalLoop, &m_interp);
/*--- Read User Input ---*/
QObject::connect(m_input_widget, SIGNAL(textUpdated(QString)),
this, SLOT(getUserText(QString)));
/*--- Evaluate with Plotscript ---*/
/*--- Display Result Graphics ---*/
QObject::connect(this, SIGNAL(sendResult(Settings)),
m_output_widget, SLOT(getResult(Settings)));
auto layout = new QGridLayout();
layout->addWidget(m_input_widget, 0, 0);
layout->addWidget(m_output_widget, 1, 0);
setLayout(layout);
resize(500, 500);
}
void NotebookApp::getUserText(QString inExp){
qDebug() << "Slot: " << inExp << "\t";
evalExpression(inExp.toStdString());
// Check for special kernel control commands
// Push message to input queue
//m_queue_in.push(inExp.toStdString());
}
int NotebookApp::startup(Interpreter & interp){
std::ifstream ifs(STARTUP_FILE);
std::ostringstream err;
if(!ifs){
emit sendResult(errFormat("Error: Could not open startup file for reading."));
return EXIT_FAILURE;
}
if(!interp.parseStream(ifs)){
ifs.close();
emit sendResult(errFormat("Error: Invalid startup program. Could not parse."));
return EXIT_FAILURE;
}
else{
try{
Expression exp = interp.evaluate();
}
catch(const SemanticError & ex){
ifs.close();
err << ex.what();
emit sendResult(errFormat(err.str()));
return EXIT_FAILURE;
}
}
ifs.close();
return EXIT_SUCCESS;
}
//void NotebookApp::threadEvalLoop(){
// // Wait for results to display
//}
bool NotebookApp::evalExpression(std::string inExp){
//==================================================================
std::istringstream inStream(inExp);
std::ostringstream outStream; // Need this to convert Expression->string
std::string strResult = "default";
Expression expResult;
qDebug() << "Expression In: " << QString::fromStdString(inExp);
if(!m_interp.parseStream(inStream)){
emit sendResult(errFormat("Error: Invalid Program. Could not parse."));
return EXIT_FAILURE;
}
else{
try{
expResult = m_interp.evaluate();
outStream << expResult;
strResult = outStream.str();
qDebug() << "Valid Expression: " << QString::fromStdString(strResult);
}
catch(const SemanticError & ex){
outStream << ex.what();
emit sendResult(errFormat(outStream.str()));
return EXIT_FAILURE;
}
}
//==================================================================
qDebug() << "Expression Out: " << QString::fromStdString(strResult);
Settings result = setGraphicsType(expResult);
emit sendResult(result);
return EXIT_SUCCESS;
}
Settings NotebookApp::setGraphicsType(Expression outExp){
// The graphic object data to send to outputWidget
Settings data;
// Pull out necessary parts of result Expression
Expression propExp = outExp.getProperty("\"object-name\"");
std::ostringstream nameStream;
nameStream << propExp;
std::string expName = nameStream.str();
// Convert Expression->string
std::ostringstream expStream;
expStream << outExp;
std::string expValue = expStream.str();
qDebug() << "Data: " << QString::fromStdString(expName) << QString::fromStdString(expValue);
// Assign graphic type and parameter data based on result
if(outExp.isHeadLambda()){
// Display nothing for procedures
data = Settings();
}
else if(outExp.isTextG()){
QString text = QString::fromStdString(outExp.asString());
// Default values
double x = 0;
double y = 0;
double scale = 1;
double rotate = 0;
// If "position" is in prop list, must be type "point" or error
if(outExp.getProperty("\"position\"") != Expression()){
Expression expProp = outExp.getProperty("\"position\"");
if(expProp.isPointG()){
Expression::List points = expProp.asList();
x = points[0].head().asNumber();
y = points[1].head().asNumber();
}
else{
return errFormat("Error: Position not a point");
}
}
// If "text-scale" is in prop list, it should be a positive Number
if(outExp.getProperty("\"text-scale\"") != Expression()){
Expression expProp = outExp.getProperty("\"text-scale\"");
if( expProp.isHeadNumber() && (expProp.head().asNumber() > 0) ){
scale = expProp.head().asNumber();
}
}
// If "text-rotation" is in prop list, it should be a Number in radians
if (outExp.getProperty("\"text-rotation\"") != Expression()) {
Expression expProp = outExp.getProperty("\"text-rotation\"");
if (expProp.isHeadNumber()) {
// Convert input to degrees for rotate function to work
double rad = expProp.head().asNumber();
double deg = rad * (180/std::atan2(0, -1));
rotate = deg;
}
}
// Package result values for output
data = Settings(Settings::Type::Text_Type, QPoint(x,y), text, scale, rotate);
}
else if(outExp.isPointG()){
// Center at the Point's coordinates with a diameter equal to the size property
Expression::List points = outExp.asList();
double x = points[0].head().asNumber();
double y = points[1].head().asNumber();
double size = 0;
// If "size" is present in the property list, it is an error if this property is not a positive Number.
if(outExp.getProperty("\"size\"") != Expression()){
Expression expProp = outExp.getProperty("\"size\"");
if( expProp.head().isNumber() && (expProp.head().asNumber() > 0 ) ){
size = outExp.getProperty("\"size\"").head().asNumber();
}
else{
return errFormat("Error: Size is not a positive number");
}
}
// Package result values for output
data = Settings(Settings::Type::Point_Type, QPoint(x, y), size);
}
else if(outExp.isLineG()){
// Use the Line's coordinates with a thickness equal to the thickness property.
Expression::List points = outExp.asList();
Expression::List point1 = points[0].asList();
Expression::List point2 = points[1].asList();
double x1 = point1[0].head().asNumber();
double y1 = point1[1].head().asNumber();
double x2 = point2[0].head().asNumber();
double y2 = point2[1].head().asNumber();
QPoint p1(x1, y1);
QPoint p2(x2, y2);
double thicc = 1;
// If "thickness" is present in the property list, it is an error if this property is not a positive Number.
if(outExp.getProperty("\"thickness\"") != Expression()){
Expression expProp = outExp.getProperty("\"thickness\"");
if( expProp.head().isNumber() && (expProp.head().asNumber() >= 0 ) ){
thicc = outExp.getProperty("\"thickness\"").head().asNumber();
}
else{
return errFormat("Error: Thickness is not a positive number");
}
}
// Package result values for output
data = Settings(Settings::Type::Line_Type, p1, p2, thicc);
}
else if(outExp.isHeadList() && (!outExp.isTailEmpty())){
// Recursively display each entry using the rules above without any surrounding parenthesis.
QVector<Settings> list;
for(Expression exp : outExp.asList()){
list.push_back(setGraphicsType(exp));
}
// Package result values for output
data = Settings(Settings::Type::List_Type, list);
}
else{ // None, Number, Complex, Symbol, String
// Package result values for output
data = Settings(Settings::Type::TUI_Type, QString::fromStdString(expValue));
}
qDebug() << "Result: " << data.itemType;
// Send graphic item parameters to output widget to display
return data;
}
Settings NotebookApp::errFormat(const std::string & message){
QString error = QString::fromStdString(message);
return Settings(Settings::Type::TUI_Type, error);
}