-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
770 lines (766 loc) · 30.4 KB
/
mainwindow.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QDebug>
#include <QFileDialog>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->CodeDisplay->setText("");
ui->textBrowser->setText("");
ui->treeDisplay->setText("");
ui->cmdLineEdit->setText("");
connect(ui->btnRunCode, &QPushButton::clicked, [ = ] ()
{
ui->cmdLineEdit->setText("RUN");
on_cmdLineEdit_editingFinished();
});
connect(ui->btnClearCode, &QPushButton::clicked, [ = ] ()
{
ui->cmdLineEdit->setText("CLEAR");
on_cmdLineEdit_editingFinished();
});
connect(ui->btnLoadCode, &QPushButton::clicked, [ = ] ()
{
ui->cmdLineEdit->setText("LOAD");
on_cmdLineEdit_editingFinished();
});
}
MainWindow::~MainWindow()
{
delete ui;
}
//删掉字符串首尾的冗余空格
string MainWindow::strFormalize(string originStr)
{
int head = 0, tail = originStr.length() - 1;
while (head <= tail && originStr[head] == ' ') head++;
while (head <= tail && originStr[tail] == ' ') tail--;
if (head > tail) return "";
string ansStr(originStr, head, tail - head + 1);
return ansStr;
}
//刷新代码框中的代码内容
void MainWindow::codeFlush()
{
ui->CodeDisplay->setText("");
for (map<int, string>::iterator it = inputStringMap.begin(); it != inputStringMap.end(); it++)
ui->CodeDisplay->append(QString::fromStdString(it->second));
}
//执行某行语句
void MainWindow::executeCmdLine(int cmdLineNumber, string cmdLineStr)
{
int mybin;
stringstream ss;
string whichStm = "";
ss << cmdLineStr;
ss >> mybin;
ss >> whichStm;
Statement* nowParsedStm = parsedStmMap[cmdLineNumber];
nowParsedStm->stat++;
if (whichStm == "REM") {}
else if (whichStm == "LET")
{
context.setValue(nowParsedStm->sons[0]->toString(), nowParsedStm->sons[1]->calc(&context));
}
else if (whichStm == "PRINT")
{
ui->textBrowser->append(QString::number(nowParsedStm->sons[0]->calc(&context)));
}
else if (whichStm == "INPUT")
{
ui->cmdLineEdit->setText(" ? ");
isInputting = 1;
inputName = nowParsedStm->sons[0]->toString();
QEventLoop loop;
connect(this, &MainWindow::inputFinished, &loop, &QEventLoop::quit);
loop.exec();
}
else if (whichStm == "GOTO")
{
pc = nowParsedStm->sons[0]->calc(&context);
}
else if (whichStm == "IF")
{
int leftVal = nowParsedStm->sons[0]->calc(&context), rightVal = nowParsedStm->sons[2]->calc(&context);
int cmp = leftVal - rightVal;
string op = nowParsedStm->sons[1]->toString();
if ((op == "<" && cmp < 0) || (op == "=" && !cmp) || (op == ">" && cmp > 0)) nowParsedStm->Lstat++, pc = nowParsedStm->sons[3]->calc(&context);
else nowParsedStm->Rstat++;
}
else if (whichStm == "END")
{
pc = 1147483647;
}
else throw int(6);
}
//执行某行语句(重载)
void MainWindow::executeCmdLine(string stmLabel, Statement* stm)
{
if (stmLabel == "LET")
{
context.setValue(stm->sons[0]->toString(), stm->sons[1]->calc(&context));
}
else if (stmLabel == "PRINT")
{
ui->textBrowser->append(QString::number(stm->sons[0]->calc(&context)));
}
else if (stmLabel == "INPUT")
{
ui->cmdLineEdit->setText(" ? ");
isInputting = 1;
inputName = stm->sons[0]->toString();
QEventLoop loop;
connect(this, &MainWindow::inputFinished, &loop, &QEventLoop::quit);
loop.exec();
}
else throw int(6);
}
//Expression* MainWindow::composeExp(char op)
//{
// Expression *RHS = operantStack.top();operantStack.pop();
// Expression *LHS = operantStack.top();operantStack.pop();
// Expression* Exp1 = new ComExp(op, LHS, RHS);
// connect(Exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
// return Exp1;
//}
//用于表达式parse过程,对于给定的运算符,从操作数栈中出栈两次,组成一个ComExp表达式
Expression* MainWindow::toLink(char tmp, int now)
{
if (operantStack.empty()) throw int(7);
Expression* operant1 = operantStack.top();
operantStack.pop();
int rIndex = operantIndex.top();
operantIndex.pop();
if (operantStack.empty()) throw int(7);
Expression* operant2 = operantStack.top();
operantStack.pop();
int lIndex = operantIndex.top();
operantIndex.pop();
if (rIndex <= now || lIndex >= now) throw int(7);
Expression* Exp1 = new ComExp(tmp, operant2, operant1);
return Exp1;
}
//对表达式进行parse操作
Expression* MainWindow::parser(string str)
{
while (!operantStack.empty()) operantStack.pop();
while (!operatorStack.empty()) operatorStack.pop();
while (!operantIndex.empty()) operantIndex.pop();
while (!operatorIndex.empty()) operatorIndex.pop();
overallIndex = 0;
if (!str.length())
{
Expression* p1 = new IdenExp("");
connect(p1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
return p1;
}
unsigned int validPos = 0, len = str.length();
bool case1 = 0;
while (validPos != len && str[validPos] == ' ') validPos++;
if (validPos != len && str[validPos] == '-') case1 = 1;
while (validPos != len)
{
char ch = str[validPos];
while (ch == ' ') ch = str[++validPos];
int mode = 0;
if (ch == 'M')
{
string strFromNow(str, validPos);
stringstream ss;
ss << strFromNow;
string testStr;
ss >> testStr;
if (testStr == "MOD") mode = 1, ch = '%';
else mode = 3;
}
else if (ch >= '0' && ch <= '9') mode = 2;
else if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || ch == '_') mode = 3;
else if (ch == '+' || ch == '-' ) mode = 4;
else if (ch == '(') mode = 5;
else if (ch == ')') mode = 6;
else if (ch == '/') mode = 7;
else if (ch == '*')
{
if (validPos + 1 < len && str[validPos + 1] == '*')
{
mode = 8, ch = '^';
}
else mode = 7;
}
else throw(3);
if (mode == 1) // %
{
validPos += 3;
while (!operatorStack.empty())
{
char tmp = operatorStack.top();
if (!(tmp == '*' || tmp == '/' || tmp == '%' || tmp == '^')) break;
operatorStack.pop();
int now = operatorIndex.top();
operatorIndex.pop();
Expression* Exp1 = toLink(tmp, now);
connect(Exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
operantStack.push(Exp1);
operantIndex.push(++overallIndex);
}
operatorStack.push(ch);
operatorIndex.push(++overallIndex);
}
else if (mode == 2) // 0-9
{
int tmp = 0;
while (validPos < len)
{
if (str[validPos] >= '0' && str[validPos] <= '9') tmp = tmp * 10 + (str[validPos++] - '0');
else if ((str[validPos] >= 'A' && str[validPos] <= 'Z') || (str[validPos] >= 'a' && str[validPos] <= 'z') || str[validPos] == '_')
throw std::invalid_argument("");
else break;
}
Expression* Exp1 = new ConstExp(tmp);
connect(Exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
operantStack.push(Exp1);
operantIndex.push(++overallIndex);
}
else if (mode == 3) // character
{
string tmpStr = "";
while (validPos < len && ( (str[validPos] >= '0' && str[validPos] <= '9') || (str[validPos] >= 'A' && str[validPos] <= 'Z') || (str[validPos] >= 'a' && str[validPos] <= 'z') || str[validPos] == '_' )) tmpStr.push_back(str[validPos++]);
Expression* Exp1 = new IdenExp(tmpStr);
if (tmpStr == "REM" || tmpStr == "LET" || tmpStr == "PRINT" || tmpStr == "INPUT" || tmpStr == "GOTO" ||
tmpStr == "IF" || tmpStr == "END" || tmpStr == "RUN" || tmpStr == "LOAD" || tmpStr == "LIST" ||
tmpStr == "CLEAR" || tmpStr == "HELP" || tmpStr == "QUIT") throw int(3);
connect(Exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
operantStack.push(Exp1);
operantIndex.push(++overallIndex);
}
else if (mode == 4) // +-
{
validPos += 1;
if (ch == '-' && (case1 || (!operatorStack.empty() && operatorStack.top() == '(')))
{
//处理出现负数的特殊情况
while (validPos != len && str[validPos] == ' ') validPos++;
if (validPos < len && str[validPos] >= '0' && str[validPos] <= '9')
{
int tmp = 0;
while (validPos < len)
{
if (str[validPos] >= '0' && str[validPos] <= '9') tmp = tmp * 10 + (str[validPos++] - '0');
else if ((str[validPos] >= 'A' && str[validPos] <= 'Z') || (str[validPos] >= 'a' && str[validPos] <= 'z') || str[validPos] == '_')
throw std::invalid_argument("");
else break;
}
Expression* Exp1 = new ConstExp(-tmp);
connect(Exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
operantStack.push(Exp1);
operantIndex.push(++overallIndex);
}
else throw std::invalid_argument("您输入了一个非法的负号");
}
else
{
while (!operatorStack.empty() && operatorStack.top() != '(')
{
char tmp = operatorStack.top();
operatorStack.pop();
int now = operatorIndex.top();
operatorIndex.pop();
Expression* Exp1 = toLink(tmp, now);
connect(Exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
operantStack.push(Exp1);
operantIndex.push(++overallIndex);
}
operatorStack.push(ch);
operatorIndex.push(++overallIndex);
}
}
else if (mode == 5) validPos += 1, operatorStack.push('('), operatorIndex.push(++overallIndex); // (
else if (mode == 6) // )
{
bool leftParenAppeared = 0;
validPos += 1;
while (!operatorStack.empty())
{
char tmp = operatorStack.top();
operatorStack.pop();
int now = operatorIndex.top();
operatorIndex.pop();
if (tmp == '(')
{
leftParenAppeared = 1;
break;
}
else
{
Expression* Exp1 = toLink(tmp, now);
connect(Exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
operantStack.push(Exp1);
operantIndex.push(++overallIndex);
}
}
if (!leftParenAppeared) throw int(7);
}
else if (mode == 7) // */
{
validPos += 1;
while (!operatorStack.empty())
{
char tmp = operatorStack.top();
if (!(tmp == '*' || tmp == '/' || tmp == '%' || tmp == '^')) break;
operatorStack.pop();
int now = operatorIndex.top();
operatorIndex.pop();
Expression* Exp1 = toLink(tmp, now);
connect(Exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
operantStack.push(Exp1);
operantIndex.push(++overallIndex);
}
operatorStack.push(ch);
operatorIndex.push(++overallIndex);
}
else if (mode == 8 ) // **
{
validPos += 2;
operatorStack.push(ch);
operatorIndex.push(++overallIndex);
}
//delete all the leading blanks
while (validPos < len && str[validPos] == ' ') validPos++;
}
while (!operatorStack.empty())
{
char tmp = operatorStack.top();
operatorStack.pop();
int now = operatorIndex.top();
operatorIndex.pop();
Expression* Exp1 = toLink(tmp, now);
connect(Exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
operantStack.push(Exp1);
operantIndex.push(++overallIndex);
}
Expression* retExp = operantStack.top();
operantStack.pop();
if (!operantStack.empty()) throw int(7);
return retExp;
}
void MainWindow::statementHandler_exe()
{
try
{
map<int, string>::iterator pcIt = inputStringMap.begin();
pc = pcIt->first;
//it = inputStringMap.end(); it--;
//int pcEnd = it->first + 10;
while (pcIt != inputStringMap.end())
{
map<int, string>::iterator tmpIt = inputStringMap.begin();
while (tmpIt != inputStringMap.end() && tmpIt->first != pc) tmpIt++;
if (tmpIt == inputStringMap.end()) throw int(5);
string tmpStr = tmpIt->second;
int nowPC = tmpIt->first;
pcIt = tmpIt;
pcIt++;
if (pcIt != inputStringMap.end()) pc = pcIt->first;
else pc = 1147483647;
if (parsedStmMap[nowPC] == nullptr)
{
QMessageBox::warning(this, "ERROR", "Runtime Error! The program has stopped.");
break;
}
executeCmdLine(nowPC, tmpStr);
pcIt = inputStringMap.begin();
while (pcIt != inputStringMap.end() && pcIt->first != pc) pcIt++;
}
}
catch (int xx)
{
qDebug() << "catch: " << xx;
QMessageBox::warning(this, "ERROR", "Runtime Error! The program has stopped.");
}
catch (std::invalid_argument)
{
qDebug() << "catch: 非法整数或非法参数";
QMessageBox::warning(this, "ERROR", "Runtime Error! The program has stopped.");
}
}
void MainWindow::statementHandler_printTree()
{
for (map<int, Statement * >::iterator it = parsedStmMap.begin(); it != parsedStmMap.end(); it++)
{
Statement* tmpStm = it->second;
if (tmpStm == nullptr)
{
printTree(Qt::black, std::to_string(it->first) + " Error\n");
}
else if (tmpStm->getType() == IF)
{
printTree(Qt::black, std::to_string(tmpStm->lineNum) + " IF THEN ");
printTree(Qt::red, std::to_string(tmpStm->Lstat) + " " + std::to_string(tmpStm->Rstat));
tmpStm->printTree();
printTree(Qt::black, "\n");
}
else if (tmpStm->getType() == LET)
{
printTree(Qt::black, std::to_string(tmpStm->lineNum) + " LET = ");
printTree(Qt::red, std::to_string(tmpStm->stat) + "\n");
printTree(Qt::black, " " + tmpStm->sons[0]->toString() + " ");
printTree(Qt::red, std::to_string(context.symbolStat[tmpStm->sons[0]->toString()]));
tmpStm->printTree();
printTree(Qt::black, "\n");
}
else if (tmpStm->getType() == REM)
{
printTree(Qt::black, std::to_string(tmpStm->lineNum) + " REM ");
printTree(Qt::red, std::to_string(tmpStm->stat));
tmpStm->printTree();
printTree(Qt::black, "\n");
}
else if (tmpStm->getType() == PRINT)
{
printTree(Qt::black, std::to_string(tmpStm->lineNum) + " PRINT ");
printTree(Qt::red, std::to_string(tmpStm->stat));
tmpStm->printTree();
printTree(Qt::black, "\n");
}
else if (tmpStm->getType() == INPUT)
{
printTree(Qt::black, std::to_string(tmpStm->lineNum) + " INPUT ");
printTree(Qt::red, std::to_string(tmpStm->stat));
tmpStm->printTree();
printTree(Qt::black, "\n");
}
else if (tmpStm->getType() == GOTO)
{
printTree(Qt::black, std::to_string(tmpStm->lineNum) + " GOTO ");
printTree(Qt::red, std::to_string(tmpStm->stat));
tmpStm->printTree();
printTree(Qt::black, "\n");
}
else if (tmpStm->getType() == END)
{
printTree(Qt::black, std::to_string(tmpStm->lineNum) + " END ");
printTree(Qt::red, std::to_string(tmpStm->stat));
tmpStm->printTree();
printTree(Qt::black, "\n");
}
}
}
//在用户输入完一行语句后对语句进行初步处理,在总体上控制parse/execute/输出syntax tree等操作
void MainWindow::statementHandler(string ii)
{
string inputStm = strFormalize(ii);
if (!inputStm.length()) return;
if (inputStm == "RUN")
{
ui->textBrowser->setText("");
ui->treeDisplay->setText("");
if (!inputStringMap.empty())
{
//parsing all
for (map<int, string>::iterator it = inputStringMap.begin(); it != inputStringMap.end(); it++)
{
int lineNumber = it->first;
try
{
stringstream ss;
string cmdLineStr = it->second, tmpStr;
ss << cmdLineStr;
ss >> tmpStr;
ss >> tmpStr;
if (tmpStr == "REM")
{
string STR;
getline(ss, STR, '\n');
STR = strFormalize(STR);
Expression* Exp1 = new IdenExp(STR);
connect(Exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
Statement* tmpStm = new RemStm(&context, lineNumber, Exp1);
connect(tmpStm, &Statement::printTreeLineSignal, this, &MainWindow::printTreeLine);
parsedStmMap[lineNumber] = tmpStm;
}
else if (tmpStr == "LET")
{
string LHSSTR, RHSSTR;
getline(ss, LHSSTR, '=');
getline(ss, RHSSTR, '\n');
LHSSTR = strFormalize(LHSSTR);
RHSSTR = strFormalize(RHSSTR);
if (LHSSTR == "" || RHSSTR == "") throw int(7);
Expression* leftIdenExp = parser(LHSSTR);
if (leftIdenExp->getType() != IDENTIFIER) throw int(3);
connect(leftIdenExp, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
Expression* rightValExp = parser(RHSSTR);
Statement* tmpStm = new LetStm(&context, lineNumber, leftIdenExp, rightValExp);
connect(tmpStm, &Statement::printTreeLineSignal, this, &MainWindow::printTreeLine);
parsedStmMap[lineNumber] = tmpStm;
}
else if (tmpStr == "PRINT")
{
string STR;
getline(ss, STR, '\n');
STR = strFormalize(STR);
Expression* Exp1 = parser(STR);
Statement* tmpStm = new PrintStm(&context, lineNumber, Exp1);
connect(tmpStm, &Statement::printTreeLineSignal, this, &MainWindow::printTreeLine);
connect(tmpStm, &Statement::printAnsLineSignal, this, &MainWindow::printAnsLine);
parsedStmMap[lineNumber] = tmpStm;
}
else if (tmpStr == "INPUT")
{
string STR;
getline(ss, STR, '\n');
STR = strFormalize(STR);
Expression* exp1 = parser(STR);
if (exp1->getType() != IDENTIFIER) throw int(3);
connect(exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
Statement* tmpStm = new InputStm(&context, lineNumber, exp1);
connect(tmpStm, &Statement::printTreeLineSignal, this, &MainWindow::printTreeLine);
parsedStmMap[lineNumber] = tmpStm;
}
else if (tmpStr == "GOTO")
{
string STR;
getline(ss, STR, '\n');
STR = strFormalize(STR);
Expression* Exp1 = new ConstExp(stoi(STR));
connect(Exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
Statement* tmpStm = new GotoStm(&context, lineNumber, Exp1);
connect(tmpStm, &Statement::printTreeLineSignal, this, &MainWindow::printTreeLine);
parsedStmMap[lineNumber] = tmpStm;
}
else if (tmpStr == "IF")
{
char ch;
ss >> std::noskipws >> ch;
string EXP1 = "", EXP2 = "", OP, thenStr = "", EXP3;
while (ss.rdbuf()->in_avail() && ch != '<' && ch != '=' && ch != '>')
{
EXP1.push_back(ch);
ss >> std::noskipws >> ch;
}
if (ch != '<' && ch != '=' && ch != '>') throw int (3);
OP = ch;
ss >> std::skipws >> EXP2;
ss >> thenStr;
if (thenStr != "THEN") throw int (3);
ss >> EXP3;
EXP1 = strFormalize(EXP1);
EXP2 = strFormalize(EXP2);
OP = strFormalize(OP);
EXP3 = strFormalize(EXP3);
if (EXP1 == "" || EXP2 == "" || EXP3 == "") throw int(3);
Expression* leftExp = parser(EXP1), * opExp = new IdenExp(OP),
* rightExp = parser(EXP2), * targetExp = new ConstExp(stoi(EXP3));
connect(opExp, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
connect(targetExp, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
Statement* tmpStm = new IfStm(&context, lineNumber, leftExp, opExp, rightExp, targetExp);
connect(tmpStm, &Statement::printTreeLineSignal, this, &MainWindow::printTreeLine);
parsedStmMap[lineNumber] = tmpStm;
}
else if (tmpStr == "END")
{
char afterEnd = ' ';
while (ss.rdbuf()->in_avail() && afterEnd == ' ')
{
ss >> afterEnd;
}
if (afterEnd != ' ') throw int(3);
Statement* tmpStm = new EndStm(&context, lineNumber);
connect(tmpStm, &Statement::printTreeLineSignal, this, &MainWindow::printTreeLine);
parsedStmMap[lineNumber] = tmpStm;
}
else throw int(6);
}
catch (int xx)
{
qDebug() << "catch: " << xx;
parsedStmMap[lineNumber] = nullptr;
}
catch (std::invalid_argument)
{
qDebug() << "catch: 非法整数或非法参数";
parsedStmMap[lineNumber] = nullptr;
}
}
//executing
statementHandler_exe();
//outputing the syntax tree with stats
statementHandler_printTree();
qDebug() << "RUN ended normally.";
}
}
else if (inputStm == "LOAD")
{
QString filePath = QFileDialog::getOpenFileName(this, tr("选择文件"), "", tr("文本文件 (*.txt);;所有文件 (*.*)"));
if (!filePath.isEmpty())
{
// 读取文件内容
QFile file(filePath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
//qDebug()<<"read 1 line successfully";
qDebug() << line;
ui->cmdLineEdit->setText(line);
on_cmdLineEdit_editingFinished();
}
file.close();
}
}
ui->cmdLineEdit->setText("RUN");
on_cmdLineEdit_editingFinished();
}
else if (inputStm == "LIST")
{
//don't need to do anything
}
else if (inputStm == "CLEAR")
{
context.symbolMap.clear();
inputStringMap.clear();
parsedStmMap.clear();
ui->CodeDisplay->setText("");
ui->textBrowser->setText("");
ui->treeDisplay->setText("");
}
else if (inputStm == "HELP")
{
QMessageBox::information(this, "Help Message", "The minimal BASIC interpreter implements only six statement forms, which appear in Table 1. The LET, PRINT, and INPUT statements can be executed directly by typing them without a line number, in which case they are evaluated immediately. In addition to the statements listed in Table 1, BASIC accepts the commands shown in Table 2. These commands cannot be part of a program and must therefore be entered without a line number.");
}
else if (inputStm == "QUIT")
{
QCoreApplication::quit();
}
else
{
//inputStrings.push_back(inputStm);
stringstream ss;
ss << inputStm;
string tmpStr;
getline(ss, tmpStr, ' ');
char tmpChar = tmpStr[0];
if (tmpChar >= '0' && tmpChar <= '9')
{
int tmpInt = stoi(tmpStr);
if (tmpInt <= 0 || tmpInt > 1000000)
{
QMessageBox::information(this, "行号错误", "输入的行号需要为不超过1000000的正整数,请重新输入!");
return;
}
string testStr;
ss >> testStr;
if (testStr == "") inputStringMap.erase(tmpInt);
else inputStringMap[tmpInt] = inputStm;
codeFlush();
}
else
{
//处理无行号的第I类语句
if (tmpStr == "LET")
{
string LHSSTR, RHSSTR;
getline(ss, LHSSTR, '=');
getline(ss, RHSSTR, '\n');
LHSSTR = strFormalize(LHSSTR);
RHSSTR = strFormalize(RHSSTR);
Expression* leftIdenExp = new IdenExp(LHSSTR);
//connect(leftIdenExp, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
Expression* rightValExp = parser(RHSSTR);
Statement* tmpStm = new LetStm(&context, 0, leftIdenExp, rightValExp);
//connect(tmpStm, &Statement::printTreeLineSignal, this, &MainWindow::printTreeLine);
executeCmdLine(tmpStr, tmpStm);
}
else if (tmpStr == "PRINT")
{
string STR;
getline(ss, STR, '\n');
STR = strFormalize(STR);
Expression* Exp1 = parser(STR);
//connect(Exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
Statement* tmpStm = new PrintStm(&context, 0, Exp1);
//connect(tmpStm, &Statement::printTreeLineSignal, this, &MainWindow::printTreeLine);
connect(tmpStm, &Statement::printAnsLineSignal, this, &MainWindow::printAnsLine);
executeCmdLine(tmpStr, tmpStm);
}
else if (tmpStr == "INPUT")
{
string STR;
getline(ss, STR, '\n');
STR = strFormalize(STR);
Expression* exp1 = new IdenExp(STR);
//connect(exp1, &Expression::printTreeLineSignal, this, &MainWindow::printTreeLine);
Statement* tmpStm = new InputStm(&context, 0, exp1);
//connect(tmpStm, &Statement::printTreeLineSignal, this, &MainWindow::printTreeLine);
executeCmdLine(tmpStr, tmpStm);
}
else
{
QMessageBox::warning(this, "ERROR", "Runtime Error! The program has stopped.");
}
}
}
}
void MainWindow::on_cmdLineEdit_editingFinished()
{
try
{
QString cmd = ui->cmdLineEdit->text();
ui->cmdLineEdit->setText("");
string inputStr = cmd.toStdString();
inputStr = strFormalize(inputStr);
if (!isInputting)
{
//ui->CodeDisplay->append(cmd);
statementHandler(inputStr);
}
else
{
isInputting = 0;
string numStr(inputStr, 2);
//qDebug()<<QString::fromStdString(numStr);
int tmp = stoi(numStr);
context.setValue(inputName, tmp);
emit inputFinished();
}
}
catch (int xx)
{
qDebug() << "catch: " << xx;
}
catch (std::invalid_argument)
{
qDebug() << "catch: 非法整数或非法参数";
}
}
//用于在syntax tree框中打印内容
void MainWindow::printTree(QColor color, string str)
{
QTextCharFormat charFormat;
charFormat.setForeground(color);
QTextCursor cursor(ui->treeDisplay->textCursor());
cursor.movePosition(QTextCursor::End);
cursor.setCharFormat(charFormat);
cursor.insertText(QString::fromStdString(str));
charFormat.setForeground(Qt::black);
cursor.setCharFormat(charFormat);
}
//用于在syntax tree框中打印一整行内容
void MainWindow::printTreeLine(int bias, string str)
{
string tmpStr = "";
for (int i = 1; i <= bias; i++) tmpStr.push_back(' ');
tmpStr = tmpStr + str;
printTree(Qt::black, "\n" + tmpStr);
//ui->treeDisplay->append(QString::fromStdString(tmpStr));
}
//用于在程序运行结果框中打印一整行内容
void MainWindow::printAnsLine(int ans)
{
ui->textBrowser->append(QString::number(ans));
}