forked from GCodeProjects/GCodeWorkShop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils-compilemacro.cpp
More file actions
291 lines (227 loc) · 7.42 KB
/
utils-compilemacro.cpp
File metadata and controls
291 lines (227 loc) · 7.42 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
/*
* Copyright (C) 2006-2018 by Artur Kozioł, artkoz78@gmail.com
* Copyright (C) 2023 Nick Egorrov, nicegorov@yandex.ru
*
* This file is part of GCodeWorkShop.
*
* GCodeWorkShop is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QCoreApplication> // for QCoreApplication
#include <QChar> // for operator==, QChar, operator!=
#include <QRegularExpression> // for QRegularExpression, QRegularExpression::CaseInsensitiveOption
#include <QRegularExpressionMatch> // for QRegularExpressionMatch
#include <basic_interpreter.h> // for BasicInterpreter
#include <utils/expressionparser.h> // for processBrc, ERR_CONVERT, ERR_DOUBLE_DOT, ERR_NO_BRAC, ERR_NO_PARAM
#include "utils-compilemacro.h"
#define tr(s) (QCoreApplication::translate("CompileMacro", s))
int Utils::CompileMacro::compile(const QString& tx)
{
int error;
m_result = tx;
QRegularExpression regexBegin("\\{BEGIN\\}");
regexBegin.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
QRegularExpression regexEnd("\\{END\\}");
regexEnd.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
auto matchBegin = regexBegin.match(m_result);
auto matchEnd = regexEnd.match(m_result, matchBegin.capturedEnd());
if (!matchBegin.hasMatch() || !matchEnd.hasMatch()) {
m_status = tr("No constant definition .\n{BEGIN}\n...\n{END}\n No macro ?");
return -1;
}
QRegularExpression regex("\\{\\$[A-Z0-9\\s]*\\b[=\\n\\r]");
regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
auto match = regex.match(m_result, matchBegin.capturedEnd());
while (match.hasMatch() && match.capturedEnd() <= matchEnd.capturedStart()) {
int pos = match.capturedEnd();
QString param = match.captured();
param = param.remove(' ');
param = param.remove('{');
param = param.remove('=');
QString val = "";
do {
val = val + m_result.at(pos);
pos++;
if (m_result.at(pos) == '\n' || m_result.at(pos) == '{') {
m_status = tr("Param list: no bracket \'}\' !");
return -1;
}
} while ((m_result.at(pos) != '}'));
val = val.remove(' ');
int i = matchEnd.capturedStart();
while ((i = m_result.indexOf(param, i)) >= 0) {
m_result.replace(i, param.length(), val);
}
match = regex.match(m_result, pos);
}
m_result.remove(matchBegin.capturedStart(), matchEnd.capturedEnd() - matchBegin.capturedStart());
regex.setPattern("\\{[-+*=.,()$/0-9A-Z\\s]*\\b[-+*=.,()$/0-9A-Z\\s]*[}]");
match = regex.match(m_result);
while (match.hasMatch()) {
QString param = match.captured();
param = param.simplified();
param = param.remove(QChar(' '));
param = param.replace(',', '.');
if (!param.isEmpty()) {
QString paramTmp = param;
error = Utils::processBrc(¶m);
if (error < 0) {
setError(error, paramTmp);
return -1;
}
if (!param.isEmpty()) {
paramTmp = param;
error = Utils::processBrc(¶m);
if (error < 0) {
setError(error, paramTmp);
return -1;
}
}
QString val = param;
val = val.remove('{');
val = val.remove('}');
m_result.replace(match.capturedStart(), match.capturedLength(), val);
}
match = regex.match(m_result);
}
regexBegin.setPattern("\\{BEGIN_SUBS\\}");
matchBegin = regexBegin.match(m_result);
regex.setPattern("\\{END_SUBS\\}");
matchEnd = regex.match(m_result, matchBegin.capturedEnd());
QString basicSubs;
if (matchBegin.hasMatch() && matchEnd.hasMatch()) {
basicSubs = m_result.mid(matchBegin.capturedEnd(),
matchEnd.capturedStart() - matchBegin.capturedEnd());
m_result.remove(matchBegin.capturedStart(),
matchEnd.capturedEnd() - matchBegin.capturedStart() + 1);
}
regexBegin.setPattern("\\{BEGIN_BASIC\\}");
regexEnd.setPattern("\\{END_BASIC\\}");
matchBegin = regexBegin.match(m_result);
matchEnd = regexEnd.match(m_result, matchBegin.capturedEnd());
while (matchBegin.hasMatch() && matchEnd.hasMatch()) {
QString basicCode = m_result.mid(matchBegin.capturedEnd(),
matchEnd.capturedStart() - matchBegin.capturedEnd());
m_result.remove(matchBegin.capturedStart(),
matchEnd.capturedEnd() - matchBegin.capturedStart() + 1);
basicCode.append(basicSubs);
error = BasicInterpreter().interpretBasic(basicCode);
if (error > 0) {
setBasicError(error);
return -1;
}
m_result.insert(matchBegin.capturedStart(), basicCode);
matchBegin = regexBegin.match(m_result, matchBegin.capturedStart());
matchEnd = regexEnd.match(m_result, matchBegin.capturedEnd());
}
cleanUp(m_result);
return 1;
}
const QString& Utils::CompileMacro::result()
{
return m_result;
}
const QString& Utils::CompileMacro::status()
{
return m_status;
}
void Utils::CompileMacro::setError(int error, const QString& tx)
{
if (error < 0) {
switch (error) {
case ERR_NO_BRAC:
m_status = tr("No ( or ) !");
break;
case ERR_NO_PARAM:
m_status = tr("Function parameter not found ! \n Check +-*/.\n\"%1\"").arg(tx);
break;
case ERR_CONVERT:
m_status = tr("Wrong number !");
break;
case ERR_UNKNOWN_FUNC:
m_status = tr("Unknown math function !\n\"%1\"").arg(tx);
break;
case ERR_DOUBLE_DOT:
m_status = tr("Decimal point or minus written two times !\n\"%1\"").arg(tx);
break;
default:
m_status = tr("Unknown error !");
}
m_status = tr("BRC error:\n%1\nCode=%2").arg(m_status).arg(error);
}
}
void Utils::CompileMacro::setBasicError(int error)
{
switch (error) {
case 0:
m_status = tr("Syntax error");
break;
case 1:
m_status = tr("Unbalanced parentheses");
break;
case 2:
m_status = tr("No expression present");
break;
case 3:
m_status = tr("Equals sign expected");
break;
case 4:
m_status = tr("Not a variable");
break;
case 5:
m_status = tr("Label table full");
break;
case 6:
m_status = tr("Duplicate label");
break;
case 7:
m_status = tr("Undefined label");
break;
case 8:
m_status = tr("THEN expected");
break;
case 9:
m_status = tr("TO expected");
break;
case 10:
m_status = tr("Too many nested FOR loops");
break;
case 11:
m_status = tr("NEXT without FOR");
break;
case 12:
m_status = tr("Too many nested GOSUBs");
break;
case 13:
m_status = tr("RETURN without GOSUBs");
break;
default:
m_status = tr("Unknown error");
}
m_status = tr("Basic interpreter error:\n%1\nCode=%2").arg(m_status).arg(error);
}
void Utils::CompileMacro::cleanUp(QString& str) //remove not needed zeros
{
QRegularExpression regex;
regex.setPattern("([\\d]+[.][-+.0-9]+)|\\([^\\n\\r]*\\)|\'[^\\n\\r]*\'|;[^\\n\\r]*$");
auto match = regex.match(str);
// FIXME: This slow code deletes non-significant zeros one at a time.
while (match.hasMatch()) {
int pos = match.capturedEnd();
if (match.capturedLength(1) != 0 && str.at(match.capturedEnd() - 1) == '0') {
str.remove(match.capturedEnd() - 1, 1);
pos = match.capturedStart();
}
match = regex.match(str, pos);
}
}