-
Notifications
You must be signed in to change notification settings - Fork 0
/
programmodel.cpp
638 lines (551 loc) · 16.2 KB
/
programmodel.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// vim:ts=4:sw=4:
//
// Interactive BASIC Compiler Project
// File: programmodel.h - program model class source file
// Copyright (C) 2013-2015 Thunder422
//
// This program 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 3 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.
//
// For a copy of the GNU General Public License,
// see <http://www.gnu.org/licenses/>.
//
//
// Change History:
//
// 2013-03-15 initial version
#include "programmodel.h"
#include "rpnlist.h"
#include "statusmessage.h"
#include "table.h"
#include "token.h"
////////////////////////////////////////////////////////////////////////////////
// //
// PROGRAM WORD FUNCTIONS //
// //
////////////////////////////////////////////////////////////////////////////////
// overloaded output stream operator for contents of an instruction word
std::ostream &operator<<(std::ostream &os, ProgramWord word)
{
TableEntry *entry {Table::entry(word.instructionCode())};
os << entry->debugName();
if (word.instructionHasSubCode(ProgramMask_SubCode))
{
os << '\'';
if (!entry->hasFlag(Command_Flag)
&& word.instructionHasSubCode(Paren_SubCode))
{
os << ')';
}
if (word.instructionHasSubCode(Option_SubCode))
{
std::string option {entry->optionName()};
os << (option.empty() ? "BUG" : option);
}
if (entry->hasFlag(Command_Flag)
&& word.instructionHasSubCode(Colon_SubCode))
{
os << ":";
}
os << '\'';
}
return os;
}
////////////////////////////////////////////////////////////////////////////////
// //
// PROGRAM UNIT MODEL FUNCTIONS //
// //
////////////////////////////////////////////////////////////////////////////////
ProgramModel::ProgramModel(QObject *parent) :
QAbstractListModel(parent),
m_remDictionary {new Dictionary(CaseSensitive::Yes)},
m_constNumDictionary {new ConstNumDictionary},
m_constStrDictionary {new ConstStrDictionary},
m_varDblDictionary {new Dictionary},
m_varIntDictionary {new Dictionary},
m_varStrDictionary {new Dictionary}
{
}
// NOTE temporary function to return the text for a program line
std::string ProgramModel::debugText(int lineIndex, bool fullInfo) const
{
std::ostringstream oss;
const LineInfo &lineInfo = m_lineInfo[lineIndex];
if (fullInfo)
{
oss << '[' << lineInfo.offset;
if (lineInfo.size > 0)
{
oss << '-' << lineInfo.offset + lineInfo.size - 1;
}
oss << ']';
}
auto line = m_code.begin() + m_lineInfo.offset(lineIndex);
int size {m_lineInfo.size(lineIndex)};
for (int i {}; i < size; i++)
{
if (i > 0 || fullInfo)
{
oss << ' ';
}
oss << i << ':' << line[i];
TableEntry *entry {Table::entry(line[i].instructionCode())};
if (auto operandText = entry->operandTextFunction())
{
SubCode subCode {line[i].instructionSubCode()};
const std::string operand {operandText(this, line[++i].operand(),
subCode)};
oss << ' ' << i << ":|" << line[i].operand() << ':' << operand
<< '|';
}
}
if (fullInfo && lineInfo.errIndex != -1)
{
const ErrorItem &errorItem {m_errors[lineInfo.errIndex]};
oss << " ERROR " << errorItem.column() << ':' << errorItem.length()
<< ' ' << StatusMessage::text(errorItem.status()).toStdString();
}
return oss.str();
}
// function to return text for a given program line
std::string ProgramModel::lineText(int lineIndex)
{
LineInfo &lineInfo = m_lineInfo[lineIndex];
// return original test if line has error
return lineInfo.errIndex == -1
? Recreator{}(decode(lineInfo)) : lineInfo.text;
}
// function to return data for a given program line
QVariant ProgramModel::data(const QModelIndex &index, int role) const
{
if (index.isValid())
{
if (role == Qt::TextAlignmentRole)
{
return int(Qt::AlignRight | Qt::AlignVCenter);
}
else if (role == Qt::DisplayRole)
{
return debugText(index.row(), true).c_str();
}
}
return QVariant();
}
// function to return the number of program lines
int ProgramModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_lineInfo.size();
}
// function to reset to contents of the program model
void ProgramModel::clear(void)
{
m_lineInfo.clear();
m_code.clear();
m_errors.clear();
m_remDictionary->clear();
m_constNumDictionary->clear();
m_constStrDictionary->clear();
m_varDblDictionary->clear();
m_varIntDictionary->clear();
m_varStrDictionary->clear();
emit programCleared();
}
// slot function to receive program updates
// - if lineNumber == 1 then append line to end of program
void ProgramModel::update(int lineNumber, int linesDeleted, int linesInserted,
std::vector<std::string> &&lines)
{
int i;
int oldSize = m_lineInfo.size();
int size = lines.size();
for (i = 0; i < size - linesInserted; i++)
{
// update changed program lines if they actually changed
if (updateLine(Operation::Change, lineNumber, std::move(lines[i])))
{
// need to emit signal that data changed
QModelIndex index {this->index(lineNumber)};
emit dataChanged(index, index);
}
lineNumber++;
}
if (linesDeleted > 0)
{
// delete lines from the program
int lastLineNumber {lineNumber + linesDeleted - 1};
beginRemoveRows(QModelIndex(), lineNumber, lastLineNumber);
while (--linesDeleted >= 0)
{
updateLine(Operation::Remove, lineNumber);
}
endRemoveRows();
}
else if (linesInserted > 0)
{
Operation operation;
if (lineNumber == -1) // append to end of program?
{
lineNumber = oldSize;
operation = Operation::Append;
}
else // insert new lines into the program
{
operation = Operation::Insert;
}
int lastLineNumber {lineNumber + linesInserted - 1};
beginInsertRows(QModelIndex(), lineNumber, lastLineNumber);
while (i < size)
{
updateLine(operation, lineNumber++, std::move(lines[i++]));
}
endInsertRows();
}
if (m_lineInfo.size() != oldSize)
{
// emit new line count if changed
emit lineCountChanged(m_lineInfo.size());
}
if (m_errors.hasChanged())
{
emit errorListChanged();
}
}
bool ProgramModel::updateLine(Operation operation, int lineNumber,
std::string &&line)
{
RpnList rpnList;
ProgramCode lineCode;
ErrorItem errorItem;
if (operation != Operation::Remove)
try
{
rpnList = Translator{line}();
}
catch (TokenError &error)
{
errorItem = ErrorItem(ErrorItem::Type::Input, lineNumber,
error.m_column, error.m_length, error());
}
if (operation == Operation::Change)
{
LineInfo &lineInfo = m_lineInfo[lineNumber];
RpnList currentRpnList {decode(lineInfo)};
bool changed {rpnList != currentRpnList || errorItem};
if (changed)
{
// derefence old line
// (line gets deleted if new line has an error)
dereference(lineInfo);
// line is different, encode it if there was no translation error
if (!errorItem)
{
lineCode = encode(std::move(rpnList));
lineInfo.text.clear();
}
else // else store line text for error line
{
lineInfo.text = std::move(line);
}
updateError(lineNumber, lineInfo, errorItem, false);
// replace with new line
m_code.replaceLine(lineInfo.offset, lineInfo.size, lineCode);
m_lineInfo.replace(lineNumber, lineCode.size());
}
if (!errorItem)
{
emit programChange(lineNumber); // only for non-error lines
}
return changed;
}
else if (operation == Operation::Append || operation == Operation::Insert)
{
LineInfo lineInfo;
lineInfo.errIndex = -1;
updateError(lineNumber, lineInfo, errorItem, true);
// encode line if there was no translation error
if (!errorItem)
{
lineCode = encode(std::move(rpnList));
}
else // else store line text for error line
{
lineInfo.text = std::move(line);
}
// find offset to insert line
if (lineNumber < m_lineInfo.size())
{
lineInfo.offset = m_lineInfo.offset(lineNumber);
}
else // append to end
{
lineInfo.offset = m_code.size();
}
lineInfo.size = lineCode.size(); // zero if line has error
// insert line into code (nothing if line has error)
m_code.insertLine(lineInfo.offset, lineCode);
m_lineInfo.insert(lineNumber, lineInfo);
if (operation == Operation::Append || !errorItem)
{
// only for appended lines or lines with no errors
emit programChange(lineNumber);
}
}
else if (operation == Operation::Remove)
{
LineInfo &lineInfo = m_lineInfo[lineNumber];
removeError(lineNumber, lineInfo, true);
// derefence old line and remove from code
dereference(lineInfo);
m_code.removeLine(lineInfo.offset, lineInfo.size);
// remove from line info list
m_lineInfo.erase(lineNumber);
}
return true;
}
// function to update error into list if line has an error
void ProgramModel::updateError(int lineNumber, LineInfo &lineInfo,
const ErrorItem &errorItem, bool lineInserted)
{
if (!lineInserted)
{
if (!errorItem)
{
removeError(lineNumber, lineInfo, false);
return; // nothing more to do
}
else if (lineInfo.errIndex != -1) // had error?
{
// replace current error
m_errors.replace(lineInfo.errIndex, errorItem);
emit errorChanged(lineInfo.errIndex, errorItem);
return; // nothing more to do
}
}
// find location in error list for line number
auto errIndex = m_errors.find(lineNumber);
if (errorItem)
{
// insert new error into error list
m_errors.insert(errIndex, errorItem);
emit errorInserted(errIndex, errorItem);
lineInfo.errIndex = errIndex++;
}
// loop thru rest of errors in list
for (; errIndex < m_errors.count(); errIndex++)
{
if (errorItem)
{
// adjust error index for inserted error
m_lineInfo[m_errors[errIndex].lineNumber()].errIndex++;
}
if (lineInserted)
{
// adjust error line number for inserted line
m_errors.incrementLineNumber(errIndex);
emit errorChanged(errIndex, m_errors.at(errIndex));
}
}
}
// function to process a line that has been edited
void ProgramModel::lineEdited(int lineNumber, int column, bool atLineEnd,
int charsAdded, int charsRemoved)
{
auto errIndex = m_errors.findIndex(lineNumber);
if (errIndex < m_errors.count()) // line has error?
{
int errColumn {m_errors.at(errIndex).column()};
int errLength {m_errors.at(errIndex).length()};
if (column - charsAdded <= errColumn + errLength)
{
// change is within or before error
if ((atLineEnd && column + charsRemoved >= errColumn)
|| column - charsAdded + charsRemoved > errColumn)
{
// change and error at end of line or change is within error
// remove error
LineInfo &lineInfo = m_lineInfo[lineNumber];
removeError(lineNumber, lineInfo, false);
}
else // cursor is before error, move error column
{
m_errors.moveColumn(errIndex, charsAdded - charsRemoved);
emit errorChanged(errIndex, m_errors.at(errIndex));
}
m_errors.hasChanged(); // clear changed flag
emit errorListChanged();
}
}
}
// function to remove an error from the list if line had an error
void ProgramModel::removeError(int lineNumber, LineInfo &lineInfo,
bool lineDeleted)
{
size_t errIndex;
bool hadError;
if (lineInfo.errIndex != -1) // has error?
{
errIndex = lineInfo.errIndex;
// remove error (for changed line with no error or deleted line)
m_errors.removeAt(errIndex);
emit errorRemoved(errIndex);
lineInfo.errIndex = -1;
hadError = true;
}
else if (lineDeleted)
{
// find location in error list for line number
errIndex = m_errors.find(lineNumber);
hadError = false;
}
else // line not deleted and did not have an error
{
return; // nothing more to do
}
// loop thru rest of errors in list
for (; errIndex < m_errors.count(); errIndex++)
{
if (hadError)
{
// adjust error index for removed error
m_lineInfo[m_errors[errIndex].lineNumber()].errIndex--;
}
if (lineDeleted)
{
// adjust error line number for deleted line
m_errors.decrementLineNumber(errIndex);
emit errorChanged(errIndex, m_errors.at(errIndex));
}
}
}
// function to find the next error from a given line number and column
//
// - returns the line number and column of the next error
// - returns whether the next error is the first error (wrapped)
// - returns true upon success, false if already at single error
bool ProgramModel::errorFindNext(int &lineNumber, int &column, bool &wrapped)
const
{
wrapped = false;
auto errIndex = m_errors.find(lineNumber);
if (errIndex >= m_errors.count()
|| lineNumber > m_errors.at(errIndex).lineNumber()
|| (lineNumber == m_errors.at(errIndex).lineNumber()
&& column >= m_errors.at(errIndex).column()))
{
// past current error, go to next error
errIndex++;
}
if (errIndex >= m_errors.count()) // past last error?
{
errIndex = 0;
// check if already at the single error in the program
if (m_errors.count() == 1 && lineNumber == m_errors.at(0).lineNumber()
&& column >= m_errors.at(0).column()
&& column < m_errors.at(0).column() + m_errors.at(0).length())
{
return false;
}
wrapped = true;
}
lineNumber = m_errors.at(errIndex).lineNumber();
column = m_errors.at(errIndex).column();
return true;
}
// function to find the previous error from a given line number and column
//
// - returns the line number and column of the previous error
// - returns whether the previous error is the last error (wrapped)
// - returns true upon success, false if already at begin of single error
bool ProgramModel::errorFindPrev(int &lineNumber, int &column, bool &wrapped)
const
{
wrapped = false;
auto errIndex = m_errors.find(lineNumber);
if (errIndex >= m_errors.count()
|| lineNumber < m_errors.at(errIndex).lineNumber()
|| (lineNumber == m_errors.at(errIndex).lineNumber()
&& column < m_errors.at(errIndex).column()
+ m_errors.at(errIndex).length()))
{
// before current error, go to previous error
errIndex--;
}
if (errIndex > m_errors.count()) // past first error?
{
errIndex = m_errors.count() - 1;
// check if already at beginning of single error in the program
if (m_errors.count() == 1
&& lineNumber == m_errors.at(errIndex).lineNumber()
&& column == m_errors.at(errIndex).column())
{
return false;
}
wrapped = true;
}
lineNumber = m_errors.at(errIndex).lineNumber();
column = m_errors.at(errIndex).column();
return true;
}
// function to return error status for a line (default if no error)
Status ProgramModel::errorStatus(int lineNumber) const
{
auto errIndex = m_errors.findIndex(lineNumber);
return errIndex == m_errors.count()
? Status{} : m_errors.at(errIndex).status();
}
// function to encode a translated RPN list
ProgramCode ProgramModel::encode(RpnList &&input)
{
ProgramCode programLine;
for (RpnItemPtr rpnItem : input)
{
TokenPtr token {rpnItem->token()};
programLine.emplace_back(token->index(), token->subCodes());
if (auto encode = token->tableEntry()->encodeFunction())
{
programLine.emplace_back(encode(this, token));
}
}
return programLine;
}
// function to dereference contents of line to prepare for its removal
void ProgramModel::dereference(const LineInfo &lineInfo)
{
auto line = m_code.begin() + lineInfo.offset;
for (int i {}; i < lineInfo.size; i++)
{
TableEntry *entry {Table::entry(line[i].instructionCode())};
if (auto remove = entry->removeFunction())
{
remove(this, line[++i].operand());
}
}
}
// function to decode a program line into an RPN list
RpnList ProgramModel::decode(const LineInfo &lineInfo)
{
RpnList rpnList;
auto line = m_code.begin() + lineInfo.offset;
for (int i {}; i < lineInfo.size; i++)
{
TableEntry *entry {Table::entry(line[i].instructionCode())};
TokenPtr token {std::make_shared<Token>(entry)};
token->addSubCode(line[i].instructionSubCode());
if (auto operandText = token->tableEntry()->operandTextFunction())
{
token->setString(operandText(this, line[++i].operand(),
Ignore_SubCode)); // don't add data type character to token
}
rpnList.append(token);
}
return rpnList;
}
// end: programmodel.cpp