-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqt_interpreter.cpp
170 lines (147 loc) · 4.53 KB
/
qt_interpreter.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
// File: qt_interpreter.cpp
// Author: Samuel McFalls
#include "qt_interpreter.hpp"
#include "qgraphics_arc_item.hpp"
#include <QObject>
#include <QGraphicsItem>
#include <QBrush>
#include <QLineF>
#include <QtMath>
#include <string>
#include <sstream>
QtInterpreter::QtInterpreter(QObject * parent): QObject(parent) {
}
void QtInterpreter::parseAndEvaluate(QString entry) {
std::string command = entry.toStdString();
std::istringstream iss(command);
bool ok = interp.parse(iss);
if (!ok) {
error(QString("Error: Failed to parse invalid expression"));
return;
}
interp.saveState();
Expression result;
try {
result = interp.eval();
createGraphics(interp.toBeDrawn());
}
catch (InterpreterSemanticError e) {
interp.restoreState();
QString errMsg;
errMsg.append("Error during evaluation: ");
errMsg.append(e.what());
error(QString(errMsg));
return;
}
if (result.getAtom().getType() == Atom::Type::BOOL) {
info(boolString(result));
}
else if (result.getAtom().getType() == Atom::Type::NUMBER) {
info(numberString(result));
}
else if (result.getAtom().getType() == Atom::Type::POINT) {
info(pointString(result));
}
else if (result.getAtom().getType() == Atom::Type::LINE) {
info(lineString(result));
}
else if (result.getAtom().getType() == Atom::Type::ARC) {
info(arcString(result));
}
else {
info("(None)");
}
}
void QtInterpreter::createGraphics(std::vector<Atom> items) {
for (auto it = items.begin(); it != items.end(); ++it) {
switch ((*it).getType()) {
case Atom::Type::POINT: {
double x = pointX((*it).getPoint());
double y = pointY((*it).getPoint());
QGraphicsEllipseItem * point = new QGraphicsEllipseItem();
point->setRect(QRectF(x - 2, y - 2, 4, 4));
point->setBrush(QBrush(Qt::black));
drawGraphic(point);
break;
}
case Atom::Type::LINE: {
double x1 = pointX((*it).getLine().first);
double y1 = pointY((*it).getLine().first);
double x2 = pointX((*it).getLine().second);
double y2 = pointY((*it).getLine().second);
QGraphicsLineItem * line = new QGraphicsLineItem();
line->setLine(QLineF(QPointF(x1, y1), QPointF(x2, y2)));
drawGraphic(line);
break;
}
case Atom::Type::ARC: {
double cx = pointX((*it).getArc().center);
double cy = pointY((*it).getArc().center);
double sx = pointX((*it).getArc().start);
double sy = pointY((*it).getArc().start);
double spanAngle = qRadiansToDegrees((*it).getArc().span) * 16;
QLineF line(QLineF(QPointF(cx, cy), QPointF(sx, sy)));
double width = line.length() * 2;
double height = width;
double startAngle = line.angle() * 16;
QGraphicsArcItem * arc = new QGraphicsArcItem();
arc->setRect(QRectF(cx - width / 2, cy - height / 2, width, height));
arc->setStartAngle(startAngle);
arc->setSpanAngle(spanAngle);
drawGraphic(arc);
break;
}
}
}
}
QString QtInterpreter::boolString(Expression exp) {
QString infoMsg;
infoMsg.append("(");
infoMsg.append(exp.getAtom().getBool() ? "True" : "False");
infoMsg.append(")");
return infoMsg;
}
QString QtInterpreter::numberString(Expression exp) {
QString infoMsg;
infoMsg.append("(");
infoMsg.append(QString::number(exp.getAtom().getNumber()));
infoMsg.append(")");
return infoMsg;
}
QString QtInterpreter::pointString(Expression exp) {
QString infoMsg;
infoMsg.append("(");
infoMsg.append(QString::number(pointX(exp.getAtom().getPoint())));
infoMsg.append(", ");
infoMsg.append(QString::number(pointY(exp.getAtom().getPoint())));
infoMsg.append(")");
return infoMsg;
}
QString QtInterpreter::lineString(Expression exp) {
QString infoMsg;
infoMsg.append(("(("));
infoMsg.append(QString::number(pointX(exp.getAtom().getLine().first)));
infoMsg.append(", ");
infoMsg.append(QString::number(pointY(exp.getAtom().getLine().first)));
infoMsg.append("), (");
infoMsg.append(QString::number(pointX(exp.getAtom().getLine().second)));
infoMsg.append(", ");
infoMsg.append(QString::number(pointY(exp.getAtom().getLine().second)));
infoMsg.append("))");
return infoMsg;
}
QString QtInterpreter::arcString(Expression exp) {
QString infoMsg;
infoMsg.append(("(("));
infoMsg.append(QString::number(pointX(exp.getAtom().getArc().center)));
infoMsg.append(", ");
infoMsg.append(QString::number(pointY(exp.getAtom().getArc().center)));
infoMsg.append("), (");
infoMsg.append(QString::number(pointX(exp.getAtom().getArc().start)));
infoMsg.append(", ");
infoMsg.append(QString::number(pointY(exp.getAtom().getArc().start)));
infoMsg.append(") ");
infoMsg.append(QString::number(exp.getAtom().getArc().span));
infoMsg.append(")");
return infoMsg;
}